engelsystem/src/Models/News.php

83 lines
2.3 KiB
PHP
Raw Normal View History

<?php
2019-11-12 22:04:39 +01:00
declare(strict_types=1);
namespace Engelsystem\Models;
use Carbon\Carbon;
use Engelsystem\Models\User\UsesUserModel;
2019-11-12 21:49:43 +01:00
use Illuminate\Database\Eloquent\Collection;
2021-07-10 00:59:20 +02:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2019-11-12 21:49:43 +01:00
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Query\Builder as QueryBuilder;
2021-12-10 22:24:18 +01:00
use Illuminate\Support\Str;
/**
2019-11-12 21:49:43 +01:00
* @property int $id
* @property string $title
* @property string $text
* @property bool $is_meeting
2020-12-27 02:51:05 +01:00
* @property bool $is_pinned
2019-11-12 21:49:43 +01:00
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
*
* @property-read Collection|NewsComment[] $comments
2021-12-10 22:24:18 +01:00
* @property-read int|null $comments_count
*
* @method static QueryBuilder|LogEntry[] whereId($value)
* @method static QueryBuilder|LogEntry[] whereTitle($value)
* @method static QueryBuilder|LogEntry[] whereText($value)
* @method static QueryBuilder|LogEntry[] whereIsMeeting($value)
2020-12-27 02:51:05 +01:00
* @method static QueryBuilder|LogEntry[] whereIsPinned($value)
* @method static QueryBuilder|LogEntry[] whereCreatedAt($value)
* @method static QueryBuilder|LogEntry[] whereUpdatedAt($value)
*/
class News extends BaseModel
{
2021-07-10 00:59:20 +02:00
use HasFactory;
use UsesUserModel;
/** @var bool Enable timestamps */
public $timestamps = true;
/** @var array */
protected $casts = [
'is_meeting' => 'boolean',
2021-12-10 22:24:18 +01:00
'is_pinned' => 'boolean',
];
/** @var array */
protected $fillable = [
'title',
'text',
'is_meeting',
2020-12-27 02:51:05 +01:00
'is_pinned',
'user_id',
];
2019-11-12 21:49:43 +01:00
/**
* @return HasMany
*/
public function comments(): HasMany
{
return $this->hasMany(NewsComment::class)
->orderBy('created_at');
}
2021-12-10 22:24:18 +01:00
/**
* @param bool $showMore
* @return string
*/
public function text(bool $showMore = true): string
{
if ($showMore || !Str::contains($this->text, 'more')) {
// Remove more tag
return preg_replace('/(.*)\[\s*more\s*\](.*)/is', '$1$2', $this->text);
}
// Only show text before more tag
$text = preg_replace('/(.*)(\s*\[\s*more\s*\].*)/is', '$1', $this->text);
return rtrim($text);
}
}