diff --git a/src/app/Http/Controllers/Admin/Events/SeatingController.php b/src/app/Http/Controllers/Admin/Events/SeatingController.php index 0139d4cef..742522811 100644 --- a/src/app/Http/Controllers/Admin/Events/SeatingController.php +++ b/src/app/Http/Controllers/Admin/Events/SeatingController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Admin\Events; use DB; +use Illuminate\Http\RedirectResponse; use Session; use Storage; @@ -53,9 +54,25 @@ public function show(Event $event, EventSeatingPlan $seatingPlan) * Add Seating Plan to Database * @param Event $event * @param Request $request - * @return Redirect + * @return RedirectResponse + */ + public function store(Event $event, Request $request): RedirectResponse + { + if ($request->get('duplicate') != null) { + return $this->duplicate($event, $request); + } + + return $this->create($event, $request); + } + + /** + * Create a new seating plan + * @param Event $event + * @param Request $request + * @return RedirectResponse + * @throws \Illuminate\Validation\ValidationException */ - public function store(Event $event, Request $request) + protected function create(Event $event, Request $request) { $rules = [ "name" => "required", @@ -208,6 +225,96 @@ public function destroy(Event $event, EventSeatingPlan $seatingPlan) return Redirect::to('/admin/events/' . $event->slug . '/seating'); } + /** + * Duplicate an existing seating plan + * @param Event $event + * @param Request $request + * @return RedirectResponse + */ + protected function duplicate(Event $event, Request $request): RedirectResponse { + // Status variables, needed further down below + $seatingPlanImageCopied = true; + $disabledSeatsSaved = true; + + // Get selected seating plan data + $plan = EventSeatingPlan::where('id', $request->get('duplicate'))->first(); + $disabledSeats = $plan->seats()->where('status', 'INACTIVE'); + + // Copy seating plan + $dupe = $plan->replicate(); + $dupe->status = 'DRAFT'; + $dupe->event_id = $event->id; + unset($dupe->slug); + + if ($request->filled('name_override')) { + $dupe->name = $request->get('name_override'); + } + if ($request->filled('short_name_override')) { + $dupe->name_short = $request->get('short_name_override'); + } + if ($dupe->image_path) { + $imageName = basename($dupe->image_path); + $imagePath = "public/images/events/{$event->slug}/seating/{$dupe->slug}/{$imageName}"; + if (($seatingPlanImageCopied= Storage::copy( + str_replace('/storage/', 'public/', $dupe->image_path), + $imagePath + ))) { + $dupe->image_path = str_replace('public/', '/storage/', $imagePath); + } else { + $dupe->image_path = null; + } + } + + $seatingPlanSaved = $dupe->save(); + + if ($seatingPlanSaved) { + foreach ($disabledSeats->get() as $seat) { + $newSeat = new EventSeating(); + $newSeat->row = $seat->row; + $newSeat->column = $seat->column; + $newSeat->status = 'INACTIVE'; + $newSeat->event_seating_plan_id = $dupe->id; + $disabledSeatsSaved &= $newSeat->save(); + } + } + + switch ( + ($seatingPlanSaved ? 4 : 0) + + ($seatingPlanImageCopied ? 2 : 0) + + ($disabledSeatsSaved ? 1 : 0) + ) { + case 7: + Session::flash('alert-success', 'Seating plan copied successfully'); + break; + case 6: + Session::flash('alert-warning', 'Seating plan copied successfully, but one ore more inactive seats could not be saved'); + break; + case 5: + Session::flash('alert-warning', 'Seating plan copied successfully, but seating plan image could not be saved'); + break; + case 4: + Session::flash('alert-warning', 'Seating plan copied successfully, but seating plan image and at least one inactive seat could not be saved'); + break; + case 3: + // Normally unreachable + Session::flash('alert-danger', 'Could not copy seating plan, but seating plan image was copied and somehow managed to copy inactive seats‽ This should not usually happen'); + break; + case 2: + Session::flash('alert-danger', 'Could not copy seating plan, but seating plan image was saved successfully‽'); + break; + case 1: + // Normally unreachable + Session::flash('alert-danger', 'Could not copy seating plan, but somehow managed to copy inactive seats‽ This should not usually happen'); + break; + case 0: + Session::flash('alert-danger', 'Could not copy seating plan'); + break; + default: + Session::flash('alert-danger', 'Just, how?'); + } + return Redirect::back(); + } + /** * Seat Participant * @param Event $event diff --git a/src/app/Libraries/Helpers.php b/src/app/Libraries/Helpers.php index de983cc40..56131840c 100644 --- a/src/app/Libraries/Helpers.php +++ b/src/app/Libraries/Helpers.php @@ -2,6 +2,8 @@ namespace App\Libraries; +use App\Event; +use App\EventSeatingPlan; use Session; use Illuminate\Http\Request; use Exception; @@ -970,4 +972,14 @@ public static function getSeoCustomDescription($description) { return $description. Helpers::getPoweredByLine(); } + + public static function getExistingSeatingPlansSelect() { + $result = []; + foreach(EventSeatingPlan::all(['id', 'name', 'event_id']) as $plan) { + $event = Event::where('id', $plan->event_id)->first(); + $result[$event->display_name] ??= []; + $result[$event->display_name][$plan->id] = $plan->name; + } + return $result; + } } diff --git a/src/resources/views/admin/events/seating/index.blade.php b/src/resources/views/admin/events/seating/index.blade.php index dd5c7c78b..6509a543d 100644 --- a/src/resources/views/admin/events/seating/index.blade.php +++ b/src/resources/views/admin/events/seating/index.blade.php @@ -108,6 +108,38 @@ +
+
+ Duplicate existing seating plan +
+
+ {{ Form::open(['url' => "/admin/events/{$event->slug}/seating"]) }} +
+ {{ Form::label('duplicate', 'Copy from') }} + {{ Form::select( + 'duplicate', + Helpers::getExistingSeatingPlansSelect(), + '', + [ + 'id' => 'duplicate', + 'class'=> 'form-control' + ] + ) }} +
+
+ {{ Form::label('name-override', 'Name override') }} + {{ Form::text('name_override', null, ['id' => 'name-override', 'class' => 'form-control']) }} +
Name for copy. Leave empty to copy original name
+
+
+ {{ Form::label('short-name-override', 'Short name override') }} + {{ Form::text('short_name_override', null, ['id' => 'short-name-override', 'class' => 'form-control']) }} +
Short name for copy. Leave empty to copy original short name
+
+ + {{ Form::close() }} +
+