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;
|
2020-10-22 17:41:59 +00:00
|
|
|
use CodeIgniter\I18n\Time;
|
2020-07-10 12:20:25 +00:00
|
|
|
|
|
|
|
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-11-24 20:18:08 +00:00
|
|
|
'transcript' => 'ext_in[transcript,txt,html,srt,json]',
|
|
|
|
'chapters' => 'ext_in[chapters,json]',
|
2020-10-22 17:41:59 +00:00
|
|
|
'publication_date' => 'valid_date[Y-m-d H:i]|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'),
|
2020-10-29 15:45:19 +00:00
|
|
|
'description_markdown' => $this->request->getPost('description'),
|
2020-07-16 10:08:23 +00:00
|
|
|
'image' => $this->request->getFile('image'),
|
2020-11-24 20:18:08 +00:00
|
|
|
'transcript' => $this->request->getFile('transcript'),
|
|
|
|
'chapters' => $this->request->getFile('chapters'),
|
2020-10-02 15:38:16 +00:00
|
|
|
'parental_advisory' =>
|
|
|
|
$this->request->getPost('parental_advisory') !== 'undefined'
|
|
|
|
? $this->request->getPost('parental_advisory')
|
|
|
|
: null,
|
2020-10-29 17:25:15 +00:00
|
|
|
'number' => $this->request->getPost('episode_number')
|
|
|
|
? $this->request->getPost('episode_number')
|
|
|
|
: null,
|
|
|
|
'season_number' => $this->request->getPost('season_number')
|
|
|
|
? $this->request->getPost('season_number')
|
|
|
|
: null,
|
2020-07-16 10:08:23 +00:00
|
|
|
'type' => $this->request->getPost('type'),
|
2020-10-29 15:45:19 +00:00
|
|
|
'is_blocked' => $this->request->getPost('block') == 'yes',
|
2020-08-14 18:27:57 +00:00
|
|
|
'created_by' => user(),
|
|
|
|
'updated_by' => user(),
|
2020-10-22 17:41:59 +00:00
|
|
|
'published_at' => Time::createFromFormat(
|
|
|
|
'Y-m-d H:i',
|
|
|
|
$this->request->getPost('publication_date'),
|
|
|
|
$this->request->getPost('client_timezone')
|
|
|
|
)->setTimezone('UTC'),
|
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-29 15:45:19 +00:00
|
|
|
// update podcast's episode_description_footer_markdown if changed
|
2020-10-02 15:38:16 +00:00
|
|
|
$podcastModel = new PodcastModel();
|
|
|
|
|
2020-10-29 15:45:19 +00:00
|
|
|
if ($this->podcast->hasChanged('episode_description_footer_markdown')) {
|
|
|
|
$this->podcast->episode_description_footer_markdown = $this->request->getPost(
|
2020-10-02 15:38:16 +00:00
|
|
|
'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-11-24 20:18:08 +00:00
|
|
|
'transcript' => 'ext_in[transcript,txt,html,srt,json]',
|
|
|
|
'chapters' => 'ext_in[chapters,json]',
|
2020-10-22 17:41:59 +00:00
|
|
|
'publication_date' => 'valid_date[Y-m-d H:i]|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');
|
2020-10-29 15:45:19 +00:00
|
|
|
$this->episode->description_markdown = $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-10-29 17:25:15 +00:00
|
|
|
$this->episode->number = $this->request->getPost('episode_number')
|
|
|
|
? $this->request->getPost('episode_number')
|
|
|
|
: null;
|
2020-07-16 10:08:23 +00:00
|
|
|
$this->episode->season_number = $this->request->getPost('season_number')
|
|
|
|
? $this->request->getPost('season_number')
|
|
|
|
: null;
|
|
|
|
$this->episode->type = $this->request->getPost('type');
|
2020-10-29 15:45:19 +00:00
|
|
|
$this->episode->is_blocked = $this->request->getPost('block') == 'yes';
|
2020-10-22 17:41:59 +00:00
|
|
|
$this->episode->published_at = Time::createFromFormat(
|
|
|
|
'Y-m-d H:i',
|
2020-08-14 18:27:57 +00:00
|
|
|
$this->request->getPost('publication_date'),
|
2020-10-22 17:41:59 +00:00
|
|
|
$this->request->getPost('client_timezone')
|
|
|
|
)->setTimezone('UTC');
|
2020-08-14 18:27:57 +00:00
|
|
|
$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-11-24 20:18:08 +00:00
|
|
|
$transcript = $this->request->getFile('transcript');
|
|
|
|
if ($transcript->isValid()) {
|
|
|
|
$this->episode->transcript = $transcript;
|
|
|
|
}
|
|
|
|
$chapters = $this->request->getFile('chapters');
|
|
|
|
if ($chapters->isValid()) {
|
|
|
|
$this->episode->chapters = $chapters;
|
|
|
|
}
|
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 (!$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-29 15:45:19 +00:00
|
|
|
// update podcast's episode_description_footer_markdown if changed
|
|
|
|
$this->podcast->episode_description_footer_markdown = $this->request->getPost(
|
2020-10-02 15:38:16 +00:00
|
|
|
'description_footer'
|
|
|
|
);
|
|
|
|
|
2020-10-29 15:45:19 +00:00
|
|
|
if ($this->podcast->hasChanged('episode_description_footer_markdown')) {
|
2020-10-02 15:38:16 +00:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
2020-11-24 20:18:08 +00:00
|
|
|
public function transcriptDelete()
|
|
|
|
{
|
|
|
|
unlink($this->episode->transcript);
|
|
|
|
$this->episode->transcript_uri = null;
|
|
|
|
|
|
|
|
$episodeModel = new EpisodeModel();
|
|
|
|
|
|
|
|
if (!$episodeModel->update($this->episode->id, $this->episode)) {
|
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withInput()
|
|
|
|
->with('errors', $episodeModel->errors());
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function chaptersDelete()
|
|
|
|
{
|
|
|
|
unlink($this->episode->chapters);
|
|
|
|
$this->episode->chapters_uri = null;
|
|
|
|
|
|
|
|
$episodeModel = new EpisodeModel();
|
|
|
|
|
|
|
|
if (!$episodeModel->update($this->episode->id, $this->episode)) {
|
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withInput()
|
|
|
|
->with('errors', $episodeModel->errors());
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|