From aa77d96f239ad4ec928ff6fbb369172eaee14792 Mon Sep 17 00:00:00 2001 From: ATM john Date: Sat, 22 Feb 2020 12:06:21 +0100 Subject: [PATCH 01/13] remove IDE warnings --- .../modules/facture/doc/pdf_sponge.modules.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/htdocs/core/modules/facture/doc/pdf_sponge.modules.php b/htdocs/core/modules/facture/doc/pdf_sponge.modules.php index 4fb6a55362e..a2d103f5531 100644 --- a/htdocs/core/modules/facture/doc/pdf_sponge.modules.php +++ b/htdocs/core/modules/facture/doc/pdf_sponge.modules.php @@ -126,10 +126,11 @@ class pdf_sponge extends ModelePDFFactures */ public $situationinvoice; + /** - * @var float X position for the situation progress column + * @var array of document table collumns */ - public $posxprogress; + public $cols; /** @@ -407,6 +408,8 @@ class pdf_sponge extends ModelePDFFactures $tab_height_newpage = 150; if (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)) $tab_height_newpage -= $top_shift; + $nexY = $tab_top - 1; + // Incoterm $height_incoterms = 0; if ($conf->incoterm->enabled) @@ -632,8 +635,6 @@ class pdf_sponge extends ModelePDFFactures if ($pageposafter > $pageposbefore) // There is a pagebreak { $pdf->rollbackTransaction(true); - $pageposafter = $pageposbefore; - //print $pageposafter.'-'.$pageposbefore;exit; $pdf->setPageOrientation('', 1, $heightforfooter); // The only function to edit the bottom margin of current page to set it. pdf_writelinedesc($pdf, $object, $i, $outputlangs, $this->getColumnContentWidth('desc'), 3, $this->getColumnContentXStart('desc'), $curY, $hideref, $hidedesc); $pageposafter = $pdf->getPage(); @@ -1052,7 +1053,7 @@ class pdf_sponge extends ModelePDFFactures /** * Show miscellaneous information (payment mode, payment term, ...) * - * @param PDF $pdf Object PDF + * @param tcpdf $pdf Object PDF * @param Object $object Object to show * @param int $posy Y * @param Translate $outputlangs Langs object @@ -1223,7 +1224,7 @@ class pdf_sponge extends ModelePDFFactures /** * Show total to pay * - * @param PDF $pdf Object PDF + * @param TCPDI $pdf Object PDF * @param Facture $object Object invoice * @param int $deja_regle Amount already paid (in the currency of invoice) * @param int $posy Position depart @@ -1789,7 +1790,7 @@ class pdf_sponge extends ModelePDFFactures /** * Show table for lines * - * @param PDF $pdf Object PDF + * @param tcpdf $pdf Object PDF * @param string $tab_top Top position of table * @param string $tab_height Height of table (rectangle) * @param int $nexY Y (not used) @@ -1849,7 +1850,7 @@ class pdf_sponge extends ModelePDFFactures /** * Show top header of page. * - * @param PDF $pdf Object PDF + * @param Tcpdf $pdf Object PDF * @param Object $object Object to show * @param int $showaddress 0=no, 1=yes * @param Translate $outputlangs Object lang for output From e209fd297320f422967dbbec5435a5d8fc901acf Mon Sep 17 00:00:00 2001 From: ATM john Date: Sat, 22 Feb 2020 16:27:44 +0100 Subject: [PATCH 02/13] Add extrafields on PDF object lines --- .../core/class/commondocgenerator.class.php | 194 +++++++++++++++++- .../facture/doc/pdf_sponge.modules.php | 19 +- 2 files changed, 211 insertions(+), 2 deletions(-) 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, From 2b027cfebf1193888ff569a877aa3e206ff1068f Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Sat, 22 Feb 2020 15:45:39 +0000 Subject: [PATCH 03/13] Fixing style errors. --- htdocs/core/class/commondocgenerator.class.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/htdocs/core/class/commondocgenerator.class.php b/htdocs/core/class/commondocgenerator.class.php index 17f25b2d183..0b77ee2a61a 100644 --- a/htdocs/core/class/commondocgenerator.class.php +++ b/htdocs/core/class/commondocgenerator.class.php @@ -1115,7 +1115,7 @@ abstract class CommonDocGenerator // Cleanup extrafield key to remove prefix if present $pos = strpos($extrafieldKey, $extrafieldsKeyPrefix); if($pos===0){ - $extrafieldKey = substr($extrafieldKey, strlen ( $extrafieldsKeyPrefix)); + $extrafieldKey = substr($extrafieldKey, strlen($extrafieldsKeyPrefix)); } $extrafieldOptionsKey = $extrafieldsKeyPrefix.$extrafieldKey; @@ -1300,7 +1300,6 @@ abstract class CommonDocGenerator 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; From 60b3d9d4db2954864b02e0919aa8ce79d7037706 Mon Sep 17 00:00:00 2001 From: ATM john Date: Sat, 22 Feb 2020 18:07:15 +0100 Subject: [PATCH 04/13] Add extrafields on PDF object lines for compatible pdf --- .../commande/doc/pdf_eratosthene.modules.php | 18 +- .../expedition/doc/pdf_espadon.modules.php | 17 +- .../modules/propale/doc/pdf_cyan.modules.php | 18 +- .../supplier_order/doc/pdf_cornas.modules.php | 244 ++---------------- 4 files changed, 67 insertions(+), 230 deletions(-) diff --git a/htdocs/core/modules/commande/doc/pdf_eratosthene.modules.php b/htdocs/core/modules/commande/doc/pdf_eratosthene.modules.php index 5bdc591c2fb..a797d97e59c 100644 --- a/htdocs/core/modules/commande/doc/pdf_eratosthene.modules.php +++ b/htdocs/core/modules/commande/doc/pdf_eratosthene.modules.php @@ -702,6 +702,17 @@ class pdf_eratosthene extends ModelePDFCommandes $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, @@ -1804,7 +1815,7 @@ class pdf_eratosthene extends ModelePDFCommandes $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 @@ -1815,6 +1826,11 @@ class pdf_eratosthene extends ModelePDFCommandes '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, diff --git a/htdocs/core/modules/expedition/doc/pdf_espadon.modules.php b/htdocs/core/modules/expedition/doc/pdf_espadon.modules.php index b47149f26a6..49a3307188a 100644 --- a/htdocs/core/modules/expedition/doc/pdf_espadon.modules.php +++ b/htdocs/core/modules/expedition/doc/pdf_espadon.modules.php @@ -558,7 +558,17 @@ class pdf_espadon extends ModelePdfExpedition $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); + } + } + } $nexY += 3; if ($weighttxt && $voltxt) $nexY += 2; @@ -1225,6 +1235,11 @@ class pdf_espadon extends ModelePdfExpedition ), ); + // Add extrafields cols + if(!empty($object->lines)) { + $line = reset($object->lines); + $this->defineColumnExtrafield($line, $outputlangs, $hidedetails); + } $parameters = array( 'object' => $object, diff --git a/htdocs/core/modules/propale/doc/pdf_cyan.modules.php b/htdocs/core/modules/propale/doc/pdf_cyan.modules.php index cbbf462b446..c7b1b1d715d 100644 --- a/htdocs/core/modules/propale/doc/pdf_cyan.modules.php +++ b/htdocs/core/modules/propale/doc/pdf_cyan.modules.php @@ -717,6 +717,17 @@ class pdf_cyan extends ModelePDFPropales $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, @@ -1952,7 +1963,7 @@ class pdf_cyan extends ModelePDFPropales $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 @@ -1963,6 +1974,11 @@ class pdf_cyan extends ModelePDFPropales '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, diff --git a/htdocs/core/modules/supplier_order/doc/pdf_cornas.modules.php b/htdocs/core/modules/supplier_order/doc/pdf_cornas.modules.php index 1c266ae89b6..b7a5f49f06d 100644 --- a/htdocs/core/modules/supplier_order/doc/pdf_cornas.modules.php +++ b/htdocs/core/modules/supplier_order/doc/pdf_cornas.modules.php @@ -649,6 +649,17 @@ class pdf_cornas extends ModelePDFSuppliersOrders $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, @@ -1596,7 +1607,7 @@ class pdf_cornas extends ModelePDFSuppliersOrders $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 @@ -1607,6 +1618,11 @@ class pdf_cornas extends ModelePDFSuppliersOrders '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, @@ -1630,230 +1646,4 @@ class pdf_cornas extends ModelePDFSuppliersOrders $this->cols = $hookmanager->resArray; } } - - /* - * - * DEBUT PARTIE NORMALEMENT DANS LA CLASSE CommonDocGenerator - * - * - */ - - /** - * uasort callback function to Sort columns fields - * - * @param array $a PDF lines array fields configs - * @param array $b PDF lines array fields configs - * @return int Return compare result - */ - public function columnSort($a, $b) - { - if (empty($a['rank'])) { $a['rank'] = 0; } - if (empty($b['rank'])) { $b['rank'] = 0; } - if ($a['rank'] == $b['rank']) { - return 0; - } - return ($a['rank'] > $b['rank']) ? -1 : 1; - } - - /** - * Prepare Array Column Field - * - * @param object $object common object - * @param Translate $outputlangs langs - * @param int $hidedetails Do not show line details - * @param int $hidedesc Do not show desc - * @param int $hideref Do not show ref - * @return null - */ - public function prepareArrayColumnField($object, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0) - { - global $conf; - - $this->defineColumnField($object, $outputlangs, $hidedetails, $hidedesc, $hideref); - - // Sorting - uasort($this->cols, array($this, 'columnSort')); - - // Positionning - $curX = $this->page_largeur - $this->marge_droite; // start from right - - // Array width - $arrayWidth = $this->page_largeur - $this->marge_droite - $this->marge_gauche; - - // Count flexible column - $totalDefinedColWidth = 0; - $countFlexCol = 0; - foreach ($this->cols as $colKey => &$colDef) - { - if (!$this->getColumnStatus($colKey)) continue; // continue if disabled - - if (!empty($colDef['scale'])) { - // In case of column widht is defined by percentage - $colDef['width'] = abs($arrayWidth * $colDef['scale'] / 100); - } - - if (empty($colDef['width'])) { - $countFlexCol++; - } - else { - $totalDefinedColWidth += $colDef['width']; - } - } - - foreach ($this->cols as $colKey => &$colDef) - { - // setting empty conf with default - if (!empty($colDef['title'])) { - $colDef['title'] = array_replace($this->defaultTitlesFieldsStyle, $colDef['title']); - } - else { - $colDef['title'] = $this->defaultTitlesFieldsStyle; - } - - // setting empty conf with default - if (!empty($colDef['content'])) { - $colDef['content'] = array_replace($this->defaultContentsFieldsStyle, $colDef['content']); - } - else { - $colDef['content'] = $this->defaultContentsFieldsStyle; - } - - if ($this->getColumnStatus($colKey)) - { - // In case of flexible column - if (empty($colDef['width'])) { - $colDef['width'] = abs(($arrayWidth - $totalDefinedColWidth)) / $countFlexCol; - } - - // Set positions - $lastX = $curX; - $curX = $lastX - $colDef['width']; - $colDef['xStartPos'] = $curX; - $colDef['xEndPos'] = $lastX; - } - } - } - - /** - * get column content width from column key - * - * @param string $colKey the column key - * @return float width in mm - */ - public function getColumnContentWidth($colKey) - { - $colDef = $this->cols[$colKey]; - return $colDef['width'] - $colDef['content']['padding'][3] - $colDef['content']['padding'][1]; - } - - - /** - * get column content X (abscissa) left position from column key - * - * @param string $colKey the column key - * @return float X position in mm - */ - public function getColumnContentXStart($colKey) - { - $colDef = $this->cols[$colKey]; - return $colDef['xStartPos'] + $colDef['content']['padding'][3]; - } - - /** - * get column position rank from column key - * - * @param string $colKey the column key - * @return int rank on success and -1 on error - */ - public function getColumnRank($colKey) - { - if (!isset($this->cols[$colKey]['rank'])) return -1; - return $this->cols[$colKey]['rank']; - } - - /** - * get column position rank from column key - * - * @param string $newColKey the new column key - * @param array $defArray a single column definition array - * @param string $targetCol target column used to place the new column beside - * @param bool $insertAfterTarget insert before or after target column ? - * @return int new rank on success and -1 on error - */ - public function insertNewColumnDef($newColKey, $defArray, $targetCol = false, $insertAfterTarget = false) - { - // prepare wanted rank - $rank = -1; - - // try to get rank from target column - if (!empty($targetCol)) { - $rank = $this->getColumnRank($targetCol); - if ($rank >= 0 && $insertAfterTarget) { $rank++; } - } - - // get rank from new column definition - if ($rank < 0 && !empty($defArray['rank'])) { - $rank = $defArray['rank']; - } - - // error: no rank - if ($rank < 0) { return -1; } - - foreach ($this->cols as $colKey =>& $colDef) - { - if ($rank <= $colDef['rank']) - { - $colDef['rank'] = $colDef['rank'] + 1; - } - } - - $defArray['rank'] = $rank; - $this->cols[$newColKey] = $defArray; // array_replace is used to preserve keys - - return $rank; - } - - - /** - * print standard column content - * - * @param PDF $pdf pdf object - * @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 - */ - public function printStdColumnContent($pdf, &$curY, $colKey, $columnText = '') - { - global $hookmanager; - - $parameters = array( - 'curY' =>& $curY, - 'columnText' => $columnText, - 'colKey' => $colKey - ); - $reshook = $hookmanager->executeHooks('printStdColumnContent', $parameters, $this); // Note that $action and $object may have been modified by hook - if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors'); - if (!$reshook) - { - if (empty($columnText)) return; - $pdf->SetXY($this->getColumnContentXStart($colKey), $curY); // Set curent position - $colDef = $this->cols[$colKey]; - $pdf->MultiCell($this->getColumnContentWidth($colKey), 2, $columnText, '', $colDef['content']['align']); - } - } - - /** - * get column status from column key - * - * @param string $colKey the column key - * @return float width in mm - */ - public function getColumnStatus($colKey) - { - if (!empty($this->cols[$colKey]['status'])) { - return true; - } - else return false; - } } From 8c36dd0ad8f6108a7e7fc7daf658a6616cfadb3c Mon Sep 17 00:00:00 2001 From: ATM john Date: Sun, 23 Feb 2020 02:26:21 +0100 Subject: [PATCH 05/13] add extrafields in notes --- .../core/class/commondocgenerator.class.php | 81 +++++++++++++------ .../commande/doc/pdf_eratosthene.modules.php | 7 ++ .../expedition/doc/pdf_espadon.modules.php | 16 +++- .../facture/doc/pdf_sponge.modules.php | 7 ++ .../modules/propale/doc/pdf_cyan.modules.php | 8 ++ .../supplier_order/doc/pdf_cornas.modules.php | 7 ++ 6 files changed, 99 insertions(+), 27 deletions(-) diff --git a/htdocs/core/class/commondocgenerator.class.php b/htdocs/core/class/commondocgenerator.class.php index 0b77ee2a61a..2517e5f4709 100644 --- a/htdocs/core/class/commondocgenerator.class.php +++ b/htdocs/core/class/commondocgenerator.class.php @@ -1102,7 +1102,7 @@ abstract class CommonDocGenerator * * @param object $object common object * @param string $extrafieldKey the extrafield key - * @return null + * @return string */ public function getExtrafieldContent($object, $extrafieldKey) { @@ -1157,48 +1157,80 @@ abstract class CommonDocGenerator /** - * print extrafields columns content + * display extrafields columns content * - * @param Tcpdf $pdf pdf object - * @param float $curY curent Y position * @param object $object line of common object + * @param Translate $outputlangs Output language * @return double max y value */ - public function printLineExtrafieldsColumnsContent($pdf, &$curY, $object) + public function getExtrafieldsInHtml($object, $outputlangs) { global $hookmanager; - if(empty($object->table_element) || !isset($this->extrafieldsCache[$object->table_element])){ + if(empty($object->table_element)){ return; } - $nextY = $curY; + // 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]; + } - // get extrafield config from cache - $extrafields =& $this->extrafieldsCache[$object->table_element]; /** * @var $extrafields ExtraFields */ + $html = ''; + $fields = array(); + 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); + // 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); + } + + // TODO : add extrafields enabled for display from extrafield config panel + + if(empty($enabled)){ + continue; + } + + $field = new stdClass(); + $field->rank = intval($extrafields->attributes[$object->table_element]['pos'][$key]); + $field->content = $this->getExtrafieldContent($object, $key); + $field->label = $outputlangs->transnoentities($label); + + $fields[] = $field; } } - return $nextY; + if(!empty($fields)) + { + uasort($fields, function($a, $b){ + return ($a->rank > $b->rank) ? -1 : 1; + } + ); + + foreach ($fields as $field){ + $html.= !empty($html)?'
':''; + $html.= $field->label.' : '.$field->content; + } + } + + return $html; } @@ -1314,15 +1346,16 @@ abstract class CommonDocGenerator // Enable extrafield ? $enabled = 1; - if ($enabled && isset($extrafields->attributes[$object->table_element]['enabled'][$key])) - { + 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])) - { + + if ($enabled && isset($extrafields->attributes[$object->table_element]['list'][$key])){ $enabled = dol_eval($extrafields->attributes[$object->table_element]['list'][$key], 1); } + // TODO : add extrafields $enabled from extrafield config panel + // Load language if required if (!empty($extrafields->attributes[$object->table_element]['langfile'][$key])) $outputlangs->load($extrafields->attributes[$object->table_element]['langfile'][$key]); diff --git a/htdocs/core/modules/commande/doc/pdf_eratosthene.modules.php b/htdocs/core/modules/commande/doc/pdf_eratosthene.modules.php index a797d97e59c..fc1d737a7d5 100644 --- a/htdocs/core/modules/commande/doc/pdf_eratosthene.modules.php +++ b/htdocs/core/modules/commande/doc/pdf_eratosthene.modules.php @@ -410,6 +410,13 @@ class pdf_eratosthene extends ModelePDFCommandes } } + // Extrafields in note + $extranote = $this->getExtrafieldsInHtml($object, $outputlangs); + if (!empty($extranote)) + { + $notetoshow = dol_concatdesc($notetoshow, $extranote); + } + $pagenb = $pdf->getPage(); if ($notetoshow) { diff --git a/htdocs/core/modules/expedition/doc/pdf_espadon.modules.php b/htdocs/core/modules/expedition/doc/pdf_espadon.modules.php index 49a3307188a..5d211c2cbae 100644 --- a/htdocs/core/modules/expedition/doc/pdf_espadon.modules.php +++ b/htdocs/core/modules/expedition/doc/pdf_espadon.modules.php @@ -336,7 +336,17 @@ class pdf_espadon extends ModelePdfExpedition } } - if (!empty($object->note_public) || !empty($object->tracking_number)) + // display note + $notetoshow = empty($object->note_public) ? '' : $object->note_public; + + // Extrafields in note + $extranote = $this->getExtrafieldsInHtml($object, $outputlangs); + if (!empty($extranote)) + { + $notetoshow = dol_concatdesc($notetoshow, $extranote); + } + + if (!empty($notetoshow) || !empty($object->tracking_number)) { $tab_top = 88 + $height_incoterms; $tab_top_alt = $tab_top; @@ -375,10 +385,10 @@ class pdf_espadon extends ModelePdfExpedition } // Notes - if (!empty($object->note_public)) + if (!empty($notetoshow)) { $pdf->SetFont('', '', $default_font_size - 1); // In loop to manage multi-page - $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top_alt, dol_htmlentitiesbr($object->note_public), 0, 1); + $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top_alt, dol_htmlentitiesbr($notetoshow), 0, 1); } $nexY = $pdf->GetY(); diff --git a/htdocs/core/modules/facture/doc/pdf_sponge.modules.php b/htdocs/core/modules/facture/doc/pdf_sponge.modules.php index 43b1fa4dbac..cd9327e25b9 100644 --- a/htdocs/core/modules/facture/doc/pdf_sponge.modules.php +++ b/htdocs/core/modules/facture/doc/pdf_sponge.modules.php @@ -447,6 +447,13 @@ class pdf_sponge extends ModelePDFFactures } } + // Extrafields in note + $extranote = $this->getExtrafieldsInHtml($object, $outputlangs); + if (!empty($extranote)) + { + $notetoshow = dol_concatdesc($notetoshow, $extranote); + } + $pagenb = $pdf->getPage(); if ($notetoshow) { diff --git a/htdocs/core/modules/propale/doc/pdf_cyan.modules.php b/htdocs/core/modules/propale/doc/pdf_cyan.modules.php index c7b1b1d715d..702202be988 100644 --- a/htdocs/core/modules/propale/doc/pdf_cyan.modules.php +++ b/htdocs/core/modules/propale/doc/pdf_cyan.modules.php @@ -418,6 +418,14 @@ class pdf_cyan extends ModelePDFPropales if (!empty($salerepobj->signature)) $notetoshow = dol_concatdesc($notetoshow, $salerepobj->signature); } } + + // Extrafields in note + $extranote = $this->getExtrafieldsInHtml($object, $outputlangs); + if (!empty($extranote)) + { + $notetoshow = dol_concatdesc($notetoshow, $extranote); + } + if (!empty($conf->global->MAIN_ADD_CREATOR_IN_NOTE) && $object->user_author_id > 0) { $tmpuser = new User($this->db); diff --git a/htdocs/core/modules/supplier_order/doc/pdf_cornas.modules.php b/htdocs/core/modules/supplier_order/doc/pdf_cornas.modules.php index b7a5f49f06d..92bc5219688 100644 --- a/htdocs/core/modules/supplier_order/doc/pdf_cornas.modules.php +++ b/htdocs/core/modules/supplier_order/doc/pdf_cornas.modules.php @@ -369,6 +369,13 @@ class pdf_cornas extends ModelePDFSuppliersOrders // Affiche notes $notetoshow = empty($object->note_public) ? '' : $object->note_public; + // Extrafields in note + $extranote = $this->getExtrafieldsInHtml($object, $outputlangs); + if (!empty($extranote)) + { + $notetoshow = dol_concatdesc($notetoshow, $extranote); + } + $pagenb = $pdf->getPage(); if ($notetoshow) { From 281e059487d3633ac1d553751d267a8a50d3e477 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Sun, 23 Feb 2020 01:28:30 +0000 Subject: [PATCH 06/13] Fixing style errors. --- htdocs/core/class/commondocgenerator.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/core/class/commondocgenerator.class.php b/htdocs/core/class/commondocgenerator.class.php index 2517e5f4709..9f3502c1836 100644 --- a/htdocs/core/class/commondocgenerator.class.php +++ b/htdocs/core/class/commondocgenerator.class.php @@ -1219,9 +1219,9 @@ abstract class CommonDocGenerator if(!empty($fields)) { - uasort($fields, function($a, $b){ + uasort($fields, function ($a, $b) { return ($a->rank > $b->rank) ? -1 : 1; - } + } ); foreach ($fields as $field){ From ff6dc735eeb25371966a430743cef547dfb70d30 Mon Sep 17 00:00:00 2001 From: ATM john Date: Sun, 23 Feb 2020 10:09:47 +0100 Subject: [PATCH 07/13] fix extrafield conf cache --- .../core/class/commondocgenerator.class.php | 37 ++++++++----------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/htdocs/core/class/commondocgenerator.class.php b/htdocs/core/class/commondocgenerator.class.php index 9f3502c1836..cfab7d7f5d6 100644 --- a/htdocs/core/class/commondocgenerator.class.php +++ b/htdocs/core/class/commondocgenerator.class.php @@ -49,6 +49,10 @@ abstract class CommonDocGenerator */ protected $db; + /** + * @var Extrafields object + */ + public $extrafieldsCache; /** * Constructor @@ -1122,14 +1126,9 @@ abstract class CommonDocGenerator // 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]; - } + if(empty($this->extrafieldsCache)){ $this->extrafieldsCache = new ExtraFields($this->db); } + if(empty($this->extrafieldsCache->attributes[$object->table_element])){ $this->extrafieldsCache->fetch_name_optionals_label($object->table_element); } + $extrafields = $this->extrafieldsCache; $extrafieldOutputContent = $extrafields->showOutputField($extrafieldKey, $object->array_options[$extrafieldOptionsKey], '', $object->table_element); @@ -1172,14 +1171,9 @@ abstract class CommonDocGenerator } // 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]; - } + if(empty($this->extrafieldsCache)){ $this->extrafieldsCache = new ExtraFields($this->db); } + if(empty($this->extrafieldsCache->attributes[$object->table_element])){ $this->extrafieldsCache->fetch_name_optionals_label($object->table_element); } + $extrafields = $this->extrafieldsCache; /** @@ -1327,14 +1321,13 @@ abstract class CommonDocGenerator return; } - $extrafields = new ExtraFields($this->db); - $extralabels = $extrafields->fetch_name_optionals_label($object->table_element); + // Load extrafiels if not allready does + if(empty($this->extrafieldsCache)){ $this->extrafieldsCache = new ExtraFields($this->db); } + if(empty($this->extrafieldsCache->attributes[$object->table_element])){ $this->extrafieldsCache->fetch_name_optionals_label($object->table_element); } + $extrafields = $this->extrafieldsCache; - 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; + if (!empty($extrafields->attributes[$object->table_element]) && is_array($extrafields->attributes[$object->table_element]['label'])) { foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $label) { From d575188d8077a3dc5c2a82787a7132a881b925fd Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Sun, 23 Feb 2020 09:11:58 +0000 Subject: [PATCH 08/13] Fixing style errors. --- htdocs/core/class/commondocgenerator.class.php | 1 - 1 file changed, 1 deletion(-) diff --git a/htdocs/core/class/commondocgenerator.class.php b/htdocs/core/class/commondocgenerator.class.php index cfab7d7f5d6..b9302536dfd 100644 --- a/htdocs/core/class/commondocgenerator.class.php +++ b/htdocs/core/class/commondocgenerator.class.php @@ -1328,7 +1328,6 @@ abstract class CommonDocGenerator if (!empty($extrafields->attributes[$object->table_element]) && is_array($extrafields->attributes[$object->table_element]['label'])) { - foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $label) { // Dont display separator yet even is set to be displayed (not compatible yet) From fe4e00b139ebabb71aa374327afd15e6982f776b Mon Sep 17 00:00:00 2001 From: ATM john Date: Sun, 23 Feb 2020 11:32:38 +0100 Subject: [PATCH 09/13] Add new params to extrafields forms to allow extrafields to be displayed on pdf documents --- htdocs/core/actions_extrafields.inc.php | 6 +++-- .../core/class/commondocgenerator.class.php | 21 ++--------------- htdocs/core/class/extrafields.class.php | 23 ++++++++++++++----- htdocs/core/tpl/admin_extrafields_add.tpl.php | 3 +++ .../core/tpl/admin_extrafields_edit.tpl.php | 6 ++++- .../core/tpl/admin_extrafields_view.tpl.php | 4 +++- .../install/mysql/migration/11.0.0-12.0.0.sql | 4 +++- htdocs/langs/en_US/modulebuilder.lang | 2 ++ 8 files changed, 39 insertions(+), 30 deletions(-) diff --git a/htdocs/core/actions_extrafields.inc.php b/htdocs/core/actions_extrafields.inc.php index ca2499ab145..486764b167e 100644 --- a/htdocs/core/actions_extrafields.inc.php +++ b/htdocs/core/actions_extrafields.inc.php @@ -183,7 +183,8 @@ if ($action == 'add') (GETPOST('entitycurrentorall', 'alpha')?0:''), GETPOST('langfile', 'alpha'), 1, - (GETPOST('totalizable', 'alpha')?1:0) + (GETPOST('totalizable', 'alpha')?1:0), + (GETPOST('documentpdf', 'alpha')?1:0) ); if ($result > 0) { @@ -352,7 +353,8 @@ if ($action == 'update') (GETPOST('entitycurrentorall', 'alpha')?0:''), GETPOST('langfile'), 1, - (GETPOST('totalizable', 'alpha')?1:0) + (GETPOST('totalizable', 'alpha')?1:0), + (GETPOST('documentpdf', 'alpha')?1:0) ); if ($result > 0) { diff --git a/htdocs/core/class/commondocgenerator.class.php b/htdocs/core/class/commondocgenerator.class.php index cfab7d7f5d6..fcd2837735d 100644 --- a/htdocs/core/class/commondocgenerator.class.php +++ b/htdocs/core/class/commondocgenerator.class.php @@ -1187,16 +1187,7 @@ abstract class CommonDocGenerator foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $label) { // 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); - } - - // TODO : add extrafields enabled for display from extrafield config panel + $enabled = !empty($extrafields->attributes[$object->table_element]['documentpdf'][$key]); if(empty($enabled)){ continue; @@ -1338,16 +1329,8 @@ abstract class CommonDocGenerator } // 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); - } + $enabled = !empty($extrafields->attributes[$object->table_element]['documentpdf'][$key]); - if ($enabled && isset($extrafields->attributes[$object->table_element]['list'][$key])){ - $enabled = dol_eval($extrafields->attributes[$object->table_element]['list'][$key], 1); - } - - // TODO : add extrafields $enabled from extrafield config panel // Load language if required if (!empty($extrafields->attributes[$object->table_element]['langfile'][$key])) $outputlangs->load($extrafields->attributes[$object->table_element]['langfile'][$key]); diff --git a/htdocs/core/class/extrafields.class.php b/htdocs/core/class/extrafields.class.php index 5d5540d77b8..7765999339d 100644 --- a/htdocs/core/class/extrafields.class.php +++ b/htdocs/core/class/extrafields.class.php @@ -374,9 +374,11 @@ class ExtraFields * @param string $langfile Language file * @param string $enabled Condition to have the field enabled or not * @param int $totalizable Is a measure. Must show a total on lists + * @param int $documentpdf Is extrafield displayed on PDF * @return int <=0 if KO, >0 if OK + * @throws Exception */ - private function create_label($attrname, $label = '', $type = '', $pos = 0, $size = 0, $elementtype = 'member', $unique = 0, $required = 0, $param = '', $alwayseditable = 0, $perms = '', $list = '-1', $help = '', $default = '', $computed = '', $entity = '', $langfile = '', $enabled = '1', $totalizable = 0) + private function create_label($attrname, $label = '', $type = '', $pos = 0, $size = 0, $elementtype = 'member', $unique = 0, $required = 0, $param = '', $alwayseditable = 0, $perms = '', $list = '-1', $help = '', $default = '', $computed = '', $entity = '', $langfile = '', $enabled = '1', $totalizable = 0, $documentpdf = 0) { // phpcs:enable global $conf, $user; @@ -421,6 +423,7 @@ class ExtraFields $sql .= " perms,"; $sql .= " langs,"; $sql .= " list,"; + $sql .= " documentpdf,"; $sql .= " fielddefault,"; $sql .= " fieldcomputed,"; $sql .= " fk_user_author,"; @@ -444,6 +447,7 @@ class ExtraFields $sql .= " ".($perms ? "'".$this->db->escape($perms)."'" : "null").","; $sql .= " ".($langfile ? "'".$this->db->escape($langfile)."'" : "null").","; $sql .= " '".$this->db->escape($list)."',"; + $sql .= " '".$this->db->escape($documentpdf)."',"; $sql .= " ".($default ? "'".$this->db->escape($default)."'" : "null").","; $sql .= " ".($computed ? "'".$this->db->escape($computed)."'" : "null").","; $sql .= " ".(is_object($user) ? $user->id : 0).","; @@ -590,9 +594,11 @@ class ExtraFields * @param string $langfile Language file * @param string $enabled Condition to have the field enabled or not * @param int $totalizable Is extrafield totalizable on list + * @param int $documentpdf Is extrafield displayed on PDF * @return int >0 if OK, <=0 if KO + * @throws Exception */ - public function update($attrname, $label, $type, $length, $elementtype, $unique = 0, $required = 0, $pos = 0, $param = '', $alwayseditable = 0, $perms = '', $list = '', $help = '', $default = '', $computed = '', $entity = '', $langfile = '', $enabled = '1', $totalizable = 0) + public function update($attrname, $label, $type, $length, $elementtype, $unique = 0, $required = 0, $pos = 0, $param = '', $alwayseditable = 0, $perms = '', $list = '', $help = '', $default = '', $computed = '', $entity = '', $langfile = '', $enabled = '1', $totalizable = 0, $documentpdf = 0) { if ($elementtype == 'thirdparty') $elementtype = 'societe'; if ($elementtype == 'contact') $elementtype = 'socpeople'; @@ -642,7 +648,7 @@ class ExtraFields { if ($label) { - $result = $this->update_label($attrname, $label, $type, $length, $elementtype, $unique, $required, $pos, $param, $alwayseditable, $perms, $list, $help, $default, $computed, $entity, $langfile, $enabled, $totalizable); + $result = $this->update_label($attrname, $label, $type, $length, $elementtype, $unique, $required, $pos, $param, $alwayseditable, $perms, $list, $help, $default, $computed, $entity, $langfile, $enabled, $totalizable, $documentpdf); } if ($result > 0) { @@ -700,13 +706,15 @@ class ExtraFields * @param string $langfile Language file * @param string $enabled Condition to have the field enabled or not * @param int $totalizable Is extrafield totalizable on list + * @param int $documentpdf Is extrafield displayed on PDF * @return int <=0 if KO, >0 if OK + * @throws Exception */ - private function update_label($attrname, $label, $type, $size, $elementtype, $unique = 0, $required = 0, $pos = 0, $param = '', $alwayseditable = 0, $perms = '', $list = '0', $help = '', $default = '', $computed = '', $entity = '', $langfile = '', $enabled = '1', $totalizable = 0) + private function update_label($attrname, $label, $type, $size, $elementtype, $unique = 0, $required = 0, $pos = 0, $param = '', $alwayseditable = 0, $perms = '', $list = '0', $help = '', $default = '', $computed = '', $entity = '', $langfile = '', $enabled = '1', $totalizable = 0, $documentpdf = 0) { // phpcs:enable global $conf, $user; - dol_syslog(get_class($this)."::update_label ".$attrname.", ".$label.", ".$type.", ".$size.", ".$elementtype.", ".$unique.", ".$required.", ".$pos.", ".$alwayseditable.", ".$perms.", ".$list.", ".$default.", ".$computed.", ".$entity.", ".$langfile.", ".$enabled.", ".$totalizable); + dol_syslog(get_class($this)."::update_label ".$attrname.", ".$label.", ".$type.", ".$size.", ".$elementtype.", ".$unique.", ".$required.", ".$pos.", ".$alwayseditable.", ".$perms.", ".$list.", ".$default.", ".$computed.", ".$entity.", ".$langfile.", ".$enabled.", ".$totalizable.", ".$documentpdf); // Clean parameters if ($elementtype == 'thirdparty') $elementtype = 'societe'; @@ -771,6 +779,7 @@ class ExtraFields $sql .= " alwayseditable,"; $sql .= " param,"; $sql .= " list,"; + $sql .= " documentpdf,"; $sql .= " totalizable,"; $sql .= " fielddefault,"; $sql .= " fieldcomputed,"; @@ -794,6 +803,7 @@ class ExtraFields $sql .= " '".$this->db->escape($alwayseditable)."',"; $sql .= " '".$this->db->escape($params)."',"; $sql .= " '".$this->db->escape($list)."', "; + $sql .= " '".$this->db->escape($documentpdf)."', "; $sql .= " ".$totalizable.","; $sql .= " ".(($default != '') ? "'".$this->db->escape($default)."'" : "null").","; $sql .= " ".($computed ? "'".$this->db->escape($computed)."'" : "null").","; @@ -871,7 +881,7 @@ class ExtraFields }*/ // We should not have several time this request. If we have, there is some optimization to do by calling a simple $extrafields->fetch_optionals() in top of code and not into subcode - $sql = "SELECT rowid,name,label,type,size,elementtype,fieldunique,fieldrequired,param,pos,alwayseditable,perms,langs,list,totalizable,fielddefault,fieldcomputed,entity,enabled,help"; + $sql = "SELECT rowid,name,label,type,size,elementtype,fieldunique,fieldrequired,param,pos,alwayseditable,perms,langs,list,documentpdf,totalizable,fielddefault,fieldcomputed,entity,enabled,help"; $sql .= " FROM ".MAIN_DB_PREFIX."extrafields"; //$sql.= " WHERE entity IN (0,".$conf->entity.")"; // Filter is done later if ($elementtype) $sql .= " WHERE elementtype = '".$elementtype."'"; // Filed with object->table_element @@ -933,6 +943,7 @@ class ExtraFields $this->attributes[$tab->elementtype]['perms'][$tab->name] = (strlen($tab->perms) == 0 ? 1 : $tab->perms); $this->attributes[$tab->elementtype]['langfile'][$tab->name] = $tab->langs; $this->attributes[$tab->elementtype]['list'][$tab->name] = $tab->list; + $this->attributes[$tab->elementtype]['documentpdf'][$tab->name] = $tab->documentpdf; $this->attributes[$tab->elementtype]['totalizable'][$tab->name] = $tab->totalizable; $this->attributes[$tab->elementtype]['entityid'][$tab->name] = $tab->entity; $this->attributes[$tab->elementtype]['enabled'][$tab->name] = $tab->enabled; diff --git a/htdocs/core/tpl/admin_extrafields_add.tpl.php b/htdocs/core/tpl/admin_extrafields_add.tpl.php index e695c7aa95f..37872f2a5c0 100644 --- a/htdocs/core/tpl/admin_extrafields_add.tpl.php +++ b/htdocs/core/tpl/admin_extrafields_add.tpl.php @@ -196,6 +196,9 @@ $langs->load("modulebuilder"); textwithpicto($langs->trans("Visibility"), $langs->trans("VisibleDesc")); ?> + +textwithpicto($langs->trans("DisplayOnPdf"), $langs->trans("DisplayOnPdfDesc")); ?> +> trans("Totalizable"); ?>> diff --git a/htdocs/core/tpl/admin_extrafields_edit.tpl.php b/htdocs/core/tpl/admin_extrafields_edit.tpl.php index 4063111a6fb..8e14ad978ca 100644 --- a/htdocs/core/tpl/admin_extrafields_edit.tpl.php +++ b/htdocs/core/tpl/admin_extrafields_edit.tpl.php @@ -165,6 +165,7 @@ $list=$extrafields->attributes[$elementtype]['list'][$attrname]; $totalizable = $extrafields->attributes[$elementtype]['totalizable'][$attrname]; $help=$extrafields->attributes[$elementtype]['help'][$attrname]; $entitycurrentorall=$extrafields->attributes[$elementtype]['entityid'][$attrname]; +$documentpdf=$extrafields->attributes[$elementtype]['documentpdf'][$attrname]; if((($type == 'select') || ($type == 'checkbox') || ($type == 'radio')) && is_array($param)) { @@ -261,8 +262,12 @@ else trans("Required"); ?>> trans("AlwaysEditable"); ?>> + textwithpicto($langs->trans("Visibility"), $langs->trans("VisibleDesc")); ?> + +textwithpicto($langs->trans("DisplayOnPdf"), $langs->trans("DisplayOnPdfDesc")); ?> +> textwithpicto($langs->trans("Totalizable"), $langs->trans("TotalizableDesc")); ?>> textwithpicto($langs->trans("HelpOnTooltip"), $langs->trans("HelpOnTooltipDesc")); ?> @@ -270,7 +275,6 @@ else trans("AllEntities"); ?>> - diff --git a/htdocs/core/tpl/admin_extrafields_view.tpl.php b/htdocs/core/tpl/admin_extrafields_view.tpl.php index 0ddd3f5a8fa..3bec8cb8d6f 100644 --- a/htdocs/core/tpl/admin_extrafields_view.tpl.php +++ b/htdocs/core/tpl/admin_extrafields_view.tpl.php @@ -64,8 +64,9 @@ print ''.$langs->trans("Unique").''; print ''.$langs->trans("Required").''; print ''.$langs->trans("AlwaysEditable").''; print ''.$form->textwithpicto($langs->trans("Visible"), $langs->trans("VisibleDesc")).''; +print ''.$form->textwithpicto($langs->trans("DisplayOnPdf"), $langs->trans("DisplayOnPdfDesc")).''; print ''.$form->textwithpicto($langs->trans("Totalizable"), $langs->trans("TotalizableDesc")).''; -if ($conf->multicompany->enabled) { +if ($conf->multicompany->enabled){ print ''.$langs->trans("Entities").''; } print ' '; @@ -92,6 +93,7 @@ if (is_array($extrafields->attributes[$elementtype]['type']) && count($extrafiel print ''.yn($extrafields->attributes[$elementtype]['required'][$key])."\n"; print ''.yn($extrafields->attributes[$elementtype]['alwayseditable'][$key])."\n"; print ''.$extrafields->attributes[$elementtype]['list'][$key]."\n"; + print ''.yn($extrafields->attributes[$elementtype]['documentpdf'][$key])."\n"; print ''.yn($extrafields->attributes[$elementtype]['totalizable'][$key])."\n"; if (! empty($conf->multicompany->enabled)) { print ''; diff --git a/htdocs/install/mysql/migration/11.0.0-12.0.0.sql b/htdocs/install/mysql/migration/11.0.0-12.0.0.sql index 0b47a28e058..0a91f960807 100644 --- a/htdocs/install/mysql/migration/11.0.0-12.0.0.sql +++ b/htdocs/install/mysql/migration/11.0.0-12.0.0.sql @@ -179,4 +179,6 @@ INSERT INTO llx_c_ticket_resolution (code, pos, label, active, use_default, desc INSERT INTO llx_c_ticket_resolution (code, pos, label, active, use_default, description) VALUES('CANCELED', '50', 'Canceled', 1, 0, NULL); INSERT INTO llx_c_ticket_resolution (code, pos, label, active, use_default, description) VALUES('OTHER', '90', 'Other', 1, 0, NULL); -DELETE FROM llx_const WHERE name = __ENCRYPT('DONATION_ART885')__; \ No newline at end of file +DELETE FROM llx_const WHERE name = __ENCRYPT('DONATION_ART885')__; + +ALTER TABLE llx_extrafields ADD COLUMN documentpdf integer DEFAULT 0; \ No newline at end of file diff --git a/htdocs/langs/en_US/modulebuilder.lang b/htdocs/langs/en_US/modulebuilder.lang index ca3fd3e68b8..9f64e93f9d1 100644 --- a/htdocs/langs/en_US/modulebuilder.lang +++ b/htdocs/langs/en_US/modulebuilder.lang @@ -84,6 +84,8 @@ ListOfPermissionsDefined=List of defined permissions SeeExamples=See examples here EnabledDesc=Condition to have this field active (Examples: 1 or $conf->global->MYMODULE_MYOPTION) VisibleDesc=Is the field visible ? (Examples: 0=Never visible, 1=Visible on list and create/update/view forms, 2=Visible on list only, 3=Visible on create/update/view form only (not list), 4=Visible on list and update/view form only (not create), 5=Visible on list end view form only (not create, not update). Using a negative value means field is not shown by default on list but can be selected for viewing). It can be an expression, for example:
preg_match('/public/', $_SERVER['PHP_SELF'])?0:1
($user->rights->holiday->define_holiday ? 1 : 0) +DisplayOnPdfDesc=Display this field on compatible PDF documents, you can manage position with "Position" field.
Currently, known compatibles PDF models are : eratostene (order), espadon (ship), sponge (invoices), cyan (propal/quotation), cornas (supplier order) +DisplayOnPdf=Display on PDF IsAMeasureDesc=Can the value of field be cumulated to get a total into list? (Examples: 1 or 0) SearchAllDesc=Is the field used to make a search from the quick search tool? (Examples: 1 or 0) SpecDefDesc=Enter here all documentation you want to provide with your module that is not already defined by other tabs. You can use .md or better, the rich .asciidoc syntax. From 8a279ed1240500861c1e4e89fa63ab664be9e040 Mon Sep 17 00:00:00 2001 From: ATM john Date: Sun, 23 Feb 2020 15:24:59 +0100 Subject: [PATCH 10/13] improve extrafields display in notes --- .../core/class/commondocgenerator.class.php | 107 +++++++++++++++++- 1 file changed, 103 insertions(+), 4 deletions(-) diff --git a/htdocs/core/class/commondocgenerator.class.php b/htdocs/core/class/commondocgenerator.class.php index 29d4fab168b..9abb15494bf 100644 --- a/htdocs/core/class/commondocgenerator.class.php +++ b/htdocs/core/class/commondocgenerator.class.php @@ -1160,9 +1160,10 @@ abstract class CommonDocGenerator * * @param object $object line of common object * @param Translate $outputlangs Output language + * @param array $params array of additionals parameters * @return double max y value */ - public function getExtrafieldsInHtml($object, $outputlangs) + public function getExtrafieldsInHtml($object, $outputlangs, $params = array()) { global $hookmanager; @@ -1175,6 +1176,31 @@ abstract class CommonDocGenerator if(empty($this->extrafieldsCache->attributes[$object->table_element])){ $this->extrafieldsCache->fetch_name_optionals_label($object->table_element); } $extrafields = $this->extrafieldsCache; + $defaultParams = array( + 'style' => '', + 'display' => 'auto', // auto, table, list + + 'table' => array( + 'maxItemsInRow' => 2, + 'cellspacing' => 0, + 'cellpadding' => 0, + 'border' => 0, + 'labelcolwidth' => '25%', + 'arrayOfLineBreakType' => array('text', 'html') + ), + + 'list' => array( + 'separator' => '
' + ), + + 'auto' => array( + 'list' => 0, // 0 for default + 'table' => 4 // if there more than x extrafield to display + ), + ); + + $params = $params + $defaultParams; + /** * @var $extrafields ExtraFields @@ -1197,6 +1223,7 @@ abstract class CommonDocGenerator $field->rank = intval($extrafields->attributes[$object->table_element]['pos'][$key]); $field->content = $this->getExtrafieldContent($object, $key); $field->label = $outputlangs->transnoentities($label); + $field->type = $extrafields->attributes[$object->table_element]['type'][$key]; $fields[] = $field; } @@ -1204,14 +1231,86 @@ abstract class CommonDocGenerator if(!empty($fields)) { + // Sort extrafields by rank uasort($fields, function ($a, $b) { return ($a->rank > $b->rank) ? -1 : 1; } ); - foreach ($fields as $field){ - $html.= !empty($html)?'
':''; - $html.= $field->label.' : '.$field->content; + + + // define some HTML content with style + $html.= ''; + + // auto select display format + if($params['display'] == 'auto') { + $lastNnumbItems = 0; + foreach ($params['auto'] as $display => $numbItems){ + if($lastNnumbItems <= $numbItems && count($fields) > $numbItems){ + $lastNnumbItems = $numbItems; + $params['display'] = $display; + } + } + } + + if($params['display'] == 'list') { + // Display in list format + foreach ($fields as $field) { + $html .= !empty($html)?$params['list']['separator']:''; + $html .= '' . $field->label . ' : '; + $html .= $field->content; + } + } + elseif($params['display'] == 'table') { + // Display in table format + $html .= ''; + + $html .= ""; + $itemsInRow = 0; + $maxItemsInRow = $params['table']['maxItemsInRow']; + foreach ($fields as $field) { + //$html.= !empty($html)?'
':''; + if ($itemsInRow >= $maxItemsInRow) { + // start a new line + $html .= ""; + $itemsInRow = 0; + } + + // for some type we need line break + if (in_array($field->type, $params['table']['arrayOfLineBreakType'])) { + + if ($itemsInRow > 0) { + // close table row and empty cols + for ($i = $itemsInRow; $i <= $maxItemsInRow; $i++) { + $html .= ""; + } + $html .= ""; + + // start a new line + $html .= ""; + } + + $itemsInRow = $maxItemsInRow; + $html .= '"; + + } else { + $itemsInRow++; + $html .= '"; + + + $html .= '"; + } + } + $html .= ""; + + $html .= '
'; + $html .= '' . $field->label . ' : '; + $html .= $field->content; + $html .= "'; + $html .= '' . $field->label . ' :'; + $html .= "'; + $html .= $field->content; + $html .= "
'; } } From 222a000d310e6c68cd48722349e0eb6b3f22da9c Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Sun, 23 Feb 2020 14:27:22 +0000 Subject: [PATCH 11/13] Fixing style errors. --- htdocs/core/class/commondocgenerator.class.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/htdocs/core/class/commondocgenerator.class.php b/htdocs/core/class/commondocgenerator.class.php index 9abb15494bf..eb214516a6f 100644 --- a/htdocs/core/class/commondocgenerator.class.php +++ b/htdocs/core/class/commondocgenerator.class.php @@ -1278,7 +1278,6 @@ abstract class CommonDocGenerator // for some type we need line break if (in_array($field->type, $params['table']['arrayOfLineBreakType'])) { - if ($itemsInRow > 0) { // close table row and empty cols for ($i = $itemsInRow; $i <= $maxItemsInRow; $i++) { @@ -1295,7 +1294,6 @@ abstract class CommonDocGenerator $html .= '' . $field->label . ' : '; $html .= $field->content; $html .= ""; - } else { $itemsInRow++; $html .= ''; From 07bd6e1fc030ad6de3276031af22956734e59692 Mon Sep 17 00:00:00 2001 From: ATM john Date: Sun, 23 Feb 2020 20:52:44 +0100 Subject: [PATCH 12/13] Fix On admin page add extrafields form, display on pdf option doesn't saved --- htdocs/core/class/extrafields.class.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/htdocs/core/class/extrafields.class.php b/htdocs/core/class/extrafields.class.php index 7765999339d..ff1e6267252 100644 --- a/htdocs/core/class/extrafields.class.php +++ b/htdocs/core/class/extrafields.class.php @@ -225,9 +225,10 @@ class ExtraFields * @param string $langfile Language file * @param string $enabled Condition to have the field enabled or not * @param int $totalizable Is a measure. Must show a total on lists + * @param int $documentpdf Is extrafield displayed on PDF * @return int <=0 if KO, >0 if OK */ - public function addExtraField($attrname, $label, $type, $pos, $size, $elementtype, $unique = 0, $required = 0, $default_value = '', $param = '', $alwayseditable = 0, $perms = '', $list = '-1', $help = '', $computed = '', $entity = '', $langfile = '', $enabled = '1', $totalizable = 0) + public function addExtraField($attrname, $label, $type, $pos, $size, $elementtype, $unique = 0, $required = 0, $default_value = '', $param = '', $alwayseditable = 0, $perms = '', $list = '-1', $help = '', $computed = '', $entity = '', $langfile = '', $enabled = '1', $totalizable = 0, $documentpdf = 0) { if (empty($attrname)) return -1; if (empty($label)) return -1; @@ -245,7 +246,7 @@ class ExtraFields if ($result > 0 || $err1 == 'DB_ERROR_COLUMN_ALREADY_EXISTS' || $type == 'separate') { // Add declaration of field into table - $result2 = $this->create_label($attrname, $label, $type, $pos, $size, $elementtype, $unique, $required, $param, $alwayseditable, $perms, $list, $help, $default_value, $computed, $entity, $langfile, $enabled, $totalizable); + $result2 = $this->create_label($attrname, $label, $type, $pos, $size, $elementtype, $unique, $required, $param, $alwayseditable, $perms, $list, $help, $default_value, $computed, $entity, $langfile, $enabled, $totalizable, $documentpdf); $err2 = $this->errno; if ($result2 > 0 || ($err1 == 'DB_ERROR_COLUMN_ALREADY_EXISTS' && $err2 == 'DB_ERROR_RECORD_ALREADY_EXISTS')) { From 93c9f07cb935c20f6b102527da57b0d484b82f1d Mon Sep 17 00:00:00 2001 From: ATM john Date: Sun, 23 Feb 2020 20:59:04 +0100 Subject: [PATCH 13/13] Fix note x position --- htdocs/core/modules/expedition/doc/pdf_espadon.modules.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/htdocs/core/modules/expedition/doc/pdf_espadon.modules.php b/htdocs/core/modules/expedition/doc/pdf_espadon.modules.php index 5d211c2cbae..0ef7acc7bef 100644 --- a/htdocs/core/modules/expedition/doc/pdf_espadon.modules.php +++ b/htdocs/core/modules/expedition/doc/pdf_espadon.modules.php @@ -313,6 +313,8 @@ class pdf_espadon extends ModelePdfExpedition $tab_height = 130; $tab_height_newpage = 150; + $this->posxdesc = $this->marge_gauche + 1; + // Incoterm $height_incoterms = 0; if ($conf->incoterm->enabled)