';
+ let formData = '";
return formData;
}
+ getImagePicker(id) {
+ return (
+ '

' +
+ '
\n' +
+ '\n' +
+ '\n' +
+ '
');
+ }
+
getFormValues(form) {
let values = [];
for (let i = 0; i < this.editableTypes.length; i++) {
@@ -181,7 +201,7 @@ class ListManager {
updateListItemCategories(categories, index) {
if (index === -1)
- index = this.displayedData.length -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']));
@@ -201,6 +221,10 @@ class ListManager {
this.associationTable = newTable;
}
+ updateArticleImage(image, index) {
+ $(this.displayedData[index].find("img")[0]).attr('src', window.URL.createObjectURL(image));
+ }
+
showEditPopup(index) {
let defaultValues = {};
let title = "Créer une nouvelle entrée";
@@ -218,6 +242,10 @@ class ListManager {
text: 'Sauvegarder',
btnClass: 'btn-blue',
action: async function () {
+ let image = undefined;
+ if (this.$content.find('input[type=file]').length > 0)
+ image = this.$content.find('input[type=file]')[0].files[0];
+
let values = thisInstance.getFormValues(this.$content);
if (!thisInstance.isFormValid(values)) {
@@ -253,6 +281,9 @@ class ListManager {
if (result) {
thisInstance.updateAssociationList(id, categories);
thisInstance.updateListItemCategories(categories, index);
+ let imageResult = await AjaxManager.saveArticleImage(image, id);
+ if (imageResult === 0)
+ thisInstance.updateArticleImage(image, index);
}
}
}
@@ -268,15 +299,6 @@ class ListManager {
cancelButton: {
text: 'Annuler',
},
- },
- onContentReady: function () {
- // bind to events
- let jc = this;
- this.$content.find('form').on('submit', function (e) {
- // if the user submits the form by pressing enter in the field.
- e.preventDefault();
- jc.$$formSubmit.trigger('click'); // reference the button and click it
- });
}
});
}
diff --git a/assets/js/stock.js b/assets/js/stock.js
deleted file mode 100644
index 4cf7945..0000000
--- a/assets/js/stock.js
+++ /dev/null
@@ -1,219 +0,0 @@
-let currentDataset = [];
-let currentTypes = [];
-
-function initValuesFromPHPDump() {
- currentDataset = json_dump.articles; // json_dump is set using PHP
- currentTypes = json_dump.types;
- console.log(currentDataset);
- console.log(currentTypes);
- generateTable(currentDataset);
-}
-
-
-
-$(document).ready(function () {
- if (json_dump !== undefined) {
- initValuesFromPHPDump();
- generateTypeCheckboxes();
- }
- setEditFieldValues('', '', '', '', '', '', '');
-});
-
-
-function generateTable(dataset) {
- for (let i = 0; i < dataset.length; i++) {
- generateLine(dataset[i]);
- }
-}
-
-function generateLine(item) {
- $.selector_cache('#stockTable').append(
- '
' +
- '' + item.name + ' | ' +
- '' + item.description + ' | ' +
- '' + item.quantity + ' | ' +
- '' + item.price + ' | ' +
- '' + item.code + ' | ' +
- ' | ' +
- ' | ' +
- '' +
- '' +
- '' +
- ' | ' +
- '
'
- );
- // Fill in the type cell
- for (let i = 0; i < item.type.length; i++) {
- let type = getTypeOfId(item.type[i]);
- $('#typeList_' + item.code).append(
- '
'
- );
- }
-}
-
-function generateTypeCheckboxes() {
- for (let i = 0; i < currentTypes.length; i++) {
- let id = 'typeCheck_' + currentTypes[i].id;
- $('#typeCheckboxesCell').append(
- '
' +
- '' +
- '' +
- '
'
- );
- }
-}
-
-function getTypeOfId(id) {
- let item = {};
- for (let i = 0; i < currentTypes.length; i++) {
- if (currentTypes[i].id === id) {
- item = currentTypes[i];
- break;
- }
- }
- return item;
-}
-
-function getItemOfCode(code) {
- let item = {};
- for (let i = 0; i < currentDataset.length; i++) {
- if (currentDataset[i].code === code) {
- item = currentDataset[i];
- break;
- }
- }
- return item;
-}
-
-function addNewItem() {
- if (isItemInputFilled()) {
- let item = {
- name: sanitizeString($.selector_cache('#nameInput').val()),
- description: sanitizeString($.selector_cache('#descriptionInput').val()),
- quantity: sanitizeNumber($.selector_cache('#quantityInput').val()),
- price: sanitizeNumber($.selector_cache('#priceInput').val()),
- code: $.selector_cache('#codeInput').val(),
- type: getTypesChecked(),
- image: $.selector_cache('#imageInput').val(),
- };
- if (isCodeAvailable(item.code)) {
- setEditFieldValues('', '', '', '', [], '', '');
- currentDataset.push(item);
- generateLine(item);
- }
- }
-}
-
-function editItem(elem) {
- if (isItemInputEmpty()) {
- let code = elem.id.replace('edit_', '');
- let item = getItemOfCode(code);
- setEditFieldValues(item.name, item.description, item.quantity, item.price, item.code, item.type, item.image);
- removeItemFromList(item); // Move the item in the edit fields
- }
-}
-
-function deleteItem(elem) {
- let code = elem.id.replace('delete_', '');
- let item = getItemOfCode(code);
- removeItemFromList(item);
-}
-
-function removeItemFromList(item) {
- currentDataset.splice(currentDataset.indexOf(item), 1);
- $('#row_' + item.code).remove();
-}
-
-function getTypesChecked() {
- let types = [];
- for (let i = 0; i < currentTypes.length; i++) {
- let id = 'typeCheck_' + currentTypes[i].id;
- if ($('#' + id).is(':checked')) {
- types.push(currentTypes[i].id);
- }
- }
- return types;
-}
-
-function setTypesChecked(types) {
- for (let i = 0; i < currentTypes.length; i++) {
- let id = 'typeCheck_' + currentTypes[i].id;
- $('#' + id).prop('checked', types.indexOf(currentTypes[i].id) !== -1);
- }
- return types;
-}
-
-function setEditFieldValues(name, description, quantity, price, code, type, image) {
- $.selector_cache('#nameInput').val(name);
- $.selector_cache('#descriptionInput').val(description);
- $.selector_cache('#quantityInput').val(quantity);
- $.selector_cache('#priceInput').val(price);
- $.selector_cache('#codeInput').val(code);
- $.selector_cache('#codeInput').val(code);
- setTypesChecked(type);
- $.selector_cache('#imageInput').val(image);
-}
-
-function isCodeAvailable (code) {
- let isAvailable = true;
- for (let i = 0; i < currentDataset.length; i++) {
- if (currentDataset[i].code === code) {
- isAvailable = false;
- break;
- }
- }
- return isAvailable;
-}
-
-function isItemInputEmpty() {
- return $.selector_cache('#nameInput').val() === '' &&
- $.selector_cache('#descriptionInput').val() === '' &&
- $.selector_cache('#quantityInput').val() === '' &&
- $.selector_cache('#priceInput').val() === '' &&
- $.selector_cache('#codeInput').val() === '' &&
- $.selector_cache('#imageInput').val() === '' &&
- getTypesChecked().length === 0;
-}
-
-function isItemInputFilled() {
- return $.selector_cache('#nameInput').val() !== '' &&
- $.selector_cache('#descriptionInput').val() !== '' &&
- $.selector_cache('#quantityInput').val() !== '' &&
- $.selector_cache('#priceInput').val() !== '' &&
- $.selector_cache('#codeInput').val() !== '' &&
- $.selector_cache('#imageInput').val() !== '' &&
- getTypesChecked().length > 0;
-}
-
-
-
-function saveDataset() {
- AjaxManager.saveData(currentTypes, currentDataset);
-}
-
-function sanitizeString(str) {
- return str.trim();
-}
-
-function sanitizeNumber(nbrStr) {
- return nbrStr.replace(/\s/g,'');
-}
-
-function scanArticle(code) {
- let data = {
- password: 'coucou',
- code : code,
- };
- $.ajax({
- type: "POST",
- url: "../ajax/scan_article.php",
- data: JSON.stringify(data),
- dataType: "json",
- contentType: "application/json; charset=utf-8",
- complete: function (data) {
- // alert(data.responseText);
- console.log(data.responseText);
- },
- });
-}
diff --git a/classes/postHandler.php b/classes/postHandler.php
index 59692c7..a72fc4b 100644
--- a/classes/postHandler.php
+++ b/classes/postHandler.php
@@ -3,7 +3,7 @@ require_once 'dao.php';
class PostHandler
{
- private $valid_types = ["article", "category", "article_categories"];
+ private $valid_types = ["article", "category", "article_categories", 'image'];
private $valid_actions = ["create", "update", "remove", "get"];
private $action;
@@ -12,7 +12,7 @@ class PostHandler
private $filesData;
private $data;
private $dao;
- private $uploadBaseDir = '../../uploaded_images/';
+ private $uploadBaseDir = '../uploaded_images/';
private $responseArray = array(
"status" => 0,
@@ -33,7 +33,9 @@ class PostHandler
public function do_action()
{
$result = -1;
- if (count($this->data) > 0) {
+ if ($this->type == "image") {
+ $result = $this->save_image();
+ } else if (count($this->data) > 0) {
if ($this->action == "create")
$result = $this->create();
else if ($this->action == "update")
@@ -55,6 +57,30 @@ class PostHandler
return $this->responseArray;
}
+ private function save_image()
+ {
+ $success = true;
+ if ($this->filesData["image"]["size"] > 0 && $this->data != null) {
+ $uploadPath = $this->uploadBaseDir.$this->data.".jpg";
+
+ if (move_uploaded_file($this->filesData["image"]["tmp_name"], $uploadPath)) {
+ $this->responseArray["message"] = "Image upload success";
+ } else {
+ $this->responseArray["message"] = "Image upload failure: ". $uploadPath;
+ $this->responseArray["status"] = 1;
+ $success = false;
+ }
+ } else {
+ $this->responseArray["message"] = "No valid file to send";
+ $this->responseArray["status"] = 1;
+ $success = false;
+ }
+ if ($success)
+ return 0;
+ else
+ return json_encode($this->filesData)."id: ".$this->data;
+ }
+
function create()
{
$result = -1;