Completed
Push — master ( c8668a...a2fb60 )
by
unknown
01:31
created

DataAkademikController::edit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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