123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- <?php
- /**
- * Insipid
- * Personal web-bookmark-system
- *
- * Copyright 2016-2023 Johannes Keßler
- *
- * Development starting from 2011: Johannes Keßler
- * https://www.bananas-playground.net/projekt/insipid/
- *
- * creator:
- * Luke Reeves <luke@neuro-tech.net>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0.
- *
- */
- /**
- * Class Tag
- */
- class Tag {
- /**
- * the database object
- *
- * @var mysqli
- */
- private mysqli $DB;
- /**
- * the current loaded tag by DB id
- *
- * @var string
- */
- private string $_id;
- /**
- * current loaded tag data
- *
- * @var array
- */
- private array $_data;
- /**
- * Tag constructor.
- *
- * @param mysqli $databaseConnectionObject
- */
- public function __construct(mysqli $databaseConnectionObject) {
- $this->DB = $databaseConnectionObject;
- }
- /**
- * by given string load the info from the DB and even create if not existing
- *
- * @param string $string
- * @param bool $doNotCreate
- * @return int 0=fail, 1=existing, 2=new, 3=newNotCreated
- */
- public function initbystring(string $string, bool $doNotCreate=false): int {
- $ret = 0;
- $this->_id = false;
- if(!empty($string)) {
- $queryStr = "SELECT `id`,`name` FROM `".DB_PREFIX."_tag`
- WHERE `name` = '".$this->DB->real_escape_string($string)."'";
- if(QUERY_DEBUG) Summoner::sysLog("QUERY ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
- try {
- $query = $this->DB->query($queryStr);
- if(!empty($query) && $query->num_rows > 0) {
- $result = $query->fetch_assoc();
- $this->_id = $result['id'];
- $this->_data = $result;
- $ret = 1;
- }
- else {
- if(!$doNotCreate) {
- $queryStr = "INSERT INTO `" . DB_PREFIX . "_tag`
- SET `name` = '" . $this->DB->real_escape_string($string) . "'";
- if(QUERY_DEBUG) Summoner::sysLog("QUERY ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
- $this->DB->query($queryStr);
- if (!empty($this->DB->insert_id)) {
- $this->_id = $this->DB->insert_id;
- $this->_data['id'] = $this->_id;
- $this->_data['name'] = $string;
- $ret = 2;
- }
- }
- else {
- $ret = 3;
- }
- }
- } catch (Exception $e) {
- Summoner::sysLog("ERROR ".__METHOD__." mysql catch: ".$e->getMessage());
- }
- }
- return $ret;
- }
- /**
- * by given DB table id load all the info we need
- *
- * @param string $id
- * @return string
- */
- public function initbyid(string $id): string {
- $this->_id = 0;
- if(!empty($id)) {
- $queryStr = "SELECT `id`,`name` FROM `".DB_PREFIX."_tag`
- WHERE `id` = '".$this->DB->real_escape_string($id)."'";
- if(QUERY_DEBUG) Summoner::sysLog("QUERY ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
- try {
- $query = $this->DB->query($queryStr);
- if(!empty($query) && $query->num_rows > 0) {
- $result = $query->fetch_assoc();
- $this->_id = $result['id'];
- $this->_data = $result;
- }
- } catch (Exception $e) {
- Summoner::sysLog("ERROR ".__METHOD__." mysql catch: ".$e->getMessage());
- }
- }
- return $this->_id;
- }
- /**
- * return all or data fpr given key on the current loaded tag
- *
- * @param bool $key
- * @return array|string
- */
- public function getData($key=false) {
- $ret = $this->_data;
- if(!empty($key) && isset($this->_data[$key])) {
- $ret = $this->_data[$key];
- }
- return $ret;
- }
- /**
- * set the relation to the given link to the loaded tag
- *
- * @param string $linkid
- * @return void
- */
- public function setRelation(string $linkid): void {
- if(!empty($linkid) && !empty($this->_id)) {
- $queryStr = "INSERT IGNORE INTO `".DB_PREFIX."_tagrelation`
- SET `linkid` = '".$this->DB->real_escape_string($linkid)."',
- `tagid` = '".$this->DB->real_escape_string($this->_id)."'";
- if(QUERY_DEBUG) Summoner::sysLog("QUERY ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
- try {
- $this->DB->query($queryStr);
- } catch (Exception $e) {
- Summoner::sysLog("ERROR ".__METHOD__." mysql catch: ".$e->getMessage());
- }
- }
- }
- /**
- * Return an array of any linkid related to the current loaded tag
- *
- * @return array
- */
- public function getReleations(): array {
- $ret = array();
- $queryStr = "SELECT linkid
- FROM `".DB_PREFIX."_tagrelation`
- WHERE tagid = '".$this->DB->real_escape_string($this->_id)."'";
- if(QUERY_DEBUG) Summoner::sysLog("QUERY ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
- try {
- $query = $this->DB->query($queryStr);
- if(!empty($query) && $query->num_rows > 0) {
- while($result = $query->fetch_assoc()) {
- $ret[] = $result['linkid'];
- }
- }
- } catch (Exception $e) {
- Summoner::sysLog("ERROR ".__METHOD__." mysql catch: ".$e->getMessage());
- }
- return $ret;
- }
- /**
- * deletes the current loaded tag from db
- *
- * @return boolean
- */
- public function delete(): bool {
- $ret = false;
- if(!empty($this->_id)) {
- $this->DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
- try {
- $queryStr = "DELETE
- FROM `".DB_PREFIX."_tagrelation`
- WHERE `tagid` = '".$this->DB->real_escape_string($this->_id)."'";
- $this->DB->query($queryStr);
- $queryStr = "DELETE
- FROM `".DB_PREFIX."_tag`
- WHERE `id` = '".$this->DB->real_escape_string($this->_id)."'";
- if(QUERY_DEBUG) Summoner::sysLog("QUERY ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
- $this->DB->query($queryStr);
- $this->DB->commit();
- } catch (Exception $e) {
- Summoner::sysLog('ERROR Failed to remove tag: '.$e->getMessage());
- $this->DB->rollback();
- }
- }
- return $ret;
- }
- /**
- * Rename current loaded tag name
- *
- * @param string $newValue
- * @return void
- */
- public function rename(string $newValue): void {
- if(!empty($newValue)) {
- $queryStr = "UPDATE `".DB_PREFIX."_tag`
- SET `name` = '".$this->DB->real_escape_string($newValue)."'
- WHERE `id` = '".$this->DB->real_escape_string($this->_id)."'";
- if(QUERY_DEBUG) Summoner::sysLog("QUERY ".__METHOD__." query: ".Summoner::cleanForLog($queryStr));
- try {
- $this->DB->query($queryStr);
- } catch (Exception $e) {
- Summoner::sysLog("ERROR ".__METHOD__." mysql catch: ".$e->getMessage());
- }
- $this->_data['name'] = $newValue;
- }
- }
- }
|