Browse Source

Add Merge Tags

stany.ferer 5 tháng trước cách đây
mục cha
commit
5dcaf2e44c

+ 47 - 16
core/class/tags.class.php

@@ -3,29 +3,47 @@
 class tags
 {
 
-    public static function getAll(float $_idTag = NULL) {
+    public static function getAll(?float $_idTag = NULL) {
         
         if($_idTag == NULL){
-            db::query("SELECT "
-                . "* "
-                . "FROM " . DB_T_TAGS . " "
-                . "ORDER BY label ASC");
+            db::query("SELECT"
+                . " *"
+                . " FROM " . DB_T_TAGS
+                . " ORDER BY label ASC");
         } else {
-            db::query("SELECT "
-                . "* "
-                . "FROM " . DB_T_TAGS 
-                . " WHERE id_type = :idTag"
+            db::query("SELECT"
+                . " *"
+                . " FROM " . DB_T_TAGS 
+                . " WHERE id_type = :idTag" 
                 . " ORDER BY label ASC");
             db::bind(':idTag', $_idTag);
         }
         return db::resultset();
     }
 
-    public static function cleanTags(){
-        db::query("");
+    public static function getTag(float $_idTag) {
+        db::query("SELECT"
+            . " *"
+            . " FROM " . DB_T_TAGS 
+            . " WHERE id = :idTag");
+        db::bind(':idTag', $_idTag);
+        return db::single();
+    }
 
+    public static function getAllAttachment(?float $_idTag = NULL) {
+        
+        db::query("SELECT"
+            . " COUNT(*) AS nbDocuments"
+            . " FROM " . DB_T_DOCUMENT_TAGS
+            . " WHERE id_tags = :idTag");
+        db::bind(':idTag', $_idTag);
+        return db::single()["nbDocuments"];
+    }
+
+    public static function cleanTags(){
+        db::query("DELETE FROM tags WHERE id_type = 2 AND NOT EXISTS (SELECT 1 FROM documents_tags WHERE documents_tags.id_tags = tags.id)");
         try {
-            
+            db::execute();
             return TRUE;
         } catch (Exception $ex) {
             return FALSE;
@@ -58,6 +76,19 @@ class tags
         }
     }
 
+    public static function merge(float $_idMaster, float $_idSlave) {
+        db::query("UPDATE " . DB_T_DOCUMENT_TAGS . " SET id_tags = :idMaster WHERE id_tags = :idSlave; ");
+        db::bind(':idMaster', $_idMaster);
+        db::bind(':idSlave', $_idSlave);
+
+        try {
+            db::execute();
+            return TRUE;
+        } catch (Exception $ex) {
+            return FALSE;
+        }
+    }
+
     static public function findAndCreate(string $_tags, float $_idTag = 2){
         $find = [];
         $tmp = explode(",", $_tags);
@@ -74,7 +105,7 @@ class tags
         }
     }
 
-    public static function getJquery(float $_idTag = NULL) {
+    public static function getJquery(?float $_idTag = NULL) {
         $tmp = "[";
         foreach (self::getAll($_idTag) as $tags) {
             $tmp .= "'".addslashes($tags["label"])."', ";
@@ -83,7 +114,7 @@ class tags
         return $tmp;
     }
 
-    public static function textToId(string $_tags = NULL, float $_idTag = NULL) {
+    public static function textToId(?string $_tags = NULL, ?float $_idTag = NULL) {
         $return = NULL;
         if($_tags != NULL) {
 
@@ -107,7 +138,7 @@ class tags
         return $return;
     }
 
-    public static function findUsersTags(string $_tags = NULL){
+    public static function findUsersTags(?string $_tags = NULL){
 
         $tmp = explode(",", $_tags);
 
@@ -136,7 +167,7 @@ class tags
         return  db::resultset();
     }
 
-    public static function compareUserDocument(string $_tag_user = NULL, string $_tag_document = NULL){
+    public static function compareUserDocument(?string $_tag_user = NULL, ?string $_tag_document = NULL){
         if($_tag_user == NULL OR $_tag_document == NULL){
             return FALSE;
         }

+ 31 - 0
core/submit/cms.tag-merge.php

@@ -0,0 +1,31 @@
+<?php
+
+if (core::ifPost("from")) {
+
+
+    tags::merge(core::getPost("idMaster"), core::getPost("idSalve"));
+    tags::cleanTags();
+
+    $nameMaster = tags::getTag(core::getPost("idMaster"));
+    $nameSalve = tags::getTag(core::getPost("idSalve"));
+
+    json::create("documents");
+    json::create("documents-limited");
+
+    historique::recRef("/tags.html");
+    historique::add(array(
+        "idType" => historique::getIdRef("ACTION"),
+        "idUser" => session::getId(),
+        "idPage" => historique::getIdRef("/tags.html"),
+        "log" => "Merge du tag " . $nameSalve["label"] . " remplacé par " . $nameMaster["label"]
+    ));
+
+    alert::recSuccess("Merge du tag " . $nameSalve["label"] . " remplacé par " . $nameMaster["label"]);
+
+    header("Location: /tags.html");
+    exit();
+
+} else {
+    header('HTTP/1.0 401 Unauthorized');
+    exit();
+}

+ 24 - 0
core/views/pages/cms.documents.php

@@ -58,6 +58,30 @@ echo core::filAriane(array(
 </div>
 
 <script>
+
+<?php 
+    if (core::ifGet("tag")) { ?>
+
+    $(function() {
+        tagValue = "<?php echo core::getGet("tag"); ?>";
+
+        if (tagValue) {
+            function applyTagFilter() {
+                var $input = $('#table input.bootstrap-table-filter-control-tags');
+                console.log('input:', $input.length, 'valeur:', tagValue);
+                if ($input.length) {
+                    $input.val(tagValue).trigger('input');
+                }
+            }
+            $('#table').on('post-body.bs.table post-header.bs.table', applyTagFilter);
+            setTimeout(applyTagFilter, 800); 
+        }
+    });
+
+<?php 
+    }
+?>
+
     function selectFormatter(value, row) {
         return '<a href="/document-' + row.id + '.html"><button type="submit" class="btn btn-outline-primary btn-sm">Ouvrir</button></a>';
     }

+ 72 - 9
core/views/pages/cms.tags.php

@@ -20,32 +20,65 @@ $allTags = tags::getAll(2);
     <thead>
         <tr>
             <th scope="col">#</th>
+            <th scope="col">Nb. Documents</th>
             <th scope="col">Tags</th>
             <th scope="col"></th>
         </tr>
     </thead>
     <tbody>
-<?php 
-    foreach ($allTags as $value) {
-        echo "  <tr>
+        <?php
+        foreach ($allTags as $value) {
+            $nbDocuments = tags::getAllAttachment($value["id"]);
+            $texteDocument = "<a href=\"/documents.html?tag=" . $value["label"] . "\" class=\"btn btn-outline-primary btn-sm\">" . (($nbDocuments > 1) ? $nbDocuments . " documents associés" : "1 document associé") . "</a>";
+            
+
+            echo "  <tr>
                     <th scope=\"row\">" . $value["id"] . "</th>
-                    <td><input type=\"text\" class=\"form-control form-control-sm\" id=\"tags-".$value["id"]."\" value=\"" . $value["label"] . "\"></td>
-                    <td><button type=\"button\" class=\"btn btn-primary btn-sm\" onclick=\"majTags(".$value["id"].");\">Modifier</button></td>
+                    <td>" . $texteDocument . "</td>
+                    <td><input type=\"text\" class=\"form-control form-control-sm\" id=\"tags-" . $value["id"] . "\" value=\"" . $value["label"] . "\"></td>
+                    <td>";
+            echo "<button type=\"button\" class=\"btn btn-outline-primary btn-sm\" onclick=\"majTags(" . $value["id"] . ");\">Renommer</button> ";
+            echo "<button type=\"button\" class=\"btn btn-outline-primary btn-sm\" data-toggle=\"modal\" data-target=\"#mergeModal\" data-tag=\"" . $value["label"] . "\" data-id=\"" . $value["id"] . "\" onclick=\"buttonMergeTags(" . $value["id"] . ");\">Fusionner</button> ";
+            echo "</td>
                 </tr>";
-    }
-?>
+        }
+        ?>
     </tbody>
 </table>
 
+<div class="modal fade" id="mergeModal" tabindex="-1" role="dialog" aria-labelledby="mergeModalLabel" aria-hidden="true">
+    <form id="mergeFormTag" method="post" action="/submit.php">
+        <input type="hidden" name="from" value="tag-merge">
+        <div class="modal-dialog modal-lg" role="document">
+            <div class="modal-content">
+                <input type="hidden" name="idMaster" value="">
+                <div class="modal-header">
+                    <h5 class="modal-title" id="mergeModalLabel"></h5>
+                    <button type="button" class="close btn" style="font-size: 1.5rem; padding:3px; line-height: 1;" data-dismiss="modal" aria-label="Close">
+                        <i class="bi bi-x"></i>
+                    </button>
+                </div>
+                <div class="modal-body">
+                    <div class="input-group">
+                        <select id="menuTags" class="form-select" name="idSalve" required></select>
+                        <button class="btn btn-outline-primary" type="submit" onclick="return confirm('Le tag sélectionné sera supprimé, vous êtes certain de vouloir continuer ?')">Valider</button>
+                    </div>
+                    <hr>
+                    <p class="alert alert-warning" id="mergeModalDescription"></p>
+                </div>
+            </div>
+    </form>
+</div>
+
 <script>
-    function majTags(id){
+    function majTags(id) {
         $.ajax({
             url: '/submit.php',
             type: 'POST',
             data: {
                 from: "tag-change",
                 id: id,
-                value: $('#tags-'+id).val()
+                value: $('#tags-' + id).val()
             },
             success: function(response) {
                 $("#printToastSuccessTxt").html("Le tag vient d'être mis à jour");
@@ -57,4 +90,34 @@ $allTags = tags::getAll(2);
             }
         });
     }
+
+    $('#mergeModal').on('show.bs.modal', function(event) {
+        const button = $(event.relatedTarget);
+        const tag = button.data('tag');
+        const id = button.data('id');
+        const modal = $(this);
+        modal.find('input[name="idMaster"]').val(id);
+        modal.find('.modal-title').text(`Vous souhaitez fusionner "${tag}" avec ...`);
+        modal.find('#mergeModalDescription').text(`Une fois fusionné, tous les documents seront rattachés à "${tag} et le tag sélectionné sera supprimé.`);
+    });
+
+    function buttonMergeTags(id) {
+        <?php
+        echo "const donnees = [";
+        foreach ($allTags as $value) {
+            echo "{id: " . $value["id"] . ", label: '" . addslashes($value["label"]) . "'},";
+        }
+        echo "];";
+        ?>
+
+        const options = donnees.filter(item => item.id !== id)
+        
+        const $menu = $('#menuTags');
+        $menu.empty();
+        $menu.append('<option value="">Sélectionner le tag avec lequel fusionner</option>');
+        options.forEach(item => {
+            $menu.append(`<option value="${item.id}">${item.label}</option>`);
+        });
+
+    }
 </script>