diff --git a/htdocs/core/class/commondocgenerator.class.php b/htdocs/core/class/commondocgenerator.class.php index dc4cbeff1ac..17f25b2d183 100644 --- a/htdocs/core/class/commondocgenerator.class.php +++ b/htdocs/core/class/commondocgenerator.class.php @@ -1074,7 +1074,7 @@ abstract class CommonDocGenerator * @param float $curY curent Y position * @param string $colKey the column key * @param string $columnText column text - * @return int new rank on success and -1 on error + * @return null */ public function printStdColumnContent($pdf, &$curY, $colKey, $columnText = '') { @@ -1096,6 +1096,111 @@ abstract class CommonDocGenerator } } + /** + * get extrafield content for pdf writeHtmlCell compatibility + * usage for PDF line columns and object note block + * + * @param object $object common object + * @param string $extrafieldKey the extrafield key + * @return null + */ + public function getExtrafieldContent($object, $extrafieldKey) + { + global $hookmanager; + + if(empty($object->table_element)){ return; } + + $extrafieldsKeyPrefix = "options_"; + + // Cleanup extrafield key to remove prefix if present + $pos = strpos($extrafieldKey, $extrafieldsKeyPrefix); + if($pos===0){ + $extrafieldKey = substr($extrafieldKey, strlen ( $extrafieldsKeyPrefix)); + } + + $extrafieldOptionsKey = $extrafieldsKeyPrefix.$extrafieldKey; + + + // Load extrafiels if not allready does + if(!isset($this->extrafieldsCache)){ $this->extrafieldsCache = array(); } + if(!isset($this->extrafieldsCache[$object->table_element])){ + $extrafields = new ExtraFields($this->db); + $extrafields->fetch_name_optionals_label($object->table_element); + } + else{ + $extrafields = $this->extrafieldsCache[$object->table_element]; + } + + $extrafieldOutputContent = $extrafields->showOutputField($extrafieldKey, $object->array_options[$extrafieldOptionsKey], '', $object->table_element); + + // TODO : allow showOutputField to be pdf public friendly, ex: in a link to object, clean getNomUrl to remove link and images... like a getName methode ... + if($extrafields->attributes[$object->table_element]['type'][$extrafieldKey] == 'link'){ + // for lack of anything better we cleanup all html tags + $extrafieldOutputContent = dol_string_nohtmltag($extrafieldOutputContent); + } + + $parameters = array( + 'object' => $object, + 'extrafields' => $extrafields, + 'extrafieldKey' => $extrafieldKey, + 'extrafieldOutputContent' =>& $extrafieldOutputContent + ); + $reshook = $hookmanager->executeHooks('getPDFExtrafieldContent', $parameters, $this); // Note that $action and $object may have been modified by hook + if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors'); + if ($reshook) + { + $extrafieldOutputContent = $hookmanager->resPrint; + } + + return $extrafieldOutputContent; + } + + + /** + * print extrafields columns content + * + * @param Tcpdf $pdf pdf object + * @param float $curY curent Y position + * @param object $object line of common object + * @return double max y value + */ + public function printLineExtrafieldsColumnsContent($pdf, &$curY, $object) + { + global $hookmanager; + + if(empty($object->table_element) || !isset($this->extrafieldsCache[$object->table_element])){ + return; + } + + $nextY = $curY; + + // get extrafield config from cache + $extrafields =& $this->extrafieldsCache[$object->table_element]; + + /** + * @var $extrafields ExtraFields + */ + + if (is_array($extrafields->attributes[$object->table_element]['label'])) { + foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $label) + { + $extrafieldColKey = "options_".$key; + + if ($this->getColumnStatus($extrafieldColKey) && isset($object->array_options[$extrafieldColKey])) + { + // TODO : allow showOutputField to be pdf public friendly, ex: in a link to object, clean getNomUrl to remove link and images... + $columnText = $extrafields->showOutputField($key, $object->array_options[$extrafieldColKey], '', $object->table_element); + + + $this->printStdColumnContent($pdf, $curY, $extrafieldColKey, $columnText); + $nextY = max($pdf->GetY(), $nextY); + } + } + } + + return $nextY; + } + /** * get column status from column key @@ -1167,4 +1272,91 @@ abstract class CommonDocGenerator } return $this->tabTitleHeight; } + + + + /** + * Define Array Column Field for extrafields + * + * @param object $object common object det + * @param Translate $outputlangs langs + * @param int $hidedetails Do not show line details + * @return null + */ + public function defineColumnExtrafield($object, $outputlangs, $hidedetails = 0) + { + global $conf; + + if(!empty($hidedetails)){ + return; + } + + if(empty($object->table_element)){ + return; + } + + $extrafields = new ExtraFields($this->db); + $extralabels = $extrafields->fetch_name_optionals_label($object->table_element); + + + if (is_array($extrafields->attributes[$object->table_element]['label'])) { + + // For cache on lines loop + if(!isset($this->extrafieldsCache)){ $this->extrafieldsCache = array(); } + $this->extrafieldsCache[$object->table_element] = $extrafields; + + foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $label) + { + // Dont display separator yet even is set to be displayed (not compatible yet) + if ($extrafields->attributes[$object->table_element]['type'][$key] == 'separate') + { + continue; + } + + // Enable extrafield ? + $enabled = 1; + if ($enabled && isset($extrafields->attributes[$object->table_element]['enabled'][$key])) + { + $enabled = dol_eval($extrafields->attributes[$object->table_element]['enabled'][$key], 1); + } + if ($enabled && isset($extrafields->attributes[$object->table_element]['list'][$key])) + { + $enabled = dol_eval($extrafields->attributes[$object->table_element]['list'][$key], 1); + } + + // Load language if required + if (!empty($extrafields->attributes[$object->table_element]['langfile'][$key])) $outputlangs->load($extrafields->attributes[$object->table_element]['langfile'][$key]); + + // TODO : add more extrafield customisation capacities for PDF like width, rank... + + // set column definition + $def = array( + 'rank' => intval($extrafields->attributes[$object->table_element]['pos'][$key]), + 'width' => 25, // in mm + 'status' => boolval($enabled), + 'title' => array( + 'label' => $outputlangs->transnoentities($label) + ), + 'content' => array( + 'align' => 'C' + ), + 'border-left' => true, // add left line separator + ); + + $alignTypeRight = array('double', 'int', 'price'); + if(in_array($extrafields->attributes[$object->table_element]['type'][$key], $alignTypeRight)){ + $def['content']['align'] = 'R'; + } + + $alignTypeLeft = array('text', 'html'); + if(in_array($extrafields->attributes[$object->table_element]['type'][$key], $alignTypeLeft)){ + $def['content']['align'] = 'L'; + } + + + // for extrafields we use rank of extrafield to place it on PDF + $this->insertNewColumnDef("options_".$key, $def); + } + } + } } diff --git a/htdocs/core/modules/facture/doc/pdf_sponge.modules.php b/htdocs/core/modules/facture/doc/pdf_sponge.modules.php index a2d103f5531..43b1fa4dbac 100644 --- a/htdocs/core/modules/facture/doc/pdf_sponge.modules.php +++ b/htdocs/core/modules/facture/doc/pdf_sponge.modules.php @@ -736,6 +736,18 @@ class pdf_sponge extends ModelePDFFactures $nexY = max($pdf->GetY(), $nexY); } + // Extrafields + if(!empty($object->lines[$i]->array_options)){ + foreach ($object->lines[$i]->array_options as $extrafieldColKey => $extrafieldValue){ + if ($this->getColumnStatus($extrafieldColKey)) + { + $extrafieldValue = $this->getExtrafieldContent($object->lines[$i], $extrafieldColKey); + $this->printStdColumnContent($pdf, $curY, $extrafieldColKey, $extrafieldValue); + $nexY = max($pdf->GetY(), $nexY); + } + } + } + $parameters = array( 'object' => $object, @@ -2346,7 +2358,7 @@ class pdf_sponge extends ModelePDFFactures $this->cols['discount']['status'] = true; } - $rank = $rank + 10; + $rank = $rank + 1000; // add a big offset to be sure is the last col because default extrafield rank is 100 $this->cols['totalexcltax'] = array( 'rank' => $rank, 'width' => 26, // in mm @@ -2357,6 +2369,11 @@ class pdf_sponge extends ModelePDFFactures 'border-left' => true, // add left line separator ); + // Add extrafields cols + if(!empty($object->lines)) { + $line = reset($object->lines); + $this->defineColumnExtrafield($line, $outputlangs, $hidedetails); + } $parameters = array( 'object' => $object,