2016-10-10 10:57:39 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2016-12-17 16:59:59 +01:00
|
|
|
use App\AlternativeTitle;
|
2016-11-28 08:48:12 +01:00
|
|
|
use App\Episode;
|
2016-10-10 10:57:39 +02:00
|
|
|
use App\Item;
|
2016-10-18 14:26:24 +02:00
|
|
|
use App\Services\Storage;
|
2016-10-10 10:57:39 +02:00
|
|
|
use App\Services\TMDB;
|
2016-12-12 09:18:13 +01:00
|
|
|
use App\Setting;
|
2016-10-10 10:57:39 +02:00
|
|
|
use Illuminate\Support\Facades\Input;
|
|
|
|
|
|
|
|
class ItemController {
|
|
|
|
|
|
|
|
private $loadingItems;
|
|
|
|
private $item;
|
2016-10-18 14:26:24 +02:00
|
|
|
private $storage;
|
2016-10-10 10:57:39 +02:00
|
|
|
|
|
|
|
/**
|
2017-01-06 13:00:18 +01:00
|
|
|
* Get the amount of loading items and create an instance for 'item'.
|
2016-10-10 10:57:39 +02:00
|
|
|
*
|
2017-01-06 13:00:18 +01:00
|
|
|
* @param Item $item
|
2016-10-18 14:26:24 +02:00
|
|
|
* @param Storage $storage
|
2016-10-10 10:57:39 +02:00
|
|
|
*/
|
2016-10-18 14:26:24 +02:00
|
|
|
public function __construct(Item $item, Storage $storage)
|
2016-10-10 10:57:39 +02:00
|
|
|
{
|
|
|
|
$this->loadingItems = config('app.LOADING_ITEMS');
|
|
|
|
$this->item = $item;
|
2016-10-18 14:26:24 +02:00
|
|
|
$this->storage = $storage;
|
2016-10-10 10:57:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-28 08:48:12 +01:00
|
|
|
* Return all items with pagination.
|
2016-10-10 10:57:39 +02:00
|
|
|
*
|
|
|
|
* @param $orderBy
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-11-28 08:48:12 +01:00
|
|
|
public function items($type, $orderBy)
|
2016-10-10 10:57:39 +02:00
|
|
|
{
|
|
|
|
$orderType = $orderBy == 'rating' ? 'asc' : 'desc';
|
|
|
|
|
2016-11-28 08:48:12 +01:00
|
|
|
$item = $this->item->orderBy($orderBy, $orderType)->with('latestEpisode');
|
|
|
|
|
|
|
|
if($type != 'home') {
|
|
|
|
$item = $item->where('media_type', $type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $item->simplePaginate($this->loadingItems);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all Episodes of an tv show.
|
|
|
|
*
|
|
|
|
* @param $tmdb_id
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function episodes($tmdb_id)
|
|
|
|
{
|
2016-12-12 09:18:13 +01:00
|
|
|
return [
|
2017-01-06 09:40:27 +01:00
|
|
|
'episodes' => Episode::findByTmdbId($tmdb_id)->get()->groupBy('season_number'),
|
2016-12-20 11:17:22 +01:00
|
|
|
'spoiler' => Setting::first()->episode_spoiler_protection
|
2016-12-12 09:18:13 +01:00
|
|
|
];
|
2016-10-10 10:57:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for items by 'title' in database or with Laravel Scout.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function search()
|
|
|
|
{
|
|
|
|
$title = Input::get('q');
|
|
|
|
|
|
|
|
if(config('scout.driver')) {
|
|
|
|
return $this->item->search($title)->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't have an smart search driver and return an simple 'like' query.
|
2017-01-06 09:40:27 +01:00
|
|
|
return $this->item->findByTitle($title)->get();
|
2016-10-10 10:57:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update rating for an movie.
|
|
|
|
*
|
|
|
|
* @param $itemID
|
|
|
|
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
|
|
|
public function changeRating($itemID)
|
|
|
|
{
|
|
|
|
$item = $this->item->find($itemID);
|
|
|
|
|
|
|
|
if( ! $item) {
|
|
|
|
return response('Not Found', 404);
|
|
|
|
}
|
|
|
|
|
|
|
|
$item->update([
|
|
|
|
'rating' => Input::get('rating')
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-06 13:00:18 +01:00
|
|
|
* Create a new movie / tv show.
|
2016-10-10 10:57:39 +02:00
|
|
|
*
|
|
|
|
* @param TMDB $tmdb
|
|
|
|
* @return Item
|
|
|
|
*/
|
2017-01-06 09:40:27 +01:00
|
|
|
public function add(TMDB $tmdb, Storage $storage, Episode $episode, AlternativeTitle $alternativeTitle)
|
2016-10-10 10:57:39 +02:00
|
|
|
{
|
|
|
|
$data = Input::get('item');
|
|
|
|
|
2017-01-06 09:40:27 +01:00
|
|
|
return $this->item->store($data, $tmdb, $storage, $episode, $alternativeTitle);
|
2016-10-10 10:57:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-28 08:48:12 +01:00
|
|
|
* Delete movie or tv show (with episodes) and remove the poster image file.
|
2016-10-10 10:57:39 +02:00
|
|
|
*
|
|
|
|
* @param $itemID
|
|
|
|
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
|
|
|
public function remove($itemID)
|
|
|
|
{
|
|
|
|
$item = $this->item->find($itemID);
|
|
|
|
|
|
|
|
if( ! $item) {
|
|
|
|
return response('Not Found', 404);
|
|
|
|
}
|
|
|
|
|
2016-10-18 14:26:24 +02:00
|
|
|
$this->storage->removePosterFile($item->poster);
|
2016-11-28 08:48:12 +01:00
|
|
|
$tmdb_id = $item->tmdb_id;
|
2016-10-10 10:57:39 +02:00
|
|
|
|
|
|
|
$item->delete();
|
2016-11-28 08:48:12 +01:00
|
|
|
|
|
|
|
// Delete all related episodes
|
|
|
|
// todo: Make this possible in migrations
|
|
|
|
Episode::where('tmdb_id', $tmdb_id)->delete();
|
2016-12-17 16:59:59 +01:00
|
|
|
AlternativeTitle::where('tmdb_id', $tmdb_id)->delete();
|
2016-10-10 10:57:39 +02:00
|
|
|
}
|
|
|
|
|
2016-11-30 15:26:38 +01:00
|
|
|
/**
|
|
|
|
* Set an episode as seen/unseen.
|
|
|
|
*
|
|
|
|
* @param $id
|
|
|
|
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
2016-11-28 08:48:12 +01:00
|
|
|
public function setSeen($id)
|
|
|
|
{
|
|
|
|
$episode = Episode::find($id);
|
|
|
|
$episode->seen = ! $episode->seen;
|
|
|
|
|
|
|
|
if( ! $episode->save()) {
|
|
|
|
return response('Server Error', 500);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:40:27 +01:00
|
|
|
public function updateAlternativeTitles(TMDB $tmdb, AlternativeTitle $alternativeTitle, $tmdbID = null)
|
|
|
|
{
|
|
|
|
return $this->item->updateAlternativeTitles($tmdb, $alternativeTitle, $tmdbID);
|
|
|
|
}
|
|
|
|
|
2016-11-30 15:26:38 +01:00
|
|
|
/**
|
|
|
|
* Toggle all episodes of an season as seen/unseen.
|
|
|
|
*/
|
|
|
|
public function toggleSeason()
|
|
|
|
{
|
2017-01-06 09:40:27 +01:00
|
|
|
$tmdbId = Input::get('tmdb_id');
|
2016-11-30 15:26:38 +01:00
|
|
|
$season = Input::get('season');
|
|
|
|
$seen = Input::get('seen');
|
|
|
|
|
2017-01-06 09:40:27 +01:00
|
|
|
$episodes = Episode::where('tmdb_id', $tmdbId)->where('season_number', $season)->get();
|
2016-11-30 15:26:38 +01:00
|
|
|
|
|
|
|
foreach($episodes as $episode) {
|
|
|
|
$episode->seen = $seen;
|
|
|
|
$episode->save();
|
|
|
|
}
|
|
|
|
}
|
2017-01-06 09:40:27 +01:00
|
|
|
}
|