skeleton_class.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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) 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 dev/skeletons/skeleton_class.class.php
  23. * \ingroup mymodule othermodule1 othermodule2
  24. * \brief This file is an example for a CRUD class file (Create/Read/Update/Delete)
  25. * Put some comments here
  26. */
  27. // Put here all includes required by your class file
  28. require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php';
  29. //require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
  30. //require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
  31. /**
  32. * Class Skeleton_Class
  33. *
  34. * Put here description of your class
  35. * @see CommonObject
  36. */
  37. class Skeleton_Class extends CommonObject
  38. {
  39. /**
  40. * @var string Id to identify managed objects
  41. */
  42. public $element = 'skeleton';
  43. /**
  44. * @var string Name of table without prefix where object is stored
  45. */
  46. public $table_element = 'skeleton';
  47. /**
  48. * @var Skeleton_ClassLine[] Lines
  49. */
  50. public $lines = array();
  51. /**
  52. * @var mixed Sample property 1
  53. */
  54. public $prop1;
  55. /**
  56. * @var mixed Sample property 2
  57. */
  58. public $prop2;
  59. //...
  60. /**
  61. * Constructor
  62. *
  63. * @param DoliDb $db Database handler
  64. */
  65. public function __construct(DoliDB $db)
  66. {
  67. $this->db = $db;
  68. }
  69. /**
  70. * Create object into database
  71. *
  72. * @param User $user User that creates
  73. * @param bool $notrigger false=launch triggers after, true=disable triggers
  74. *
  75. * @return int <0 if KO, Id of created object if OK
  76. */
  77. public function create(User $user, $notrigger = false)
  78. {
  79. dol_syslog(__METHOD__, LOG_DEBUG);
  80. $error = 0;
  81. // Clean parameters
  82. if (isset($this->prop1)) {
  83. $this->prop1 = trim($this->prop1);
  84. }
  85. if (isset($this->prop2)) {
  86. $this->prop2 = trim($this->prop2);
  87. }
  88. //...
  89. // Check parameters
  90. // Put here code to add control on parameters values
  91. // Insert request
  92. $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this->table_element . '(';
  93. $sql .= ' field1,';
  94. $sql .= ' field2';
  95. //...
  96. $sql .= ') VALUES (';
  97. $sql .= ' \'' . $this->prop1 . '\',';
  98. $sql .= ' \'' . $this->prop2 . '\'';
  99. //...
  100. $sql .= ')';
  101. $this->db->begin();
  102. $resql = $this->db->query($sql);
  103. if (!$resql) {
  104. $error ++;
  105. $this->errors[] = 'Error ' . $this->db->lasterror();
  106. dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
  107. }
  108. if (!$error) {
  109. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
  110. if (!$notrigger) {
  111. // Uncomment this and change MYOBJECT to your own tag if you
  112. // want this action to call a trigger.
  113. //// Call triggers
  114. //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
  115. //if ($result < 0) $error++;
  116. //// End call triggers
  117. }
  118. }
  119. // Commit or rollback
  120. if ($error) {
  121. $this->db->rollback();
  122. return - 1 * $error;
  123. } else {
  124. $this->db->commit();
  125. return $this->id;
  126. }
  127. }
  128. /**
  129. * Load object in memory from the database
  130. *
  131. * @param int $id Id object
  132. * @param string $ref Ref
  133. *
  134. * @return int <0 if KO, 0 if not found, >0 if OK
  135. */
  136. public function fetch($id, $ref = null)
  137. {
  138. dol_syslog(__METHOD__, LOG_DEBUG);
  139. $sql = 'SELECT';
  140. $sql .= ' t.rowid,';
  141. $sql .= ' t.field1,';
  142. $sql .= ' t.field2';
  143. //...
  144. $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
  145. if (null !== $ref) {
  146. $sql .= ' WHERE t.ref = ' . '\'' . $ref . '\'';
  147. } else {
  148. $sql .= ' WHERE t.rowid = ' . $id;
  149. }
  150. $resql = $this->db->query($sql);
  151. if ($resql) {
  152. $numrows = $this->db->num_rows($resql);
  153. if ($numrows) {
  154. $obj = $this->db->fetch_object($resql);
  155. $this->id = $obj->rowid;
  156. $this->prop1 = $obj->field1;
  157. $this->prop2 = $obj->field2;
  158. //...
  159. }
  160. $this->db->free($resql);
  161. if ($numrows) {
  162. return 1;
  163. } else {
  164. return 0;
  165. }
  166. } else {
  167. $this->errors[] = 'Error ' . $this->db->lasterror();
  168. dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
  169. return - 1;
  170. }
  171. }
  172. /**
  173. * Load object in memory from the database
  174. *
  175. * @param string $sortorder Sort Order
  176. * @param string $sortfield Sort field
  177. * @param int $limit offset limit
  178. * @param int $offset offset limit
  179. * @param array $filter filter array
  180. * @param string $filtermode filter mode (AND or OR)
  181. *
  182. * @return int <0 if KO, >0 if OK
  183. */
  184. public function fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter = array(), $filtermode='AND')
  185. {
  186. dol_syslog(__METHOD__, LOG_DEBUG);
  187. $sql = 'SELECT';
  188. $sql .= ' t.rowid,';
  189. $sql .= ' t.field1,';
  190. $sql .= ' t.field2';
  191. //...
  192. $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element. ' as t';
  193. // Manage filter
  194. $sqlwhere = array();
  195. if (count($filter) > 0) {
  196. foreach ($filter as $key => $value) {
  197. $sqlwhere [] = $key . ' LIKE \'%' . $this->db->escape($value) . '%\'';
  198. }
  199. }
  200. if (count($sqlwhere) > 0) {
  201. $sql .= ' WHERE ' . implode(' '.$filtermode.' ', $sqlwhere);
  202. }
  203. if (!empty($sortfield)) {
  204. $sql .= $this->db->order($sortfield,$sortorder);
  205. }
  206. if (!empty($limit)) {
  207. $sql .= ' ' . $this->db->plimit($limit + 1, $offset);
  208. }
  209. $this->lines = array();
  210. $resql = $this->db->query($sql);
  211. if ($resql) {
  212. $num = $this->db->num_rows($resql);
  213. while ($obj = $this->db->fetch_object($resql)) {
  214. $line = new Skeleton_ClassLine();
  215. $line->id = $obj->rowid;
  216. $line->prop1 = $obj->field1;
  217. $line->prop2 = $obj->field2;
  218. $this->lines[$line->id] = $line;
  219. //...
  220. }
  221. $this->db->free($resql);
  222. return $num;
  223. } else {
  224. $this->errors[] = 'Error ' . $this->db->lasterror();
  225. dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
  226. return - 1;
  227. }
  228. }
  229. /**
  230. * Update object into database
  231. *
  232. * @param User $user User that modifies
  233. * @param bool $notrigger false=launch triggers after, true=disable triggers
  234. *
  235. * @return int <0 if KO, >0 if OK
  236. */
  237. public function update(User $user, $notrigger = false)
  238. {
  239. $error = 0;
  240. dol_syslog(__METHOD__, LOG_DEBUG);
  241. // Clean parameters
  242. if (isset($this->prop1)) {
  243. $this->prop1 = trim($this->prop1);
  244. }
  245. if (isset($this->prop2)) {
  246. $this->prop2 = trim($this->prop2);
  247. }
  248. //...
  249. // Check parameters
  250. // Put here code to add a control on parameters values
  251. // Update request
  252. $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET';
  253. $sql .= " field1=".(isset($this->field1)?"'".$this->db->escape($this->field1)."'":"null").",";
  254. $sql .= " field2=".(isset($this->field2)?"'".$this->db->escape($this->field2)."'":"null")."";
  255. //...
  256. $sql .= ' WHERE rowid=' . $this->id;
  257. $this->db->begin();
  258. $resql = $this->db->query($sql);
  259. if (!$resql) {
  260. $error ++;
  261. $this->errors[] = 'Error ' . $this->db->lasterror();
  262. dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
  263. }
  264. if (!$error && !$notrigger) {
  265. // Uncomment this and change MYOBJECT to your own tag if you
  266. // want this action calls a trigger.
  267. //// Call triggers
  268. //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
  269. //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  270. //// End call triggers
  271. }
  272. // Commit or rollback
  273. if ($error) {
  274. $this->db->rollback();
  275. return - 1 * $error;
  276. } else {
  277. $this->db->commit();
  278. return 1;
  279. }
  280. }
  281. /**
  282. * Delete object in database
  283. *
  284. * @param User $user User that deletes
  285. * @param bool $notrigger false=launch triggers after, true=disable triggers
  286. *
  287. * @return int <0 if KO, >0 if OK
  288. */
  289. public function delete(User $user, $notrigger = false)
  290. {
  291. dol_syslog(__METHOD__, LOG_DEBUG);
  292. $error = 0;
  293. $this->db->begin();
  294. if (!$error) {
  295. if (!$notrigger) {
  296. // Uncomment this and change MYOBJECT to your own tag if you
  297. // want this action calls a trigger.
  298. //// Call triggers
  299. //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
  300. //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  301. //// End call triggers
  302. }
  303. }
  304. if (!$error) {
  305. $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this->table_element;
  306. $sql .= ' WHERE rowid=' . $this->id;
  307. $resql = $this->db->query($sql);
  308. if (!$resql) {
  309. $error ++;
  310. $this->errors[] = 'Error ' . $this->db->lasterror();
  311. dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
  312. }
  313. }
  314. // Commit or rollback
  315. if ($error) {
  316. $this->db->rollback();
  317. return - 1 * $error;
  318. } else {
  319. $this->db->commit();
  320. return 1;
  321. }
  322. }
  323. /**
  324. * Load an object from its id and create a new one in database
  325. *
  326. * @param int $fromid Id of object to clone
  327. *
  328. * @return int New id of clone
  329. */
  330. public function createFromClone($fromid)
  331. {
  332. dol_syslog(__METHOD__, LOG_DEBUG);
  333. global $user;
  334. $error = 0;
  335. $object = new Skeleton_Class($this->db);
  336. $this->db->begin();
  337. // Load source object
  338. $object->fetch($fromid);
  339. // Reset object
  340. $object->id = 0;
  341. // Clear fields
  342. // ...
  343. // Create clone
  344. $result = $object->create($user);
  345. // Other options
  346. if ($result < 0) {
  347. $error ++;
  348. $this->errors = $object->errors;
  349. dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
  350. }
  351. // End
  352. if (!$error) {
  353. $this->db->commit();
  354. return $object->id;
  355. } else {
  356. $this->db->rollback();
  357. return - 1;
  358. }
  359. }
  360. /**
  361. * Return a link to the user card (with optionaly the picto)
  362. * Use this->id,this->lastname, this->firstname
  363. *
  364. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  365. * @param string $option On what the link point to
  366. * @param integer $notooltip 1=Disable tooltip
  367. * @param int $maxlen Max length of visible user name
  368. * @param string $morecss Add more css on link
  369. * @return string String with URL
  370. */
  371. function getNomUrl($withpicto=0, $option='', $notooltip=0, $maxlen=24, $morecss='')
  372. {
  373. global $langs, $conf, $db;
  374. global $dolibarr_main_authentication, $dolibarr_main_demo;
  375. global $menumanager;
  376. $result = '';
  377. $companylink = '';
  378. $label = '<u>' . $langs->trans("MyModule") . '</u>';
  379. $label.= '<div width="100%">';
  380. $label.= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
  381. $link = '<a href="'.DOL_URL_ROOT.'/mymodule/card.php?id='.$this->id.'"';
  382. $link.= ($notooltip?'':' title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip'.($morecss?' '.$morecss:'').'"');
  383. $link.= '>';
  384. $linkend='</a>';
  385. if ($withpicto)
  386. {
  387. $result.=($link.img_object(($notooltip?'':$label), 'label', ($notooltip?'':'class="classfortooltip"')).$linkend);
  388. if ($withpicto != 2) $result.=' ';
  389. }
  390. $result.= $link . $this->ref . $linkend;
  391. return $result;
  392. }
  393. /**
  394. * Retourne le libelle du status d'un user (actif, inactif)
  395. *
  396. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  397. * @return string Label of status
  398. */
  399. function getLibStatut($mode=0)
  400. {
  401. return $this->LibStatut($this->status,$mode);
  402. }
  403. /**
  404. * Renvoi le libelle d'un status donne
  405. *
  406. * @param int $status Id status
  407. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  408. * @return string Label of status
  409. */
  410. function LibStatut($status,$mode=0)
  411. {
  412. global $langs;
  413. if ($mode == 0)
  414. {
  415. $prefix='';
  416. if ($status == 1) return $langs->trans('Enabled');
  417. if ($status == 0) return $langs->trans('Disabled');
  418. }
  419. if ($mode == 1)
  420. {
  421. if ($status == 1) return $langs->trans('Enabled');
  422. if ($status == 0) return $langs->trans('Disabled');
  423. }
  424. if ($mode == 2)
  425. {
  426. if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4').' '.$langs->trans('Enabled');
  427. if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5').' '.$langs->trans('Disabled');
  428. }
  429. if ($mode == 3)
  430. {
  431. if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4');
  432. if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5');
  433. }
  434. if ($mode == 4)
  435. {
  436. if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4').' '.$langs->trans('Enabled');
  437. if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5').' '.$langs->trans('Disabled');
  438. }
  439. if ($mode == 5)
  440. {
  441. if ($status == 1) return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'),'statut4');
  442. if ($status == 0) return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'),'statut5');
  443. }
  444. }
  445. /**
  446. * Initialise object with example values
  447. * Id must be 0 if object instance is a specimen
  448. *
  449. * @return void
  450. */
  451. public function initAsSpecimen()
  452. {
  453. $this->id = 0;
  454. $this->prop1 = 'prop1';
  455. $this->prop2 = 'prop2';
  456. }
  457. }
  458. /**
  459. * Class Skeleton_ClassLine
  460. */
  461. class Skeleton_ClassLine
  462. {
  463. /**
  464. * @var int ID
  465. */
  466. public $id;
  467. /**
  468. * @var mixed Sample line property 1
  469. */
  470. public $prop1;
  471. /**
  472. * @var mixed Sample line property 2
  473. */
  474. public $prop2;
  475. }