]> 91.132.146.200 Git - bibliotheca-php.git/commitdiff
manage collection fields will also remove the data from the collection since the...
authorBanana <mail@bananas-playground.net>
Tue, 5 Jan 2021 11:45:15 +0000 (12:45 +0100)
committerBanana <mail@bananas-playground.net>
Tue, 5 Jan 2021 11:45:15 +0000 (12:45 +0100)
TODO
webclient/lib/managecollectionfields.class.php
webclient/view/default/managecollectionfields/managecollectionfields.html

diff --git a/TODO b/TODO
index e6d856d83d4372c424003b0b770031c9cf5755d5..6788105002c9acb82c63d5867af05dda0a44ed5c 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,5 +1,3 @@
-* Collection fields: final remove from entry table
-       Add waring about it!
 * rights example setup and also check if it matches the menu and actions
        + admin
        + Management
index 072aa09080d0d74628ff1dc736b924351e420fbd..f4dadc71af1e8ede335dcd4b3f8488beb2a6bd20 100644 (file)
@@ -137,9 +137,9 @@ class ManageCollectionFields {
        }
 
        /**
-        * $fieldsSortString have to be validated already
+        * Deletes relations and data from the collection!
         *
-        * @todo remove non existing ones from table
+        * $fieldsSortString have to be validated already
         *
         * @param string $fieldsSortString
         * @return bool
@@ -158,38 +158,74 @@ class ManageCollectionFields {
 
                if(!empty($ids)) {
 
+                       $_newColumns = $this->_getSQLForCollectionColumns($ids);
+                       $_existingFields = $this->getExistingFields();
+
+                       // use the createsting info to determine if the field needs to be remove
+                       // from entry table or lookup table
+                       $_fieldsToCheckForDelete = $_existingFields;
+                       $queriesDeleteEntryTable = array();
+                       foreach($ids as $_id) {
+                               if(isset($_fieldsToCheckForDelete[$_id])) {
+                                       unset($_fieldsToCheckForDelete[$_id]);
+                               }
+                       }
+                       if(!empty($_fieldsToCheckForDelete)) {
+                               foreach($_fieldsToCheckForDelete as $k=>$v)  {
+                                       if(!empty($v['createstring'])) {
+                                               $queriesDeleteEntryTable[] = "ALTER TABLE `".DB_PREFIX."_collection_entry_".$this->_collectionId."`
+                                                                                                               DROP `".$v['identifier']."`";
+                                       }
+                               }
+                       }
 
-                       $queryStr1 = "DELETE FROM `".DB_PREFIX."_collection_fields_".$this->_collectionId."`
+                       $queryStrDeleteFields = "DELETE FROM `".DB_PREFIX."_collection_fields_".$this->_collectionId."`
                                                WHERE `fk_field_id` NOT IN (".implode(",",$ids).")";
+                       if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStrDeleteFields,true));
+
+                       $queryStrDeletee2l = "DELETE FROM `".DB_PREFIX."_collection_entry2lookup_".$this->_collectionId."`
+                                               WHERE `fk_field` NOT IN (".implode(",",$ids).")";
+                       if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStrDeletee2l,true));
+
+                       $queryStrInsertFields = "INSERT INTO `".DB_PREFIX."_collection_fields_".$this->_collectionId."` (`fk_field_id`,`sort`) VALUES ";
+                       foreach ($ids as $k => $v) {
+                               $queryStrInsertFields .= "($v,$k),";
+                       }
+                       $queryStrInsertFields = trim($queryStrInsertFields, ",");
+                       $queryStrInsertFields .= " ON DUPLICATE KEY UPDATE `sort` = VALUES(`sort`)";
+                       if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStrInsertFields,true));
+
+                       if(!empty($_newColumns)) {
+                               $queryStrAlterEntry = "ALTER TABLE `".DB_PREFIX."_collection_entry_".$this->_collectionId."`";
+                               foreach($_newColumns as $k=>$v) {
+                                       $queryStrAlterEntry .= " ADD ".$v['createstring'].",";
+                               }
+                               $queryStrAlterEntry = trim($queryStrAlterEntry, ",");
+                               if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStrAlterEntry,true));
+                       }
+
                        try {
                                $this->_DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
 
-                               $q1 = $this->_DB->query($queryStr1);
-                               if($q1 !== false) {
-                                       // https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html
-                                       $queryStr = "INSERT INTO `".DB_PREFIX."_collection_fields_".$this->_collectionId."` (`fk_field_id`,`sort`) VALUES ";
-                                       foreach ($ids as $k => $v) {
-                                               $queryStr .= "($v,$k),";
+                               if($this->_DB->query($queryStrDeleteFields) !== false && $this->_DB->query($queryStrDeletee2l) !== false) {
+
+                                       $_check = true;
+                                       if(!empty($queriesDeleteEntryTable)) {
+                                               foreach($queriesDeleteEntryTable as $q) {
+                                                       if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($q,true));
+                                                       if($this->_DB->query($q) == false) {
+                                                               $_check = false;
+                                                               break;
+                                                       }
+                                               }
                                        }
-                                       $queryStr = trim($queryStr, ",");
-                                       $queryStr .= " ON DUPLICATE KEY UPDATE `sort` = VALUES(`sort`)";
 
-                                       if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true));
-                                       $q2 = $this->_DB->query($queryStr);
-                                       if($q2 !== false) {
-                                               $_newColumns = $this->_getSQLForCollectionColumns($ids);
+                                       if($this->_DB->query($queryStrInsertFields) !== false && $_check === true) {
                                                $alterQuery = false;
                                                if(!empty($_newColumns)) {
-                                                       $alterString = "ALTER TABLE `".DB_PREFIX."_collection_entry_".$this->_collectionId."`";
-                                                       foreach($_newColumns as $k=>$v) {
-                                                               $alterString .= " ADD ".$v['createstring'].",";
-                                                       }
-                                                       $alterString = trim($alterString, ",");
-
-                                                       if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($alterQuery,true));
-                                                       $alterQuery = $this->_DB->query($alterString);
+                                                       $alterQuery = $this->_DB->query($queryStrAlterEntry);
                                                }
-                                               if(!empty($_newColumns) && $alterQuery == false) {
+                                               if(!empty($_newColumns) && $alterQuery === false) {
                                                        throw new Exception("Failed to insert alter the table.");
                                                }
                                        }
@@ -223,7 +259,8 @@ class ManageCollectionFields {
                        return $this->_cacheExistingSysFields;
                }
 
-               $queryStr = "SELECT `cf`.`fk_field_id` AS id, `sf`.`type`, `sf`.`displayname`, `sf`.`identifier`
+               $queryStr = "SELECT `cf`.`fk_field_id` AS id, `sf`.`type`, `sf`.`displayname`, `sf`.`identifier`,
+                                                       `sf`.`createstring`
                                                FROM `".DB_PREFIX."_collection_fields_".$this->_collectionId."` AS cf
                                                LEFT JOIN `".DB_PREFIX."_sys_fields` AS sf ON `cf`.`fk_field_id` = `sf`.`id`
                                                ORDER BY `cf`.`sort`";
index fce9fffb74eda9f3bb76839f0405ed463b20d406..8b4be134f487af736637cef9192e01f0d29fdee0 100644 (file)
@@ -5,7 +5,7 @@
                <h4 class="uk-h4">Add or remove a field</h4>
                <p>
                        Just use drag and drop below to add, remove or order your fields.<br />
-                       <span class="" uk-icon="icon: warning"></span> Removing a field will remove
+                       <span class="" uk-icon="icon: warning"></span> Removing a field will <span class="uk-text-danger">remove</span>
                        the stored data from the collection.<br />
                        <span class="" uk-icon="icon: warning"></span> Make sure at least the <b>title</b>, <b>cover image</b>
                        and <b>description</b> fields are available.