myobject.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. <?php
  2. /* Copyright (C) 2007-2017 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2014-2016 Juanjo Menent <jmenent@2byte.es>
  4. * Copyright (C) 2015 Florian Henry <florian.henry@open-concept.pro>
  5. * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  6. * Copyright (C) ---Put here your own copyright and developer email---
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file htdocs/modulebuilder/template/class/myobject.class.php
  23. * \ingroup mymodule othermodule1 othermodule2
  24. * \brief This file is an example for a CRUD class file (Create/Read/Update/Delete)
  25. */
  26. // Put here all includes required by your class file
  27. require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php';
  28. //require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
  29. //require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
  30. /**
  31. * Class MyModuleObject
  32. *
  33. * Put here description of your class.
  34. */
  35. class MyModuleObject extends CommonObject
  36. {
  37. /**
  38. * @var string Id to identify managed object
  39. */
  40. public $element = 'mymoduleobject';
  41. /**
  42. * @var string Name of table without prefix where object is stored
  43. */
  44. public $table_element = 'mymoduleobject';
  45. /**
  46. * @var array Array with all fields and their property
  47. */
  48. public $picto = 'generic';
  49. /**
  50. * @var array Array with all fields and their property
  51. */
  52. public $fields;
  53. /**
  54. * @var mixed Sample property 1
  55. */
  56. public $prop1;
  57. /**
  58. * @var mixed Sample property 2
  59. */
  60. public $prop2;
  61. //...
  62. protected $ismultientitymanaged = 1; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  63. public $table_element_line = 'mymoduleobjectdet';
  64. public $class_element_line = 'MyModuleObjectline';
  65. public $fk_element = 'fk_mymoduleobject';
  66. /**
  67. * @var MyModuleObjectLine[] Lines
  68. */
  69. public $lines = array();
  70. /**
  71. * Constructor
  72. *
  73. * @param DoliDb $db Database handler
  74. */
  75. public function __construct(DoliDB $db)
  76. {
  77. $this->db = $db;
  78. }
  79. /**
  80. * Create object into database
  81. *
  82. * @param User $user User that creates
  83. * @param bool $notrigger false=launch triggers after, true=disable triggers
  84. *
  85. * @return int <0 if KO, Id of created object if OK
  86. */
  87. public function create(User $user, $notrigger = false)
  88. {
  89. dol_syslog(__METHOD__, LOG_DEBUG);
  90. $error = 0;
  91. // Clean parameters
  92. if (isset($this->prop1)) {
  93. $this->prop1 = trim($this->prop1);
  94. }
  95. if (isset($this->prop2)) {
  96. $this->prop2 = trim($this->prop2);
  97. }
  98. //...
  99. // Check parameters
  100. // Put here code to add control on parameters values
  101. // Insert request
  102. $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this->table_element . '(';
  103. $sql .= ' field1,';
  104. $sql .= ' field2';
  105. //...
  106. $sql .= ') VALUES (';
  107. $sql .= ' \'' . $this->prop1 . '\',';
  108. $sql .= ' \'' . $this->prop2 . '\'';
  109. //...
  110. $sql .= ')';
  111. $this->db->begin();
  112. $resql = $this->db->query($sql);
  113. if (!$resql) {
  114. $error ++;
  115. $this->errors[] = 'Error ' . $this->db->lasterror();
  116. dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
  117. }
  118. if (!$error) {
  119. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
  120. if (!$notrigger) {
  121. // Uncomment this and change MYOBJECT to your own tag if you
  122. // want this action to call a trigger.
  123. //// Call triggers
  124. //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
  125. //if ($result < 0) $error++;
  126. //// End call triggers
  127. }
  128. }
  129. // Commit or rollback
  130. if ($error) {
  131. $this->db->rollback();
  132. return - 1 * $error;
  133. } else {
  134. $this->db->commit();
  135. return $this->id;
  136. }
  137. }
  138. /**
  139. * Load object in memory from the database
  140. *
  141. * @param int $id Id object
  142. * @param string $ref Ref
  143. *
  144. * @return int <0 if KO, 0 if not found, >0 if OK
  145. */
  146. public function fetch($id, $ref = null)
  147. {
  148. dol_syslog(__METHOD__, LOG_DEBUG);
  149. $sql = 'SELECT';
  150. $sql .= ' t.rowid,';
  151. $sql .= ' t.field1,';
  152. $sql .= ' t.field2';
  153. //...
  154. $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
  155. $sql.= ' WHERE 1 = 1';
  156. if (! empty($conf->multicompany->enabled)) {
  157. $sql .= " AND entity IN (" . getEntity("mymoduleobject", 1) . ")";
  158. }
  159. if (null !== $ref) {
  160. $sql .= ' AND t.ref = ' . '\'' . $ref . '\'';
  161. } else {
  162. $sql .= ' AND t.rowid = ' . $id;
  163. }
  164. $resql = $this->db->query($sql);
  165. if ($resql) {
  166. $numrows = $this->db->num_rows($resql);
  167. if ($numrows) {
  168. $obj = $this->db->fetch_object($resql);
  169. $this->id = $obj->rowid;
  170. $this->prop1 = $obj->field1;
  171. $this->prop2 = $obj->field2;
  172. //...
  173. }
  174. // Retrieve all extrafields for invoice
  175. // fetch optionals attributes and labels
  176. /*
  177. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  178. $extrafields=new ExtraFields($this->db);
  179. $extralabels=$extrafields->fetch_name_optionals_label($this->table_element,true);
  180. $this->fetch_optionals($this->id,$extralabels);
  181. */
  182. // $this->fetch_lines();
  183. $this->db->free($resql);
  184. if ($numrows) {
  185. return 1;
  186. } else {
  187. return 0;
  188. }
  189. } else {
  190. $this->errors[] = 'Error ' . $this->db->lasterror();
  191. dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
  192. return - 1;
  193. }
  194. }
  195. /**
  196. * Load object in memory from the database
  197. *
  198. * @param string $sortorder Sort Order
  199. * @param string $sortfield Sort field
  200. * @param int $limit offset limit
  201. * @param int $offset offset limit
  202. * @param array $filter filter array
  203. * @param string $filtermode filter mode (AND or OR)
  204. *
  205. * @return int <0 if KO, >0 if OK
  206. */
  207. public function fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter = array(), $filtermode='AND')
  208. {
  209. dol_syslog(__METHOD__, LOG_DEBUG);
  210. $sql = 'SELECT';
  211. $sql .= ' t.rowid,';
  212. $sql .= ' t.field1,';
  213. $sql .= ' t.field2';
  214. //...
  215. $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element. ' as t';
  216. // Manage filter
  217. $sqlwhere = array();
  218. if (count($filter) > 0) {
  219. foreach ($filter as $key => $value) {
  220. $sqlwhere [] = $key . ' LIKE \'%' . $this->db->escape($value) . '%\'';
  221. }
  222. }
  223. $sql.= ' WHERE 1 = 1';
  224. if (! empty($conf->multicompany->enabled)) {
  225. $sql .= " AND entity IN (" . getEntity("mymoduleobject", 1) . ")";
  226. }
  227. if (count($sqlwhere) > 0) {
  228. $sql .= ' AND ' . implode(' '.$filtermode.' ', $sqlwhere);
  229. }
  230. if (!empty($sortfield)) {
  231. $sql .= $this->db->order($sortfield,$sortorder);
  232. }
  233. if (!empty($limit)) {
  234. $sql .= ' ' . $this->db->plimit($limit, $offset);
  235. }
  236. $resql = $this->db->query($sql);
  237. if ($resql) {
  238. $num = $this->db->num_rows($resql);
  239. while ($obj = $this->db->fetch_object($resql)) {
  240. $line = new self($this->db);
  241. $line->id = $obj->rowid;
  242. $line->prop1 = $obj->field1;
  243. $line->prop2 = $obj->field2;
  244. //...
  245. }
  246. $this->db->free($resql);
  247. return $num;
  248. } else {
  249. $this->errors[] = 'Error ' . $this->db->lasterror();
  250. dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
  251. return - 1;
  252. }
  253. }
  254. /**
  255. * Update object into database
  256. *
  257. * @param User $user User that modifies
  258. * @param bool $notrigger false=launch triggers after, true=disable triggers
  259. *
  260. * @return int <0 if KO, >0 if OK
  261. */
  262. public function update(User $user, $notrigger = false)
  263. {
  264. dol_syslog(__METHOD__, LOG_DEBUG);
  265. $error = 0;
  266. // Clean parameters
  267. if (isset($this->prop1)) {
  268. $this->prop1 = trim($this->prop1);
  269. }
  270. if (isset($this->prop2)) {
  271. $this->prop2 = trim($this->prop2);
  272. }
  273. //...
  274. // Check parameters
  275. // Put here code to add a control on parameters values
  276. // Update request
  277. $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET';
  278. $sql .= " field1=".(isset($this->field1)?"'".$this->db->escape($this->field1)."'":"null").",";
  279. $sql .= " field2=".(isset($this->field2)?"'".$this->db->escape($this->field2)."'":"null")."";
  280. //...
  281. $sql .= ' WHERE rowid=' . $this->id;
  282. $this->db->begin();
  283. $resql = $this->db->query($sql);
  284. if (!$resql) {
  285. $error ++;
  286. $this->errors[] = 'Error ' . $this->db->lasterror();
  287. dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
  288. }
  289. if (!$error && !$notrigger) {
  290. // Uncomment this and change MYOBJECT to your own tag if you
  291. // want this action calls a trigger.
  292. //// Call triggers
  293. //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
  294. //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  295. //// End call triggers
  296. }
  297. // Commit or rollback
  298. if ($error) {
  299. $this->db->rollback();
  300. return - 1 * $error;
  301. } else {
  302. $this->db->commit();
  303. return 1;
  304. }
  305. }
  306. /**
  307. * Delete object in database
  308. *
  309. * @param User $user User that deletes
  310. * @param bool $notrigger false=launch triggers after, true=disable triggers
  311. *
  312. * @return int <0 if KO, >0 if OK
  313. */
  314. public function delete(User $user, $notrigger = false)
  315. {
  316. dol_syslog(__METHOD__, LOG_DEBUG);
  317. $error = 0;
  318. $this->db->begin();
  319. if (!$error) {
  320. if (!$notrigger) {
  321. // Uncomment this and change MYOBJECT to your own tag if you
  322. // want this action calls a trigger.
  323. //// Call triggers
  324. //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
  325. //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  326. //// End call triggers
  327. }
  328. }
  329. // If you need to delete child tables to, you can insert them here
  330. if (!$error) {
  331. $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this->table_element;
  332. $sql .= ' WHERE rowid=' . $this->id;
  333. $resql = $this->db->query($sql);
  334. if (!$resql) {
  335. $error ++;
  336. $this->errors[] = 'Error ' . $this->db->lasterror();
  337. dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
  338. }
  339. }
  340. // Commit or rollback
  341. if ($error) {
  342. $this->db->rollback();
  343. return - 1 * $error;
  344. } else {
  345. $this->db->commit();
  346. return 1;
  347. }
  348. }
  349. /**
  350. * Load an object from its id and create a new one in database
  351. *
  352. * @param int $fromid Id of object to clone
  353. *
  354. * @return int New id of clone
  355. */
  356. public function createFromClone($fromid)
  357. {
  358. dol_syslog(__METHOD__, LOG_DEBUG);
  359. global $user;
  360. $error = 0;
  361. $object = new MyModuleObject($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. $result = $object->create($user);
  371. // Other options
  372. if ($result < 0) {
  373. $error ++;
  374. $this->errors = $object->errors;
  375. dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
  376. }
  377. // End
  378. if (!$error) {
  379. $this->db->commit();
  380. return $object->id;
  381. } else {
  382. $this->db->rollback();
  383. return - 1;
  384. }
  385. }
  386. /**
  387. * Return a link to the object card (with optionaly the picto)
  388. *
  389. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  390. * @param string $option On what the link point to
  391. * @param int $notooltip 1=Disable tooltip
  392. * @param int $maxlen Max length of visible user name
  393. * @param string $morecss Add more css on link
  394. * @return string String with URL
  395. */
  396. function getNomUrl($withpicto=0, $option='', $notooltip=0, $maxlen=24, $morecss='')
  397. {
  398. global $db, $conf, $langs;
  399. global $dolibarr_main_authentication, $dolibarr_main_demo;
  400. global $menumanager;
  401. if (! empty($conf->dol_no_mouse_hover)) $notooltip=1; // Force disable tooltips
  402. $result = '';
  403. $companylink = '';
  404. $label = '<u>' . $langs->trans("MyModule") . '</u>';
  405. $label.= '<br>';
  406. $label.= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
  407. $url = DOL_URL_ROOT.'/mymodule/'.$this->table_name.'_card.php?id='.$this->id;
  408. $linkclose='';
  409. if (empty($notooltip))
  410. {
  411. if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
  412. {
  413. $label=$langs->trans("ShowProject");
  414. $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
  415. }
  416. $linkclose.=' title="'.dol_escape_htmltag($label, 1).'"';
  417. $linkclose.=' class="classfortooltip'.($morecss?' '.$morecss:'').'"';
  418. }
  419. else $linkclose = ($morecss?' class="'.$morecss.'"':'');
  420. $linkstart = '<a href="'.$url.'"';
  421. $linkstart.=$linkclose.'>';
  422. $linkend='</a>';
  423. if ($withpicto)
  424. {
  425. $result.=($linkstart.img_object(($notooltip?'':$label), 'label', ($notooltip?'':'class="classfortooltip"')).$linkend);
  426. if ($withpicto != 2) $result.=' ';
  427. }
  428. $result.= $linkstart . $this->ref . $linkend;
  429. return $result;
  430. }
  431. /**
  432. * Retourne le libelle du status d'un user (actif, inactif)
  433. *
  434. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  435. * @return string Label of status
  436. */
  437. function getLibStatut($mode=0)
  438. {
  439. return $this->LibStatut($this->status,$mode);
  440. }
  441. /**
  442. * Return the status
  443. *
  444. * @param int $status Id status
  445. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 5=Long label + Picto
  446. * @return string Label of status
  447. */
  448. static function LibStatut($status,$mode=0)
  449. {
  450. global $langs;
  451. if ($mode == 0)
  452. {
  453. $prefix='';
  454. if ($status == 1) return $langs->trans('Enabled');
  455. if ($status == 0) return $langs->trans('Disabled');
  456. }
  457. if ($mode == 1)
  458. {
  459. if ($status == 1) return $langs->trans('Enabled');
  460. if ($status == 0) return $langs->trans('Disabled');
  461. }
  462. if ($mode == 2)
  463. {
  464. if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4').' '.$langs->trans('Enabled');
  465. if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5').' '.$langs->trans('Disabled');
  466. }
  467. if ($mode == 3)
  468. {
  469. if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4');
  470. if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5');
  471. }
  472. if ($mode == 4)
  473. {
  474. if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4').' '.$langs->trans('Enabled');
  475. if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5').' '.$langs->trans('Disabled');
  476. }
  477. if ($mode == 5)
  478. {
  479. if ($status == 1) return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'),'statut4');
  480. if ($status == 0) return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'),'statut5');
  481. }
  482. if ($mode == 6)
  483. {
  484. if ($status == 1) return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'),'statut4');
  485. if ($status == 0) return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'),'statut5');
  486. }
  487. }
  488. /**
  489. * Initialise object with example values
  490. * Id must be 0 if object instance is a specimen
  491. *
  492. * @return void
  493. */
  494. public function initAsSpecimen()
  495. {
  496. $this->id = 0;
  497. $this->prop1 = 'prop1';
  498. $this->prop2 = 'prop2';
  499. }
  500. }
  501. /**
  502. * Class MyModuleObjectLine
  503. */
  504. class MyModuleObjectLine
  505. {
  506. /**
  507. * @var int ID
  508. */
  509. public $id;
  510. /**
  511. * @var mixed Sample line property 1
  512. */
  513. public $prop1;
  514. /**
  515. * @var mixed Sample line property 2
  516. */
  517. public $prop2;
  518. }