bookmark.class.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. If > 0, bookmark of one user. If == 0, bookmark public (for everybody)
  55. */
  56. public $fk_user;
  57. /**
  58. * Date creation record (datec)
  59. *
  60. * @var integer
  61. */
  62. public $datec;
  63. /**
  64. * @var string url
  65. */
  66. public $url;
  67. public $target; // 0=replace, 1=new window
  68. /**
  69. * @var string title
  70. */
  71. public $title;
  72. /**
  73. * @var int position of bookmark
  74. */
  75. public $position;
  76. /**
  77. * @var string favicon
  78. */
  79. public $favicon;
  80. /**
  81. * Constructor
  82. *
  83. * @param DoliDB $db Database handler
  84. */
  85. public function __construct($db)
  86. {
  87. $this->db = $db;
  88. }
  89. /**
  90. * Directs the bookmark
  91. *
  92. * @param int $id Bookmark Id Loader
  93. * @return int <0 if KO, >0 if OK
  94. */
  95. public function fetch($id)
  96. {
  97. global $conf;
  98. $sql = "SELECT rowid, fk_user, dateb as datec, url, target,";
  99. $sql .= " title, position, favicon";
  100. $sql .= " FROM ".MAIN_DB_PREFIX."bookmark";
  101. $sql .= " WHERE rowid = ".((int) $id);
  102. $sql .= " AND entity = ".$conf->entity;
  103. dol_syslog("Bookmark::fetch", LOG_DEBUG);
  104. $resql = $this->db->query($sql);
  105. if ($resql) {
  106. $obj = $this->db->fetch_object($resql);
  107. $this->id = $obj->rowid;
  108. $this->ref = $obj->rowid;
  109. $this->fk_user = $obj->fk_user;
  110. $this->datec = $this->db->jdate($obj->datec);
  111. $this->url = $obj->url;
  112. $this->target = $obj->target;
  113. $this->title = $obj->title;
  114. $this->position = $obj->position;
  115. $this->favicon = $obj->favicon;
  116. $this->db->free($resql);
  117. return $this->id;
  118. } else {
  119. dol_print_error($this->db);
  120. return -1;
  121. }
  122. }
  123. /**
  124. * Insert bookmark into database
  125. *
  126. * @return int <0 si ko, rowid du bookmark cree si ok
  127. */
  128. public function create()
  129. {
  130. global $conf;
  131. // Clean parameters
  132. $this->url = trim($this->url);
  133. $this->title = trim($this->title);
  134. if (empty($this->position)) {
  135. $this->position = 0;
  136. }
  137. $now = dol_now();
  138. $this->db->begin();
  139. $sql = "INSERT INTO ".MAIN_DB_PREFIX."bookmark (fk_user,dateb,url,target";
  140. $sql .= ",title,favicon,position";
  141. $sql .= ",entity";
  142. $sql .= ") VALUES (";
  143. $sql .= ($this->fk_user > 0 ? $this->fk_user : "0").",";
  144. $sql .= " '".$this->db->idate($now)."',";
  145. $sql .= " '".$this->db->escape($this->url)."', '".$this->db->escape($this->target)."',";
  146. $sql .= " '".$this->db->escape($this->title)."', '".$this->db->escape($this->favicon)."', ".(int) $this->position;
  147. $sql .= ", ".(int) $conf->entity;
  148. $sql .= ")";
  149. dol_syslog("Bookmark::create", LOG_DEBUG);
  150. $resql = $this->db->query($sql);
  151. if ($resql) {
  152. $id = $this->db->last_insert_id(MAIN_DB_PREFIX."bookmark");
  153. if ($id > 0) {
  154. $this->id = $id;
  155. $this->db->commit();
  156. return $id;
  157. } else {
  158. $this->error = $this->db->lasterror();
  159. $this->errno = $this->db->lasterrno();
  160. $this->db->rollback();
  161. return -2;
  162. }
  163. } else {
  164. $this->error = $this->db->lasterror();
  165. $this->errno = $this->db->lasterrno();
  166. $this->db->rollback();
  167. return -1;
  168. }
  169. }
  170. /**
  171. * Update bookmark record
  172. *
  173. * @return int <0 if KO, > if OK
  174. */
  175. public function update()
  176. {
  177. // Clean parameters
  178. $this->url = trim($this->url);
  179. $this->title = trim($this->title);
  180. if (empty($this->position)) {
  181. $this->position = 0;
  182. }
  183. $sql = "UPDATE ".MAIN_DB_PREFIX."bookmark";
  184. $sql .= " SET fk_user = ".($this->fk_user > 0 ? $this->fk_user : "0");
  185. $sql .= " ,dateb = '".$this->db->idate($this->datec)."'";
  186. $sql .= " ,url = '".$this->db->escape($this->url)."'";
  187. $sql .= " ,target = '".$this->db->escape($this->target)."'";
  188. $sql .= " ,title = '".$this->db->escape($this->title)."'";
  189. $sql .= " ,favicon = '".$this->db->escape($this->favicon)."'";
  190. $sql .= " ,position = ".(int) $this->position;
  191. $sql .= " WHERE rowid = ".((int) $this->id);
  192. dol_syslog("Bookmark::update", LOG_DEBUG);
  193. if ($this->db->query($sql)) {
  194. return 1;
  195. } else {
  196. $this->error = $this->db->lasterror();
  197. return -1;
  198. }
  199. }
  200. /**
  201. * Removes the bookmark
  202. *
  203. * @param User $user User deleting
  204. * @return int <0 if KO, >0 if OK
  205. */
  206. public function delete($user)
  207. {
  208. $sql = "DELETE FROM ".MAIN_DB_PREFIX."bookmark";
  209. $sql .= " WHERE rowid = ".((int) $this->id);
  210. $resql = $this->db->query($sql);
  211. if ($resql) {
  212. return 1;
  213. } else {
  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)) {
  257. $notooltip = 1; // Force disable tooltips
  258. }
  259. $result = '';
  260. $label = '<u>'.$langs->trans("Bookmark").'</u>';
  261. $label .= '<br>';
  262. $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
  263. $url = DOL_URL_ROOT.'/bookmarks/card.php?id='.$this->id;
  264. if ($option != 'nolink') {
  265. // Add param to save lastsearch_values or not
  266. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  267. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  268. $add_save_lastsearch_values = 1;
  269. }
  270. if ($add_save_lastsearch_values) {
  271. $url .= '&save_lastsearch_values=1';
  272. }
  273. }
  274. $linkclose = '';
  275. if (empty($notooltip)) {
  276. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  277. $label = $langs->trans("ShowBookmark");
  278. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  279. }
  280. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  281. $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
  282. } else {
  283. $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
  284. }
  285. $linkstart = '<a href="'.$url.'"';
  286. $linkstart .= $linkclose.'>';
  287. $linkend = '</a>';
  288. $result .= $linkstart;
  289. if ($withpicto) {
  290. $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);
  291. }
  292. if ($withpicto != 2) {
  293. $result .= $this->ref;
  294. }
  295. $result .= $linkend;
  296. //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
  297. global $action, $hookmanager;
  298. $hookmanager->initHooks(array('mybookmarkdao'));
  299. $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
  300. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  301. if ($reshook > 0) {
  302. $result = $hookmanager->resPrint;
  303. } else {
  304. $result .= $hookmanager->resPrint;
  305. }
  306. return $result;
  307. }
  308. }