@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | * |
33 | 33 | * @param array $relations |
34 | 34 | * @param string $sortBy |
35 | - * @param boolean $desc |
|
35 | + * @param integer $desc |
|
36 | 36 | * @param array $columns |
37 | 37 | * @return collection |
38 | 38 | */ |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | * @param integer $perPage |
51 | 51 | * @param array $relations |
52 | 52 | * @param string $sortBy |
53 | - * @param boolean $desc |
|
53 | + * @param integer $desc |
|
54 | 54 | * @param array $columns |
55 | 55 | * @return collection |
56 | 56 | */ |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | * @param integer $perPage |
140 | 140 | * @param array $relations |
141 | 141 | * @param string $sortBy |
142 | - * @param boolean $desc |
|
142 | + * @param integer $desc |
|
143 | 143 | * @param array $columns |
144 | 144 | * @return collection |
145 | 145 | */ |
@@ -157,7 +157,7 @@ discard block |
||
157 | 157 | * @param integer $perPage |
158 | 158 | * @param array $relations |
159 | 159 | * @param string $sortBy |
160 | - * @param boolean $desc |
|
160 | + * @param integer $desc |
|
161 | 161 | * @param array $columns |
162 | 162 | * @return collection |
163 | 163 | */ |
@@ -174,7 +174,7 @@ discard block |
||
174 | 174 | * |
175 | 175 | * @param array $data |
176 | 176 | * @param boolean $saveLog |
177 | - * @return object |
|
177 | + * @return boolean |
|
178 | 178 | */ |
179 | 179 | public function save(array $data, $saveLog = true) |
180 | 180 | { |
@@ -469,7 +469,7 @@ discard block |
||
469 | 469 | * @param array $conditions array of conditions |
470 | 470 | * @param array $relations |
471 | 471 | * @param string $sortBy |
472 | - * @param boolean $desc |
|
472 | + * @param integer $desc |
|
473 | 473 | * @param array $columns |
474 | 474 | * @return collection |
475 | 475 | */ |
@@ -4,534 +4,534 @@ |
||
4 | 4 | |
5 | 5 | abstract class AbstractRepository implements RepositoryInterface |
6 | 6 | { |
7 | - /** |
|
8 | - * The model implementation. |
|
9 | - * |
|
10 | - * @var model |
|
11 | - */ |
|
12 | - public $model; |
|
7 | + /** |
|
8 | + * The model implementation. |
|
9 | + * |
|
10 | + * @var model |
|
11 | + */ |
|
12 | + public $model; |
|
13 | 13 | |
14 | - /** |
|
15 | - * The config implementation. |
|
16 | - * |
|
17 | - * @var config |
|
18 | - */ |
|
19 | - protected $config; |
|
14 | + /** |
|
15 | + * The config implementation. |
|
16 | + * |
|
17 | + * @var config |
|
18 | + */ |
|
19 | + protected $config; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Create new AbstractRepository instance. |
|
23 | - */ |
|
24 | - public function __construct() |
|
25 | - { |
|
26 | - $this->config = \CoreConfig::getConfig(); |
|
27 | - $this->model = \App::make($this->getModel()); |
|
28 | - } |
|
21 | + /** |
|
22 | + * Create new AbstractRepository instance. |
|
23 | + */ |
|
24 | + public function __construct() |
|
25 | + { |
|
26 | + $this->config = \CoreConfig::getConfig(); |
|
27 | + $this->model = \App::make($this->getModel()); |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * Fetch all records with relations from the storage. |
|
32 | - * |
|
33 | - * @param array $relations |
|
34 | - * @param string $sortBy |
|
35 | - * @param boolean $desc |
|
36 | - * @param array $columns |
|
37 | - * @return collection |
|
38 | - */ |
|
39 | - public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
40 | - { |
|
41 | - $sort = $desc ? 'desc' : 'asc'; |
|
42 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->get($columns); |
|
43 | - } |
|
30 | + /** |
|
31 | + * Fetch all records with relations from the storage. |
|
32 | + * |
|
33 | + * @param array $relations |
|
34 | + * @param string $sortBy |
|
35 | + * @param boolean $desc |
|
36 | + * @param array $columns |
|
37 | + * @return collection |
|
38 | + */ |
|
39 | + public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
40 | + { |
|
41 | + $sort = $desc ? 'desc' : 'asc'; |
|
42 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->get($columns); |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * Fetch all records with relations from storage in pages |
|
47 | - * that matche the given query. |
|
48 | - * |
|
49 | - * @param string $query |
|
50 | - * @param integer $perPage |
|
51 | - * @param array $relations |
|
52 | - * @param string $sortBy |
|
53 | - * @param boolean $desc |
|
54 | - * @param array $columns |
|
55 | - * @return collection |
|
56 | - */ |
|
57 | - public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
58 | - { |
|
59 | - $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
60 | - $conditionColumns = $this->model->searchable; |
|
61 | - $sort = $desc ? 'desc' : 'asc'; |
|
45 | + /** |
|
46 | + * Fetch all records with relations from storage in pages |
|
47 | + * that matche the given query. |
|
48 | + * |
|
49 | + * @param string $query |
|
50 | + * @param integer $perPage |
|
51 | + * @param array $relations |
|
52 | + * @param string $sortBy |
|
53 | + * @param boolean $desc |
|
54 | + * @param array $columns |
|
55 | + * @return collection |
|
56 | + */ |
|
57 | + public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
58 | + { |
|
59 | + $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
60 | + $conditionColumns = $this->model->searchable; |
|
61 | + $sort = $desc ? 'desc' : 'asc'; |
|
62 | 62 | |
63 | - /** |
|
64 | - * Construct the select conditions for the model. |
|
65 | - */ |
|
66 | - $model->where(function ($q) use ($query, $conditionColumns, $relations){ |
|
63 | + /** |
|
64 | + * Construct the select conditions for the model. |
|
65 | + */ |
|
66 | + $model->where(function ($q) use ($query, $conditionColumns, $relations){ |
|
67 | 67 | |
68 | - if (count($conditionColumns)) |
|
69 | - { |
|
70 | - /** |
|
71 | - * Use the first element in the model columns to construct the first condition. |
|
72 | - */ |
|
73 | - $q->where(\DB::raw('LOWER(' . array_shift($conditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
74 | - } |
|
68 | + if (count($conditionColumns)) |
|
69 | + { |
|
70 | + /** |
|
71 | + * Use the first element in the model columns to construct the first condition. |
|
72 | + */ |
|
73 | + $q->where(\DB::raw('LOWER(' . array_shift($conditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * Loop through the rest of the columns to construct or where conditions. |
|
78 | - */ |
|
79 | - foreach ($conditionColumns as $column) |
|
80 | - { |
|
81 | - $q->orWhere(\DB::raw('LOWER(' . $column . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
82 | - } |
|
76 | + /** |
|
77 | + * Loop through the rest of the columns to construct or where conditions. |
|
78 | + */ |
|
79 | + foreach ($conditionColumns as $column) |
|
80 | + { |
|
81 | + $q->orWhere(\DB::raw('LOWER(' . $column . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * Loop through the model relations. |
|
86 | - */ |
|
87 | - foreach ($relations as $relation) |
|
88 | - { |
|
89 | - /** |
|
90 | - * Remove the sub relation if exists. |
|
91 | - */ |
|
92 | - $relation = explode('.', $relation)[0]; |
|
84 | + /** |
|
85 | + * Loop through the model relations. |
|
86 | + */ |
|
87 | + foreach ($relations as $relation) |
|
88 | + { |
|
89 | + /** |
|
90 | + * Remove the sub relation if exists. |
|
91 | + */ |
|
92 | + $relation = explode('.', $relation)[0]; |
|
93 | 93 | |
94 | - /** |
|
95 | - * Try to fetch the relation repository from the core. |
|
96 | - */ |
|
97 | - if (\Core::$relation()) |
|
98 | - { |
|
99 | - /** |
|
100 | - * Construct the relation condition. |
|
101 | - */ |
|
102 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
94 | + /** |
|
95 | + * Try to fetch the relation repository from the core. |
|
96 | + */ |
|
97 | + if (\Core::$relation()) |
|
98 | + { |
|
99 | + /** |
|
100 | + * Construct the relation condition. |
|
101 | + */ |
|
102 | + $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
103 | 103 | |
104 | - $subModel->where(function ($q) use ($query, $relation){ |
|
104 | + $subModel->where(function ($q) use ($query, $relation){ |
|
105 | 105 | |
106 | - /** |
|
107 | - * Get columns of the relation. |
|
108 | - */ |
|
109 | - $subConditionColumns = \Core::$relation()->model->searchable; |
|
106 | + /** |
|
107 | + * Get columns of the relation. |
|
108 | + */ |
|
109 | + $subConditionColumns = \Core::$relation()->model->searchable; |
|
110 | 110 | |
111 | - if (count($subConditionColumns)) |
|
112 | - { |
|
113 | - /** |
|
114 | - * Use the first element in the relation model columns to construct the first condition. |
|
115 | - */ |
|
116 | - $q->where(\DB::raw('LOWER(' . array_shift($subConditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
117 | - } |
|
111 | + if (count($subConditionColumns)) |
|
112 | + { |
|
113 | + /** |
|
114 | + * Use the first element in the relation model columns to construct the first condition. |
|
115 | + */ |
|
116 | + $q->where(\DB::raw('LOWER(' . array_shift($subConditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
117 | + } |
|
118 | 118 | |
119 | - /** |
|
120 | - * Loop through the rest of the columns to construct or where conditions. |
|
121 | - */ |
|
122 | - foreach ($subConditionColumns as $subConditionColumn) |
|
123 | - { |
|
124 | - $q->orWhere(\DB::raw('LOWER(' . $subConditionColumn . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
125 | - } |
|
126 | - }); |
|
119 | + /** |
|
120 | + * Loop through the rest of the columns to construct or where conditions. |
|
121 | + */ |
|
122 | + foreach ($subConditionColumns as $subConditionColumn) |
|
123 | + { |
|
124 | + $q->orWhere(\DB::raw('LOWER(' . $subConditionColumn . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
125 | + } |
|
126 | + }); |
|
127 | 127 | |
128 | - }); |
|
129 | - } |
|
130 | - } |
|
131 | - }); |
|
128 | + }); |
|
129 | + } |
|
130 | + } |
|
131 | + }); |
|
132 | 132 | |
133 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
134 | - } |
|
133 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
134 | + } |
|
135 | 135 | |
136 | - /** |
|
137 | - * Fetch all records with relations from storage in pages. |
|
138 | - * |
|
139 | - * @param integer $perPage |
|
140 | - * @param array $relations |
|
141 | - * @param string $sortBy |
|
142 | - * @param boolean $desc |
|
143 | - * @param array $columns |
|
144 | - * @return collection |
|
145 | - */ |
|
146 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
147 | - { |
|
148 | - $sort = $desc ? 'desc' : 'asc'; |
|
149 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
150 | - } |
|
136 | + /** |
|
137 | + * Fetch all records with relations from storage in pages. |
|
138 | + * |
|
139 | + * @param integer $perPage |
|
140 | + * @param array $relations |
|
141 | + * @param string $sortBy |
|
142 | + * @param boolean $desc |
|
143 | + * @param array $columns |
|
144 | + * @return collection |
|
145 | + */ |
|
146 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
147 | + { |
|
148 | + $sort = $desc ? 'desc' : 'asc'; |
|
149 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
150 | + } |
|
151 | 151 | |
152 | - /** |
|
153 | - * Fetch all records with relations based on |
|
154 | - * the given condition from storage in pages. |
|
155 | - * |
|
156 | - * @param array $conditions array of conditions |
|
157 | - * @param integer $perPage |
|
158 | - * @param array $relations |
|
159 | - * @param string $sortBy |
|
160 | - * @param boolean $desc |
|
161 | - * @param array $columns |
|
162 | - * @return collection |
|
163 | - */ |
|
164 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
165 | - { |
|
166 | - unset($conditions['page']); |
|
167 | - $conditions = $this->constructConditions($conditions); |
|
168 | - $sort = $desc ? 'desc' : 'asc'; |
|
169 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
170 | - } |
|
152 | + /** |
|
153 | + * Fetch all records with relations based on |
|
154 | + * the given condition from storage in pages. |
|
155 | + * |
|
156 | + * @param array $conditions array of conditions |
|
157 | + * @param integer $perPage |
|
158 | + * @param array $relations |
|
159 | + * @param string $sortBy |
|
160 | + * @param boolean $desc |
|
161 | + * @param array $columns |
|
162 | + * @return collection |
|
163 | + */ |
|
164 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
165 | + { |
|
166 | + unset($conditions['page']); |
|
167 | + $conditions = $this->constructConditions($conditions); |
|
168 | + $sort = $desc ? 'desc' : 'asc'; |
|
169 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
170 | + } |
|
171 | 171 | |
172 | - /** |
|
173 | - * Save the given model to the storage. |
|
174 | - * |
|
175 | - * @param array $data |
|
176 | - * @param boolean $saveLog |
|
177 | - * @return object |
|
178 | - */ |
|
179 | - public function save(array $data, $saveLog = true) |
|
180 | - { |
|
181 | - $model = false; |
|
182 | - $modelClass = $this->model; |
|
183 | - $relations = []; |
|
172 | + /** |
|
173 | + * Save the given model to the storage. |
|
174 | + * |
|
175 | + * @param array $data |
|
176 | + * @param boolean $saveLog |
|
177 | + * @return object |
|
178 | + */ |
|
179 | + public function save(array $data, $saveLog = true) |
|
180 | + { |
|
181 | + $model = false; |
|
182 | + $modelClass = $this->model; |
|
183 | + $relations = []; |
|
184 | 184 | |
185 | - \DB::transaction(function () use (&$model, &$relations, $data, $saveLog, $modelClass) { |
|
186 | - /** |
|
187 | - * If the id is present in the data then select the model for updating, |
|
188 | - * else create new model. |
|
189 | - * @var array |
|
190 | - */ |
|
191 | - $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
192 | - if ( ! $model) |
|
193 | - { |
|
194 | - \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
195 | - } |
|
185 | + \DB::transaction(function () use (&$model, &$relations, $data, $saveLog, $modelClass) { |
|
186 | + /** |
|
187 | + * If the id is present in the data then select the model for updating, |
|
188 | + * else create new model. |
|
189 | + * @var array |
|
190 | + */ |
|
191 | + $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
192 | + if ( ! $model) |
|
193 | + { |
|
194 | + \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
195 | + } |
|
196 | 196 | |
197 | - /** |
|
198 | - * Construct the model object with the given data, |
|
199 | - * and if there is a relation add it to relations array, |
|
200 | - * then save the model. |
|
201 | - */ |
|
202 | - foreach ($data as $key => $value) |
|
203 | - { |
|
204 | - /** |
|
205 | - * If the attribute is a relation. |
|
206 | - */ |
|
207 | - $relation = camel_case($key); |
|
208 | - if (method_exists($model, $relation)) |
|
209 | - { |
|
197 | + /** |
|
198 | + * Construct the model object with the given data, |
|
199 | + * and if there is a relation add it to relations array, |
|
200 | + * then save the model. |
|
201 | + */ |
|
202 | + foreach ($data as $key => $value) |
|
203 | + { |
|
204 | + /** |
|
205 | + * If the attribute is a relation. |
|
206 | + */ |
|
207 | + $relation = camel_case($key); |
|
208 | + if (method_exists($model, $relation)) |
|
209 | + { |
|
210 | 210 | |
211 | - /** |
|
212 | - * Check if the relation is a collection. |
|
213 | - */ |
|
214 | - if (class_basename($model->$relation) == 'Collection') |
|
215 | - { |
|
216 | - /** |
|
217 | - * If the relation has no value then marke the relation data |
|
218 | - * related to the model to be deleted. |
|
219 | - */ |
|
220 | - if ( ! $value || ! count($value)) |
|
221 | - { |
|
222 | - $relations[$relation] = 'delete'; |
|
223 | - } |
|
224 | - } |
|
225 | - if (is_array($value)) |
|
226 | - { |
|
227 | - /** |
|
228 | - * Loop through the relation data. |
|
229 | - */ |
|
230 | - foreach ($value as $attr => $val) |
|
231 | - { |
|
232 | - /** |
|
233 | - * Get the relation model. |
|
234 | - */ |
|
235 | - $relationBaseModel = \Core::$relation()->model; |
|
211 | + /** |
|
212 | + * Check if the relation is a collection. |
|
213 | + */ |
|
214 | + if (class_basename($model->$relation) == 'Collection') |
|
215 | + { |
|
216 | + /** |
|
217 | + * If the relation has no value then marke the relation data |
|
218 | + * related to the model to be deleted. |
|
219 | + */ |
|
220 | + if ( ! $value || ! count($value)) |
|
221 | + { |
|
222 | + $relations[$relation] = 'delete'; |
|
223 | + } |
|
224 | + } |
|
225 | + if (is_array($value)) |
|
226 | + { |
|
227 | + /** |
|
228 | + * Loop through the relation data. |
|
229 | + */ |
|
230 | + foreach ($value as $attr => $val) |
|
231 | + { |
|
232 | + /** |
|
233 | + * Get the relation model. |
|
234 | + */ |
|
235 | + $relationBaseModel = \Core::$relation()->model; |
|
236 | 236 | |
237 | - /** |
|
238 | - * Check if the relation is a collection. |
|
239 | - */ |
|
240 | - if (class_basename($model->$relation) == 'Collection') |
|
241 | - { |
|
242 | - /** |
|
243 | - * If the id is present in the data then select the relation model for updating, |
|
244 | - * else create new model. |
|
245 | - */ |
|
246 | - $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
237 | + /** |
|
238 | + * Check if the relation is a collection. |
|
239 | + */ |
|
240 | + if (class_basename($model->$relation) == 'Collection') |
|
241 | + { |
|
242 | + /** |
|
243 | + * If the id is present in the data then select the relation model for updating, |
|
244 | + * else create new model. |
|
245 | + */ |
|
246 | + $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
247 | 247 | |
248 | - /** |
|
249 | - * If model doesn't exists. |
|
250 | - */ |
|
251 | - if ( ! $relationModel) |
|
252 | - { |
|
253 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
254 | - } |
|
248 | + /** |
|
249 | + * If model doesn't exists. |
|
250 | + */ |
|
251 | + if ( ! $relationModel) |
|
252 | + { |
|
253 | + \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
254 | + } |
|
255 | 255 | |
256 | - /** |
|
257 | - * Loop through the relation attributes. |
|
258 | - */ |
|
259 | - foreach ($val as $attr => $val) |
|
260 | - { |
|
261 | - /** |
|
262 | - * Prevent the sub relations or attributes not in the fillable. |
|
263 | - */ |
|
264 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
265 | - { |
|
266 | - $relationModel->$attr = $val; |
|
267 | - } |
|
268 | - } |
|
269 | - $relations[$relation][] = $relationModel; |
|
270 | - } |
|
271 | - /** |
|
272 | - * If not collection. |
|
273 | - */ |
|
274 | - else |
|
275 | - { |
|
276 | - /** |
|
277 | - * Prevent the sub relations. |
|
278 | - */ |
|
279 | - if (gettype($val) !== 'object' && gettype($val) !== 'array') |
|
280 | - { |
|
281 | - /** |
|
282 | - * If the id is present in the data then select the relation model for updating, |
|
283 | - * else create new model. |
|
284 | - */ |
|
285 | - $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
256 | + /** |
|
257 | + * Loop through the relation attributes. |
|
258 | + */ |
|
259 | + foreach ($val as $attr => $val) |
|
260 | + { |
|
261 | + /** |
|
262 | + * Prevent the sub relations or attributes not in the fillable. |
|
263 | + */ |
|
264 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
265 | + { |
|
266 | + $relationModel->$attr = $val; |
|
267 | + } |
|
268 | + } |
|
269 | + $relations[$relation][] = $relationModel; |
|
270 | + } |
|
271 | + /** |
|
272 | + * If not collection. |
|
273 | + */ |
|
274 | + else |
|
275 | + { |
|
276 | + /** |
|
277 | + * Prevent the sub relations. |
|
278 | + */ |
|
279 | + if (gettype($val) !== 'object' && gettype($val) !== 'array') |
|
280 | + { |
|
281 | + /** |
|
282 | + * If the id is present in the data then select the relation model for updating, |
|
283 | + * else create new model. |
|
284 | + */ |
|
285 | + $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
286 | 286 | |
287 | - /** |
|
288 | - * If model doesn't exists. |
|
289 | - */ |
|
290 | - if ( ! $relationModel) |
|
291 | - { |
|
292 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
293 | - } |
|
287 | + /** |
|
288 | + * If model doesn't exists. |
|
289 | + */ |
|
290 | + if ( ! $relationModel) |
|
291 | + { |
|
292 | + \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
293 | + } |
|
294 | 294 | |
295 | - /** |
|
296 | - * Prevent attributes not in the fillable. |
|
297 | - */ |
|
298 | - if (array_search($attr, $relationModel->getFillable(), true) !== false) |
|
299 | - { |
|
300 | - $relationModel->$attr = $val; |
|
301 | - $relations[$relation] = $relationModel; |
|
302 | - } |
|
303 | - } |
|
304 | - } |
|
305 | - } |
|
306 | - } |
|
307 | - } |
|
308 | - /** |
|
309 | - * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
310 | - */ |
|
311 | - else if (array_search($key, $model->getFillable(), true) !== false) |
|
312 | - { |
|
313 | - $model->$key = $value; |
|
314 | - } |
|
315 | - } |
|
316 | - /** |
|
317 | - * Save the model. |
|
318 | - */ |
|
319 | - $model->save(); |
|
295 | + /** |
|
296 | + * Prevent attributes not in the fillable. |
|
297 | + */ |
|
298 | + if (array_search($attr, $relationModel->getFillable(), true) !== false) |
|
299 | + { |
|
300 | + $relationModel->$attr = $val; |
|
301 | + $relations[$relation] = $relationModel; |
|
302 | + } |
|
303 | + } |
|
304 | + } |
|
305 | + } |
|
306 | + } |
|
307 | + } |
|
308 | + /** |
|
309 | + * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
310 | + */ |
|
311 | + else if (array_search($key, $model->getFillable(), true) !== false) |
|
312 | + { |
|
313 | + $model->$key = $value; |
|
314 | + } |
|
315 | + } |
|
316 | + /** |
|
317 | + * Save the model. |
|
318 | + */ |
|
319 | + $model->save(); |
|
320 | 320 | |
321 | - /** |
|
322 | - * Loop through the relations array. |
|
323 | - */ |
|
324 | - foreach ($relations as $key => $value) |
|
325 | - { |
|
326 | - /** |
|
327 | - * If the relation is marked for delete then delete it. |
|
328 | - */ |
|
329 | - if ($value == 'delete' && $model->$key()->count()) |
|
330 | - { |
|
331 | - $model->$key()->delete(); |
|
332 | - } |
|
333 | - /** |
|
334 | - * If the relation is an array. |
|
335 | - */ |
|
336 | - else if (gettype($value) == 'array') |
|
337 | - { |
|
338 | - $ids = []; |
|
339 | - /** |
|
340 | - * Loop through the relations. |
|
341 | - */ |
|
342 | - foreach ($value as $val) |
|
343 | - { |
|
344 | - switch (class_basename($model->$key())) |
|
345 | - { |
|
346 | - /** |
|
347 | - * If the relation is one to many then update it's foreign key with |
|
348 | - * the model id and save it then add its id to ids array to delete all |
|
349 | - * relations who's id isn't in the ids array. |
|
350 | - */ |
|
351 | - case 'HasMany': |
|
352 | - $foreignKeyName = explode('.', $model->$key()->getForeignKey())[1]; |
|
353 | - $val->$foreignKeyName = $model->id; |
|
354 | - $val->save(); |
|
355 | - $ids[] = $val->id; |
|
356 | - break; |
|
321 | + /** |
|
322 | + * Loop through the relations array. |
|
323 | + */ |
|
324 | + foreach ($relations as $key => $value) |
|
325 | + { |
|
326 | + /** |
|
327 | + * If the relation is marked for delete then delete it. |
|
328 | + */ |
|
329 | + if ($value == 'delete' && $model->$key()->count()) |
|
330 | + { |
|
331 | + $model->$key()->delete(); |
|
332 | + } |
|
333 | + /** |
|
334 | + * If the relation is an array. |
|
335 | + */ |
|
336 | + else if (gettype($value) == 'array') |
|
337 | + { |
|
338 | + $ids = []; |
|
339 | + /** |
|
340 | + * Loop through the relations. |
|
341 | + */ |
|
342 | + foreach ($value as $val) |
|
343 | + { |
|
344 | + switch (class_basename($model->$key())) |
|
345 | + { |
|
346 | + /** |
|
347 | + * If the relation is one to many then update it's foreign key with |
|
348 | + * the model id and save it then add its id to ids array to delete all |
|
349 | + * relations who's id isn't in the ids array. |
|
350 | + */ |
|
351 | + case 'HasMany': |
|
352 | + $foreignKeyName = explode('.', $model->$key()->getForeignKey())[1]; |
|
353 | + $val->$foreignKeyName = $model->id; |
|
354 | + $val->save(); |
|
355 | + $ids[] = $val->id; |
|
356 | + break; |
|
357 | 357 | |
358 | - /** |
|
359 | - * If the relation is many to many then add it's id to the ids array to |
|
360 | - * attache these ids to the model. |
|
361 | - */ |
|
362 | - case 'BelongsToMany': |
|
363 | - $val->save(); |
|
364 | - $ids[] = $val->id; |
|
365 | - break; |
|
366 | - } |
|
367 | - } |
|
368 | - switch (class_basename($model->$key())) |
|
369 | - { |
|
370 | - /** |
|
371 | - * If the relation is one to many then delete all |
|
372 | - * relations who's id isn't in the ids array. |
|
373 | - */ |
|
374 | - case 'HasMany': |
|
375 | - $model->$key()->whereNotIn('id', $ids)->delete(); |
|
376 | - break; |
|
358 | + /** |
|
359 | + * If the relation is many to many then add it's id to the ids array to |
|
360 | + * attache these ids to the model. |
|
361 | + */ |
|
362 | + case 'BelongsToMany': |
|
363 | + $val->save(); |
|
364 | + $ids[] = $val->id; |
|
365 | + break; |
|
366 | + } |
|
367 | + } |
|
368 | + switch (class_basename($model->$key())) |
|
369 | + { |
|
370 | + /** |
|
371 | + * If the relation is one to many then delete all |
|
372 | + * relations who's id isn't in the ids array. |
|
373 | + */ |
|
374 | + case 'HasMany': |
|
375 | + $model->$key()->whereNotIn('id', $ids)->delete(); |
|
376 | + break; |
|
377 | 377 | |
378 | - /** |
|
379 | - * If the relation is many to many then |
|
380 | - * detach the previous data and attach |
|
381 | - * the ids array to the model. |
|
382 | - */ |
|
383 | - case 'BelongsToMany': |
|
384 | - $model->$key()->detach(); |
|
385 | - $model->$key()->attach($ids); |
|
386 | - break; |
|
387 | - } |
|
388 | - } |
|
389 | - /** |
|
390 | - * If the relation isn't array. |
|
391 | - */ |
|
392 | - else |
|
393 | - { |
|
394 | - switch (class_basename($model->$key())) |
|
395 | - { |
|
396 | - /** |
|
397 | - * If the relation is one to many or one to one. |
|
398 | - */ |
|
399 | - case 'BelongsTo': |
|
400 | - $value->save(); |
|
401 | - $model->$key()->associate($value); |
|
402 | - $model->save(); |
|
403 | - break; |
|
404 | - } |
|
405 | - } |
|
406 | - } |
|
378 | + /** |
|
379 | + * If the relation is many to many then |
|
380 | + * detach the previous data and attach |
|
381 | + * the ids array to the model. |
|
382 | + */ |
|
383 | + case 'BelongsToMany': |
|
384 | + $model->$key()->detach(); |
|
385 | + $model->$key()->attach($ids); |
|
386 | + break; |
|
387 | + } |
|
388 | + } |
|
389 | + /** |
|
390 | + * If the relation isn't array. |
|
391 | + */ |
|
392 | + else |
|
393 | + { |
|
394 | + switch (class_basename($model->$key())) |
|
395 | + { |
|
396 | + /** |
|
397 | + * If the relation is one to many or one to one. |
|
398 | + */ |
|
399 | + case 'BelongsTo': |
|
400 | + $value->save(); |
|
401 | + $model->$key()->associate($value); |
|
402 | + $model->save(); |
|
403 | + break; |
|
404 | + } |
|
405 | + } |
|
406 | + } |
|
407 | 407 | |
408 | - $saveLog ? \Logging::saveLog(array_key_exists('id', $data) ? 'update' : 'create', class_basename($modelClass), $this->getModel(), $model->id, $model) : false; |
|
409 | - }); |
|
408 | + $saveLog ? \Logging::saveLog(array_key_exists('id', $data) ? 'update' : 'create', class_basename($modelClass), $this->getModel(), $model->id, $model) : false; |
|
409 | + }); |
|
410 | 410 | |
411 | - /** |
|
412 | - * return the saved mdel with the given relations. |
|
413 | - */ |
|
414 | - return $model; |
|
415 | - } |
|
411 | + /** |
|
412 | + * return the saved mdel with the given relations. |
|
413 | + */ |
|
414 | + return $model; |
|
415 | + } |
|
416 | 416 | |
417 | - /** |
|
418 | - * Delete record from the storage based on the given |
|
419 | - * condition. |
|
420 | - * |
|
421 | - * @param var $value condition value |
|
422 | - * @param string $attribute condition column name |
|
423 | - * @return void |
|
424 | - */ |
|
425 | - public function delete($value, $attribute = 'id', $saveLog = true) |
|
426 | - { |
|
427 | - if ($attribute == 'id') |
|
428 | - { |
|
429 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
430 | - $model = $this->model->lockForUpdate()->find($value); |
|
431 | - if ( ! $model) |
|
432 | - { |
|
433 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
434 | - } |
|
417 | + /** |
|
418 | + * Delete record from the storage based on the given |
|
419 | + * condition. |
|
420 | + * |
|
421 | + * @param var $value condition value |
|
422 | + * @param string $attribute condition column name |
|
423 | + * @return void |
|
424 | + */ |
|
425 | + public function delete($value, $attribute = 'id', $saveLog = true) |
|
426 | + { |
|
427 | + if ($attribute == 'id') |
|
428 | + { |
|
429 | + \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
430 | + $model = $this->model->lockForUpdate()->find($value); |
|
431 | + if ( ! $model) |
|
432 | + { |
|
433 | + \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
434 | + } |
|
435 | 435 | |
436 | - $model->delete(); |
|
437 | - $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
438 | - }); |
|
439 | - } |
|
440 | - else |
|
441 | - { |
|
442 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
443 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ |
|
444 | - $model->delete(); |
|
445 | - $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
446 | - }); |
|
447 | - }); |
|
448 | - } |
|
449 | - } |
|
436 | + $model->delete(); |
|
437 | + $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
438 | + }); |
|
439 | + } |
|
440 | + else |
|
441 | + { |
|
442 | + \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
443 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ |
|
444 | + $model->delete(); |
|
445 | + $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
446 | + }); |
|
447 | + }); |
|
448 | + } |
|
449 | + } |
|
450 | 450 | |
451 | - /** |
|
452 | - * Fetch records from the storage based on the given |
|
453 | - * id. |
|
454 | - * |
|
455 | - * @param integer $id |
|
456 | - * @param array $relations |
|
457 | - * @param array $columns |
|
458 | - * @return object |
|
459 | - */ |
|
460 | - public function find($id, $relations = [], $columns = array('*')) |
|
461 | - { |
|
462 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
463 | - } |
|
451 | + /** |
|
452 | + * Fetch records from the storage based on the given |
|
453 | + * id. |
|
454 | + * |
|
455 | + * @param integer $id |
|
456 | + * @param array $relations |
|
457 | + * @param array $columns |
|
458 | + * @return object |
|
459 | + */ |
|
460 | + public function find($id, $relations = [], $columns = array('*')) |
|
461 | + { |
|
462 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
463 | + } |
|
464 | 464 | |
465 | - /** |
|
466 | - * Fetch records from the storage based on the given |
|
467 | - * condition. |
|
468 | - * |
|
469 | - * @param array $conditions array of conditions |
|
470 | - * @param array $relations |
|
471 | - * @param string $sortBy |
|
472 | - * @param boolean $desc |
|
473 | - * @param array $columns |
|
474 | - * @return collection |
|
475 | - */ |
|
476 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
477 | - { |
|
478 | - $conditions = $this->constructConditions($conditions); |
|
479 | - $sort = $desc ? 'desc' : 'asc'; |
|
480 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
481 | - } |
|
465 | + /** |
|
466 | + * Fetch records from the storage based on the given |
|
467 | + * condition. |
|
468 | + * |
|
469 | + * @param array $conditions array of conditions |
|
470 | + * @param array $relations |
|
471 | + * @param string $sortBy |
|
472 | + * @param boolean $desc |
|
473 | + * @param array $columns |
|
474 | + * @return collection |
|
475 | + */ |
|
476 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
477 | + { |
|
478 | + $conditions = $this->constructConditions($conditions); |
|
479 | + $sort = $desc ? 'desc' : 'asc'; |
|
480 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
481 | + } |
|
482 | 482 | |
483 | - /** |
|
484 | - * Fetch the first record from the storage based on the given |
|
485 | - * condition. |
|
486 | - * |
|
487 | - * @param array $conditions array of conditions |
|
488 | - * @param array $relations |
|
489 | - * @param array $columns |
|
490 | - * @return object |
|
491 | - */ |
|
492 | - public function first($conditions, $relations = [], $columns = array('*')) |
|
493 | - { |
|
494 | - $conditions = $this->constructConditions($conditions); |
|
495 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
496 | - } |
|
483 | + /** |
|
484 | + * Fetch the first record from the storage based on the given |
|
485 | + * condition. |
|
486 | + * |
|
487 | + * @param array $conditions array of conditions |
|
488 | + * @param array $relations |
|
489 | + * @param array $columns |
|
490 | + * @return object |
|
491 | + */ |
|
492 | + public function first($conditions, $relations = [], $columns = array('*')) |
|
493 | + { |
|
494 | + $conditions = $this->constructConditions($conditions); |
|
495 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
496 | + } |
|
497 | 497 | |
498 | - /** |
|
499 | - * Build the conditions recursively for the retrieving methods. |
|
500 | - * @param array $conditions |
|
501 | - * @return array |
|
502 | - */ |
|
503 | - protected function constructConditions($conditions) |
|
504 | - { |
|
505 | - $conditionString = ''; |
|
506 | - $conditionValues = []; |
|
507 | - foreach ($conditions as $key => $value) |
|
508 | - { |
|
509 | - if ($key == 'and') |
|
510 | - { |
|
511 | - $conditionString .= str_replace('{op}', 'and', $this->constructConditions($value)['conditionString']) . ' {op} '; |
|
512 | - $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); |
|
513 | - } |
|
514 | - else if ($key == 'or') |
|
515 | - { |
|
516 | - $conditionString .= str_replace('{op}', 'or', $this->constructConditions($value)['conditionString']) . ' {op} '; |
|
517 | - $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); |
|
518 | - } |
|
519 | - else |
|
520 | - { |
|
521 | - $conditionString .= $key . '=? {op} '; |
|
522 | - $conditionValues[] = $value; |
|
523 | - } |
|
524 | - } |
|
525 | - $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
526 | - return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
527 | - } |
|
498 | + /** |
|
499 | + * Build the conditions recursively for the retrieving methods. |
|
500 | + * @param array $conditions |
|
501 | + * @return array |
|
502 | + */ |
|
503 | + protected function constructConditions($conditions) |
|
504 | + { |
|
505 | + $conditionString = ''; |
|
506 | + $conditionValues = []; |
|
507 | + foreach ($conditions as $key => $value) |
|
508 | + { |
|
509 | + if ($key == 'and') |
|
510 | + { |
|
511 | + $conditionString .= str_replace('{op}', 'and', $this->constructConditions($value)['conditionString']) . ' {op} '; |
|
512 | + $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); |
|
513 | + } |
|
514 | + else if ($key == 'or') |
|
515 | + { |
|
516 | + $conditionString .= str_replace('{op}', 'or', $this->constructConditions($value)['conditionString']) . ' {op} '; |
|
517 | + $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); |
|
518 | + } |
|
519 | + else |
|
520 | + { |
|
521 | + $conditionString .= $key . '=? {op} '; |
|
522 | + $conditionValues[] = $value; |
|
523 | + } |
|
524 | + } |
|
525 | + $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
526 | + return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
527 | + } |
|
528 | 528 | |
529 | - /** |
|
530 | - * Abstract method that return the necessary |
|
531 | - * information (full model namespace) |
|
532 | - * needed to preform the previous actions. |
|
533 | - * |
|
534 | - * @return string |
|
535 | - */ |
|
536 | - abstract protected function getModel(); |
|
529 | + /** |
|
530 | + * Abstract method that return the necessary |
|
531 | + * information (full model namespace) |
|
532 | + * needed to preform the previous actions. |
|
533 | + * |
|
534 | + * @return string |
|
535 | + */ |
|
536 | + abstract protected function getModel(); |
|
537 | 537 | } |
538 | 538 | \ No newline at end of file |
@@ -63,14 +63,14 @@ discard block |
||
63 | 63 | /** |
64 | 64 | * Construct the select conditions for the model. |
65 | 65 | */ |
66 | - $model->where(function ($q) use ($query, $conditionColumns, $relations){ |
|
66 | + $model->where(function($q) use ($query, $conditionColumns, $relations){ |
|
67 | 67 | |
68 | 68 | if (count($conditionColumns)) |
69 | 69 | { |
70 | 70 | /** |
71 | 71 | * Use the first element in the model columns to construct the first condition. |
72 | 72 | */ |
73 | - $q->where(\DB::raw('LOWER(' . array_shift($conditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
73 | + $q->where(\DB::raw('LOWER('.array_shift($conditionColumns).')'), 'LIKE', '%'.strtolower($query).'%'); |
|
74 | 74 | } |
75 | 75 | |
76 | 76 | /** |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | */ |
79 | 79 | foreach ($conditionColumns as $column) |
80 | 80 | { |
81 | - $q->orWhere(\DB::raw('LOWER(' . $column . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
81 | + $q->orWhere(\DB::raw('LOWER('.$column.')'), 'LIKE', '%'.strtolower($query).'%'); |
|
82 | 82 | } |
83 | 83 | |
84 | 84 | /** |
@@ -99,9 +99,9 @@ discard block |
||
99 | 99 | /** |
100 | 100 | * Construct the relation condition. |
101 | 101 | */ |
102 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
102 | + $q->orWhereHas($relation, function($subModel) use ($query, $relation){ |
|
103 | 103 | |
104 | - $subModel->where(function ($q) use ($query, $relation){ |
|
104 | + $subModel->where(function($q) use ($query, $relation){ |
|
105 | 105 | |
106 | 106 | /** |
107 | 107 | * Get columns of the relation. |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | /** |
114 | 114 | * Use the first element in the relation model columns to construct the first condition. |
115 | 115 | */ |
116 | - $q->where(\DB::raw('LOWER(' . array_shift($subConditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
116 | + $q->where(\DB::raw('LOWER('.array_shift($subConditionColumns).')'), 'LIKE', '%'.strtolower($query).'%'); |
|
117 | 117 | } |
118 | 118 | |
119 | 119 | /** |
@@ -121,7 +121,7 @@ discard block |
||
121 | 121 | */ |
122 | 122 | foreach ($subConditionColumns as $subConditionColumn) |
123 | 123 | { |
124 | - $q->orWhere(\DB::raw('LOWER(' . $subConditionColumn . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
124 | + $q->orWhere(\DB::raw('LOWER('.$subConditionColumn.')'), 'LIKE', '%'.strtolower($query).'%'); |
|
125 | 125 | } |
126 | 126 | }); |
127 | 127 | |
@@ -182,7 +182,7 @@ discard block |
||
182 | 182 | $modelClass = $this->model; |
183 | 183 | $relations = []; |
184 | 184 | |
185 | - \DB::transaction(function () use (&$model, &$relations, $data, $saveLog, $modelClass) { |
|
185 | + \DB::transaction(function() use (&$model, &$relations, $data, $saveLog, $modelClass) { |
|
186 | 186 | /** |
187 | 187 | * If the id is present in the data then select the model for updating, |
188 | 188 | * else create new model. |
@@ -191,7 +191,7 @@ discard block |
||
191 | 191 | $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
192 | 192 | if ( ! $model) |
193 | 193 | { |
194 | - \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
194 | + \ErrorHandler::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
195 | 195 | } |
196 | 196 | |
197 | 197 | /** |
@@ -250,7 +250,7 @@ discard block |
||
250 | 250 | */ |
251 | 251 | if ( ! $relationModel) |
252 | 252 | { |
253 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
253 | + \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
254 | 254 | } |
255 | 255 | |
256 | 256 | /** |
@@ -261,7 +261,7 @@ discard block |
||
261 | 261 | /** |
262 | 262 | * Prevent the sub relations or attributes not in the fillable. |
263 | 263 | */ |
264 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
264 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
265 | 265 | { |
266 | 266 | $relationModel->$attr = $val; |
267 | 267 | } |
@@ -289,7 +289,7 @@ discard block |
||
289 | 289 | */ |
290 | 290 | if ( ! $relationModel) |
291 | 291 | { |
292 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
292 | + \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
293 | 293 | } |
294 | 294 | |
295 | 295 | /** |
@@ -426,11 +426,11 @@ discard block |
||
426 | 426 | { |
427 | 427 | if ($attribute == 'id') |
428 | 428 | { |
429 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
429 | + \DB::transaction(function() use ($value, $attribute, &$result, $saveLog) { |
|
430 | 430 | $model = $this->model->lockForUpdate()->find($value); |
431 | 431 | if ( ! $model) |
432 | 432 | { |
433 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
433 | + \ErrorHandler::notFound(class_basename($this->model).' with id : '.$value); |
|
434 | 434 | } |
435 | 435 | |
436 | 436 | $model->delete(); |
@@ -439,8 +439,8 @@ discard block |
||
439 | 439 | } |
440 | 440 | else |
441 | 441 | { |
442 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
443 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ |
|
442 | + \DB::transaction(function() use ($value, $attribute, &$result, $saveLog) { |
|
443 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function($model) { |
|
444 | 444 | $model->delete(); |
445 | 445 | $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
446 | 446 | }); |
@@ -477,7 +477,7 @@ discard block |
||
477 | 477 | { |
478 | 478 | $conditions = $this->constructConditions($conditions); |
479 | 479 | $sort = $desc ? 'desc' : 'asc'; |
480 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
480 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
481 | 481 | } |
482 | 482 | |
483 | 483 | /** |
@@ -508,21 +508,21 @@ discard block |
||
508 | 508 | { |
509 | 509 | if ($key == 'and') |
510 | 510 | { |
511 | - $conditionString .= str_replace('{op}', 'and', $this->constructConditions($value)['conditionString']) . ' {op} '; |
|
511 | + $conditionString .= str_replace('{op}', 'and', $this->constructConditions($value)['conditionString']).' {op} '; |
|
512 | 512 | $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); |
513 | 513 | } |
514 | 514 | else if ($key == 'or') |
515 | 515 | { |
516 | - $conditionString .= str_replace('{op}', 'or', $this->constructConditions($value)['conditionString']) . ' {op} '; |
|
516 | + $conditionString .= str_replace('{op}', 'or', $this->constructConditions($value)['conditionString']).' {op} '; |
|
517 | 517 | $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); |
518 | 518 | } |
519 | 519 | else |
520 | 520 | { |
521 | - $conditionString .= $key . '=? {op} '; |
|
521 | + $conditionString .= $key.'=? {op} '; |
|
522 | 522 | $conditionValues[] = $value; |
523 | 523 | } |
524 | 524 | } |
525 | - $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
525 | + $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
526 | 526 | return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
527 | 527 | } |
528 | 528 |
@@ -5,32 +5,32 @@ |
||
5 | 5 | |
6 | 6 | class Report extends Model{ |
7 | 7 | |
8 | - use SoftDeletes; |
|
8 | + use SoftDeletes; |
|
9 | 9 | protected $table = 'reports'; |
10 | 10 | protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
11 | 11 | protected $hidden = ['deleted_at']; |
12 | 12 | protected $guarded = ['id']; |
13 | 13 | protected $fillable = ['report_name', 'view_name']; |
14 | - public $searchable = ['report_name', 'view_name']; |
|
14 | + public $searchable = ['report_name', 'view_name']; |
|
15 | 15 | |
16 | 16 | public function getCreatedAtAttribute($value) |
17 | - { |
|
18 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
19 | - } |
|
17 | + { |
|
18 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
19 | + } |
|
20 | 20 | |
21 | - public function getUpdatedAtAttribute($value) |
|
22 | - { |
|
23 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
24 | - } |
|
21 | + public function getUpdatedAtAttribute($value) |
|
22 | + { |
|
23 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
24 | + } |
|
25 | 25 | |
26 | - public function getDeletedAtAttribute($value) |
|
27 | - { |
|
28 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
29 | - } |
|
26 | + public function getDeletedAtAttribute($value) |
|
27 | + { |
|
28 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
29 | + } |
|
30 | 30 | |
31 | - public static function boot() |
|
32 | - { |
|
33 | - parent::boot(); |
|
34 | - parent::observe(\App::make('App\Modules\V1\Reporting\ModelObservers\ReprotObserver')); |
|
35 | - } |
|
31 | + public static function boot() |
|
32 | + { |
|
33 | + parent::boot(); |
|
34 | + parent::observe(\App::make('App\Modules\V1\Reporting\ModelObservers\ReprotObserver')); |
|
35 | + } |
|
36 | 36 | } |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | use Illuminate\Database\Eloquent\Model; |
4 | 4 | use Illuminate\Database\Eloquent\SoftDeletes; |
5 | 5 | |
6 | -class Report extends Model{ |
|
6 | +class Report extends Model { |
|
7 | 7 | |
8 | 8 | use SoftDeletes; |
9 | 9 | protected $table = 'reports'; |
@@ -11,7 +11,7 @@ discard block |
||
11 | 11 | protected $hidden = ['deleted_at']; |
12 | 12 | protected $guarded = ['id']; |
13 | 13 | protected $fillable = ['report_name', 'view_name']; |
14 | - public $searchable = ['report_name', 'view_name']; |
|
14 | + public $searchable = ['report_name', 'view_name']; |
|
15 | 15 | |
16 | 16 | public function getCreatedAtAttribute($value) |
17 | 17 | { |
@@ -5,37 +5,37 @@ |
||
5 | 5 | |
6 | 6 | class Notification extends Model{ |
7 | 7 | |
8 | - use SoftDeletes; |
|
9 | - protected $table = 'notifications'; |
|
10 | - protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
11 | - protected $hidden = ['deleted_at', 'item_type']; |
|
12 | - protected $guarded = ['id']; |
|
13 | - protected $fillable = ['name', 'description', 'item_name', 'item_type', 'item_id', 'notified']; |
|
14 | - public $searchable = ['name', 'description', 'item_name', 'item_type']; |
|
8 | + use SoftDeletes; |
|
9 | + protected $table = 'notifications'; |
|
10 | + protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
11 | + protected $hidden = ['deleted_at', 'item_type']; |
|
12 | + protected $guarded = ['id']; |
|
13 | + protected $fillable = ['name', 'description', 'item_name', 'item_type', 'item_id', 'notified']; |
|
14 | + public $searchable = ['name', 'description', 'item_name', 'item_type']; |
|
15 | 15 | |
16 | - public function getCreatedAtAttribute($value) |
|
17 | - { |
|
18 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
19 | - } |
|
16 | + public function getCreatedAtAttribute($value) |
|
17 | + { |
|
18 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
19 | + } |
|
20 | 20 | |
21 | - public function getUpdatedAtAttribute($value) |
|
22 | - { |
|
23 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
24 | - } |
|
21 | + public function getUpdatedAtAttribute($value) |
|
22 | + { |
|
23 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
24 | + } |
|
25 | 25 | |
26 | - public function getDeletedAtAttribute($value) |
|
27 | - { |
|
28 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
29 | - } |
|
26 | + public function getDeletedAtAttribute($value) |
|
27 | + { |
|
28 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
29 | + } |
|
30 | 30 | |
31 | - public function item() |
|
32 | - { |
|
33 | - return $this->morphTo(); |
|
34 | - } |
|
31 | + public function item() |
|
32 | + { |
|
33 | + return $this->morphTo(); |
|
34 | + } |
|
35 | 35 | |
36 | - public static function boot() |
|
37 | - { |
|
38 | - parent::boot(); |
|
39 | - parent::observe(\App::make('App\Modules\V1\Notifications\ModelObservers\NotificationObserver')); |
|
40 | - } |
|
36 | + public static function boot() |
|
37 | + { |
|
38 | + parent::boot(); |
|
39 | + parent::observe(\App::make('App\Modules\V1\Notifications\ModelObservers\NotificationObserver')); |
|
40 | + } |
|
41 | 41 | } |
@@ -5,37 +5,37 @@ |
||
5 | 5 | |
6 | 6 | class PushNotificationDevice extends Model{ |
7 | 7 | |
8 | - use SoftDeletes; |
|
9 | - protected $table = 'push_notifications_devices'; |
|
10 | - protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
11 | - protected $hidden = ['deleted_at']; |
|
12 | - protected $guarded = ['id']; |
|
13 | - protected $fillable = ['device_token', 'device_type', 'user_id', 'active']; |
|
14 | - public $searchable = ['device_token', 'device_type']; |
|
8 | + use SoftDeletes; |
|
9 | + protected $table = 'push_notifications_devices'; |
|
10 | + protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
11 | + protected $hidden = ['deleted_at']; |
|
12 | + protected $guarded = ['id']; |
|
13 | + protected $fillable = ['device_token', 'device_type', 'user_id', 'active']; |
|
14 | + public $searchable = ['device_token', 'device_type']; |
|
15 | 15 | |
16 | - public function getCreatedAtAttribute($value) |
|
17 | - { |
|
18 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
19 | - } |
|
16 | + public function getCreatedAtAttribute($value) |
|
17 | + { |
|
18 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
19 | + } |
|
20 | 20 | |
21 | - public function getUpdatedAtAttribute($value) |
|
22 | - { |
|
23 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
24 | - } |
|
21 | + public function getUpdatedAtAttribute($value) |
|
22 | + { |
|
23 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
24 | + } |
|
25 | 25 | |
26 | - public function getDeletedAtAttribute($value) |
|
27 | - { |
|
28 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
29 | - } |
|
26 | + public function getDeletedAtAttribute($value) |
|
27 | + { |
|
28 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
29 | + } |
|
30 | 30 | |
31 | - public function user() |
|
32 | - { |
|
33 | - return $this->belongsTo('App\Modules\V1\Acl\AclUser'); |
|
34 | - } |
|
31 | + public function user() |
|
32 | + { |
|
33 | + return $this->belongsTo('App\Modules\V1\Acl\AclUser'); |
|
34 | + } |
|
35 | 35 | |
36 | - public static function boot() |
|
37 | - { |
|
38 | - parent::boot(); |
|
39 | - parent::observe(\App::make('App\Modules\V1\Notifications\ModelObservers\PushNotificationDeviceObserver')); |
|
40 | - } |
|
36 | + public static function boot() |
|
37 | + { |
|
38 | + parent::boot(); |
|
39 | + parent::observe(\App::make('App\Modules\V1\Notifications\ModelObservers\PushNotificationDeviceObserver')); |
|
40 | + } |
|
41 | 41 | } |
@@ -5,42 +5,42 @@ |
||
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 | - public $searchable = ['name']; |
|
15 | - |
|
16 | - public function getCreatedAtAttribute($value) |
|
17 | - { |
|
18 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
19 | - } |
|
20 | - |
|
21 | - public function getUpdatedAtAttribute($value) |
|
22 | - { |
|
23 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
24 | - } |
|
25 | - |
|
26 | - public function getDeletedAtAttribute($value) |
|
27 | - { |
|
28 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
29 | - } |
|
30 | - |
|
31 | - public function users() |
|
32 | - { |
|
33 | - return $this->belongsToMany('\App\Modules\V1\Acl\AclUser','users_groups','group_id','user_id')->whereNull('users_groups.deleted_at')->withTimestamps(); |
|
34 | - } |
|
35 | - |
|
36 | - public function permissions() |
|
37 | - { |
|
38 | - return $this->belongsToMany('\App\Modules\V1\Acl\AclPermission','groups_permissions','group_id','permission_id')->whereNull('groups_permissions.deleted_at')->withTimestamps(); |
|
39 | - } |
|
40 | - |
|
41 | - public static function boot() |
|
42 | - { |
|
43 | - parent::boot(); |
|
44 | - parent::observe(\App::make('App\Modules\V1\Acl\ModelObservers\AclGroupObserver')); |
|
45 | - } |
|
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 | + public $searchable = ['name']; |
|
15 | + |
|
16 | + public function getCreatedAtAttribute($value) |
|
17 | + { |
|
18 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
19 | + } |
|
20 | + |
|
21 | + public function getUpdatedAtAttribute($value) |
|
22 | + { |
|
23 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
24 | + } |
|
25 | + |
|
26 | + public function getDeletedAtAttribute($value) |
|
27 | + { |
|
28 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
29 | + } |
|
30 | + |
|
31 | + public function users() |
|
32 | + { |
|
33 | + return $this->belongsToMany('\App\Modules\V1\Acl\AclUser','users_groups','group_id','user_id')->whereNull('users_groups.deleted_at')->withTimestamps(); |
|
34 | + } |
|
35 | + |
|
36 | + public function permissions() |
|
37 | + { |
|
38 | + return $this->belongsToMany('\App\Modules\V1\Acl\AclPermission','groups_permissions','group_id','permission_id')->whereNull('groups_permissions.deleted_at')->withTimestamps(); |
|
39 | + } |
|
40 | + |
|
41 | + public static function boot() |
|
42 | + { |
|
43 | + parent::boot(); |
|
44 | + parent::observe(\App::make('App\Modules\V1\Acl\ModelObservers\AclGroupObserver')); |
|
45 | + } |
|
46 | 46 | } |
@@ -6,67 +6,67 @@ |
||
6 | 6 | |
7 | 7 | class AclUser extends User { |
8 | 8 | |
9 | - use SoftDeletes; |
|
10 | - protected $table = 'users'; |
|
11 | - protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
12 | - protected $hidden = ['password', 'remember_token','deleted_at']; |
|
13 | - protected $guarded = ['id']; |
|
14 | - protected $fillable = ['name', 'email', 'password']; |
|
15 | - protected $appends = ['permissions']; |
|
16 | - public $searchable = ['name', 'email']; |
|
9 | + use SoftDeletes; |
|
10 | + protected $table = 'users'; |
|
11 | + protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
12 | + protected $hidden = ['password', 'remember_token','deleted_at']; |
|
13 | + protected $guarded = ['id']; |
|
14 | + protected $fillable = ['name', 'email', 'password']; |
|
15 | + protected $appends = ['permissions']; |
|
16 | + public $searchable = ['name', 'email']; |
|
17 | 17 | |
18 | - public function getCreatedAtAttribute($value) |
|
19 | - { |
|
20 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
21 | - } |
|
18 | + public function getCreatedAtAttribute($value) |
|
19 | + { |
|
20 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
21 | + } |
|
22 | 22 | |
23 | - public function getUpdatedAtAttribute($value) |
|
24 | - { |
|
25 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
26 | - } |
|
23 | + public function getUpdatedAtAttribute($value) |
|
24 | + { |
|
25 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
26 | + } |
|
27 | 27 | |
28 | - public function getDeletedAtAttribute($value) |
|
29 | - { |
|
30 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
31 | - } |
|
28 | + public function getDeletedAtAttribute($value) |
|
29 | + { |
|
30 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * Encrypt the password attribute before |
|
35 | - * saving it in the storage. |
|
36 | - * |
|
37 | - * @param string $value |
|
38 | - */ |
|
39 | - public function setPasswordAttribute($value) |
|
40 | - { |
|
41 | - $this->attributes['password'] = bcrypt($value); |
|
42 | - } |
|
33 | + /** |
|
34 | + * Encrypt the password attribute before |
|
35 | + * saving it in the storage. |
|
36 | + * |
|
37 | + * @param string $value |
|
38 | + */ |
|
39 | + public function setPasswordAttribute($value) |
|
40 | + { |
|
41 | + $this->attributes['password'] = bcrypt($value); |
|
42 | + } |
|
43 | 43 | |
44 | - public function logs() |
|
45 | - { |
|
46 | - return $this->hasMany('App\Modules\V1\Core\Log', 'user_id'); |
|
47 | - } |
|
44 | + public function logs() |
|
45 | + { |
|
46 | + return $this->hasMany('App\Modules\V1\Core\Log', 'user_id'); |
|
47 | + } |
|
48 | 48 | |
49 | - public function groups() |
|
50 | - { |
|
51 | - return $this->belongsToMany('\App\Modules\V1\Acl\AclGroup','users_groups','user_id','group_id')->whereNull('users_groups.deleted_at')->withTimestamps(); |
|
52 | - } |
|
49 | + public function groups() |
|
50 | + { |
|
51 | + return $this->belongsToMany('\App\Modules\V1\Acl\AclGroup','users_groups','user_id','group_id')->whereNull('users_groups.deleted_at')->withTimestamps(); |
|
52 | + } |
|
53 | 53 | |
54 | - public function getPermissionsAttribute() |
|
55 | - { |
|
56 | - $permissions = []; |
|
57 | - foreach ($this->groups as $group) |
|
58 | - { |
|
59 | - $group->permissions->each(function ($permission) use (&$permissions){ |
|
60 | - $permissions[$permission->model][$permission->id] = $permission->name; |
|
61 | - }); |
|
62 | - } |
|
54 | + public function getPermissionsAttribute() |
|
55 | + { |
|
56 | + $permissions = []; |
|
57 | + foreach ($this->groups as $group) |
|
58 | + { |
|
59 | + $group->permissions->each(function ($permission) use (&$permissions){ |
|
60 | + $permissions[$permission->model][$permission->id] = $permission->name; |
|
61 | + }); |
|
62 | + } |
|
63 | 63 | |
64 | - return \Illuminate\Database\Eloquent\Collection::make($permissions); |
|
65 | - } |
|
64 | + return \Illuminate\Database\Eloquent\Collection::make($permissions); |
|
65 | + } |
|
66 | 66 | |
67 | - public static function boot() |
|
68 | - { |
|
69 | - parent::boot(); |
|
70 | - parent::observe(\App::make('App\Modules\V1\Acl\ModelObservers\AclUserObserver')); |
|
71 | - } |
|
67 | + public static function boot() |
|
68 | + { |
|
69 | + parent::boot(); |
|
70 | + parent::observe(\App::make('App\Modules\V1\Acl\ModelObservers\AclUserObserver')); |
|
71 | + } |
|
72 | 72 | } |
@@ -4,232 +4,232 @@ |
||
4 | 4 | |
5 | 5 | class UserRepository extends AbstractRepository |
6 | 6 | { |
7 | - /** |
|
8 | - * Return the model full namespace. |
|
9 | - * |
|
10 | - * @return string |
|
11 | - */ |
|
12 | - protected function getModel() |
|
13 | - { |
|
14 | - return 'App\Modules\V1\Acl\AclUser'; |
|
15 | - } |
|
16 | - |
|
17 | - /** |
|
18 | - * Check if the logged in user or the given user |
|
19 | - * has the given permissions on the given model. |
|
20 | - * |
|
21 | - * @param string $nameOfPermission |
|
22 | - * @param string $model |
|
23 | - * @param boolean $user |
|
24 | - * @return boolean |
|
25 | - */ |
|
26 | - public function can($nameOfPermission, $model, $user = false ) |
|
27 | - { |
|
28 | - $user = $user ?: \JWTAuth::parseToken()->authenticate(); |
|
29 | - $permissions = []; |
|
30 | - \Core::users()->find($user->id, ['groups.permissions'])->groups->lists('permissions')->each(function ($permission) use (&$permissions, $model){ |
|
31 | - $permissions = array_merge($permissions, $permission->where('model', $model)->lists('name')->toArray()); |
|
32 | - }); |
|
7 | + /** |
|
8 | + * Return the model full namespace. |
|
9 | + * |
|
10 | + * @return string |
|
11 | + */ |
|
12 | + protected function getModel() |
|
13 | + { |
|
14 | + return 'App\Modules\V1\Acl\AclUser'; |
|
15 | + } |
|
16 | + |
|
17 | + /** |
|
18 | + * Check if the logged in user or the given user |
|
19 | + * has the given permissions on the given model. |
|
20 | + * |
|
21 | + * @param string $nameOfPermission |
|
22 | + * @param string $model |
|
23 | + * @param boolean $user |
|
24 | + * @return boolean |
|
25 | + */ |
|
26 | + public function can($nameOfPermission, $model, $user = false ) |
|
27 | + { |
|
28 | + $user = $user ?: \JWTAuth::parseToken()->authenticate(); |
|
29 | + $permissions = []; |
|
30 | + \Core::users()->find($user->id, ['groups.permissions'])->groups->lists('permissions')->each(function ($permission) use (&$permissions, $model){ |
|
31 | + $permissions = array_merge($permissions, $permission->where('model', $model)->lists('name')->toArray()); |
|
32 | + }); |
|
33 | 33 | |
34 | - return in_array($nameOfPermission, $permissions); |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * Check if the logged in user has the given group. |
|
39 | - * |
|
40 | - * @param string $groupName |
|
41 | - * @return boolean |
|
42 | - */ |
|
43 | - public function hasGroup($groupName) |
|
44 | - { |
|
45 | - $groups = \Core::users()->find(\JWTAuth::parseToken()->authenticate()->id)->groups; |
|
46 | - return $groups->lists('name')->search($groupName, true) === false ? false : true; |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * Assign the given group ids to the given user. |
|
51 | - * |
|
52 | - * @param integer $user_id |
|
53 | - * @param array $group_ids |
|
54 | - * @return object |
|
55 | - */ |
|
56 | - public function assignGroups($user_id, $group_ids) |
|
57 | - { |
|
58 | - \DB::transaction(function () use ($user_id, $group_ids) { |
|
59 | - $user = \Core::users()->find($user_id); |
|
60 | - $user->groups()->detach(); |
|
61 | - $user->groups()->attach($group_ids); |
|
62 | - }); |
|
63 | - |
|
64 | - return \Core::users()->find($user_id); |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * Handle a login request to the application. |
|
69 | - * |
|
70 | - * @param array $credentials |
|
71 | - * @param boolean $adminLogin |
|
72 | - * @return string |
|
73 | - */ |
|
74 | - public function login($credentials, $adminLogin = false) |
|
75 | - { |
|
76 | - if ( ! $user = \Core::users()->first(['email' => $credentials['email']])) |
|
77 | - { |
|
78 | - \ErrorHandler::loginFailed(); |
|
79 | - } |
|
80 | - else if ($adminLogin && $user->groups->lists('name')->search('Admin', true) === false) |
|
81 | - { |
|
82 | - \ErrorHandler::loginFailed(); |
|
83 | - } |
|
84 | - else if ( ! $adminLogin && $user->groups->lists('name')->search('Admin', true) !== false) |
|
85 | - { |
|
86 | - \ErrorHandler::loginFailed(); |
|
87 | - } |
|
88 | - else if ($user->blocked) |
|
89 | - { |
|
90 | - \ErrorHandler::userIsBlocked(); |
|
91 | - } |
|
92 | - else if ($token = \JWTAuth::attempt($credentials)) |
|
93 | - { |
|
94 | - return ['token' => $token]; |
|
95 | - } |
|
96 | - else |
|
97 | - { |
|
98 | - \ErrorHandler::loginFailed(); |
|
99 | - } |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * Handle a registration request. |
|
104 | - * |
|
105 | - * @param array $credentials |
|
106 | - * @return string |
|
107 | - */ |
|
108 | - public function register($credentials) |
|
109 | - { |
|
110 | - $user = \Core::users()->model->create($credentials); |
|
111 | - $this->assignGroups($user->id, \Core::groups()->model->where('name', 'User')->select('id')->lists('id')->toArray()); |
|
112 | - |
|
113 | - return ['token' => \JWTAuth::fromUser($user)]; |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Logout the user. |
|
118 | - * |
|
119 | - * @return boolean |
|
120 | - */ |
|
121 | - public function logout() |
|
122 | - { |
|
123 | - return \JWTAuth::invalidate(\JWTAuth::getToken()); |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Block the user. |
|
128 | - * |
|
129 | - * @param integer $user_id |
|
130 | - * @return object |
|
131 | - */ |
|
132 | - public function block($user_id) |
|
133 | - { |
|
134 | - if ( ! $user = \Core::users()->find($user_id)) |
|
135 | - { |
|
136 | - \ErrorHandler::notFound('user'); |
|
137 | - } |
|
138 | - if ( ! $this->hasGroup('Admin')) |
|
139 | - { |
|
140 | - \ErrorHandler::noPermissions(); |
|
141 | - } |
|
142 | - else if (\JWTAuth::parseToken()->authenticate()->id == $user_id) |
|
143 | - { |
|
144 | - \ErrorHandler::noPermissions(); |
|
145 | - } |
|
146 | - else if ($user->groups->lists('name')->search('Admin', true) !== false) |
|
147 | - { |
|
148 | - \ErrorHandler::noPermissions(); |
|
149 | - } |
|
150 | - |
|
151 | - $user->blocked = 1; |
|
152 | - $user->save(); |
|
34 | + return in_array($nameOfPermission, $permissions); |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * Check if the logged in user has the given group. |
|
39 | + * |
|
40 | + * @param string $groupName |
|
41 | + * @return boolean |
|
42 | + */ |
|
43 | + public function hasGroup($groupName) |
|
44 | + { |
|
45 | + $groups = \Core::users()->find(\JWTAuth::parseToken()->authenticate()->id)->groups; |
|
46 | + return $groups->lists('name')->search($groupName, true) === false ? false : true; |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * Assign the given group ids to the given user. |
|
51 | + * |
|
52 | + * @param integer $user_id |
|
53 | + * @param array $group_ids |
|
54 | + * @return object |
|
55 | + */ |
|
56 | + public function assignGroups($user_id, $group_ids) |
|
57 | + { |
|
58 | + \DB::transaction(function () use ($user_id, $group_ids) { |
|
59 | + $user = \Core::users()->find($user_id); |
|
60 | + $user->groups()->detach(); |
|
61 | + $user->groups()->attach($group_ids); |
|
62 | + }); |
|
63 | + |
|
64 | + return \Core::users()->find($user_id); |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * Handle a login request to the application. |
|
69 | + * |
|
70 | + * @param array $credentials |
|
71 | + * @param boolean $adminLogin |
|
72 | + * @return string |
|
73 | + */ |
|
74 | + public function login($credentials, $adminLogin = false) |
|
75 | + { |
|
76 | + if ( ! $user = \Core::users()->first(['email' => $credentials['email']])) |
|
77 | + { |
|
78 | + \ErrorHandler::loginFailed(); |
|
79 | + } |
|
80 | + else if ($adminLogin && $user->groups->lists('name')->search('Admin', true) === false) |
|
81 | + { |
|
82 | + \ErrorHandler::loginFailed(); |
|
83 | + } |
|
84 | + else if ( ! $adminLogin && $user->groups->lists('name')->search('Admin', true) !== false) |
|
85 | + { |
|
86 | + \ErrorHandler::loginFailed(); |
|
87 | + } |
|
88 | + else if ($user->blocked) |
|
89 | + { |
|
90 | + \ErrorHandler::userIsBlocked(); |
|
91 | + } |
|
92 | + else if ($token = \JWTAuth::attempt($credentials)) |
|
93 | + { |
|
94 | + return ['token' => $token]; |
|
95 | + } |
|
96 | + else |
|
97 | + { |
|
98 | + \ErrorHandler::loginFailed(); |
|
99 | + } |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * Handle a registration request. |
|
104 | + * |
|
105 | + * @param array $credentials |
|
106 | + * @return string |
|
107 | + */ |
|
108 | + public function register($credentials) |
|
109 | + { |
|
110 | + $user = \Core::users()->model->create($credentials); |
|
111 | + $this->assignGroups($user->id, \Core::groups()->model->where('name', 'User')->select('id')->lists('id')->toArray()); |
|
112 | + |
|
113 | + return ['token' => \JWTAuth::fromUser($user)]; |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Logout the user. |
|
118 | + * |
|
119 | + * @return boolean |
|
120 | + */ |
|
121 | + public function logout() |
|
122 | + { |
|
123 | + return \JWTAuth::invalidate(\JWTAuth::getToken()); |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Block the user. |
|
128 | + * |
|
129 | + * @param integer $user_id |
|
130 | + * @return object |
|
131 | + */ |
|
132 | + public function block($user_id) |
|
133 | + { |
|
134 | + if ( ! $user = \Core::users()->find($user_id)) |
|
135 | + { |
|
136 | + \ErrorHandler::notFound('user'); |
|
137 | + } |
|
138 | + if ( ! $this->hasGroup('Admin')) |
|
139 | + { |
|
140 | + \ErrorHandler::noPermissions(); |
|
141 | + } |
|
142 | + else if (\JWTAuth::parseToken()->authenticate()->id == $user_id) |
|
143 | + { |
|
144 | + \ErrorHandler::noPermissions(); |
|
145 | + } |
|
146 | + else if ($user->groups->lists('name')->search('Admin', true) !== false) |
|
147 | + { |
|
148 | + \ErrorHandler::noPermissions(); |
|
149 | + } |
|
150 | + |
|
151 | + $user->blocked = 1; |
|
152 | + $user->save(); |
|
153 | 153 | |
154 | - return $user; |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * Unblock the user. |
|
159 | - * |
|
160 | - * @param integer $user_id |
|
161 | - * @return object |
|
162 | - */ |
|
163 | - public function unblock($user_id) |
|
164 | - { |
|
165 | - if ( ! $this->hasGroup('Admin')) |
|
166 | - { |
|
167 | - \ErrorHandler::noPermissions(); |
|
168 | - } |
|
169 | - |
|
170 | - $user = \Core::users()->find($user_id); |
|
171 | - $user->blocked = 0; |
|
172 | - $user->save(); |
|
173 | - |
|
174 | - return $user; |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * Send a reset link to the given user. |
|
179 | - * |
|
180 | - * @param string $url |
|
181 | - * @param string $email |
|
182 | - * @return void |
|
183 | - */ |
|
184 | - public function sendReset($email, $url) |
|
185 | - { |
|
186 | - view()->composer('auth.emails.password', function($view) use ($url) { |
|
187 | - $view->with(['url' => $url]); |
|
188 | - }); |
|
189 | - |
|
190 | - $response = \Password::sendResetLink($email, function (\Illuminate\Mail\Message $message) { |
|
191 | - $message->subject('Your Password Reset Link'); |
|
192 | - }); |
|
193 | - |
|
194 | - switch ($response) |
|
195 | - { |
|
196 | - case \Password::INVALID_USER: |
|
197 | - \ErrorHandler::notFound('email'); |
|
198 | - } |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * Reset the given user's password. |
|
203 | - * |
|
204 | - * @param array $credentials |
|
205 | - * @return integer |
|
206 | - */ |
|
207 | - public function resetPassword($credentials) |
|
208 | - { |
|
209 | - $token = false; |
|
210 | - $response = \Password::reset($credentials, function ($user, $password) use (&$token) { |
|
211 | - $user->password = bcrypt($password); |
|
212 | - $user->save(); |
|
213 | - |
|
214 | - $token = \JWTAuth::fromUser($user); |
|
215 | - }); |
|
216 | - |
|
217 | - |
|
218 | - switch ($response) { |
|
219 | - case \Password::PASSWORD_RESET: |
|
220 | - return $token; |
|
154 | + return $user; |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * Unblock the user. |
|
159 | + * |
|
160 | + * @param integer $user_id |
|
161 | + * @return object |
|
162 | + */ |
|
163 | + public function unblock($user_id) |
|
164 | + { |
|
165 | + if ( ! $this->hasGroup('Admin')) |
|
166 | + { |
|
167 | + \ErrorHandler::noPermissions(); |
|
168 | + } |
|
169 | + |
|
170 | + $user = \Core::users()->find($user_id); |
|
171 | + $user->blocked = 0; |
|
172 | + $user->save(); |
|
173 | + |
|
174 | + return $user; |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * Send a reset link to the given user. |
|
179 | + * |
|
180 | + * @param string $url |
|
181 | + * @param string $email |
|
182 | + * @return void |
|
183 | + */ |
|
184 | + public function sendReset($email, $url) |
|
185 | + { |
|
186 | + view()->composer('auth.emails.password', function($view) use ($url) { |
|
187 | + $view->with(['url' => $url]); |
|
188 | + }); |
|
189 | + |
|
190 | + $response = \Password::sendResetLink($email, function (\Illuminate\Mail\Message $message) { |
|
191 | + $message->subject('Your Password Reset Link'); |
|
192 | + }); |
|
193 | + |
|
194 | + switch ($response) |
|
195 | + { |
|
196 | + case \Password::INVALID_USER: |
|
197 | + \ErrorHandler::notFound('email'); |
|
198 | + } |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * Reset the given user's password. |
|
203 | + * |
|
204 | + * @param array $credentials |
|
205 | + * @return integer |
|
206 | + */ |
|
207 | + public function resetPassword($credentials) |
|
208 | + { |
|
209 | + $token = false; |
|
210 | + $response = \Password::reset($credentials, function ($user, $password) use (&$token) { |
|
211 | + $user->password = bcrypt($password); |
|
212 | + $user->save(); |
|
213 | + |
|
214 | + $token = \JWTAuth::fromUser($user); |
|
215 | + }); |
|
216 | + |
|
217 | + |
|
218 | + switch ($response) { |
|
219 | + case \Password::PASSWORD_RESET: |
|
220 | + return $token; |
|
221 | 221 | |
222 | - case \Password::INVALID_TOKEN: |
|
223 | - \ErrorHandler::invalidResetToken('token'); |
|
222 | + case \Password::INVALID_TOKEN: |
|
223 | + \ErrorHandler::invalidResetToken('token'); |
|
224 | 224 | |
225 | - case \Password::INVALID_PASSWORD: |
|
226 | - \ErrorHandler::invalidResetPassword('email'); |
|
225 | + case \Password::INVALID_PASSWORD: |
|
226 | + \ErrorHandler::invalidResetPassword('email'); |
|
227 | 227 | |
228 | - case \Password::INVALID_USER: |
|
229 | - \ErrorHandler::notFound('user'); |
|
228 | + case \Password::INVALID_USER: |
|
229 | + \ErrorHandler::notFound('user'); |
|
230 | 230 | |
231 | - default: |
|
232 | - \ErrorHandler::generalError(); |
|
233 | - } |
|
234 | - } |
|
231 | + default: |
|
232 | + \ErrorHandler::generalError(); |
|
233 | + } |
|
234 | + } |
|
235 | 235 | } |
@@ -5,37 +5,37 @@ |
||
5 | 5 | |
6 | 6 | class AclPermission extends Model { |
7 | 7 | |
8 | - use SoftDeletes; |
|
9 | - protected $table = 'permissions'; |
|
10 | - protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
11 | - protected $hidden = ['deleted_at']; |
|
12 | - protected $guarded = ['id']; |
|
13 | - protected $fillable = ['name', 'model']; |
|
14 | - public $searchable = ['name', 'model']; |
|
8 | + use SoftDeletes; |
|
9 | + protected $table = 'permissions'; |
|
10 | + protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
11 | + protected $hidden = ['deleted_at']; |
|
12 | + protected $guarded = ['id']; |
|
13 | + protected $fillable = ['name', 'model']; |
|
14 | + public $searchable = ['name', 'model']; |
|
15 | 15 | |
16 | - public function getCreatedAtAttribute($value) |
|
17 | - { |
|
18 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
19 | - } |
|
16 | + public function getCreatedAtAttribute($value) |
|
17 | + { |
|
18 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
19 | + } |
|
20 | 20 | |
21 | - public function getUpdatedAtAttribute($value) |
|
22 | - { |
|
23 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
24 | - } |
|
21 | + public function getUpdatedAtAttribute($value) |
|
22 | + { |
|
23 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
24 | + } |
|
25 | 25 | |
26 | - public function getDeletedAtAttribute($value) |
|
27 | - { |
|
28 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
29 | - } |
|
26 | + public function getDeletedAtAttribute($value) |
|
27 | + { |
|
28 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
29 | + } |
|
30 | 30 | |
31 | - public function groups() |
|
32 | - { |
|
33 | - return $this->belongsToMany('\App\Modules\V1\Acl\AclGroup','groups_permissions','permission_id','group_id')->whereNull('groups_permissions.deleted_at')->withTimestamps(); |
|
34 | - } |
|
31 | + public function groups() |
|
32 | + { |
|
33 | + return $this->belongsToMany('\App\Modules\V1\Acl\AclGroup','groups_permissions','permission_id','group_id')->whereNull('groups_permissions.deleted_at')->withTimestamps(); |
|
34 | + } |
|
35 | 35 | |
36 | - public static function boot() |
|
37 | - { |
|
38 | - parent::boot(); |
|
39 | - parent::observe(\App::make('App\Modules\V1\Acl\ModelObservers\AclPermissionObserver')); |
|
40 | - } |
|
36 | + public static function boot() |
|
37 | + { |
|
38 | + parent::boot(); |
|
39 | + parent::observe(\App::make('App\Modules\V1\Acl\ModelObservers\AclPermissionObserver')); |
|
40 | + } |
|
41 | 41 | } |
@@ -12,203 +12,203 @@ discard block |
||
12 | 12 | */ |
13 | 13 | public function up() |
14 | 14 | { |
15 | - /** |
|
16 | - * Delete previous permissions. |
|
17 | - */ |
|
15 | + /** |
|
16 | + * Delete previous permissions. |
|
17 | + */ |
|
18 | 18 | DB::table('permissions')->whereIn('model', ['users', 'permissions', 'groups'])->delete(); |
19 | 19 | |
20 | 20 | /** |
21 | - * Insert the permissions related to this module. |
|
22 | - */ |
|
23 | - DB::table('permissions')->insert( |
|
24 | - [ |
|
25 | - /** |
|
26 | - * Users model permissions. |
|
27 | - */ |
|
28 | - [ |
|
29 | - 'name' => 'save', |
|
30 | - 'model' => 'users', |
|
31 | - 'created_at' => \DB::raw('NOW()'), |
|
32 | - 'updated_at' => \DB::raw('NOW()') |
|
33 | - ], |
|
34 | - [ |
|
35 | - 'name' => 'delete', |
|
36 | - 'model' => 'users', |
|
37 | - 'created_at' => \DB::raw('NOW()'), |
|
38 | - 'updated_at' => \DB::raw('NOW()') |
|
39 | - ], |
|
40 | - [ |
|
41 | - 'name' => 'find', |
|
42 | - 'model' => 'users', |
|
43 | - 'created_at' => \DB::raw('NOW()'), |
|
44 | - 'updated_at' => \DB::raw('NOW()') |
|
45 | - ], |
|
46 | - [ |
|
47 | - 'name' => 'list', |
|
48 | - 'model' => 'users', |
|
49 | - 'created_at' => \DB::raw('NOW()'), |
|
50 | - 'updated_at' => \DB::raw('NOW()') |
|
51 | - ], |
|
52 | - [ |
|
53 | - 'name' => 'search', |
|
54 | - 'model' => 'users', |
|
55 | - 'created_at' => \DB::raw('NOW()'), |
|
56 | - 'updated_at' => \DB::raw('NOW()') |
|
57 | - ], |
|
58 | - [ |
|
59 | - 'name' => 'findby', |
|
60 | - 'model' => 'users', |
|
61 | - 'created_at' => \DB::raw('NOW()'), |
|
62 | - 'updated_at' => \DB::raw('NOW()') |
|
63 | - ], |
|
64 | - [ |
|
65 | - 'name' => 'first', |
|
66 | - 'model' => 'users', |
|
67 | - 'created_at' => \DB::raw('NOW()'), |
|
68 | - 'updated_at' => \DB::raw('NOW()') |
|
69 | - ], |
|
70 | - [ |
|
71 | - 'name' => 'paginate', |
|
72 | - 'model' => 'users', |
|
73 | - 'created_at' => \DB::raw('NOW()'), |
|
74 | - 'updated_at' => \DB::raw('NOW()') |
|
75 | - ], |
|
76 | - [ |
|
77 | - 'name' => 'paginateby', |
|
78 | - 'model' => 'users', |
|
79 | - 'created_at' => \DB::raw('NOW()'), |
|
80 | - 'updated_at' => \DB::raw('NOW()') |
|
81 | - ], |
|
82 | - [ |
|
83 | - 'name' => 'assigngroups', |
|
84 | - 'model' => 'users', |
|
85 | - 'created_at' => \DB::raw('NOW()'), |
|
86 | - 'updated_at' => \DB::raw('NOW()') |
|
87 | - ], |
|
88 | - [ |
|
89 | - 'name' => 'block', |
|
90 | - 'model' => 'users', |
|
91 | - 'created_at' => \DB::raw('NOW()'), |
|
92 | - 'updated_at' => \DB::raw('NOW()') |
|
93 | - ], |
|
94 | - [ |
|
95 | - 'name' => 'unblock', |
|
96 | - 'model' => 'users', |
|
97 | - 'created_at' => \DB::raw('NOW()'), |
|
98 | - 'updated_at' => \DB::raw('NOW()') |
|
99 | - ], |
|
21 | + * Insert the permissions related to this module. |
|
22 | + */ |
|
23 | + DB::table('permissions')->insert( |
|
24 | + [ |
|
25 | + /** |
|
26 | + * Users model permissions. |
|
27 | + */ |
|
28 | + [ |
|
29 | + 'name' => 'save', |
|
30 | + 'model' => 'users', |
|
31 | + 'created_at' => \DB::raw('NOW()'), |
|
32 | + 'updated_at' => \DB::raw('NOW()') |
|
33 | + ], |
|
34 | + [ |
|
35 | + 'name' => 'delete', |
|
36 | + 'model' => 'users', |
|
37 | + 'created_at' => \DB::raw('NOW()'), |
|
38 | + 'updated_at' => \DB::raw('NOW()') |
|
39 | + ], |
|
40 | + [ |
|
41 | + 'name' => 'find', |
|
42 | + 'model' => 'users', |
|
43 | + 'created_at' => \DB::raw('NOW()'), |
|
44 | + 'updated_at' => \DB::raw('NOW()') |
|
45 | + ], |
|
46 | + [ |
|
47 | + 'name' => 'list', |
|
48 | + 'model' => 'users', |
|
49 | + 'created_at' => \DB::raw('NOW()'), |
|
50 | + 'updated_at' => \DB::raw('NOW()') |
|
51 | + ], |
|
52 | + [ |
|
53 | + 'name' => 'search', |
|
54 | + 'model' => 'users', |
|
55 | + 'created_at' => \DB::raw('NOW()'), |
|
56 | + 'updated_at' => \DB::raw('NOW()') |
|
57 | + ], |
|
58 | + [ |
|
59 | + 'name' => 'findby', |
|
60 | + 'model' => 'users', |
|
61 | + 'created_at' => \DB::raw('NOW()'), |
|
62 | + 'updated_at' => \DB::raw('NOW()') |
|
63 | + ], |
|
64 | + [ |
|
65 | + 'name' => 'first', |
|
66 | + 'model' => 'users', |
|
67 | + 'created_at' => \DB::raw('NOW()'), |
|
68 | + 'updated_at' => \DB::raw('NOW()') |
|
69 | + ], |
|
70 | + [ |
|
71 | + 'name' => 'paginate', |
|
72 | + 'model' => 'users', |
|
73 | + 'created_at' => \DB::raw('NOW()'), |
|
74 | + 'updated_at' => \DB::raw('NOW()') |
|
75 | + ], |
|
76 | + [ |
|
77 | + 'name' => 'paginateby', |
|
78 | + 'model' => 'users', |
|
79 | + 'created_at' => \DB::raw('NOW()'), |
|
80 | + 'updated_at' => \DB::raw('NOW()') |
|
81 | + ], |
|
82 | + [ |
|
83 | + 'name' => 'assigngroups', |
|
84 | + 'model' => 'users', |
|
85 | + 'created_at' => \DB::raw('NOW()'), |
|
86 | + 'updated_at' => \DB::raw('NOW()') |
|
87 | + ], |
|
88 | + [ |
|
89 | + 'name' => 'block', |
|
90 | + 'model' => 'users', |
|
91 | + 'created_at' => \DB::raw('NOW()'), |
|
92 | + 'updated_at' => \DB::raw('NOW()') |
|
93 | + ], |
|
94 | + [ |
|
95 | + 'name' => 'unblock', |
|
96 | + 'model' => 'users', |
|
97 | + 'created_at' => \DB::raw('NOW()'), |
|
98 | + 'updated_at' => \DB::raw('NOW()') |
|
99 | + ], |
|
100 | 100 | |
101 | - /** |
|
102 | - * Permissions model permissions. |
|
103 | - */ |
|
104 | - [ |
|
105 | - 'name' => 'find', |
|
106 | - 'model' => 'permissions', |
|
107 | - 'created_at' => \DB::raw('NOW()'), |
|
108 | - 'updated_at' => \DB::raw('NOW()') |
|
109 | - ], |
|
110 | - [ |
|
111 | - 'name' => 'search', |
|
112 | - 'model' => 'permissions', |
|
113 | - 'created_at' => \DB::raw('NOW()'), |
|
114 | - 'updated_at' => \DB::raw('NOW()') |
|
115 | - ], |
|
116 | - [ |
|
117 | - 'name' => 'list', |
|
118 | - 'model' => 'permissions', |
|
119 | - 'created_at' => \DB::raw('NOW()'), |
|
120 | - 'updated_at' => \DB::raw('NOW()') |
|
121 | - ], |
|
122 | - [ |
|
123 | - 'name' => 'findby', |
|
124 | - 'model' => 'permissions', |
|
125 | - 'created_at' => \DB::raw('NOW()'), |
|
126 | - 'updated_at' => \DB::raw('NOW()') |
|
127 | - ], |
|
128 | - [ |
|
129 | - 'name' => 'first', |
|
130 | - 'model' => 'permissions', |
|
131 | - 'created_at' => \DB::raw('NOW()'), |
|
132 | - 'updated_at' => \DB::raw('NOW()') |
|
133 | - ], |
|
134 | - [ |
|
135 | - 'name' => 'paginate', |
|
136 | - 'model' => 'permissions', |
|
137 | - 'created_at' => \DB::raw('NOW()'), |
|
138 | - 'updated_at' => \DB::raw('NOW()') |
|
139 | - ], |
|
140 | - [ |
|
141 | - 'name' => 'paginateby', |
|
142 | - 'model' => 'permissions', |
|
143 | - 'created_at' => \DB::raw('NOW()'), |
|
144 | - 'updated_at' => \DB::raw('NOW()') |
|
145 | - ], |
|
101 | + /** |
|
102 | + * Permissions model permissions. |
|
103 | + */ |
|
104 | + [ |
|
105 | + 'name' => 'find', |
|
106 | + 'model' => 'permissions', |
|
107 | + 'created_at' => \DB::raw('NOW()'), |
|
108 | + 'updated_at' => \DB::raw('NOW()') |
|
109 | + ], |
|
110 | + [ |
|
111 | + 'name' => 'search', |
|
112 | + 'model' => 'permissions', |
|
113 | + 'created_at' => \DB::raw('NOW()'), |
|
114 | + 'updated_at' => \DB::raw('NOW()') |
|
115 | + ], |
|
116 | + [ |
|
117 | + 'name' => 'list', |
|
118 | + 'model' => 'permissions', |
|
119 | + 'created_at' => \DB::raw('NOW()'), |
|
120 | + 'updated_at' => \DB::raw('NOW()') |
|
121 | + ], |
|
122 | + [ |
|
123 | + 'name' => 'findby', |
|
124 | + 'model' => 'permissions', |
|
125 | + 'created_at' => \DB::raw('NOW()'), |
|
126 | + 'updated_at' => \DB::raw('NOW()') |
|
127 | + ], |
|
128 | + [ |
|
129 | + 'name' => 'first', |
|
130 | + 'model' => 'permissions', |
|
131 | + 'created_at' => \DB::raw('NOW()'), |
|
132 | + 'updated_at' => \DB::raw('NOW()') |
|
133 | + ], |
|
134 | + [ |
|
135 | + 'name' => 'paginate', |
|
136 | + 'model' => 'permissions', |
|
137 | + 'created_at' => \DB::raw('NOW()'), |
|
138 | + 'updated_at' => \DB::raw('NOW()') |
|
139 | + ], |
|
140 | + [ |
|
141 | + 'name' => 'paginateby', |
|
142 | + 'model' => 'permissions', |
|
143 | + 'created_at' => \DB::raw('NOW()'), |
|
144 | + 'updated_at' => \DB::raw('NOW()') |
|
145 | + ], |
|
146 | 146 | |
147 | - /** |
|
148 | - * Groups model permissions. |
|
149 | - */ |
|
150 | - [ |
|
151 | - 'name' => 'save', |
|
152 | - 'model' => 'groups', |
|
153 | - 'created_at' => \DB::raw('NOW()'), |
|
154 | - 'updated_at' => \DB::raw('NOW()') |
|
155 | - ], |
|
156 | - [ |
|
157 | - 'name' => 'delete', |
|
158 | - 'model' => 'groups', |
|
159 | - 'created_at' => \DB::raw('NOW()'), |
|
160 | - 'updated_at' => \DB::raw('NOW()') |
|
161 | - ], |
|
162 | - [ |
|
163 | - 'name' => 'find', |
|
164 | - 'model' => 'groups', |
|
165 | - 'created_at' => \DB::raw('NOW()'), |
|
166 | - 'updated_at' => \DB::raw('NOW()') |
|
167 | - ], |
|
168 | - [ |
|
169 | - 'name' => 'search', |
|
170 | - 'model' => 'groups', |
|
171 | - 'created_at' => \DB::raw('NOW()'), |
|
172 | - 'updated_at' => \DB::raw('NOW()') |
|
173 | - ], |
|
174 | - [ |
|
175 | - 'name' => 'list', |
|
176 | - 'model' => 'groups', |
|
177 | - 'created_at' => \DB::raw('NOW()'), |
|
178 | - 'updated_at' => \DB::raw('NOW()') |
|
179 | - ], |
|
180 | - [ |
|
181 | - 'name' => 'findby', |
|
182 | - 'model' => 'groups', |
|
183 | - 'created_at' => \DB::raw('NOW()'), |
|
184 | - 'updated_at' => \DB::raw('NOW()') |
|
185 | - ], |
|
186 | - [ |
|
187 | - 'name' => 'first', |
|
188 | - 'model' => 'groups', |
|
189 | - 'created_at' => \DB::raw('NOW()'), |
|
190 | - 'updated_at' => \DB::raw('NOW()') |
|
191 | - ], |
|
192 | - [ |
|
193 | - 'name' => 'paginate', |
|
194 | - 'model' => 'groups', |
|
195 | - 'created_at' => \DB::raw('NOW()'), |
|
196 | - 'updated_at' => \DB::raw('NOW()') |
|
197 | - ], |
|
198 | - [ |
|
199 | - 'name' => 'paginateby', |
|
200 | - 'model' => 'groups', |
|
201 | - 'created_at' => \DB::raw('NOW()'), |
|
202 | - 'updated_at' => \DB::raw('NOW()') |
|
203 | - ], |
|
204 | - [ |
|
205 | - 'name' => 'assignpermissions', |
|
206 | - 'model' => 'groups', |
|
207 | - 'created_at' => \DB::raw('NOW()'), |
|
208 | - 'updated_at' => \DB::raw('NOW()') |
|
209 | - ], |
|
210 | - ] |
|
211 | - ); |
|
147 | + /** |
|
148 | + * Groups model permissions. |
|
149 | + */ |
|
150 | + [ |
|
151 | + 'name' => 'save', |
|
152 | + 'model' => 'groups', |
|
153 | + 'created_at' => \DB::raw('NOW()'), |
|
154 | + 'updated_at' => \DB::raw('NOW()') |
|
155 | + ], |
|
156 | + [ |
|
157 | + 'name' => 'delete', |
|
158 | + 'model' => 'groups', |
|
159 | + 'created_at' => \DB::raw('NOW()'), |
|
160 | + 'updated_at' => \DB::raw('NOW()') |
|
161 | + ], |
|
162 | + [ |
|
163 | + 'name' => 'find', |
|
164 | + 'model' => 'groups', |
|
165 | + 'created_at' => \DB::raw('NOW()'), |
|
166 | + 'updated_at' => \DB::raw('NOW()') |
|
167 | + ], |
|
168 | + [ |
|
169 | + 'name' => 'search', |
|
170 | + 'model' => 'groups', |
|
171 | + 'created_at' => \DB::raw('NOW()'), |
|
172 | + 'updated_at' => \DB::raw('NOW()') |
|
173 | + ], |
|
174 | + [ |
|
175 | + 'name' => 'list', |
|
176 | + 'model' => 'groups', |
|
177 | + 'created_at' => \DB::raw('NOW()'), |
|
178 | + 'updated_at' => \DB::raw('NOW()') |
|
179 | + ], |
|
180 | + [ |
|
181 | + 'name' => 'findby', |
|
182 | + 'model' => 'groups', |
|
183 | + 'created_at' => \DB::raw('NOW()'), |
|
184 | + 'updated_at' => \DB::raw('NOW()') |
|
185 | + ], |
|
186 | + [ |
|
187 | + 'name' => 'first', |
|
188 | + 'model' => 'groups', |
|
189 | + 'created_at' => \DB::raw('NOW()'), |
|
190 | + 'updated_at' => \DB::raw('NOW()') |
|
191 | + ], |
|
192 | + [ |
|
193 | + 'name' => 'paginate', |
|
194 | + 'model' => 'groups', |
|
195 | + 'created_at' => \DB::raw('NOW()'), |
|
196 | + 'updated_at' => \DB::raw('NOW()') |
|
197 | + ], |
|
198 | + [ |
|
199 | + 'name' => 'paginateby', |
|
200 | + 'model' => 'groups', |
|
201 | + 'created_at' => \DB::raw('NOW()'), |
|
202 | + 'updated_at' => \DB::raw('NOW()') |
|
203 | + ], |
|
204 | + [ |
|
205 | + 'name' => 'assignpermissions', |
|
206 | + 'model' => 'groups', |
|
207 | + 'created_at' => \DB::raw('NOW()'), |
|
208 | + 'updated_at' => \DB::raw('NOW()') |
|
209 | + ], |
|
210 | + ] |
|
211 | + ); |
|
212 | 212 | |
213 | 213 | /** |
214 | 214 | * Delete previous data. |
@@ -236,29 +236,29 @@ discard block |
||
236 | 236 | * Create Default users. |
237 | 237 | */ |
238 | 238 | $adminUserId = DB::table('users')->insertGetId( |
239 | - [ |
|
239 | + [ |
|
240 | 240 | 'email' => '[email protected]', |
241 | 241 | 'password' => bcrypt('123456'), |
242 | 242 | 'created_at' => \DB::raw('NOW()'), |
243 | 243 | 'updated_at' => \DB::raw('NOW()') |
244 | 244 | ] |
245 | - ); |
|
245 | + ); |
|
246 | 246 | |
247 | 247 | /** |
248 | 248 | * Assign users to groups. |
249 | 249 | */ |
250 | 250 | DB::table('users_groups')->insert( |
251 | - [ |
|
252 | - [ |
|
251 | + [ |
|
252 | + [ |
|
253 | 253 | 'user_id' => $adminUserId, |
254 | 254 | 'group_id' => $adminGroupId, |
255 | 255 | 'created_at' => \DB::raw('NOW()'), |
256 | 256 | 'updated_at' => \DB::raw('NOW()') |
257 | - ] |
|
258 | - ] |
|
259 | - ); |
|
257 | + ] |
|
258 | + ] |
|
259 | + ); |
|
260 | 260 | |
261 | - /** |
|
261 | + /** |
|
262 | 262 | * Assign the permissions to the admin group. |
263 | 263 | */ |
264 | 264 | $permissionIds = DB::table('permissions')->whereIn('model', ['users', 'permissions', 'groups'])->select('id')->lists('id'); |