link.class.php 9.6 KB

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