mirror of
https://github.com/ad-aures/castopod.git
synced 2026-04-04 15:26:43 +02:00
feat(soundbites): add soundbite list and creation forms with audio-clipper component
This commit is contained in:
parent
602654b99b
commit
de19317138
24 changed files with 582 additions and 331 deletions
163
modules/Admin/Controllers/SoundbiteController.php
Normal file
163
modules/Admin/Controllers/SoundbiteController.php
Normal file
|
|
@ -0,0 +1,163 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright 2020 Podlibre
|
||||
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
||||
* @link https://castopod.org/
|
||||
*/
|
||||
|
||||
namespace Modules\Admin\Controllers;
|
||||
|
||||
use App\Entities\Clip\Soundbite;
|
||||
use App\Entities\Episode;
|
||||
use App\Entities\Podcast;
|
||||
use App\Models\ClipModel;
|
||||
use App\Models\EpisodeModel;
|
||||
use App\Models\MediaModel;
|
||||
use App\Models\PodcastModel;
|
||||
use CodeIgniter\Exceptions\PageNotFoundException;
|
||||
use CodeIgniter\HTTP\RedirectResponse;
|
||||
|
||||
class SoundbiteController extends BaseController
|
||||
{
|
||||
protected Podcast $podcast;
|
||||
|
||||
protected Episode $episode;
|
||||
|
||||
public function _remap(string $method, string ...$params): mixed
|
||||
{
|
||||
if (
|
||||
($podcast = (new PodcastModel())->getPodcastById((int) $params[0])) === null
|
||||
) {
|
||||
throw PageNotFoundException::forPageNotFound();
|
||||
}
|
||||
|
||||
$this->podcast = $podcast;
|
||||
|
||||
if (count($params) > 1) {
|
||||
if (
|
||||
! ($episode = (new EpisodeModel())
|
||||
->where([
|
||||
'id' => $params[1],
|
||||
'podcast_id' => $params[0],
|
||||
])
|
||||
->first())
|
||||
) {
|
||||
throw PageNotFoundException::forPageNotFound();
|
||||
}
|
||||
|
||||
$this->episode = $episode;
|
||||
|
||||
unset($params[1]);
|
||||
unset($params[0]);
|
||||
}
|
||||
|
||||
return $this->{$method}(...$params);
|
||||
}
|
||||
|
||||
public function list(): string
|
||||
{
|
||||
$soundbitesBuilder = (new ClipModel('audio'))
|
||||
->where([
|
||||
'podcast_id' => $this->podcast->id,
|
||||
'episode_id' => $this->episode->id,
|
||||
'type' => 'audio',
|
||||
])
|
||||
->orderBy('created_at', 'desc');
|
||||
|
||||
$soundbites = $soundbitesBuilder->paginate(10);
|
||||
|
||||
$data = [
|
||||
'podcast' => $this->podcast,
|
||||
'episode' => $this->episode,
|
||||
'soundbites' => $soundbites,
|
||||
'pager' => $soundbitesBuilder->pager,
|
||||
];
|
||||
|
||||
replace_breadcrumb_params([
|
||||
0 => $this->podcast->title,
|
||||
1 => $this->episode->title,
|
||||
]);
|
||||
return view('episode/soundbites_list', $data);
|
||||
}
|
||||
|
||||
public function create(): string
|
||||
{
|
||||
helper(['form']);
|
||||
|
||||
$data = [
|
||||
'podcast' => $this->podcast,
|
||||
'episode' => $this->episode,
|
||||
];
|
||||
|
||||
replace_breadcrumb_params([
|
||||
0 => $this->podcast->title,
|
||||
1 => $this->episode->title,
|
||||
]);
|
||||
return view('episode/soundbites_new', $data);
|
||||
}
|
||||
|
||||
public function attemptCreate(): RedirectResponse
|
||||
{
|
||||
$rules = [
|
||||
'title' => 'required',
|
||||
'start_time' => 'required|greater_than_equal_to[0]',
|
||||
'duration' => 'required|greater_than[0]',
|
||||
];
|
||||
|
||||
if (! $this->validate($rules)) {
|
||||
return redirect()
|
||||
->back()
|
||||
->withInput()
|
||||
->with('errors', $this->validator->getErrors());
|
||||
}
|
||||
|
||||
$newSoundbite = new Soundbite([
|
||||
'title' => $this->request->getPost('title'),
|
||||
'start_time' => (float) $this->request->getPost('start_time'),
|
||||
'duration' => (float) $this->request->getPost('duration',),
|
||||
'type' => 'audio',
|
||||
'status' => '',
|
||||
'podcast_id' => $this->podcast->id,
|
||||
'episode_id' => $this->episode->id,
|
||||
'created_by' => user_id(),
|
||||
'updated_by' => user_id(),
|
||||
]);
|
||||
|
||||
$clipModel = new ClipModel('audio');
|
||||
if (! $clipModel->save($newSoundbite)) {
|
||||
return redirect()
|
||||
->back()
|
||||
->withInput()
|
||||
->with('errors', $clipModel->errors());
|
||||
}
|
||||
|
||||
return redirect()->route('soundbites-list', [$this->podcast->id, $this->episode->id]);
|
||||
}
|
||||
|
||||
public function delete(string $soundbiteId): RedirectResponse
|
||||
{
|
||||
$soundbite = (new ClipModel())->getSoundbiteById((int) $soundbiteId);
|
||||
|
||||
if ($soundbite === null) {
|
||||
throw PageNotFoundException::forPageNotFound();
|
||||
}
|
||||
|
||||
if ($soundbite->media === null) {
|
||||
// delete Clip directly
|
||||
(new ClipModel())->delete($soundbite->id);
|
||||
} else {
|
||||
$mediaModel = new MediaModel();
|
||||
if (! $mediaModel->deleteMedia($soundbite->media)) {
|
||||
return redirect()
|
||||
->back()
|
||||
->withInput()
|
||||
->with('errors', $mediaModel->errors());
|
||||
}
|
||||
}
|
||||
|
||||
return redirect()->route('soundbites-list', [$this->podcast->id, $this->episode->id]);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue