Commit f75bd764 authored by Yassine Doghri's avatar Yassine Doghri
Browse files

fix: unpublish episode before deleting it + add validation step before deletion

fixes #112, closes #55
parent b9db9364
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ class Checkbox extends FormComponent
        $hint = $this->hint === null ? '' : hint_tooltip($this->hint, 'ml-1');

        return <<<HTML
            <label class="leading-8">
            <label class="leading-8 {$this->class}">
                {$checkboxInput}
                <span class="ml-2">{$this->slot}{$hint}</label>
            </label>
+8 −0
Original line number Diff line number Diff line
@@ -272,6 +272,14 @@ $routes->group(
                                    'permission:podcast_episodes-delete',
                            ],
                        );
                        $routes->post(
                            'delete',
                            'EpisodeController::attemptDelete/$1/$2',
                            [
                                'filter' =>
                                    'permission:podcast_episodes-delete',
                            ],
                        );
                        $routes->get(
                            'transcript-delete',
                            'EpisodeController::transcriptDelete/$1/$2',
+56 −2
Original line number Diff line number Diff line
@@ -684,9 +684,63 @@ class EpisodeController extends BaseController
        return redirect()->route('episode-view', [$this->podcast->id, $this->episode->id]);
    }

    public function delete(): RedirectResponse
    public function delete(): string
    {
        (new EpisodeModel())->delete($this->episode->id);
        helper(['form']);

        $data = [
            'podcast' => $this->podcast,
            'episode' => $this->episode,
        ];

        replace_breadcrumb_params([
            0 => $this->podcast->title,
            1 => $this->episode->title,
        ]);
        return view('episode/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();

        $allPostsLinkedToEpisode = (new PostModel())
            ->where([
                'episode_id' => $this->episode->id,
            ])
            ->findAll();
        foreach ($allPostsLinkedToEpisode as $post) {
            (new PostModel())->removePost($post);
        }

        // set episode published_at to null to unpublish before deletion
        $this->episode->published_at = null;

        $episodeModel = new EpisodeModel();
        if (! $episodeModel->update($this->episode->id, $this->episode)) {
            $db->transRollback();
            return redirect()
                ->back()
                ->withInput()
                ->with('errors', $episodeModel->errors());
        }

        $episodeModel->delete($this->episode->id);

        $db->transComplete();

        return redirect()->route('episode-list', [$this->podcast->id]);
    }
+1 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ return [
    'publish' => 'publish',
    'publish-edit' => 'edit publication',
    'unpublish' => 'unpublish',
    'delete' => 'delete',
    'fediverse' => 'fediverse',
    'block-lists' => 'block lists',
    'users' => 'users',
+7 −1
Original line number Diff line number Diff line
@@ -137,10 +137,16 @@ return [
    ],
    'unpublish_form' => [
        'disclaimer' =>
            "Unpublishing the episode will delete all the notes associated with the episode and remove it from the podcast's RSS feed.",
            "Unpublishing the episode will delete all the posts associated with it and remove it from the podcast's RSS feed.",
        'understand' => 'I understand, I want to unpublish the episode',
        'submit' => 'Unpublish',
    ],
    'delete_form' => [
        'disclaimer' =>
            "Deleting the episode will delete all the posts associated with it and remove it from the podcast's RSS feed.",
        'understand' => 'I understand, I want to delete the episode',
        'submit' => 'Delete',
    ],
    'soundbites' => 'Soundbites',
    'soundbites_form' => [
        'title' => 'Edit soundbites',
Loading