* @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\Bin\Canary as Debug; use TheTempusProject\Bedrock\Classes\CustomException; use TheTempusProject\Classes\DatabaseModel; use TheTempusProject\TheTempusProject as App; class Timers extends DatabaseModel { public $tableName = 'timers'; public $databaseMatrix = [ [ 'name', 'varchar', '86' ], [ 'type', 'varchar', '24' ], [ 'currentSeconds', 'int', '10' ], [ 'hours', 'int', '10' ], [ 'minutes', 'int', '10' ], [ 'seconds', 'int', '10' ], [ 'createdBy', 'int', '11' ], [ 'createdAt', 'int', '11' ], [ 'updatedAt', 'int', '11' ], [ 'pausedAt', 'int', '11' ], ]; public function __construct() { parent::__construct(); } public function filter( $data, $params = [] ) { return $data; } public function listByUser( $limit = 0 ) { $where = [ 'createdBy', '=', App::$activeUser->ID ]; if ( empty( $limit ) ) { $timers = self::$db->getPaginated( $this->tableName, $where, 'createdAt', 'DESC' ); } else { $timers = self::$db->get( $this->tableName, $where, 'createdAt', 'DESC', [ 0, $limit ] ); } if ( ! $timers->count() ) { Debug::info( 'No timers found.' ); return []; } return $this->filter( $timers->results() ); } public function userTimers( $limit = 0 ) { $where = [ 'createdBy', '=', App::$activeUser->ID, 'AND', 'type', '=', 'timer' ]; if ( empty( $limit ) ) { $timers = self::$db->getPaginated( $this->tableName, $where, 'createdAt', 'DESC' ); } else { $timers = self::$db->get( $this->tableName, $where, 'createdAt', 'DESC', [ 0, $limit ] ); } if ( ! $timers->count() ) { Debug::info( 'No timers found.' ); return []; } return $this->filter( $timers->results() ); } public function userStopWatches( $limit = 0 ) { $where = [ 'createdBy', '=', App::$activeUser->ID, 'AND', 'type', '=', 'stopwatch' ]; if ( empty( $limit ) ) { $timers = self::$db->getPaginated( $this->tableName, $where, 'createdAt', 'DESC' ); } else { $timers = self::$db->get( $this->tableName, $where, 'createdAt', 'DESC', [ 0, $limit ] ); } if ( ! $timers->count() ) { Debug::info( 'No timers found.' ); return []; } return $this->filter( $timers->results() ); } public function create( $name, $currentSeconds, $type = 'stopwatch', $pausedAt = 0, $hours = 0, $minutes = 0, $seconds = 0 ) { $fields = [ 'name' => $name, 'currentSeconds' => $currentSeconds, 'type' => $type, 'pausedAt' => $pausedAt, 'hours' => $hours, 'minutes' => $minutes, 'seconds' => $seconds, 'createdBy' => App::$activeUser->ID, 'createdAt' => time(), ]; if ( !self::$db->insert( $this->tableName, $fields ) ) { new CustomException( $this->tableName ); return false; } return self::$db->lastId(); } public function update( $ID, $currentSeconds, $pausedAt = 0 ) { if ( !Check::id( $ID ) ) { Debug::info( 'timer: illegal ID.' ); return false; } if ( !Check::id( $pausedAt ) ) { Debug::info( 'timer: illegal pausedAt.' ); return false; } $whereClause = [ 'createdBy', '=', App::$activeUser->ID, 'AND', 'ID', '=', $ID ]; $timer = self::$db->get( $this->tableName, $whereClause ); if ( ! $timer->count() ) { Debug::info( "Timer: Doesn't exist or user doesn't have access" ); return false; } $fields = [ 'pausedAt' => $pausedAt, 'currentSeconds' => $currentSeconds, 'updatedAt' => time(), ]; if ( !self::$db->update( $this->tableName, $ID, $fields ) ) { new CustomException( $this->tableName ); return false; } return true; } public function pause( $ID, $currentSeconds ) { if ( !Check::id( $ID ) ) { Debug::info( 'Timer: illegal ID.' ); return false; } $whereClause = [ 'createdBy', '=', App::$activeUser->ID, 'AND', 'ID', '=', $ID ]; $timer = self::$db->get( $this->tableName, $whereClause ); if ( ! $timer->count() ) { Debug::info( "Timer: Doesn't exist or user doesn't have access" ); return false; } $fields = [ 'pausedAt' => time(), 'currentSeconds' => $currentSeconds, ]; if ( !self::$db->update( $this->tableName, $ID, $fields ) ) { new CustomException( $this->tableName ); return false; } return true; } public function resume( $I, $currentSecondsD ) { if ( !Check::id( $ID ) ) { Debug::info( 'Timer: illegal ID.' ); return false; } $whereClause = [ 'createdBy', '=', App::$activeUser->ID, 'AND', 'ID', '=', $ID ]; $timer = self::$db->get( $this->tableName, $whereClause ); if ( ! $timer->count() ) { Debug::info( "Timer: Doesn't exist or user doesn't have access" ); return false; } $fields = [ 'pausedAt' => '', 'currentSeconds' => $currentSeconds, ]; if ( !self::$db->update( $this->tableName, $ID, $fields ) ) { new CustomException( $this->tableName ); return false; } return true; } }