tag.class.php 4.0 KB

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