Completed
Push — master ( b1d075...e12f33 )
by Martin
05:37
created

EventController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 67
c 1
b 0
f 0
dl 0
loc 105
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 14 1
A update() 0 13 1
A showCreate() 0 15 1
A delete() 0 10 2
A get() 0 16 1
A index() 0 22 4
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Event;
6
use App\Http\Controllers\Controller;
7
use App\Http\Requests\Admin\CreateUpdateEvent;
8
use App\Location;
9
use App\PriceList;
10
use App\Project;
11
use App\SeatMap;
12
use Illuminate\Database\Eloquent\Builder;
13
14
class EventController extends Controller
15
{
16
    public function index()
17
    {
18
        $projects = Project::where('is_archived', false)->get();
19
        $archive = Project::where('is_archived', true)->get();
20
        foreach( $archive as $project )
21
        {
22
            // Set the time range of the project by selecting the last and first dates of its events
23
            // if no events are associated with the project, set the dates to '-'
24
            $firstEvent = $project->events()->orderBy('start_date', 'ASC')->first();
25
            $project->start_date = $firstEvent === null ? '-' : $firstEvent->start_date;
1 ignored issue
show
Bug introduced by
The property start_date does not seem to exist on App\Project. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
26
            $lastEvent = $project->events()->orderBy('end_date', 'DESC')->first();
27
            $project->end_date = $lastEvent === null ? '-' : $lastEvent->end_date;
1 ignored issue
show
Bug introduced by
The property end_date does not seem to exist on App\Project. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
28
        }
29
30
        $events = Event::whereHas('project', function(Builder $query) {
31
            $query->where('is_archived', false);
32
        })->get();
33
34
        return view('admin.events.index', [
35
            'projects' => $projects,
36
            'events'   => $events,
37
            'archived' => $archive
38
        ]);
39
    }
40
41
    public function showCreate()
42
    {
43
        $projects = Project::where('is_archived', false)->orderBy('name', 'ASC')->get();
44
        $archive = Project::where('is_archived', true)->orderBy('name', 'ASC')->get();
45
        $locations = Location::orderBy('name', 'ASC')->get();
46
        $seatMaps = SeatMap::orderBy('name', 'ASC')->get();
47
        $priceLists = PriceList::orderBy('name', 'ASC')->get();
48
49
        return view('admin.events.manage-event', [
50
            'create' => true,
51
            'projects' => $projects,
52
            'archive' => $archive,
53
            'locations' => $locations,
54
            'seatmaps' => $seatMaps,
55
            'pricelists' => $priceLists
56
        ]);
57
    }
58
59
    public function create(CreateUpdateEvent $request)
60
    {
61
        $event = new Event();
62
        $event->project_id = $request->project;
63
        $event->second_name = $request->name;
64
        $event->start_date = $request->start;
65
        $event->end_date = $request->end;
66
        $event->location_id = $request->location;
67
        $event->seat_map_id = $request->seatmap;
68
        $event->price_list_id = $request->pricelist;
69
        $event->save();
70
71
        return redirect()->route('admin.events.get', $event)
72
                ->with('status', 'Updated Event successfully!');
73
    }
74
75
    public function get(Event $event)
76
    {
77
        $projects = Project::where('is_archived', false)->orderBy('name', 'ASC')->get();
78
        $archive = Project::where('is_archived', true)->orderBy('name', 'ASC')->get();
79
        $locations = Location::orderBy('name', 'ASC')->get();
80
        $seatMaps = SeatMap::orderBy('name', 'ASC')->get();
81
        $priceLists = PriceList::orderBy('name', 'ASC')->get();
82
83
        return view('admin.events.manage-event', [
84
            'create' => false,
85
            'event' => $event,
86
            'projects' => $projects,
87
            'archive' => $archive,
88
            'locations' => $locations,
89
            'seatmaps' => $seatMaps,
90
            'pricelists' => $priceLists
91
        ]);
92
    }
93
94
    public function update(Event $event, CreateUpdateEvent $request)
95
    {
96
        $event->project_id = $request->project;
97
        $event->second_name = $request->name;
98
        $event->start_date = $request->start;
99
        $event->end_date = $request->end;
100
        $event->location_id = $request->location;
101
        $event->seat_map_id = $request->seatmap;
102
        $event->price_list_id = $request->pricelist;
103
        $event->save();
104
105
        return redirect()->route('admin.events.get', $event)
106
                ->with('status', 'Updated Event successfully!');
107
    }
108
109
    public function delete(Event $event)
110
    {
111
        if($event->tickets()->exists())
112
        {
113
            return redirect()->route('admin.events.get', $event)
114
                    ->with('status', 'Error on deletion: Event has tickets!');
115
        }
116
        $event->delete();
117
        return redirect()->route('admin.events.dashboard')
118
                        ->with('status', 'Deleted Event successfully!');
119
    }
120
}
121