tag.class.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * Insipid
  4. * Personal web-bookmark-system
  5. *
  6. * Copyright 2016-2021 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. /**
  29. * Class Tag
  30. */
  31. class Tag {
  32. /**
  33. * the database object
  34. *
  35. * @var object
  36. */
  37. private $DB;
  38. /**
  39. * the current loaded tag by DB id
  40. *
  41. * @var int
  42. */
  43. private $_id;
  44. /**
  45. * current loaded tag data
  46. *
  47. * @var array
  48. */
  49. private $_data;
  50. /**
  51. * Tag constructor.
  52. *
  53. * @param Obnject $databaseConnectionObject
  54. */
  55. public function __construct($databaseConnectionObject) {
  56. $this->DB = $databaseConnectionObject;
  57. }
  58. /**
  59. * by given string load the info from the DB and even create if not existing
  60. *
  61. * @param string $string
  62. * @param bool $doNotCreate
  63. * @return int 0=fail, 1=existing, 2=new, 3=newNotCreated
  64. */
  65. public function initbystring(string $string, $doNotCreate=false): int {
  66. $ret = 0;
  67. $this->_id = false;
  68. if(!empty($string)) {
  69. $queryStr = "SELECT `id`,`name` FROM `".DB_PREFIX."_tag`
  70. WHERE `name` = '".$this->DB->real_escape_string($string)."'";
  71. $query = $this->DB->query($queryStr);
  72. if(!empty($query) && $query->num_rows > 0) {
  73. $result = $query->fetch_assoc();
  74. $this->_id = $result['id'];
  75. $this->_data = $result;
  76. $ret = 1;
  77. }
  78. else {
  79. if(!$doNotCreate) {
  80. $queryStr = "INSERT INTO `" . DB_PREFIX . "_tag`
  81. SET `name` = '" . $this->DB->real_escape_string($string) . "'";
  82. $this->DB->query($queryStr);
  83. if (!empty($this->DB->insert_id)) {
  84. $this->_id = $this->DB->insert_id;
  85. $this->_data['id'] = $this->_id;
  86. $this->_data['name'] = $string;
  87. $ret = 2;
  88. }
  89. }
  90. else {
  91. $ret=3;
  92. }
  93. }
  94. }
  95. return $ret;
  96. }
  97. /**
  98. * by given DB table id load all the info we need
  99. *
  100. * @param int $id
  101. * @return int
  102. */
  103. public function initbyid(int $id): int {
  104. $this->_id = 0;
  105. if(!empty($id)) {
  106. $queryStr = "SELECT `id`,`name` FROM `".DB_PREFIX."_tag`
  107. WHERE `id` = '".$this->DB->real_escape_string($id)."'";
  108. $query = $this->DB->query($queryStr);
  109. if(!empty($query) && $query->num_rows > 0) {
  110. $result = $query->fetch_assoc();
  111. $this->_id = $result['id'];
  112. $this->_data = $result;
  113. }
  114. }
  115. return $this->_id;
  116. }
  117. /**
  118. * return all or data fpr given key on the current loaded tag
  119. *
  120. * @param bool $key
  121. * @return array|string
  122. */
  123. public function getData($key=false) {
  124. $ret = $this->_data;
  125. if(!empty($key) && isset($this->_data[$key])) {
  126. $ret = $this->_data[$key];
  127. }
  128. return $ret;
  129. }
  130. /**
  131. * set the relation to the given link to the loaded tag
  132. *
  133. * @param int $linkid
  134. * @return void
  135. */
  136. public function setRelation(int $linkid) {
  137. if(!empty($linkid) && !empty($this->_id)) {
  138. $queryStr = "INSERT IGNORE INTO `".DB_PREFIX."_tagrelation`
  139. SET `linkid` = '".$this->DB->real_escape_string($linkid)."',
  140. `tagid` = '".$this->DB->real_escape_string($this->_id)."'";
  141. $this->DB->query($queryStr);
  142. }
  143. }
  144. /**
  145. * Return an array of any linkid related to the current loaded tag
  146. *
  147. * @return array
  148. */
  149. public function getReleations(): array {
  150. $ret = array();
  151. $queryStr = "SELECT linkid
  152. FROM `".DB_PREFIX."_tagrelation`
  153. WHERE tagid = '".$this->DB->real_escape_string($this->_id)."'";
  154. $query = $this->DB->query($queryStr);
  155. if(!empty($query) && $query->num_rows > 0) {
  156. while($result = $query->fetch_assoc()) {
  157. $ret[] = $result['linkid'];
  158. }
  159. }
  160. return $ret;
  161. }
  162. /**
  163. * deletes the current loaded tag from db
  164. *
  165. * @return boolean
  166. */
  167. public function delete(): bool {
  168. $ret = false;
  169. if(!empty($this->_id)) {
  170. $this->DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
  171. try {
  172. $queryStr = "DELETE
  173. FROM `".DB_PREFIX."_tagrelation`
  174. WHERE `tagid` = '".$this->DB->real_escape_string($this->_id)."'";
  175. $this->DB->query($queryStr);
  176. $queryStr = "DELETE
  177. FROM `".DB_PREFIX."_tag`
  178. WHERE `id` = '".$this->DB->real_escape_string($this->_id)."'";
  179. $this->DB->query($queryStr);
  180. $this->DB->commit();
  181. } catch (Exception $e) {
  182. if(DEBUG) {
  183. var_dump($e->getMessage());
  184. }
  185. error_log('ERROR Failed to remove tag: '.var_export($e->getMessage(),true));
  186. $this->DB->rollback();
  187. }
  188. }
  189. return $ret;
  190. }
  191. /**
  192. * Rename current loaded tag name
  193. *
  194. * @param string $newValue
  195. * @return void
  196. */
  197. public function rename(string $newValue) {
  198. if(!empty($newValue)) {
  199. $queryStr = "UPDATE `".DB_PREFIX."_tag`
  200. SET `name` = '".$this->DB->real_escape_string($newValue)."'
  201. WHERE `id` = '".$this->DB->real_escape_string($this->_id)."'";
  202. $this->DB->query($queryStr);
  203. $this->_data['name'] = $newValue;
  204. }
  205. }
  206. }