* @link https://TheTempusProject.com * @license https://opensource.org/licenses/MIT [MIT LICENSE] */ namespace TheTempusProject\Models; use TheTempusProject\Bedrock\Functions\Check; use TheTempusProject\Canary\Bin\Canary as Debug; use TheTempusProject\Classes\DatabaseModel; use TheTempusProject\TheTempusProject as App; use TheTempusProject\Houdini\Classes\Filters; use TheTempusProject\Canary\Classes\CustomException; class BookmarkDashboards extends DatabaseModel { public $tableName = 'bookmark_dashboards'; public $databaseMatrix = [ [ 'title', 'varchar', '256' ], [ 'saved_prefs', 'text', '' ], [ 'link_order', 'text', '' ], [ 'description', 'text', '' ], [ 'createdBy', 'int', '11' ], [ 'createdAt', 'int', '11' ], [ 'updatedAt', 'int', '11' ], [ 'uuid', 'char', '36' ], ]; /** * The model constructor. */ public function __construct() { parent::__construct(); } public function create( $title, $saved_prefs, $link_order, $description = '' ) { if ( ! Check::dataTitle( $title ) ) { Debug::info( 'Dash: illegal title.' ); return false; } $fields = [ 'title' => $title, 'description' => $description, 'saved_prefs' => $saved_prefs, 'link_order' => $link_order, 'uuid' => generateUuidV4(), 'createdBy' => App::$activeUser->ID, 'createdAt' => time(), ]; if ( ! self::$db->insert( $this->tableName, $fields ) ) { new CustomException( 'dashCreate' ); Debug::error( "Dash: not created " . var_export($fields,true) ); return false; } return self::$db->lastId(); } public function update( $id, $title, $saved_prefs, $link_order, $description = '' ) { if ( !Check::id( $id ) ) { Debug::info( 'Dash: illegal ID.' ); return false; } if ( !Check::dataTitle( $title ) ) { Debug::info( 'Dash: illegal title.' ); return false; } $fields = [ 'title' => $title, 'description' => $description, 'saved_prefs' => $saved_prefs, 'link_order' => $link_order, ]; if ( !self::$db->update( $this->tableName, $id, $fields ) ) { new CustomException( 'dashUpdate' ); Debug::error( "Dash: $id not updated" ); return false; } return true; } public function updateDash( $id, $saved_prefs = '', $link_order = '' ) { if ( !Check::id( $id ) ) { Debug::info( 'Dash: illegal ID.' ); return false; } $fields = []; $fields['saved_prefs'] = $saved_prefs; if ( ! empty( $link_order ) ) { $fields['link_order'] = $link_order; } if ( empty( $fields ) ) { return true; } if ( !self::$db->update( $this->tableName, $id, $fields ) ) { new CustomException( 'dashUpdate' ); Debug::error( "Dash: $id not updated" ); 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; } public function findByUuid( $id ) { $whereClause = ['uuid', '=', $id]; $dashboards = self::$db->get( $this->tableName, $whereClause ); if ( !$dashboards->count() ) { Debug::info( 'No Dashboards found.' ); return false; } return $this->filter( $dashboards->first() ); } }