defaultvalues.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. /* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2021 Florian HENRY <florian.henry@scopen.fr>
  4. * Copyright (C) 2021 Frédéric France <frederic.france@netlogic.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/core/class/defaultvalues.class.php
  21. * \brief This file is a CRUD class file for DefaultValues (Create/Read/Update/Delete)
  22. */
  23. // Put here all includes required by your class file
  24. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  25. //require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
  26. //require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
  27. /**
  28. * Class for MyObject
  29. */
  30. class DefaultValues extends CommonObject
  31. {
  32. /**
  33. * @var string ID to identify managed object.
  34. */
  35. public $element = 'defaultvalues';
  36. /**
  37. * @var string Name of table without prefix where object is stored. This is also the key used for extrafields management.
  38. */
  39. public $table_element = 'default_values';
  40. /**
  41. * @var int Does this object support multicompany module ?
  42. * 0=No test on entity, 1=Test with field entity, 'field@table'=Test with link by field@table
  43. */
  44. public $ismultientitymanaged = 1;
  45. /**
  46. * @var int Does object support extrafields ? 0=No, 1=Yes
  47. */
  48. public $isextrafieldmanaged = 0;
  49. /**
  50. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  51. */
  52. public $picto = '';
  53. /**
  54. * 'type' field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter]]', 'sellist:TableName:LabelFieldName[:KeyFieldName[:KeyFieldParent[:Filter]]]', 'varchar(x)', 'double(24,8)', 'real', 'price', 'text', 'text:none', 'html', 'date', 'datetime', 'timestamp', 'duration', 'mail', 'phone', 'url', 'password')
  55. * Note: Filter can be a string like "(t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL)"
  56. * 'label' the translation key.
  57. * 'picto' is code of a picto to show before value in forms
  58. * 'enabled' is a condition when the field must be managed (Example: 1 or '$conf->global->MY_SETUP_PARAM)
  59. * 'position' is the sort order of field.
  60. * 'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
  61. * 'visible' says if field is visible in list (Examples: 0=Not visible, 1=Visible on list and create/update/view forms, 2=Visible on list only, 3=Visible on create/update/view form only (not list), 4=Visible on list and update/view form only (not create). 5=Visible on list and view only (not create/not update). Using a negative value means field is not shown by default on list but can be selected for viewing)
  62. * 'noteditable' says if field is not editable (1 or 0)
  63. * 'default' is a default value for creation (can still be overwrote by the Setup of Default Values if field is editable in creation form). Note: If default is set to '(PROV)' and field is 'ref', the default value will be set to '(PROVid)' where id is rowid when a new record is created.
  64. * 'index' if we want an index in database.
  65. * 'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...).
  66. * 'searchall' is 1 if we want to search in this field when making a search from the quick search button.
  67. * 'isameasure' must be set to 1 if you want to have a total on list for this field. Field type must be summable like integer or double(24,8).
  68. * 'css' and 'cssview' and 'csslist' is the CSS style to use on field. 'css' is used in creation and update. 'cssview' is used in view mode. 'csslist' is used for columns in lists. For example: 'maxwidth200', 'wordbreak', 'tdoverflowmax200'
  69. * 'help' is a 'TranslationString' to use to show a tooltip on field. You can also use 'TranslationString:keyfortooltiponlick' for a tooltip on click.
  70. * 'showoncombobox' if value of the field must be visible into the label of the combobox that list record
  71. * 'disabled' is 1 if we want to have the field locked by a 'disabled' attribute. In most cases, this is never set into the definition of $fields into class, but is set dynamically by some part of code.
  72. * 'arrayofkeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel")
  73. * 'autofocusoncreate' to have field having the focus on a create form. Only 1 field should have this property set to 1.
  74. * 'comment' is not used. You can store here any text of your choice. It is not used by application.
  75. *
  76. * Note: To have value dynamic, you can set value to 0 in definition and edit the value on the fly into the constructor.
  77. */
  78. // BEGIN MODULEBUILDER PROPERTIES
  79. /**
  80. * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
  81. */
  82. public $fields=array(
  83. 'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
  84. 'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>15, 'index'=>1),
  85. 'type' =>array('type'=>'varchar(10)', 'label'=>'Type', 'enabled'=>1, 'visible'=>-1, 'position'=>20),
  86. 'user_id' =>array('type'=>'integer', 'label'=>'Userid', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>25),
  87. 'page' =>array('type'=>'varchar(255)', 'label'=>'RelativeURL', 'enabled'=>1, 'visible'=>-1, 'position'=>30),
  88. 'param' =>array('type'=>'varchar(255)', 'label'=>'Field', 'enabled'=>1, 'visible'=>-1, 'position'=>35),
  89. 'value' =>array('type'=>'varchar(128)', 'label'=>'Value', 'enabled'=>1, 'visible'=>-1, 'position'=>40),
  90. );
  91. /**
  92. * @var int ID
  93. */
  94. public $rowid;
  95. /**
  96. * @var int Entity
  97. */
  98. public $entity;
  99. /**
  100. * @var string Type
  101. */
  102. public $type;
  103. /**
  104. * @var int User Id
  105. */
  106. public $user_id;
  107. /**
  108. * @var string Page
  109. */
  110. public $page;
  111. /**
  112. * @var string Param
  113. */
  114. public $param;
  115. /**
  116. * @var string Value
  117. */
  118. public $value;
  119. // END MODULEBUILDER PROPERTIES
  120. /**
  121. * Constructor
  122. *
  123. * @param DoliDb $db Database handler
  124. */
  125. public function __construct(DoliDB $db)
  126. {
  127. global $conf, $langs;
  128. $this->db = $db;
  129. if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields['rowid'])) $this->fields['rowid']['visible'] = 0;
  130. if (empty($conf->multicompany->enabled) && isset($this->fields['entity'])) $this->fields['entity']['enabled'] = 0;
  131. // Unset fields that are disabled
  132. foreach ($this->fields as $key => $val) {
  133. if (isset($val['enabled']) && empty($val['enabled'])) {
  134. unset($this->fields[$key]);
  135. }
  136. }
  137. // Translate some data of arrayofkeyval
  138. if (is_object($langs)) {
  139. foreach ($this->fields as $key => $val) {
  140. if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval'])) {
  141. foreach ($val['arrayofkeyval'] as $key2 => $val2) {
  142. $this->fields[$key]['arrayofkeyval'][$key2] = $langs->trans($val2);
  143. }
  144. }
  145. }
  146. }
  147. }
  148. /**
  149. * Create object into database
  150. *
  151. * @param User $user User that creates
  152. * @param bool $notrigger false=launch triggers after, true=disable triggers
  153. * @return int <0 if KO, Id of created object if OK
  154. */
  155. public function create(User $user, $notrigger = false)
  156. {
  157. return $this->createCommon($user, $notrigger);
  158. }
  159. /**
  160. * Clone an object into another one
  161. *
  162. * @param User $user User that creates
  163. * @param int $fromid Id of object to clone
  164. * @return mixed New object created, <0 if KO
  165. */
  166. public function createFromClone(User $user, $fromid)
  167. {
  168. global $langs, $extrafields;
  169. $error = 0;
  170. dol_syslog(__METHOD__, LOG_DEBUG);
  171. $object = new self($this->db);
  172. $this->db->begin();
  173. // Load source object
  174. $result = $object->fetchCommon($fromid);
  175. // Reset some properties
  176. unset($object->id);
  177. // Create clone
  178. $object->context['createfromclone'] = 'createfromclone';
  179. $result = $object->createCommon($user);
  180. if ($result < 0) {
  181. $error++;
  182. $this->error = $object->error;
  183. $this->errors = $object->errors;
  184. }
  185. unset($object->context['createfromclone']);
  186. // End
  187. if (!$error) {
  188. $this->db->commit();
  189. return $object;
  190. } else {
  191. $this->db->rollback();
  192. return -1;
  193. }
  194. }
  195. /**
  196. * Load object in memory from the database
  197. *
  198. * @param int $id Id object
  199. * @return int <0 if KO, 0 if not found, >0 if OK
  200. */
  201. public function fetch($id)
  202. {
  203. $result = $this->fetchCommon($id, null);
  204. return $result;
  205. }
  206. /**
  207. * Load list of objects in memory from the database.
  208. *
  209. * @param string $sortorder Sort Order
  210. * @param string $sortfield Sort field
  211. * @param int $limit limit
  212. * @param int $offset Offset
  213. * @param array $filter Filter array. Example array('field'=>'valueforlike', 'customurl'=>...)
  214. * @param string $filtermode Filter mode (AND or OR)
  215. * @return array|int int <0 if KO, array of pages if OK
  216. */
  217. public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
  218. {
  219. global $conf;
  220. dol_syslog(__METHOD__, LOG_DEBUG);
  221. $records = array();
  222. $sql = 'SELECT ';
  223. $sql .= $this->getFieldList();
  224. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  225. $sql .= ' WHERE 1 = 1';
  226. // Manage filter
  227. $sqlwhere = array();
  228. if (count($filter) > 0) {
  229. foreach ($filter as $key => $value) {
  230. if ($key == 't.rowid' || ($key == 't.entity' && !is_array($value)) || ($key == 't.user_id' && !is_array($value))) {
  231. $sqlwhere[] = $key." = ".((int) $value);
  232. } elseif (isset($this->fields[$key]) && in_array($this->fields[$key]['type'], array('date', 'datetime', 'timestamp'))) {
  233. $sqlwhere[] = $key." = '".$this->db->idate($value)."'";
  234. } elseif ($key == 't.page' || $key == 't.param' || $key == 't.type') {
  235. $sqlwhere[] = $key." = '".$this->db->escape($value)."'";
  236. } elseif ($key == 'customsql') {
  237. $sqlwhere[] = $value;
  238. } elseif (is_array($value)) {
  239. $sqlwhere[] = $key." IN (".$this->db->sanitize(implode(',', $value)).")";
  240. } else {
  241. $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
  242. }
  243. }
  244. }
  245. if (count($sqlwhere) > 0) {
  246. $sql .= ' AND ('.implode(' '.$filtermode.' ', $sqlwhere).')';
  247. }
  248. if (!empty($sortfield)) {
  249. $sql .= $this->db->order($sortfield, $sortorder);
  250. }
  251. if (!empty($limit)) {
  252. $sql .= $this->db->plimit($limit, $offset);
  253. }
  254. $resql = $this->db->query($sql);
  255. if ($resql) {
  256. $num = $this->db->num_rows($resql);
  257. $i = 0;
  258. while ($i < ($limit ? min($limit, $num) : $num)) {
  259. $obj = $this->db->fetch_object($resql);
  260. $record = new self($this->db);
  261. $record->setVarsFromFetchObj($obj);
  262. $records[$record->id] = $record;
  263. $i++;
  264. }
  265. $this->db->free($resql);
  266. return $records;
  267. } else {
  268. $this->errors[] = 'Error '.$this->db->lasterror();
  269. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  270. return -1;
  271. }
  272. }
  273. /**
  274. * Update object into database
  275. *
  276. * @param User $user User that modifies
  277. * @param bool $notrigger false=launch triggers after, true=disable triggers
  278. * @return int <0 if KO, >0 if OK
  279. */
  280. public function update(User $user, $notrigger = false)
  281. {
  282. return $this->updateCommon($user, $notrigger);
  283. }
  284. /**
  285. * Delete object in database
  286. *
  287. * @param User $user User that deletes
  288. * @param bool $notrigger false=launch triggers after, true=disable triggers
  289. * @return int <0 if KO, >0 if OK
  290. */
  291. public function delete(User $user, $notrigger = false)
  292. {
  293. return $this->deleteCommon($user, $notrigger);
  294. }
  295. /**
  296. * Initialise object with example values
  297. * Id must be 0 if object instance is a specimen
  298. *
  299. * @return void
  300. */
  301. public function initAsSpecimen()
  302. {
  303. $this->initAsSpecimenCommon();
  304. }
  305. }