* @link https://TheTempusProject.com * @license https://opensource.org/licenses/MIT [MIT LICENSE] */ namespace TheTempusProject\Models; use TheTempusProject\Bedrock\Functions\Check; use TheTempusProject\Bedrock\Classes\Config; use TheTempusProject\Canary\Canary as Debug; use TheTempusProject\Bedrock\Classes\CustomException; use TheTempusProject\Classes\DatabaseModel; use TheTempusProject\Plugins\Bugreport as Plugin; use TheTempusProject\TheTempusProject as App; class Tasklists extends DatabaseModel { public $tableName = 'tasklists'; public $databaseMatrix = [ [ 'title', 'varchar', '128' ], [ 'active', 'varchar', '5' ], [ 'createdAt', 'int', '10' ], [ 'color', 'varchar', '48' ], [ 'createdBy', 'int', '11' ], ]; public $plugin; /** * The model constructor. */ public function __construct() { parent::__construct(); $this->plugin = new Plugin; } /** * This function parses the bug reports description and * separates it into separate keys in the array. * * @param array $data - The data being parsed. * * @return array */ public function filter( $data, $params = [] ) { foreach ( $data as $instance ) { if ( !is_object( $instance ) ) { $instance = $data; $end = true; } $instance->submittedBy = self::$user->getUsername( $instance->createdBy ); $out[] = $instance; if ( !empty( $end ) ) { $out = $out[0]; break; } } return $out; } /** * Logs a Bug Report form. * * @param int $ID the user ID submitting the form * @param string $url the url * @param string $o_url the original url * @param int $repeat is repeatable? * @param string $description_ description of the event. * * @return null */ public function create( $title, $active = 'true' ) { if ( !$this->plugin->checkEnabled() ) { Debug::info( 'ToDo Plugin is disabled in the config.' ); return false; } $fields = [ 'createdBy' => App::$activeUser->ID, 'createdAt' => time(), 'title' => $title, 'active' => $active, ]; if ( !self::$db->insert( $this->tableName, $fields ) ) { new CustomException( $this->tableName ); return false; } return self::$db->lastId(); } public function update( $ID, $title, $active = 'true' ) { if ( !$this->plugin->checkEnabled() ) { Debug::info( 'ToDo Plugin is disabled in the config.' ); return false; } if ( !Check::id( $ID ) ) { Debug::info( 'ToDo List: illegal ID.' ); return false; } $data = $this->findById( $ID ); if ( $data == false ) { Debug::info( 'ToDo List: not found.' ); return false; } $fields = [ 'title' => $title, 'active' => $active, ]; if ( !self::$db->update( $this->tableName, $ID, $fields ) ) { new CustomException( $this->tableName ); Debug::error( "ToDo List: $ID not updated: $fields" ); return false; } return true; } public function activate( $ID ) { if ( !Check::id( $ID ) ) { Debug::info( 'TaskList: illegal ID.' ); return false; } $fields = [ 'active' => 'true', ]; if ( !self::$db->update( $this->tableName, $ID, $fields ) ) { new CustomException( $this->tableName ); Debug::error( "TaskList: $ID not activated: $fields" ); return false; } return true; } public function hide( $ID ) { if ( !Check::id( $ID ) ) { Debug::info( 'TaskList: illegal ID.' ); return false; } $fields = [ 'active' => 'false', ]; if ( !self::$db->update( $this->tableName, $ID, $fields ) ) { new CustomException( $this->tableName ); Debug::error( "TaskList: $ID not Hidden: $fields" ); return false; } return true; } public function recent( $limit = null ) { $where = ['createdBy', '=', App::$activeUser->ID]; $data = self::$db->get( $this->tableName, $where, 'createdAt', 'DESC', [0, $limit] ); if ( !$data->count() ) { Debug::info( 'No tasks found.' ); return []; } return $this->filter( $data->results() ); } public function getSimpleList( $includeDefault = false ) { $out = []; if ( $includeDefault === true ) { $out[ 'No List' ] = 0; } $lists = self::$db->get( $this->tableName, [ 'createdBy', '=', App::$activeUser->ID ] ); if ( !$lists->count() ) { Debug::warn( 'Could not find any ' . $this->tableName ); return $out; } $taskLists = $lists->results(); foreach ( $taskLists as &$list ) { $out[ $list->title ] = $list->ID; } return $out; } public function simpleObjectByUser() { $whereClause = ['createdBy', '=', App::$activeUser->ID]; $lists = self::$db->get( $this->tableName, $whereClause ); if ( !$lists->count() ) { Debug::warn( 'Could not find any tasklists' ); return false; } $taskLists = $lists->results(); $out = []; foreach ( $taskLists as &$list ) { $obj = new \stdClass(); $obj->title = $list->title; $obj->ID = $list->ID; $out[] = $obj; } return $out; } }