website.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. <?php
  2. /* Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2014 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 websites/website.class.php
  23. * \ingroup websites
  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 Website
  33. *
  34. * Put here description of your class
  35. * @see CommonObject
  36. */
  37. class Website extends CommonObject
  38. {
  39. /**
  40. * @var string Id to identify managed objects
  41. */
  42. public $element = 'website';
  43. /**
  44. * @var string Name of table without prefix where object is stored
  45. */
  46. public $table_element = 'website';
  47. /**
  48. * @var WebsitePage[] Lines of all pages
  49. */
  50. public $lines = array();
  51. /**
  52. * @var int
  53. */
  54. public $entity;
  55. /**
  56. * @var string
  57. */
  58. public $ref;
  59. /**
  60. * @var string
  61. */
  62. public $description;
  63. /**
  64. * @var int
  65. */
  66. public $status;
  67. /**
  68. * @var mixed
  69. */
  70. public $date_creation;
  71. /**
  72. * @var mixed
  73. */
  74. public $date_modification;
  75. /**
  76. * @var mixed
  77. */
  78. public $tms = '';
  79. /**
  80. * @var integer
  81. */
  82. public $fk_default_home;
  83. /**
  84. * @var string
  85. */
  86. public $virtualhost;
  87. public $records;
  88. /**
  89. */
  90. /**
  91. * Constructor
  92. *
  93. * @param DoliDb $db Database handler
  94. */
  95. public function __construct(DoliDB $db)
  96. {
  97. $this->db = $db;
  98. return 1;
  99. }
  100. /**
  101. * Create object into database
  102. *
  103. * @param User $user User that creates
  104. * @param bool $notrigger false=launch triggers after, true=disable triggers
  105. *
  106. * @return int <0 if KO, Id of created object if OK
  107. */
  108. public function create(User $user, $notrigger = false)
  109. {
  110. dol_syslog(__METHOD__, LOG_DEBUG);
  111. $error = 0;
  112. // Clean parameters
  113. if (isset($this->entity)) {
  114. $this->entity = trim($this->entity);
  115. }
  116. if (isset($this->ref)) {
  117. $this->ref = trim($this->ref);
  118. }
  119. if (isset($this->description)) {
  120. $this->description = trim($this->description);
  121. }
  122. if (isset($this->status)) {
  123. $this->status = trim($this->status);
  124. }
  125. // Check parameters
  126. // Put here code to add control on parameters values
  127. // Insert request
  128. $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this->table_element . '(';
  129. $sql.= 'entity,';
  130. $sql.= 'ref,';
  131. $sql.= 'description,';
  132. $sql.= 'status,';
  133. $sql.= 'fk_default_home,';
  134. $sql.= 'virtualhost,';
  135. $sql.= 'date_creation,';
  136. $sql.= 'date_modification';
  137. $sql .= ') VALUES (';
  138. $sql .= ' '.(! isset($this->entity)?'NULL':$this->entity).',';
  139. $sql .= ' '.(! isset($this->ref)?'NULL':"'".$this->db->escape($this->ref)."'").',';
  140. $sql .= ' '.(! isset($this->description)?'NULL':"'".$this->db->escape($this->description)."'").',';
  141. $sql .= ' '.(! isset($this->status)?'NULL':$this->status).',';
  142. $sql .= ' '.(! isset($this->fk_default_home)?'NULL':$this->fk_default_home).',';
  143. $sql .= ' '.(! isset($this->virtualhost)?'NULL':$this->virtualhost).',';
  144. $sql .= ' '.(! isset($this->date_creation) || dol_strlen($this->date_creation)==0?'NULL':"'".$this->db->idate($this->date_creation)."'").',';
  145. $sql .= ' '.(! isset($this->date_modification) || dol_strlen($this->date_modification)==0?'NULL':"'".$this->db->idate($this->date_modification)."'");
  146. $sql .= ')';
  147. $this->db->begin();
  148. $resql = $this->db->query($sql);
  149. if (!$resql) {
  150. $error ++;
  151. $this->errors[] = 'Error ' . $this->db->lasterror();
  152. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  153. }
  154. if (!$error) {
  155. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
  156. if (!$notrigger) {
  157. // Uncomment this and change MYOBJECT to your own tag if you
  158. // want this action to call a trigger.
  159. //// Call triggers
  160. //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
  161. //if ($result < 0) $error++;
  162. //// End call triggers
  163. }
  164. }
  165. // Commit or rollback
  166. if ($error) {
  167. $this->db->rollback();
  168. return - 1 * $error;
  169. } else {
  170. $this->db->commit();
  171. return $this->id;
  172. }
  173. }
  174. /**
  175. * Load object in memory from the database
  176. *
  177. * @param int $id Id object
  178. * @param string $ref Ref
  179. *
  180. * @return int <0 if KO, 0 if not found, >0 if OK
  181. */
  182. public function fetch($id, $ref = null)
  183. {
  184. dol_syslog(__METHOD__, LOG_DEBUG);
  185. $sql = 'SELECT';
  186. $sql .= ' t.rowid,';
  187. $sql .= " t.entity,";
  188. $sql .= " t.ref,";
  189. $sql .= " t.description,";
  190. $sql .= " t.status,";
  191. $sql .= " t.fk_default_home,";
  192. $sql .= " t.virtualhost,";
  193. $sql .= " t.date_creation,";
  194. $sql .= " t.date_modification,";
  195. $sql .= " t.tms";
  196. $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
  197. if (null !== $ref) {
  198. $sql .= ' WHERE t.ref = ' . '\'' . $ref . '\'';
  199. } else {
  200. $sql .= ' WHERE t.rowid = ' . $id;
  201. }
  202. $resql = $this->db->query($sql);
  203. if ($resql) {
  204. $numrows = $this->db->num_rows($resql);
  205. if ($numrows) {
  206. $obj = $this->db->fetch_object($resql);
  207. $this->id = $obj->rowid;
  208. $this->entity = $obj->entity;
  209. $this->ref = $obj->ref;
  210. $this->description = $obj->description;
  211. $this->status = $obj->status;
  212. $this->fk_default_home = $obj->fk_default_home;
  213. $this->virtualhost = $obj->virtualhost;
  214. $this->date_creation = $this->db->jdate($obj->date_creation);
  215. $this->date_modification = $this->db->jdate($obj->date_modification);
  216. $this->tms = $this->db->jdate($obj->tms);
  217. }
  218. $this->db->free($resql);
  219. if ($numrows) {
  220. return 1;
  221. } else {
  222. return 0;
  223. }
  224. } else {
  225. $this->errors[] = 'Error ' . $this->db->lasterror();
  226. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  227. return - 1;
  228. }
  229. }
  230. /**
  231. * Load object in memory from the database
  232. *
  233. * @param string $sortorder Sort Order
  234. * @param string $sortfield Sort field
  235. * @param int $limit offset limit
  236. * @param int $offset offset limit
  237. * @param array $filter filter array
  238. * @param string $filtermode filter mode (AND or OR)
  239. *
  240. * @return int <0 if KO, >0 if OK
  241. */
  242. public function fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter = array(), $filtermode='AND')
  243. {
  244. dol_syslog(__METHOD__, LOG_DEBUG);
  245. $sql = 'SELECT';
  246. $sql .= ' t.rowid,';
  247. $sql .= " t.entity,";
  248. $sql .= " t.ref,";
  249. $sql .= " t.description,";
  250. $sql .= " t.status,";
  251. $sql .= " t.fk_default_home,";
  252. $sql .= " t.virtualhost,";
  253. $sql .= " t.date_creation,";
  254. $sql .= " t.date_modification,";
  255. $sql .= " t.tms";
  256. $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element. ' as t';
  257. // Manage filter
  258. $sqlwhere = array();
  259. if (count($filter) > 0) {
  260. foreach ($filter as $key => $value) {
  261. $sqlwhere [] = $key . ' LIKE \'%' . $this->db->escape($value) . '%\'';
  262. }
  263. }
  264. if (count($sqlwhere) > 0) {
  265. $sql .= ' WHERE ' . implode(' '.$filtermode.' ', $sqlwhere);
  266. }
  267. if (!empty($sortfield)) {
  268. $sql .= $this->db->order($sortfield,$sortorder);
  269. }
  270. if (!empty($limit)) {
  271. $sql .= ' ' . $this->db->plimit($limit + 1, $offset);
  272. }
  273. $this->records = array();
  274. $resql = $this->db->query($sql);
  275. if ($resql) {
  276. $num = $this->db->num_rows($resql);
  277. while ($obj = $this->db->fetch_object($resql)) {
  278. $line = new WebsiteLine();
  279. $line->id = $obj->rowid;
  280. $line->entity = $obj->entity;
  281. $line->ref = $obj->ref;
  282. $line->description = $obj->description;
  283. $line->status = $obj->status;
  284. $line->fk_default_home = $obj->fk_default_home;
  285. $line->virtualhost = $obj->virtualhost;
  286. $line->date_creation = $this->db->jdate($obj->date_creation);
  287. $line->date_modification = $this->db->jdate($obj->date_modification);
  288. $line->tms = $this->db->jdate($obj->tms);
  289. $this->records[$line->id] = $line;
  290. }
  291. $this->db->free($resql);
  292. return $num;
  293. } else {
  294. $this->errors[] = 'Error ' . $this->db->lasterror();
  295. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  296. return - 1;
  297. }
  298. }
  299. /**
  300. * Update object into database
  301. *
  302. * @param User $user User that modifies
  303. * @param bool $notrigger false=launch triggers after, true=disable triggers
  304. *
  305. * @return int <0 if KO, >0 if OK
  306. */
  307. public function update(User $user, $notrigger = false)
  308. {
  309. $error = 0;
  310. dol_syslog(__METHOD__, LOG_DEBUG);
  311. // Clean parameters
  312. if (isset($this->entity)) {
  313. $this->entity = trim($this->entity);
  314. }
  315. if (isset($this->ref)) {
  316. $this->ref = trim($this->ref);
  317. }
  318. if (isset($this->description)) {
  319. $this->description = trim($this->description);
  320. }
  321. if (isset($this->status)) {
  322. $this->status = trim($this->status);
  323. }
  324. // Check parameters
  325. // Put here code to add a control on parameters values
  326. // Update request
  327. $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET';
  328. $sql .= ' entity = '.(isset($this->entity)?$this->entity:"null").',';
  329. $sql .= ' ref = '.(isset($this->ref)?"'".$this->db->escape($this->ref)."'":"null").',';
  330. $sql .= ' description = '.(isset($this->description)?"'".$this->db->escape($this->description)."'":"null").',';
  331. $sql .= ' status = '.(isset($this->status)?$this->status:"null").',';
  332. $sql .= ' fk_default_home = '.(($this->fk_default_home > 0)?$this->fk_default_home:"null").',';
  333. $sql .= ' virtualhost = '.(($this->virtualhost != '')?$this->virtualhost:"null").',';
  334. $sql .= ' date_creation = '.(! isset($this->date_creation) || dol_strlen($this->date_creation) != 0 ? "'".$this->db->idate($this->date_creation)."'" : 'null').',';
  335. $sql .= ' date_modification = '.(! isset($this->date_modification) || dol_strlen($this->date_modification) != 0 ? "'".$this->db->idate($this->date_modification)."'" : 'null').',';
  336. $sql .= ' tms = '.(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : "'".$this->db->idate(dol_now())."'");
  337. $sql .= ' WHERE rowid=' . $this->id;
  338. $this->db->begin();
  339. $resql = $this->db->query($sql);
  340. if (!$resql) {
  341. $error ++;
  342. $this->errors[] = 'Error ' . $this->db->lasterror();
  343. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  344. }
  345. if (!$error && !$notrigger) {
  346. // Uncomment this and change MYOBJECT to your own tag if you
  347. // want this action calls a trigger.
  348. //// Call triggers
  349. //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
  350. //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  351. //// End call triggers
  352. }
  353. // Commit or rollback
  354. if ($error) {
  355. $this->db->rollback();
  356. return - 1 * $error;
  357. } else {
  358. $this->db->commit();
  359. return 1;
  360. }
  361. }
  362. /**
  363. * Delete object in database
  364. *
  365. * @param User $user User that deletes
  366. * @param bool $notrigger false=launch triggers after, true=disable triggers
  367. *
  368. * @return int <0 if KO, >0 if OK
  369. */
  370. public function delete(User $user, $notrigger = false)
  371. {
  372. dol_syslog(__METHOD__, LOG_DEBUG);
  373. $error = 0;
  374. $this->db->begin();
  375. if (!$error) {
  376. if (!$notrigger) {
  377. // Uncomment this and change MYOBJECT to your own tag if you
  378. // want this action calls a trigger.
  379. //// Call triggers
  380. //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
  381. //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  382. //// End call triggers
  383. }
  384. }
  385. if (!$error) {
  386. $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this->table_element;
  387. $sql .= ' WHERE rowid=' . $this->id;
  388. $resql = $this->db->query($sql);
  389. if (!$resql) {
  390. $error ++;
  391. $this->errors[] = 'Error ' . $this->db->lasterror();
  392. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  393. }
  394. }
  395. // Commit or rollback
  396. if ($error) {
  397. $this->db->rollback();
  398. return - 1 * $error;
  399. } else {
  400. $this->db->commit();
  401. return 1;
  402. }
  403. }
  404. /**
  405. * Load an object from its id and create a new one in database
  406. *
  407. * @param int $fromid Id of object to clone
  408. *
  409. * @return int New id of clone
  410. */
  411. public function createFromClone($fromid)
  412. {
  413. dol_syslog(__METHOD__, LOG_DEBUG);
  414. global $user;
  415. $error = 0;
  416. $object = new Website($this->db);
  417. $this->db->begin();
  418. // Load source object
  419. $object->fetch($fromid);
  420. // Reset object
  421. $object->id = 0;
  422. // Clear fields
  423. // ...
  424. // Create clone
  425. $result = $object->create($user);
  426. // Other options
  427. if ($result < 0) {
  428. $error ++;
  429. $this->errors = $object->errors;
  430. dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
  431. }
  432. // End
  433. if (!$error) {
  434. $this->db->commit();
  435. return $object->id;
  436. } else {
  437. $this->db->rollback();
  438. return - 1;
  439. }
  440. }
  441. /**
  442. * Return a link to the user card (with optionaly the picto)
  443. * Use this->id,this->lastname, this->firstname
  444. *
  445. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  446. * @param string $option On what the link point to
  447. * @param integer $notooltip 1=Disable tooltip
  448. * @param int $maxlen Max length of visible user name
  449. * @param string $morecss Add more css on link
  450. * @return string String with URL
  451. */
  452. function getNomUrl($withpicto=0, $option='', $notooltip=0, $maxlen=24, $morecss='')
  453. {
  454. global $langs, $conf, $db;
  455. global $dolibarr_main_authentication, $dolibarr_main_demo;
  456. global $menumanager;
  457. $result = '';
  458. $companylink = '';
  459. $label = '<u>' . $langs->trans("MyModule") . '</u>';
  460. $label.= '<div width="100%">';
  461. $label.= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
  462. $link = '<a href="'.DOL_URL_ROOT.'/websites/card.php?id='.$this->id.'"';
  463. $link.= ($notooltip?'':' title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip'.($morecss?' '.$morecss:'').'"');
  464. $link.= '>';
  465. $linkend='</a>';
  466. if ($withpicto)
  467. {
  468. $result.=($link.img_object(($notooltip?'':$label), 'label', ($notooltip?'':'class="classfortooltip"')).$linkend);
  469. if ($withpicto != 2) $result.=' ';
  470. }
  471. $result.= $link . $this->ref . $linkend;
  472. return $result;
  473. }
  474. /**
  475. * Retourne le libelle du status d'un user (actif, inactif)
  476. *
  477. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  478. * @return string Label of status
  479. */
  480. function getLibStatut($mode=0)
  481. {
  482. return $this->LibStatut($this->status,$mode);
  483. }
  484. /**
  485. * Renvoi le libelle d'un status donne
  486. *
  487. * @param int $status Id status
  488. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  489. * @return string Label of status
  490. */
  491. function LibStatut($status,$mode=0)
  492. {
  493. global $langs;
  494. if ($mode == 0)
  495. {
  496. $prefix='';
  497. if ($status == 1) return $langs->trans('Enabled');
  498. if ($status == 0) return $langs->trans('Disabled');
  499. }
  500. if ($mode == 1)
  501. {
  502. if ($status == 1) return $langs->trans('Enabled');
  503. if ($status == 0) return $langs->trans('Disabled');
  504. }
  505. if ($mode == 2)
  506. {
  507. if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4').' '.$langs->trans('Enabled');
  508. if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5').' '.$langs->trans('Disabled');
  509. }
  510. if ($mode == 3)
  511. {
  512. if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4');
  513. if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5');
  514. }
  515. if ($mode == 4)
  516. {
  517. if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4').' '.$langs->trans('Enabled');
  518. if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5').' '.$langs->trans('Disabled');
  519. }
  520. if ($mode == 5)
  521. {
  522. if ($status == 1) return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'),'statut4');
  523. if ($status == 0) return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'),'statut5');
  524. }
  525. }
  526. /**
  527. * Initialise object with example values
  528. * Id must be 0 if object instance is a specimen
  529. *
  530. * @return void
  531. */
  532. public function initAsSpecimen()
  533. {
  534. $this->id = 0;
  535. $this->entity = 1;
  536. $this->ref = 'myspecimenwebsite';
  537. $this->description = 'A specimen website';
  538. $this->status = '';
  539. $this->fk_default_home = null;
  540. $this->virtualhost = 'http://myvirtualhost';
  541. $this->date_creation = dol_now();
  542. $this->date_modification = dol_now();
  543. $this->tms = dol_now();
  544. }
  545. }
  546. /**
  547. * Class WebsiteLine
  548. */
  549. class WebsiteLine
  550. {
  551. /**
  552. * @var int ID
  553. */
  554. public $id;
  555. /**
  556. * @var int
  557. */
  558. public $entity;
  559. /**
  560. * @var string
  561. */
  562. public $ref;
  563. /**
  564. * @var string
  565. */
  566. public $description;
  567. /**
  568. * @var int
  569. */
  570. public $status;
  571. /**
  572. * @var int
  573. */
  574. public $fk_default_home;
  575. /**
  576. * @var string
  577. */
  578. public $virtualhost;
  579. /**
  580. * @var mixed
  581. */
  582. public $date_creation;
  583. /**
  584. * @var mixed
  585. */
  586. public $date_modification;
  587. /**
  588. * @var mixed
  589. */
  590. public $tms = '';
  591. }