Improved Search Functionality

This commit is contained in:
Joey Kimsey
2025-01-30 18:07:45 -05:00
parent b1e74f9652
commit 795784f02e
3 changed files with 39 additions and 4 deletions

View File

@ -637,10 +637,29 @@ class Database {
return true; return true;
} }
public function search( $table, $column, $param ) { public function searchColumn( $table, $column, $param ) {
return $this->action( 'SELECT *', $table, [$column, 'LIKE', '%' . $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. * Selects data from the database.
* *

View File

@ -17,8 +17,9 @@ use TheTempusProject\Bedrock\Functions\Check;
use TheTempusProject\Bedrock\Bedrock; use TheTempusProject\Bedrock\Bedrock;
class DatabaseModel extends Model { class DatabaseModel extends Model {
public $databaseMatrix;
public $tableName; public $tableName;
public $databaseMatrix;
public $searchFields;
public function __construct() { public function __construct() {
parent::__construct(); parent::__construct();
@ -190,4 +191,19 @@ class DatabaseModel extends Model {
} }
return $this->filter( $data->results() ); 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 [];
}
} }

View File

@ -99,11 +99,11 @@ class Check {
* @return {bool} * @return {bool}
*/ */
public static function imageUpload( $imageName ) { public static function imageUpload( $imageName ) {
if ( !Config::getValue( 'uploads/images' ) ) { if ( ! Config::getValue( 'uploads/images' ) ) {
self::addUserError( 'Image uploads are disabled.' ); self::addUserError( 'Image uploads are disabled.' );
return false; return false;
} }
if ( !isset( $_FILES[$imageName] ) ) { if ( ! isset( $_FILES[ $imageName ] ) ) {
self::addUserError( 'File not found.', $imageName ); self::addUserError( 'File not found.', $imageName );
return false; return false;
} }