More work on image processing

This commit is contained in:
sctn4elk 2024-05-09 20:25:56 -05:00
parent 97207ea536
commit 79ad9030bc
7 changed files with 163 additions and 188 deletions

View File

@ -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.
*

View File

@ -1,9 +1,5 @@
<?php
header("Content-Type: application/json");
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
*/
/**
* Description of CustomerController
@ -17,11 +13,6 @@ class CustomerController extends BaseController {
* "/customer/list" Endpoint - Get list of users
*/
private $customerModel;
private $requestMethod;
private $arrQueryStringParams;
private $strErrorDesc;
private $strErrorHeader;
public $action;
function __construct() {
@ -183,15 +174,6 @@ class CustomerController extends BaseController {
return $response;
}
private function checkRequestType($request)
{
$response = 'false';
if (strtoupper($this->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;
}
}

View File

@ -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() {
@ -80,13 +75,25 @@ class ImageController extends BaseController {
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;
}
}

View File

@ -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()
{

View File

@ -1,10 +1,4 @@
<?php
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
*/
/**
* Description of Database
*
@ -67,9 +61,8 @@ class Database {
public function processStatement($query = "")
{
try {
//return var_dump($query);
//Prepare the statement
$stmt = $this->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;
}
}

View File

@ -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]] . "', ";
}
$query .= $inputModel[$keys[$i]] . ")";
//return var_dump($query);
$trace = debug_backtrace();
trigger_error(
'Undefined property via __get(): ' . $name .
' in ' . $trace[0]['file'] .
' on line ' . $trace[0]['line'],
E_USER_NOTICE);
return null;
$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]);
}
}

54
Model/ModelTraits.php Normal file
View File

@ -0,0 +1,54 @@
<?php
/**
* Description of ModelTraits
*
* @author SCTN4
*/
Trait ModelTraits {
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]);
}
}