bookmark.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. /* Copyright (C) 2005 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/bookmarks/class/bookmark.class.php
  20. * \ingroup bookmark
  21. * \brief File of class to manage bookmarks
  22. */
  23. /**
  24. * Class to manage bookmarks
  25. */
  26. class Bookmark extends CommonObject
  27. {
  28. /**
  29. * @var string ID to identify managed object
  30. */
  31. public $element = 'bookmark';
  32. /**
  33. * @var string Name of table without prefix where object is stored
  34. */
  35. public $table_element = 'bookmark';
  36. /**
  37. * 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  38. * @var int
  39. */
  40. public $ismultientitymanaged = 1;
  41. /**
  42. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  43. */
  44. public $picto = 'bookmark';
  45. /**
  46. * @var DoliDB Database handler.
  47. */
  48. public $db;
  49. /**
  50. * @var int ID
  51. */
  52. public $id;
  53. /**
  54. * @var int User ID
  55. */
  56. public $fk_user;
  57. /**
  58. * Date creation record (datec)
  59. *
  60. * @var integer
  61. */
  62. public $datec;
  63. public $url;
  64. public $target; // 0=replace, 1=new window
  65. public $title;
  66. public $position;
  67. public $favicon;
  68. /**
  69. * Constructor
  70. *
  71. * @param DoliDB $db Database handler
  72. */
  73. public function __construct($db)
  74. {
  75. $this->db = $db;
  76. }
  77. /**
  78. * Directs the bookmark
  79. *
  80. * @param int $id Bookmark Id Loader
  81. * @return int <0 if KO, >0 if OK
  82. */
  83. public function fetch($id)
  84. {
  85. global $conf;
  86. $sql = "SELECT rowid, fk_user, dateb as datec, url, target,";
  87. $sql .= " title, position, favicon";
  88. $sql .= " FROM ".MAIN_DB_PREFIX."bookmark";
  89. $sql .= " WHERE rowid = ".$id;
  90. $sql .= " AND entity = ".$conf->entity;
  91. dol_syslog("Bookmark::fetch", LOG_DEBUG);
  92. $resql = $this->db->query($sql);
  93. if ($resql)
  94. {
  95. $obj = $this->db->fetch_object($resql);
  96. $this->id = $obj->rowid;
  97. $this->ref = $obj->rowid;
  98. $this->fk_user = $obj->fk_user;
  99. $this->datec = $this->db->jdate($obj->datec);
  100. $this->url = $obj->url;
  101. $this->target = $obj->target;
  102. $this->title = $obj->title;
  103. $this->position = $obj->position;
  104. $this->favicon = $obj->favicon;
  105. $this->db->free($resql);
  106. return $this->id;
  107. }
  108. else
  109. {
  110. dol_print_error($this->db);
  111. return -1;
  112. }
  113. }
  114. /**
  115. * Insert bookmark into database
  116. *
  117. * @return int <0 si ko, rowid du bookmark cree si ok
  118. */
  119. public function create()
  120. {
  121. global $conf;
  122. // Clean parameters
  123. $this->url = trim($this->url);
  124. $this->title = trim($this->title);
  125. if (empty($this->position)) $this->position = 0;
  126. $now = dol_now();
  127. $this->db->begin();
  128. $sql = "INSERT INTO ".MAIN_DB_PREFIX."bookmark (fk_user,dateb,url,target";
  129. $sql .= ",title,favicon,position";
  130. $sql .= ",entity";
  131. $sql .= ") VALUES (";
  132. $sql .= ($this->fk_user > 0 ? $this->fk_user : "0").",";
  133. $sql .= " '".$this->db->idate($now)."',";
  134. $sql .= " '".$this->db->escape($this->url)."', '".$this->db->escape($this->target)."',";
  135. $sql .= " '".$this->db->escape($this->title)."', '".$this->db->escape($this->favicon)."', '".$this->db->escape($this->position)."'";
  136. $sql .= ", ".$this->db->escape($conf->entity);
  137. $sql .= ")";
  138. dol_syslog("Bookmark::create", LOG_DEBUG);
  139. $resql = $this->db->query($sql);
  140. if ($resql)
  141. {
  142. $id = $this->db->last_insert_id(MAIN_DB_PREFIX."bookmark");
  143. if ($id > 0)
  144. {
  145. $this->id = $id;
  146. $this->db->commit();
  147. return $id;
  148. }
  149. else
  150. {
  151. $this->error = $this->db->lasterror();
  152. $this->errno = $this->db->lasterrno();
  153. $this->db->rollback();
  154. return -2;
  155. }
  156. }
  157. else
  158. {
  159. $this->error = $this->db->lasterror();
  160. $this->errno = $this->db->lasterrno();
  161. $this->db->rollback();
  162. return -1;
  163. }
  164. }
  165. /**
  166. * Update bookmark record
  167. *
  168. * @return int <0 if KO, > if OK
  169. */
  170. public function update()
  171. {
  172. // Clean parameters
  173. $this->url = trim($this->url);
  174. $this->title = trim($this->title);
  175. if (empty($this->position)) $this->position = 0;
  176. $sql = "UPDATE ".MAIN_DB_PREFIX."bookmark";
  177. $sql .= " SET fk_user = ".($this->fk_user > 0 ? $this->fk_user : "0");
  178. $sql .= " ,dateb = '".$this->db->idate($this->datec)."'";
  179. $sql .= " ,url = '".$this->db->escape($this->url)."'";
  180. $sql .= " ,target = '".$this->db->escape($this->target)."'";
  181. $sql .= " ,title = '".$this->db->escape($this->title)."'";
  182. $sql .= " ,favicon = '".$this->db->escape($this->favicon)."'";
  183. $sql .= " ,position = '".$this->db->escape($this->position)."'";
  184. $sql .= " WHERE rowid = ".$this->id;
  185. dol_syslog("Bookmark::update", LOG_DEBUG);
  186. if ($this->db->query($sql))
  187. {
  188. return 1;
  189. }
  190. else
  191. {
  192. $this->error = $this->db->lasterror();
  193. return -1;
  194. }
  195. }
  196. /**
  197. * Removes the bookmark
  198. *
  199. * @param int $id Id removed bookmark
  200. * @return int <0 si ko, >0 si ok
  201. */
  202. public function remove($id)
  203. {
  204. $sql = "DELETE FROM ".MAIN_DB_PREFIX."bookmark";
  205. $sql .= " WHERE rowid = ".$id;
  206. dol_syslog("Bookmark::remove", LOG_DEBUG);
  207. $resql = $this->db->query($sql);
  208. if ($resql)
  209. {
  210. return 1;
  211. }
  212. else
  213. {
  214. $this->error = $this->db->lasterror();
  215. return -1;
  216. }
  217. }
  218. /**
  219. * Function used to replace a thirdparty id with another one.
  220. *
  221. * @param DoliDB $db Database handler
  222. * @param int $origin_id Old thirdparty id
  223. * @param int $dest_id New thirdparty id
  224. * @return bool
  225. */
  226. public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
  227. {
  228. $tables = array(
  229. 'bookmark'
  230. );
  231. return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
  232. }
  233. /**
  234. * Return label of contact status
  235. *
  236. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  237. * @return string Label of contact status
  238. */
  239. public function getLibStatut($mode)
  240. {
  241. return '';
  242. }
  243. /**
  244. * Return a link to the object card (with optionaly the picto)
  245. *
  246. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  247. * @param string $option On what the link point to ('nolink', ...)
  248. * @param int $notooltip 1=Disable tooltip
  249. * @param string $morecss Add more css on link
  250. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  251. * @return string String with URL
  252. */
  253. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  254. {
  255. global $conf, $langs, $hookmanager;
  256. if (!empty($conf->dol_no_mouse_hover)) $notooltip = 1; // Force disable tooltips
  257. $result = '';
  258. $label = '<u>'.$langs->trans("Bookmark").'</u>';
  259. $label .= '<br>';
  260. $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
  261. $url = DOL_URL_ROOT.'/bookmarks/card.php?id='.$this->id;
  262. if ($option != 'nolink')
  263. {
  264. // Add param to save lastsearch_values or not
  265. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  266. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values = 1;
  267. if ($add_save_lastsearch_values) $url .= '&save_lastsearch_values=1';
  268. }
  269. $linkclose = '';
  270. if (empty($notooltip))
  271. {
  272. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
  273. {
  274. $label = $langs->trans("ShowBookmark");
  275. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  276. }
  277. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  278. $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
  279. }
  280. else $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
  281. $linkstart = '<a href="'.$url.'"';
  282. $linkstart .= $linkclose.'>';
  283. $linkend = '</a>';
  284. $result .= $linkstart;
  285. if ($withpicto) $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
  286. if ($withpicto != 2) $result .= $this->ref;
  287. $result .= $linkend;
  288. //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
  289. global $action, $hookmanager;
  290. $hookmanager->initHooks(array('mybookmarkdao'));
  291. $parameters = array('id'=>$this->id, 'getnomurl'=>$result);
  292. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  293. if ($reshook > 0) $result = $hookmanager->resPrint;
  294. else $result .= $hookmanager->resPrint;
  295. return $result;
  296. }
  297. }