From 79ad9030bce8801506b1c3ad39e25a5b1dd3d884 Mon Sep 17 00:00:00 2001 From: sctn4elk Date: Thu, 9 May 2024 20:25:56 -0500 Subject: [PATCH] More work on image processing --- Controller/API/BaseController.php | 38 ++++++++++++++- Controller/API/CustomerController.php | 40 +--------------- Controller/API/ImageController.php | 68 +++++++-------------------- Model/CustomerModel.php | 46 +----------------- Model/Database.php | 37 +++++++++++---- Model/ImageModel.php | 68 ++++++++++----------------- Model/ModelTraits.php | 54 +++++++++++++++++++++ 7 files changed, 163 insertions(+), 188 deletions(-) create mode 100644 Model/ModelTraits.php diff --git a/Controller/API/BaseController.php b/Controller/API/BaseController.php index 94314da..784baf5 100644 --- a/Controller/API/BaseController.php +++ b/Controller/API/BaseController.php @@ -6,10 +6,14 @@ * @author Mike Howard */ class BaseController { - public $name; + public $basename; + public $requestMethod; + public $arrQueryStringParams; + public $strErrorDesc; + public $strErrorHeader; public function __construct() { - $this->name = 'BaseController'; + $this->basename = 'BaseController'; } public static function create() { return new self(); } @@ -21,6 +25,36 @@ class BaseController { { $this->sendOutput('', array('HTTP/1.1 404 Non-Existant method or inaccessible method called')); } + + public function checkRequestType($request) + { + $response = 'false'; + if (strtoupper($this->requestMethod) == $request) { + $response = 'true'; + } + return $response; + } + + public function unprocessableEntityResponse() + { + $response['status_code_header'] = 'HTTP/1.1 422 Unprocessable Payload'; + $response['body'] = json_encode([ + 'error' => 'Invalid input' + ]); + $this->strErrorDesc = 'Unprocessable Payload'; + $this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Payload'; + return $response; + } + + public 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; + } + /** * Get URI elements. * diff --git a/Controller/API/CustomerController.php b/Controller/API/CustomerController.php index 354e085..8483b4c 100644 --- a/Controller/API/CustomerController.php +++ b/Controller/API/CustomerController.php @@ -1,9 +1,5 @@ requestMethod) == $request) { - $response = 'true'; - } - return $response; - } - private function validatePerson($input) { $validtion = false; @@ -203,24 +185,4 @@ class CustomerController extends BaseController { } return $validtion; } - - private function unprocessableEntityResponse() - { - $response['status_code_header'] = 'HTTP/1.1 422 Unprocessable Payload'; - $response['body'] = json_encode([ - 'error' => 'Invalid input' - ]); - $this->strErrorDesc = 'Unprocessable Payload'; - $this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Payload'; - return $response; - } - - 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; - } } diff --git a/Controller/API/ImageController.php b/Controller/API/ImageController.php index 45814fd..e57d78a 100644 --- a/Controller/API/ImageController.php +++ b/Controller/API/ImageController.php @@ -9,11 +9,6 @@ header("Content-Type: application/json"); class ImageController extends BaseController { private $imageModel; - private $requestMethod; - private $arrQueryStringParams; - private $strErrorDesc; - private $strErrorHeader; - public $action; function __construct() { @@ -79,14 +74,26 @@ class ImageController extends BaseController { 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); + $response = $this->insertImage(); return $response; } + private function insertImage() { + header("Access-Control-Allow-Origin: *"); + header("Access-Control-Allow-Methods: POST"); + header("Content-Type: application/json"); + + if (isset($_FILES['image'])) { + $uploadImage = $this->processImage(); + $success = false; + + if (!$uploadImage['error']) { + + } + } + } + private function processImage() { $error = false; $msg = null; @@ -123,47 +130,4 @@ class ImageController extends BaseController { ]; 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; - } } diff --git a/Model/CustomerModel.php b/Model/CustomerModel.php index 275a170..ecbff70 100644 --- a/Model/CustomerModel.php +++ b/Model/CustomerModel.php @@ -30,51 +30,7 @@ require_once PROJECT_ROOT_PATH . "/Model/Database.php"; class CustomerModel extends Database { - private $params = array(); - - /* - * @assert ('name', 'value') - */ - public function __set($name, $value) - { - //echo "Setting '$name' to '$value'\n"; - $this->params[$name] = $value; - } - - /* - * @assert ('name') == 'value' - */ - public function __get($name) - { - //echo "Getting '$name'\n"; - if (array_key_exists($name, $this->params)) { - return $this->params[$name]; - } - - $trace = debug_backtrace(); - trigger_error( - 'Undefined property via __get(): ' . $name . - ' in ' . $trace[0]['file'] . - ' on line ' . $trace[0]['line'], - E_USER_NOTICE); - return null; - } - - /* - * @assert ('name') == 'true' - * @assert ('test') == 'false' - */ - public function __isset($name) - { - //echo "Is '$name' set?\n"; - return isset($this->params[$name]); - } - - public function __unset($name) - { - //echo "Unsetting '$name'\n"; - unset($this->params[$name]); - } + use ModelTraits; public function findAllCustomers() { diff --git a/Model/Database.php b/Model/Database.php index 8a3d329..2f262fe 100644 --- a/Model/Database.php +++ b/Model/Database.php @@ -1,10 +1,4 @@ connection->stmt_init(); + //$stmt = $this->connection->stmt_init(); $stmt = $this->connection->prepare($query); if($stmt === false) { throw New Exception("Unable to prepare the statement: " . $query); @@ -93,4 +86,32 @@ class Database { } return false; } + + public function processImage($query = "" , $params = []) { + try { + $stmt = $this->connection->prepare($query); + if($stmt === false) { + throw New Exception("Unable to prepare the statement: " . $query); + } + + $stmt->bind_param("ss", $params['filename'], $params['filepath']); + + $result = $stmt->execute(); + if($result === false) { + throw New Exception("Unable to execute the statement: " . $query); + } + + $rowCount = $this->connection->affected_rows; + if($rowCount < 1) + { + throw New Exception("Statement did not return any rows: " . $query); + } + + $stmt->close(); + return $rowCount; + } catch(Exception $e) { + throw New Exception( $e->getMessage() ); + } + return false; + } } diff --git a/Model/ImageModel.php b/Model/ImageModel.php index e4daf9c..a20baba 100644 --- a/Model/ImageModel.php +++ b/Model/ImageModel.php @@ -11,51 +11,35 @@ * @author SCTN4 */ class ImageModel extends Database { + use ModelTraits; - private $params = array(); - /* - * @assert ('name', 'value') - */ - public function __set($name, $value) + public function insertImage($inputModel) { - //echo "Setting '$name' to '$value'\n"; - $this->params[$name] = $value; - } + // table name for admin profiles + $query = "INSERT INTO " . $this->imageTable; + $query .= " (filename, filepath) VALUES (?,?)"; + + } - /* - * @assert ('name') == 'value' - */ - public function __get($name) - { - //echo "Getting '$name'\n"; - if (array_key_exists($name, $this->params)) { - return $this->params[$name]; + $data = [ + 'Errormsg' => $uploadImage['msg'] ?? '', + 'success' => $success + ]; + + return json_encode($data); + + //return var_dump($jsonPayLoad); + $keys = array_keys($inputModel); + $n = count($keys); + + $query .= "CALL insert_new_customer_proc ("; + for($i = 0; $i < $n-1; $i++) { + $query .= "'" . $inputModel[$keys[$i]] . "', "; } - - $trace = debug_backtrace(); - trigger_error( - 'Undefined property via __get(): ' . $name . - ' in ' . $trace[0]['file'] . - ' on line ' . $trace[0]['line'], - E_USER_NOTICE); - return null; + $query .= $inputModel[$keys[$i]] . ")"; + //return var_dump($query); + + $rowCount = $this->processImage($query); + return $rowCount; } - - /* - * @assert ('name') == 'true' - * @assert ('test') == 'false' - */ - public function __isset($name) - { - //echo "Is '$name' set?\n"; - return isset($this->params[$name]); - } - - public function __unset($name) - { - //echo "Unsetting '$name'\n"; - unset($this->params[$name]); - } - - } diff --git a/Model/ModelTraits.php b/Model/ModelTraits.php new file mode 100644 index 0000000..d94d852 --- /dev/null +++ b/Model/ModelTraits.php @@ -0,0 +1,54 @@ +params[$name] = $value; + } + + /* + * @assert ('name') == 'value' + */ + public function __get($name) + { + //echo "Getting '$name'\n"; + if (array_key_exists($name, $this->params)) { + return $this->params[$name]; + } + + $trace = debug_backtrace(); + trigger_error( + 'Undefined property via __get(): ' . $name . + ' in ' . $trace[0]['file'] . + ' on line ' . $trace[0]['line'], + E_USER_NOTICE); + return null; + } + + /* + * @assert ('name') == 'true' + * @assert ('test') == 'false' + */ + public function __isset($name) + { + //echo "Is '$name' set?\n"; + return isset($this->params[$name]); + } + + public function __unset($name) + { + //echo "Unsetting '$name'\n"; + unset($this->params[$name]); + } +}