Skip to content

Commit

Permalink
Merge pull request #672 from icewindow/copy-seatplan
Browse files Browse the repository at this point in the history
Create duplicate of seating plan
  • Loading branch information
Apfelwurm committed Sep 27, 2024
2 parents b0e694b + b3745e4 commit c77fbf0
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 2 deletions.
111 changes: 109 additions & 2 deletions src/app/Http/Controllers/Admin/Events/SeatingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers\Admin\Events;

use DB;
use Illuminate\Http\RedirectResponse;
use Session;
use Storage;

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/app/Libraries/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Libraries;

use App\Event;
use App\EventSeatingPlan;
use Session;
use Illuminate\Http\Request;
use Exception;
Expand Down Expand Up @@ -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;
}
}
32 changes: 32 additions & 0 deletions src/resources/views/admin/events/seating/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,38 @@
</div>
</div>

<div class="card mb-3">
<div class="card-header">
<i class="fa fa-copy fa-fw"></i>Duplicate existing seating plan
</div>
<div class="card-body">
{{ Form::open(['url' => "/admin/events/{$event->slug}/seating"]) }}
<div class="mb-3">
{{ Form::label('duplicate', 'Copy from') }}
{{ Form::select(
'duplicate',
Helpers::getExistingSeatingPlansSelect(),
'',
[
'id' => 'duplicate',
'class'=> 'form-control'
]
) }}
</div>
<div class="mb-3">
{{ Form::label('name-override', 'Name override') }}
{{ Form::text('name_override', null, ['id' => 'name-override', 'class' => 'form-control']) }}
<div class="form-text">Name for copy. Leave empty to copy original name</div>
</div>
<div class="mb-3">
{{ Form::label('short-name-override', 'Short name override') }}
{{ Form::text('short_name_override', null, ['id' => 'short-name-override', 'class' => 'form-control']) }}
<div class="form-text">Short name for copy. Leave empty to copy original short name</div>
</div>
<button type="submit" class="btn btn-success btn-block">Submit</button>
{{ Form::close() }}
</div>
</div>
</div>
</div>

Expand Down

0 comments on commit c77fbf0

Please sign in to comment.