Completed
Push — master ( c1b837...1e8b34 )
by Sherif
07:31
created
src/Modules/V1/Reporting/Routes/api.php 1 patch
Unused Use Statements   -2 removed lines patch added patch discarded remove patch
@@ -1,7 +1,5 @@
 block discarded – undo
1 1
 <?php
2 2
 
3
-use Illuminate\Http\Request;
4
-
5 3
 /*
6 4
 |--------------------------------------------------------------------------
7 5
 | API Routes
Please login to merge, or discard this patch.
src/Modules/V1/Acl/Repositories/UserRepository.php 1 patch
Indentation   +355 added lines, -355 removed lines patch added patch discarded remove patch
@@ -4,365 +4,365 @@
 block discarded – undo
4 4
 
5 5
 class UserRepository extends AbstractRepository
6 6
 {
7
-    /**
8
-     * Return the model full namespace.
9
-     * 
10
-     * @return string
11
-     */
12
-    protected function getModel()
13
-    {
14
-        return 'App\Modules\V1\Acl\AclUser';
15
-    }
16
-
17
-    /**
18
-     * Return the logged in user account.
19
-     *
20
-     * @param  array   $relations
21
-     * @return boolean
22
-     */
23
-    public function account($relations = [])
24
-    {
25
-        $permissions = [];
26
-        $user        = \Core::users()->find(\JWTAuth::parseToken()->authenticate()->id, $relations);
27
-        foreach ($user->groups()->get() as $group)
28
-        {
29
-            $group->permissions->each(function ($permission) use (&$permissions){
30
-                $permissions[$permission->model][$permission->id] = $permission->name;
31
-            });
32
-        }
33
-        $user->permissions = $permissions;
34
-
35
-       return $user;
36
-    }
37
-
38
-    /**
39
-     * Check if the logged in user or the given user 
40
-     * has the given permissions on the given model.
41
-     * 
42
-     * @param  string  $nameOfPermission
43
-     * @param  string  $model            
44
-     * @param  boolean $user
45
-     * @return boolean
46
-     */
47
-    public function can($nameOfPermission, $model, $user = false )
48
-    {      
49
-        $user        = $user ?: \JWTAuth::parseToken()->authenticate();
50
-        $permissions = [];
51
-
52
-        if ( ! $user = $this->find($user->id, ['groups.permissions'])) 
53
-        {
54
-            \ErrorHandler::tokenExpired();
55
-        }
56
-
57
-        $user->groups->pluck('permissions')->each(function ($permission) use (&$permissions, $model){
58
-            $permissions = array_merge($permissions, $permission->where('model', $model)->pluck('name')->toArray()); 
59
-        });
7
+	/**
8
+	 * Return the model full namespace.
9
+	 * 
10
+	 * @return string
11
+	 */
12
+	protected function getModel()
13
+	{
14
+		return 'App\Modules\V1\Acl\AclUser';
15
+	}
16
+
17
+	/**
18
+	 * Return the logged in user account.
19
+	 *
20
+	 * @param  array   $relations
21
+	 * @return boolean
22
+	 */
23
+	public function account($relations = [])
24
+	{
25
+		$permissions = [];
26
+		$user        = \Core::users()->find(\JWTAuth::parseToken()->authenticate()->id, $relations);
27
+		foreach ($user->groups()->get() as $group)
28
+		{
29
+			$group->permissions->each(function ($permission) use (&$permissions){
30
+				$permissions[$permission->model][$permission->id] = $permission->name;
31
+			});
32
+		}
33
+		$user->permissions = $permissions;
34
+
35
+	   return $user;
36
+	}
37
+
38
+	/**
39
+	 * Check if the logged in user or the given user 
40
+	 * has the given permissions on the given model.
41
+	 * 
42
+	 * @param  string  $nameOfPermission
43
+	 * @param  string  $model            
44
+	 * @param  boolean $user
45
+	 * @return boolean
46
+	 */
47
+	public function can($nameOfPermission, $model, $user = false )
48
+	{      
49
+		$user        = $user ?: \JWTAuth::parseToken()->authenticate();
50
+		$permissions = [];
51
+
52
+		if ( ! $user = $this->find($user->id, ['groups.permissions'])) 
53
+		{
54
+			\ErrorHandler::tokenExpired();
55
+		}
56
+
57
+		$user->groups->pluck('permissions')->each(function ($permission) use (&$permissions, $model){
58
+			$permissions = array_merge($permissions, $permission->where('model', $model)->pluck('name')->toArray()); 
59
+		});
60 60
         
61
-        return in_array($nameOfPermission, $permissions);
62
-    }
63
-
64
-    /**
65
-     * Check if the logged in user has the given group.
66
-     * 
67
-     * @param  string  $groupName
68
-     * @return boolean
69
-     */
70
-    public function hasGroup($groupName)
71
-    {
72
-        $groups = $this->find(\JWTAuth::parseToken()->authenticate()->id)->groups;
73
-        return $groups->pluck('name')->search($groupName, true) === false ? false : true;
74
-    }
75
-
76
-    /**
77
-     * Assign the given group ids to the given user.
78
-     * 
79
-     * @param  integer $user_id    
80
-     * @param  array   $group_ids
81
-     * @return object
82
-     */
83
-    public function assignGroups($user_id, $group_ids)
84
-    {
85
-        \DB::transaction(function () use ($user_id, $group_ids) {
86
-            $user = $this->find($user_id);
87
-            $user->groups()->detach();
88
-            $user->groups()->attach($group_ids);
89
-        });
90
-
91
-        return $this->find($user_id);
92
-    }
93
-
94
-    /**
95
-     * Handle a login request to the application.
96
-     * 
97
-     * @param  array   $credentials    
98
-     * @param  boolean $adminLogin
99
-     * @return array
100
-     */
101
-    public function login($credentials, $adminLogin = false)
102
-    {
103
-        if ( ! $user = $this->first(['email' => $credentials['email']])) 
104
-        {
105
-            \ErrorHandler::loginFailed();
106
-        }
107
-        else if ($adminLogin && $user->groups->pluck('name')->search('Admin', true) === false) 
108
-        {
109
-            \ErrorHandler::loginFailed();
110
-        }
111
-        else if ( ! $adminLogin && $user->groups->pluck('name')->search('Admin', true) !== false) 
112
-        {
113
-            \ErrorHandler::loginFailed();
114
-        }
115
-        else if ($user->blocked)
116
-        {
117
-            \ErrorHandler::userIsBlocked();
118
-        }
119
-        else if ($token = \JWTAuth::attempt($credentials))
120
-        {
121
-            return ['token' => $token];
122
-        }
123
-        else
124
-        {
125
-            \ErrorHandler::loginFailed();
126
-        }
127
-    }
128
-
129
-    /**
130
-     * Handle a social login request of the none admin to the application.
131
-     * 
132
-     * @param  array   $credentials
133
-     * @return array
134
-     */
135
-    public function loginSocial($credentials)
136
-    {
137
-        $access_token = $credentials['auth_code'] ? \Socialite::driver($credentials['type'])->getAccessToken($credentials['auth_code']) : $credentials['access_token'];
138
-        $user         = \Socialite::driver($credentials['type'])->userFromToken($access_token);
139
-
140
-        if ( ! $user->email)
141
-        {
142
-            \ErrorHandler::noSocialEmail();
143
-        }
144
-
145
-        if ( ! $registeredUser = $this->model->where('email', $user->email)->first()) 
146
-        {
147
-            $data = ['email' => $user->email, 'password' => ''];
148
-            return $this->register($data);
149
-        }
150
-        else
151
-        {
152
-            if ( ! \Auth::attempt(['email' => $registeredUser->email, 'password' => '']))
153
-            {
154
-                \ErrorHandler::userAlreadyRegistered();
155
-            }
156
-            return $this->login(['email' => $registeredUser->email, 'password' => ''], false);
157
-        }
158
-    }
61
+		return in_array($nameOfPermission, $permissions);
62
+	}
63
+
64
+	/**
65
+	 * Check if the logged in user has the given group.
66
+	 * 
67
+	 * @param  string  $groupName
68
+	 * @return boolean
69
+	 */
70
+	public function hasGroup($groupName)
71
+	{
72
+		$groups = $this->find(\JWTAuth::parseToken()->authenticate()->id)->groups;
73
+		return $groups->pluck('name')->search($groupName, true) === false ? false : true;
74
+	}
75
+
76
+	/**
77
+	 * Assign the given group ids to the given user.
78
+	 * 
79
+	 * @param  integer $user_id    
80
+	 * @param  array   $group_ids
81
+	 * @return object
82
+	 */
83
+	public function assignGroups($user_id, $group_ids)
84
+	{
85
+		\DB::transaction(function () use ($user_id, $group_ids) {
86
+			$user = $this->find($user_id);
87
+			$user->groups()->detach();
88
+			$user->groups()->attach($group_ids);
89
+		});
90
+
91
+		return $this->find($user_id);
92
+	}
93
+
94
+	/**
95
+	 * Handle a login request to the application.
96
+	 * 
97
+	 * @param  array   $credentials    
98
+	 * @param  boolean $adminLogin
99
+	 * @return array
100
+	 */
101
+	public function login($credentials, $adminLogin = false)
102
+	{
103
+		if ( ! $user = $this->first(['email' => $credentials['email']])) 
104
+		{
105
+			\ErrorHandler::loginFailed();
106
+		}
107
+		else if ($adminLogin && $user->groups->pluck('name')->search('Admin', true) === false) 
108
+		{
109
+			\ErrorHandler::loginFailed();
110
+		}
111
+		else if ( ! $adminLogin && $user->groups->pluck('name')->search('Admin', true) !== false) 
112
+		{
113
+			\ErrorHandler::loginFailed();
114
+		}
115
+		else if ($user->blocked)
116
+		{
117
+			\ErrorHandler::userIsBlocked();
118
+		}
119
+		else if ($token = \JWTAuth::attempt($credentials))
120
+		{
121
+			return ['token' => $token];
122
+		}
123
+		else
124
+		{
125
+			\ErrorHandler::loginFailed();
126
+		}
127
+	}
128
+
129
+	/**
130
+	 * Handle a social login request of the none admin to the application.
131
+	 * 
132
+	 * @param  array   $credentials
133
+	 * @return array
134
+	 */
135
+	public function loginSocial($credentials)
136
+	{
137
+		$access_token = $credentials['auth_code'] ? \Socialite::driver($credentials['type'])->getAccessToken($credentials['auth_code']) : $credentials['access_token'];
138
+		$user         = \Socialite::driver($credentials['type'])->userFromToken($access_token);
139
+
140
+		if ( ! $user->email)
141
+		{
142
+			\ErrorHandler::noSocialEmail();
143
+		}
144
+
145
+		if ( ! $registeredUser = $this->model->where('email', $user->email)->first()) 
146
+		{
147
+			$data = ['email' => $user->email, 'password' => ''];
148
+			return $this->register($data);
149
+		}
150
+		else
151
+		{
152
+			if ( ! \Auth::attempt(['email' => $registeredUser->email, 'password' => '']))
153
+			{
154
+				\ErrorHandler::userAlreadyRegistered();
155
+			}
156
+			return $this->login(['email' => $registeredUser->email, 'password' => ''], false);
157
+		}
158
+	}
159 159
     
160
-    /**
161
-     * Handle a registration request.
162
-     * 
163
-     * @param  array $credentials
164
-     * @return array
165
-     */
166
-    public function register($credentials)
167
-    {
168
-        return ['token' => \JWTAuth::fromUser($this->model->create($credentials))];
169
-    }
170
-
171
-    /**
172
-     * Logout the user.
173
-     * 
174
-     * @return boolean
175
-     */
176
-    public function logout()
177
-    {
178
-        return \JWTAuth::invalidate(\JWTAuth::getToken());
179
-    }
180
-
181
-    /**
182
-     * Block the user.
183
-     *
184
-     * @param  integer $user_id
185
-     * @return object
186
-     */
187
-    public function block($user_id)
188
-    {
189
-        if ( ! $user = $this->find($user_id)) 
190
-        {
191
-            \ErrorHandler::notFound('user');
192
-        }
193
-        if ( ! $this->hasGroup('Admin'))
194
-        {
195
-            \ErrorHandler::noPermissions();
196
-        }
197
-        else if (\JWTAuth::parseToken()->authenticate()->id == $user_id)
198
-        {
199
-            \ErrorHandler::noPermissions();
200
-        }
201
-        else if ($user->groups->pluck('name')->search('Admin', true) !== false) 
202
-        {
203
-            \ErrorHandler::noPermissions();
204
-        }
205
-
206
-        $user->blocked = 1;
207
-        $user->save();
160
+	/**
161
+	 * Handle a registration request.
162
+	 * 
163
+	 * @param  array $credentials
164
+	 * @return array
165
+	 */
166
+	public function register($credentials)
167
+	{
168
+		return ['token' => \JWTAuth::fromUser($this->model->create($credentials))];
169
+	}
170
+
171
+	/**
172
+	 * Logout the user.
173
+	 * 
174
+	 * @return boolean
175
+	 */
176
+	public function logout()
177
+	{
178
+		return \JWTAuth::invalidate(\JWTAuth::getToken());
179
+	}
180
+
181
+	/**
182
+	 * Block the user.
183
+	 *
184
+	 * @param  integer $user_id
185
+	 * @return object
186
+	 */
187
+	public function block($user_id)
188
+	{
189
+		if ( ! $user = $this->find($user_id)) 
190
+		{
191
+			\ErrorHandler::notFound('user');
192
+		}
193
+		if ( ! $this->hasGroup('Admin'))
194
+		{
195
+			\ErrorHandler::noPermissions();
196
+		}
197
+		else if (\JWTAuth::parseToken()->authenticate()->id == $user_id)
198
+		{
199
+			\ErrorHandler::noPermissions();
200
+		}
201
+		else if ($user->groups->pluck('name')->search('Admin', true) !== false) 
202
+		{
203
+			\ErrorHandler::noPermissions();
204
+		}
205
+
206
+		$user->blocked = 1;
207
+		$user->save();
208 208
         
209
-        return $user;
210
-    }
211
-
212
-    /**
213
-     * Unblock the user.
214
-     *
215
-     * @param  integer $user_id
216
-     * @return object
217
-     */
218
-    public function unblock($user_id)
219
-    {
220
-        if ( ! $this->hasGroup('Admin'))
221
-        {
222
-            \ErrorHandler::noPermissions();
223
-        }
224
-
225
-        $user          = $this->find($user_id);
226
-        $user->blocked = 0;
227
-        $user->save();
228
-
229
-        return $user;
230
-    }
231
-
232
-    /**
233
-     * Send a reset link to the given user.
234
-     *
235
-     * @param  string  $url
236
-     * @param  string  $email
237
-     * @return void
238
-     */
239
-    public function sendReset($email, $url)
240
-    {
241
-        view()->composer('auth.emails.password', function($view) use ($url) {
242
-            $view->with(['url' => $url]);
243
-        });
244
-
245
-        $response = \Password::sendResetLink($email, function (\Illuminate\Mail\Message $message) {
246
-            $message->subject('Your Password Reset Link');
247
-        });
248
-
249
-        switch ($response) 
250
-        {
251
-            case \Password::INVALID_USER:
252
-                \ErrorHandler::notFound('email');
253
-        }
254
-    }
255
-
256
-    /**
257
-     * Reset the given user's password.
258
-     *
259
-     * @param  array  $credentials
260
-     * @return array
261
-     */
262
-    public function resetPassword($credentials)
263
-    {
264
-        $token    = false;
265
-        $response = \Password::reset($credentials, function ($user, $password) use (&$token) {
266
-            $user->password = bcrypt($password);
267
-            $user->save();
268
-
269
-            $token = \JWTAuth::fromUser($user);
270
-        });
271
-
272
-        switch ($response) {
273
-            case \Password::PASSWORD_RESET:
274
-                return ['token' => $token];
209
+		return $user;
210
+	}
211
+
212
+	/**
213
+	 * Unblock the user.
214
+	 *
215
+	 * @param  integer $user_id
216
+	 * @return object
217
+	 */
218
+	public function unblock($user_id)
219
+	{
220
+		if ( ! $this->hasGroup('Admin'))
221
+		{
222
+			\ErrorHandler::noPermissions();
223
+		}
224
+
225
+		$user          = $this->find($user_id);
226
+		$user->blocked = 0;
227
+		$user->save();
228
+
229
+		return $user;
230
+	}
231
+
232
+	/**
233
+	 * Send a reset link to the given user.
234
+	 *
235
+	 * @param  string  $url
236
+	 * @param  string  $email
237
+	 * @return void
238
+	 */
239
+	public function sendReset($email, $url)
240
+	{
241
+		view()->composer('auth.emails.password', function($view) use ($url) {
242
+			$view->with(['url' => $url]);
243
+		});
244
+
245
+		$response = \Password::sendResetLink($email, function (\Illuminate\Mail\Message $message) {
246
+			$message->subject('Your Password Reset Link');
247
+		});
248
+
249
+		switch ($response) 
250
+		{
251
+			case \Password::INVALID_USER:
252
+				\ErrorHandler::notFound('email');
253
+		}
254
+	}
255
+
256
+	/**
257
+	 * Reset the given user's password.
258
+	 *
259
+	 * @param  array  $credentials
260
+	 * @return array
261
+	 */
262
+	public function resetPassword($credentials)
263
+	{
264
+		$token    = false;
265
+		$response = \Password::reset($credentials, function ($user, $password) use (&$token) {
266
+			$user->password = bcrypt($password);
267
+			$user->save();
268
+
269
+			$token = \JWTAuth::fromUser($user);
270
+		});
271
+
272
+		switch ($response) {
273
+			case \Password::PASSWORD_RESET:
274
+				return ['token' => $token];
275 275
                 
276
-            case \Password::INVALID_TOKEN:
277
-                \ErrorHandler::invalidResetToken('token');
278
-
279
-            case \Password::INVALID_PASSWORD:
280
-                \ErrorHandler::invalidResetPassword('email');
281
-
282
-            case \Password::INVALID_USER:
283
-                \ErrorHandler::notFound('user');
284
-
285
-            default:
286
-                \ErrorHandler::generalError();
287
-        }
288
-    }
289
-
290
-    /**
291
-     * Change the logged in user password.
292
-     *
293
-     * @param  array  $credentials
294
-     * @return void
295
-     */
296
-    public function changePassword($credentials)
297
-    {
298
-        $user = $this->find(\JWTAuth::parseToken()->authenticate()->id, $relations);
299
-        if ( ! \Hash::check($credentials['old_password'], $user->password)) 
300
-        {
301
-            \ErrorHandler::invalidOldPassword();
302
-        }
303
-
304
-        $user->password = $credentials['password'];
305
-        $user->save();
306
-    }
307
-
308
-    /**
309
-     * Refresh the expired login token.
310
-     *
311
-     * @return array
312
-     */
313
-    public function refreshtoken()
314
-    {
315
-        $token = \JWTAuth::parseToken()->refresh();
316
-
317
-        return ['token' => $token];
318
-    }
319
-
320
-    /**
321
-     * Paginate all users in the given group based on the given conditions.
322
-     * 
323
-     * @param  string  $groupName
324
-     * @param  array   $relations
325
-     * @param  integer $perPage
326
-     * @param  string  $sortBy
327
-     * @param  boolean $desc
328
-     * @return \Illuminate\Http\Response
329
-     */
330
-    public function group($conditions, $groupName, $relations, $perPage, $sortBy, $desc)
331
-    {   
332
-        unset($conditions['page']);
333
-        $conditions = $this->constructConditions($conditions);
334
-        $sort       = $desc ? 'desc' : 'asc';
335
-        $model      = call_user_func_array("{$this->getModel()}::with", array($relations));
336
-
337
-        $model->whereHas('groups', function($q) use ($groupName){
338
-            $q->where('name', $groupName);
339
-        });
276
+			case \Password::INVALID_TOKEN:
277
+				\ErrorHandler::invalidResetToken('token');
278
+
279
+			case \Password::INVALID_PASSWORD:
280
+				\ErrorHandler::invalidResetPassword('email');
281
+
282
+			case \Password::INVALID_USER:
283
+				\ErrorHandler::notFound('user');
284
+
285
+			default:
286
+				\ErrorHandler::generalError();
287
+		}
288
+	}
289
+
290
+	/**
291
+	 * Change the logged in user password.
292
+	 *
293
+	 * @param  array  $credentials
294
+	 * @return void
295
+	 */
296
+	public function changePassword($credentials)
297
+	{
298
+		$user = $this->find(\JWTAuth::parseToken()->authenticate()->id, $relations);
299
+		if ( ! \Hash::check($credentials['old_password'], $user->password)) 
300
+		{
301
+			\ErrorHandler::invalidOldPassword();
302
+		}
303
+
304
+		$user->password = $credentials['password'];
305
+		$user->save();
306
+	}
307
+
308
+	/**
309
+	 * Refresh the expired login token.
310
+	 *
311
+	 * @return array
312
+	 */
313
+	public function refreshtoken()
314
+	{
315
+		$token = \JWTAuth::parseToken()->refresh();
316
+
317
+		return ['token' => $token];
318
+	}
319
+
320
+	/**
321
+	 * Paginate all users in the given group based on the given conditions.
322
+	 * 
323
+	 * @param  string  $groupName
324
+	 * @param  array   $relations
325
+	 * @param  integer $perPage
326
+	 * @param  string  $sortBy
327
+	 * @param  boolean $desc
328
+	 * @return \Illuminate\Http\Response
329
+	 */
330
+	public function group($conditions, $groupName, $relations, $perPage, $sortBy, $desc)
331
+	{   
332
+		unset($conditions['page']);
333
+		$conditions = $this->constructConditions($conditions);
334
+		$sort       = $desc ? 'desc' : 'asc';
335
+		$model      = call_user_func_array("{$this->getModel()}::with", array($relations));
336
+
337
+		$model->whereHas('groups', function($q) use ($groupName){
338
+			$q->where('name', $groupName);
339
+		});
340 340
 
341 341
         
342
-        if (count($conditions['conditionValues']))
343
-        {
344
-            $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']);
345
-        }
346
-
347
-        if ($perPage) 
348
-        {
349
-            return $model->orderBy($sortBy, $sort)->paginate($perPage);
350
-        }
351
-
352
-        return $model->orderBy($sortBy, $sort)->get();
353
-    }
354
-
355
-    /**
356
-     * Save the given data to the logged in user.
357
-     *
358
-     * @param  array $credentials
359
-     * @return object
360
-     */
361
-    public function saveProfile($credentials) 
362
-    {
363
-        $user = \JWTAuth::parseToken()->authenticate();
364
-        $user->save($credentials);
365
-
366
-        return $user;
367
-    }
342
+		if (count($conditions['conditionValues']))
343
+		{
344
+			$model->whereRaw($conditions['conditionString'], $conditions['conditionValues']);
345
+		}
346
+
347
+		if ($perPage) 
348
+		{
349
+			return $model->orderBy($sortBy, $sort)->paginate($perPage);
350
+		}
351
+
352
+		return $model->orderBy($sortBy, $sort)->get();
353
+	}
354
+
355
+	/**
356
+	 * Save the given data to the logged in user.
357
+	 *
358
+	 * @param  array $credentials
359
+	 * @return object
360
+	 */
361
+	public function saveProfile($credentials) 
362
+	{
363
+		$user = \JWTAuth::parseToken()->authenticate();
364
+		$user->save($credentials);
365
+
366
+		return $user;
367
+	}
368 368
 }
Please login to merge, or discard this patch.
src/Modules/V1/Acl/Database/Migrations/2016_01_05_130507_initialize_acl.php 1 patch
Indentation   +233 added lines, -233 removed lines patch added patch discarded remove patch
@@ -13,233 +13,233 @@  discard block
 block discarded – undo
13 13
 	public function up()
14 14
 	{
15 15
 		/**
16
-         * Insert the permissions related to this module.
17
-         */
18
-        DB::table('permissions')->insert(
19
-        	[
20
-        		/**
21
-        		 * Users model permissions.
22
-        		 */
23
-	        	[
24
-	        	'name'       => 'save',
25
-	        	'model'      => 'users',
26
-	        	'created_at' => \DB::raw('NOW()'),
27
-	        	'updated_at' => \DB::raw('NOW()')
28
-	        	],
29
-	        	[
30
-	        	'name'       => 'delete',
31
-	        	'model'      => 'users',
32
-	        	'created_at' => \DB::raw('NOW()'),
33
-	        	'updated_at' => \DB::raw('NOW()')
34
-	        	],
35
-	        	[
36
-	        	'name'       => 'find',
37
-	        	'model'      => 'users',
38
-	        	'created_at' => \DB::raw('NOW()'),
39
-	        	'updated_at' => \DB::raw('NOW()')
40
-	        	],
41
-	        	[
42
-	        	'name'       => 'list',
43
-	        	'model'      => 'users',
44
-	        	'created_at' => \DB::raw('NOW()'),
45
-	        	'updated_at' => \DB::raw('NOW()')
46
-	        	],
47
-	        	[
48
-	        	'name'       => 'search',
49
-	        	'model'      => 'users',
50
-	        	'created_at' => \DB::raw('NOW()'),
51
-	        	'updated_at' => \DB::raw('NOW()')
52
-	        	],
53
-	        	[
54
-	        	'name'       => 'findby',
55
-	        	'model'      => 'users',
56
-	        	'created_at' => \DB::raw('NOW()'),
57
-	        	'updated_at' => \DB::raw('NOW()')
58
-	        	],
59
-	        	[
60
-	        	'name'       => 'first',
61
-	        	'model'      => 'users',
62
-	        	'created_at' => \DB::raw('NOW()'),
63
-	        	'updated_at' => \DB::raw('NOW()')
64
-	        	],
65
-	        	[
66
-	        	'name'       => 'paginate',
67
-	        	'model'      => 'users',
68
-	        	'created_at' => \DB::raw('NOW()'),
69
-	        	'updated_at' => \DB::raw('NOW()')
70
-	        	],
71
-	        	[
72
-	        	'name'       => 'paginateby',
73
-	        	'model'      => 'users',
74
-	        	'created_at' => \DB::raw('NOW()'),
75
-	        	'updated_at' => \DB::raw('NOW()')
76
-	        	],
77
-	        	[
78
-	        	'name'       => 'assigngroups',
79
-	        	'model'      => 'users',
80
-	        	'created_at' => \DB::raw('NOW()'),
81
-	        	'updated_at' => \DB::raw('NOW()')
82
-	        	],
83
-	        	[
84
-	        	'name'       => 'block',
85
-	        	'model'      => 'users',
86
-	        	'created_at' => \DB::raw('NOW()'),
87
-	        	'updated_at' => \DB::raw('NOW()')
88
-	        	],
89
-	        	[
90
-	        	'name'       => 'unblock',
91
-	        	'model'      => 'users',
92
-	        	'created_at' => \DB::raw('NOW()'),
93
-	        	'updated_at' => \DB::raw('NOW()')
94
-	        	],
95
-	        	[
96
-	        	'name'       => 'group',
97
-	        	'model'      => 'users',
98
-	        	'created_at' => \DB::raw('NOW()'),
99
-	        	'updated_at' => \DB::raw('NOW()')
100
-	        	],
101
-                [
102
-                'name'       => 'deleted',
103
-                'model'      => 'users',
104
-                'created_at' => \DB::raw('NOW()'),
105
-                'updated_at' => \DB::raw('NOW()')
106
-                ],
107
-                [
108
-                'name'       => 'restore',
109
-                'model'      => 'users',
110
-                'created_at' => \DB::raw('NOW()'),
111
-                'updated_at' => \DB::raw('NOW()')
112
-                ],
16
+		 * Insert the permissions related to this module.
17
+		 */
18
+		DB::table('permissions')->insert(
19
+			[
20
+				/**
21
+				 * Users model permissions.
22
+				 */
23
+				[
24
+				'name'       => 'save',
25
+				'model'      => 'users',
26
+				'created_at' => \DB::raw('NOW()'),
27
+				'updated_at' => \DB::raw('NOW()')
28
+				],
29
+				[
30
+				'name'       => 'delete',
31
+				'model'      => 'users',
32
+				'created_at' => \DB::raw('NOW()'),
33
+				'updated_at' => \DB::raw('NOW()')
34
+				],
35
+				[
36
+				'name'       => 'find',
37
+				'model'      => 'users',
38
+				'created_at' => \DB::raw('NOW()'),
39
+				'updated_at' => \DB::raw('NOW()')
40
+				],
41
+				[
42
+				'name'       => 'list',
43
+				'model'      => 'users',
44
+				'created_at' => \DB::raw('NOW()'),
45
+				'updated_at' => \DB::raw('NOW()')
46
+				],
47
+				[
48
+				'name'       => 'search',
49
+				'model'      => 'users',
50
+				'created_at' => \DB::raw('NOW()'),
51
+				'updated_at' => \DB::raw('NOW()')
52
+				],
53
+				[
54
+				'name'       => 'findby',
55
+				'model'      => 'users',
56
+				'created_at' => \DB::raw('NOW()'),
57
+				'updated_at' => \DB::raw('NOW()')
58
+				],
59
+				[
60
+				'name'       => 'first',
61
+				'model'      => 'users',
62
+				'created_at' => \DB::raw('NOW()'),
63
+				'updated_at' => \DB::raw('NOW()')
64
+				],
65
+				[
66
+				'name'       => 'paginate',
67
+				'model'      => 'users',
68
+				'created_at' => \DB::raw('NOW()'),
69
+				'updated_at' => \DB::raw('NOW()')
70
+				],
71
+				[
72
+				'name'       => 'paginateby',
73
+				'model'      => 'users',
74
+				'created_at' => \DB::raw('NOW()'),
75
+				'updated_at' => \DB::raw('NOW()')
76
+				],
77
+				[
78
+				'name'       => 'assigngroups',
79
+				'model'      => 'users',
80
+				'created_at' => \DB::raw('NOW()'),
81
+				'updated_at' => \DB::raw('NOW()')
82
+				],
83
+				[
84
+				'name'       => 'block',
85
+				'model'      => 'users',
86
+				'created_at' => \DB::raw('NOW()'),
87
+				'updated_at' => \DB::raw('NOW()')
88
+				],
89
+				[
90
+				'name'       => 'unblock',
91
+				'model'      => 'users',
92
+				'created_at' => \DB::raw('NOW()'),
93
+				'updated_at' => \DB::raw('NOW()')
94
+				],
95
+				[
96
+				'name'       => 'group',
97
+				'model'      => 'users',
98
+				'created_at' => \DB::raw('NOW()'),
99
+				'updated_at' => \DB::raw('NOW()')
100
+				],
101
+				[
102
+				'name'       => 'deleted',
103
+				'model'      => 'users',
104
+				'created_at' => \DB::raw('NOW()'),
105
+				'updated_at' => \DB::raw('NOW()')
106
+				],
107
+				[
108
+				'name'       => 'restore',
109
+				'model'      => 'users',
110
+				'created_at' => \DB::raw('NOW()'),
111
+				'updated_at' => \DB::raw('NOW()')
112
+				],
113 113
 
114
-	        	/**
115
-        		 * Permissions model permissions.
116
-        		 */
117
-        		[
118
-	        	'name'       => 'find',
119
-	        	'model'      => 'permissions',
120
-	        	'created_at' => \DB::raw('NOW()'),
121
-	        	'updated_at' => \DB::raw('NOW()')
122
-	        	],
123
-	        	[
124
-	        	'name'       => 'search',
125
-	        	'model'      => 'permissions',
126
-	        	'created_at' => \DB::raw('NOW()'),
127
-	        	'updated_at' => \DB::raw('NOW()')
128
-	        	],
129
-	        	[
130
-	        	'name'       => 'list',
131
-	        	'model'      => 'permissions',
132
-	        	'created_at' => \DB::raw('NOW()'),
133
-	        	'updated_at' => \DB::raw('NOW()')
134
-	        	],
135
-	        	[
136
-	        	'name'       => 'findby',
137
-	        	'model'      => 'permissions',
138
-	        	'created_at' => \DB::raw('NOW()'),
139
-	        	'updated_at' => \DB::raw('NOW()')
140
-	        	],
141
-	        	[
142
-	        	'name'       => 'first',
143
-	        	'model'      => 'permissions',
144
-	        	'created_at' => \DB::raw('NOW()'),
145
-	        	'updated_at' => \DB::raw('NOW()')
146
-	        	],
147
-	        	[
148
-	        	'name'       => 'paginate',
149
-	        	'model'      => 'permissions',
150
-	        	'created_at' => \DB::raw('NOW()'),
151
-	        	'updated_at' => \DB::raw('NOW()')
152
-	        	],
153
-	        	[
154
-	        	'name'       => 'paginateby',
155
-	        	'model'      => 'permissions',
156
-	        	'created_at' => \DB::raw('NOW()'),
157
-	        	'updated_at' => \DB::raw('NOW()')
158
-	        	],
114
+				/**
115
+				 * Permissions model permissions.
116
+				 */
117
+				[
118
+				'name'       => 'find',
119
+				'model'      => 'permissions',
120
+				'created_at' => \DB::raw('NOW()'),
121
+				'updated_at' => \DB::raw('NOW()')
122
+				],
123
+				[
124
+				'name'       => 'search',
125
+				'model'      => 'permissions',
126
+				'created_at' => \DB::raw('NOW()'),
127
+				'updated_at' => \DB::raw('NOW()')
128
+				],
129
+				[
130
+				'name'       => 'list',
131
+				'model'      => 'permissions',
132
+				'created_at' => \DB::raw('NOW()'),
133
+				'updated_at' => \DB::raw('NOW()')
134
+				],
135
+				[
136
+				'name'       => 'findby',
137
+				'model'      => 'permissions',
138
+				'created_at' => \DB::raw('NOW()'),
139
+				'updated_at' => \DB::raw('NOW()')
140
+				],
141
+				[
142
+				'name'       => 'first',
143
+				'model'      => 'permissions',
144
+				'created_at' => \DB::raw('NOW()'),
145
+				'updated_at' => \DB::raw('NOW()')
146
+				],
147
+				[
148
+				'name'       => 'paginate',
149
+				'model'      => 'permissions',
150
+				'created_at' => \DB::raw('NOW()'),
151
+				'updated_at' => \DB::raw('NOW()')
152
+				],
153
+				[
154
+				'name'       => 'paginateby',
155
+				'model'      => 'permissions',
156
+				'created_at' => \DB::raw('NOW()'),
157
+				'updated_at' => \DB::raw('NOW()')
158
+				],
159 159
 
160
-	        	/**
161
-        		 * Groups model permissions.
162
-        		 */
163
-	        	[
164
-	        	'name'       => 'save',
165
-	        	'model'      => 'groups',
166
-	        	'created_at' => \DB::raw('NOW()'),
167
-	        	'updated_at' => \DB::raw('NOW()')
168
-	        	],
169
-	        	[
170
-	        	'name'       => 'delete',
171
-	        	'model'      => 'groups',
172
-	        	'created_at' => \DB::raw('NOW()'),
173
-	        	'updated_at' => \DB::raw('NOW()')
174
-	        	],
175
-	        	[
176
-	        	'name'       => 'find',
177
-	        	'model'      => 'groups',
178
-	        	'created_at' => \DB::raw('NOW()'),
179
-	        	'updated_at' => \DB::raw('NOW()')
180
-	        	],
181
-	        	[
182
-	        	'name'       => 'search',
183
-	        	'model'      => 'groups',
184
-	        	'created_at' => \DB::raw('NOW()'),
185
-	        	'updated_at' => \DB::raw('NOW()')
186
-	        	],
187
-	        	[
188
-	        	'name'       => 'list',
189
-	        	'model'      => 'groups',
190
-	        	'created_at' => \DB::raw('NOW()'),
191
-	        	'updated_at' => \DB::raw('NOW()')
192
-	        	],
193
-	        	[
194
-	        	'name'       => 'findby',
195
-	        	'model'      => 'groups',
196
-	        	'created_at' => \DB::raw('NOW()'),
197
-	        	'updated_at' => \DB::raw('NOW()')
198
-	        	],
199
-	        	[
200
-	        	'name'       => 'first',
201
-	        	'model'      => 'groups',
202
-	        	'created_at' => \DB::raw('NOW()'),
203
-	        	'updated_at' => \DB::raw('NOW()')
204
-	        	],
205
-	        	[
206
-	        	'name'       => 'paginate',
207
-	        	'model'      => 'groups',
208
-	        	'created_at' => \DB::raw('NOW()'),
209
-	        	'updated_at' => \DB::raw('NOW()')
210
-	        	],
211
-	        	[
212
-	        	'name'       => 'paginateby',
213
-	        	'model'      => 'groups',
214
-	        	'created_at' => \DB::raw('NOW()'),
215
-	        	'updated_at' => \DB::raw('NOW()')
216
-	        	],
217
-	        	[
218
-	        	'name'       => 'assignpermissions',
219
-	        	'model'      => 'groups',
220
-	        	'created_at' => \DB::raw('NOW()'),
221
-	        	'updated_at' => \DB::raw('NOW()')
222
-	        	],
223
-	        	[
224
-	        	'name'       => 'users',
225
-	        	'model'      => 'groups',
226
-	        	'created_at' => \DB::raw('NOW()'),
227
-	        	'updated_at' => \DB::raw('NOW()')
228
-	        	],
229
-                [
230
-                'name'       => 'deleted',
231
-                'model'      => 'groups',
232
-                'created_at' => \DB::raw('NOW()'),
233
-                'updated_at' => \DB::raw('NOW()')
234
-                ],
235
-                [
236
-                'name'       => 'restore',
237
-                'model'      => 'groups',
238
-                'created_at' => \DB::raw('NOW()'),
239
-                'updated_at' => \DB::raw('NOW()')
240
-                ],
241
-        	]
242
-        );
160
+				/**
161
+				 * Groups model permissions.
162
+				 */
163
+				[
164
+				'name'       => 'save',
165
+				'model'      => 'groups',
166
+				'created_at' => \DB::raw('NOW()'),
167
+				'updated_at' => \DB::raw('NOW()')
168
+				],
169
+				[
170
+				'name'       => 'delete',
171
+				'model'      => 'groups',
172
+				'created_at' => \DB::raw('NOW()'),
173
+				'updated_at' => \DB::raw('NOW()')
174
+				],
175
+				[
176
+				'name'       => 'find',
177
+				'model'      => 'groups',
178
+				'created_at' => \DB::raw('NOW()'),
179
+				'updated_at' => \DB::raw('NOW()')
180
+				],
181
+				[
182
+				'name'       => 'search',
183
+				'model'      => 'groups',
184
+				'created_at' => \DB::raw('NOW()'),
185
+				'updated_at' => \DB::raw('NOW()')
186
+				],
187
+				[
188
+				'name'       => 'list',
189
+				'model'      => 'groups',
190
+				'created_at' => \DB::raw('NOW()'),
191
+				'updated_at' => \DB::raw('NOW()')
192
+				],
193
+				[
194
+				'name'       => 'findby',
195
+				'model'      => 'groups',
196
+				'created_at' => \DB::raw('NOW()'),
197
+				'updated_at' => \DB::raw('NOW()')
198
+				],
199
+				[
200
+				'name'       => 'first',
201
+				'model'      => 'groups',
202
+				'created_at' => \DB::raw('NOW()'),
203
+				'updated_at' => \DB::raw('NOW()')
204
+				],
205
+				[
206
+				'name'       => 'paginate',
207
+				'model'      => 'groups',
208
+				'created_at' => \DB::raw('NOW()'),
209
+				'updated_at' => \DB::raw('NOW()')
210
+				],
211
+				[
212
+				'name'       => 'paginateby',
213
+				'model'      => 'groups',
214
+				'created_at' => \DB::raw('NOW()'),
215
+				'updated_at' => \DB::raw('NOW()')
216
+				],
217
+				[
218
+				'name'       => 'assignpermissions',
219
+				'model'      => 'groups',
220
+				'created_at' => \DB::raw('NOW()'),
221
+				'updated_at' => \DB::raw('NOW()')
222
+				],
223
+				[
224
+				'name'       => 'users',
225
+				'model'      => 'groups',
226
+				'created_at' => \DB::raw('NOW()'),
227
+				'updated_at' => \DB::raw('NOW()')
228
+				],
229
+				[
230
+				'name'       => 'deleted',
231
+				'model'      => 'groups',
232
+				'created_at' => \DB::raw('NOW()'),
233
+				'updated_at' => \DB::raw('NOW()')
234
+				],
235
+				[
236
+				'name'       => 'restore',
237
+				'model'      => 'groups',
238
+				'created_at' => \DB::raw('NOW()'),
239
+				'updated_at' => \DB::raw('NOW()')
240
+				],
241
+			]
242
+		);
243 243
 
244 244
 		/**
245 245
 		 * Create Default groups.
@@ -256,29 +256,29 @@  discard block
 block discarded – undo
256 256
 		 * Create Default users.
257 257
 		 */
258 258
 		$adminUserId = DB::table('users')->insertGetId(
259
-            [
259
+			[
260 260
 			'email'      => '[email protected]',
261 261
 			'password'   => bcrypt('123456'),
262 262
 			'created_at' => \DB::raw('NOW()'),
263 263
 			'updated_at' => \DB::raw('NOW()')
264 264
 			]
265
-        );
265
+		);
266 266
 
267 267
 		/**
268 268
 		 * Assign users to groups.
269 269
 		 */
270 270
 		DB::table('users_groups')->insert(
271
-        	[
272
-	            [
271
+			[
272
+				[
273 273
 				'user_id'    => $adminUserId,
274 274
 				'group_id'   => $adminGroupId,
275 275
 				'created_at' => \DB::raw('NOW()'),
276 276
 				'updated_at' => \DB::raw('NOW()')
277
-	            ]
278
-        	]
279
-        );
277
+				]
278
+			]
279
+		);
280 280
 
281
-        /**
281
+		/**
282 282
 		 * Assign the permissions to the admin group.
283 283
 		 */
284 284
 		$permissionIds = DB::table('permissions')->whereIn('model', ['users', 'permissions', 'groups'])->select('id')->pluck('id');
Please login to merge, or discard this patch.
src/Modules/V1/Acl/Http/Controllers/UsersController.php 2 patches
Indentation   +221 added lines, -221 removed lines patch added patch discarded remove patch
@@ -7,252 +7,252 @@
 block discarded – undo
7 7
 
8 8
 class UsersController extends BaseApiController
9 9
 {
10
-    /**
11
-     * The name of the model that is used by the base api controller 
12
-     * to preform actions like (add, edit ... etc).
13
-     * @var string
14
-     */
15
-    protected $model               = 'users';
10
+	/**
11
+	 * The name of the model that is used by the base api controller 
12
+	 * to preform actions like (add, edit ... etc).
13
+	 * @var string
14
+	 */
15
+	protected $model               = 'users';
16 16
 
17
-    /**
18
-     * List of all route actions that the base api controller
19
-     * will skip permissions check for them.
20
-     * @var array
21
-     */
22
-    protected $skipPermissionCheck = ['account', 'logout', 'sendreset'];
17
+	/**
18
+	 * List of all route actions that the base api controller
19
+	 * will skip permissions check for them.
20
+	 * @var array
21
+	 */
22
+	protected $skipPermissionCheck = ['account', 'logout', 'sendreset'];
23 23
 
24
-    /**
25
-     * List of all route actions that the base api controller
26
-     * will skip login check for them.
27
-     * @var array
28
-     */
29
-    protected $skipLoginCheck      = ['login', 'loginSocial', 'register', 'sendreset', 'resetpassword', 'refreshtoken'];
24
+	/**
25
+	 * List of all route actions that the base api controller
26
+	 * will skip login check for them.
27
+	 * @var array
28
+	 */
29
+	protected $skipLoginCheck      = ['login', 'loginSocial', 'register', 'sendreset', 'resetpassword', 'refreshtoken'];
30 30
 
31
-    /**
32
-     * The validations rules used by the base api controller
33
-     * to check before add.
34
-     * @var array
35
-     */
36
-    protected $validationRules     = [
37
-        'user_name'     => 'string|unique:users,user_name,{id}', 
38
-        'email'         => 'required|email|unique:users,email,{id}', 
39
-        'password'      => 'min:6'
40
-    ];
31
+	/**
32
+	 * The validations rules used by the base api controller
33
+	 * to check before add.
34
+	 * @var array
35
+	 */
36
+	protected $validationRules     = [
37
+		'user_name'     => 'string|unique:users,user_name,{id}', 
38
+		'email'         => 'required|email|unique:users,email,{id}', 
39
+		'password'      => 'min:6'
40
+	];
41 41
 
42
-    /**
43
-     * Return the logged in user account.
44
-     * 
45
-     * @return \Illuminate\Http\Response
46
-     */
47
-    public function account()
48
-    {
49
-        $relations = $this->relations && $this->relations['account'] ? $this->relations['account'] : [];
50
-        return \Response::json(\Core::users()->account($relations), 200);
51
-    }
42
+	/**
43
+	 * Return the logged in user account.
44
+	 * 
45
+	 * @return \Illuminate\Http\Response
46
+	 */
47
+	public function account()
48
+	{
49
+		$relations = $this->relations && $this->relations['account'] ? $this->relations['account'] : [];
50
+		return \Response::json(\Core::users()->account($relations), 200);
51
+	}
52 52
 
53
-    /**
54
-     * Block the user.
55
-     *
56
-     * @param  integer  $id
57
-     * @return \Illuminate\Http\Response
58
-     */
59
-    public function block($id)
60
-    {
61
-        return \Response::json(\Core::users()->block($id), 200);
62
-    }
53
+	/**
54
+	 * Block the user.
55
+	 *
56
+	 * @param  integer  $id
57
+	 * @return \Illuminate\Http\Response
58
+	 */
59
+	public function block($id)
60
+	{
61
+		return \Response::json(\Core::users()->block($id), 200);
62
+	}
63 63
 
64
-    /**
65
-     * Unblock the user.
66
-     *
67
-     * @param  integer  $id
68
-     * @return \Illuminate\Http\Response
69
-     */
70
-    public function unblock($id)
71
-    {
72
-        return \Response::json(\Core::users()->unblock($id), 200);
73
-    }
64
+	/**
65
+	 * Unblock the user.
66
+	 *
67
+	 * @param  integer  $id
68
+	 * @return \Illuminate\Http\Response
69
+	 */
70
+	public function unblock($id)
71
+	{
72
+		return \Response::json(\Core::users()->unblock($id), 200);
73
+	}
74 74
 
75
-    /**
76
-     * Logout the user.
77
-     * 
78
-     * @return \Illuminate\Http\Response
79
-     */
80
-    public function logout()
81
-    {
82
-        return \Response::json(\Core::users()->logout(), 200);
83
-    }
75
+	/**
76
+	 * Logout the user.
77
+	 * 
78
+	 * @return \Illuminate\Http\Response
79
+	 */
80
+	public function logout()
81
+	{
82
+		return \Response::json(\Core::users()->logout(), 200);
83
+	}
84 84
 
85
-    /**
86
-     * Handle a registration request.
87
-     *
88
-     * @param  \Illuminate\Http\Request  $request
89
-     * @return \Illuminate\Http\Response
90
-     */
91
-    public function register(Request $request)
92
-    {
93
-        $this->validate($request, [
94
-            'user_name'     => 'string|unique:users,user_name,{id}', 
95
-            'email'         => 'required|email|unique:users,email,{id}', 
96
-            'password'      => 'required|min:6'
97
-            ]);
85
+	/**
86
+	 * Handle a registration request.
87
+	 *
88
+	 * @param  \Illuminate\Http\Request  $request
89
+	 * @return \Illuminate\Http\Response
90
+	 */
91
+	public function register(Request $request)
92
+	{
93
+		$this->validate($request, [
94
+			'user_name'     => 'string|unique:users,user_name,{id}', 
95
+			'email'         => 'required|email|unique:users,email,{id}', 
96
+			'password'      => 'required|min:6'
97
+			]);
98 98
 
99
-        return \Response::json(\Core::users()->register($request->only('email', 'password')), 200);
100
-    }
99
+		return \Response::json(\Core::users()->register($request->only('email', 'password')), 200);
100
+	}
101 101
 
102
-    /**
103
-     * Handle a login request of the none admin to the application.
104
-     *
105
-     * @param  \Illuminate\Http\Request  $request
106
-     * @return \Illuminate\Http\Response
107
-     */
108
-    public function login(Request $request)
109
-    {
110
-        $this->validate($request, [
111
-            'email'    => 'required|email', 
112
-            'password' => 'required|min:6',
113
-            'admin'    => 'boolean'
114
-            ]);
102
+	/**
103
+	 * Handle a login request of the none admin to the application.
104
+	 *
105
+	 * @param  \Illuminate\Http\Request  $request
106
+	 * @return \Illuminate\Http\Response
107
+	 */
108
+	public function login(Request $request)
109
+	{
110
+		$this->validate($request, [
111
+			'email'    => 'required|email', 
112
+			'password' => 'required|min:6',
113
+			'admin'    => 'boolean'
114
+			]);
115 115
 
116
-        return \Response::json(\Core::users()->login($request->only('email', 'password'), $request->get('admin')), 200);
117
-    }
116
+		return \Response::json(\Core::users()->login($request->only('email', 'password'), $request->get('admin')), 200);
117
+	}
118 118
 
119
-    /**
120
-     * Handle a social login request of the none admin to the application.
121
-     *
122
-     * @param  \Illuminate\Http\Request  $request
123
-     * @return \Illuminate\Http\Response
124
-     */
125
-    public function loginSocial(Request $request)
126
-    {
127
-        $this->validate($request, [
128
-            'auth_code'    => 'required_without:access_token',
129
-            'access_token' => 'required_without:auth_code',
130
-            'type'         => 'required|in:facebook,google'
131
-            ]);
119
+	/**
120
+	 * Handle a social login request of the none admin to the application.
121
+	 *
122
+	 * @param  \Illuminate\Http\Request  $request
123
+	 * @return \Illuminate\Http\Response
124
+	 */
125
+	public function loginSocial(Request $request)
126
+	{
127
+		$this->validate($request, [
128
+			'auth_code'    => 'required_without:access_token',
129
+			'access_token' => 'required_without:auth_code',
130
+			'type'         => 'required|in:facebook,google'
131
+			]);
132 132
 
133
-        return \Response::json(\Core::users()->loginSocial($request->only('auth_code', 'access_token', 'type')), 200);
134
-    }
133
+		return \Response::json(\Core::users()->loginSocial($request->only('auth_code', 'access_token', 'type')), 200);
134
+	}
135 135
 
136
-    /**
137
-     * Handle an assign groups to user request.
138
-     *
139
-     * @param  \Illuminate\Http\Request  $request
140
-     * @return \Illuminate\Http\Response
141
-     */
142
-    public function assigngroups(Request $request)
143
-    {
144
-        $this->validate($request, [
145
-            'group_ids' => 'required|exists:groups,id', 
146
-            'user_id'   => 'required|exists:users,id'
147
-            ]);
136
+	/**
137
+	 * Handle an assign groups to user request.
138
+	 *
139
+	 * @param  \Illuminate\Http\Request  $request
140
+	 * @return \Illuminate\Http\Response
141
+	 */
142
+	public function assigngroups(Request $request)
143
+	{
144
+		$this->validate($request, [
145
+			'group_ids' => 'required|exists:groups,id', 
146
+			'user_id'   => 'required|exists:users,id'
147
+			]);
148 148
 
149
-        return \Response::json(\Core::users()->assignGroups($request->get('user_id'), $request->get('group_ids')), 200);
150
-    }
149
+		return \Response::json(\Core::users()->assignGroups($request->get('user_id'), $request->get('group_ids')), 200);
150
+	}
151 151
 
152
-    /**
153
-     * Send a reset link to the given user.
154
-     *
155
-     * @param  \Illuminate\Http\Request  $request
156
-     * @return \Illuminate\Http\Response
157
-     */
158
-    public function sendreset(Request $request)
159
-    {
160
-        $this->validate($request, ['email' => 'required|email', 'url' => 'required|url']);
152
+	/**
153
+	 * Send a reset link to the given user.
154
+	 *
155
+	 * @param  \Illuminate\Http\Request  $request
156
+	 * @return \Illuminate\Http\Response
157
+	 */
158
+	public function sendreset(Request $request)
159
+	{
160
+		$this->validate($request, ['email' => 'required|email', 'url' => 'required|url']);
161 161
 
162
-        return \Response::json(\Core::users()->sendReset($request->only('email'), $request->get('url')), 200);
163
-    }
162
+		return \Response::json(\Core::users()->sendReset($request->only('email'), $request->get('url')), 200);
163
+	}
164 164
 
165
-    /**
166
-     * Reset the given user's password.
167
-     *
168
-     * @param  \Illuminate\Http\Request  $request
169
-     * @return \Illuminate\Http\Response
170
-     */
171
-    public function resetpassword(Request $request)
172
-    {
173
-        $this->validate($request, [
174
-            'token'                 => 'required',
175
-            'email'                 => 'required|email',
176
-            'password'              => 'required|confirmed|min:6',
177
-            'password_confirmation' => 'required',
178
-        ]);
165
+	/**
166
+	 * Reset the given user's password.
167
+	 *
168
+	 * @param  \Illuminate\Http\Request  $request
169
+	 * @return \Illuminate\Http\Response
170
+	 */
171
+	public function resetpassword(Request $request)
172
+	{
173
+		$this->validate($request, [
174
+			'token'                 => 'required',
175
+			'email'                 => 'required|email',
176
+			'password'              => 'required|confirmed|min:6',
177
+			'password_confirmation' => 'required',
178
+		]);
179 179
 
180
-        return \Response::json(\Core::users()->resetPassword($request->only('email', 'password', 'password_confirmation', 'token')), 200);
181
-    }
180
+		return \Response::json(\Core::users()->resetPassword($request->only('email', 'password', 'password_confirmation', 'token')), 200);
181
+	}
182 182
 
183
-    /**
184
-     * Change the logged in user password.
185
-     *
186
-     * @param  \Illuminate\Http\Request  $request
187
-     * @return \Illuminate\Http\Response
188
-     */
189
-    public function changePassword(Request $request)
190
-    {
191
-        $this->validate($request, [
192
-            'old_password'          => 'required',
193
-            'password'              => 'required|confirmed|min:6',
194
-            'password_confirmation' => 'required',
195
-        ]);
183
+	/**
184
+	 * Change the logged in user password.
185
+	 *
186
+	 * @param  \Illuminate\Http\Request  $request
187
+	 * @return \Illuminate\Http\Response
188
+	 */
189
+	public function changePassword(Request $request)
190
+	{
191
+		$this->validate($request, [
192
+			'old_password'          => 'required',
193
+			'password'              => 'required|confirmed|min:6',
194
+			'password_confirmation' => 'required',
195
+		]);
196 196
 
197
-        return \Response::json(\Core::users()->changePassword($request->only('old_password', 'password', 'password_confirmation')), 200);
198
-    }
197
+		return \Response::json(\Core::users()->changePassword($request->only('old_password', 'password', 'password_confirmation')), 200);
198
+	}
199 199
 
200
-    /**
201
-     * Refresh the expired login token.
202
-     *
203
-     * @return \Illuminate\Http\Response
204
-     */
205
-    public function refreshtoken()
206
-    {
207
-        return \Response::json(\Core::users()->refreshtoken(), 200);
208
-    }
200
+	/**
201
+	 * Refresh the expired login token.
202
+	 *
203
+	 * @return \Illuminate\Http\Response
204
+	 */
205
+	public function refreshtoken()
206
+	{
207
+		return \Response::json(\Core::users()->refreshtoken(), 200);
208
+	}
209 209
 
210
-    /**
211
-     * Paginate all users with inthe given group.
212
-     * 
213
-     * @param  \Illuminate\Http\Request  $request
214
-     * @param  string $groupName
215
-     * @param  integer $perPage
216
-     * @param  string  $sortBy
217
-     * @param  boolean $desc
218
-     * @return \Illuminate\Http\Response
219
-     */
220
-    public function group(Request $request, $groupName, $perPage = false, $sortBy = 'created_at', $desc = 1)
221
-    {
222
-        $relations = $this->relations && $this->relations['group'] ? $this->relations['group'] : [];
223
-        return \Response::json(\Core::users()->group($request->all(), $groupName, $relations, $perPage, $sortBy, $desc), 200);
224
-    }
210
+	/**
211
+	 * Paginate all users with inthe given group.
212
+	 * 
213
+	 * @param  \Illuminate\Http\Request  $request
214
+	 * @param  string $groupName
215
+	 * @param  integer $perPage
216
+	 * @param  string  $sortBy
217
+	 * @param  boolean $desc
218
+	 * @return \Illuminate\Http\Response
219
+	 */
220
+	public function group(Request $request, $groupName, $perPage = false, $sortBy = 'created_at', $desc = 1)
221
+	{
222
+		$relations = $this->relations && $this->relations['group'] ? $this->relations['group'] : [];
223
+		return \Response::json(\Core::users()->group($request->all(), $groupName, $relations, $perPage, $sortBy, $desc), 200);
224
+	}
225 225
 
226
-    /**
227
-     * Save the given data to the logged in user.
228
-     *
229
-     * @param  \Illuminate\Http\Request  $request
230
-     * @return \Illuminate\Http\Response
231
-     */
232
-    public function saveProfile(Request $request) 
233
-    {
234
-        foreach ($this->validationRules as &$rule) 
235
-        {
236
-            if (strpos($rule, 'exists') && ! strpos($rule, 'deleted_at,NULL')) 
237
-            {
238
-                $rule .= ',deleted_at,NULL';
239
-            }
226
+	/**
227
+	 * Save the given data to the logged in user.
228
+	 *
229
+	 * @param  \Illuminate\Http\Request  $request
230
+	 * @return \Illuminate\Http\Response
231
+	 */
232
+	public function saveProfile(Request $request) 
233
+	{
234
+		foreach ($this->validationRules as &$rule) 
235
+		{
236
+			if (strpos($rule, 'exists') && ! strpos($rule, 'deleted_at,NULL')) 
237
+			{
238
+				$rule .= ',deleted_at,NULL';
239
+			}
240 240
 
241
-            if ($request->has('id')) 
242
-            {
243
-                $rule = str_replace('{id}', $request->get('id'), $rule);
244
-            }
245
-            else
246
-            {
247
-                $rule = str_replace(',{id}', '', $rule);
248
-            }
249
-        }
241
+			if ($request->has('id')) 
242
+			{
243
+				$rule = str_replace('{id}', $request->get('id'), $rule);
244
+			}
245
+			else
246
+			{
247
+				$rule = str_replace(',{id}', '', $rule);
248
+			}
249
+		}
250 250
 
251
-        $this->validate($request, $this->validationRules);
251
+		$this->validate($request, $this->validationRules);
252 252
 
253
-        if ($this->model)
254
-        {
255
-            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->saveProfile($request->all()), 200);
256
-        }
257
-    }
253
+		if ($this->model)
254
+		{
255
+			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->saveProfile($request->all()), 200);
256
+		}
257
+	}
258 258
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -12,7 +12,7 @@  discard block
 block discarded – undo
12 12
      * to preform actions like (add, edit ... etc).
13 13
      * @var string
14 14
      */
15
-    protected $model               = 'users';
15
+    protected $model = 'users';
16 16
 
17 17
     /**
18 18
      * List of all route actions that the base api controller
@@ -26,14 +26,14 @@  discard block
 block discarded – undo
26 26
      * will skip login check for them.
27 27
      * @var array
28 28
      */
29
-    protected $skipLoginCheck      = ['login', 'loginSocial', 'register', 'sendreset', 'resetpassword', 'refreshtoken'];
29
+    protected $skipLoginCheck = ['login', 'loginSocial', 'register', 'sendreset', 'resetpassword', 'refreshtoken'];
30 30
 
31 31
     /**
32 32
      * The validations rules used by the base api controller
33 33
      * to check before add.
34 34
      * @var array
35 35
      */
36
-    protected $validationRules     = [
36
+    protected $validationRules = [
37 37
         'user_name'     => 'string|unique:users,user_name,{id}', 
38 38
         'email'         => 'required|email|unique:users,email,{id}', 
39 39
         'password'      => 'min:6'
Please login to merge, or discard this patch.
src/Modules/V1/Core/AbstractRepositories/AbstractRepository.php 3 patches
Indentation   +642 added lines, -642 removed lines patch added patch discarded remove patch
@@ -4,656 +4,656 @@
 block discarded – undo
4 4
 
5 5
 abstract class AbstractRepository implements RepositoryInterface
6 6
 {
7
-    /**
8
-     * The model implementation.
9
-     * 
10
-     * @var model
11
-     */
12
-    public $model;
7
+	/**
8
+	 * The model implementation.
9
+	 * 
10
+	 * @var model
11
+	 */
12
+	public $model;
13 13
     
14
-    /**
15
-     * The config implementation.
16
-     * 
17
-     * @var config
18
-     */
19
-    protected $config;
14
+	/**
15
+	 * The config implementation.
16
+	 * 
17
+	 * @var config
18
+	 */
19
+	protected $config;
20 20
     
21
-    /**
22
-     * Create new AbstractRepository instance.
23
-     */
24
-    public function __construct()
25
-    {   
26
-        $this->config = \CoreConfig::getConfig();
27
-        $this->model  = \App::make($this->getModel());
28
-    }
29
-
30
-    /**
31
-     * Fetch all records with relations from the storage.
32
-     *
33
-     * @param  array   $relations
34
-     * @param  string  $sortBy
35
-     * @param  boolean $desc
36
-     * @param  array   $columns
37
-     * @return collection
38
-     */
39
-    public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*'))
40
-    {
41
-        $sort = $desc ? 'desc' : 'asc';
42
-        return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->get($columns);
43
-    }
44
-
45
-    /**
46
-     * Fetch all records with relations from storage in pages 
47
-     * that matche the given query.
48
-     * 
49
-     * @param  string  $query
50
-     * @param  integer $perPage
51
-     * @param  array   $relations
52
-     * @param  string  $sortBy
53
-     * @param  boolean $desc
54
-     * @param  array   $columns
55
-     * @return collection
56
-     */
57
-    public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*'))
58
-    {
59
-        $model            = call_user_func_array("{$this->getModel()}::with", array($relations));
60
-        $conditionColumns = $this->model->searchable;
61
-        $sort             = $desc ? 'desc' : 'asc';
62
-
63
-        /**
64
-         * Construct the select conditions for the model.
65
-         */
66
-        $model->where(function ($q) use ($query, $conditionColumns, $relations){
67
-
68
-            if (count($conditionColumns)) 
69
-            {
70
-                /**
71
-                 * Use the first element in the model columns to construct the first condition.
72
-                 */
73
-                $q->where(\DB::raw('LOWER(' . array_shift($conditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%');
74
-            }
75
-
76
-            /**
77
-             * Loop through the rest of the columns to construct or where conditions.
78
-             */
79
-            foreach ($conditionColumns as $column) 
80
-            {
81
-                $q->orWhere(\DB::raw('LOWER(' . $column . ')'), 'LIKE', '%' . strtolower($query) . '%');
82
-            }
83
-
84
-            /**
85
-             * Loop through the model relations.
86
-             */
87
-            foreach ($relations as $relation) 
88
-            {
89
-                /**
90
-                 * Remove the sub relation if exists.
91
-                 */
92
-                $relation = explode('.', $relation)[0];
93
-
94
-                /**
95
-                 * Try to fetch the relation repository from the core.
96
-                 */
97
-                if (\Core::$relation()) 
98
-                {
99
-                    /**
100
-                     * Construct the relation condition.
101
-                     */
102
-                    $q->orWhereHas($relation, function ($subModel) use ($query, $relation){
103
-
104
-                        $subModel->where(function ($q) use ($query, $relation){
105
-
106
-                            /**
107
-                             * Get columns of the relation.
108
-                             */
109
-                            $subConditionColumns = \Core::$relation()->model->searchable;
110
-
111
-                            if (count($subConditionColumns)) 
112
-                            {
113
-                                /**
114
-                                * Use the first element in the relation model columns to construct the first condition.
115
-                                 */
116
-                                $q->where(\DB::raw('LOWER(' . array_shift($subConditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%');
117
-                            }
118
-
119
-                            /**
120
-                             * Loop through the rest of the columns to construct or where conditions.
121
-                             */
122
-                            foreach ($subConditionColumns as $subConditionColumn)
123
-                            {
124
-                                $q->orWhere(\DB::raw('LOWER(' . $subConditionColumn . ')'), 'LIKE', '%' . strtolower($query) . '%');
125
-                            } 
126
-                        });
127
-
128
-                    });
129
-                }
130
-            }
131
-        });
21
+	/**
22
+	 * Create new AbstractRepository instance.
23
+	 */
24
+	public function __construct()
25
+	{   
26
+		$this->config = \CoreConfig::getConfig();
27
+		$this->model  = \App::make($this->getModel());
28
+	}
29
+
30
+	/**
31
+	 * Fetch all records with relations from the storage.
32
+	 *
33
+	 * @param  array   $relations
34
+	 * @param  string  $sortBy
35
+	 * @param  boolean $desc
36
+	 * @param  array   $columns
37
+	 * @return collection
38
+	 */
39
+	public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*'))
40
+	{
41
+		$sort = $desc ? 'desc' : 'asc';
42
+		return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->get($columns);
43
+	}
44
+
45
+	/**
46
+	 * Fetch all records with relations from storage in pages 
47
+	 * that matche the given query.
48
+	 * 
49
+	 * @param  string  $query
50
+	 * @param  integer $perPage
51
+	 * @param  array   $relations
52
+	 * @param  string  $sortBy
53
+	 * @param  boolean $desc
54
+	 * @param  array   $columns
55
+	 * @return collection
56
+	 */
57
+	public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*'))
58
+	{
59
+		$model            = call_user_func_array("{$this->getModel()}::with", array($relations));
60
+		$conditionColumns = $this->model->searchable;
61
+		$sort             = $desc ? 'desc' : 'asc';
62
+
63
+		/**
64
+		 * Construct the select conditions for the model.
65
+		 */
66
+		$model->where(function ($q) use ($query, $conditionColumns, $relations){
67
+
68
+			if (count($conditionColumns)) 
69
+			{
70
+				/**
71
+				 * Use the first element in the model columns to construct the first condition.
72
+				 */
73
+				$q->where(\DB::raw('LOWER(' . array_shift($conditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%');
74
+			}
75
+
76
+			/**
77
+			 * Loop through the rest of the columns to construct or where conditions.
78
+			 */
79
+			foreach ($conditionColumns as $column) 
80
+			{
81
+				$q->orWhere(\DB::raw('LOWER(' . $column . ')'), 'LIKE', '%' . strtolower($query) . '%');
82
+			}
83
+
84
+			/**
85
+			 * Loop through the model relations.
86
+			 */
87
+			foreach ($relations as $relation) 
88
+			{
89
+				/**
90
+				 * Remove the sub relation if exists.
91
+				 */
92
+				$relation = explode('.', $relation)[0];
93
+
94
+				/**
95
+				 * Try to fetch the relation repository from the core.
96
+				 */
97
+				if (\Core::$relation()) 
98
+				{
99
+					/**
100
+					 * Construct the relation condition.
101
+					 */
102
+					$q->orWhereHas($relation, function ($subModel) use ($query, $relation){
103
+
104
+						$subModel->where(function ($q) use ($query, $relation){
105
+
106
+							/**
107
+							 * Get columns of the relation.
108
+							 */
109
+							$subConditionColumns = \Core::$relation()->model->searchable;
110
+
111
+							if (count($subConditionColumns)) 
112
+							{
113
+								/**
114
+								 * Use the first element in the relation model columns to construct the first condition.
115
+								 */
116
+								$q->where(\DB::raw('LOWER(' . array_shift($subConditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%');
117
+							}
118
+
119
+							/**
120
+							 * Loop through the rest of the columns to construct or where conditions.
121
+							 */
122
+							foreach ($subConditionColumns as $subConditionColumn)
123
+							{
124
+								$q->orWhere(\DB::raw('LOWER(' . $subConditionColumn . ')'), 'LIKE', '%' . strtolower($query) . '%');
125
+							} 
126
+						});
127
+
128
+					});
129
+				}
130
+			}
131
+		});
132 132
         
133
-        return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);
134
-    }
133
+		return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);
134
+	}
135 135
     
136
-    /**
137
-     * Fetch all records with relations from storage in pages.
138
-     * 
139
-     * @param  integer $perPage
140
-     * @param  array   $relations
141
-     * @param  string  $sortBy
142
-     * @param  boolean $desc
143
-     * @param  array   $columns
144
-     * @return collection
145
-     */
146
-    public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*'))
147
-    {
148
-        $sort = $desc ? 'desc' : 'asc';
149
-        return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns);
150
-    }
151
-
152
-    /**
153
-     * Fetch all records with relations based on
154
-     * the given condition from storage in pages.
155
-     * 
156
-     * @param  array   $conditions array of conditions
157
-     * @param  integer $perPage
158
-     * @param  array   $relations
159
-     * @param  string  $sortBy
160
-     * @param  boolean $desc
161
-     * @param  array   $columns
162
-     * @return collection
163
-     */
164
-    public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*'))
165
-    {
166
-        unset($conditions['page']);
167
-        $conditions = $this->constructConditions($conditions, $this->model);
168
-        $sort       = $desc ? 'desc' : 'asc';
169
-        return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns);
170
-    }
136
+	/**
137
+	 * Fetch all records with relations from storage in pages.
138
+	 * 
139
+	 * @param  integer $perPage
140
+	 * @param  array   $relations
141
+	 * @param  string  $sortBy
142
+	 * @param  boolean $desc
143
+	 * @param  array   $columns
144
+	 * @return collection
145
+	 */
146
+	public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*'))
147
+	{
148
+		$sort = $desc ? 'desc' : 'asc';
149
+		return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns);
150
+	}
151
+
152
+	/**
153
+	 * Fetch all records with relations based on
154
+	 * the given condition from storage in pages.
155
+	 * 
156
+	 * @param  array   $conditions array of conditions
157
+	 * @param  integer $perPage
158
+	 * @param  array   $relations
159
+	 * @param  string  $sortBy
160
+	 * @param  boolean $desc
161
+	 * @param  array   $columns
162
+	 * @return collection
163
+	 */
164
+	public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*'))
165
+	{
166
+		unset($conditions['page']);
167
+		$conditions = $this->constructConditions($conditions, $this->model);
168
+		$sort       = $desc ? 'desc' : 'asc';
169
+		return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns);
170
+	}
171 171
     
172
-    /**
173
-     * Save the given model to the storage.
174
-     * 
175
-     * @param  array   $data
176
-     * @param  boolean $saveLog
177
-     * @return void
178
-     */
179
-    public function save(array $data, $saveLog = true)
180
-    {
181
-        $model      = false;
182
-        $modelClass = $this->model;
183
-        $relations  = [];
184
-
185
-        \DB::transaction(function () use (&$model, &$relations, $data, $saveLog, $modelClass) {
186
-            /**
187
-             * If the id is present in the data then select the model for updating,
188
-             * else create new model.
189
-             * @var array
190
-             */
191
-            $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass;
192
-            if ( ! $model) 
193
-            {
194
-                \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']);
195
-            }
196
-
197
-            /**
198
-             * Construct the model object with the given data,
199
-             * and if there is a relation add it to relations array,
200
-             * then save the model.
201
-             */
202
-            foreach ($data as $key => $value) 
203
-            {
204
-                /**
205
-                 * If the attribute is a relation.
206
-                 */
207
-                $relation = camel_case($key);
208
-                if (method_exists($model, $relation) && \Core::$relation())
209
-                {
210
-                    /**
211
-                     * Check if the relation is a collection.
212
-                     */
213
-                    if (class_basename($model->$relation) == 'Collection') 
214
-                    {   
215
-                        /**
216
-                         * If the relation has no value then marke the relation data 
217
-                         * related to the model to be deleted.
218
-                         */
219
-                        if ( ! $value || ! count($value)) 
220
-                        {
221
-                            $relations[$relation] = 'delete';
222
-                        }   
223
-                    }
224
-                    if (is_array($value)) 
225
-                    {
226
-                        /**
227
-                         * Loop through the relation data.
228
-                         */
229
-                        foreach ($value as $attr => $val) 
230
-                        {
231
-                            /**
232
-                             * Get the relation model.
233
-                             */
234
-                            $relationBaseModel = \Core::$relation()->model;
235
-
236
-                            /**
237
-                             * Check if the relation is a collection.
238
-                             */
239
-                            if (class_basename($model->$relation) == 'Collection')
240
-                            {
241
-                                /**
242
-                                 * If the id is present in the data then select the relation model for updating,
243
-                                 * else create new model.
244
-                                 */
245
-                                $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel;
246
-
247
-                                /**
248
-                                 * If model doesn't exists.
249
-                                 */
250
-                                if ( ! $relationModel) 
251
-                                {
252
-                                    \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']);
253
-                                }
254
-
255
-                                /**
256
-                                 * Loop through the relation attributes.
257
-                                 */
258
-                                foreach ($val as $attr => $val) 
259
-                                {
260
-                                    /**
261
-                                     * Prevent the sub relations or attributes not in the fillable.
262
-                                     */
263
-                                    if (gettype($val) !== 'object' && gettype($val) !== 'array' &&  array_search($attr, $relationModel->getFillable(), true) !== false)
264
-                                    {
265
-                                        $relationModel->$attr = $val;
266
-                                    }
267
-                                }
268
-
269
-                                $relations[$relation][] = $relationModel;
270
-                            }
271
-                            /**
272
-                             * If not collection.
273
-                             */
274
-                            else
275
-                            {
276
-                                /**
277
-                                 * Prevent the sub relations.
278
-                                 */
279
-                                if (gettype($val) !== 'object' && gettype($val) !== 'array') 
280
-                                {
281
-
282
-                                    /**
283
-                                     * If the id is present in the data then select the relation model for updating,
284
-                                     * else create new model.
285
-                                     */
286
-                                    $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel;
287
-
288
-                                    /**
289
-                                     * If model doesn't exists.
290
-                                     */
291
-                                    if ( ! $relationModel) 
292
-                                    {
293
-                                        \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']);
294
-                                    }
295
-
296
-                                    foreach ($value as $relationAttribute => $relationValue) 
297
-                                    {
298
-                                        /**
299
-                                         * Prevent attributes not in the fillable.
300
-                                         */
301
-                                        if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) 
302
-                                        {
303
-                                            $relationModel->$relationAttribute = $relationValue;
304
-                                        }
305
-                                    }
306
-
307
-                                    $relations[$relation] = $relationModel;
308
-                                }
309
-                            }
310
-                        }
311
-                    }
312
-                }
313
-                /**
314
-                 * If the attribute isn't a relation and prevent attributes not in the fillable.
315
-                 */
316
-                else if (array_search($key, $model->getFillable(), true) !== false)
317
-                {
318
-                    $model->$key = $value;   
319
-                }
320
-            }
321
-            /**
322
-             * Save the model.
323
-             */
324
-            $model->save();
325
-
326
-            /**
327
-             * Loop through the relations array.
328
-             */
329
-            foreach ($relations as $key => $value) 
330
-            {
331
-                /**
332
-                 * If the relation is marked for delete then delete it.
333
-                 */
334
-                if ($value == 'delete' && $model->$key()->count())
335
-                {
336
-                    $model->$key()->delete();
337
-                }
338
-                /**
339
-                 * If the relation is an array.
340
-                 */
341
-                else if (gettype($value) == 'array') 
342
-                {
343
-                    $ids = [];
344
-                    /**
345
-                     * Loop through the relations.
346
-                     */
347
-                    foreach ($value as $val) 
348
-                    {
349
-                        switch (class_basename($model->$key())) 
350
-                        {
351
-                            /**
352
-                             * If the relation is one to many then update it's foreign key with
353
-                             * the model id and save it then add its id to ids array to delete all 
354
-                             * relations who's id isn't in the ids array.
355
-                             */
356
-                            case 'HasMany':
357
-                                $foreignKeyName       = $model->$key()->getForeignKeyName();
358
-                                $val->$foreignKeyName = $model->id;
359
-                                $val->save();
360
-                                $ids[] = $val->id;
361
-                                break;
362
-
363
-                            /**
364
-                             * If the relation is many to many then add it's id to the ids array to
365
-                             * attache these ids to the model.
366
-                             */
367
-                            case 'BelongsToMany':
368
-                                $val->save();
369
-                                $ids[] = $val->id;
370
-                                break;
371
-                        }
372
-                    }
373
-                    switch (class_basename($model->$key())) 
374
-                    {
375
-                        /**
376
-                         * If the relation is one to many then delete all 
377
-                         * relations who's id isn't in the ids array.
378
-                         */
379
-                        case 'HasMany':
380
-                            $model->$key()->whereNotIn('id', $ids)->delete();
381
-                            break;
382
-
383
-                        /**
384
-                         * If the relation is many to many then 
385
-                         * detach the previous data and attach 
386
-                         * the ids array to the model.
387
-                         */
388
-                        case 'BelongsToMany':
389
-                            $model->$key()->detach();
390
-                            $model->$key()->attach($ids);
391
-                            break;
392
-                    }
393
-                }
394
-                /**
395
-                 * If the relation isn't array.
396
-                 */
397
-                else
398
-                {
399
-                    switch (class_basename($model->$key())) 
400
-                    {
401
-                        /**
402
-                         * If the relation is one to many or one to one.
403
-                         */
404
-                        case 'HasOne':
405
-                            $foreignKeyName         = $model->$key()->getForeignKeyName();
406
-                            $value->$foreignKeyName = $model->id;
407
-                            $value->save();
408
-                            break;
409
-                    }
410
-                }
411
-            }
412
-
413
-            $saveLog ? \Logging::saveLog(array_key_exists('id', $data) ? 'update' : 'create', class_basename($modelClass), $this->getModel(), $model->id, $model) : false;
414
-        });
415
-    }
172
+	/**
173
+	 * Save the given model to the storage.
174
+	 * 
175
+	 * @param  array   $data
176
+	 * @param  boolean $saveLog
177
+	 * @return void
178
+	 */
179
+	public function save(array $data, $saveLog = true)
180
+	{
181
+		$model      = false;
182
+		$modelClass = $this->model;
183
+		$relations  = [];
184
+
185
+		\DB::transaction(function () use (&$model, &$relations, $data, $saveLog, $modelClass) {
186
+			/**
187
+			 * If the id is present in the data then select the model for updating,
188
+			 * else create new model.
189
+			 * @var array
190
+			 */
191
+			$model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass;
192
+			if ( ! $model) 
193
+			{
194
+				\ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']);
195
+			}
196
+
197
+			/**
198
+			 * Construct the model object with the given data,
199
+			 * and if there is a relation add it to relations array,
200
+			 * then save the model.
201
+			 */
202
+			foreach ($data as $key => $value) 
203
+			{
204
+				/**
205
+				 * If the attribute is a relation.
206
+				 */
207
+				$relation = camel_case($key);
208
+				if (method_exists($model, $relation) && \Core::$relation())
209
+				{
210
+					/**
211
+					 * Check if the relation is a collection.
212
+					 */
213
+					if (class_basename($model->$relation) == 'Collection') 
214
+					{   
215
+						/**
216
+						 * If the relation has no value then marke the relation data 
217
+						 * related to the model to be deleted.
218
+						 */
219
+						if ( ! $value || ! count($value)) 
220
+						{
221
+							$relations[$relation] = 'delete';
222
+						}   
223
+					}
224
+					if (is_array($value)) 
225
+					{
226
+						/**
227
+						 * Loop through the relation data.
228
+						 */
229
+						foreach ($value as $attr => $val) 
230
+						{
231
+							/**
232
+							 * Get the relation model.
233
+							 */
234
+							$relationBaseModel = \Core::$relation()->model;
235
+
236
+							/**
237
+							 * Check if the relation is a collection.
238
+							 */
239
+							if (class_basename($model->$relation) == 'Collection')
240
+							{
241
+								/**
242
+								 * If the id is present in the data then select the relation model for updating,
243
+								 * else create new model.
244
+								 */
245
+								$relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel;
246
+
247
+								/**
248
+								 * If model doesn't exists.
249
+								 */
250
+								if ( ! $relationModel) 
251
+								{
252
+									\ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']);
253
+								}
254
+
255
+								/**
256
+								 * Loop through the relation attributes.
257
+								 */
258
+								foreach ($val as $attr => $val) 
259
+								{
260
+									/**
261
+									 * Prevent the sub relations or attributes not in the fillable.
262
+									 */
263
+									if (gettype($val) !== 'object' && gettype($val) !== 'array' &&  array_search($attr, $relationModel->getFillable(), true) !== false)
264
+									{
265
+										$relationModel->$attr = $val;
266
+									}
267
+								}
268
+
269
+								$relations[$relation][] = $relationModel;
270
+							}
271
+							/**
272
+							 * If not collection.
273
+							 */
274
+							else
275
+							{
276
+								/**
277
+								 * Prevent the sub relations.
278
+								 */
279
+								if (gettype($val) !== 'object' && gettype($val) !== 'array') 
280
+								{
281
+
282
+									/**
283
+									 * If the id is present in the data then select the relation model for updating,
284
+									 * else create new model.
285
+									 */
286
+									$relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel;
287
+
288
+									/**
289
+									 * If model doesn't exists.
290
+									 */
291
+									if ( ! $relationModel) 
292
+									{
293
+										\ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']);
294
+									}
295
+
296
+									foreach ($value as $relationAttribute => $relationValue) 
297
+									{
298
+										/**
299
+										 * Prevent attributes not in the fillable.
300
+										 */
301
+										if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) 
302
+										{
303
+											$relationModel->$relationAttribute = $relationValue;
304
+										}
305
+									}
306
+
307
+									$relations[$relation] = $relationModel;
308
+								}
309
+							}
310
+						}
311
+					}
312
+				}
313
+				/**
314
+				 * If the attribute isn't a relation and prevent attributes not in the fillable.
315
+				 */
316
+				else if (array_search($key, $model->getFillable(), true) !== false)
317
+				{
318
+					$model->$key = $value;   
319
+				}
320
+			}
321
+			/**
322
+			 * Save the model.
323
+			 */
324
+			$model->save();
325
+
326
+			/**
327
+			 * Loop through the relations array.
328
+			 */
329
+			foreach ($relations as $key => $value) 
330
+			{
331
+				/**
332
+				 * If the relation is marked for delete then delete it.
333
+				 */
334
+				if ($value == 'delete' && $model->$key()->count())
335
+				{
336
+					$model->$key()->delete();
337
+				}
338
+				/**
339
+				 * If the relation is an array.
340
+				 */
341
+				else if (gettype($value) == 'array') 
342
+				{
343
+					$ids = [];
344
+					/**
345
+					 * Loop through the relations.
346
+					 */
347
+					foreach ($value as $val) 
348
+					{
349
+						switch (class_basename($model->$key())) 
350
+						{
351
+							/**
352
+							 * If the relation is one to many then update it's foreign key with
353
+							 * the model id and save it then add its id to ids array to delete all 
354
+							 * relations who's id isn't in the ids array.
355
+							 */
356
+							case 'HasMany':
357
+								$foreignKeyName       = $model->$key()->getForeignKeyName();
358
+								$val->$foreignKeyName = $model->id;
359
+								$val->save();
360
+								$ids[] = $val->id;
361
+								break;
362
+
363
+							/**
364
+							 * If the relation is many to many then add it's id to the ids array to
365
+							 * attache these ids to the model.
366
+							 */
367
+							case 'BelongsToMany':
368
+								$val->save();
369
+								$ids[] = $val->id;
370
+								break;
371
+						}
372
+					}
373
+					switch (class_basename($model->$key())) 
374
+					{
375
+						/**
376
+						 * If the relation is one to many then delete all 
377
+						 * relations who's id isn't in the ids array.
378
+						 */
379
+						case 'HasMany':
380
+							$model->$key()->whereNotIn('id', $ids)->delete();
381
+							break;
382
+
383
+						/**
384
+						 * If the relation is many to many then 
385
+						 * detach the previous data and attach 
386
+						 * the ids array to the model.
387
+						 */
388
+						case 'BelongsToMany':
389
+							$model->$key()->detach();
390
+							$model->$key()->attach($ids);
391
+							break;
392
+					}
393
+				}
394
+				/**
395
+				 * If the relation isn't array.
396
+				 */
397
+				else
398
+				{
399
+					switch (class_basename($model->$key())) 
400
+					{
401
+						/**
402
+						 * If the relation is one to many or one to one.
403
+						 */
404
+						case 'HasOne':
405
+							$foreignKeyName         = $model->$key()->getForeignKeyName();
406
+							$value->$foreignKeyName = $model->id;
407
+							$value->save();
408
+							break;
409
+					}
410
+				}
411
+			}
412
+
413
+			$saveLog ? \Logging::saveLog(array_key_exists('id', $data) ? 'update' : 'create', class_basename($modelClass), $this->getModel(), $model->id, $model) : false;
414
+		});
415
+	}
416 416
     
417
-    /**
418
-     * Update record in the storage based on the given
419
-     * condition.
420
-     * 
421
-     * @param  [type] $value condition value
422
-     * @param  array $data
423
-     * @param  string $attribute condition column name
424
-     * @return void
425
-     */
426
-    public function update($value, array $data, $attribute = 'id', $saveLog = true)
427
-    {
428
-        if ($attribute == 'id') 
429
-        {
430
-            $model = $this->model->lockForUpdate()->find($value);
431
-            $model ? $model->update($data) : 0;
432
-            $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $value, $model) : false;
433
-        }
434
-        else
435
-        {
436
-            call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){
437
-                $model->update($data);
438
-                $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $model->id, $model) : false;
439
-            });
440
-        }
441
-    }
442
-
443
-    /**
444
-     * Delete record from the storage based on the given
445
-     * condition.
446
-     * 
447
-     * @param  var $value condition value
448
-     * @param  string $attribute condition column name
449
-     * @return void
450
-     */
451
-    public function delete($value, $attribute = 'id', $saveLog = true)
452
-    {
453
-        if ($attribute == 'id') 
454
-        {
455
-            \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) {
456
-                $model = $this->model->lockForUpdate()->find($value);
457
-                if ( ! $model) 
458
-                {
459
-                    \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value);
460
-                }
417
+	/**
418
+	 * Update record in the storage based on the given
419
+	 * condition.
420
+	 * 
421
+	 * @param  [type] $value condition value
422
+	 * @param  array $data
423
+	 * @param  string $attribute condition column name
424
+	 * @return void
425
+	 */
426
+	public function update($value, array $data, $attribute = 'id', $saveLog = true)
427
+	{
428
+		if ($attribute == 'id') 
429
+		{
430
+			$model = $this->model->lockForUpdate()->find($value);
431
+			$model ? $model->update($data) : 0;
432
+			$saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $value, $model) : false;
433
+		}
434
+		else
435
+		{
436
+			call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){
437
+				$model->update($data);
438
+				$saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $model->id, $model) : false;
439
+			});
440
+		}
441
+	}
442
+
443
+	/**
444
+	 * Delete record from the storage based on the given
445
+	 * condition.
446
+	 * 
447
+	 * @param  var $value condition value
448
+	 * @param  string $attribute condition column name
449
+	 * @return void
450
+	 */
451
+	public function delete($value, $attribute = 'id', $saveLog = true)
452
+	{
453
+		if ($attribute == 'id') 
454
+		{
455
+			\DB::transaction(function () use ($value, $attribute, &$result, $saveLog) {
456
+				$model = $this->model->lockForUpdate()->find($value);
457
+				if ( ! $model) 
458
+				{
459
+					\ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value);
460
+				}
461 461
                 
462
-                $model->delete();
463
-                $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false;
464
-            });
465
-        }
466
-        else
467
-        {
468
-            \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) {
469
-                call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){
470
-                    $model->delete();
471
-                    $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false;
472
-                });
473
-            });   
474
-        }
475
-    }
462
+				$model->delete();
463
+				$saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false;
464
+			});
465
+		}
466
+		else
467
+		{
468
+			\DB::transaction(function () use ($value, $attribute, &$result, $saveLog) {
469
+				call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){
470
+					$model->delete();
471
+					$saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false;
472
+				});
473
+			});   
474
+		}
475
+	}
476 476
     
477
-    /**
478
-     * Fetch records from the storage based on the given
479
-     * id.
480
-     * 
481
-     * @param  integer $id
482
-     * @param  array   $relations
483
-     * @param  array   $columns
484
-     * @return object
485
-     */
486
-    public function find($id, $relations = [], $columns = array('*'))
487
-    {
488
-        return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns);
489
-    }
477
+	/**
478
+	 * Fetch records from the storage based on the given
479
+	 * id.
480
+	 * 
481
+	 * @param  integer $id
482
+	 * @param  array   $relations
483
+	 * @param  array   $columns
484
+	 * @return object
485
+	 */
486
+	public function find($id, $relations = [], $columns = array('*'))
487
+	{
488
+		return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns);
489
+	}
490 490
     
491
-    /**
492
-     * Fetch records from the storage based on the given
493
-     * condition.
494
-     * 
495
-     * @param  array   $conditions array of conditions
496
-     * @param  array   $relations
497
-     * @param  string  $sortBy
498
-     * @param  boolean $desc
499
-     * @param  array   $columns
500
-     * @return collection
501
-     */
502
-    public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*'))
503
-    {
504
-        $conditions = $this->constructConditions($conditions, $this->model);
505
-        $sort       = $desc ? 'desc' : 'asc';
506
-        return call_user_func_array("{$this->getModel()}::with",  array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns);
507
-    }
508
-
509
-    /**
510
-     * Fetch the first record from the storage based on the given
511
-     * condition.
512
-     *
513
-     * @param  array   $conditions array of conditions
514
-     * @param  array   $relations
515
-     * @param  array   $columns
516
-     * @return object
517
-     */
518
-    public function first($conditions, $relations = [], $columns = array('*'))
519
-    {
520
-        $conditions = $this->constructConditions($conditions, $this->model);
521
-        return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns);  
522
-    }
523
-
524
-    /**
525
-     * Return the deleted models in pages based on the given conditions.
526
-     * 
527
-     * @param  array   $conditions array of conditions
528
-     * @param  integer $perPage
529
-     * @param  string  $sortBy
530
-     * @param  boolean $desc
531
-     * @param  array   $columns
532
-     * @return collection
533
-     */
534
-    public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = array('*'))
535
-    {
536
-        unset($conditions['page']);
537
-        $conditions = $this->constructConditions($conditions, $this->model);
538
-        $sort       = $desc ? 'desc' : 'asc';
539
-        $model      = $this->model->onlyTrashed();
540
-
541
-        if (count($conditions['conditionValues']))
542
-        {
543
-            $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']);
544
-        }
545
-
546
-        return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);;
547
-    }
548
-
549
-    /**
550
-     * Restore the deleted model.
551
-     * 
552
-     * @param  integer $id
553
-     * @param  string  $attribute condition column name
554
-     * @return void
555
-     */
556
-    public function restore($id)
557
-    {
558
-        $model = $this->model->onlyTrashed()->find($id);
559
-
560
-        if ( ! $model) 
561
-        {
562
-            \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id);
563
-        }
564
-
565
-        $model->restore();
566
-    }
567
-
568
-    /**
569
-     * Build the conditions recursively for the retrieving methods.
570
-     * @param  array $conditions
571
-     * @return array
572
-     */
573
-    protected function constructConditions($conditions, $model)
574
-    {   
575
-        $conditionString = '';
576
-        $conditionValues = [];
577
-        foreach ($conditions as $key => $value) 
578
-        {
579
-            if ($key == 'and') 
580
-            {
581
-                $conditions       = $this->constructConditions($value, $model);
582
-                $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} ';
583
-                $conditionValues  = array_merge($conditionValues, $conditions['conditionValues']);
584
-            }
585
-            else if ($key == 'or')
586
-            {
587
-                $conditions       = $this->constructConditions($value, $model);
588
-                $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} ';
589
-                $conditionValues  = array_merge($conditionValues, $conditions['conditionValues']);
590
-            }
591
-            else
592
-            {
593
-                if (is_array($value)) 
594
-                {
595
-                    $operator = $value['op'];
596
-                    if (strtolower($operator) == 'between') 
597
-                    {
598
-                        $value1 = $value['val1'];
599
-                        $value2 = $value['val2'];
600
-                    }
601
-                    else
602
-                    {
603
-                        $value = array_key_exists('val', $value) ? $value['val'] : '';
604
-                    }
605
-                }
606
-                else
607
-                {
608
-                    $operator = '=';
609
-                }
491
+	/**
492
+	 * Fetch records from the storage based on the given
493
+	 * condition.
494
+	 * 
495
+	 * @param  array   $conditions array of conditions
496
+	 * @param  array   $relations
497
+	 * @param  string  $sortBy
498
+	 * @param  boolean $desc
499
+	 * @param  array   $columns
500
+	 * @return collection
501
+	 */
502
+	public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*'))
503
+	{
504
+		$conditions = $this->constructConditions($conditions, $this->model);
505
+		$sort       = $desc ? 'desc' : 'asc';
506
+		return call_user_func_array("{$this->getModel()}::with",  array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns);
507
+	}
508
+
509
+	/**
510
+	 * Fetch the first record from the storage based on the given
511
+	 * condition.
512
+	 *
513
+	 * @param  array   $conditions array of conditions
514
+	 * @param  array   $relations
515
+	 * @param  array   $columns
516
+	 * @return object
517
+	 */
518
+	public function first($conditions, $relations = [], $columns = array('*'))
519
+	{
520
+		$conditions = $this->constructConditions($conditions, $this->model);
521
+		return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns);  
522
+	}
523
+
524
+	/**
525
+	 * Return the deleted models in pages based on the given conditions.
526
+	 * 
527
+	 * @param  array   $conditions array of conditions
528
+	 * @param  integer $perPage
529
+	 * @param  string  $sortBy
530
+	 * @param  boolean $desc
531
+	 * @param  array   $columns
532
+	 * @return collection
533
+	 */
534
+	public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = array('*'))
535
+	{
536
+		unset($conditions['page']);
537
+		$conditions = $this->constructConditions($conditions, $this->model);
538
+		$sort       = $desc ? 'desc' : 'asc';
539
+		$model      = $this->model->onlyTrashed();
540
+
541
+		if (count($conditions['conditionValues']))
542
+		{
543
+			$model->whereRaw($conditions['conditionString'], $conditions['conditionValues']);
544
+		}
545
+
546
+		return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);;
547
+	}
548
+
549
+	/**
550
+	 * Restore the deleted model.
551
+	 * 
552
+	 * @param  integer $id
553
+	 * @param  string  $attribute condition column name
554
+	 * @return void
555
+	 */
556
+	public function restore($id)
557
+	{
558
+		$model = $this->model->onlyTrashed()->find($id);
559
+
560
+		if ( ! $model) 
561
+		{
562
+			\ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id);
563
+		}
564
+
565
+		$model->restore();
566
+	}
567
+
568
+	/**
569
+	 * Build the conditions recursively for the retrieving methods.
570
+	 * @param  array $conditions
571
+	 * @return array
572
+	 */
573
+	protected function constructConditions($conditions, $model)
574
+	{   
575
+		$conditionString = '';
576
+		$conditionValues = [];
577
+		foreach ($conditions as $key => $value) 
578
+		{
579
+			if ($key == 'and') 
580
+			{
581
+				$conditions       = $this->constructConditions($value, $model);
582
+				$conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} ';
583
+				$conditionValues  = array_merge($conditionValues, $conditions['conditionValues']);
584
+			}
585
+			else if ($key == 'or')
586
+			{
587
+				$conditions       = $this->constructConditions($value, $model);
588
+				$conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} ';
589
+				$conditionValues  = array_merge($conditionValues, $conditions['conditionValues']);
590
+			}
591
+			else
592
+			{
593
+				if (is_array($value)) 
594
+				{
595
+					$operator = $value['op'];
596
+					if (strtolower($operator) == 'between') 
597
+					{
598
+						$value1 = $value['val1'];
599
+						$value2 = $value['val2'];
600
+					}
601
+					else
602
+					{
603
+						$value = array_key_exists('val', $value) ? $value['val'] : '';
604
+					}
605
+				}
606
+				else
607
+				{
608
+					$operator = '=';
609
+				}
610 610
                 
611
-                if (strtolower($operator) == 'between') 
612
-                {
613
-                    $conditionString  .= $key . ' >= ? and ';
614
-                    $conditionValues[] = $value1;
615
-
616
-                    $conditionString  .= $key . ' <= ? {op} ';
617
-                    $conditionValues[] = $value2;
618
-                }
619
-                elseif (strtolower($operator) == 'in') 
620
-                {
621
-                    $conditionValues  = array_merge($conditionValues, $value);
622
-                    $inBindingsString = rtrim(str_repeat('?,', count($value)), ',');
623
-                    $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} ';
624
-                }
625
-                elseif (strtolower($operator) == 'null') 
626
-                {
627
-                    $conditionString .= $key . ' is null {op} ';
628
-                }
629
-                elseif (strtolower($operator) == 'not null') 
630
-                {
631
-                    $conditionString .= $key . ' is not null {op} ';
632
-                }
633
-                elseif (strtolower($operator) == 'has') 
634
-                {
635
-                    $sql              = $model->withTrashed()->has($key)->toSql();
636
-                    $conditions       = $this->constructConditions($value, $model->first()->$key);
637
-                    $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ')';
638
-                    $conditionValues  = array_merge($conditionValues, $conditions['conditionValues']);
639
-                }
640
-                else
641
-                {
642
-                    $conditionString  .= $key . ' ' . $operator . ' ? {op} ';
643
-                    $conditionValues[] = $value;
644
-                }
645
-            }
646
-        }
647
-        $conditionString = '(' . rtrim($conditionString, '{op} ') . ')';
648
-        return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues];
649
-    }
650
-
651
-    /**
652
-     * Abstract method that return the necessary 
653
-     * information (full model namespace)
654
-     * needed to preform the previous actions.
655
-     * 
656
-     * @return string
657
-     */
658
-    abstract protected function getModel();
611
+				if (strtolower($operator) == 'between') 
612
+				{
613
+					$conditionString  .= $key . ' >= ? and ';
614
+					$conditionValues[] = $value1;
615
+
616
+					$conditionString  .= $key . ' <= ? {op} ';
617
+					$conditionValues[] = $value2;
618
+				}
619
+				elseif (strtolower($operator) == 'in') 
620
+				{
621
+					$conditionValues  = array_merge($conditionValues, $value);
622
+					$inBindingsString = rtrim(str_repeat('?,', count($value)), ',');
623
+					$conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} ';
624
+				}
625
+				elseif (strtolower($operator) == 'null') 
626
+				{
627
+					$conditionString .= $key . ' is null {op} ';
628
+				}
629
+				elseif (strtolower($operator) == 'not null') 
630
+				{
631
+					$conditionString .= $key . ' is not null {op} ';
632
+				}
633
+				elseif (strtolower($operator) == 'has') 
634
+				{
635
+					$sql              = $model->withTrashed()->has($key)->toSql();
636
+					$conditions       = $this->constructConditions($value, $model->first()->$key);
637
+					$conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ')';
638
+					$conditionValues  = array_merge($conditionValues, $conditions['conditionValues']);
639
+				}
640
+				else
641
+				{
642
+					$conditionString  .= $key . ' ' . $operator . ' ? {op} ';
643
+					$conditionValues[] = $value;
644
+				}
645
+			}
646
+		}
647
+		$conditionString = '(' . rtrim($conditionString, '{op} ') . ')';
648
+		return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues];
649
+	}
650
+
651
+	/**
652
+	 * Abstract method that return the necessary 
653
+	 * information (full model namespace)
654
+	 * needed to preform the previous actions.
655
+	 * 
656
+	 * @return string
657
+	 */
658
+	abstract protected function getModel();
659 659
 }
660 660
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +30 added lines, -30 removed lines patch added patch discarded remove patch
@@ -63,14 +63,14 @@  discard block
 block discarded – undo
63 63
         /**
64 64
          * Construct the select conditions for the model.
65 65
          */
66
-        $model->where(function ($q) use ($query, $conditionColumns, $relations){
66
+        $model->where(function($q) use ($query, $conditionColumns, $relations){
67 67
 
68 68
             if (count($conditionColumns)) 
69 69
             {
70 70
                 /**
71 71
                  * Use the first element in the model columns to construct the first condition.
72 72
                  */
73
-                $q->where(\DB::raw('LOWER(' . array_shift($conditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%');
73
+                $q->where(\DB::raw('LOWER('.array_shift($conditionColumns).')'), 'LIKE', '%'.strtolower($query).'%');
74 74
             }
75 75
 
76 76
             /**
@@ -78,7 +78,7 @@  discard block
 block discarded – undo
78 78
              */
79 79
             foreach ($conditionColumns as $column) 
80 80
             {
81
-                $q->orWhere(\DB::raw('LOWER(' . $column . ')'), 'LIKE', '%' . strtolower($query) . '%');
81
+                $q->orWhere(\DB::raw('LOWER('.$column.')'), 'LIKE', '%'.strtolower($query).'%');
82 82
             }
83 83
 
84 84
             /**
@@ -99,9 +99,9 @@  discard block
 block discarded – undo
99 99
                     /**
100 100
                      * Construct the relation condition.
101 101
                      */
102
-                    $q->orWhereHas($relation, function ($subModel) use ($query, $relation){
102
+                    $q->orWhereHas($relation, function($subModel) use ($query, $relation){
103 103
 
104
-                        $subModel->where(function ($q) use ($query, $relation){
104
+                        $subModel->where(function($q) use ($query, $relation){
105 105
 
106 106
                             /**
107 107
                              * Get columns of the relation.
@@ -113,7 +113,7 @@  discard block
 block discarded – undo
113 113
                                 /**
114 114
                                 * Use the first element in the relation model columns to construct the first condition.
115 115
                                  */
116
-                                $q->where(\DB::raw('LOWER(' . array_shift($subConditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%');
116
+                                $q->where(\DB::raw('LOWER('.array_shift($subConditionColumns).')'), 'LIKE', '%'.strtolower($query).'%');
117 117
                             }
118 118
 
119 119
                             /**
@@ -121,7 +121,7 @@  discard block
 block discarded – undo
121 121
                              */
122 122
                             foreach ($subConditionColumns as $subConditionColumn)
123 123
                             {
124
-                                $q->orWhere(\DB::raw('LOWER(' . $subConditionColumn . ')'), 'LIKE', '%' . strtolower($query) . '%');
124
+                                $q->orWhere(\DB::raw('LOWER('.$subConditionColumn.')'), 'LIKE', '%'.strtolower($query).'%');
125 125
                             } 
126 126
                         });
127 127
 
@@ -182,7 +182,7 @@  discard block
 block discarded – undo
182 182
         $modelClass = $this->model;
183 183
         $relations  = [];
184 184
 
185
-        \DB::transaction(function () use (&$model, &$relations, $data, $saveLog, $modelClass) {
185
+        \DB::transaction(function() use (&$model, &$relations, $data, $saveLog, $modelClass) {
186 186
             /**
187 187
              * If the id is present in the data then select the model for updating,
188 188
              * else create new model.
@@ -191,7 +191,7 @@  discard block
 block discarded – undo
191 191
             $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass;
192 192
             if ( ! $model) 
193 193
             {
194
-                \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']);
194
+                \ErrorHandler::notFound(class_basename($modelClass).' with id : '.$data['id']);
195 195
             }
196 196
 
197 197
             /**
@@ -249,7 +249,7 @@  discard block
 block discarded – undo
249 249
                                  */
250 250
                                 if ( ! $relationModel) 
251 251
                                 {
252
-                                    \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']);
252
+                                    \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$val['id']);
253 253
                                 }
254 254
 
255 255
                                 /**
@@ -260,7 +260,7 @@  discard block
 block discarded – undo
260 260
                                     /**
261 261
                                      * Prevent the sub relations or attributes not in the fillable.
262 262
                                      */
263
-                                    if (gettype($val) !== 'object' && gettype($val) !== 'array' &&  array_search($attr, $relationModel->getFillable(), true) !== false)
263
+                                    if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false)
264 264
                                     {
265 265
                                         $relationModel->$attr = $val;
266 266
                                     }
@@ -290,7 +290,7 @@  discard block
 block discarded – undo
290 290
                                      */
291 291
                                     if ( ! $relationModel) 
292 292
                                     {
293
-                                        \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']);
293
+                                        \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$value['id']);
294 294
                                     }
295 295
 
296 296
                                     foreach ($value as $relationAttribute => $relationValue) 
@@ -433,7 +433,7 @@  discard block
 block discarded – undo
433 433
         }
434 434
         else
435 435
         {
436
-            call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){
436
+            call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function($model) use ($data, $saveLog){
437 437
                 $model->update($data);
438 438
                 $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $model->id, $model) : false;
439 439
             });
@@ -452,11 +452,11 @@  discard block
 block discarded – undo
452 452
     {
453 453
         if ($attribute == 'id') 
454 454
         {
455
-            \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) {
455
+            \DB::transaction(function() use ($value, $attribute, &$result, $saveLog) {
456 456
                 $model = $this->model->lockForUpdate()->find($value);
457 457
                 if ( ! $model) 
458 458
                 {
459
-                    \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value);
459
+                    \ErrorHandler::notFound(class_basename($this->model).' with id : '.$value);
460 460
                 }
461 461
                 
462 462
                 $model->delete();
@@ -465,8 +465,8 @@  discard block
 block discarded – undo
465 465
         }
466 466
         else
467 467
         {
468
-            \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) {
469
-                call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){
468
+            \DB::transaction(function() use ($value, $attribute, &$result, $saveLog) {
469
+                call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function($model) {
470 470
                     $model->delete();
471 471
                     $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false;
472 472
                 });
@@ -503,7 +503,7 @@  discard block
 block discarded – undo
503 503
     {
504 504
         $conditions = $this->constructConditions($conditions, $this->model);
505 505
         $sort       = $desc ? 'desc' : 'asc';
506
-        return call_user_func_array("{$this->getModel()}::with",  array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns);
506
+        return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns);
507 507
     }
508 508
 
509 509
     /**
@@ -543,7 +543,7 @@  discard block
 block discarded – undo
543 543
             $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']);
544 544
         }
545 545
 
546
-        return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);;
546
+        return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); ;
547 547
     }
548 548
 
549 549
     /**
@@ -559,7 +559,7 @@  discard block
 block discarded – undo
559 559
 
560 560
         if ( ! $model) 
561 561
         {
562
-            \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id);
562
+            \ErrorHandler::notFound(class_basename($this->model).' with id : '.$id);
563 563
         }
564 564
 
565 565
         $model->restore();
@@ -579,13 +579,13 @@  discard block
 block discarded – undo
579 579
             if ($key == 'and') 
580 580
             {
581 581
                 $conditions       = $this->constructConditions($value, $model);
582
-                $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} ';
582
+                $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} ';
583 583
                 $conditionValues  = array_merge($conditionValues, $conditions['conditionValues']);
584 584
             }
585 585
             else if ($key == 'or')
586 586
             {
587 587
                 $conditions       = $this->constructConditions($value, $model);
588
-                $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} ';
588
+                $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} ';
589 589
                 $conditionValues  = array_merge($conditionValues, $conditions['conditionValues']);
590 590
             }
591 591
             else
@@ -610,41 +610,41 @@  discard block
 block discarded – undo
610 610
                 
611 611
                 if (strtolower($operator) == 'between') 
612 612
                 {
613
-                    $conditionString  .= $key . ' >= ? and ';
613
+                    $conditionString  .= $key.' >= ? and ';
614 614
                     $conditionValues[] = $value1;
615 615
 
616
-                    $conditionString  .= $key . ' <= ? {op} ';
616
+                    $conditionString  .= $key.' <= ? {op} ';
617 617
                     $conditionValues[] = $value2;
618 618
                 }
619 619
                 elseif (strtolower($operator) == 'in') 
620 620
                 {
621 621
                     $conditionValues  = array_merge($conditionValues, $value);
622 622
                     $inBindingsString = rtrim(str_repeat('?,', count($value)), ',');
623
-                    $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} ';
623
+                    $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} ';
624 624
                 }
625 625
                 elseif (strtolower($operator) == 'null') 
626 626
                 {
627
-                    $conditionString .= $key . ' is null {op} ';
627
+                    $conditionString .= $key.' is null {op} ';
628 628
                 }
629 629
                 elseif (strtolower($operator) == 'not null') 
630 630
                 {
631
-                    $conditionString .= $key . ' is not null {op} ';
631
+                    $conditionString .= $key.' is not null {op} ';
632 632
                 }
633 633
                 elseif (strtolower($operator) == 'has') 
634 634
                 {
635 635
                     $sql              = $model->withTrashed()->has($key)->toSql();
636 636
                     $conditions       = $this->constructConditions($value, $model->first()->$key);
637
-                    $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ')';
637
+                    $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')').' and '.$conditions['conditionString'].')';
638 638
                     $conditionValues  = array_merge($conditionValues, $conditions['conditionValues']);
639 639
                 }
640 640
                 else
641 641
                 {
642
-                    $conditionString  .= $key . ' ' . $operator . ' ? {op} ';
642
+                    $conditionString  .= $key.' '.$operator.' ? {op} ';
643 643
                     $conditionValues[] = $value;
644 644
                 }
645 645
             }
646 646
         }
647
-        $conditionString = '(' . rtrim($conditionString, '{op} ') . ')';
647
+        $conditionString = '('.rtrim($conditionString, '{op} ').')';
648 648
         return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues];
649 649
     }
650 650
 
Please login to merge, or discard this patch.
Braces   +11 added lines, -22 removed lines patch added patch discarded remove patch
@@ -430,8 +430,7 @@  discard block
 block discarded – undo
430 430
             $model = $this->model->lockForUpdate()->find($value);
431 431
             $model ? $model->update($data) : 0;
432 432
             $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $value, $model) : false;
433
-        }
434
-        else
433
+        } else
435 434
         {
436 435
             call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){
437 436
                 $model->update($data);
@@ -462,8 +461,7 @@  discard block
 block discarded – undo
462 461
                 $model->delete();
463 462
                 $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false;
464 463
             });
465
-        }
466
-        else
464
+        } else
467 465
         {
468 466
             \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) {
469 467
                 call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){
@@ -581,14 +579,12 @@  discard block
 block discarded – undo
581 579
                 $conditions       = $this->constructConditions($value, $model);
582 580
                 $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} ';
583 581
                 $conditionValues  = array_merge($conditionValues, $conditions['conditionValues']);
584
-            }
585
-            else if ($key == 'or')
582
+            } else if ($key == 'or')
586 583
             {
587 584
                 $conditions       = $this->constructConditions($value, $model);
588 585
                 $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} ';
589 586
                 $conditionValues  = array_merge($conditionValues, $conditions['conditionValues']);
590
-            }
591
-            else
587
+            } else
592 588
             {
593 589
                 if (is_array($value)) 
594 590
                 {
@@ -597,13 +593,11 @@  discard block
 block discarded – undo
597 593
                     {
598 594
                         $value1 = $value['val1'];
599 595
                         $value2 = $value['val2'];
600
-                    }
601
-                    else
596
+                    } else
602 597
                     {
603 598
                         $value = array_key_exists('val', $value) ? $value['val'] : '';
604 599
                     }
605
-                }
606
-                else
600
+                } else
607 601
                 {
608 602
                     $operator = '=';
609 603
                 }
@@ -615,29 +609,24 @@  discard block
 block discarded – undo
615 609
 
616 610
                     $conditionString  .= $key . ' <= ? {op} ';
617 611
                     $conditionValues[] = $value2;
618
-                }
619
-                elseif (strtolower($operator) == 'in') 
612
+                } elseif (strtolower($operator) == 'in') 
620 613
                 {
621 614
                     $conditionValues  = array_merge($conditionValues, $value);
622 615
                     $inBindingsString = rtrim(str_repeat('?,', count($value)), ',');
623 616
                     $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} ';
624
-                }
625
-                elseif (strtolower($operator) == 'null') 
617
+                } elseif (strtolower($operator) == 'null') 
626 618
                 {
627 619
                     $conditionString .= $key . ' is null {op} ';
628
-                }
629
-                elseif (strtolower($operator) == 'not null') 
620
+                } elseif (strtolower($operator) == 'not null') 
630 621
                 {
631 622
                     $conditionString .= $key . ' is not null {op} ';
632
-                }
633
-                elseif (strtolower($operator) == 'has') 
623
+                } elseif (strtolower($operator) == 'has') 
634 624
                 {
635 625
                     $sql              = $model->withTrashed()->has($key)->toSql();
636 626
                     $conditions       = $this->constructConditions($value, $model->first()->$key);
637 627
                     $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ')';
638 628
                     $conditionValues  = array_merge($conditionValues, $conditions['conditionValues']);
639
-                }
640
-                else
629
+                } else
641 630
                 {
642 631
                     $conditionString  .= $key . ' ' . $operator . ' ? {op} ';
643 632
                     $conditionValues[] = $value;
Please login to merge, or discard this patch.
src/Modules/V1/Core/Http/Controllers/BaseApiController.php 2 patches
Indentation   +245 added lines, -245 removed lines patch added patch discarded remove patch
@@ -6,272 +6,272 @@
 block discarded – undo
6 6
 
7 7
 class BaseApiController extends Controller
8 8
 {
9
-    /**
10
-     * The model implementation.
11
-     * 
12
-     * @var model
13
-     */
14
-    protected $model;
9
+	/**
10
+	 * The model implementation.
11
+	 * 
12
+	 * @var model
13
+	 */
14
+	protected $model;
15 15
 
16
-    /**
17
-     * The config implementation.
18
-     * 
19
-     * @var config
20
-     */
21
-    protected $config;
16
+	/**
17
+	 * The config implementation.
18
+	 * 
19
+	 * @var config
20
+	 */
21
+	protected $config;
22 22
 
23
-    public function __construct()
24
-    {
25
-        \Session::put('timeZoneDiff', \Request::header('time-zone-diff') ?: 0);
23
+	public function __construct()
24
+	{
25
+		\Session::put('timeZoneDiff', \Request::header('time-zone-diff') ?: 0);
26 26
 
27
-        $locale = \Request::header('locale');
28
-        switch ($locale) 
29
-        {
30
-            case 'en':
31
-            \App::setLocale('en');
32
-            \Session::put('locale', 'en');
33
-            break;
27
+		$locale = \Request::header('locale');
28
+		switch ($locale) 
29
+		{
30
+			case 'en':
31
+			\App::setLocale('en');
32
+			\Session::put('locale', 'en');
33
+			break;
34 34
 
35
-            case 'ar':
36
-            \App::setLocale('ar');
37
-            \Session::put('locale', 'ar');
38
-            break;
35
+			case 'ar':
36
+			\App::setLocale('ar');
37
+			\Session::put('locale', 'ar');
38
+			break;
39 39
 
40
-            case 'all':
41
-            \App::setLocale('en');
42
-            \Session::put('locale', 'all');
43
-            break;
40
+			case 'all':
41
+			\App::setLocale('en');
42
+			\Session::put('locale', 'all');
43
+			break;
44 44
 
45
-            default:
46
-            \App::setLocale('en');
47
-            \Session::put('locale', 'en');
48
-            break;
49
-        }
45
+			default:
46
+			\App::setLocale('en');
47
+			\Session::put('locale', 'en');
48
+			break;
49
+		}
50 50
         
51
-        $this->config              = \CoreConfig::getConfig();
52
-        $this->model               = property_exists($this, 'model') ? $this->model : false;
53
-        $this->validationRules     = property_exists($this, 'validationRules') ? $this->validationRules : false;
54
-        $this->skipPermissionCheck = property_exists($this, 'skipPermissionCheck') ? $this->skipPermissionCheck : [];
55
-        $this->skipLoginCheck      = property_exists($this, 'skipLoginCheck') ? $this->skipLoginCheck : [];
56
-        $this->relations           = array_key_exists($this->model, $this->config['relations']) ? $this->config['relations'][$this->model] : false;
57
-        $route                     = explode('@',\Route::currentRouteAction())[1];
58
-        $this->checkPermission($route);
59
-    }
51
+		$this->config              = \CoreConfig::getConfig();
52
+		$this->model               = property_exists($this, 'model') ? $this->model : false;
53
+		$this->validationRules     = property_exists($this, 'validationRules') ? $this->validationRules : false;
54
+		$this->skipPermissionCheck = property_exists($this, 'skipPermissionCheck') ? $this->skipPermissionCheck : [];
55
+		$this->skipLoginCheck      = property_exists($this, 'skipLoginCheck') ? $this->skipLoginCheck : [];
56
+		$this->relations           = array_key_exists($this->model, $this->config['relations']) ? $this->config['relations'][$this->model] : false;
57
+		$route                     = explode('@',\Route::currentRouteAction())[1];
58
+		$this->checkPermission($route);
59
+	}
60 60
 
61
-    /**
62
-     * Fetch all records with relations from model repository.
63
-     * 
64
-     * @return \Illuminate\Http\Response
65
-     */
66
-    public function index() 
67
-    {
68
-        if ($this->model)
69
-        {
70
-            $relations = $this->relations && $this->relations['all'] ? $this->relations['all'] : [];
71
-            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->all($relations), 200);
72
-        }
73
-    }
61
+	/**
62
+	 * Fetch all records with relations from model repository.
63
+	 * 
64
+	 * @return \Illuminate\Http\Response
65
+	 */
66
+	public function index() 
67
+	{
68
+		if ($this->model)
69
+		{
70
+			$relations = $this->relations && $this->relations['all'] ? $this->relations['all'] : [];
71
+			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->all($relations), 200);
72
+		}
73
+	}
74 74
 
75
-    /**
76
-     * Fetch the single object with relations from model repository.
77
-     * 
78
-     * @param  integer $id
79
-     * @return \Illuminate\Http\Response
80
-     */
81
-    public function find($id) 
82
-    {
83
-        if ($this->model) 
84
-        {
85
-            $relations = $this->relations && $this->relations['find'] ? $this->relations['find'] : [];
86
-            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->find($id, $relations), 200);
87
-        }
88
-    }
75
+	/**
76
+	 * Fetch the single object with relations from model repository.
77
+	 * 
78
+	 * @param  integer $id
79
+	 * @return \Illuminate\Http\Response
80
+	 */
81
+	public function find($id) 
82
+	{
83
+		if ($this->model) 
84
+		{
85
+			$relations = $this->relations && $this->relations['find'] ? $this->relations['find'] : [];
86
+			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->find($id, $relations), 200);
87
+		}
88
+	}
89 89
 
90
-    /**
91
-     * Paginate all records with relations from model repository
92
-     * that matche the given query.
93
-     * 
94
-     * @param  string  $query
95
-     * @param  integer $perPage
96
-     * @param  string  $sortBy
97
-     * @param  boolean $desc
98
-     * @return \Illuminate\Http\Response
99
-     */
100
-    public function search($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) 
101
-    {
102
-        if ($this->model) 
103
-        {
104
-            $relations = $this->relations && $this->relations['search'] ? $this->relations['search'] : [];
105
-            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->search($query, $perPage, $relations, $sortBy, $desc), 200);
106
-        }
107
-    }
90
+	/**
91
+	 * Paginate all records with relations from model repository
92
+	 * that matche the given query.
93
+	 * 
94
+	 * @param  string  $query
95
+	 * @param  integer $perPage
96
+	 * @param  string  $sortBy
97
+	 * @param  boolean $desc
98
+	 * @return \Illuminate\Http\Response
99
+	 */
100
+	public function search($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) 
101
+	{
102
+		if ($this->model) 
103
+		{
104
+			$relations = $this->relations && $this->relations['search'] ? $this->relations['search'] : [];
105
+			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->search($query, $perPage, $relations, $sortBy, $desc), 200);
106
+		}
107
+	}
108 108
 
109
-    /**
110
-     * Fetch records from the storage based on the given
111
-     * condition.
112
-     * 
113
-     * @param  \Illuminate\Http\Request  $request
114
-     * @param  string  $sortBy
115
-     * @param  boolean $desc
116
-     * @return \Illuminate\Http\Response
117
-     */
118
-    public function findby(Request $request, $sortBy = 'created_at', $desc = 1) 
119
-    {
120
-        if ($this->model) 
121
-        {
122
-            $relations = $this->relations && $this->relations['findBy'] ? $this->relations['findBy'] : [];
123
-            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->findBy($request->all(), $relations, $sortBy, $desc), 200);
124
-        }
125
-    }
109
+	/**
110
+	 * Fetch records from the storage based on the given
111
+	 * condition.
112
+	 * 
113
+	 * @param  \Illuminate\Http\Request  $request
114
+	 * @param  string  $sortBy
115
+	 * @param  boolean $desc
116
+	 * @return \Illuminate\Http\Response
117
+	 */
118
+	public function findby(Request $request, $sortBy = 'created_at', $desc = 1) 
119
+	{
120
+		if ($this->model) 
121
+		{
122
+			$relations = $this->relations && $this->relations['findBy'] ? $this->relations['findBy'] : [];
123
+			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->findBy($request->all(), $relations, $sortBy, $desc), 200);
124
+		}
125
+	}
126 126
 
127
-    /**
128
-     * Fetch the first record from the storage based on the given
129
-     * condition.
130
-     * 
131
-     * @param  \Illuminate\Http\Request  $request
132
-     * @return \Illuminate\Http\Response
133
-     */
134
-    public function first(Request $request) 
135
-    {
136
-        if ($this->model) 
137
-        {
138
-            $relations = $this->relations && $this->relations['first'] ? $this->relations['first'] : [];
139
-            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->first($request->all(), $relations), 200);
140
-        }
141
-    }
127
+	/**
128
+	 * Fetch the first record from the storage based on the given
129
+	 * condition.
130
+	 * 
131
+	 * @param  \Illuminate\Http\Request  $request
132
+	 * @return \Illuminate\Http\Response
133
+	 */
134
+	public function first(Request $request) 
135
+	{
136
+		if ($this->model) 
137
+		{
138
+			$relations = $this->relations && $this->relations['first'] ? $this->relations['first'] : [];
139
+			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->first($request->all(), $relations), 200);
140
+		}
141
+	}
142 142
 
143
-    /**
144
-     * Paginate all records with relations from model repository.
145
-     * 
146
-     * @param  integer $perPage
147
-     * @param  string  $sortBy
148
-     * @param  boolean $desc
149
-     * @return \Illuminate\Http\Response
150
-     */
151
-    public function paginate($perPage = 15, $sortBy = 'created_at', $desc = 1) 
152
-    {
153
-        if ($this->model) 
154
-        {
155
-            $relations = $this->relations && $this->relations['paginate'] ? $this->relations['paginate'] : [];
156
-            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginate($perPage, $relations, $sortBy, $desc), 200);
157
-        }
158
-    }
143
+	/**
144
+	 * Paginate all records with relations from model repository.
145
+	 * 
146
+	 * @param  integer $perPage
147
+	 * @param  string  $sortBy
148
+	 * @param  boolean $desc
149
+	 * @return \Illuminate\Http\Response
150
+	 */
151
+	public function paginate($perPage = 15, $sortBy = 'created_at', $desc = 1) 
152
+	{
153
+		if ($this->model) 
154
+		{
155
+			$relations = $this->relations && $this->relations['paginate'] ? $this->relations['paginate'] : [];
156
+			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginate($perPage, $relations, $sortBy, $desc), 200);
157
+		}
158
+	}
159 159
 
160
-    /**
161
-     * Fetch all records with relations based on
162
-     * the given condition from storage in pages.
163
-     * 
164
-     * @param  \Illuminate\Http\Request  $request
165
-     * @param  integer $perPage
166
-     * @param  string  $sortBy
167
-     * @param  boolean $desc
168
-     * @return \Illuminate\Http\Response
169
-     */
170
-    public function paginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) 
171
-    {
172
-        if ($this->model) 
173
-        {
174
-            $relations = $this->relations && $this->relations['paginateBy'] ? $this->relations['paginateBy'] : [];
175
-            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginateBy($request->all(), $perPage, $relations, $sortBy, $desc), 200);
176
-        }
177
-    }
160
+	/**
161
+	 * Fetch all records with relations based on
162
+	 * the given condition from storage in pages.
163
+	 * 
164
+	 * @param  \Illuminate\Http\Request  $request
165
+	 * @param  integer $perPage
166
+	 * @param  string  $sortBy
167
+	 * @param  boolean $desc
168
+	 * @return \Illuminate\Http\Response
169
+	 */
170
+	public function paginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) 
171
+	{
172
+		if ($this->model) 
173
+		{
174
+			$relations = $this->relations && $this->relations['paginateBy'] ? $this->relations['paginateBy'] : [];
175
+			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginateBy($request->all(), $perPage, $relations, $sortBy, $desc), 200);
176
+		}
177
+	}
178 178
 
179
-    /**
180
-     * Save the given model to repository.
181
-     * 
182
-     * @param  \Illuminate\Http\Request  $request
183
-     * @return \Illuminate\Http\Response
184
-     */
185
-    public function save(Request $request) 
186
-    {
187
-        foreach ($this->validationRules as &$rule) 
188
-        {
189
-            if (strpos($rule, 'exists') && ! strpos($rule, 'deleted_at,NULL')) 
190
-            {
191
-                $rule .= ',deleted_at,NULL';
192
-            }
179
+	/**
180
+	 * Save the given model to repository.
181
+	 * 
182
+	 * @param  \Illuminate\Http\Request  $request
183
+	 * @return \Illuminate\Http\Response
184
+	 */
185
+	public function save(Request $request) 
186
+	{
187
+		foreach ($this->validationRules as &$rule) 
188
+		{
189
+			if (strpos($rule, 'exists') && ! strpos($rule, 'deleted_at,NULL')) 
190
+			{
191
+				$rule .= ',deleted_at,NULL';
192
+			}
193 193
 
194
-            if ($request->has('id')) 
195
-            {
196
-                $rule = str_replace('{id}', $request->get('id'), $rule);
197
-            }
198
-            else
199
-            {
200
-                $rule = str_replace(',{id}', '', $rule);
201
-            }
202
-        }
194
+			if ($request->has('id')) 
195
+			{
196
+				$rule = str_replace('{id}', $request->get('id'), $rule);
197
+			}
198
+			else
199
+			{
200
+				$rule = str_replace(',{id}', '', $rule);
201
+			}
202
+		}
203 203
         
204
-        $this->validate($request, $this->validationRules);
204
+		$this->validate($request, $this->validationRules);
205 205
 
206
-        if ($this->model) 
207
-        {
208
-            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->save($request->all()), 200);
209
-        }
210
-    }
206
+		if ($this->model) 
207
+		{
208
+			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->save($request->all()), 200);
209
+		}
210
+	}
211 211
 
212
-    /**
213
-     * Delete by the given id from model repository.
214
-     * 
215
-     * @param  integer  $id
216
-     * @return \Illuminate\Http\Response
217
-     */
218
-    public function delete($id) 
219
-    {
220
-        if ($this->model) 
221
-        {
222
-            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->delete($id), 200);
223
-        }
224
-    }
212
+	/**
213
+	 * Delete by the given id from model repository.
214
+	 * 
215
+	 * @param  integer  $id
216
+	 * @return \Illuminate\Http\Response
217
+	 */
218
+	public function delete($id) 
219
+	{
220
+		if ($this->model) 
221
+		{
222
+			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->delete($id), 200);
223
+		}
224
+	}
225 225
 
226
-    /**
227
-     * Return the deleted models in pages based on the given conditions.
228
-     *
229
-     * @param  \Illuminate\Http\Request  $request
230
-     * @param  integer $perPage
231
-     * @param  string  $sortBy
232
-     * @param  boolean $desc
233
-     * @return \Illuminate\Http\Response
234
-     */
235
-    public function deleted(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) 
236
-    {
237
-        return \Response::json(call_user_func_array("\Core::{$this->model}", [])->deleted($request->all(), $perPage, $sortBy, $desc), 200);
238
-    }
226
+	/**
227
+	 * Return the deleted models in pages based on the given conditions.
228
+	 *
229
+	 * @param  \Illuminate\Http\Request  $request
230
+	 * @param  integer $perPage
231
+	 * @param  string  $sortBy
232
+	 * @param  boolean $desc
233
+	 * @return \Illuminate\Http\Response
234
+	 */
235
+	public function deleted(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) 
236
+	{
237
+		return \Response::json(call_user_func_array("\Core::{$this->model}", [])->deleted($request->all(), $perPage, $sortBy, $desc), 200);
238
+	}
239 239
 
240
-    /**
241
-     * Restore the deleted model.
242
-     * 
243
-     * @param  integer  $id
244
-     * @return \Illuminate\Http\Response
245
-     */
246
-    public function restore($id) 
247
-    {
248
-        if ($this->model) 
249
-        {
250
-            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->restore($id), 200);
251
-        }
252
-    }
240
+	/**
241
+	 * Restore the deleted model.
242
+	 * 
243
+	 * @param  integer  $id
244
+	 * @return \Illuminate\Http\Response
245
+	 */
246
+	public function restore($id) 
247
+	{
248
+		if ($this->model) 
249
+		{
250
+			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->restore($id), 200);
251
+		}
252
+	}
253 253
 
254
-    /**
255
-     * Check if the logged in user can do the given permission.
256
-     * 
257
-     * @param  string $permission
258
-     * @return void
259
-     */
260
-    private function checkPermission($permission)
261
-    {
262
-        $permission = $permission !== 'index' ? $permission : 'list';
263
-        if ( ! in_array($permission, $this->skipLoginCheck)) 
264
-        {
265
-            $user = \Core::users()->find(\JWTAuth::parseToken()->authenticate()->id);
266
-            if ($user->blocked)
267
-            {
268
-                \ErrorHandler::userIsBlocked();
269
-            }
254
+	/**
255
+	 * Check if the logged in user can do the given permission.
256
+	 * 
257
+	 * @param  string $permission
258
+	 * @return void
259
+	 */
260
+	private function checkPermission($permission)
261
+	{
262
+		$permission = $permission !== 'index' ? $permission : 'list';
263
+		if ( ! in_array($permission, $this->skipLoginCheck)) 
264
+		{
265
+			$user = \Core::users()->find(\JWTAuth::parseToken()->authenticate()->id);
266
+			if ($user->blocked)
267
+			{
268
+				\ErrorHandler::userIsBlocked();
269
+			}
270 270
             
271
-            if ( ! in_array($permission, $this->skipPermissionCheck) && ! \Core::users()->can($permission, $this->model))
272
-            {
273
-                \ErrorHandler::noPermissions();
274
-            }
275
-        }
276
-    }
271
+			if ( ! in_array($permission, $this->skipPermissionCheck) && ! \Core::users()->can($permission, $this->model))
272
+			{
273
+				\ErrorHandler::noPermissions();
274
+			}
275
+		}
276
+	}
277 277
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -54,7 +54,7 @@
 block discarded – undo
54 54
         $this->skipPermissionCheck = property_exists($this, 'skipPermissionCheck') ? $this->skipPermissionCheck : [];
55 55
         $this->skipLoginCheck      = property_exists($this, 'skipLoginCheck') ? $this->skipLoginCheck : [];
56 56
         $this->relations           = array_key_exists($this->model, $this->config['relations']) ? $this->config['relations'][$this->model] : false;
57
-        $route                     = explode('@',\Route::currentRouteAction())[1];
57
+        $route                     = explode('@', \Route::currentRouteAction())[1];
58 58
         $this->checkPermission($route);
59 59
     }
60 60
 
Please login to merge, or discard this patch.
Database/Migrations/2016_01_24_111942_push_notifications_devices.php 2 patches
Indentation   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -21,7 +21,7 @@
 block discarded – undo
21 21
 			$table->unique(array('device_token', 'device_type'));
22 22
 			$table->softDeletes();
23 23
 			$table->timestamps();
24
-        });
24
+		});
25 25
 	}
26 26
 
27 27
 	/**
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -12,7 +12,7 @@
 block discarded – undo
12 12
 	 */
13 13
 	public function up()
14 14
 	{
15
-		Schema::create('push_notifications_devices', function (Blueprint $table) {
15
+		Schema::create('push_notifications_devices', function(Blueprint $table) {
16 16
 			$table->increments('id');
17 17
 			$table->string('device_token');
18 18
 			$table->enum('device_type', ['android', 'ios']);
Please login to merge, or discard this patch.