From f0f4933905c3da568b7555ce49716af37e3bad5c Mon Sep 17 00:00:00 2001 From: abb Date: Sat, 16 Apr 2016 11:19:46 +0100 Subject: [PATCH 1/2] new function updateExtraField to update one particular extrafield Conflicts: htdocs/core/class/commonobject.class.php --- htdocs/core/class/commonobject.class.php | 82 ++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 7de64135245..2017ef3e873 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -10,6 +10,7 @@ * Copyright (C) 2012-2015 Raphaƫl Doursenaud * Copyright (C) 2012 Cedric Salvador * Copyright (C) 2015 Alexandre Spangaro + * Copyright (C) 2016 Bahfir abbes * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -4237,6 +4238,87 @@ abstract class CommonObject } else return 0; } + /** + * Update an exta field value for the current object. + * Data to describe values to insert/update are stored into $this->array_options=array('options_codeforfield1'=>'valueforfield1', 'options_codeforfield2'=>'valueforfield2', ...) + * This function delte record with all extrafields and insert them again from the array $this->array_options. + * $key key of the extrafield + * @return int -1=error, O=did nothing, 1=OK + */ + function updateExtraField($key) + { + global $conf,$langs; + + $error=0; + + if (! empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) return 0; // For avoid conflicts if trigger used + + if (! empty($this->array_options) && !empty($this->array_options["options_$key"])) + { + // Check parameters + $langs->load('admin'); + require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'; + $extrafields = new ExtraFields($this->db); + $target_extrafields=$extrafields->fetch_name_optionals_label($this->table_element); + + $attributeType = $extrafields->attribute_type[$key]; + $attributeLabel = $extrafields->attribute_label[$key]; + $attributeParam = $extrafields->attribute_param[$key]; + switch ($attributeType) + { + case 'int': + if (!is_numeric($value) && $value!='') + { + $this->errors[]=$langs->trans("ExtraFieldHasWrongValue",$attributeLabel); + return -1; + } + elseif ($value=='') + { + $this->array_options[$key] = null; + } + break; + case 'price': + $this->array_options[$key] = price2num($this->array_options[$key]); + break; + case 'date': + $this->array_options[$key]=$this->db->idate($this->array_options[$key]); + break; + case 'datetime': + $this->array_options[$key]=$this->db->idate($this->array_options[$key]); + break; + case 'link': + $param_list=array_keys($attributeParam ['options']); + // 0 : ObjectName + // 1 : classPath + $InfoFieldList = explode(":", $param_list[0]); + dol_include_once($InfoFieldList[1]); + $object = new $InfoFieldList[0]($this->db); + if ($value) + { + $object->fetch(0,$value); + $this->array_options[$key]=$object->id; + } + break; + } + + $this->db->begin(); + $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element."_extrafields SET $key=$value"; + $sql .= " WHERE fk_object = ".$this->id; + $resql = $this->db->query($sql); + if (! $resql) + { + $this->error=$this->db->lasterror(); + $this->db->rollback(); + return -1; + } + else + { + $this->db->commit(); + return 1; + } + } + else return 0; + } /** * Function to show lines of extrafields with output datas From 62aa6285245260af46a080039b09e5b7606776bc Mon Sep 17 00:00:00 2001 From: abb Date: Sat, 16 Apr 2016 17:41:47 +0100 Subject: [PATCH 2/2] fixmycommit:new function updateExtraField to update one particular extrafield --- htdocs/core/class/commonobject.class.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 2017ef3e873..46278ffcdf4 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -4261,6 +4261,7 @@ abstract class CommonObject $extrafields = new ExtraFields($this->db); $target_extrafields=$extrafields->fetch_name_optionals_label($this->table_element); + $value=$this->array_options["options_$key"]; $attributeType = $extrafields->attribute_type[$key]; $attributeLabel = $extrafields->attribute_label[$key]; $attributeParam = $extrafields->attribute_param[$key]; @@ -4274,17 +4275,17 @@ abstract class CommonObject } elseif ($value=='') { - $this->array_options[$key] = null; + $this->array_options["options_$key"] = null; } break; case 'price': - $this->array_options[$key] = price2num($this->array_options[$key]); + $this->array_options["options_$key"] = price2num($this->array_options["options_$key"]); break; case 'date': - $this->array_options[$key]=$this->db->idate($this->array_options[$key]); + $this->array_options["options_$key"]=$this->db->idate($this->array_options["options_$key"]); break; case 'datetime': - $this->array_options[$key]=$this->db->idate($this->array_options[$key]); + $this->array_options["options_$key"]=$this->db->idate($this->array_options["options_$key"]); break; case 'link': $param_list=array_keys($attributeParam ['options']); @@ -4296,13 +4297,13 @@ abstract class CommonObject if ($value) { $object->fetch(0,$value); - $this->array_options[$key]=$object->id; + $this->array_options["options_$key"]=$object->id; } break; } $this->db->begin(); - $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element."_extrafields SET $key=$value"; + $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element."_extrafields SET $key=".$this->array_options["options_$key"]; $sql .= " WHERE fk_object = ".$this->id; $resql = $this->db->query($sql); if (! $resql)