123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710 |
- <?php
- /* Copyright (C) 2002-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
- * Copyright (C) 2002-2003 Jean-Louis Bergamo <jlb@j1b.org>
- * Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
- * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
- * Copyright (C) 2009-2012 Laurent Destailleur <eldy@users.sourceforge.net>
- * Copyright (C) 2009-2012 Regis Houssin <regis.houssin@capnetworks.com>
- * Copyright (C) 2013 Florian Henry <forian.henry@open-concept.pro>
- * Copyright (C) 2015 Charles-Fr BENKE <charles.fr@benke.fr>
- * Copyright (C) 2016 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
- *
- * 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
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- /**
- * \file htdocs/core/class/extrafields.class.php
- * \ingroup core
- * \brief File of class to manage extra fields
- */
- /**
- * Class to manage standard extra fields
- */
- class ExtraFields
- {
- var $db;
- // type of element (for what object is the extrafield)
- var $attribute_elementtype;
-
- // Array with type of the extra field
- var $attribute_type;
- // Array with label of extra field
- var $attribute_label;
- // Array with size of extra field
- var $attribute_size;
- // 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
- var $attribute_required;
- // Array to store parameters of attribute (used in select type)
- var $attribute_param;
- // Array to store position of attribute
- var $attribute_pos;
- // Array to store if attribute is editable regardless of the document status
- var $attribute_alwayseditable;
- // Array to store permission to check
- var $attribute_perms;
- // Array to store permission to check
- var $attribute_list;
- // Array to store if extra field is hidden
- var $attribute_hidden; // warning, do not rely on this. If your module need a hidden data, it must use its own table.
-
- var $error;
- var $errno;
- public static $type2label=array(
- 'varchar'=>'String',
- 'text'=>'TextLong',
- 'int'=>'Int',
- 'double'=>'Float',
- 'date'=>'Date',
- 'datetime'=>'DateAndTime',
- 'boolean'=>'Boolean',
- 'price'=>'ExtrafieldPrice',
- 'phone'=>'ExtrafieldPhone',
- 'mail'=>'ExtrafieldMail',
- 'url'=>'ExtrafieldUrl',
- 'password' => 'ExtrafieldPassword',
- 'select' => 'ExtrafieldSelect',
- 'sellist' => 'ExtrafieldSelectList',
- 'radio' => 'ExtrafieldRadio',
- 'checkbox' => 'ExtrafieldCheckBox',
- 'chkbxlst' => 'ExtrafieldCheckBoxFromList',
- 'link' => 'ExtrafieldLink',
- 'separate' => 'ExtrafieldSeparator',
- );
-
- /**
- * Constructor
- *
- * @param DoliDB $db Database handler
- */
- function __construct($db)
- {
- $this->db = $db;
- $this->error = array();
- $this->attribute_elementtype = array();
- $this->attribute_type = array();
- $this->attribute_label = array();
- $this->attribute_size = array();
- $this->attribute_computed = array();
- $this->attribute_default = array();
- $this->attribute_unique = array();
- $this->attribute_required = array();
- $this->attribute_perms = array();
- $this->attribute_list = array();
- $this->attribute_hidden = array();
- }
- /**
- * Add a new extra field parameter
- *
- * @param string $attrname Code of attribute
- * @param string $label label of attribute
- * @param int $type Type of attribute ('boolean', 'int', 'text', 'varchar', 'date', 'datehour','price','phone','mail','password','url','select','checkbox', ...)
- * @param int $pos Position of attribute
- * @param string $size Size/length of attribute
- * @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 (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, $computed='')
- {
- if (empty($attrname)) return -1;
- if (empty($label)) return -1;
- if ($elementtype == 'thirdparty') $elementtype='societe';
- if ($elementtype == 'contact') $elementtype='socpeople';
- // 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, $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, $default, $computed);
- $err2=$this->errno;
- if ($result2 > 0 || ($err1 == 'DB_ERROR_COLUMN_ALREADY_EXISTS' && $err2 == 'DB_ERROR_RECORD_ALREADY_EXISTS'))
- {
- $this->error='';
- $this->errno=0;
- return 1;
- }
- else return -2;
- }
- else
- {
- return -1;
- }
- }
- /**
- * Add a new optional attribute.
- * This is a private method. For public method, use addExtraField.
- *
- * @param string $attrname code of attribute
- * @param int $type Type of attribute ('boolean', 'int', 'text', 'varchar', 'date', 'datehour','price','phone','mail','password','url','select','checkbox', ...)
- * @param string $length Size/length of attribute ('5', '24,8', ...)
- * @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 (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, $computed='')
- {
- if ($elementtype == 'thirdparty') $elementtype='societe';
- if ($elementtype == 'contact') $elementtype='socpeople';
- $table=$elementtype.'_extrafields';
- if (! empty($attrname) && preg_match("/^\w[a-zA-Z0-9_]*$/",$attrname) && ! is_numeric($attrname))
- {
- if ($type=='boolean') {
- $typedb='int';
- $lengthdb='1';
- } elseif($type=='price') {
- $typedb='double';
- $lengthdb='24,8';
- } elseif($type=='phone') {
- $typedb='varchar';
- $lengthdb='20';
- } elseif($type=='mail') {
- $typedb='varchar';
- $lengthdb='128';
- } elseif($type=='url') {
- $typedb='varchar';
- $lengthdb='255';
- } elseif (($type=='select') || ($type=='sellist') || ($type=='radio') ||($type=='checkbox') ||($type=='chkbxlst')){
- $typedb='text';
- $lengthdb='';
- } elseif ($type=='link') {
- $typedb='int';
- $lengthdb='11';
- } elseif($type=='password') {
- $typedb='varchar';
- $lengthdb='50';
- } else {
- $typedb=$type;
- $lengthdb=$length;
- }
- $field_desc = array(
- 'type'=>$typedb,
- 'value'=>$lengthdb,
- 'null'=>($required?'NOT NULL':'NULL'),
- 'default' => $default_value
- );
- $result=$this->db->DDLAddField(MAIN_DB_PREFIX.$table, $attrname, $field_desc);
- if ($result > 0)
- {
- if ($unique)
- {
- $sql="ALTER TABLE ".MAIN_DB_PREFIX.$table." ADD UNIQUE INDEX uk_".$table."_".$attrname." (".$attrname.")";
- $resql=$this->db->query($sql,1,'dml');
- }
- return 1;
- }
- else
- {
- $this->error=$this->db->lasterror();
- $this->errno=$this->db->lasterrno();
- return -1;
- }
- }
- else
- {
- return 0;
- }
- }
- /**
- * Add description of a new optional attribute
- *
- * @param string $attrname code of attribute
- * @param string $label label of attribute
- * @param int $type Type of attribute ('int', 'text', 'varchar', 'date', 'datehour', 'float')
- * @param int $pos Position of attribute
- * @param string $size Size/length of attribute ('5', '24,8', ...)
- * @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 array||string $param Params for field (ex for select list : array('options' => array(value'=>'label of option')) )
- * @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 $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, $default='', $computed='')
- {
- global $conf;
- if ($elementtype == 'thirdparty') $elementtype='societe';
- if ($elementtype == 'contact') $elementtype='socpeople';
- // Clean parameters
- if (empty($pos)) $pos=0;
- if (empty($list)) $list=0;
- if (! empty($attrname) && preg_match("/^\w[a-zA-Z0-9-_]*$/",$attrname) && ! is_numeric($attrname))
- {
- if(is_array($param) && count($param) > 0)
- {
- $params = $this->db->escape(serialize($param));
- }
- elseif (strlen($param) > 0)
- {
- $params = trim($param);
- }
- else
- {
- $params='';
- }
- $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."',";
- $sql.= " '".$pos."',";
- $sql.= " '".$size."',";
- $sql.= " ".$conf->entity.",";
- $sql.= " '".$elementtype."',";
- $sql.= " '".$unique."',";
- $sql.= " '".$required."',";
- $sql.= " '".$params."',";
- $sql.= " '".$alwayseditable."',";
- $sql.= " ".($perms?"'".$this->db->escape($perms)."'":"null").",";
- $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);
- if ($this->db->query($sql))
- {
- return 1;
- }
- else
- {
- $this->error=$this->db->lasterror();
- $this->errno=$this->db->lasterrno();
- return -1;
- }
- }
- }
- /**
- * Delete an optional attribute
- *
- * @param string $attrname Code of attribute to delete
- * @param string $elementtype Element type ('member', 'product', 'thirdparty', 'contact', ...)
- * @return int < 0 if KO, 0 if nothing is done, 1 if OK
- */
- function delete($attrname, $elementtype='member')
- {
- if ($elementtype == 'thirdparty') $elementtype='societe';
- if ($elementtype == 'contact') $elementtype='socpeople';
- $table=$elementtype.'_extrafields';
- $error=0;
-
- if (! empty($attrname) && preg_match("/^\w[a-zA-Z0-9-_]*$/",$attrname))
- {
- $result=$this->delete_label($attrname,$elementtype);
- if ($result < 0)
- {
- $this->error=$this->db->lasterror();
- $error++;
- }
- if (! $error)
- {
- $sql = "SELECT COUNT(rowid) as nb";
- $sql.= " FROM ".MAIN_DB_PREFIX."extrafields";
- $sql.= " WHERE elementtype = '".$elementtype."'";
- $sql.= " AND name = '".$attrname."'";
- //$sql.= " AND entity IN (0,".$conf->entity.")"; Do not test on entity here. We want to see if there is still on field remaning in other entities before deleting field in table
- $resql = $this->db->query($sql);
- if ($resql)
- {
- $obj = $this->db->fetch_object($resql);
- if ($obj->nb <= 0)
- {
- $result=$this->db->DDLDropField(MAIN_DB_PREFIX.$table,$attrname); // This also drop the unique key
- if ($result < 0)
- {
- $this->error=$this->db->lasterror();
- $error++;
- }
- }
- }
- }
-
- return $result;
- }
- else
- {
- return 0;
- }
- }
- /**
- * Delete description of an optional attribute
- *
- * @param string $attrname Code of attribute to delete
- * @param string $elementtype Element type ('member', 'product', 'thirdparty', ...)
- * @return int < 0 if KO, 0 if nothing is done, 1 if OK
- */
- private function delete_label($attrname, $elementtype='member')
- {
- global $conf;
- if ($elementtype == 'thirdparty') $elementtype='societe';
- if ($elementtype == 'contact') $elementtype='socpeople';
- if (isset($attrname) && $attrname != '' && preg_match("/^\w[a-zA-Z0-9-_]*$/",$attrname))
- {
- $sql = "DELETE FROM ".MAIN_DB_PREFIX."extrafields";
- $sql.= " WHERE name = '".$attrname."'";
- $sql.= " AND entity IN (0,".$conf->entity.')';
- $sql.= " AND elementtype = '".$elementtype."'";
- dol_syslog(get_class($this)."::delete_label", LOG_DEBUG);
- $resql=$this->db->query($sql);
- if ($resql)
- {
- return 1;
- }
- else
- {
- print dol_print_error($this->db);
- return -1;
- }
- }
- else
- {
- return 0;
- }
- }
- /**
- * Modify type of a personalized attribute
- *
- * @param string $attrname Name of attribute
- * @param string $label Label of attribute
- * @param string $type Type of attribute
- * @param int $length Length of attribute
- * @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 int $pos Position of attribute
- * @param array $param Params for field (ex for select list : array('options' => array(value'=>'label of option')) )
- * @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 $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,$default='',$computed='')
- {
- if ($elementtype == 'thirdparty') $elementtype='societe';
- if ($elementtype == 'contact') $elementtype='socpeople';
- $table=$elementtype.'_extrafields';
- if (isset($attrname) && $attrname != '' && preg_match("/^\w[a-zA-Z0-9-_]*$/",$attrname))
- {
- if ($type=='boolean') {
- $typedb='int';
- $lengthdb='1';
- } elseif($type=='price') {
- $typedb='double';
- $lengthdb='24,8';
- } elseif($type=='phone') {
- $typedb='varchar';
- $lengthdb='20';
- } elseif($type=='mail') {
- $typedb='varchar';
- $lengthdb='128';
- } elseif($type=='url') {
- $typedb='varchar';
- $lengthdb='255';
- } elseif (($type=='select') || ($type=='sellist') || ($type=='radio') || ($type=='checkbox') || ($type=='chkbxlst')) {
- $typedb='text';
- $lengthdb='';
- } elseif ($type=='link') {
- $typedb='int';
- $lengthdb='11';
- } elseif($type=='password') {
- $typedb='varchar';
- $lengthdb='50';
- } else {
- $typedb=$type;
- $lengthdb=$length;
- }
- $field_desc = array('type'=>$typedb, 'value'=>$lengthdb, 'null'=>($required?'NOT NULL':'NULL'));
- if ($type != 'separate') // No table update when separate type
- {
- $result=$this->db->DDLUpdateField(MAIN_DB_PREFIX.$table, $attrname, $field_desc);
- }
- if ($result > 0 || $type == 'separate')
- {
- if ($label)
- {
- $result=$this->update_label($attrname,$label,$type,$length,$elementtype,$unique,$required,$pos,$param,$alwayseditable,$perms,$list,$ishidden,$default,$computed);
- }
- if ($result > 0)
- {
- $sql='';
- if ($unique)
- {
- $sql="ALTER TABLE ".MAIN_DB_PREFIX.$table." ADD UNIQUE INDEX uk_".$table."_".$attrname." (".$attrname.")";
- }
- else
- {
- $sql="ALTER TABLE ".MAIN_DB_PREFIX.$table." DROP INDEX uk_".$table."_".$attrname;
- }
- dol_syslog(get_class($this).'::update', LOG_DEBUG);
- $resql=$this->db->query($sql,1,'dml');
- return 1;
- }
- else
- {
- $this->error=$this->db->lasterror();
- return -1;
- }
- }
- else
- {
- $this->error=$this->db->lasterror();
- return -1;
- }
- }
- else
- {
- return 0;
- }
- }
- /**
- * Modify description of personalized attribute
- *
- * @param string $attrname Name of attribute
- * @param string $label Label of attribute
- * @param string $type Type of attribute
- * @param int $size Length of attribute
- * @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 int $pos Position of attribute
- * @param array $param Params for field (ex for select list : array('options' => array(value'=>'label of option')) )
- * @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 $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,$default='',$computed='')
- {
- global $conf;
- 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';
- if ($elementtype == 'contact') $elementtype='socpeople';
- if (empty($list)) $list=0;
- if (isset($attrname) && $attrname != '' && preg_match("/^\w[a-zA-Z0-9-_]*$/",$attrname))
- {
- $this->db->begin();
- if(is_array($param) && count($param) > 0)
- {
- $param = $this->db->escape(serialize($param));
- }
- $sql_del = "DELETE FROM ".MAIN_DB_PREFIX."extrafields";
- $sql_del.= " WHERE name = '".$attrname."'";
- $sql_del.= " AND entity = ".$conf->entity;
- $sql_del.= " AND elementtype = '".$elementtype."'";
- $resql1=$this->db->query($sql_del);
- $sql = "INSERT INTO ".MAIN_DB_PREFIX."extrafields(";
- $sql.= " name,"; // This is code
- $sql.= " entity,";
- $sql.= " label,";
- $sql.= " type,";
- $sql.= " size,";
- $sql.= " elementtype,";
- $sql.= " fieldunique,";
- $sql.= " fieldrequired,";
- $sql.= " perms,";
- $sql.= " pos,";
- $sql.= " alwayseditable,";
- $sql.= " param,";
- $sql.= " list,";
- $sql.= " ishidden,";
- $sql.= " fielddefault,";
- $sql.= " fieldcomputed";
- $sql.= ") VALUES (";
- $sql.= "'".$attrname."',";
- $sql.= " ".$conf->entity.",";
- $sql.= " '".$this->db->escape($label)."',";
- $sql.= " '".$type."',";
- $sql.= " '".$size."',";
- $sql.= " '".$elementtype."',";
- $sql.= " '".$unique."',";
- $sql.= " '".$required."',";
- $sql.= " ".($perms?"'".$this->db->escape($perms)."'":"null").",";
- $sql.= " '".$pos."',";
- $sql.= " '".$alwayseditable."',";
- $sql.= " '".$param."',";
- $sql.= " ".$list.", ";
- $sql.= " ".$ishidden.", ";
- $sql.= " ".($default?"'".$this->db->escape($default)."'":"null").",";
- $sql.= " ".($computed?"'".$this->db->escape($computed)."'":"null");
- $sql.= ")";
- $resql2=$this->db->query($sql);
- if ($resql1 && $resql2)
- {
- $this->db->commit();
- return 1;
- }
- else
- {
- $this->db->rollback();
- print dol_print_error($this->db);
- return -1;
- }
- }
- else
- {
- return 0;
- }
- }
- /**
- * Load array this->attribute_xxx like attribute_label, attribute_type, ...
- *
- * @param string $elementtype Type of element ('adherent', 'commande', 'thirdparty', 'facture', 'propal', 'product', ...)
- * @param boolean $forceload Force load of extra fields whatever is option MAIN_EXTRAFIELDS_DISABLED
- * @return array Array of attributes for all extra fields
- */
- function fetch_name_optionals_label($elementtype,$forceload=false)
- {
- global $conf;
- if ( empty($elementtype) ) return array();
- if ($elementtype == 'thirdparty') $elementtype='societe';
- if ($elementtype == 'contact') $elementtype='socpeople';
- $array_name_label=array();
- // 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,fielddefault,fieldcomputed";
- $sql.= " FROM ".MAIN_DB_PREFIX."extrafields";
- $sql.= " WHERE entity IN (0,".$conf->entity.")";
- if ($elementtype) $sql.= " AND elementtype = '".$elementtype."'";
- $sql.= " ORDER BY pos";
- $resql=$this->db->query($sql);
- if ($resql)
- {
- if ($this->db->num_rows($resql))
- {
- while ($tab = $this->db->fetch_object($resql))
- {
- // we can add this attribute to adherent object
- if ($tab->type != 'separate')
- {
- $array_name_label[$tab->name]=$tab->label;
- }
- $this->attribute_type[$tab->name]=$tab->type;
- $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) : '');
- $this->attribute_pos[$tab->name]=$tab->pos;
- $this->attribute_alwayseditable[$tab->name]=$tab->alwayseditable;
- $this->attribute_perms[$tab->name]=$tab->perms;
- $this->attribute_list[$tab->name]=$tab->list;
- $this->attribute_hidden[$tab->name]=$tab->ishidden;
- }
- }
- }
- else
- {
- $this->error=$this->db->lasterror();
- dol_syslog(get_class($this)."::fetch_name_optionals_label ".$this->error, LOG_ERR);
- }
- return $array_name_label;
- }
- /**
- * Return HTML string to put an input field into a page
- *
- * @param string $key Key of attribute
- * @param string $value Preselected value to show (for date type it must be in timestamp format)
- * @param string $moreparam To add more parametes on html input tag
- * @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)
- * @param mixed $showsize Value for css to define size. May also be a numeric.
- * @param int $objectid Current object id
- * @return string
- */
- function showInputField($key, $value, $moreparam='', $keyprefix='', $keysuffix='', $showsize=0, $objectid=0)
- {
- global $conf,$langs;
- $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];
- $param=$this->attribute_param[$key];
- $perms=$this->attribute_perms[$key];
- $list=$this->attribute_list[$key];
- $hidden=$this->attribute_hidden[$key];
- if ($computed) return '<span class="opacitymedium">'.$langs->trans("AutomaticallyCalculated").'</span>';
-
- if (empty($showsize))
- {
- if ($type == 'date')
- {
- //$showsize=10;
- $showsize = 'minwidth100imp';
- }
- elseif ($type == 'datetime')
- {
- //$showsize=19;
- $showsize = 'minwidth200imp';
- }
- elseif (in_array($type,array('int','double')))
- {
- //$showsize=10;
- $showsize = 'minwidth100imp';
- }
- elseif ($type == 'url')
- {
- $showsize='minwidth400imp';
- }
- elseif ($type == 'boolean')
- {
- $showsize='';
- }
- else
- {
- if (round($size) < 12)
- {
- $showsize = 'minwidth100imp';
- }
- else if (round($size) <= 48)
- {
- $showsize = 'minwidth200imp';
- }
- else
- {
- //$showsize=48;
- $showsize = 'minwidth400imp';
- }
- }
- }
-
- if (in_array($type,array('date','datetime')))
- {
- $tmp=explode(',',$size);
- $newsize=$tmp[0];
- $showtime = in_array($type,array('datetime')) ? 1 : 0;
- // Do not show current date when field not required (see select_date() method)
- if (!$required && $value == '') $value = '-1';
- require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
- global $form;
- if (! is_object($form)) $form=new Form($this->db);
- // TODO Must also support $moreparam
- $out = $form->select_date($value, $keysuffix.'options_'.$key.$keyprefix, $showtime, $showtime, $required, '', 1, 1, 1, 0, 1);
- }
- elseif (in_array($type,array('int')))
- {
- $tmp=explode(',',$size);
- $newsize=$tmp[0];
- $out='<input type="text" class="flat '.$showsize.' maxwidthonsmartphone" name="'.$keysuffix.'options_'.$key.$keyprefix.'" " maxlength="'.$newsize.'" value="'.$value.'"'.($moreparam?$moreparam:'').'>';
- }
- elseif ($type == 'varchar')
- {
- $out='<input type="text" class="flat '.$showsize.' maxwidthonsmartphone" name="'.$keysuffix.'options_'.$key.$keyprefix.'" maxlength="'.$size.'" value="'.dol_escape_htmltag($value).'"'.($moreparam?$moreparam:'').'>';
- }
- elseif (in_array($type, array('mail', 'phone', 'url')))
- {
- $out='<input type="text" class="flat '.$showsize.' maxwidthonsmartphone" name="'.$keysuffix.'options_'.$key.$keyprefix.'" value="'.$value.'" '.($moreparam?$moreparam:'').'>';
- }
- elseif ($type == 'text')
- {
- require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
- $doleditor=new DolEditor($keysuffix.'options_'.$key.$keyprefix,$value,'',200,'dolibarr_notes','In',false,false,! empty($conf->fckeditor->enabled) && $conf->global->FCKEDITOR_ENABLE_SOCIETE,ROWS_5,'90%');
- $out=$doleditor->Create(1);
- }
- elseif ($type == 'boolean')
- {
- $checked='';
- if (!empty($value)) {
- $checked=' checked value="1" ';
- } else {
- $checked=' value="1" ';
- }
- $out='<input type="checkbox" class="flat '.$showsize.' maxwidthonsmartphone" name="'.$keysuffix.'options_'.$key.$keyprefix.'" '.$checked.' '.($moreparam?$moreparam:'').'>';
- }
- elseif ($type == 'price')
- {
- $out='<input type="text" class="flat '.$showsize.' maxwidthonsmartphone" name="'.$keysuffix.'options_'.$key.$keyprefix.'" value="'.price2num($value).'" '.($moreparam?$moreparam:'').'> '.$langs->getCurrencySymbol($conf->currency);
- }
- elseif ($type == 'double')
- {
- if (!empty($value)) {
- $value=price($value);
- }
- $out='<input type="text" class="flat '.$showsize.' maxwidthonsmartphone" name="'.$keysuffix.'options_'.$key.$keyprefix.'" value="'.$value.'" '.($moreparam?$moreparam:'').'> ';
- }
- elseif ($type == 'select')
- {
- $out = '';
- if (! empty($conf->use_javascript_ajax) && ! empty($conf->global->MAIN_EXTRAFIELDS_USE_SELECT2))
- {
- include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php';
- $out.= ajax_combobox($keysuffix.'options_'.$key.$keyprefix, array(), 0);
- }
- $out.='<select class="flat '.$showsize.' maxwidthonsmartphone" name="'.$keysuffix.'options_'.$key.$keyprefix.'" id="options_'.$key.$keyprefix.'" '.($moreparam?$moreparam:'').'>';
- $out.='<option value="0"> </option>';
- foreach ($param['options'] as $key => $val)
- {
- if ($key == '') continue;
- list($val, $parent) = explode('|', $val);
- $out.='<option value="'.$key.'"';
- $out.= ($value==$key?' selected':'');
- $out.= (!empty($parent)?' parent="'.$parent.'"':'');
- $out.='>'.$val.'</option>';
- }
- $out.='</select>';
- }
- elseif ($type == 'sellist')
- {
- $out = '';
- if (! empty($conf->use_javascript_ajax) && ! empty($conf->global->MAIN_EXTRAFIELDS_USE_SELECT2))
- {
- include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php';
- $out.= ajax_combobox($keysuffix.'options_'.$key.$keyprefix, array(), 0);
- }
- $out.='<select class="flat '.$showsize.' maxwidthonsmartphone" name="'.$keysuffix.'options_'.$key.$keyprefix.'" id="options_'.$key.$keyprefix.'" '.($moreparam?$moreparam:'').'>';
- if (is_array($param['options']))
- {
- $param_list=array_keys($param['options']);
- $InfoFieldList = explode(":", $param_list[0]);
- // 0 : tableName
- // 1 : label field name
- // 2 : key fields name (if differ of rowid)
- // 3 : key field parent (for dependent lists)
- // 4 : where clause filter on column or table extrafield, syntax field='value' or extra.field=value
- $keyList=(empty($InfoFieldList[2])?'rowid':$InfoFieldList[2].' as rowid');
- if (count($InfoFieldList) > 4 && ! empty($InfoFieldList[4]))
- {
- if (strpos($InfoFieldList[4], 'extra.') !== false)
- {
- $keyList='main.'.$InfoFieldList[2].' as rowid';
- } else {
- $keyList=$InfoFieldList[2].' as rowid';
- }
- }
- if (count($InfoFieldList) > 3 && ! empty($InfoFieldList[3]))
- {
- list($parentName, $parentField) = explode('|', $InfoFieldList[3]);
- $keyList.= ', '.$parentField;
- }
- $fields_label = explode('|',$InfoFieldList[1]);
- if (is_array($fields_label))
- {
- $keyList .=', ';
- $keyList .= implode(', ', $fields_label);
- }
- $sqlwhere='';
- $sql = 'SELECT '.$keyList;
- $sql.= ' FROM '.MAIN_DB_PREFIX .$InfoFieldList[0];
- if (!empty($InfoFieldList[4]))
- {
- // can use SELECT request
- if (strpos($InfoFieldList[4], '$SEL$')!==false) {
- $InfoFieldList[4]=str_replace('$SEL$','SELECT',$InfoFieldList[4]);
- }
- // current object id can be use into filter
- if (strpos($InfoFieldList[4], '$ID$')!==false && !empty($objectid)) {
- $InfoFieldList[4]=str_replace('$ID$',$objectid,$InfoFieldList[4]);
- } else {
- $InfoFieldList[4]=str_replace('$ID$','0',$InfoFieldList[4]);
- }
- //We have to join on extrafield table
- if (strpos($InfoFieldList[4], 'extra')!==false)
- {
- $sql.= ' as main, '.MAIN_DB_PREFIX .$InfoFieldList[0].'_extrafields as extra';
- $sqlwhere.= ' WHERE extra.fk_object=main.'.$InfoFieldList[2]. ' AND '.$InfoFieldList[4];
- }
- else
- {
- $sqlwhere.= ' WHERE '.$InfoFieldList[4];
- }
- }
- else
- {
- $sqlwhere.= ' WHERE 1=1';
- }
- // Some tables may have field, some other not. For the moment we disable it.
- if (in_array($InfoFieldList[0],array('tablewithentity')))
- {
- $sqlwhere.= ' AND entity = '.$conf->entity;
- }
- $sql.=$sqlwhere;
- //print $sql;
- $sql .= ' ORDER BY ' . implode(', ', $fields_label);
- dol_syslog(get_class($this).'::showInputField type=sellist', LOG_DEBUG);
- $resql = $this->db->query($sql);
- if ($resql)
- {
- $out.='<option value="0"> </option>';
- $num = $this->db->num_rows($resql);
- $i = 0;
- while ($i < $num)
- {
- $labeltoshow='';
- $obj = $this->db->fetch_object($resql);
- // Several field into label (eq table:code|libelle:rowid)
- $fields_label = explode('|',$InfoFieldList[1]);
- if(is_array($fields_label))
- {
- $notrans = true;
- foreach ($fields_label as $field_toshow)
- {
- $labeltoshow.= $obj->$field_toshow.' ';
- }
- }
- else
- {
- $labeltoshow=$obj->{$InfoFieldList[1]};
- }
- $labeltoshow=dol_trunc($labeltoshow,45);
- if ($value==$obj->rowid)
- {
- foreach ($fields_label as $field_toshow)
- {
- $translabel=$langs->trans($obj->$field_toshow);
- if ($translabel!=$obj->$field_toshow) {
- $labeltoshow=dol_trunc($translabel,18).' ';
- }else {
- $labeltoshow=dol_trunc($obj->$field_toshow,18).' ';
- }
- }
- $out.='<option value="'.$obj->rowid.'" selected>'.$labeltoshow.'</option>';
- }
- else
- {
- if(!$notrans)
- {
- $translabel=$langs->trans($obj->{$InfoFieldList[1]});
- if ($translabel!=$obj->{$InfoFieldList[1]}) {
- $labeltoshow=dol_trunc($translabel,18);
- }
- else {
- $labeltoshow=dol_trunc($obj->{$InfoFieldList[1]},18);
- }
- }
- if (empty($labeltoshow)) $labeltoshow='(not defined)';
- if ($value==$obj->rowid)
- {
- $out.='<option value="'.$obj->rowid.'" selected>'.$labeltoshow.'</option>';
- }
- if (!empty($InfoFieldList[3]))
- {
- $parent = $parentName.':'.$obj->{$parentField};
- }
- $out.='<option value="'.$obj->rowid.'"';
- $out.= ($value==$obj->rowid?' selected':'');
- $out.= (!empty($parent)?' parent="'.$parent.'"':'');
- $out.='>'.$labeltoshow.'</option>';
- }
- $i++;
- }
- $this->db->free($resql);
- }
- else {
- print 'Error in request '.$sql.' '.$this->db->lasterror().'. Check setup of extra parameters.<br>';
- }
- }
- $out.='</select>';
- }
- elseif ($type == 'checkbox')
- {
- require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
- $form = new Form($db);
-
- $value_arr=explode(',',$value);
- $out=$form->multiselectarray($keysuffix.'options_'.$key.$keyprefix, $param['options'], $value_arr, '', 0, '', 0, '100%');
- }
- elseif ($type == 'radio')
- {
- $out='';
- foreach ($param['options'] as $keyopt=>$val )
- {
- $out.='<input class="flat '.$showsize.'" type="radio" name="'.$keysuffix.'options_'.$key.$keyprefix.'" '.($moreparam?$moreparam:'');
- $out.=' value="'.$keyopt.'"';
- $out.=' id="'.$keysuffix.'options_'.$key.$keyprefix.'_'.$keyopt.'"';
- $out.= ($value==$keyopt?'checked':'');
- $out.='/><label for="'.$keysuffix.'options_'.$key.$keyprefix.'_'.$keyopt.'">'.$val.'</label><br>';
- }
- }
- elseif ($type == 'chkbxlst')
- {
- if (is_array($value)) {
- $value_arr = $value;
- }
- else {
- $value_arr = explode(',', $value);
- }
- if (is_array($param['options'])) {
- $param_list = array_keys($param['options']);
- $InfoFieldList = explode(":", $param_list[0]);
- // 0 : tableName
- // 1 : label field name
- // 2 : key fields name (if differ of rowid)
- // 3 : key field parent (for dependent lists)
- // 4 : where clause filter on column or table extrafield, syntax field='value' or extra.field=value
- $keyList = (empty($InfoFieldList[2]) ? 'rowid' : $InfoFieldList[2] . ' as rowid');
- if (count($InfoFieldList) > 3 && ! empty($InfoFieldList[3])) {
- list ( $parentName, $parentField ) = explode('|', $InfoFieldList[3]);
- $keyList .= ', ' . $parentField;
- }
- if (count($InfoFieldList) > 4 && ! empty($InfoFieldList[4])) {
- if (strpos($InfoFieldList[4], 'extra.') !== false) {
- $keyList = 'main.' . $InfoFieldList[2] . ' as rowid';
- } else {
- $keyList = $InfoFieldList[2] . ' as rowid';
- }
- }
- $fields_label = explode('|', $InfoFieldList[1]);
- if (is_array($fields_label)) {
- $keyList .= ', ';
- $keyList .= implode(', ', $fields_label);
- }
- $sqlwhere = '';
- $sql = 'SELECT ' . $keyList;
- $sql .= ' FROM ' . MAIN_DB_PREFIX . $InfoFieldList[0];
- if (! empty($InfoFieldList[4])) {
- // can use SELECT request
- if (strpos($InfoFieldList[4], '$SEL$')!==false) {
- $InfoFieldList[4]=str_replace('$SEL$','SELECT',$InfoFieldList[4]);
- }
- // current object id can be use into filter
- if (strpos($InfoFieldList[4], '$ID$')!==false && !empty($objectid)) {
- $InfoFieldList[4]=str_replace('$ID$',$objectid,$InfoFieldList[4]);
- } else {
- $InfoFieldList[4]=str_replace('$ID$','0',$InfoFieldList[4]);
- }
- // We have to join on extrafield table
- if (strpos($InfoFieldList[4], 'extra') !== false) {
- $sql .= ' as main, ' . MAIN_DB_PREFIX . $InfoFieldList[0] . '_extrafields as extra';
- $sqlwhere .= ' WHERE extra.fk_object=main.' . $InfoFieldList[2] . ' AND ' . $InfoFieldList[4];
- } else {
- $sqlwhere .= ' WHERE ' . $InfoFieldList[4];
- }
- } else {
- $sqlwhere .= ' WHERE 1=1';
- }
- // Some tables may have field, some other not. For the moment we disable it.
- if (in_array($InfoFieldList[0], array ('tablewithentity')))
- {
- $sqlwhere .= ' AND entity = ' . $conf->entity;
- }
- // $sql.=preg_replace('/^ AND /','',$sqlwhere);
- // print $sql;
- $sql .= $sqlwhere;
- dol_syslog(get_class($this) . '::showInputField type=chkbxlst',LOG_DEBUG);
- $resql = $this->db->query($sql);
- if ($resql) {
- $num = $this->db->num_rows($resql);
- $i = 0;
-
- $data=array();
-
- while ( $i < $num ) {
- $labeltoshow = '';
- $obj = $this->db->fetch_object($resql);
- // Several field into label (eq table:code|libelle:rowid)
- $fields_label = explode('|', $InfoFieldList[1]);
- if (is_array($fields_label)) {
- $notrans = true;
- foreach ( $fields_label as $field_toshow ) {
- $labeltoshow .= $obj->$field_toshow . ' ';
- }
- } else {
- $labeltoshow = $obj->{$InfoFieldList[1]};
- }
- $labeltoshow = dol_trunc($labeltoshow, 45);
- if (is_array($value_arr) && in_array($obj->rowid, $value_arr)) {
- foreach ( $fields_label as $field_toshow ) {
- $translabel = $langs->trans($obj->$field_toshow);
- if ($translabel != $obj->$field_toshow) {
- $labeltoshow = dol_trunc($translabel, 18) . ' ';
- } else {
- $labeltoshow = dol_trunc($obj->$field_toshow, 18) . ' ';
- }
- }
-
- $data[$obj->rowid]=$labeltoshow;
-
- } else {
- if (! $notrans) {
- $translabel = $langs->trans($obj->{$InfoFieldList[1]});
- if ($translabel != $obj->{$InfoFieldList[1]}) {
- $labeltoshow = dol_trunc($translabel, 18);
- } else {
- $labeltoshow = dol_trunc($obj->{$InfoFieldList[1]}, 18);
- }
- }
- if (empty($labeltoshow))
- $labeltoshow = '(not defined)';
- if (is_array($value_arr) && in_array($obj->rowid, $value_arr)) {
- $data[$obj->rowid]=$labeltoshow;
- }
- if (! empty($InfoFieldList[3])) {
- $parent = $parentName . ':' . $obj->{$parentField};
- }
- $data[$obj->rowid]=$labeltoshow;
- }
-
- $i ++;
- }
- $this->db->free($resql);
-
- require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
- $form = new Form($db);
-
- $out=$form->multiselectarray($keysuffix.'options_'.$key.$keyprefix, $data, $value_arr, '', 0, '', 0, '100%');
-
- } else {
- print 'Error in request ' . $sql . ' ' . $this->db->lasterror() . '. Check setup of extra parameters.<br>';
- }
- }
- $out .= '</select>';
- }
- elseif ($type == 'link')
- {
- $out='';
- $param_list=array_keys($param['options']);
- // 0 : ObjectName
- // 1 : classPath
- $InfoFieldList = explode(":", $param_list[0]);
- dol_include_once($InfoFieldList[1]);
- if ($InfoFieldList[0] && class_exists($InfoFieldList[0]))
- {
- $valuetoshow=$value;
- if (!empty($value))
- {
- $object = new $InfoFieldList[0]($this->db);
- $resfetch=$object->fetch($value);
- if ($resfetch > 0)
- {
- $valuetoshow=$object->ref;
- if ($object->element == 'societe') $valuetoshow=$object->name; // Special case for thirdparty because ->ref is not name but id (because name is not unique)
- }
- }
- $out.='<input type="text" class="flat '.$showsize.'" name="'.$keysuffix.'options_'.$key.$keyprefix.'" value="'.$valuetoshow.'" >';
- }
- else
- {
- dol_syslog('Error bad setup of extrafield', LOG_WARNING);
- $out.='Error bad setup of extrafield';
- }
- }
- elseif ($type == 'password')
- {
- $out='<input type="password" class="flat '.$showsize.'" name="'.$keysuffix.'options_'.$key.$keyprefix.'" value="'.$value.'" '.($moreparam?$moreparam:'').'>';
- }
- if (!empty($hidden)) {
- $out='<input type="hidden" value="'.$value.'" name="'.$keysuffix.'options_'.$key.$keyprefix.'" id="'.$keysuffix.'options_'.$key.$keyprefix.'"/>';
- }
- /* Add comments
- if ($type == 'date') $out.=' (YYYY-MM-DD)';
- elseif ($type == 'datetime') $out.=' (YYYY-MM-DD HH:MM:SS)';
- */
- return $out;
- }
- /**
- * Return HTML string to put an output field into a page
- *
- * @param string $key Key of attribute
- * @param string $value Value to show
- * @param string $moreparam To add more parametes on html input tag (only checkbox use html input for output rendering)
- * @return string Formated value
- */
- function showOutputField($key,$value,$moreparam='')
- {
- global $conf,$langs;
- $elementtype=$this->attribute_elementtype[$key];
- $label=$this->attribute_label[$key];
- $type=$this->attribute_type[$key];
- $size=$this->attribute_size[$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];
- $perms=$this->attribute_perms[$key];
- $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')
- {
- $showsize=10;
- $value=dol_print_date($value,'day');
- }
- elseif ($type == 'datetime')
- {
- $showsize=19;
- $value=dol_print_date($value,'dayhour');
- }
- elseif ($type == 'int')
- {
- $showsize=10;
- }
- elseif ($type == 'double')
- {
- if (!empty($value)) {
- $value=price($value);
- }
- }
- elseif ($type == 'boolean')
- {
- $checked='';
- if (!empty($value)) {
- $checked=' checked ';
- }
- $value='<input type="checkbox" '.$checked.' '.($moreparam?$moreparam:'').' readonly disabled>';
- }
- elseif ($type == 'mail')
- {
- $value=dol_print_email($value,0,0,0,64,1,1);
- }
- elseif ($type == 'url')
- {
- $value=dol_print_url($value,'_blank',32,1);
- }
- elseif ($type == 'phone')
- {
- $value=dol_print_phone($value, '', 0, 0, '', ' ', 1);
- }
- elseif ($type == 'price')
- {
- $value=price($value,0,$langs,0,0,-1,$conf->currency);
- }
- elseif ($type == 'select')
- {
- $value=$params['options'][$value];
- }
- elseif ($type == 'sellist')
- {
- $param_list=array_keys($params['options']);
- $InfoFieldList = explode(":", $param_list[0]);
- $selectkey="rowid";
- $keyList='rowid';
- if (count($InfoFieldList)>=3)
- {
- $selectkey = $InfoFieldList[2];
- $keyList=$InfoFieldList[2].' as rowid';
- }
- $fields_label = explode('|',$InfoFieldList[1]);
- if(is_array($fields_label)) {
- $keyList .=', ';
- $keyList .= implode(', ', $fields_label);
- }
- $sql = 'SELECT '.$keyList;
- $sql.= ' FROM '.MAIN_DB_PREFIX .$InfoFieldList[0];
- if (strpos($InfoFieldList[4], 'extra')!==false)
- {
- $sql.= ' as main';
- }
- if ($selectkey=='rowid' && empty($value)) {
- $sql.= " WHERE ".$selectkey."=0";
- } elseif ($selectkey=='rowid') {
- $sql.= " WHERE ".$selectkey."=".$this->db->escape($value);
- }else {
- $sql.= " WHERE ".$selectkey."='".$this->db->escape($value)."'";
- }
- //$sql.= ' AND entity = '.$conf->entity;
- dol_syslog(get_class($this).':showOutputField:$type=sellist', LOG_DEBUG);
- $resql = $this->db->query($sql);
- if ($resql)
- {
- $value=''; // value was used, so now we reste it to use it to build final output
- $obj = $this->db->fetch_object($resql);
- // Several field into label (eq table:code|libelle:rowid)
- $fields_label = explode('|',$InfoFieldList[1]);
- if(is_array($fields_label) && count($fields_label)>1)
- {
- foreach ($fields_label as $field_toshow)
- {
- $translabel='';
- if (!empty($obj->$field_toshow)) {
- $translabel=$langs->trans($obj->$field_toshow);
- }
- if ($translabel!=$field_toshow) {
- $value.=dol_trunc($translabel,18).' ';
- }else {
- $value.=$obj->$field_toshow.' ';
- }
- }
- }
- else
- {
- $translabel='';
- if (!empty($obj->{$InfoFieldList[1]})) {
- $translabel=$langs->trans($obj->{$InfoFieldList[1]});
- }
- if ($translabel!=$obj->{$InfoFieldList[1]}) {
- $value=dol_trunc($translabel,18);
- }else {
- $value=$obj->{$InfoFieldList[1]};
- }
- }
- }
- else dol_syslog(get_class($this).'::showOutputField error '.$this->db->lasterror(), LOG_WARNING);
- }
- elseif ($type == 'radio')
- {
- $value=$params['options'][$value];
- }
- elseif ($type == 'checkbox')
- {
- $value_arr=explode(',',$value);
- $value='';
- if (is_array($value_arr))
- {
- foreach ($value_arr as $keyval=>$valueval) {
- $toprint[]='<li class="select2-search-choice-dolibarr noborderoncategories" style="background: #aaa">'.$params['options'][$valueval].'</li>';
- }
- }
- $value='<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">'.implode(' ', $toprint).'</ul></div>';
- }
- elseif ($type == 'chkbxlst')
- {
- $value_arr = explode(',', $value);
- $param_list = array_keys($params['options']);
- $InfoFieldList = explode(":", $param_list[0]);
- $selectkey = "rowid";
- $keyList = 'rowid';
- if (count($InfoFieldList) >= 3) {
- $selectkey = $InfoFieldList[2];
- $keyList = $InfoFieldList[2] . ' as rowid';
- }
- $fields_label = explode('|', $InfoFieldList[1]);
- if (is_array($fields_label)) {
- $keyList .= ', ';
- $keyList .= implode(', ', $fields_label);
- }
- $sql = 'SELECT ' . $keyList;
- $sql .= ' FROM ' . MAIN_DB_PREFIX . $InfoFieldList[0];
- if (strpos($InfoFieldList[4], 'extra') !== false) {
- $sql .= ' as main';
- }
- // $sql.= " WHERE ".$selectkey."='".$this->db->escape($value)."'";
- // $sql.= ' AND entity = '.$conf->entity;
- dol_syslog(get_class($this) . ':showOutputField:$type=chkbxlst',LOG_DEBUG);
- $resql = $this->db->query($sql);
- if ($resql) {
- $value = ''; // value was used, so now we reste it to use it to build final output
- $toprint=array();
- while ( $obj = $this->db->fetch_object($resql) ) {
- // Several field into label (eq table:code|libelle:rowid)
- $fields_label = explode('|', $InfoFieldList[1]);
- if (is_array($value_arr) && in_array($obj->rowid, $value_arr)) {
- if (is_array($fields_label) && count($fields_label) > 1) {
- foreach ( $fields_label as $field_toshow ) {
- $translabel = '';
- if (! empty($obj->$field_toshow)) {
- $translabel = $langs->trans($obj->$field_toshow);
- }
- if ($translabel != $field_toshow) {
- $toprint[]='<li class="select2-search-choice-dolibarr noborderoncategories" style="background: #aaa">'.dol_trunc($translabel, 18).'</li>';
- } else {
- $toprint[]='<li class="select2-search-choice-dolibarr noborderoncategories" style="background: #aaa">'.$obj->$field_toshow.'</li>';
- }
- }
- } else {
- $translabel = '';
- if (! empty($obj->{$InfoFieldList[1]})) {
- $translabel = $langs->trans($obj->{$InfoFieldList[1]});
- }
- if ($translabel != $obj->{$InfoFieldList[1]}) {
- $toprint[]='<li class="select2-search-choice-dolibarr noborderoncategories" style="background: #aaa">'.dol_trunc($translabel, 18).'</li>';
- } else {
- $toprint[]='<li class="select2-search-choice-dolibarr noborderoncategories" style="background: #aaa">'.$obj->{$InfoFieldList[1]}.'</li>';
- }
- }
- }
- }
- $value='<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">'.implode(' ', $toprint).'</ul></div>';
-
- } else {
- dol_syslog(get_class($this) . '::showOutputField error ' . $this->db->lasterror(), LOG_WARNING);
- }
- }
- elseif ($type == 'link')
- {
- $out='';
- // only if something to display (perf)
- if ($value)
- {
- $param_list=array_keys($params['options']);
- // 0 : ObjectName
- // 1 : classPath
- $InfoFieldList = explode(":", $param_list[0]);
- dol_include_once($InfoFieldList[1]);
- if ($InfoFieldList[0] && class_exists($InfoFieldList[0]))
- {
- $object = new $InfoFieldList[0]($this->db);
- $object->fetch($value);
- $value=$object->getNomUrl(3);
- }
- else
- {
- dol_syslog('Error bad setup of extrafield', LOG_WARNING);
- $out.='Error bad setup of extrafield';
- }
- }
- }
- elseif ($type == 'text')
- {
- $value=dol_htmlentitiesbr($value);
- }
- elseif ($type == 'password')
- {
- $value=preg_replace('/./i','*',$value);
- }
- else
- {
- $showsize=round($size);
- if ($showsize > 48) $showsize=48;
- }
- //print $type.'-'.$size;
- $out=$value;
- if (!empty($hidden)) {
- $out='';
- }
- return $out;
- }
- /**
- * Return tag to describe alignement to use for this extrafield
- *
- * @param string $key Key of attribute
- * @return string Formated value
- */
- function getAlignFlag($key)
- {
- global $conf,$langs;
- $type=$this->attribute_type[$key];
- $align='';
- if ($type == 'date')
- {
- $align="center";
- }
- elseif ($type == 'datetime')
- {
- $align="center";
- }
- elseif ($type == 'int')
- {
- $align="right";
- }
- elseif ($type == 'double')
- {
- $align="right";
- }
- elseif ($type == 'boolean')
- {
- $align="center";
- }
- elseif ($type == 'radio')
- {
- $align="center";
- }
- elseif ($type == 'checkbox')
- {
- $align="center";
- }
- return $align;
- }
- /**
- * Return HTML string to print separator extrafield
- *
- * @param string $key Key of attribute
- * @return string
- */
- function showSeparator($key)
- {
- $out = '<tr class="liste_titre"><td colspan="4"><strong>'.$this->attribute_label[$key].'</strong></td></tr>';
- return $out;
- }
- /**
- * Fill array_options property of object by extrafields value (using for data sent by forms)
- *
- * @param array $extralabels $array of extrafields
- * @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='')
- {
- global $_POST, $langs;
- $nofillrequired='';// For error when required field left blank
- $error_field_required = array();
- if (is_array($extralabels))
- {
- // Get extra fields
- foreach ($extralabels as $key => $value)
- {
- if (! empty($onlykey) && $key != $onlykey) continue;
- $key_type = $this->attribute_type[$key];
- if($this->attribute_required[$key] && !GETPOST("options_$key",2))
- {
- $nofillrequired++;
- $error_field_required[] = $value;
- }
- if (in_array($key_type,array('date','datetime')))
- {
- // Clean parameters
- $value_key=dol_mktime($_POST["options_".$key."hour"], $_POST["options_".$key."min"], 0, $_POST["options_".$key."month"], $_POST["options_".$key."day"], $_POST["options_".$key."year"]);
- }
- else if (in_array($key_type,array('checkbox','chkbxlst')))
- {
- $value_arr=GETPOST("options_".$key);
- if (!empty($value_arr)) {
- $value_key=implode($value_arr,',');
- }else {
- $value_key='';
- }
- }
- else if (in_array($key_type,array('price','double')))
- {
- $value_arr=GETPOST("options_".$key);
- $value_key=price2num($value_arr);
- }
- else
- {
- $value_key=GETPOST("options_".$key);
- }
- $object->array_options["options_".$key]=$value_key;
- }
- if($nofillrequired) {
- $langs->load('errors');
- setEventMessages($langs->trans('ErrorFieldsRequired').' : '.implode(', ',$error_field_required), null, 'errors');
- return -1;
- }
- else {
- return 1;
- }
- }
- else {
- return 0;
- }
- }
- /**
- * return array_options array for object by extrafields value (using for data send by forms)
- *
- * @param array $extralabels $array of extrafields
- * @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 int 1 if array_options set / 0 if no value
- */
- function getOptionalsFromPost($extralabels,$keyprefix='',$keysuffix='')
- {
- global $_POST;
- $array_options = array();
- if (is_array($extralabels))
- {
- // Get extra fields
- foreach ($extralabels as $key => $value)
- {
- $key_type = $this->attribute_type[$key];
- if (in_array($key_type,array('date','datetime')))
- {
- // Clean parameters
- $value_key=dol_mktime($_POST[$keysuffix."options_".$key.$keyprefix."hour"], $_POST[$keysuffix."options_".$key.$keyprefix."min"], 0, $_POST[$keysuffix."options_".$key.$keyprefix."month"], $_POST[$keysuffix."options_".$key.$keyprefix."day"], $_POST[$keysuffix."options_".$key.$keyprefix."year"]);
- }
- else if (in_array($key_type,array('checkbox')))
- {
- $value_arr=GETPOST($keysuffix."options_".$key.$keyprefix);
- // Make sure we get an array even if there's only one checkbox
- $value_arr=(array) $value_arr;
- $value_key=implode(',', $value_arr);
- }
- else if (in_array($key_type,array('price','double')))
- {
- $value_arr=GETPOST($keysuffix."options_".$key.$keyprefix);
- $value_key=price2num($value_arr);
- }
- else
- {
- $value_key=GETPOST($keysuffix."options_".$key.$keyprefix);
- }
- $array_options[$keysuffix."options_".$key]=$value_key; // No keyprefix here. keyprefix is used only for read.
- }
- return $array_options;
- }
- else {
- return 0;
- }
- }
- }
|