You've already forked Atomcms-edit
Initial commit
This commit is contained in:
Executable
+21
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models\Articles;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Tag extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
#[\Override]
|
||||
protected $guarded = ['id', 'created_at', 'updated_at'];
|
||||
|
||||
public function websiteArticles()
|
||||
{
|
||||
return $this->morphedByMany(WebsiteArticle::class, 'taggable');
|
||||
}
|
||||
}
|
||||
Executable
+99
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Articles;
|
||||
|
||||
use App\Models\Concerns\BelongsToUser;
|
||||
use App\Models\User;
|
||||
use Carbon\Carbon;
|
||||
use Database\Factories\Articles\WebsiteArticleFactory;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Spatie\Sluggable\HasSlug;
|
||||
use Spatie\Sluggable\SlugOptions;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $title
|
||||
* @property string $slug
|
||||
* @property string $short_story
|
||||
* @property string $full_story
|
||||
* @property string $image
|
||||
* @property int $user_id
|
||||
* @property Carbon|null $deleted_at
|
||||
* @property Carbon|null $created_at
|
||||
* @property Carbon|null $updated_at
|
||||
* @property-read User|null $user
|
||||
* @property-read Collection<int, WebsiteArticleReaction> $reactions
|
||||
* @property-read Collection<int, WebsiteArticleComment> $comments
|
||||
* @property-read Collection<int, Tag> $tags
|
||||
*
|
||||
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteArticle where($column, $operator = null, $value = null)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteArticle whereNot($column, $operator = null, $value = null)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteArticle whereHas($relation, \Closure $callback = null, $operator = '>=', $count = 1)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteArticle latest($column = 'created_at')
|
||||
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteArticle get($columns = ['*'])
|
||||
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteArticle take(int $value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteArticle with($relations)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteArticle has($relation, \Closure $callback = null, $operator = '>=', $count = 1)
|
||||
*/
|
||||
class WebsiteArticle extends Model
|
||||
{
|
||||
use BelongsToUser;
|
||||
use HasSlug, \Illuminate\Database\Eloquent\Factories\HasFactory, SoftDeletes;
|
||||
|
||||
/** @var array<int, string> */
|
||||
#[\Override]
|
||||
protected $with = ['user'];
|
||||
|
||||
protected static function newFactory()
|
||||
{
|
||||
return WebsiteArticleFactory::new();
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
protected $guarded = ['id', 'created_at', 'updated_at', 'user_id', 'slug', 'visible', 'fixed'];
|
||||
|
||||
public function getSlugOptions(): SlugOptions
|
||||
{
|
||||
return SlugOptions::create()
|
||||
->generateSlugsFrom('title')
|
||||
->saveSlugsTo('slug')
|
||||
->usingSeparator('-')->allowDuplicateSlugs();
|
||||
}
|
||||
|
||||
public function reactions(): HasMany
|
||||
{
|
||||
return $this->hasMany(WebsiteArticleReaction::class, 'article_id')
|
||||
->whereActive(true);
|
||||
}
|
||||
|
||||
public function comments(): HasMany
|
||||
{
|
||||
return $this->hasMany(WebsiteArticleComment::class, 'article_id');
|
||||
}
|
||||
|
||||
public function userHasReachedArticleCommentLimit(): bool
|
||||
{
|
||||
return $this->comments()->where('user_id', '=', Auth::id())->count() >= (int) setting('max_comment_per_article');
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::saving(function ($model) {
|
||||
if (empty($model->image)) {
|
||||
$model->image = '';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function tags()
|
||||
{
|
||||
return $this->morphToMany(Tag::class, 'taggable');
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Articles;
|
||||
|
||||
use App\Models\Concerns\BelongsToUser;
|
||||
use App\Models\User;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $article_id
|
||||
* @property int $user_id
|
||||
* @property string $comment
|
||||
* @property Carbon|null $created_at
|
||||
* @property Carbon|null $updated_at
|
||||
* @property-read WebsiteArticle|null $article
|
||||
* @property-read User|null $user
|
||||
*/
|
||||
class WebsiteArticleComment extends Model
|
||||
{
|
||||
use BelongsToUser;
|
||||
|
||||
#[\Override]
|
||||
protected $guarded = ['id', 'created_at', 'updated_at'];
|
||||
|
||||
public function article(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(WebsiteArticle::class, 'article_id');
|
||||
}
|
||||
|
||||
public function canBeDeleted(): bool
|
||||
{
|
||||
return $this->user_id === Auth::id() || hasPermission('delete_article_comments');
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Articles;
|
||||
|
||||
use App\Models\Concerns\BelongsToUser;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteArticleReaction where($column, $operator = null, $value = null)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteArticleReaction first($columns = ['*'])
|
||||
*/
|
||||
class WebsiteArticleReaction extends Model
|
||||
{
|
||||
use BelongsToUser;
|
||||
use HasFactory;
|
||||
|
||||
#[\Override]
|
||||
protected $guarded = ['id', 'user_id', 'article_id'];
|
||||
|
||||
#[\Override]
|
||||
public $timestamps = false;
|
||||
|
||||
#[\Override]
|
||||
protected $hidden = [
|
||||
'user_id',
|
||||
'article_id',
|
||||
];
|
||||
|
||||
/**
|
||||
* @return static|null
|
||||
*/
|
||||
public static function getReaction(int $articleId, int $userId, string $reaction): ?self
|
||||
{
|
||||
/** @var static|null $reaction */
|
||||
$reaction = self::where('user_id', $userId)
|
||||
->where('article_id', $articleId)
|
||||
->where('reaction', $reaction)
|
||||
->first();
|
||||
|
||||
return $reaction;
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public static function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::creating(function ($model) {
|
||||
$model->user_id = auth()->id();
|
||||
});
|
||||
}
|
||||
|
||||
public function article(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(WebsiteArticle::class, 'article_id');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user