2016-10-10 10:57:39 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
2017-01-06 09:40:27 +01:00
|
|
|
use App\Services\Storage;
|
|
|
|
use App\Services\TMDB;
|
2016-10-10 10:57:39 +02:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Laravel\Scout\Searchable;
|
|
|
|
|
|
|
|
class Item extends Model {
|
|
|
|
|
|
|
|
// Uncomment this if you are using Laravel Scout.
|
|
|
|
//use Searchable;
|
|
|
|
|
|
|
|
public $timestamps = false;
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
'tmdb_id',
|
|
|
|
'title',
|
2016-11-23 16:24:34 +01:00
|
|
|
'original_title',
|
2016-10-10 10:57:39 +02:00
|
|
|
'poster',
|
2016-11-24 11:52:10 +01:00
|
|
|
'media_type',
|
2016-10-10 10:57:39 +02:00
|
|
|
'rating',
|
|
|
|
'released',
|
|
|
|
'created_at',
|
2016-10-17 10:57:42 +02:00
|
|
|
'genre',
|
2017-02-02 17:38:07 +01:00
|
|
|
'fp_name',
|
2017-01-23 22:26:04 +01:00
|
|
|
'src',
|
2017-01-26 16:14:32 +01:00
|
|
|
'subtitles',
|
2016-10-10 10:57:39 +02:00
|
|
|
];
|
2016-11-28 08:48:12 +01:00
|
|
|
|
2017-01-06 09:40:27 +01:00
|
|
|
/**
|
|
|
|
* Create the new movie / tv show.
|
|
|
|
*
|
|
|
|
* @param $data
|
|
|
|
* @return Item
|
|
|
|
*/
|
2017-01-23 22:26:04 +01:00
|
|
|
public function store($data)
|
2017-01-06 09:40:27 +01:00
|
|
|
{
|
2017-01-23 22:26:04 +01:00
|
|
|
return $this->create([
|
|
|
|
'tmdb_id' => $data['tmdb_id'],
|
2017-01-06 09:40:27 +01:00
|
|
|
'title' => $data['title'],
|
2017-01-23 22:26:04 +01:00
|
|
|
'media_type' => $data['media_type'],
|
2017-01-06 09:40:27 +01:00
|
|
|
'original_title' => $data['original_title'],
|
2017-02-07 16:16:06 +01:00
|
|
|
'poster' => $data['poster'] ? $data['poster'] : '',
|
2017-01-06 09:40:27 +01:00
|
|
|
'rating' => 1,
|
|
|
|
'released' => $data['released'],
|
|
|
|
'genre' => $data['genre'],
|
|
|
|
'created_at' => time(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-02-02 17:38:07 +01:00
|
|
|
/**
|
|
|
|
* @param $data
|
|
|
|
* @param $mediaType
|
|
|
|
* @return Item
|
|
|
|
*/
|
|
|
|
public function storeEmpty($data, $mediaType)
|
|
|
|
{
|
|
|
|
return $this->create([
|
|
|
|
'tmdb_id' => null,
|
|
|
|
'fp_name' => $data['name'],
|
|
|
|
'title' => $data['name'],
|
|
|
|
'media_type' => $mediaType,
|
|
|
|
'poster' => '',
|
|
|
|
'rating' => 1,
|
|
|
|
'released' => time(),
|
|
|
|
'created_at' => time(),
|
|
|
|
'src' => $data['src'],
|
|
|
|
'subtitles' => $data['subtitles'],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:40:27 +01:00
|
|
|
/*
|
|
|
|
* Relations
|
|
|
|
*/
|
|
|
|
|
2016-11-28 08:48:12 +01:00
|
|
|
public function episodes()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Episode::class, 'tmdb_id', 'tmdb_id');
|
|
|
|
}
|
|
|
|
|
2016-12-17 16:59:59 +01:00
|
|
|
public function alternativeTitles()
|
|
|
|
{
|
|
|
|
return $this->hasMany(AlternativeTitle::class, 'tmdb_id', 'tmdb_id');
|
|
|
|
}
|
|
|
|
|
2016-11-28 08:48:12 +01:00
|
|
|
public function latestEpisode()
|
|
|
|
{
|
|
|
|
return $this->hasOne(Episode::class, 'tmdb_id', 'tmdb_id')
|
|
|
|
->orderBy('id', 'desc')
|
|
|
|
->where('seen', true)
|
|
|
|
->latest();
|
|
|
|
}
|
2017-01-06 09:40:27 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Scopes
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function scopeFindByTmdbId($query, $tmdbId)
|
|
|
|
{
|
|
|
|
return $query->where('tmdb_id', $tmdbId);
|
|
|
|
}
|
|
|
|
|
2017-02-02 17:38:07 +01:00
|
|
|
public function scopeFindByFPName($query, $item)
|
|
|
|
{
|
|
|
|
$changed = isset($item->changed->name) ? $item->changed->name : $item->name;
|
|
|
|
|
|
|
|
return $query->where('fp_name', $item->name)->orWhere('fp_name', $changed);
|
|
|
|
}
|
|
|
|
|
2017-01-25 14:44:51 +01:00
|
|
|
public function scopeFindBySrc($query, $src)
|
|
|
|
{
|
|
|
|
return $query->where('src', $src);
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:40:27 +01:00
|
|
|
public function scopeFindByTitle($query, $title)
|
|
|
|
{
|
|
|
|
return $query->where('title', 'like', '%' . $title . '%')
|
|
|
|
->orWhere('original_title', 'like', '%' . $title . '%')
|
|
|
|
->orWhereHas('alternativeTitles', function($query) use ($title) {
|
|
|
|
$query->where('title', 'like', '%' . $title . '%');
|
|
|
|
});
|
|
|
|
}
|
2016-10-10 10:57:39 +02:00
|
|
|
}
|