* @link https://TableTopElite.com */ namespace TheTempusProject\Models; use TheTempusProject\Canary\Bin\Canary as Debug; use TheTempusProject\Classes\DatabaseModel; use TheTempusProject\TheTempusProject as App; use TheTempusProject\Bedrock\Classes\CustomException; class Sourcebooks extends DatabaseModel { public $tableName = 'dnd_sourcebooks'; public $databaseMatrix = [ // Name [ 'name', 'varchar', '32' ], // General [ 'createdBy', 'int', '11' ], [ 'createdAt', 'int', '11' ], [ 'privacy', 'varchar', '16' ], [ 'version', 'varchar', '5' ], [ 'shortDescription', 'text', '' ], [ 'description', 'text', '' ], [ 'image', 'text', '' ], // Core [ 'type', 'varchar', '5' ], [ 'publish_year', 'int', '4' ], [ 'amazon_url', 'text', '' ], ]; /** * The model constructor. */ public function __construct() { parent::__construct(); } public function create( $name, $privacy, $version, $type = '', $publishYear = '', $amazonUrl = '', $description = '', $shortDescription = '' ) { $fields = [ 'name' => $name, 'privacy' => $privacy, 'version' => $version, 'type' => $type, 'description' => $description, 'shortDescription' => $shortDescription, 'publish_year' => $publishYear, 'amazon_url' => $amazonUrl, 'createdBy' => App::$activeUser->ID, 'createdAt' => time(), ]; if ( ! self::$db->insert( $this->tableName, $fields ) ) { new CustomException( 'Create' ); Debug::error( "SourceBook: not created " . var_export($fields,true) ); return false; } return self::$db->lastId(); } public function update( $id, $name, $privacy, $version, $type = '', $publishYear = '', $amazonUrl = '', $description = '', $shortDescription = '' ) { $fields = [ 'name' => $name, 'privacy' => $privacy, 'version' => $version, 'type' => $type, 'description' => $description, 'shortDescription' => $shortDescription, 'publish_year' => $publishYear, 'amazon_url' => $amazonUrl, ]; if ( !self::$db->update( $this->tableName, $id, $fields ) ) { new CustomException( 'Update' ); Debug::error( "SourceBook: $id not updated: $fields" ); return false; } return true; } public function paginatedList() { $whereClause = ['createdBy', '=', App::$activeUser->ID]; $objects = self::$db->getPaginated( $this->tableName, $whereClause ); if ( !$objects->count() ) { Debug::info( 'No SourceBooks found.' ); return false; } return $this->filter( $objects->results() ); } 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 SourceBooks found.' ); return false; } return $this->filter( $objects->results() ); } public function simpleList() { $whereClause = ['createdBy', '=', App::$activeUser->ID]; $objects = self::$db->get( $this->tableName, $whereClause ); if ( ! $objects->count() ) { Debug::warn( 'No Languages found.' ); return false; } $objects = $objects->results(); $out = []; foreach ( $objects as &$object ) { $out[ $object->name ] = $object->ID; } return $out; } } // origins // Player's Handbook / Basic Rules // The main Rulebook for D&D. // Elemental Evil Player's Companion // Volo's Guide to Monsters // Mordenkainen Presents: Monsters of the Multiverse // Eberron: Rising from the Last War // Fizban’s Treasury of Dragons // Guildmaster’s Guide to Ravnica // Locathah Rising // Mordenkainen’s Tome of Foes // Mythic Odysseys of Theros // One Grung Above // The Tortle Package // The Wild Beyond the Witchlight // Dragonlance: Shadow of the Dragon Queen // Spelljammer: Adventures in Space // Strixhaven: Curriculum of Chaos // Van Richten's Guide to Ravenloft // Tasha's Cauldron of Everything // Grim Hollow: Player Pack // Humblewood Campaign Setting // Acquisitions Incorporated // Sword Coast Adventurer's Guide