Browse Source

Refactored to use a more modular approach

keplyx 1 year ago
parent
commit
fce5b89657

+ 2
- 49
admin/categories.php View File

@@ -1,53 +1,6 @@
1 1
 <?php
2 2
 $relativePath = "../";
3
-require_once $relativePath . 'classes/dao.php';
4 3
 
5
-$dao = new Dao();
6
-
7
-$categories = $dao->get_categories();
8
-
9
-ob_start();
10
-?>
11
-<div class="admin-container">
12
-
13
-    <a href="index.php">
14
-        <button class="btn btn-primary btn-large">Retour</button>
15
-    </a>
16
-
17
-    <h1 class="text-center">Gestion des Catégories</h1>
18
-
19
-    <div style="display: flex">
20
-        <button class="btn btn-success" style="margin: auto" onclick="listManager.showEditPopup(-1)">
21
-            <i class="mdi mdi-plus"></i>
22
-        </button>
23
-    </div>
24
-
25
-    <ul id="dataList">
26
-
27
-    </ul>
28
-
29
-    <script type="text/javascript">
30
-        let json_dump = <?php echo json_encode($categories); ?>;
31
-    </script>
32
-
33
-
34
-</div>
35
-
36
-<link type="text/css" rel="stylesheet" href="https://cdn.materialdesignicons.com/4.4.95/css/materialdesignicons.min.css"
37
-      media="screen,projection"/>
38
-<link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/admin.css" media="screen,projection"/>
39
-<link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/categories.css" media="screen,projection"/>
40
-
41
-
42
-<?php
43
-$pageContent = ob_get_clean();
44 4
 $pageTitle = "Gestion des catégories";
45
-$pageScripts =
46
-    "
47
-    <script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/jquery-confirm-defaults.js\"></script>
48
-    <script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/saveManager.js\"></script>
49
-    <script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/listManager.js\"></script>
50
-    <script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/categories.js\"></script>
51
-    ";
52
-include($relativePath . "includes/template.php");
53
-?>
5
+$script = "<script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/categories.js\"></script>";
6
+include("template.php");

+ 3
- 32
admin/save_manager.php View File

@@ -1,39 +1,10 @@
1 1
 <?php
2 2
 $relativePath = "../";
3
-require_once $relativePath . 'classes/dao.php';
3
+require_once $relativePath.'classes/postHandler.php';
4 4
 
5 5
 $rest_json = file_get_contents("php://input");
6 6
 $_POST = json_decode($rest_json, true);
7 7
 
8
+$handler = new PostHandler($_POST, $_FILES);
8 9
 
9
-//var_dump($_POST);
10
-$result = -1;
11
-$status = 0;
12
-$message = "Success";
13
-
14
-$dao = new Dao();
15
-
16
-if ($_POST["function"] == "create_category")
17
-    $result = $dao->create_category($_POST["data"]);
18
-else if ($_POST["function"] == "update_category") {
19
-    $result = $dao->update_category($_POST["data"]);
20
-} else if ($_POST["function"] == "remove_category")
21
-    $result = $dao->remove_category($_POST["data"]);
22
-else {
23
-    $status = 1;
24
-    $message = "Error: Unknown function";
25
-}
26
-
27
-if ($result < 0) {
28
-    $status = 2;
29
-    $message = "Error: SQL error";
30
-}
31
-
32
-$array = array(
33
-    "status" => $status,
34
-    "message" => $message,
35
-    "data" => $result,
36
-);
37
-
38
-echo json_encode($array);
39
-
10
+echo json_encode($handler->do_action());

+ 2
- 2
admin/stock.php View File

@@ -82,7 +82,7 @@ ob_start();
82 82
     <h2 class="text-center">Fichier de stock V2 non trouvé</h2>
83 83
         <div style="display: flex; margin-top: 50px; margin-bottom: 50px">
84 84
             <button class="btn btn-success btn-lg" style="margin: auto"
85
-                    onclick="SaveManager.convertToV2()">
85
+                    onclick="AjaxManager.convertToV2()">
86 86
                 Convertir le stock existant en V2
87 87
             </button>
88 88
         </div>
@@ -118,6 +118,6 @@ ob_start();
118 118
 <?php
119 119
 $pageContent = ob_get_clean();
120 120
 $pageTitle = "Gestion du stock";
121
-$pageScripts = "<script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/saveManager.js\"></script><script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/stock.js\"></script>";
121
+$pageScripts = "<script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/ajaxManager.js\"></script><script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/stock.js\"></script>";
122 122
 include($relativePath . "includes/template.php");
123 123
 ?>

+ 44
- 0
admin/template.php View File

@@ -0,0 +1,44 @@
1
+<?php
2
+$relativePath = "../";
3
+
4
+ob_start();
5
+?>
6
+<div class="admin-container">
7
+
8
+    <a href="index.php">
9
+        <button class="btn btn-primary btn-large">Retour</button>
10
+    </a>
11
+
12
+    <h1 class="text-center"><?= $pageTitle ?></h1>
13
+
14
+    <div style="display: flex">
15
+        <button class="btn btn-success" style="margin: auto" onclick="listManager.showEditPopup(-1)">
16
+            <i class="mdi mdi-plus"></i>
17
+        </button>
18
+    </div>
19
+
20
+    <div id="listContainer">
21
+        <ul id="dataList">
22
+
23
+        </ul>
24
+    </div>
25
+
26
+</div>
27
+
28
+<link type="text/css" rel="stylesheet" href="https://cdn.materialdesignicons.com/4.4.95/css/materialdesignicons.min.css"
29
+      media="screen,projection"/>
30
+<link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/admin.css" media="screen,projection"/>
31
+<link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/categories.css" media="screen,projection"/>
32
+
33
+
34
+<?php
35
+$pageContent = ob_get_clean();
36
+$pageScripts =
37
+    "
38
+    <script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/jquery-confirm-defaults.js\"></script>
39
+    <script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/ajaxManager.js\"></script>
40
+    <script type=\"text/javascript\" src=\"" . $relativePath . "assets/js/listManager.js\"></script>
41
+    ";
42
+$pageScripts .= $script;
43
+include($relativePath . "includes/template.php");
44
+?>

+ 8
- 0
assets/css/categories.css View File

@@ -1,5 +1,7 @@
1 1
 #dataList {
2 2
     list-style: none;
3
+    margin: auto;
4
+    padding: 0;
3 5
 }
4 6
 
5 7
 #dataList li {
@@ -9,6 +11,7 @@
9 11
     display: flex;
10 12
     cursor: pointer;
11 13
     transition: 0.2s;
14
+    padding: 0 20px 0 20px;
12 15
 }
13 16
 
14 17
 #dataList li:hover {
@@ -20,6 +23,11 @@
20 23
     display: flex;
21 24
 }
22 25
 
26
+#listContainer {
27
+    display: flex;
28
+    margin: 20px 0 20px 0;
29
+}
30
+
23 31
 .mdi {
24 32
     font-size: 2rem;
25 33
 }

+ 57
- 0
assets/js/ajaxManager.js View File

@@ -0,0 +1,57 @@
1
+
2
+class AjaxManager {
3
+
4
+    static async getCategories() {
5
+        let data = {
6
+            type: 'category',
7
+            action: 'get',
8
+        };
9
+        let response = await $.ajax({
10
+            type: "POST",
11
+            url: "save_manager.php",
12
+            data: JSON.stringify(data),
13
+            dataType: "json",
14
+            contentType: "application/json; charset=utf-8",
15
+        });
16
+        console.log(response);
17
+        return response['data'];
18
+    }
19
+
20
+    static async deleteCategory(id) {
21
+        let data = {
22
+            type: 'category',
23
+            action: 'remove',
24
+            data: id,
25
+        };
26
+        let response = await $.ajax({
27
+            type: "POST",
28
+            url: "save_manager.php",
29
+            data: JSON.stringify(data),
30
+            dataType: "json",
31
+            contentType: "application/json; charset=utf-8",
32
+        });
33
+        console.log(response);
34
+        return response['status'];
35
+    }
36
+
37
+    static async saveCategory(category, isNew) {
38
+        let data = {
39
+            type: 'category',
40
+            action: isNew ? 'create' : 'update',
41
+            data: category,
42
+        };
43
+
44
+        let response = await $.ajax({
45
+            type: "POST",
46
+            url: "save_manager.php",
47
+            data: JSON.stringify(data),
48
+            dataType: "json",
49
+            contentType: "application/json; charset=utf-8",
50
+        });
51
+        console.log(response);
52
+        return response['data'];
53
+    }
54
+}
55
+
56
+
57
+

+ 1
- 127
assets/js/categories.js View File

@@ -1,11 +1,7 @@
1
-// let listContainer = $("#dataList");
2
-// let currentTypes = [];
3
-// let displayedTypes = [];
4
-
5 1
 let listManager;
6 2
 
7 3
 $(document).ready(function () {
8
-    listManager = new ListManager($("#dataList"), json_dump,
4
+    listManager = new ListManager($("#dataList"), 'category',
9 5
         [
10 6
             {
11 7
                 name: 'icon',
@@ -19,125 +15,3 @@ $(document).ready(function () {
19 15
             },
20 16
         ]);
21 17
 });
22
-//
23
-// function initValuesFromPHPDump() {
24
-//     currentTypes = json_dump;
25
-//     console.log(currentTypes);
26
-//
27
-// }
28
-//
29
-// $(document).ready(function () {
30
-//     initValuesFromPHPDump();
31
-//     generateList();
32
-//     console.log(displayedTypes);
33
-// });
34
-//
35
-// function generateList() {
36
-//     for (let i = 0; i < currentTypes.length; i++) {
37
-//         createNewListEntry(currentTypes[i]);
38
-//     }
39
-// }
40
-//
41
-// function createNewListEntry(type) {
42
-//     displayedTypes.push(
43
-//         $("<li id='listItem_" + type["id"] + "' onclick='showEditPopup(" + displayedTypes.length + ")'>" +
44
-//         "<div>" +
45
-//         "<span class='category-icon'><i class='mdi mdi-" + type["icon"] + "'></i></span>" +
46
-//         "<span class='category-title'>" + type["name"] + "</span>" +
47
-//         "</div>" +
48
-//         "</li>"));
49
-//     listContainer.append(displayedTypes[displayedTypes.length - 1]);
50
-// }
51
-//
52
-// function deleteListEntry(id) {
53
-//     $('#listItem_' + id).remove();
54
-// }
55
-//
56
-//
57
-// function showEditPopup(index) {
58
-//     let defaultValues = {
59
-//         name: "",
60
-//         icon: ""
61
-//     };
62
-//     let title = "Créer une catégorie";
63
-//     if (index !== -1) {
64
-//         defaultValues = currentTypes[index];
65
-//         title = "Modifier la catégorie";
66
-//     }
67
-//     $.confirm({
68
-//         title: title,
69
-//         content:
70
-//             '<form action="" class="categoryForm">' +
71
-//             '<div class="form-group">' +
72
-//             '<label for="nameInput">Nom :</label>' +
73
-//             '<input id="nameInput" type="text" placeholder="Nom" class="name-input form-control" value="' + defaultValues['name'] + '" required />' +
74
-//             '<label for="iconInput">Icone :</label>' +
75
-//             '<input id="iconInput" type="text" placeholder="icone" class="icon-input form-control" value="' + defaultValues['icon'] + '" required />' +
76
-//             '</div>' +
77
-//             '</form>',
78
-//         buttons: {
79
-//             formSubmit: {
80
-//                 text: 'Sauvegarder',
81
-//                 btnClass: 'btn-blue',
82
-//                 action: async function () {
83
-//                     let name = this.$content.find('#nameInput').val();
84
-//                     let icon = this.$content.find('#iconInput').val();
85
-//                     if (!name) {
86
-//                         $.alert('Merci de donner un nom');
87
-//                         return false;
88
-//                     } else if (!icon) {
89
-//                         $.alert('Merci de donner une icone');
90
-//                         return false;
91
-//                     }
92
-//                     let itemToSave;
93
-//                     if (index !== -1) {
94
-//                         currentTypes[index]['name'] = name;
95
-//                         currentTypes[index]['icon'] = icon;
96
-//                         itemToSave = currentTypes[index];
97
-//                         displayedTypes[index].find(".category-title").text(name);
98
-//                         displayedTypes[index].find(".mdi")[0].className = "mdi mdi-" + icon;
99
-//                     } else {
100
-//                         itemToSave = {
101
-//                             name: name,
102
-//                             icon: icon,
103
-//                             display_order: 0,
104
-//                         };
105
-//                     }
106
-//                     let id = await SaveManager.saveCategory(itemToSave, index === -1);
107
-//                     if (id >= 0 && index === -1) {
108
-//                         itemToSave = {
109
-//                             name: name,
110
-//                             icon: icon,
111
-//                             display_order: 0,
112
-//                             id: id
113
-//                         };
114
-//                         currentTypes[displayedTypes.length] = itemToSave;
115
-//                         createNewListEntry(itemToSave);
116
-//                     }
117
-//                 }
118
-//             },
119
-//             deleteButton: {
120
-//                 text: 'Supprimer',
121
-//                 btnClass: 'btn-red',
122
-//                 isHidden: index === -1,
123
-//                 action: function () {
124
-//                     let id = currentTypes[index]['id'];
125
-//                     SaveManager.deleteCategory(id);
126
-//                     deleteListEntry(id);
127
-//                 }
128
-//             },
129
-//             cancelButton: {
130
-//                 text: 'Annuler',
131
-//             },
132
-//         },
133
-//         onContentReady: function () {
134
-//             // bind to events
135
-//             let jc = this;
136
-//             this.$content.find('form').on('submit', function (e) {
137
-//                 // if the user submits the form by pressing enter in the field.
138
-//                 e.preventDefault();
139
-//                 jc.$$formSubmit.trigger('click'); // reference the button and click it
140
-//             });
141
-//         }
142
-//     });
143
-// }

+ 57
- 23
assets/js/listManager.js View File

@@ -1,28 +1,31 @@
1
-
2 1
 class ListManager {
3 2
 
4
-    currentTypes = [];
5
-    displayedTypes = [];
3
+    currentData = [];
4
+    displayedData = [];
6 5
     editableTypes = [];
7 6
 
8
-    constructor(listContainer, initialData, editableTypes) {
7
+    constructor(listContainer, type, editableTypes) {
9 8
         this.listContainer = listContainer;
10
-        this.currentTypes = initialData;
11 9
         this.editableTypes = editableTypes;
12
-        this.generateList();
10
+        if (type === 'category') {
11
+            AjaxManager.getCategories().then((data) => {
12
+                this.currentData = data;
13
+                this.generateList()
14
+            });
15
+        }
13 16
     }
14 17
 
15 18
     generateList() {
16
-        for (let i = 0; i < this.currentTypes.length; i++) {
17
-            this.createNewListEntry(this.currentTypes[i]);
19
+        for (let i = 0; i < this.currentData.length; i++) {
20
+            this.createNewListEntry(this.currentData[i]);
18 21
         }
19 22
     }
20 23
 
21 24
     createNewListEntry(item) {
22 25
         let listItem = this.getListItem(item);
23
-        this.displayedTypes.push($(listItem));
24
-        const index = this.displayedTypes.length-1;
25
-        let JQueryItem = this.displayedTypes[index];
26
+        this.displayedData.push($(listItem));
27
+        const index = this.displayedData.length - 1;
28
+        let JQueryItem = this.displayedData[index];
26 29
         this.listContainer.append(JQueryItem);
27 30
         JQueryItem.on('click', () => {
28 31
             this.showEditPopup(index);
@@ -51,7 +54,14 @@ class ListManager {
51 54
         for (let i = 0; i < this.editableTypes.length; i++) {
52 55
             let inputId = this.editableTypes[i]['name'] + 'Input';
53 56
             let value = defaultValues[this.editableTypes[i]['name']];
54
-            formData += '<label for="' + inputId + '">' + this.editableTypes[i]['description'] + ' :</label>' +
57
+            let icon = '';
58
+            if (this.editableTypes[i]['name'] === 'icon') {
59
+                icon = "<i class='mdi mdi-" + value + "' style='margin-left: 5px'></i>";
60
+                let onchangeCallback = function (value) {
61
+                    console.log($(icon));
62
+                }
63
+            }
64
+            formData += '<label for="' + inputId + '">' + this.editableTypes[i]['description'] + ' :</label>' + icon +
55 65
                 '<input id="' + inputId + '" type="text" placeholder="Entrez une valeur" class="form-control" value="' + value + '" required />';
56 66
         }
57 67
         formData += "</div></form>";
@@ -87,9 +97,9 @@ class ListManager {
87 97
     updateListItem(values, index) {
88 98
         for (let i = 0; i < values.length; i++) {
89 99
             if (this.editableTypes[i]['type'] === 'icon')
90
-                this.displayedTypes[index].find(".mdi")[0].className = "mdi mdi-" + values[i];
100
+                this.displayedData[index].find(".mdi")[0].className = "mdi mdi-" + values[i];
91 101
             else
92
-                this.displayedTypes[index].find(".list-" + this.editableTypes[i]['name']).text(values[i]);
102
+                this.displayedData[index].find(".list-" + this.editableTypes[i]['name']).text(values[i]);
93 103
         }
94 104
     }
95 105
 
@@ -100,7 +110,7 @@ class ListManager {
100 110
         };
101 111
         let title = "Créer une nouvelle entrée";
102 112
         if (index !== -1) {
103
-            defaultValues = this.currentTypes[index];
113
+            defaultValues = this.currentData[index];
104 114
             title = "Modifier l'entrée";
105 115
         }
106 116
         let formData = this.getFormData(defaultValues);
@@ -118,22 +128,24 @@ class ListManager {
118 128
                             $.alert('Merci de rentrer toutes les valeurs');
119 129
                             return false;
120 130
                         }
121
-                        let itemToSave =  {};
131
+                        let itemToSave = {};
122 132
                         if (index !== -1) {
123
-                            itemToSave = thisInstance.currentTypes[index];
133
+                            itemToSave = thisInstance.currentData[index];
124 134
                         }
125 135
 
126 136
                         itemToSave = thisInstance.insertFormDataIntoObject(values, itemToSave);
127 137
 
128 138
                         if (index !== -1) {
129
-                            thisInstance.currentTypes[index] = itemToSave;
139
+                            thisInstance.currentData[index] = itemToSave;
130 140
                             thisInstance.updateListItem(values, index);
131 141
                         }
132
-                        let id = await SaveManager.saveCategory(itemToSave, index === -1);
142
+                        let id = await AjaxManager.saveCategory(itemToSave, index === -1);
133 143
                         if (id >= 0 && index === -1) {
134 144
                             itemToSave["id"] = id;
135
-                            thisInstance.currentTypes[thisInstance.displayedTypes.length] = itemToSave;
145
+                            thisInstance.currentData[thisInstance.displayedData.length] = itemToSave;
136 146
                             thisInstance.createNewListEntry(itemToSave);
147
+                        } else if (id === -1) {
148
+                            $.alert("Erreur serveur !");
137 149
                         }
138 150
                     }
139 151
                 },
@@ -142,9 +154,7 @@ class ListManager {
142 154
                     btnClass: 'btn-red',
143 155
                     isHidden: index === -1,
144 156
                     action: function () {
145
-                        let id = thisInstance.currentTypes[index]['id'];
146
-                        SaveManager.deleteCategory(id);
147
-                        thisInstance.deleteListEntry(id);
157
+                        thisInstance.showDeleteConfirmation(index);
148 158
                     }
149 159
                 },
150 160
                 cancelButton: {
@@ -163,5 +173,29 @@ class ListManager {
163 173
         });
164 174
     }
165 175
 
176
+    showDeleteConfirmation(index) {
177
+        let thisInstance = this;
178
+        $.confirm({
179
+            title: "Confirmation",
180
+            content: "Êtes vous sûr de vouloir supprimer cette entrée ?",
181
+            buttons: {
182
+                deleteButton: {
183
+                    text: 'Confirmer',
184
+                    btnClass: 'btn-red',
185
+                    action: async function () {
186
+                        let id = thisInstance.currentData[index]['id'];
187
+                        let status = await AjaxManager.deleteCategory(id);
188
+                        if (status === 0)
189
+                            thisInstance.deleteListEntry(id);
190
+                        else
191
+                            $.alert("Erreur serveur !");
192
+                    }
193
+                },
194
+                cancelButton: {
195
+                    text: 'Annuler',
196
+                },
197
+            }
198
+        });
199
+    }
166 200
 
167 201
 }

+ 0
- 42
assets/js/saveManager.js View File

@@ -1,42 +0,0 @@
1
-
2
-class SaveManager {
3
-
4
-    static deleteCategory(id) {
5
-        let data = {
6
-            function: "remove_category",
7
-            data: id,
8
-        };
9
-        console.log(data);
10
-        $.ajax({
11
-            type: "POST",
12
-            url: "save_manager.php",
13
-            data: JSON.stringify(data),
14
-            dataType: "json",
15
-            contentType: "application/json; charset=utf-8",
16
-            complete: function (data) {
17
-                $.alert(data.responseText);
18
-                console.log(data);
19
-            },
20
-        });
21
-    }
22
-
23
-    static async saveCategory(category, isNew) {
24
-        let data = {
25
-            function: isNew ? "create_category" : "update_category",
26
-            data: category,
27
-        };
28
-
29
-        let response = await $.ajax({
30
-            type: "POST",
31
-            url: "save_manager.php",
32
-            data: JSON.stringify(data),
33
-            dataType: "json",
34
-            contentType: "application/json; charset=utf-8",
35
-        });
36
-        console.log(response);
37
-        return response['data'];
38
-    }
39
-}
40
-
41
-
42
-

+ 1
- 1
assets/js/stock.js View File

@@ -189,7 +189,7 @@ function isItemInputFilled() {
189 189
 
190 190
 
191 191
 function saveDataset() {
192
-    SaveManager.saveData(currentTypes, currentDataset);
192
+    AjaxManager.saveData(currentTypes, currentDataset);
193 193
 }
194 194
 
195 195
 function sanitizeString(str) {

+ 5
- 1
classes/dao.php View File

@@ -78,6 +78,10 @@ class Dao
78 78
         $sql = 'DELETE FROM categories WHERE id=?';
79 79
         $cursor = $this->conn->prepare($sql);
80 80
         $data = [$id];
81
-        return $cursor->execute($data);
81
+        $result = $cursor->execute($data);
82
+        if ($result)
83
+            return $cursor->rowCount();
84
+        else
85
+            return 0;
82 86
     }
83 87
 }

+ 157
- 0
classes/postHandler.php View File

@@ -0,0 +1,157 @@
1
+<?php
2
+require_once 'dao.php';
3
+
4
+class PostHandler
5
+{
6
+    private $valid_types = ["article", "category"];
7
+    private $valid_actions = ["create", "update", "remove", "get"];
8
+
9
+    private $action;
10
+    private $type;
11
+    private $postData;
12
+    private $filesData;
13
+    private $data;
14
+    private $dao;
15
+    private $uploadBaseDir = '../../uploaded_images/';
16
+
17
+    private $responseArray = array(
18
+        "status" => 0,
19
+        "message" => "Success",
20
+        "data" => "",
21
+    );
22
+
23
+    public function __construct($post, $files)
24
+    {
25
+        $this->filesData = $files;
26
+        $this->action = $this->get_action($post);
27
+        $this->type = $this->get_type($post);
28
+        $this->data = $this->get_data($post);
29
+        $this->postData = $post;
30
+        $this->dao = new Dao();
31
+    }
32
+
33
+    public function do_action()
34
+    {
35
+        $result = -1;
36
+        if (count($this->data) > 0) {
37
+            if ($this->action == "create")
38
+                $result = $this->create();
39
+            else if ($this->action == "update")
40
+                $result = $this->update();
41
+            else if ($this->action == "remove")
42
+                $result = $this->remove();
43
+            else
44
+                $this->setUnknownActionResponse();
45
+        } else if ($this->action == "get")
46
+            $result = $this->get();
47
+        else
48
+            $this->setUnknownDataResponse();
49
+
50
+        if ($this->responseArray["status"] == 0 && $result < 0) {
51
+            $this->setProcessingErrorResponse();
52
+            $result = -1;
53
+        }
54
+        $this->responseArray["data"] = $result;
55
+        return $this->responseArray;
56
+    }
57
+
58
+    function create()
59
+    {
60
+        $result = -1;
61
+        if ($this->type == "article") {
62
+
63
+        } else if ($this->type == "category") {
64
+            $result = $this->dao->create_category($this->data);
65
+        } else
66
+            $this->setUnknownTypeResponse();
67
+        return $result;
68
+    }
69
+
70
+    function update()
71
+    {
72
+        $result = -1;
73
+        if ($this->type == "article") {
74
+
75
+        } else if ($this->type == "category") {
76
+            $result = $this->dao->update_category($this->data);
77
+        } else
78
+            $this->setUnknownTypeResponse();
79
+        return $result;
80
+    }
81
+
82
+    function remove()
83
+    {
84
+        $result = -1;
85
+        if ($this->type == "article") {
86
+
87
+        } else if ($this->type == "category") {
88
+            $result = $this->dao->remove_category($this->data);
89
+        } else
90
+            $this->setUnknownTypeResponse();
91
+        if ($result == 0)
92
+            return -1;
93
+        else
94
+            return 1;
95
+    }
96
+
97
+    function get()
98
+    {
99
+        $result = -1;
100
+        if ($this->type == "article") {
101
+
102
+        } else if ($this->type == "category") {
103
+            $result = $this->dao->get_categories();
104
+        } else
105
+            $this->setUnknownTypeResponse();
106
+        return $result;
107
+    }
108
+
109
+    function setUnknownTypeResponse()
110
+    {
111
+        $this->responseArray["status"] = 1;
112
+        $this->responseArray["message"] = "Error: Unknown type";
113
+    }
114
+
115
+    function setUnknownActionResponse()
116
+    {
117
+        $this->responseArray["status"] = 2;
118
+        $this->responseArray["message"] = "Error: Unknown action";
119
+    }
120
+
121
+    function setUnknownDataResponse()
122
+    {
123
+        $this->responseArray["status"] = 3;
124
+        $this->responseArray["message"] = "Error: Unknown data";
125
+    }
126
+
127
+    function setProcessingErrorResponse()
128
+    {
129
+        $this->responseArray["status"] = 4;
130
+        $this->responseArray["message"] = "Error: Data processing error";
131
+    }
132
+
133
+    private function get_action($inputData)
134
+    {
135
+        if (!in_array($inputData["action"], $this->valid_actions))
136
+            return "";
137
+        else
138
+            return $inputData["action"];
139
+    }
140
+
141
+    private function get_type($inputData)
142
+    {
143
+        if (!in_array($inputData["type"], $this->valid_types))
144
+            return "";
145
+        else
146
+            return $inputData["type"];
147
+    }
148
+
149
+    private function get_data($inputData)
150
+    {
151
+        if ($inputData["data"] == null)
152
+            return [];
153
+        else
154
+            return $inputData["data"];
155
+    }
156
+
157
+}

Loading…
Cancel
Save