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
|
|
|
|
{
|
2020-08-05 17:26:04 +00:00
|
|
|
/**
|
|
|
|
* @var \App\Entities\Podcast
|
|
|
|
*/
|
|
|
|
protected $podcast;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \App\Entities\Episode|null
|
|
|
|
*/
|
|
|
|
protected $episode;
|
2020-07-10 12:20:25 +00:00
|
|
|
|
|
|
|
public function _remap($method, ...$params)
|
|
|
|
{
|
2020-09-04 09:09:26 +00:00
|
|
|
$this->podcast = (new PodcastModel())->getPodcastById($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()
|
|
|
|
{
|
2020-10-02 15:38:16 +00:00
|
|
|
$episodes = (new EpisodeModel())
|
|
|
|
->where('podcast_id', $this->podcast->id)
|
|
|
|
->orderBy('created_at', 'desc');
|
|
|
|
|
2020-07-10 12:20:25 +00:00
|
|
|
$data = [
|
|
|
|
'podcast' => $this->podcast,
|
2020-10-02 15:38:16 +00:00
|
|
|
'episodes' => $episodes->paginate(10),
|
|
|
|
'pager' => $episodes->pager,
|
2020-07-10 12:20:25 +00:00
|
|
|
];
|
|
|
|
|
2020-08-05 16:10:39 +00:00
|
|
|
replace_breadcrumb_params([
|
|
|
|
0 => $this->podcast->title,
|
|
|
|
]);
|
2020-07-10 12:20:25 +00:00
|
|
|
return view('admin/episode/list', $data);
|
|
|
|
}
|
|
|
|
|
2020-07-27 09:35:34 +00:00
|
|
|
public function view()
|
|
|
|
{
|
2020-10-02 15:38:16 +00:00
|
|
|
$data = [
|
|
|
|
'podcast' => $this->podcast,
|
|
|
|
'episode' => $this->episode,
|
|
|
|
];
|
2020-07-27 09:35:34 +00:00
|
|
|
|
2020-08-05 16:10:39 +00:00
|
|
|
replace_breadcrumb_params([
|
|
|
|
0 => $this->podcast->title,
|
|
|
|
1 => $this->episode->title,
|
|
|
|
]);
|
2020-07-27 09:35:34 +00:00
|
|
|
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-08-05 16:10:39 +00:00
|
|
|
replace_breadcrumb_params([
|
|
|
|
0 => $this->podcast->title,
|
|
|
|
]);
|
|
|
|
return view('admin/episode/create', $data);
|
2020-07-16 10:08:23 +00:00
|
|
|
}
|
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' =>
|
2020-09-08 11:45:17 +00:00
|
|
|
'is_image[image]|ext_in[image,jpg,png]|min_dims[image,1400,1400]|is_image_squared[image]',
|
2020-08-14 18:27:57 +00:00
|
|
|
'publication_date' => 'valid_date[Y-m-d]|permit_empty',
|
|
|
|
'publication_time' =>
|
|
|
|
'regex_match[/^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/]|permit_empty',
|
2020-07-16 10:08:23 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
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'),
|
2020-10-12 15:57:01 +00:00
|
|
|
'guid' => '',
|
2020-07-16 10:08:23 +00:00
|
|
|
'enclosure' => $this->request->getFile('enclosure'),
|
|
|
|
'description' => $this->request->getPost('description'),
|
|
|
|
'image' => $this->request->getFile('image'),
|
2020-10-02 15:38:16 +00:00
|
|
|
'parental_advisory' =>
|
|
|
|
$this->request->getPost('parental_advisory') !== 'undefined'
|
|
|
|
? $this->request->getPost('parental_advisory')
|
|
|
|
: null,
|
2020-07-16 10:08:23 +00:00
|
|
|
'number' => $this->request->getPost('episode_number'),
|
|
|
|
'season_number' => $this->request->getPost('season_number'),
|
|
|
|
'type' => $this->request->getPost('type'),
|
2020-08-14 18:27:57 +00:00
|
|
|
'block' => $this->request->getPost('block') == 'yes',
|
|
|
|
'created_by' => user(),
|
|
|
|
'updated_by' => user(),
|
2020-07-16 10:08:23 +00:00
|
|
|
]);
|
2020-08-14 18:27:57 +00:00
|
|
|
$newEpisode->setPublishedAt(
|
|
|
|
$this->request->getPost('publication_date'),
|
|
|
|
$this->request->getPost('publication_time')
|
|
|
|
);
|
2020-07-16 10:08:23 +00:00
|
|
|
|
2020-08-04 11:25:22 +00:00
|
|
|
$episodeModel = new EpisodeModel();
|
2020-07-16 10:08:23 +00:00
|
|
|
|
2020-10-02 15:38:16 +00:00
|
|
|
if (!($newEpisodeId = $episodeModel->insert($newEpisode, true))) {
|
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
|
|
|
}
|
|
|
|
|
2020-10-02 15:38:16 +00:00
|
|
|
// update podcast's episode_description_footer if changed
|
|
|
|
$podcastModel = new PodcastModel();
|
|
|
|
|
|
|
|
if ($this->podcast->hasChanged('episode_description_footer')) {
|
|
|
|
$this->podcast->episode_description_footer = $this->request->getPost(
|
|
|
|
'description_footer'
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$podcastModel->update($this->podcast->id, $this->podcast)) {
|
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withInput()
|
|
|
|
->with('errors', $podcastModel->errors());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->route('episode-view', [
|
|
|
|
$this->podcast->id,
|
|
|
|
$newEpisodeId,
|
|
|
|
]);
|
2020-07-10 12:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function edit()
|
|
|
|
{
|
|
|
|
helper(['form']);
|
|
|
|
|
2020-07-16 10:08:23 +00:00
|
|
|
$data = [
|
2020-10-02 15:38:16 +00:00
|
|
|
'podcast' => $this->podcast,
|
2020-07-16 10:08:23 +00:00
|
|
|
'episode' => $this->episode,
|
|
|
|
];
|
2020-07-10 12:20:25 +00:00
|
|
|
|
2020-08-05 16:10:39 +00:00
|
|
|
replace_breadcrumb_params([
|
|
|
|
0 => $this->podcast->title,
|
|
|
|
1 => $this->episode->title,
|
|
|
|
]);
|
|
|
|
return view('admin/episode/edit', $data);
|
2020-07-16 10:08:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function attemptEdit()
|
|
|
|
{
|
|
|
|
$rules = [
|
|
|
|
'enclosure' =>
|
|
|
|
'uploaded[enclosure]|ext_in[enclosure,mp3,m4a]|permit_empty',
|
|
|
|
'image' =>
|
2020-09-08 11:45:17 +00:00
|
|
|
'is_image[image]|ext_in[image,jpg,png]|min_dims[image,1400,1400]|is_image_squared[image]',
|
2020-08-14 18:27:57 +00:00
|
|
|
'publication_date' => 'valid_date[Y-m-d]|permit_empty',
|
|
|
|
'publication_time' =>
|
|
|
|
'regex_match[/^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/]|permit_empty',
|
2020-07-16 10:08:23 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
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->description = $this->request->getPost('description');
|
2020-10-02 15:38:16 +00:00
|
|
|
$this->episode->parental_advisory =
|
|
|
|
$this->request->getPost('parental_advisory') !== 'undefined'
|
|
|
|
? $this->request->getPost('parental_advisory')
|
|
|
|
: null;
|
2020-07-16 10:08:23 +00:00
|
|
|
$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');
|
2020-08-14 18:27:57 +00:00
|
|
|
$this->episode->block = $this->request->getPost('block') == 'yes';
|
|
|
|
$this->episode->setPublishedAt(
|
|
|
|
$this->request->getPost('publication_date'),
|
|
|
|
$this->request->getPost('publication_time')
|
|
|
|
);
|
|
|
|
$this->episode->updated_by = user();
|
2020-07-16 10:08:23 +00:00
|
|
|
|
|
|
|
$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-10-02 15:38:16 +00:00
|
|
|
if (!$episodeModel->update($this->episode->id, $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
|
|
|
}
|
|
|
|
|
2020-10-02 15:38:16 +00:00
|
|
|
// update podcast's episode_description_footer if changed
|
|
|
|
$this->podcast->episode_description_footer = $this->request->getPost(
|
|
|
|
'description_footer'
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($this->podcast->hasChanged('episode_description_footer')) {
|
|
|
|
$podcastModel = new PodcastModel();
|
|
|
|
if (!$podcastModel->update($this->podcast->id, $this->podcast)) {
|
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withInput()
|
|
|
|
->with('errors', $podcastModel->errors());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->route('episode-view', [
|
|
|
|
$this->podcast->id,
|
|
|
|
$this->episode->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-08-14 18:27:57 +00:00
|
|
|
return redirect()->route('episode-list', [$this->podcast->id]);
|
2020-07-10 12:20:25 +00:00
|
|
|
}
|
|
|
|
}
|