tag.class.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /**
  3. * Insipid
  4. * Personal web-bookmark-system
  5. *
  6. * Copyright 2016-2023 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 mysqli
  36. */
  37. private mysqli $DB;
  38. /**
  39. * the current loaded tag by DB id
  40. *
  41. * @var string
  42. */
  43. private string $_id;
  44. /**
  45. * current loaded tag data
  46. *
  47. * @var array
  48. */
  49. private array $_data;
  50. /**
  51. * Tag constructor.
  52. *
  53. * @param mysqli $databaseConnectionObject
  54. */
  55. public function __construct(mysqli $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, bool $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. if(QUERY_DEBUG) Summoner::sysLog("QUERY ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  72. try {
  73. $query = $this->DB->query($queryStr);
  74. if(!empty($query) && $query->num_rows > 0) {
  75. $result = $query->fetch_assoc();
  76. $this->_id = $result['id'];
  77. $this->_data = $result;
  78. $ret = 1;
  79. }
  80. else {
  81. if(!$doNotCreate) {
  82. $queryStr = "INSERT INTO `" . DB_PREFIX . "_tag`
  83. SET `name` = '" . $this->DB->real_escape_string($string) . "'";
  84. if(QUERY_DEBUG) Summoner::sysLog("QUERY ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  85. $this->DB->query($queryStr);
  86. if (!empty($this->DB->insert_id)) {
  87. $this->_id = $this->DB->insert_id;
  88. $this->_data['id'] = $this->_id;
  89. $this->_data['name'] = $string;
  90. $ret = 2;
  91. }
  92. }
  93. else {
  94. $ret = 3;
  95. }
  96. }
  97. } catch (Exception $e) {
  98. Summoner::sysLog("ERROR ".__METHOD__." mysql catch: ".$e->getMessage());
  99. }
  100. }
  101. return $ret;
  102. }
  103. /**
  104. * by given DB table id load all the info we need
  105. *
  106. * @param string $id
  107. * @return string
  108. */
  109. public function initbyid(string $id): string {
  110. $this->_id = 0;
  111. if(!empty($id)) {
  112. $queryStr = "SELECT `id`,`name` FROM `".DB_PREFIX."_tag`
  113. WHERE `id` = '".$this->DB->real_escape_string($id)."'";
  114. if(QUERY_DEBUG) Summoner::sysLog("QUERY ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  115. try {
  116. $query = $this->DB->query($queryStr);
  117. if(!empty($query) && $query->num_rows > 0) {
  118. $result = $query->fetch_assoc();
  119. $this->_id = $result['id'];
  120. $this->_data = $result;
  121. }
  122. } catch (Exception $e) {
  123. Summoner::sysLog("ERROR ".__METHOD__." mysql catch: ".$e->getMessage());
  124. }
  125. }
  126. return $this->_id;
  127. }
  128. /**
  129. * return all or data fpr given key on the current loaded tag
  130. *
  131. * @param bool $key
  132. * @return array|string
  133. */
  134. public function getData($key=false) {
  135. $ret = $this->_data;
  136. if(!empty($key) && isset($this->_data[$key])) {
  137. $ret = $this->_data[$key];
  138. }
  139. return $ret;
  140. }
  141. /**
  142. * set the relation to the given link to the loaded tag
  143. *
  144. * @param string $linkid
  145. * @return void
  146. */
  147. public function setRelation(string $linkid): void {
  148. if(!empty($linkid) && !empty($this->_id)) {
  149. $queryStr = "INSERT IGNORE INTO `".DB_PREFIX."_tagrelation`
  150. SET `linkid` = '".$this->DB->real_escape_string($linkid)."',
  151. `tagid` = '".$this->DB->real_escape_string($this->_id)."'";
  152. if(QUERY_DEBUG) Summoner::sysLog("QUERY ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  153. try {
  154. $this->DB->query($queryStr);
  155. } catch (Exception $e) {
  156. Summoner::sysLog("ERROR ".__METHOD__." mysql catch: ".$e->getMessage());
  157. }
  158. }
  159. }
  160. /**
  161. * Return an array of any linkid related to the current loaded tag
  162. *
  163. * @return array
  164. */
  165. public function getReleations(): array {
  166. $ret = array();
  167. $queryStr = "SELECT linkid
  168. FROM `".DB_PREFIX."_tagrelation`
  169. WHERE tagid = '".$this->DB->real_escape_string($this->_id)."'";
  170. if(QUERY_DEBUG) Summoner::sysLog("QUERY ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  171. try {
  172. $query = $this->DB->query($queryStr);
  173. if(!empty($query) && $query->num_rows > 0) {
  174. while($result = $query->fetch_assoc()) {
  175. $ret[] = $result['linkid'];
  176. }
  177. }
  178. } catch (Exception $e) {
  179. Summoner::sysLog("ERROR ".__METHOD__." mysql catch: ".$e->getMessage());
  180. }
  181. return $ret;
  182. }
  183. /**
  184. * deletes the current loaded tag from db
  185. *
  186. * @return boolean
  187. */
  188. public function delete(): bool {
  189. $ret = false;
  190. if(!empty($this->_id)) {
  191. $this->DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
  192. try {
  193. $queryStr = "DELETE
  194. FROM `".DB_PREFIX."_tagrelation`
  195. WHERE `tagid` = '".$this->DB->real_escape_string($this->_id)."'";
  196. $this->DB->query($queryStr);
  197. $queryStr = "DELETE
  198. FROM `".DB_PREFIX."_tag`
  199. WHERE `id` = '".$this->DB->real_escape_string($this->_id)."'";
  200. if(QUERY_DEBUG) Summoner::sysLog("QUERY ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  201. $this->DB->query($queryStr);
  202. $this->DB->commit();
  203. } catch (Exception $e) {
  204. Summoner::sysLog('ERROR Failed to remove tag: '.$e->getMessage());
  205. $this->DB->rollback();
  206. }
  207. }
  208. return $ret;
  209. }
  210. /**
  211. * Rename current loaded tag name
  212. *
  213. * @param string $newValue
  214. * @return void
  215. */
  216. public function rename(string $newValue): void {
  217. if(!empty($newValue)) {
  218. $queryStr = "UPDATE `".DB_PREFIX."_tag`
  219. SET `name` = '".$this->DB->real_escape_string($newValue)."'
  220. WHERE `id` = '".$this->DB->real_escape_string($this->_id)."'";
  221. if(QUERY_DEBUG) Summoner::sysLog("QUERY ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  222. try {
  223. $this->DB->query($queryStr);
  224. } catch (Exception $e) {
  225. Summoner::sysLog("ERROR ".__METHOD__." mysql catch: ".$e->getMessage());
  226. }
  227. $this->_data['name'] = $newValue;
  228. }
  229. }
  230. }