* @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\Plugins\Reviews as Plugin; use TheTempusProject\TheTempusProject as App; use TheTempusProject\Bedrock\Classes\CustomException; class Reviewcategory extends DatabaseModel { public $tableName = 'review_categories'; public $databaseMatrix = [ [ 'name', 'varchar', '128' ], [ 'slug', 'varchar', '64' ], [ 'createdBy', 'int', '11' ], [ 'createdAt', 'int', '11' ], ]; public $plugin; /** * The model constructor. */ public function __construct() { parent::__construct(); $this->plugin = new Plugin; } public function create( $name, $slug ) { if ( ! $this->plugin->checkEnabled() ) { Debug::info( 'Reviews are disabled in the config.' ); return false; } if ( !Check::dataTitle( $slug ) ) { Debug::info( 'Review Categories: illegal title.' ); return false; } $fields = [ 'name' => $name, 'slug' => $slug, 'createdAt' => time(), 'createdBy' => App::$activeUser->ID, ]; if ( ! self::$db->insert( $this->tableName, $fields ) ) { Debug::info( 'ReviewCategories::create - failed to insert to db' ); return false; } return self::$db->lastId(); } public function update( $id, $name, $slug ) { if ( ! $this->plugin->checkEnabled() ) { Debug::info( 'Reviews are disabled in the config.' ); return false; } if ( !Check::dataTitle( $slug ) ) { Debug::info( 'Review Categories: illegal title.' ); return false; } $fields = [ 'name' => $name, 'slug' => $slug, ]; if ( !self::$db->update( $this->tableName, $id, $fields ) ) { new CustomException( 'reviewCategoryUpdate' ); Debug::error( "Review Categories: $id not updated: $fields" ); return false; } return true; } public function findBySlug( $slug, $limit = null ) { $whereClause = [ 'slug', '=', $slug ]; if ( empty( $limit ) ) { $categories = self::$db->get( $this->tableName, $whereClause ); } else { $categories = self::$db->get( $this->tableName, $whereClause, 'ID', 'DESC', [0, $limit] ); } if ( !$categories->count() ) { Debug::info( 'No categories found.' ); return false; } return $this->filter( $categories->first() ); } public function simple() { $categories = self::$db->get( $this->tableName, '*' ); if ( !$categories->count() ) { Debug::warn( 'Could not find any categories' ); return false; } $categories = $categories->results(); $out = []; foreach ( $categories as &$category ) { $out[ $category->name ] = $category->ID; } return $out; } }