2020-07-10 12:20:25 +00:00
|
|
|
<?php
|
2020-08-04 11:25:22 +00:00
|
|
|
|
2020-07-10 12:20:25 +00:00
|
|
|
/**
|
|
|
|
* @copyright 2020 Podlibre
|
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Controllers\Admin;
|
|
|
|
|
|
|
|
use App\Models\EpisodeModel;
|
|
|
|
use App\Models\PodcastModel;
|
|
|
|
|
|
|
|
class Episode extends BaseController
|
|
|
|
{
|
|
|
|
protected \App\Entities\Podcast $podcast;
|
|
|
|
protected ?\App\Entities\Episode $episode;
|
|
|
|
|
|
|
|
public function _remap($method, ...$params)
|
|
|
|
{
|
2020-07-31 16:05:10 +00:00
|
|
|
$this->podcast = (new PodcastModel())->find($params[0]);
|
2020-07-10 12:20:25 +00:00
|
|
|
|
|
|
|
if (count($params) > 1) {
|
|
|
|
if (
|
2020-08-04 11:25:22 +00:00
|
|
|
!($this->episode = (new EpisodeModel())
|
2020-07-10 12:20:25 +00:00
|
|
|
->where([
|
2020-07-16 10:08:23 +00:00
|
|
|
'id' => $params[1],
|
|
|
|
'podcast_id' => $params[0],
|
2020-07-10 12:20:25 +00:00
|
|
|
])
|
|
|
|
->first())
|
|
|
|
) {
|
|
|
|
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->$method();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function list()
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'podcast' => $this->podcast,
|
|
|
|
];
|
|
|
|
|
|
|
|
return view('admin/episode/list', $data);
|
|
|
|
}
|
|
|
|
|
2020-07-27 09:35:34 +00:00
|
|
|
public function view()
|
|
|
|
{
|
|
|
|
$data = ['episode' => $this->episode];
|
|
|
|
|
|
|
|
return view('admin/episode/view', $data);
|
|
|
|
}
|
|
|
|
|
2020-07-10 12:20:25 +00:00
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
helper(['form']);
|
|
|
|
|
2020-07-16 10:08:23 +00:00
|
|
|
$data = [
|
|
|
|
'podcast' => $this->podcast,
|
|
|
|
];
|
2020-07-10 12:20:25 +00:00
|
|
|
|
2020-07-16 10:08:23 +00:00
|
|
|
echo view('admin/episode/create', $data);
|
|
|
|
}
|
2020-07-10 12:20:25 +00:00
|
|
|
|
2020-07-16 10:08:23 +00:00
|
|
|
public function attemptCreate()
|
|
|
|
{
|
|
|
|
$rules = [
|
|
|
|
'enclosure' => 'uploaded[enclosure]|ext_in[enclosure,mp3,m4a]',
|
|
|
|
'image' =>
|
|
|
|
'uploaded[image]|is_image[image]|ext_in[image,jpg,png]|permit_empty',
|
|
|
|
];
|
|
|
|
|
|
|
|
if (!$this->validate($rules)) {
|
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withInput()
|
|
|
|
->with('errors', $this->validator->getErrors());
|
2020-07-10 12:20:25 +00:00
|
|
|
}
|
2020-07-16 10:08:23 +00:00
|
|
|
|
2020-08-04 11:25:22 +00:00
|
|
|
$newEpisode = new \App\Entities\Episode([
|
2020-07-16 10:08:23 +00:00
|
|
|
'podcast_id' => $this->podcast->id,
|
|
|
|
'title' => $this->request->getPost('title'),
|
|
|
|
'slug' => $this->request->getPost('slug'),
|
|
|
|
'enclosure' => $this->request->getFile('enclosure'),
|
|
|
|
'pub_date' => $this->request->getPost('pub_date'),
|
|
|
|
'description' => $this->request->getPost('description'),
|
|
|
|
'image' => $this->request->getFile('image'),
|
|
|
|
'explicit' => (bool) $this->request->getPost('explicit'),
|
|
|
|
'number' => $this->request->getPost('episode_number'),
|
|
|
|
'season_number' => $this->request->getPost('season_number'),
|
|
|
|
'type' => $this->request->getPost('type'),
|
|
|
|
'author_name' => $this->request->getPost('author_name'),
|
|
|
|
'author_email' => $this->request->getPost('author_email'),
|
|
|
|
'block' => (bool) $this->request->getPost('block'),
|
|
|
|
]);
|
|
|
|
|
2020-08-04 11:25:22 +00:00
|
|
|
$episodeModel = new EpisodeModel();
|
2020-07-16 10:08:23 +00:00
|
|
|
|
2020-08-04 11:25:22 +00:00
|
|
|
if (!$episodeModel->save($newEpisode)) {
|
2020-07-16 10:08:23 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withInput()
|
2020-08-04 11:25:22 +00:00
|
|
|
->with('errors', $episodeModel->errors());
|
2020-07-16 10:08:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->route('episode_list', [$this->podcast->id]);
|
2020-07-10 12:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function edit()
|
|
|
|
{
|
|
|
|
helper(['form']);
|
|
|
|
|
2020-07-16 10:08:23 +00:00
|
|
|
$data = [
|
|
|
|
'episode' => $this->episode,
|
|
|
|
];
|
2020-07-10 12:20:25 +00:00
|
|
|
|
2020-07-16 10:08:23 +00:00
|
|
|
echo view('admin/episode/edit', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function attemptEdit()
|
|
|
|
{
|
|
|
|
$rules = [
|
|
|
|
'enclosure' =>
|
|
|
|
'uploaded[enclosure]|ext_in[enclosure,mp3,m4a]|permit_empty',
|
|
|
|
'image' =>
|
|
|
|
'uploaded[image]|is_image[image]|ext_in[image,jpg,png]|permit_empty',
|
|
|
|
];
|
|
|
|
|
|
|
|
if (!$this->validate($rules)) {
|
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withInput()
|
|
|
|
->with('errors', $this->validator->getErrors());
|
|
|
|
}
|
2020-07-10 12:20:25 +00:00
|
|
|
|
2020-07-16 10:08:23 +00:00
|
|
|
$this->episode->title = $this->request->getPost('title');
|
|
|
|
$this->episode->slug = $this->request->getPost('slug');
|
|
|
|
$this->episode->pub_date = $this->request->getPost('pub_date');
|
|
|
|
$this->episode->description = $this->request->getPost('description');
|
|
|
|
$this->episode->explicit = (bool) $this->request->getPost('explicit');
|
|
|
|
$this->episode->number = $this->request->getPost('episode_number');
|
|
|
|
$this->episode->season_number = $this->request->getPost('season_number')
|
|
|
|
? $this->request->getPost('season_number')
|
|
|
|
: null;
|
|
|
|
$this->episode->type = $this->request->getPost('type');
|
|
|
|
$this->episode->author_name = $this->request->getPost('author_name');
|
|
|
|
$this->episode->author_email = $this->request->getPost('author_email');
|
|
|
|
$this->episode->block = (bool) $this->request->getPost('block');
|
|
|
|
|
|
|
|
$enclosure = $this->request->getFile('enclosure');
|
|
|
|
if ($enclosure->isValid()) {
|
|
|
|
$this->episode->enclosure = $enclosure;
|
2020-07-10 12:20:25 +00:00
|
|
|
}
|
2020-07-16 10:08:23 +00:00
|
|
|
$image = $this->request->getFile('image');
|
|
|
|
if ($image) {
|
|
|
|
$this->episode->image = $image;
|
|
|
|
}
|
|
|
|
|
2020-08-04 11:25:22 +00:00
|
|
|
$episodeModel = new EpisodeModel();
|
2020-07-16 10:08:23 +00:00
|
|
|
|
2020-08-04 11:25:22 +00:00
|
|
|
if (!$episodeModel->save($this->episode)) {
|
2020-07-16 10:08:23 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withInput()
|
2020-08-04 11:25:22 +00:00
|
|
|
->with('errors', $episodeModel->errors());
|
2020-07-16 10:08:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->route('episode_list', [$this->podcast->id]);
|
2020-07-10 12:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function delete()
|
|
|
|
{
|
2020-08-04 11:25:22 +00:00
|
|
|
(new EpisodeModel())->delete($this->episode->id);
|
2020-07-10 12:20:25 +00:00
|
|
|
|
2020-07-16 10:08:23 +00:00
|
|
|
return redirect()->route('episode_list', [$this->podcast->id]);
|
2020-07-10 12:20:25 +00:00
|
|
|
}
|
|
|
|
}
|