From 8a279ed1240500861c1e4e89fa63ab664be9e040 Mon Sep 17 00:00:00 2001 From: ATM john Date: Sun, 23 Feb 2020 15:24:59 +0100 Subject: [PATCH] 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 .= "
'; } }