managetags.class.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Bibliotheca
  4. *
  5. * Copyright 2018-2023 Johannes Keßler
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0.
  19. */
  20. /**
  21. * Class ManageTags to manage the tags of a collection
  22. */
  23. class ManageTags {
  24. /**
  25. * The database object
  26. *
  27. * @var mysqli
  28. */
  29. private mysqli $_DB;
  30. /**
  31. * The user object to query with
  32. *
  33. * @var Doomguy
  34. */
  35. private Doomguy $_User;
  36. /**
  37. * Currently loaded collection to work with
  38. *
  39. * @var string Number
  40. */
  41. private string $_collectionId;
  42. /**
  43. * ManageTags constructor.
  44. *
  45. * @param mysqli $databaseConnectionObject
  46. * @param Doomguy $userObj
  47. */
  48. public function __construct(mysqli $databaseConnectionObject, Doomguy $userObj) {
  49. $this->_DB = $databaseConnectionObject;
  50. $this->_User = $userObj;
  51. }
  52. /**
  53. * Set the to work with collection id
  54. *
  55. * @param string $collectionId Number
  56. */
  57. public function setCollection(string $collectionId): void {
  58. if(!empty($collectionId)) {
  59. $this->_collectionId = $collectionId;
  60. }
  61. }
  62. /**
  63. * Either move, rename or delete (only one of those) with the given field
  64. * and its value
  65. *
  66. * Return strategy here: empty string if everything works or nothing is to do. String error msg for error
  67. *
  68. * @param string $ident ID to use in lookup table
  69. * @param array $data Needs use=fromString, move=toString, doDelete=true
  70. * @return string
  71. */
  72. public function doWithTag(string $ident, array $data): string {
  73. $ret = '';
  74. if(!empty($this->_collectionId) && !empty($ident) && !empty($data) && isset($data['use']) && !empty($data['use'])) {
  75. if(isset($data['move']) && !empty($data['move'])) {
  76. $ret = $this->_move($ident, $data['use'], $data['move']);
  77. }
  78. elseif (isset($data['rename']) && !empty($data['rename'])) {
  79. $ret = $this->_move($ident, $data['use'], $data['rename']);
  80. }
  81. elseif (isset($data['doDelete']) && !empty($data['doDelete'])) {
  82. $ret = $this->_delete($ident, $data['use']);
  83. }
  84. }
  85. return $ret;
  86. }
  87. /**
  88. * Move in field from to given new string
  89. * Does a BINARY compare in SQL for $from
  90. *
  91. * @param string $field Field ID to use in lookup table
  92. * @param string $from Value string to search for in lookup table
  93. * @param string $to Value string to set to in lookup table
  94. * @return string
  95. */
  96. private function _move(string $field, string $from, string $to): string {
  97. $ret = '';
  98. if(!Summoner::validate($field,'digit')) return 'Invalid field id for move/rename';
  99. if(!Summoner::validate($from)) return 'Invalid use data for move/rename';
  100. if(!Summoner::validate($to)) return 'Invalid to data for move/rename';
  101. $queryStr = "UPDATE `".DB_PREFIX."_collection_entry2lookup_".$this->_DB->real_escape_string($this->_collectionId)."`
  102. SET `value` = '".$this->_DB->real_escape_string($to)."'
  103. WHERE `fk_field` = '".$this->_DB->real_escape_string($field)."'
  104. AND `value` = BINARY '".$this->_DB->real_escape_string($from)."'";
  105. if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  106. try {
  107. $this->_DB->query($queryStr);
  108. }
  109. catch (Exception $e) {
  110. Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
  111. $ret = 'Error in move/rename query. See logs.';
  112. }
  113. return $ret;
  114. }
  115. /**
  116. * Delete the given $what for field $field in entry lookup table.
  117. * Does a BINARY compare in SQL for $what
  118. *
  119. * @param string $field Field ID to use in lookup table
  120. * @param string $what Value to search for and delete from lookup table
  121. * @return string
  122. */
  123. private function _delete(string $field, string $what): string {
  124. $ret = '';
  125. if(!Summoner::validate($field,'digit')) return 'Invalid field id for delete';
  126. if(!Summoner::validate($what)) return 'Invalid use data for delete';
  127. $queryStr = "DELETE FROM `".DB_PREFIX."_collection_entry2lookup_".$this->_DB->real_escape_string($this->_collectionId)."`
  128. WHERE `fk_field` = '".$this->_DB->real_escape_string($field)."'
  129. AND `value` = BINARY '".$this->_DB->real_escape_string($what)."'";
  130. if(QUERY_DEBUG) Summoner::sysLog("[QUERY] ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
  131. try {
  132. $this->_DB->query($queryStr);
  133. }
  134. catch (Exception $e) {
  135. Summoner::sysLog("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
  136. $ret = 'Error in delete query. See logs.';
  137. }
  138. return $ret;
  139. }
  140. }