+ Fixed bug #8 It is possible to add empty tags and categories.
Special chars check done on server side. JS has problems with unicode.
+ + Fixed bug #7 Edit categories/tags. Rename and move
version 2.5.1 - Caves of Circe (2020-03-22)
++ multiple user accounts and stuff
+ adapt new php 7
++ http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op
++ combine cat and tag class into one.
/**
* 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
* @return bool|int
*/
- public function initbystring($string) {
+ public function initbystring($string, $doNotCreate=false) {
+ $ret = 0;
$this->_id = false;
- if(!empty($string)) {
- $queryStr = "SELECT `id`,`name` FROM `".DB_PREFIX."_category`
+ if(!empty($string)) {
+ $queryStr = "SELECT `id`,`name` FROM `".DB_PREFIX."_category`
WHERE `name` = '".$this->DB->real_escape_string($string)."'";
- $query = $this->DB->query($queryStr);
- if(!empty($query) && $query->num_rows > 0) {
- $result = $query->fetch_assoc();
- $this->_id = $result['id'];
- $this->_data = $result;
- }
- else {
- $queryStr = "INSERT INTO `".DB_PREFIX."_category`
- 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;
- }
- }
- }
- return $this->_id;
+ $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 . "_category`
+ 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 an array of any linkid related to the current loaded category
+ * @return array
+ */
+ public function getReleations() {
+ $ret = array();
+
+ $queryStr = "SELECT linkid
+ FROM `".DB_PREFIX."_categoryrelation`
+ WHERE `categoryid` = '".$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 category from db
* @return boolean
return $ret;
}
+
+ /**
+ * Rename current loaded cat name
+ * @param $newValue
+ * @return void
+ */
+ public function rename($newValue) {
+ if(!empty($newValue)) {
+ $queryStr = "UPDATE `".DB_PREFIX."_category`
+ 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;
+ }
+ }
}
$this->_data['name'] = $newValue;
}
}
-
}
if(isset($_POST['category']) && !empty($_POST['category']) && isset($_POST['updateCategories'])) {
$categoryData = $_POST['category'];
+ $newCategory = $_POST['newCategory'];
+
+ # 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';
+
+ $catToUpdate = array();
+ foreach ($categoryData as $cid=>$cNewValue) {
+ $_c = Summoner::validate($cNewValue,'nospace');
+ if($_c === true) {
+ $catToUpdate[$cid] = $cNewValue;
+ }
+ }
$deleteCategoryData = array();
if(isset($_POST['deleteCategory'])) {
$deleteCategoryData = $_POST['deleteCategory'];
}
- $newCategory = $_POST['newCategory'];
-
- # first deletion, then update and then add
- # adding a new one which matches an existing one will update it.
-
- $submitFeedback['message'] = array();
- $submitFeedback['status'] = 'success';
+ $catDoNotDeleteFromUpdate = array();
+ if(!empty($catToUpdate)) {
+ $submitFeedback['message'][] = 'Categories renamed successfully.';
+ foreach ($catToUpdate as $k=>$v) {
+ $catObjAlternative = new Category($DB);
+ $do = $catObjAlternative->initbystring($v,true);
+ if($do === 1) { # existing
+ // the target cat should not be removed!
+ $catDoNotDeleteFromUpdate[$catObjAlternative->getData('id')] = $catObjAlternative->getData('id');
+ $catObjOld = new Category($DB);
+ if(!empty($catObjOld->initbyid($k))) {
+ $linksToUpdate = $catObjOld->getReleations();
+ if(!empty($linksToUpdate)) {
+ foreach($linksToUpdate as $linkId) {
+ $catObjAlternative->setRelation($linkId);
+ }
+ $catObjOld->delete();
+ }
+ }
+ else {
+ $submitFeedback['message'][] = 'Categories could not be renamed.';
+ $submitFeedback['status'] = 'error';
+ }
+ }
+ elseif ($do === 3) { # not existing one. Can be renamed
+ $catObjRename = new Category($DB);
+ if(!empty($catObjRename->initbyid($k))) {
+ $catObjRename->rename($v);
+ }
+ }
+ else {
+ $submitFeedback['message'][] = 'Categories could not be renamed.';
+ $submitFeedback['status'] = 'error';
+ }
+ }
+ }
if(!empty($deleteCategoryData)) {
$submitFeedback['message'][] = 'Categories deleted successfully.';
foreach($deleteCategoryData as $k=>$v) {
- if($v == "delete") {
+ if($v == "delete" && !isset($catDoNotDeleteFromUpdate[$k])) {
$catObj = new Category($DB);
$load = $catObj->initbyid($k);
if($load !== false) {