Added ability to set article category
This commit is contained in:
parent
8a8f202e67
commit
cf599f0ef9
5 changed files with 180 additions and 33 deletions
|
@ -1,6 +1,13 @@
|
||||||
|
|
||||||
class AjaxManager {
|
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) {
|
static async get(type) {
|
||||||
let data = {
|
let data = {
|
||||||
type: type,
|
type: type,
|
||||||
|
@ -40,7 +47,6 @@ class AjaxManager {
|
||||||
action: isNew ? 'create' : 'update',
|
action: isNew ? 'create' : 'update',
|
||||||
data: data,
|
data: data,
|
||||||
};
|
};
|
||||||
console.log(formattedData);
|
|
||||||
let response = await $.ajax({
|
let response = await $.ajax({
|
||||||
type: "POST",
|
type: "POST",
|
||||||
url: "save_manager.php",
|
url: "save_manager.php",
|
||||||
|
|
|
@ -23,5 +23,10 @@ $(document).ready(function () {
|
||||||
description: 'Code',
|
description: 'Code',
|
||||||
type: 'text'
|
type: 'text'
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'category',
|
||||||
|
description: 'Catégories',
|
||||||
|
type: 'checkboxes'
|
||||||
|
},
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
|
@ -5,15 +5,27 @@ class ListManager {
|
||||||
editableTypes = [];
|
editableTypes = [];
|
||||||
type = '';
|
type = '';
|
||||||
|
|
||||||
|
referredTable = [];
|
||||||
|
associationTable = [];
|
||||||
|
|
||||||
constructor(listContainer, type, editableTypes) {
|
constructor(listContainer, type, editableTypes) {
|
||||||
this.listContainer = listContainer;
|
this.listContainer = listContainer;
|
||||||
this.editableTypes = editableTypes;
|
this.editableTypes = editableTypes;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
AjaxManager.get(type).then((data) => {
|
|
||||||
this.currentData = data;
|
if (type === 'article') {
|
||||||
this.generateList()
|
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() {
|
generateList() {
|
||||||
|
@ -37,34 +49,84 @@ class ListManager {
|
||||||
$('#listItem_' + id).remove();
|
$('#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) {
|
getListItem(item) {
|
||||||
let listItem = "<li id='listItem_" + item["id"] + "'><div>";
|
let listItem = "<li id='listItem_" + item["id"] + "'><div>";
|
||||||
for (let i = 0; i < this.editableTypes.length; i++) {
|
for (let i = 0; i < this.editableTypes.length; i++) {
|
||||||
listItem += "<span class='list-" + this.editableTypes[i]["name"] + "'>";
|
listItem += "<span class='list-" + this.editableTypes[i]["name"] + "'>";
|
||||||
if (this.editableTypes[i]["type"] === "icon")
|
if (this.editableTypes[i]["type"] === "icon")
|
||||||
listItem += "<i class='mdi mdi-" + item["icon"] + "' style='margin-right: 5px'></i></span>";
|
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 += item[this.editableTypes[i]["name"]] + '</span>';
|
||||||
}
|
}
|
||||||
listItem += "</div></li>";
|
listItem += "</div></li>";
|
||||||
return listItem;
|
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) {
|
getFormData(defaultValues) {
|
||||||
let formData = '<form action="" class="categoryForm"><div class="form-group">';
|
let formData = '<form action="" class="categoryForm"><div class="form-group">';
|
||||||
for (let i = 0; i < this.editableTypes.length; i++) {
|
for (let i = 0; i < this.editableTypes.length; i++) {
|
||||||
let inputId = this.editableTypes[i]['name'] + 'Input';
|
let inputId = this.editableTypes[i]['name'] + 'Input';
|
||||||
let inputType = this.editableTypes[i]['type'] === 'number' ? 'number' : 'text';
|
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 = '';
|
let icon = '';
|
||||||
if (this.editableTypes[i]['name'] === 'icon') {
|
formData += '<label for="' + inputId + '">' + this.editableTypes[i]['description'] + ' :</label>';
|
||||||
icon = "<i class='mdi mdi-" + value + "' style='margin-left: 5px'></i>";
|
if (this.editableTypes[i]['type'] !== 'checkboxes') {
|
||||||
let onchangeCallback = function (value) {
|
if (this.editableTypes[i]['name'] === 'icon')
|
||||||
console.log($(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>";
|
formData += "</div></form>";
|
||||||
return formData;
|
return formData;
|
||||||
|
@ -89,6 +151,18 @@ class ListManager {
|
||||||
return isValid;
|
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) {
|
insertFormDataIntoObject(values, object) {
|
||||||
for (let i = 0; i < values.length; i++) {
|
for (let i = 0; i < values.length; i++) {
|
||||||
object[this.editableTypes[i]['name']] = values[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) {
|
showEditPopup(index) {
|
||||||
let defaultValues = {
|
let defaultValues = {};
|
||||||
name: "",
|
|
||||||
icon: ""
|
|
||||||
};
|
|
||||||
let title = "Créer une nouvelle entrée";
|
let title = "Créer une nouvelle entrée";
|
||||||
if (index !== -1) {
|
if (index !== -1) {
|
||||||
defaultValues = this.currentData[index];
|
defaultValues = this.currentData[index];
|
||||||
|
@ -126,6 +219,7 @@ class ListManager {
|
||||||
btnClass: 'btn-blue',
|
btnClass: 'btn-blue',
|
||||||
action: async function () {
|
action: async function () {
|
||||||
let values = thisInstance.getFormValues(this.$content);
|
let values = thisInstance.getFormValues(this.$content);
|
||||||
|
|
||||||
if (!thisInstance.isFormValid(values)) {
|
if (!thisInstance.isFormValid(values)) {
|
||||||
$.alert('Merci de rentrer toutes les valeurs');
|
$.alert('Merci de rentrer toutes les valeurs');
|
||||||
return false;
|
return false;
|
||||||
|
@ -149,6 +243,18 @@ class ListManager {
|
||||||
} else if (id === -1) {
|
} else if (id === -1) {
|
||||||
$.alert("Erreur serveur !");
|
$.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: {
|
deleteButton: {
|
||||||
|
|
|
@ -35,17 +35,37 @@ class Dao
|
||||||
return $cursor->fetchAll(PDO::FETCH_ASSOC);
|
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 = $this->conn->prepare($sql);
|
||||||
$cursor->execute([$article_id]);
|
$cursor->execute();
|
||||||
$result = $cursor->fetchAll(PDO::FETCH_ASSOC);
|
return $cursor->fetchAll(PDO::FETCH_ASSOC);
|
||||||
$final = [];
|
}
|
||||||
foreach ($result as $row) {
|
|
||||||
array_push($final, $row["category_id"]);
|
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()
|
public function get_categories()
|
||||||
|
@ -56,7 +76,8 @@ class Dao
|
||||||
return $cursor->fetchAll(PDO::FETCH_ASSOC);
|
return $cursor->fetchAll(PDO::FETCH_ASSOC);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function create_category($category) {
|
public function create_category($category)
|
||||||
|
{
|
||||||
$sql = 'INSERT INTO categories (name, icon) VALUES (?, ?)';
|
$sql = 'INSERT INTO categories (name, icon) VALUES (?, ?)';
|
||||||
$cursor = $this->conn->prepare($sql);
|
$cursor = $this->conn->prepare($sql);
|
||||||
$data = [$category["name"], $category["icon"]];
|
$data = [$category["name"], $category["icon"]];
|
||||||
|
@ -79,13 +100,15 @@ class Dao
|
||||||
$cursor = $this->conn->prepare($sql);
|
$cursor = $this->conn->prepare($sql);
|
||||||
$data = [$id];
|
$data = [$id];
|
||||||
$result = $cursor->execute($data);
|
$result = $cursor->execute($data);
|
||||||
if ($result)
|
if ($result) {
|
||||||
|
$this->remove_article_categories_of_category($id);
|
||||||
return $cursor->rowCount();
|
return $cursor->rowCount();
|
||||||
else
|
} else
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function create_article($article) {
|
public function create_article($article)
|
||||||
|
{
|
||||||
$sql = 'INSERT INTO articles (name, description, price, code) VALUES (?, ?, ?, ?)';
|
$sql = 'INSERT INTO articles (name, description, price, code) VALUES (?, ?, ?, ?)';
|
||||||
$cursor = $this->conn->prepare($sql);
|
$cursor = $this->conn->prepare($sql);
|
||||||
$data = [$article["name"], $article["description"], $article["price"], $article["code"]];
|
$data = [$article["name"], $article["description"], $article["price"], $article["code"]];
|
||||||
|
@ -108,9 +131,10 @@ class Dao
|
||||||
$cursor = $this->conn->prepare($sql);
|
$cursor = $this->conn->prepare($sql);
|
||||||
$data = [$id];
|
$data = [$id];
|
||||||
$result = $cursor->execute($data);
|
$result = $cursor->execute($data);
|
||||||
if ($result)
|
if ($result) {
|
||||||
|
$this->remove_article_categories_of_article($id);
|
||||||
return $cursor->rowCount();
|
return $cursor->rowCount();
|
||||||
else
|
} else
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ require_once 'dao.php';
|
||||||
|
|
||||||
class PostHandler
|
class PostHandler
|
||||||
{
|
{
|
||||||
private $valid_types = ["article", "category"];
|
private $valid_types = ["article", "category", "article_categories"];
|
||||||
private $valid_actions = ["create", "update", "remove", "get"];
|
private $valid_actions = ["create", "update", "remove", "get"];
|
||||||
|
|
||||||
private $action;
|
private $action;
|
||||||
|
@ -62,6 +62,10 @@ class PostHandler
|
||||||
$result = $this->dao->create_article($this->data);
|
$result = $this->dao->create_article($this->data);
|
||||||
} else if ($this->type == "category") {
|
} else if ($this->type == "category") {
|
||||||
$result = $this->dao->create_category($this->data);
|
$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
|
} else
|
||||||
$this->setUnknownTypeResponse();
|
$this->setUnknownTypeResponse();
|
||||||
return $result;
|
return $result;
|
||||||
|
@ -101,6 +105,8 @@ class PostHandler
|
||||||
$result = $this->dao->get_articles();
|
$result = $this->dao->get_articles();
|
||||||
} else if ($this->type == "category") {
|
} else if ($this->type == "category") {
|
||||||
$result = $this->dao->get_categories();
|
$result = $this->dao->get_categories();
|
||||||
|
} else if ($this->type == "article_categories") {
|
||||||
|
$result = $this->dao->get_article_categories();
|
||||||
} else
|
} else
|
||||||
$this->setUnknownTypeResponse();
|
$this->setUnknownTypeResponse();
|
||||||
return $result;
|
return $result;
|
||||||
|
|
Loading…
Reference in a new issue