"app/Controllers/git@code.castopod.org:ajeremias/castopod.git" did not exist on "d882981b3a86c81921ce6b07d4cf61fc13983689"
Newer
Older

Yassine Doghri
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?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])->with(
'message',
lang('Soundbite.messages.createSuccess')
);

Yassine Doghri
committed
}
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())->deleteSoundbite($this->podcast->id, $this->episode->id, $soundbite->id);

Yassine Doghri
committed
} else {
(new ClipModel())->clearSoundbiteCache($this->podcast->id, $this->episode->id, $soundbite->id);

Yassine Doghri
committed
$mediaModel = new MediaModel();
// delete the soundbite file, the clip will be deleted on cascade

Yassine Doghri
committed
if (! $mediaModel->deleteMedia($soundbite->media)) {
return redirect()
->back()
->withInput()
->with('errors', $mediaModel->errors());
}
}
return redirect()->route('soundbites-list', [$this->podcast->id, $this->episode->id])->with(
'message',
lang('Soundbite.messages.deleteSuccess')
);