Added ability to set article category

This commit is contained in:
keplyx 2020-02-12 11:44:29 +01:00
parent 8a8f202e67
commit cf599f0ef9
5 changed files with 180 additions and 33 deletions

View file

@ -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",

View file

@ -23,5 +23,10 @@ $(document).ready(function () {
description: 'Code',
type: 'text'
},
{
name: 'category',
description: 'Catégories',
type: 'checkboxes'
},
]);
});

View file

@ -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 += "<i class='mdi mdi-" + this.getCategoryOfId(categories[i])["icon"] + "' style='margin-right: 5px'></i>";
}
return icons;
}
getListItem(item) {
let listItem = "<li id='listItem_" + item["id"] + "'><div>";
for (let i = 0; i < this.editableTypes.length; i++) {
listItem += "<span class='list-" + this.editableTypes[i]["name"] + "'>";
if (this.editableTypes[i]["type"] === "icon")
listItem += "<i class='mdi mdi-" + item["icon"] + "' style='margin-right: 5px'></i></span>";
else
else if (this.editableTypes[i]["type"] === 'checkboxes') {
listItem += this.getCategoryIcons(item['id']);
} else
listItem += item[this.editableTypes[i]["name"]] + '</span>';
}
listItem += "</div></li>";
return listItem;
}
getCategoryCheckboxes(articleId) {
let checkboxes = "<div id='checkboxesContainer' style='display: flex'>";
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 +=
'<div class="custom-control custom-checkbox" style="margin-right: 20px">\n' +
'<input type="checkbox" class="custom-control-input" id="checkbox_' + id + '"' + checked + ' >\n' +
'<label class="custom-control-label" for="checkbox_' + id + '">' +
'<i class="mdi mdi-' + cat["icon"] + '" style="margin-right: 5px"></i>' +
'</label>\n' +
'</div>';
}
checkboxes += '</div>';
return checkboxes;
}
getFormData(defaultValues) {
let formData = '<form action="" class="categoryForm"><div class="form-group">';
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 = "<i class='mdi mdi-" + value + "' style='margin-left: 5px'></i>";
let onchangeCallback = function (value) {
console.log($(icon));
}
formData += '<label for="' + inputId + '">' + this.editableTypes[i]['description'] + ' :</label>';
if (this.editableTypes[i]['type'] !== 'checkboxes') {
if (this.editableTypes[i]['name'] === 'icon')
formData += "<i class='mdi mdi-" + value + "' style='margin-left: 5px'></i>";
formData += '<input id="' + inputId + '" type="' + inputType + '" placeholder="Entrez une valeur" class="form-control" value="' + value + '" required />';
} else {
formData += this.getCategoryCheckboxes(defaultValues['id']);
}
formData += '<label for="' + inputId + '">' + this.editableTypes[i]['description'] + ' :</label>' + icon +
'<input id="' + inputId + '" type="' + inputType + '" placeholder="Entrez une valeur" class="form-control" value="' + value + '" required />';
}
formData += "</div></form>";
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: {

View file

@ -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;
}
}

View file

@ -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;