From 795784f02e5144ea4158da3b2f83f95c08f568e3 Mon Sep 17 00:00:00 2001 From: Joey Kimsey Date: Thu, 30 Jan 2025 18:07:45 -0500 Subject: [PATCH] Improved Search Functionality --- Classes/Database.php | 21 ++++++++++++++++++++- Classes/DatabaseModel.php | 18 +++++++++++++++++- Functions/Check.php | 4 ++-- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/Classes/Database.php b/Classes/Database.php index 71e6e79..207ded7 100644 --- a/Classes/Database.php +++ b/Classes/Database.php @@ -637,10 +637,29 @@ class Database { return true; } - public function search( $table, $column, $param ) { + public function searchColumn( $table, $column, $param ) { return $this->action( 'SELECT *', $table, [$column, 'LIKE', '%' . $param . '%'] ); } + public function search( $table, $columns, $param ) { + if ( empty( $columns ) || ! is_array( $columns ) ) { + Debug::log( 'No columns provided for search' ); + return []; + } + + $conditions = []; + foreach ( $columns as $column ) { + $conditions[] = $column; + $conditions[] = 'LIKE'; + $conditions[] = '%' . $param . '%'; + $conditions[] = 'OR'; + } + array_pop( $conditions ); + + return $this->action( 'SELECT *', $table, $conditions ); + // return $this->action( 'SELECT ' . implode( ',', $columns ), $table, $conditions ); // need to find a way to casually make this the default.... + } + /** * Selects data from the database. * diff --git a/Classes/DatabaseModel.php b/Classes/DatabaseModel.php index 536fb21..d61561e 100644 --- a/Classes/DatabaseModel.php +++ b/Classes/DatabaseModel.php @@ -17,8 +17,9 @@ use TheTempusProject\Bedrock\Functions\Check; use TheTempusProject\Bedrock\Bedrock; class DatabaseModel extends Model { - public $databaseMatrix; public $tableName; + public $databaseMatrix; + public $searchFields; public function __construct() { parent::__construct(); @@ -190,4 +191,19 @@ class DatabaseModel extends Model { } return $this->filter( $data->results() ); } + + public function search($param) { + if (empty($this->searchFields)) { + Debug::log('searchFields is empty'); + return []; + } + + $result = self::$db->search($this->tableName, $this->searchFields, $param); + + if ( $result->count() ) { + return $this->filter( $result->results() ); + } + + return []; + } } diff --git a/Functions/Check.php b/Functions/Check.php index 184b170..cb61c10 100644 --- a/Functions/Check.php +++ b/Functions/Check.php @@ -99,11 +99,11 @@ class Check { * @return {bool} */ public static function imageUpload( $imageName ) { - if ( !Config::getValue( 'uploads/images' ) ) { + if ( ! Config::getValue( 'uploads/images' ) ) { self::addUserError( 'Image uploads are disabled.' ); return false; } - if ( !isset( $_FILES[$imageName] ) ) { + if ( ! isset( $_FILES[ $imageName ] ) ) { self::addUserError( 'File not found.', $imageName ); return false; }