* @link https://TableTopElite.com */ namespace TheTempusProject\Models; use TheTempusProject\Canary\Canary as Debug; use TheTempusProject\Classes\DatabaseModel; use TheTempusProject\TheTempusProject as App; use TheTempusProject\Bedrock\Classes\CustomException; use TheTempusProject\Models\Sourcebooks as SourcebooksModel; class Traits extends DatabaseModel { protected static $sourcebooks; public $tableName = 'dnd_racial_traits'; public $databaseMatrix = [ // Core [ 'name', 'varchar', '64' ], [ 'description', 'text', '' ], [ 'shortDescription', 'text', '' ], // General [ 'createdBy', 'int', '11' ], [ 'createdAt', 'int', '11' ], [ 'privacy', 'varchar', '16' ], [ 'version', 'varchar', '5' ], [ 'sourcebookID', 'int', '11' ], [ 'image', 'text', '' ], ]; /** * The model constructor. */ public function __construct() { parent::__construct(); self::$sourcebooks = new SourcebooksModel; } public function create( $name, $privacy, $version, $sourcebookID, $description = '', $shortDescription = '' ) { $fields = [ 'name' => $name, 'privacy' => $privacy, 'version' => $version, 'sourcebookID' => $sourcebookID, 'description' => $description, 'shortDescription' => $shortDescription, 'createdBy' => App::$activeUser->ID, 'createdAt' => time(), ]; if ( ! self::$db->insert( $this->tableName, $fields ) ) { new CustomException( 'Create' ); Debug::error( "Traits: not created " . var_export($fields,true) ); return false; } return self::$db->lastId(); } public function update( $id, $name, $privacy, $version, $sourcebookID, $description = '', $shortDescription = '' ) { $fields = [ 'name' => $name, 'privacy' => $privacy, 'version' => $version, 'sourcebookID' => $sourcebookID, 'description' => $description, 'shortDescription' => $shortDescription, ]; if ( !self::$db->update( $this->tableName, $id, $fields ) ) { new CustomException( 'Update' ); Debug::error( "Traits: $id not updated: $fields" ); return false; } return true; } public function byUser( $limit = null ) { $whereClause = ['createdBy', '=', App::$activeUser->ID]; if ( empty( $limit ) ) { $objects = self::$db->get( $this->tableName, $whereClause ); } else { $objects = self::$db->get( $this->tableName, $whereClause, 'ID', 'DESC', [0, $limit] ); } if ( !$objects->count() ) { Debug::info( 'No Traits found.' ); return false; } return $this->filter( $objects->results() ); } public function bySystem( $limit = null ) { $whereClause = ['createdBy', '=', '0']; if ( empty( $limit ) ) { $objects = self::$db->get( $this->tableName, $whereClause ); } else { $objects = self::$db->get( $this->tableName, $whereClause, 'ID', 'DESC', [0, $limit] ); } if ( !$objects->count() ) { Debug::info( 'No Traits found.' ); return false; } return $this->filter( $objects->results() ); } public function filter( $data, $params = [] ) { $out = []; foreach ( $data as $instance ) { if ( !is_object( $instance ) ) { $instance = $data; $end = true; } $sourcebook = self::$sourcebooks->findById( $instance->sourcebookID ); if ( !empty( $sourcebook ) ) { $instance->sourcebookName = $sourcebook->name; } else { $instance->sourcebookName = 'Unknown'; } $out[] = $instance; if ( !empty( $end ) ) { $out = $out[0]; break; } } return $out; } }