158 lines
4.9 KiB
PHP
158 lines
4.9 KiB
PHP
<?php
|
|
/**
|
|
* app/plugins/bookmarks/models/folders.php
|
|
*
|
|
* This class is used for the manipulation of the folders database table.
|
|
*
|
|
* @package TP Bookmarks
|
|
* @version 3.0
|
|
* @author Joey Kimsey <Joey@thetempusproject.com>
|
|
* @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\Bin\Canary as Debug;
|
|
use TheTempusProject\Classes\DatabaseModel;
|
|
use TheTempusProject\TheTempusProject as App;
|
|
use TheTempusProject\Houdini\Classes\Filters;
|
|
use TheTempusProject\Bedrock\Classes\CustomException;
|
|
|
|
class Folders extends DatabaseModel {
|
|
public $tableName = 'folders';
|
|
public $databaseMatrix = [
|
|
[ 'title', 'varchar', '256' ],
|
|
[ 'color', 'varchar', '48' ],
|
|
[ 'privacy', 'varchar', '48' ],
|
|
[ 'description', 'text', '' ],
|
|
[ 'folderID', 'int', '11' ],
|
|
[ 'createdBy', 'int', '11' ],
|
|
[ 'createdAt', 'int', '11' ],
|
|
];
|
|
|
|
/**
|
|
* The model constructor.
|
|
*/
|
|
public function __construct() {
|
|
parent::__construct();
|
|
}
|
|
|
|
public function create( $title, $folderID = 0, $description = '', $color = 'default', $privacy = 'private' ) {
|
|
if ( ! Check::dataTitle( $title ) ) {
|
|
Debug::info( 'Folders: illegal title.' );
|
|
return false;
|
|
}
|
|
$fields = [
|
|
'title' => $title,
|
|
'description' => $description,
|
|
'color' => $color,
|
|
'privacy' => $privacy,
|
|
'createdBy' => App::$activeUser->ID,
|
|
'createdAt' => time(),
|
|
];
|
|
if ( !empty( $folderID ) ) {
|
|
$fields['folderID'] = $folderID;
|
|
}
|
|
if ( ! self::$db->insert( $this->tableName, $fields ) ) {
|
|
new CustomException( 'folderCreate' );
|
|
Debug::error( "Folders: not created " . var_export($fields,true) );
|
|
return false;
|
|
}
|
|
return self::$db->lastId();
|
|
}
|
|
|
|
public function update( $id, $title, $folderID = 0, $description = '', $color = 'default', $privacy = 'private' ) {
|
|
if ( !Check::id( $id ) ) {
|
|
Debug::info( 'Folders: illegal ID.' );
|
|
return false;
|
|
}
|
|
if ( !Check::dataTitle( $title ) ) {
|
|
Debug::info( 'Folders: illegal title.' );
|
|
return false;
|
|
}
|
|
$fields = [
|
|
'title' => $title,
|
|
'description' => $description,
|
|
'color' => $color,
|
|
'privacy' => $privacy,
|
|
];
|
|
if ( !empty( $folderID ) ) {
|
|
$fields['folderID'] = $folderID;
|
|
}
|
|
if ( !self::$db->update( $this->tableName, $id, $fields ) ) {
|
|
new CustomException( 'folderUpdate' );
|
|
Debug::error( "Folders: $id not updated: $fields" );
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public function byUser( $limit = null ) {
|
|
$whereClause = ['createdBy', '=', App::$activeUser->ID];
|
|
if ( empty( $limit ) ) {
|
|
$folders = self::$db->get( $this->tableName, $whereClause );
|
|
} else {
|
|
$folders = self::$db->get( $this->tableName, $whereClause, 'ID', 'DESC', [0, $limit] );
|
|
}
|
|
if ( !$folders->count() ) {
|
|
Debug::info( 'No Folders found.' );
|
|
return false;
|
|
}
|
|
return $this->filter( $folders->results() );
|
|
}
|
|
|
|
public function getName( $id ) {
|
|
$folder = self::findById( $id );
|
|
if (false == $folder) {
|
|
return 'unknown';
|
|
}
|
|
return $folder->title;
|
|
}
|
|
|
|
public function getColor( $id ) {
|
|
$folder = self::findById( $id );
|
|
if (false == $folder) {
|
|
return 'default';
|
|
}
|
|
return $folder->color;
|
|
}
|
|
|
|
public function simpleByUser() {
|
|
$whereClause = ['createdBy', '=', App::$activeUser->ID];
|
|
$folders = self::$db->get( $this->tableName, $whereClause );
|
|
if ( !$folders->count() ) {
|
|
Debug::warn( 'Could not find any folders' );
|
|
return false;
|
|
}
|
|
|
|
$folders = $folders->results();
|
|
$out = [];
|
|
$out[ 'No Folder' ] = 0;
|
|
foreach ( $folders as $folder ) {
|
|
$out[ $folder->title ] = $folder->ID;
|
|
}
|
|
return $out;
|
|
}
|
|
|
|
public function simpleObjectByUser() {
|
|
$whereClause = ['createdBy', '=', App::$activeUser->ID];
|
|
$folders = self::$db->get( $this->tableName, $whereClause );
|
|
if ( !$folders->count() ) {
|
|
Debug::warn( 'Could not find any folders' );
|
|
return false;
|
|
}
|
|
|
|
$folders = $folders->results();
|
|
$out = [];
|
|
foreach ( $folders as $folder ) {
|
|
$obj = new \stdClass();
|
|
$obj->title = $folder->title;
|
|
$obj->ID = $folder->ID;
|
|
$out[] = $obj;
|
|
}
|
|
return $out;
|
|
}
|
|
}
|