Site-Proximo/classes/postHandler.php

157 lines
3.9 KiB
PHP

<?php
require_once 'dao.php';
class PostHandler
{
private $valid_types = ["article", "category"];
private $valid_actions = ["create", "update", "remove", "get"];
private $action;
private $type;
private $postData;
private $filesData;
private $data;
private $dao;
private $uploadBaseDir = '../../uploaded_images/';
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;
if (count($this->data) > 0) {
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;
}
function create()
{
$result = -1;
if ($this->type == "article") {
} else if ($this->type == "category") {
$result = $this->dao->create_category($this->data);
} else
$this->setUnknownTypeResponse();
return $result;
}
function update()
{
$result = -1;
if ($this->type == "article") {
} 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") {
} 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") {
} else if ($this->type == "category") {
$result = $this->dao->get_categories();
} else
$this->setUnknownTypeResponse();
return $result;
}
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";
}
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"];
}
}