PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/core/class/extrafields.class.php

https://github.com/asterix14/dolibarr
PHP | 481 lines | 323 code | 45 blank | 113 comment | 88 complexity | e3b06800b014bc3c6ca0313dce797218 MD5 | raw file
Possible License(s): LGPL-2.0
  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-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  7. * Copyright (C) 2009-2011 Regis Houssin <regis@dolibarr.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/core/class/extrafields.class.php
  24. * \ingroup core
  25. * \brief File of class to manage extra fields
  26. */
  27. /**
  28. * \class ExtraFields
  29. * \brief 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. var $error;
  41. /**
  42. * Constructor
  43. *
  44. * @param DoliDB $DB Database handler
  45. */
  46. function ExtraFields($DB)
  47. {
  48. $this->db = $DB ;
  49. $this->error = array();
  50. $this->attribute_type = array();
  51. $this->attribute_label = array();
  52. $this->attribute_size = array();
  53. $this->attribute_elementtype = array();
  54. }
  55. /**
  56. * Add a new extra field parameter
  57. * @param attrname code of attribute
  58. * @param label label of attribute
  59. * @param type Type of attribute ('int', 'text', 'varchar', 'date', 'datehour')
  60. * @param pos Position of attribute
  61. * @param size Size/length of attribute
  62. * @param elementtype Element type ('member', 'product', 'company', ...)
  63. * @return int <=0 if KO, >0 if OK
  64. */
  65. function addExtraField($attrname,$label,$type='',$pos=0,$size=0, $elementtype='member')
  66. {
  67. if (empty($attrname)) return -1;
  68. if (empty($label)) return -1;
  69. $result=$this->create($attrname,$type,$size,$elementtype);
  70. if ($result > 0)
  71. {
  72. $result2=$this->create_label($attrname,$label,$type,$pos,$size,$elementtype);
  73. if ($result2 > 0) return 1;
  74. else return -2;
  75. }
  76. else
  77. {
  78. return -1;
  79. }
  80. }
  81. /**
  82. * Add a new optionnal attribute
  83. * @param attrname code of attribute
  84. * @param type Type of attribute ('int', 'text', 'varchar', 'date', 'datehour')
  85. * @param length Size/length of attribute
  86. * @param elementtype Element type ('member', 'product', 'company', ...)
  87. * @return int <=0 if KO, >0 if OK
  88. */
  89. function create($attrname,$type='varchar',$length=255,$elementtype='member')
  90. {
  91. $table='';
  92. if ($elementtype == 'member') $table='adherent_extrafields';
  93. if ($elementtype == 'company') $table='societe_extrafields';
  94. if ($elementtype == 'contact') $table='socpeople_extrafields';
  95. if ($elementtype == 'product') $table='product_extrafields';
  96. if (empty($table))
  97. {
  98. print 'ErrorBarValueForParameters';
  99. return -1;
  100. }
  101. if (isset($attrname) && $attrname != '' && preg_match("/^\w[a-zA-Z0-9-_]*$/",$attrname))
  102. {
  103. $field_desc = array('type'=>$type, 'value'=>$length);
  104. $result=$this->db->DDLAddField(MAIN_DB_PREFIX.$table, $attrname, $field_desc);
  105. if ($result > 0)
  106. {
  107. return 1;
  108. }
  109. else
  110. {
  111. $this->error=$this->db->lasterror();
  112. return -1;
  113. }
  114. }
  115. else
  116. {
  117. return 0;
  118. }
  119. }
  120. /**
  121. * Add description of a new optionnal attribute
  122. * @param attrname code of attribute
  123. * @param label label of attribute
  124. * @param type Type of attribute ('int', 'text', 'varchar', 'date', 'datehour')
  125. * @param pos Position of attribute
  126. * @param size Size/length of attribute
  127. * @param elementtype Element type ('member', 'product', 'company', ...)
  128. * @return int <=0 if KO, >0 if OK
  129. */
  130. function create_label($attrname,$label='',$type='',$pos=0,$size=0, $elementtype='member')
  131. {
  132. global $conf;
  133. // Clean parameters
  134. if (empty($pos)) $pos=0;
  135. if (isset($attrname) && $attrname != '' && preg_match("/^\w[a-zA-Z0-9-_]*$/",$attrname))
  136. {
  137. $sql = "INSERT INTO ".MAIN_DB_PREFIX."extrafields(name, label, type, pos, size, entity, elementtype)";
  138. $sql.= " VALUES('".$attrname."',";
  139. $sql.= " '".$this->db->escape($label)."',";
  140. $sql.= " '".$type."',";
  141. $sql.= " '".$pos."',";
  142. $sql.= " '".$size."',";
  143. $sql.= " ".$conf->entity.",";
  144. $sql.= " '".$elementtype."'";
  145. $sql.=')';
  146. dol_syslog(get_class($this)."::create_label sql=".$sql);
  147. if ($this->db->query($sql))
  148. {
  149. return 1;
  150. }
  151. else
  152. {
  153. print dol_print_error($this->db);
  154. return 0;
  155. }
  156. }
  157. }
  158. /**
  159. * Delete an optionnal attribute
  160. * @param attrname Code of attribute to delete
  161. * @param elementtype Element type ('member', 'product', 'company', ...)
  162. * @return int < 0 if KO, 0 if nothing is done, 1 if OK
  163. */
  164. function delete($attrname,$elementtype='member')
  165. {
  166. $table='';
  167. if ($elementtype == 'member') $table='adherent_extrafields';
  168. if ($elementtype == 'company') $table='societe_extrafields';
  169. if ($elementtype == 'contact') $table='socpeople_extrafields';
  170. if ($elementtype == 'product') $table='product_extrafields';
  171. if (empty($table))
  172. {
  173. print 'ErrorBarValueForParameters';
  174. return -1;
  175. }
  176. if (! empty($attrname) && preg_match("/^\w[a-zA-Z0-9-_]*$/",$attrname))
  177. {
  178. $result=$this->db->DDLDropField(MAIN_DB_PREFIX.$table,$attrname);
  179. if ($result < 0)
  180. {
  181. $this->error=$this->db->lasterror();
  182. dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
  183. }
  184. $result=$this->delete_label($attrname,$elementtype);
  185. return $result;
  186. }
  187. else
  188. {
  189. return 0;
  190. }
  191. }
  192. /**
  193. * Delete description of an optionnal attribute
  194. * @param attrname Code of attribute to delete
  195. * @param elementtype Element type ('member', 'product', 'company', ...)
  196. * @return int < 0 if KO, 0 if nothing is done, 1 if OK
  197. */
  198. function delete_label($attrname,$elementtype='member')
  199. {
  200. global $conf;
  201. if (isset($attrname) && $attrname != '' && preg_match("/^\w[a-zA-Z0-9-_]*$/",$attrname))
  202. {
  203. $sql = "DELETE FROM ".MAIN_DB_PREFIX."extrafields";
  204. $sql.= " WHERE name = '".$attrname."'";
  205. $sql.= " AND entity = ".$conf->entity;
  206. $sql.= " AND elementtype = '".$elementtype."'";
  207. dol_syslog(get_class($this)."::delete_label sql=".$sql);
  208. if ( $this->db->query( $sql) )
  209. {
  210. return 1;
  211. }
  212. else
  213. {
  214. print dol_print_error($this->db);
  215. return -1;
  216. }
  217. }
  218. else
  219. {
  220. return 0;
  221. }
  222. }
  223. /**
  224. * Modify type of a personalized attribute
  225. * @param attrname name of attribute
  226. * @param type type of attribute
  227. * @param length length of attribute
  228. * @param elementtype Element type ('member', 'product', 'company', ...)
  229. * @return int >0 if OK, <=0 if KO
  230. */
  231. function update($attrname,$type='varchar',$length=255,$elementtype='member')
  232. {
  233. $table='';
  234. if ($elementtype == 'member') $table='adherent_extrafields';
  235. if ($elementtype == 'company') $table='societe_extrafields';
  236. if ($elementtype == 'contact') $table='socpeople_extrafields';
  237. if ($elementtype == 'product') $table='product_extrafields';
  238. if (empty($table))
  239. {
  240. print 'ErrorBarValueForParameters';
  241. return -1;
  242. }
  243. if (isset($attrname) && $attrname != '' && preg_match("/^\w[a-zA-Z0-9-_]*$/",$attrname))
  244. {
  245. $field_desc = array('type'=>$type, 'value'=>$length);
  246. $result=$this->db->DDLUpdateField(MAIN_DB_PREFIX.$table, $attrname, $field_desc);
  247. if ($result > 0)
  248. {
  249. return 1;
  250. }
  251. else
  252. {
  253. $this->error=$this->db->lasterror();
  254. return -1;
  255. }
  256. }
  257. else
  258. {
  259. return 0;
  260. }
  261. }
  262. /**
  263. * Modify description of personalized attribute
  264. * @param attrname name of attribute
  265. * @param label label of attribute
  266. * @param type type of attribute
  267. * @param length length of attribute
  268. * @param elementtype Element type ('member', 'product', 'company', ...)
  269. */
  270. function update_label($attrname,$label,$type,$size,$elementtype='member')
  271. {
  272. global $conf;
  273. dol_syslog(get_class($this)."::update_label $attrname,$label,$type,$size");
  274. if (isset($attrname) && $attrname != '' && preg_match("/^\w[a-zA-Z0-9-_]*$/",$attrname))
  275. {
  276. $this->db->begin();
  277. $sql_del = "DELETE FROM ".MAIN_DB_PREFIX."extrafields";
  278. $sql_del.= " WHERE name = '".$attrname."'";
  279. $sql_del.= " AND entity = ".$conf->entity;
  280. $sql_del.= " AND elementtype = '".$elementtype."'";
  281. dol_syslog(get_class($this)."::update_label sql=".$sql_del);
  282. $resql1=$this->db->query($sql_del);
  283. $sql = "INSERT INTO ".MAIN_DB_PREFIX."extrafields(";
  284. $sql.= " name,"; // This is code
  285. $sql.= " entity,";
  286. $sql.= " label,";
  287. $sql.= " type,";
  288. $sql.= " size,";
  289. $sql.= " elementtype";
  290. $sql.= ") VALUES (";
  291. $sql.= "'".$attrname."',";
  292. $sql.= " ".$conf->entity.",";
  293. $sql.= " '".$this->db->escape($label)."',";
  294. $sql.= " '".$type."',";
  295. $sql.= " '".$size."',";
  296. $sql.= " '".$elementtype."'";
  297. $sql.= ")";
  298. dol_syslog(get_class($this)."::update_label sql=".$sql);
  299. $resql2=$this->db->query($sql);
  300. if ($resql1 && $resql2)
  301. {
  302. $this->db->commit();
  303. return 1;
  304. }
  305. else
  306. {
  307. $this->db->rollback();
  308. print dol_print_error($this->db);
  309. return 0;
  310. }
  311. }
  312. else
  313. {
  314. return 0;
  315. }
  316. }
  317. /**
  318. * Load array of labels
  319. */
  320. function fetch_optionals()
  321. {
  322. $this->fetch_name_optionals_label();
  323. }
  324. /**
  325. * Load array this->attribute_label
  326. */
  327. function fetch_name_optionals_label($elementtype='member')
  328. {
  329. global $conf;
  330. $array_name_label=array();
  331. $sql = "SELECT rowid,name,label,type,size,elementtype";
  332. $sql.= " FROM ".MAIN_DB_PREFIX."extrafields";
  333. $sql.= " WHERE entity = ".$conf->entity;
  334. if ($elementtype) $sql.= " AND elementtype = '".$elementtype."'";
  335. $sql.= " ORDER BY pos";
  336. dol_syslog(get_class($this)."::fetch_name_optionals_label sql=".$sql);
  337. $resql=$this->db->query($sql);
  338. if ($resql)
  339. {
  340. if ($this->db->num_rows($resql))
  341. {
  342. while ($tab = $this->db->fetch_object($resql))
  343. {
  344. // we can add this attribute to adherent object
  345. $array_name_label[$tab->name]=$tab->label;
  346. $this->attribute_type[$tab->name]=$tab->type;
  347. $this->attribute_label[$tab->name]=$tab->label;
  348. $this->attribute_size[$tab->name]=$tab->size;
  349. $this->attribute_elementtype[$tab->name]=$tab->elementtype;
  350. }
  351. }
  352. return $array_name_label;
  353. }
  354. else
  355. {
  356. print dol_print_error($this->db);
  357. }
  358. }
  359. /**
  360. * Return HTML string to put an input field into a page
  361. * @param key Key of attribute
  362. * @param value Value to show
  363. * @param moreparam To add more parametes on html input tag
  364. */
  365. function showInputField($key,$value,$moreparam='')
  366. {
  367. global $conf;
  368. $label=$this->attribute_label[$key];
  369. $type=$this->attribute_type[$key];
  370. $size=$this->attribute_size[$key];
  371. $elementtype=$this->attribute_elementtype[$key];
  372. if ($type == 'date')
  373. {
  374. $showsize=10;
  375. }
  376. elseif ($type == 'datetime')
  377. {
  378. $showsize=19;
  379. }
  380. elseif ($type == 'int')
  381. {
  382. $showsize=10;
  383. }
  384. else
  385. {
  386. $showsize=round($size);
  387. if ($showsize > 48) $showsize=48;
  388. }
  389. if ($type == 'varchar')
  390. {
  391. $out='<input type="text" name="options_'.$key.'" size="'.$showsize.'" maxlength="'.$size.'" value="'.$value.'"'.($moreparam?$moreparam:'').'>';
  392. }
  393. else if ($type == 'text')
  394. {
  395. require_once(DOL_DOCUMENT_ROOT."/core/class/doleditor.class.php");
  396. $doleditor=new DolEditor('options_'.$key,$value,'',200,'dolibarr_notes','In',false,false,$conf->fckeditor->enabled && $conf->global->FCKEDITOR_ENABLE_SOCIETE,5,100);
  397. $out=$doleditor->Create(1);
  398. }
  399. else if ($type == 'date') $out.=' (YYYY-MM-DD)';
  400. else if ($type == 'datetime') $out.=' (YYYY-MM-DD HH:MM:SS)';
  401. return $out;
  402. }
  403. /**
  404. * Return HTML string to put an output field into a page
  405. * @param key Key of attribute
  406. * @param value Value to show
  407. */
  408. function showOutputField($key,$value,$moreparam='')
  409. {
  410. $label=$this->attribute_label[$key];
  411. $type=$this->attribute_type[$key];
  412. $size=$this->attribute_size[$key];
  413. $elementtype=$this->attribute_elementtype[$key];
  414. if ($type == 'date')
  415. {
  416. $showsize=10;
  417. }
  418. elseif ($type == 'datetime')
  419. {
  420. $showsize=19;
  421. }
  422. elseif ($type == 'int')
  423. {
  424. $showsize=10;
  425. }
  426. else
  427. {
  428. $showsize=round($size);
  429. if ($showsize > 48) $showsize=48;
  430. }
  431. //print $type.'-'.$size;
  432. $out=$value;
  433. return $out;
  434. }
  435. }
  436. ?>