2020-02-11 14:32:23 +01:00
|
|
|
<?php
|
|
|
|
require_once 'dao.php';
|
|
|
|
|
|
|
|
class PostHandler
|
|
|
|
{
|
2020-02-15 19:01:27 +01:00
|
|
|
private $valid_types = ["article", "category", "article_categories", "image", "stock"];
|
|
|
|
private $valid_actions = ["create", "update", "remove", "get", "buy", "sell"];
|
2020-02-11 14:32:23 +01:00
|
|
|
|
|
|
|
private $action;
|
|
|
|
private $type;
|
|
|
|
private $postData;
|
|
|
|
private $filesData;
|
|
|
|
private $data;
|
|
|
|
private $dao;
|
2020-02-14 12:05:21 +01:00
|
|
|
private $uploadBaseDir = '../uploaded_images/';
|
2020-02-20 12:20:42 +01:00
|
|
|
private $stockFile = "../data/stock-v2.json";
|
2020-02-20 12:50:58 +01:00
|
|
|
private $imageBaseUrl = "https://etud.insa-toulouse.fr/~proximo/uploaded_images/";
|
2020-02-11 14:32:23 +01:00
|
|
|
|
|
|
|
private $responseArray = array(
|
|
|
|
"status" => 0,
|
|
|
|
"message" => "Success",
|
|
|
|
"data" => "",
|
|
|
|
);
|
|
|
|
|
|
|
|
public function __construct($post, $files)
|
|
|
|
{
|
|
|
|
$this->filesData = $files;
|
|
|
|
$this->action = $this->get_action($post);
|
|
|
|
$this->type = $this->get_type($post);
|
|
|
|
$this->data = $this->get_data($post);
|
|
|
|
$this->postData = $post;
|
|
|
|
$this->dao = new Dao();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function do_action()
|
|
|
|
{
|
|
|
|
$result = -1;
|
2020-02-14 12:05:21 +01:00
|
|
|
if ($this->type == "image") {
|
|
|
|
$result = $this->save_image();
|
2020-02-15 19:01:27 +01:00
|
|
|
} else if ($this->type == "stock") {
|
|
|
|
$result = $this->updateStock();
|
2020-02-14 12:05:21 +01:00
|
|
|
} else if (count($this->data) > 0) {
|
2020-02-11 14:32:23 +01:00
|
|
|
if ($this->action == "create")
|
|
|
|
$result = $this->create();
|
|
|
|
else if ($this->action == "update")
|
|
|
|
$result = $this->update();
|
|
|
|
else if ($this->action == "remove")
|
|
|
|
$result = $this->remove();
|
|
|
|
else
|
|
|
|
$this->setUnknownActionResponse();
|
|
|
|
} else if ($this->action == "get")
|
|
|
|
$result = $this->get();
|
|
|
|
else
|
|
|
|
$this->setUnknownDataResponse();
|
|
|
|
|
|
|
|
if ($this->responseArray["status"] == 0 && $result < 0) {
|
|
|
|
$this->setProcessingErrorResponse();
|
|
|
|
$result = -1;
|
|
|
|
}
|
|
|
|
$this->responseArray["data"] = $result;
|
|
|
|
return $this->responseArray;
|
|
|
|
}
|
|
|
|
|
2020-02-20 12:20:42 +01:00
|
|
|
public function write_json()
|
|
|
|
{
|
|
|
|
$result = 0;
|
|
|
|
$array = array(
|
|
|
|
"types" => $this->dao->get_categories(),
|
2020-02-20 12:50:58 +01:00
|
|
|
"articles" => $this->get_articles_json_list(),
|
2020-02-20 12:20:42 +01:00
|
|
|
);
|
2020-09-09 15:27:39 +02:00
|
|
|
$fp = fopen($this->stockFile, "w");
|
|
|
|
if ($fp) {
|
|
|
|
fwrite($fp, json_encode($array));
|
|
|
|
fclose($fp);
|
|
|
|
$this->responseArray["data"] = $result;
|
|
|
|
} else {
|
|
|
|
$this->setFileErrorResponse();
|
|
|
|
}
|
2020-02-20 12:20:42 +01:00
|
|
|
|
|
|
|
return $this->responseArray;
|
|
|
|
}
|
|
|
|
|
2020-02-20 12:50:58 +01:00
|
|
|
public function get_articles_json_list()
|
|
|
|
{
|
|
|
|
$articles = $this->dao->get_articles();
|
|
|
|
$formatted_articles = [];
|
|
|
|
foreach ($articles as $article) {
|
|
|
|
$article["type"] = $this->dao->get_categories_of_article($article["id"]);
|
2020-08-29 09:08:08 +02:00
|
|
|
$article["image"] = $this->imageBaseUrl . $article["id"] . ".jpg";
|
2020-02-20 12:50:58 +01:00
|
|
|
array_push($formatted_articles, $article);
|
|
|
|
}
|
|
|
|
return $formatted_articles;
|
|
|
|
}
|
|
|
|
|
2020-02-14 12:05:21 +01:00
|
|
|
private function save_image()
|
|
|
|
{
|
|
|
|
$success = true;
|
|
|
|
if ($this->filesData["image"]["size"] > 0 && $this->data != null) {
|
2020-02-20 12:20:42 +01:00
|
|
|
$uploadPath = $this->uploadBaseDir . $this->data . ".jpg";
|
2020-02-14 12:05:21 +01:00
|
|
|
|
|
|
|
if (move_uploaded_file($this->filesData["image"]["tmp_name"], $uploadPath)) {
|
|
|
|
$this->responseArray["message"] = "Image upload success";
|
|
|
|
} else {
|
2020-02-20 12:20:42 +01:00
|
|
|
$this->responseArray["message"] = "Image upload failure: " . $uploadPath;
|
2020-02-14 12:05:21 +01:00
|
|
|
$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
|
2020-02-20 12:20:42 +01:00
|
|
|
return json_encode($this->filesData) . "id: " . $this->data;
|
2020-02-14 12:05:21 +01:00
|
|
|
}
|
|
|
|
|
2020-02-20 12:20:42 +01:00
|
|
|
private function remove_image()
|
|
|
|
{
|
|
|
|
$uploadPath = $this->uploadBaseDir . $this->data["id"] . ".jpg";
|
2020-02-14 12:11:42 +01:00
|
|
|
if (file_exists($uploadPath) && unlink($uploadPath)) {
|
|
|
|
$this->responseArray["message"] = "Success: Deleted image";
|
|
|
|
} else if (!file_exists($uploadPath)) {
|
|
|
|
$this->responseArray["message"] = "Success: No image to delete";
|
|
|
|
} else {
|
|
|
|
$this->responseArray["message"] = "Success: Could not delete image";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 14:32:23 +01:00
|
|
|
function create()
|
|
|
|
{
|
|
|
|
$result = -1;
|
|
|
|
if ($this->type == "article") {
|
2020-02-11 15:12:31 +01:00
|
|
|
$result = $this->dao->create_article($this->data);
|
2020-02-11 14:32:23 +01:00
|
|
|
} else if ($this->type == "category") {
|
|
|
|
$result = $this->dao->create_category($this->data);
|
2020-02-12 11:44:29 +01:00
|
|
|
} 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']);
|
2020-02-11 14:32:23 +01:00
|
|
|
} else
|
|
|
|
$this->setUnknownTypeResponse();
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function update()
|
|
|
|
{
|
|
|
|
$result = -1;
|
|
|
|
if ($this->type == "article") {
|
2020-02-11 15:12:31 +01:00
|
|
|
$result = $this->dao->update_article($this->data);
|
2020-02-11 14:32:23 +01:00
|
|
|
} else if ($this->type == "category") {
|
|
|
|
$result = $this->dao->update_category($this->data);
|
|
|
|
} else
|
|
|
|
$this->setUnknownTypeResponse();
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function remove()
|
|
|
|
{
|
|
|
|
$result = -1;
|
|
|
|
if ($this->type == "article") {
|
2020-02-11 15:12:31 +01:00
|
|
|
$result = $this->dao->remove_article($this->data);
|
2020-02-14 12:11:42 +01:00
|
|
|
if ($result != 0)
|
|
|
|
$this->remove_image();
|
2020-02-11 14:32:23 +01:00
|
|
|
} else if ($this->type == "category") {
|
|
|
|
$result = $this->dao->remove_category($this->data);
|
|
|
|
} else
|
|
|
|
$this->setUnknownTypeResponse();
|
|
|
|
if ($result == 0)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get()
|
|
|
|
{
|
|
|
|
$result = -1;
|
|
|
|
if ($this->type == "article") {
|
2020-02-11 15:12:31 +01:00
|
|
|
$result = $this->dao->get_articles();
|
2020-02-11 14:32:23 +01:00
|
|
|
} else if ($this->type == "category") {
|
|
|
|
$result = $this->dao->get_categories();
|
2020-02-12 11:44:29 +01:00
|
|
|
} else if ($this->type == "article_categories") {
|
|
|
|
$result = $this->dao->get_article_categories();
|
2020-02-11 14:32:23 +01:00
|
|
|
} else
|
|
|
|
$this->setUnknownTypeResponse();
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2020-02-20 12:20:42 +01:00
|
|
|
function updateStock()
|
|
|
|
{
|
2020-02-15 19:01:27 +01:00
|
|
|
$result = 0;
|
|
|
|
foreach ($this->data as $row) {
|
|
|
|
$value = $row["value"];
|
|
|
|
if ($this->action == "sell")
|
|
|
|
$value = -$value;
|
|
|
|
$result = $this->dao->update_article_stock($row["id"], $value);
|
|
|
|
if (!$result)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2020-02-11 14:32:23 +01:00
|
|
|
function setUnknownTypeResponse()
|
|
|
|
{
|
|
|
|
$this->responseArray["status"] = 1;
|
|
|
|
$this->responseArray["message"] = "Error: Unknown type";
|
|
|
|
}
|
|
|
|
|
|
|
|
function setUnknownActionResponse()
|
|
|
|
{
|
|
|
|
$this->responseArray["status"] = 2;
|
|
|
|
$this->responseArray["message"] = "Error: Unknown action";
|
|
|
|
}
|
|
|
|
|
|
|
|
function setUnknownDataResponse()
|
|
|
|
{
|
|
|
|
$this->responseArray["status"] = 3;
|
|
|
|
$this->responseArray["message"] = "Error: Unknown data";
|
|
|
|
}
|
|
|
|
|
|
|
|
function setProcessingErrorResponse()
|
|
|
|
{
|
|
|
|
$this->responseArray["status"] = 4;
|
|
|
|
$this->responseArray["message"] = "Error: Data processing error";
|
|
|
|
}
|
|
|
|
|
2020-09-09 15:27:39 +02:00
|
|
|
function setFileErrorResponse()
|
|
|
|
{
|
|
|
|
$this->responseArray["status"] = 5;
|
|
|
|
$this->responseArray["message"] = "Error: Could not open file";
|
|
|
|
}
|
|
|
|
|
2020-02-11 14:32:23 +01:00
|
|
|
private function get_action($inputData)
|
|
|
|
{
|
|
|
|
if (!in_array($inputData["action"], $this->valid_actions))
|
|
|
|
return "";
|
|
|
|
else
|
|
|
|
return $inputData["action"];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function get_type($inputData)
|
|
|
|
{
|
|
|
|
if (!in_array($inputData["type"], $this->valid_types))
|
|
|
|
return "";
|
|
|
|
else
|
|
|
|
return $inputData["type"];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function get_data($inputData)
|
|
|
|
{
|
|
|
|
if ($inputData["data"] == null)
|
|
|
|
return [];
|
|
|
|
else
|
|
|
|
return $inputData["data"];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|