From 882b39628f81710491c61258b9993bac76adca75 Mon Sep 17 00:00:00 2001 From: florian HENRY Date: Thu, 23 Apr 2020 15:50:42 +0200 Subject: [PATCH] fix: uniformize extrafeild replacemnt for ODT docs --- .../core/class/commondocgenerator.class.php | 88 +++++++------------ .../task/doc/doc_generic_task_odt.modules.php | 25 ++++-- 2 files changed, 46 insertions(+), 67 deletions(-) diff --git a/htdocs/core/class/commondocgenerator.class.php b/htdocs/core/class/commondocgenerator.class.php index a5cb9bf7d35..2317636f42a 100644 --- a/htdocs/core/class/commondocgenerator.class.php +++ b/htdocs/core/class/commondocgenerator.class.php @@ -170,12 +170,13 @@ abstract class CommonDocGenerator * * @param Societe $object Object * @param Translate $outputlangs Language object for output + * @param string $array_key Name of the key for return array * @return array Array of substitution key->code */ - public function get_substitutionarray_thirdparty($object, $outputlangs) + public function get_substitutionarray_thirdparty($object, $outputlangs, $array_key = 'company') { // phpcs:enable - global $conf; + global $conf, $extrafields; if (empty($object->country) && !empty($object->country_code)) { @@ -221,27 +222,14 @@ abstract class CommonDocGenerator 'company_default_bank_bic'=>$object->bank_account->bic ); - // Retrieve extrafields - if (is_array($object->array_options) && count($object->array_options)) - { - require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'; - $extrafields = new ExtraFields($this->db); - $extrafields->fetch_name_optionals_label($object->table_element, true); - $object->fetch_optionals(); + // Retrieve extrafields + if (is_array($object->array_options) && count($object->array_options)) + { + $object->fetch_optionals(); - foreach ($extrafields->attributes[$object->table_element]['label'] as $key=>$label) - { - if ($extrafields->attributes[$object->table_element]['type'][$key] == 'price') - { - $object->array_options['options_'.$key] = price($object->array_options['options_'.$key], 0, $outputlangs, 0, 0, -1, $conf->currency); - } - elseif ($extrafields->attributes[$object->table_element]['type'][$key] == 'select' || $extrafields->attributes[$object->table_element]['type'][$key] == 'checkbox') - { - $object->array_options['options_'.$key] = $extrafields->attributes[$object->table_element]['param'][$key]['options'][$object->array_options['options_'.$key]]; - } - $array_thirdparty = array_merge($array_thirdparty, array('company_options_'.$key => $object->array_options ['options_'.$key])); - } - } + $array_thirdparty = $this->fill_substitutionarray_with_extrafields($object, $array_thirdparty, $extrafields, $array_key, $outputlangs); + + } return $array_thirdparty; } @@ -251,13 +239,13 @@ abstract class CommonDocGenerator * * @param Contact $object contact * @param Translate $outputlangs object for output - * @param array $array_key Name of the key for return array + * @param string $array_key Name of the key for return array * @return array Array of substitution key->code */ public function get_substitutionarray_contact($object, $outputlangs, $array_key = 'object') { // phpcs:enable - global $conf; + global $conf, $extrafields; if (empty($object->country) && !empty($object->country_code)) { @@ -298,24 +286,14 @@ abstract class CommonDocGenerator $array_key.'_civility' => $object->civility, ); - // Retrieve extrafields - require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'; - $extrafields = new ExtraFields($this->db); - $extrafields->fetch_name_optionals_label($object->table_element, true); - $object->fetch_optionals(); + // Retrieve extrafields + if (is_array($object->array_options) && count($object->array_options)) + { + $object->fetch_optionals(); - foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $label) - { - if ($extrafields->attributes[$object->table_element]['type'][$key] == 'price') - { - $object->array_options['options_'.$key] = price($object->array_options ['options_'.$key], 0, $outputlangs, 0, 0, - 1, $conf->currency); - } - elseif ($extrafields->attributes[$object->table_element]['type'][$key] == 'select' || $extrafields->attributes[$object->table_element]['type'][$key] == 'checkbox') - { - $object->array_options['options_'.$key] = $extrafields->attributes[$object->table_element]['param'][$key]['options'][$object->array_options['options_'.$key]]; - } - $array_contact = array_merge($array_contact, array($array_key.'_options_'.$key => $object->array_options['options_'.$key])); - } + $array_contact = $this->fill_substitutionarray_with_extrafields($object, $array_contact, $extrafields, $array_key, $outputlangs); + + } return $array_contact; } @@ -370,7 +348,7 @@ abstract class CommonDocGenerator public function get_substitutionarray_object($object, $outputlangs, $array_key = 'object') { // phpcs:enable - global $conf; + global $conf, $extrafields; $sumpayed = $sumdeposit = $sumcreditnote = ''; $already_payed_all = 0; @@ -528,14 +506,10 @@ abstract class CommonDocGenerator // Retrieve extrafields if (is_array($object->array_options) && count($object->array_options)) { - $extrafieldkey = $object->element; - - require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'; - $extrafields = new ExtraFields($this->db); - $extrafields->fetch_name_optionals_label($extrafieldkey, true); $object->fetch_optionals(); $resarray = $this->fill_substitutionarray_with_extrafields($object, $resarray, $extrafields, $array_key, $outputlangs); + } return $resarray; @@ -605,7 +579,7 @@ abstract class CommonDocGenerator } // Retrieve extrafields - $extrafieldkey = $line->element; + $extrafieldkey = $line->table_element; $array_key = "line"; require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'; $extrafields = new ExtraFields($this->db); @@ -672,7 +646,7 @@ abstract class CommonDocGenerator public function get_substitutionarray_shipment($object, $outputlangs, $array_key = 'object') { // phpcs:enable - global $conf; + global $conf, $extrafields; dol_include_once('/core/lib/product.lib.php'); $object->list_delivery_methods($object->shipping_method_id); $calculatedVolume = ($object->trueWidth * $object->trueHeight * $object->trueDepth); @@ -708,16 +682,14 @@ abstract class CommonDocGenerator $array_shipment[$array_key.'_total_vat_'.$line->tva_tx] += $line->total_tva; } - // Retrieve extrafields - if (is_array($object->array_options) && count($object->array_options)) - { - require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'; - $extrafields = new ExtraFields($this->db); - $extrafields->fetch_name_optionals_label('expedition', true); - $object->fetch_optionals(); + // Retrieve extrafields + if (is_array($object->array_options) && count($object->array_options)) + { + $object->fetch_optionals(); - $array_shipment = $this->fill_substitutionarray_with_extrafields($object, $array_shipment, $extrafields, $array_key, $outputlangs); - } + $array_shipment = $this->fill_substitutionarray_with_extrafields($object, $array_shipment, $extrafields, $array_key, $outputlangs); + + } return $array_shipment; } diff --git a/htdocs/core/modules/project/task/doc/doc_generic_task_odt.modules.php b/htdocs/core/modules/project/task/doc/doc_generic_task_odt.modules.php index 8a38450697f..1aa8f05d9df 100644 --- a/htdocs/core/modules/project/task/doc/doc_generic_task_odt.modules.php +++ b/htdocs/core/modules/project/task/doc/doc_generic_task_odt.modules.php @@ -130,7 +130,7 @@ class doc_generic_task_odt extends ModelePDFTask public function get_substitutionarray_object($object, $outputlangs, $array_key = 'object') { // phpcs:enable - global $conf; + global $conf, $extrafields; $resarray = array( $array_key.'_id'=>$object->id, @@ -150,14 +150,10 @@ class doc_generic_task_odt extends ModelePDFTask // Retrieve extrafields if (is_array($object->array_options) && count($object->array_options)) { - $extrafieldkey = $object->element; - - require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'; - $extrafields = new ExtraFields($this->db); - $extrafields->fetch_name_optionals_label($extrafieldkey, true); $object->fetch_optionals(); $resarray = $this->fill_substitutionarray_with_extrafields($object, $resarray, $extrafields, $array_key, $outputlangs); + } return $resarray; @@ -169,14 +165,15 @@ class doc_generic_task_odt extends ModelePDFTask * * @param array $task Task Object * @param Translate $outputlangs Lang object to use for output + * @param string $array_key Name of the key for return array * @return array Return a substitution array */ - public function get_substitutionarray_tasks($task, $outputlangs) + public function get_substitutionarray_tasks($task, $outputlangs, $array_key = 'task') { // phpcs:enable - global $conf; + global $conf, $extrafields; - return array( + $resarray = array( 'task_ref'=>$task->ref, 'task_fk_project'=>$task->fk_project, 'task_projectref'=>$task->projectref, @@ -192,6 +189,16 @@ class doc_generic_task_odt extends ModelePDFTask 'task_note_private'=>$task->note_private, 'task_note_public'=>$task->note_public ); + + // Retrieve extrafields + if (is_array($task->array_options) && count($task->array_options)) + { + $task->fetch_optionals(); + + $resarray = $this->fill_substitutionarray_with_extrafields($task, $resarray, $extrafields, $array_key, $outputlangs); + } + + return $resarray; } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps