cgenericdic.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. <?php
  2. /* Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2014-2016 Juanjo Menent <jmenent@2byte.es>
  4. * Copyright (C) 2016 Florian Henry <florian.henry@atm-consulting.fr>
  5. * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/core/class/cgenericdic.class.php
  22. * \ingroup resource
  23. */
  24. /**
  25. * Class CGenericDic
  26. *
  27. * @see CommonObject
  28. */
  29. class CGenericDic
  30. {
  31. /**
  32. * @var string Id to identify managed objects
  33. */
  34. public $element = 'undefined'; // Will be defined into constructor
  35. /**
  36. * @var string Name of table without prefix where object is stored
  37. */
  38. public $table_element = 'undefined'; // Will be defined into constructor
  39. /**
  40. * @var CtyperesourceLine[] Lines
  41. */
  42. public $lines = array();
  43. public $code;
  44. /**
  45. * @var string Type resource label
  46. */
  47. public $label;
  48. public $active;
  49. /**
  50. * Constructor
  51. *
  52. * @param DoliDb $db Database handler
  53. */
  54. public function __construct(DoliDB $db)
  55. {
  56. $this->db = $db;
  57. // Don't forget to set this->element and this->table_element after the construction
  58. }
  59. /**
  60. * Create object into database
  61. *
  62. * @param User $user User that creates
  63. * @param bool $notrigger false=launch triggers after, true=disable triggers
  64. *
  65. * @return int <0 if KO, Id of created object if OK
  66. */
  67. public function create(User $user, $notrigger = false)
  68. {
  69. dol_syslog(__METHOD__, LOG_DEBUG);
  70. $fieldlabel = 'label';
  71. if ($this->table_element == 'c_stcomm') {
  72. $fieldlabel = 'libelle';
  73. } elseif ($this->table_element == 'c_type_fees') {
  74. $fieldrowid = 'id';
  75. }
  76. $error = 0;
  77. // Clean parameters
  78. if (isset($this->code)) {
  79. $this->code = trim($this->code);
  80. }
  81. if (isset($this->label)) {
  82. $this->label = trim($this->label);
  83. }
  84. if (isset($this->active)) {
  85. $this->active = trim($this->active);
  86. }
  87. // Insert request
  88. $sql = 'INSERT INTO '.$this->db->prefix().$this->table_element.'(';
  89. $sql .= 'code,';
  90. $sql .= $fieldlabel;
  91. $sql .= 'active';
  92. $sql .= ') VALUES (';
  93. $sql .= ' '.(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").',';
  94. $sql .= ' '.(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").',';
  95. $sql .= ' '.(!isset($this->active) ? 'NULL' : $this->active);
  96. $sql .= ')';
  97. $this->db->begin();
  98. $resql = $this->db->query($sql);
  99. if (!$resql) {
  100. $error++;
  101. $this->errors[] = 'Error '.$this->db->lasterror();
  102. dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
  103. }
  104. if (!$error) {
  105. $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
  106. // Uncomment this and change CTYPERESOURCE to your own tag if you
  107. // want this action to call a trigger.
  108. //if (!$notrigger) {
  109. // // Call triggers
  110. // $result=$this->call_trigger('CTYPERESOURCE_CREATE',$user);
  111. // if ($result < 0) $error++;
  112. // // End call triggers
  113. //}
  114. }
  115. // Commit or rollback
  116. if ($error) {
  117. $this->db->rollback();
  118. return -1 * $error;
  119. } else {
  120. $this->db->commit();
  121. return $this->id;
  122. }
  123. }
  124. /**
  125. * Load object in memory from the database
  126. *
  127. * @param int $id Id object
  128. * @param string $code code
  129. * @param string $label Label
  130. *
  131. * @return int <0 if KO, 0 if not found, >0 if OK
  132. */
  133. public function fetch($id, $code = '', $label = '')
  134. {
  135. dol_syslog(__METHOD__, LOG_DEBUG);
  136. $fieldrowid = 'rowid';
  137. $fieldlabel = 'label';
  138. if ($this->table_element == 'c_stcomm') {
  139. $fieldrowid = 'id';
  140. $fieldlabel = 'libelle';
  141. } elseif ($this->table_element == 'c_type_fees') {
  142. $fieldrowid = 'id';
  143. }
  144. $sql = "SELECT";
  145. $sql .= " t.".$fieldrowid.",";
  146. $sql .= " t.code,";
  147. $sql .= " t.".$fieldlabel." as label,";
  148. $sql .= " t.active";
  149. $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
  150. if ($id) {
  151. $sql .= " WHERE t.".$fieldrowid." = ".((int) $id);
  152. } elseif ($code) {
  153. $sql .= " WHERE t.code = '".$this->db->escape($code)."'";
  154. } elseif ($label) {
  155. $sql .= " WHERE t.label = '".$this->db->escape($label)."'";
  156. }
  157. $resql = $this->db->query($sql);
  158. if ($resql) {
  159. $numrows = $this->db->num_rows($resql);
  160. if ($numrows) {
  161. $obj = $this->db->fetch_object($resql);
  162. $this->id = $obj->$fieldrowid;
  163. $this->code = $obj->code;
  164. $this->label = $obj->label;
  165. $this->active = $obj->active;
  166. }
  167. // Retrieve all extrafields for invoice
  168. // fetch optionals attributes and labels
  169. // $this->fetch_optionals();
  170. // $this->fetch_lines();
  171. $this->db->free($resql);
  172. if ($numrows) {
  173. return 1;
  174. } else {
  175. return 0;
  176. }
  177. } else {
  178. $this->errors[] = 'Error '.$this->db->lasterror();
  179. dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
  180. return -1;
  181. }
  182. }
  183. /**
  184. * Load object in memory from the database
  185. *
  186. * @param string $sortorder Sort Order
  187. * @param string $sortfield Sort field
  188. * @param int $limit offset limit
  189. * @param int $offset offset limit
  190. * @param array $filter filter array
  191. * @param string $filtermode filter mode (AND or OR)
  192. *
  193. * @return int <0 if KO, >0 if OK
  194. */
  195. public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
  196. {
  197. dol_syslog(__METHOD__, LOG_DEBUG);
  198. $fieldrowid = 'rowid';
  199. $fieldlabel = 'label';
  200. if ($this->table_element == 'c_stcomm') {
  201. $fieldrowid = 'id';
  202. $fieldlabel = 'libelle';
  203. } elseif ($this->table_element == 'c_type_fees') {
  204. $fieldrowid = 'id';
  205. }
  206. $sql = "SELECT";
  207. $sql .= " t.".$fieldrowid.",";
  208. $sql .= " t.code,";
  209. $sql .= " t.".$fieldlabel." as label,";
  210. $sql .= " t.active";
  211. $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
  212. // Manage filter
  213. $sqlwhere = array();
  214. if (count($filter) > 0) {
  215. foreach ($filter as $key => $value) {
  216. $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
  217. }
  218. }
  219. if (count($sqlwhere) > 0) {
  220. $sql .= " WHERE ".implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
  221. }
  222. if (!empty($sortfield)) {
  223. $sql .= $this->db->order($sortfield, $sortorder);
  224. }
  225. if (!empty($limit)) {
  226. $sql .= $this->db->plimit($limit, $offset);
  227. }
  228. $resql = $this->db->query($sql);
  229. if ($resql) {
  230. $num = $this->db->num_rows($resql);
  231. while ($obj = $this->db->fetch_object($resql)) {
  232. $line = new self($this->db);
  233. $line->id = $obj->$fieldrowid;
  234. $line->code = $obj->code;
  235. $line->label = $obj->label;
  236. $line->active = $obj->active;
  237. }
  238. $this->db->free($resql);
  239. return $num;
  240. } else {
  241. $this->errors[] = 'Error '.$this->db->lasterror();
  242. dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
  243. return -1;
  244. }
  245. }
  246. /**
  247. * Update object into database
  248. *
  249. * @param User $user User that modifies
  250. * @param bool $notrigger false=launch triggers after, true=disable triggers
  251. *
  252. * @return int <0 if KO, >0 if OK
  253. */
  254. public function update(User $user, $notrigger = false)
  255. {
  256. $error = 0;
  257. dol_syslog(__METHOD__, LOG_DEBUG);
  258. $fieldrowid = 'rowid';
  259. $fieldlabel = 'label';
  260. if ($this->table_element == 'c_stcomm') {
  261. $fieldrowid = 'id';
  262. $fieldlabel = 'libelle';
  263. } elseif ($this->table_element == 'c_type_fees') {
  264. $fieldrowid = 'id';
  265. }
  266. // Clean parameters
  267. if (isset($this->code)) {
  268. $this->code = trim($this->code);
  269. }
  270. if (isset($this->label)) {
  271. $this->label = trim($this->label);
  272. }
  273. if (isset($this->active)) {
  274. $this->active = trim($this->active);
  275. }
  276. // Check parameters
  277. // Put here code to add a control on parameters values
  278. // Update request
  279. $sql = "UPDATE ".$this->db->prefix().$this->table_element.' SET';
  280. $sql .= " code = ".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").',';
  281. $sql .= " ".$fieldlabel." = ".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").',';
  282. $sql .= " active = ".(isset($this->active) ? $this->active : "null");
  283. $sql .= " WHERE ".$fieldrowid." = ".((int) $this->id);
  284. $this->db->begin();
  285. $resql = $this->db->query($sql);
  286. if (!$resql) {
  287. $error++;
  288. $this->errors[] = 'Error '.$this->db->lasterror();
  289. dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
  290. }
  291. // Uncomment this and change CTYPERESOURCE to your own tag if you
  292. // want this action calls a trigger.
  293. //if (!$error && !$notrigger) {
  294. // // Call triggers
  295. // $result=$this->call_trigger('CTYPERESOURCE_MODIFY',$user);
  296. // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  297. // // End call triggers
  298. //}
  299. // Commit or rollback
  300. if ($error) {
  301. $this->db->rollback();
  302. return -1 * $error;
  303. } else {
  304. $this->db->commit();
  305. return 1;
  306. }
  307. }
  308. /**
  309. * Delete object in database
  310. *
  311. * @param User $user User that deletes
  312. * @param bool $notrigger false=launch triggers after, true=disable triggers
  313. *
  314. * @return int <0 if KO, >0 if OK
  315. */
  316. public function delete(User $user, $notrigger = false)
  317. {
  318. dol_syslog(__METHOD__, LOG_DEBUG);
  319. $fieldrowid = 'rowid';
  320. $error = 0;
  321. $this->db->begin();
  322. // Uncomment this and change CTYPERESOURCE to your own tag if you
  323. // want this action calls a trigger.
  324. //if (!$error && !$notrigger) {
  325. // // Call triggers
  326. // $result=$this->call_trigger('CTYPERESOURCE_DELETE',$user);
  327. // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  328. // // End call triggers
  329. //}
  330. // If you need to delete child tables to, you can insert them here
  331. if (!$error) {
  332. $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
  333. $sql .= " WHERE ".$fieldrowid." = ".((int) $this->id);
  334. $resql = $this->db->query($sql);
  335. if (!$resql) {
  336. $error++;
  337. $this->errors[] = 'Error '.$this->db->lasterror();
  338. dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
  339. }
  340. }
  341. // Commit or rollback
  342. if ($error) {
  343. $this->db->rollback();
  344. return -1 * $error;
  345. } else {
  346. $this->db->commit();
  347. return 1;
  348. }
  349. }
  350. /**
  351. * Load an object from its id and create a new one in database
  352. *
  353. * @param User $user User making the clone
  354. * @param int $fromid Id of object to clone
  355. * @return int New id of clone
  356. */
  357. public function createFromClone(User $user, $fromid)
  358. {
  359. dol_syslog(__METHOD__, LOG_DEBUG);
  360. $error = 0;
  361. $object = new Ctyperesource($this->db);
  362. $this->db->begin();
  363. // Load source object
  364. $object->fetch($fromid);
  365. // Reset object
  366. $object->id = 0;
  367. // Clear fields
  368. // ...
  369. // Create clone
  370. $object->context['createfromclone'] = 'createfromclone';
  371. $result = $object->create($user);
  372. // Other options
  373. if ($result < 0) {
  374. $error++;
  375. $this->errors = $object->errors;
  376. dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
  377. }
  378. unset($object->context['createfromclone']);
  379. // End
  380. if (!$error) {
  381. $this->db->commit();
  382. return $object->id;
  383. } else {
  384. $this->db->rollback();
  385. return -1;
  386. }
  387. }
  388. /**
  389. * Initialise object with example values
  390. * Id must be 0 if object instance is a specimen
  391. *
  392. * @return void
  393. */
  394. public function initAsSpecimen()
  395. {
  396. $this->id = 0;
  397. $this->code = 'CODE';
  398. $this->label = 'Label';
  399. $this->active = 1;
  400. }
  401. }