wip from ATB
This commit is contained in:
@ -32,6 +32,7 @@ class Posts extends DatabaseModel {
|
||||
[ 'edited', 'int', '10' ],
|
||||
[ 'draft', 'int', '1' ],
|
||||
[ 'title', 'varchar', '86' ],
|
||||
[ 'slug', 'varchar', '64' ],
|
||||
[ 'content', 'text', '' ],
|
||||
];
|
||||
|
||||
@ -45,7 +46,7 @@ class Posts extends DatabaseModel {
|
||||
}
|
||||
}
|
||||
|
||||
public function newPost( $title, $post, $draft ) {
|
||||
public function newPost( $title, $post, $slug, $draft ) {
|
||||
if ( !Check::dataTitle( $title ) ) {
|
||||
Debug::info( 'modelBlog: illegal title.' );
|
||||
|
||||
@ -59,6 +60,7 @@ class Posts extends DatabaseModel {
|
||||
$fields = [
|
||||
'author' => App::$activeUser->ID,
|
||||
'draft' => $draft,
|
||||
'slug' => $slug,
|
||||
'created' => time(),
|
||||
'edited' => time(),
|
||||
'content' => Sanitize::rich( $post ),
|
||||
@ -73,7 +75,7 @@ class Posts extends DatabaseModel {
|
||||
return true;
|
||||
}
|
||||
|
||||
public function updatePost( $id, $title, $content, $draft ) {
|
||||
public function updatePost( $id, $title, $content, $slug, $draft ) {
|
||||
if ( empty( self::$log ) ) {
|
||||
self::$log = new Log;
|
||||
}
|
||||
@ -94,6 +96,7 @@ class Posts extends DatabaseModel {
|
||||
}
|
||||
$fields = [
|
||||
'draft' => $draft,
|
||||
'slug' => $slug,
|
||||
'edited' => time(),
|
||||
'content' => Sanitize::rich( $content ),
|
||||
'title' => $title,
|
||||
@ -131,39 +134,47 @@ class Posts extends DatabaseModel {
|
||||
}
|
||||
$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 .= '... <a href="{ROOT_URL}blog/post/' . $instance->ID . '">Read More</a>';
|
||||
}
|
||||
|
||||
// Summarize
|
||||
if ( ! empty( $instance->slug ) ) {
|
||||
$identifier = $instance->slug;
|
||||
} else {
|
||||
$identifier = $instance->ID;
|
||||
}
|
||||
|
||||
$cleanPost = Sanitize::contentShort( $instance->content );
|
||||
// By Word
|
||||
$wordsArray = explode( ' ', $cleanPost );
|
||||
$wordSummary = implode( ' ', array_splice( $wordsArray, 0, 100 ) );
|
||||
// By Line
|
||||
$linesArray = explode( "\n", $cleanPost );
|
||||
$lineSummary = implode( "\n", array_splice( $linesArray, 0, 5 ) );
|
||||
|
||||
if ( strlen( $wordSummary ) < strlen( $lineSummary ) ) {
|
||||
$contentSummaryNoLink = $wordSummary;
|
||||
$contentSummary = $wordSummary . '... <a href="{ROOT_URL}blog/post/' . $identifier . '" class="text-decoration-none">Read More</a>';
|
||||
} else {
|
||||
// @todo: need to refine this after testing
|
||||
$contentSummaryNoLink = $lineSummary;
|
||||
$contentSummary = $lineSummary . '... <a href="{ROOT_URL}blog/post/' . $instance->ID . '">Read More</a>';
|
||||
$contentSummary = $lineSummary . '... <a href="{ROOT_URL}blog/post/' . $identifier . '" class="text-decoration-none">Read More</a>';
|
||||
}
|
||||
|
||||
$instance->contentSummaryNoLink = $contentSummaryNoLink;
|
||||
$instance->contentSummary = $contentSummary;
|
||||
|
||||
if ( isset( $params['stripHtml'] ) && $params['stripHtml'] === true ) {
|
||||
$instance->contentSummary = strip_tags( $instance->content );
|
||||
}
|
||||
if ( $instance->draft != '0' ) {
|
||||
$draft = ' <b>Draft</b>';
|
||||
}
|
||||
$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 );
|
||||
}
|
||||
if ( self::$comments !== false ) {
|
||||
$instance->commentCount = self::$comments->count( 'blog', $instance->ID );
|
||||
}
|
||||
$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];
|
||||
@ -291,6 +302,22 @@ class Posts extends DatabaseModel {
|
||||
return $this->filter( $postData->results() );
|
||||
}
|
||||
|
||||
public function findBySlug( $slug, $includeDraft = false ) {
|
||||
$whereClause = [];
|
||||
if ( $includeDraft !== true ) {
|
||||
$whereClause = ['draft', '=', '0', 'AND'];
|
||||
}
|
||||
|
||||
$whereClause = array_merge( $whereClause, ['slug', '=', $slug] );
|
||||
|
||||
$postData = self::$db->get( $this->tableName, $whereClause );
|
||||
if ( !$postData->count() ) {
|
||||
Debug::info( 'No Blog posts found.' );
|
||||
return false;
|
||||
}
|
||||
return $this->filter( $postData->first() );
|
||||
}
|
||||
|
||||
public function byMonth( $month, $year = 0, $includeDraft = false ) {
|
||||
if ( 0 === $year ) {
|
||||
$year = date( 'Y' );
|
||||
|
Reference in New Issue
Block a user