2
0
forked from Wavyzz/dolibarr

NEW Mutualize code for action="update_extras"

NEW Can use setValueFrom without user modification field
FIX Exclude separators from mandatory extrafields test
More code to use the new framework array for extrafields
Can use text condition in extrafield visibility
This commit is contained in:
Laurent Destailleur
2018-04-12 23:16:23 +02:00
parent 1c9661d9d6
commit 3cecfd02c7
12 changed files with 155 additions and 92 deletions

View File

@@ -137,7 +137,7 @@ class ExtraFields
* @param int $type Type of attribute ('boolean','int','varchar','text','html','date','datehour','price','phone','mail','password','url','select','checkbox','separate',...)
* @param int $pos Position of attribute
* @param string $size Size/length of attribute
* @param string $elementtype Element type ('member', 'product', 'thirdparty', ...)
* @param string $elementtype Element type. Same value than object->table_element (Example 'member', 'product', 'thirdparty', ...)
* @param int $unique Is field unique or not
* @param int $required Is field required or not
* @param string $default_value Defaulted value (In database. use the default_value feature for default value on screen. Example: '', '0', 'null', 'avalue')
@@ -761,7 +761,7 @@ class ExtraFields
$sql = "SELECT rowid,name,label,type,size,elementtype,fieldunique,fieldrequired,param,pos,alwayseditable,perms,langs,list,fielddefault,fieldcomputed,entity";
$sql.= " FROM ".MAIN_DB_PREFIX."extrafields";
$sql.= " WHERE entity IN (0,".$conf->entity.")";
if ($elementtype) $sql.= " AND elementtype = '".$elementtype."'";
if ($elementtype) $sql.= " AND elementtype = '".$elementtype."'"; // Filed with object->table_element
$sql.= " ORDER BY pos";
$resql=$this->db->query($sql);
@@ -1375,7 +1375,7 @@ class ExtraFields
* @param string $key Key of attribute
* @param string $value Value to show
* @param string $moreparam To add more parameters on html input tag (only checkbox use html input for output rendering)
* @param string $extrafieldsobjectkey If defined, use the new method to get extrafields data
* @param string $extrafieldsobjectkey If defined (for example $object->table_element), use the new method to get extrafields data
* @return string Formated value
*/
function showOutputField($key, $value, $moreparam='', $extrafieldsobjectkey='')
@@ -1393,10 +1393,10 @@ class ExtraFields
$unique=$this->attributes[$extrafieldsobjectkey]['unique'][$key];
$required=$this->attributes[$extrafieldsobjectkey]['required'][$key];
$param=$this->attributes[$extrafieldsobjectkey]['param'][$key];
$perms=$this->attributes[$extrafieldsobjectkey]['perms'][$key];
$perms=dol_eval($this->attributes[$extrafieldsobjectkey]['perms'][$key], 1);
$langfile=$this->attributes[$extrafieldsobjectkey]['langfile'][$key];
$list=$this->attributes[$extrafieldsobjectkey]['list'][$key];
$hidden=(($list == 0) ? 1 : 0); // If zero, we are sure it is hidden, otherwise we show. If it depends on mode (view/create/edit form or list, this must be filtered by caller)
$list=dol_eval($this->attributes[$extrafieldsobjectkey]['list'][$key], 1);
$hidden=(empty($list) ? 1 : 0); // If empty, we are sure it is hidden, otherwise we show. If it depends on mode (view/create/edit form or list, this must be filtered by caller)
}
else // Old usage
{
@@ -1409,10 +1409,10 @@ class ExtraFields
$unique=$this->attribute_unique[$key];
$required=$this->attribute_required[$key];
$param=$this->attribute_param[$key];
$perms=$this->attribute_perms[$key];
$perms=dol_eval($this->attribute_perms[$key], 1);
$langfile=$this->attribute_langfile[$key];
$list=$this->attribute_list[$key];
$hidden=(($list == 0) ? 1 : 0); // If zero, we are sure it is hidden, otherwise we show. If it depends on mode (view/create/edit form or list, this must be filtered by caller)
$list=dol_eval($this->attribute_list[$key], 1);
$hidden=(empty($list) ? 1 : 0); // If empty, we are sure it is hidden, otherwise we show. If it depends on mode (view/create/edit form or list, this must be filtered by caller)
}
if ($hidden) return ''; // This is a protection. If field is hidden, we should just not call this method.
@@ -1754,17 +1754,19 @@ class ExtraFields
/**
* Fill array_options property of object by extrafields value (using for data sent by forms)
*
* @param array $extralabels $array of extrafields
* @param array $extralabels $array of extrafields (@deprecated)
* @param object $object Object
* @param string $onlykey Only following key is filled. When we make update of only one extrafield ($action = 'update_extras'), calling page must must set this to avoid to have other extrafields being reset.
* @return int 1 if array_options set, 0 if no value, -1 if error (field required missing for example)
*/
function setOptionalsFromPost($extralabels,&$object,$onlykey='')
function setOptionalsFromPost($extralabels, &$object, $onlykey='')
{
global $_POST, $langs;
$nofillrequired='';// For error when required field left blank
$error_field_required = array();
if (is_array($this->attributes[$object->table_element]['label'])) $extralabels=$this->attributes[$object->table_element]['label'];
if (is_array($extralabels))
{
// Get extra fields
@@ -1772,11 +1774,27 @@ class ExtraFields
{
if (! empty($onlykey) && $key != $onlykey) continue;
$key_type = $this->attribute_type[$key];
if ($this->attribute_required[$key] && empty($_POST["options_".$key])) // Check if empty without GETPOST, value can be alpha, int, array, etc...
$key_type = $this->attributes[$object->table_element]['type'][$key];
if ($key_type == 'separate') continue;
$enabled = 1;
if (isset($this->attributes[$object->table_element]['list'][$key]))
{
$enabled = dol_eval($this->attributes[$object->table_element]['list'][$key], 1);
}
$perms = 1;
if (isset($this->attributes[$object->table_element]['perms'][$key]))
{
$perms = dol_eval($this->attributes[$object->table_element]['perms'][$key], 1);
}
if (empty($enabled)) continue;
if (empty($perms)) continue;
if ($this->attributes[$object->table_element]['required'][$key] && empty($_POST["options_".$key])) // Check if empty without GETPOST, value can be alpha, int, array, etc...
{
//print 'ccc'.$value.'-'.$this->attributes[$object->table_element]['required'][$key];
$nofillrequired++;
$error_field_required[] = $value;
$error_field_required[] = $langs->transnoentitiesnoconv($value);
}
if (in_array($key_type,array('date')))
@@ -1829,7 +1847,7 @@ class ExtraFields
/**
* return array_options array of data of extrafields value of object sent by a search form
*
* @param array $extralabels $array of extrafields
* @param array $extralabels $array of extrafields (@deprecated)
* @param string $keyprefix Prefix string to add into name and id of field (can be used to avoid duplicate names)
* @param string $keysuffix Suffix string to add into name and id of field (can be used to avoid duplicate names)
* @return array|int array_options set or 0 if no value
@@ -1838,13 +1856,15 @@ class ExtraFields
{
global $_POST;
if (is_array($this->attributes[$object->table_element]['label'])) $extralabels=$this->attributes[$object->table_element]['label'];
$array_options = array();
if (is_array($extralabels))
{
// Get extra fields
foreach ($extralabels as $key => $value)
{
$key_type = $this->attribute_type[$key];
$key_type = $this->attributes[$object->table_element]['type'][$key];
if (in_array($key_type,array('date','datetime')))
{