category.class.php 8.3 KB

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