';
- if (in_array($typeofextrafield, array('varchar', 'int', 'double', 'select')))
+ if (in_array($typeofextrafield, array('varchar', 'int', 'double', 'select')) && empty($extrafields->attribute_computed[$key]))
{
$crit=$val;
$tmpkey=preg_replace('/search_options_/','',$key);
@@ -664,7 +664,9 @@ if (is_array($extrafields->attribute_label) && count($extrafields->attribute_lab
if (! empty($arrayfields["ef.".$key]['checked']))
{
$align=$extrafields->getAlignFlag($key);
- print_liste_field_titre($langs->trans($extralabels[$key]),$_SERVER["PHP_SELF"],"ef.".$key,"",$param,($align?'align="'.$align.'"':''),$sortfield,$sortorder);
+ $sortonfield = "ef.".$key;
+ if (! empty($extrafields->attribute_computed[$key])) $sortonfield='';
+ print_liste_field_titre($langs->trans($extralabels[$key]),$_SERVER["PHP_SELF"],$sortonfield,"",$param,($align?'align="'.$align.'"':''),$sortfield,$sortorder);
}
}
}
diff --git a/htdocs/core/actions_extrafields.inc.php b/htdocs/core/actions_extrafields.inc.php
index afcf929589b..b34cbe886ab 100644
--- a/htdocs/core/actions_extrafields.inc.php
+++ b/htdocs/core/actions_extrafields.inc.php
@@ -14,6 +14,8 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
* or see http://www.gnu.org/
+ *
+ * $elementype must be defined.
*/
/**
@@ -24,11 +26,14 @@
$maxsizestring=255;
$maxsizeint=10;
-$extrasize=GETPOST('size');
-if (GETPOST('type')=='double' && strpos($extrasize,',')===false) $extrasize='24,8';
-if (GETPOST('type')=='date') $extrasize='';
-if (GETPOST('type')=='datetime') $extrasize='';
-if (GETPOST('type')=='select') $extrasize='';
+$extrasize=GETPOST('size','int');
+$type=GETPOST('type','alpha');
+$param=GETPOST('param','alpha');;
+
+if ($type=='double' && strpos($extrasize,',')===false) $extrasize='24,8';
+if ($type=='date') $extrasize='';
+if ($type=='datetime') $extrasize='';
+if ($type=='select') $extrasize='';
// Add attribute
@@ -37,73 +42,73 @@ if ($action == 'add')
if ($_POST["button"] != $langs->trans("Cancel"))
{
// Check values
- if (! GETPOST('type'))
+ if (! $type)
{
$error++;
$langs->load("errors");
$mesg[]=$langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Type"));
$action = 'create';
}
- if (GETPOST('type')=='varchar' && $extrasize <= 0)
+ if ($type=='varchar' && $extrasize <= 0)
{
$error++;
$langs->load("errors");
$mesg[]=$langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Size"));
$action = 'edit';
}
- if (GETPOST('type')=='varchar' && $extrasize > $maxsizestring)
+ if ($type=='varchar' && $extrasize > $maxsizestring)
{
$error++;
$langs->load("errors");
$mesg[]=$langs->trans("ErrorSizeTooLongForVarcharType",$maxsizestring);
$action = 'create';
}
- if (GETPOST('type')=='int' && $extrasize > $maxsizeint)
+ if ($type=='int' && $extrasize > $maxsizeint)
{
$error++;
$langs->load("errors");
$mesg[]=$langs->trans("ErrorSizeTooLongForIntType",$maxsizeint);
$action = 'create';
}
- if (GETPOST('type')=='select' && !GETPOST('param'))
+ if ($type=='select' && !$param)
{
$error++;
$langs->load("errors");
$mesg[]=$langs->trans("ErrorNoValueForSelectType");
$action = 'create';
}
- if (GETPOST('type')=='sellist' && !GETPOST('param'))
+ if ($type=='sellist' && !$param)
{
$error++;
$langs->load("errors");
$mesg[]=$langs->trans("ErrorNoValueForSelectListType");
$action = 'create';
}
- if (GETPOST('type')=='checkbox' && !GETPOST('param'))
+ if ($type=='checkbox' && !$param)
{
$error++;
$langs->load("errors");
$mesg[]=$langs->trans("ErrorNoValueForCheckBoxType");
$action = 'create';
}
- if (GETPOST('type')=='link' && !GETPOST('param'))
+ if ($type=='link' && !$param)
{
$error++;
$langs->load("errors");
$mesg[]=$langs->trans("ErrorNoValueForLinkType");
$action = 'create';
}
- if (GETPOST('type')=='radio' && !GETPOST('param'))
+ if ($type=='radio' && !$param)
{
$error++;
$langs->load("errors");
$mesg[]=$langs->trans("ErrorNoValueForRadioType");
$action = 'create';
}
- if (((GETPOST('type')=='radio') || (GETPOST('type')=='checkbox')) && GETPOST('param'))
+ if ((($type=='radio') || ($type=='checkbox')) && $param)
{
// Construct array for parameter (value of select list)
- $parameters = GETPOST('param');
+ $parameters = $param;
$parameters_array = explode("\r\n",$parameters);
foreach($parameters_array as $param_ligne)
{
@@ -134,11 +139,11 @@ if ($action == 'add')
if (isset($_POST["attrname"]) && preg_match("/^[a-z0-9-_]+$/",$_POST['attrname']) && !is_numeric($_POST["attrname"]))
{
// Construct array for parameter (value of select list)
- $default_value = GETPOST('default_value');
- $parameters = GETPOST('param');
+ $default_value = GETPOST('default_value','alpha');
+ $parameters = $param;
$parameters_array = explode("\r\n",$parameters);
//In sellist we have only one line and it can have come to do SQL expression
- if (GETPOST('type')=='sellist') {
+ if ($type=='sellist') {
foreach($parameters_array as $param_ligne)
{
$params['options'] = array($parameters=>null);
@@ -155,20 +160,21 @@ if ($action == 'add')
}
$result=$extrafields->addExtraField(
- GETPOST('attrname'),
- GETPOST('label'),
- GETPOST('type'),
- GETPOST('pos'),
+ GETPOST('attrname', 'alpha'),
+ GETPOST('label', 'alpha'),
+ $type,
+ GETPOST('pos', 'alpha'),
$extrasize,
$elementtype,
- (GETPOST('unique')?1:0),
- (GETPOST('required')?1:0),
+ (GETPOST('unique', 'alpha')?1:0),
+ (GETPOST('required', 'alpha')?1:0),
$default_value,
$params,
- (GETPOST('alwayseditable')?1:0),
- (GETPOST('perms')?GETPOST('perms'):''),
- (GETPOST('list')?1:0),
- (GETPOST('ishidden')?1:0)
+ (GETPOST('alwayseditable', 'alpha')?1:0),
+ (GETPOST('perms', 'alpha')?GETPOST('perms', 'alpha'):''),
+ (GETPOST('list', 'alpha')?1:0),
+ (GETPOST('ishidden', 'alpha')?1:0),
+ GETPOST('computed_value','alpha')
);
if ($result > 0)
{
@@ -205,66 +211,66 @@ if ($action == 'update')
if ($_POST["button"] != $langs->trans("Cancel"))
{
// Check values
- if (! GETPOST('type'))
+ if (! $type)
{
$error++;
$langs->load("errors");
$mesg[]=$langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Type"));
$action = 'edit';
}
- if (GETPOST('type')=='varchar' && $extrasize <= 0)
+ if ($type=='varchar' && $extrasize <= 0)
{
$error++;
$langs->load("errors");
$mesg[]=$langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Size"));
$action = 'edit';
}
- if (GETPOST('type')=='varchar' && $extrasize > $maxsizestring)
+ if ($type=='varchar' && $extrasize > $maxsizestring)
{
$error++;
$langs->load("errors");
$mesg[]=$langs->trans("ErrorSizeTooLongForVarcharType",$maxsizestring);
$action = 'edit';
}
- if (GETPOST('type')=='int' && $extrasize > $maxsizeint)
+ if ($type=='int' && $extrasize > $maxsizeint)
{
$error++;
$langs->load("errors");
$mesg[]=$langs->trans("ErrorSizeTooLongForIntType",$maxsizeint);
$action = 'edit';
}
- if (GETPOST('type')=='select' && !GETPOST('param'))
+ if ($type=='select' && !$param)
{
$error++;
$langs->load("errors");
$mesg[]=$langs->trans("ErrorNoValueForSelectType");
$action = 'edit';
}
- if (GETPOST('type')=='sellist' && !GETPOST('param'))
+ if ($type=='sellist' && !$param)
{
$error++;
$langs->load("errors");
$mesg[]=$langs->trans("ErrorNoValueForSelectListType");
$action = 'edit';
}
- if (GETPOST('type')=='checkbox' && !GETPOST('param'))
+ if ($type=='checkbox' && !$param)
{
$error++;
$langs->load("errors");
$mesg[]=$langs->trans("ErrorNoValueForCheckBoxType");
$action = 'edit';
}
- if (GETPOST('type')=='radio' && !GETPOST('param'))
+ if ($type=='radio' && !$param)
{
$error++;
$langs->load("errors");
$mesg[]=$langs->trans("ErrorNoValueForRadioType");
$action = 'edit';
}
- if (((GETPOST('type')=='radio') || (GETPOST('type')=='checkbox')) && GETPOST('param'))
+ if ((($type=='radio') || ($type=='checkbox')) && $param)
{
// Construct array for parameter (value of select list)
- $parameters = GETPOST('param');
+ $parameters = $param;
$parameters_array = explode("\r\n",$parameters);
foreach($parameters_array as $param_ligne)
{
@@ -295,10 +301,10 @@ if ($action == 'update')
{
$pos = GETPOST('pos','int');
// Construct array for parameter (value of select list)
- $parameters = GETPOST('param');
+ $parameters = $param;
$parameters_array = explode("\r\n",$parameters);
//In sellist we have only one line and it can have come to do SQL expression
- if (GETPOST('type')=='sellist') {
+ if ($type=='sellist') {
foreach($parameters_array as $param_ligne)
{
$params['options'] = array($parameters=>null);
@@ -315,19 +321,21 @@ if ($action == 'update')
}
$result=$extrafields->update(
- GETPOST('attrname'),
- GETPOST('label'),
- GETPOST('type'),
+ GETPOST('attrname', 'alpha'),
+ GETPOST('label', 'alpha'),
+ $type,
$extrasize,
$elementtype,
- (GETPOST('unique')?1:0),
- (GETPOST('required')?1:0),
+ (GETPOST('unique', 'alpha')?1:0),
+ (GETPOST('required', 'alpha')?1:0),
$pos,
$params,
- (GETPOST('alwayseditable')?1:0),
- (GETPOST('perms')?GETPOST('perms'):''),
- (GETPOST('list')?1:0),
- (GETPOST('ishidden')?1:0)
+ (GETPOST('alwayseditable', 'alpha')?1:0),
+ (GETPOST('perms', 'alpha')?GETPOST('perms', 'alpha'):''),
+ (GETPOST('list', 'alpha')?1:0),
+ (GETPOST('ishidden', 'alpha')?1:0),
+ GETPOST('default_value','alpha'),
+ GETPOST('computed_value','alpha')
);
if ($result > 0)
{
diff --git a/htdocs/core/class/extrafields.class.php b/htdocs/core/class/extrafields.class.php
index f86f786e0fb..c89fc593745 100644
--- a/htdocs/core/class/extrafields.class.php
+++ b/htdocs/core/class/extrafields.class.php
@@ -36,14 +36,22 @@
class ExtraFields
{
var $db;
- // Tableau contenant le nom des champs en clef et la definition de ces champs
+
+ // type of element (for what object is the extrafield)
+ var $attribute_elementtype;
+
+ // Array with type of the extra field
var $attribute_type;
- // Tableau contenant le nom des champs en clef et le label de ces champs en value
+ // Array with label of extra field
var $attribute_label;
- // Tableau contenant le nom des champs en clef et la taille/longueur max de ces champs en value
+ // Array with size of extra field
var $attribute_size;
- // Tableau contenant le nom des choix en clef et la valeur de ces choix en value
+ // array with list of possible values for some types of extra fields
var $attribute_choice;
+ // Array to store compute formula for computed fields
+ var $attribute_computed;
+ // Array to store default value
+ var $attribute_default;
// Array to store if attribute is unique or not
var $attribute_unique;
// Array to store if attribute is required or not
@@ -77,16 +85,17 @@ class ExtraFields
'phone'=>'ExtrafieldPhone',
'mail'=>'ExtrafieldMail',
'url'=>'ExtrafieldUrl',
+ 'password' => 'ExtrafieldPassword',
'select' => 'ExtrafieldSelect',
'sellist' => 'ExtrafieldSelectList',
'radio' => 'ExtrafieldRadio',
'checkbox' => 'ExtrafieldCheckBox',
'chkbxlst' => 'ExtrafieldCheckBoxFromList',
'link' => 'ExtrafieldLink',
- 'password' => 'ExtrafieldPassword',
'separate' => 'ExtrafieldSeparator',
);
+
/**
* Constructor
*
@@ -96,10 +105,12 @@ class ExtraFields
{
$this->db = $db;
$this->error = array();
+ $this->attribute_elementtype = array();
$this->attribute_type = array();
$this->attribute_label = array();
$this->attribute_size = array();
- $this->attribute_elementtype = array();
+ $this->attribute_computed = array();
+ $this->attribute_default = array();
$this->attribute_unique = array();
$this->attribute_required = array();
$this->attribute_perms = array();
@@ -118,15 +129,16 @@ class ExtraFields
* @param string $elementtype Element type ('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 (Example: '', '0', 'null', 'avalue')
+ * @param string $default_value Defaulted value (In database. use the default_value feature for default value on screen. Example: '', '0', 'null', 'avalue')
* @param array $param Params for field
* @param int $alwayseditable Is attribute always editable regardless of the document status
* @param string $perms Permission to check
* @param int $list Into list view by default
* @param int $ishidden Is hidden extrafield (warning, do not rely on this. If your module need a hidden data, it must use its own table)
+ * @param string $computed Computed value
* @return int <=0 if KO, >0 if OK
*/
- function addExtraField($attrname, $label, $type, $pos, $size, $elementtype, $unique=0, $required=0, $default_value='', $param=0, $alwayseditable=0, $perms='', $list=0, $ishidden=0)
+ function addExtraField($attrname, $label, $type, $pos, $size, $elementtype, $unique=0, $required=0, $default_value='', $param=0, $alwayseditable=0, $perms='', $list=0, $ishidden=0, $computed='')
{
if (empty($attrname)) return -1;
if (empty($label)) return -1;
@@ -137,13 +149,13 @@ class ExtraFields
// Create field into database except for separator type which is not stored in database
if ($type != 'separate')
{
- $result=$this->create($attrname, $type, $size, $elementtype, $unique, $required, $default_value, $param, $perms, $list);
+ $result=$this->create($attrname, $type, $size, $elementtype, $unique, $required, $default_value, $param, $perms, $list, $copmputed);
}
$err1=$this->errno;
if ($result > 0 || $err1 == 'DB_ERROR_COLUMN_ALREADY_EXISTS' || $type == 'separate')
{
// Add declaration of field into table
- $result2=$this->create_label($attrname,$label,$type,$pos,$size,$elementtype, $unique, $required, $param, $alwayseditable, $perms, $list, $ishidden);
+ $result2=$this->create_label($attrname, $label, $type, $pos, $size, $elementtype, $unique, $required, $param, $alwayseditable, $perms, $list, $ishidden, $default, $computed);
$err2=$this->errno;
if ($result2 > 0 || ($err1 == 'DB_ERROR_COLUMN_ALREADY_EXISTS' && $err2 == 'DB_ERROR_RECORD_ALREADY_EXISTS'))
{
@@ -169,13 +181,14 @@ class ExtraFields
* @param string $elementtype Element type ('member', 'product', 'thirdparty', 'contact', ...)
* @param int $unique Is field unique or not
* @param int $required Is field required or not
- * @param string $default_value Default value for field
+ * @param string $default_value Default value for field (in database)
* @param array $param Params for field (ex for select list : array('options'=>array('value'=>'label of option'))
* @param string $perms Permission
* @param int $list Into list view by default
+ * @param string $computed Computed value
* @return int <=0 if KO, >0 if OK
*/
- private function create($attrname, $type='varchar', $length=255, $elementtype='member', $unique=0, $required=0, $default_value='',$param='', $perms='', $list=0)
+ private function create($attrname, $type='varchar', $length=255, $elementtype='member', $unique=0, $required=0, $default_value='',$param='', $perms='', $list=0, $computed='')
{
if ($elementtype == 'thirdparty') $elementtype='societe';
if ($elementtype == 'contact') $elementtype='socpeople';
@@ -258,9 +271,11 @@ class ExtraFields
* @param string $perms Permission to check
* @param int $list Into list view by default
* @param int $ishidden Is hidden extrafield (warning, do not rely on this. If your module need a hidden data, it must use its own table)
+ * @param string $default Default value (in database. use the default_value feature for default value on screen).
+ * @param string $computed Computed value
* @return int <=0 if KO, >0 if OK
*/
- private function create_label($attrname, $label='', $type='', $pos=0, $size=0, $elementtype='member', $unique=0, $required=0, $param='', $alwayseditable=0, $perms='', $list=0, $ishidden=0)
+ private function create_label($attrname, $label='', $type='', $pos=0, $size=0, $elementtype='member', $unique=0, $required=0, $param='', $alwayseditable=0, $perms='', $list=0, $ishidden=0, $default='', $computed='')
{
global $conf;
@@ -286,7 +301,7 @@ class ExtraFields
$params='';
}
- $sql = "INSERT INTO ".MAIN_DB_PREFIX."extrafields(name, label, type, pos, size, entity, elementtype, fieldunique, fieldrequired, param, alwayseditable, perms, list, ishidden)";
+ $sql = "INSERT INTO ".MAIN_DB_PREFIX."extrafields(name, label, type, pos, size, entity, elementtype, fieldunique, fieldrequired, param, alwayseditable, perms, list, ishidden, fielddefault, fieldcomputed)";
$sql.= " VALUES('".$attrname."',";
$sql.= " '".$this->db->escape($label)."',";
$sql.= " '".$type."',";
@@ -299,8 +314,10 @@ class ExtraFields
$sql.= " '".$params."',";
$sql.= " '".$alwayseditable."',";
$sql.= " ".($perms?"'".$this->db->escape($perms)."'":"null").",";
- $sql.= " ".$list;
- $sql.= ", ".$ishidden;
+ $sql.= " ".$list.",";
+ $sql.= " ".$ishidden.",";
+ $sql.= " ".($default?"'".$this->db->escape($default)."'":"null").",";
+ $sql.= " ".($computed?"'".$this->db->escape($computed)."'":"null");
$sql.=')';
dol_syslog(get_class($this)."::create_label", LOG_DEBUG);
@@ -430,9 +447,11 @@ class ExtraFields
* @param string $perms Permission to check
* @param int $list Into list view by default
* @param int $ishidden Is hidden extrafield (warning, do not rely on this. If your module need a hidden data, it must use its own table)
+ * @param string $default Default value (in database. use the default_value feature for default value on screen).
+ * @param string $computed Computed value
* @return int >0 if OK, <=0 if KO
*/
- function update($attrname,$label,$type,$length,$elementtype,$unique=0,$required=0,$pos=0,$param='',$alwayseditable=0, $perms='',$list='',$ishidden=0)
+ function update($attrname,$label,$type,$length,$elementtype,$unique=0,$required=0,$pos=0,$param='',$alwayseditable=0, $perms='',$list='',$ishidden=0,$default='',$computed='')
{
if ($elementtype == 'thirdparty') $elementtype='societe';
if ($elementtype == 'contact') $elementtype='socpeople';
@@ -479,7 +498,7 @@ class ExtraFields
{
if ($label)
{
- $result=$this->update_label($attrname,$label,$type,$length,$elementtype,$unique,$required,$pos,$param,$alwayseditable,$perms,$list,$ishidden);
+ $result=$this->update_label($attrname,$label,$type,$length,$elementtype,$unique,$required,$pos,$param,$alwayseditable,$perms,$list,$ishidden,$default,$computed);
}
if ($result > 0)
{
@@ -531,12 +550,14 @@ class ExtraFields
* @param string $perms Permission to check
* @param int $list Into list view by default
* @param int $ishidden Is hidden extrafield (warning, do not rely on this. If your module need a hidden data, it must use its own table)
+ * @param string $default Default value (in database. use the default_value feature for default value on screen).
+ * @param string $computed Computed value
* @return int <=0 if KO, >0 if OK
*/
- private function update_label($attrname,$label,$type,$size,$elementtype,$unique=0,$required=0,$pos=0,$param='',$alwayseditable=0,$perms='',$list=0,$ishidden=0)
+ private function update_label($attrname,$label,$type,$size,$elementtype,$unique=0,$required=0,$pos=0,$param='',$alwayseditable=0,$perms='',$list=0,$ishidden=0,$default='',$computed='')
{
global $conf;
- dol_syslog(get_class($this)."::update_label ".$attrname.", ".$label.", ".$type.", ".$size.", ".$elementtype.", ".$unique.", ".$required.", ".$pos.", ".$alwayseditable.", ".$perms.", ".$list.", ".$ishidden);
+ dol_syslog(get_class($this)."::update_label ".$attrname.", ".$label.", ".$type.", ".$size.", ".$elementtype.", ".$unique.", ".$required.", ".$pos.", ".$alwayseditable.", ".$perms.", ".$list.", ".$ishidden.", ".$default.", ".$computed);
// Clean parameters
if ($elementtype == 'thirdparty') $elementtype='societe';
@@ -557,7 +578,7 @@ class ExtraFields
$sql_del.= " WHERE name = '".$attrname."'";
$sql_del.= " AND entity = ".$conf->entity;
$sql_del.= " AND elementtype = '".$elementtype."'";
- dol_syslog(get_class($this)."::update_label", LOG_DEBUG);
+
$resql1=$this->db->query($sql_del);
$sql = "INSERT INTO ".MAIN_DB_PREFIX."extrafields(";
@@ -573,8 +594,10 @@ class ExtraFields
$sql.= " pos,";
$sql.= " alwayseditable,";
$sql.= " param,";
- $sql.= " list";
- $sql.= ", ishidden";
+ $sql.= " list,";
+ $sql.= " ishidden,";
+ $sql.= " fielddefault,";
+ $sql.= " fieldcomputed";
$sql.= ") VALUES (";
$sql.= "'".$attrname."',";
$sql.= " ".$conf->entity.",";
@@ -588,10 +611,12 @@ class ExtraFields
$sql.= " '".$pos."',";
$sql.= " '".$alwayseditable."',";
$sql.= " '".$param."',";
- $sql.= " ".$list;
- $sql.= ", ".$ishidden;
+ $sql.= " ".$list.", ";
+ $sql.= " ".$ishidden.", ";
+ $sql.= " ".($default?"'".$this->db->escape($default)."'":"null").",";
+ $sql.= " ".($computed?"'".$this->db->escape($computed)."'":"null");
$sql.= ")";
- dol_syslog(get_class($this)."::update_label", LOG_DEBUG);
+
$resql2=$this->db->query($sql);
if ($resql1 && $resql2)
@@ -635,7 +660,7 @@ class ExtraFields
// For avoid conflicts with external modules
if (!$forceload && !empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) return $array_name_label;
- $sql = "SELECT rowid,name,label,type,size,elementtype,fieldunique,fieldrequired,param,pos,alwayseditable,perms,list,ishidden";
+ $sql = "SELECT rowid,name,label,type,size,elementtype,fieldunique,fieldrequired,param,pos,alwayseditable,perms,list,ishidden,fielddefault,fieldcomputed";
$sql.= " FROM ".MAIN_DB_PREFIX."extrafields";
$sql.= " WHERE entity IN (0,".$conf->entity.")";
if ($elementtype) $sql.= " AND elementtype = '".$elementtype."'";
@@ -658,6 +683,8 @@ class ExtraFields
$this->attribute_label[$tab->name]=$tab->label;
$this->attribute_size[$tab->name]=$tab->size;
$this->attribute_elementtype[$tab->name]=$tab->elementtype;
+ $this->attribute_default[$tab->name]=$tab->fielddefault;
+ $this->attribute_computed[$tab->name]=$tab->fieldcomputed;
$this->attribute_unique[$tab->name]=$tab->fieldunique;
$this->attribute_required[$tab->name]=$tab->fieldrequired;
$this->attribute_param[$tab->name]=($tab->param ? unserialize($tab->param) : '');
@@ -699,6 +726,8 @@ class ExtraFields
$type =$this->attribute_type[$key];
$size =$this->attribute_size[$key];
$elementtype=$this->attribute_elementtype[$key];
+ $default=$this->attribute_default[$key];
+ $computed=$this->attribute_computed[$key];
$unique=$this->attribute_unique[$key];
$required=$this->attribute_required[$key];
$param=$this->attribute_param[$key];
@@ -706,6 +735,8 @@ class ExtraFields
$list=$this->attribute_list[$key];
$hidden=$this->attribute_hidden[$key];
+ if ($computed) return ''.$langs->trans("AutomaticallyCalculated").'';
+
if (empty($showsize))
{
if ($type == 'date')
@@ -1218,10 +1249,12 @@ class ExtraFields
{
global $conf,$langs;
+ $elementtype=$this->attribute_elementtype[$key];
$label=$this->attribute_label[$key];
$type=$this->attribute_type[$key];
$size=$this->attribute_size[$key];
- $elementtype=$this->attribute_elementtype[$key];
+ $default=$this->attribute_default[$key];
+ $computed=$this->attribute_computed[$key];
$unique=$this->attribute_unique[$key];
$required=$this->attribute_required[$key];
$params=$this->attribute_param[$key];
@@ -1229,6 +1262,14 @@ class ExtraFields
$list=$this->attribute_list[$key];
$hidden=$this->attribute_hidden[$key]; // warning, do not rely on this. If your module need a hidden data, it must use its own table.
+ // If field is a computed field, value must become result of compute
+ if ($computed)
+ {
+ // Make the eval of compute string
+ //var_dump($computed);
+ $value = dol_eval($computed, 1, 0);
+ }
+
$showsize=0;
if ($type == 'date')
{
diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php
index e8ead0ed829..9ecfdc037f3 100644
--- a/htdocs/core/lib/functions.lib.php
+++ b/htdocs/core/lib/functions.lib.php
@@ -5604,20 +5604,32 @@ function verifCond($strRights)
*
* @param string $s String to evaluate
* @param int $returnvalue 0=No return (used to execute eval($a=something)). 1=Value of eval is returned (used to eval($something)).
+ * @param int $hideerrors 1=Hide errors
* @return mixed Nothing or return of eval
*/
-function dol_eval($s,$returnvalue=0)
+function dol_eval($s, $returnvalue=0, $hideerrors=1)
{
// Only global variables can be changed by eval function and returned to caller
- global $langs, $user, $conf;
- global $leftmenu;
+ global $db, $langs, $user, $conf;
+ global $mainmenu, $leftmenu;
global $rights;
global $object;
- global $soc;
+ global $mysoc;
+
+ global $obj; // To get $obj used into list when dol_eval is used for computed fields and $obj is not yet $object
+ global $soc; // For backward compatibility
//print $s." \n";
- if ($returnvalue) return @eval('return '.$s.';');
- else @eval($s);
+ if ($returnvalue)
+ {
+ if ($hideerrors) return @eval('return '.$s.';');
+ else return eval('return '.$s.';');
+ }
+ else
+ {
+ if ($hideerrors) @eval($s);
+ else eval($s);
+ }
}
/**
diff --git a/htdocs/core/tpl/admin_extrafields_add.tpl.php b/htdocs/core/tpl/admin_extrafields_add.tpl.php
index 9e4fd464ee6..62821372e56 100644
--- a/htdocs/core/tpl/admin_extrafields_add.tpl.php
+++ b/htdocs/core/tpl/admin_extrafields_add.tpl.php
@@ -18,10 +18,12 @@
*/
/**
- * The following vars must be defined
+ * The following vars must be defined:
* $type2label
* $form
* $conf, $lang,
+ * The following vars may also be defined:
+ * $elementtype
*/
?>
@@ -31,15 +33,16 @@
jQuery(document).ready(function() {
function init_typeoffields(type)
{
- console.log("select new type "+type);
+ console.log("selected type is "+type);
var size = jQuery("#size");
+ var computed_value = jQuery("#computed_value");
+ var default_value = jQuery("#default_value");
var unique = jQuery("#unique");
var required = jQuery("#required");
- var default_value = jQuery("#default_value");
var alwayseditable = jQuery("#alwayseditable");
var list = jQuery("#list");
+ // Case of computed field
+ console.log(type);
+ if (type == '' || type == 'varchar' || type == 'int' || type == 'double' || type == 'price') {
+ jQuery("tr.extra_computed_value").show();
+ } else {
+ computed_value.val(''); jQuery("tr.extra_computed_value").hide();
+ }
+ if (computed_value.val())
+ {
+ console.log("We enter a computed formula");
+ jQuery("#default_value").val('');
+ /* jQuery("#unique, #required, #alwayseditable, #ishidden, #list").removeAttr('checked'); */
+ jQuery("#default_value, #unique, #required, #alwayseditable, #ishidden, #list").attr('disabled', true);
+ jQuery("tr.extra_default_value, tr.extra_unique, tr.extra_required, tr.extra_alwayseditable, tr.extra_ishidden, tr.extra_list").hide();
+ }
+ else
+ {
+ console.log("No computed formula");
+ jQuery("#default_value, #unique, #required, #alwayseditable, #ishidden, #list").attr('disabled', false);
+ jQuery("tr.extra_default_value, tr.extra_unique, tr.extra_required, tr.extra_alwayseditable, tr.extra_ishidden, tr.extra_list").show();
+ }
+
if (type == 'date') { size.val('').prop('disabled', true); unique.removeAttr('disabled'); jQuery("#value_choice").hide();jQuery("#helpchkbxlst").hide(); }
else if (type == 'datetime') { size.val('').prop('disabled', true); unique.removeAttr('disabled'); jQuery("#value_choice").hide(); jQuery("#helpchkbxlst").hide();}
else if (type == 'double') { size.val('24,8').removeAttr('disabled'); unique.removeAttr('disabled'); jQuery("#value_choice").hide(); jQuery("#helpchkbxlst").hide();}
@@ -65,7 +90,10 @@
else if (type == 'checkbox') { size.val('').prop('disabled', true); unique.removeAttr('checked').prop('disabled', true); jQuery("#value_choice").show();jQuery("#helpselect").show();jQuery("#helpsellist").hide();jQuery("#helpchkbxlst").hide();jQuery("#helplink").hide();}
else if (type == 'chkbxlst') { size.val('').prop('disabled', true); unique.removeAttr('checked').prop('disabled', true); jQuery("#value_choice").show();jQuery("#helpselect").hide();jQuery("#helpsellist").hide();jQuery("#helpchkbxlst").show();jQuery("#helplink").hide();}
else if (type == 'link') { size.val('').prop('disabled', true); unique.removeAttr('disabled'); jQuery("#value_choice").show();jQuery("#helpselect").hide();jQuery("#helpsellist").hide();jQuery("#helpchkbxlst").hide();jQuery("#helplink").show();}
- else if (type == 'separate') { size.val('').prop('disabled', true); unique.removeAttr('checked').prop('disabled', true); required.val('').prop('disabled', true); default_value.val('').prop('disabled', true); jQuery("#value_choice").hide();jQuery("#helpselect").hide();jQuery("#helpsellist").hide();jQuery("#helpchkbxlst").hide();jQuery("#helplink").hide();}
+ else if (type == 'separate') {
+ size.val('').prop('disabled', true); unique.removeAttr('checked').prop('disabled', true); required.val('').prop('disabled', true);
+ jQuery("#value_choice").hide();jQuery("#helpselect").hide();jQuery("#helpsellist").hide();jQuery("#helpchkbxlst").hide();jQuery("#helplink").hide();
+ }
else { // type = string
size.val('').prop('disabled', true);
unique.removeAttr('disabled');
@@ -73,10 +101,12 @@
if (type == 'separate')
{
- unique.removeAttr('checked').prop('disabled', true); required.removeAttr('checked').prop('disabled', true); alwayseditable.removeAttr('checked').prop('disabled', true); list.val('').prop('disabled', true);
+ required.removeAttr('checked').prop('disabled', true); alwayseditable.removeAttr('checked').prop('disabled', true); list.val('').prop('disabled', true);
+ jQuery('#size, #default_value').val('').prop('disabled', true);
}
else
{
+ default_value.removeAttr('disabled');
required.removeAttr('disabled'); alwayseditable.removeAttr('disabled'); list.val('').removeAttr('disabled');
}
}
@@ -84,6 +114,11 @@
jQuery("#type").change(function() {
init_typeoffields($(this).val());
});
+
+ // If we enter a formula, we disable other fields
+ jQuery("#computed_value").keyup(function() {
+ init_typeoffields(jQuery('#type').val());
+ });
});
@@ -103,9 +138,7 @@
selectarray('type',$type2label,GETPOST('type')); ?>
\n";
if (! empty($conf->global->MAIN_CAN_HIDE_EXTRAFIELDS)) print '
'.yn($extrafields->attribute_hidden[$key])."
\n"; // Add hidden option on not working feature. Why hide if user can't see it.
diff --git a/htdocs/install/mysql/migration/5.0.0-6.0.0.sql b/htdocs/install/mysql/migration/5.0.0-6.0.0.sql
index 6c71f279867..f7ed5b7575b 100644
--- a/htdocs/install/mysql/migration/5.0.0-6.0.0.sql
+++ b/htdocs/install/mysql/migration/5.0.0-6.0.0.sql
@@ -34,6 +34,10 @@
-- VMYSQL4.3 ALTER TABLE llx_opensurvey_sondage MODIFY COLUMN date_fin DATETIME NULL DEFAULT NULL;
-- VPGSQL8.2 ALTER TABLE llx_opensurvey_sondage ALTER COLUMN date_fin DROP NOT NULL;
+
+ALTER TABLE llx_extrafields ADD COLUMN fieldcomputed text;
+ALTER TABLE llx_extrafields ADD COLUMN fielddefault varchar(255);
+
ALTER TABLE llx_opensurvey_sondage MODIFY COLUMN tms timestamp DEFAULT CURRENT_TIMESTAMP;
ALTER TABLE llx_opensurvey_sondage ADD COLUMN fk_user_creat integer NOT NULL DEFAULT 0;
@@ -251,7 +255,6 @@ ALTER TABLE llx_product_fournisseur_price_log ADD COLUMN multicurrency_tx d
ALTER TABLE llx_product_fournisseur_price_log ADD COLUMN multicurrency_price double(24,8) DEFAULT NULL;
ALTER TABLE llx_product_fournisseur_price_log ADD COLUMN multicurrency_price_ttc double(24,8) DEFAULT NULL;
-
create table llx_payment_various
(
rowid integer AUTO_INCREMENT PRIMARY KEY,
diff --git a/htdocs/install/mysql/tables/llx_extrafields.sql b/htdocs/install/mysql/tables/llx_extrafields.sql
index 2cc144db092..aee51524e6f 100644
--- a/htdocs/install/mysql/tables/llx_extrafields.sql
+++ b/htdocs/install/mysql/tables/llx_extrafields.sql
@@ -21,12 +21,14 @@ create table llx_extrafields
(
rowid integer AUTO_INCREMENT PRIMARY KEY,
name varchar(64) NOT NULL, -- name of field into extrafields tables
- entity integer DEFAULT 1 NOT NULL, -- multi company id
- elementtype varchar(64) NOT NULL DEFAULT 'member', -- for which element this extra fields is for
+ entity integer DEFAULT 1 NOT NULL, -- multi company id
+ elementtype varchar(64) NOT NULL DEFAULT 'member', -- for which element this extra fields is for
tms timestamp, -- date of last update
label varchar(255) NOT NULL, -- label to show for attribute
type varchar(8),
size varchar(8) DEFAULT NULL,
+ fieldcomputed text,
+ fielddefault varchar(255),
fieldunique integer DEFAULT 0,
fieldrequired integer DEFAULT 0,
perms varchar(255), -- not used yet
diff --git a/htdocs/langs/en_US/admin.lang b/htdocs/langs/en_US/admin.lang
index d25a97c28e5..fe547931908 100644
--- a/htdocs/langs/en_US/admin.lang
+++ b/htdocs/langs/en_US/admin.lang
@@ -375,19 +375,21 @@ Int=Integer
Float=Float
DateAndTime=Date and hour
Unique=Unique
-Boolean=Boolean (Checkbox)
+Boolean=Boolean (one checkbox)
ExtrafieldPhone = Phone
ExtrafieldPrice = Price
ExtrafieldMail = Email
ExtrafieldUrl = Url
ExtrafieldSelect = Select list
ExtrafieldSelectList = Select from table
-ExtrafieldSeparator=Separator
+ExtrafieldSeparator=Separator (not a field)
ExtrafieldPassword=Password
-ExtrafieldCheckBox=Checkbox
-ExtrafieldRadio=Radio button
-ExtrafieldCheckBoxFromList= Checkbox from table
+ExtrafieldRadio=Radio buttons (on choice only)
+ExtrafieldCheckBox=Checkboxes
+ExtrafieldCheckBoxFromList=Checkboxes from table
ExtrafieldLink=Link to an object
+ComputedFormula=Computed field
+ComputedFormulaDesc=You can enter here a formula using other properties of object or any PHP coding to get a dynamic computed value. You can use any PHP compatible formulas including the "?" condition operator, and following global object: $db, $conf, $langs, $mysoc, $user, $object. WARNING: Only some properties of $object may be available. If you need a properties not loaded, just fetch yourself the object into your formula like in the second example. Using a computed field means you can't enter yourself any value from interface. Also, if there is a syntax error, the formula may return nothing.
Example of formula: $object->id < 5 ? round($object->id / 2, 2) : ($object->id + 2*$user->id) * (int) substr($mysoc->zip, 1, 2)
Example of formula to force load of object and its parent object: (($reloadedobj = new Task($db)) && ($reloadedobj->fetch($object->id) > 0) && ($secondloadedobj = new Project($db)) && ($secondloadedobj->fetch($reloadedobj->fk_project) > 0)) ? $secondloadedobj->ref : 'Parent project not found'
Other example to reload object (($reloadedobj = new Societe($db)) && ($reloadedobj->fetch($obj->id ? $obj->id : ($obj->rowid ? $obj->rowid : $object->id)) > 0)) ? round($reloadedobj->capital / 5) : '-1'
ExtrafieldParamHelpselect=Parameters list have to be like key,value
for example : 1,value1 2,value2 3,value3 ...
In order to have the list depending on another complementary attribute list : 1,value1|options_parent_list_code:parent_key 2,value2|options_parent_list_code:parent_key
In order to have the list depending on another list : 1,value1|parent_list_code:parent_key 2,value2|parent_list_code:parent_key
ExtrafieldParamHelpcheckbox=Parameters list have to be like key,value
for example : 1,value1 2,value2 3,value3 ...
ExtrafieldParamHelpradio=Parameters list have to be like key,value
for example : 1,value1 2,value2 3,value3 ...
diff --git a/htdocs/langs/en_US/main.lang b/htdocs/langs/en_US/main.lang
index 77b5af78f81..a170bc3f6e1 100644
--- a/htdocs/langs/en_US/main.lang
+++ b/htdocs/langs/en_US/main.lang
@@ -773,6 +773,7 @@ BulkActions=Bulk actions
ClickToShowHelp=Click to show tooltip help
HR=HR
HRAndBank=HR and Bank
+AutomaticallyCalculated=Automatically calculated
# Week day
Monday=Monday
Tuesday=Tuesday
diff --git a/htdocs/modulebuilder/skeletons/skeleton_list.php b/htdocs/modulebuilder/skeletons/skeleton_list.php
index 9c258162759..47aeda79fdc 100644
--- a/htdocs/modulebuilder/skeletons/skeleton_list.php
+++ b/htdocs/modulebuilder/skeletons/skeleton_list.php
@@ -126,13 +126,7 @@ if (is_array($extrafields->attribute_label) && count($extrafields->attribute_lab
}
-// Load object if id or ref is provided as parameter
$object=new Skeleton_Class($db);
-if (($id > 0 || ! empty($ref)) && $action != 'add')
-{
- $result=$object->fetch($id,$ref);
- if ($result < 0) dol_print_error($db);
-}
@@ -354,7 +348,9 @@ if (is_array($extrafields->attribute_label) && count($extrafields->attribute_lab
if (! empty($arrayfields["ef.".$key]['checked']))
{
$align=$extrafields->getAlignFlag($key);
- print_liste_field_titre($langs->trans($extralabels[$key]),$_SERVER["PHP_SELF"],"ef.".$key,"",$param,($align?'align="'.$align.'"':''),$sortfield,$sortorder);
+ $sortonfield = "ef.".$key;
+ if (! empty($extrafields->attribute_computed[$key])) $sortonfield='';
+ print_liste_field_titre($langs->trans($extralabels[$key]),$_SERVER["PHP_SELF"],$sortonfield,"",$param,($align?'align="'.$align.'"':''),$sortfield,$sortorder);
}
}
}
@@ -383,7 +379,7 @@ if (is_array($extrafields->attribute_label) && count($extrafields->attribute_lab
$align=$extrafields->getAlignFlag($key);
$typeofextrafield=$extrafields->attribute_type[$key];
print '
'."\n";
+// Detect if we need a fetch on each output line
+$needToFetchEachLine=0;
+foreach ($extrafields->attribute_computed as $key => $val)
+{
+ if (preg_match('/\$object/',$val)) $needToFetchEachLine++; // There is at least one compute field that use $object
+}
+
+
$i=0;
$totalarray=array();
while ($i < min($num, $limit))
diff --git a/htdocs/projet/list.php b/htdocs/projet/list.php
index 6230626a320..b327573e3db 100644
--- a/htdocs/projet/list.php
+++ b/htdocs/projet/list.php
@@ -141,6 +141,8 @@ if (is_array($extrafields->attribute_label) && count($extrafields->attribute_lab
}
}
+$object = new Project($db);
+
/*
* Actions
@@ -200,7 +202,6 @@ if (empty($reshook))
* View
*/
-$projectstatic = new Project($db);
$socstatic = new Societe($db);
$form = new Form($db);
$formother = new FormOther($db);
@@ -212,12 +213,12 @@ $title=$langs->trans("Projects");
// Get list of project id allowed to user (in a string list separated by coma)
$projectsListId='';
-if (! $user->rights->projet->all->lire) $projectsListId = $projectstatic->getProjectsAuthorizedForUser($user,0,1,$socid);
+if (! $user->rights->projet->all->lire) $projectsListId = $object->getProjectsAuthorizedForUser($user,0,1,$socid);
// Get id of types of contacts for projects (This list never contains a lot of elements)
$listofprojectcontacttype=array();
$sql = "SELECT ctc.rowid, ctc.code FROM ".MAIN_DB_PREFIX."c_type_contact as ctc";
-$sql.= " WHERE ctc.element = '" . $projectstatic->element . "'";
+$sql.= " WHERE ctc.element = '" . $object->element . "'";
$sql.= " AND ctc.source = 'internal'";
$resql = $db->query($sql);
if ($resql)
@@ -232,7 +233,7 @@ if (count($listofprojectcontacttype) == 0) $listofprojectcontacttype[0]='0';
$distinct='DISTINCT'; // We add distinct until we are added a protection to be sure a contact of a project and task is only once.
-$sql = "SELECT ".$distinct." p.rowid as projectid, p.ref, p.title, p.fk_statut, p.fk_opp_status, p.public, p.fk_user_creat";
+$sql = "SELECT ".$distinct." p.rowid as id, p.ref, p.title, p.fk_statut, p.fk_opp_status, p.public, p.fk_user_creat";
$sql.= ", p.datec as date_creation, p.dateo as date_start, p.datee as date_end, p.opp_amount, p.opp_percent, p.tms as date_update, p.budget_amount";
$sql.= ", s.nom as name, s.rowid as socid";
$sql.= ", cls.code as opp_status_code";
@@ -360,8 +361,7 @@ $arrayofselected=is_array($toselect)?$toselect:array();
if ($num == 1 && ! empty($conf->global->MAIN_SEARCH_DIRECT_OPEN_IF_ONLY_ONE) && $search_all)
{
$obj = $db->fetch_object($resql);
- $id = $obj->projectid;
- header("Location: ".DOL_URL_ROOT.'/projet/card.php?id='.$id);
+ header("Location: ".DOL_URL_ROOT.'/projet/card.php?id='.$obj->id);
exit;
}
@@ -566,7 +566,7 @@ if (is_array($extrafields->attribute_label) && count($extrafields->attribute_lab
$align=$extrafields->getAlignFlag($key);
$typeofextrafield=$extrafields->attribute_type[$key];
print '
';
- if (in_array($typeofextrafield, array('varchar', 'int', 'double', 'select')))
+ if (in_array($typeofextrafield, array('varchar', 'int', 'double', 'select')) && empty($extrafields->attribute_computed[$key]))
{
$crit=$val;
$tmpkey=preg_replace('/search_options_/','',$key);
@@ -599,7 +599,7 @@ if (! empty($arrayfields['p.fk_statut']['checked']))
{
print '
';
@@ -669,8 +671,8 @@ while ($i < min($num,$limit))
if (! empty($arrayfields['p.ref']['checked']))
{
print '
';
- print $projectstatic->getNomUrl(1);
- if ($projectstatic->hasDelay()) print img_warning($langs->trans('Late'));
+ print $object->getNomUrl(1);
+ if ($object->hasDelay()) print img_warning($langs->trans('Late'));
print '
';
if (! $i) $totalarray['nbfield']++;
}
@@ -851,8 +853,7 @@ while ($i < min($num,$limit))
// Status
if (! empty($arrayfields['p.fk_statut']['checked']))
{
- $projectstatic->statut = $obj->fk_statut;
- print '
'.$projectstatic->getLibStatut(5).'
';
+ print '
'.$object->getLibStatut(5).'
';
if (! $i) $totalarray['nbfield']++;
}
// Action column
@@ -860,8 +861,8 @@ while ($i < min($num,$limit))
if ($massactionbutton || $massaction) // If we are in select mode (massactionbutton defined) or if we have already selected and sent an action ($massaction) defined
{
$selected=0;
- if (in_array($obj->projectid, $arrayofselected)) $selected=1;
- print '';
+ if (in_array($obj->id, $arrayofselected)) $selected=1;
+ print '';
}
print '';
if (! $i) $totalarray['nbfield']++;
@@ -871,7 +872,6 @@ while ($i < min($num,$limit))
}
$i++;
-
}
// Show total line
diff --git a/htdocs/projet/tasks/list.php b/htdocs/projet/tasks/list.php
index 1f2459e85db..4e31434dbc4 100644
--- a/htdocs/projet/tasks/list.php
+++ b/htdocs/projet/tasks/list.php
@@ -131,6 +131,8 @@ if (is_array($extrafields->attribute_label) && count($extrafields->attribute_lab
}
}
+$object = new Task($db);
+
/*
* Actions
@@ -190,11 +192,11 @@ if (empty($search_projectstatus) && $search_projectstatus == '') $search_project
* View
*/
+$now = dol_now();
$form=new Form($db);
$formother=new FormOther($db);
$socstatic=new Societe($db);
$projectstatic = new Project($db);
-$taskstatic = new Task($db);
$puser=new User($db);
$tuser=new User($db);
if ($search_project_user > 0) $puser->fetch($search_project_user);
@@ -231,7 +233,7 @@ if (count($listofprojectcontacttype) == 0) $listofprojectcontacttype[0]='0';
// Get id of types of contacts for tasks (This list never contains a lot of elements)
$listoftaskcontacttype=array();
$sql = "SELECT ctc.rowid, ctc.code FROM ".MAIN_DB_PREFIX."c_type_contact as ctc";
-$sql.= " WHERE ctc.element = '" . $taskstatic->element . "'";
+$sql.= " WHERE ctc.element = '" . $object->element . "'";
$sql.= " AND ctc.source = 'internal'";
$resql = $db->query($sql);
if ($resql)
@@ -549,7 +551,7 @@ if (is_array($extrafields->attribute_label) && count($extrafields->attribute_lab
$align=$extrafields->getAlignFlag($key);
$typeofextrafield=$extrafields->attribute_type[$key];
print '