extrafields.class.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  1. <?php
  2. /* Copyright (C) 2002-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2002-2003 Jean-Louis Bergamo <jlb@j1b.org>
  4. * Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
  5. * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
  6. * Copyright (C) 2009-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  7. * Copyright (C) 2009-2012 Regis Houssin <regis.houssin@capnetworks.com>
  8. * Copyright (C) 2013 Florian Henry <forian.henry@open-concept.pro>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. /**
  24. * \file htdocs/core/class/extrafields.class.php
  25. * \ingroup core
  26. * \brief File of class to manage extra fields
  27. */
  28. /**
  29. * Class to manage standard extra fields
  30. */
  31. class ExtraFields
  32. {
  33. var $db;
  34. // Tableau contenant le nom des champs en clef et la definition de ces champs
  35. var $attribute_type;
  36. // Tableau contenant le nom des champs en clef et le label de ces champs en value
  37. var $attribute_label;
  38. // Tableau contenant le nom des champs en clef et la taille de ces champs en value
  39. var $attribute_size;
  40. // Tableau contenant le nom des choix en clef et la valeur de ces choix en value
  41. var $attribute_choice;
  42. // Array to store if attribute is unique or not
  43. var $attribute_unique;
  44. // Array to store if attribute is required or not
  45. var $attribute_required;
  46. // Array to store parameters of attribute (used in select type)
  47. var $attribute_param;
  48. // Int to store position of attribute
  49. var $attribute_pos;
  50. // Int to store if attribute is editable regardless of the document status
  51. var $attribute_alwayseditable;
  52. var $error;
  53. var $errno;
  54. public static $type2label=array(
  55. 'varchar'=>'String',
  56. 'text'=>'TextLong',
  57. 'int'=>'Int',
  58. 'double'=>'Float',
  59. 'date'=>'Date',
  60. 'datetime'=>'DateAndTime',
  61. 'boolean'=>'Boolean',
  62. 'price'=>'ExtrafieldPrice',
  63. 'phone'=>'ExtrafieldPhone',
  64. 'mail'=>'ExtrafieldMail',
  65. 'select' => 'ExtrafieldSelect',
  66. 'sellist' => 'ExtrafieldSelectList',
  67. 'separate' => 'ExtrafieldSeparator',
  68. 'checkbox' => 'ExtrafieldCheckBox',
  69. 'radio' => 'ExtrafieldRadio',
  70. );
  71. /**
  72. * Constructor
  73. *
  74. * @param DoliDB $db Database handler
  75. */
  76. function __construct($db)
  77. {
  78. $this->db = $db;
  79. $this->error = array();
  80. $this->attribute_type = array();
  81. $this->attribute_label = array();
  82. $this->attribute_size = array();
  83. $this->attribute_elementtype = array();
  84. $this->attribute_unique = array();
  85. $this->attribute_required = array();
  86. }
  87. /**
  88. * Add a new extra field parameter
  89. *
  90. * @param string $attrname Code of attribute
  91. * @param string $label label of attribute
  92. * @param int $type Type of attribute ('int', 'text', 'varchar', 'date', 'datehour')
  93. * @param int $pos Position of attribute
  94. * @param string $size Size/length of attribute
  95. * @param string $elementtype Element type ('member', 'product', 'thirdparty', ...)
  96. * @param int $unique Is field unique or not
  97. * @param int $required Is field required or not
  98. * @param string $default_value Defaulted value
  99. * @param array $param Params for field
  100. * @param int $alwayseditable Is attribute always editable regardless of the document status
  101. * @return int <=0 if KO, >0 if OK
  102. */
  103. function addExtraField($attrname, $label, $type, $pos, $size, $elementtype, $unique=0, $required=0, $default_value='', $param=0, $alwayseditable=0)
  104. {
  105. if (empty($attrname)) return -1;
  106. if (empty($label)) return -1;
  107. if ($elementtype == 'thirdparty') $elementtype='societe';
  108. // Create field into database except for separator type which is not stored in database
  109. if ($type != 'separate')
  110. {
  111. $result=$this->create($attrname, $type, $size, $elementtype, $unique, $required, $default_value, $param);
  112. }
  113. $err1=$this->errno;
  114. if ($result > 0 || $err1 == 'DB_ERROR_COLUMN_ALREADY_EXISTS' || $type == 'separate')
  115. {
  116. // Add declaration of field into table
  117. $result2=$this->create_label($attrname,$label,$type,$pos,$size,$elementtype, $unique, $required, $param, $alwayseditable);
  118. $err2=$this->errno;
  119. if ($result2 > 0 || ($err1 == 'DB_ERROR_COLUMN_ALREADY_EXISTS' && $err2 == 'DB_ERROR_RECORD_ALREADY_EXISTS'))
  120. {
  121. $this->error='';
  122. $this->errno=0;
  123. return 1;
  124. }
  125. else return -2;
  126. }
  127. else
  128. {
  129. return -1;
  130. }
  131. }
  132. /**
  133. * Add a new optional attribute.
  134. * This is a private method. For public method, use addExtraField.
  135. *
  136. * @param string $attrname code of attribute
  137. * @param int $type Type of attribute ('int', 'text', 'varchar', 'date', 'datehour')
  138. * @param string $length Size/length of attribute ('5', '24,8', ...)
  139. * @param string $elementtype Element type ('member', 'product', 'thirdparty', 'contact', ...)
  140. * @param int $unique Is field unique or not
  141. * @param int $required Is field required or not
  142. * @param string $default_value Default value for field
  143. * @param array $param Params for field (ex for select list : array('options'=>array('value'=>'label of option'))
  144. *
  145. * @return int <=0 if KO, >0 if OK
  146. */
  147. private function create($attrname, $type='varchar', $length=255, $elementtype='member', $unique=0, $required=0, $default_value='',$param='')
  148. {
  149. if ($elementtype == 'thirdparty') $elementtype='societe';
  150. $table=$elementtype.'_extrafields';
  151. if (! empty($attrname) && preg_match("/^\w[a-zA-Z0-9_]*$/",$attrname) && ! is_numeric($attrname))
  152. {
  153. if ($type=='boolean') {
  154. $typedb='int';
  155. $lengthdb='1';
  156. } elseif($type=='price') {
  157. $typedb='double';
  158. $lengthdb='24,8';
  159. } elseif($type=='phone') {
  160. $typedb='varchar';
  161. $lengthdb='20';
  162. }elseif($type=='mail') {
  163. $typedb='varchar';
  164. $lengthdb='128';
  165. } elseif (($type=='select') || ($type=='sellist') || ($type=='radio') ||($type=='checkbox')){
  166. $typedb='text';
  167. $lengthdb='';
  168. } else {
  169. $typedb=$type;
  170. $lengthdb=$length;
  171. }
  172. $field_desc = array(
  173. 'type'=>$typedb,
  174. 'value'=>$lengthdb,
  175. 'null'=>($required?'NOT NULL':'NULL'),
  176. 'default' => $default_value
  177. );
  178. $result=$this->db->DDLAddField(MAIN_DB_PREFIX.$table, $attrname, $field_desc);
  179. if ($result > 0)
  180. {
  181. if ($unique)
  182. {
  183. $sql="ALTER TABLE ".MAIN_DB_PREFIX.$table." ADD UNIQUE INDEX uk_".$table."_".$attrname." (".$attrname.")";
  184. $resql=$this->db->query($sql,1,'dml');
  185. }
  186. return 1;
  187. }
  188. else
  189. {
  190. $this->error=$this->db->lasterror();
  191. $this->errno=$this->db->lasterrno();
  192. return -1;
  193. }
  194. }
  195. else
  196. {
  197. return 0;
  198. }
  199. }
  200. /**
  201. * Add description of a new optional attribute
  202. *
  203. * @param string $attrname code of attribute
  204. * @param string $label label of attribute
  205. * @param int $type Type of attribute ('int', 'text', 'varchar', 'date', 'datehour', 'float')
  206. * @param int $pos Position of attribute
  207. * @param string $size Size/length of attribute ('5', '24,8', ...)
  208. * @param string $elementtype Element type ('member', 'product', 'thirdparty', ...)
  209. * @param int $unique Is field unique or not
  210. * @param int $required Is field required or not
  211. * @param array||string $param Params for field (ex for select list : array('options' => array(value'=>'label of option')) )
  212. * @param int $alwayseditable Is attribute always editable regardless of the document status
  213. * @return int <=0 if KO, >0 if OK
  214. */
  215. private function create_label($attrname, $label='', $type='', $pos=0, $size=0, $elementtype='member', $unique=0, $required=0, $param='', $alwayseditable=0)
  216. {
  217. global $conf;
  218. if ($elementtype == 'thirdparty') $elementtype='societe';
  219. // Clean parameters
  220. if (empty($pos)) $pos=0;
  221. if (! empty($attrname) && preg_match("/^\w[a-zA-Z0-9-_]*$/",$attrname) && ! is_numeric($attrname))
  222. {
  223. if(is_array($param) and count($param) > 0)
  224. {
  225. $params = $this->db->escape(serialize($param));
  226. }
  227. elseif (strlen($param) > 0)
  228. {
  229. $params = trim($param);
  230. }
  231. else
  232. {
  233. $params='';
  234. }
  235. $sql = "INSERT INTO ".MAIN_DB_PREFIX."extrafields(name, label, type, pos, size, entity, elementtype, fieldunique, fieldrequired, param, alwayseditable)";
  236. $sql.= " VALUES('".$attrname."',";
  237. $sql.= " '".$this->db->escape($label)."',";
  238. $sql.= " '".$type."',";
  239. $sql.= " '".$pos."',";
  240. $sql.= " '".$size."',";
  241. $sql.= " ".$conf->entity.",";
  242. $sql.= " '".$elementtype."',";
  243. $sql.= " '".$unique."',";
  244. $sql.= " '".$required."',";
  245. $sql.= " '".$params."',";
  246. $sql.= " '".$alwayseditable."'";
  247. $sql.=')';
  248. dol_syslog(get_class($this)."::create_label", LOG_DEBUG);
  249. if ($this->db->query($sql))
  250. {
  251. return 1;
  252. }
  253. else
  254. {
  255. $this->error=$this->db->lasterror();
  256. $this->errno=$this->db->lasterrno();
  257. return -1;
  258. }
  259. }
  260. }
  261. /**
  262. * Delete an optional attribute
  263. *
  264. * @param string $attrname Code of attribute to delete
  265. * @param string $elementtype Element type ('member', 'product', 'thirdparty', 'contact', ...)
  266. * @return int < 0 if KO, 0 if nothing is done, 1 if OK
  267. */
  268. function delete($attrname, $elementtype='member')
  269. {
  270. if ($elementtype == 'thirdparty') $elementtype='societe';
  271. $table=$elementtype.'_extrafields';
  272. if (! empty($attrname) && preg_match("/^\w[a-zA-Z0-9-_]*$/",$attrname))
  273. {
  274. $result=$this->db->DDLDropField(MAIN_DB_PREFIX.$table,$attrname); // This also drop the unique key
  275. if ($result < 0)
  276. {
  277. $this->error=$this->db->lasterror();
  278. }
  279. $result=$this->delete_label($attrname,$elementtype);
  280. return $result;
  281. }
  282. else
  283. {
  284. return 0;
  285. }
  286. }
  287. /**
  288. * Delete description of an optional attribute
  289. *
  290. * @param string $attrname Code of attribute to delete
  291. * @param string $elementtype Element type ('member', 'product', 'thirdparty', ...)
  292. * @return int < 0 if KO, 0 if nothing is done, 1 if OK
  293. */
  294. private function delete_label($attrname, $elementtype='member')
  295. {
  296. global $conf;
  297. if ($elementtype == 'thirdparty') $elementtype='societe';
  298. if (isset($attrname) && $attrname != '' && preg_match("/^\w[a-zA-Z0-9-_]*$/",$attrname))
  299. {
  300. $sql = "DELETE FROM ".MAIN_DB_PREFIX."extrafields";
  301. $sql.= " WHERE name = '".$attrname."'";
  302. $sql.= " AND entity IN (0,".$conf->entity.')';
  303. $sql.= " AND elementtype = '".$elementtype."'";
  304. dol_syslog(get_class($this)."::delete_label", LOG_DEBUG);
  305. $resql=$this->db->query($sql);
  306. if ($resql)
  307. {
  308. return 1;
  309. }
  310. else
  311. {
  312. print dol_print_error($this->db);
  313. return -1;
  314. }
  315. }
  316. else
  317. {
  318. return 0;
  319. }
  320. }
  321. /**
  322. * Modify type of a personalized attribute
  323. *
  324. * @param string $attrname Name of attribute
  325. * @param string $label Label of attribute
  326. * @param string $type Type of attribute
  327. * @param int $length Length of attribute
  328. * @param string $elementtype Element type ('member', 'product', 'thirdparty', 'contact', ...)
  329. * @param int $unique Is field unique or not
  330. * @param int $required Is field required or not
  331. * @param int $pos Position of attribute
  332. * @param array $param Params for field (ex for select list : array('options' => array(value'=>'label of option')) )
  333. * @param int $alwayseditable Is attribute always editable regardless of the document status
  334. * @return int >0 if OK, <=0 if KO
  335. */
  336. function update($attrname,$label,$type,$length,$elementtype,$unique=0,$required=0,$pos=0,$param='',$alwayseditable=0)
  337. {
  338. if ($elementtype == 'thirdparty') $elementtype='societe';
  339. $table=$elementtype.'_extrafields';
  340. if (isset($attrname) && $attrname != '' && preg_match("/^\w[a-zA-Z0-9-_]*$/",$attrname))
  341. {
  342. if ($type=='boolean') {
  343. $typedb='int';
  344. $lengthdb='1';
  345. } elseif($type=='price') {
  346. $typedb='double';
  347. $lengthdb='24,8';
  348. } elseif($type=='phone') {
  349. $typedb='varchar';
  350. $lengthdb='20';
  351. }elseif($type=='mail') {
  352. $typedb='varchar';
  353. $lengthdb='128';
  354. } elseif (($type=='select') || ($type=='sellist') || ($type=='radio') ||($type=='checkbox')) {
  355. $typedb='text';
  356. $lengthdb='';
  357. } else {
  358. $typedb=$type;
  359. $lengthdb=$length;
  360. }
  361. $field_desc = array('type'=>$typedb, 'value'=>$lengthdb, 'null'=>($required?'NOT NULL':'NULL'));
  362. if ($type != 'separate') // No table update when separate type
  363. {
  364. $result=$this->db->DDLUpdateField(MAIN_DB_PREFIX.$table, $attrname, $field_desc);
  365. }
  366. if ($result > 0 || $type == 'separate')
  367. {
  368. if ($label)
  369. {
  370. $result=$this->update_label($attrname,$label,$type,$length,$elementtype,$unique,$required,$pos,$param,$alwayseditable);
  371. }
  372. if ($result > 0)
  373. {
  374. $sql='';
  375. if ($unique)
  376. {
  377. $sql="ALTER TABLE ".MAIN_DB_PREFIX.$table." ADD UNIQUE INDEX uk_".$table."_".$attrname." (".$attrname.")";
  378. }
  379. else
  380. {
  381. $sql="ALTER TABLE ".MAIN_DB_PREFIX.$table." DROP INDEX uk_".$table."_".$attrname;
  382. }
  383. dol_syslog(get_class($this).'::update', LOG_DEBUG);
  384. $resql=$this->db->query($sql,1,'dml');
  385. return 1;
  386. }
  387. else
  388. {
  389. $this->error=$this->db->lasterror();
  390. return -1;
  391. }
  392. }
  393. else
  394. {
  395. $this->error=$this->db->lasterror();
  396. return -1;
  397. }
  398. }
  399. else
  400. {
  401. return 0;
  402. }
  403. }
  404. /**
  405. * Modify description of personalized attribute
  406. *
  407. * @param string $attrname Name of attribute
  408. * @param string $label Label of attribute
  409. * @param string $type Type of attribute
  410. * @param int $size Length of attribute
  411. * @param string $elementtype Element type ('member', 'product', 'thirdparty', ...)
  412. * @param int $unique Is field unique or not
  413. * @param int $required Is field required or not
  414. * @param int $pos Position of attribute
  415. * @param array $param Params for field (ex for select list : array('options' => array(value'=>'label of option')) )
  416. * @param int $alwayseditable Is attribute always editable regardless of the document status
  417. * @return int <=0 if KO, >0 if OK
  418. */
  419. private function update_label($attrname,$label,$type,$size,$elementtype,$unique=0,$required=0,$pos=0,$param='',$alwayseditable=0)
  420. {
  421. global $conf;
  422. dol_syslog(get_class($this)."::update_label ".$attrname.", ".$label.", ".$type.", ".$size.", ".$elementtype.", ".$unique.", ".$required);
  423. if ($elementtype == 'thirdparty') $elementtype='societe';
  424. if (isset($attrname) && $attrname != '' && preg_match("/^\w[a-zA-Z0-9-_]*$/",$attrname))
  425. {
  426. $this->db->begin();
  427. if(is_array($param) && count($param) > 0)
  428. {
  429. $param = $this->db->escape(serialize($param));
  430. }
  431. $sql_del = "DELETE FROM ".MAIN_DB_PREFIX."extrafields";
  432. $sql_del.= " WHERE name = '".$attrname."'";
  433. $sql_del.= " AND entity = ".$conf->entity;
  434. $sql_del.= " AND elementtype = '".$elementtype."'";
  435. dol_syslog(get_class($this)."::update_label", LOG_DEBUG);
  436. $resql1=$this->db->query($sql_del);
  437. $sql = "INSERT INTO ".MAIN_DB_PREFIX."extrafields(";
  438. $sql.= " name,"; // This is code
  439. $sql.= " entity,";
  440. $sql.= " label,";
  441. $sql.= " type,";
  442. $sql.= " size,";
  443. $sql.= " elementtype,";
  444. $sql.= " fieldunique,";
  445. $sql.= " fieldrequired,";
  446. $sql.= " pos,";
  447. $sql.= " alwayseditable,";
  448. $sql.= " param";
  449. $sql.= ") VALUES (";
  450. $sql.= "'".$attrname."',";
  451. $sql.= " ".$conf->entity.",";
  452. $sql.= " '".$this->db->escape($label)."',";
  453. $sql.= " '".$type."',";
  454. $sql.= " '".$size."',";
  455. $sql.= " '".$elementtype."',";
  456. $sql.= " '".$unique."',";
  457. $sql.= " '".$required."',";
  458. $sql.= " '".$pos."',";
  459. $sql.= " '".$alwayseditable."',";
  460. $sql.= " '".$param."'";
  461. $sql.= ")";
  462. dol_syslog(get_class($this)."::update_label", LOG_DEBUG);
  463. $resql2=$this->db->query($sql);
  464. if ($resql1 && $resql2)
  465. {
  466. $this->db->commit();
  467. return 1;
  468. }
  469. else
  470. {
  471. $this->db->rollback();
  472. print dol_print_error($this->db);
  473. return -1;
  474. }
  475. }
  476. else
  477. {
  478. return 0;
  479. }
  480. }
  481. /**
  482. * Load array this->attribute_xxx like attribute_label, attribute_type, ...
  483. *
  484. * @param string $elementtype Type of element ('adherent', 'commande', thirdparty', 'facture', 'propal', 'product', ...)
  485. * @param boolean $forceload Force load of extra fields whatever is option MAIN_EXTRAFIELDS_DISABLED
  486. * @return array Array of attributes for all extra fields
  487. */
  488. function fetch_name_optionals_label($elementtype,$forceload=false)
  489. {
  490. global $conf;
  491. if ($elementtype == 'thirdparty') $elementtype='societe';
  492. $array_name_label=array();
  493. // For avoid conflicts with external modules
  494. if (!$forceload && !empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) return $array_name_label;
  495. $sql = "SELECT rowid,name,label,type,size,elementtype,fieldunique,fieldrequired,param,pos,alwayseditable";
  496. $sql.= " FROM ".MAIN_DB_PREFIX."extrafields";
  497. $sql.= " WHERE entity IN (0,".$conf->entity.")";
  498. if ($elementtype) $sql.= " AND elementtype = '".$elementtype."'";
  499. $sql.= " ORDER BY pos";
  500. dol_syslog(get_class($this)."::fetch_name_optionals_label", LOG_DEBUG);
  501. $resql=$this->db->query($sql);
  502. if ($resql)
  503. {
  504. if ($this->db->num_rows($resql))
  505. {
  506. while ($tab = $this->db->fetch_object($resql))
  507. {
  508. // we can add this attribute to adherent object
  509. if ($tab->type != 'separate')
  510. {
  511. $array_name_label[$tab->name]=$tab->label;
  512. }
  513. $this->attribute_type[$tab->name]=$tab->type;
  514. $this->attribute_label[$tab->name]=$tab->label;
  515. $this->attribute_size[$tab->name]=$tab->size;
  516. $this->attribute_elementtype[$tab->name]=$tab->elementtype;
  517. $this->attribute_unique[$tab->name]=$tab->fieldunique;
  518. $this->attribute_required[$tab->name]=$tab->fieldrequired;
  519. $this->attribute_param[$tab->name]=unserialize($tab->param);
  520. $this->attribute_pos[$tab->name]=$tab->pos;
  521. $this->attribute_alwayseditable[$tab->name]=$tab->alwayseditable;
  522. }
  523. }
  524. }
  525. else
  526. {
  527. print dol_print_error($this->db);
  528. }
  529. return $array_name_label;
  530. }
  531. /**
  532. * Return HTML string to put an input field into a page
  533. *
  534. * @param string $key Key of attribute
  535. * @param string $value Value to show (for date type it must be in timestamp format)
  536. * @param string $moreparam To add more parametes on html input tag
  537. * @param string $keyprefix Prefix string to add into name and id of field (can be used to avoid duplicate names)
  538. * @return string
  539. */
  540. function showInputField($key,$value,$moreparam='',$keyprefix='')
  541. {
  542. global $conf,$langs;
  543. $label=$this->attribute_label[$key];
  544. $type =$this->attribute_type[$key];
  545. $size =$this->attribute_size[$key];
  546. $elementtype=$this->attribute_elementtype[$key];
  547. $unique=$this->attribute_unique[$key];
  548. $required=$this->attribute_required[$key];
  549. $param=$this->attribute_param[$key];
  550. if ($type == 'date')
  551. {
  552. $showsize=10;
  553. }
  554. elseif ($type == 'datetime')
  555. {
  556. $showsize=19;
  557. }
  558. elseif (in_array($type,array('int','double')))
  559. {
  560. $showsize=10;
  561. }
  562. else
  563. {
  564. $showsize=round($size);
  565. if ($showsize > 48) $showsize=48;
  566. }
  567. if (in_array($type,array('date','datetime')))
  568. {
  569. $tmp=explode(',',$size);
  570. $newsize=$tmp[0];
  571. $showtime = in_array($type,array('datetime')) ? 1 : 0;
  572. // Do not show current date when field not required (see select_date() method)
  573. if (!$required && $value == '') $value = '-1';
  574. require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
  575. global $form;
  576. if (! is_object($form)) $form=new Form($this->db);
  577. // TODO Must also support $moreparam
  578. $out = $form->select_date($value, 'options_'.$key.$keyprefix, $showtime, $showtime, $required, '', 1, 1, 1, 0, 1);
  579. }
  580. elseif (in_array($type,array('int')))
  581. {
  582. $tmp=explode(',',$size);
  583. $newsize=$tmp[0];
  584. $out='<input type="text" class="flat" name="options_'.$key.$keyprefix.'" size="'.$showsize.'" maxlength="'.$newsize.'" value="'.$value.'"'.($moreparam?$moreparam:'').'>';
  585. }
  586. elseif ($type == 'varchar')
  587. {
  588. $out='<input type="text" class="flat" name="options_'.$key.$keyprefix.'" size="'.$showsize.'" maxlength="'.$size.'" value="'.$value.'"'.($moreparam?$moreparam:'').'>';
  589. }
  590. elseif ($type == 'text')
  591. {
  592. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  593. $doleditor=new DolEditor('options_'.$key.$keyprefix,$value,'',200,'dolibarr_notes','In',false,false,! empty($conf->fckeditor->enabled) && $conf->global->FCKEDITOR_ENABLE_SOCIETE,5,100);
  594. $out=$doleditor->Create(1);
  595. }
  596. elseif ($type == 'boolean')
  597. {
  598. $checked='';
  599. if (!empty($value)) {
  600. $checked=' checked="checked" value="1" ';
  601. } else {
  602. $checked=' value="1" ';
  603. }
  604. $out='<input type="checkbox" class="flat" name="options_'.$key.$keyprefix.'" '.$checked.' '.($moreparam?$moreparam:'').'>';
  605. }
  606. elseif ($type == 'mail')
  607. {
  608. $out='<input type="text" class="flat" name="options_'.$key.$keyprefix.'" size="32" value="'.$value.'" '.($moreparam?$moreparam:'').'>';
  609. }
  610. elseif ($type == 'phone')
  611. {
  612. $out='<input type="text" class="flat" name="options_'.$key.$keyprefix.'" size="20" value="'.$value.'" '.($moreparam?$moreparam:'').'>';
  613. }
  614. elseif ($type == 'price')
  615. {
  616. $out='<input type="text" class="flat" name="options_'.$key.$keyprefix.'" size="6" value="'.price($value).'" '.($moreparam?$moreparam:'').'> '.$langs->getCurrencySymbol($conf->currency);
  617. }
  618. elseif ($type == 'double')
  619. {
  620. if (!empty($value)) {
  621. $value=price($value);
  622. }
  623. $out='<input type="text" class="flat" name="options_'.$key.$keyprefix.'" size="6" value="'.$value.'" '.($moreparam?$moreparam:'').'> ';
  624. }
  625. elseif ($type == 'select')
  626. {
  627. $out = '';
  628. if (! empty($conf->use_javascript_ajax) && ! empty($conf->global->COMPANY_USE_SEARCH_TO_SELECT) && ! $forcecombo)
  629. {
  630. include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php';
  631. $out.= ajax_combobox('options_'.$key.$keyprefix, array(), $conf->global->COMPANY_USE_SEARCH_TO_SELECT);
  632. }
  633. $out.='<select class="flat" name="options_'.$key.$keyprefix.'" id="options_'.$key.$keyprefix.'" '.($moreparam?$moreparam:'').'>';
  634. $out.='<option value="0">&nbsp;</option>';
  635. foreach ($param['options'] as $key=>$val )
  636. {
  637. list($val, $parent) = explode('|', $val);
  638. $out.='<option value="'.$key.'"';
  639. $out.= ($value==$key?' selected="selected"':'');
  640. $out.= (!empty($parent)?' parent="'.$parent.'"':'');
  641. $out.='>'.$val.'</option>';
  642. }
  643. $out.='</select>';
  644. }
  645. elseif ($type == 'sellist')
  646. {
  647. $out = '';
  648. if ($conf->use_javascript_ajax && $conf->global->COMPANY_USE_SEARCH_TO_SELECT && ! $forcecombo)
  649. {
  650. include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php';
  651. $out.= ajax_combobox('options_'.$key.$keyprefix, array(), $conf->global->COMPANY_USE_SEARCH_TO_SELECT);
  652. }
  653. $out.='<select class="flat" name="options_'.$key.$keyprefix.'" id="options_'.$key.$keyprefix.'" '.($moreparam?$moreparam:'').'>';
  654. if (is_array($param['options']))
  655. {
  656. $param_list=array_keys($param['options']);
  657. $InfoFieldList = explode(":", $param_list[0]);
  658. // 0 : tableName
  659. // 1 : label field name
  660. // 2 : key fields name (if differ of rowid)
  661. // 3 : key field parent (for dependent lists)
  662. // 4 : where clause filter on column or table extrafield, syntax field='value' or extra.field=value
  663. $keyList=(empty($InfoFieldList[2])?'rowid':$InfoFieldList[2].' as rowid');
  664. if (count($InfoFieldList) > 3 && ! empty($InfoFieldList[3]))
  665. {
  666. list($parentName, $parentField) = explode('|', $InfoFieldList[3]);
  667. $keyList.= ', '.$parentField;
  668. }
  669. if (count($InfoFieldList) > 4 && ! empty($InfoFieldList[4]))
  670. {
  671. if (strpos($InfoFieldList[4], 'extra.') !== false)
  672. {
  673. $keyList='main.'.$InfoFieldList[2].' as rowid';
  674. } else {
  675. $keyList=$InfoFieldList[2].' as rowid';
  676. }
  677. }
  678. $fields_label = explode('|',$InfoFieldList[1]);
  679. if (is_array($fields_label))
  680. {
  681. $keyList .=', ';
  682. $keyList .= implode(', ', $fields_label);
  683. }
  684. $sqlwhere='';
  685. $sql = 'SELECT '.$keyList;
  686. $sql.= ' FROM '.MAIN_DB_PREFIX .$InfoFieldList[0];
  687. if (!empty($InfoFieldList[4]))
  688. {
  689. //We have to join on extrafield table
  690. if (strpos($InfoFieldList[4], 'extra')!==false)
  691. {
  692. $sql.= ' as main, '.MAIN_DB_PREFIX .$InfoFieldList[0].'_extrafields as extra';
  693. $sqlwhere.= ' WHERE extra.fk_object=main.'.$InfoFieldList[2]. ' AND '.$InfoFieldList[4];
  694. }
  695. else
  696. {
  697. $sqlwhere.= ' WHERE '.$InfoFieldList[4];
  698. }
  699. }else {
  700. $sqlwhere.= ' WHERE 1';
  701. }
  702. if (in_array($InfoFieldList[0],array('tablewithentity'))) $sqlwhere.= ' AND entity = '.$conf->entity; // Some tables may have field, some other not. For the moment we disable it.
  703. $sql.=$sqlwhere;
  704. //print $sql;
  705. dol_syslog(get_class($this).'::showInputField type=sellist', LOG_DEBUG);
  706. $resql = $this->db->query($sql);
  707. if ($resql)
  708. {
  709. $out.='<option value="0">&nbsp;</option>';
  710. $num = $this->db->num_rows($resql);
  711. $i = 0;
  712. while ($i < $num)
  713. {
  714. $labeltoshow='';
  715. $obj = $this->db->fetch_object($resql);
  716. // Several field into label (eq table:code|libelle:rowid)
  717. $fields_label = explode('|',$InfoFieldList[1]);
  718. if(is_array($fields_label))
  719. {
  720. $notrans = true;
  721. foreach ($fields_label as $field_toshow)
  722. {
  723. $labeltoshow.= $obj->$field_toshow.' ';
  724. }
  725. }
  726. else
  727. {
  728. $labeltoshow=$obj->$InfoFieldList[1];
  729. }
  730. $labeltoshow=dol_trunc($labeltoshow,45);
  731. if ($value==$obj->rowid)
  732. {
  733. foreach ($fields_label as $field_toshow)
  734. {
  735. $translabel=$langs->trans($obj->$field_toshow);
  736. if ($translabel!=$obj->$field_toshow) {
  737. $labeltoshow=dol_trunc($translabel,18).' ';
  738. }else {
  739. $labeltoshow=dol_trunc($obj->$field_toshow,18).' ';
  740. }
  741. }
  742. $out.='<option value="'.$obj->rowid.'" selected="selected">'.$labeltoshow.'</option>';
  743. }
  744. else
  745. {
  746. if(!$notrans)
  747. {
  748. $translabel=$langs->trans($obj->$InfoFieldList[1]);
  749. if ($translabel!=$obj->$InfoFieldList[1]) {
  750. $labeltoshow=dol_trunc($translabel,18);
  751. }
  752. else {
  753. $labeltoshow=dol_trunc($obj->$InfoFieldList[1],18);
  754. }
  755. }
  756. if (empty($labeltoshow)) $labeltoshow='(not defined)';
  757. if ($value==$obj->rowid)
  758. {
  759. $out.='<option value="'.$obj->rowid.'" selected="selected">'.$labeltoshow.'</option>';
  760. }
  761. if (!empty($InfoFieldList[3]))
  762. {
  763. $parent = $parentName.':'.$obj->{$parentField};
  764. }
  765. $out.='<option value="'.$obj->rowid.'"';
  766. $out.= ($value==$obj->rowid?' selected="selected"':'');
  767. $out.= (!empty($parent)?' parent="'.$parent.'"':'');
  768. $out.='>'.$labeltoshow.'</option>';
  769. }
  770. $i++;
  771. }
  772. $this->db->free($resql);
  773. }
  774. else {
  775. print 'Error in request '.$sql.' '.$this->db->lasterror().'. Check setup of extra parameters.<br>';
  776. }
  777. }
  778. $out.='</select>';
  779. }
  780. elseif ($type == 'checkbox')
  781. {
  782. $out='';
  783. $value_arr=explode(',',$value);
  784. foreach ($param['options'] as $keyopt=>$val )
  785. {
  786. $out.='<input class="flat" type="checkbox" name="options_'.$key.$keyprefix.'[]" '.($moreparam?$moreparam:'');
  787. $out.=' value="'.$keyopt.'"';
  788. if ((is_array($value_arr)) && in_array($keyopt,$value_arr)) {
  789. $out.= 'checked="checked"';
  790. }else {
  791. $out.='';
  792. }
  793. $out.='/>'.$val.'<br>';
  794. }
  795. }
  796. elseif ($type == 'radio')
  797. {
  798. $out='';
  799. foreach ($param['options'] as $keyopt=>$val )
  800. {
  801. $out.='<input class="flat" type="radio" name="options_'.$key.$keyprefix.'" '.($moreparam?$moreparam:'');
  802. $out.=' value="'.$keyopt.'"';
  803. $out.= ($value==$keyopt?'checked="checked"':'');
  804. $out.='/>'.$val.'<br>';
  805. }
  806. }
  807. /* Add comments
  808. if ($type == 'date') $out.=' (YYYY-MM-DD)';
  809. elseif ($type == 'datetime') $out.=' (YYYY-MM-DD HH:MM:SS)';
  810. */
  811. return $out;
  812. }
  813. /**
  814. * Return HTML string to put an output field into a page
  815. *
  816. * @param string $key Key of attribute
  817. * @param string $value Value to show
  818. * @param string $moreparam More param
  819. * @return string Formated value
  820. */
  821. function showOutputField($key,$value,$moreparam='')
  822. {
  823. global $conf,$langs;
  824. $label=$this->attribute_label[$key];
  825. $type=$this->attribute_type[$key];
  826. $size=$this->attribute_size[$key];
  827. $elementtype=$this->attribute_elementtype[$key];
  828. $unique=$this->attribute_unique[$key];
  829. $required=$this->attribute_required[$key];
  830. $params=$this->attribute_param[$key];
  831. if ($type == 'date')
  832. {
  833. $showsize=10;
  834. $value=dol_print_date($value,'day');
  835. }
  836. elseif ($type == 'datetime')
  837. {
  838. $showsize=19;
  839. $value=dol_print_date($value,'dayhour');
  840. }
  841. elseif ($type == 'int')
  842. {
  843. $showsize=10;
  844. }
  845. elseif ($type == 'double')
  846. {
  847. if (!empty($value)) {
  848. $value=price($value);
  849. }
  850. }
  851. elseif ($type == 'boolean')
  852. {
  853. $checked='';
  854. if (!empty($value)) {
  855. $checked=' checked="checked" ';
  856. }
  857. $value='<input type="checkbox" '.$checked.' '.($moreparam?$moreparam:'').' readonly="readonly" disabled="disabled">';
  858. }
  859. elseif ($type == 'mail')
  860. {
  861. $value=dol_print_email($value);
  862. }
  863. elseif ($type == 'phone')
  864. {
  865. $value=dol_print_phone($value);
  866. }
  867. elseif ($type == 'price')
  868. {
  869. $value=price($value,0,$langs,0,0,-1,$conf->currency);
  870. }
  871. elseif ($type == 'select')
  872. {
  873. $value=$params['options'][$value];
  874. }
  875. elseif ($type == 'sellist')
  876. {
  877. $param_list=array_keys($params['options']);
  878. $InfoFieldList = explode(":", $param_list[0]);
  879. $selectkey="rowid";
  880. $keyList='rowid';
  881. if (count($InfoFieldList)>=3)
  882. {
  883. $selectkey = $InfoFieldList[2];
  884. $keyList=$InfoFieldList[2].' as rowid';
  885. }
  886. $fields_label = explode('|',$InfoFieldList[1]);
  887. if(is_array($fields_label)) {
  888. $keyList .=', ';
  889. $keyList .= implode(', ', $fields_label);
  890. }
  891. $sql = 'SELECT '.$keyList;
  892. $sql.= ' FROM '.MAIN_DB_PREFIX .$InfoFieldList[0];
  893. if (strpos($InfoFieldList[4], 'extra')!==false)
  894. {
  895. $sql.= ' as main';
  896. }
  897. $sql.= " WHERE ".$selectkey."='".$this->db->escape($value)."'";
  898. //$sql.= ' AND entity = '.$conf->entity;
  899. dol_syslog(get_class($this).':showOutputField:$type=sellist', LOG_DEBUG);
  900. $resql = $this->db->query($sql);
  901. if ($resql)
  902. {
  903. $value=''; // value was used, so now we reste it to use it to build final output
  904. $obj = $this->db->fetch_object($resql);
  905. // Several field into label (eq table:code|libelle:rowid)
  906. $fields_label = explode('|',$InfoFieldList[1]);
  907. if(is_array($fields_label) && count($fields_label)>1)
  908. {
  909. foreach ($fields_label as $field_toshow)
  910. {
  911. $translabel='';
  912. if (!empty($obj->$field_toshow)) {
  913. $translabel=$langs->trans($obj->$field_toshow);
  914. }
  915. if ($translabel!=$field_toshow) {
  916. $value.=dol_trunc($translabel,18).' ';
  917. }else {
  918. $value.=$obj->$field_toshow.' ';
  919. }
  920. }
  921. }
  922. else
  923. {
  924. $translabel='';
  925. if (!empty($obj->$InfoFieldList[1])) {
  926. $translabel=$langs->trans($obj->$InfoFieldList[1]);
  927. }
  928. if ($translabel!=$obj->$InfoFieldList[1]) {
  929. $value=dol_trunc($translabel,18);
  930. }else {
  931. $value=$obj->$InfoFieldList[1];
  932. }
  933. }
  934. }
  935. else dol_syslog(get_class($this).'::showOutputField error '.$this->db->lasterror(), LOG_WARNING);
  936. }
  937. elseif ($type == 'radio')
  938. {
  939. $value=$params['options'][$value];
  940. }
  941. elseif ($type == 'checkbox')
  942. {
  943. $value_arr=explode(',',$value);
  944. $value='';
  945. if (is_array($value_arr))
  946. {
  947. foreach ($value_arr as $keyval=>$valueval) {
  948. $value.=$params['options'][$valueval].'<br>';
  949. }
  950. }
  951. }
  952. if ($type == 'text')
  953. {
  954. $value=dol_htmlentitiesbr($value);
  955. }
  956. else
  957. {
  958. $showsize=round($size);
  959. if ($showsize > 48) $showsize=48;
  960. }
  961. //print $type.'-'.$size;
  962. $out=$value;
  963. return $out;
  964. }
  965. /**
  966. * Return HTML string to print separator extrafield
  967. *
  968. * @param string $key Key of attribute
  969. * @return string
  970. */
  971. function showSeparator($key)
  972. {
  973. $out = '<tr class="liste_titre"><td colspan="4"><strong>'.$this->attribute_label[$key].'</strong></td></tr>';
  974. return $out;
  975. }
  976. /**
  977. * Fill array_options property of object by extrafields value (using for data sent by forms)
  978. *
  979. * @param array $extralabels $array of extrafields
  980. * @param object $object Object
  981. * @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.
  982. * @return int 1 if array_options set, 0 if no value, -1 if error (field required missing for example)
  983. */
  984. function setOptionalsFromPost($extralabels,&$object,$onlykey='')
  985. {
  986. global $_POST, $langs;
  987. $nofillrequired='';// For error when required field left blank
  988. $error_field_required = array();
  989. if (is_array($extralabels))
  990. {
  991. // Get extra fields
  992. foreach ($extralabels as $key => $value)
  993. {
  994. if (! empty($onlykey) && $key != $onlykey) continue;
  995. $key_type = $this->attribute_type[$key];
  996. if($this->attribute_required[$key] && !GETPOST("options_$key",2))
  997. {
  998. $nofillrequired++;
  999. $error_field_required[] = $value;
  1000. }
  1001. if (in_array($key_type,array('date','datetime')))
  1002. {
  1003. // Clean parameters
  1004. $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"]);
  1005. }
  1006. else if (in_array($key_type,array('checkbox')))
  1007. {
  1008. $value_arr=GETPOST("options_".$key);
  1009. if (!empty($value_arr)) {
  1010. $value_key=implode($value_arr,',');
  1011. }else {
  1012. $value_key='';
  1013. }
  1014. }
  1015. else if (in_array($key_type,array('price','double')))
  1016. {
  1017. $value_arr=GETPOST("options_".$key);
  1018. $value_key=price2num($value_arr);
  1019. }
  1020. else
  1021. {
  1022. $value_key=GETPOST("options_".$key);
  1023. }
  1024. $object->array_options["options_".$key]=$value_key;
  1025. }
  1026. if($nofillrequired) {
  1027. $langs->load('errors');
  1028. setEventMessage($langs->trans('ErrorFieldsRequired').' : '.implode(', ',$error_field_required),'errors');
  1029. return -1;
  1030. }
  1031. else {
  1032. return 1;
  1033. }
  1034. }
  1035. else {
  1036. return 0;
  1037. }
  1038. }
  1039. /**
  1040. * return array_options array for object by extrafields value (using for data send by forms)
  1041. *
  1042. * @param array $extralabels $array of extrafields
  1043. * @param string $keyprefix Prefix string to add into name and id of field (can be used to avoid duplicate names)
  1044. * @return int 1 if array_options set / 0 if no value
  1045. */
  1046. function getOptionalsFromPost($extralabels,$keyprefix='')
  1047. {
  1048. global $_POST;
  1049. $array_options = array();
  1050. if (is_array($extralabels))
  1051. {
  1052. // Get extra fields
  1053. foreach ($extralabels as $key => $value)
  1054. {
  1055. $key_type = $this->attribute_type[$key];
  1056. if (in_array($key_type,array('date','datetime')))
  1057. {
  1058. // Clean parameters
  1059. $value_key=dol_mktime($_POST["options_".$key.$keyprefix."hour"], $_POST["options_".$key.$keyprefix."min"], 0, $_POST["options_".$key.$keyprefix."month"], $_POST["options_".$key.$keyprefix."day"], $_POST["options_".$key.$keyprefix."year"]);
  1060. }
  1061. else if (in_array($key_type,array('checkbox')))
  1062. {
  1063. $value_arr=GETPOST("options_".$key.$keyprefix);
  1064. $value_key=implode($value_arr,',');
  1065. }
  1066. else if (in_array($key_type,array('price','double')))
  1067. {
  1068. $value_arr=GETPOST("options_".$key.$keyprefix);
  1069. $value_key=price2num($value_arr);
  1070. }
  1071. else
  1072. {
  1073. $value_key=GETPOST("options_".$key.$keyprefix);
  1074. }
  1075. $array_options["options_".$key]=$value_key; // No keyprefix here. keyprefix is used only for read.
  1076. }
  1077. return $array_options;
  1078. }
  1079. else {
  1080. return 0;
  1081. }
  1082. }
  1083. }