From 71b1b5f775af475b1dc78328330e277f565e41b6 Mon Sep 17 00:00:00 2001
From: Yassine Doghri <yassine@doghri.fr>
Date: Wed, 9 Jun 2021 17:00:18 +0000
Subject: [PATCH] fix: set location to null when getting empty string

---
 app/Controllers/Admin/EpisodeController.php | 8 ++++++--
 app/Controllers/Admin/PodcastController.php | 8 ++++++--
 app/Views/admin/podcast/edit.php            | 2 +-
 3 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/app/Controllers/Admin/EpisodeController.php b/app/Controllers/Admin/EpisodeController.php
index 2a847444ee..a49b3b5ae0 100644
--- a/app/Controllers/Admin/EpisodeController.php
+++ b/app/Controllers/Admin/EpisodeController.php
@@ -137,7 +137,9 @@ class EpisodeController extends BaseController
             'audio_file' => $this->request->getFile('audio_file'),
             'description_markdown' => $this->request->getPost('description'),
             'image' => $image,
-            'location' => new Location($this->request->getPost('location_name')),
+            'location' => $this->request->getPost('location_name') === '' ? null : new Location($this->request->getPost(
+                'location_name'
+            )),
             'transcript' => $this->request->getFile('transcript'),
             'chapters' => $this->request->getFile('chapters'),
             'parental_advisory' =>
@@ -242,7 +244,9 @@ class EpisodeController extends BaseController
         $this->episode->title = $this->request->getPost('title');
         $this->episode->slug = $this->request->getPost('slug');
         $this->episode->description_markdown = $this->request->getPost('description');
-        $this->episode->location = new Location($this->request->getPost('location_name'));
+        $this->episode->location = $this->request->getPost('location_name') === '' ? null : new Location(
+            $this->request->getPost('location_name')
+        );
         $this->episode->parental_advisory =
             $this->request->getPost('parental_advisory') !== 'undefined'
                 ? $this->request->getPost('parental_advisory')
diff --git a/app/Controllers/Admin/PodcastController.php b/app/Controllers/Admin/PodcastController.php
index d6b0e577f3..f1570d094c 100644
--- a/app/Controllers/Admin/PodcastController.php
+++ b/app/Controllers/Admin/PodcastController.php
@@ -207,7 +207,9 @@ class PodcastController extends BaseController
             'publisher' => $this->request->getPost('publisher'),
             'type' => $this->request->getPost('type'),
             'copyright' => $this->request->getPost('copyright'),
-            'location' => new Location($this->request->getPost('location_name')),
+            'location' => $this->request->getPost('location_name') === '' ? null : new Location($this->request->getPost(
+                'location_name'
+            )),
             'payment_pointer' => $this->request->getPost(
                 'payment_pointer'
             ) === '' ? null : $this->request->getPost('payment_pointer'),
@@ -315,7 +317,9 @@ class PodcastController extends BaseController
         $this->podcast->owner_email = $this->request->getPost('owner_email');
         $this->podcast->type = $this->request->getPost('type');
         $this->podcast->copyright = $this->request->getPost('copyright');
-        $this->podcast->location = new Location($this->request->getPost('location_name'));
+        $this->podcast->location = $this->request->getPost('location_name') === '' ? null : new Location(
+            $this->request->getPost('location_name')
+        );
         $this->podcast->payment_pointer = $this->request->getPost(
             'payment_pointer'
         ) === '' ? null : $this->request->getPost('payment_pointer');
diff --git a/app/Views/admin/podcast/edit.php b/app/Views/admin/podcast/edit.php
index 28df6e4df2..c0ca9e64f6 100644
--- a/app/Views/admin/podcast/edit.php
+++ b/app/Views/admin/podcast/edit.php
@@ -254,7 +254,7 @@
     'id' => 'location_name',
     'name' => 'location_name',
     'class' => 'form-input mb-4',
-    'value' => old('location_name', $podcast->location->name),
+    'value' => old('location_name', $podcast->location_name),
 ]) ?>
 <?= form_section_close() ?>
 
-- 
GitLab