1
0
mirror of https://github.com/devfake/flox.git synced 2024-11-15 06:32:34 +01:00
flox/backend/app/Episode.php
Viktor Geringer 6f26fdc376
Feature/calendar (#107)
* add more comments for models

* add translation

* add calendar component

* style calendar, add hotkeys

* update readme

* move into own service

* add calendar test

* fix import test, update readme

* fix scrollbar for weeks

* check for empty request

* remove update-genre-list

* relation for tv shows has been forgotten to load

* stop loading twice and prevent overwriting

* remove events outside from month, fix chrome styling
2018-11-09 22:10:33 +01:00

114 lines
2.5 KiB
PHP

<?php
namespace App;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
class Episode extends Model {
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = [
'release_episode_human_format',
'startDate',
];
/**
* Guard accessors from import.
*
* @var array
*/
protected $guarded = ['release_episode_human_format', 'startDate'];
/**
* Accessor for human formatted release date.
*/
public function getReleaseEpisodeHumanFormatAttribute()
{
$release = Carbon::createFromTimestamp($this->release_episode);
if($release > now()) {
return $release->diffForHumans();
}
return null;
}
/**
* Accessor for formatted release date.
*/
public function getStartDateAttribute()
{
if($this->release_episode) {
return Carbon::createFromTimestamp($this->release_episode)->format('Y-m-d');
}
}
/**
* Belongs to an item.
*/
public function item()
{
return $this->belongsTo(Item::class, 'tmdb_id', 'tmdb_id');
}
/**
* Belongs to an item (simpler query).
*/
public function calendarItem()
{
return $this->belongsTo(Item::class, 'tmdb_id', 'tmdb_id')
->select(['tmdb_id', 'title', 'watchlist']);
}
/**
* Scope to find the result via tmdb_id.
*/
public function scopeFindByTmdbId($query, $tmdbId)
{
return $query->where('tmdb_id', $tmdbId);
}
/**
* Scope to find the result via src.
*/
public function scopeFindBySrc($query, $src)
{
return $query->where('src', $src);
}
/**
* Scope to find the result via fp_name.
*/
public function scopeFindByFPName($query, $item)
{
return $query->where('fp_name', $item->name)->orWhere('fp_name', getFileName($item));
}
/**
* Scope to find a specific episode.
*/
public function scopeFindSpecificEpisode($query, $tmdbId, $episode)
{
$season = $episode->changed->season_number ?? $episode->season_number;
$episode = $episode->changed->episode_number ?? $episode->episode_number;
return $query->where('tmdb_id', $tmdbId)
->where('season_number', $season)
->where('episode_number', $episode);
}
/**
* Scope to find a complete season.
*/
public function scopeFindSeason($query, $tmdbId, $season)
{
return $query->where('tmdb_id', $tmdbId)
->where('season_number', $season);
}
}