Files
eatvio-infra/app/Models/Plan.php

189 lines
6.1 KiB
PHP

<?php
namespace App\Models;
use App\Presenters\PlanPresenter;
use CyrildeWit\EloquentViewable\Contracts\Viewable;
use CyrildeWit\EloquentViewable\InteractsWithViews;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Laracasts\Presenter\PresentableTrait;
use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
/**
* App\Models\Plan
*
* @property int $id
* @property string $name name of plan
* @property string|null $image public path to image
* @property int $duration plan duration - 7, 14, 21 days
* @property string $state active/draft/archived
* @property array $type to lose weight, to gain weight, to improve your mood, to shape your eating behavior
* @property string $details plan details
* @property int $packets_count 1 on 3 packets of options in plan
* @property array $packet1_options
* @property array $packet2_options
* @property array $packet3_options
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property string $slug
*
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\User> $author
* @property-read int|null $author_count
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\PlanComment> $comments
* @property-read int|null $comments_count
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\PlanLike> $likes
* @property-read int|null $likes_count
* @property-read \Illuminate\Database\Eloquent\Collection<int, \CyrildeWit\EloquentViewable\View> $views
* @property-read int|null $views_count
*
* @method static \Database\Factories\PlanFactory factory($count = null, $state = [])
* @method static Builder|Plan negativeComments()
* @method static Builder|Plan newModelQuery()
* @method static Builder|Plan newQuery()
* @method static Builder|Plan orderByUniqueViews(string $direction = 'desc', $period = null, ?string $collection = null, string $as = 'unique_views_count')
* @method static Builder|Plan orderByViews(string $direction = 'desc', ?\CyrildeWit\EloquentViewable\Support\Period $period = null, ?string $collection = null, bool $unique = false, string $as = 'views_count')
* @method static Builder|Plan positiveComments()
* @method static Builder|Plan query()
* @method static Builder|Plan whereCreatedAt($value)
* @method static Builder|Plan whereDetails($value)
* @method static Builder|Plan whereDuration($value)
* @method static Builder|Plan whereId($value)
* @method static Builder|Plan whereImage($value)
* @method static Builder|Plan whereName($value)
* @method static Builder|Plan wherePacket1Options($value)
* @method static Builder|Plan wherePacket2Options($value)
* @method static Builder|Plan wherePacket3Options($value)
* @method static Builder|Plan wherePacketsCount($value)
* @method static Builder|Plan whereSlug($value)
* @method static Builder|Plan whereState($value)
* @method static Builder|Plan whereType($value)
* @method static Builder|Plan whereUpdatedAt($value)
* @method static Builder|Plan withViewsCount(?\CyrildeWit\EloquentViewable\Support\Period $period = null, ?string $collection = null, bool $unique = false, string $as = 'views_count')
*
* @mixin \Eloquent
*/
class Plan extends Model implements Viewable
{
use HasFactory;
use HasSlug;
use InteractsWithViews;
use PresentableTrait;
protected $withCount = [
'likes',
'comments',
];
protected $casts = [
'type' => 'array',
'packet1_options' => 'array',
'packet2_options' => 'array',
'packet3_options' => 'array',
];
protected string $presenter = PlanPresenter::class;
public function getSlugOptions(): SlugOptions
{
return SlugOptions::create()
->generateSlugsFrom(['name'])
->saveSlugsTo('slug')
->usingSeparator('_');
}
public function getRouteKeyName(): string
{
return 'slug';
}
public function author(): BelongsToMany
{
return $this->belongsToMany(User::class, 'plan_author', 'plan_id', 'user_id');
}
public function comments(): HasMany
{
return $this->hasMany(PlanComment::class);
}
public function likes(): HasMany
{
return $this->hasMany(PlanLike::class);
}
public function isLiked(): bool|int
{
if (auth()->user()) {
return auth()->user()->plan_likes()->forPlan($this)->count();
}
return false;
}
public function allItems(): array
{
$result = [];
for ($i = 1; $i <= $this->packets_count; $i++) {
$key = 'packet'.$i.'_options';
$data = $this->$key;
if (! is_null($data)) {
$result = array_merge($result, data_get($data, 'items', []));
}
}
return array_values(array_unique($result));
}
public function types(): Attribute
{
$data = $this->type;
return new Attribute(
get: fn () => array_keys($data),
);
}
public function minPrice(): Attribute
{
$result = 0;
for ($i = 1; $i <= $this->packets_count; $i++) {
$key = 'packet'.$i.'_options';
$data = $this->$key;
if (! is_null($data)) {
$curPrice = (int) data_get($data, 'price', 0);
$result = $curPrice > $result ? $curPrice : $result;
}
}
return new Attribute(
get: fn () => $result,
);
}
public function removeLike(): bool
{
if (auth()->user()) {
return auth()->user()->plan_likes()->forPlan($this)->delete();
}
return false;
}
public function scopeNegativeComments(Builder $query): HasMany
{
return static::comments()->where('grade', '<=', '3');
}
public function scopePositiveComments(Builder $query): HasMany
{
return static::comments()->where('grade', '>', '3');
}
}