tag.class.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * Insipid
  4. * Personal web-bookmark-system
  5. *
  6. * Copyright 2016-2020 Johannes Keßler
  7. *
  8. * Development starting from 2011: Johannes Keßler
  9. * https://www.bananas-playground.net/projekt/insipid/
  10. *
  11. * creator:
  12. * Luke Reeves <luke@neuro-tech.net>
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation, either version 3 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0.
  26. *
  27. */
  28. class Tag {
  29. /**
  30. * the database object
  31. * @var object
  32. */
  33. private $DB;
  34. /**
  35. * the current loaded tag by DB id
  36. * @var int
  37. */
  38. private $_id;
  39. /**
  40. * current loaded tag data
  41. * @var array
  42. */
  43. private $_data;
  44. public function __construct($databaseConnectionObject) {
  45. $this->DB = $databaseConnectionObject;
  46. }
  47. /**
  48. * by given string load the info from the DB and even create if not existing
  49. * @param string $string
  50. * @return int 0=fail, 1=existing, 2=new, 3=newNotCreated
  51. */
  52. public function initbystring($string, $doNotCreate=false) {
  53. $ret = 0;
  54. $this->_id = false;
  55. if(!empty($string)) {
  56. $queryStr = "SELECT `id`,`name` FROM `".DB_PREFIX."_tag`
  57. WHERE `name` = '".$this->DB->real_escape_string($string)."'";
  58. $query = $this->DB->query($queryStr);
  59. if(!empty($query) && $query->num_rows > 0) {
  60. $result = $query->fetch_assoc();
  61. $this->_id = $result['id'];
  62. $this->_data = $result;
  63. $ret = 1;
  64. }
  65. else {
  66. if(!$doNotCreate) {
  67. $queryStr = "INSERT INTO `" . DB_PREFIX . "_tag`
  68. SET `name` = '" . $this->DB->real_escape_string($string) . "'";
  69. $this->DB->query($queryStr);
  70. if (!empty($this->DB->insert_id)) {
  71. $this->_id = $this->DB->insert_id;
  72. $this->_data['id'] = $this->_id;
  73. $this->_data['name'] = $string;
  74. $ret = 2;
  75. }
  76. }
  77. else {
  78. $ret=3;
  79. }
  80. }
  81. }
  82. return $ret;
  83. }
  84. /**
  85. * by given DB table id load all the info we need
  86. * @param int $id
  87. * @return bool|int
  88. */
  89. public function initbyid($id) {
  90. $this->_id = false;
  91. if(!empty($id)) {
  92. $queryStr = "SELECT `id`,`name` FROM `".DB_PREFIX."_tag`
  93. WHERE `id` = '".$this->DB->real_escape_string($id)."'";
  94. $query = $this->DB->query($queryStr);
  95. if(!empty($query) && $query->num_rows > 0) {
  96. $result = $query->fetch_assoc();
  97. $this->_id = $result['id'];
  98. $this->_data = $result;
  99. }
  100. }
  101. return $this->_id;
  102. }
  103. /**
  104. * return all or data fpr given key on the current loaded tag
  105. * @param bool $key
  106. * @return array|mixed
  107. */
  108. public function getData($key=false) {
  109. $ret = $this->_data;
  110. if(!empty($key) && isset($this->_data[$key])) {
  111. $ret = $this->_data[$key];
  112. }
  113. return $ret;
  114. }
  115. /**
  116. * set the relation to the given link to the loaded tag
  117. * @param int $linkid
  118. * @return void
  119. */
  120. public function setRelation($linkid) {
  121. if(!empty($linkid) && !empty($this->_id)) {
  122. $queryStr = "INSERT IGNORE INTO `".DB_PREFIX."_tagrelation`
  123. SET `linkid` = '".$this->DB->real_escape_string($linkid)."',
  124. `tagid` = '".$this->DB->real_escape_string($this->_id)."'";
  125. $this->DB->query($queryStr);
  126. }
  127. }
  128. /**
  129. * Return an array of any linkid related to the current loaded tag
  130. * @return array
  131. */
  132. public function getReleations() {
  133. $ret = array();
  134. $queryStr = "SELECT linkid
  135. FROM `".DB_PREFIX."_tagrelation`
  136. WHERE tagid = '".$this->DB->real_escape_string($this->_id)."'";
  137. $query = $this->DB->query($queryStr);
  138. if(!empty($query) && $query->num_rows > 0) {
  139. while($result = $query->fetch_assoc()) {
  140. $ret[] = $result['linkid'];
  141. }
  142. }
  143. return $ret;
  144. }
  145. /**
  146. * deletes the current loaded tag from db
  147. * @return boolean
  148. */
  149. public function delete() {
  150. $ret = false;
  151. if(!empty($this->_id)) {
  152. $this->DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
  153. try {
  154. $queryStr = "DELETE
  155. FROM `".DB_PREFIX."_tagrelation`
  156. WHERE `tagid` = '".$this->DB->real_escape_string($this->_id)."'";
  157. $this->DB->query($queryStr);
  158. $queryStr = "DELETE
  159. FROM `".DB_PREFIX."_tag`
  160. WHERE `id` = '".$this->DB->real_escape_string($this->_id)."'";
  161. $this->DB->query($queryStr);
  162. $this->DB->commit();
  163. } catch (Exception $e) {
  164. if(DEBUG) {
  165. var_dump($e->getMessage());
  166. }
  167. error_log('ERROR Failed to remove tag: '.var_export($e->getMessage(),true));
  168. $this->DB->rollback();
  169. }
  170. }
  171. return $ret;
  172. }
  173. /**
  174. * Rename current loaded tag name
  175. * @param $newValue
  176. * @return void
  177. */
  178. public function rename($newValue) {
  179. if(!empty($newValue)) {
  180. $queryStr = "UPDATE `".DB_PREFIX."_tag`
  181. SET `name` = '".$this->DB->real_escape_string($newValue)."'
  182. WHERE `id` = '".$this->DB->real_escape_string($this->_id)."'";
  183. $this->DB->query($queryStr);
  184. $this->_data['name'] = $newValue;
  185. }
  186. }
  187. }