Merge pull request #5043 from bafbes/abb-15
new function updateExtraField to update one particular extrafield
This commit is contained in:
commit
7adf3a4789
@ -10,6 +10,7 @@
|
||||
* Copyright (C) 2012-2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
|
||||
* Copyright (C) 2012 Cedric Salvador <csalvador@gpcsolutions.fr>
|
||||
* Copyright (C) 2015 Alexandre Spangaro <aspangaro.dolibarr@gmail.com>
|
||||
* Copyright (C) 2016 Bahfir abbes <dolipar@dolipar.org>
|
||||
*
|
||||
* 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
|
||||
@ -4229,6 +4230,88 @@ 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);
|
||||
|
||||
$value=$this->array_options["options_$key"];
|
||||
$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["options_$key"] = null;
|
||||
}
|
||||
break;
|
||||
case 'price':
|
||||
$this->array_options["options_$key"] = price2num($this->array_options["options_$key"]);
|
||||
break;
|
||||
case 'date':
|
||||
$this->array_options["options_$key"]=$this->db->idate($this->array_options["options_$key"]);
|
||||
break;
|
||||
case 'datetime':
|
||||
$this->array_options["options_$key"]=$this->db->idate($this->array_options["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["options_$key"]=$object->id;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
$this->db->begin();
|
||||
$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)
|
||||
{
|
||||
$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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user