Newer
Older

Yassine Doghri
committed
<?php

Yassine Doghri
committed

Yassine Doghri
committed
declare(strict_types=1);

Yassine Doghri
committed
/**
* @copyright 2020 Ad Aures

Yassine Doghri
committed
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/

Yassine Doghri
committed

Yassine Doghri
committed
namespace Modules\Admin\Controllers;

Yassine Doghri
committed

Yassine Doghri
committed
use App\Entities\Location;
use App\Entities\Podcast;
use App\Entities\Post;
use App\Models\ActorModel;

Yassine Doghri
committed
use App\Models\CategoryModel;
use App\Models\EpisodeModel;

Yassine Doghri
committed
use App\Models\LanguageModel;
use App\Models\PodcastModel;
use App\Models\PostModel;
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\I18n\Time;
use Modules\Analytics\Models\AnalyticsPodcastByCountryModel;
use Modules\Analytics\Models\AnalyticsPodcastByEpisodeModel;
use Modules\Analytics\Models\AnalyticsPodcastByHourModel;
use Modules\Analytics\Models\AnalyticsPodcastByPlayerModel;
use Modules\Analytics\Models\AnalyticsPodcastByRegionModel;
use Modules\Analytics\Models\AnalyticsPodcastModel;
use Modules\Analytics\Models\AnalyticsWebsiteByBrowserModel;
use Modules\Analytics\Models\AnalyticsWebsiteByEntryPageModel;
use Modules\Analytics\Models\AnalyticsWebsiteByRefererModel;
use Modules\Media\Entities\Image;
use Modules\Media\FileManagers\FileManagerInterface;
use Modules\Media\Models\MediaModel;

Yassine Doghri
committed
class PodcastController extends BaseController

Yassine Doghri
committed
{
protected Podcast $podcast;

Yassine Doghri
committed
public function _remap(string $method, string ...$params): mixed

Yassine Doghri
committed
{
return $this->{$method}();

Yassine Doghri
committed
}
($podcast = (new PodcastModel())->getPodcastById((int) $params[0])) instanceof Podcast
return $this->{$method}();

Yassine Doghri
committed
}
throw PageNotFoundException::forPageNotFound();

Yassine Doghri
committed
}
public function list(): string

Yassine Doghri
committed
{

Yassine Doghri
committed
if (auth()->user()->can('podcasts.view')) {

Yassine Doghri
committed
$data = [

Yassine Doghri
committed
'podcasts' => (new PodcastModel())->findAll(),

Yassine Doghri
committed
];
} else {
$data = [

Yassine Doghri
committed
'podcasts' => get_user_podcasts(auth()->user()),
];
return view('podcast/list', $data);
}
public function view(): string

Yassine Doghri
committed
{
$data = [
'podcast' => $this->podcast,
];

Yassine Doghri
committed
replace_breadcrumb_params([

Yassine Doghri
committed
0 => $this->podcast->at_handle,
]);
return view('podcast/view', $data);

Yassine Doghri
committed
}
public function viewAnalytics(): string
$data = [
'podcast' => $this->podcast,
];
replace_breadcrumb_params([

Yassine Doghri
committed
0 => $this->podcast->at_handle,
]);
return view('podcast/analytics/index', $data);

Benjamin Bellamy
committed
}
public function viewAnalyticsWebpages(): string

Benjamin Bellamy
committed
{
$data = [
'podcast' => $this->podcast,
];

Benjamin Bellamy
committed
replace_breadcrumb_params([

Yassine Doghri
committed
0 => $this->podcast->at_handle,
]);
return view('podcast/analytics/webpages', $data);

Benjamin Bellamy
committed
}
public function viewAnalyticsLocations(): string

Benjamin Bellamy
committed
{
$data = [
'podcast' => $this->podcast,
];

Benjamin Bellamy
committed
replace_breadcrumb_params([

Yassine Doghri
committed
0 => $this->podcast->at_handle,
]);
return view('podcast/analytics/locations', $data);

Benjamin Bellamy
committed
}
public function viewAnalyticsUniqueListeners(): string

Benjamin Bellamy
committed
{
$data = [
'podcast' => $this->podcast,
];

Benjamin Bellamy
committed
replace_breadcrumb_params([

Yassine Doghri
committed
0 => $this->podcast->at_handle,
]);
return view('podcast/analytics/unique_listeners', $data);

Benjamin Bellamy
committed
}
public function viewAnalyticsListeningTime(): string
$data = [
'podcast' => $this->podcast,
];
replace_breadcrumb_params([

Yassine Doghri
committed
0 => $this->podcast->at_handle,
]);
return view('podcast/analytics/listening_time', $data);
public function viewAnalyticsTimePeriods(): string
$data = [
'podcast' => $this->podcast,
];
replace_breadcrumb_params([

Yassine Doghri
committed
0 => $this->podcast->at_handle,
]);
return view('podcast/analytics/time_periods', $data);
public function viewAnalyticsPlayers(): string

Benjamin Bellamy
committed
{
$data = [
'podcast' => $this->podcast,
];

Benjamin Bellamy
committed
replace_breadcrumb_params([

Yassine Doghri
committed
0 => $this->podcast->at_handle,
]);
return view('podcast/analytics/players', $data);
public function create(): string

Yassine Doghri
committed
{
helper(['form', 'misc']);
$languageOptions = (new LanguageModel())->getLanguageOptions();
$categoryOptions = (new CategoryModel())->getCategoryOptions();
$data = [
'languageOptions' => $languageOptions,
'categoryOptions' => $categoryOptions,
'browserLang' => get_browser_language($this->request->getServer('HTTP_ACCEPT_LANGUAGE')),
];
return view('podcast/create', $data);
}
public function attemptCreate(): RedirectResponse
{
$rules = [
'cover' => 'uploaded[cover]|is_image[cover]|ext_in[cover,jpg,jpeg,png]|min_dims[cover,1400,1400]|is_image_ratio[cover,1,1]',
'banner' => 'is_image[banner]|ext_in[banner,jpg,jpeg,png]|min_dims[banner,1500,500]|is_image_ratio[banner,3,1]',
];
if (! $this->validate($rules)) {
return redirect()
->back()
->withInput()
->with('errors', $this->validator->getErrors());
}

Yassine Doghri
committed
$db = db_connect();
$db->transStart();
$newPodcast = new Podcast([
'created_by' => user_id(),
'updated_by' => user_id(),
'title' => $this->request->getPost('title'),
'handle' => $this->request->getPost('handle'),
'cover' => $this->request->getFile('cover'),
'banner' => $this->request->getFile('banner'),
'description_markdown' => $this->request->getPost('description'),
'language_code' => $this->request->getPost('language'),
'category_id' => $this->request->getPost('category'),
'parental_advisory' => $this->request->getPost('parental_advisory') !== 'undefined'
? $this->request->getPost('parental_advisory')
: null,
'owner_name' => $this->request->getPost('owner_name'),
'owner_email' => $this->request->getPost('owner_email'),
'is_owner_email_removed_from_feed' => $this->request->getPost('is_owner_email_removed_from_feed') === 'yes',
'publisher' => $this->request->getPost('publisher'),
'type' => $this->request->getPost('type'),
Guy Martin (Dwev)
committed
'medium' => $this->request->getPost('medium'),
'copyright' => $this->request->getPost('copyright'),
'location' => $this->request->getPost('location_name') === '' ? null : new Location(
$this->request->getPost('location_name')
),
'verify_txt' => $this->request->getPost('verify_txt'),
'custom_rss_string' => $this->request->getPost('custom_rss'),
'is_blocked' => $this->request->getPost('block') === 'yes',
'is_completed' => $this->request->getPost('complete') === 'yes',
'is_locked' => $this->request->getPost('lock') === 'yes',
'is_premium_by_default' => $this->request->getPost('premium_by_default') === 'yes',
'published_at' => null,
]);

Yassine Doghri
committed
$podcastModel = new PodcastModel();
if (! ($newPodcastId = $podcastModel->insert($newPodcast, true))) {
$db->transRollback();
return redirect()
->back()
->withInput()

Yassine Doghri
committed
->with('errors', $podcastModel->errors());

Yassine Doghri
committed
}

Yassine Doghri
committed
// generate podcast roles and permissions
// before setting current user as podcast admin
config('AuthGroups')

Yassine Doghri
committed
->generatePodcastAuthorizations($newPodcastId);
add_podcast_group(auth()->user(), (int) $newPodcastId, setting('AuthGroups.mostPowerfulPodcastGroup'));
// set Podcast categories
(new CategoryModel())->setPodcastCategories(
(int) $newPodcastId,

Yassine Doghri
committed
$this->request->getPost('other_categories') ?? [],
);

Yassine Doghri
committed
// OP3
service('settings')
->set('Analytics.enableOP3', $this->request->getPost('enable_op3') === 'yes', 'podcast:' . $newPodcastId);
$db->transComplete();
return redirect()->route('podcast-view', [$newPodcastId])->with(
'message',
lang('Podcast.messages.createSuccess')
);

Yassine Doghri
committed
}
public function edit(): string

Yassine Doghri
committed
{
helper('form');

Yassine Doghri
committed
$languageOptions = (new LanguageModel())->getLanguageOptions();
$categoryOptions = (new CategoryModel())->getCategoryOptions();
$data = [
'podcast' => $this->podcast,
'languageOptions' => $languageOptions,
'categoryOptions' => $categoryOptions,
];

Yassine Doghri
committed
replace_breadcrumb_params([

Yassine Doghri
committed
0 => $this->podcast->at_handle,
]);
return view('podcast/edit', $data);

Yassine Doghri
committed
public function attemptEdit(): RedirectResponse
{
$rules = [
'cover' => 'is_image[cover]|ext_in[cover,jpg,jpeg,png]|min_dims[cover,1400,1400]|is_image_ratio[cover,1,1]',
'banner' => 'is_image[banner]|ext_in[banner,jpg,jpeg,png]|min_dims[banner,1500,500]|is_image_ratio[banner,3,1]',
];
if (! $this->validate($rules)) {
return redirect()
->back()
->withInput()
->with('errors', $this->validator->getErrors());
}
$this->podcast->updated_by = (int) user_id();
$this->podcast->title = $this->request->getPost('title');

Yassine Doghri
committed
$this->podcast->description_markdown = $this->request->getPost('description');
$this->podcast->setCover($this->request->getFile('cover'));
$this->podcast->setBanner($this->request->getFile('banner'));
$this->podcast->language_code = $this->request->getPost('language');
$this->podcast->category_id = $this->request->getPost('category');
$this->podcast->parental_advisory =
$this->request->getPost('parental_advisory') !== 'undefined'
? $this->request->getPost('parental_advisory')
: null;
$this->podcast->publisher = $this->request->getPost('publisher');
$this->podcast->owner_name = $this->request->getPost('owner_name');
$this->podcast->owner_email = $this->request->getPost('owner_email');
$this->podcast->is_owner_email_removed_from_feed = $this->request->getPost(
'is_owner_email_removed_from_feed'
) === 'yes';
$this->podcast->type = $this->request->getPost('type');
Guy Martin (Dwev)
committed
$this->podcast->medium = $this->request->getPost('medium');
$this->podcast->copyright = $this->request->getPost('copyright');
$this->podcast->location = $this->request->getPost('location_name') === '' ? null : new Location(
$this->request->getPost('location_name')
);
$this->podcast->verify_txt = $this->request->getPost('verify_txt') === '' ? null : $this->request->getPost(
'verify_txt'
);

Yassine Doghri
committed
$this->podcast->custom_rss_string = $this->request->getPost('custom_rss');

Yassine Doghri
committed
$this->podcast->new_feed_url = $this->request->getPost('new_feed_url') === '' ? null : $this->request->getPost(
'new_feed_url'
);

Yassine Doghri
committed
$this->podcast->is_blocked = $this->request->getPost('block') === 'yes';
$this->podcast->is_completed =
$this->request->getPost('complete') === 'yes';
$this->podcast->is_locked = $this->request->getPost('lock') === 'yes';
$this->podcast->is_premium_by_default = $this->request->getPost('premium_by_default') === 'yes';

Yassine Doghri
committed
// republish on websub hubs upon edit
$this->podcast->is_published_on_hubs = false;
$db = db_connect();
$db->transStart();
$podcastModel = new PodcastModel();
if (! $podcastModel->update($this->podcast->id, $this->podcast)) {
$db->transRollback();
return redirect()
->back()
->withInput()

Yassine Doghri
committed
->with('errors', $podcastModel->errors());

Yassine Doghri
committed
}
// set Podcast categories
(new CategoryModel())->setPodcastCategories(
$this->podcast->id,

Yassine Doghri
committed
$this->request->getPost('other_categories') ?? [],
);

Yassine Doghri
committed
// enable/disable OP3?
service('settings')
->set(
'Analytics.enableOP3',
$this->request->getPost('enable_op3') === 'yes',
'podcast:' . $this->podcast->id
);
// New feed url redirect
service('settings')
->set(
'Podcast.redirect_to_new_feed',
$this->request->getPost('redirect_to_new_feed') === 'yes',
'podcast:' . $this->podcast->id
);
$db->transComplete();
return redirect()->route('podcast-edit', [$this->podcast->id])->with(
'message',
lang('Podcast.messages.editSuccess')
);
}

Yassine Doghri
committed
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
public function monetizationOther(): string
{
helper('form');
$data = [
'podcast' => $this->podcast,
];
replace_breadcrumb_params([
0 => $this->podcast->at_handle,
]);
return view('podcast/monetization_other', $data);
}
public function monetizationOtherAction(): RedirectResponse
{
if (
($partnerId = $this->request->getPost('partner_id')) === '' ||
($partnerLinkUrl = $this->request->getPost('partner_link_url')) === '' ||
($partnerImageUrl = $this->request->getPost('partner_image_url')) === '') {
$partnerId = null;
$partnerLinkUrl = null;
$partnerImageUrl = null;
}
$this->podcast->payment_pointer = $this->request->getPost(
'payment_pointer'
) === '' ? null : $this->request->getPost('payment_pointer');
$this->podcast->partner_id = $partnerId;
$this->podcast->partner_link_url = $partnerLinkUrl;
$this->podcast->partner_image_url = $partnerImageUrl;
$podcastModel = new PodcastModel();
if (! $podcastModel->update($this->podcast->id, $this->podcast)) {
return redirect()
->back()
->withInput()
->with('errors', $podcastModel->errors());
}
return redirect()->route('podcast-monetization-other', [$this->podcast->id])->with(
'message',
lang('Podcast.messages.editSuccess')
);
}

Yassine Doghri
committed
public function deleteBanner(): RedirectResponse
{
if (! $this->podcast->banner instanceof Image) {

Yassine Doghri
committed
return redirect()->back();
}
$db = db_connect();
$db->transStart();
$mediaModel = new MediaModel();

Yassine Doghri
committed
if (! $mediaModel->deleteMedia($this->podcast->banner)) {

Yassine Doghri
committed
return redirect()
->back()
->withInput()
->with('errors', $mediaModel->errors());

Yassine Doghri
committed
}

Yassine Doghri
committed
(new PodcastModel())->clearCache([
'id' => $this->podcast->id,
]);

Yassine Doghri
committed
// remove banner url from actor
$actor = (new ActorModel())->getActorById($this->podcast->actor_id);
if ($actor instanceof Actor) {
$actor->cover_image_url = null;
$actor->cover_image_mimetype = null;
(new ActorModel())->update($actor->id, $actor);
}
$db->transComplete();

Yassine Doghri
committed
return redirect()->back();
}
public function latestEpisodes(int $limit, int $podcastId): string
{
$episodes = (new EpisodeModel())
->where('podcast_id', $podcastId)

Yassine Doghri
committed
->orderBy('-`published_at`', '', false)
->orderBy('created_at', 'desc')
->findAll($limit);
return view('podcast/latest_episodes', [
'episodes' => $episodes,
'podcast' => (new PodcastModel())->getPodcastById($podcastId),
]);

Yassine Doghri
committed
}
public function delete(): string

Yassine Doghri
committed
{

Yassine Doghri
committed
$data = [
'podcast' => $this->podcast,
];
replace_breadcrumb_params([

Yassine Doghri
committed
0 => $this->podcast->at_handle,
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
]);
return view('podcast/delete', $data);
}
public function attemptDelete(): RedirectResponse
{
$rules = [
'understand' => 'required',
];
if (! $this->validate($rules)) {
return redirect()
->back()
->withInput()
->with('errors', $this->validator->getErrors());
}
$db = db_connect();
$db->transStart();
//delete podcast episodes
$podcastEpisodes = (new EpisodeModel())->where('podcast_id', $this->podcast->id)
->findAll();
foreach ($podcastEpisodes as $podcastEpisode) {
$episodeModel = new EpisodeModel();
if (! $episodeModel->delete($podcastEpisode->id)) {
$db->transRollback();
return redirect()
->back()
->withInput()
->with('errors', $episodeModel->errors());
}
$episodeMediaList = [$podcastEpisode->transcript, $podcastEpisode->chapters, $podcastEpisode->audio];
//only delete episode cover if different from podcast's
if ($podcastEpisode->cover_id !== null) {
$episodeMediaList[] = $podcastEpisode->cover;
}
$mediaModel = new MediaModel();
foreach ($episodeMediaList as $episodeMedia) {
if ($episodeMedia !== null && ! $mediaModel->delete($episodeMedia->id)) {
$db->transRollback();
return redirect()
->back()
->withInput()
->with('error', lang('Podcast.messages.deleteEpisodeMediaError', [
'episode_slug' => $podcastEpisode->slug,
'type' => $episodeMedia->type,
]));
}
}
}
//delete podcast
$podcastModel = new PodcastModel();
if (! $podcastModel->delete($this->podcast->id)) {
$db->transRollback();
return redirect()
->back()
->withInput()
->with('errors', $podcastModel->errors());
}
//delete podcast media
$podcastMediaList = [
[
'type' => 'cover',
'file' => $this->podcast->cover,
],
];
if ($this->podcast->banner_id !== null) {
$podcastMediaList[] =
[
'type' => 'banner',
'file' => $this->podcast->banner,
$mediaModel = new MediaModel();
foreach ($podcastMediaList as $podcastMedia) {
if ($podcastMedia['file'] instanceof Image && ! $mediaModel->delete($podcastMedia['file']->id)) {
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
$db->transRollback();
return redirect()
->back()
->withInput()
->with('error', lang('Podcast.messages.deletePodcastMediaError', [
'type' => $podcastMedia['type'],
]));
}
}
//delete podcast actor
$actorModel = new ActorModel();
if (! $actorModel->delete($this->podcast->actor_id)) {
$db->transRollback();
return redirect()
->back()
->withInput()
->with('errors', $actorModel->errors());
}
//delete podcast analytics
$analyticsModels = [
new AnalyticsPodcastModel(),
new AnalyticsPodcastByCountryModel(),
new AnalyticsPodcastByEpisodeModel(),
new AnalyticsPodcastByHourModel(),
new AnalyticsPodcastByPlayerModel(),
new AnalyticsPodcastByRegionModel(),
new AnalyticsWebsiteByBrowserModel(),
new AnalyticsWebsiteByEntryPageModel(),
new AnalyticsWebsiteByRefererModel(),
];
foreach ($analyticsModels as $analyticsModel) {
if (! $analyticsModel->where([
'podcast_id' => $this->podcast->id,
])->delete()) {
$db->transRollback();
return redirect()
->back()
->withInput()
->with('errors', $analyticsModel->errors());
}
}
$db->transComplete();
/** @var FileManagerInterface $fileManager */
$fileManager = service('file_manager');
//delete podcast media files and folder
$folder = 'podcasts/' . $this->podcast->handle;
if (! $fileManager->deleteAll($folder)) {
return redirect()->route('podcast-list')
->with('message', lang('Podcast.messages.deleteSuccess', [
'podcast_handle' => $this->podcast->handle,
]))
->with('warning', lang('Podcast.messages.deletePodcastMediaFolderError', [
'folder_path' => $folder,
]));
}
return redirect()->route('podcast-list')
->with('message', lang('Podcast.messages.deleteSuccess', [
'podcast_handle' => $this->podcast->handle,
]));

Yassine Doghri
committed
}
public function publish(): string | RedirectResponse
{
helper(['form']);
$data = [
'podcast' => $this->podcast,
];
replace_breadcrumb_params([

Yassine Doghri
committed
0 => $this->podcast->at_handle,
]);
return view('podcast/publish', $data);
}
public function attemptPublish(): RedirectResponse
{
if ($this->podcast->publication_status !== 'not_published') {
return redirect()->route('podcast-view', [$this->podcast->id])->with(
'error',
lang('Podcast.messages.publishError')
);
}
$rules = [
'publication_method' => 'required',
'scheduled_publication_date' => 'valid_date[Y-m-d H:i]|permit_empty',
];
if (! $this->validate($rules)) {
return redirect()
->back()
->withInput()
->with('errors', $this->validator->getErrors());
}

Yassine Doghri
committed
$validData = $this->validator->getValidated();
$db = db_connect();
$db->transStart();

Yassine Doghri
committed
$publishMethod = $validData['publication_method'];
if ($publishMethod === 'schedule') {

Yassine Doghri
committed
$scheduledPublicationDate = $validData['scheduled_publication_date'];
if ($scheduledPublicationDate) {
$this->podcast->published_at = Time::createFromFormat(
'Y-m-d H:i',
$scheduledPublicationDate,
$this->request->getPost('client_timezone'),
)->setTimezone(app_timezone());
} else {
$db->transRollback();
return redirect()
->back()
->withInput()
->with('error', lang('Podcast.messages.scheduleDateError'));
}
} else {
$this->podcast->published_at = Time::now();
}
$message = $this->request->getPost('message');
// only create post if message is not empty
if ($message !== '') {
$newPost = new Post([
'actor_id' => $this->podcast->actor_id,
'message' => $message,
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
'created_by' => user_id(),
]);
$newPost->published_at = $this->podcast->published_at;
$postModel = new PostModel();
if (! $postModel->addPost($newPost)) {
$db->transRollback();
return redirect()
->back()
->withInput()
->with('errors', $postModel->errors());
}
}
$episodes = (new EpisodeModel())
->where('podcast_id', $this->podcast->id)
->where('published_at !=', null)
->findAll();
foreach ($episodes as $episode) {
$episode->published_at = $this->podcast->published_at->addSeconds(1);
$episodeModel = new EpisodeModel();
if (! $episodeModel->update($episode->id, $episode)) {
$db->transRollback();
return redirect()
->back()
->withInput()
->with('errors', $episodeModel->errors());
}
$post = (new PostModel())->where('episode_id', $episode->id)
->first();
if ($post instanceof Post) {
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
$post->published_at = $episode->published_at;
$postModel = new PostModel();
if (! $postModel->update($post->id, $post)) {
$db->transRollback();
return redirect()
->back()
->withInput()
->with('errors', $postModel->errors());
}
}
}
$podcastModel = new PodcastModel();
if (! $podcastModel->update($this->podcast->id, $this->podcast)) {
$db->transRollback();
return redirect()
->back()
->withInput()
->with('errors', $podcastModel->errors());
}
$db->transComplete();
return redirect()->route('podcast-view', [$this->podcast->id]);
}
public function publishEdit(): string | RedirectResponse
{
helper(['form']);
$data = [
'podcast' => $this->podcast,
'post' => (new PostModel())
'actor_id' => $this->podcast->actor_id,
'episode_id' => null,
])
->first(),
];
replace_breadcrumb_params([

Yassine Doghri
committed
0 => $this->podcast->at_handle,
]);
return view('podcast/publish_edit', $data);
}
public function attemptPublishEdit(): RedirectResponse
{
if ($this->podcast->publication_status !== 'scheduled') {
return redirect()->route('podcast-view', [$this->podcast->id])->with(
'error',
lang('Podcast.messages.publishEditError')
);
}
$rules = [
'publication_method' => 'required',
'scheduled_publication_date' => 'valid_date[Y-m-d H:i]|permit_empty',
];
if (! $this->validate($rules)) {
return redirect()
->back()
->withInput()
->with('errors', $this->validator->getErrors());
}

Yassine Doghri
committed
$validData = $this->validator->getValidated();
$db = db_connect();
$db->transStart();

Yassine Doghri
committed
$publishMethod = $validData['publication_method'];
if ($publishMethod === 'schedule') {

Yassine Doghri
committed
$scheduledPublicationDate = $validData['scheduled_publication_date'];
if ($scheduledPublicationDate) {
$this->podcast->published_at = Time::createFromFormat(
'Y-m-d H:i',
$scheduledPublicationDate,
$this->request->getPost('client_timezone'),
)->setTimezone(app_timezone());
} else {
$db->transRollback();
return redirect()
->back()
->withInput()
->with('error', lang('Podcast.messages.scheduleDateError'));
}
} else {
$this->podcast->published_at = Time::now();
}
$post = (new PostModel())
->where([
'actor_id' => $this->podcast->actor_id,
'episode_id' => null,
])
->first();
$newPostMessage = $this->request->getPost('message');
if ($post instanceof Post) {
if ($newPostMessage !== '') {
// edit post if post exists and message is not empty
$post->message = $newPostMessage;
$post->published_at = $this->podcast->published_at;
$postModel = new PostModel();
if (! $postModel->editPost($post)) {
$db->transRollback();
return redirect()
->back()
->withInput()
->with('errors', $postModel->errors());
}
} else {
// remove post if post exists and message is empty
$postModel = new PostModel();
$post = $postModel
->where([
'actor_id' => $this->podcast->actor_id,
'episode_id' => null,
])
->first();
$postModel->removePost($post);
}
} elseif ($newPostMessage !== '') {
// create post if there is no post and message is not empty
$newPost = new Post([
'actor_id' => $this->podcast->actor_id,
'message' => $newPostMessage,
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
'created_by' => user_id(),
]);
$newPost->published_at = $this->podcast->published_at;
$postModel = new PostModel();
if (! $postModel->addPost($newPost)) {
$db->transRollback();
return redirect()
->back()
->withInput()
->with('errors', $postModel->errors());
}
}
$episodes = (new EpisodeModel())
->where('podcast_id', $this->podcast->id)
->where('published_at !=', null)
->findAll();
foreach ($episodes as $episode) {
$episode->published_at = $this->podcast->published_at->addSeconds(1);
$episodeModel = new EpisodeModel();
if (! $episodeModel->update($episode->id, $episode)) {
$db->transRollback();
return redirect()
->back()
->withInput()
->with('errors', $episodeModel->errors());
}
$post = (new PostModel())->where('episode_id', $episode->id)
->first();
if ($post instanceof Post) {
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
$post->published_at = $episode->published_at;
$postModel = new PostModel();
if (! $postModel->update($post->id, $post)) {
$db->transRollback();
return redirect()
->back()
->withInput()
->with('errors', $postModel->errors());
}
}
}
$podcastModel = new PodcastModel();
if (! $podcastModel->update($this->podcast->id, $this->podcast)) {
$db->transRollback();
return redirect()
->back()
->withInput()
->with('errors', $podcastModel->errors());
}
$db->transComplete();
return redirect()->route('podcast-view', [$this->podcast->id]);
}
public function publishCancel(): RedirectResponse
{
if ($this->podcast->publication_status !== 'scheduled') {
return redirect()->route('podcast-view', [$this->podcast->id]);
}
$db = db_connect();
$db->transStart();
$postModel = new PostModel();
$post = $postModel
->where([
'actor_id' => $this->podcast->actor_id,
'episode_id' => null,
])
->first();
if ($post instanceof Post) {
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
$postModel->removePost($post);
}
$episodes = (new EpisodeModel())
->where('podcast_id', $this->podcast->id)
->where('published_at !=', null)
->findAll();
foreach ($episodes as $episode) {
$episode->published_at = null;
$episodeModel = new EpisodeModel();
if (! $episodeModel->update($episode->id, $episode)) {
$db->transRollback();
return redirect()
->back()
->withInput()
->with('errors', $episodeModel->errors());
}
$postModel = new PostModel();
$post = $postModel->where('episode_id', $episode->id)
->first();
$postModel->removePost($post);
}
$this->podcast->published_at = null;
$podcastModel = new PodcastModel();
if (! $podcastModel->update($this->podcast->id, $this->podcast)) {
$db->transRollback();
return redirect()
->back()
->withInput()
->with('errors', $podcastModel->errors());