Initial commit

This commit is contained in:
Joey Kimsey
2024-08-04 21:15:59 -04:00
parent c9d1fb983f
commit 0d469501ee
695 changed files with 70184 additions and 71 deletions

View File

@ -0,0 +1,169 @@
<?php
/**
* app/plugins/notes/models/notebooks.php
*
* This class is used for the manipulation of the notebooks database table.
*
* @package TP Notes
* @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\Canary as Debug;
use TheTempusProject\Classes\DatabaseModel;
use TheTempusProject\TheTempusProject as App;
use TheTempusProject\Houdini\Classes\Filters;
use TheTempusProject\Bedrock\Classes\CustomException;
class Notebooks extends DatabaseModel {
public $tableName = 'notebooks';
public $databaseMatrix = [
[ 'title', 'varchar', '256' ],
[ 'description', 'text', '' ],
[ 'color', 'varchar', '48' ],
[ 'icon', 'varchar', '48' ],
[ 'createdBy', 'int', '11' ],
[ 'createdAt', 'int', '11' ],
[ 'notebookID', 'int', '11' ],
[ 'order', 'int', '11' ],
];
/**
* The model constructor.
*/
public function __construct() {
parent::__construct();
}
public function create( $title, $description = '', $color = 'default', $notebookID = 0, $icon = '' ) {
if ( ! Check::dataTitle( $title ) ) {
Debug::info( 'Notebooks: illegal title.' );
return false;
}
$fields = [
'title' => $title,
'description' => $description,
'color' => $color,
'notebookID' => $notebookID,
'icon' => $icon,
'createdBy' => App::$activeUser->ID,
'createdAt' => time(),
];
if ( ! self::$db->insert( $this->tableName, $fields ) ) {
new CustomException( 'notebookCreate' );
Debug::error( "Notebooks: not created " . var_export($fields,true) );
return false;
}
return self::$db->lastId();
}
public function update( $id, $title, $description = '', $color = 'default', $notebookID = 0, $icon = '' ) {
if ( !Check::id( $id ) ) {
Debug::info( 'Notebooks: illegal ID.' );
return false;
}
if ( !Check::dataTitle( $title ) ) {
Debug::info( 'Notebooks: illegal title.' );
return false;
}
$fields = [
'title' => $title,
'notebookID' => $notebookID,
'description' => $description,
'color' => $color,
'icon' => $icon,
];
if ( !self::$db->update( $this->tableName, $id, $fields ) ) {
new CustomException( 'notebooksUpdate' );
Debug::error( "Notebooks: $id not updated" );
return false;
}
return true;
}
public function byUser( $limit = null ) {
$whereClause = ['createdBy', '=', App::$activeUser->ID];
if ( empty( $limit ) ) {
$notebooks = self::$db->get( $this->tableName, $whereClause );
} else {
$notebooks = self::$db->get( $this->tableName, $whereClause, 'ID', 'DESC', [0, $limit] );
}
if ( !$notebooks->count() ) {
Debug::info( 'No Notebooks found.' );
return false;
}
return $this->filter( $notebooks->results() );
}
public function getName( $id ) {
$notebooks = self::findById( $id );
return $notebooks->title;
}
public function simpleList( $param = '') {
$whereClause = ['createdBy', '=', App::$activeUser->ID];
$notebooks = self::$db->get( $this->tableName, $whereClause );
if ( !$notebooks->count() ) {
Debug::warn( 'Could not find any notebooks' );
return [];
}
$notebooks = $notebooks->results();
$out = [ 'None' => '0'];
foreach ( $notebooks as &$notebook ) {
$out[ $notebook->title ] = $notebook->ID;
}
return $out;
}
public function simpleObjectByUser() {
$whereClause = ['createdBy', '=', App::$activeUser->ID];
$notebooks = self::$db->get( $this->tableName, $whereClause );
if ( !$notebooks->count() ) {
Debug::warn( 'Could not find any notebooks' );
return false;
}
$notebooks = $notebooks->results();
$out = [];
foreach ( $notebooks as &$notebook ) {
$obj = new \stdClass();
$obj->title = $notebook->title;
$obj->ID = $notebook->ID;
$out[] = $obj;
}
return $out;
}
public function getTree() {
$whereClause = ['createdBy', '=', App::$activeUser->ID];
$notebooks = self::$db->get( $this->tableName, $whereClause );
if ( !$notebooks->count() ) {
Debug::warn( 'Could not find any notebooks' );
return [];
}
$notebooks = $notebooks->results();
$formattedNotebooks = [];
foreach ($notebooks as $notebook) {
if ( !empty($notebook->notebookID) ) {
$notebookID = $notebook->notebookID;
if ( ! isset( $formattedNotebooks[ $notebookID ])) {
$formattedNotebooks[ $notebookID ][ 'notebooks' ] = [];
}
$formattedNotebooks[ $notebookID ][ 'notebooks' ][] = $notebook;
} else {
$notebookID = $notebook->ID;
$formattedNotebooks[ $notebookID ][ 'notebook' ] = $notebook;
}
}
return $formattedNotebooks;
}
}