1 | <?php
|
---|
2 |
|
---|
3 | namespace App\Http\Controllers;
|
---|
4 |
|
---|
5 | use App\Enum\OfferStatus;
|
---|
6 | use App\Http\Controllers\Utils\UtilController;
|
---|
7 | use App\Models\Artist;
|
---|
8 | use App\Models\Offer;
|
---|
9 | use App\Models\User;
|
---|
10 | use Illuminate\Contracts\Foundation\Application;
|
---|
11 | use Illuminate\Contracts\View\Factory;
|
---|
12 | use Illuminate\Contracts\View\View;
|
---|
13 | use Illuminate\Http\RedirectResponse;
|
---|
14 | use Illuminate\Http\Request;
|
---|
15 | use App\Models\EventType;
|
---|
16 | use App\Models\Event;
|
---|
17 | use App\Models\Organizer;
|
---|
18 | use Illuminate\Support\Facades\Auth;
|
---|
19 | use Illuminate\Support\Facades\Response;
|
---|
20 | use PHPUnit\Exception;
|
---|
21 |
|
---|
22 | class EventController extends Controller
|
---|
23 | {
|
---|
24 | //
|
---|
25 | public function __construct()
|
---|
26 | {
|
---|
27 | $this->middleware(['auth', 'verified', 'onboarding']);
|
---|
28 | $this->middleware(['role:artist,organizer,manager'])->only('show');
|
---|
29 | $this->middleware('role:organizer')->except('show');
|
---|
30 | }
|
---|
31 |
|
---|
32 | public function index()
|
---|
33 | {
|
---|
34 | return view('web.organizer.event.events')
|
---|
35 | ->with('events', Event::where('organizer_id', '=', Auth::id())->paginate(15));
|
---|
36 | }
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Show the form for creating a new resource.
|
---|
40 | *
|
---|
41 | * @return Response|Application|Factory|View
|
---|
42 | */
|
---|
43 | public function create($username = null)
|
---|
44 | {
|
---|
45 | if (is_null($username)) {
|
---|
46 | return view('web.organizer.event.create')
|
---|
47 | ->with('event_types', EventType::all());
|
---|
48 | } else {
|
---|
49 | $user = User::where('username', $username)->firstOrFail();
|
---|
50 | $artist = Artist::where('user_id', $user->id)->firstOrFail();
|
---|
51 |
|
---|
52 | return view('web.organizer.event.create')
|
---|
53 | ->with('event_types', EventType::all())
|
---|
54 | ->with('artist', $artist);
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Store a newly created resource in storage.
|
---|
60 | *
|
---|
61 | * @return RedirectResponse
|
---|
62 | */
|
---|
63 | public function store(Request $request)
|
---|
64 | {
|
---|
65 | $events = Event::where([
|
---|
66 | ['organizer_id', Auth::id()],
|
---|
67 | ['title', trim($request->title)]
|
---|
68 | ])->exists();
|
---|
69 |
|
---|
70 | if ($events) {
|
---|
71 | return redirect()->route('organizer.events')
|
---|
72 | ->with('warning', 'You can not add an event with an existing name');
|
---|
73 | }
|
---|
74 |
|
---|
75 | $event = new Event();
|
---|
76 | $event->title = trim($request->title);
|
---|
77 | $event->slug = UtilController::generateSlugFromString($request->title);
|
---|
78 | $event->event_date = $request->event_date;
|
---|
79 | $event->start_time = $request->start_time;
|
---|
80 | $event->end_time = $request->end_time;
|
---|
81 | $event->city = $request->city;
|
---|
82 | $event->country = $request->country;
|
---|
83 | $event->description = $request->description;
|
---|
84 |
|
---|
85 | $eventType = EventType::findOrFail($request->event_type);
|
---|
86 | $event->event_type()->associate($eventType);
|
---|
87 |
|
---|
88 | $organizer = Organizer::findOrFail(Auth::id());
|
---|
89 | $event->organizer()->associate($organizer);
|
---|
90 |
|
---|
91 | try {
|
---|
92 | $event->save();
|
---|
93 | } catch (\Exception $e) {
|
---|
94 | return redirect()->back()
|
---|
95 | ->with('error', $e->getMessage());
|
---|
96 | }
|
---|
97 |
|
---|
98 | if ($request->artist_id != null) { // Znaci deka doagja od offer
|
---|
99 | $content = new Request();
|
---|
100 | $content->artist_id = $request->artist_id;
|
---|
101 | $content->event_id = $event->id;
|
---|
102 |
|
---|
103 | $offerStore = (new OfferController())->store($content);
|
---|
104 | return redirect()->to($offerStore->getTargetUrl());
|
---|
105 | }
|
---|
106 |
|
---|
107 | return redirect()->route('organizer.events');
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Display the specified resource.
|
---|
112 | *
|
---|
113 | * @return Response|Application|Factory|View
|
---|
114 | */
|
---|
115 | public function show($slug)
|
---|
116 | {
|
---|
117 | $event = Event::where('slug', $slug)->firstOrFail();
|
---|
118 |
|
---|
119 | return view('web.organizer.event.event')
|
---|
120 | ->with('event', $event);
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Show the form for editing the specified resource.
|
---|
125 | *
|
---|
126 | * @param $slug
|
---|
127 | * @return Application|Factory|View|RedirectResponse
|
---|
128 | */
|
---|
129 | public function edit($slug)
|
---|
130 | {
|
---|
131 | $event = Event::where('slug', $slug)->firstOrFail();
|
---|
132 |
|
---|
133 | $offers = Offer::where([
|
---|
134 | ['event_id', $event->id],
|
---|
135 | ['status', OfferStatus::IN_PROGRESS->value]])->exists();
|
---|
136 |
|
---|
137 | if ($offers) {
|
---|
138 | return redirect()->route('organizer.events')
|
---|
139 | ->with('warning', 'You cannot change event that is currently in progress.');
|
---|
140 | }
|
---|
141 |
|
---|
142 | return view('web.organizer.event.create')
|
---|
143 | ->with('event_types', EventType::all())
|
---|
144 | ->with('event', $event);
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Update the specified resource in storage.
|
---|
149 | *
|
---|
150 | * @param int $id
|
---|
151 | * @return Application|Factory|View|RedirectResponse
|
---|
152 | */
|
---|
153 | public function update(int $id, Request $request)
|
---|
154 | {
|
---|
155 | //
|
---|
156 | $offers = Offer::where([
|
---|
157 | ['event_id', $id],
|
---|
158 | ['status', OfferStatus::IN_PROGRESS->value]])->exists();
|
---|
159 |
|
---|
160 | if ($offers) {
|
---|
161 | return redirect()->route('organizer.events')
|
---|
162 | ->with('warning', 'You cannot change event that is currently in progress.');
|
---|
163 | }
|
---|
164 |
|
---|
165 | $event = Event::findOrFail($id);
|
---|
166 |
|
---|
167 | $event->title = $request->title;
|
---|
168 | $event->slug = UtilController::generateSlugFromString($request->title);
|
---|
169 | $event->event_date = $request->event_date;
|
---|
170 | $event->start_time = $request->start_time;
|
---|
171 | $event->end_time = $request->end_time;
|
---|
172 | $event->city = $request->city;
|
---|
173 | $event->country = $request->country;
|
---|
174 | $event->description = $request->description;
|
---|
175 |
|
---|
176 | $eventType = EventType::find($request->event_type);
|
---|
177 | $event->event_type()->associate($eventType);
|
---|
178 | try {
|
---|
179 | $event->update();
|
---|
180 | } catch (Exception $e) {
|
---|
181 | return redirect()->back()
|
---|
182 | ->with('error', $e->getMessage());
|
---|
183 | }
|
---|
184 |
|
---|
185 | return redirect()->route('organizer.events');
|
---|
186 |
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Remove the specified resource from storage.
|
---|
191 | *
|
---|
192 | * @param int $id
|
---|
193 | * @return Response
|
---|
194 | */
|
---|
195 | public function destroy($id)
|
---|
196 | {
|
---|
197 | //TODO: Add delete option for events that have been created but no offer is related to it...
|
---|
198 | }
|
---|
199 | }
|
---|