From 278e77d715e76d9608256d79d9c5e5ab1b756d35 Mon Sep 17 00:00:00 2001
From: Matteo <matteo.bacca@gmail.com>
Date: Mon, 13 Mar 2023 16:22:50 +0100
Subject: [PATCH] Fix per inserimento e modifica nuove checklist

---
 modules/checklists/ajax.php                  | 29 +++++++--
 modules/checklists/components/edit-check.php | 14 +++-
 modules/checklists/edit.php                  | 62 ++++++++++++++++--
 modules/checklists/init.php                  |  4 +-
 modules/checklists/js/checklist.js           | 30 ---------
 modules/checklists/modutil.php               | 68 +++++++++++++++++++-
 6 files changed, 159 insertions(+), 48 deletions(-)

diff --git a/modules/checklists/ajax.php b/modules/checklists/ajax.php
index 92abafa51..b3a3c71e8 100644
--- a/modules/checklists/ajax.php
+++ b/modules/checklists/ajax.php
@@ -19,24 +19,37 @@
 
 include_once __DIR__.'/../../core.php';
 use Modules\Checklists\Check;
+use Modules\Checklists\ChecklistItem;
 
 switch(post('op')){
 
     case "delete_check":
         $id = post('id');
+        $main_check = post('main_check');
+
+        if($main_check){
+            $record = ChecklistItem::find($id);
+        }else{
+            $record = Check::find($id);
+        }
 
-        $record = Check::find($id);
         $record->delete();
 
         break;
 
     case "update_position":
 
+        $main_check = post('main_check');
         $order = explode(',', post('order', true));
 
-        foreach($order as $i => $id){
-            $dbo->query("UPDATE zz_checks SET `order`=".prepare($i)." WHERE id=".prepare($id));
-            echo "UPDATE zz_checks SET `order`=".prepare($i)." WHERE id=".prepare($id);
+        if($main_check){
+            foreach($order as $i => $id){
+                $dbo->query("UPDATE zz_checklist_items SET `order`=".prepare($i)." WHERE id=".prepare($id));
+            }
+        }else{
+            foreach($order as $i => $id){
+                $dbo->query("UPDATE zz_checks SET `order`=".prepare($i)." WHERE id=".prepare($id));
+            }
         }
 
         break;
@@ -65,8 +78,14 @@ switch(post('op')){
 
     case "edit_check":
         $id_record = post('id_record');
+        $main_check = post('main_check');
+
+        if($main_check){
+            $record = ChecklistItem::find($id_record);
+        }else{
+            $record = Check::find($id_record);
+        }
 
-        $record = Check::find($id_record);
         $record->content = post('content');
         $record->save();
 
diff --git a/modules/checklists/components/edit-check.php b/modules/checklists/components/edit-check.php
index 804df8dd5..238da8019 100644
--- a/modules/checklists/components/edit-check.php
+++ b/modules/checklists/components/edit-check.php
@@ -18,16 +18,23 @@
  */
 
 include_once __DIR__.'/../../../core.php';
+use Modules\Checklists\ChecklistItem;
 use Modules\Checklists\Check;
 
 $id_record = get("id_record");
-$record = Check::find($id_record);
+$main_check = get("main_check");
+
+if($main_check){
+    $record = ChecklistItem::find($id_record);
+}else{
+    $record = Check::find($id_record);
+}
 
 ?>
 
 <div class="row">
     <div class="col-md-6">
-        {[ "type": "text", "label": "<?php echo tr('Descrizione'); ?>", "name": "content", "required": 1, "value": "<?=$record->content?>" ]}
+        {[ "type": "text", "label": "<?php echo tr('Descrizione'); ?>", "name": "content_edit", "required": 1, "value": "<?=$record->content?>" ]}
     </div>
 </div>
 
@@ -45,7 +52,8 @@ $record = Check::find($id_record);
         $.post('<?php echo $rootdir; ?>/modules/checklists/ajax.php', {
             op: "edit_check",
             id_record: "<?=$id_record?>",
-            content: $('#content').val()
+            content: $('#content_edit').val(),
+            main_check: "<?=$main_check?>",
         }, function(){
             location.reload();
         });
diff --git a/modules/checklists/edit.php b/modules/checklists/edit.php
index 1e6fbadc9..16e17fd37 100755
--- a/modules/checklists/edit.php
+++ b/modules/checklists/edit.php
@@ -68,7 +68,7 @@ $(document).ready(function() {
 });
 </script>';
 
-$checks = $record->checks;
+$checks = $record->mainChecks();
 
 $list = [];
 foreach ($checks as $check) {
@@ -108,12 +108,15 @@ echo '
 
         <ul class="todo-list checklist">';
 
-    $checks = $record->mainChecks();
-    foreach ($checks as $check) {
-        echo renderChecklist($check);
-    }
+echo "      <table class='table'>
+                <tbody class='sort' data-sonof='0'>";
+foreach ($checks as $check) {
+    echo renderChecklistInserimento($check);
+}
+echo "          </tbody>
+            </table>";
 
-    echo '
+echo '
         </ul>
     </div>
 </div>';
@@ -167,4 +170,49 @@ $(document).ready(function() {
 echo '
 <a class="btn btn-danger ask" data-backto="record-list">
     <i class="fa fa-trash"></i> '.tr('Elimina').'
-</a>';
+</a>
+
+<script>
+
+sortable(".sort", {
+    axis: "y",
+    handle: ".handle",
+    cursor: "move",
+    dropOnEmpty: true,
+    scroll: true,
+});
+
+sortable_table = sortable(".sort").length;
+
+for(i=0; i<sortable_table; i++){
+    sortable(".sort")[i].addEventListener("sortupdate", function(e) {
+
+        var sonof = $(this).data("sonof");
+
+        let order = $(this).find(".sonof_"+sonof+"[data-id]").toArray().map(a => $(a).data("id"))
+    
+        $.post("'.$checklist_module->fileurl('ajax.php').'", {
+            op: "update_position",
+            order: order.join(","),
+            main_check: 1,
+        });
+    });
+}
+
+function delete_check(id){
+    if(confirm("Eliminare questa checklist?")){
+        $.post("'.$checklist_module->fileurl('ajax.php').'", {
+            op: "delete_check",
+            id: id,
+            main_check: 1,
+        }, function(){
+            location.reload();
+        });
+    }
+}
+
+function edit_check(id){
+    launch_modal("Modifica checklist", "'.$checklist_module->fileurl('components/edit-check.php').'?id_record="+id+"&main_check=1", 1);
+}
+
+</script>';
\ No newline at end of file
diff --git a/modules/checklists/init.php b/modules/checklists/init.php
index 4a7c61262..6fc04b7c0 100755
--- a/modules/checklists/init.php
+++ b/modules/checklists/init.php
@@ -21,6 +21,8 @@ include_once __DIR__.'/../../core.php';
 
 use Modules\Checklists\Checklist;
 
+$checklist_module = Modules::get('Checklists');
+
 if (isset($id_record)) {
     $record = Checklist::find($id_record);
-}
+}
\ No newline at end of file
diff --git a/modules/checklists/js/checklist.js b/modules/checklists/js/checklist.js
index 55a51a68d..49c331105 100755
--- a/modules/checklists/js/checklist.js
+++ b/modules/checklists/js/checklist.js
@@ -56,36 +56,6 @@ class Checklist {
         });
     }
 
-    deleteCheck(id) {
-        this.request({
-            op: "rimuovi-check",
-            check_id: id,
-        });
-
-        return true;
-    }
-
-    toggleCheck(id) {
-        this.request({
-            op: "toggle-check",
-            check_id: id,
-        });
-
-        return true;
-    }
-
-    findCheck(id) {
-        var li = $("#check_" + id);
-
-        return {
-            item: li,
-            input: li.find("input"),
-            info: li.find(".badge"),
-            text: li.find(".text"),
-            children: li.find("ul"),
-        };
-    }
-
     showLoader() {
         $("#loading_" + this.id).removeClass("hide");
     }
diff --git a/modules/checklists/modutil.php b/modules/checklists/modutil.php
index cc1e1f550..71035e453 100644
--- a/modules/checklists/modutil.php
+++ b/modules/checklists/modutil.php
@@ -55,7 +55,6 @@ function renderChecklist($check, $level = 1, $parent = 0) {
                         ]) : '').'
                         </span>';
     }
-
     $result .= '
                     </td>'; 
 
@@ -94,7 +93,72 @@ function renderChecklist($check, $level = 1, $parent = 0) {
         </td>
 
         <td style="width:40px;text-align:center;border-top:0px;">
-            <button class="btn btn-xs btn-default handle '.(!$enabled ? 'disabled' : '').'" title="Modifica ordine delle righe" draggable="true">
+            <button class="btn btn-xs btn-default handle" title="Modifica ordine delle righe" draggable="true">
+                <i class="fa fa-sort"></i>
+            </button>
+        </td>
+
+    </tr>';
+
+    return $result;
+}
+
+function renderChecklistInserimento($check, $level = 1, $parent = 0)
+{
+
+    global $record;
+    
+    $margin = ($level*20);
+
+    $result = '
+    <tr id="check_'.$check->id.'" data-id="'.$check->id.'" class="sortablerow sonof_'.$parent.'" >
+        <td style="padding-top:0px;padding-bottom:0px;border-top:0px;">
+            <table class="table" style="margin-bottom:0px;">
+                <tr>';
+
+    $result .= '
+                    <td style="width:40px;border-top:0px;border-left:3px solid #eaeaea;">';
+    $result .= '
+                        <span class="text">'.$check->content.'</span>';
+    $result .= '
+                    </td>'; 
+
+    $result .= '
+                    <td style="width:40px;text-align:right;border-top:0px;">
+                        <div class="input-group-btn">
+                            <button class="btn btn-warning btn-xs" onclick="edit_check(\''.$check->id.'\')"><i class="fa fa-edit"></i></button>
+                            <button class="btn btn-danger btn-xs" onclick="delete_check(\''.$check->id.'\')"><i class="fa fa-trash"></i></button>
+                        </div>
+                    </td>';
+
+
+
+    $result .= '
+                </tr>';
+
+    if(sizeof($check->children)>0){
+        $result .= '
+                <tr>
+                    <td colspan="4" style="padding-left:'.$margin.'px;padding-right:0px;padding-top:0px;padding-bottom:0px;border-top:0px;">
+                        <table class="table" style="margin-bottom:0px;">
+                            <tbody class="sort" data-sonof="'.$check->id.'">';
+            $children = $record->checks()->where('id_parent', $check->id)->orderBy('order')->get();
+            foreach ($children as $child) {
+                $result .= renderChecklistInserimento($child, $level + 1, $check->id);
+            }
+        $result .= '
+                            </tbody>
+                        </table>
+                    </td>
+                </tr>';
+    }
+
+    $result .= '
+            </table>
+        </td>
+
+        <td style="width:40px;text-align:center;border-top:0px;">
+            <button class="btn btn-xs btn-default handle" title="Modifica ordine delle righe" draggable="true">
                 <i class="fa fa-sort"></i>
             </button>
         </td>