Completed
Push — master ( 6c09c9...188db0 )
by
unknown
01:47 queued 28s
created

OrangTuaController::create()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 19
nc 3
nop 0
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Bantenprov\OrangTua\Http\Controllers;
4
5
/* Require */
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Bantenprov\OrangTua\Facades\OrangTuaFacade;
9
10
/* Models */
11
use Bantenprov\OrangTua\Models\Bantenprov\OrangTua\OrangTua;
12
use App\User;
13
14
/* Etc */
15
use Validator;
16
17
/**
18
 * The OrangTuaController class.
19
 *
20
 * @package Bantenprov\OrangTua
21
 * @author  bantenprov <[email protected]>
22
 */
23
class OrangTuaController extends Controller
24
{
25
    /**
26
     * Create a new controller instance.
27
     *
28
     * @return void
29
     */
30
31
     protected $orang_tua;
32
     protected $user;
33
34
    public function __construct(OrangTua $orang_tua, User $user)
35
    {
36
        $this->orang_tua = $orang_tua;
37
        $this->user = $user;
38
    }
39
40
    /**
41
     * Display a listing of the resource.
42
     *
43
     * @return \Illuminate\Http\Response
44
     */
45
    public function index(Request $request)
46
    {
47
        if ($request->has('sort')) {
48
            list($sortCol, $sortDir) = explode('|', $request->sort);
49
50
            $query = $this->orang_tua->orderBy($sortCol, $sortDir);
51
        } else {
52
            $query = $this->orang_tua->orderBy('id', 'asc');
53
        }
54
55
        if ($request->exists('filter')) {
56
            $query->where(function($q) use($request) {
57
               $value = "%{$request->filter}%";
58
                $q->where('nomor_un', 'like', $value)
59
                  ->orWhere('no_kk', 'like', $value);
60
            });
61
        }
62
63
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
64
        $response = $query->with('user')->paginate($perPage);
65
66
        return response()->json($response)
67
            ->header('Access-Control-Allow-Origin', '*')
68
            ->header('Access-Control-Allow-Methods', 'GET');
69
    }
70
71
    /**
72
     * Show the form for creating a new resource.
73
     *
74
     * @return \Illuminate\Http\Response
75
     */
76
    public function create()
77
    {
78
        $response = [];
79
        $users_special = $this->user->all();
80
        $users_standar = $this->user->find(\Auth::User()->id);
81
        $current_user = \Auth::User();
82
83
        $role_check = \Auth::User()->hasRole(['superadministrator','administrator']);
84
85
        if($role_check){
86
            $response['user_special'] = true;
87
            foreach($users_special as $user){
88
                array_set($user, 'label', $user->name);
89
            }
90
            $response['user'] = $users_special;
91
        }else{
92
            $response['user_special'] = false;
93
            array_set($users_standar, 'label', $users_standar->name);
94
            $response['user'] = $users_standar;
95
        }
96
97
        array_set($current_user, 'label', $current_user->name);
98
99
        $response['current_user'] = $current_user;
100
        $response['status'] = true;
101
102
        return response()->json($response);
103
    }
104
105
    /**
106
     * Display the specified resource.
107
     *
108
     * @param  \App\OrangTua  $orang_tua
0 ignored issues
show
Bug introduced by
There is no parameter named $orang_tua. 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...
109
     * @return \Illuminate\Http\Response
110
     */
111
    public function store(Request $request)
112
    {
113
        $orang_tua = $this->orang_tua;
114
115
        $validator = Validator::make($request->all(), [
116
            'user_id' => 'required|unique:orangtuas,user_id',
117
            'nomor_un'   => 'required|unique:orangtuas,nomor_un',
118
            'no_kk'   => 'required|unique:orangtuas,no_kk',
119
            'no_telp'   => 'required|unique:orangtuas,no_telp',
120
            'nama_ayah'   => 'required',
121
            'nama_ibu'   => 'required',
122
            'pendidikan_ayah'   => 'required',
123
            'kerja_ayah'   => 'required',
124
            'pendidikan_ibu'   => 'required',
125
            'kerja_ibu'   => 'required',
126
            'alamat_ortu'   => 'required',
127
        ]);
128
129
        if($validator->fails()){
130
            $check = $orang_tua->where('user_id',$request->user_id)->orWhere('nomor_un',$request->nomor_un)->orWhere('no_kk',$request->no_kk)->orWhere('no_telp',$request->no_telp)->whereNull('deleted_at')->count();
131
132
            if ($check > 0) {
133
                $response['message'] = 'Failed ! Username, Nomor UN, Nomor KK, Nomor Telp 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...
134 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...
135
            $orang_tua->user_id = $request->input('user_id');
136
            $orang_tua->nomor_un = $request->input('nomor_un');
137
            $orang_tua->no_kk = $request->input('no_kk');
138
            $orang_tua->no_telp = $request->input('no_telp');
139
            $orang_tua->nama_ayah = $request->input('nama_ayah');
140
            $orang_tua->nama_ibu = $request->input('nama_ibu');
141
            $orang_tua->pendidikan_ayah = $request->input('pendidikan_ayah');
142
            $orang_tua->kerja_ayah = $request->input('kerja_ayah');
143
            $orang_tua->pendidikan_ibu = $request->input('pendidikan_ibu');
144
            $orang_tua->kerja_ibu = $request->input('kerja_ibu');
145
            $orang_tua->alamat_ortu = $request->input('alamat_ortu');
146
            $orang_tua->save();
147
148
            $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...
149
            }
150
151 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...
152
            $orang_tua->user_id = $request->input('user_id');
153
            $orang_tua->nomor_un = $request->input('nomor_un');
154
            $orang_tua->no_kk = $request->input('no_kk');
155
            $orang_tua->no_telp = $request->input('no_telp');
156
            $orang_tua->nama_ayah = $request->input('nama_ayah');
157
            $orang_tua->nama_ibu = $request->input('nama_ibu');
158
            $orang_tua->pendidikan_ayah = $request->input('pendidikan_ayah');
159
            $orang_tua->kerja_ayah = $request->input('kerja_ayah');
160
            $orang_tua->pendidikan_ibu = $request->input('pendidikan_ibu');
161
            $orang_tua->kerja_ibu = $request->input('kerja_ibu');
162
            $orang_tua->alamat_ortu = $request->input('alamat_ortu');
163
            $orang_tua->save();
164
            $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...
165
        }
166
167
        $response['status'] = true;
168
169
        return response()->json($response);
170
    }
171
172
    /**
173
     * Store a newly created resource in storage.
174
     *
175
     * @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...
176
     * @return \Illuminate\Http\Response
177
     */
178
    public function show($id)
179
    {
180
        $orang_tua = $this->orang_tua->findOrFail($id);
181
182
        $response['user'] = $orang_tua->user;
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...
183
        $response['orang_tua'] = $orang_tua;
184
        $response['status'] = true;
185
186
        return response()->json($response);
187
    }
188
189
    /**
190
     * Show the form for editing the specified resource.
191
     *
192
     * @param  \App\OrangTua  $orang_tua
0 ignored issues
show
Bug introduced by
There is no parameter named $orang_tua. 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...
193
     * @return \Illuminate\Http\Response
194
     */
195
    public function edit($id)
196
    {
197
        $orang_tua = $this->orang_tua->findOrFail($id);
198
199
        array_set($orang_tua->user, 'label', $orang_tua->user->name);
200
201
        $response['user'] = $orang_tua->user;
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...
202
        $response['orang_tua'] = $orang_tua;
203
        $response['status'] = true;
204
205
        return response()->json($response);
206
    }
207
208
    /**
209
     * Update the specified resource in storage.
210
     *
211
     * @param  \Illuminate\Http\Request  $request
212
     * @param  \App\OrangTua  $orang_tua
0 ignored issues
show
Bug introduced by
There is no parameter named $orang_tua. 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...
213
     * @return \Illuminate\Http\Response
214
     */
215
    public function update(Request $request, $id)
216
    {
217
        $response = array();
218
        $message  = array();
219
        $orang_tua = $this->orang_tua->findOrFail($id);
220
221
        $validator = Validator::make($request->all(), [
222
            'user_id' => 'required|unique:orangtuas,user_id,'.$id,
223
            'nomor_un'   => 'required |unique:orangtuas,nomor_un,'.$id,
224
            'no_kk'   => 'required|unique:orangtuas,no_kk,'.$id,
225
            'no_telp'   => 'required|unique:orangtuas,no_telp,'.$id,
226
            'nama_ayah'   => 'required',
227
            'nama_ibu'   => 'required',
228
            'pendidikan_ayah'   => 'required',
229
            'kerja_ayah'   => 'required',
230
            'pendidikan_ibu'   => 'required',
231
            'kerja_ibu'   => 'required',
232
            'alamat_ortu'   => 'required',
233
        ]);
234
235
        if ($validator->fails()) {
236
237
            foreach($validator->messages()->getMessages() as $key => $error){
238
                        foreach($error AS $error_get) {
239
                            array_push($message, $error_get);
240
                        }
241
                    }
242
243
             $check_user     = $this->orang_tua->where('id','!=', $id)->where('user_id', $request->user_id);
244
             $check_nomor_un = $this->orang_tua->where('id','!=', $id)->where('nomor_un', $request->nomor_un);
245
             $check_no_kk = $this->orang_tua->where('id','!=', $id)->where('no_kk', $request->no_kk);
246
             $check_no_telp = $this->orang_tua->where('id','!=', $id)->where('no_telp', $request->no_telp);
247
248
             if($check_user->count() > 0 || $check_nomor_un->count() > 0 || $check_no_kk->count() > 0 || $check_no_telp->count() > 0){
249
                  $response['message'] = implode("\n",$message);
250
        } else {
251
            $orang_tua->user_id    = $request->input('user_id');
252
            $orang_tua->nomor_un = $request->input('nomor_un');
253
            $orang_tua->no_kk = $request->input('no_kk');
254
            $orang_tua->no_telp = $request->input('no_telp');
255
            $orang_tua->nama_ayah = $request->input('nama_ayah');
256
            $orang_tua->nama_ibu = $request->input('nama_ibu');
257
            $orang_tua->pendidikan_ayah = $request->input('pendidikan_ayah');
258
            $orang_tua->kerja_ayah = $request->input('kerja_ayah');
259
            $orang_tua->pendidikan_ibu = $request->input('pendidikan_ibu');
260
            $orang_tua->kerja_ibu = $request->input('kerja_ibu');
261
            $orang_tua->alamat_ortu = $request->input('alamat_ortu');
262
            $orang_tua->save();
263
264
            $response['message'] = 'success';
265
266
          }
267
        } else {
268
            $orang_tua->user_id    = $request->input('user_id');
269
            $orang_tua->nomor_un = $request->input('nomor_un');
270
            $orang_tua->no_kk = $request->input('no_kk');
271
            $orang_tua->no_telp = $request->input('no_telp');
272
            $orang_tua->nama_ayah = $request->input('nama_ayah');
273
            $orang_tua->nama_ibu = $request->input('nama_ibu');
274
            $orang_tua->pendidikan_ayah = $request->input('pendidikan_ayah');
275
            $orang_tua->kerja_ayah = $request->input('kerja_ayah');
276
            $orang_tua->pendidikan_ibu = $request->input('pendidikan_ibu');
277
            $orang_tua->kerja_ibu = $request->input('kerja_ibu');
278
            $orang_tua->alamat_ortu = $request->input('alamat_ortu');
279
            $orang_tua->save();
280
281
            $response['message'] = 'success';
282
        }
283
284
        $response['status'] = true;
285
286
        return response()->json($response);
287
    }
288
289
    /**
290
     * Remove the specified resource from storage.
291
     *
292
     * @param  \App\OrangTua  $orang_tua
0 ignored issues
show
Bug introduced by
There is no parameter named $orang_tua. 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...
293
     * @return \Illuminate\Http\Response
294
     */
295
    public function destroy($id)
296
    {
297
        $orang_tua = $this->orang_tua->findOrFail($id);
298
299
        if ($orang_tua->delete()) {
300
            $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...
301
        } else {
302
            $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...
303
        }
304
305
        return json_encode($response);
306
    }
307
}
308