diff --git a/assets/js/ajaxManager.js b/assets/js/ajaxManager.js index a51d0bb..5cc9d89 100644 --- a/assets/js/ajaxManager.js +++ b/assets/js/ajaxManager.js @@ -1,6 +1,13 @@ class AjaxManager { + static async getAll() { + let categories = await AjaxManager.get('category'); + let articles = await AjaxManager.get('article'); + let article_categories = await AjaxManager.get('article_categories'); + return {articles: articles, categories: categories, article_categories: article_categories}; + } + static async get(type) { let data = { type: type, @@ -40,7 +47,6 @@ class AjaxManager { action: isNew ? 'create' : 'update', data: data, }; - console.log(formattedData); let response = await $.ajax({ type: "POST", url: "save_manager.php", diff --git a/assets/js/articles.js b/assets/js/articles.js index a71eeca..ca2efb6 100644 --- a/assets/js/articles.js +++ b/assets/js/articles.js @@ -23,5 +23,10 @@ $(document).ready(function () { description: 'Code', type: 'text' }, + { + name: 'category', + description: 'Catégories', + type: 'checkboxes' + }, ]); }); diff --git a/assets/js/listManager.js b/assets/js/listManager.js index d5110d8..d362951 100644 --- a/assets/js/listManager.js +++ b/assets/js/listManager.js @@ -5,15 +5,27 @@ class ListManager { editableTypes = []; type = ''; + referredTable = []; + associationTable = []; constructor(listContainer, type, editableTypes) { this.listContainer = listContainer; this.editableTypes = editableTypes; this.type = type; - AjaxManager.get(type).then((data) => { - this.currentData = data; - this.generateList() - }); + + if (type === 'article') { + AjaxManager.getAll().then((data) => { + this.currentData = data['articles']; + this.referredTable = data['categories']; + this.associationTable = data['article_categories']; + this.generateList() + }); + } else { + AjaxManager.get(type).then((data) => { + this.currentData = data; + this.generateList() + }); + } } generateList() { @@ -37,34 +49,84 @@ class ListManager { $('#listItem_' + id).remove(); } + getCategoriesOfArticle(articleId) { + let filteredList = []; + for (let i = 0; i < this.associationTable.length; i++) { + if (this.associationTable[i]['article_id'] === articleId) + filteredList.push(this.associationTable[i]['category_id']); + } + return filteredList; + } + + getCategoryOfId(categoryId) { + for (let i = 0; i < this.referredTable.length; i++) { + if (this.referredTable[i]['id'] === categoryId) { + return this.referredTable[i]; + } + } + return ""; + } + + getCategoryIcons(articleId) { + let icons = ""; + let categories = this.getCategoriesOfArticle(articleId); + for (let i = 0; i < categories.length; i++) { + icons += ""; + } + return icons; + } + getListItem(item) { let listItem = "
  • "; for (let i = 0; i < this.editableTypes.length; i++) { listItem += ""; if (this.editableTypes[i]["type"] === "icon") listItem += ""; - else + else if (this.editableTypes[i]["type"] === 'checkboxes') { + listItem += this.getCategoryIcons(item['id']); + } else listItem += item[this.editableTypes[i]["name"]] + ''; } listItem += "
  • "; return listItem; } + getCategoryCheckboxes(articleId) { + let checkboxes = "
    "; + let categories = []; + if (articleId !== undefined) + categories = this.getCategoriesOfArticle(articleId); + for (let i = 0; i < this.referredTable.length; i++) { + let id = this.referredTable[i]['id']; + let cat = this.getCategoryOfId(id); + let checked = categories.indexOf(id) !== -1 ? 'checked' : ''; + checkboxes += + '
    \n' + + '\n' + + '\n' + + '
    '; + } + checkboxes += '
    '; + return checkboxes; + } + getFormData(defaultValues) { let formData = '
    '; for (let i = 0; i < this.editableTypes.length; i++) { let inputId = this.editableTypes[i]['name'] + 'Input'; let inputType = this.editableTypes[i]['type'] === 'number' ? 'number' : 'text'; - let value = defaultValues[this.editableTypes[i]['name']]; + let value = defaultValues[this.editableTypes[i]['name']] !== undefined ? defaultValues[this.editableTypes[i]['name']] : ''; let icon = ''; - if (this.editableTypes[i]['name'] === 'icon') { - icon = ""; - let onchangeCallback = function (value) { - console.log($(icon)); - } + formData += ''; + if (this.editableTypes[i]['type'] !== 'checkboxes') { + if (this.editableTypes[i]['name'] === 'icon') + formData += ""; + formData += ''; + } else { + formData += this.getCategoryCheckboxes(defaultValues['id']); } - formData += '' + icon + - ''; } formData += "
    "; return formData; @@ -89,6 +151,18 @@ class ListManager { return isValid; } + getCheckedCategories(content) { + let checkedList = []; + let checkboxes = content.find('#checkboxesContainer').children(); + for (let i = 0; i < checkboxes.length; i++) { + let input = $(checkboxes[i]).find('input')[0]; + let id = $(input).attr('id').replace('checkbox_', ''); + if (input.checked) + checkedList.push(id); + } + return checkedList; + } + insertFormDataIntoObject(values, object) { for (let i = 0; i < values.length; i++) { object[this.editableTypes[i]['name']] = values[i]; @@ -105,11 +179,30 @@ class ListManager { } } + updateListItemCategories(categories, index) { + if (index === -1) + index = this.displayedData.length -1; + let iconContainer = this.displayedData[index].find(".list-category"); + for (let i = 0; i < categories.length; i++) { + iconContainer.html(this.getCategoryIcons(this.currentData[index]['id'])); + } + } + + updateAssociationList(articleId, newCategories) { + let newTable = []; + for (let i = 0; i < this.associationTable.length; i++) { + if (this.associationTable[i]['article_id'] !== articleId) + newTable.push(this.associationTable[i]); + } + for (let i = 0; i < newCategories.length; i++) { + newTable.push({article_id: articleId, category_id: newCategories[i]}); + } + console.log(newTable); + this.associationTable = newTable; + } + showEditPopup(index) { - let defaultValues = { - name: "", - icon: "" - }; + let defaultValues = {}; let title = "Créer une nouvelle entrée"; if (index !== -1) { defaultValues = this.currentData[index]; @@ -126,6 +219,7 @@ class ListManager { btnClass: 'btn-blue', action: async function () { let values = thisInstance.getFormValues(this.$content); + if (!thisInstance.isFormValid(values)) { $.alert('Merci de rentrer toutes les valeurs'); return false; @@ -149,6 +243,18 @@ class ListManager { } else if (id === -1) { $.alert("Erreur serveur !"); } + if (id > 0 && thisInstance.type === 'article') { + let categories = thisInstance.getCheckedCategories(this.$content); + let data = { + id: id, + categories: categories + }; + let result = await AjaxManager.save(data, true, 'article_categories'); + if (result) { + thisInstance.updateAssociationList(id, categories); + thisInstance.updateListItemCategories(categories, index); + } + } } }, deleteButton: { diff --git a/classes/dao.php b/classes/dao.php index 7f2274d..9a70b92 100644 --- a/classes/dao.php +++ b/classes/dao.php @@ -35,17 +35,37 @@ class Dao return $cursor->fetchAll(PDO::FETCH_ASSOC); } - public function get_article_categories($article_id) + public function get_article_categories() { - $sql = 'SELECT category_id FROM article_categories WHERE article_id=?'; + $sql = 'SELECT * FROM article_categories'; $cursor = $this->conn->prepare($sql); - $cursor->execute([$article_id]); - $result = $cursor->fetchAll(PDO::FETCH_ASSOC); - $final = []; - foreach ($result as $row) { - array_push($final, $row["category_id"]); + $cursor->execute(); + return $cursor->fetchAll(PDO::FETCH_ASSOC); + } + + public function remove_article_categories_of_article($articleId) + { + $sql = 'DELETE FROM article_categories WHERE article_id=?'; + $cursor = $this->conn->prepare($sql); + return $cursor->execute([$articleId]); + } + + public function remove_article_categories_of_category($categoryId) + { + $sql = 'DELETE FROM article_categories WHERE category_id=?'; + $cursor = $this->conn->prepare($sql); + return $cursor->execute([$categoryId]); + } + + public function save_article_categories($articleId, $categories) + { + foreach ($categories as $category) { + $sql = 'INSERT INTO article_categories (article_id, category_id) VALUES (?, ?)'; + $cursor = $this->conn->prepare($sql); + $data = [$articleId, $category]; + $cursor->execute($data); } - return $final; + return 1; } public function get_categories() @@ -56,7 +76,8 @@ class Dao return $cursor->fetchAll(PDO::FETCH_ASSOC); } - public function create_category($category) { + public function create_category($category) + { $sql = 'INSERT INTO categories (name, icon) VALUES (?, ?)'; $cursor = $this->conn->prepare($sql); $data = [$category["name"], $category["icon"]]; @@ -79,13 +100,15 @@ class Dao $cursor = $this->conn->prepare($sql); $data = [$id]; $result = $cursor->execute($data); - if ($result) + if ($result) { + $this->remove_article_categories_of_category($id); return $cursor->rowCount(); - else + } else return 0; } - public function create_article($article) { + public function create_article($article) + { $sql = 'INSERT INTO articles (name, description, price, code) VALUES (?, ?, ?, ?)'; $cursor = $this->conn->prepare($sql); $data = [$article["name"], $article["description"], $article["price"], $article["code"]]; @@ -108,9 +131,10 @@ class Dao $cursor = $this->conn->prepare($sql); $data = [$id]; $result = $cursor->execute($data); - if ($result) + if ($result) { + $this->remove_article_categories_of_article($id); return $cursor->rowCount(); - else + } else return 0; } } diff --git a/classes/postHandler.php b/classes/postHandler.php index 486d70c..59692c7 100644 --- a/classes/postHandler.php +++ b/classes/postHandler.php @@ -3,7 +3,7 @@ require_once 'dao.php'; class PostHandler { - private $valid_types = ["article", "category"]; + private $valid_types = ["article", "category", "article_categories"]; private $valid_actions = ["create", "update", "remove", "get"]; private $action; @@ -62,6 +62,10 @@ class PostHandler $result = $this->dao->create_article($this->data); } else if ($this->type == "category") { $result = $this->dao->create_category($this->data); + } else if ($this->type == "article_categories") { + $result = $this->dao->remove_article_categories_of_article($this->data['id']); + if ($result) + $result = $this->dao->save_article_categories($this->data['id'], $this->data['categories']); } else $this->setUnknownTypeResponse(); return $result; @@ -101,6 +105,8 @@ class PostHandler $result = $this->dao->get_articles(); } else if ($this->type == "category") { $result = $this->dao->get_categories(); + } else if ($this->type == "article_categories") { + $result = $this->dao->get_article_categories(); } else $this->setUnknownTypeResponse(); return $result;