Completed
Push — master ( f39e45...7bc33b )
by
unknown
10s
created

ZonaController::create()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 23
nc 6
nop 0
dl 0
loc 34
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace Bantenprov\Zona\Http\Controllers;
4
5
/* Require */
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Bantenprov\BudgetAbsorption\Facades\ZonaFacade;
9
10
/* Models */
11
use Bantenprov\Zona\Models\Bantenprov\Zona\Zona;
12
use Bantenprov\Siswa\Models\Bantenprov\Siswa\Siswa;
13
use App\User;
14
15
/* Etc */
16
use Validator;
17
18
/**
19
 * The ZonaController class.
20
 *
21
 * @package Bantenprov\Zona
22
 * @author  bantenprov <[email protected]>
23
 */
24
class ZonaController extends Controller
25
{  
0 ignored issues
show
Coding Style introduced by
The opening class brace should be on a newline by itself.
Loading history...
26
    /**
27
     * Create a new controller instance.
28
     *
29
     * @return void
30
     */
31
    protected $siswa;
32
    protected $zona;
33
    protected $user;
34
35
    public function __construct(Zona $zona, Siswa $siswa, User $user)
36
    {
37
        $this->zona             = $zona;
38
        $this->siswa            = $siswa;
39
        $this->user             = $user;
40
    }
41
42
    /**
43
     * Display a listing of the resource.
44
     *
45
     * @return \Illuminate\Http\Response
46
     */
47
    public function index(Request $request)
48
    {
49
        if (request()->has('sort')) {
50
            list($sortCol, $sortDir) = explode('|', request()->sort);
51
52
            $query = $this->zona->orderBy($sortCol, $sortDir);
53
        } else {
54
            $query = $this->zona->orderBy('id', 'asc');
55
        }
56
57
        if ($request->exists('filter')) {
58
            $query->where(function($q) use($request) {
59
                $value = "%{$request->filter}%";
60
                $q->where('id', 'like', $value)
61
                    ->orWhere('siswa_id', 'like', $value);
62
            });
63
        }
64
65
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
66
        $response = $query->with('siswa')->with('user')->paginate($perPage);
67
68
        return response()->json($response)
69
            ->header('Access-Control-Allow-Origin', '*')
70
            ->header('Access-Control-Allow-Methods', 'GET');
71
    }
72
73
    /**
74
     * Show the form for creating a new resource.
75
     *
76
     * @return \Illuminate\Http\Response
77
     */
78
    public function create()
79
    {   
80
        $response = [];
81
        $siswas = $this->siswa->all();
82
        $users_special = $this->user->all();
83
        $users_standar = $this->user->find(\Auth::User()->id);
84
        $current_user = \Auth::User();
85
86
        $role_check = \Auth::User()->hasRole(['superadministrator','administrator']);
87
88
        if($role_check){
89
            $response['user_special'] = true;
90
            foreach($users_special as $user){
91
                array_set($user, 'label', $user->name);
92
            }
93
            $response['user'] = $users_special;
94
        }else{
95
            $response['user_special'] = false;
96
            array_set($users_standar, 'label', $users_standar->name);
97
            $response['user'] = $users_standar;
98
        }
99
100
        array_set($current_user, 'label', $current_user->name);
101
102
        foreach($siswas as $siswa){
103
            array_set($siswa, 'label', $siswa->nama_siswa);
104
        }
105
106
        $response['siswa'] = $siswas;
107
        $response['current_user'] = $current_user;
108
        $response['status'] = true;
109
110
        return response()->json($response);
111
    }
112
113
    /**
114
     * Display the specified resource.
115
     *
116
     * @param  \App\Zona  $zona
0 ignored issues
show
Bug introduced by
There is no parameter named $zona. 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...
117
     * @return \Illuminate\Http\Response
118
     */
119
    public function store(Request $request)
120
    {
121
        $zona = $this->zona;
122
123
        $validator = Validator::make($request->all(), [
124
            'user_id' => 'required|unique:zonas,user_id',
125
            'master_zona_id' => 'required',
126
            'nomor_un' => 'required|unique:zonas,nomor_un',
127
            'sekolah_id' => 'required',
128
            'zona_siswa' => 'required',
129
            'zona_sekolah' => 'required',
130
            'lokasi_siswa' => 'required',
131
            'lokasi_sekolah' => 'required',
132
            'nilai_zona' => 'required',
133
        ]);
134
135
        if($validator->fails()){
136
            $check = $zona->where('user_id',$request->user_id)->orWhere('nomor_un',$request->nomor_un)->whereNull('deleted_at')->count();
137
138
            if ($check > 0) {
139
                $response['message'] = 'Failed ! Username, Nama Siswa already exists';
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...
140 View Code Duplication
            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
                $zona->user_id = $request->input('user_id');
142
                $zona->master_zona_id = $request->input('master_zona_id');
143
                $zona->nomor_un = $request->input('nomor_un');
144
                $zona->sekolah_id = $request->input('sekolah_id');
145
                $zona->zona_siswa = $request->input('zona_siswa');
146
                $zona->zona_sekolah = $request->input('zona_sekolah');
147
                $zona->lokasi_siswa = $request->input('lokasi_siswa');
148
                $zona->lokasi_sekolah = $request->input('lokasi_sekolah');
149
                $zona->nilai_zona = $request->input('nilai_zona');
150
                $zona->save();
151
152
                $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...
153
            }
154 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
            $zona->user_id = $request->input('user_id');
156
                $zona->master_zona_id = $request->input('master_zona_id');
157
                $zona->nomor_un = $request->input('nomor_un');
158
                $zona->sekolah_id = $request->input('sekolah_id');
159
                $zona->zona_siswa = $request->input('zona_siswa');
160
                $zona->zona_sekolah = $request->input('zona_sekolah');
161
                $zona->lokasi_siswa = $request->input('lokasi_siswa');
162
                $zona->lokasi_sekolah = $request->input('lokasi_sekolah');
163
                $zona->nilai_zona = $request->input('nilai_zona');
164
                $zona->save();
165
166
            $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...
167
        }
168
169
        $response['status'] = true;
170
171
        return response()->json($response);
172
    }
173
174
    /**
175
     * Store a newly created resource in storage.
176
     *
177
     * @param  \Illuminate\Http\Request  $request
0 ignored issues
show
Bug introduced by
There is no parameter named $request. 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...
178
     * @return \Illuminate\Http\Response
179
     */
180
    public function show($id)
181
    {
182
        $zona = $this->zona->findOrFail($id);
183
184
        $response['zona'] = $zona;
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...
185
        $response['siswa'] = $zona->siswa;
186
        $response['user'] = $zona->user;
187
        $response['status'] = true;
188
189
        return response()->json($response);
190
    }
191
192
    /**
193
     * Show the form for editing the specified resource.
194
     *
195
     * @param  \App\Zona  $zona
0 ignored issues
show
Bug introduced by
There is no parameter named $zona. 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...
196
     * @return \Illuminate\Http\Response
197
     */
198
    public function edit($id)
199
    {
200
        $zona = $this->zona->findOrFail($id);
201
202
        array_set($zona->user, 'label', $zona->user->name);
203
        array_set($zona->siswa, 'label', $zona->siswa->nama_siswa);
204
205
        $response['zona'] = $zona;
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...
206
        $response['siswa'] = $zona->siswa;
207
        $response['user'] = $zona->user;
208
        $response['status'] = true;
209
210
        return response()->json($response);
211
    }
212
213
    /**
214
     * Update the specified resource in storage.
215
     *
216
     * @param  \Illuminate\Http\Request  $request
217
     * @param  \App\Zona  $zona
0 ignored issues
show
Bug introduced by
There is no parameter named $zona. 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...
218
     * @return \Illuminate\Http\Response
219
     */
220
    public function update(Request $request, $id)
221
    {
222
        $response = array();
223
        $message  = array();
224
225
        $zona = $this->zona->findOrFail($id);
226
227
        $validator = Validator::make($request->all(), [
228
            'user_id' => 'required|unique:zonas,user_id,'.$id,
229
            'master_zona_id' => 'required',
230
            'nomor_un' => 'required|unique:zonas,nomor_un,'.$id,
231
            'sekolah_id' => 'required',
232
            'zona_siswa' => 'required',
233
            'zona_sekolah' => 'required',
234
            'lokasi_siswa' => 'required',
235
            'lokasi_sekolah' => 'required',
236
            'nilai_zona' => 'required',
237
        ]);
238
239
        if ($validator->fails()) {
240
241
            foreach($validator->messages()->getMessages() as $key => $error){
242
                        foreach($error AS $error_get) {
243
                            array_push($message, $error_get);
244
                        }                
245
                    } 
246
247
             $check_user     = $this->zona->where('id','!=', $id)->where('user_id', $request->user_id);
248
             $check_siswa = $this->zona->where('id','!=', $id)->where('nomor_un', $request->nomor_un);
249
250
             if($check_user->count() > 0 || $check_siswa->count() > 0){
251
                  $response['message'] = implode("\n",$message);
252 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
253
            $zona->user_id = $request->input('user_id');
254
                $zona->master_zona_id = $request->input('master_zona_id');
255
                $zona->nomor_un = $request->input('nomor_un');
256
                $zona->sekolah_id = $request->input('sekolah_id');
257
                $zona->zona_siswa = $request->input('zona_siswa');
258
                $zona->zona_sekolah = $request->input('zona_sekolah');
259
                $zona->lokasi_siswa = $request->input('lokasi_siswa');
260
                $zona->lokasi_sekolah = $request->input('lokasi_sekolah');
261
                $zona->nilai_zona = $request->input('nilai_zona');
262
                $zona->save();
263
264
            $response['message'] = 'success';
265
        }
266
267 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
268
            $zona->user_id = $request->input('user_id');
269
                $zona->master_zona_id = $request->input('master_zona_id');
270
                $zona->nomor_un = $request->input('nomor_un');
271
                $zona->sekolah_id = $request->input('sekolah_id');
272
                $zona->zona_siswa = $request->input('zona_siswa');
273
                $zona->zona_sekolah = $request->input('zona_sekolah');
274
                $zona->lokasi_siswa = $request->input('lokasi_siswa');
275
                $zona->lokasi_sekolah = $request->input('lokasi_sekolah');
276
                $zona->nilai_zona = $request->input('nilai_zona');
277
                $zona->save();
278
279
                $response['status'] = true;
280
281
        }
282
283
        return response()->json($response);
284
    }
285
286
    /**
287
     * Remove the specified resource from storage.
288
     *
289
     * @param  \App\Zona  $zona
0 ignored issues
show
Bug introduced by
There is no parameter named $zona. 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...
290
     * @return \Illuminate\Http\Response
291
     */
292
    public function destroy($id)
293
    {
294
        $zona = $this->zona->findOrFail($id);
295
296
        if ($zona->delete()) {
297
            $response['status'] = true;
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...
298
        } else {
299
            $response['status'] = false;
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...
300
        }
301
302
        return json_encode($response);
303
    }
304
}
305