194 lines
5.7 KiB
PHP
194 lines
5.7 KiB
PHP
<?php
|
|
/**
|
|
* core/database_model.php
|
|
*
|
|
* The class provides some basic functionality for models that interact
|
|
* with the database.
|
|
*
|
|
* @version 3.0
|
|
* @author Joey Kimsey <Joey@thetempusproject.com>
|
|
* @link https://TheTempusProject.com/Core
|
|
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
|
*/
|
|
namespace TheTempusProject\Bedrock\Classes;
|
|
|
|
use TheTempusProject\Canary\Bin\Canary as Debug;
|
|
use TheTempusProject\Bedrock\Functions\Check;
|
|
use TheTempusProject\Bedrock\Bedrock;
|
|
|
|
class DatabaseModel extends Model {
|
|
public $databaseMatrix;
|
|
public $tableName;
|
|
|
|
public function __construct() {
|
|
parent::__construct();
|
|
}
|
|
|
|
public function enabled() {
|
|
if ( empty( $this->enabled ) ) {
|
|
$this->enabled = self::$db->enabled();
|
|
}
|
|
return $this->enabled;
|
|
}
|
|
|
|
public function filter( $data, $params = [] ) {
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* This method will remove all the installed model components.
|
|
*
|
|
* @return bool - If the uninstall was completed without error
|
|
*/
|
|
public function uninstall() {
|
|
parent::uninstall();
|
|
$this->uninstallTable();
|
|
return true;
|
|
}
|
|
|
|
public function rowMatrixToArray( $row_matrix ) {
|
|
$row_array = [];
|
|
$row_array['row_name'] = array_shift( $row_matrix );
|
|
$row_array['row_type'] = array_shift( $row_matrix );
|
|
$row_array['length'] = array_shift( $row_matrix );
|
|
if ( !empty( $row_matrix ) ) {
|
|
$row_array['is_null'] = array_shift( $row_matrix );
|
|
} else {
|
|
$row_array['is_null'] = true;
|
|
}
|
|
if ( !empty( $row_matrix ) ) {
|
|
$row_array['default_value'] = array_shift( $row_matrix );
|
|
} else {
|
|
$row_array['default_value'] = null;
|
|
}
|
|
if ( !empty( $row_matrix ) ) {
|
|
$row_array['comment'] = array_shift( $row_matrix );
|
|
} else {
|
|
$row_array['comment'] = '';
|
|
}
|
|
return $row_array;
|
|
}
|
|
|
|
/**
|
|
* Install db tables needed for the model.
|
|
*
|
|
* @return {bool}
|
|
*/
|
|
public function installTable() {
|
|
// should have some sort of DELTA functionality and safeguards
|
|
if ( empty( $this->tableName ) || empty( $this->databaseMatrix )) {
|
|
Debug::log( 'databaseMatrix is empty' );
|
|
return true;
|
|
}
|
|
if ( false === self::$db->newTable( $this->tableName ) ) {
|
|
return false;
|
|
}
|
|
Debug::log( 'adding a new table named: ' . $this->tableName );
|
|
foreach ( $this->databaseMatrix as $key => $row_matrix ) {
|
|
$row = $this->rowMatrixToArray( $row_matrix );
|
|
self::$db->addField(
|
|
$row['row_name'],
|
|
$row['row_type'],
|
|
$row['length'],
|
|
$row['is_null'],
|
|
$row['default_value'],
|
|
$row['comment'],
|
|
);
|
|
}
|
|
self::$db->createTable();
|
|
return self::$db->getStatus();
|
|
}
|
|
|
|
public function uninstallTable() {
|
|
if ( empty( $this->tableName ) ) {
|
|
return;
|
|
}
|
|
return self::$db->removeTable( $this->tableName );
|
|
}
|
|
|
|
/**
|
|
* Retrieves a row by its ID and filters it.
|
|
*
|
|
* @param {int} [$id]
|
|
* @return {object} - The filtered db entry.
|
|
*/
|
|
public function findById( $id ) {
|
|
if ( !Check::id( $id ) ) {
|
|
Debug::warn( "$this->tableName fingByID: illegal ID: $id" );
|
|
return false;
|
|
}
|
|
$data = self::$db->get( $this->tableName, ['ID', '=', $id] );
|
|
if ( !$data->count() ) {
|
|
Debug::info( 'No ' . $this->tableName . ' data found.' );
|
|
return false;
|
|
}
|
|
return $this->filter( $data->first() );
|
|
}
|
|
|
|
/**
|
|
* Function to delete the specified entry.
|
|
*
|
|
* @param int|array $ID the log ID or array of ID's to be deleted
|
|
* @return bool
|
|
*/
|
|
public function delete( $idArray ) {
|
|
if ( !is_array( $idArray ) ) {
|
|
$idArray = [ $idArray ];
|
|
}
|
|
foreach ( $idArray as $id ) {
|
|
if ( !Check::id( $id ) ) {
|
|
Debug::info( "invalid ID: $id." );
|
|
$error = true;
|
|
continue;
|
|
}
|
|
if ( self::$db->delete( $this->tableName, [ 'ID', '=', $id ] )->error() ) {
|
|
Debug::info( "Error Deleting id: $id" );
|
|
$error = true;
|
|
continue;
|
|
}
|
|
Debug::info( $this->tableName . " deleted: $id" );
|
|
}
|
|
if ( !empty( $error ) ) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Function to clear entries of a defined type.
|
|
*
|
|
* @todo this is probably dumb
|
|
* @param string $data - The log type to be cleared
|
|
* @return bool
|
|
*/
|
|
public function empty() {
|
|
self::$db->delete( $this->tableName, ['ID', '>=', '0'] );
|
|
Debug::info( $this->tableName . ' Cleared' );
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* retrieves a list of paginated (limited) results.
|
|
*
|
|
* @param array $filter - A filter to be applied to the list.
|
|
* @return bool|object - Depending on success.
|
|
*/
|
|
public function listPaginated( $filter = null ) {
|
|
$data = self::$db->getPaginated( $this->tableName, '*' );
|
|
if ( !$data->count() ) {
|
|
Debug::info( $this->tableName . ' - No entries found' );
|
|
return false;
|
|
}
|
|
return $this->filter( $data->results() );
|
|
}
|
|
|
|
public function list( $filter = null ) {
|
|
$data = self::$db->get( $this->tableName, '*' );
|
|
if ( !$data->count() ) {
|
|
Debug::info( $this->tableName . ' - No entries found' );
|
|
return false;
|
|
}
|
|
return $this->filter( $data->results() );
|
|
}
|
|
}
|