* @link https://TheTempusProject.com * @license https://opensource.org/licenses/MIT [MIT LICENSE] */ namespace TheTempusProject\Models; use TheTempusProject\Canary\Canary as Debug; use TheTempusProject\Bedrock\Functions\Check; use TheTempusProject\Bedrock\Functions\Sanitize; use TheTempusProject\Classes\DatabaseModel; use TheTempusProject\TheTempusProject as App; use TheTempusProject\Bedrock\Classes\CustomException; use TheTempusProject\Houdini\Classes\Filters; class Posts extends DatabaseModel { public $tableName = 'posts'; public $databaseMatrix = [ [ 'author', 'int', '11' ], [ 'created', 'int', '10' ], [ 'edited', 'int', '10' ], [ 'draft', 'int', '1' ], [ 'title', 'varchar', '86' ], [ 'content', 'text', '' ], ]; public $resourceMatrix = [ [ 'title' => 'Welcome', 'content' => '

This is just a simple message to say thank you for installing The Tempus Project. If you have any questions you can find everything through our website here.

', 'author' => 1, 'created' => '{time}', 'edited' => '{time}', 'draft' => 0, ], ]; public function __construct() { parent::__construct(); } public function newPost( $title, $post, $draft ) { if ( !Check::dataTitle( $title ) ) { Debug::info( 'modelBlog: illegal title.' ); return false; } if ( $draft === 'saveDraft' ) { $draft = 1; } else { $draft = 0; } $fields = [ 'author' => App::$activeUser->ID, 'draft' => $draft, 'created' => time(), 'edited' => time(), 'content' => Sanitize::rich( $post ), 'title' => $title, ]; if ( !self::$db->insert( $this->tableName, $fields ) ) { Debug::error( "Blog Post: $data not updated: $fields" ); new customException( 'blogCreate' ); return false; } return true; } public function updatePost( $id, $title, $content, $draft ) { if ( empty( self::$log ) ) { self::$log = new Log; } if ( !Check::id( $id ) ) { Debug::info( 'modelBlog: illegal ID.' ); return false; } if ( !Check::dataTitle( $title ) ) { Debug::info( 'modelBlog: illegal title.' ); return false; } if ( $draft === 'saveDraft' ) { $draft = 1; } else { $draft = 0; } $fields = [ 'draft' => $draft, 'edited' => time(), 'content' => Sanitize::rich( $content ), 'title' => $title, ]; if ( !self::$db->update( $this->tableName, $id, $fields ) ) { new CustomException( 'blogUpdate' ); Debug::error( "Blog Post: $id not updated: $fields" ); return false; } self::$log->admin( "Updated Blog Post: $id" ); return true; } public function preview( $title, $content ) { if ( !Check::dataTitle( $title ) ) { Debug::info( 'modelBlog: illegal characters.' ); return false; } $fields = [ 'title' => $title, 'content' => $content, 'authorName' => App::$activeUser->username, 'created' => time(), ]; return (object) $fields; } public function filter( $postArray, $params = [] ) { foreach ( $postArray as $instance ) { if ( !is_object( $instance ) ) { $instance = $postArray; $end = true; } $draft = ''; $authorName = self::$user->getUsername( $instance->author ); $cleanPost = Sanitize::contentShort( $instance->content ); $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; if ( count( $postSpace, 1 ) <= 100 ) { $contentSummaryNoLink = $contentSummary; $contentSummary .= '... Read More'; } } else { // @todo: need to refine this after testing $contentSummaryNoLink = $lineSummary; $contentSummary = $lineSummary . '... Read More'; } if ( $instance->draft != '0' ) { $draft = ' Draft'; } $instance->isDraft = $draft; $instance->authorName = $authorName; $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; } public function archive( $includeDraft = false ) { $whereClause = []; $currentTimeUnix = time(); $x = 0; $dataOut = []; $month = date( 'F', $currentTimeUnix ); $year = date( 'Y', $currentTimeUnix ); $previous = date( 'U', strtotime( "$month 1st $year" ) ); if ( $includeDraft !== true ) { $whereClause = ['draft', '=', '0', 'AND']; } while ( $x <= 5 ) { $where = array_merge( $whereClause, ['created', '<=', $currentTimeUnix, 'AND', 'created', '>=', $previous] ); $data = self::$db->get( $this->tableName, $where ); $x++; $month = date( 'm', $previous ); $montht = date( 'F', $previous ); $year = date( 'Y', $previous ); if ( !$data ) { $count = 0; } else { $count = $data->count(); } $dataOut[] = (object) [ 'count' => $count, 'month' => $month, 'year' => $year, 'monthText' => $montht, ]; $currentTimeUnix = $previous; $previous = date( 'U', strtotime( '-1 months', $currentTimeUnix ) ); } if ( !$data ) { Debug::info( 'No Blog posts found.' ); return false; } return (object) $dataOut; } public function recent( $limit = null, $includeDraft = false ) { $whereClause = []; if ( $includeDraft !== true ) { $whereClause = ['draft', '=', '0']; } else { $whereClause = '*'; } if ( empty( $limit ) ) { $postData = self::$db->getPaginated( $this->tableName, $whereClause ); } else { $postData = self::$db->getPaginated( $this->tableName, $whereClause, 'ID', 'DESC', [0, $limit] ); } if ( !$postData->count() ) { Debug::info( 'No Blog posts found.' ); return false; } return $this->filter( $postData->results() ); } public function listPosts( $params = [] ) { if ( isset( $params['includeDrafts'] ) && $params['includeDrafts'] === true ) { $whereClause = '*'; } else { $whereClause = ['draft', '=', '0']; } $postData = self::$db->getPaginated( $this->tableName, $whereClause ); if ( !$postData->count() ) { Debug::info( 'No Blog posts found.' ); return false; } if ( isset( $params['stripHtml'] ) && $params['stripHtml'] === true ) { return $this->filter( $postData->results(), ['stripHtml' => true] ); } return $this->filter( $postData->results() ); } public function byYear( $year, $includeDraft = false ) { if ( !Check::id( $year ) ) { Debug::info( 'Invalid Year' ); return false; } $whereClause = []; if ( $includeDraft !== true ) { $whereClause = ['draft', '=', '0', 'AND']; } $firstDayUnix = date( 'U', strtotime( "first day of $year" ) ); $lastDayUnix = date( 'U', strtotime( "last day of $year" ) ); $whereClause = array_merge( $whereClause, ['created', '<=', $lastDayUnix, 'AND', 'created', '>=', $firstDayUnix] ); $postData = self::$db->getPaginated( $this->tableName, $whereClause ); if ( !$postData->count() ) { Debug::info( 'No Blog posts found.' ); return false; } return $this->filter( $postData->results() ); } public function byAuthor( $ID, $includeDraft = false ) { if ( !Check::id( $ID ) ) { Debug::info( 'Invalid Author' ); return false; } $whereClause = []; if ( $includeDraft !== true ) { $whereClause = ['draft', '=', '0', 'AND']; } $whereClause = array_merge( $whereClause, ['author' => $ID] ); $postData = self::$db->getPaginated( $this->tableName, $whereClause ); if ( !$postData->count() ) { Debug::info( 'No Blog posts found.' ); return false; } return $this->filter( $postData->results() ); } public function byMonth( $month, $year = 0, $includeDraft = false ) { if ( 0 === $year ) { $year = date( 'Y' ); } if ( !Check::id( $month ) ) { Debug::info( 'Invalid Month' ); return false; } if ( !Check::id( $year ) ) { Debug::info( 'Invalid Year' ); return false; } $whereClause = []; if ( $includeDraft !== true ) { $whereClause = ['draft', '=', '0', 'AND']; } $firstDayUnix = date( 'U', strtotime( "$month/01/$year" ) ); $month = date( 'F', $firstDayUnix ); $lastDayUnix = date( 'U', strtotime( "last day of $month $year" ) ); $whereClause = array_merge( $whereClause, ['created', '<=', $lastDayUnix, 'AND', 'created', '>=', $firstDayUnix] ); $postData = self::$db->getPaginated( $this->tableName, $whereClause ); if ( !$postData->count() ) { Debug::info( 'No Blog posts found.' ); return false; } return $this->filter( $postData->results() ); } }