link.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. public $element = 'link';
  29. public $table_element = 'links';
  30. public $entity;
  31. public $datea;
  32. public $url;
  33. public $label;
  34. public $objecttype;
  35. public $objectid;
  36. /**
  37. * Constructor
  38. *
  39. * @param DoliDB $db Database handler
  40. */
  41. public function __construct($db)
  42. {
  43. global $conf;
  44. $this->db = $db;
  45. return 1;
  46. }
  47. /**
  48. * Create link in database
  49. *
  50. * @param User $user Object of user that ask creation
  51. * @return int >= 0 if OK, < 0 if KO
  52. */
  53. public function create($user='')
  54. {
  55. global $langs,$conf;
  56. $error=0;
  57. $langs->load("errors");
  58. // Clean parameters
  59. if (empty($this->label)) {
  60. $this->label = trim(basename($this->url));
  61. }
  62. if (empty($this->datea)) {
  63. $this->datea = dol_now();
  64. }
  65. $this->url = trim($this->url);
  66. dol_syslog(get_class($this)."::create ".$this->url);
  67. // Check parameters
  68. if (empty($this->url)) {
  69. $this->error = $langs->trans("NoUrl");
  70. return -1;
  71. }
  72. $this->db->begin();
  73. $sql = "INSERT INTO ".MAIN_DB_PREFIX."links (entity, datea, url, label, objecttype, objectid)";
  74. $sql .= " VALUES ('".$conf->entity."', '".$this->db->idate($this->datea)."'";
  75. $sql .= ", '" . $this->db->escape($this->url) . "'";
  76. $sql .= ", '" . $this->db->escape($this->label) . "'";
  77. $sql .= ", '" . $this->objecttype . "'";
  78. $sql .= ", " . $this->objectid . ")";
  79. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  80. $result = $this->db->query($sql);
  81. if ($result) {
  82. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "links");
  83. if ($this->id > 0) {
  84. // Call trigger
  85. $result=$this->call_trigger('LINK_CREATE',$user);
  86. if ($result < 0) $error++;
  87. // End call triggers
  88. } else {
  89. $error++;
  90. }
  91. if (! $error)
  92. {
  93. dol_syslog(get_class($this)."::Create success id=" . $this->id);
  94. $this->db->commit();
  95. return $this->id;
  96. }
  97. else
  98. {
  99. dol_syslog(get_class($this)."::Create echec update " . $this->error, LOG_ERR);
  100. $this->db->rollback();
  101. return -3;
  102. }
  103. }
  104. else
  105. {
  106. if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS')
  107. {
  108. $this->error=$langs->trans("ErrorCompanyNameAlreadyExists",$this->name);
  109. $result=-1;
  110. }
  111. else
  112. {
  113. $this->error=$this->db->lasterror();
  114. $result=-2;
  115. }
  116. $this->db->rollback();
  117. return $result;
  118. }
  119. }
  120. /**
  121. * Update parameters of third party
  122. *
  123. * @param User $user User executing update
  124. * @param int $call_trigger 0=no, 1=yes
  125. * @return int <0 if KO, >=0 if OK
  126. */
  127. public function update($user='', $call_trigger=1)
  128. {
  129. global $langs,$conf;
  130. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  131. $langs->load("errors");
  132. $error=0;
  133. dol_syslog(get_class($this)."::Update id = " . $this->id . " call_trigger = " . $call_trigger);
  134. // Check parameters
  135. if (empty($this->url))
  136. {
  137. $this->error = $langs->trans("NoURL");
  138. return -1;
  139. }
  140. // Clean parameters
  141. $this->url = clean_url($this->url,1);
  142. if (empty($this->label)) $this->label = basename($this->url);
  143. $this->label = trim($this->label);
  144. $this->db->begin();
  145. $sql = "UPDATE " . MAIN_DB_PREFIX . "links SET ";
  146. $sql .= "entity = '" . $conf->entity ."'";
  147. $sql .= ", datea = '" . $this->db->idate(dol_now()) . "'";
  148. $sql .= ", url = '" . $this->db->escape($this->url) . "'";
  149. $sql .= ", label = '" . $this->db->escape($this->label) . "'";
  150. $sql .= ", objecttype = '" . $this->db->escape($this->objecttype) . "'";
  151. $sql .= ", objectid = " . $this->objectid;
  152. $sql .= " WHERE rowid = " . $this->id;
  153. dol_syslog(get_class($this)."::update sql = " .$sql);
  154. $resql = $this->db->query($sql);
  155. if ($resql)
  156. {
  157. if ($call_trigger)
  158. {
  159. // Call trigger
  160. $result=$this->call_trigger('LINK_MODIFY',$user);
  161. if ($result < 0) $error++;
  162. // End call triggers
  163. }
  164. if (! $error)
  165. {
  166. dol_syslog(get_class($this) . "::Update success");
  167. $this->db->commit();
  168. return 1;
  169. } else {
  170. setEventMessages('', $this->errors, 'errors');
  171. $this->db->rollback();
  172. return -1;
  173. }
  174. }
  175. else
  176. {
  177. if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS')
  178. {
  179. // Doublon
  180. $this->error = $langs->trans("ErrorDuplicateField");
  181. $result = -1;
  182. }
  183. else
  184. {
  185. $this->error = $langs->trans("Error sql = " . $sql);
  186. $result = -2;
  187. }
  188. $this->db->rollback();
  189. return $result;
  190. }
  191. }
  192. /**
  193. * Loads all links from database
  194. *
  195. * @param array $links array of Link objects to fill
  196. * @param string $objecttype type of the associated object in dolibarr
  197. * @param int $objectid id of the associated object in dolibarr
  198. * @param string $sortfield field used to sort
  199. * @param string $sortorder sort order
  200. * @return int 1 if ok, 0 if no records, -1 if error
  201. **/
  202. public function fetchAll(&$links, $objecttype, $objectid, $sortfield=null, $sortorder=null)
  203. {
  204. global $conf;
  205. $sql = "SELECT rowid, entity, datea, url, label, objecttype, objectid FROM " . MAIN_DB_PREFIX . "links";
  206. $sql .= " WHERE objecttype = '" . $objecttype . "' AND objectid = " . $objectid;
  207. if ($conf->entity != 0) $sql .= " AND entity = " . $conf->entity;
  208. if ($sortfield) {
  209. if (empty($sortorder)) {
  210. $sortorder = "ASC";
  211. }
  212. $sql .= " ORDER BY " . $sortfield . " " . $sortorder;
  213. }
  214. dol_syslog(get_class($this)."::fetchAll", LOG_DEBUG);
  215. $resql = $this->db->query($sql);
  216. if ($resql)
  217. {
  218. $num = $this->db->num_rows($resql);
  219. dol_syslog(get_class($this)."::fetchAll " . $num . "records", LOG_DEBUG);
  220. if ($num > 0)
  221. {
  222. while ($obj = $this->db->fetch_object($resql))
  223. {
  224. $link = new Link($this->db);
  225. $link->id = $obj->rowid;
  226. $link->entity = $obj->entity;
  227. $link->datea = $this->db->jdate($obj->datea);
  228. $link->url = $obj->url;
  229. $link->label = $obj->label;
  230. $link->objecttype = $obj->objecttype;
  231. $link->objectid = $obj->objectid;
  232. $links[] = $link;
  233. }
  234. return 1;
  235. } else {
  236. return 0;
  237. }
  238. } else {
  239. return -1;
  240. }
  241. }
  242. /**
  243. * Return nb of links
  244. *
  245. * @param DoliDb $db Database handler
  246. * @param string $objecttype Type of the associated object in dolibarr
  247. * @param int $objectid Id of the associated object in dolibarr
  248. * @return int Nb of links, -1 if error
  249. **/
  250. public static function count($db, $objecttype, $objectid)
  251. {
  252. global $conf;
  253. $sql = "SELECT COUNT(rowid) as nb FROM " . MAIN_DB_PREFIX . "links";
  254. $sql .= " WHERE objecttype = '" . $objecttype . "' AND objectid = " . $objectid;
  255. if ($conf->entity != 0) $sql .= " AND entity = " . $conf->entity;
  256. $resql = $db->query($sql);
  257. if ($resql)
  258. {
  259. $obj = $db->fetch_object($resql);
  260. if ($obj) return $obj->nb;
  261. }
  262. return -1;
  263. }
  264. /**
  265. * Loads a link from database
  266. *
  267. * @param int $rowid Id of link to load
  268. * @return int 1 if ok, 0 if no record found, -1 if error
  269. **/
  270. public function fetch($rowid=null)
  271. {
  272. global $conf;
  273. if (empty($rowid)) {
  274. $rowid = $this->id;
  275. }
  276. $sql = "SELECT rowid, entity, datea, url, label, objecttype, objectid FROM " . MAIN_DB_PREFIX . "links";
  277. $sql .= " WHERE rowid = " . $rowid;
  278. if($conf->entity != 0) $sql .= " AND entity = " . $conf->entity;
  279. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  280. $resql = $this->db->query($sql);
  281. if ($resql)
  282. {
  283. if($this->db->num_rows($resql) > 0)
  284. {
  285. $obj = $this->db->fetch_object($resql);
  286. $this->entity = $obj->entity;
  287. $this->datea = $this->db->jdate($obj->datea);
  288. $this->url = $obj->url;
  289. $this->label = $obj->label;
  290. $this->objecttype = $obj->objecttype;
  291. $this->objectid = $obj->objectid;
  292. return 1;
  293. }
  294. else
  295. {
  296. return 0;
  297. }
  298. } else {
  299. $this->error=$this->db->lasterror();
  300. return -1;
  301. }
  302. }
  303. /**
  304. * Delete a link from database
  305. *
  306. * @return int <0 if KO, 0 if nothing done, >0 if OK
  307. */
  308. public function delete()
  309. {
  310. global $user, $langs, $conf;
  311. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  312. $error = 0;
  313. // Call trigger
  314. $result=$this->call_trigger('LINK_DELETE',$user);
  315. if ($result < 0) return -1;
  316. // End call triggers
  317. $this->db->begin();
  318. // Remove link
  319. $sql = "DELETE FROM " . MAIN_DB_PREFIX . "links";
  320. $sql.= " WHERE rowid = " . $this->id;
  321. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  322. if (! $this->db->query($sql))
  323. {
  324. $error++;
  325. $this->error = $this->db->lasterror();
  326. }
  327. if (! $error) {
  328. $this->db->commit();
  329. return 1;
  330. } else {
  331. $this->db->rollback();
  332. return -1;
  333. }
  334. }
  335. }