Files
UNIT3D-Community-Edition/app/Models/Article.php
2020-02-12 14:51:22 -05:00

139 lines
4.0 KiB
PHP

<?php
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition 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 Community Edition
*
* @author HDVinnie <hdinnovations@protonmail.com>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/
namespace App\Models;
use App\Helpers\Bbcode;
use App\Helpers\Linkify;
use App\Traits\Auditable;
use Illuminate\Database\Eloquent\Model;
use voku\helper\AntiXSS;
/**
* App\Models\Article.
*
* @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
*
* @property-read int|null $comments_count
*/
class Article extends Model
{
use Auditable;
/**
* 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 = 20, $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)
{
$antiXss = new AntiXSS();
$this->attributes['content'] = $antiXss->xss_clean($value);
}
/**
* Parse Content And Return Valid HTML.
*
* @return string Parsed BBCODE To HTML
*/
public function getContentHtml()
{
$bbcode = new Bbcode();
$linkify = new Linkify();
return $bbcode->parse($linkify->linky($this->content), true);
}
}