PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/speedealing/speedealing
PHP | 337 lines | 193 code | 43 blank | 101 comment | 57 complexity | d20965d58cbb8860a511fd358eb26b14 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  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.houssin@capnetworks.com>
  8. * Copyright (C) 2012 Herve Prot <herve.prot@symeos.com>
  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. require_once(DOL_DOCUMENT_ROOT . "/core/class/nosqlDocument.class.php");
  24. class ExtraFields extends nosqlDocument {
  25. var $db;
  26. // Tableau contenant le nom des champs en clef et la definition de ces champs
  27. var $attribute_type;
  28. // Tableau contenant le nom des champs en clef et le label de ces champs en value
  29. var $attribute_label;
  30. // Tableau contenant le nom des champs en clef et la taille de ces champs en value
  31. var $attribute_size;
  32. // Tableau contenant le statut unique ou non
  33. var $attribute_unique;
  34. var $error;
  35. var $errno;
  36. var $type2label;
  37. /**
  38. * Constructor
  39. *
  40. * @param DoliDB $db Database handler
  41. */
  42. function __construct($db = null) {
  43. parent::__construct($db);
  44. $this->useDatabase("system");
  45. $this->type2label = array(
  46. 'text' => 'TextLong',
  47. 'int' => 'Int',
  48. 'double' => 'Float',
  49. 'select' => "Select",
  50. 'date' => 'Date',
  51. 'datetime' => 'DateAndTime'
  52. );
  53. return 1;
  54. }
  55. /**
  56. * Add a new extra field parameter
  57. *
  58. * @param string $attrname Code of attribute
  59. * @param string $label label of attribute
  60. * @param int $type Type of attribute ('int', 'text', 'varchar', 'date', 'datehour')
  61. * @param int $size Size/length of attribute
  62. * @param string $elementtype Element type ('member', 'product', 'company', ...)
  63. * @param int $unique Is field unique or not
  64. * @return int <=0 if KO, >0 if OK
  65. */
  66. function addExtraField($attrname, $label, $type, $size) {
  67. if (empty($attrname))
  68. return -1;
  69. if (empty($label))
  70. return -1;
  71. // Create field into database
  72. if ($attrname != '' && preg_match("/^\w[a-zA-Z0-9-_]*$/", $attrname)) {
  73. $maxpos = 0;
  74. foreach ($this->fields as $row) {
  75. if ($row->optional) {
  76. if ($row->pos > $maxpos)
  77. $maxpos = $row->pos;
  78. }
  79. }
  80. $this->fields->$attrname->enable = true;
  81. $this->fields->$attrname->pos = $maxpos;
  82. $this->fields->$attrname->edit = true;
  83. $this->fields->$attrname->optional = true; // Is an extrafields create by user
  84. return $this->update($attrname, $label, $type, $size);
  85. } else {
  86. return -1;
  87. }
  88. }
  89. /**
  90. * Delete an optionnal attribute
  91. *
  92. * @param string $attrname Code of attribute to delete
  93. * @return int < 0 if KO, 0 if nothing is done, 1 if OK
  94. */
  95. function delete($attrname) {
  96. $table = '';
  97. if (!empty($attrname) && preg_match("/^\w[a-zA-Z0-9-_]*$/", $attrname) && $this->fields->$attrname->optional) {
  98. unset($this->fields->$attrname);
  99. unset($this->type2label);
  100. $this->record(true);
  101. return 1;
  102. } else {
  103. return 0;
  104. }
  105. }
  106. /**
  107. * Enable or Disable an optional attribut
  108. *
  109. * @param string $attrname Code of attribute to delete
  110. * @param boolean $enable Enable = 1 or Disable = 0
  111. * @return int < 0 if KO, 0 if nothing is done, 1 if OK
  112. */
  113. function setStatus($attrname, $enable = false) {
  114. if (!empty($attrname) && preg_match("/^\w[a-zA-Z0-9-_]*$/", $attrname)) {
  115. $this->fields->$attrname->enable = $enable;
  116. unset($this->type2label);
  117. $this->record(true);
  118. return 1;
  119. } else {
  120. return 0;
  121. }
  122. }
  123. /**
  124. * Modify type of a personalized attribute
  125. *
  126. * @param string $attrname Name of attribute
  127. * @param string $label Label of attribute
  128. * @param string $type Type of attribute
  129. * @param int $length Length of attribute
  130. * @return int >0 if OK, <=0 if KO
  131. */
  132. function update($attrname, $label, $type, $length) {
  133. $table = '';
  134. if (isset($attrname) && $attrname != '' && preg_match("/^\w[a-zA-Z0-9-_]*$/", $attrname)) {
  135. $this->fields->$attrname->type = $type;
  136. $this->fields->$attrname->label = $label;
  137. $this->fields->$attrname->size = $length;
  138. unset($this->type2label);
  139. $this->record(true);
  140. return 1;
  141. } else {
  142. return -1;
  143. }
  144. }
  145. /**
  146. * Load array of labels
  147. *
  148. * @return void
  149. */
  150. function fetch($class) {
  151. global $langs;
  152. require_once DOL_DOCUMENT_ROOT . '/admin/class/dict.class.php';
  153. try {
  154. $this->load("extrafields:" . $class, true); // load and cache
  155. //print_r($this->fields->Status);
  156. } catch (Exception $e) {
  157. }
  158. if (isset($this->fields) && count($this->fields))
  159. foreach ($this->fields as $aRow) {
  160. if (isset($aRow->dict)) {
  161. $dict = new Dict($this->db);
  162. try {
  163. $values = $dict->load($aRow->dict, true);
  164. } catch (Exception $e) {
  165. error_log($aRow->dict . " : Not found");
  166. }
  167. if (is_object($values->values))
  168. $aRow->values = clone $values->values;
  169. else
  170. $aRow->values = $values->values;
  171. }
  172. }
  173. if (isset($this->langs) && count($this->langs) && !empty($langs->defaultlang))
  174. foreach ($this->langs as $aRow) {
  175. $langs->load($aRow);
  176. }
  177. return 1;
  178. }
  179. /**
  180. * Return HTML string to put an input field into a page
  181. *
  182. * @param string $key Key of attribute
  183. * @param string $value Value to show
  184. * @param string $moreparam To add more parametes on html input tag
  185. * @return void
  186. */
  187. function showInputField($key, $value, $moreparam = '') {
  188. global $conf;
  189. $label = $this->fields->$key->label;
  190. $type = $this->fields->$key->type;
  191. $size = $this->fields->$key->size;
  192. if ($type == 'date') {
  193. $showsize = 10;
  194. } elseif ($type == 'datetime') {
  195. $showsize = 19;
  196. } elseif (in_array($type, array('int', 'double'))) {
  197. $showsize = 10;
  198. } else {
  199. $showsize = round($size);
  200. if ($showsize > 48)
  201. $showsize = 48;
  202. }
  203. if (in_array($type, array('date', 'datetime'))) {
  204. $tmp = explode(',', $size);
  205. $newsize = $tmp[0];
  206. $out = '<input type="text" name="options_' . $key . '" size="' . $showsize . '" maxlength="' . $newsize . '" value="' . $value . '"' . ($moreparam ? $moreparam : '') . '>';
  207. } else if (in_array($type, array('int', 'double'))) {
  208. $tmp = explode(',', $size);
  209. $newsize = $tmp[0];
  210. $out = '<input type="text" name="options_' . $key . '" size="' . $showsize . '" maxlength="' . $newsize . '" value="' . $value . '"' . ($moreparam ? $moreparam : '') . '>';
  211. } else if ($type == 'text') {
  212. $out = '<input type="text" name="options_' . $key . '" size="' . $showsize . '" maxlength="' . $size . '" value="' . $value . '"' . ($moreparam ? $moreparam : '') . '>';
  213. } else if ($type == 'textarea') {
  214. require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php';
  215. $doleditor = new DolEditor('options_' . $key, $value, '', 200, 'dolibarr_notes', 'In', false, false, !empty($conf->fckeditor->enabled) && $conf->global->FCKEDITOR_ENABLE_SOCIETE, 5, 100);
  216. $out = $doleditor->Create(1);
  217. }
  218. // Add comments
  219. if ($type == 'date')
  220. $out.=' (YYYY-MM-DD)';
  221. elseif ($type == 'datetime')
  222. $out.=' (YYYY-MM-DD HH:MM:SS)';
  223. return $out;
  224. }
  225. /**
  226. * Return HTML string to put an output field into a page
  227. *
  228. * @param string $key Key of attribute
  229. * @param string $value Value to show
  230. * @param string $moreparam More param
  231. * @return string Formated value
  232. */
  233. function showOutputField($key, $value, $moreparam = '') {
  234. $label = $this->fields->$key->label;
  235. $type = $this->fields->$key->type;
  236. $size = $this->fields->$key->size;
  237. if ($type == 'date') {
  238. $showsize = 10;
  239. } elseif ($type == 'datetime') {
  240. $showsize = 19;
  241. } elseif ($type == 'int') {
  242. $showsize = 10;
  243. } else {
  244. $showsize = round($size);
  245. if ($showsize > 48)
  246. $showsize = 48;
  247. }
  248. //print $type.'-'.$size;
  249. $out = $value;
  250. return $out;
  251. }
  252. /**
  253. * Compare this->position for usort
  254. *
  255. * @param int $a first element
  256. * @param int $b second element
  257. * @return -1,0,1
  258. */
  259. public function compare($a, $b) {
  260. $a1 = $a->pos;
  261. $b1 = $b->pos;
  262. if ($a1 == $b1) {
  263. return 0;
  264. }
  265. return ($a1 > $b1) ? +1 : -1;
  266. }
  267. /**
  268. * Add a model PDF or ODT in list
  269. */
  270. public function setModel($name) {
  271. if (!is_array($this->models))
  272. $this->models = array();
  273. if (!in_array($name, $this->models, true)) {
  274. array_push($this->models, $name);
  275. $this->record(true);
  276. }
  277. return 1;
  278. }
  279. /**
  280. * Remove a model PDF or ODT from list model
  281. */
  282. public function delModel($name) {
  283. if (!is_array($this->models))
  284. $this->models = array();
  285. $result = array_search($name, $this->models);
  286. if ($result !== false) {
  287. unset($this->models[$result]);
  288. $this->models = array_merge($this->models);
  289. $this->record(true);
  290. }
  291. return 1;
  292. }
  293. }
  294. ?>