182 lines
5.9 KiB
PHP
182 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Enums\PlaylistContentType;
|
|
use App\Helpers\LengthFormatter;
|
|
use App\Models\File;
|
|
use App\Models\Playlist;
|
|
use App\Models\PlaylistContent;
|
|
use App\Models\SmartBlock;
|
|
use DateInterval;
|
|
use DateTime;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class PlaylistController extends Controller
|
|
{
|
|
|
|
/**
|
|
* List all playlists
|
|
* @return string
|
|
*/
|
|
public function index(Request $request) {
|
|
if (!isset($request->per_page) || is_null($request)) {
|
|
$pagination = 5;
|
|
} else {
|
|
$pagination = $request->per_page;
|
|
}
|
|
|
|
return Playlist::searchFilter($request)
|
|
->with(['creator', 'tracks.file', 'tracks.block', 'tracks.block.criteria', 'tracks.block.creator'])
|
|
->orderBy('name')
|
|
->paginate($pagination)
|
|
->toJson();
|
|
}
|
|
|
|
/**
|
|
* Save a playlist and its tracks list to the DB
|
|
* @param Request $request
|
|
* @return mixed
|
|
* @throws \DateMalformedIntervalStringException
|
|
*/
|
|
public function store(Request $request) {
|
|
$user = Auth::user();
|
|
$request->validate([
|
|
'name' => 'required',
|
|
'tracks' => 'required|array'
|
|
]);
|
|
|
|
$length = 0;
|
|
|
|
$dbPlaylist = Playlist::create([
|
|
'name' => $request->get('name'),
|
|
'creator_id' => $user->id,
|
|
'description' => $request->description,
|
|
]);
|
|
// dd($request->tracks);
|
|
foreach($request->tracks as $key => $file) {
|
|
if (!isset($file['id'])) {
|
|
$file = $file['db_element'];
|
|
}
|
|
switch ($file['type']) {
|
|
case 'audioclip':
|
|
$model = File::whereId($file['id'])->first();
|
|
break;
|
|
case 'block':
|
|
$model = SmartBlock::whereId($file['id'])->first();
|
|
break;
|
|
case 'stream':
|
|
//Todo: $model = Stream::whereId($file['id'])->first();
|
|
break;
|
|
}
|
|
$modelTime = new LengthFormatter($model->length);
|
|
$modelCuein = new LengthFormatter($model->cuein);
|
|
$modelCueout = new LengthFormatter($model->cueout);
|
|
|
|
$length = $length + $modelTime->toSeconds();
|
|
|
|
PlaylistContent::create([
|
|
'playlist_id' => $dbPlaylist->id,
|
|
'file_id' => ($file['type'] === 'audioclip') ? $file['id'] : null,
|
|
'block_id' => ($file['type'] === 'block') ? $file['id'] : null,
|
|
'stream_id' => ($file['type'] === 'stream') ? $file['id'] : null,
|
|
'type' => PlaylistContentType::fromName($file['type']),
|
|
'position' => $key,
|
|
'trackoffset' => 0, //ToDo: understand this field
|
|
'cliplength' => $modelTime->toSeconds(),
|
|
'cuein' => $modelCuein->toSeconds() ?? 0,
|
|
'cueout' => $modelCueout->toSeconds() ?? $modelTime->toSeconds(),
|
|
'fadein' => '00:00:00',
|
|
'fadeout' => '00:00:00',
|
|
]);
|
|
}
|
|
|
|
$dbPlaylist->update(['length' => $length]);
|
|
|
|
return $dbPlaylist->with('tracks')->get()->toJson();
|
|
}
|
|
|
|
/**
|
|
* Return a playlist with its tracks list
|
|
* @param $id
|
|
* @return mixed
|
|
*/
|
|
public function show($id) {
|
|
return Playlist::whereId($id)->with('tracks')->with('tracks.file')->first()->toJson();
|
|
}
|
|
|
|
/**
|
|
* Update a playlist and its tracks list
|
|
* @param Request $request
|
|
* @param $id
|
|
* @return mixed
|
|
* @throws \DateMalformedIntervalStringException
|
|
*/
|
|
public function update(Request $request, $id) {
|
|
$user = Auth::user();
|
|
$request->validate([
|
|
'name' => 'required',
|
|
'tracks' => 'required|array'
|
|
]);
|
|
|
|
$length = 0;
|
|
|
|
$playlist = Playlist::whereId($id)->first();
|
|
$playlist->fill($request->all())->save();
|
|
foreach ($request->tracks as $key => $file) {
|
|
switch ($file['type']) {
|
|
case 'audioclip':
|
|
$model = File::whereId($file['id'])->first();
|
|
$data = [
|
|
'playlist_id' => $playlist->id,
|
|
'file_id' => $file['id'],
|
|
];
|
|
break;
|
|
case 'block':
|
|
//Todo: $model = Block::whereId($file['id'])->first();
|
|
$data = [
|
|
'playlist_id' => $playlist->id,
|
|
'block_id' => $file['id'],
|
|
];
|
|
break;
|
|
case 'stream':
|
|
//Todo: $model = Stream::whereId($file['id'])->first();
|
|
$data = [
|
|
'playlist_id' => $playlist->id,
|
|
'stream_id' => $file['id'],
|
|
];
|
|
break;
|
|
}
|
|
$modelTime = new LengthFormatter($model->length);
|
|
$length = $length + $modelTime->toSeconds();
|
|
PlaylistContent::updateOrCreate(
|
|
$data,
|
|
[
|
|
'type' => PlaylistContentType::fromName($file['type']),
|
|
'position' => $key,
|
|
'trackoffset' => 0, //ToDo: understand this field
|
|
'cliplength' => $model['length'],
|
|
'cuein' => $model['cuein'] ?? 0,
|
|
'cueout' => $model['cueout'] ?? 0,
|
|
'fadein' => '00:00:00',
|
|
'fadeout' => '00:00:00',
|
|
]
|
|
);
|
|
}
|
|
$playlist->update(['length' => $length]);
|
|
|
|
return $playlist->with('tracks')->get()->toJson();
|
|
}
|
|
|
|
/**
|
|
* Delete a playlist and its tracks list
|
|
* @param $id
|
|
* @return true
|
|
*/
|
|
public function destroy($id) {
|
|
PlaylistContent::where('playlist_id', $id)->delete();
|
|
Playlist::destroy($id);
|
|
return true;
|
|
}
|
|
}
|