* @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;
use TheTempusProject\Bedrock\Functions\Sanitize;
class Notes extends DatabaseModel {
public $tableName = 'notes';
public $databaseMatrix = [
[ 'title', 'varchar', '256' ],
[ 'note', 'text', '' ],
[ 'color', 'varchar', '48' ],
[ 'notebookID', 'int', '11' ],
[ 'createdBy', 'int', '11' ],
[ 'createdAt', 'int', '11' ],
[ 'order', 'int', '11' ],
];
/**
* The model constructor.
*/
public function __construct() {
parent::__construct();
}
public function create( $title, $note = '', $color = 'default', $notebookID = 0 ) {
$fields = [
'title' => $title,
'note' => $note,
'color' => $color,
'notebookID' => $notebookID,
'createdBy' => App::$activeUser->ID,
'createdAt' => time(),
];
if ( ! self::$db->insert( $this->tableName, $fields ) ) {
new CustomException( 'noteCreate' );
Debug::error( "Notes: not created " . var_export($fields,true) );
return false;
}
return self::$db->lastId();
}
public function update( $id, $title, $note = '', $color = 'default', $notebookID= 0 ) {
if ( !Check::id( $id ) ) {
Debug::info( 'Notes: illegal ID.' );
return false;
}
if ( !Check::id( $notebookID ) ) {
Debug::info( 'Notes: illegal ID.' );
return false;
}
$fields = [
'title' => $title,
'note' => $note,
'color' => $color,
'notebookID' => $notebookID,
];
if ( !self::$db->update( $this->tableName, $id, $fields ) ) {
new CustomException( 'noteUpdate' );
Debug::error( "Notes: $id not updated: $fields" );
return false;
}
return true;
}
public function byUser( $limit = null ) {
$whereClause = ['createdBy', '=', App::$activeUser->ID];
if ( empty( $limit ) ) {
$notes = self::$db->get( $this->tableName, $whereClause );
} else {
$notes = self::$db->get( $this->tableName, $whereClause, 'ID', 'DESC', [0, $limit] );
}
if ( !$notes->count() ) {
Debug::info( 'No Notes found.' );
return false;
}
return $this->filter( $notes->results() );
}
public function getName( $id ) {
$note = self::findById( $id );
return $note->title;
}
public function simpleByUser() {
$whereClause = ['createdBy', '=', App::$activeUser->ID];
$notes = self::$db->get( $this->tableName, $whereClause );
if ( !$notes->count() ) {
Debug::warn( 'Could not find any notes' );
return false;
}
$notes = $notes->results();
$out = [];
foreach ( $notes as &$note ) {
$out[ $note->title ] = $note->ID;
}
return $out;
}
public function simpleObjectByUser() {
$whereClause = ['createdBy', '=', App::$activeUser->ID];
$notes = self::$db->get( $this->tableName, $whereClause );
if ( !$notes->count() ) {
Debug::warn( 'Could not find any notes' );
return false;
}
$notes = $notes->results();
$out = [];
foreach ( $notes as &$note ) {
$obj = new \stdClass();
$obj->title = $note->title;
$obj->ID = $note->ID;
$out[] = $obj;
}
return $out;
}
public function simpleObjectByNotebook( $id = 0 ) {
$whereClause = [];
if ( ! empty( $id ) ) {
$whereClause = [
'notebookID', '=', $id, 'AND',
];
}
$whereClause = array_merge( $whereClause, [
'createdBy', '=', App::$activeUser->ID,
] );
$notes = self::$db->get( $this->tableName, $whereClause );
if ( !$notes->count() ) {
Debug::warn( 'Could not find any notes' );
return false;
}
$notes = $notes->results();
$out = [];
foreach ( $notes as &$note ) {
$obj = new \stdClass();
$obj->title = $note->title;
$obj->ID = $note->ID;
$out[] = $obj;
}
return $out;
}
public function findByNotebook( $id = 0 ) {
$whereClause = [];
if ( ! empty( $id ) ) {
$whereClause = [
'notebookID', '=', $id, 'AND',
];
}
$whereClause = array_merge( $whereClause, [
'createdBy', '=', App::$activeUser->ID,
] );
$notes = self::$db->get( $this->tableName, $whereClause );
if ( ! $notes->count() ) {
Debug::info( 'No ' . $this->tableName . ' data found.' );
return [];
}
return $this->filter( $notes->results() );
}
public function filter( $postArray, $params = [] ) {
foreach ( $postArray as $instance ) {
if ( !is_object( $instance ) ) {
$instance = $postArray;
$end = true;
}
$cleanPost = Sanitize::contentShort( $instance->note );
$postSpace = explode( ' ', $cleanPost );
$postLine = explode( "\n", $cleanPost );
// summary by words: 100
$spaceSummary = implode( ' ', array_splice( $postSpace, 0, 100 ) );
// summary by lines: 5
$lineSummary = implode( "\n", array_splice( $postLine, 0, 5 ) );
if ( strlen( $spaceSummary ) < strlen( $lineSummary ) ) {
$contentSummary = $spaceSummary;
$contentSummaryNoLink = $contentSummary;
if ( count( $postSpace, 1 ) <= 100 ) {
$contentSummaryNoLink = $contentSummary;
$contentSummary .= '... Read More';
}
} else {
// @todo: need to refine this after testing
$contentSummaryNoLink = $lineSummary;
$contentSummary = $lineSummary . '... Read More';
}
$instance->contentSummaryNoLink = $contentSummaryNoLink;
$instance->contentSummary = $contentSummary;
if ( isset( $params['stripHtml'] ) && $params['stripHtml'] === true ) {
$instance->contentSummary = strip_tags( $instance->content );
}
// $instance->content = Filters::applyOne( 'mentions.0', $instance->content, true );
// $instance->content = Filters::applyOne( 'hashtags.0', $instance->content, true );
$out[] = $instance;
if ( !empty( $end ) ) {
$out = $out[0];
break;
}
}
return $out;
}
}