category.class.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * Insipid
  4. * Personal web-bookmark-system
  5. *
  6. * Copyright 2016-2017 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 bool|int
  51. */
  52. public function initbystring($string) {
  53. $this->_id = false;
  54. if(!empty($string)) {
  55. $queryStr = "SELECT `id`,`name` FROM `".DB_PREFIX."_category`
  56. WHERE `name` = '".$this->DB->real_escape_string($string)."'";
  57. $query = $this->DB->query($queryStr);
  58. if(!empty($query) && $query->num_rows > 0) {
  59. $result = $query->fetch_assoc();
  60. $this->_id = $result['id'];
  61. $this->_data = $result;
  62. }
  63. else {
  64. $queryStr = "INSERT INTO `".DB_PREFIX."_category`
  65. SET `name` = '".$this->DB->real_escape_string($string)."'";
  66. $this->DB->query($queryStr);
  67. if(!empty($this->DB->insert_id)) {
  68. $this->_id = $this->DB->insert_id;
  69. $this->_data['id'] = $this->_id;
  70. $this->_data['name'] = $string;
  71. }
  72. }
  73. }
  74. return $this->_id;
  75. }
  76. /**
  77. * by given DB table id load all the info we need
  78. * @param int $id
  79. * @return mixed
  80. */
  81. public function initbyid($id) {
  82. $this->_id = false;
  83. if(!empty($id)) {
  84. $queryStr = "SELECT id,name
  85. FROM `".DB_PREFIX."_category`
  86. WHERE `id` = '".$this->DB->real_escape_string($id)."'";
  87. $query = $this->DB->query($queryStr);
  88. if(!empty($query) && $query->num_rows > 0) {
  89. $result = $query->fetch_assoc();
  90. $this->_id = $id;
  91. $this->_data = $result;
  92. }
  93. }
  94. return $this->_id;
  95. }
  96. /**
  97. * return all or data fpr given key on the current loaded tag
  98. * @param bool $key
  99. * @return array|mixed
  100. */
  101. public function getData($key=false) {
  102. $ret = $this->_data;
  103. if(!empty($key) && isset($this->_data[$key])) {
  104. $ret = $this->_data[$key];
  105. }
  106. return $ret;
  107. }
  108. /**
  109. * set the relation to the given link to the loaded category
  110. * @param int $linkid
  111. * @return void
  112. */
  113. public function setRelation($linkid) {
  114. if(!empty($linkid) && !empty($this->_id)) {
  115. $queryStr = "INSERT IGNORE INTO `".DB_PREFIX."_categoryrelation`
  116. SET `linkid` = '".$this->DB->real_escape_string($linkid)."',
  117. `categoryid` = '".$this->DB->real_escape_string($this->_id)."'";
  118. $this->DB->query($queryStr);
  119. }
  120. }
  121. /**
  122. * deletes the current loaded category from db
  123. * @return boolean
  124. */
  125. public function delete() {
  126. $ret = false;
  127. if(!empty($this->_id)) {
  128. $this->DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
  129. try {
  130. $queryStr = "DELETE
  131. FROM `".DB_PREFIX."_categoryrelation`
  132. WHERE `categoryid` = '".$this->DB->real_escape_string($this->_id)."'";
  133. $this->DB->query($queryStr);
  134. $queryStr = "DELETE
  135. FROM `".DB_PREFIX."_category`
  136. WHERE `id` = '".$this->DB->real_escape_string($this->_id)."'";
  137. $this->DB->query($queryStr);
  138. $this->DB->commit();
  139. } catch (Exception $e) {
  140. if(DEBUG) {
  141. var_dump($e->getMessage());
  142. }
  143. error_log('ERROR Failed to remove category: '.var_export($e->getMessage(),true));
  144. $this->DB->rollback();
  145. }
  146. }
  147. return $ret;
  148. }
  149. }