Passed
Push — master ( 83e6c6...5697af )
by Davide
56:57 queued 23:00
created

OrganizerController::index()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3.0105

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 29
ccs 17
cts 19
cp 0.8947
rs 9.6333
cc 3
nc 4
nop 1
crap 3.0105
1
<?php
2
3
namespace DavideCasiraghi\LaravelEventsCalendar\Http\Controllers;
4
5
use DavideCasiraghi\LaravelEventsCalendar\Models\Organizer;
6
use Illuminate\Foundation\Auth\User;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\DB;
9
use Illuminate\Support\Facades\Validator;
10
11
class OrganizerController extends Controller
12
{
13
    /* Restrict the access to this resource just to logged in users except show view */
14 13
    public function __construct()
15
    {
16 13
        $this->middleware('auth', ['except' => ['show', 'organizerBySlug']]);
17 13
    }
18
19
    /***************************************************************************/
20
21
    /**
22
     * Display a listing of the resource.
23
     *
24
     * @return \Illuminate\View\View
25
     */
26 2
    public function index(Request $request)
27
    {
28
        // To show just the organizers created by the the user - If admin or super admin is set to null show all the organizers
29 2
        $authorUserId = ($this->getLoggedAuthorId()) ? $this->getLoggedAuthorId() : null; // if is 0 (super admin or admin) it's setted to null to avoid include it in the query
30
31 2
        $searchKeywords = $request->input('keywords');
32
33 2
        if ($searchKeywords) {
34 1
            $organizers = DB::table('organizers')
35
                ->when($authorUserId, function ($query, $authorUserId) {
36
                    return $query->where('created_by', $authorUserId);
37 1
                })
38
                ->when($searchKeywords, function ($query, $searchKeywords) {
39 1
                    return $query->where('name', $searchKeywords)->orWhere('name', 'like', '%'.$searchKeywords.'%');
40 1
                })
41 1
                ->orderBy('name')
42 1
                ->paginate(20);
43
        } else {
44 1
            $organizers = DB::table('organizers')
45
            ->when($authorUserId, function ($query, $authorUserId) {
46
                return $query->where('created_by', $authorUserId);
47 1
            })
48 1
            ->orderBy('name')
49 1
            ->paginate(20);
50
        }
51
52 2
        return view('laravel-events-calendar::organizers.index', compact('organizers'))
53 2
            ->with('i', (request()->input('page', 1) - 1) * 20)
54 2
            ->with('searchKeywords', $searchKeywords);
55
    }
56
57
    /***************************************************************************/
58
59
    /**
60
     * Show the form for creating a new resource.
61
     *
62
     * @return \Illuminate\View\View
63
     */
64 1
    public function create()
65
    {
66 1
        $users = User::pluck('name', 'id');
67 1
        $authorUserId = $this->getLoggedAuthorId();
68
69 1
        return view('laravel-events-calendar::organizers.create')
70 1
            ->with('users', $users)
71 1
            ->with('authorUserId', $authorUserId);
72
    }
73
74
    /***************************************************************************/
75
76
    /**
77
     * Store a newly created resource in storage.
78
     *
79
     * @param  \Illuminate\Http\Request  $request
80
     * @return \Illuminate\Http\RedirectResponse
81
     */
82 2
    public function store(Request $request)
83
    {
84
        // Validate form datas
85 2
        $validator = $this->organizersValidator($request);
86 2
        if ($validator->fails()) {
87 1
            return back()->withErrors($validator)->withInput();
88
        }
89
90 1
        $organizer = new Organizer();
91 1
        $organizer->preSave($request);
92 1
        $organizer->save();
93
94 1
        return redirect()->route('organizers.index')
95 1
                        ->with('success', __('laravel-events-calendar::messages.organizer_added_successfully'));
96
    }
97
98
    /***************************************************************************/
99
100
    /**
101
     * Display the specified resource.
102
     *
103
     * @param  \DavideCasiraghi\LaravelEventsCalendar\Models\Organizer  $organizer
104
     * @return \Illuminate\View\View
105
     */
106 1
    public function show(Organizer $organizer)
107
    {
108 1
        return view('laravel-events-calendar::organizers.show', compact('organizer'));
109
    }
110
111
    /***************************************************************************/
112
113
    /**
114
     * Show the form for editing the specified resource.
115
     *
116
     * @param  \DavideCasiraghi\LaravelEventsCalendar\Models\Organizer  $organizer
117
     * @return \Illuminate\View\View
118
     */
119 1
    public function edit(Organizer $organizer)
120
    {
121 1
        $authorUserId = $this->getLoggedAuthorId();
122 1
        $users = User::pluck('name', 'id');
123
124 1
        return view('laravel-events-calendar::organizers.edit', compact('organizer'))
125 1
            ->with('users', $users)
126 1
            ->with('authorUserId', $authorUserId);
127
    }
128
129
    /***************************************************************************/
130
131
    /**
132
     * Update the specified resource in storage.
133
     *
134
     * @param  \Illuminate\Http\Request  $request
135
     * @param  \DavideCasiraghi\LaravelEventsCalendar\Models\Organizer  $organizer
136
     * @return \Illuminate\Http\RedirectResponse
137
     */
138 2
    public function update(Request $request, Organizer $organizer)
139
    {
140
        // Validate form datas
141 2
        $validator = $this->organizersValidator($request);
142 2
        if ($validator->fails()) {
143 1
            return back()->withErrors($validator)->withInput();
144
        }
145
146 1
        $organizer->preSave($request);
147 1
        $organizer->save();
148
149 1
        return redirect()->route('organizers.index')
150 1
                        ->with('success', __('laravel-events-calendar::messages.organizer_updated_successfully'));
151
    }
152
153
    /***************************************************************************/
154
155
    /**
156
     * Remove the specified resource from storage.
157
     *
158
     * @param  \DavideCasiraghi\LaravelEventsCalendar\Models\Organizer  $organizer
159
     * @return \Illuminate\Http\RedirectResponse
160
     */
161 1
    public function destroy(Organizer $organizer)
162
    {
163 1
        $organizer->delete();
164
165 1
        return redirect()->route('organizers.index')
166 1
                        ->with('success', __('laravel-events-calendar::messages.organizer_deleted_successfully'));
167
    }
168
169
    /***************************************************************************/
170
171
    /**
172
     * Open a modal in the event view when 'create new organizer' button is clicked.
173
     *
174
     * @return \Illuminate\View\View
175
     */
176 1
    public function modal()
177
    {
178 1
        $users = User::pluck('name', 'id');
179
180 1
        return view('laravel-events-calendar::organizers.modal')
181 1
                    ->with('users', $users);
182
    }
183
184
    /***************************************************************************/
185
186
    /**
187
     * Store a newly created organizer from the create event view modal in storage.
188
     *
189
     * @param  \Illuminate\Http\Request  $request
190
     * @return \Illuminate\Http\JsonResponse
191
     */
192 1
    public function storeFromModal(Request $request)
193
    {
194 1
        $organizer = new Organizer();
195 1
        $organizer->preSave($request);
196 1
        $organizer->save();
197
198 1
        return response()->json([
199 1
            'organizerId' => $organizer->id,
200 1
            'organizerName' => $organizer->name,
201
        ]);
202
    }
203
204
    /***************************************************************************/
205
206
    /**
207
     * Return the organizer by SLUG. (eg. http://websitename.com/organizer/xxxxx).
208
     *
209
     * @param  string $slug
210
     * @return \Illuminate\View\View
211
     */
212
    public function organizerBySlug($slug)
213
    {
214
        $organizer = Organizer::
215
                where('slug', $slug)
216
                ->first();
217
218
        return $this->show($organizer);
219
    }
220
221
    /***************************************************************************/
222
223
    /**
224
     * Return the validator with all the defined constraint.
225
     *
226
     * @param  \Illuminate\Http\Request  $request
227
     * @return \Illuminate\Contracts\Validation\Validator
228
     */
229 4
    public function organizersValidator(Request $request)
230
    {
231
        $rules = [
232 4
            'name' => 'required',
233
            'email' => 'required|email',
234
            'website' => 'nullable|url',
235
        ];
236
        $messages = [
237 4
            'website.url' => 'The website link is invalid. It should start with https://',
238
        ];
239
240 4
        $validator = Validator::make($request->all(), $rules, $messages);
241
242 4
        return $validator;
243
    }
244
}
245