Completed
Push — master ( bbf405...b342bf )
by
unknown
11s
created

SiswaController::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 0
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Bantenprov\Siswa\Http\Controllers;
4
5
/* Require */
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Bantenprov\Siswa\Facades\SiswaFacade;
9
10
/* Models */
11
use Bantenprov\Siswa\Models\Bantenprov\Siswa\Siswa;
12
use Laravolt\Indonesia\Models\Province;
13
use Laravolt\Indonesia\Models\City;
14
use Laravolt\Indonesia\Models\District;
15
use Laravolt\Indonesia\Models\Village;
16
use Bantenprov\Sekolah\Models\Bantenprov\Sekolah\Sekolah;
17
use Bantenprov\Sekolah\Models\Bantenprov\Sekolah\ProdiSekolah;
18
use App\User;
19
20
/* Etc */
21
use Validator;
22
use Auth;
23
24
/**
25
 * The SiswaController class.
26
 *
27
 * @package Bantenprov\Siswa
28
 * @author  bantenprov <[email protected]>
29
 */
30
class SiswaController extends Controller
31
{
32
    protected $siswa;
33
    protected $province;
34
    protected $city;
35
    protected $district;
36
    protected $village;
37
    protected $sekolah;
38
    protected $prodi_sekolah;
39
    protected $user;
40
41
    /**
42
     * Create a new controller instance.
43
     *
44
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
45
     */
46
    public function __construct()
47
    {
48
        $this->siswa            = new Siswa;
49
        $this->province         = new Province;
50
        $this->city             = new City;
51
        $this->district         = new District;
52
        $this->village          = new Village;
53
        $this->sekolah          = new Sekolah;
54
        $this->prodi_sekolah    = new ProdiSekolah;
55
        $this->user             = new User;
56
    }
57
58
    /**
59
     * Display a listing of the resource.
60
     *
61
     * @return \Illuminate\Http\Response
62
     */
63
    public function index(Request $request)
64
    {
65
        if (request()->has('sort')) {
66
            list($sortCol, $sortDir) = explode('|', request()->sort);
67
68
            $query = $this->siswa->orderBy($sortCol, $sortDir);
69
        } else {
70
            $query = $this->siswa->orderBy('id', 'asc');
71
        }
72
73
        if ($request->exists('filter')) {
74
            $query->where(function($q) use($request) {
75
                $value = "%{$request->filter}%";
76
77
                $q->where('nomor_un', 'like', $value)
78
                    ->orWhere('nik', 'like', $value)
79
                    ->orWhere('nama_siswa', 'like', $value)
80
                    ->orWhere('no_kk', 'like', $value)
81
                    ->orWhere('nisn', 'like', $value);
82
            });
83
        }
84
85
        $perPage    = request()->has('per_page') ? (int) request()->per_page : null;
86
87
        $response   = $query->with(['province', 'city', 'district', 'village', 'sekolah', 'prodi_sekolah', 'user'])->paginate($perPage);
88
89
        foreach($response as $siswa){
90
            if (isset($siswa->prodi_sekolah->program_keahlian)) {
91
                $siswa->prodi_sekolah->program_keahlian;
92
            }
93
        }
94
95
        return response()->json($response)
96
            ->header('Access-Control-Allow-Origin', '*')
97
            ->header('Access-Control-Allow-Methods', 'GET');
98
    }
99
100
    /**
101
     * Display a listing of the resource.
102
     *
103
     * @return \Illuminate\Http\Response
104
     */
105
    public function get()
106
    {
107
        $siswas = $this->siswa->with(['province', 'city', 'district', 'village', 'sekolah', 'prodi_sekolah', 'user'])->get();
108
109
        foreach($siswas as $siswa){
110
            array_set($siswa, 'label', $siswa->nama);
111
112
            if (isset($siswa->prodi_sekolah->program_keahlian)) {
113
                $siswa->prodi_sekolah->program_keahlian;
114
            }
115
        }
116
117
        $response['siswas']   = $siswas;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
118
        $response['error']      = false;
119
        $response['message']    = 'Success';
120
        $response['status']     = true;
121
122
        return response()->json($response);
123
    }
124
125
    /**
126
     * Show the form for creating a new resource.
127
     *
128
     * @return \Illuminate\Http\Response
129
     */
130
    public function create(Request $request)
131
    {
132
        $user_id        = isset(Auth::User()->id) ? Auth::User()->id : null;
133
        $siswa          = $this->siswa->getAttributes();
134
        $provinces      = $this->province->getAttributes();
135
        $cities         = $this->city->getAttributes();
136
        $districts      = $this->district->getAttributes();
137
        $villages       = $this->village->getAttributes();
138
        $sekolahs       = $this->sekolah->getAttributes();
139
        $prodi_sekolahs = $this->prodi_sekolah->getAttributes();
140
        $users          = $this->user->getAttributes();
0 ignored issues
show
Unused Code introduced by
$users is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
141
        $users_special  = $this->user->all();
142
        $users_standar  = $this->user->findOrFail($user_id);
143
        $current_user   = Auth::User();
144
145
        foreach($provinces as $province){
146
            array_set($province, 'label', $province->name);
147
        }
148
149
        foreach($cities as $city){
150
            array_set($city, 'label', $city->name);
151
        }
152
153
        foreach($districts as $district){
154
            array_set($district, 'label', $district->name);
155
        }
156
157
        foreach($villages as $village){
158
            array_set($village, 'label', $village->name);
159
        }
160
161
        foreach($sekolahs as $sekolah){
162
            array_set($sekolah, 'label', $sekolah->nama);
163
        }
164
165
        foreach($prodi_sekolahs as $prodi_sekolah){
166
            array_set($prodi_sekolah, 'label', $prodi_sekolah->keterangan);
167
        }
168
169
        $role_check = Auth::User()->hasRole(['superadministrator','administrator']);
170
171
        if($role_check){
172
            $user_special = true;
173
174
            foreach($users_special as $user){
175
                array_set($user, 'label', $user->name);
176
            }
177
178
            $users = $users_special;
179
        }else{
180
            $user_special = false;
181
182
            array_set($users_standar, 'label', $users_standar->name);
183
184
            $users = $users_standar;
185
        }
186
187
        array_set($current_user, 'label', $current_user->name);
188
189
        $response['siswa']          = $siswa;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
190
        $response['provinces']      = $provinces;
191
        $response['cities']         = $cities;
192
        $response['districts']      = $districts;
193
        $response['villages']       = $villages;
194
        $response['sekolahs']       = $sekolahs;
195
        $response['prodi_sekolahs'] = $prodi_sekolahs;
196
        $response['users']          = $users;
197
        $response['user_special']   = $user_special;
198
        $response['current_user']   = $current_user;
199
        $response['error']          = false;
200
        $response['message']        = 'Success';
201
        $response['status']         = true;
202
203
        return response()->json($response);
204
    }
205
206
    /**
207
     * Store a newly created resource in storage.
208
     *
209
     * @param  \Illuminate\Http\Request  $request
210
     * @return \Illuminate\Http\Response
211
     */
212
    public function store(Request $request)
213
    {
214
        $siswa      = $this->siswa;
215
        $validator  = Validator::make($request->all(), [
216
            'nomor_un'          => "required|max:255|unique:{$this->siswa->getTable()},nomor_un,NULL,id,deleted_at,NULL",
217
            'nik'               => "required|numeric|max:16|unique:{$this->siswa->getTable()},nik,NULL,id,deleted_at,NULL",
218
            'nama_siswa'        => 'required|max:255',
219
            'no_kk'             => "required|numeric|max:16|unique:{$this->siswa->getTable()},no_kk,NULL,id,deleted_at,NULL",
220
            'alamat_kk'         => 'required|max:255',
221
            'province_id'       => "required|exists:{$this->province->getTable()},id",
222
            'city_id'           => "required|exists:{$this->city->getTable()},id",
223
            'district_id'       => "required|exists:{$this->district->getTable()},id",
224
            'village_id'        => "required|exists:{$this->village->getTable()},id",
225
            'tempat_lahir'      => 'required|max:255',
226
            'tgl_lahir'         => 'required|date',
227
            'jenis_kelamin'     => 'required|max:255',
228
            'agama'             => 'required|max:255',
229
            'nisn'              => 'required|numeric|max:255',
230
            'tahun_lulus'       => 'required|date_format:Y',
231
            'sekolah_id'        => "required|exists:{$this->sekolah->getTable()},id",
232
            'prodi_sekolah_id'  => "required|exists:{$this->prodi_sekolah->getTable()},id",
233
            'user_id'           => "required|exists:{$this->user->getTable()},id",
234
        ]);
235
236
        if ($validator->fails()) {
237
            $error      = true;
238
            $message    = $validator->errors()->first();
239
        } else {
240
            $siswa->user_id             = $request->input('user_id');
241
            $siswa->nomor_un            = $request->input('nomor_un');
242
            $siswa->nik                 = $request->input('nik');
243
            $siswa->nama_siswa          = $request->input('nama_siswa');
244
            $siswa->no_kk               = $request->input('no_kk');
245
            $siswa->alamat_kk           = $request->input('alamat_kk');
246
            $siswa->province_id         = $request->input('province_id');
247
            $siswa->city_id             = $request->input('city_id');
248
            $siswa->district_id         = $request->input('district_id');
249
            $siswa->village_id          = $request->input('village_id');
250
            $siswa->tempat_lahir        = $request->input('tempat_lahir');
251
            $siswa->tgl_lahir           = $request->input('tgl_lahir');
252
            $siswa->jenis_kelamin       = $request->input('jenis_kelamin');
253
            $siswa->agama               = $request->input('agama');
254
            $siswa->nisn                = $request->input('nisn');
255
            $siswa->sekolah_id          = $request->input('sekolah_id');
256
            $siswa->prodi_sekolah_id    = $request->input('prodi_sekolah_id');
257
            $siswa->tahun_lulus         = $request->input('tahun_lulus');
258
            $siswa->save();
259
260
            $error      = false;
261
            $message    = 'Success';
262
        }
263
264
        $response['siswa']      = $siswa;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
265
        $response['error']      = $error;
266
        $response['message']    = $message;
267
        $response['status']     = true;
268
269
        return response()->json($response);
270
    }
271
272
    /**
273
     * Display the specified resource.
274
     *
275
     * @param  \App\Siswa  $siswa
0 ignored issues
show
Bug introduced by
There is no parameter named $siswa. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
276
     * @return \Illuminate\Http\Response
277
     */
278
    public function show($id)
279
    {
280
        $siswa = $this->siswa->with(['province', 'city', 'district', 'village', 'sekolah', 'prodi_sekolah', 'user'])->findOrFail($id);
281
282
        if (isset($siswa->prodi_sekolah->program_keahlian)) {
283
            $siswa->prodi_sekolah->program_keahlian;
284
        }
285
286
        $response['siswa']      = $siswa;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
287
        $response['error']      = false;
288
        $response['message']    = 'Success';
289
        $response['status']     = true;
290
291
        return response()->json($response);
292
    }
293
294
    /**
295
     * Show the form for editing the specified resource.
296
     *
297
     * @param  \App\Siswa  $siswa
0 ignored issues
show
Bug introduced by
There is no parameter named $siswa. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
298
     * @return \Illuminate\Http\Response
299
     */
300
    public function edit($id)
301
    {
302
        $siswa = $this->siswa->with(['province', 'city', 'district', 'village', 'sekolah', 'prodi_sekolah', 'user'])->findOrFail($id);
303
304
        $response['siswa']      = $siswa;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
305
        $response['error']      = false;
306
        $response['message']    = 'Success';
307
        $response['status']     = true;
308
309
        return response()->json($response);
310
    }
311
312
    /**
313
     * Update the specified resource in storage.
314
     *
315
     * @param  \Illuminate\Http\Request  $request
316
     * @param  \App\Siswa  $siswa
0 ignored issues
show
Bug introduced by
There is no parameter named $siswa. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
317
     * @return \Illuminate\Http\Response
318
     */
319
    public function update(Request $request, $id)
320
    {
321
        $response = array();
322
        $message  = array();
323
        $siswa = $this->siswa->findOrFail($id);
324
325
            $validator = Validator::make($request->all(), [
326
                'user_id'       => 'required|unique:siswas,user_id,'.$id,
327
                'label'         => 'required',
328
                'description'   => 'required',
329
                'nomor_un'      => 'required|unique:siswas,nomor_un,'.$id,
330
                'nik'           => 'required|unique:siswas,nik,'.$id,
331
                'nama_siswa'    => 'required',
332
                'alamat_kk'     => 'required',
333
                'tempat_lahir'  => 'required',
334
                'tgl_lahir'     => 'required',
335
                'jenis_kelamin' => 'required',
336
                'agama'         => 'required',
337
                'nisn'          => 'required',
338
                'tahun_lulus'   => 'required',
339
340
                ]);
341
342
                if($validator->fails()){
343
344
                    foreach($validator->messages()->getMessages() as $key => $error){
345
                        foreach($error AS $error_get) {
346
                            array_push($message, $error_get);
347
                        }
348
                    }
349
350
351
                    $check_user     = $this->siswa->where('id','!=', $id)->where('user_id', $request->user_id);
352
                    $check_nomor_un = $this->siswa->where('id','!=', $id)->where('nomor_un', $request->nomor_un);
353
                    $check_nik      = $this->siswa->where('id','!=', $id)->where('nik', $request->nik);
354
355
                    if($check_nomor_un->count() > 0 || $check_user->count() > 0 || $check_nik->count() > 0){
356
                        $response['message'] = implode("\n",$message);
357
358
                    }else{
359
                         $siswa->user_id = $request->input('user_id');
360
                        $siswa->label = $request->input('label');
361
                        $siswa->description = $request->input('description');
362
                        $siswa->nomor_un = $request->input('nomor_un');
363
                        $siswa->nik = $request->input('nik');
364
                        $siswa->nama_siswa = $request->input('nama_siswa');
365
                        $siswa->alamat_kk = $request->input('alamat_kk');
366
                        $siswa->tempat_lahir = $request->input('tempat_lahir');
367
                        $siswa->tgl_lahir = $request->input('tgl_lahir');
368
                        $siswa->jenis_kelamin = $request->input('jenis_kelamin');
369
                        $siswa->agama = $request->input('agama');
370
                        $siswa->nisn = $request->input('nisn');
371
                        $siswa->tahun_lulus = $request->input('tahun_lulus');
372
                        $siswa->save();
373
                        $response['message'] = 'success';
374
                    }
375
             }else{
376
                    $siswa->user_id = $request->input('user_id');
377
                    $siswa->label = $request->input('label');
378
                    $siswa->description = $request->input('description');
379
                    $siswa->nomor_un = $request->input('nomor_un');
380
                    $siswa->nik = $request->input('nik');
381
                    $siswa->nama_siswa = $request->input('nama_siswa');
382
                    $siswa->alamat_kk = $request->input('alamat_kk');
383
                    $siswa->tempat_lahir = $request->input('tempat_lahir');
384
                    $siswa->tgl_lahir = $request->input('tgl_lahir');
385
                    $siswa->jenis_kelamin = $request->input('jenis_kelamin');
386
                    $siswa->agama = $request->input('agama');
387
                    $siswa->nisn = $request->input('nisn');
388
                    $siswa->tahun_lulus = $request->input('tahun_lulus');
389
                    $siswa->save();
390
                    $response['message'] = 'success';
391
        }
392
393
        $response['status'] = true;
394
        return response()->json($response);
395
    }
396
397
    /**
398
     * Remove the specified resource from storage.
399
     *
400
     * @param  \App\Siswa  $siswa
0 ignored issues
show
Bug introduced by
There is no parameter named $siswa. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
401
     * @return \Illuminate\Http\Response
402
     */
403
    public function destroy($id)
404
    {
405
        $siswa = $this->siswa->findOrFail($id);
406
407
        if ($siswa->delete()) {
408
            $response['message']    = 'Success';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
409
            $response['success']    = true;
410
            $response['status']     = true;
411
        } else {
412
            $response['message']    = 'Failed';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
413
            $response['success']    = false;
414
            $response['status']     = false;
415
        }
416
417
        return json_encode($response);
418
    }
419
}
420