* @link https://TheTempusProject.com * @license https://opensource.org/licenses/MIT [MIT LICENSE] */ namespace TheTempusProject\Models; use TheTempusProject\Bedrock\Classes\Config; use TheTempusProject\Bedrock\Functions\Check; use TheTempusProject\Canary\Canary as Debug; use TheTempusProject\Classes\DatabaseModel; use TheTempusProject\TheTempusProject as App; use TheTempusProject\Houdini\Classes\Filters; use TheTempusProject\Bedrock\Classes\CustomException; class BookmarkViews extends DatabaseModel { public $tableName = 'bookmark_views'; public $databaseMatrix = [ [ 'title', 'varchar', '256' ], [ 'description', 'text', '' ], [ 'privacy', 'varchar', '48' ], [ 'createdBy', 'int', '11' ], [ 'createdAt', 'int', '11' ], [ 'updatedAt', 'int', '11' ], ]; /** * The model constructor. */ public function __construct() { parent::__construct(); } public function create( $title, $description = '', $privacy = 'private' ) { if ( ! Check::dataTitle( $title ) ) { Debug::info( 'Views: illegal title.' ); return false; } $fields = [ 'title' => $title, 'description' => $description, 'privacy' => $privacy, 'createdBy' => App::$activeUser->ID, 'createdAt' => time(), ]; if ( ! self::$db->insert( $this->tableName, $fields ) ) { new CustomException( 'viewCreate' ); Debug::error( "Views: not created " . var_export($fields,true) ); return false; } return self::$db->lastId(); } public function update( $id, $title, $description = '', $privacy = 'private' ) { if ( !Check::id( $id ) ) { Debug::info( 'Views: illegal ID.' ); return false; } if ( !Check::dataTitle( $title ) ) { Debug::info( 'Views: illegal title.' ); return false; } $fields = [ 'title' => $title, 'description' => $description, 'privacy' => $privacy, ]; if ( !self::$db->update( $this->tableName, $id, $fields ) ) { new CustomException( 'viewUpdate' ); Debug::error( "Views: $id not updated: $fields" ); return false; } return true; } public function byUser( $limit = null ) { $whereClause = ['createdBy', '=', App::$activeUser->ID]; if ( empty( $limit ) ) { $views = self::$db->get( $this->tableName, $whereClause ); } else { $views = self::$db->get( $this->tableName, $whereClause, 'ID', 'DESC', [0, $limit] ); } if ( !$views->count() ) { Debug::info( 'No Views found.' ); return false; } return $this->filter( $views->results() ); } public function getName( $id ) { $views = self::findById( $id ); if (false == $views) { return 'unknown'; } return $views->title; } public function simpleByUser() { $whereClause = ['createdBy', '=', App::$activeUser->ID]; $views = self::$db->get( $this->tableName, $whereClause ); if ( !$views->count() ) { Debug::warn( 'Could not find any Views' ); return false; } $views = $views->results(); $out = []; foreach ( $views as $view ) { $out[ $view->title ] = $view->ID; } return $out; } public function simpleObjectByUser() { $whereClause = ['createdBy', '=', App::$activeUser->ID]; $views = self::$db->get( $this->tableName, $whereClause ); if ( !$views->count() ) { Debug::warn( 'Could not find any Views' ); return false; } $views = $views->results(); $out = []; foreach ( $views as $view ) { $obj = new \stdClass(); $obj->title = $view->title; $obj->ID = $view->ID; $out[] = $obj; } return $out; } }