$this->DB = $databaseConnectionObject;
}
- /**
- * by given string load the info from the DB and even create if not existing
- * @param string $string
- */
- public function initbystring($string) {
+ /**
+ * by given string load the info from the DB and even create if not existing
+ * @param string $string
+ * @return int 0=fail, 1=existing, 2=new, 3=newNotCreated
+ */
+ public function initbystring($string, $doNotCreate=false) {
+ $ret = 0;
$this->_id = false;
if(!empty($string)) {
$queryStr = "SELECT `id`,`name` FROM `".DB_PREFIX."_tag`
$result = $query->fetch_assoc();
$this->_id = $result['id'];
$this->_data = $result;
+ $ret = 1;
}
else {
- $queryStr = "INSERT INTO `".DB_PREFIX."_tag`
- SET `name` = '".$this->DB->real_escape_string($string)."'";
- $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;
- }
+ if(!$doNotCreate) {
+ $queryStr = "INSERT INTO `" . DB_PREFIX . "_tag`
+ SET `name` = '" . $this->DB->real_escape_string($string) . "'";
+ $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;
+ }
}
}
+ return $ret;
}
/**
return $ret;
}
- /**
- * set the relation to the given link to the loaded tag
- * @param int $linkid
- * @return boolean
- */
+ /**
+ * set the relation to the given link to the loaded tag
+ * @param int $linkid
+ * @return void
+ */
public function setRelation($linkid) {
if(!empty($linkid) && !empty($this->_id)) {
$queryStr = "INSERT IGNORE INTO `".DB_PREFIX."_tagrelation`
}
}
+ /**
+ * Return an array of any linkid related to the current loaded tag
+ * @return array
+ */
+ public function getReleations() {
+ $ret = array();
+
+ $queryStr = "SELECT linkid
+ FROM `".DB_PREFIX."_tagrelation`
+ WHERE tagid = '".$this->DB->real_escape_string($this->_id)."'";
+ $query = $this->DB->query($queryStr);
+ if(!empty($query) && $query->num_rows > 0) {
+ while($result = $query->fetch_assoc()) {
+ $ret[] = $result['linkid'];
+ }
+ }
+
+ return $ret;
+ }
+
/**
* deletes the current loaded tag from db
* @return boolean
return $ret;
}
+
+ /**
+ * Rename current loaded tag name
+ * @param $newValue
+ * @return void
+ */
+ public function rename($newValue) {
+ 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)."'";
+ $this->DB->query($queryStr);
+ $this->_data['name'] = $newValue;
+ }
+ }
+
}
if(isset($_POST['tag']) && !empty($_POST['tag']) && isset($_POST['updateTags'])) {
$tagData = $_POST['tag'];
+ $newTag = $_POST['newTag'];
- $deleteTagData = array();
- if(isset($_POST['deleteTag'])) {
- $deleteTagData = $_POST['deleteTag'];
- }
+ # first update then deletion and then add
+ # adding a new one which matches an existing one will update it.
+
+ $submitFeedback['message'] = array();
+ $submitFeedback['status'] = 'success';
- $newTag = $_POST['newTag'];
+ $tagToUpdate = array();
+ foreach ($tagData as $tid=>$tNewValue) {
+ $_c = Summoner::validate($tNewValue,'nospace');
+ if($_c === true) {
+ $tagToUpdate[$tid] = $tNewValue;
+ }
+ }
- # first deletion, then update and then add
- # adding a new one which matches an existing one will update it.
+ $deleteTagData = array();
+ if(isset($_POST['deleteTag'])) {
+ $deleteTagData = $_POST['deleteTag'];
+ }
- $submitFeedback['message'] = array();
- $submitFeedback['status'] = 'success';
+ $tagDoNotDeleteFromUpdate = array();
+ if(!empty($tagToUpdate)) {
+ $submitFeedback['message'][] = 'Tags renamed successfully.';
+ foreach ($tagToUpdate as $k=>$v) {
+ $tagObjAlternative = new Tag($DB);
+ $do = $tagObjAlternative->initbystring($v,true);
+ if($do === 1) { # existing
+ // the target tag should not be removed!
+ $tagDoNotDeleteFromUpdate[$tagObjAlternative->getData('id')] = $tagObjAlternative->getData('id');
+ $tagObjOld = new Tag($DB);
+ if(!empty($tagObjOld->initbyid($k))) {
+ $linksToUpdate = $tagObjOld->getReleations();
+ if(!empty($linksToUpdate)) {
+ foreach($linksToUpdate as $linkId) {
+ $tagObjAlternative->setRelation($linkId);
+ }
+ $tagObjOld->delete();
+ }
+ }
+ else {
+ $submitFeedback['message'][] = 'Tags could not be renamed.';
+ $submitFeedback['status'] = 'error';
+ }
+ }
+ elseif ($do === 3) { # not existing one. Can be renamed
+ $tagObjRename = new Tag($DB);
+ if(!empty($tagObjRename->initbyid($k))) {
+ $tagObjRename->rename($v);
+ }
+ }
+ else {
+ $submitFeedback['message'][] = 'Tags could not be renamed.';
+ $submitFeedback['status'] = 'error';
+ }
+ }
+ }
if(!empty($deleteTagData)) {
$submitFeedback['message'][] = 'Tags deleted successfully.';
foreach($deleteTagData as $k=>$v) {
- if($v == "delete") {
+ if($v == "delete" && !isset($tagDoNotDeleteFromUpdate[$k])) {
$tagObj = new Tag($DB);
$load = $tagObj->initbyid($k);
if($load !== false) {