category.class.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 Category {
  29. /**
  30. * the database object
  31. * @var object
  32. */
  33. private $DB;
  34. /**
  35. * the current loaded category 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. * @return bool|int
  52. */
  53. public function initbystring($string, $doNotCreate=false) {
  54. $ret = 0;
  55. $this->_id = false;
  56. if(!empty($string)) {
  57. $queryStr = "SELECT `id`,`name` FROM `".DB_PREFIX."_category`
  58. WHERE `name` = '".$this->DB->real_escape_string($string)."'";
  59. $query = $this->DB->query($queryStr);
  60. if(!empty($query) && $query->num_rows > 0) {
  61. $result = $query->fetch_assoc();
  62. $this->_id = $result['id'];
  63. $this->_data = $result;
  64. $ret = 1;
  65. }
  66. else {
  67. if(!$doNotCreate) {
  68. $queryStr = "INSERT INTO `" . DB_PREFIX . "_category`
  69. SET `name` = '" . $this->DB->real_escape_string($string) . "'";
  70. $this->DB->query($queryStr);
  71. if (!empty($this->DB->insert_id)) {
  72. $this->_id = $this->DB->insert_id;
  73. $this->_data['id'] = $this->_id;
  74. $this->_data['name'] = $string;
  75. $ret = 2;
  76. }
  77. }
  78. else {
  79. $ret=3;
  80. }
  81. }
  82. }
  83. return $ret;
  84. }
  85. /**
  86. * by given DB table id load all the info we need
  87. * @param int $id
  88. * @return mixed
  89. */
  90. public function initbyid($id) {
  91. $this->_id = false;
  92. if(!empty($id)) {
  93. $queryStr = "SELECT id,name
  94. FROM `".DB_PREFIX."_category`
  95. WHERE `id` = '".$this->DB->real_escape_string($id)."'";
  96. $query = $this->DB->query($queryStr);
  97. if(!empty($query) && $query->num_rows > 0) {
  98. $result = $query->fetch_assoc();
  99. $this->_id = $id;
  100. $this->_data = $result;
  101. }
  102. }
  103. return $this->_id;
  104. }
  105. /**
  106. * return all or data fpr given key on the current loaded tag
  107. * @param bool $key
  108. * @return array|mixed
  109. */
  110. public function getData($key=false) {
  111. $ret = $this->_data;
  112. if(!empty($key) && isset($this->_data[$key])) {
  113. $ret = $this->_data[$key];
  114. }
  115. return $ret;
  116. }
  117. /**
  118. * set the relation to the given link to the loaded category
  119. * @param int $linkid
  120. * @return void
  121. */
  122. public function setRelation($linkid) {
  123. if(!empty($linkid) && !empty($this->_id)) {
  124. $queryStr = "INSERT IGNORE INTO `".DB_PREFIX."_categoryrelation`
  125. SET `linkid` = '".$this->DB->real_escape_string($linkid)."',
  126. `categoryid` = '".$this->DB->real_escape_string($this->_id)."'";
  127. $this->DB->query($queryStr);
  128. }
  129. }
  130. /**
  131. * Return an array of any linkid related to the current loaded category
  132. * @return array
  133. */
  134. public function getReleations() {
  135. $ret = array();
  136. $queryStr = "SELECT linkid
  137. FROM `".DB_PREFIX."_categoryrelation`
  138. WHERE `categoryid` = '".$this->DB->real_escape_string($this->_id)."'";
  139. $query = $this->DB->query($queryStr);
  140. if(!empty($query) && $query->num_rows > 0) {
  141. while($result = $query->fetch_assoc()) {
  142. $ret[] = $result['linkid'];
  143. }
  144. }
  145. return $ret;
  146. }
  147. /**
  148. * deletes the current loaded category from db
  149. * @return boolean
  150. */
  151. public function delete() {
  152. $ret = false;
  153. if(!empty($this->_id)) {
  154. $this->DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
  155. try {
  156. $queryStr = "DELETE
  157. FROM `".DB_PREFIX."_categoryrelation`
  158. WHERE `categoryid` = '".$this->DB->real_escape_string($this->_id)."'";
  159. $this->DB->query($queryStr);
  160. $queryStr = "DELETE
  161. FROM `".DB_PREFIX."_category`
  162. WHERE `id` = '".$this->DB->real_escape_string($this->_id)."'";
  163. $this->DB->query($queryStr);
  164. $this->DB->commit();
  165. } catch (Exception $e) {
  166. if(DEBUG) {
  167. var_dump($e->getMessage());
  168. }
  169. error_log('ERROR Failed to remove category: '.var_export($e->getMessage(),true));
  170. $this->DB->rollback();
  171. }
  172. }
  173. return $ret;
  174. }
  175. /**
  176. * Rename current loaded cat name
  177. * @param $newValue
  178. * @return void
  179. */
  180. public function rename($newValue) {
  181. if(!empty($newValue)) {
  182. $queryStr = "UPDATE `".DB_PREFIX."_category`
  183. SET `name` = '".$this->DB->real_escape_string($newValue)."'
  184. WHERE `id` = '".$this->DB->real_escape_string($this->_id)."'";
  185. $this->DB->query($queryStr);
  186. $this->_data['name'] = $newValue;
  187. }
  188. }
  189. }