1
0
mirror of https://github.com/devfake/flox.git synced 2024-11-15 22:52:32 +01:00
flox/backend/app/Http/Controllers/ItemController.php

177 lines
4.3 KiB
PHP
Raw Normal View History

2016-10-10 10:57:39 +02:00
<?php
namespace App\Http\Controllers;
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;
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;
private $storage;
2016-10-10 10:57:39 +02:00
/**
* Get the amount of loading items and create an instance for 'item'.
2016-10-10 10:57:39 +02:00
*
* @param Item $item
* @param Storage $storage
2016-10-10 10:57:39 +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;
$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 [
'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.
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')
]);
}
/**
* Create a new movie / tv show.
2016-10-10 10:57:39 +02:00
*
* @param TMDB $tmdb
* @return Item
*/
public function add(TMDB $tmdb, Storage $storage, Episode $episode, AlternativeTitle $alternativeTitle)
2016-10-10 10:57:39 +02:00
{
$data = Input::get('item');
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);
}
$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();
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);
}
}
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()
{
$tmdbId = Input::get('tmdb_id');
2016-11-30 15:26:38 +01:00
$season = Input::get('season');
$seen = Input::get('seen');
$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();
}
}
}