2018-04-18 00:38:11 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2018-04-26 20:26:18 -04:00
|
|
|
use Parsedown;
|
2020-12-09 16:00:01 -05:00
|
|
|
use App\Models\User;
|
2020-04-24 14:56:57 -04:00
|
|
|
use App\Models\BlogTags;
|
2018-04-18 00:38:11 -04:00
|
|
|
|
|
|
|
class Blog extends DashboardModel
|
|
|
|
{
|
|
|
|
protected $table = 'blog';
|
|
|
|
|
2020-04-24 00:22:42 -04:00
|
|
|
public static $items_per_page = 10;
|
|
|
|
|
2018-04-18 00:38:11 -04:00
|
|
|
public static $dashboard_type = 'edit';
|
|
|
|
|
|
|
|
public static $dashboard_display = [ 'title', 'created_at' ];
|
|
|
|
|
|
|
|
public static $dashboard_columns = [
|
|
|
|
[ 'name' => 'user_id', 'type' => 'user' ],
|
|
|
|
[ 'name' => 'created_at', 'title' => 'Date', 'type' => 'display' ],
|
2020-04-24 00:22:42 -04:00
|
|
|
[ 'name' => 'title', 'required' => true, 'unique' => true, 'type' => 'string' ],
|
|
|
|
[ 'name' => 'body', 'required' => true, 'type' => 'mkd' ],
|
2022-06-14 01:36:21 -04:00
|
|
|
[ 'name' => 'header-image', 'title' => 'Header Image', 'type' => 'image', 'delete' => true, 'ext' => 'jpg' ],
|
|
|
|
[ 'name' => 'tags', 'type' => 'list', 'model' => 'BlogTags', 'foreign' => 'blog_id', 'sort' => 'order' ]
|
2018-04-18 00:38:11 -04:00
|
|
|
];
|
2018-04-26 20:26:18 -04:00
|
|
|
|
|
|
|
public static function getBlogEntries()
|
|
|
|
{
|
|
|
|
$blog_entries = [];
|
|
|
|
|
|
|
|
foreach (self::orderBy(self::$dashboard_sort_column, self::$dashboard_sort_direction)->get() as $blog_entry) {
|
|
|
|
// Add the name of the user that created the post
|
|
|
|
$blog_entry['username'] = User::find($blog_entry->user_id)->name;
|
|
|
|
|
|
|
|
// Add a string with the date and time the post was made
|
|
|
|
$blog_entry['date'] = date('M j, Y @ g:iA', strtotime($blog_entry->created_at));
|
|
|
|
|
|
|
|
// Convert the markdown in the body to html
|
|
|
|
$blog_entry['body'] = Parsedown::instance()->setBreaksEnabled(true)->setMarkupEscaped(true)->parse($blog_entry['body']);
|
|
|
|
|
|
|
|
// Replace the tags string with an array
|
|
|
|
$tags = [];
|
|
|
|
|
2020-04-24 14:56:57 -04:00
|
|
|
foreach (BlogTags::where('blog_id', $blog_entry->id)->orderBy('order')->get() as $tag) {
|
|
|
|
array_push($tags, $tag['name']);
|
2018-04-26 20:26:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$blog_entry['tags'] = $tags;
|
|
|
|
|
|
|
|
// Add the header image if one exists
|
2021-07-29 16:40:55 -04:00
|
|
|
$header_image_path = $blog_entry->getUploadsPath('image') . $blog_entry->id . '-header-image.jpg';
|
|
|
|
$blog_entry['headerimage'] = file_exists(public_path($header_image_path)) ? $header_image_path . '?version=' . $blog_entry->timestamp() : '';
|
2018-04-26 20:26:18 -04:00
|
|
|
|
|
|
|
// Add the processed blog entry to the array
|
|
|
|
array_push($blog_entries, $blog_entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $blog_entries;
|
|
|
|
}
|
2018-04-18 00:38:11 -04:00
|
|
|
}
|