Files
UNIT3D-Community-Edition/app/Models/Article.php
2019-10-28 14:39:16 -04:00

126 lines
3.7 KiB
PHP
Executable File

<?php
/**
* NOTICE OF LICENSE.
*
* UNIT3D is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D
*
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
* @author HDVinnie
*/
namespace App\Models;
use App\Helpers\Bbcode;
use Illuminate\Database\Eloquent\Model;
/**
* @property int $id
* @property string $title
* @property string $slug
* @property string|null $image
* @property string $content
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property int $user_id
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Comment[] $comments
* @property-read \App\Models\User $user
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article query()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article whereContent($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article whereImage($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article whereSlug($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article whereTitle($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Article whereUserId($value)
* @mixin \Eloquent
*/
class Article extends Model
{
/**
* Belongs To A User.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo(User::class)->withDefault([
'username' => 'System',
'id' => '1',
]);
}
/**
* Has Many Comments.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function comments()
{
return $this->hasMany(Comment::class);
}
/**
* Article Trimming.
*
* @param $length
* @param $ellipses
* @param $strip_html
*
* @return string Formatted And Trimmed Content
*/
public function getBrief($length = 100, $ellipses = true, $strip_html = false)
{
$input = $this->content;
//strip tags, if desired
if ($strip_html) {
$input = strip_tags($input);
}
//no need to trim, already shorter than trim length
if (strlen($input) <= $length) {
return $input;
}
//find last space within length
$last_space = strrpos(substr($input, 0, $length), ' ');
$trimmed_text = substr($input, 0, $last_space);
//add ellipses (...)
if ($ellipses) {
$trimmed_text .= '...';
}
return $trimmed_text;
}
/**
* Set The Articles Content After Its Been Purified.
*
* @param string $value
*
* @return void
*/
public function setContentAttribute($value)
{
$this->attributes['content'] = htmlspecialchars($value);
}
/**
* Parse Content And Return Valid HTML.
*
* @return string Parsed BBCODE To HTML
*/
public function getContentHtml()
{
$bbcode = new Bbcode();
return $bbcode->parse($this->content, true);
}
}