PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/bookmarks/class/bookmark.class.php

https://github.com/asterix14/dolibarr
PHP | 206 lines | 131 code | 22 blank | 53 comment | 9 complexity | 8be6b8b09787846ba850e1bf87c98dc9 MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2005 Laurent Destailleur <eldy@users.sourceforge.net>
  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 2 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/bookmarks/class/bookmark.class.php
  19. * \ingroup bookmark
  20. * \brief File of class to manage bookmarks
  21. */
  22. /**
  23. * \class Bookmark
  24. * \brief Class to manage bookmarks
  25. */
  26. class Bookmark
  27. {
  28. var $db;
  29. var $id;
  30. var $fk_user;
  31. var $datec;
  32. var $url;
  33. var $target; // 0=replace, 1=new window
  34. var $title;
  35. var $position;
  36. var $favicon;
  37. /**
  38. * Constructor
  39. *
  40. * @param DoliDB $db Database handler
  41. */
  42. function Bookmark($db)
  43. {
  44. $this->db = $db;
  45. }
  46. /**
  47. * Directs the bookmark
  48. *
  49. * @param int $id Bookmark Id Loader
  50. * @return int <0 if KO, >0 if OK
  51. */
  52. function fetch($id)
  53. {
  54. $sql = "SELECT rowid, fk_user, dateb as datec, url, target,";
  55. $sql.= " title, position, favicon";
  56. $sql.= " FROM ".MAIN_DB_PREFIX."bookmark";
  57. $sql.= " WHERE rowid = ".$id;
  58. dol_syslog("Bookmark::fetch sql=".$sql, LOG_DEBUG);
  59. $resql = $this->db->query($sql);
  60. if ($resql)
  61. {
  62. $obj = $this->db->fetch_object($resql);
  63. $this->id = $obj->rowid;
  64. $this->ref = $obj->rowid;
  65. $this->fk_user = $obj->fk_user;
  66. $this->datec = $this->db->jdate($obj->datec);
  67. $this->url = $obj->url;
  68. $this->target = $obj->target;
  69. $this->title = $obj->title;
  70. $this->position= $obj->position;
  71. $this->favicon = $obj->favicon;
  72. $this->db->free($resql);
  73. return $this->id;
  74. }
  75. else
  76. {
  77. dol_print_error($this->db);
  78. return -1;
  79. }
  80. }
  81. /**
  82. * Insert bookmark into database
  83. *
  84. * @return int <0 si ko, rowid du bookmark cree si ok
  85. */
  86. function create()
  87. {
  88. // Clean parameters
  89. $this->url=trim($this->url);
  90. $this->title=trim($this->title);
  91. if (empty($this->position)) $this->position=0;
  92. $this->db->begin();
  93. $sql = "INSERT INTO ".MAIN_DB_PREFIX."bookmark (fk_user,dateb,url,target";
  94. $sql.= " ,title,favicon,position";
  95. if ($this->fk_soc) $sql.=",fk_soc";
  96. $sql.= ") VALUES (";
  97. $sql.= ($this->fk_user > 0?"'".$this->fk_user."'":"0").",";
  98. $sql.= " ".$this->db->idate(gmmktime()).",";
  99. $sql.= " '".$this->url."', '".$this->target."',";
  100. $sql.= " '".$this->db->escape($this->title)."', '".$this->favicon."', '".$this->position."'";
  101. if ($this->fk_soc) $sql.=",".$this->fk_soc;
  102. $sql.= ")";
  103. dol_syslog("Bookmark::update sql=".$sql, LOG_DEBUG);
  104. $resql = $this->db->query($sql);
  105. if ($resql)
  106. {
  107. $id = $this->db->last_insert_id(MAIN_DB_PREFIX."bookmark");
  108. if ($id > 0)
  109. {
  110. $this->id = $id;
  111. $this->db->commit();
  112. return $id;
  113. }
  114. else
  115. {
  116. $this->error=$this->db->lasterror();
  117. $this->errno=$this->db->lasterrno();
  118. $this->db->rollback();
  119. return -2;
  120. }
  121. }
  122. else
  123. {
  124. $this->error=$this->db->lasterror();
  125. $this->errno=$this->db->lasterrno();
  126. $this->db->rollback();
  127. return -1;
  128. }
  129. }
  130. /**
  131. * Update bookmark record
  132. *
  133. * @return int <0 if KO, > if OK
  134. */
  135. function update()
  136. {
  137. // Clean parameters
  138. $this->url=trim($this->url);
  139. $this->title=trim($this->title);
  140. if (empty($this->position)) $this->position=0;
  141. $sql = "UPDATE ".MAIN_DB_PREFIX."bookmark";
  142. $sql.= " SET fk_user = ".($this->fk_user > 0?"'".$this->fk_user."'":"0");
  143. $sql.= " ,dateb = '".$this->db->idate($this->datec)."'";
  144. $sql.= " ,url = '".$this->db->escape($this->url)."'";
  145. $sql.= " ,target = '".$this->target."'";
  146. $sql.= " ,title = '".$this->db->escape($this->title)."'";
  147. $sql.= " ,favicon = '".$this->favicon."'";
  148. $sql.= " ,position = '".$this->position."'";
  149. $sql.= " WHERE rowid = ".$this->id;
  150. dol_syslog("Bookmark::update sql=".$sql, LOG_DEBUG);
  151. if ($this->db->query($sql))
  152. {
  153. return 1;
  154. }
  155. else
  156. {
  157. $this->error=$this->db->lasterror();
  158. return -1;
  159. }
  160. }
  161. /**
  162. * Removes the bookmark
  163. *
  164. * @param int $id Id removed bookmark
  165. * @return int <0 si ko, >0 si ok
  166. */
  167. function remove($id)
  168. {
  169. $sql = "DELETE FROM ".MAIN_DB_PREFIX."bookmark";
  170. $sql .= " WHERE rowid = ".$id;
  171. dol_syslog("Bookmark::remove sql=".$sql, LOG_DEBUG);
  172. $resql=$this->db->query($sql);
  173. if ($resql)
  174. {
  175. return 1;
  176. }
  177. else
  178. {
  179. $this->error=$this->db->lasterror();
  180. return -1;
  181. }
  182. }
  183. }
  184. ?>