link.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. /* Copyright (C) 2013 Cédric Salvador <csalvador@gpcsolutions.fr>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/core/class/link.class.php
  19. * \ingroup link
  20. * \brief File for link class
  21. */
  22. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  23. /**
  24. * Class to manage links
  25. */
  26. class Link extends CommonObject
  27. {
  28. /**
  29. * @var string ID to identify managed object
  30. */
  31. public $element = 'link';
  32. /**
  33. * @var string Name of table without prefix where object is stored
  34. */
  35. public $table_element = 'links';
  36. /**
  37. * @var int Entity
  38. */
  39. public $entity;
  40. public $datea;
  41. public $url;
  42. /**
  43. * @var string Links label
  44. */
  45. public $label;
  46. public $objecttype;
  47. public $objectid;
  48. /**
  49. * Constructor
  50. *
  51. * @param DoliDB $db Database handler
  52. */
  53. public function __construct($db)
  54. {
  55. $this->db = $db;
  56. }
  57. /**
  58. * Create link in database
  59. *
  60. * @param User $user Object of user that ask creation
  61. * @return int >= 0 if OK, < 0 if KO
  62. */
  63. public function create($user='')
  64. {
  65. global $langs,$conf;
  66. $error=0;
  67. $langs->load("errors");
  68. // Clean parameters
  69. if (empty($this->label)) {
  70. $this->label = trim(basename($this->url));
  71. }
  72. if (empty($this->datea)) {
  73. $this->datea = dol_now();
  74. }
  75. $this->url = trim($this->url);
  76. dol_syslog(get_class($this)."::create ".$this->url);
  77. // Check parameters
  78. if (empty($this->url)) {
  79. $this->error = $langs->trans("NoUrl");
  80. return -1;
  81. }
  82. $this->db->begin();
  83. $sql = "INSERT INTO ".MAIN_DB_PREFIX."links (entity, datea, url, label, objecttype, objectid)";
  84. $sql .= " VALUES ('".$conf->entity."', '".$this->db->idate($this->datea)."'";
  85. $sql .= ", '" . $this->db->escape($this->url) . "'";
  86. $sql .= ", '" . $this->db->escape($this->label) . "'";
  87. $sql .= ", '" . $this->db->escape($this->objecttype) . "'";
  88. $sql .= ", " . $this->objectid . ")";
  89. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  90. $result = $this->db->query($sql);
  91. if ($result) {
  92. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "links");
  93. if ($this->id > 0) {
  94. // Call trigger
  95. $result=$this->call_trigger('LINK_CREATE',$user);
  96. if ($result < 0) $error++;
  97. // End call triggers
  98. } else {
  99. $error++;
  100. }
  101. if (! $error)
  102. {
  103. dol_syslog(get_class($this)."::Create success id=" . $this->id);
  104. $this->db->commit();
  105. return $this->id;
  106. }
  107. else
  108. {
  109. dol_syslog(get_class($this)."::Create echec update " . $this->error, LOG_ERR);
  110. $this->db->rollback();
  111. return -3;
  112. }
  113. }
  114. else
  115. {
  116. if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS')
  117. {
  118. $this->error=$langs->trans("ErrorCompanyNameAlreadyExists",$this->name);
  119. $result=-1;
  120. }
  121. else
  122. {
  123. $this->error=$this->db->lasterror();
  124. $result=-2;
  125. }
  126. $this->db->rollback();
  127. return $result;
  128. }
  129. }
  130. /**
  131. * Update parameters of third party
  132. *
  133. * @param User $user User executing update
  134. * @param int $call_trigger 0=no, 1=yes
  135. * @return int <0 if KO, >=0 if OK
  136. */
  137. public function update($user='', $call_trigger=1)
  138. {
  139. global $langs,$conf;
  140. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  141. $langs->load("errors");
  142. $error=0;
  143. dol_syslog(get_class($this)."::Update id = " . $this->id . " call_trigger = " . $call_trigger);
  144. // Check parameters
  145. if (empty($this->url))
  146. {
  147. $this->error = $langs->trans("NoURL");
  148. return -1;
  149. }
  150. // Clean parameters
  151. $this->url = clean_url($this->url,1);
  152. if (empty($this->label)) $this->label = basename($this->url);
  153. $this->label = trim($this->label);
  154. $this->db->begin();
  155. $sql = "UPDATE " . MAIN_DB_PREFIX . "links SET ";
  156. $sql .= "entity = '" . $conf->entity ."'";
  157. $sql .= ", datea = '" . $this->db->idate(dol_now()) . "'";
  158. $sql .= ", url = '" . $this->db->escape($this->url) . "'";
  159. $sql .= ", label = '" . $this->db->escape($this->label) . "'";
  160. $sql .= ", objecttype = '" . $this->db->escape($this->objecttype) . "'";
  161. $sql .= ", objectid = " . $this->objectid;
  162. $sql .= " WHERE rowid = " . $this->id;
  163. dol_syslog(get_class($this)."::update sql = " .$sql);
  164. $resql = $this->db->query($sql);
  165. if ($resql)
  166. {
  167. if ($call_trigger)
  168. {
  169. // Call trigger
  170. $result=$this->call_trigger('LINK_MODIFY',$user);
  171. if ($result < 0) $error++;
  172. // End call triggers
  173. }
  174. if (! $error)
  175. {
  176. dol_syslog(get_class($this) . "::Update success");
  177. $this->db->commit();
  178. return 1;
  179. } else {
  180. setEventMessages('', $this->errors, 'errors');
  181. $this->db->rollback();
  182. return -1;
  183. }
  184. }
  185. else
  186. {
  187. if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS')
  188. {
  189. // Doublon
  190. $this->error = $langs->trans("ErrorDuplicateField");
  191. $result = -1;
  192. }
  193. else
  194. {
  195. $this->error = $langs->trans("Error sql = " . $sql);
  196. $result = -2;
  197. }
  198. $this->db->rollback();
  199. return $result;
  200. }
  201. }
  202. /**
  203. * Loads all links from database
  204. *
  205. * @param array $links array of Link objects to fill
  206. * @param string $objecttype type of the associated object in dolibarr
  207. * @param int $objectid id of the associated object in dolibarr
  208. * @param string $sortfield field used to sort
  209. * @param string $sortorder sort order
  210. * @return int 1 if ok, 0 if no records, -1 if error
  211. **/
  212. public function fetchAll(&$links, $objecttype, $objectid, $sortfield=null, $sortorder=null)
  213. {
  214. global $conf;
  215. $sql = "SELECT rowid, entity, datea, url, label, objecttype, objectid FROM " . MAIN_DB_PREFIX . "links";
  216. $sql .= " WHERE objecttype = '" . $objecttype . "' AND objectid = " . $objectid;
  217. if ($conf->entity != 0) $sql .= " AND entity = " . $conf->entity;
  218. if ($sortfield) {
  219. if (empty($sortorder)) {
  220. $sortorder = "ASC";
  221. }
  222. $sql .= " ORDER BY " . $sortfield . " " . $sortorder;
  223. }
  224. dol_syslog(get_class($this)."::fetchAll", LOG_DEBUG);
  225. $resql = $this->db->query($sql);
  226. if ($resql)
  227. {
  228. $num = $this->db->num_rows($resql);
  229. dol_syslog(get_class($this)."::fetchAll " . $num . "records", LOG_DEBUG);
  230. if ($num > 0)
  231. {
  232. while ($obj = $this->db->fetch_object($resql))
  233. {
  234. $link = new Link($this->db);
  235. $link->id = $obj->rowid;
  236. $link->entity = $obj->entity;
  237. $link->datea = $this->db->jdate($obj->datea);
  238. $link->url = $obj->url;
  239. $link->label = $obj->label;
  240. $link->objecttype = $obj->objecttype;
  241. $link->objectid = $obj->objectid;
  242. $links[] = $link;
  243. }
  244. return 1;
  245. } else {
  246. return 0;
  247. }
  248. } else {
  249. return -1;
  250. }
  251. }
  252. /**
  253. * Return nb of links
  254. *
  255. * @param DoliDb $db Database handler
  256. * @param string $objecttype Type of the associated object in dolibarr
  257. * @param int $objectid Id of the associated object in dolibarr
  258. * @return int Nb of links, -1 if error
  259. **/
  260. public static function count($db, $objecttype, $objectid)
  261. {
  262. global $conf;
  263. $sql = "SELECT COUNT(rowid) as nb FROM " . MAIN_DB_PREFIX . "links";
  264. $sql .= " WHERE objecttype = '" . $objecttype . "' AND objectid = " . $objectid;
  265. if ($conf->entity != 0) $sql .= " AND entity = " . $conf->entity;
  266. $resql = $db->query($sql);
  267. if ($resql)
  268. {
  269. $obj = $db->fetch_object($resql);
  270. if ($obj) return $obj->nb;
  271. }
  272. return -1;
  273. }
  274. /**
  275. * Loads a link from database
  276. *
  277. * @param int $rowid Id of link to load
  278. * @return int 1 if ok, 0 if no record found, -1 if error
  279. **/
  280. public function fetch($rowid=null)
  281. {
  282. global $conf;
  283. if (empty($rowid)) {
  284. $rowid = $this->id;
  285. }
  286. $sql = "SELECT rowid, entity, datea, url, label, objecttype, objectid FROM " . MAIN_DB_PREFIX . "links";
  287. $sql .= " WHERE rowid = " . $rowid;
  288. if($conf->entity != 0) $sql .= " AND entity = " . $conf->entity;
  289. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  290. $resql = $this->db->query($sql);
  291. if ($resql)
  292. {
  293. if($this->db->num_rows($resql) > 0)
  294. {
  295. $obj = $this->db->fetch_object($resql);
  296. $this->entity = $obj->entity;
  297. $this->datea = $this->db->jdate($obj->datea);
  298. $this->url = $obj->url;
  299. $this->label = $obj->label;
  300. $this->objecttype = $obj->objecttype;
  301. $this->objectid = $obj->objectid;
  302. return 1;
  303. }
  304. else
  305. {
  306. return 0;
  307. }
  308. } else {
  309. $this->error=$this->db->lasterror();
  310. return -1;
  311. }
  312. }
  313. /**
  314. * Delete a link from database
  315. *
  316. * @return int <0 if KO, 0 if nothing done, >0 if OK
  317. */
  318. public function delete()
  319. {
  320. global $user, $langs, $conf;
  321. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  322. $error = 0;
  323. // Call trigger
  324. $result=$this->call_trigger('LINK_DELETE',$user);
  325. if ($result < 0) return -1;
  326. // End call triggers
  327. $this->db->begin();
  328. // Remove link
  329. $sql = "DELETE FROM " . MAIN_DB_PREFIX . "links";
  330. $sql.= " WHERE rowid = " . $this->id;
  331. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  332. if (! $this->db->query($sql))
  333. {
  334. $error++;
  335. $this->error = $this->db->lasterror();
  336. }
  337. if (! $error) {
  338. $this->db->commit();
  339. return 1;
  340. } else {
  341. $this->db->rollback();
  342. return -1;
  343. }
  344. }
  345. }