From 49dd1529149482ab2c80fb6e9c7539ba4a2dd603 Mon Sep 17 00:00:00 2001 From: sctn4elk Date: Wed, 29 May 2024 14:47:51 -0500 Subject: [PATCH] Work on getting image from database --- Controller/API/BaseController.php | 6 +-- Controller/API/ImageController.php | 59 +++++++++++++++++++++++++++--- Model/CustomerModel.php | 4 ++ Model/Database.php | 6 +-- Model/ImageModel.php | 45 +++++++++++------------ 5 files changed, 85 insertions(+), 35 deletions(-) diff --git a/Controller/API/BaseController.php b/Controller/API/BaseController.php index 5d0efd6..b7900b0 100644 --- a/Controller/API/BaseController.php +++ b/Controller/API/BaseController.php @@ -45,7 +45,7 @@ class BaseController { /* * Set the error description when an unknown method is called */ - public function unprocessableRequestResponse($msg) + public function unprocessableRequestResponse($msg = "Unknown Error") { $response['status_code_header'] = 'HTTP/1.1 405 Method Not Supported'; $response['body'] = json_encode([ @@ -60,7 +60,7 @@ class BaseController { /* * Set the error description when the payload does not contain the required info */ - public function unprocessableEntityResponse($msg) + public function unprocessableEntityResponse($msg = "Unknown Error") { $response['status_code_header'] = 'HTTP/1.1 422 Unprocessable Payload'; $response['body'] = json_encode([ @@ -75,7 +75,7 @@ class BaseController { /* * Set the error description when the */ - public function notFoundResponse($msg) + public function notFoundResponse($msg = "Unknown Error") { $response['status_code_header'] = 'HTTP/1.1 404 Entity Not Found'; $response['body'] = null; diff --git a/Controller/API/ImageController.php b/Controller/API/ImageController.php index 28b9e6a..264df8d 100644 --- a/Controller/API/ImageController.php +++ b/Controller/API/ImageController.php @@ -31,6 +31,10 @@ class ImageController extends BaseController { $this->imageModel = new ImageModel(); switch($this->action) { + case "select": + $response = $this->selectImage(); + break; + case "upload": $response = $this->uploadImage(); break; @@ -69,18 +73,45 @@ class ImageController extends BaseController { } } + private function selectImage(){ + if ($this->checkRequestType('GET') == 'false') { + return $this->unprocessableRequestResponse("Request type is not GET"); + } + + $this->arrQueryStringParams = $this->getQueryStringParams(); + + if (isset($this->arrQueryStringParams['member_id'])) { + $response = $this->selectByIdAction(); + } else { + $this->imageModel->limit = 20; + + if (isset($this->arrQueryStringParams['limit'])) { + $this->imageModel->limit = $this->arrQueryStringParams['limit']; + } + $response = $this->imageModel->findAllImages(); + unset($this->imageModel->limit); + } + return $response; + } + + private function selectByIdAction(){ + if (isset($this->arrQueryStringParams['member_id'])) { + $this->imageModel->memberId = $this->arrQueryStringParams['member_id']; + $response = $this->imageModel->findImageByMemberId(); + unset($this->imageModel->memberId); + } else { + $response = $this->notFoundResponse("selectByIdAction"); + } + + return $response; + } + /* Upload the image and store on server as file */ private function uploadImage(){ if ($this->checkRequestType('POST') == 'false') { return $this->unprocessableRequestResponse(); } - /* - * array(1) { - ["MemberImageModel"]=> - string(240) "{"CustomerID":1,"ImagePath":"C:\\DEV\\CustomerRewardsAdminPortal\\Resources\\Images\\headshot.jpg","ImageName":"headshot.jpg","ImageType":"image/jpg","ImageBlob":{"Headers":[{"Key":"Content-Type","Value":["image/jpg"]}]},"SourceImage":null}" -} - */ // reads the raw POST data $jsonPayload = filter_input_array(INPUT_POST); $input = json_decode($jsonPayload['MemberImageModel'], TRUE); @@ -169,6 +200,22 @@ class ImageController extends BaseController { return $check; } + private function updateImage(){ + if ($this->checkRequestType('PUT') == 'false') { + return $this->unprocessableRequestResponse("Request type is not PUT"); + } + + return $this->unprocessableRequestResponse("updateImage is not implemented"); + } + + private function deleteImage(){ + if ($this->checkRequestType('DELETE') == 'false') { + return $this->unprocessableRequestResponse("Request type is not DELETE"); + } + + return $this->unprocessableRequestResponse("deleteImage is not implemented"); + } + private function validateImage(){ $validtion = false; if(in_array($this->imageModel->fileType, $this->imageModel->allowFileType)){ diff --git a/Model/CustomerModel.php b/Model/CustomerModel.php index 1bc8783..48a4db3 100644 --- a/Model/CustomerModel.php +++ b/Model/CustomerModel.php @@ -28,6 +28,10 @@ require_once PD . "/Model/ModelTraits.php"; class CustomerModel extends Database { use ModelTraits; + function __construct() { + parent::__construct(); + } + public function findAllCustomers() { return $this->processQuery("SELECT * FROM customer_view ORDER BY customer_id ASC LIMIT ?", ["i", $this->limit]); diff --git a/Model/Database.php b/Model/Database.php index 6ceff7a..085a1ef 100644 --- a/Model/Database.php +++ b/Model/Database.php @@ -70,13 +70,13 @@ class Database { return false; } - public function processImage($query = "", $imageModel) { + public function processImage($query, $imageModel) { try { - if($this->connection == null) + /*if($this->connection == null) { $this->connection = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE_NAME); - } + }*/ $data = $this->connection->real_escape_string($imageModel->imageBlob); $result = $this->connection->query($query."(".$imageModel->memberId.", '".$imageModel->imageType."', '".$data."')"); diff --git a/Model/ImageModel.php b/Model/ImageModel.php index cccc51f..d049d0d 100644 --- a/Model/ImageModel.php +++ b/Model/ImageModel.php @@ -35,6 +35,7 @@ class ImageModel extends Database { public $fileType; function __construct() { + parent::__construct(); $this->uploadTo = PD . "\\public\\images\\"; //$this->uploadTo = PD . '/public/images/'; $this->allowFileType = array('jpg','png','jpeg','gif'); @@ -43,31 +44,29 @@ class ImageModel extends Database { /* Process the uploaded image and store in database */ public function insertImage() { - //$escaped_string = mysql_real_escape_string($this->imageBlob); - //$escaped_string = addslashes($this->imageBlob); - //return var_dump($escaped_string); - //$sql = "INSERT INTO ImageStore(ImageId,Image) VALUES('$this->image_id','" . mysql_escape_string(file_get_contents($tmp_image)) . "')"; - - //$fp=addslashes(file_get_contents($_FILES['image']['tmp_name'])); //will store the image to fp - //$query = "CALL insert_loyalty_member_image_proc('{$fileName}','{$fp}');"; - - /* - * $data = mysql_real_escape_string(fread(fopen($form_data, "r"), filesize($form_data))); - - $result = mysql_query("INSERT INTO binary_data (description, bin_data, filename, filesize, filetype) ". - "VALUES ('$form_description', '$data', '$form_data_name', '$form_data_size', '$form_data_type')"); - - $id= mysql_insert_id(); - * - * UPDATE t - SET blob_col=LOAD_FILE('/tmp/picture') - WHERE id=1; - */ - //$query = "CALL insert_loyalty_member_image_proc (" . $this->memberId . ", '" . $escaped_string . "');"; $query = "CALL insert_loyalty_member_image_proc"; - //return var_dump($query); - $rowCount = $this->processImage($query, $this); return $rowCount; } + + public function findImageByMemberId() + { + return $this->processQuery("SELECT loyalty_value_id, + loyalty_member_id, + loyalty_attribute_id, + loyalty_value_mime_type + FROM toast_schema.loyalty_value_blob + WHERE loyalty_member_id = ?", ["i", $this->memberId]); + } + + public function findAllImages() + { + return $this->processQuery("SELECT + loyalty_value_id, + loyalty_member_id, + loyalty_attribute_id, + loyalty_value_mime_type + FROM toast_schema.loyalty_value_blob + ORDER BY loyalty_member_id ASC LIMIT ?", ["i", $this->limit]); + } }