@@ -8,282 +8,282 @@ |
||
8 | 8 | |
9 | 9 | class UsersController extends BaseApiController |
10 | 10 | { |
11 | - /** |
|
12 | - * The name of the model that is used by the base api controller |
|
13 | - * to preform actions like (add, edit ... etc). |
|
14 | - * @var string |
|
15 | - */ |
|
16 | - protected $model = 'users'; |
|
17 | - |
|
18 | - /** |
|
19 | - * List of all route actions that the base api controller |
|
20 | - * will skip permissions check for them. |
|
21 | - * @var array |
|
22 | - */ |
|
23 | - protected $skipPermissionCheck = ['account', 'logout', 'changePassword', 'saveProfile', 'account']; |
|
24 | - |
|
25 | - /** |
|
26 | - * List of all route actions that the base api controller |
|
27 | - * will skip login check for them. |
|
28 | - * @var array |
|
29 | - */ |
|
30 | - protected $skipLoginCheck = ['login', 'loginSocial', 'register', 'sendreset', 'resetpassword', 'refreshtoken', 'confirmEmail', 'resendEmailConfirmation']; |
|
31 | - |
|
32 | - /** |
|
33 | - * The validations rules used by the base api controller |
|
34 | - * to check before add. |
|
35 | - * @var array |
|
36 | - */ |
|
37 | - protected $validationRules = [ |
|
38 | - 'name' => 'nullable|string', |
|
39 | - 'email' => 'required|email|unique:users,email,{id}', |
|
40 | - 'password' => 'nullable|min:6' |
|
41 | - ]; |
|
42 | - |
|
43 | - /** |
|
44 | - * The loginProxy implementation. |
|
45 | - * |
|
46 | - * @var \App\Modules\Acl\Proxy\LoginProxy |
|
47 | - */ |
|
48 | - protected $loginProxy; |
|
49 | - |
|
50 | - public function __construct(LoginProxy $loginProxy) |
|
51 | - { |
|
52 | - $this->loginProxy = $loginProxy; |
|
53 | - parent::__construct(); |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Return the logged in user account. |
|
58 | - * |
|
59 | - * @return \Illuminate\Http\Response |
|
60 | - */ |
|
61 | - public function account() |
|
62 | - { |
|
63 | - return \Response::json($this->repo->account($this->relations), 200); |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * Block the user. |
|
68 | - * |
|
69 | - * @param integer $id Id of the user. |
|
70 | - * @return \Illuminate\Http\Response |
|
71 | - */ |
|
72 | - public function block($id) |
|
73 | - { |
|
74 | - return \Response::json($this->repo->block($id), 200); |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Unblock the user. |
|
79 | - * |
|
80 | - * @param integer $id Id of the user. |
|
81 | - * @return \Illuminate\Http\Response |
|
82 | - */ |
|
83 | - public function unblock($id) |
|
84 | - { |
|
85 | - return \Response::json($this->repo->unblock($id), 200); |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Logout the user. |
|
90 | - * |
|
91 | - * @return \Illuminate\Http\Response |
|
92 | - */ |
|
93 | - public function logout() |
|
94 | - { |
|
95 | - return \Response::json($this->loginProxy->logout(), 200); |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Handle a registration request. |
|
100 | - * |
|
101 | - * @param \Illuminate\Http\Request $request |
|
102 | - * @return \Illuminate\Http\Response |
|
103 | - */ |
|
104 | - public function register(Request $request) |
|
105 | - { |
|
106 | - $this->validate($request, [ |
|
107 | - 'name' => 'nullable|string', |
|
108 | - 'email' => 'required|email|unique:users,email,{id}', |
|
109 | - 'password' => 'required|min:6' |
|
110 | - ]); |
|
111 | - |
|
112 | - return \Response::json($this->repo->register($request->only('name', 'email', 'password')), 200); |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Handle a login request to the application. |
|
117 | - * |
|
118 | - * @param \Illuminate\Http\Request $request |
|
119 | - * @return \Illuminate\Http\Response |
|
120 | - */ |
|
121 | - public function login(Request $request) |
|
122 | - { |
|
123 | - $this->validate($request, [ |
|
124 | - 'email' => 'required|email', |
|
125 | - 'password' => 'required|min:6', |
|
126 | - 'admin' => 'nullable|boolean' |
|
127 | - ]); |
|
128 | - |
|
129 | - return \Response::json($this->loginProxy->login($request->only('email', 'password'), $request->get('admin')), 200); |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Handle a social login request of the none admin to the application. |
|
134 | - * |
|
135 | - * @param \Illuminate\Http\Request $request |
|
136 | - * @return \Illuminate\Http\Response |
|
137 | - */ |
|
138 | - public function loginSocial(Request $request) |
|
139 | - { |
|
140 | - $this->validate($request, [ |
|
141 | - 'auth_code' => 'required_without:access_token', |
|
142 | - 'access_token' => 'required_without:auth_code', |
|
143 | - 'type' => 'required|in:facebook,google' |
|
144 | - ]); |
|
145 | - |
|
146 | - return \Response::json($this->repo->loginSocial($request->get('auth_code'), $request->get('access_token'), $request->get('type')), 200); |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Assign the given groups to the given user. |
|
151 | - * |
|
152 | - * @param \Illuminate\Http\Request $request |
|
153 | - * @return \Illuminate\Http\Response |
|
154 | - */ |
|
155 | - public function assigngroups(Request $request) |
|
156 | - { |
|
157 | - $this->validate($request, [ |
|
158 | - 'group_ids' => 'required|exists:groups,id', |
|
159 | - 'user_id' => 'required|exists:users,id' |
|
160 | - ]); |
|
161 | - |
|
162 | - return \Response::json($this->repo->assignGroups($request->get('user_id'), $request->get('group_ids')), 200); |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * Send a reset link to the given user. |
|
167 | - * |
|
168 | - * @param \Illuminate\Http\Request $request |
|
169 | - * @return \Illuminate\Http\Response |
|
170 | - */ |
|
171 | - public function sendreset(Request $request) |
|
172 | - { |
|
173 | - $this->validate($request, ['email' => 'required|email']); |
|
174 | - |
|
175 | - return \Response::json($this->repo->sendReset($request->get('email')), 200); |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * Reset the given user's password. |
|
180 | - * |
|
181 | - * @param \Illuminate\Http\Request $request |
|
182 | - * @return \Illuminate\Http\Response |
|
183 | - */ |
|
184 | - public function resetpassword(Request $request) |
|
185 | - { |
|
186 | - $this->validate($request, [ |
|
187 | - 'token' => 'required', |
|
188 | - 'email' => 'required|email', |
|
189 | - 'password' => 'required|confirmed|min:6', |
|
190 | - 'password_confirmation' => 'required', |
|
191 | - ]); |
|
192 | - |
|
193 | - return \Response::json($this->repo->resetPassword($request->only('email', 'password', 'password_confirmation', 'token')), 200); |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * Change the logged in user password. |
|
198 | - * |
|
199 | - * @param \Illuminate\Http\Request $request |
|
200 | - * @return \Illuminate\Http\Response |
|
201 | - */ |
|
202 | - public function changePassword(Request $request) |
|
203 | - { |
|
204 | - $this->validate($request, [ |
|
205 | - 'old_password' => 'required', |
|
206 | - 'password' => 'required|confirmed|min:6', |
|
207 | - 'password_confirmation' => 'required', |
|
208 | - ]); |
|
209 | - |
|
210 | - return \Response::json($this->repo->changePassword($request->only('old_password', 'password', 'password_confirmation')), 200); |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * Confirm email using the confirmation code. |
|
215 | - * |
|
216 | - * @param \Illuminate\Http\Request $request |
|
217 | - * @return \Illuminate\Http\Response |
|
218 | - */ |
|
219 | - public function confirmEmail(Request $request) |
|
220 | - { |
|
221 | - $this->validate($request, [ |
|
222 | - 'confirmation_code' => 'required|string|exists:users,confirmation_code' |
|
223 | - ]); |
|
224 | - |
|
225 | - return \Response::json($this->repo->confirmEmail($request->only('confirmation_code')), 200); |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * Resend the email confirmation mail. |
|
230 | - * |
|
231 | - * @param \Illuminate\Http\Request $request |
|
232 | - * @return \Illuminate\Http\Response |
|
233 | - */ |
|
234 | - public function resendEmailConfirmation(Request $request) |
|
235 | - { |
|
236 | - $this->validate($request, [ |
|
237 | - 'email' => 'required|exists:users,email' |
|
238 | - ]); |
|
239 | - |
|
240 | - return \Response::json($this->repo->sendConfirmationEmail($request->get('email')), 200); |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * Refresh the expired login token. |
|
245 | - * |
|
246 | - * @param \Illuminate\Http\Request $request |
|
247 | - * @return \Illuminate\Http\Response |
|
248 | - */ |
|
249 | - public function refreshtoken(Request $request) |
|
250 | - { |
|
251 | - $this->validate($request, [ |
|
252 | - 'refreshtoken' => 'required', |
|
253 | - ]); |
|
254 | - |
|
255 | - return \Response::json($this->loginProxy->refreshtoken($request->get('refreshtoken')), 200); |
|
256 | - } |
|
257 | - |
|
258 | - /** |
|
259 | - * Paginate all users with in the given group. |
|
260 | - * |
|
261 | - * @param \Illuminate\Http\Request $request |
|
262 | - * @param string $groupName The name of the requested group. |
|
263 | - * @param integer $perPage Number of rows per page default 15. |
|
264 | - * @param string $sortBy The name of the column to sort by. |
|
265 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
266 | - * @return \Illuminate\Http\Response |
|
267 | - */ |
|
268 | - public function group(Request $request, $groupName, $perPage = false, $sortBy = 'created_at', $desc = 1) |
|
269 | - { |
|
270 | - return \Response::json($this->repo->group($request->all(), $groupName, $this->relations, $perPage, $sortBy, $desc), 200); |
|
271 | - } |
|
272 | - |
|
273 | - /** |
|
274 | - * Save the given data to the logged in user. |
|
275 | - * |
|
276 | - * @param \Illuminate\Http\Request $request |
|
277 | - * @return \Illuminate\Http\Response |
|
278 | - */ |
|
279 | - public function saveProfile(Request $request) |
|
280 | - { |
|
281 | - $this->validate($request, [ |
|
282 | - 'profile_picture' => 'nullable|string', |
|
283 | - 'name' => 'nullable|string', |
|
284 | - 'email' => 'required|email|unique:users,email,' . \Auth::id() |
|
285 | - ]); |
|
286 | - |
|
287 | - return \Response::json($this->repo->saveProfile($request->only('name', 'email', 'profile_picture')), 200); |
|
288 | - } |
|
11 | + /** |
|
12 | + * The name of the model that is used by the base api controller |
|
13 | + * to preform actions like (add, edit ... etc). |
|
14 | + * @var string |
|
15 | + */ |
|
16 | + protected $model = 'users'; |
|
17 | + |
|
18 | + /** |
|
19 | + * List of all route actions that the base api controller |
|
20 | + * will skip permissions check for them. |
|
21 | + * @var array |
|
22 | + */ |
|
23 | + protected $skipPermissionCheck = ['account', 'logout', 'changePassword', 'saveProfile', 'account']; |
|
24 | + |
|
25 | + /** |
|
26 | + * List of all route actions that the base api controller |
|
27 | + * will skip login check for them. |
|
28 | + * @var array |
|
29 | + */ |
|
30 | + protected $skipLoginCheck = ['login', 'loginSocial', 'register', 'sendreset', 'resetpassword', 'refreshtoken', 'confirmEmail', 'resendEmailConfirmation']; |
|
31 | + |
|
32 | + /** |
|
33 | + * The validations rules used by the base api controller |
|
34 | + * to check before add. |
|
35 | + * @var array |
|
36 | + */ |
|
37 | + protected $validationRules = [ |
|
38 | + 'name' => 'nullable|string', |
|
39 | + 'email' => 'required|email|unique:users,email,{id}', |
|
40 | + 'password' => 'nullable|min:6' |
|
41 | + ]; |
|
42 | + |
|
43 | + /** |
|
44 | + * The loginProxy implementation. |
|
45 | + * |
|
46 | + * @var \App\Modules\Acl\Proxy\LoginProxy |
|
47 | + */ |
|
48 | + protected $loginProxy; |
|
49 | + |
|
50 | + public function __construct(LoginProxy $loginProxy) |
|
51 | + { |
|
52 | + $this->loginProxy = $loginProxy; |
|
53 | + parent::__construct(); |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Return the logged in user account. |
|
58 | + * |
|
59 | + * @return \Illuminate\Http\Response |
|
60 | + */ |
|
61 | + public function account() |
|
62 | + { |
|
63 | + return \Response::json($this->repo->account($this->relations), 200); |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * Block the user. |
|
68 | + * |
|
69 | + * @param integer $id Id of the user. |
|
70 | + * @return \Illuminate\Http\Response |
|
71 | + */ |
|
72 | + public function block($id) |
|
73 | + { |
|
74 | + return \Response::json($this->repo->block($id), 200); |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Unblock the user. |
|
79 | + * |
|
80 | + * @param integer $id Id of the user. |
|
81 | + * @return \Illuminate\Http\Response |
|
82 | + */ |
|
83 | + public function unblock($id) |
|
84 | + { |
|
85 | + return \Response::json($this->repo->unblock($id), 200); |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Logout the user. |
|
90 | + * |
|
91 | + * @return \Illuminate\Http\Response |
|
92 | + */ |
|
93 | + public function logout() |
|
94 | + { |
|
95 | + return \Response::json($this->loginProxy->logout(), 200); |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Handle a registration request. |
|
100 | + * |
|
101 | + * @param \Illuminate\Http\Request $request |
|
102 | + * @return \Illuminate\Http\Response |
|
103 | + */ |
|
104 | + public function register(Request $request) |
|
105 | + { |
|
106 | + $this->validate($request, [ |
|
107 | + 'name' => 'nullable|string', |
|
108 | + 'email' => 'required|email|unique:users,email,{id}', |
|
109 | + 'password' => 'required|min:6' |
|
110 | + ]); |
|
111 | + |
|
112 | + return \Response::json($this->repo->register($request->only('name', 'email', 'password')), 200); |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Handle a login request to the application. |
|
117 | + * |
|
118 | + * @param \Illuminate\Http\Request $request |
|
119 | + * @return \Illuminate\Http\Response |
|
120 | + */ |
|
121 | + public function login(Request $request) |
|
122 | + { |
|
123 | + $this->validate($request, [ |
|
124 | + 'email' => 'required|email', |
|
125 | + 'password' => 'required|min:6', |
|
126 | + 'admin' => 'nullable|boolean' |
|
127 | + ]); |
|
128 | + |
|
129 | + return \Response::json($this->loginProxy->login($request->only('email', 'password'), $request->get('admin')), 200); |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Handle a social login request of the none admin to the application. |
|
134 | + * |
|
135 | + * @param \Illuminate\Http\Request $request |
|
136 | + * @return \Illuminate\Http\Response |
|
137 | + */ |
|
138 | + public function loginSocial(Request $request) |
|
139 | + { |
|
140 | + $this->validate($request, [ |
|
141 | + 'auth_code' => 'required_without:access_token', |
|
142 | + 'access_token' => 'required_without:auth_code', |
|
143 | + 'type' => 'required|in:facebook,google' |
|
144 | + ]); |
|
145 | + |
|
146 | + return \Response::json($this->repo->loginSocial($request->get('auth_code'), $request->get('access_token'), $request->get('type')), 200); |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Assign the given groups to the given user. |
|
151 | + * |
|
152 | + * @param \Illuminate\Http\Request $request |
|
153 | + * @return \Illuminate\Http\Response |
|
154 | + */ |
|
155 | + public function assigngroups(Request $request) |
|
156 | + { |
|
157 | + $this->validate($request, [ |
|
158 | + 'group_ids' => 'required|exists:groups,id', |
|
159 | + 'user_id' => 'required|exists:users,id' |
|
160 | + ]); |
|
161 | + |
|
162 | + return \Response::json($this->repo->assignGroups($request->get('user_id'), $request->get('group_ids')), 200); |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * Send a reset link to the given user. |
|
167 | + * |
|
168 | + * @param \Illuminate\Http\Request $request |
|
169 | + * @return \Illuminate\Http\Response |
|
170 | + */ |
|
171 | + public function sendreset(Request $request) |
|
172 | + { |
|
173 | + $this->validate($request, ['email' => 'required|email']); |
|
174 | + |
|
175 | + return \Response::json($this->repo->sendReset($request->get('email')), 200); |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * Reset the given user's password. |
|
180 | + * |
|
181 | + * @param \Illuminate\Http\Request $request |
|
182 | + * @return \Illuminate\Http\Response |
|
183 | + */ |
|
184 | + public function resetpassword(Request $request) |
|
185 | + { |
|
186 | + $this->validate($request, [ |
|
187 | + 'token' => 'required', |
|
188 | + 'email' => 'required|email', |
|
189 | + 'password' => 'required|confirmed|min:6', |
|
190 | + 'password_confirmation' => 'required', |
|
191 | + ]); |
|
192 | + |
|
193 | + return \Response::json($this->repo->resetPassword($request->only('email', 'password', 'password_confirmation', 'token')), 200); |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * Change the logged in user password. |
|
198 | + * |
|
199 | + * @param \Illuminate\Http\Request $request |
|
200 | + * @return \Illuminate\Http\Response |
|
201 | + */ |
|
202 | + public function changePassword(Request $request) |
|
203 | + { |
|
204 | + $this->validate($request, [ |
|
205 | + 'old_password' => 'required', |
|
206 | + 'password' => 'required|confirmed|min:6', |
|
207 | + 'password_confirmation' => 'required', |
|
208 | + ]); |
|
209 | + |
|
210 | + return \Response::json($this->repo->changePassword($request->only('old_password', 'password', 'password_confirmation')), 200); |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * Confirm email using the confirmation code. |
|
215 | + * |
|
216 | + * @param \Illuminate\Http\Request $request |
|
217 | + * @return \Illuminate\Http\Response |
|
218 | + */ |
|
219 | + public function confirmEmail(Request $request) |
|
220 | + { |
|
221 | + $this->validate($request, [ |
|
222 | + 'confirmation_code' => 'required|string|exists:users,confirmation_code' |
|
223 | + ]); |
|
224 | + |
|
225 | + return \Response::json($this->repo->confirmEmail($request->only('confirmation_code')), 200); |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * Resend the email confirmation mail. |
|
230 | + * |
|
231 | + * @param \Illuminate\Http\Request $request |
|
232 | + * @return \Illuminate\Http\Response |
|
233 | + */ |
|
234 | + public function resendEmailConfirmation(Request $request) |
|
235 | + { |
|
236 | + $this->validate($request, [ |
|
237 | + 'email' => 'required|exists:users,email' |
|
238 | + ]); |
|
239 | + |
|
240 | + return \Response::json($this->repo->sendConfirmationEmail($request->get('email')), 200); |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * Refresh the expired login token. |
|
245 | + * |
|
246 | + * @param \Illuminate\Http\Request $request |
|
247 | + * @return \Illuminate\Http\Response |
|
248 | + */ |
|
249 | + public function refreshtoken(Request $request) |
|
250 | + { |
|
251 | + $this->validate($request, [ |
|
252 | + 'refreshtoken' => 'required', |
|
253 | + ]); |
|
254 | + |
|
255 | + return \Response::json($this->loginProxy->refreshtoken($request->get('refreshtoken')), 200); |
|
256 | + } |
|
257 | + |
|
258 | + /** |
|
259 | + * Paginate all users with in the given group. |
|
260 | + * |
|
261 | + * @param \Illuminate\Http\Request $request |
|
262 | + * @param string $groupName The name of the requested group. |
|
263 | + * @param integer $perPage Number of rows per page default 15. |
|
264 | + * @param string $sortBy The name of the column to sort by. |
|
265 | + * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
266 | + * @return \Illuminate\Http\Response |
|
267 | + */ |
|
268 | + public function group(Request $request, $groupName, $perPage = false, $sortBy = 'created_at', $desc = 1) |
|
269 | + { |
|
270 | + return \Response::json($this->repo->group($request->all(), $groupName, $this->relations, $perPage, $sortBy, $desc), 200); |
|
271 | + } |
|
272 | + |
|
273 | + /** |
|
274 | + * Save the given data to the logged in user. |
|
275 | + * |
|
276 | + * @param \Illuminate\Http\Request $request |
|
277 | + * @return \Illuminate\Http\Response |
|
278 | + */ |
|
279 | + public function saveProfile(Request $request) |
|
280 | + { |
|
281 | + $this->validate($request, [ |
|
282 | + 'profile_picture' => 'nullable|string', |
|
283 | + 'name' => 'nullable|string', |
|
284 | + 'email' => 'required|email|unique:users,email,' . \Auth::id() |
|
285 | + ]); |
|
286 | + |
|
287 | + return \Response::json($this->repo->saveProfile($request->only('name', 'email', 'profile_picture')), 200); |
|
288 | + } |
|
289 | 289 | } |
@@ -2,117 +2,117 @@ |
||
2 | 2 | |
3 | 3 | class CachingDecorator |
4 | 4 | { |
5 | - /** |
|
6 | - * The repo implementation. |
|
7 | - * |
|
8 | - * @var string |
|
9 | - */ |
|
10 | - public $repo; |
|
5 | + /** |
|
6 | + * The repo implementation. |
|
7 | + * |
|
8 | + * @var string |
|
9 | + */ |
|
10 | + public $repo; |
|
11 | 11 | |
12 | - /** |
|
13 | - * The cache implementation. |
|
14 | - * |
|
15 | - * @var object |
|
16 | - */ |
|
17 | - protected $cache; |
|
12 | + /** |
|
13 | + * The cache implementation. |
|
14 | + * |
|
15 | + * @var object |
|
16 | + */ |
|
17 | + protected $cache; |
|
18 | 18 | |
19 | - /** |
|
20 | - * The modelKey implementation. |
|
21 | - * |
|
22 | - * @var string |
|
23 | - */ |
|
24 | - public $modelKey; |
|
19 | + /** |
|
20 | + * The modelKey implementation. |
|
21 | + * |
|
22 | + * @var string |
|
23 | + */ |
|
24 | + public $modelKey; |
|
25 | 25 | |
26 | - /** |
|
27 | - * The model implementation. |
|
28 | - * |
|
29 | - * @var string |
|
30 | - */ |
|
31 | - public $model; |
|
26 | + /** |
|
27 | + * The model implementation. |
|
28 | + * |
|
29 | + * @var string |
|
30 | + */ |
|
31 | + public $model; |
|
32 | 32 | |
33 | - /** |
|
34 | - * The modelClass implementation. |
|
35 | - * |
|
36 | - * @var string |
|
37 | - */ |
|
38 | - public $modelClass; |
|
33 | + /** |
|
34 | + * The modelClass implementation. |
|
35 | + * |
|
36 | + * @var string |
|
37 | + */ |
|
38 | + public $modelClass; |
|
39 | 39 | |
40 | - /** |
|
41 | - * The cacheConfig implementation. |
|
42 | - * |
|
43 | - * @var mixed |
|
44 | - */ |
|
45 | - public $cacheConfig; |
|
40 | + /** |
|
41 | + * The cacheConfig implementation. |
|
42 | + * |
|
43 | + * @var mixed |
|
44 | + */ |
|
45 | + public $cacheConfig; |
|
46 | 46 | |
47 | - /** |
|
48 | - * The cacheTag implementation. |
|
49 | - * |
|
50 | - * @var string |
|
51 | - */ |
|
52 | - public $cacheTag; |
|
47 | + /** |
|
48 | + * The cacheTag implementation. |
|
49 | + * |
|
50 | + * @var string |
|
51 | + */ |
|
52 | + public $cacheTag; |
|
53 | 53 | |
54 | - /** |
|
55 | - * Create new CachingDecorator instance. |
|
56 | - */ |
|
57 | - public function __construct($repo, $cache) |
|
58 | - { |
|
59 | - $this->repo = $repo; |
|
60 | - $this->cache = $cache; |
|
61 | - $this->model = $this->repo->model; |
|
62 | - $this->modelClass = get_class($this->model); |
|
63 | - $repoClass = explode('\\', get_class($this->repo)); |
|
64 | - $repoName = end($repoClass); |
|
65 | - $this->cacheTag = str_plural(lcfirst(substr($repoName, 0, strpos($repoName, 'Repository')))); |
|
66 | - } |
|
54 | + /** |
|
55 | + * Create new CachingDecorator instance. |
|
56 | + */ |
|
57 | + public function __construct($repo, $cache) |
|
58 | + { |
|
59 | + $this->repo = $repo; |
|
60 | + $this->cache = $cache; |
|
61 | + $this->model = $this->repo->model; |
|
62 | + $this->modelClass = get_class($this->model); |
|
63 | + $repoClass = explode('\\', get_class($this->repo)); |
|
64 | + $repoName = end($repoClass); |
|
65 | + $this->cacheTag = str_plural(lcfirst(substr($repoName, 0, strpos($repoName, 'Repository')))); |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * Handle the cache mechanism for the called method |
|
70 | - * based the configurations. |
|
71 | - * |
|
72 | - * @param string $name the called method name |
|
73 | - * @param array $arguments the method arguments |
|
74 | - * @return object |
|
75 | - */ |
|
76 | - public function __call($name, $arguments) |
|
77 | - { |
|
78 | - $this->setCacheConfig($name); |
|
68 | + /** |
|
69 | + * Handle the cache mechanism for the called method |
|
70 | + * based the configurations. |
|
71 | + * |
|
72 | + * @param string $name the called method name |
|
73 | + * @param array $arguments the method arguments |
|
74 | + * @return object |
|
75 | + */ |
|
76 | + public function __call($name, $arguments) |
|
77 | + { |
|
78 | + $this->setCacheConfig($name); |
|
79 | 79 | |
80 | - if ($this->cacheConfig && $this->cacheConfig == 'cache') |
|
81 | - { |
|
82 | - $page = \Request::get('page') !== null ? \Request::get('page') : '1'; |
|
83 | - $cacheKey = $name . $page . \Session::get('locale') . serialize($arguments); |
|
84 | - return $this->cache->tags([$this->cacheTag])->rememberForever($cacheKey, function() use ($arguments, $name) { |
|
85 | - return call_user_func_array([$this->repo, $name], $arguments); |
|
86 | - }); |
|
87 | - } |
|
88 | - else if ($this->cacheConfig) |
|
89 | - { |
|
90 | - $this->cache->tags($this->cacheConfig)->flush(); |
|
91 | - return call_user_func_array([$this->repo, $name], $arguments); |
|
92 | - } |
|
80 | + if ($this->cacheConfig && $this->cacheConfig == 'cache') |
|
81 | + { |
|
82 | + $page = \Request::get('page') !== null ? \Request::get('page') : '1'; |
|
83 | + $cacheKey = $name . $page . \Session::get('locale') . serialize($arguments); |
|
84 | + return $this->cache->tags([$this->cacheTag])->rememberForever($cacheKey, function() use ($arguments, $name) { |
|
85 | + return call_user_func_array([$this->repo, $name], $arguments); |
|
86 | + }); |
|
87 | + } |
|
88 | + else if ($this->cacheConfig) |
|
89 | + { |
|
90 | + $this->cache->tags($this->cacheConfig)->flush(); |
|
91 | + return call_user_func_array([$this->repo, $name], $arguments); |
|
92 | + } |
|
93 | 93 | |
94 | - return call_user_func_array([$this->repo, $name], $arguments); |
|
95 | - } |
|
94 | + return call_user_func_array([$this->repo, $name], $arguments); |
|
95 | + } |
|
96 | 96 | |
97 | - /** |
|
98 | - * Set cache config based on the called method. |
|
99 | - * |
|
100 | - * @param string $name |
|
101 | - * @return void |
|
102 | - */ |
|
103 | - private function setCacheConfig($name) |
|
104 | - { |
|
105 | - $config = \CoreConfig::getConfig(); |
|
106 | - $cacheConfig = array_key_exists($this->cacheTag, $config['cacheConfig']) ? $config['cacheConfig'][$this->cacheTag] : false; |
|
107 | - $this->cacheConfig = false; |
|
97 | + /** |
|
98 | + * Set cache config based on the called method. |
|
99 | + * |
|
100 | + * @param string $name |
|
101 | + * @return void |
|
102 | + */ |
|
103 | + private function setCacheConfig($name) |
|
104 | + { |
|
105 | + $config = \CoreConfig::getConfig(); |
|
106 | + $cacheConfig = array_key_exists($this->cacheTag, $config['cacheConfig']) ? $config['cacheConfig'][$this->cacheTag] : false; |
|
107 | + $this->cacheConfig = false; |
|
108 | 108 | |
109 | - if ($cacheConfig && in_array($name, $cacheConfig['cache'])) |
|
110 | - { |
|
111 | - $this->cacheConfig = 'cache'; |
|
112 | - } |
|
113 | - else if ($cacheConfig && isset($cacheConfig['clear'][$name])) |
|
114 | - { |
|
115 | - $this->cacheConfig = $cacheConfig['clear'][$name]; |
|
116 | - } |
|
117 | - } |
|
109 | + if ($cacheConfig && in_array($name, $cacheConfig['cache'])) |
|
110 | + { |
|
111 | + $this->cacheConfig = 'cache'; |
|
112 | + } |
|
113 | + else if ($cacheConfig && isset($cacheConfig['clear'][$name])) |
|
114 | + { |
|
115 | + $this->cacheConfig = $cacheConfig['clear'][$name]; |
|
116 | + } |
|
117 | + } |
|
118 | 118 | } |
119 | 119 | \ No newline at end of file |
@@ -4,717 +4,717 @@ |
||
4 | 4 | |
5 | 5 | abstract class AbstractRepository implements RepositoryInterface |
6 | 6 | { |
7 | - /** |
|
8 | - * The model implementation. |
|
9 | - * |
|
10 | - * @var object |
|
11 | - */ |
|
12 | - public $model; |
|
7 | + /** |
|
8 | + * The model implementation. |
|
9 | + * |
|
10 | + * @var object |
|
11 | + */ |
|
12 | + public $model; |
|
13 | 13 | |
14 | - /** |
|
15 | - * The config implementation. |
|
16 | - * |
|
17 | - * @var array |
|
18 | - */ |
|
19 | - protected $config; |
|
14 | + /** |
|
15 | + * The config implementation. |
|
16 | + * |
|
17 | + * @var array |
|
18 | + */ |
|
19 | + protected $config; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Create new AbstractRepository instance. |
|
23 | - */ |
|
24 | - public function __construct() |
|
25 | - { |
|
26 | - $this->config = \CoreConfig::getConfig(); |
|
27 | - $this->model = \App::make($this->getModel()); |
|
28 | - } |
|
29 | - |
|
30 | - /** |
|
31 | - * Fetch all records with relations from the storage. |
|
32 | - * |
|
33 | - * @param array $relations |
|
34 | - * @param string $sortBy |
|
35 | - * @param boolean $desc |
|
36 | - * @param array $columns |
|
37 | - * @return collection |
|
38 | - */ |
|
39 | - public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
40 | - { |
|
41 | - $sort = $desc ? 'desc' : 'asc'; |
|
42 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->get($columns); |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * Fetch all records with relations from storage in pages |
|
47 | - * that matche the given query. |
|
48 | - * |
|
49 | - * @param string $query |
|
50 | - * @param integer $perPage |
|
51 | - * @param array $relations |
|
52 | - * @param string $sortBy |
|
53 | - * @param boolean $desc |
|
54 | - * @param array $columns |
|
55 | - * @return collection |
|
56 | - */ |
|
57 | - public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
58 | - { |
|
59 | - $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
60 | - $conditionColumns = $this->model->searchable; |
|
61 | - $sort = $desc ? 'desc' : 'asc'; |
|
62 | - |
|
63 | - /** |
|
64 | - * Construct the select conditions for the model. |
|
65 | - */ |
|
66 | - $model->where(function ($q) use ($query, $conditionColumns, $relations){ |
|
67 | - |
|
68 | - if (count($conditionColumns)) |
|
69 | - { |
|
70 | - $column = 'LOWER(' . array_shift($conditionColumns) . ')'; |
|
71 | - if (str_contains($column, '->')) |
|
72 | - { |
|
73 | - $column = $this->wrapJsonSelector($column); |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Use the first element in the model columns to construct the first condition. |
|
78 | - */ |
|
79 | - $q->where(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Loop through the rest of the columns to construct or where conditions. |
|
84 | - */ |
|
85 | - foreach ($conditionColumns as $column) |
|
86 | - { |
|
87 | - $column = 'LOWER(' . $column . ')'; |
|
88 | - if (str_contains($column, '->')) |
|
89 | - { |
|
90 | - $column = $this->wrapJsonSelector($column); |
|
91 | - } |
|
92 | - |
|
93 | - $q->orWhere(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Loop through the model relations. |
|
98 | - */ |
|
99 | - foreach ($relations as $relation) |
|
100 | - { |
|
101 | - /** |
|
102 | - * Remove the sub relation if exists. |
|
103 | - */ |
|
104 | - $relation = explode('.', $relation)[0]; |
|
105 | - |
|
106 | - /** |
|
107 | - * Try to fetch the relation repository from the core. |
|
108 | - */ |
|
109 | - if (\Core::$relation()) |
|
110 | - { |
|
111 | - /** |
|
112 | - * Construct the relation condition. |
|
113 | - */ |
|
114 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
115 | - |
|
116 | - $subModel->where(function ($q) use ($query, $relation){ |
|
117 | - |
|
118 | - /** |
|
119 | - * Get columns of the relation. |
|
120 | - */ |
|
121 | - $subConditionColumns = \Core::$relation()->model->searchable; |
|
122 | - |
|
123 | - if (count($subConditionColumns)) |
|
124 | - { |
|
125 | - |
|
126 | - $column = 'LOWER(' . array_shift($subConditionColumns) . ')'; |
|
127 | - if (str_contains($column, '->')) |
|
128 | - { |
|
129 | - $column = $this->wrapJsonSelector($column); |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Use the first element in the relation model columns to construct the first condition. |
|
134 | - */ |
|
135 | - $q->where(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * Loop through the rest of the columns to construct or where conditions. |
|
140 | - */ |
|
141 | - foreach ($subConditionColumns as $subConditionColumn) |
|
142 | - { |
|
143 | - $column = 'LOWER(' . $subConditionColumn . ')'; |
|
144 | - if (str_contains($column, '->')) |
|
145 | - { |
|
146 | - $column = $this->wrapJsonSelector($column); |
|
147 | - } |
|
21 | + /** |
|
22 | + * Create new AbstractRepository instance. |
|
23 | + */ |
|
24 | + public function __construct() |
|
25 | + { |
|
26 | + $this->config = \CoreConfig::getConfig(); |
|
27 | + $this->model = \App::make($this->getModel()); |
|
28 | + } |
|
29 | + |
|
30 | + /** |
|
31 | + * Fetch all records with relations from the storage. |
|
32 | + * |
|
33 | + * @param array $relations |
|
34 | + * @param string $sortBy |
|
35 | + * @param boolean $desc |
|
36 | + * @param array $columns |
|
37 | + * @return collection |
|
38 | + */ |
|
39 | + public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
40 | + { |
|
41 | + $sort = $desc ? 'desc' : 'asc'; |
|
42 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->get($columns); |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * Fetch all records with relations from storage in pages |
|
47 | + * that matche the given query. |
|
48 | + * |
|
49 | + * @param string $query |
|
50 | + * @param integer $perPage |
|
51 | + * @param array $relations |
|
52 | + * @param string $sortBy |
|
53 | + * @param boolean $desc |
|
54 | + * @param array $columns |
|
55 | + * @return collection |
|
56 | + */ |
|
57 | + public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
58 | + { |
|
59 | + $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
60 | + $conditionColumns = $this->model->searchable; |
|
61 | + $sort = $desc ? 'desc' : 'asc'; |
|
62 | + |
|
63 | + /** |
|
64 | + * Construct the select conditions for the model. |
|
65 | + */ |
|
66 | + $model->where(function ($q) use ($query, $conditionColumns, $relations){ |
|
67 | + |
|
68 | + if (count($conditionColumns)) |
|
69 | + { |
|
70 | + $column = 'LOWER(' . array_shift($conditionColumns) . ')'; |
|
71 | + if (str_contains($column, '->')) |
|
72 | + { |
|
73 | + $column = $this->wrapJsonSelector($column); |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Use the first element in the model columns to construct the first condition. |
|
78 | + */ |
|
79 | + $q->where(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Loop through the rest of the columns to construct or where conditions. |
|
84 | + */ |
|
85 | + foreach ($conditionColumns as $column) |
|
86 | + { |
|
87 | + $column = 'LOWER(' . $column . ')'; |
|
88 | + if (str_contains($column, '->')) |
|
89 | + { |
|
90 | + $column = $this->wrapJsonSelector($column); |
|
91 | + } |
|
92 | + |
|
93 | + $q->orWhere(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Loop through the model relations. |
|
98 | + */ |
|
99 | + foreach ($relations as $relation) |
|
100 | + { |
|
101 | + /** |
|
102 | + * Remove the sub relation if exists. |
|
103 | + */ |
|
104 | + $relation = explode('.', $relation)[0]; |
|
105 | + |
|
106 | + /** |
|
107 | + * Try to fetch the relation repository from the core. |
|
108 | + */ |
|
109 | + if (\Core::$relation()) |
|
110 | + { |
|
111 | + /** |
|
112 | + * Construct the relation condition. |
|
113 | + */ |
|
114 | + $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
115 | + |
|
116 | + $subModel->where(function ($q) use ($query, $relation){ |
|
117 | + |
|
118 | + /** |
|
119 | + * Get columns of the relation. |
|
120 | + */ |
|
121 | + $subConditionColumns = \Core::$relation()->model->searchable; |
|
122 | + |
|
123 | + if (count($subConditionColumns)) |
|
124 | + { |
|
125 | + |
|
126 | + $column = 'LOWER(' . array_shift($subConditionColumns) . ')'; |
|
127 | + if (str_contains($column, '->')) |
|
128 | + { |
|
129 | + $column = $this->wrapJsonSelector($column); |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Use the first element in the relation model columns to construct the first condition. |
|
134 | + */ |
|
135 | + $q->where(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * Loop through the rest of the columns to construct or where conditions. |
|
140 | + */ |
|
141 | + foreach ($subConditionColumns as $subConditionColumn) |
|
142 | + { |
|
143 | + $column = 'LOWER(' . $subConditionColumn . ')'; |
|
144 | + if (str_contains($column, '->')) |
|
145 | + { |
|
146 | + $column = $this->wrapJsonSelector($column); |
|
147 | + } |
|
148 | 148 | |
149 | - $q->orWhere(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
150 | - } |
|
151 | - }); |
|
152 | - |
|
153 | - }); |
|
154 | - } |
|
155 | - } |
|
156 | - }); |
|
149 | + $q->orWhere(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
150 | + } |
|
151 | + }); |
|
152 | + |
|
153 | + }); |
|
154 | + } |
|
155 | + } |
|
156 | + }); |
|
157 | 157 | |
158 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
159 | - } |
|
158 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
159 | + } |
|
160 | 160 | |
161 | - /** |
|
162 | - * Fetch all records with relations from storage in pages. |
|
163 | - * |
|
164 | - * @param integer $perPage |
|
165 | - * @param array $relations |
|
166 | - * @param string $sortBy |
|
167 | - * @param boolean $desc |
|
168 | - * @param array $columns |
|
169 | - * @return collection |
|
170 | - */ |
|
171 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
172 | - { |
|
173 | - $sort = $desc ? 'desc' : 'asc'; |
|
174 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * Fetch all records with relations based on |
|
179 | - * the given condition from storage in pages. |
|
180 | - * |
|
181 | - * @param array $conditions array of conditions |
|
182 | - * @param integer $perPage |
|
183 | - * @param array $relations |
|
184 | - * @param string $sortBy |
|
185 | - * @param boolean $desc |
|
186 | - * @param array $columns |
|
187 | - * @return collection |
|
188 | - */ |
|
189 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
190 | - { |
|
191 | - unset($conditions['page']); |
|
192 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
193 | - $sort = $desc ? 'desc' : 'asc'; |
|
194 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
195 | - } |
|
161 | + /** |
|
162 | + * Fetch all records with relations from storage in pages. |
|
163 | + * |
|
164 | + * @param integer $perPage |
|
165 | + * @param array $relations |
|
166 | + * @param string $sortBy |
|
167 | + * @param boolean $desc |
|
168 | + * @param array $columns |
|
169 | + * @return collection |
|
170 | + */ |
|
171 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
172 | + { |
|
173 | + $sort = $desc ? 'desc' : 'asc'; |
|
174 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * Fetch all records with relations based on |
|
179 | + * the given condition from storage in pages. |
|
180 | + * |
|
181 | + * @param array $conditions array of conditions |
|
182 | + * @param integer $perPage |
|
183 | + * @param array $relations |
|
184 | + * @param string $sortBy |
|
185 | + * @param boolean $desc |
|
186 | + * @param array $columns |
|
187 | + * @return collection |
|
188 | + */ |
|
189 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
190 | + { |
|
191 | + unset($conditions['page']); |
|
192 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
193 | + $sort = $desc ? 'desc' : 'asc'; |
|
194 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
195 | + } |
|
196 | 196 | |
197 | - /** |
|
198 | - * Save the given model to the storage. |
|
199 | - * |
|
200 | - * @param array $data |
|
201 | - * @return mixed |
|
202 | - */ |
|
203 | - public function save(array $data) |
|
204 | - { |
|
205 | - \Session::put('locale', 'all'); |
|
206 | - $model = false; |
|
207 | - $modelClass = $this->model; |
|
208 | - $relations = []; |
|
209 | - |
|
210 | - \DB::transaction(function () use (&$model, &$relations, $data, $modelClass) { |
|
211 | - /** |
|
212 | - * If the id is present in the data then select the model for updating, |
|
213 | - * else create new model. |
|
214 | - * @var array |
|
215 | - */ |
|
216 | - $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
217 | - if ( ! $model) |
|
218 | - { |
|
219 | - \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * Construct the model object with the given data, |
|
224 | - * and if there is a relation add it to relations array, |
|
225 | - * then save the model. |
|
226 | - */ |
|
227 | - foreach ($data as $key => $value) |
|
228 | - { |
|
229 | - /** |
|
230 | - * If the attribute is a relation. |
|
231 | - */ |
|
232 | - $relation = camel_case($key); |
|
233 | - if (method_exists($model, $relation) && \Core::$relation()) |
|
234 | - { |
|
235 | - /** |
|
236 | - * Check if the relation is a collection. |
|
237 | - */ |
|
238 | - if (class_basename($model->$relation) == 'Collection') |
|
239 | - { |
|
240 | - /** |
|
241 | - * If the relation has no value then marke the relation data |
|
242 | - * related to the model to be deleted. |
|
243 | - */ |
|
244 | - if ( ! $value || ! count($value)) |
|
245 | - { |
|
246 | - $relations[$relation] = 'delete'; |
|
247 | - } |
|
248 | - } |
|
249 | - if (is_array($value)) |
|
250 | - { |
|
251 | - /** |
|
252 | - * Loop through the relation data. |
|
253 | - */ |
|
254 | - foreach ($value as $attr => $val) |
|
255 | - { |
|
256 | - /** |
|
257 | - * Get the relation model. |
|
258 | - */ |
|
259 | - $relationBaseModel = \Core::$relation()->model; |
|
260 | - |
|
261 | - /** |
|
262 | - * Check if the relation is a collection. |
|
263 | - */ |
|
264 | - if (class_basename($model->$relation) == 'Collection') |
|
265 | - { |
|
266 | - /** |
|
267 | - * If the id is present in the data then select the relation model for updating, |
|
268 | - * else create new model. |
|
269 | - */ |
|
270 | - $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
271 | - |
|
272 | - /** |
|
273 | - * If model doesn't exists. |
|
274 | - */ |
|
275 | - if ( ! $relationModel) |
|
276 | - { |
|
277 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
278 | - } |
|
279 | - |
|
280 | - /** |
|
281 | - * Loop through the relation attributes. |
|
282 | - */ |
|
283 | - foreach ($val as $attr => $val) |
|
284 | - { |
|
285 | - /** |
|
286 | - * Prevent the sub relations or attributes not in the fillable. |
|
287 | - */ |
|
288 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
289 | - { |
|
290 | - $relationModel->$attr = $val; |
|
291 | - } |
|
292 | - } |
|
293 | - |
|
294 | - $relations[$relation][] = $relationModel; |
|
295 | - } |
|
296 | - /** |
|
297 | - * If not collection. |
|
298 | - */ |
|
299 | - else |
|
300 | - { |
|
301 | - /** |
|
302 | - * Prevent the sub relations. |
|
303 | - */ |
|
304 | - if (gettype($val) !== 'object' && gettype($val) !== 'array') |
|
305 | - { |
|
306 | - |
|
307 | - /** |
|
308 | - * If the id is present in the data then select the relation model for updating, |
|
309 | - * else create new model. |
|
310 | - */ |
|
311 | - $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
312 | - |
|
313 | - /** |
|
314 | - * If model doesn't exists. |
|
315 | - */ |
|
316 | - if ( ! $relationModel) |
|
317 | - { |
|
318 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
319 | - } |
|
320 | - |
|
321 | - foreach ($value as $relationAttribute => $relationValue) |
|
322 | - { |
|
323 | - /** |
|
324 | - * Prevent attributes not in the fillable. |
|
325 | - */ |
|
326 | - if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) |
|
327 | - { |
|
328 | - $relationModel->$relationAttribute = $relationValue; |
|
329 | - } |
|
330 | - } |
|
331 | - |
|
332 | - $relations[$relation] = $relationModel; |
|
333 | - } |
|
334 | - } |
|
335 | - } |
|
336 | - } |
|
337 | - } |
|
338 | - /** |
|
339 | - * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
340 | - */ |
|
341 | - else if (array_search($key, $model->getFillable(), true) !== false) |
|
342 | - { |
|
343 | - $model->$key = $value; |
|
344 | - } |
|
345 | - } |
|
346 | - |
|
347 | - /** |
|
348 | - * Loop through the relations array. |
|
349 | - */ |
|
350 | - foreach ($relations as $key => $value) |
|
351 | - { |
|
352 | - /** |
|
353 | - * If the relation is marked for delete then delete it. |
|
354 | - */ |
|
355 | - if ($value == 'delete' && $model->$key()->count()) |
|
356 | - { |
|
357 | - $model->$key()->delete(); |
|
358 | - } |
|
359 | - /** |
|
360 | - * If the relation is an array. |
|
361 | - */ |
|
362 | - else if (gettype($value) == 'array') |
|
363 | - { |
|
364 | - /** |
|
365 | - * Save the model. |
|
366 | - */ |
|
367 | - $model->save(); |
|
368 | - $ids = []; |
|
369 | - |
|
370 | - /** |
|
371 | - * Loop through the relations. |
|
372 | - */ |
|
373 | - foreach ($value as $val) |
|
374 | - { |
|
375 | - switch (class_basename($model->$key())) |
|
376 | - { |
|
377 | - /** |
|
378 | - * If the relation is one to many then update it's foreign key with |
|
379 | - * the model id and save it then add its id to ids array to delete all |
|
380 | - * relations who's id isn't in the ids array. |
|
381 | - */ |
|
382 | - case 'HasMany': |
|
383 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
384 | - $val->$foreignKeyName = $model->id; |
|
385 | - $val->save(); |
|
386 | - $ids[] = $val->id; |
|
387 | - break; |
|
388 | - |
|
389 | - /** |
|
390 | - * If the relation is many to many then add it's id to the ids array to |
|
391 | - * attache these ids to the model. |
|
392 | - */ |
|
393 | - case 'BelongsToMany': |
|
394 | - $val->save(); |
|
395 | - $ids[] = $val->id; |
|
396 | - break; |
|
397 | - } |
|
398 | - } |
|
399 | - switch (class_basename($model->$key())) |
|
400 | - { |
|
401 | - /** |
|
402 | - * If the relation is one to many then delete all |
|
403 | - * relations who's id isn't in the ids array. |
|
404 | - */ |
|
405 | - case 'HasMany': |
|
406 | - $model->$key()->whereNotIn('id', $ids)->delete(); |
|
407 | - break; |
|
408 | - |
|
409 | - /** |
|
410 | - * If the relation is many to many then |
|
411 | - * detach the previous data and attach |
|
412 | - * the ids array to the model. |
|
413 | - */ |
|
414 | - case 'BelongsToMany': |
|
415 | - $model->$key()->detach(); |
|
416 | - $model->$key()->attach($ids); |
|
417 | - break; |
|
418 | - } |
|
419 | - } |
|
420 | - /** |
|
421 | - * If the relation isn't array. |
|
422 | - */ |
|
423 | - else |
|
424 | - { |
|
425 | - switch (class_basename($model->$key())) |
|
426 | - { |
|
427 | - /** |
|
428 | - * If the relation is one to one. |
|
429 | - */ |
|
430 | - case 'HasOne': |
|
431 | - /** |
|
432 | - * Save the model. |
|
433 | - */ |
|
434 | - $model->save(); |
|
435 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
436 | - $value->$foreignKeyName = $model->id; |
|
437 | - $value->save(); |
|
438 | - break; |
|
439 | - case 'BelongsTo': |
|
440 | - /** |
|
441 | - * Save the model. |
|
442 | - */ |
|
443 | - $value->save(); |
|
444 | - $model->$key()->associate($value); |
|
445 | - break; |
|
446 | - } |
|
447 | - } |
|
448 | - } |
|
449 | - |
|
450 | - /** |
|
451 | - * Save the model. |
|
452 | - */ |
|
453 | - $model->save(); |
|
454 | - }); |
|
197 | + /** |
|
198 | + * Save the given model to the storage. |
|
199 | + * |
|
200 | + * @param array $data |
|
201 | + * @return mixed |
|
202 | + */ |
|
203 | + public function save(array $data) |
|
204 | + { |
|
205 | + \Session::put('locale', 'all'); |
|
206 | + $model = false; |
|
207 | + $modelClass = $this->model; |
|
208 | + $relations = []; |
|
209 | + |
|
210 | + \DB::transaction(function () use (&$model, &$relations, $data, $modelClass) { |
|
211 | + /** |
|
212 | + * If the id is present in the data then select the model for updating, |
|
213 | + * else create new model. |
|
214 | + * @var array |
|
215 | + */ |
|
216 | + $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
217 | + if ( ! $model) |
|
218 | + { |
|
219 | + \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * Construct the model object with the given data, |
|
224 | + * and if there is a relation add it to relations array, |
|
225 | + * then save the model. |
|
226 | + */ |
|
227 | + foreach ($data as $key => $value) |
|
228 | + { |
|
229 | + /** |
|
230 | + * If the attribute is a relation. |
|
231 | + */ |
|
232 | + $relation = camel_case($key); |
|
233 | + if (method_exists($model, $relation) && \Core::$relation()) |
|
234 | + { |
|
235 | + /** |
|
236 | + * Check if the relation is a collection. |
|
237 | + */ |
|
238 | + if (class_basename($model->$relation) == 'Collection') |
|
239 | + { |
|
240 | + /** |
|
241 | + * If the relation has no value then marke the relation data |
|
242 | + * related to the model to be deleted. |
|
243 | + */ |
|
244 | + if ( ! $value || ! count($value)) |
|
245 | + { |
|
246 | + $relations[$relation] = 'delete'; |
|
247 | + } |
|
248 | + } |
|
249 | + if (is_array($value)) |
|
250 | + { |
|
251 | + /** |
|
252 | + * Loop through the relation data. |
|
253 | + */ |
|
254 | + foreach ($value as $attr => $val) |
|
255 | + { |
|
256 | + /** |
|
257 | + * Get the relation model. |
|
258 | + */ |
|
259 | + $relationBaseModel = \Core::$relation()->model; |
|
260 | + |
|
261 | + /** |
|
262 | + * Check if the relation is a collection. |
|
263 | + */ |
|
264 | + if (class_basename($model->$relation) == 'Collection') |
|
265 | + { |
|
266 | + /** |
|
267 | + * If the id is present in the data then select the relation model for updating, |
|
268 | + * else create new model. |
|
269 | + */ |
|
270 | + $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
271 | + |
|
272 | + /** |
|
273 | + * If model doesn't exists. |
|
274 | + */ |
|
275 | + if ( ! $relationModel) |
|
276 | + { |
|
277 | + \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
278 | + } |
|
279 | + |
|
280 | + /** |
|
281 | + * Loop through the relation attributes. |
|
282 | + */ |
|
283 | + foreach ($val as $attr => $val) |
|
284 | + { |
|
285 | + /** |
|
286 | + * Prevent the sub relations or attributes not in the fillable. |
|
287 | + */ |
|
288 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
289 | + { |
|
290 | + $relationModel->$attr = $val; |
|
291 | + } |
|
292 | + } |
|
293 | + |
|
294 | + $relations[$relation][] = $relationModel; |
|
295 | + } |
|
296 | + /** |
|
297 | + * If not collection. |
|
298 | + */ |
|
299 | + else |
|
300 | + { |
|
301 | + /** |
|
302 | + * Prevent the sub relations. |
|
303 | + */ |
|
304 | + if (gettype($val) !== 'object' && gettype($val) !== 'array') |
|
305 | + { |
|
306 | + |
|
307 | + /** |
|
308 | + * If the id is present in the data then select the relation model for updating, |
|
309 | + * else create new model. |
|
310 | + */ |
|
311 | + $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
312 | + |
|
313 | + /** |
|
314 | + * If model doesn't exists. |
|
315 | + */ |
|
316 | + if ( ! $relationModel) |
|
317 | + { |
|
318 | + \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
319 | + } |
|
320 | + |
|
321 | + foreach ($value as $relationAttribute => $relationValue) |
|
322 | + { |
|
323 | + /** |
|
324 | + * Prevent attributes not in the fillable. |
|
325 | + */ |
|
326 | + if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) |
|
327 | + { |
|
328 | + $relationModel->$relationAttribute = $relationValue; |
|
329 | + } |
|
330 | + } |
|
331 | + |
|
332 | + $relations[$relation] = $relationModel; |
|
333 | + } |
|
334 | + } |
|
335 | + } |
|
336 | + } |
|
337 | + } |
|
338 | + /** |
|
339 | + * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
340 | + */ |
|
341 | + else if (array_search($key, $model->getFillable(), true) !== false) |
|
342 | + { |
|
343 | + $model->$key = $value; |
|
344 | + } |
|
345 | + } |
|
346 | + |
|
347 | + /** |
|
348 | + * Loop through the relations array. |
|
349 | + */ |
|
350 | + foreach ($relations as $key => $value) |
|
351 | + { |
|
352 | + /** |
|
353 | + * If the relation is marked for delete then delete it. |
|
354 | + */ |
|
355 | + if ($value == 'delete' && $model->$key()->count()) |
|
356 | + { |
|
357 | + $model->$key()->delete(); |
|
358 | + } |
|
359 | + /** |
|
360 | + * If the relation is an array. |
|
361 | + */ |
|
362 | + else if (gettype($value) == 'array') |
|
363 | + { |
|
364 | + /** |
|
365 | + * Save the model. |
|
366 | + */ |
|
367 | + $model->save(); |
|
368 | + $ids = []; |
|
369 | + |
|
370 | + /** |
|
371 | + * Loop through the relations. |
|
372 | + */ |
|
373 | + foreach ($value as $val) |
|
374 | + { |
|
375 | + switch (class_basename($model->$key())) |
|
376 | + { |
|
377 | + /** |
|
378 | + * If the relation is one to many then update it's foreign key with |
|
379 | + * the model id and save it then add its id to ids array to delete all |
|
380 | + * relations who's id isn't in the ids array. |
|
381 | + */ |
|
382 | + case 'HasMany': |
|
383 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
384 | + $val->$foreignKeyName = $model->id; |
|
385 | + $val->save(); |
|
386 | + $ids[] = $val->id; |
|
387 | + break; |
|
388 | + |
|
389 | + /** |
|
390 | + * If the relation is many to many then add it's id to the ids array to |
|
391 | + * attache these ids to the model. |
|
392 | + */ |
|
393 | + case 'BelongsToMany': |
|
394 | + $val->save(); |
|
395 | + $ids[] = $val->id; |
|
396 | + break; |
|
397 | + } |
|
398 | + } |
|
399 | + switch (class_basename($model->$key())) |
|
400 | + { |
|
401 | + /** |
|
402 | + * If the relation is one to many then delete all |
|
403 | + * relations who's id isn't in the ids array. |
|
404 | + */ |
|
405 | + case 'HasMany': |
|
406 | + $model->$key()->whereNotIn('id', $ids)->delete(); |
|
407 | + break; |
|
408 | + |
|
409 | + /** |
|
410 | + * If the relation is many to many then |
|
411 | + * detach the previous data and attach |
|
412 | + * the ids array to the model. |
|
413 | + */ |
|
414 | + case 'BelongsToMany': |
|
415 | + $model->$key()->detach(); |
|
416 | + $model->$key()->attach($ids); |
|
417 | + break; |
|
418 | + } |
|
419 | + } |
|
420 | + /** |
|
421 | + * If the relation isn't array. |
|
422 | + */ |
|
423 | + else |
|
424 | + { |
|
425 | + switch (class_basename($model->$key())) |
|
426 | + { |
|
427 | + /** |
|
428 | + * If the relation is one to one. |
|
429 | + */ |
|
430 | + case 'HasOne': |
|
431 | + /** |
|
432 | + * Save the model. |
|
433 | + */ |
|
434 | + $model->save(); |
|
435 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
436 | + $value->$foreignKeyName = $model->id; |
|
437 | + $value->save(); |
|
438 | + break; |
|
439 | + case 'BelongsTo': |
|
440 | + /** |
|
441 | + * Save the model. |
|
442 | + */ |
|
443 | + $value->save(); |
|
444 | + $model->$key()->associate($value); |
|
445 | + break; |
|
446 | + } |
|
447 | + } |
|
448 | + } |
|
449 | + |
|
450 | + /** |
|
451 | + * Save the model. |
|
452 | + */ |
|
453 | + $model->save(); |
|
454 | + }); |
|
455 | 455 | |
456 | - return $model; |
|
457 | - } |
|
456 | + return $model; |
|
457 | + } |
|
458 | 458 | |
459 | - /** |
|
460 | - * Update record in the storage based on the given |
|
461 | - * condition. |
|
462 | - * |
|
463 | - * @param var $value condition value |
|
464 | - * @param array $data |
|
465 | - * @param string $attribute condition column name |
|
466 | - * @return void |
|
467 | - */ |
|
468 | - public function update($value, array $data, $attribute = 'id') |
|
469 | - { |
|
470 | - if ($attribute == 'id') |
|
471 | - { |
|
472 | - $model = $this->model->lockForUpdate()->find($value); |
|
473 | - $model ? $model->update($data) : 0; |
|
474 | - } |
|
475 | - else |
|
476 | - { |
|
477 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data){ |
|
478 | - $model->update($data); |
|
479 | - }); |
|
480 | - } |
|
481 | - } |
|
482 | - |
|
483 | - /** |
|
484 | - * Delete record from the storage based on the given |
|
485 | - * condition. |
|
486 | - * |
|
487 | - * @param var $value condition value |
|
488 | - * @param string $attribute condition column name |
|
489 | - * @return void |
|
490 | - */ |
|
491 | - public function delete($value, $attribute = 'id') |
|
492 | - { |
|
493 | - if ($attribute == 'id') |
|
494 | - { |
|
495 | - \DB::transaction(function () use ($value, $attribute, &$result) { |
|
496 | - $model = $this->model->lockForUpdate()->find($value); |
|
497 | - if ( ! $model) |
|
498 | - { |
|
499 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
500 | - } |
|
459 | + /** |
|
460 | + * Update record in the storage based on the given |
|
461 | + * condition. |
|
462 | + * |
|
463 | + * @param var $value condition value |
|
464 | + * @param array $data |
|
465 | + * @param string $attribute condition column name |
|
466 | + * @return void |
|
467 | + */ |
|
468 | + public function update($value, array $data, $attribute = 'id') |
|
469 | + { |
|
470 | + if ($attribute == 'id') |
|
471 | + { |
|
472 | + $model = $this->model->lockForUpdate()->find($value); |
|
473 | + $model ? $model->update($data) : 0; |
|
474 | + } |
|
475 | + else |
|
476 | + { |
|
477 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data){ |
|
478 | + $model->update($data); |
|
479 | + }); |
|
480 | + } |
|
481 | + } |
|
482 | + |
|
483 | + /** |
|
484 | + * Delete record from the storage based on the given |
|
485 | + * condition. |
|
486 | + * |
|
487 | + * @param var $value condition value |
|
488 | + * @param string $attribute condition column name |
|
489 | + * @return void |
|
490 | + */ |
|
491 | + public function delete($value, $attribute = 'id') |
|
492 | + { |
|
493 | + if ($attribute == 'id') |
|
494 | + { |
|
495 | + \DB::transaction(function () use ($value, $attribute, &$result) { |
|
496 | + $model = $this->model->lockForUpdate()->find($value); |
|
497 | + if ( ! $model) |
|
498 | + { |
|
499 | + \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
500 | + } |
|
501 | 501 | |
502 | - $model->delete(); |
|
503 | - }); |
|
504 | - } |
|
505 | - else |
|
506 | - { |
|
507 | - \DB::transaction(function () use ($value, $attribute, &$result) { |
|
508 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ |
|
509 | - $model->delete(); |
|
510 | - }); |
|
511 | - }); |
|
512 | - } |
|
513 | - } |
|
502 | + $model->delete(); |
|
503 | + }); |
|
504 | + } |
|
505 | + else |
|
506 | + { |
|
507 | + \DB::transaction(function () use ($value, $attribute, &$result) { |
|
508 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ |
|
509 | + $model->delete(); |
|
510 | + }); |
|
511 | + }); |
|
512 | + } |
|
513 | + } |
|
514 | 514 | |
515 | - /** |
|
516 | - * Fetch records from the storage based on the given |
|
517 | - * id. |
|
518 | - * |
|
519 | - * @param integer $id |
|
520 | - * @param array $relations |
|
521 | - * @param array $columns |
|
522 | - * @return object |
|
523 | - */ |
|
524 | - public function find($id, $relations = [], $columns = array('*')) |
|
525 | - { |
|
526 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
527 | - } |
|
515 | + /** |
|
516 | + * Fetch records from the storage based on the given |
|
517 | + * id. |
|
518 | + * |
|
519 | + * @param integer $id |
|
520 | + * @param array $relations |
|
521 | + * @param array $columns |
|
522 | + * @return object |
|
523 | + */ |
|
524 | + public function find($id, $relations = [], $columns = array('*')) |
|
525 | + { |
|
526 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
527 | + } |
|
528 | 528 | |
529 | - /** |
|
530 | - * Fetch records from the storage based on the given |
|
531 | - * condition. |
|
532 | - * |
|
533 | - * @param array $conditions array of conditions |
|
534 | - * @param array $relations |
|
535 | - * @param string $sortBy |
|
536 | - * @param boolean $desc |
|
537 | - * @param array $columns |
|
538 | - * @return collection |
|
539 | - */ |
|
540 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
541 | - { |
|
542 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
543 | - $sort = $desc ? 'desc' : 'asc'; |
|
544 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
545 | - } |
|
546 | - |
|
547 | - /** |
|
548 | - * Fetch the first record from the storage based on the given |
|
549 | - * condition. |
|
550 | - * |
|
551 | - * @param array $conditions array of conditions |
|
552 | - * @param array $relations |
|
553 | - * @param array $columns |
|
554 | - * @return object |
|
555 | - */ |
|
556 | - public function first($conditions, $relations = [], $columns = array('*')) |
|
557 | - { |
|
558 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
559 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
560 | - } |
|
561 | - |
|
562 | - /** |
|
563 | - * Return the deleted models in pages based on the given conditions. |
|
564 | - * |
|
565 | - * @param array $conditions array of conditions |
|
566 | - * @param integer $perPage |
|
567 | - * @param string $sortBy |
|
568 | - * @param boolean $desc |
|
569 | - * @param array $columns |
|
570 | - * @return collection |
|
571 | - */ |
|
572 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
573 | - { |
|
574 | - unset($conditions['page']); |
|
575 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
576 | - $sort = $desc ? 'desc' : 'asc'; |
|
577 | - $model = $this->model->onlyTrashed(); |
|
578 | - |
|
579 | - if (count($conditions['conditionValues'])) |
|
580 | - { |
|
581 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
582 | - } |
|
583 | - |
|
584 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);; |
|
585 | - } |
|
586 | - |
|
587 | - /** |
|
588 | - * Restore the deleted model. |
|
589 | - * |
|
590 | - * @param integer $id |
|
591 | - * @return void |
|
592 | - */ |
|
593 | - public function restore($id) |
|
594 | - { |
|
595 | - $model = $this->model->onlyTrashed()->find($id); |
|
596 | - |
|
597 | - if ( ! $model) |
|
598 | - { |
|
599 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id); |
|
600 | - } |
|
601 | - |
|
602 | - $model->restore(); |
|
603 | - } |
|
604 | - |
|
605 | - /** |
|
606 | - * Build the conditions recursively for the retrieving methods. |
|
607 | - * @param array $conditions |
|
608 | - * @return array |
|
609 | - */ |
|
610 | - protected function constructConditions($conditions, $model) |
|
611 | - { |
|
612 | - $conditionString = ''; |
|
613 | - $conditionValues = []; |
|
614 | - foreach ($conditions as $key => $value) |
|
615 | - { |
|
616 | - if (str_contains($key, '->')) |
|
617 | - { |
|
618 | - $key = $this->wrapJsonSelector($key); |
|
619 | - } |
|
620 | - |
|
621 | - if ($key == 'and') |
|
622 | - { |
|
623 | - $conditions = $this->constructConditions($value, $model); |
|
624 | - $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
|
625 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
626 | - } |
|
627 | - else if ($key == 'or') |
|
628 | - { |
|
629 | - $conditions = $this->constructConditions($value, $model); |
|
630 | - $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
|
631 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
632 | - } |
|
633 | - else |
|
634 | - { |
|
635 | - if (is_array($value)) |
|
636 | - { |
|
637 | - $operator = $value['op']; |
|
638 | - if (strtolower($operator) == 'between') |
|
639 | - { |
|
640 | - $value1 = $value['val1']; |
|
641 | - $value2 = $value['val2']; |
|
642 | - } |
|
643 | - else |
|
644 | - { |
|
645 | - $value = array_key_exists('val', $value) ? $value['val'] : ''; |
|
646 | - } |
|
647 | - } |
|
648 | - else |
|
649 | - { |
|
650 | - $operator = '='; |
|
651 | - } |
|
529 | + /** |
|
530 | + * Fetch records from the storage based on the given |
|
531 | + * condition. |
|
532 | + * |
|
533 | + * @param array $conditions array of conditions |
|
534 | + * @param array $relations |
|
535 | + * @param string $sortBy |
|
536 | + * @param boolean $desc |
|
537 | + * @param array $columns |
|
538 | + * @return collection |
|
539 | + */ |
|
540 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
541 | + { |
|
542 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
543 | + $sort = $desc ? 'desc' : 'asc'; |
|
544 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
545 | + } |
|
546 | + |
|
547 | + /** |
|
548 | + * Fetch the first record from the storage based on the given |
|
549 | + * condition. |
|
550 | + * |
|
551 | + * @param array $conditions array of conditions |
|
552 | + * @param array $relations |
|
553 | + * @param array $columns |
|
554 | + * @return object |
|
555 | + */ |
|
556 | + public function first($conditions, $relations = [], $columns = array('*')) |
|
557 | + { |
|
558 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
559 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
560 | + } |
|
561 | + |
|
562 | + /** |
|
563 | + * Return the deleted models in pages based on the given conditions. |
|
564 | + * |
|
565 | + * @param array $conditions array of conditions |
|
566 | + * @param integer $perPage |
|
567 | + * @param string $sortBy |
|
568 | + * @param boolean $desc |
|
569 | + * @param array $columns |
|
570 | + * @return collection |
|
571 | + */ |
|
572 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
573 | + { |
|
574 | + unset($conditions['page']); |
|
575 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
576 | + $sort = $desc ? 'desc' : 'asc'; |
|
577 | + $model = $this->model->onlyTrashed(); |
|
578 | + |
|
579 | + if (count($conditions['conditionValues'])) |
|
580 | + { |
|
581 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
582 | + } |
|
583 | + |
|
584 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);; |
|
585 | + } |
|
586 | + |
|
587 | + /** |
|
588 | + * Restore the deleted model. |
|
589 | + * |
|
590 | + * @param integer $id |
|
591 | + * @return void |
|
592 | + */ |
|
593 | + public function restore($id) |
|
594 | + { |
|
595 | + $model = $this->model->onlyTrashed()->find($id); |
|
596 | + |
|
597 | + if ( ! $model) |
|
598 | + { |
|
599 | + \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id); |
|
600 | + } |
|
601 | + |
|
602 | + $model->restore(); |
|
603 | + } |
|
604 | + |
|
605 | + /** |
|
606 | + * Build the conditions recursively for the retrieving methods. |
|
607 | + * @param array $conditions |
|
608 | + * @return array |
|
609 | + */ |
|
610 | + protected function constructConditions($conditions, $model) |
|
611 | + { |
|
612 | + $conditionString = ''; |
|
613 | + $conditionValues = []; |
|
614 | + foreach ($conditions as $key => $value) |
|
615 | + { |
|
616 | + if (str_contains($key, '->')) |
|
617 | + { |
|
618 | + $key = $this->wrapJsonSelector($key); |
|
619 | + } |
|
620 | + |
|
621 | + if ($key == 'and') |
|
622 | + { |
|
623 | + $conditions = $this->constructConditions($value, $model); |
|
624 | + $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
|
625 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
626 | + } |
|
627 | + else if ($key == 'or') |
|
628 | + { |
|
629 | + $conditions = $this->constructConditions($value, $model); |
|
630 | + $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
|
631 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
632 | + } |
|
633 | + else |
|
634 | + { |
|
635 | + if (is_array($value)) |
|
636 | + { |
|
637 | + $operator = $value['op']; |
|
638 | + if (strtolower($operator) == 'between') |
|
639 | + { |
|
640 | + $value1 = $value['val1']; |
|
641 | + $value2 = $value['val2']; |
|
642 | + } |
|
643 | + else |
|
644 | + { |
|
645 | + $value = array_key_exists('val', $value) ? $value['val'] : ''; |
|
646 | + } |
|
647 | + } |
|
648 | + else |
|
649 | + { |
|
650 | + $operator = '='; |
|
651 | + } |
|
652 | 652 | |
653 | - if (strtolower($operator) == 'between') |
|
654 | - { |
|
655 | - $conditionString .= $key . ' >= ? and '; |
|
656 | - $conditionValues[] = $value1; |
|
657 | - |
|
658 | - $conditionString .= $key . ' <= ? {op} '; |
|
659 | - $conditionValues[] = $value2; |
|
660 | - } |
|
661 | - elseif (strtolower($operator) == 'in') |
|
662 | - { |
|
663 | - $conditionValues = array_merge($conditionValues, $value); |
|
664 | - $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
665 | - $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
|
666 | - } |
|
667 | - elseif (strtolower($operator) == 'null') |
|
668 | - { |
|
669 | - $conditionString .= $key . ' is null {op} '; |
|
670 | - } |
|
671 | - elseif (strtolower($operator) == 'not null') |
|
672 | - { |
|
673 | - $conditionString .= $key . ' is not null {op} '; |
|
674 | - } |
|
675 | - elseif (strtolower($operator) == 'has') |
|
676 | - { |
|
677 | - $sql = $model->withTrashed()->has($key)->toSql(); |
|
678 | - $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
679 | - $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
|
680 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
681 | - } |
|
682 | - else |
|
683 | - { |
|
684 | - $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
|
685 | - $conditionValues[] = $value; |
|
686 | - } |
|
687 | - } |
|
688 | - } |
|
689 | - $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
690 | - return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
691 | - } |
|
692 | - |
|
693 | - /** |
|
694 | - * Wrap the given JSON selector. |
|
695 | - * |
|
696 | - * @param string $value |
|
697 | - * @return string |
|
698 | - */ |
|
699 | - protected function wrapJsonSelector($value) |
|
700 | - { |
|
701 | - $removeLast = strpos($value, ')'); |
|
702 | - $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
703 | - $path = explode('->', $value); |
|
704 | - $field = array_shift($path); |
|
705 | - $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
706 | - return '"'.$part.'"'; |
|
707 | - })->implode('.')); |
|
653 | + if (strtolower($operator) == 'between') |
|
654 | + { |
|
655 | + $conditionString .= $key . ' >= ? and '; |
|
656 | + $conditionValues[] = $value1; |
|
657 | + |
|
658 | + $conditionString .= $key . ' <= ? {op} '; |
|
659 | + $conditionValues[] = $value2; |
|
660 | + } |
|
661 | + elseif (strtolower($operator) == 'in') |
|
662 | + { |
|
663 | + $conditionValues = array_merge($conditionValues, $value); |
|
664 | + $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
665 | + $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
|
666 | + } |
|
667 | + elseif (strtolower($operator) == 'null') |
|
668 | + { |
|
669 | + $conditionString .= $key . ' is null {op} '; |
|
670 | + } |
|
671 | + elseif (strtolower($operator) == 'not null') |
|
672 | + { |
|
673 | + $conditionString .= $key . ' is not null {op} '; |
|
674 | + } |
|
675 | + elseif (strtolower($operator) == 'has') |
|
676 | + { |
|
677 | + $sql = $model->withTrashed()->has($key)->toSql(); |
|
678 | + $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
679 | + $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
|
680 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
681 | + } |
|
682 | + else |
|
683 | + { |
|
684 | + $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
|
685 | + $conditionValues[] = $value; |
|
686 | + } |
|
687 | + } |
|
688 | + } |
|
689 | + $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
690 | + return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
691 | + } |
|
692 | + |
|
693 | + /** |
|
694 | + * Wrap the given JSON selector. |
|
695 | + * |
|
696 | + * @param string $value |
|
697 | + * @return string |
|
698 | + */ |
|
699 | + protected function wrapJsonSelector($value) |
|
700 | + { |
|
701 | + $removeLast = strpos($value, ')'); |
|
702 | + $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
703 | + $path = explode('->', $value); |
|
704 | + $field = array_shift($path); |
|
705 | + $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
706 | + return '"'.$part.'"'; |
|
707 | + })->implode('.')); |
|
708 | 708 | |
709 | - return $removeLast === false ? $result : $result . ')'; |
|
710 | - } |
|
711 | - |
|
712 | - /** |
|
713 | - * Abstract method that return the necessary |
|
714 | - * information (full model namespace) |
|
715 | - * needed to preform the previous actions. |
|
716 | - * |
|
717 | - * @return string |
|
718 | - */ |
|
719 | - abstract protected function getModel(); |
|
709 | + return $removeLast === false ? $result : $result . ')'; |
|
710 | + } |
|
711 | + |
|
712 | + /** |
|
713 | + * Abstract method that return the necessary |
|
714 | + * information (full model namespace) |
|
715 | + * needed to preform the previous actions. |
|
716 | + * |
|
717 | + * @return string |
|
718 | + */ |
|
719 | + abstract protected function getModel(); |
|
720 | 720 | } |
721 | 721 | \ No newline at end of file |
@@ -2,63 +2,63 @@ |
||
2 | 2 | |
3 | 3 | class Media |
4 | 4 | { |
5 | - /** |
|
6 | - * Upload the given image. |
|
7 | - * |
|
8 | - * @param object $image |
|
9 | - * @param string $dir |
|
10 | - * @return string |
|
11 | - */ |
|
12 | - public function uploadImage($image, $dir) |
|
13 | - { |
|
14 | - $imageName = str_slug('image' . uniqid() . time() . '_' . $image->getClientOriginalName()); |
|
15 | - $destinationPath = 'media' . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR; |
|
5 | + /** |
|
6 | + * Upload the given image. |
|
7 | + * |
|
8 | + * @param object $image |
|
9 | + * @param string $dir |
|
10 | + * @return string |
|
11 | + */ |
|
12 | + public function uploadImage($image, $dir) |
|
13 | + { |
|
14 | + $imageName = str_slug('image' . uniqid() . time() . '_' . $image->getClientOriginalName()); |
|
15 | + $destinationPath = 'media' . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR; |
|
16 | 16 | |
17 | - ! file_exists($destinationPath) ? \File::makeDirectory($destinationPath) : false; |
|
18 | - $image->move($destinationPath, $imageName); |
|
17 | + ! file_exists($destinationPath) ? \File::makeDirectory($destinationPath) : false; |
|
18 | + $image->move($destinationPath, $imageName); |
|
19 | 19 | |
20 | - return url($destinationPath . $imageName); |
|
21 | - } |
|
20 | + return url($destinationPath . $imageName); |
|
21 | + } |
|
22 | 22 | |
23 | - /** |
|
24 | - * Upload the given image. |
|
25 | - * |
|
26 | - * @param object $image |
|
27 | - * @param string $dir |
|
28 | - * @return string |
|
29 | - */ |
|
30 | - public function uploadImageBas64($image, $dir) |
|
31 | - { |
|
32 | - if (! strlen($image)) |
|
33 | - { |
|
34 | - return null; |
|
35 | - } |
|
23 | + /** |
|
24 | + * Upload the given image. |
|
25 | + * |
|
26 | + * @param object $image |
|
27 | + * @param string $dir |
|
28 | + * @return string |
|
29 | + */ |
|
30 | + public function uploadImageBas64($image, $dir) |
|
31 | + { |
|
32 | + if (! strlen($image)) |
|
33 | + { |
|
34 | + return null; |
|
35 | + } |
|
36 | 36 | |
37 | - $imageName = 'image' . uniqid() . time() . '.jpg'; |
|
38 | - $destinationPath = 'media' . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR; |
|
37 | + $imageName = 'image' . uniqid() . time() . '.jpg'; |
|
38 | + $destinationPath = 'media' . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR; |
|
39 | 39 | |
40 | - ! file_exists($destinationPath) ? \File::makeDirectory($destinationPath) : false; |
|
40 | + ! file_exists($destinationPath) ? \File::makeDirectory($destinationPath) : false; |
|
41 | 41 | |
42 | - $base = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $image)); |
|
43 | - \Image::make($base)->save($destinationPath . $imageName); |
|
42 | + $base = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $image)); |
|
43 | + \Image::make($base)->save($destinationPath . $imageName); |
|
44 | 44 | |
45 | - return url($destinationPath . $imageName); |
|
46 | - } |
|
45 | + return url($destinationPath . $imageName); |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Delete the given image. |
|
50 | - * |
|
51 | - * @param object $path |
|
52 | - * @param string $dir |
|
53 | - * @return void |
|
54 | - */ |
|
55 | - public function deleteImage($path, $dir) |
|
56 | - { |
|
57 | - $arr = explode('/', $path); |
|
58 | - $path = 'media' . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR . end($arr); |
|
59 | - if (\File::exists($path)) |
|
60 | - { |
|
61 | - \File::delete($path); |
|
62 | - } |
|
63 | - } |
|
48 | + /** |
|
49 | + * Delete the given image. |
|
50 | + * |
|
51 | + * @param object $path |
|
52 | + * @param string $dir |
|
53 | + * @return void |
|
54 | + */ |
|
55 | + public function deleteImage($path, $dir) |
|
56 | + { |
|
57 | + $arr = explode('/', $path); |
|
58 | + $path = 'media' . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR . end($arr); |
|
59 | + if (\File::exists($path)) |
|
60 | + { |
|
61 | + \File::delete($path); |
|
62 | + } |
|
63 | + } |
|
64 | 64 | } |
@@ -2,50 +2,50 @@ |
||
2 | 2 | |
3 | 3 | trait Translatable |
4 | 4 | { |
5 | - /** |
|
6 | - * Create a new model instance that is existing. |
|
7 | - * |
|
8 | - * @param array $attributes |
|
9 | - * @param string|null $connection |
|
10 | - * @return static |
|
11 | - */ |
|
12 | - public function newFromBuilder($attributes = [], $connection = null) |
|
13 | - { |
|
14 | - $model = parent::newFromBuilder($attributes, $connection); |
|
5 | + /** |
|
6 | + * Create a new model instance that is existing. |
|
7 | + * |
|
8 | + * @param array $attributes |
|
9 | + * @param string|null $connection |
|
10 | + * @return static |
|
11 | + */ |
|
12 | + public function newFromBuilder($attributes = [], $connection = null) |
|
13 | + { |
|
14 | + $model = parent::newFromBuilder($attributes, $connection); |
|
15 | 15 | |
16 | - foreach ($model->attributes AS $key => $value) |
|
17 | - { |
|
18 | - if (isset($this->translatable) && in_array($key, $this->translatable)) |
|
19 | - { |
|
20 | - $model->$key = $this->getTranslatedAttribute($value); |
|
21 | - } |
|
22 | - } |
|
16 | + foreach ($model->attributes AS $key => $value) |
|
17 | + { |
|
18 | + if (isset($this->translatable) && in_array($key, $this->translatable)) |
|
19 | + { |
|
20 | + $model->$key = $this->getTranslatedAttribute($value); |
|
21 | + } |
|
22 | + } |
|
23 | 23 | |
24 | - return $model; |
|
25 | - } |
|
24 | + return $model; |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * Returns a translatable model attribute based on the application's locale settings. |
|
29 | - * |
|
30 | - * @param $values |
|
31 | - * @return string |
|
32 | - */ |
|
33 | - protected function getTranslatedAttribute($values) |
|
34 | - { |
|
35 | - $values = json_decode($values); |
|
36 | - $primaryLocale = \Session::get('locale'); |
|
37 | - $fallbackLocale = 'en'; |
|
27 | + /** |
|
28 | + * Returns a translatable model attribute based on the application's locale settings. |
|
29 | + * |
|
30 | + * @param $values |
|
31 | + * @return string |
|
32 | + */ |
|
33 | + protected function getTranslatedAttribute($values) |
|
34 | + { |
|
35 | + $values = json_decode($values); |
|
36 | + $primaryLocale = \Session::get('locale'); |
|
37 | + $fallbackLocale = 'en'; |
|
38 | 38 | |
39 | - if ($primaryLocale == 'all') |
|
40 | - { |
|
41 | - return $values; |
|
42 | - } |
|
39 | + if ($primaryLocale == 'all') |
|
40 | + { |
|
41 | + return $values; |
|
42 | + } |
|
43 | 43 | |
44 | - if ( ! $primaryLocale || ! is_object($values) || ! property_exists($values, $primaryLocale)) |
|
45 | - { |
|
46 | - return $values ? isset($values->$fallbackLocale) ? $values->$fallbackLocale : $values : ''; |
|
47 | - } |
|
44 | + if ( ! $primaryLocale || ! is_object($values) || ! property_exists($values, $primaryLocale)) |
|
45 | + { |
|
46 | + return $values ? isset($values->$fallbackLocale) ? $values->$fallbackLocale : $values : ''; |
|
47 | + } |
|
48 | 48 | |
49 | - return $primaryLocale == 'all' ? $values : $values->$primaryLocale; |
|
50 | - } |
|
49 | + return $primaryLocale == 'all' ? $values : $values->$primaryLocale; |
|
50 | + } |
|
51 | 51 | } |
52 | 52 | \ No newline at end of file |
@@ -7,50 +7,50 @@ |
||
7 | 7 | class ModuleServiceProvider extends ServiceProvider |
8 | 8 | { |
9 | 9 | /** |
10 | - * Bootstrap the module services. |
|
11 | - * |
|
12 | - * @return void |
|
13 | - */ |
|
14 | - public function boot() |
|
15 | - { |
|
16 | - $this->loadTranslationsFrom(__DIR__.'/../Resources/Lang', 'core'); |
|
17 | - $this->loadViewsFrom(__DIR__.'/../Resources/Views', 'core'); |
|
18 | - |
|
19 | - $factory = app('Illuminate\Database\Eloquent\Factory'); |
|
20 | - $factory->load(__DIR__.'/../Database/Factories'); |
|
21 | - } |
|
22 | - |
|
23 | - /** |
|
24 | - * Register the module services. |
|
25 | - * |
|
26 | - * @return void |
|
27 | - */ |
|
28 | - public function register() |
|
29 | - { |
|
30 | - //Bind Core Facade to the IoC Container |
|
31 | - \App::bind('Core', function() |
|
32 | - { |
|
33 | - return new \App\Modules\Core\Core; |
|
34 | - }); |
|
35 | - |
|
36 | - //Bind ErrorHandler Facade to the IoC Container |
|
37 | - \App::bind('ErrorHandler', function() |
|
38 | - { |
|
39 | - return new \App\Modules\Core\Utl\ErrorHandler; |
|
40 | - }); |
|
41 | - |
|
42 | - //Bind CoreConfig Facade to the IoC Container |
|
43 | - \App::bind('CoreConfig', function() |
|
44 | - { |
|
45 | - return new \App\Modules\Core\Utl\CoreConfig; |
|
46 | - }); |
|
47 | - |
|
48 | - //Bind Mpgs Facade to the IoC Container |
|
49 | - \App::bind('Media', function() |
|
50 | - { |
|
51 | - return new \App\Modules\Core\Utl\Media; |
|
52 | - }); |
|
10 | + * Bootstrap the module services. |
|
11 | + * |
|
12 | + * @return void |
|
13 | + */ |
|
14 | + public function boot() |
|
15 | + { |
|
16 | + $this->loadTranslationsFrom(__DIR__.'/../Resources/Lang', 'core'); |
|
17 | + $this->loadViewsFrom(__DIR__.'/../Resources/Views', 'core'); |
|
18 | + |
|
19 | + $factory = app('Illuminate\Database\Eloquent\Factory'); |
|
20 | + $factory->load(__DIR__.'/../Database/Factories'); |
|
21 | + } |
|
22 | + |
|
23 | + /** |
|
24 | + * Register the module services. |
|
25 | + * |
|
26 | + * @return void |
|
27 | + */ |
|
28 | + public function register() |
|
29 | + { |
|
30 | + //Bind Core Facade to the IoC Container |
|
31 | + \App::bind('Core', function() |
|
32 | + { |
|
33 | + return new \App\Modules\Core\Core; |
|
34 | + }); |
|
35 | + |
|
36 | + //Bind ErrorHandler Facade to the IoC Container |
|
37 | + \App::bind('ErrorHandler', function() |
|
38 | + { |
|
39 | + return new \App\Modules\Core\Utl\ErrorHandler; |
|
40 | + }); |
|
41 | + |
|
42 | + //Bind CoreConfig Facade to the IoC Container |
|
43 | + \App::bind('CoreConfig', function() |
|
44 | + { |
|
45 | + return new \App\Modules\Core\Utl\CoreConfig; |
|
46 | + }); |
|
47 | + |
|
48 | + //Bind Mpgs Facade to the IoC Container |
|
49 | + \App::bind('Media', function() |
|
50 | + { |
|
51 | + return new \App\Modules\Core\Utl\Media; |
|
52 | + }); |
|
53 | 53 | |
54 | - $this->app->register(RouteServiceProvider::class); |
|
55 | - } |
|
54 | + $this->app->register(RouteServiceProvider::class); |
|
55 | + } |
|
56 | 56 | } |
@@ -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)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
19 | - } |
|
17 | + { |
|
18 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
19 | + } |
|
20 | 20 | |
21 | - public function getUpdatedAtAttribute($value) |
|
22 | - { |
|
23 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
24 | - } |
|
21 | + public function getUpdatedAtAttribute($value) |
|
22 | + { |
|
23 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
24 | + } |
|
25 | 25 | |
26 | - public function getDeletedAtAttribute($value) |
|
27 | - { |
|
28 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
29 | - } |
|
26 | + public function getDeletedAtAttribute($value) |
|
27 | + { |
|
28 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
29 | + } |
|
30 | 30 | |
31 | - public static function boot() |
|
32 | - { |
|
33 | - parent::boot(); |
|
34 | - Report::observe(\App::make('App\Modules\Reporting\ModelObservers\ReprotObserver')); |
|
35 | - } |
|
31 | + public static function boot() |
|
32 | + { |
|
33 | + parent::boot(); |
|
34 | + Report::observe(\App::make('App\Modules\Reporting\ModelObservers\ReprotObserver')); |
|
35 | + } |
|
36 | 36 | } |
@@ -5,37 +5,37 @@ |
||
5 | 5 | |
6 | 6 | class Settings extends Model{ |
7 | 7 | |
8 | - use SoftDeletes; |
|
9 | - protected $table = 'settings'; |
|
10 | - protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
11 | - protected $hidden = ['deleted_at']; |
|
12 | - protected $guarded = ['id', 'key']; |
|
13 | - protected $fillable = ['name','value']; |
|
14 | - public $searchable = ['name', 'value', 'key']; |
|
8 | + use SoftDeletes; |
|
9 | + protected $table = 'settings'; |
|
10 | + protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
11 | + protected $hidden = ['deleted_at']; |
|
12 | + protected $guarded = ['id', 'key']; |
|
13 | + protected $fillable = ['name','value']; |
|
14 | + public $searchable = ['name', 'value', 'key']; |
|
15 | 15 | |
16 | - public function getCreatedAtAttribute($value) |
|
17 | - { |
|
18 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
19 | - } |
|
16 | + public function getCreatedAtAttribute($value) |
|
17 | + { |
|
18 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
19 | + } |
|
20 | 20 | |
21 | - public function getUpdatedAtAttribute($value) |
|
22 | - { |
|
23 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
24 | - } |
|
21 | + public function getUpdatedAtAttribute($value) |
|
22 | + { |
|
23 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
24 | + } |
|
25 | 25 | |
26 | - public function getDeletedAtAttribute($value) |
|
27 | - { |
|
28 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
29 | - } |
|
26 | + public function getDeletedAtAttribute($value) |
|
27 | + { |
|
28 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
29 | + } |
|
30 | 30 | |
31 | - public function newCollection(array $models = []) |
|
32 | - { |
|
33 | - return parent::newCollection($models)->keyBy('key'); |
|
34 | - } |
|
31 | + public function newCollection(array $models = []) |
|
32 | + { |
|
33 | + return parent::newCollection($models)->keyBy('key'); |
|
34 | + } |
|
35 | 35 | |
36 | - public static function boot() |
|
37 | - { |
|
38 | - parent::boot(); |
|
39 | - Settings::observe(\App::make('App\Modules\Core\ModelObservers\SettingsObserver')); |
|
40 | - } |
|
36 | + public static function boot() |
|
37 | + { |
|
38 | + parent::boot(); |
|
39 | + Settings::observe(\App::make('App\Modules\Core\ModelObservers\SettingsObserver')); |
|
40 | + } |
|
41 | 41 | } |
@@ -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)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
19 | - } |
|
16 | + public function getCreatedAtAttribute($value) |
|
17 | + { |
|
18 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
19 | + } |
|
20 | 20 | |
21 | - public function getUpdatedAtAttribute($value) |
|
22 | - { |
|
23 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
24 | - } |
|
21 | + public function getUpdatedAtAttribute($value) |
|
22 | + { |
|
23 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
24 | + } |
|
25 | 25 | |
26 | - public function getDeletedAtAttribute($value) |
|
27 | - { |
|
28 | - return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
29 | - } |
|
26 | + public function getDeletedAtAttribute($value) |
|
27 | + { |
|
28 | + return \Carbon\Carbon::parse($value)->tz(\Session::get('time-zone'))->toDateTimeString(); |
|
29 | + } |
|
30 | 30 | |
31 | - public function groups() |
|
32 | - { |
|
33 | - return $this->belongsToMany('\App\Modules\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\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 | - AclPermission::observe(\App::make('App\Modules\Acl\ModelObservers\AclPermissionObserver')); |
|
40 | - } |
|
36 | + public static function boot() |
|
37 | + { |
|
38 | + parent::boot(); |
|
39 | + AclPermission::observe(\App::make('App\Modules\Acl\ModelObservers\AclPermissionObserver')); |
|
40 | + } |
|
41 | 41 | } |