@@ -6,67 +6,67 @@ |
||
6 | 6 | class SettingsObserver |
7 | 7 | { |
8 | 8 | |
9 | - public function saving($model) |
|
10 | - { |
|
11 | - // |
|
12 | - } |
|
9 | + public function saving($model) |
|
10 | + { |
|
11 | + // |
|
12 | + } |
|
13 | 13 | |
14 | - public function saved($model) |
|
15 | - { |
|
16 | - // |
|
17 | - } |
|
14 | + public function saved($model) |
|
15 | + { |
|
16 | + // |
|
17 | + } |
|
18 | 18 | |
19 | - /** |
|
20 | - * Prevent the creating of the settings. |
|
21 | - * |
|
22 | - * @param object $model the model beign created. |
|
23 | - * @return void |
|
24 | - */ |
|
25 | - public function creating($model) |
|
26 | - { |
|
27 | - \Errors::cannotCreateSetting(); |
|
28 | - } |
|
19 | + /** |
|
20 | + * Prevent the creating of the settings. |
|
21 | + * |
|
22 | + * @param object $model the model beign created. |
|
23 | + * @return void |
|
24 | + */ |
|
25 | + public function creating($model) |
|
26 | + { |
|
27 | + \Errors::cannotCreateSetting(); |
|
28 | + } |
|
29 | 29 | |
30 | - public function created($model) |
|
31 | - { |
|
32 | - // |
|
33 | - } |
|
30 | + public function created($model) |
|
31 | + { |
|
32 | + // |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * Prevent updating of the setting key. |
|
37 | - * |
|
38 | - * @param object $model the model beign updated. |
|
39 | - * @return void |
|
40 | - */ |
|
41 | - public function updating($model) |
|
42 | - { |
|
43 | - if ($model->getOriginal('key') !== $model->key) { |
|
44 | - \Errors::cannotUpdateSettingKey(); |
|
45 | - } |
|
46 | - } |
|
35 | + /** |
|
36 | + * Prevent updating of the setting key. |
|
37 | + * |
|
38 | + * @param object $model the model beign updated. |
|
39 | + * @return void |
|
40 | + */ |
|
41 | + public function updating($model) |
|
42 | + { |
|
43 | + if ($model->getOriginal('key') !== $model->key) { |
|
44 | + \Errors::cannotUpdateSettingKey(); |
|
45 | + } |
|
46 | + } |
|
47 | 47 | |
48 | - public function updated($model) |
|
49 | - { |
|
50 | - // |
|
51 | - } |
|
48 | + public function updated($model) |
|
49 | + { |
|
50 | + // |
|
51 | + } |
|
52 | 52 | |
53 | - public function deleting($model) |
|
54 | - { |
|
55 | - // |
|
56 | - } |
|
53 | + public function deleting($model) |
|
54 | + { |
|
55 | + // |
|
56 | + } |
|
57 | 57 | |
58 | - public function deleted($model) |
|
59 | - { |
|
60 | - // |
|
61 | - } |
|
58 | + public function deleted($model) |
|
59 | + { |
|
60 | + // |
|
61 | + } |
|
62 | 62 | |
63 | - public function restoring($model) |
|
64 | - { |
|
65 | - // |
|
66 | - } |
|
63 | + public function restoring($model) |
|
64 | + { |
|
65 | + // |
|
66 | + } |
|
67 | 67 | |
68 | - public function restored($model) |
|
69 | - { |
|
70 | - // |
|
71 | - } |
|
68 | + public function restored($model) |
|
69 | + { |
|
70 | + // |
|
71 | + } |
|
72 | 72 | } |
@@ -6,676 +6,676 @@ |
||
6 | 6 | |
7 | 7 | abstract class BaseRepository implements BaseRepositoryInterface |
8 | 8 | { |
9 | - /** |
|
10 | - * @var object |
|
11 | - */ |
|
12 | - public $model; |
|
9 | + /** |
|
10 | + * @var object |
|
11 | + */ |
|
12 | + public $model; |
|
13 | 13 | |
14 | - /** |
|
15 | - * Init new object. |
|
16 | - * |
|
17 | - * @var mixed model |
|
18 | - * @return void |
|
19 | - */ |
|
20 | - public function __construct($model) |
|
21 | - { |
|
22 | - $this->model = $model; |
|
23 | - } |
|
24 | - |
|
25 | - /** |
|
26 | - * Fetch records with relations based on the given params. |
|
27 | - * |
|
28 | - * @param string $relations |
|
29 | - * @param array $conditions |
|
30 | - * @param integer $perPage |
|
31 | - * @param string $sortBy |
|
32 | - * @param boolean $desc |
|
33 | - * @return collection |
|
34 | - */ |
|
35 | - public function list($relations = [], $conditions = false, $perPage = 15, $sortBy = 'created_at', $desc = true) |
|
36 | - { |
|
37 | - unset($conditions['perPage']); |
|
38 | - unset($conditions['sortBy']); |
|
39 | - unset($conditions['sort']); |
|
40 | - unset($conditions['page']); |
|
41 | - |
|
42 | - if (count($conditions)) { |
|
43 | - return $this->paginateBy(['and' => $conditions], $perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
44 | - } |
|
45 | - |
|
46 | - return $this->paginate($perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * Fetch all records with relations from the storage. |
|
51 | - * |
|
52 | - * @param array $relations |
|
53 | - * @param string $sortBy |
|
54 | - * @param boolean $desc |
|
55 | - * @param array $columns |
|
56 | - * @return collection |
|
57 | - */ |
|
58 | - public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
59 | - { |
|
60 | - $sort = $desc ? 'desc' : 'asc'; |
|
61 | - return $this->model->with($relations)->orderBy($sortBy, $sort)->get($columns); |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * Fetch all records with relations from storage in pages |
|
66 | - * that matche the given query. |
|
67 | - * |
|
68 | - * @param string $query |
|
69 | - * @param integer $perPage |
|
70 | - * @param array $relations |
|
71 | - * @param string $sortBy |
|
72 | - * @param boolean $desc |
|
73 | - * @param array $columns |
|
74 | - * @return collection |
|
75 | - */ |
|
76 | - public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
77 | - { |
|
78 | - $model = $this->model->with($relations); |
|
79 | - $conditionColumns = $this->model->searchable; |
|
80 | - $sort = $desc ? 'desc' : 'asc'; |
|
81 | - |
|
82 | - /** |
|
83 | - * Construct the select conditions for the model. |
|
84 | - */ |
|
85 | - $model->where(function ($q) use ($query, $conditionColumns, $relations) { |
|
86 | - |
|
87 | - if (count($conditionColumns)) { |
|
88 | - $column = 'LOWER('.array_shift($conditionColumns).')'; |
|
89 | - if (Str::contains($column, '->')) { |
|
90 | - $column = $this->wrapJsonSelector($column); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Use the first element in the model columns to construct the first condition. |
|
95 | - */ |
|
96 | - $q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Loop through the rest of the columns to construct or where conditions. |
|
101 | - */ |
|
102 | - foreach ($conditionColumns as $column) { |
|
103 | - $column = 'LOWER('.$column.')'; |
|
104 | - if (Str::contains($column, '->')) { |
|
105 | - $column = $this->wrapJsonSelector($column); |
|
106 | - } |
|
107 | - |
|
108 | - $q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * Loop through the model relations. |
|
113 | - */ |
|
114 | - foreach ($relations as $relation) { |
|
115 | - /** |
|
116 | - * Remove the sub relation if exists. |
|
117 | - */ |
|
118 | - $relation = explode('.', $relation)[0]; |
|
119 | - |
|
120 | - /** |
|
121 | - * Try to fetch the relation repository from the core. |
|
122 | - */ |
|
123 | - if (\Core::$relation()) { |
|
124 | - /** |
|
125 | - * Construct the relation condition. |
|
126 | - */ |
|
127 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation) { |
|
128 | - |
|
129 | - $subModel->where(function ($q) use ($query, $relation) { |
|
130 | - |
|
131 | - /** |
|
132 | - * Get columns of the relation. |
|
133 | - */ |
|
134 | - $subConditionColumns = \Core::$relation()->model->searchable; |
|
135 | - |
|
136 | - if (count($subConditionColumns)) { |
|
137 | - $column = 'LOWER('.array_shift($subConditionColumns).')'; |
|
138 | - if (Str::contains($column, '->')) { |
|
139 | - $column = $this->wrapJsonSelector($column); |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Use the first element in the relation model columns to construct the first condition. |
|
144 | - */ |
|
145 | - $q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * Loop through the rest of the columns to construct or where conditions. |
|
150 | - */ |
|
151 | - foreach ($subConditionColumns as $subConditionColumn) { |
|
152 | - $column = 'LOWER('.$subConditionColumn.')'; |
|
153 | - if (Str::contains($column, '->')) { |
|
154 | - $column = $this->wrapJsonSelector($column); |
|
155 | - } |
|
14 | + /** |
|
15 | + * Init new object. |
|
16 | + * |
|
17 | + * @var mixed model |
|
18 | + * @return void |
|
19 | + */ |
|
20 | + public function __construct($model) |
|
21 | + { |
|
22 | + $this->model = $model; |
|
23 | + } |
|
24 | + |
|
25 | + /** |
|
26 | + * Fetch records with relations based on the given params. |
|
27 | + * |
|
28 | + * @param string $relations |
|
29 | + * @param array $conditions |
|
30 | + * @param integer $perPage |
|
31 | + * @param string $sortBy |
|
32 | + * @param boolean $desc |
|
33 | + * @return collection |
|
34 | + */ |
|
35 | + public function list($relations = [], $conditions = false, $perPage = 15, $sortBy = 'created_at', $desc = true) |
|
36 | + { |
|
37 | + unset($conditions['perPage']); |
|
38 | + unset($conditions['sortBy']); |
|
39 | + unset($conditions['sort']); |
|
40 | + unset($conditions['page']); |
|
41 | + |
|
42 | + if (count($conditions)) { |
|
43 | + return $this->paginateBy(['and' => $conditions], $perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
44 | + } |
|
45 | + |
|
46 | + return $this->paginate($perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * Fetch all records with relations from the storage. |
|
51 | + * |
|
52 | + * @param array $relations |
|
53 | + * @param string $sortBy |
|
54 | + * @param boolean $desc |
|
55 | + * @param array $columns |
|
56 | + * @return collection |
|
57 | + */ |
|
58 | + public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
59 | + { |
|
60 | + $sort = $desc ? 'desc' : 'asc'; |
|
61 | + return $this->model->with($relations)->orderBy($sortBy, $sort)->get($columns); |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * Fetch all records with relations from storage in pages |
|
66 | + * that matche the given query. |
|
67 | + * |
|
68 | + * @param string $query |
|
69 | + * @param integer $perPage |
|
70 | + * @param array $relations |
|
71 | + * @param string $sortBy |
|
72 | + * @param boolean $desc |
|
73 | + * @param array $columns |
|
74 | + * @return collection |
|
75 | + */ |
|
76 | + public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
77 | + { |
|
78 | + $model = $this->model->with($relations); |
|
79 | + $conditionColumns = $this->model->searchable; |
|
80 | + $sort = $desc ? 'desc' : 'asc'; |
|
81 | + |
|
82 | + /** |
|
83 | + * Construct the select conditions for the model. |
|
84 | + */ |
|
85 | + $model->where(function ($q) use ($query, $conditionColumns, $relations) { |
|
86 | + |
|
87 | + if (count($conditionColumns)) { |
|
88 | + $column = 'LOWER('.array_shift($conditionColumns).')'; |
|
89 | + if (Str::contains($column, '->')) { |
|
90 | + $column = $this->wrapJsonSelector($column); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Use the first element in the model columns to construct the first condition. |
|
95 | + */ |
|
96 | + $q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Loop through the rest of the columns to construct or where conditions. |
|
101 | + */ |
|
102 | + foreach ($conditionColumns as $column) { |
|
103 | + $column = 'LOWER('.$column.')'; |
|
104 | + if (Str::contains($column, '->')) { |
|
105 | + $column = $this->wrapJsonSelector($column); |
|
106 | + } |
|
107 | + |
|
108 | + $q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * Loop through the model relations. |
|
113 | + */ |
|
114 | + foreach ($relations as $relation) { |
|
115 | + /** |
|
116 | + * Remove the sub relation if exists. |
|
117 | + */ |
|
118 | + $relation = explode('.', $relation)[0]; |
|
119 | + |
|
120 | + /** |
|
121 | + * Try to fetch the relation repository from the core. |
|
122 | + */ |
|
123 | + if (\Core::$relation()) { |
|
124 | + /** |
|
125 | + * Construct the relation condition. |
|
126 | + */ |
|
127 | + $q->orWhereHas($relation, function ($subModel) use ($query, $relation) { |
|
128 | + |
|
129 | + $subModel->where(function ($q) use ($query, $relation) { |
|
130 | + |
|
131 | + /** |
|
132 | + * Get columns of the relation. |
|
133 | + */ |
|
134 | + $subConditionColumns = \Core::$relation()->model->searchable; |
|
135 | + |
|
136 | + if (count($subConditionColumns)) { |
|
137 | + $column = 'LOWER('.array_shift($subConditionColumns).')'; |
|
138 | + if (Str::contains($column, '->')) { |
|
139 | + $column = $this->wrapJsonSelector($column); |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Use the first element in the relation model columns to construct the first condition. |
|
144 | + */ |
|
145 | + $q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * Loop through the rest of the columns to construct or where conditions. |
|
150 | + */ |
|
151 | + foreach ($subConditionColumns as $subConditionColumn) { |
|
152 | + $column = 'LOWER('.$subConditionColumn.')'; |
|
153 | + if (Str::contains($column, '->')) { |
|
154 | + $column = $this->wrapJsonSelector($column); |
|
155 | + } |
|
156 | 156 | |
157 | - $q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
158 | - } |
|
159 | - }); |
|
160 | - }); |
|
161 | - } |
|
162 | - } |
|
163 | - }); |
|
157 | + $q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
158 | + } |
|
159 | + }); |
|
160 | + }); |
|
161 | + } |
|
162 | + } |
|
163 | + }); |
|
164 | 164 | |
165 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
166 | - } |
|
165 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
166 | + } |
|
167 | 167 | |
168 | - /** |
|
169 | - * Fetch all records with relations from storage in pages. |
|
170 | - * |
|
171 | - * @param integer $perPage |
|
172 | - * @param array $relations |
|
173 | - * @param string $sortBy |
|
174 | - * @param boolean $desc |
|
175 | - * @param array $columns |
|
176 | - * @return collection |
|
177 | - */ |
|
178 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
179 | - { |
|
180 | - $sort = $desc ? 'desc' : 'asc'; |
|
181 | - return $this->model->with($relations)->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * Fetch all records with relations based on |
|
186 | - * the given condition from storage in pages. |
|
187 | - * |
|
188 | - * @param array $conditions array of conditions |
|
189 | - * @param integer $perPage |
|
190 | - * @param array $relations |
|
191 | - * @param string $sortBy |
|
192 | - * @param boolean $desc |
|
193 | - * @param array $columns |
|
194 | - * @return collection |
|
195 | - */ |
|
196 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
197 | - { |
|
198 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
199 | - $sort = $desc ? 'desc' : 'asc'; |
|
200 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
201 | - } |
|
168 | + /** |
|
169 | + * Fetch all records with relations from storage in pages. |
|
170 | + * |
|
171 | + * @param integer $perPage |
|
172 | + * @param array $relations |
|
173 | + * @param string $sortBy |
|
174 | + * @param boolean $desc |
|
175 | + * @param array $columns |
|
176 | + * @return collection |
|
177 | + */ |
|
178 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
179 | + { |
|
180 | + $sort = $desc ? 'desc' : 'asc'; |
|
181 | + return $this->model->with($relations)->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * Fetch all records with relations based on |
|
186 | + * the given condition from storage in pages. |
|
187 | + * |
|
188 | + * @param array $conditions array of conditions |
|
189 | + * @param integer $perPage |
|
190 | + * @param array $relations |
|
191 | + * @param string $sortBy |
|
192 | + * @param boolean $desc |
|
193 | + * @param array $columns |
|
194 | + * @return collection |
|
195 | + */ |
|
196 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
197 | + { |
|
198 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
199 | + $sort = $desc ? 'desc' : 'asc'; |
|
200 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
201 | + } |
|
202 | 202 | |
203 | - /** |
|
204 | - * Save the given model to the storage. |
|
205 | - * |
|
206 | - * @param array $data |
|
207 | - * @return mixed |
|
208 | - */ |
|
209 | - public function save(array $data) |
|
210 | - { |
|
211 | - \Session::put('locale', 'all'); |
|
212 | - $model = false; |
|
213 | - $relations = []; |
|
214 | - |
|
215 | - \DB::transaction(function () use (&$model, $relations, $data) { |
|
203 | + /** |
|
204 | + * Save the given model to the storage. |
|
205 | + * |
|
206 | + * @param array $data |
|
207 | + * @return mixed |
|
208 | + */ |
|
209 | + public function save(array $data) |
|
210 | + { |
|
211 | + \Session::put('locale', 'all'); |
|
212 | + $model = false; |
|
213 | + $relations = []; |
|
214 | + |
|
215 | + \DB::transaction(function () use (&$model, $relations, $data) { |
|
216 | 216 | |
217 | - $model = $this->prepareModel($data); |
|
218 | - $relations = $this->prepareRelations($data, $model); |
|
219 | - $model = $this->saveModel($model, $relations); |
|
220 | - }); |
|
217 | + $model = $this->prepareModel($data); |
|
218 | + $relations = $this->prepareRelations($data, $model); |
|
219 | + $model = $this->saveModel($model, $relations); |
|
220 | + }); |
|
221 | 221 | |
222 | - return $model; |
|
223 | - } |
|
224 | - |
|
225 | - /** |
|
226 | - * Delete record from the storage based on the given |
|
227 | - * condition. |
|
228 | - * |
|
229 | - * @param var $value condition value |
|
230 | - * @param string $attribute condition column name |
|
231 | - * @return void |
|
232 | - */ |
|
233 | - public function delete($value, $attribute = 'id') |
|
234 | - { |
|
235 | - if ($attribute == 'id') { |
|
236 | - \DB::transaction(function () use ($value) { |
|
237 | - $model = $this->model->lockForUpdate()->find($value); |
|
238 | - if (! $model) { |
|
239 | - \Errors::notFound(class_basename($this->model).' with id : '.$value); |
|
240 | - } |
|
222 | + return $model; |
|
223 | + } |
|
224 | + |
|
225 | + /** |
|
226 | + * Delete record from the storage based on the given |
|
227 | + * condition. |
|
228 | + * |
|
229 | + * @param var $value condition value |
|
230 | + * @param string $attribute condition column name |
|
231 | + * @return void |
|
232 | + */ |
|
233 | + public function delete($value, $attribute = 'id') |
|
234 | + { |
|
235 | + if ($attribute == 'id') { |
|
236 | + \DB::transaction(function () use ($value) { |
|
237 | + $model = $this->model->lockForUpdate()->find($value); |
|
238 | + if (! $model) { |
|
239 | + \Errors::notFound(class_basename($this->model).' with id : '.$value); |
|
240 | + } |
|
241 | 241 | |
242 | - $model->delete(); |
|
243 | - }); |
|
244 | - } else { |
|
245 | - \DB::transaction(function () use ($value, $attribute) { |
|
246 | - $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
247 | - $model->delete(); |
|
248 | - }); |
|
249 | - }); |
|
250 | - } |
|
251 | - } |
|
242 | + $model->delete(); |
|
243 | + }); |
|
244 | + } else { |
|
245 | + \DB::transaction(function () use ($value, $attribute) { |
|
246 | + $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
247 | + $model->delete(); |
|
248 | + }); |
|
249 | + }); |
|
250 | + } |
|
251 | + } |
|
252 | 252 | |
253 | - /** |
|
254 | - * Fetch records from the storage based on the given |
|
255 | - * id. |
|
256 | - * |
|
257 | - * @param integer $id |
|
258 | - * @param string[] $relations |
|
259 | - * @param array $columns |
|
260 | - * @return object |
|
261 | - */ |
|
262 | - public function find($id, $relations = [], $columns = ['*']) |
|
263 | - { |
|
264 | - return $this->model->with($relations)->find($id, $columns); |
|
265 | - } |
|
253 | + /** |
|
254 | + * Fetch records from the storage based on the given |
|
255 | + * id. |
|
256 | + * |
|
257 | + * @param integer $id |
|
258 | + * @param string[] $relations |
|
259 | + * @param array $columns |
|
260 | + * @return object |
|
261 | + */ |
|
262 | + public function find($id, $relations = [], $columns = ['*']) |
|
263 | + { |
|
264 | + return $this->model->with($relations)->find($id, $columns); |
|
265 | + } |
|
266 | 266 | |
267 | - /** |
|
268 | - * Fetch records from the storage based on the given |
|
269 | - * condition. |
|
270 | - * |
|
271 | - * @param array $conditions array of conditions |
|
272 | - * @param array $relations |
|
273 | - * @param string $sortBy |
|
274 | - * @param boolean $desc |
|
275 | - * @param array $columns |
|
276 | - * @return collection |
|
277 | - */ |
|
278 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
279 | - { |
|
280 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
281 | - $sort = $desc ? 'desc' : 'asc'; |
|
282 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
283 | - } |
|
284 | - |
|
285 | - /** |
|
286 | - * Fetch the first record from the storage based on the given |
|
287 | - * condition. |
|
288 | - * |
|
289 | - * @param array $conditions array of conditions |
|
290 | - * @param array $relations |
|
291 | - * @param array $columns |
|
292 | - * @return object |
|
293 | - */ |
|
294 | - public function first($conditions, $relations = [], $columns = ['*']) |
|
295 | - { |
|
296 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
297 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
298 | - } |
|
299 | - |
|
300 | - /** |
|
301 | - * Return the deleted models in pages based on the given conditions. |
|
302 | - * |
|
303 | - * @param array $conditions array of conditions |
|
304 | - * @param integer $perPage |
|
305 | - * @param string $sortBy |
|
306 | - * @param boolean $desc |
|
307 | - * @param array $columns |
|
308 | - * @return collection |
|
309 | - */ |
|
310 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
311 | - { |
|
312 | - unset($conditions['page']); |
|
313 | - unset($conditions['perPage']); |
|
314 | - unset($conditions['sortBy']); |
|
315 | - unset($conditions['sort']); |
|
316 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
317 | - $sort = $desc ? 'desc' : 'asc'; |
|
318 | - $model = $this->model->onlyTrashed(); |
|
319 | - |
|
320 | - if (count($conditions['conditionValues'])) { |
|
321 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
322 | - } |
|
323 | - |
|
324 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
325 | - } |
|
326 | - |
|
327 | - /** |
|
328 | - * Restore the deleted model. |
|
329 | - * |
|
330 | - * @param integer $id |
|
331 | - * @return void |
|
332 | - */ |
|
333 | - public function restore($id) |
|
334 | - { |
|
335 | - $model = $this->model->onlyTrashed()->find($id); |
|
336 | - |
|
337 | - if (! $model) { |
|
338 | - \Errors::notFound(class_basename($this->model).' with id : '.$id); |
|
339 | - } |
|
340 | - |
|
341 | - $model->restore(); |
|
342 | - } |
|
343 | - |
|
344 | - /** |
|
345 | - * Fill the model with the given data. |
|
346 | - * |
|
347 | - * @param array $data |
|
348 | - * |
|
349 | - * @return object |
|
350 | - */ |
|
351 | - public function prepareModel($data) |
|
352 | - { |
|
353 | - $modelClass = $this->model; |
|
354 | - |
|
355 | - /** |
|
356 | - * If the id is present in the data then select the model for updating, |
|
357 | - * else create new model. |
|
358 | - * @var array |
|
359 | - */ |
|
360 | - $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
361 | - if (! $model) { |
|
362 | - \Errors::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
363 | - } |
|
364 | - |
|
365 | - /** |
|
366 | - * Construct the model object with the given data, |
|
367 | - * and if there is a relation add it to relations array, |
|
368 | - * then save the model. |
|
369 | - */ |
|
370 | - foreach ($data as $key => $value) { |
|
371 | - if (array_search($key, $model->getFillable(), true) !== false) { |
|
372 | - /** |
|
373 | - * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
374 | - */ |
|
375 | - $model->$key = $value; |
|
376 | - } |
|
377 | - } |
|
378 | - |
|
379 | - return $model; |
|
380 | - } |
|
267 | + /** |
|
268 | + * Fetch records from the storage based on the given |
|
269 | + * condition. |
|
270 | + * |
|
271 | + * @param array $conditions array of conditions |
|
272 | + * @param array $relations |
|
273 | + * @param string $sortBy |
|
274 | + * @param boolean $desc |
|
275 | + * @param array $columns |
|
276 | + * @return collection |
|
277 | + */ |
|
278 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
279 | + { |
|
280 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
281 | + $sort = $desc ? 'desc' : 'asc'; |
|
282 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
283 | + } |
|
284 | + |
|
285 | + /** |
|
286 | + * Fetch the first record from the storage based on the given |
|
287 | + * condition. |
|
288 | + * |
|
289 | + * @param array $conditions array of conditions |
|
290 | + * @param array $relations |
|
291 | + * @param array $columns |
|
292 | + * @return object |
|
293 | + */ |
|
294 | + public function first($conditions, $relations = [], $columns = ['*']) |
|
295 | + { |
|
296 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
297 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
298 | + } |
|
299 | + |
|
300 | + /** |
|
301 | + * Return the deleted models in pages based on the given conditions. |
|
302 | + * |
|
303 | + * @param array $conditions array of conditions |
|
304 | + * @param integer $perPage |
|
305 | + * @param string $sortBy |
|
306 | + * @param boolean $desc |
|
307 | + * @param array $columns |
|
308 | + * @return collection |
|
309 | + */ |
|
310 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
311 | + { |
|
312 | + unset($conditions['page']); |
|
313 | + unset($conditions['perPage']); |
|
314 | + unset($conditions['sortBy']); |
|
315 | + unset($conditions['sort']); |
|
316 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
317 | + $sort = $desc ? 'desc' : 'asc'; |
|
318 | + $model = $this->model->onlyTrashed(); |
|
319 | + |
|
320 | + if (count($conditions['conditionValues'])) { |
|
321 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
322 | + } |
|
323 | + |
|
324 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
325 | + } |
|
326 | + |
|
327 | + /** |
|
328 | + * Restore the deleted model. |
|
329 | + * |
|
330 | + * @param integer $id |
|
331 | + * @return void |
|
332 | + */ |
|
333 | + public function restore($id) |
|
334 | + { |
|
335 | + $model = $this->model->onlyTrashed()->find($id); |
|
336 | + |
|
337 | + if (! $model) { |
|
338 | + \Errors::notFound(class_basename($this->model).' with id : '.$id); |
|
339 | + } |
|
340 | + |
|
341 | + $model->restore(); |
|
342 | + } |
|
343 | + |
|
344 | + /** |
|
345 | + * Fill the model with the given data. |
|
346 | + * |
|
347 | + * @param array $data |
|
348 | + * |
|
349 | + * @return object |
|
350 | + */ |
|
351 | + public function prepareModel($data) |
|
352 | + { |
|
353 | + $modelClass = $this->model; |
|
354 | + |
|
355 | + /** |
|
356 | + * If the id is present in the data then select the model for updating, |
|
357 | + * else create new model. |
|
358 | + * @var array |
|
359 | + */ |
|
360 | + $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
361 | + if (! $model) { |
|
362 | + \Errors::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
363 | + } |
|
364 | + |
|
365 | + /** |
|
366 | + * Construct the model object with the given data, |
|
367 | + * and if there is a relation add it to relations array, |
|
368 | + * then save the model. |
|
369 | + */ |
|
370 | + foreach ($data as $key => $value) { |
|
371 | + if (array_search($key, $model->getFillable(), true) !== false) { |
|
372 | + /** |
|
373 | + * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
374 | + */ |
|
375 | + $model->$key = $value; |
|
376 | + } |
|
377 | + } |
|
378 | + |
|
379 | + return $model; |
|
380 | + } |
|
381 | 381 | |
382 | - /** |
|
383 | - * Prepare related models based on the given data for the given model. |
|
384 | - * |
|
385 | - * @param array $data |
|
386 | - * @param object $model |
|
387 | - * |
|
388 | - * @return array |
|
389 | - */ |
|
390 | - public function prepareRelations($data, $model) |
|
391 | - { |
|
392 | - /** |
|
393 | - * Construct the model object with the given data, |
|
394 | - * and if there is a relation add it to relations array, |
|
395 | - * then save the model. |
|
396 | - */ |
|
397 | - foreach ($data as $key => $value) { |
|
398 | - /** |
|
399 | - * If the attribute is a relation. |
|
400 | - */ |
|
401 | - $relation = \Str::camel($key); |
|
402 | - if (method_exists($model, $relation) && \Core::$relation()) { |
|
403 | - /** |
|
404 | - * Check if the relation is a collection. |
|
405 | - */ |
|
406 | - if (class_basename($model->$relation) == 'Collection') { |
|
407 | - /** |
|
408 | - * If the relation has no value then marke the relation data |
|
409 | - * related to the model to be deleted. |
|
410 | - */ |
|
411 | - if (! $value || ! count($value)) { |
|
412 | - $relations[$relation] = 'delete'; |
|
413 | - } |
|
414 | - } |
|
415 | - if (is_array($value)) { |
|
416 | - /** |
|
417 | - * Loop through the relation data. |
|
418 | - */ |
|
419 | - foreach ($value as $attr => $val) { |
|
420 | - /** |
|
421 | - * Get the relation model. |
|
422 | - */ |
|
423 | - $relationBaseModel = \Core::$relation()->model; |
|
424 | - |
|
425 | - /** |
|
426 | - * Check if the relation is a collection. |
|
427 | - */ |
|
428 | - if (class_basename($model->$relation) == 'Collection') { |
|
429 | - /** |
|
430 | - * If the id is present in the data then select the relation model for updating, |
|
431 | - * else create new model. |
|
432 | - */ |
|
433 | - $relationModel = Arr::has($val, 'id') ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
434 | - |
|
435 | - /** |
|
436 | - * If model doesn't exists. |
|
437 | - */ |
|
438 | - if (! $relationModel) { |
|
439 | - \Errors::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
440 | - } |
|
441 | - |
|
442 | - /** |
|
443 | - * Loop through the relation attributes. |
|
444 | - */ |
|
445 | - foreach ($val as $attr => $val) { |
|
446 | - /** |
|
447 | - * Prevent the sub relations or attributes not in the fillable. |
|
448 | - */ |
|
449 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) { |
|
450 | - $relationModel->$attr = $val; |
|
451 | - } |
|
452 | - } |
|
453 | - |
|
454 | - $relations[$relation][] = $relationModel; |
|
455 | - } else { |
|
456 | - /** |
|
457 | - * Prevent the sub relations. |
|
458 | - */ |
|
459 | - if (gettype($val) !== 'object' && gettype($val) !== 'array') { |
|
460 | - /** |
|
461 | - * If the id is present in the data then select the relation model for updating, |
|
462 | - * else create new model. |
|
463 | - */ |
|
464 | - $relationModel = Arr::has($value, 'id') ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
465 | - |
|
466 | - /** |
|
467 | - * If model doesn't exists. |
|
468 | - */ |
|
469 | - if (! $relationModel) { |
|
470 | - \Errors::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
471 | - } |
|
472 | - |
|
473 | - foreach ($value as $relationAttribute => $relationValue) { |
|
474 | - /** |
|
475 | - * Prevent attributes not in the fillable. |
|
476 | - */ |
|
477 | - if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) { |
|
478 | - $relationModel->$relationAttribute = $relationValue; |
|
479 | - } |
|
480 | - } |
|
481 | - |
|
482 | - $relations[$relation] = $relationModel; |
|
483 | - } |
|
484 | - } |
|
485 | - } |
|
486 | - } |
|
487 | - } |
|
488 | - } |
|
489 | - |
|
490 | - return $relations; |
|
491 | - } |
|
492 | - |
|
493 | - /** |
|
494 | - * Save the model with related models. |
|
495 | - * |
|
496 | - * @param object $model |
|
497 | - * @param array $relations |
|
498 | - * |
|
499 | - * @return object |
|
500 | - */ |
|
501 | - public function saveModel($model, $relations) |
|
502 | - { |
|
503 | - |
|
504 | - /** |
|
505 | - * Loop through the relations array. |
|
506 | - */ |
|
507 | - foreach ($relations as $key => $value) { |
|
508 | - /** |
|
509 | - * If the relation is marked for delete then delete it. |
|
510 | - */ |
|
511 | - if ($value == 'delete' && $model->$key()->count()) { |
|
512 | - $model->$key()->delete(); |
|
513 | - } elseif (gettype($value) == 'array') { |
|
514 | - /** |
|
515 | - * Save the model. |
|
516 | - */ |
|
517 | - $model->save(); |
|
518 | - $ids = []; |
|
519 | - |
|
520 | - /** |
|
521 | - * Loop through the relations. |
|
522 | - */ |
|
523 | - foreach ($value as $val) { |
|
524 | - switch (class_basename($model->$key())) { |
|
525 | - /** |
|
526 | - * If the relation is one to many then update it's foreign key with |
|
527 | - * the model id and save it then add its id to ids array to delete all |
|
528 | - * relations who's id isn't in the ids array. |
|
529 | - */ |
|
530 | - case 'HasMany': |
|
531 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
532 | - $val->$foreignKeyName = $model->id; |
|
533 | - $val->save(); |
|
534 | - $ids[] = $val->id; |
|
535 | - break; |
|
536 | - |
|
537 | - /** |
|
538 | - * If the relation is many to many then add it's id to the ids array to |
|
539 | - * attache these ids to the model. |
|
540 | - */ |
|
541 | - case 'BelongsToMany': |
|
542 | - $val->save(); |
|
543 | - $ids[] = $val->id; |
|
544 | - break; |
|
545 | - } |
|
546 | - } |
|
547 | - switch (class_basename($model->$key())) { |
|
548 | - /** |
|
549 | - * If the relation is one to many then delete all |
|
550 | - * relations who's id isn't in the ids array. |
|
551 | - */ |
|
552 | - case 'HasMany': |
|
553 | - $model->$key()->whereNotIn('id', $ids)->delete(); |
|
554 | - break; |
|
555 | - |
|
556 | - /** |
|
557 | - * If the relation is many to many then |
|
558 | - * detach the previous data and attach |
|
559 | - * the ids array to the model. |
|
560 | - */ |
|
561 | - case 'BelongsToMany': |
|
562 | - $model->$key()->detach(); |
|
563 | - $model->$key()->attach($ids); |
|
564 | - break; |
|
565 | - } |
|
566 | - } else { |
|
567 | - switch (class_basename($model->$key())) { |
|
568 | - /** |
|
569 | - * If the relation is one to one. |
|
570 | - */ |
|
571 | - case 'HasOne': |
|
572 | - /** |
|
573 | - * Save the model. |
|
574 | - */ |
|
575 | - $model->save(); |
|
576 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
577 | - $value->$foreignKeyName = $model->id; |
|
578 | - $value->save(); |
|
579 | - break; |
|
580 | - case 'BelongsTo': |
|
581 | - /** |
|
582 | - * Save the model. |
|
583 | - */ |
|
584 | - $value->save(); |
|
585 | - $model->$key()->associate($value); |
|
586 | - break; |
|
587 | - } |
|
588 | - } |
|
589 | - } |
|
590 | - |
|
591 | - /** |
|
592 | - * Save the model. |
|
593 | - */ |
|
594 | - $model->save(); |
|
595 | - |
|
596 | - return $model; |
|
597 | - } |
|
598 | - |
|
599 | - /** |
|
600 | - * Build the conditions recursively for the retrieving methods. |
|
601 | - * @param array $conditions |
|
602 | - * @return array |
|
603 | - */ |
|
604 | - protected function constructConditions($conditions, $model) |
|
605 | - { |
|
606 | - $conditionString = ''; |
|
607 | - $conditionValues = []; |
|
608 | - foreach ($conditions as $key => $value) { |
|
609 | - if (Str::contains($key, '->')) { |
|
610 | - $key = $this->wrapJsonSelector($key); |
|
611 | - } |
|
612 | - |
|
613 | - if ($key == 'and') { |
|
614 | - $conditions = $this->constructConditions($value, $model); |
|
615 | - $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
616 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
617 | - } elseif ($key == 'or') { |
|
618 | - $conditions = $this->constructConditions($value, $model); |
|
619 | - $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
620 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
621 | - } else { |
|
622 | - if (is_array($value)) { |
|
623 | - $operator = $value['op']; |
|
624 | - if (strtolower($operator) == 'between') { |
|
625 | - $value1 = $value['val1']; |
|
626 | - $value2 = $value['val2']; |
|
627 | - } else { |
|
628 | - $value = Arr::get($value, 'val', ''); |
|
629 | - } |
|
630 | - } else { |
|
631 | - $operator = '='; |
|
632 | - } |
|
382 | + /** |
|
383 | + * Prepare related models based on the given data for the given model. |
|
384 | + * |
|
385 | + * @param array $data |
|
386 | + * @param object $model |
|
387 | + * |
|
388 | + * @return array |
|
389 | + */ |
|
390 | + public function prepareRelations($data, $model) |
|
391 | + { |
|
392 | + /** |
|
393 | + * Construct the model object with the given data, |
|
394 | + * and if there is a relation add it to relations array, |
|
395 | + * then save the model. |
|
396 | + */ |
|
397 | + foreach ($data as $key => $value) { |
|
398 | + /** |
|
399 | + * If the attribute is a relation. |
|
400 | + */ |
|
401 | + $relation = \Str::camel($key); |
|
402 | + if (method_exists($model, $relation) && \Core::$relation()) { |
|
403 | + /** |
|
404 | + * Check if the relation is a collection. |
|
405 | + */ |
|
406 | + if (class_basename($model->$relation) == 'Collection') { |
|
407 | + /** |
|
408 | + * If the relation has no value then marke the relation data |
|
409 | + * related to the model to be deleted. |
|
410 | + */ |
|
411 | + if (! $value || ! count($value)) { |
|
412 | + $relations[$relation] = 'delete'; |
|
413 | + } |
|
414 | + } |
|
415 | + if (is_array($value)) { |
|
416 | + /** |
|
417 | + * Loop through the relation data. |
|
418 | + */ |
|
419 | + foreach ($value as $attr => $val) { |
|
420 | + /** |
|
421 | + * Get the relation model. |
|
422 | + */ |
|
423 | + $relationBaseModel = \Core::$relation()->model; |
|
424 | + |
|
425 | + /** |
|
426 | + * Check if the relation is a collection. |
|
427 | + */ |
|
428 | + if (class_basename($model->$relation) == 'Collection') { |
|
429 | + /** |
|
430 | + * If the id is present in the data then select the relation model for updating, |
|
431 | + * else create new model. |
|
432 | + */ |
|
433 | + $relationModel = Arr::has($val, 'id') ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
434 | + |
|
435 | + /** |
|
436 | + * If model doesn't exists. |
|
437 | + */ |
|
438 | + if (! $relationModel) { |
|
439 | + \Errors::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
440 | + } |
|
441 | + |
|
442 | + /** |
|
443 | + * Loop through the relation attributes. |
|
444 | + */ |
|
445 | + foreach ($val as $attr => $val) { |
|
446 | + /** |
|
447 | + * Prevent the sub relations or attributes not in the fillable. |
|
448 | + */ |
|
449 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) { |
|
450 | + $relationModel->$attr = $val; |
|
451 | + } |
|
452 | + } |
|
453 | + |
|
454 | + $relations[$relation][] = $relationModel; |
|
455 | + } else { |
|
456 | + /** |
|
457 | + * Prevent the sub relations. |
|
458 | + */ |
|
459 | + if (gettype($val) !== 'object' && gettype($val) !== 'array') { |
|
460 | + /** |
|
461 | + * If the id is present in the data then select the relation model for updating, |
|
462 | + * else create new model. |
|
463 | + */ |
|
464 | + $relationModel = Arr::has($value, 'id') ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
465 | + |
|
466 | + /** |
|
467 | + * If model doesn't exists. |
|
468 | + */ |
|
469 | + if (! $relationModel) { |
|
470 | + \Errors::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
471 | + } |
|
472 | + |
|
473 | + foreach ($value as $relationAttribute => $relationValue) { |
|
474 | + /** |
|
475 | + * Prevent attributes not in the fillable. |
|
476 | + */ |
|
477 | + if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) { |
|
478 | + $relationModel->$relationAttribute = $relationValue; |
|
479 | + } |
|
480 | + } |
|
481 | + |
|
482 | + $relations[$relation] = $relationModel; |
|
483 | + } |
|
484 | + } |
|
485 | + } |
|
486 | + } |
|
487 | + } |
|
488 | + } |
|
489 | + |
|
490 | + return $relations; |
|
491 | + } |
|
492 | + |
|
493 | + /** |
|
494 | + * Save the model with related models. |
|
495 | + * |
|
496 | + * @param object $model |
|
497 | + * @param array $relations |
|
498 | + * |
|
499 | + * @return object |
|
500 | + */ |
|
501 | + public function saveModel($model, $relations) |
|
502 | + { |
|
503 | + |
|
504 | + /** |
|
505 | + * Loop through the relations array. |
|
506 | + */ |
|
507 | + foreach ($relations as $key => $value) { |
|
508 | + /** |
|
509 | + * If the relation is marked for delete then delete it. |
|
510 | + */ |
|
511 | + if ($value == 'delete' && $model->$key()->count()) { |
|
512 | + $model->$key()->delete(); |
|
513 | + } elseif (gettype($value) == 'array') { |
|
514 | + /** |
|
515 | + * Save the model. |
|
516 | + */ |
|
517 | + $model->save(); |
|
518 | + $ids = []; |
|
519 | + |
|
520 | + /** |
|
521 | + * Loop through the relations. |
|
522 | + */ |
|
523 | + foreach ($value as $val) { |
|
524 | + switch (class_basename($model->$key())) { |
|
525 | + /** |
|
526 | + * If the relation is one to many then update it's foreign key with |
|
527 | + * the model id and save it then add its id to ids array to delete all |
|
528 | + * relations who's id isn't in the ids array. |
|
529 | + */ |
|
530 | + case 'HasMany': |
|
531 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
532 | + $val->$foreignKeyName = $model->id; |
|
533 | + $val->save(); |
|
534 | + $ids[] = $val->id; |
|
535 | + break; |
|
536 | + |
|
537 | + /** |
|
538 | + * If the relation is many to many then add it's id to the ids array to |
|
539 | + * attache these ids to the model. |
|
540 | + */ |
|
541 | + case 'BelongsToMany': |
|
542 | + $val->save(); |
|
543 | + $ids[] = $val->id; |
|
544 | + break; |
|
545 | + } |
|
546 | + } |
|
547 | + switch (class_basename($model->$key())) { |
|
548 | + /** |
|
549 | + * If the relation is one to many then delete all |
|
550 | + * relations who's id isn't in the ids array. |
|
551 | + */ |
|
552 | + case 'HasMany': |
|
553 | + $model->$key()->whereNotIn('id', $ids)->delete(); |
|
554 | + break; |
|
555 | + |
|
556 | + /** |
|
557 | + * If the relation is many to many then |
|
558 | + * detach the previous data and attach |
|
559 | + * the ids array to the model. |
|
560 | + */ |
|
561 | + case 'BelongsToMany': |
|
562 | + $model->$key()->detach(); |
|
563 | + $model->$key()->attach($ids); |
|
564 | + break; |
|
565 | + } |
|
566 | + } else { |
|
567 | + switch (class_basename($model->$key())) { |
|
568 | + /** |
|
569 | + * If the relation is one to one. |
|
570 | + */ |
|
571 | + case 'HasOne': |
|
572 | + /** |
|
573 | + * Save the model. |
|
574 | + */ |
|
575 | + $model->save(); |
|
576 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
577 | + $value->$foreignKeyName = $model->id; |
|
578 | + $value->save(); |
|
579 | + break; |
|
580 | + case 'BelongsTo': |
|
581 | + /** |
|
582 | + * Save the model. |
|
583 | + */ |
|
584 | + $value->save(); |
|
585 | + $model->$key()->associate($value); |
|
586 | + break; |
|
587 | + } |
|
588 | + } |
|
589 | + } |
|
590 | + |
|
591 | + /** |
|
592 | + * Save the model. |
|
593 | + */ |
|
594 | + $model->save(); |
|
595 | + |
|
596 | + return $model; |
|
597 | + } |
|
598 | + |
|
599 | + /** |
|
600 | + * Build the conditions recursively for the retrieving methods. |
|
601 | + * @param array $conditions |
|
602 | + * @return array |
|
603 | + */ |
|
604 | + protected function constructConditions($conditions, $model) |
|
605 | + { |
|
606 | + $conditionString = ''; |
|
607 | + $conditionValues = []; |
|
608 | + foreach ($conditions as $key => $value) { |
|
609 | + if (Str::contains($key, '->')) { |
|
610 | + $key = $this->wrapJsonSelector($key); |
|
611 | + } |
|
612 | + |
|
613 | + if ($key == 'and') { |
|
614 | + $conditions = $this->constructConditions($value, $model); |
|
615 | + $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
616 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
617 | + } elseif ($key == 'or') { |
|
618 | + $conditions = $this->constructConditions($value, $model); |
|
619 | + $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
620 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
621 | + } else { |
|
622 | + if (is_array($value)) { |
|
623 | + $operator = $value['op']; |
|
624 | + if (strtolower($operator) == 'between') { |
|
625 | + $value1 = $value['val1']; |
|
626 | + $value2 = $value['val2']; |
|
627 | + } else { |
|
628 | + $value = Arr::get($value, 'val', ''); |
|
629 | + } |
|
630 | + } else { |
|
631 | + $operator = '='; |
|
632 | + } |
|
633 | 633 | |
634 | - if (strtolower($operator) == 'between') { |
|
635 | - $conditionString .= $key.' >= ? and '; |
|
636 | - $conditionValues[] = $value1; |
|
637 | - |
|
638 | - $conditionString .= $key.' <= ? {op} '; |
|
639 | - $conditionValues[] = $value2; |
|
640 | - } elseif (strtolower($operator) == 'in') { |
|
641 | - $conditionValues = array_merge($conditionValues, $value); |
|
642 | - $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
643 | - $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
644 | - } elseif (strtolower($operator) == 'null') { |
|
645 | - $conditionString .= $key.' is null {op} '; |
|
646 | - } elseif (strtolower($operator) == 'not null') { |
|
647 | - $conditionString .= $key.' is not null {op} '; |
|
648 | - } elseif (strtolower($operator) == 'has') { |
|
649 | - $sql = $model->withTrashed()->has($key)->toSql(); |
|
650 | - $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
651 | - $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')').' and '.$conditions['conditionString'].') {op} '; |
|
652 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
653 | - } else { |
|
654 | - $conditionString .= $key.' '.$operator.' ? {op} '; |
|
655 | - $conditionValues[] = $value; |
|
656 | - } |
|
657 | - } |
|
658 | - } |
|
659 | - $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
660 | - return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
661 | - } |
|
662 | - |
|
663 | - /** |
|
664 | - * Wrap the given JSON selector. |
|
665 | - * |
|
666 | - * @param string $value |
|
667 | - * @return string |
|
668 | - */ |
|
669 | - protected function wrapJsonSelector($value) |
|
670 | - { |
|
671 | - $removeLast = strpos($value, ')'); |
|
672 | - $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
673 | - $path = explode('->', $value); |
|
674 | - $field = array_shift($path); |
|
675 | - $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
676 | - return '"'.$part.'"'; |
|
677 | - })->implode('.')); |
|
634 | + if (strtolower($operator) == 'between') { |
|
635 | + $conditionString .= $key.' >= ? and '; |
|
636 | + $conditionValues[] = $value1; |
|
637 | + |
|
638 | + $conditionString .= $key.' <= ? {op} '; |
|
639 | + $conditionValues[] = $value2; |
|
640 | + } elseif (strtolower($operator) == 'in') { |
|
641 | + $conditionValues = array_merge($conditionValues, $value); |
|
642 | + $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
643 | + $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
644 | + } elseif (strtolower($operator) == 'null') { |
|
645 | + $conditionString .= $key.' is null {op} '; |
|
646 | + } elseif (strtolower($operator) == 'not null') { |
|
647 | + $conditionString .= $key.' is not null {op} '; |
|
648 | + } elseif (strtolower($operator) == 'has') { |
|
649 | + $sql = $model->withTrashed()->has($key)->toSql(); |
|
650 | + $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
651 | + $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')').' and '.$conditions['conditionString'].') {op} '; |
|
652 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
653 | + } else { |
|
654 | + $conditionString .= $key.' '.$operator.' ? {op} '; |
|
655 | + $conditionValues[] = $value; |
|
656 | + } |
|
657 | + } |
|
658 | + } |
|
659 | + $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
660 | + return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
661 | + } |
|
662 | + |
|
663 | + /** |
|
664 | + * Wrap the given JSON selector. |
|
665 | + * |
|
666 | + * @param string $value |
|
667 | + * @return string |
|
668 | + */ |
|
669 | + protected function wrapJsonSelector($value) |
|
670 | + { |
|
671 | + $removeLast = strpos($value, ')'); |
|
672 | + $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
673 | + $path = explode('->', $value); |
|
674 | + $field = array_shift($path); |
|
675 | + $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
676 | + return '"'.$part.'"'; |
|
677 | + })->implode('.')); |
|
678 | 678 | |
679 | - return $removeLast === false ? $result : $result.')'; |
|
680 | - } |
|
679 | + return $removeLast === false ? $result : $result.')'; |
|
680 | + } |
|
681 | 681 | } |
@@ -7,37 +7,37 @@ |
||
7 | 7 | class Setting extends Model |
8 | 8 | { |
9 | 9 | |
10 | - use SoftDeletes; |
|
11 | - protected $table = 'settings'; |
|
12 | - protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
13 | - protected $hidden = ['deleted_at']; |
|
14 | - protected $guarded = ['id', 'key']; |
|
15 | - protected $fillable = ['name', 'value']; |
|
16 | - public $searchable = ['name', 'value', 'key']; |
|
10 | + use SoftDeletes; |
|
11 | + protected $table = 'settings'; |
|
12 | + protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
13 | + protected $hidden = ['deleted_at']; |
|
14 | + protected $guarded = ['id', 'key']; |
|
15 | + protected $fillable = ['name', 'value']; |
|
16 | + public $searchable = ['name', 'value', 'key']; |
|
17 | 17 | |
18 | - public function getCreatedAtAttribute($value) |
|
19 | - { |
|
20 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
21 | - } |
|
18 | + public function getCreatedAtAttribute($value) |
|
19 | + { |
|
20 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
21 | + } |
|
22 | 22 | |
23 | - public function getUpdatedAtAttribute($value) |
|
24 | - { |
|
25 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
26 | - } |
|
23 | + public function getUpdatedAtAttribute($value) |
|
24 | + { |
|
25 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
26 | + } |
|
27 | 27 | |
28 | - public function getDeletedAtAttribute($value) |
|
29 | - { |
|
30 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
31 | - } |
|
28 | + public function getDeletedAtAttribute($value) |
|
29 | + { |
|
30 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
31 | + } |
|
32 | 32 | |
33 | - public function newCollection(array $models = []) |
|
34 | - { |
|
35 | - return parent::newCollection($models)->keyBy('key'); |
|
36 | - } |
|
33 | + public function newCollection(array $models = []) |
|
34 | + { |
|
35 | + return parent::newCollection($models)->keyBy('key'); |
|
36 | + } |
|
37 | 37 | |
38 | - public static function boot() |
|
39 | - { |
|
40 | - parent::boot(); |
|
41 | - Setting::observe(SettingsObserver::class); |
|
42 | - } |
|
38 | + public static function boot() |
|
39 | + { |
|
40 | + parent::boot(); |
|
41 | + Setting::observe(SettingsObserver::class); |
|
42 | + } |
|
43 | 43 | } |
@@ -5,102 +5,102 @@ |
||
5 | 5 | |
6 | 6 | class CachingDecorator |
7 | 7 | { |
8 | - /** |
|
9 | - * @var string |
|
10 | - */ |
|
11 | - public $repo; |
|
8 | + /** |
|
9 | + * @var string |
|
10 | + */ |
|
11 | + public $repo; |
|
12 | 12 | |
13 | - /** |
|
14 | - * @var Cache |
|
15 | - */ |
|
16 | - protected $cache; |
|
13 | + /** |
|
14 | + * @var Cache |
|
15 | + */ |
|
16 | + protected $cache; |
|
17 | 17 | |
18 | - /** |
|
19 | - * @var string |
|
20 | - */ |
|
21 | - public $modelKey; |
|
18 | + /** |
|
19 | + * @var string |
|
20 | + */ |
|
21 | + public $modelKey; |
|
22 | 22 | |
23 | - /** |
|
24 | - * @var string |
|
25 | - */ |
|
26 | - public $model; |
|
23 | + /** |
|
24 | + * @var string |
|
25 | + */ |
|
26 | + public $model; |
|
27 | 27 | |
28 | - /** |
|
29 | - * @var string |
|
30 | - */ |
|
31 | - public $modelClass; |
|
28 | + /** |
|
29 | + * @var string |
|
30 | + */ |
|
31 | + public $modelClass; |
|
32 | 32 | |
33 | - /** |
|
34 | - * @var mixed |
|
35 | - */ |
|
36 | - public $cacheConfig; |
|
33 | + /** |
|
34 | + * @var mixed |
|
35 | + */ |
|
36 | + public $cacheConfig; |
|
37 | 37 | |
38 | - /** |
|
39 | - * @var string |
|
40 | - */ |
|
41 | - public $cacheTag; |
|
38 | + /** |
|
39 | + * @var string |
|
40 | + */ |
|
41 | + public $cacheTag; |
|
42 | 42 | |
43 | - /** |
|
44 | - * Init new object. |
|
45 | - * |
|
46 | - * @param string $repo |
|
47 | - * @param Cache $cache |
|
48 | - * |
|
49 | - * @return void |
|
50 | - */ |
|
51 | - public function __construct($repo, Cache $cache) |
|
52 | - { |
|
53 | - $this->repo = $repo; |
|
54 | - $this->cache = $cache; |
|
55 | - $this->model = $this->repo->model; |
|
56 | - $this->modelClass = get_class($this->model); |
|
57 | - $repoClass = explode('\\', get_class($this->repo)); |
|
58 | - $repoName = end($repoClass); |
|
59 | - $this->cacheTag = lcfirst(substr($repoName, 0, strpos($repoName, 'Repository'))); |
|
60 | - } |
|
43 | + /** |
|
44 | + * Init new object. |
|
45 | + * |
|
46 | + * @param string $repo |
|
47 | + * @param Cache $cache |
|
48 | + * |
|
49 | + * @return void |
|
50 | + */ |
|
51 | + public function __construct($repo, Cache $cache) |
|
52 | + { |
|
53 | + $this->repo = $repo; |
|
54 | + $this->cache = $cache; |
|
55 | + $this->model = $this->repo->model; |
|
56 | + $this->modelClass = get_class($this->model); |
|
57 | + $repoClass = explode('\\', get_class($this->repo)); |
|
58 | + $repoName = end($repoClass); |
|
59 | + $this->cacheTag = lcfirst(substr($repoName, 0, strpos($repoName, 'Repository'))); |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * Handle the cache mechanism for the called method |
|
64 | - * based the configurations. |
|
65 | - * |
|
66 | - * @param string $name the called method name |
|
67 | - * @param array $arguments the method arguments |
|
68 | - * @return object |
|
69 | - */ |
|
70 | - public function __call($name, $arguments) |
|
71 | - { |
|
72 | - $this->setCacheConfig($name); |
|
62 | + /** |
|
63 | + * Handle the cache mechanism for the called method |
|
64 | + * based the configurations. |
|
65 | + * |
|
66 | + * @param string $name the called method name |
|
67 | + * @param array $arguments the method arguments |
|
68 | + * @return object |
|
69 | + */ |
|
70 | + public function __call($name, $arguments) |
|
71 | + { |
|
72 | + $this->setCacheConfig($name); |
|
73 | 73 | |
74 | - if ($this->cacheConfig && $this->cacheConfig == 'cache') { |
|
75 | - $page = \Request::get('page') !== null ? \Request::get('page') : '1'; |
|
76 | - $cacheKey = $name.$page.\Session::get('locale').serialize($arguments); |
|
77 | - return $this->cache->tags([$this->cacheTag])->rememberForever($cacheKey, function () use ($arguments, $name) { |
|
78 | - return call_user_func_array([$this->repo, $name], $arguments); |
|
79 | - }); |
|
80 | - } elseif ($this->cacheConfig && $this->cacheConfig == 'clear') { |
|
81 | - $this->cache->tags($this->cacheConfig)->flush(); |
|
82 | - return call_user_func_array([$this->repo, $name], $arguments); |
|
83 | - } |
|
74 | + if ($this->cacheConfig && $this->cacheConfig == 'cache') { |
|
75 | + $page = \Request::get('page') !== null ? \Request::get('page') : '1'; |
|
76 | + $cacheKey = $name.$page.\Session::get('locale').serialize($arguments); |
|
77 | + return $this->cache->tags([$this->cacheTag])->rememberForever($cacheKey, function () use ($arguments, $name) { |
|
78 | + return call_user_func_array([$this->repo, $name], $arguments); |
|
79 | + }); |
|
80 | + } elseif ($this->cacheConfig && $this->cacheConfig == 'clear') { |
|
81 | + $this->cache->tags($this->cacheConfig)->flush(); |
|
82 | + return call_user_func_array([$this->repo, $name], $arguments); |
|
83 | + } |
|
84 | 84 | |
85 | - return call_user_func_array([$this->repo, $name], $arguments); |
|
86 | - } |
|
85 | + return call_user_func_array([$this->repo, $name], $arguments); |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * Set cache config based on the called method. |
|
90 | - * |
|
91 | - * @param string $name |
|
92 | - * @return void |
|
93 | - */ |
|
94 | - private function setCacheConfig($name) |
|
95 | - { |
|
96 | - $config = \CoreConfig::getConfig(); |
|
97 | - $cacheConfig = Arr::get($config['cacheConfig'], $this->cacheTag, false); |
|
98 | - $this->cacheConfig = false; |
|
88 | + /** |
|
89 | + * Set cache config based on the called method. |
|
90 | + * |
|
91 | + * @param string $name |
|
92 | + * @return void |
|
93 | + */ |
|
94 | + private function setCacheConfig($name) |
|
95 | + { |
|
96 | + $config = \CoreConfig::getConfig(); |
|
97 | + $cacheConfig = Arr::get($config['cacheConfig'], $this->cacheTag, false); |
|
98 | + $this->cacheConfig = false; |
|
99 | 99 | |
100 | - if ($cacheConfig && in_array($name, $cacheConfig['cache'])) { |
|
101 | - $this->cacheConfig = 'cache'; |
|
102 | - } elseif ($cacheConfig && isset($cacheConfig['clear'][$name])) { |
|
103 | - $this->cacheConfig = $cacheConfig['clear'][$name]; |
|
104 | - } |
|
105 | - } |
|
100 | + if ($cacheConfig && in_array($name, $cacheConfig['cache'])) { |
|
101 | + $this->cacheConfig = 'cache'; |
|
102 | + } elseif ($cacheConfig && isset($cacheConfig['clear'][$name])) { |
|
103 | + $this->cacheConfig = $cacheConfig['clear'][$name]; |
|
104 | + } |
|
105 | + } |
|
106 | 106 | } |
@@ -8,28 +8,28 @@ |
||
8 | 8 | |
9 | 9 | class OauthClient extends Client |
10 | 10 | { |
11 | - protected $dates = ['created_at', 'updated_at']; |
|
12 | - protected $fillable = ['name', 'redirect', 'user_id', 'personal_access_client', 'password_client', 'revoked']; |
|
13 | - public $searchable = ['name']; |
|
11 | + protected $dates = ['created_at', 'updated_at']; |
|
12 | + protected $fillable = ['name', 'redirect', 'user_id', 'personal_access_client', 'password_client', 'revoked']; |
|
13 | + public $searchable = ['name']; |
|
14 | 14 | |
15 | - public function getCreatedAtAttribute($value) |
|
16 | - { |
|
17 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
18 | - } |
|
15 | + public function getCreatedAtAttribute($value) |
|
16 | + { |
|
17 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
18 | + } |
|
19 | 19 | |
20 | - public function getUpdatedAtAttribute($value) |
|
21 | - { |
|
22 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
23 | - } |
|
20 | + public function getUpdatedAtAttribute($value) |
|
21 | + { |
|
22 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
23 | + } |
|
24 | 24 | |
25 | - public function user() |
|
26 | - { |
|
27 | - return $this->belongsTo(AclUser::class); |
|
28 | - } |
|
25 | + public function user() |
|
26 | + { |
|
27 | + return $this->belongsTo(AclUser::class); |
|
28 | + } |
|
29 | 29 | |
30 | - public static function boot() |
|
31 | - { |
|
32 | - parent::boot(); |
|
33 | - OauthClient::observe(OauthClientObserver::class); |
|
34 | - } |
|
30 | + public static function boot() |
|
31 | + { |
|
32 | + parent::boot(); |
|
33 | + OauthClient::observe(OauthClientObserver::class); |
|
34 | + } |
|
35 | 35 | } |
@@ -6,69 +6,69 @@ |
||
6 | 6 | class RoleObserver |
7 | 7 | { |
8 | 8 | |
9 | - public function saving($model) |
|
10 | - { |
|
11 | - // |
|
12 | - } |
|
9 | + public function saving($model) |
|
10 | + { |
|
11 | + // |
|
12 | + } |
|
13 | 13 | |
14 | - public function saved($model) |
|
15 | - { |
|
16 | - // |
|
17 | - } |
|
14 | + public function saved($model) |
|
15 | + { |
|
16 | + // |
|
17 | + } |
|
18 | 18 | |
19 | - public function creating($model) |
|
20 | - { |
|
21 | - // |
|
22 | - } |
|
19 | + public function creating($model) |
|
20 | + { |
|
21 | + // |
|
22 | + } |
|
23 | 23 | |
24 | - public function created($model) |
|
25 | - { |
|
26 | - // |
|
27 | - } |
|
24 | + public function created($model) |
|
25 | + { |
|
26 | + // |
|
27 | + } |
|
28 | 28 | |
29 | - /** |
|
30 | - * Prevent updating of the admin role. |
|
31 | - * |
|
32 | - * @param object $model the model beign updated. |
|
33 | - * @return void |
|
34 | - */ |
|
35 | - public function updating($model) |
|
36 | - { |
|
37 | - if ($model->getOriginal('name') == 'Admin') { |
|
38 | - \Errors::noPermissions(); |
|
39 | - } |
|
40 | - } |
|
29 | + /** |
|
30 | + * Prevent updating of the admin role. |
|
31 | + * |
|
32 | + * @param object $model the model beign updated. |
|
33 | + * @return void |
|
34 | + */ |
|
35 | + public function updating($model) |
|
36 | + { |
|
37 | + if ($model->getOriginal('name') == 'Admin') { |
|
38 | + \Errors::noPermissions(); |
|
39 | + } |
|
40 | + } |
|
41 | 41 | |
42 | - public function updated($model) |
|
43 | - { |
|
44 | - // |
|
45 | - } |
|
42 | + public function updated($model) |
|
43 | + { |
|
44 | + // |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * Prevent deleting the admin role. |
|
49 | - * |
|
50 | - * @param object $model the delted model. |
|
51 | - * @return void |
|
52 | - */ |
|
53 | - public function deleting($model) |
|
54 | - { |
|
55 | - if ($model->getOriginal('name') == 'Admin') { |
|
56 | - \Errors::noPermissions(); |
|
57 | - } |
|
58 | - } |
|
47 | + /** |
|
48 | + * Prevent deleting the admin role. |
|
49 | + * |
|
50 | + * @param object $model the delted model. |
|
51 | + * @return void |
|
52 | + */ |
|
53 | + public function deleting($model) |
|
54 | + { |
|
55 | + if ($model->getOriginal('name') == 'Admin') { |
|
56 | + \Errors::noPermissions(); |
|
57 | + } |
|
58 | + } |
|
59 | 59 | |
60 | - public function deleted($model) |
|
61 | - { |
|
62 | - // |
|
63 | - } |
|
60 | + public function deleted($model) |
|
61 | + { |
|
62 | + // |
|
63 | + } |
|
64 | 64 | |
65 | - public function restoring($model) |
|
66 | - { |
|
67 | - // |
|
68 | - } |
|
65 | + public function restoring($model) |
|
66 | + { |
|
67 | + // |
|
68 | + } |
|
69 | 69 | |
70 | - public function restored($model) |
|
71 | - { |
|
72 | - // |
|
73 | - } |
|
70 | + public function restored($model) |
|
71 | + { |
|
72 | + // |
|
73 | + } |
|
74 | 74 | } |
@@ -9,42 +9,42 @@ |
||
9 | 9 | class Role extends Model |
10 | 10 | { |
11 | 11 | |
12 | - use SoftDeletes; |
|
13 | - protected $table = 'roles'; |
|
14 | - protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
15 | - protected $hidden = ['deleted_at']; |
|
16 | - protected $guarded = ['id']; |
|
17 | - protected $fillable = ['name']; |
|
18 | - public $searchable = ['name']; |
|
19 | - |
|
20 | - public function getCreatedAtAttribute($value) |
|
21 | - { |
|
22 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
23 | - } |
|
24 | - |
|
25 | - public function getUpdatedAtAttribute($value) |
|
26 | - { |
|
27 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
28 | - } |
|
29 | - |
|
30 | - public function getDeletedAtAttribute($value) |
|
31 | - { |
|
32 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
33 | - } |
|
34 | - |
|
35 | - public function users() |
|
36 | - { |
|
37 | - return $this->belongsToMany(AclUser::class, 'role_user', 'role_id', 'user_id')->whereNull('role_user.deleted_at')->withTimestamps(); |
|
38 | - } |
|
39 | - |
|
40 | - public function permissions() |
|
41 | - { |
|
42 | - return $this->belongsToMany(Permission::class, 'permission_role', 'role_id', 'permission_id')->whereNull('permission_role.deleted_at')->withTimestamps(); |
|
43 | - } |
|
44 | - |
|
45 | - public static function boot() |
|
46 | - { |
|
47 | - parent::boot(); |
|
48 | - Role::observe(RoleObserver::class); |
|
49 | - } |
|
12 | + use SoftDeletes; |
|
13 | + protected $table = 'roles'; |
|
14 | + protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
15 | + protected $hidden = ['deleted_at']; |
|
16 | + protected $guarded = ['id']; |
|
17 | + protected $fillable = ['name']; |
|
18 | + public $searchable = ['name']; |
|
19 | + |
|
20 | + public function getCreatedAtAttribute($value) |
|
21 | + { |
|
22 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
23 | + } |
|
24 | + |
|
25 | + public function getUpdatedAtAttribute($value) |
|
26 | + { |
|
27 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
28 | + } |
|
29 | + |
|
30 | + public function getDeletedAtAttribute($value) |
|
31 | + { |
|
32 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
33 | + } |
|
34 | + |
|
35 | + public function users() |
|
36 | + { |
|
37 | + return $this->belongsToMany(AclUser::class, 'role_user', 'role_id', 'user_id')->whereNull('role_user.deleted_at')->withTimestamps(); |
|
38 | + } |
|
39 | + |
|
40 | + public function permissions() |
|
41 | + { |
|
42 | + return $this->belongsToMany(Permission::class, 'permission_role', 'role_id', 'permission_id')->whereNull('permission_role.deleted_at')->withTimestamps(); |
|
43 | + } |
|
44 | + |
|
45 | + public static function boot() |
|
46 | + { |
|
47 | + parent::boot(); |
|
48 | + Role::observe(RoleObserver::class); |
|
49 | + } |
|
50 | 50 | } |
@@ -11,133 +11,133 @@ |
||
11 | 11 | class AclUser extends User |
12 | 12 | { |
13 | 13 | |
14 | - use SoftDeletes, HasApiTokens; |
|
15 | - protected $table = 'users'; |
|
16 | - protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
17 | - protected $hidden = ['password', 'remember_token', 'deleted_at']; |
|
18 | - protected $guarded = ['id']; |
|
19 | - protected $fillable = ['profile_picture', 'name', 'email', 'password', 'locale', 'timezone']; |
|
20 | - public $searchable = ['name', 'email']; |
|
14 | + use SoftDeletes, HasApiTokens; |
|
15 | + protected $table = 'users'; |
|
16 | + protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
17 | + protected $hidden = ['password', 'remember_token', 'deleted_at']; |
|
18 | + protected $guarded = ['id']; |
|
19 | + protected $fillable = ['profile_picture', 'name', 'email', 'password', 'locale', 'timezone']; |
|
20 | + public $searchable = ['name', 'email']; |
|
21 | 21 | |
22 | - public function getCreatedAtAttribute($value) |
|
23 | - { |
|
24 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
25 | - } |
|
26 | - |
|
27 | - public function getUpdatedAtAttribute($value) |
|
28 | - { |
|
29 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
30 | - } |
|
31 | - |
|
32 | - public function getDeletedAtAttribute($value) |
|
33 | - { |
|
34 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * Get the profile picture url. |
|
39 | - * @return string |
|
40 | - */ |
|
41 | - public function getProfilePictureAttribute($value) |
|
42 | - { |
|
43 | - return url(\Storage::url($value)); |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * Encrypt the password attribute before |
|
48 | - * saving it in the storage. |
|
49 | - * |
|
50 | - * @param string $value |
|
51 | - */ |
|
52 | - public function setPasswordAttribute($value) |
|
53 | - { |
|
54 | - $this->attributes['password'] = \Hash::make($value); |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * Get the entity's notifications. |
|
59 | - */ |
|
60 | - public function notifications() |
|
61 | - { |
|
62 | - return $this->morphMany(Notification::class, 'notifiable')->orderBy('created_at', 'desc'); |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * Get the entity's read notifications. |
|
67 | - */ |
|
68 | - public function readNotifications() |
|
69 | - { |
|
70 | - return $this->notifications()->whereNotNull('read_at'); |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * Get the entity's unread notifications. |
|
75 | - */ |
|
76 | - public function unreadNotifications() |
|
77 | - { |
|
78 | - return $this->notifications()->whereNull('read_at'); |
|
79 | - } |
|
80 | - |
|
81 | - public function roles() |
|
82 | - { |
|
83 | - return $this->belongsToMany(Role::class, 'role_user', 'user_id', 'role_id')->whereNull('role_user.deleted_at')->withTimestamps(); |
|
84 | - } |
|
85 | - |
|
86 | - public function oauthClients() |
|
87 | - { |
|
88 | - return $this->hasMany(OauthClient::class, 'user_id'); |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * Return fcm device tokens that will be used in sending fcm notifications. |
|
93 | - * |
|
94 | - * @return array |
|
95 | - */ |
|
96 | - public function routeNotificationForFCM() |
|
97 | - { |
|
98 | - $devices = \Core::pushNotificationDevices()->findBy(['user_id' => $this->id]); |
|
99 | - $tokens = []; |
|
100 | - |
|
101 | - foreach ($devices as $device) { |
|
102 | - if (\Core::oauthClients()->accessTokenExpiredOrRevoked($device->access_token)) { |
|
103 | - $device->forceDelete(); |
|
104 | - continue; |
|
105 | - } |
|
106 | - |
|
107 | - $tokens[] = $device->device_token; |
|
108 | - } |
|
109 | - |
|
110 | - return $tokens; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * The channels the user receives notification broadcasts on. |
|
115 | - * |
|
116 | - * @return string |
|
117 | - */ |
|
118 | - public function receivesBroadcastNotificationsOn() |
|
119 | - { |
|
120 | - return 'users.'.$this->id; |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Custom password validation. |
|
125 | - * |
|
126 | - * @param string $password |
|
127 | - * @return boolean |
|
128 | - */ |
|
129 | - public function validateForPassportPasswordGrant($password) |
|
130 | - { |
|
131 | - if ($password == config('skeleton.social_pass')) { |
|
132 | - return true; |
|
133 | - } |
|
134 | - |
|
135 | - return \Hash::check($password, $this->password); |
|
136 | - } |
|
22 | + public function getCreatedAtAttribute($value) |
|
23 | + { |
|
24 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
25 | + } |
|
26 | + |
|
27 | + public function getUpdatedAtAttribute($value) |
|
28 | + { |
|
29 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
30 | + } |
|
31 | + |
|
32 | + public function getDeletedAtAttribute($value) |
|
33 | + { |
|
34 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * Get the profile picture url. |
|
39 | + * @return string |
|
40 | + */ |
|
41 | + public function getProfilePictureAttribute($value) |
|
42 | + { |
|
43 | + return url(\Storage::url($value)); |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * Encrypt the password attribute before |
|
48 | + * saving it in the storage. |
|
49 | + * |
|
50 | + * @param string $value |
|
51 | + */ |
|
52 | + public function setPasswordAttribute($value) |
|
53 | + { |
|
54 | + $this->attributes['password'] = \Hash::make($value); |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * Get the entity's notifications. |
|
59 | + */ |
|
60 | + public function notifications() |
|
61 | + { |
|
62 | + return $this->morphMany(Notification::class, 'notifiable')->orderBy('created_at', 'desc'); |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * Get the entity's read notifications. |
|
67 | + */ |
|
68 | + public function readNotifications() |
|
69 | + { |
|
70 | + return $this->notifications()->whereNotNull('read_at'); |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * Get the entity's unread notifications. |
|
75 | + */ |
|
76 | + public function unreadNotifications() |
|
77 | + { |
|
78 | + return $this->notifications()->whereNull('read_at'); |
|
79 | + } |
|
80 | + |
|
81 | + public function roles() |
|
82 | + { |
|
83 | + return $this->belongsToMany(Role::class, 'role_user', 'user_id', 'role_id')->whereNull('role_user.deleted_at')->withTimestamps(); |
|
84 | + } |
|
85 | + |
|
86 | + public function oauthClients() |
|
87 | + { |
|
88 | + return $this->hasMany(OauthClient::class, 'user_id'); |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * Return fcm device tokens that will be used in sending fcm notifications. |
|
93 | + * |
|
94 | + * @return array |
|
95 | + */ |
|
96 | + public function routeNotificationForFCM() |
|
97 | + { |
|
98 | + $devices = \Core::pushNotificationDevices()->findBy(['user_id' => $this->id]); |
|
99 | + $tokens = []; |
|
100 | + |
|
101 | + foreach ($devices as $device) { |
|
102 | + if (\Core::oauthClients()->accessTokenExpiredOrRevoked($device->access_token)) { |
|
103 | + $device->forceDelete(); |
|
104 | + continue; |
|
105 | + } |
|
106 | + |
|
107 | + $tokens[] = $device->device_token; |
|
108 | + } |
|
109 | + |
|
110 | + return $tokens; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * The channels the user receives notification broadcasts on. |
|
115 | + * |
|
116 | + * @return string |
|
117 | + */ |
|
118 | + public function receivesBroadcastNotificationsOn() |
|
119 | + { |
|
120 | + return 'users.'.$this->id; |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Custom password validation. |
|
125 | + * |
|
126 | + * @param string $password |
|
127 | + * @return boolean |
|
128 | + */ |
|
129 | + public function validateForPassportPasswordGrant($password) |
|
130 | + { |
|
131 | + if ($password == config('skeleton.social_pass')) { |
|
132 | + return true; |
|
133 | + } |
|
134 | + |
|
135 | + return \Hash::check($password, $this->password); |
|
136 | + } |
|
137 | 137 | |
138 | - public static function boot() |
|
139 | - { |
|
140 | - parent::boot(); |
|
141 | - AclUser::observe(AclUserObserver::class); |
|
142 | - } |
|
138 | + public static function boot() |
|
139 | + { |
|
140 | + parent::boot(); |
|
141 | + AclUser::observe(AclUserObserver::class); |
|
142 | + } |
|
143 | 143 | } |
@@ -5,64 +5,64 @@ |
||
5 | 5 | |
6 | 6 | class LoginProxy |
7 | 7 | { |
8 | - /** |
|
9 | - * Attempt to create an access token using user credentials. |
|
10 | - * |
|
11 | - * @param string $email |
|
12 | - * @param string $password |
|
13 | - * @return array |
|
14 | - */ |
|
15 | - public function login($email, $password) |
|
16 | - { |
|
17 | - return $this->proxy('password', [ |
|
18 | - 'username' => $email, |
|
19 | - 'password' => $password |
|
20 | - ]); |
|
21 | - } |
|
8 | + /** |
|
9 | + * Attempt to create an access token using user credentials. |
|
10 | + * |
|
11 | + * @param string $email |
|
12 | + * @param string $password |
|
13 | + * @return array |
|
14 | + */ |
|
15 | + public function login($email, $password) |
|
16 | + { |
|
17 | + return $this->proxy('password', [ |
|
18 | + 'username' => $email, |
|
19 | + 'password' => $password |
|
20 | + ]); |
|
21 | + } |
|
22 | 22 | |
23 | - /** |
|
24 | - * Attempt to refresh the access token using the given refresh token. |
|
25 | - * |
|
26 | - * @param string $refreshToken |
|
27 | - * @return array |
|
28 | - */ |
|
29 | - public function refreshToken($refreshToken) |
|
30 | - { |
|
31 | - return $this->proxy('refresh_token', [ |
|
32 | - 'refresh_token' => $refreshToken |
|
33 | - ]); |
|
34 | - } |
|
23 | + /** |
|
24 | + * Attempt to refresh the access token using the given refresh token. |
|
25 | + * |
|
26 | + * @param string $refreshToken |
|
27 | + * @return array |
|
28 | + */ |
|
29 | + public function refreshToken($refreshToken) |
|
30 | + { |
|
31 | + return $this->proxy('refresh_token', [ |
|
32 | + 'refresh_token' => $refreshToken |
|
33 | + ]); |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * Proxy a request to the OAuth server. |
|
38 | - * |
|
39 | - * @param string $grantType what type of grant type should be proxied |
|
40 | - * @param array |
|
41 | - */ |
|
42 | - public function proxy($grantType, array $data = []) |
|
43 | - { |
|
44 | - $data = array_merge($data, [ |
|
45 | - 'client_id' => config('skeleton.passport_client_id'), |
|
46 | - 'client_secret' => config('skeleton.passport_client_secret'), |
|
47 | - 'grant_type' => $grantType |
|
48 | - ]); |
|
36 | + /** |
|
37 | + * Proxy a request to the OAuth server. |
|
38 | + * |
|
39 | + * @param string $grantType what type of grant type should be proxied |
|
40 | + * @param array |
|
41 | + */ |
|
42 | + public function proxy($grantType, array $data = []) |
|
43 | + { |
|
44 | + $data = array_merge($data, [ |
|
45 | + 'client_id' => config('skeleton.passport_client_id'), |
|
46 | + 'client_secret' => config('skeleton.passport_client_secret'), |
|
47 | + 'grant_type' => $grantType |
|
48 | + ]); |
|
49 | 49 | |
50 | - $response = \ApiConsumer::post('/oauth/token', $data); |
|
50 | + $response = \ApiConsumer::post('/oauth/token', $data); |
|
51 | 51 | |
52 | - if (! $response->isSuccessful()) { |
|
53 | - if ($grantType == 'refresh_token') { |
|
54 | - \Errors::invalidRefreshToken(); |
|
55 | - } |
|
52 | + if (! $response->isSuccessful()) { |
|
53 | + if ($grantType == 'refresh_token') { |
|
54 | + \Errors::invalidRefreshToken(); |
|
55 | + } |
|
56 | 56 | |
57 | - \Errors::loginFailed(); |
|
58 | - } |
|
57 | + \Errors::loginFailed(); |
|
58 | + } |
|
59 | 59 | |
60 | - $data = json_decode($response->getContent()); |
|
60 | + $data = json_decode($response->getContent()); |
|
61 | 61 | |
62 | - return [ |
|
63 | - 'access_token' => $data->access_token, |
|
64 | - 'refresh_token' => $data->refresh_token, |
|
65 | - 'expires_in' => $data->expires_in |
|
66 | - ]; |
|
67 | - } |
|
62 | + return [ |
|
63 | + 'access_token' => $data->access_token, |
|
64 | + 'refresh_token' => $data->refresh_token, |
|
65 | + 'expires_in' => $data->expires_in |
|
66 | + ]; |
|
67 | + } |
|
68 | 68 | } |