Commit 07d740b7 authored by Yassine Doghri's avatar Yassine Doghri
Browse files

feat: prefill season and episode numbers + set episode number as mandatory for serial podcasts

closes #134, #136
parent d0cb964b
Pipeline #1187 passed with stages
in 7 minutes and 46 seconds
......@@ -268,6 +268,32 @@ class EpisodeModel extends Model
: false;
}
public function getCurrentSeasonNumber(int $podcastId): ?int
{
$result = $this->select('MAX(season_number) as current_season_number')
->where([
'podcast_id' => $podcastId,
$this->deletedField => null,
])
->get()
->getResultArray();
return $result[0]['current_season_number'] ? (int) $result[0]['current_season_number'] : null;
}
public function getNextEpisodeNumber(int $podcastId, ?int $seasonNumber): int
{
$result = $this->select('MAX(number) as next_episode_number')
->where([
'podcast_id' => $podcastId,
'season_number' => $seasonNumber,
$this->deletedField => null,
])->get()
->getResultArray();
return (int) $result[0]['next_episode_number'] + 1;
}
/**
* @param mixed[] $data
*
......
......@@ -97,10 +97,12 @@ class EpisodeController extends BaseController
{
helper(['form']);
$currentSeasonNumber = (new EpisodeModel())->getCurrentSeasonNumber($this->podcast->id);
$data = [
'podcast' => $this->podcast,
'currentSeasonNumber' => $currentSeasonNumber,
'nextEpisodeNumber' => (new EpisodeModel())->getNextEpisodeNumber($this->podcast->id, $currentSeasonNumber),
];
replace_breadcrumb_params([
0 => $this->podcast->title,
]);
......@@ -118,6 +120,10 @@ class EpisodeController extends BaseController
'chapters_file' => 'ext_in[chapters,json]|permit_empty',
];
if ($this->podcast->type === 'serial') {
$rules['episode_number'] = 'required';
}
if (! $this->validate($rules)) {
return redirect()
->back()
......@@ -239,6 +245,10 @@ class EpisodeController extends BaseController
'chapters_file' => 'ext_in[chapters_file,json]|permit_empty',
];
if ($this->podcast->type === 'serial') {
$rules['episode_number'] = 'required';
}
if (! $this->validate($rules)) {
return redirect()
->back()
......
......@@ -51,17 +51,20 @@
</div>
<div class="flex flex-col gap-x-2 gap-y-4 md:flex-row">
<Forms.Field
<Forms.Field
class="flex-1 w-0"
name="season_number"
label="<?= lang('Episode.form.season_number') ?>"
type="number"
value="<?= $currentSeasonNumber ?>"
/>
<Forms.Field
<Forms.Field
class="flex-1 w-0"
name="episode_number"
label="<?= lang('Episode.form.episode_number') ?>"
type="number"
value="<?= $nextEpisodeNumber ?>"
required="<?= $podcast->type === 'serial' ? 'true' : 'false' ?>"
/>
</div>
......
......@@ -55,19 +55,20 @@
</div>
<div class="flex flex-col gap-x-2 gap-y-4 md:flex-row">
<Forms.Field
<Forms.Field
class="flex-1 w-0"
name="season_number"
label="<?= lang('Episode.form.season_number') ?>"
type="number"
value="<?= $episode->season_number ?>"
/>
<Forms.Field
<Forms.Field
class="flex-1 w-0"
name="episode_number"
label="<?= lang('Episode.form.episode_number') ?>"
type="number"
value="<?= $episode->number ?>"
required="<?= $podcast->type === 'serial' ? 'true' : 'false' ?>"
/>
</div>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment