mirror of
https://github.com/sctn4elk/CustomerRewardsRESTAPI.git
synced 2025-01-09 23:34:30 -06:00
170 lines
5.4 KiB
PHP
170 lines
5.4 KiB
PHP
|
<?php
|
||
|
header("Content-Type: application/json");
|
||
|
|
||
|
/**
|
||
|
* Description of ImageController
|
||
|
*
|
||
|
* @author SCTN4
|
||
|
*/
|
||
|
class ImageController extends BaseController {
|
||
|
|
||
|
private $imageModel;
|
||
|
private $requestMethod;
|
||
|
private $arrQueryStringParams;
|
||
|
private $strErrorDesc;
|
||
|
private $strErrorHeader;
|
||
|
|
||
|
public $action;
|
||
|
|
||
|
function __construct() {
|
||
|
$this->imageModel = new ImageModel();
|
||
|
$this->requestMethod = $this->getServerRequestMethod();
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* http://localhost/CustomerRewardsRESTAPI/index.php/image/process/upload
|
||
|
*/
|
||
|
public function processAction() {
|
||
|
$this->strErrorDesc = '';
|
||
|
$this->strErrorHeader = '';
|
||
|
try {
|
||
|
switch($this->action) {
|
||
|
case "upload":
|
||
|
$response = $this->uploadImage();
|
||
|
break;
|
||
|
|
||
|
case "update":
|
||
|
$response = $this->updateImage();
|
||
|
break;
|
||
|
|
||
|
case "delete":
|
||
|
$response = $this->deleteImage();
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
$response = (object) ['Result' => 'Default'];
|
||
|
$this->strErrorDesc = 'Controller Method not supported for processAction: ' . $this->action;
|
||
|
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
$responseData = json_encode($response);
|
||
|
} catch (Error $e) {
|
||
|
$this->strErrorDesc = $e->getMessage().' Something went wrong in processAction! Please contact support.';
|
||
|
$this->strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
|
||
|
}
|
||
|
|
||
|
// send output
|
||
|
if (!$this->strErrorDesc) {
|
||
|
$this->sendOutput(
|
||
|
$responseData,
|
||
|
array('Content-Type: application/json', 'HTTP/1.1 200 OK')
|
||
|
);
|
||
|
} else {
|
||
|
$this->sendOutput(json_encode(array('error' => $this->strErrorDesc)),
|
||
|
array('Content-Type: application/json', $this->strErrorHeader)
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private function uploadImage(){
|
||
|
if ($this->checkRequestType('POST') == 'false') {
|
||
|
$this->strErrorDesc = 'Request Method not supported for processAction';
|
||
|
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request';
|
||
|
return;
|
||
|
}
|
||
|
// reads the raw POST data and returns it as a string.
|
||
|
$jsonPayload = file_get_contents('php://input');
|
||
|
$input = json_decode($jsonPayload, TRUE);
|
||
|
if (! $this->validatePerson($input)) {
|
||
|
return $this->unprocessableEntityResponse();
|
||
|
}
|
||
|
|
||
|
//remove customer_id field so it doesn't break
|
||
|
unset($input['customer_id']);
|
||
|
|
||
|
$response = $this->customerModel->insertCustomer($input);
|
||
|
return $response;
|
||
|
}
|
||
|
|
||
|
private function processImage() {
|
||
|
$error = false;
|
||
|
$msg = null;
|
||
|
|
||
|
$uploadTo = "public/images/";
|
||
|
$allowFileType = array('jpg','png','jpeg');
|
||
|
$fileName = $_FILES['image']['name'];
|
||
|
$tempPath = $_FILES["image"]["tmp_name"];
|
||
|
|
||
|
$basename = basename($fileName);
|
||
|
$originalPath = $uploadTo.$basename;
|
||
|
$fileType = pathinfo($originalPath, PATHINFO_EXTENSION);
|
||
|
|
||
|
if(!empty($fileName)){
|
||
|
if(in_array($fileType, $allowFileType)){
|
||
|
if(!move_uploaded_file($tempPath, $originalPath)){
|
||
|
$msg = 'Image Not uploaded ! try again';
|
||
|
$error = true;
|
||
|
}
|
||
|
} else {
|
||
|
$msg = 'Image type is not allowed';
|
||
|
$error = true;
|
||
|
}
|
||
|
} else {
|
||
|
$msg = 'Image is required';
|
||
|
$error = true;
|
||
|
}
|
||
|
|
||
|
$imageInfo = [
|
||
|
"error" => $error,
|
||
|
"msg" => $msg,
|
||
|
"filename" => $fileName,
|
||
|
"filepath" => $originalPath
|
||
|
];
|
||
|
return $imageInfo;
|
||
|
}
|
||
|
|
||
|
private function insert() {
|
||
|
header("Access-Control-Allow-Origin: *");
|
||
|
header("Access-Control-Allow-Methods: POST");
|
||
|
header("Content-Type: application/json");
|
||
|
|
||
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||
|
if (isset($_FILES['image'])) {
|
||
|
$uploadImage = $this->processImage();
|
||
|
$success = false;
|
||
|
|
||
|
if (!$uploadImage['error']) {
|
||
|
// table name for admin profiles
|
||
|
$query = "INSERT INTO " . $this->imageTable;
|
||
|
$query .= " (filename, filepath) VALUES (?,?)";
|
||
|
$stmt = $this->conn->prepare($query);
|
||
|
|
||
|
$stmt->bind_param("ss", $uploadImage['filename'], $uploadImage['filepath']);
|
||
|
|
||
|
if ($stmt->execute()) {
|
||
|
$success = true;
|
||
|
$stmt->close();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$data = [
|
||
|
'Errormsg' => $uploadImage['msg'] ?? '',
|
||
|
'success' => $success
|
||
|
];
|
||
|
|
||
|
return json_encode($data);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private function notFoundResponse()
|
||
|
{
|
||
|
$response['status_code_header'] = 'HTTP/1.1 404 Entity Not Found';
|
||
|
$response['body'] = null;
|
||
|
$this->strErrorDesc = 'Request Entity Not Found';
|
||
|
$this->strErrorHeader = 'HTTP/1.1 422 Entity Not Found';
|
||
|
return $response;
|
||
|
}
|
||
|
}
|