@@ -41,7 +41,7 @@ discard block |
||
41 | 41 | /** |
42 | 42 | * Return the logged in user account. |
43 | 43 | * |
44 | - * @return object |
|
44 | + * @return \Illuminate\Http\JsonResponse |
|
45 | 45 | */ |
46 | 46 | public function getAccount() |
47 | 47 | { |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | /** |
53 | 53 | * Logout the user. |
54 | 54 | * |
55 | - * @return void |
|
55 | + * @return \Illuminate\Http\JsonResponse |
|
56 | 56 | */ |
57 | 57 | public function getLogout() |
58 | 58 | { |
@@ -63,7 +63,7 @@ discard block |
||
63 | 63 | * Handle a registration request. |
64 | 64 | * |
65 | 65 | * @param \Illuminate\Http\Request $request |
66 | - * @return \Illuminate\Http\Response |
|
66 | + * @return \Illuminate\Http\JsonResponse |
|
67 | 67 | */ |
68 | 68 | public function postRegister(Request $request) |
69 | 69 | { |
@@ -76,7 +76,7 @@ discard block |
||
76 | 76 | * Handle a login request to the application. |
77 | 77 | * |
78 | 78 | * @param \Illuminate\Http\Request $request |
79 | - * @return \Illuminate\Http\Response |
|
79 | + * @return \Illuminate\Http\JsonResponse|null |
|
80 | 80 | */ |
81 | 81 | public function postLogin(Request $request) |
82 | 82 | { |
@@ -96,7 +96,7 @@ discard block |
||
96 | 96 | * Handle an assign groups to user request. |
97 | 97 | * |
98 | 98 | * @param \Illuminate\Http\Request $request |
99 | - * @return \Illuminate\Http\Response |
|
99 | + * @return \Illuminate\Http\JsonResponse |
|
100 | 100 | */ |
101 | 101 | public function postAssigngroups(Request $request) |
102 | 102 | { |
@@ -1,7 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | namespace App\Modules\Acl\Http\Controllers; |
3 | 3 | |
4 | -use Illuminate\Foundation\Http\FormRequest; |
|
5 | 4 | use App\Modules\Core\Http\Controllers\BaseApiController; |
6 | 5 | use Illuminate\Http\Request; |
7 | 6 |
@@ -84,8 +84,7 @@ |
||
84 | 84 | { |
85 | 85 | $relations = $this->relations && $this->relations['find'] ? $this->relations['find'] : []; |
86 | 86 | return \Response::json(call_user_func_array("\Core::{$this->model}", [])->find(\Auth::id(), $relations), 200); |
87 | - } |
|
88 | - else |
|
87 | + } else |
|
89 | 88 | { |
90 | 89 | $error = $this->errorHandler->loginFailed(); |
91 | 90 | abort($error['status'], $error['message']); |
@@ -12,7 +12,7 @@ discard block |
||
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 |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | * to check before add. |
34 | 34 | * @var array |
35 | 35 | */ |
36 | - protected $validationRules = [ |
|
36 | + protected $validationRules = [ |
|
37 | 37 | 'email' => 'required|email|unique:users,email,{id}', |
38 | 38 | 'password' => 'min:6' |
39 | 39 | ]; |
@@ -7,108 +7,108 @@ |
||
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']; |
|
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']; |
|
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', 'register']; |
|
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', 'register']; |
|
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 | - 'email' => 'required|email|unique:users,email,{id}', |
|
38 | - 'password' => 'min:6' |
|
39 | - ]; |
|
31 | + /** |
|
32 | + * The validations rules used by the base api controller |
|
33 | + * to check before add. |
|
34 | + * @var array |
|
35 | + */ |
|
36 | + protected $validationRules = [ |
|
37 | + 'email' => 'required|email|unique:users,email,{id}', |
|
38 | + 'password' => 'min:6' |
|
39 | + ]; |
|
40 | 40 | |
41 | - /** |
|
42 | - * Return the logged in user account. |
|
43 | - * |
|
44 | - * @return object |
|
45 | - */ |
|
46 | - public function getAccount() |
|
47 | - { |
|
48 | - $relations = $this->relations && $this->relations['find'] ? $this->relations['find'] : []; |
|
49 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->find(\JWTAuth::parseToken()->authenticate()->id, $relations), 200); |
|
50 | - } |
|
41 | + /** |
|
42 | + * Return the logged in user account. |
|
43 | + * |
|
44 | + * @return object |
|
45 | + */ |
|
46 | + public function getAccount() |
|
47 | + { |
|
48 | + $relations = $this->relations && $this->relations['find'] ? $this->relations['find'] : []; |
|
49 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->find(\JWTAuth::parseToken()->authenticate()->id, $relations), 200); |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * Logout the user. |
|
54 | - * |
|
55 | - * @return void |
|
56 | - */ |
|
57 | - public function getLogout() |
|
58 | - { |
|
59 | - return \Response::json(\JWTAuth::invalidate(\JWTAuth::getToken()), 200); |
|
60 | - } |
|
52 | + /** |
|
53 | + * Logout the user. |
|
54 | + * |
|
55 | + * @return void |
|
56 | + */ |
|
57 | + public function getLogout() |
|
58 | + { |
|
59 | + return \Response::json(\JWTAuth::invalidate(\JWTAuth::getToken()), 200); |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * Handle a registration request. |
|
64 | - * |
|
65 | - * @param \Illuminate\Http\Request $request |
|
66 | - * @return \Illuminate\Http\Response |
|
67 | - */ |
|
68 | - public function postRegister(Request $request) |
|
69 | - { |
|
70 | - $this->validate($request, ['email' => 'required|email|unique:users,email,{id}', 'password' => 'required|min:6']); |
|
62 | + /** |
|
63 | + * Handle a registration request. |
|
64 | + * |
|
65 | + * @param \Illuminate\Http\Request $request |
|
66 | + * @return \Illuminate\Http\Response |
|
67 | + */ |
|
68 | + public function postRegister(Request $request) |
|
69 | + { |
|
70 | + $this->validate($request, ['email' => 'required|email|unique:users,email,{id}', 'password' => 'required|min:6']); |
|
71 | 71 | |
72 | - $credentials = $request->only('email', 'password'); |
|
73 | - $token = \JWTAuth::fromUser(\Core::users()->model->create($credentials)); |
|
74 | - return \Response::json($token, 200); |
|
75 | - } |
|
72 | + $credentials = $request->only('email', 'password'); |
|
73 | + $token = \JWTAuth::fromUser(\Core::users()->model->create($credentials)); |
|
74 | + return \Response::json($token, 200); |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * Handle a login request to the application. |
|
79 | - * |
|
80 | - * @param \Illuminate\Http\Request $request |
|
81 | - * @return \Illuminate\Http\Response |
|
82 | - */ |
|
83 | - public function postLogin(Request $request) |
|
84 | - { |
|
85 | - $credentials = $request->only('email', 'password'); |
|
77 | + /** |
|
78 | + * Handle a login request to the application. |
|
79 | + * |
|
80 | + * @param \Illuminate\Http\Request $request |
|
81 | + * @return \Illuminate\Http\Response |
|
82 | + */ |
|
83 | + public function postLogin(Request $request) |
|
84 | + { |
|
85 | + $credentials = $request->only('email', 'password'); |
|
86 | 86 | |
87 | - if ($token = \JWTAuth::attempt($credentials)) |
|
88 | - { |
|
89 | - $relations = $this->relations && $this->relations['find'] ? $this->relations['find'] : []; |
|
90 | - return \Response::json($token, 200); |
|
91 | - } |
|
92 | - else |
|
93 | - { |
|
94 | - $error = \ErrorHandler::loginFailed(); |
|
95 | - abort($error['status'], $error['message']); |
|
96 | - } |
|
97 | - } |
|
87 | + if ($token = \JWTAuth::attempt($credentials)) |
|
88 | + { |
|
89 | + $relations = $this->relations && $this->relations['find'] ? $this->relations['find'] : []; |
|
90 | + return \Response::json($token, 200); |
|
91 | + } |
|
92 | + else |
|
93 | + { |
|
94 | + $error = \ErrorHandler::loginFailed(); |
|
95 | + abort($error['status'], $error['message']); |
|
96 | + } |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * Handle an assign groups to user request. |
|
101 | - * |
|
102 | - * @param \Illuminate\Http\Request $request |
|
103 | - * @return \Illuminate\Http\Response |
|
104 | - */ |
|
105 | - public function postAssigngroups(Request $request) |
|
106 | - { |
|
107 | - $this->validate($request, [ |
|
108 | - 'group_ids' => 'required|exists:groups,id', |
|
109 | - 'user_id' => 'required|exists:users,id' |
|
110 | - ]); |
|
99 | + /** |
|
100 | + * Handle an assign groups to user request. |
|
101 | + * |
|
102 | + * @param \Illuminate\Http\Request $request |
|
103 | + * @return \Illuminate\Http\Response |
|
104 | + */ |
|
105 | + public function postAssigngroups(Request $request) |
|
106 | + { |
|
107 | + $this->validate($request, [ |
|
108 | + 'group_ids' => 'required|exists:groups,id', |
|
109 | + 'user_id' => 'required|exists:users,id' |
|
110 | + ]); |
|
111 | 111 | |
112 | - return \Response::json(\Core::users()->assignGroups($request->get('user_id'), $request->get('group_ids')), 200); |
|
113 | - } |
|
112 | + return \Response::json(\Core::users()->assignGroups($request->get('user_id'), $request->get('group_ids')), 200); |
|
113 | + } |
|
114 | 114 | } |
@@ -51,7 +51,7 @@ discard block |
||
51 | 51 | * |
52 | 52 | * @param array $relations |
53 | 53 | * @param string $sortBy |
54 | - * @param boolean $desc |
|
54 | + * @param integer $desc |
|
55 | 55 | * @param array $columns |
56 | 56 | * @return collection |
57 | 57 | */ |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | * @param integer $perPage |
70 | 70 | * @param array $relations |
71 | 71 | * @param string $sortBy |
72 | - * @param boolean $desc |
|
72 | + * @param integer $desc |
|
73 | 73 | * @param array $columns |
74 | 74 | * @return collection |
75 | 75 | */ |
@@ -116,7 +116,7 @@ discard block |
||
116 | 116 | * @param integer $perPage |
117 | 117 | * @param array $relations |
118 | 118 | * @param string $sortBy |
119 | - * @param boolean $desc |
|
119 | + * @param integer $desc |
|
120 | 120 | * @param array $columns |
121 | 121 | * @return collection |
122 | 122 | */ |
@@ -134,7 +134,7 @@ discard block |
||
134 | 134 | * @param integer $perPage |
135 | 135 | * @param array $relations |
136 | 136 | * @param string $sortBy |
137 | - * @param boolean $desc |
|
137 | + * @param integer $desc |
|
138 | 138 | * @param array $columns |
139 | 139 | * @return collection |
140 | 140 | */ |
@@ -391,7 +391,7 @@ discard block |
||
391 | 391 | * @param array $conditions array of conditions |
392 | 392 | * @param array $relations |
393 | 393 | * @param string $sortBy |
394 | - * @param boolean $desc |
|
394 | + * @param integer $desc |
|
395 | 395 | * @param array $columns |
396 | 396 | * @return collection |
397 | 397 | */ |
@@ -408,7 +408,7 @@ discard block |
||
408 | 408 | * |
409 | 409 | * @param array $conditions array of conditions |
410 | 410 | * @param array $relations |
411 | - * @param array $colunmns |
|
411 | + * @param array $columns |
|
412 | 412 | * @return object |
413 | 413 | */ |
414 | 414 | public function first($conditions, $relations = [], $columns = array('*')) |
@@ -452,9 +452,9 @@ discard block |
||
452 | 452 | * Abstract method that is called in after |
453 | 453 | * the save method finish. |
454 | 454 | * |
455 | - * @param object $model |
|
455 | + * @param boolean $model |
|
456 | 456 | * @param array $relations |
457 | - * @return void |
|
457 | + * @return boolean |
|
458 | 458 | */ |
459 | 459 | protected function afterSave($model, $relations) |
460 | 460 | { |
@@ -213,8 +213,7 @@ discard block |
||
213 | 213 | } |
214 | 214 | } |
215 | 215 | $relations[$relation][] = $relationModel; |
216 | - } |
|
217 | - else |
|
216 | + } else |
|
218 | 217 | { |
219 | 218 | if (gettype($val) !== 'object' && gettype($val) !== 'array') |
220 | 219 | { |
@@ -234,8 +233,7 @@ discard block |
||
234 | 233 | } |
235 | 234 | } |
236 | 235 | } |
237 | - } |
|
238 | - else if (array_search($key, $model->getFillable(), true) !== false) |
|
236 | + } else if (array_search($key, $model->getFillable(), true) !== false) |
|
239 | 237 | { |
240 | 238 | $model->$key = $value; |
241 | 239 | } |
@@ -247,8 +245,7 @@ discard block |
||
247 | 245 | if ($value == 'delete' && $model->$key()->count()) |
248 | 246 | { |
249 | 247 | $model->$key()->delete(); |
250 | - } |
|
251 | - else if (gettype($value) == 'array') |
|
248 | + } else if (gettype($value) == 'array') |
|
252 | 249 | { |
253 | 250 | $ids = []; |
254 | 251 | foreach ($value as $val) |
@@ -279,8 +276,7 @@ discard block |
||
279 | 276 | $model->$key()->attach($ids); |
280 | 277 | break; |
281 | 278 | } |
282 | - } |
|
283 | - else |
|
279 | + } else |
|
284 | 280 | { |
285 | 281 | switch (class_basename($model->$key())) |
286 | 282 | { |
@@ -358,8 +354,7 @@ discard block |
||
358 | 354 | $model->delete(); |
359 | 355 | $saveLog ? $this->logs->saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false; |
360 | 356 | }); |
361 | - } |
|
362 | - else |
|
357 | + } else |
|
363 | 358 | { |
364 | 359 | \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
365 | 360 | call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ |
@@ -432,13 +427,11 @@ discard block |
||
432 | 427 | { |
433 | 428 | $conditionString .= str_replace('{op}', 'and', $this->constructConditions($value)['conditionString']) . ' {op} '; |
434 | 429 | $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); |
435 | - } |
|
436 | - else if ($key == 'or') |
|
430 | + } else if ($key == 'or') |
|
437 | 431 | { |
438 | 432 | $conditionString .= str_replace('{op}', 'or', $this->constructConditions($value)['conditionString']) . ' {op} '; |
439 | 433 | $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); |
440 | - } |
|
441 | - else |
|
434 | + } else |
|
442 | 435 | { |
443 | 436 | $conditionString .= $key . '=? {op} '; |
444 | 437 | $conditionValues[] = $value; |
@@ -4,443 +4,443 @@ |
||
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 | - * Create new AbstractRepository instance. |
|
16 | - */ |
|
17 | - public function __construct() |
|
18 | - { |
|
19 | - $this->config = \CoreConfig::getConfig(); |
|
20 | - $this->model = \App::make($this->getModel()); |
|
21 | - } |
|
14 | + /** |
|
15 | + * Create new AbstractRepository instance. |
|
16 | + */ |
|
17 | + public function __construct() |
|
18 | + { |
|
19 | + $this->config = \CoreConfig::getConfig(); |
|
20 | + $this->model = \App::make($this->getModel()); |
|
21 | + } |
|
22 | 22 | |
23 | - /** |
|
24 | - * Fetch all records with relations from the storage. |
|
25 | - * |
|
26 | - * @param array $relations |
|
27 | - * @param string $sortBy |
|
28 | - * @param boolean $desc |
|
29 | - * @param array $columns |
|
30 | - * @return collection |
|
31 | - */ |
|
23 | + /** |
|
24 | + * Fetch all records with relations from the storage. |
|
25 | + * |
|
26 | + * @param array $relations |
|
27 | + * @param string $sortBy |
|
28 | + * @param boolean $desc |
|
29 | + * @param array $columns |
|
30 | + * @return collection |
|
31 | + */ |
|
32 | 32 | public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
33 | 33 | { |
34 | - $sort = $desc ? 'desc' : 'asc'; |
|
35 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->get($columns); |
|
34 | + $sort = $desc ? 'desc' : 'asc'; |
|
35 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->get($columns); |
|
36 | 36 | } |
37 | 37 | |
38 | - /** |
|
39 | - * Fetch all records with relations from storage in pages |
|
40 | - * that matche the given query. |
|
41 | - * |
|
42 | - * @param string $query |
|
43 | - * @param integer $perPage |
|
44 | - * @param array $relations |
|
45 | - * @param string $sortBy |
|
46 | - * @param boolean $desc |
|
47 | - * @param array $columns |
|
48 | - * @return collection |
|
49 | - */ |
|
50 | - public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
51 | - { |
|
52 | - $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
53 | - $conditionColumns = $this->model->getFillable(); |
|
54 | - $sort = $desc ? 'desc' : 'asc'; |
|
38 | + /** |
|
39 | + * Fetch all records with relations from storage in pages |
|
40 | + * that matche the given query. |
|
41 | + * |
|
42 | + * @param string $query |
|
43 | + * @param integer $perPage |
|
44 | + * @param array $relations |
|
45 | + * @param string $sortBy |
|
46 | + * @param boolean $desc |
|
47 | + * @param array $columns |
|
48 | + * @return collection |
|
49 | + */ |
|
50 | + public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
51 | + { |
|
52 | + $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
53 | + $conditionColumns = $this->model->getFillable(); |
|
54 | + $sort = $desc ? 'desc' : 'asc'; |
|
55 | 55 | |
56 | - $model->where(function ($q) use ($query, $conditionColumns, $relations){ |
|
57 | - $q->where(\DB::raw('LOWER(CAST(' . array_shift($conditionColumns) . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); |
|
58 | - foreach ($conditionColumns as $column) |
|
59 | - { |
|
60 | - $q->orWhere(\DB::raw('LOWER(CAST(' . $column . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); |
|
61 | - } |
|
62 | - foreach ($relations as $relation) |
|
63 | - { |
|
64 | - $relation = explode('.', $relation)[0]; |
|
65 | - if (\Core::$relation()) |
|
66 | - { |
|
67 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
56 | + $model->where(function ($q) use ($query, $conditionColumns, $relations){ |
|
57 | + $q->where(\DB::raw('LOWER(CAST(' . array_shift($conditionColumns) . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); |
|
58 | + foreach ($conditionColumns as $column) |
|
59 | + { |
|
60 | + $q->orWhere(\DB::raw('LOWER(CAST(' . $column . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); |
|
61 | + } |
|
62 | + foreach ($relations as $relation) |
|
63 | + { |
|
64 | + $relation = explode('.', $relation)[0]; |
|
65 | + if (\Core::$relation()) |
|
66 | + { |
|
67 | + $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
68 | 68 | |
69 | - $subModel->where(function ($q) use ($query, $relation){ |
|
69 | + $subModel->where(function ($q) use ($query, $relation){ |
|
70 | 70 | |
71 | - $subConditionColumns = \Core::$relation()->model->getFillable(); |
|
72 | - $q->where(\DB::raw('LOWER(CAST(' . array_shift($subConditionColumns) . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); |
|
73 | - foreach ($subConditionColumns as $column) |
|
74 | - { |
|
75 | - $q->orWhere(\DB::raw('LOWER(CAST(' . $column . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); |
|
76 | - } |
|
77 | - }); |
|
71 | + $subConditionColumns = \Core::$relation()->model->getFillable(); |
|
72 | + $q->where(\DB::raw('LOWER(CAST(' . array_shift($subConditionColumns) . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); |
|
73 | + foreach ($subConditionColumns as $column) |
|
74 | + { |
|
75 | + $q->orWhere(\DB::raw('LOWER(CAST(' . $column . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); |
|
76 | + } |
|
77 | + }); |
|
78 | 78 | |
79 | - }); |
|
80 | - } |
|
81 | - } |
|
82 | - }); |
|
79 | + }); |
|
80 | + } |
|
81 | + } |
|
82 | + }); |
|
83 | 83 | |
84 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
85 | - } |
|
84 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * Fetch all records with relations from storage in pages. |
|
89 | - * |
|
90 | - * @param integer $perPage |
|
91 | - * @param array $relations |
|
92 | - * @param string $sortBy |
|
93 | - * @param boolean $desc |
|
94 | - * @param array $columns |
|
95 | - * @return collection |
|
96 | - */ |
|
97 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
98 | - { |
|
99 | - $sort = $desc ? 'desc' : 'asc'; |
|
100 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
101 | - } |
|
87 | + /** |
|
88 | + * Fetch all records with relations from storage in pages. |
|
89 | + * |
|
90 | + * @param integer $perPage |
|
91 | + * @param array $relations |
|
92 | + * @param string $sortBy |
|
93 | + * @param boolean $desc |
|
94 | + * @param array $columns |
|
95 | + * @return collection |
|
96 | + */ |
|
97 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
98 | + { |
|
99 | + $sort = $desc ? 'desc' : 'asc'; |
|
100 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
101 | + } |
|
102 | 102 | |
103 | - /** |
|
104 | - * Fetch all records with relations based on |
|
105 | - * the given condition from storage in pages. |
|
106 | - * |
|
107 | - * @param array $conditions array of conditions |
|
108 | - * @param integer $perPage |
|
109 | - * @param array $relations |
|
110 | - * @param string $sortBy |
|
111 | - * @param boolean $desc |
|
112 | - * @param array $columns |
|
113 | - * @return collection |
|
114 | - */ |
|
115 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
116 | - { |
|
117 | - unset($conditions['page']); |
|
118 | - $conditions = $this->constructConditions($conditions); |
|
119 | - $sort = $desc ? 'desc' : 'asc'; |
|
120 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
121 | - } |
|
103 | + /** |
|
104 | + * Fetch all records with relations based on |
|
105 | + * the given condition from storage in pages. |
|
106 | + * |
|
107 | + * @param array $conditions array of conditions |
|
108 | + * @param integer $perPage |
|
109 | + * @param array $relations |
|
110 | + * @param string $sortBy |
|
111 | + * @param boolean $desc |
|
112 | + * @param array $columns |
|
113 | + * @return collection |
|
114 | + */ |
|
115 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
116 | + { |
|
117 | + unset($conditions['page']); |
|
118 | + $conditions = $this->constructConditions($conditions); |
|
119 | + $sort = $desc ? 'desc' : 'asc'; |
|
120 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
121 | + } |
|
122 | 122 | |
123 | - /** |
|
124 | - * Save the given model to the storage. |
|
125 | - * |
|
126 | - * @param array $data |
|
127 | - * @param boolean $saveLog |
|
128 | - * @return object |
|
129 | - */ |
|
130 | - public function save(array $data, $saveLog = true) |
|
131 | - { |
|
132 | - $model = false; |
|
133 | - $modelClass = $this->model; |
|
134 | - $relations = []; |
|
135 | - $with = []; |
|
123 | + /** |
|
124 | + * Save the given model to the storage. |
|
125 | + * |
|
126 | + * @param array $data |
|
127 | + * @param boolean $saveLog |
|
128 | + * @return object |
|
129 | + */ |
|
130 | + public function save(array $data, $saveLog = true) |
|
131 | + { |
|
132 | + $model = false; |
|
133 | + $modelClass = $this->model; |
|
134 | + $relations = []; |
|
135 | + $with = []; |
|
136 | 136 | |
137 | - \DB::transaction(function () use (&$model, &$relations, &$with, $data, $saveLog, $modelClass) { |
|
138 | - /** |
|
139 | - * If the id is present in the data then select the model for updating, |
|
140 | - * else create new model. |
|
141 | - * @var array |
|
142 | - */ |
|
143 | - $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
144 | - if ( ! $model) |
|
145 | - { |
|
146 | - $error = \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
147 | - abort($error['status'], $error['message']); |
|
148 | - } |
|
137 | + \DB::transaction(function () use (&$model, &$relations, &$with, $data, $saveLog, $modelClass) { |
|
138 | + /** |
|
139 | + * If the id is present in the data then select the model for updating, |
|
140 | + * else create new model. |
|
141 | + * @var array |
|
142 | + */ |
|
143 | + $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
144 | + if ( ! $model) |
|
145 | + { |
|
146 | + $error = \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
147 | + abort($error['status'], $error['message']); |
|
148 | + } |
|
149 | 149 | |
150 | - /** |
|
151 | - * Construct the model object with the given data, |
|
152 | - * and if there is a relation add it to relations array, |
|
153 | - * then save the model. |
|
154 | - */ |
|
155 | - foreach ($data as $key => $value) |
|
156 | - { |
|
157 | - $relation = camel_case($key); |
|
158 | - if (method_exists($model, $relation)) |
|
159 | - { |
|
160 | - $with[] = $relation; |
|
161 | - if (class_basename($model->$relation) == 'Collection') |
|
162 | - { |
|
163 | - if ( ! $value || ! count($value)) |
|
164 | - { |
|
165 | - $relations[$relation] = 'delete'; |
|
166 | - } |
|
167 | - } |
|
168 | - if (is_array($value)) |
|
169 | - { |
|
170 | - foreach ($value as $attr => $val) |
|
171 | - { |
|
172 | - $relationBaseModel = \Core::$relation()->model; |
|
173 | - if (class_basename($model->$relation) == 'Collection') |
|
174 | - { |
|
175 | - $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
176 | - if ( ! $relationModel) |
|
177 | - { |
|
178 | - $error = \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
179 | - abort($error['status'], $error['message']); |
|
180 | - } |
|
150 | + /** |
|
151 | + * Construct the model object with the given data, |
|
152 | + * and if there is a relation add it to relations array, |
|
153 | + * then save the model. |
|
154 | + */ |
|
155 | + foreach ($data as $key => $value) |
|
156 | + { |
|
157 | + $relation = camel_case($key); |
|
158 | + if (method_exists($model, $relation)) |
|
159 | + { |
|
160 | + $with[] = $relation; |
|
161 | + if (class_basename($model->$relation) == 'Collection') |
|
162 | + { |
|
163 | + if ( ! $value || ! count($value)) |
|
164 | + { |
|
165 | + $relations[$relation] = 'delete'; |
|
166 | + } |
|
167 | + } |
|
168 | + if (is_array($value)) |
|
169 | + { |
|
170 | + foreach ($value as $attr => $val) |
|
171 | + { |
|
172 | + $relationBaseModel = \Core::$relation()->model; |
|
173 | + if (class_basename($model->$relation) == 'Collection') |
|
174 | + { |
|
175 | + $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
176 | + if ( ! $relationModel) |
|
177 | + { |
|
178 | + $error = \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
179 | + abort($error['status'], $error['message']); |
|
180 | + } |
|
181 | 181 | |
182 | - foreach ($val as $attr => $val) |
|
183 | - { |
|
184 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
185 | - { |
|
186 | - $relationModel->$attr = $val; |
|
187 | - } |
|
188 | - } |
|
189 | - $relations[$relation][] = $relationModel; |
|
190 | - } |
|
191 | - else |
|
192 | - { |
|
193 | - if (gettype($val) !== 'object' && gettype($val) !== 'array') |
|
194 | - { |
|
195 | - $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
196 | - if ( ! $relationModel) |
|
197 | - { |
|
198 | - $error = \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
199 | - abort($error['status'], $error['message']); |
|
200 | - } |
|
182 | + foreach ($val as $attr => $val) |
|
183 | + { |
|
184 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
185 | + { |
|
186 | + $relationModel->$attr = $val; |
|
187 | + } |
|
188 | + } |
|
189 | + $relations[$relation][] = $relationModel; |
|
190 | + } |
|
191 | + else |
|
192 | + { |
|
193 | + if (gettype($val) !== 'object' && gettype($val) !== 'array') |
|
194 | + { |
|
195 | + $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
196 | + if ( ! $relationModel) |
|
197 | + { |
|
198 | + $error = \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
199 | + abort($error['status'], $error['message']); |
|
200 | + } |
|
201 | 201 | |
202 | - if (array_search($attr, $relationModel->getFillable(), true) !== false) |
|
203 | - { |
|
204 | - $relationModel->$attr = $val; |
|
205 | - $relations[$relation] = $relationModel; |
|
206 | - } |
|
207 | - } |
|
208 | - } |
|
209 | - } |
|
210 | - } |
|
211 | - } |
|
212 | - else if (array_search($key, $model->getFillable(), true) !== false) |
|
213 | - { |
|
214 | - $model->$key = $value; |
|
215 | - } |
|
216 | - } |
|
217 | - $model->save(); |
|
202 | + if (array_search($attr, $relationModel->getFillable(), true) !== false) |
|
203 | + { |
|
204 | + $relationModel->$attr = $val; |
|
205 | + $relations[$relation] = $relationModel; |
|
206 | + } |
|
207 | + } |
|
208 | + } |
|
209 | + } |
|
210 | + } |
|
211 | + } |
|
212 | + else if (array_search($key, $model->getFillable(), true) !== false) |
|
213 | + { |
|
214 | + $model->$key = $value; |
|
215 | + } |
|
216 | + } |
|
217 | + $model->save(); |
|
218 | 218 | |
219 | - foreach ($relations as $key => $value) |
|
220 | - { |
|
221 | - if ($value == 'delete' && $model->$key()->count()) |
|
222 | - { |
|
223 | - $model->$key()->delete(); |
|
224 | - } |
|
225 | - else if (gettype($value) == 'array') |
|
226 | - { |
|
227 | - $ids = []; |
|
228 | - foreach ($value as $val) |
|
229 | - { |
|
230 | - switch (class_basename($model->$key())) |
|
231 | - { |
|
232 | - case 'HasMany': |
|
233 | - $foreignKeyName = explode('.', $model->$key()->getForeignKey())[1]; |
|
234 | - $val->$foreignKeyName = $model->id; |
|
235 | - $val->save(); |
|
236 | - $ids[] = $val->id; |
|
237 | - break; |
|
219 | + foreach ($relations as $key => $value) |
|
220 | + { |
|
221 | + if ($value == 'delete' && $model->$key()->count()) |
|
222 | + { |
|
223 | + $model->$key()->delete(); |
|
224 | + } |
|
225 | + else if (gettype($value) == 'array') |
|
226 | + { |
|
227 | + $ids = []; |
|
228 | + foreach ($value as $val) |
|
229 | + { |
|
230 | + switch (class_basename($model->$key())) |
|
231 | + { |
|
232 | + case 'HasMany': |
|
233 | + $foreignKeyName = explode('.', $model->$key()->getForeignKey())[1]; |
|
234 | + $val->$foreignKeyName = $model->id; |
|
235 | + $val->save(); |
|
236 | + $ids[] = $val->id; |
|
237 | + break; |
|
238 | 238 | |
239 | - case 'BelongsToMany': |
|
240 | - $val->save(); |
|
241 | - $ids[] = $val->id; |
|
242 | - break; |
|
243 | - } |
|
244 | - } |
|
245 | - switch (class_basename($model->$key())) |
|
246 | - { |
|
247 | - case 'HasMany': |
|
248 | - $model->$key()->whereNotIn('id', $ids)->delete(); |
|
249 | - break; |
|
239 | + case 'BelongsToMany': |
|
240 | + $val->save(); |
|
241 | + $ids[] = $val->id; |
|
242 | + break; |
|
243 | + } |
|
244 | + } |
|
245 | + switch (class_basename($model->$key())) |
|
246 | + { |
|
247 | + case 'HasMany': |
|
248 | + $model->$key()->whereNotIn('id', $ids)->delete(); |
|
249 | + break; |
|
250 | 250 | |
251 | - case 'BelongsToMany': |
|
252 | - $model->$key()->detach(); |
|
253 | - $model->$key()->attach($ids); |
|
254 | - break; |
|
255 | - } |
|
256 | - } |
|
257 | - else |
|
258 | - { |
|
259 | - switch (class_basename($model->$key())) |
|
260 | - { |
|
261 | - case 'BelongsTo': |
|
262 | - $value->save(); |
|
263 | - $model->$key()->associate($value); |
|
264 | - $model->save(); |
|
265 | - break; |
|
266 | - } |
|
267 | - } |
|
268 | - } |
|
269 | - $saveLog ? \Logging::saveLog(array_key_exists('id', $data) ? 'update' : 'create', class_basename($modelClass), $this->getModel(), $model->id, $model) : false; |
|
270 | - }); |
|
251 | + case 'BelongsToMany': |
|
252 | + $model->$key()->detach(); |
|
253 | + $model->$key()->attach($ids); |
|
254 | + break; |
|
255 | + } |
|
256 | + } |
|
257 | + else |
|
258 | + { |
|
259 | + switch (class_basename($model->$key())) |
|
260 | + { |
|
261 | + case 'BelongsTo': |
|
262 | + $value->save(); |
|
263 | + $model->$key()->associate($value); |
|
264 | + $model->save(); |
|
265 | + break; |
|
266 | + } |
|
267 | + } |
|
268 | + } |
|
269 | + $saveLog ? \Logging::saveLog(array_key_exists('id', $data) ? 'update' : 'create', class_basename($modelClass), $this->getModel(), $model->id, $model) : false; |
|
270 | + }); |
|
271 | 271 | |
272 | - $this->afterSave($model, $relations); |
|
272 | + $this->afterSave($model, $relations); |
|
273 | 273 | |
274 | - return $this->find($model->id, $with); |
|
275 | - } |
|
274 | + return $this->find($model->id, $with); |
|
275 | + } |
|
276 | 276 | |
277 | - /** |
|
278 | - * Save the given models to the storage. |
|
279 | - * |
|
280 | - * @param array $data |
|
281 | - * @return object |
|
282 | - */ |
|
283 | - public function saveMany(array $data) |
|
284 | - { |
|
285 | - $result = []; |
|
286 | - \DB::transaction(function () use (&$result, $data) { |
|
287 | - foreach ($data as $key => $value) |
|
288 | - { |
|
289 | - $result[] = $this->save($value); |
|
290 | - } |
|
291 | - }); |
|
292 | - return $result; |
|
293 | - } |
|
277 | + /** |
|
278 | + * Save the given models to the storage. |
|
279 | + * |
|
280 | + * @param array $data |
|
281 | + * @return object |
|
282 | + */ |
|
283 | + public function saveMany(array $data) |
|
284 | + { |
|
285 | + $result = []; |
|
286 | + \DB::transaction(function () use (&$result, $data) { |
|
287 | + foreach ($data as $key => $value) |
|
288 | + { |
|
289 | + $result[] = $this->save($value); |
|
290 | + } |
|
291 | + }); |
|
292 | + return $result; |
|
293 | + } |
|
294 | 294 | |
295 | - /** |
|
296 | - * Update record in the storage based on the given |
|
297 | - * condition. |
|
298 | - * |
|
299 | - * @param [type] $value condition value |
|
300 | - * @param array $data |
|
301 | - * @param string $attribute condition column name |
|
302 | - * @return void |
|
303 | - */ |
|
304 | - public function update($value, array $data, $attribute = 'id', $saveLog = true) |
|
305 | - { |
|
306 | - if ($attribute == 'id') |
|
307 | - { |
|
308 | - $model = $this->model->lockForUpdate()->find($value); |
|
309 | - $model ? $model->update($data) : 0; |
|
310 | - $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
311 | - } |
|
312 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){ |
|
313 | - $model->update($data); |
|
314 | - $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
315 | - }); |
|
316 | - } |
|
295 | + /** |
|
296 | + * Update record in the storage based on the given |
|
297 | + * condition. |
|
298 | + * |
|
299 | + * @param [type] $value condition value |
|
300 | + * @param array $data |
|
301 | + * @param string $attribute condition column name |
|
302 | + * @return void |
|
303 | + */ |
|
304 | + public function update($value, array $data, $attribute = 'id', $saveLog = true) |
|
305 | + { |
|
306 | + if ($attribute == 'id') |
|
307 | + { |
|
308 | + $model = $this->model->lockForUpdate()->find($value); |
|
309 | + $model ? $model->update($data) : 0; |
|
310 | + $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
311 | + } |
|
312 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){ |
|
313 | + $model->update($data); |
|
314 | + $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
315 | + }); |
|
316 | + } |
|
317 | 317 | |
318 | - /** |
|
319 | - * Delete record from the storage based on the given |
|
320 | - * condition. |
|
321 | - * |
|
322 | - * @param [type] $value condition value |
|
323 | - * @param string $attribute condition column name |
|
324 | - * @return void |
|
325 | - */ |
|
326 | - public function delete($value, $attribute = 'id', $saveLog = true) |
|
327 | - { |
|
328 | - if ($attribute == 'id') |
|
329 | - { |
|
330 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
331 | - $model = $this->model->lockForUpdate()->find($value); |
|
332 | - $model->delete(); |
|
333 | - $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
334 | - }); |
|
335 | - } |
|
336 | - else |
|
337 | - { |
|
338 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
339 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ |
|
340 | - $model->delete(); |
|
341 | - $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
342 | - }); |
|
343 | - }); |
|
344 | - } |
|
345 | - } |
|
318 | + /** |
|
319 | + * Delete record from the storage based on the given |
|
320 | + * condition. |
|
321 | + * |
|
322 | + * @param [type] $value condition value |
|
323 | + * @param string $attribute condition column name |
|
324 | + * @return void |
|
325 | + */ |
|
326 | + public function delete($value, $attribute = 'id', $saveLog = true) |
|
327 | + { |
|
328 | + if ($attribute == 'id') |
|
329 | + { |
|
330 | + \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
331 | + $model = $this->model->lockForUpdate()->find($value); |
|
332 | + $model->delete(); |
|
333 | + $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
334 | + }); |
|
335 | + } |
|
336 | + else |
|
337 | + { |
|
338 | + \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
339 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ |
|
340 | + $model->delete(); |
|
341 | + $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
342 | + }); |
|
343 | + }); |
|
344 | + } |
|
345 | + } |
|
346 | 346 | |
347 | - /** |
|
348 | - * Fetch records from the storage based on the given |
|
349 | - * id. |
|
350 | - * |
|
351 | - * @param integer $id |
|
352 | - * @param array $relations |
|
353 | - * @param array $columns |
|
354 | - * @return object |
|
355 | - */ |
|
356 | - public function find($id, $relations = [], $columns = array('*')) |
|
357 | - { |
|
358 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
359 | - } |
|
347 | + /** |
|
348 | + * Fetch records from the storage based on the given |
|
349 | + * id. |
|
350 | + * |
|
351 | + * @param integer $id |
|
352 | + * @param array $relations |
|
353 | + * @param array $columns |
|
354 | + * @return object |
|
355 | + */ |
|
356 | + public function find($id, $relations = [], $columns = array('*')) |
|
357 | + { |
|
358 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
359 | + } |
|
360 | 360 | |
361 | - /** |
|
362 | - * Fetch records from the storage based on the given |
|
363 | - * condition. |
|
364 | - * |
|
365 | - * @param array $conditions array of conditions |
|
366 | - * @param array $relations |
|
367 | - * @param string $sortBy |
|
368 | - * @param boolean $desc |
|
369 | - * @param array $columns |
|
370 | - * @return collection |
|
371 | - */ |
|
372 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
373 | - { |
|
374 | - $conditions = $this->constructConditions($conditions); |
|
375 | - $sort = $desc ? 'desc' : 'asc'; |
|
376 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
377 | - } |
|
361 | + /** |
|
362 | + * Fetch records from the storage based on the given |
|
363 | + * condition. |
|
364 | + * |
|
365 | + * @param array $conditions array of conditions |
|
366 | + * @param array $relations |
|
367 | + * @param string $sortBy |
|
368 | + * @param boolean $desc |
|
369 | + * @param array $columns |
|
370 | + * @return collection |
|
371 | + */ |
|
372 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
373 | + { |
|
374 | + $conditions = $this->constructConditions($conditions); |
|
375 | + $sort = $desc ? 'desc' : 'asc'; |
|
376 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
377 | + } |
|
378 | 378 | |
379 | - /** |
|
380 | - * Fetch the first record from the storage based on the given |
|
381 | - * condition. |
|
382 | - * |
|
383 | - * @param array $conditions array of conditions |
|
384 | - * @param array $relations |
|
385 | - * @param array $colunmns |
|
386 | - * @return object |
|
387 | - */ |
|
388 | - public function first($conditions, $relations = [], $columns = array('*')) |
|
389 | - { |
|
390 | - $conditions = $this->constructConditions($conditions); |
|
391 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
392 | - } |
|
379 | + /** |
|
380 | + * Fetch the first record from the storage based on the given |
|
381 | + * condition. |
|
382 | + * |
|
383 | + * @param array $conditions array of conditions |
|
384 | + * @param array $relations |
|
385 | + * @param array $colunmns |
|
386 | + * @return object |
|
387 | + */ |
|
388 | + public function first($conditions, $relations = [], $columns = array('*')) |
|
389 | + { |
|
390 | + $conditions = $this->constructConditions($conditions); |
|
391 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
392 | + } |
|
393 | 393 | |
394 | - /** |
|
395 | - * Build the conditions recursively for the retrieving methods. |
|
396 | - * @param array $conditions |
|
397 | - * @return array |
|
398 | - */ |
|
399 | - protected function constructConditions($conditions) |
|
400 | - { |
|
401 | - $conditionString = ''; |
|
402 | - $conditionValues = []; |
|
403 | - foreach ($conditions as $key => $value) |
|
404 | - { |
|
405 | - if ($key == 'and') |
|
406 | - { |
|
407 | - $conditionString .= str_replace('{op}', 'and', $this->constructConditions($value)['conditionString']) . ' {op} '; |
|
408 | - $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); |
|
409 | - } |
|
410 | - else if ($key == 'or') |
|
411 | - { |
|
412 | - $conditionString .= str_replace('{op}', 'or', $this->constructConditions($value)['conditionString']) . ' {op} '; |
|
413 | - $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); |
|
414 | - } |
|
415 | - else |
|
416 | - { |
|
417 | - $conditionString .= $key . '=? {op} '; |
|
418 | - $conditionValues[] = $value; |
|
419 | - } |
|
420 | - } |
|
421 | - $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
422 | - return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
423 | - } |
|
394 | + /** |
|
395 | + * Build the conditions recursively for the retrieving methods. |
|
396 | + * @param array $conditions |
|
397 | + * @return array |
|
398 | + */ |
|
399 | + protected function constructConditions($conditions) |
|
400 | + { |
|
401 | + $conditionString = ''; |
|
402 | + $conditionValues = []; |
|
403 | + foreach ($conditions as $key => $value) |
|
404 | + { |
|
405 | + if ($key == 'and') |
|
406 | + { |
|
407 | + $conditionString .= str_replace('{op}', 'and', $this->constructConditions($value)['conditionString']) . ' {op} '; |
|
408 | + $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); |
|
409 | + } |
|
410 | + else if ($key == 'or') |
|
411 | + { |
|
412 | + $conditionString .= str_replace('{op}', 'or', $this->constructConditions($value)['conditionString']) . ' {op} '; |
|
413 | + $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); |
|
414 | + } |
|
415 | + else |
|
416 | + { |
|
417 | + $conditionString .= $key . '=? {op} '; |
|
418 | + $conditionValues[] = $value; |
|
419 | + } |
|
420 | + } |
|
421 | + $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
422 | + return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
423 | + } |
|
424 | 424 | |
425 | - /** |
|
426 | - * Abstract method that is called in after |
|
427 | - * the save method finish. |
|
428 | - * |
|
429 | - * @param object $model |
|
430 | - * @param array $relations |
|
431 | - * @return void |
|
432 | - */ |
|
433 | - protected function afterSave($model, $relations) |
|
434 | - { |
|
435 | - return false; |
|
436 | - } |
|
425 | + /** |
|
426 | + * Abstract method that is called in after |
|
427 | + * the save method finish. |
|
428 | + * |
|
429 | + * @param object $model |
|
430 | + * @param array $relations |
|
431 | + * @return void |
|
432 | + */ |
|
433 | + protected function afterSave($model, $relations) |
|
434 | + { |
|
435 | + return false; |
|
436 | + } |
|
437 | 437 | |
438 | - /** |
|
439 | - * Abstract method that return the necessary |
|
440 | - * information (full model namespace) |
|
441 | - * needed to preform the previous actions. |
|
442 | - * |
|
443 | - * @return string |
|
444 | - */ |
|
445 | - abstract protected function getModel(); |
|
438 | + /** |
|
439 | + * Abstract method that return the necessary |
|
440 | + * information (full model namespace) |
|
441 | + * needed to preform the previous actions. |
|
442 | + * |
|
443 | + * @return string |
|
444 | + */ |
|
445 | + abstract protected function getModel(); |
|
446 | 446 | } |
447 | 447 | \ No newline at end of file |
@@ -53,26 +53,26 @@ discard block |
||
53 | 53 | $conditionColumns = $this->model->getFillable(); |
54 | 54 | $sort = $desc ? 'desc' : 'asc'; |
55 | 55 | |
56 | - $model->where(function ($q) use ($query, $conditionColumns, $relations){ |
|
57 | - $q->where(\DB::raw('LOWER(CAST(' . array_shift($conditionColumns) . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); |
|
56 | + $model->where(function($q) use ($query, $conditionColumns, $relations){ |
|
57 | + $q->where(\DB::raw('LOWER(CAST('.array_shift($conditionColumns).' AS TEXT))'), 'LIKE', '%'.strtolower($query).'%'); |
|
58 | 58 | foreach ($conditionColumns as $column) |
59 | 59 | { |
60 | - $q->orWhere(\DB::raw('LOWER(CAST(' . $column . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); |
|
60 | + $q->orWhere(\DB::raw('LOWER(CAST('.$column.' AS TEXT))'), 'LIKE', '%'.strtolower($query).'%'); |
|
61 | 61 | } |
62 | 62 | foreach ($relations as $relation) |
63 | 63 | { |
64 | 64 | $relation = explode('.', $relation)[0]; |
65 | 65 | if (\Core::$relation()) |
66 | 66 | { |
67 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
67 | + $q->orWhereHas($relation, function($subModel) use ($query, $relation){ |
|
68 | 68 | |
69 | - $subModel->where(function ($q) use ($query, $relation){ |
|
69 | + $subModel->where(function($q) use ($query, $relation){ |
|
70 | 70 | |
71 | 71 | $subConditionColumns = \Core::$relation()->model->getFillable(); |
72 | - $q->where(\DB::raw('LOWER(CAST(' . array_shift($subConditionColumns) . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); |
|
72 | + $q->where(\DB::raw('LOWER(CAST('.array_shift($subConditionColumns).' AS TEXT))'), 'LIKE', '%'.strtolower($query).'%'); |
|
73 | 73 | foreach ($subConditionColumns as $column) |
74 | 74 | { |
75 | - $q->orWhere(\DB::raw('LOWER(CAST(' . $column . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); |
|
75 | + $q->orWhere(\DB::raw('LOWER(CAST('.$column.' AS TEXT))'), 'LIKE', '%'.strtolower($query).'%'); |
|
76 | 76 | } |
77 | 77 | }); |
78 | 78 | |
@@ -134,7 +134,7 @@ discard block |
||
134 | 134 | $relations = []; |
135 | 135 | $with = []; |
136 | 136 | |
137 | - \DB::transaction(function () use (&$model, &$relations, &$with, $data, $saveLog, $modelClass) { |
|
137 | + \DB::transaction(function() use (&$model, &$relations, &$with, $data, $saveLog, $modelClass) { |
|
138 | 138 | /** |
139 | 139 | * If the id is present in the data then select the model for updating, |
140 | 140 | * else create new model. |
@@ -143,7 +143,7 @@ discard block |
||
143 | 143 | $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
144 | 144 | if ( ! $model) |
145 | 145 | { |
146 | - $error = \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
146 | + $error = \ErrorHandler::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
147 | 147 | abort($error['status'], $error['message']); |
148 | 148 | } |
149 | 149 | |
@@ -175,13 +175,13 @@ discard block |
||
175 | 175 | $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
176 | 176 | if ( ! $relationModel) |
177 | 177 | { |
178 | - $error = \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
178 | + $error = \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
179 | 179 | abort($error['status'], $error['message']); |
180 | 180 | } |
181 | 181 | |
182 | 182 | foreach ($val as $attr => $val) |
183 | 183 | { |
184 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
184 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
185 | 185 | { |
186 | 186 | $relationModel->$attr = $val; |
187 | 187 | } |
@@ -195,7 +195,7 @@ discard block |
||
195 | 195 | $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
196 | 196 | if ( ! $relationModel) |
197 | 197 | { |
198 | - $error = \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
198 | + $error = \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
199 | 199 | abort($error['status'], $error['message']); |
200 | 200 | } |
201 | 201 | |
@@ -283,7 +283,7 @@ discard block |
||
283 | 283 | public function saveMany(array $data) |
284 | 284 | { |
285 | 285 | $result = []; |
286 | - \DB::transaction(function () use (&$result, $data) { |
|
286 | + \DB::transaction(function() use (&$result, $data) { |
|
287 | 287 | foreach ($data as $key => $value) |
288 | 288 | { |
289 | 289 | $result[] = $this->save($value); |
@@ -309,7 +309,7 @@ discard block |
||
309 | 309 | $model ? $model->update($data) : 0; |
310 | 310 | $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $value, $model) : false; |
311 | 311 | } |
312 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){ |
|
312 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function($model) use ($data, $saveLog){ |
|
313 | 313 | $model->update($data); |
314 | 314 | $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
315 | 315 | }); |
@@ -327,7 +327,7 @@ discard block |
||
327 | 327 | { |
328 | 328 | if ($attribute == 'id') |
329 | 329 | { |
330 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
330 | + \DB::transaction(function() use ($value, $attribute, &$result, $saveLog) { |
|
331 | 331 | $model = $this->model->lockForUpdate()->find($value); |
332 | 332 | $model->delete(); |
333 | 333 | $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false; |
@@ -335,8 +335,8 @@ discard block |
||
335 | 335 | } |
336 | 336 | else |
337 | 337 | { |
338 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
339 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ |
|
338 | + \DB::transaction(function() use ($value, $attribute, &$result, $saveLog) { |
|
339 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function($model) { |
|
340 | 340 | $model->delete(); |
341 | 341 | $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
342 | 342 | }); |
@@ -373,7 +373,7 @@ discard block |
||
373 | 373 | { |
374 | 374 | $conditions = $this->constructConditions($conditions); |
375 | 375 | $sort = $desc ? 'desc' : 'asc'; |
376 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
376 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
377 | 377 | } |
378 | 378 | |
379 | 379 | /** |
@@ -404,21 +404,21 @@ discard block |
||
404 | 404 | { |
405 | 405 | if ($key == 'and') |
406 | 406 | { |
407 | - $conditionString .= str_replace('{op}', 'and', $this->constructConditions($value)['conditionString']) . ' {op} '; |
|
407 | + $conditionString .= str_replace('{op}', 'and', $this->constructConditions($value)['conditionString']).' {op} '; |
|
408 | 408 | $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); |
409 | 409 | } |
410 | 410 | else if ($key == 'or') |
411 | 411 | { |
412 | - $conditionString .= str_replace('{op}', 'or', $this->constructConditions($value)['conditionString']) . ' {op} '; |
|
412 | + $conditionString .= str_replace('{op}', 'or', $this->constructConditions($value)['conditionString']).' {op} '; |
|
413 | 413 | $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); |
414 | 414 | } |
415 | 415 | else |
416 | 416 | { |
417 | - $conditionString .= $key . '=? {op} '; |
|
417 | + $conditionString .= $key.'=? {op} '; |
|
418 | 418 | $conditionValues[] = $value; |
419 | 419 | } |
420 | 420 | } |
421 | - $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
421 | + $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
422 | 422 | return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
423 | 423 | } |
424 | 424 |
@@ -54,9 +54,7 @@ discard block |
||
54 | 54 | /** |
55 | 55 | * Fetch all records with relations from model repository. |
56 | 56 | * |
57 | - * @param string $sortBy |
|
58 | - * @param boolean $desc |
|
59 | - * @return \Illuminate\Http\Response |
|
57 | + * @return \Illuminate\Http\JsonResponse|null |
|
60 | 58 | */ |
61 | 59 | public function getIndex() |
62 | 60 | { |
@@ -71,7 +69,7 @@ discard block |
||
71 | 69 | * Fetch the single object with relations from model repository. |
72 | 70 | * |
73 | 71 | * @param integer $id |
74 | - * @return \Illuminate\Http\Response |
|
72 | + * @return \Illuminate\Http\JsonResponse|null |
|
75 | 73 | */ |
76 | 74 | public function getFind($id) |
77 | 75 | { |
@@ -89,8 +87,8 @@ discard block |
||
89 | 87 | * @param string $query |
90 | 88 | * @param integer $perPage |
91 | 89 | * @param string $sortBy |
92 | - * @param boolean $desc |
|
93 | - * @return \Illuminate\Http\Response |
|
90 | + * @param integer $desc |
|
91 | + * @return \Illuminate\Http\JsonResponse|null |
|
94 | 92 | */ |
95 | 93 | public function getSearch($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) |
96 | 94 | { |
@@ -107,8 +105,8 @@ discard block |
||
107 | 105 | * |
108 | 106 | * @param \Illuminate\Http\Request $request |
109 | 107 | * @param string $sortBy |
110 | - * @param boolean $desc |
|
111 | - * @return \Illuminate\Http\Response |
|
108 | + * @param integer $desc |
|
109 | + * @return \Illuminate\Http\JsonResponse|null |
|
112 | 110 | */ |
113 | 111 | public function postFindby(Request $request, $sortBy = 'created_at', $desc = 1) |
114 | 112 | { |
@@ -124,7 +122,7 @@ discard block |
||
124 | 122 | * condition. |
125 | 123 | * |
126 | 124 | * @param \Illuminate\Http\Request $request |
127 | - * @return \Illuminate\Http\Response |
|
125 | + * @return \Illuminate\Http\JsonResponse|null |
|
128 | 126 | */ |
129 | 127 | public function postFirst(Request $request) |
130 | 128 | { |
@@ -140,8 +138,8 @@ discard block |
||
140 | 138 | * |
141 | 139 | * @param integer $perPage |
142 | 140 | * @param string $sortBy |
143 | - * @param boolean $desc |
|
144 | - * @return \Illuminate\Http\Response |
|
141 | + * @param integer $desc |
|
142 | + * @return \Illuminate\Http\JsonResponse|null |
|
145 | 143 | */ |
146 | 144 | public function getPaginate($perPage = 15, $sortBy = 'created_at', $desc = 1) |
147 | 145 | { |
@@ -159,8 +157,8 @@ discard block |
||
159 | 157 | * @param \Illuminate\Http\Request $request |
160 | 158 | * @param integer $perPage |
161 | 159 | * @param string $sortBy |
162 | - * @param boolean $desc |
|
163 | - * @return \Illuminate\Http\Response |
|
160 | + * @param integer $desc |
|
161 | + * @return \Illuminate\Http\JsonResponse|null |
|
164 | 162 | */ |
165 | 163 | public function postPaginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
166 | 164 | { |
@@ -175,7 +173,7 @@ discard block |
||
175 | 173 | * Save the given model to repository. |
176 | 174 | * |
177 | 175 | * @param \Illuminate\Http\Request $request |
178 | - * @return \Illuminate\Http\Response |
|
176 | + * @return \Illuminate\Http\JsonResponse|null |
|
179 | 177 | */ |
180 | 178 | public function postSave(Request $request) |
181 | 179 | { |
@@ -208,7 +206,7 @@ discard block |
||
208 | 206 | * Delete by the given id from model repository. |
209 | 207 | * |
210 | 208 | * @param integer $id |
211 | - * @return \Illuminate\Http\Response |
|
209 | + * @return \Illuminate\Http\JsonResponse|null |
|
212 | 210 | */ |
213 | 211 | public function getDelete($id) |
214 | 212 | { |
@@ -47,7 +47,7 @@ |
||
47 | 47 | $this->skipLoginCheck = property_exists($this, 'skipLoginCheck') ? $this->skipLoginCheck : []; |
48 | 48 | |
49 | 49 | $this->relations = array_key_exists($this->model, $this->config['relations']) ? $this->config['relations'][$this->model] : false; |
50 | - $route = explode('@',\Route::currentRouteAction())[1]; |
|
50 | + $route = explode('@', \Route::currentRouteAction())[1]; |
|
51 | 51 | $this->checkPermission(explode('_', snake_case($route))[1]); |
52 | 52 | } |
53 | 53 |
@@ -190,8 +190,7 @@ discard block |
||
190 | 190 | if ($request->has('id')) |
191 | 191 | { |
192 | 192 | $rule = str_replace('{id}', $request->get('id'), $rule); |
193 | - } |
|
194 | - else |
|
193 | + } else |
|
195 | 194 | { |
196 | 195 | $rule = str_replace(',{id}', '', $rule); |
197 | 196 | } |
@@ -232,8 +231,7 @@ discard block |
||
232 | 231 | { |
233 | 232 | $error = $this->errorHandler->notFound('method'); |
234 | 233 | abort($error['status'], $error['message']); |
235 | - } |
|
236 | - else if ( ! in_array($permission, $this->skipLoginCheck)) |
|
234 | + } else if ( ! in_array($permission, $this->skipLoginCheck)) |
|
237 | 235 | { |
238 | 236 | \JWTAuth::parseToken()->authenticate(); |
239 | 237 | if ( ! in_array($permission, $this->skipPermissionCheck) && ! \Core::users()->can($permission, $this->model)) |
@@ -6,216 +6,216 @@ |
||
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 | - public function __construct() |
|
17 | - { |
|
18 | - \Session::set('timeZoneDiff', \Request::header('time-zone-diff') ?: 0); |
|
16 | + public function __construct() |
|
17 | + { |
|
18 | + \Session::set('timeZoneDiff', \Request::header('time-zone-diff') ?: 0); |
|
19 | 19 | |
20 | - $this->config = \CoreConfig::getConfig(); |
|
21 | - $this->model = property_exists($this, 'model') ? $this->model : false; |
|
22 | - $this->validationRules = property_exists($this, 'validationRules') ? $this->validationRules : false; |
|
23 | - $this->skipPermissionCheck = property_exists($this, 'skipPermissionCheck') ? $this->skipPermissionCheck : []; |
|
24 | - $this->skipLoginCheck = property_exists($this, 'skipLoginCheck') ? $this->skipLoginCheck : []; |
|
25 | - $this->relations = array_key_exists($this->model, $this->config['relations']) ? $this->config['relations'][$this->model] : false; |
|
26 | - $route = explode('@',\Route::currentRouteAction())[1]; |
|
27 | - $this->checkPermission(explode('_', snake_case($route))[1]); |
|
28 | - } |
|
20 | + $this->config = \CoreConfig::getConfig(); |
|
21 | + $this->model = property_exists($this, 'model') ? $this->model : false; |
|
22 | + $this->validationRules = property_exists($this, 'validationRules') ? $this->validationRules : false; |
|
23 | + $this->skipPermissionCheck = property_exists($this, 'skipPermissionCheck') ? $this->skipPermissionCheck : []; |
|
24 | + $this->skipLoginCheck = property_exists($this, 'skipLoginCheck') ? $this->skipLoginCheck : []; |
|
25 | + $this->relations = array_key_exists($this->model, $this->config['relations']) ? $this->config['relations'][$this->model] : false; |
|
26 | + $route = explode('@',\Route::currentRouteAction())[1]; |
|
27 | + $this->checkPermission(explode('_', snake_case($route))[1]); |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * Fetch all records with relations from model repository. |
|
32 | - * |
|
33 | - * @param string $sortBy |
|
34 | - * @param boolean $desc |
|
35 | - * @return \Illuminate\Http\Response |
|
36 | - */ |
|
37 | - public function getIndex() |
|
38 | - { |
|
39 | - if ($this->model) |
|
40 | - { |
|
41 | - $relations = $this->relations && $this->relations['all'] ? $this->relations['all'] : []; |
|
42 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->all($relations), 200); |
|
43 | - } |
|
44 | - } |
|
30 | + /** |
|
31 | + * Fetch all records with relations from model repository. |
|
32 | + * |
|
33 | + * @param string $sortBy |
|
34 | + * @param boolean $desc |
|
35 | + * @return \Illuminate\Http\Response |
|
36 | + */ |
|
37 | + public function getIndex() |
|
38 | + { |
|
39 | + if ($this->model) |
|
40 | + { |
|
41 | + $relations = $this->relations && $this->relations['all'] ? $this->relations['all'] : []; |
|
42 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->all($relations), 200); |
|
43 | + } |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * Fetch the single object with relations from model repository. |
|
48 | - * |
|
49 | - * @param integer $id |
|
50 | - * @return \Illuminate\Http\Response |
|
51 | - */ |
|
52 | - public function getFind($id) |
|
53 | - { |
|
54 | - if ($this->model) |
|
55 | - { |
|
56 | - $relations = $this->relations && $this->relations['find'] ? $this->relations['find'] : []; |
|
57 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->find($id, $relations), 200); |
|
58 | - } |
|
59 | - } |
|
46 | + /** |
|
47 | + * Fetch the single object with relations from model repository. |
|
48 | + * |
|
49 | + * @param integer $id |
|
50 | + * @return \Illuminate\Http\Response |
|
51 | + */ |
|
52 | + public function getFind($id) |
|
53 | + { |
|
54 | + if ($this->model) |
|
55 | + { |
|
56 | + $relations = $this->relations && $this->relations['find'] ? $this->relations['find'] : []; |
|
57 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->find($id, $relations), 200); |
|
58 | + } |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * Paginate all records with relations from model repository |
|
63 | - * that matche the given query. |
|
64 | - * |
|
65 | - * @param string $query |
|
66 | - * @param integer $perPage |
|
67 | - * @param string $sortBy |
|
68 | - * @param boolean $desc |
|
69 | - * @return \Illuminate\Http\Response |
|
70 | - */ |
|
71 | - public function getSearch($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
72 | - { |
|
73 | - if ($this->model) |
|
74 | - { |
|
75 | - $relations = $this->relations && $this->relations['paginate'] ? $this->relations['paginate'] : []; |
|
76 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->search($query, $perPage, $relations, $sortBy, $desc), 200); |
|
77 | - } |
|
78 | - } |
|
61 | + /** |
|
62 | + * Paginate all records with relations from model repository |
|
63 | + * that matche the given query. |
|
64 | + * |
|
65 | + * @param string $query |
|
66 | + * @param integer $perPage |
|
67 | + * @param string $sortBy |
|
68 | + * @param boolean $desc |
|
69 | + * @return \Illuminate\Http\Response |
|
70 | + */ |
|
71 | + public function getSearch($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
72 | + { |
|
73 | + if ($this->model) |
|
74 | + { |
|
75 | + $relations = $this->relations && $this->relations['paginate'] ? $this->relations['paginate'] : []; |
|
76 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->search($query, $perPage, $relations, $sortBy, $desc), 200); |
|
77 | + } |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * Fetch records from the storage based on the given |
|
82 | - * condition. |
|
83 | - * |
|
84 | - * @param \Illuminate\Http\Request $request |
|
85 | - * @param string $sortBy |
|
86 | - * @param boolean $desc |
|
87 | - * @return \Illuminate\Http\Response |
|
88 | - */ |
|
89 | - public function postFindby(Request $request, $sortBy = 'created_at', $desc = 1) |
|
90 | - { |
|
91 | - if ($this->model) |
|
92 | - { |
|
93 | - $relations = $this->relations && $this->relations['findBy'] ? $this->relations['findBy'] : []; |
|
94 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->findBy($request->all(), $relations, $sortBy, $desc), 200); |
|
95 | - } |
|
96 | - } |
|
80 | + /** |
|
81 | + * Fetch records from the storage based on the given |
|
82 | + * condition. |
|
83 | + * |
|
84 | + * @param \Illuminate\Http\Request $request |
|
85 | + * @param string $sortBy |
|
86 | + * @param boolean $desc |
|
87 | + * @return \Illuminate\Http\Response |
|
88 | + */ |
|
89 | + public function postFindby(Request $request, $sortBy = 'created_at', $desc = 1) |
|
90 | + { |
|
91 | + if ($this->model) |
|
92 | + { |
|
93 | + $relations = $this->relations && $this->relations['findBy'] ? $this->relations['findBy'] : []; |
|
94 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->findBy($request->all(), $relations, $sortBy, $desc), 200); |
|
95 | + } |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * Fetch the first record from the storage based on the given |
|
100 | - * condition. |
|
101 | - * |
|
102 | - * @param \Illuminate\Http\Request $request |
|
103 | - * @return \Illuminate\Http\Response |
|
104 | - */ |
|
105 | - public function postFirst(Request $request) |
|
106 | - { |
|
107 | - if ($this->model) |
|
108 | - { |
|
109 | - $relations = $this->relations && $this->relations['first'] ? $this->relations['first'] : []; |
|
110 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->first($request->all(), $relations), 200); |
|
111 | - } |
|
112 | - } |
|
98 | + /** |
|
99 | + * Fetch the first record from the storage based on the given |
|
100 | + * condition. |
|
101 | + * |
|
102 | + * @param \Illuminate\Http\Request $request |
|
103 | + * @return \Illuminate\Http\Response |
|
104 | + */ |
|
105 | + public function postFirst(Request $request) |
|
106 | + { |
|
107 | + if ($this->model) |
|
108 | + { |
|
109 | + $relations = $this->relations && $this->relations['first'] ? $this->relations['first'] : []; |
|
110 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->first($request->all(), $relations), 200); |
|
111 | + } |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * Paginate all records with relations from model repository. |
|
116 | - * |
|
117 | - * @param integer $perPage |
|
118 | - * @param string $sortBy |
|
119 | - * @param boolean $desc |
|
120 | - * @return \Illuminate\Http\Response |
|
121 | - */ |
|
122 | - public function getPaginate($perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
123 | - { |
|
124 | - if ($this->model) |
|
125 | - { |
|
126 | - $relations = $this->relations && $this->relations['paginate'] ? $this->relations['paginate'] : []; |
|
127 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginate($perPage, $relations, $sortBy, $desc), 200); |
|
128 | - } |
|
129 | - } |
|
114 | + /** |
|
115 | + * Paginate all records with relations from model repository. |
|
116 | + * |
|
117 | + * @param integer $perPage |
|
118 | + * @param string $sortBy |
|
119 | + * @param boolean $desc |
|
120 | + * @return \Illuminate\Http\Response |
|
121 | + */ |
|
122 | + public function getPaginate($perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
123 | + { |
|
124 | + if ($this->model) |
|
125 | + { |
|
126 | + $relations = $this->relations && $this->relations['paginate'] ? $this->relations['paginate'] : []; |
|
127 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginate($perPage, $relations, $sortBy, $desc), 200); |
|
128 | + } |
|
129 | + } |
|
130 | 130 | |
131 | - /** |
|
132 | - * Fetch all records with relations based on |
|
133 | - * the given condition from storage in pages. |
|
134 | - * |
|
135 | - * @param \Illuminate\Http\Request $request |
|
136 | - * @param integer $perPage |
|
137 | - * @param string $sortBy |
|
138 | - * @param boolean $desc |
|
139 | - * @return \Illuminate\Http\Response |
|
140 | - */ |
|
141 | - public function postPaginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
142 | - { |
|
143 | - if ($this->model) |
|
144 | - { |
|
145 | - $relations = $this->relations && $this->relations['paginateBy'] ? $this->relations['paginateBy'] : []; |
|
146 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginateBy($request->all(), $perPage, $relations, $sortBy, $desc), 200); |
|
147 | - } |
|
148 | - } |
|
131 | + /** |
|
132 | + * Fetch all records with relations based on |
|
133 | + * the given condition from storage in pages. |
|
134 | + * |
|
135 | + * @param \Illuminate\Http\Request $request |
|
136 | + * @param integer $perPage |
|
137 | + * @param string $sortBy |
|
138 | + * @param boolean $desc |
|
139 | + * @return \Illuminate\Http\Response |
|
140 | + */ |
|
141 | + public function postPaginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
142 | + { |
|
143 | + if ($this->model) |
|
144 | + { |
|
145 | + $relations = $this->relations && $this->relations['paginateBy'] ? $this->relations['paginateBy'] : []; |
|
146 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginateBy($request->all(), $perPage, $relations, $sortBy, $desc), 200); |
|
147 | + } |
|
148 | + } |
|
149 | 149 | |
150 | - /** |
|
151 | - * Save the given model to repository. |
|
152 | - * |
|
153 | - * @param \Illuminate\Http\Request $request |
|
154 | - * @return \Illuminate\Http\Response |
|
155 | - */ |
|
156 | - public function postSave(Request $request) |
|
157 | - { |
|
158 | - foreach ($this->validationRules as &$rule) |
|
159 | - { |
|
160 | - if (strpos($rule, 'exists') && ! strpos($rule, 'deleted_at,NULL')) |
|
161 | - { |
|
162 | - $rule .= ',deleted_at,NULL'; |
|
163 | - } |
|
150 | + /** |
|
151 | + * Save the given model to repository. |
|
152 | + * |
|
153 | + * @param \Illuminate\Http\Request $request |
|
154 | + * @return \Illuminate\Http\Response |
|
155 | + */ |
|
156 | + public function postSave(Request $request) |
|
157 | + { |
|
158 | + foreach ($this->validationRules as &$rule) |
|
159 | + { |
|
160 | + if (strpos($rule, 'exists') && ! strpos($rule, 'deleted_at,NULL')) |
|
161 | + { |
|
162 | + $rule .= ',deleted_at,NULL'; |
|
163 | + } |
|
164 | 164 | |
165 | - if ($request->has('id')) |
|
166 | - { |
|
167 | - $rule = str_replace('{id}', $request->get('id'), $rule); |
|
168 | - } |
|
169 | - else |
|
170 | - { |
|
171 | - $rule = str_replace(',{id}', '', $rule); |
|
172 | - } |
|
173 | - } |
|
165 | + if ($request->has('id')) |
|
166 | + { |
|
167 | + $rule = str_replace('{id}', $request->get('id'), $rule); |
|
168 | + } |
|
169 | + else |
|
170 | + { |
|
171 | + $rule = str_replace(',{id}', '', $rule); |
|
172 | + } |
|
173 | + } |
|
174 | 174 | |
175 | - $this->validate($request, $this->validationRules); |
|
175 | + $this->validate($request, $this->validationRules); |
|
176 | 176 | |
177 | - if ($this->model) |
|
178 | - { |
|
179 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->save($request->all()), 200); |
|
180 | - } |
|
181 | - } |
|
177 | + if ($this->model) |
|
178 | + { |
|
179 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->save($request->all()), 200); |
|
180 | + } |
|
181 | + } |
|
182 | 182 | |
183 | - /** |
|
184 | - * Delete by the given id from model repository. |
|
185 | - * |
|
186 | - * @param integer $id |
|
187 | - * @return \Illuminate\Http\Response |
|
188 | - */ |
|
189 | - public function getDelete($id) |
|
190 | - { |
|
191 | - if ($this->model) |
|
192 | - { |
|
193 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->delete($id), 200); |
|
194 | - } |
|
195 | - } |
|
183 | + /** |
|
184 | + * Delete by the given id from model repository. |
|
185 | + * |
|
186 | + * @param integer $id |
|
187 | + * @return \Illuminate\Http\Response |
|
188 | + */ |
|
189 | + public function getDelete($id) |
|
190 | + { |
|
191 | + if ($this->model) |
|
192 | + { |
|
193 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->delete($id), 200); |
|
194 | + } |
|
195 | + } |
|
196 | 196 | |
197 | - /** |
|
198 | - * Check if the logged in user can do the given permission. |
|
199 | - * |
|
200 | - * @param string $permission |
|
201 | - * @return void |
|
202 | - */ |
|
203 | - private function checkPermission($permission) |
|
204 | - { |
|
205 | - $permission = $permission !== 'index' ? $permission : 'list'; |
|
206 | - if ($permission == 'method') |
|
207 | - { |
|
208 | - $error = \ErrorHandler::notFound('method'); |
|
209 | - abort($error['status'], $error['message']); |
|
210 | - } |
|
211 | - else if ( ! in_array($permission, $this->skipLoginCheck)) |
|
212 | - { |
|
213 | - \JWTAuth::parseToken()->authenticate(); |
|
214 | - if ( ! in_array($permission, $this->skipPermissionCheck) && ! \Core::users()->can($permission, $this->model)) |
|
215 | - { |
|
216 | - $error = \ErrorHandler::noPermissions(); |
|
217 | - abort($error['status'], $error['message']); |
|
218 | - } |
|
219 | - } |
|
220 | - } |
|
197 | + /** |
|
198 | + * Check if the logged in user can do the given permission. |
|
199 | + * |
|
200 | + * @param string $permission |
|
201 | + * @return void |
|
202 | + */ |
|
203 | + private function checkPermission($permission) |
|
204 | + { |
|
205 | + $permission = $permission !== 'index' ? $permission : 'list'; |
|
206 | + if ($permission == 'method') |
|
207 | + { |
|
208 | + $error = \ErrorHandler::notFound('method'); |
|
209 | + abort($error['status'], $error['message']); |
|
210 | + } |
|
211 | + else if ( ! in_array($permission, $this->skipLoginCheck)) |
|
212 | + { |
|
213 | + \JWTAuth::parseToken()->authenticate(); |
|
214 | + if ( ! in_array($permission, $this->skipPermissionCheck) && ! \Core::users()->can($permission, $this->model)) |
|
215 | + { |
|
216 | + $error = \ErrorHandler::noPermissions(); |
|
217 | + abort($error['status'], $error['message']); |
|
218 | + } |
|
219 | + } |
|
220 | + } |
|
221 | 221 | } |
@@ -7,7 +7,7 @@ discard block |
||
7 | 7 | * |
8 | 8 | * @param array $relations |
9 | 9 | * @param array $sortBy |
10 | - * @param array $desc |
|
10 | + * @param integer $desc |
|
11 | 11 | * @param array $columns |
12 | 12 | * @return collection |
13 | 13 | */ |
@@ -21,7 +21,7 @@ discard block |
||
21 | 21 | * @param integer $perPage |
22 | 22 | * @param array $relations |
23 | 23 | * @param array $sortBy |
24 | - * @param array $desc |
|
24 | + * @param integer $desc |
|
25 | 25 | * @param array $columns |
26 | 26 | * @return collection |
27 | 27 | */ |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | * @param integer $perPage |
34 | 34 | * @param array $relations |
35 | 35 | * @param array $sortBy |
36 | - * @param array $desc |
|
36 | + * @param integer $desc |
|
37 | 37 | * @param array $columns |
38 | 38 | * @return collection |
39 | 39 | */ |
@@ -47,7 +47,7 @@ discard block |
||
47 | 47 | * @param integer $perPage |
48 | 48 | * @param array $relations |
49 | 49 | * @param array $sortBy |
50 | - * @param array $desc |
|
50 | + * @param integer $desc |
|
51 | 51 | * @param array $columns |
52 | 52 | * @return collection |
53 | 53 | */ |
@@ -109,7 +109,7 @@ discard block |
||
109 | 109 | * @param array $conditions array of conditions |
110 | 110 | * @param array $relations |
111 | 111 | * @param array $sortBy |
112 | - * @param array $desc |
|
112 | + * @param integer $desc |
|
113 | 113 | * @param array $columns |
114 | 114 | * @return collection |
115 | 115 | */ |
@@ -120,7 +120,6 @@ discard block |
||
120 | 120 | * condition. |
121 | 121 | * |
122 | 122 | * @param array $conditions array of conditions |
123 | - * @param [type] $value condition value |
|
124 | 123 | * @param array $relations |
125 | 124 | * @param array $columns |
126 | 125 | * @return object |
@@ -3,127 +3,127 @@ |
||
3 | 3 | interface RepositoryInterface |
4 | 4 | { |
5 | 5 | /** |
6 | - * Fetch all records with relations from the storage. |
|
7 | - * |
|
8 | - * @param array $relations |
|
9 | - * @param array $sortBy |
|
10 | - * @param array $desc |
|
11 | - * @param array $columns |
|
12 | - * @return collection |
|
13 | - */ |
|
6 | + * Fetch all records with relations from the storage. |
|
7 | + * |
|
8 | + * @param array $relations |
|
9 | + * @param array $sortBy |
|
10 | + * @param array $desc |
|
11 | + * @param array $columns |
|
12 | + * @return collection |
|
13 | + */ |
|
14 | 14 | public function all($relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); |
15 | 15 | |
16 | - /** |
|
17 | - * Fetch all records with relations from storage in pages |
|
18 | - * that matche the given query. |
|
19 | - * |
|
20 | - * @param string $query |
|
21 | - * @param integer $perPage |
|
22 | - * @param array $relations |
|
23 | - * @param array $sortBy |
|
24 | - * @param array $desc |
|
25 | - * @param array $columns |
|
26 | - * @return collection |
|
27 | - */ |
|
28 | - public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); |
|
16 | + /** |
|
17 | + * Fetch all records with relations from storage in pages |
|
18 | + * that matche the given query. |
|
19 | + * |
|
20 | + * @param string $query |
|
21 | + * @param integer $perPage |
|
22 | + * @param array $relations |
|
23 | + * @param array $sortBy |
|
24 | + * @param array $desc |
|
25 | + * @param array $columns |
|
26 | + * @return collection |
|
27 | + */ |
|
28 | + public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); |
|
29 | 29 | |
30 | 30 | /** |
31 | - * Fetch all records with relations from storage in pages. |
|
32 | - * |
|
33 | - * @param integer $perPage |
|
34 | - * @param array $relations |
|
35 | - * @param array $sortBy |
|
36 | - * @param array $desc |
|
37 | - * @param array $columns |
|
38 | - * @return collection |
|
39 | - */ |
|
40 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); |
|
31 | + * Fetch all records with relations from storage in pages. |
|
32 | + * |
|
33 | + * @param integer $perPage |
|
34 | + * @param array $relations |
|
35 | + * @param array $sortBy |
|
36 | + * @param array $desc |
|
37 | + * @param array $columns |
|
38 | + * @return collection |
|
39 | + */ |
|
40 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); |
|
41 | 41 | |
42 | 42 | /** |
43 | - * Fetch all records with relations based on |
|
44 | - * the given condition from storage in pages. |
|
45 | - * |
|
46 | - * @param array $conditions array of conditions |
|
47 | - * @param integer $perPage |
|
48 | - * @param array $relations |
|
49 | - * @param array $sortBy |
|
50 | - * @param array $desc |
|
51 | - * @param array $columns |
|
52 | - * @return collection |
|
53 | - */ |
|
54 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); |
|
43 | + * Fetch all records with relations based on |
|
44 | + * the given condition from storage in pages. |
|
45 | + * |
|
46 | + * @param array $conditions array of conditions |
|
47 | + * @param integer $perPage |
|
48 | + * @param array $relations |
|
49 | + * @param array $sortBy |
|
50 | + * @param array $desc |
|
51 | + * @param array $columns |
|
52 | + * @return collection |
|
53 | + */ |
|
54 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); |
|
55 | 55 | |
56 | - /** |
|
57 | - * Save the given model/models to the storage. |
|
58 | - * |
|
59 | - * @param array $data |
|
60 | - * @param boolean $saveLog |
|
61 | - * @return object |
|
62 | - */ |
|
63 | - public function save(array $data, $saveLog = true); |
|
56 | + /** |
|
57 | + * Save the given model/models to the storage. |
|
58 | + * |
|
59 | + * @param array $data |
|
60 | + * @param boolean $saveLog |
|
61 | + * @return object |
|
62 | + */ |
|
63 | + public function save(array $data, $saveLog = true); |
|
64 | 64 | |
65 | - /** |
|
66 | - * Insert multiple records to the storage. |
|
67 | - * |
|
68 | - * @param array $data |
|
69 | - * @return object |
|
70 | - */ |
|
71 | - public function saveMany(array $data); |
|
65 | + /** |
|
66 | + * Insert multiple records to the storage. |
|
67 | + * |
|
68 | + * @param array $data |
|
69 | + * @return object |
|
70 | + */ |
|
71 | + public function saveMany(array $data); |
|
72 | 72 | |
73 | 73 | /** |
74 | - * Update record in the storage based on the given |
|
75 | - * condition. |
|
76 | - * |
|
77 | - * @param [type] $value condition value |
|
78 | - * @param array $data |
|
79 | - * @param string $attribute condition column name |
|
80 | - * @return integer affected rows |
|
81 | - */ |
|
82 | - public function update($value, array $data, $attribute = 'id'); |
|
74 | + * Update record in the storage based on the given |
|
75 | + * condition. |
|
76 | + * |
|
77 | + * @param [type] $value condition value |
|
78 | + * @param array $data |
|
79 | + * @param string $attribute condition column name |
|
80 | + * @return integer affected rows |
|
81 | + */ |
|
82 | + public function update($value, array $data, $attribute = 'id'); |
|
83 | 83 | |
84 | 84 | /** |
85 | - * Delete record from the storage based on the given |
|
86 | - * condition. |
|
87 | - * |
|
88 | - * @param [type] $value condition value |
|
89 | - * @param string $attribute condition column name |
|
90 | - * @return integer affected rows |
|
91 | - */ |
|
92 | - public function delete($value, $attribute = 'id'); |
|
85 | + * Delete record from the storage based on the given |
|
86 | + * condition. |
|
87 | + * |
|
88 | + * @param [type] $value condition value |
|
89 | + * @param string $attribute condition column name |
|
90 | + * @return integer affected rows |
|
91 | + */ |
|
92 | + public function delete($value, $attribute = 'id'); |
|
93 | 93 | |
94 | 94 | /** |
95 | - * Fetch records from the storage based on the given |
|
96 | - * id. |
|
97 | - * |
|
98 | - * @param integer $id |
|
99 | - * @param array $relations |
|
100 | - * @param array $columns |
|
101 | - * @return object |
|
102 | - */ |
|
103 | - public function find($id, $relations = [], $columns = array('*')); |
|
95 | + * Fetch records from the storage based on the given |
|
96 | + * id. |
|
97 | + * |
|
98 | + * @param integer $id |
|
99 | + * @param array $relations |
|
100 | + * @param array $columns |
|
101 | + * @return object |
|
102 | + */ |
|
103 | + public function find($id, $relations = [], $columns = array('*')); |
|
104 | 104 | |
105 | 105 | /** |
106 | - * Fetch records from the storage based on the given |
|
107 | - * condition. |
|
108 | - * |
|
109 | - * @param array $conditions array of conditions |
|
110 | - * @param array $relations |
|
111 | - * @param array $sortBy |
|
112 | - * @param array $desc |
|
113 | - * @param array $columns |
|
114 | - * @return collection |
|
115 | - */ |
|
116 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); |
|
106 | + * Fetch records from the storage based on the given |
|
107 | + * condition. |
|
108 | + * |
|
109 | + * @param array $conditions array of conditions |
|
110 | + * @param array $relations |
|
111 | + * @param array $sortBy |
|
112 | + * @param array $desc |
|
113 | + * @param array $columns |
|
114 | + * @return collection |
|
115 | + */ |
|
116 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); |
|
117 | 117 | |
118 | - /** |
|
119 | - * Fetch the first record fro the storage based on the given |
|
120 | - * condition. |
|
121 | - * |
|
122 | - * @param array $conditions array of conditions |
|
123 | - * @param [type] $value condition value |
|
124 | - * @param array $relations |
|
125 | - * @param array $columns |
|
126 | - * @return object |
|
127 | - */ |
|
128 | - public function first($conditions, $relations = [], $columns = array('*')); |
|
118 | + /** |
|
119 | + * Fetch the first record fro the storage based on the given |
|
120 | + * condition. |
|
121 | + * |
|
122 | + * @param array $conditions array of conditions |
|
123 | + * @param [type] $value condition value |
|
124 | + * @param array $relations |
|
125 | + * @param array $columns |
|
126 | + * @return object |
|
127 | + */ |
|
128 | + public function first($conditions, $relations = [], $columns = array('*')); |
|
129 | 129 | } |
130 | 130 | \ No newline at end of file |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | use Illuminate\Database\Eloquent\Model; |
4 | 4 | use Illuminate\Database\Eloquent\SoftDeletes; |
5 | 5 | |
6 | -class AclGroup extends Model{ |
|
6 | +class AclGroup extends Model { |
|
7 | 7 | |
8 | 8 | use SoftDeletes; |
9 | 9 | protected $table = 'groups'; |
@@ -29,12 +29,12 @@ discard block |
||
29 | 29 | |
30 | 30 | public function users() |
31 | 31 | { |
32 | - return $this->belongsToMany('\App\Modules\Acl\AclUser','users_groups','group_id','user_id')->whereNull('users_groups.deleted_at')->withTimestamps(); |
|
32 | + return $this->belongsToMany('\App\Modules\Acl\AclUser', 'users_groups', 'group_id', 'user_id')->whereNull('users_groups.deleted_at')->withTimestamps(); |
|
33 | 33 | } |
34 | 34 | |
35 | 35 | public function permissions() |
36 | 36 | { |
37 | - return $this->belongsToMany('\App\Modules\Acl\AclPermission','groups_permissions','group_id','permission_id')->whereNull('groups_permissions.deleted_at')->withTimestamps(); |
|
37 | + return $this->belongsToMany('\App\Modules\Acl\AclPermission', 'groups_permissions', 'group_id', 'permission_id')->whereNull('groups_permissions.deleted_at')->withTimestamps(); |
|
38 | 38 | } |
39 | 39 | |
40 | 40 | public static function boot() |
@@ -5,41 +5,41 @@ |
||
5 | 5 | |
6 | 6 | class AclGroup extends Model{ |
7 | 7 | |
8 | - use SoftDeletes; |
|
9 | - protected $table = 'groups'; |
|
10 | - protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
11 | - protected $hidden = ['deleted_at']; |
|
12 | - protected $guarded = ['id']; |
|
13 | - protected $fillable = ['name']; |
|
14 | - |
|
15 | - public function getCreatedAtAttribute($value) |
|
16 | - { |
|
17 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
18 | - } |
|
19 | - |
|
20 | - public function getUpdatedAtAttribute($value) |
|
21 | - { |
|
22 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
23 | - } |
|
24 | - |
|
25 | - public function getDeletedAtAttribute($value) |
|
26 | - { |
|
27 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
28 | - } |
|
29 | - |
|
30 | - public function users() |
|
31 | - { |
|
32 | - return $this->belongsToMany('\App\Modules\Acl\AclUser','users_groups','group_id','user_id')->whereNull('users_groups.deleted_at')->withTimestamps(); |
|
33 | - } |
|
34 | - |
|
35 | - public function permissions() |
|
36 | - { |
|
37 | - return $this->belongsToMany('\App\Modules\Acl\AclPermission','groups_permissions','group_id','permission_id')->whereNull('groups_permissions.deleted_at')->withTimestamps(); |
|
38 | - } |
|
39 | - |
|
40 | - public static function boot() |
|
41 | - { |
|
42 | - parent::boot(); |
|
43 | - parent::observe(\App::make('App\Modules\Acl\ModelObservers\AclGroupObserver')); |
|
44 | - } |
|
8 | + use SoftDeletes; |
|
9 | + protected $table = 'groups'; |
|
10 | + protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
11 | + protected $hidden = ['deleted_at']; |
|
12 | + protected $guarded = ['id']; |
|
13 | + protected $fillable = ['name']; |
|
14 | + |
|
15 | + public function getCreatedAtAttribute($value) |
|
16 | + { |
|
17 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
18 | + } |
|
19 | + |
|
20 | + public function getUpdatedAtAttribute($value) |
|
21 | + { |
|
22 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
23 | + } |
|
24 | + |
|
25 | + public function getDeletedAtAttribute($value) |
|
26 | + { |
|
27 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
28 | + } |
|
29 | + |
|
30 | + public function users() |
|
31 | + { |
|
32 | + return $this->belongsToMany('\App\Modules\Acl\AclUser','users_groups','group_id','user_id')->whereNull('users_groups.deleted_at')->withTimestamps(); |
|
33 | + } |
|
34 | + |
|
35 | + public function permissions() |
|
36 | + { |
|
37 | + return $this->belongsToMany('\App\Modules\Acl\AclPermission','groups_permissions','group_id','permission_id')->whereNull('groups_permissions.deleted_at')->withTimestamps(); |
|
38 | + } |
|
39 | + |
|
40 | + public static function boot() |
|
41 | + { |
|
42 | + parent::boot(); |
|
43 | + parent::observe(\App::make('App\Modules\Acl\ModelObservers\AclGroupObserver')); |
|
44 | + } |
|
45 | 45 | } |
@@ -61,6 +61,6 @@ |
||
61 | 61 | $user->groups()->attach($group_ids); |
62 | 62 | }); |
63 | 63 | |
64 | - return \Core::users()->find($user_id); |
|
64 | + return \Core::users()->find($user_id); |
|
65 | 65 | } |
66 | 66 | } |
@@ -23,11 +23,11 @@ discard block |
||
23 | 23 | * @param boolean $user |
24 | 24 | * @return boolean |
25 | 25 | */ |
26 | - public function can($nameOfPermission, $model, $user = false ) |
|
26 | + public function can($nameOfPermission, $model, $user = false) |
|
27 | 27 | { |
28 | 28 | $user = $user ?: \Auth::user(); |
29 | 29 | $permissions = []; |
30 | - \Core::users()->find($user->id, ['groups.permissions'])->groups->lists('permissions')->each(function ($permission) use (&$permissions, $model){ |
|
30 | + \Core::users()->find($user->id, ['groups.permissions'])->groups->lists('permissions')->each(function($permission) use (&$permissions, $model){ |
|
31 | 31 | $permissions = array_merge($permissions, $permission->where('model', $model)->lists('name')->toArray()); |
32 | 32 | }); |
33 | 33 | |
@@ -55,7 +55,7 @@ discard block |
||
55 | 55 | */ |
56 | 56 | public function assignGroups($user_id, $group_ids) |
57 | 57 | { |
58 | - \DB::transaction(function () use ($user_id, $group_ids) { |
|
58 | + \DB::transaction(function() use ($user_id, $group_ids) { |
|
59 | 59 | $user = \Core::users()->find($user_id); |
60 | 60 | $user->groups()->detach(); |
61 | 61 | $user->groups()->attach($group_ids); |
@@ -29,6 +29,6 @@ |
||
29 | 29 | $group->permissions()->attach($permission_ids); |
30 | 30 | }); |
31 | 31 | |
32 | - return \Core::groups()->find($group_id); |
|
32 | + return \Core::groups()->find($group_id); |
|
33 | 33 | } |
34 | 34 | } |
@@ -23,7 +23,7 @@ |
||
23 | 23 | */ |
24 | 24 | public function assignPermissions($group_id, $permission_ids) |
25 | 25 | { |
26 | - \DB::transaction(function () use ($group_id, $permission_ids) { |
|
26 | + \DB::transaction(function() use ($group_id, $permission_ids) { |
|
27 | 27 | $group = \Core::groups()->find($group_id); |
28 | 28 | $group->permissions()->detach(); |
29 | 29 | $group->permissions()->attach($permission_ids); |
@@ -8,35 +8,35 @@ |
||
8 | 8 | |
9 | 9 | class GroupsController extends BaseApiController |
10 | 10 | { |
11 | - /** |
|
12 | - * The name of the model that is used by the base api controller |
|
13 | - * to preform actions like (add, edit ... etc). |
|
14 | - * @var string |
|
15 | - */ |
|
16 | - protected $model = 'groups'; |
|
11 | + /** |
|
12 | + * The name of the model that is used by the base api controller |
|
13 | + * to preform actions like (add, edit ... etc). |
|
14 | + * @var string |
|
15 | + */ |
|
16 | + protected $model = 'groups'; |
|
17 | 17 | |
18 | - /** |
|
19 | - * The validations rules used by the base api controller |
|
20 | - * to check before add. |
|
21 | - * @var array |
|
22 | - */ |
|
23 | - protected $validationRules = [ |
|
24 | - 'name' => 'required|string|max:100|unique:groups,name,{id}' |
|
25 | - ]; |
|
18 | + /** |
|
19 | + * The validations rules used by the base api controller |
|
20 | + * to check before add. |
|
21 | + * @var array |
|
22 | + */ |
|
23 | + protected $validationRules = [ |
|
24 | + 'name' => 'required|string|max:100|unique:groups,name,{id}' |
|
25 | + ]; |
|
26 | 26 | |
27 | - /** |
|
28 | - * Handle an assign permissions to group request. |
|
29 | - * |
|
30 | - * @param \Illuminate\Http\Request $request |
|
31 | - * @return \Illuminate\Http\Response |
|
32 | - */ |
|
33 | - public function postAssignpermissions(Request $request) |
|
34 | - { |
|
35 | - $this->validate($request, [ |
|
36 | - 'permission_ids' => 'required|exists:permissions,id', |
|
37 | - 'group_id' => 'required|exists:groups,id' |
|
38 | - ]); |
|
27 | + /** |
|
28 | + * Handle an assign permissions to group request. |
|
29 | + * |
|
30 | + * @param \Illuminate\Http\Request $request |
|
31 | + * @return \Illuminate\Http\Response |
|
32 | + */ |
|
33 | + public function postAssignpermissions(Request $request) |
|
34 | + { |
|
35 | + $this->validate($request, [ |
|
36 | + 'permission_ids' => 'required|exists:permissions,id', |
|
37 | + 'group_id' => 'required|exists:groups,id' |
|
38 | + ]); |
|
39 | 39 | |
40 | - return \Response::json(\Core::groups()->assignPermissions($request->get('group_id'), $request->get('permission_ids')), 200); |
|
41 | - } |
|
40 | + return \Response::json(\Core::groups()->assignPermissions($request->get('group_id'), $request->get('permission_ids')), 200); |
|
41 | + } |
|
42 | 42 | } |
@@ -13,14 +13,14 @@ |
||
13 | 13 | * to preform actions like (add, edit ... etc). |
14 | 14 | * @var string |
15 | 15 | */ |
16 | - protected $model = 'groups'; |
|
16 | + protected $model = 'groups'; |
|
17 | 17 | |
18 | 18 | /** |
19 | 19 | * The validations rules used by the base api controller |
20 | 20 | * to check before add. |
21 | 21 | * @var array |
22 | 22 | */ |
23 | - protected $validationRules = [ |
|
23 | + protected $validationRules = [ |
|
24 | 24 | 'name' => 'required|string|max:100|unique:groups,name,{id}' |
25 | 25 | ]; |
26 | 26 |
@@ -7,10 +7,10 @@ |
||
7 | 7 | |
8 | 8 | class PermissionsController 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 = 'permissions'; |
|
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 = 'permissions'; |
|
16 | 16 | } |