@@ -61,8 +61,8 @@ discard block |
||
| 61 | 61 | /** |
| 62 | 62 | * Check if the logged in user has the given group. |
| 63 | 63 | * |
| 64 | - * @param string $groupName |
|
| 65 | - * @param integer $userId |
|
| 64 | + * @param integer $user |
|
| 65 | + * @param string[] $groups |
|
| 66 | 66 | * @return boolean |
| 67 | 67 | */ |
| 68 | 68 | public function hasGroup($groups, $user = false) |
@@ -244,7 +244,7 @@ discard block |
||
| 244 | 244 | * Reset the given user's password. |
| 245 | 245 | * |
| 246 | 246 | * @param array $credentials |
| 247 | - * @return array |
|
| 247 | + * @return string|null |
|
| 248 | 248 | */ |
| 249 | 249 | public function resetPassword($credentials) |
| 250 | 250 | { |
@@ -361,7 +361,6 @@ discard block |
||
| 361 | 361 | /** |
| 362 | 362 | * Save the given data to the logged in user. |
| 363 | 363 | * |
| 364 | - * @param array $credentials |
|
| 365 | 364 | * @return void |
| 366 | 365 | */ |
| 367 | 366 | public function saveProfile($data) |
@@ -5,394 +5,394 @@ |
||
| 5 | 5 | |
| 6 | 6 | class UserRepository extends AbstractRepository |
| 7 | 7 | { |
| 8 | - /** |
|
| 9 | - * Return the model full namespace. |
|
| 10 | - * |
|
| 11 | - * @return string |
|
| 12 | - */ |
|
| 13 | - protected function getModel() |
|
| 14 | - { |
|
| 15 | - return 'App\Modules\V1\Acl\AclUser'; |
|
| 16 | - } |
|
| 17 | - |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * Return the logged in user account. |
|
| 21 | - * |
|
| 22 | - * @param array $relations |
|
| 23 | - * @return boolean |
|
| 24 | - */ |
|
| 25 | - public function account($relations = []) |
|
| 26 | - { |
|
| 27 | - $permissions = []; |
|
| 28 | - $user = \Core::users()->find(\Auth::id(), $relations); |
|
| 29 | - foreach ($user->groups()->get() as $group) |
|
| 30 | - { |
|
| 31 | - $group->permissions->each(function ($permission) use (&$permissions){ |
|
| 32 | - $permissions[$permission->model][$permission->id] = $permission->name; |
|
| 33 | - }); |
|
| 34 | - } |
|
| 35 | - $user->permissions = $permissions; |
|
| 36 | - |
|
| 37 | - return $user; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Check if the logged in user or the given user |
|
| 42 | - * has the given permissions on the given model. |
|
| 43 | - * |
|
| 44 | - * @param string $nameOfPermission |
|
| 45 | - * @param string $model |
|
| 46 | - * @param boolean $user |
|
| 47 | - * @return boolean |
|
| 48 | - */ |
|
| 49 | - public function can($nameOfPermission, $model, $user = false) |
|
| 50 | - { |
|
| 51 | - $user = $user ?: $this->find(\Auth::id(), ['groups.permissions']); |
|
| 52 | - $permissions = []; |
|
| 53 | - |
|
| 54 | - $user->groups->pluck('permissions')->each(function ($permission) use (&$permissions, $model){ |
|
| 55 | - $permissions = array_merge($permissions, $permission->where('model', $model)->pluck('name')->toArray()); |
|
| 56 | - }); |
|
| 8 | + /** |
|
| 9 | + * Return the model full namespace. |
|
| 10 | + * |
|
| 11 | + * @return string |
|
| 12 | + */ |
|
| 13 | + protected function getModel() |
|
| 14 | + { |
|
| 15 | + return 'App\Modules\V1\Acl\AclUser'; |
|
| 16 | + } |
|
| 17 | + |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * Return the logged in user account. |
|
| 21 | + * |
|
| 22 | + * @param array $relations |
|
| 23 | + * @return boolean |
|
| 24 | + */ |
|
| 25 | + public function account($relations = []) |
|
| 26 | + { |
|
| 27 | + $permissions = []; |
|
| 28 | + $user = \Core::users()->find(\Auth::id(), $relations); |
|
| 29 | + foreach ($user->groups()->get() as $group) |
|
| 30 | + { |
|
| 31 | + $group->permissions->each(function ($permission) use (&$permissions){ |
|
| 32 | + $permissions[$permission->model][$permission->id] = $permission->name; |
|
| 33 | + }); |
|
| 34 | + } |
|
| 35 | + $user->permissions = $permissions; |
|
| 36 | + |
|
| 37 | + return $user; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Check if the logged in user or the given user |
|
| 42 | + * has the given permissions on the given model. |
|
| 43 | + * |
|
| 44 | + * @param string $nameOfPermission |
|
| 45 | + * @param string $model |
|
| 46 | + * @param boolean $user |
|
| 47 | + * @return boolean |
|
| 48 | + */ |
|
| 49 | + public function can($nameOfPermission, $model, $user = false) |
|
| 50 | + { |
|
| 51 | + $user = $user ?: $this->find(\Auth::id(), ['groups.permissions']); |
|
| 52 | + $permissions = []; |
|
| 53 | + |
|
| 54 | + $user->groups->pluck('permissions')->each(function ($permission) use (&$permissions, $model){ |
|
| 55 | + $permissions = array_merge($permissions, $permission->where('model', $model)->pluck('name')->toArray()); |
|
| 56 | + }); |
|
| 57 | 57 | |
| 58 | - return in_array($nameOfPermission, $permissions); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Check if the logged in user has the given group. |
|
| 63 | - * |
|
| 64 | - * @param string $groupName |
|
| 65 | - * @param integer $userId |
|
| 66 | - * @return boolean |
|
| 67 | - */ |
|
| 68 | - public function hasGroup($groups, $user = false) |
|
| 69 | - { |
|
| 70 | - $user = $user ?: $this->find(\Auth::id()); |
|
| 71 | - return $user->groups->whereIn('name', $groups)->count() ? true : false; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Assign the given group ids to the given user. |
|
| 76 | - * |
|
| 77 | - * @param integer $user_id |
|
| 78 | - * @param array $group_ids |
|
| 79 | - * @return object |
|
| 80 | - */ |
|
| 81 | - public function assignGroups($user_id, $group_ids) |
|
| 82 | - { |
|
| 83 | - \DB::transaction(function () use ($user_id, $group_ids) { |
|
| 84 | - $user = $this->find($user_id); |
|
| 85 | - $user->groups()->detach(); |
|
| 86 | - $user->groups()->attach($group_ids); |
|
| 87 | - }); |
|
| 88 | - |
|
| 89 | - return $this->find($user_id); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Handle a login request to the application. |
|
| 95 | - * |
|
| 96 | - * @param array $credentials |
|
| 97 | - * @param boolean $adminLogin |
|
| 98 | - * @return object |
|
| 99 | - */ |
|
| 100 | - public function login($credentials, $adminLogin = false) |
|
| 101 | - { |
|
| 102 | - if ( ! $user = $this->first(['email' => $credentials['email']])) |
|
| 103 | - { |
|
| 104 | - \ErrorHandler::loginFailed(); |
|
| 105 | - } |
|
| 106 | - else if ($adminLogin && ! $user->groups->whereIn('name', ['Admin'])->count()) |
|
| 107 | - { |
|
| 108 | - \ErrorHandler::loginFailed(); |
|
| 109 | - } |
|
| 110 | - else if ( ! $adminLogin && $user->groups->whereIn('name', ['Admin'])->count()) |
|
| 111 | - { |
|
| 112 | - \ErrorHandler::loginFailed(); |
|
| 113 | - } |
|
| 114 | - else if ($user->blocked) |
|
| 115 | - { |
|
| 116 | - \ErrorHandler::userIsBlocked(); |
|
| 117 | - } |
|
| 118 | - else if ( ! $user->confirmed) |
|
| 119 | - { |
|
| 120 | - \ErrorHandler::emailNotConfirmed(); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - return $user; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Handle a social login request of the none admin to the application. |
|
| 128 | - * |
|
| 129 | - * @param array $credentials |
|
| 130 | - * @return array |
|
| 131 | - */ |
|
| 132 | - public function loginSocial($credentials) |
|
| 133 | - { |
|
| 134 | - $access_token = $credentials['auth_code'] ? \Socialite::driver($credentials['type'])->getAccessToken($credentials['auth_code']) : $credentials['access_token']; |
|
| 135 | - $user = \Socialite::driver($credentials['type'])->userFromToken($access_token); |
|
| 136 | - |
|
| 137 | - if ( ! $user->email) |
|
| 138 | - { |
|
| 139 | - \ErrorHandler::noSocialEmail(); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - if ( ! $registeredUser = $this->model->where('email', $user->email)->first()) |
|
| 143 | - { |
|
| 144 | - $data = ['email' => $user->email, 'password' => '']; |
|
| 145 | - return $this->register($data); |
|
| 146 | - } |
|
| 147 | - else |
|
| 148 | - { |
|
| 149 | - if ( ! \Auth::attempt(['email' => $registeredUser->email, 'password' => ''])) |
|
| 150 | - { |
|
| 151 | - \ErrorHandler::userAlreadyRegistered(); |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - $loginProxy = \App::make('App\Modules\V1\Acl\Proxy\LoginProxy'); |
|
| 155 | - return $loginProxy->login(['email' => $registeredUser->email, 'password' => ''], 0); |
|
| 156 | - } |
|
| 157 | - } |
|
| 58 | + return in_array($nameOfPermission, $permissions); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Check if the logged in user has the given group. |
|
| 63 | + * |
|
| 64 | + * @param string $groupName |
|
| 65 | + * @param integer $userId |
|
| 66 | + * @return boolean |
|
| 67 | + */ |
|
| 68 | + public function hasGroup($groups, $user = false) |
|
| 69 | + { |
|
| 70 | + $user = $user ?: $this->find(\Auth::id()); |
|
| 71 | + return $user->groups->whereIn('name', $groups)->count() ? true : false; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Assign the given group ids to the given user. |
|
| 76 | + * |
|
| 77 | + * @param integer $user_id |
|
| 78 | + * @param array $group_ids |
|
| 79 | + * @return object |
|
| 80 | + */ |
|
| 81 | + public function assignGroups($user_id, $group_ids) |
|
| 82 | + { |
|
| 83 | + \DB::transaction(function () use ($user_id, $group_ids) { |
|
| 84 | + $user = $this->find($user_id); |
|
| 85 | + $user->groups()->detach(); |
|
| 86 | + $user->groups()->attach($group_ids); |
|
| 87 | + }); |
|
| 88 | + |
|
| 89 | + return $this->find($user_id); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Handle a login request to the application. |
|
| 95 | + * |
|
| 96 | + * @param array $credentials |
|
| 97 | + * @param boolean $adminLogin |
|
| 98 | + * @return object |
|
| 99 | + */ |
|
| 100 | + public function login($credentials, $adminLogin = false) |
|
| 101 | + { |
|
| 102 | + if ( ! $user = $this->first(['email' => $credentials['email']])) |
|
| 103 | + { |
|
| 104 | + \ErrorHandler::loginFailed(); |
|
| 105 | + } |
|
| 106 | + else if ($adminLogin && ! $user->groups->whereIn('name', ['Admin'])->count()) |
|
| 107 | + { |
|
| 108 | + \ErrorHandler::loginFailed(); |
|
| 109 | + } |
|
| 110 | + else if ( ! $adminLogin && $user->groups->whereIn('name', ['Admin'])->count()) |
|
| 111 | + { |
|
| 112 | + \ErrorHandler::loginFailed(); |
|
| 113 | + } |
|
| 114 | + else if ($user->blocked) |
|
| 115 | + { |
|
| 116 | + \ErrorHandler::userIsBlocked(); |
|
| 117 | + } |
|
| 118 | + else if ( ! $user->confirmed) |
|
| 119 | + { |
|
| 120 | + \ErrorHandler::emailNotConfirmed(); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + return $user; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Handle a social login request of the none admin to the application. |
|
| 128 | + * |
|
| 129 | + * @param array $credentials |
|
| 130 | + * @return array |
|
| 131 | + */ |
|
| 132 | + public function loginSocial($credentials) |
|
| 133 | + { |
|
| 134 | + $access_token = $credentials['auth_code'] ? \Socialite::driver($credentials['type'])->getAccessToken($credentials['auth_code']) : $credentials['access_token']; |
|
| 135 | + $user = \Socialite::driver($credentials['type'])->userFromToken($access_token); |
|
| 136 | + |
|
| 137 | + if ( ! $user->email) |
|
| 138 | + { |
|
| 139 | + \ErrorHandler::noSocialEmail(); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + if ( ! $registeredUser = $this->model->where('email', $user->email)->first()) |
|
| 143 | + { |
|
| 144 | + $data = ['email' => $user->email, 'password' => '']; |
|
| 145 | + return $this->register($data); |
|
| 146 | + } |
|
| 147 | + else |
|
| 148 | + { |
|
| 149 | + if ( ! \Auth::attempt(['email' => $registeredUser->email, 'password' => ''])) |
|
| 150 | + { |
|
| 151 | + \ErrorHandler::userAlreadyRegistered(); |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + $loginProxy = \App::make('App\Modules\V1\Acl\Proxy\LoginProxy'); |
|
| 155 | + return $loginProxy->login(['email' => $registeredUser->email, 'password' => ''], 0); |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | 158 | |
| 159 | - /** |
|
| 160 | - * Handle a registration request. |
|
| 161 | - * |
|
| 162 | - * @param array $credentials |
|
| 163 | - * @return array |
|
| 164 | - */ |
|
| 165 | - public function register($credentials) |
|
| 166 | - { |
|
| 167 | - $user = $this->model->create($credentials); |
|
| 168 | - |
|
| 169 | - if ( ! env('DISABLE_CONFIRM_EMAIL')) |
|
| 170 | - { |
|
| 171 | - $this->sendConfirmationEmail($user->email); |
|
| 172 | - } |
|
| 173 | - } |
|
| 159 | + /** |
|
| 160 | + * Handle a registration request. |
|
| 161 | + * |
|
| 162 | + * @param array $credentials |
|
| 163 | + * @return array |
|
| 164 | + */ |
|
| 165 | + public function register($credentials) |
|
| 166 | + { |
|
| 167 | + $user = $this->model->create($credentials); |
|
| 168 | + |
|
| 169 | + if ( ! env('DISABLE_CONFIRM_EMAIL')) |
|
| 170 | + { |
|
| 171 | + $this->sendConfirmationEmail($user->email); |
|
| 172 | + } |
|
| 173 | + } |
|
| 174 | 174 | |
| 175 | - /** |
|
| 176 | - * Block the user. |
|
| 177 | - * |
|
| 178 | - * @param integer $user_id |
|
| 179 | - * @return object |
|
| 180 | - */ |
|
| 181 | - public function block($user_id) |
|
| 182 | - { |
|
| 183 | - if ( ! $user = $this->find($user_id)) |
|
| 184 | - { |
|
| 185 | - \ErrorHandler::notFound('user'); |
|
| 186 | - } |
|
| 187 | - if ( ! $this->hasGroup(['Admin'])) |
|
| 188 | - { |
|
| 189 | - \ErrorHandler::noPermissions(); |
|
| 190 | - } |
|
| 191 | - else if (\Auth::id() == $user_id) |
|
| 192 | - { |
|
| 193 | - \ErrorHandler::noPermissions(); |
|
| 194 | - } |
|
| 195 | - else if ($user->groups->pluck('name')->search('Admin', true) !== false) |
|
| 196 | - { |
|
| 197 | - \ErrorHandler::noPermissions(); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - $user->blocked = 1; |
|
| 201 | - $user->save(); |
|
| 175 | + /** |
|
| 176 | + * Block the user. |
|
| 177 | + * |
|
| 178 | + * @param integer $user_id |
|
| 179 | + * @return object |
|
| 180 | + */ |
|
| 181 | + public function block($user_id) |
|
| 182 | + { |
|
| 183 | + if ( ! $user = $this->find($user_id)) |
|
| 184 | + { |
|
| 185 | + \ErrorHandler::notFound('user'); |
|
| 186 | + } |
|
| 187 | + if ( ! $this->hasGroup(['Admin'])) |
|
| 188 | + { |
|
| 189 | + \ErrorHandler::noPermissions(); |
|
| 190 | + } |
|
| 191 | + else if (\Auth::id() == $user_id) |
|
| 192 | + { |
|
| 193 | + \ErrorHandler::noPermissions(); |
|
| 194 | + } |
|
| 195 | + else if ($user->groups->pluck('name')->search('Admin', true) !== false) |
|
| 196 | + { |
|
| 197 | + \ErrorHandler::noPermissions(); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + $user->blocked = 1; |
|
| 201 | + $user->save(); |
|
| 202 | 202 | |
| 203 | - return $user; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * Unblock the user. |
|
| 208 | - * |
|
| 209 | - * @param integer $user_id |
|
| 210 | - * @return object |
|
| 211 | - */ |
|
| 212 | - public function unblock($user_id) |
|
| 213 | - { |
|
| 214 | - if ( ! $this->hasGroup(['Admin'])) |
|
| 215 | - { |
|
| 216 | - \ErrorHandler::noPermissions(); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - $user = $this->find($user_id); |
|
| 220 | - $user->blocked = 0; |
|
| 221 | - $user->save(); |
|
| 222 | - |
|
| 223 | - return $user; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * Send a reset link to the given user. |
|
| 228 | - * |
|
| 229 | - * @param string $email |
|
| 230 | - * @return void |
|
| 231 | - */ |
|
| 232 | - public function sendReset($email) |
|
| 233 | - { |
|
| 234 | - if ( ! $user = $this->model->where('email', $email)->first()) |
|
| 235 | - { |
|
| 236 | - \ErrorHandler::notFound('email'); |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - $token = \Password::getRepository()->create($user); |
|
| 240 | - \Core::notifications()->notify($user, 'ResetPassword', $token); |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * Reset the given user's password. |
|
| 245 | - * |
|
| 246 | - * @param array $credentials |
|
| 247 | - * @return array |
|
| 248 | - */ |
|
| 249 | - public function resetPassword($credentials) |
|
| 250 | - { |
|
| 251 | - $response = \Password::reset($credentials, function ($user, $password) { |
|
| 252 | - $user->password = $password; |
|
| 253 | - $user->save(); |
|
| 254 | - }); |
|
| 255 | - |
|
| 256 | - switch ($response) { |
|
| 257 | - case \Password::PASSWORD_RESET: |
|
| 258 | - return 'success'; |
|
| 203 | + return $user; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * Unblock the user. |
|
| 208 | + * |
|
| 209 | + * @param integer $user_id |
|
| 210 | + * @return object |
|
| 211 | + */ |
|
| 212 | + public function unblock($user_id) |
|
| 213 | + { |
|
| 214 | + if ( ! $this->hasGroup(['Admin'])) |
|
| 215 | + { |
|
| 216 | + \ErrorHandler::noPermissions(); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + $user = $this->find($user_id); |
|
| 220 | + $user->blocked = 0; |
|
| 221 | + $user->save(); |
|
| 222 | + |
|
| 223 | + return $user; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * Send a reset link to the given user. |
|
| 228 | + * |
|
| 229 | + * @param string $email |
|
| 230 | + * @return void |
|
| 231 | + */ |
|
| 232 | + public function sendReset($email) |
|
| 233 | + { |
|
| 234 | + if ( ! $user = $this->model->where('email', $email)->first()) |
|
| 235 | + { |
|
| 236 | + \ErrorHandler::notFound('email'); |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + $token = \Password::getRepository()->create($user); |
|
| 240 | + \Core::notifications()->notify($user, 'ResetPassword', $token); |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * Reset the given user's password. |
|
| 245 | + * |
|
| 246 | + * @param array $credentials |
|
| 247 | + * @return array |
|
| 248 | + */ |
|
| 249 | + public function resetPassword($credentials) |
|
| 250 | + { |
|
| 251 | + $response = \Password::reset($credentials, function ($user, $password) { |
|
| 252 | + $user->password = $password; |
|
| 253 | + $user->save(); |
|
| 254 | + }); |
|
| 255 | + |
|
| 256 | + switch ($response) { |
|
| 257 | + case \Password::PASSWORD_RESET: |
|
| 258 | + return 'success'; |
|
| 259 | 259 | |
| 260 | - case \Password::INVALID_TOKEN: |
|
| 261 | - \ErrorHandler::invalidResetToken('token'); |
|
| 262 | - |
|
| 263 | - case \Password::INVALID_PASSWORD: |
|
| 264 | - \ErrorHandler::invalidResetPassword('email'); |
|
| 265 | - |
|
| 266 | - case \Password::INVALID_USER: |
|
| 267 | - \ErrorHandler::notFound('user'); |
|
| 268 | - |
|
| 269 | - default: |
|
| 270 | - \ErrorHandler::generalError(); |
|
| 271 | - } |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * Change the logged in user password. |
|
| 276 | - * |
|
| 277 | - * @param array $credentials |
|
| 278 | - * @return void |
|
| 279 | - */ |
|
| 280 | - public function changePassword($credentials) |
|
| 281 | - { |
|
| 282 | - $user = \Auth::user(); |
|
| 283 | - if ( ! \Hash::check($credentials['old_password'], $user->password)) |
|
| 284 | - { |
|
| 285 | - \ErrorHandler::invalidOldPassword(); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - $user->password = $credentials['password']; |
|
| 289 | - $user->save(); |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - /** |
|
| 293 | - * Confirm email using the confirmation code. |
|
| 294 | - * |
|
| 295 | - * @param string $confirmationCode |
|
| 296 | - * @return void |
|
| 297 | - */ |
|
| 298 | - public function confirmEmail($confirmationCode) |
|
| 299 | - { |
|
| 300 | - $user = $this->first(['confirmation_code' => $confirmationCode]); |
|
| 301 | - $user->confirmed = 1; |
|
| 302 | - $user->confirmation_code = null; |
|
| 303 | - $user->save(); |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * Send the confirmation mail. |
|
| 308 | - * |
|
| 309 | - * @param string $email |
|
| 310 | - * @return void |
|
| 311 | - */ |
|
| 312 | - public function sendConfirmationEmail($email) |
|
| 313 | - { |
|
| 314 | - $user = $this->first(['email' => $email]); |
|
| 315 | - if ($user->confirmed) |
|
| 316 | - { |
|
| 317 | - \ErrorHandler::emailAlreadyConfirmed(); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - $user->confirmed = 0; |
|
| 321 | - $user->confirmation_code = sha1(microtime()); |
|
| 322 | - $user->save(); |
|
| 323 | - \Core::notifications()->notify($user, 'ConfirmEmail'); |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - /** |
|
| 327 | - * Paginate all users in the given group based on the given conditions. |
|
| 328 | - * |
|
| 329 | - * @param string $groupName |
|
| 330 | - * @param array $relations |
|
| 331 | - * @param integer $perPage |
|
| 332 | - * @param string $sortBy |
|
| 333 | - * @param boolean $desc |
|
| 334 | - * @return \Illuminate\Http\Response |
|
| 335 | - */ |
|
| 336 | - public function group($conditions, $groupName, $relations, $perPage, $sortBy, $desc) |
|
| 337 | - { |
|
| 338 | - unset($conditions['page']); |
|
| 339 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 340 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 341 | - $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
| 342 | - |
|
| 343 | - $model->whereHas('groups', function($q) use ($groupName){ |
|
| 344 | - $q->where('name', $groupName); |
|
| 345 | - }); |
|
| 260 | + case \Password::INVALID_TOKEN: |
|
| 261 | + \ErrorHandler::invalidResetToken('token'); |
|
| 262 | + |
|
| 263 | + case \Password::INVALID_PASSWORD: |
|
| 264 | + \ErrorHandler::invalidResetPassword('email'); |
|
| 265 | + |
|
| 266 | + case \Password::INVALID_USER: |
|
| 267 | + \ErrorHandler::notFound('user'); |
|
| 268 | + |
|
| 269 | + default: |
|
| 270 | + \ErrorHandler::generalError(); |
|
| 271 | + } |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * Change the logged in user password. |
|
| 276 | + * |
|
| 277 | + * @param array $credentials |
|
| 278 | + * @return void |
|
| 279 | + */ |
|
| 280 | + public function changePassword($credentials) |
|
| 281 | + { |
|
| 282 | + $user = \Auth::user(); |
|
| 283 | + if ( ! \Hash::check($credentials['old_password'], $user->password)) |
|
| 284 | + { |
|
| 285 | + \ErrorHandler::invalidOldPassword(); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + $user->password = $credentials['password']; |
|
| 289 | + $user->save(); |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + /** |
|
| 293 | + * Confirm email using the confirmation code. |
|
| 294 | + * |
|
| 295 | + * @param string $confirmationCode |
|
| 296 | + * @return void |
|
| 297 | + */ |
|
| 298 | + public function confirmEmail($confirmationCode) |
|
| 299 | + { |
|
| 300 | + $user = $this->first(['confirmation_code' => $confirmationCode]); |
|
| 301 | + $user->confirmed = 1; |
|
| 302 | + $user->confirmation_code = null; |
|
| 303 | + $user->save(); |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * Send the confirmation mail. |
|
| 308 | + * |
|
| 309 | + * @param string $email |
|
| 310 | + * @return void |
|
| 311 | + */ |
|
| 312 | + public function sendConfirmationEmail($email) |
|
| 313 | + { |
|
| 314 | + $user = $this->first(['email' => $email]); |
|
| 315 | + if ($user->confirmed) |
|
| 316 | + { |
|
| 317 | + \ErrorHandler::emailAlreadyConfirmed(); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + $user->confirmed = 0; |
|
| 321 | + $user->confirmation_code = sha1(microtime()); |
|
| 322 | + $user->save(); |
|
| 323 | + \Core::notifications()->notify($user, 'ConfirmEmail'); |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + /** |
|
| 327 | + * Paginate all users in the given group based on the given conditions. |
|
| 328 | + * |
|
| 329 | + * @param string $groupName |
|
| 330 | + * @param array $relations |
|
| 331 | + * @param integer $perPage |
|
| 332 | + * @param string $sortBy |
|
| 333 | + * @param boolean $desc |
|
| 334 | + * @return \Illuminate\Http\Response |
|
| 335 | + */ |
|
| 336 | + public function group($conditions, $groupName, $relations, $perPage, $sortBy, $desc) |
|
| 337 | + { |
|
| 338 | + unset($conditions['page']); |
|
| 339 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 340 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 341 | + $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
| 342 | + |
|
| 343 | + $model->whereHas('groups', function($q) use ($groupName){ |
|
| 344 | + $q->where('name', $groupName); |
|
| 345 | + }); |
|
| 346 | 346 | |
| 347 | 347 | |
| 348 | - if (count($conditions['conditionValues'])) |
|
| 349 | - { |
|
| 350 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - if ($perPage) |
|
| 354 | - { |
|
| 355 | - return $model->orderBy($sortBy, $sort)->paginate($perPage); |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - return $model->orderBy($sortBy, $sort)->get(); |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - /** |
|
| 362 | - * Save the given data to the logged in user. |
|
| 363 | - * |
|
| 364 | - * @param array $credentials |
|
| 365 | - * @return void |
|
| 366 | - */ |
|
| 367 | - public function saveProfile($data) |
|
| 368 | - { |
|
| 369 | - if (array_key_exists('profile_picture', $data)) |
|
| 370 | - { |
|
| 371 | - $data['profile_picture'] = \Media::uploadImageBas64($data['profile_picture'], 'admins/profile_pictures'); |
|
| 372 | - } |
|
| 348 | + if (count($conditions['conditionValues'])) |
|
| 349 | + { |
|
| 350 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + if ($perPage) |
|
| 354 | + { |
|
| 355 | + return $model->orderBy($sortBy, $sort)->paginate($perPage); |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + return $model->orderBy($sortBy, $sort)->get(); |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + /** |
|
| 362 | + * Save the given data to the logged in user. |
|
| 363 | + * |
|
| 364 | + * @param array $credentials |
|
| 365 | + * @return void |
|
| 366 | + */ |
|
| 367 | + public function saveProfile($data) |
|
| 368 | + { |
|
| 369 | + if (array_key_exists('profile_picture', $data)) |
|
| 370 | + { |
|
| 371 | + $data['profile_picture'] = \Media::uploadImageBas64($data['profile_picture'], 'admins/profile_pictures'); |
|
| 372 | + } |
|
| 373 | 373 | |
| 374 | - $data['id'] = \Auth::id(); |
|
| 375 | - $this->save($data); |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - /** |
|
| 379 | - * Ensure access token hasn't expired or revoked. |
|
| 380 | - * |
|
| 381 | - * @param string $accessToken |
|
| 382 | - * @return boolean |
|
| 383 | - */ |
|
| 384 | - public function accessTokenExpiredOrRevoked($accessToken) |
|
| 385 | - { |
|
| 386 | - |
|
| 387 | - $accessTokenRepository = \App::make('League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface'); |
|
| 388 | - $data = new ValidationData(); |
|
| 389 | - $data->setCurrentTime(time()); |
|
| 390 | - |
|
| 391 | - if ($accessToken->validate($data) === false || $accessTokenRepository->isAccessTokenRevoked($accessToken->getClaim('jti'))) |
|
| 392 | - { |
|
| 393 | - return true; |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - return false; |
|
| 397 | - } |
|
| 374 | + $data['id'] = \Auth::id(); |
|
| 375 | + $this->save($data); |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + /** |
|
| 379 | + * Ensure access token hasn't expired or revoked. |
|
| 380 | + * |
|
| 381 | + * @param string $accessToken |
|
| 382 | + * @return boolean |
|
| 383 | + */ |
|
| 384 | + public function accessTokenExpiredOrRevoked($accessToken) |
|
| 385 | + { |
|
| 386 | + |
|
| 387 | + $accessTokenRepository = \App::make('League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface'); |
|
| 388 | + $data = new ValidationData(); |
|
| 389 | + $data->setCurrentTime(time()); |
|
| 390 | + |
|
| 391 | + if ($accessToken->validate($data) === false || $accessTokenRepository->isAccessTokenRevoked($accessToken->getClaim('jti'))) |
|
| 392 | + { |
|
| 393 | + return true; |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + return false; |
|
| 397 | + } |
|
| 398 | 398 | } |
@@ -102,20 +102,16 @@ discard block |
||
| 102 | 102 | if ( ! $user = $this->first(['email' => $credentials['email']])) |
| 103 | 103 | { |
| 104 | 104 | \ErrorHandler::loginFailed(); |
| 105 | - } |
|
| 106 | - else if ($adminLogin && ! $user->groups->whereIn('name', ['Admin'])->count()) |
|
| 105 | + } else if ($adminLogin && ! $user->groups->whereIn('name', ['Admin'])->count()) |
|
| 107 | 106 | { |
| 108 | 107 | \ErrorHandler::loginFailed(); |
| 109 | - } |
|
| 110 | - else if ( ! $adminLogin && $user->groups->whereIn('name', ['Admin'])->count()) |
|
| 108 | + } else if ( ! $adminLogin && $user->groups->whereIn('name', ['Admin'])->count()) |
|
| 111 | 109 | { |
| 112 | 110 | \ErrorHandler::loginFailed(); |
| 113 | - } |
|
| 114 | - else if ($user->blocked) |
|
| 111 | + } else if ($user->blocked) |
|
| 115 | 112 | { |
| 116 | 113 | \ErrorHandler::userIsBlocked(); |
| 117 | - } |
|
| 118 | - else if ( ! $user->confirmed) |
|
| 114 | + } else if ( ! $user->confirmed) |
|
| 119 | 115 | { |
| 120 | 116 | \ErrorHandler::emailNotConfirmed(); |
| 121 | 117 | } |
@@ -143,8 +139,7 @@ discard block |
||
| 143 | 139 | { |
| 144 | 140 | $data = ['email' => $user->email, 'password' => '']; |
| 145 | 141 | return $this->register($data); |
| 146 | - } |
|
| 147 | - else |
|
| 142 | + } else |
|
| 148 | 143 | { |
| 149 | 144 | if ( ! \Auth::attempt(['email' => $registeredUser->email, 'password' => ''])) |
| 150 | 145 | { |
@@ -187,12 +182,10 @@ discard block |
||
| 187 | 182 | if ( ! $this->hasGroup(['Admin'])) |
| 188 | 183 | { |
| 189 | 184 | \ErrorHandler::noPermissions(); |
| 190 | - } |
|
| 191 | - else if (\Auth::id() == $user_id) |
|
| 185 | + } else if (\Auth::id() == $user_id) |
|
| 192 | 186 | { |
| 193 | 187 | \ErrorHandler::noPermissions(); |
| 194 | - } |
|
| 195 | - else if ($user->groups->pluck('name')->search('Admin', true) !== false) |
|
| 188 | + } else if ($user->groups->pluck('name')->search('Admin', true) !== false) |
|
| 196 | 189 | { |
| 197 | 190 | \ErrorHandler::noPermissions(); |
| 198 | 191 | } |
@@ -10,43 +10,43 @@ |
||
| 10 | 10 | |
| 11 | 11 | class ResetPassword extends Notification implements ShouldQueue |
| 12 | 12 | { |
| 13 | - use Queueable; |
|
| 13 | + use Queueable; |
|
| 14 | 14 | |
| 15 | - protected $token; |
|
| 15 | + protected $token; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Create a new notification instance. |
|
| 19 | - * |
|
| 20 | - * @return void |
|
| 21 | - */ |
|
| 22 | - public function __construct($token) |
|
| 23 | - { |
|
| 24 | - $this->token = $token; |
|
| 25 | - } |
|
| 17 | + /** |
|
| 18 | + * Create a new notification instance. |
|
| 19 | + * |
|
| 20 | + * @return void |
|
| 21 | + */ |
|
| 22 | + public function __construct($token) |
|
| 23 | + { |
|
| 24 | + $this->token = $token; |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Get the notification's delivery channels. |
|
| 29 | - * |
|
| 30 | - * @param mixed $notifiable |
|
| 31 | - * @return array |
|
| 32 | - */ |
|
| 33 | - public function via($notifiable) |
|
| 34 | - { |
|
| 35 | - return ['mail']; |
|
| 36 | - } |
|
| 27 | + /** |
|
| 28 | + * Get the notification's delivery channels. |
|
| 29 | + * |
|
| 30 | + * @param mixed $notifiable |
|
| 31 | + * @return array |
|
| 32 | + */ |
|
| 33 | + public function via($notifiable) |
|
| 34 | + { |
|
| 35 | + return ['mail']; |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Get the mail representation of the notification. |
|
| 40 | - * |
|
| 41 | - * @param mixed $notifiable |
|
| 42 | - * @return \Illuminate\Notifications\Messages\MailMessage |
|
| 43 | - */ |
|
| 44 | - public function toMail($notifiable) |
|
| 45 | - { |
|
| 46 | - return (new MailMessage) |
|
| 47 | - ->subject('Reset passowrd') |
|
| 48 | - ->line('Reset passowrd') |
|
| 49 | - ->line('To reset your password click on the button below') |
|
| 50 | - ->action('Reset password', env('RESET_PASSWORD_URL') . '/' . $this->token); |
|
| 51 | - } |
|
| 38 | + /** |
|
| 39 | + * Get the mail representation of the notification. |
|
| 40 | + * |
|
| 41 | + * @param mixed $notifiable |
|
| 42 | + * @return \Illuminate\Notifications\Messages\MailMessage |
|
| 43 | + */ |
|
| 44 | + public function toMail($notifiable) |
|
| 45 | + { |
|
| 46 | + return (new MailMessage) |
|
| 47 | + ->subject('Reset passowrd') |
|
| 48 | + ->line('Reset passowrd') |
|
| 49 | + ->line('To reset your password click on the button below') |
|
| 50 | + ->action('Reset password', env('RESET_PASSWORD_URL') . '/' . $this->token); |
|
| 51 | + } |
|
| 52 | 52 | } |
| 53 | 53 | \ No newline at end of file |
@@ -14,19 +14,19 @@ |
||
| 14 | 14 | return 'App\Modules\V1\Core\Settings'; |
| 15 | 15 | } |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Save list of settings. |
|
| 19 | - * |
|
| 20 | - * @param array $data |
|
| 21 | - * @return void |
|
| 22 | - */ |
|
| 23 | - public function saveMany(array $data) |
|
| 24 | - { |
|
| 25 | - \DB::transaction(function () use ($data) { |
|
| 26 | - foreach ($data as $key => $value) |
|
| 27 | - { |
|
| 28 | - $this->save($value); |
|
| 29 | - } |
|
| 30 | - }); |
|
| 31 | - } |
|
| 17 | + /** |
|
| 18 | + * Save list of settings. |
|
| 19 | + * |
|
| 20 | + * @param array $data |
|
| 21 | + * @return void |
|
| 22 | + */ |
|
| 23 | + public function saveMany(array $data) |
|
| 24 | + { |
|
| 25 | + \DB::transaction(function () use ($data) { |
|
| 26 | + foreach ($data as $key => $value) |
|
| 27 | + { |
|
| 28 | + $this->save($value); |
|
| 29 | + } |
|
| 30 | + }); |
|
| 31 | + } |
|
| 32 | 32 | } |
@@ -22,7 +22,7 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | public function saveMany(array $data) |
| 24 | 24 | { |
| 25 | - \DB::transaction(function () use ($data) { |
|
| 25 | + \DB::transaction(function() use ($data) { |
|
| 26 | 26 | foreach ($data as $key => $value) |
| 27 | 27 | { |
| 28 | 28 | $this->save($value); |
@@ -2,67 +2,67 @@ |
||
| 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 | - $response = []; |
|
| 15 | - $image = $image; |
|
| 16 | - $imageName = str_slug('image' . uniqid() . time() . '_' . $image->getClientOriginalName()); |
|
| 17 | - $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 | + $response = []; |
|
| 15 | + $image = $image; |
|
| 16 | + $imageName = str_slug('image' . uniqid() . time() . '_' . $image->getClientOriginalName()); |
|
| 17 | + $destinationPath = 'media' . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR; |
|
| 18 | 18 | |
| 19 | - ! file_exists($destinationPath) ? \File::makeDirectory($destinationPath) : false; |
|
| 20 | - $image->move($destinationPath, $imageName); |
|
| 19 | + ! file_exists($destinationPath) ? \File::makeDirectory($destinationPath) : false; |
|
| 20 | + $image->move($destinationPath, $imageName); |
|
| 21 | 21 | |
| 22 | - return url($destinationPath . $imageName); |
|
| 23 | - } |
|
| 22 | + return url($destinationPath . $imageName); |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Upload the given image. |
|
| 27 | - * |
|
| 28 | - * @param object $image |
|
| 29 | - * @param string $dir |
|
| 30 | - * @return string |
|
| 31 | - */ |
|
| 32 | - public function uploadImageBas64($image, $dir) |
|
| 33 | - { |
|
| 34 | - if (! strlen($image)) |
|
| 35 | - { |
|
| 36 | - return null; |
|
| 37 | - } |
|
| 25 | + /** |
|
| 26 | + * Upload the given image. |
|
| 27 | + * |
|
| 28 | + * @param object $image |
|
| 29 | + * @param string $dir |
|
| 30 | + * @return string |
|
| 31 | + */ |
|
| 32 | + public function uploadImageBas64($image, $dir) |
|
| 33 | + { |
|
| 34 | + if (! strlen($image)) |
|
| 35 | + { |
|
| 36 | + return null; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - $response = []; |
|
| 40 | - $image = $image; |
|
| 41 | - $imageName = 'image' . uniqid() . time() . '.jpg'; |
|
| 42 | - $destinationPath = 'media' . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR; |
|
| 39 | + $response = []; |
|
| 40 | + $image = $image; |
|
| 41 | + $imageName = 'image' . uniqid() . time() . '.jpg'; |
|
| 42 | + $destinationPath = 'media' . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR; |
|
| 43 | 43 | |
| 44 | - ! file_exists($destinationPath) ? \File::makeDirectory($destinationPath) : false; |
|
| 44 | + ! file_exists($destinationPath) ? \File::makeDirectory($destinationPath) : false; |
|
| 45 | 45 | |
| 46 | - $base = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $image)); |
|
| 47 | - $image = \Image::make($base)->save($destinationPath . $imageName); |
|
| 46 | + $base = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $image)); |
|
| 47 | + $image = \Image::make($base)->save($destinationPath . $imageName); |
|
| 48 | 48 | |
| 49 | - return url($destinationPath . $imageName); |
|
| 50 | - } |
|
| 49 | + return url($destinationPath . $imageName); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Delete the given image. |
|
| 54 | - * |
|
| 55 | - * @param object $path |
|
| 56 | - * @param string $dir |
|
| 57 | - * @return void |
|
| 58 | - */ |
|
| 59 | - public function deleteImage($path, $dir) |
|
| 60 | - { |
|
| 61 | - $arr = explode('/', $path); |
|
| 62 | - $path = 'media' . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR . end($arr); |
|
| 63 | - if (\File::exists($path)) |
|
| 64 | - { |
|
| 65 | - \File::delete($path); |
|
| 66 | - } |
|
| 67 | - } |
|
| 52 | + /** |
|
| 53 | + * Delete the given image. |
|
| 54 | + * |
|
| 55 | + * @param object $path |
|
| 56 | + * @param string $dir |
|
| 57 | + * @return void |
|
| 58 | + */ |
|
| 59 | + public function deleteImage($path, $dir) |
|
| 60 | + { |
|
| 61 | + $arr = explode('/', $path); |
|
| 62 | + $path = 'media' . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR . end($arr); |
|
| 63 | + if (\File::exists($path)) |
|
| 64 | + { |
|
| 65 | + \File::delete($path); |
|
| 66 | + } |
|
| 67 | + } |
|
| 68 | 68 | } |
@@ -13,13 +13,13 @@ discard block |
||
| 13 | 13 | { |
| 14 | 14 | $response = []; |
| 15 | 15 | $image = $image; |
| 16 | - $imageName = str_slug('image' . uniqid() . time() . '_' . $image->getClientOriginalName()); |
|
| 17 | - $destinationPath = 'media' . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR; |
|
| 16 | + $imageName = str_slug('image'.uniqid().time().'_'.$image->getClientOriginalName()); |
|
| 17 | + $destinationPath = 'media'.DIRECTORY_SEPARATOR.$dir.DIRECTORY_SEPARATOR; |
|
| 18 | 18 | |
| 19 | 19 | ! file_exists($destinationPath) ? \File::makeDirectory($destinationPath) : false; |
| 20 | 20 | $image->move($destinationPath, $imageName); |
| 21 | 21 | |
| 22 | - return url($destinationPath . $imageName); |
|
| 22 | + return url($destinationPath.$imageName); |
|
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | /** |
@@ -31,22 +31,22 @@ discard block |
||
| 31 | 31 | */ |
| 32 | 32 | public function uploadImageBas64($image, $dir) |
| 33 | 33 | { |
| 34 | - if (! strlen($image)) |
|
| 34 | + if ( ! strlen($image)) |
|
| 35 | 35 | { |
| 36 | 36 | return null; |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | $response = []; |
| 40 | 40 | $image = $image; |
| 41 | - $imageName = 'image' . uniqid() . time() . '.jpg'; |
|
| 42 | - $destinationPath = 'media' . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR; |
|
| 41 | + $imageName = 'image'.uniqid().time().'.jpg'; |
|
| 42 | + $destinationPath = 'media'.DIRECTORY_SEPARATOR.$dir.DIRECTORY_SEPARATOR; |
|
| 43 | 43 | |
| 44 | 44 | ! file_exists($destinationPath) ? \File::makeDirectory($destinationPath) : false; |
| 45 | 45 | |
| 46 | 46 | $base = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $image)); |
| 47 | - $image = \Image::make($base)->save($destinationPath . $imageName); |
|
| 47 | + $image = \Image::make($base)->save($destinationPath.$imageName); |
|
| 48 | 48 | |
| 49 | - return url($destinationPath . $imageName); |
|
| 49 | + return url($destinationPath.$imageName); |
|
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | /** |
@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | public function deleteImage($path, $dir) |
| 60 | 60 | { |
| 61 | 61 | $arr = explode('/', $path); |
| 62 | - $path = 'media' . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR . end($arr); |
|
| 62 | + $path = 'media'.DIRECTORY_SEPARATOR.$dir.DIRECTORY_SEPARATOR.end($arr); |
|
| 63 | 63 | if (\File::exists($path)) |
| 64 | 64 | { |
| 65 | 65 | \File::delete($path); |
@@ -6,272 +6,272 @@ |
||
| 6 | 6 | |
| 7 | 7 | class GenerateDoc extends Command |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * The name and signature of the console command. |
|
| 11 | - * |
|
| 12 | - * @var string |
|
| 13 | - */ |
|
| 14 | - protected $signature = 'doc:generate'; |
|
| 9 | + /** |
|
| 10 | + * The name and signature of the console command. |
|
| 11 | + * |
|
| 12 | + * @var string |
|
| 13 | + */ |
|
| 14 | + protected $signature = 'doc:generate'; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * The console command description. |
|
| 18 | - * |
|
| 19 | - * @var string |
|
| 20 | - */ |
|
| 21 | - protected $description = 'Generate api documentation'; |
|
| 16 | + /** |
|
| 17 | + * The console command description. |
|
| 18 | + * |
|
| 19 | + * @var string |
|
| 20 | + */ |
|
| 21 | + protected $description = 'Generate api documentation'; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Create a new command instance. |
|
| 25 | - * |
|
| 26 | - * @return void |
|
| 27 | - */ |
|
| 28 | - public function __construct() |
|
| 29 | - { |
|
| 30 | - parent::__construct(); |
|
| 31 | - } |
|
| 23 | + /** |
|
| 24 | + * Create a new command instance. |
|
| 25 | + * |
|
| 26 | + * @return void |
|
| 27 | + */ |
|
| 28 | + public function __construct() |
|
| 29 | + { |
|
| 30 | + parent::__construct(); |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * Execute the console command. |
|
| 35 | - * |
|
| 36 | - * @return mixed |
|
| 37 | - */ |
|
| 38 | - public function handle() |
|
| 39 | - { |
|
| 40 | - $docData = []; |
|
| 41 | - $docData['models'] = []; |
|
| 42 | - $routes = $this->getRoutes(); |
|
| 43 | - foreach ($routes as $route) |
|
| 44 | - { |
|
| 45 | - if ($route) |
|
| 46 | - { |
|
| 47 | - $actoinArray = explode('@', $route['action']); |
|
| 48 | - $controller = $actoinArray[0]; |
|
| 49 | - $method = $actoinArray[1]; |
|
| 50 | - $route['name'] = $method !== 'index' ? $method : 'list'; |
|
| 33 | + /** |
|
| 34 | + * Execute the console command. |
|
| 35 | + * |
|
| 36 | + * @return mixed |
|
| 37 | + */ |
|
| 38 | + public function handle() |
|
| 39 | + { |
|
| 40 | + $docData = []; |
|
| 41 | + $docData['models'] = []; |
|
| 42 | + $routes = $this->getRoutes(); |
|
| 43 | + foreach ($routes as $route) |
|
| 44 | + { |
|
| 45 | + if ($route) |
|
| 46 | + { |
|
| 47 | + $actoinArray = explode('@', $route['action']); |
|
| 48 | + $controller = $actoinArray[0]; |
|
| 49 | + $method = $actoinArray[1]; |
|
| 50 | + $route['name'] = $method !== 'index' ? $method : 'list'; |
|
| 51 | 51 | |
| 52 | - $reflectionClass = new \ReflectionClass($controller); |
|
| 53 | - $reflectionMethod = $reflectionClass->getMethod($method); |
|
| 54 | - $classProperties = $reflectionClass->getDefaultProperties(); |
|
| 55 | - $skipLoginCheck = array_key_exists('skipLoginCheck', $classProperties) ? $classProperties['skipLoginCheck'] : false; |
|
| 56 | - $validationRules = array_key_exists('validationRules', $classProperties) ? $classProperties['validationRules'] : false; |
|
| 52 | + $reflectionClass = new \ReflectionClass($controller); |
|
| 53 | + $reflectionMethod = $reflectionClass->getMethod($method); |
|
| 54 | + $classProperties = $reflectionClass->getDefaultProperties(); |
|
| 55 | + $skipLoginCheck = array_key_exists('skipLoginCheck', $classProperties) ? $classProperties['skipLoginCheck'] : false; |
|
| 56 | + $validationRules = array_key_exists('validationRules', $classProperties) ? $classProperties['validationRules'] : false; |
|
| 57 | 57 | |
| 58 | - $this->processDocBlock($route, $reflectionMethod); |
|
| 59 | - $this->getHeaders($route, $method, $skipLoginCheck); |
|
| 60 | - $this->getPostData($route, $reflectionMethod, $validationRules); |
|
| 58 | + $this->processDocBlock($route, $reflectionMethod); |
|
| 59 | + $this->getHeaders($route, $method, $skipLoginCheck); |
|
| 60 | + $this->getPostData($route, $reflectionMethod, $validationRules); |
|
| 61 | 61 | |
| 62 | - $route['response'] = $this->getResponseObject($classProperties['model'], $route['name'], $route['returnDocBlock']); |
|
| 62 | + $route['response'] = $this->getResponseObject($classProperties['model'], $route['name'], $route['returnDocBlock']); |
|
| 63 | 63 | |
| 64 | - preg_match('/api\/v1\/([^#]+)\//iU', $route['uri'], $module); |
|
| 65 | - $docData['modules'][$module[1]][substr($route['prefix'], strlen('/api/v1/' . $module[1] . '/') - 1)][] = $route; |
|
| 64 | + preg_match('/api\/v1\/([^#]+)\//iU', $route['uri'], $module); |
|
| 65 | + $docData['modules'][$module[1]][substr($route['prefix'], strlen('/api/v1/' . $module[1] . '/') - 1)][] = $route; |
|
| 66 | 66 | |
| 67 | - $this->getModels($classProperties['model'], $docData); |
|
| 68 | - } |
|
| 69 | - } |
|
| 67 | + $this->getModels($classProperties['model'], $docData); |
|
| 68 | + } |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - $docData['errors'] = $this->getErrors(); |
|
| 72 | - \File::put(app_path('Modules/V1/Core/Resources/api.json'), json_encode($docData)); |
|
| 73 | - } |
|
| 71 | + $docData['errors'] = $this->getErrors(); |
|
| 72 | + \File::put(app_path('Modules/V1/Core/Resources/api.json'), json_encode($docData)); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * Get list of all registered routes. |
|
| 77 | - * |
|
| 78 | - * @return collection |
|
| 79 | - */ |
|
| 80 | - protected function getRoutes() |
|
| 81 | - { |
|
| 82 | - return collect(\Route::getRoutes())->map(function ($route) { |
|
| 83 | - if (strpos($route->uri(), 'api/v') !== false) |
|
| 84 | - { |
|
| 85 | - return [ |
|
| 86 | - 'method' => $route->methods()[0], |
|
| 87 | - 'uri' => $route->uri(), |
|
| 88 | - 'action' => $route->getActionName(), |
|
| 89 | - 'prefix' => $route->getPrefix() |
|
| 90 | - ]; |
|
| 91 | - } |
|
| 92 | - return false; |
|
| 93 | - })->all(); |
|
| 94 | - } |
|
| 75 | + /** |
|
| 76 | + * Get list of all registered routes. |
|
| 77 | + * |
|
| 78 | + * @return collection |
|
| 79 | + */ |
|
| 80 | + protected function getRoutes() |
|
| 81 | + { |
|
| 82 | + return collect(\Route::getRoutes())->map(function ($route) { |
|
| 83 | + if (strpos($route->uri(), 'api/v') !== false) |
|
| 84 | + { |
|
| 85 | + return [ |
|
| 86 | + 'method' => $route->methods()[0], |
|
| 87 | + 'uri' => $route->uri(), |
|
| 88 | + 'action' => $route->getActionName(), |
|
| 89 | + 'prefix' => $route->getPrefix() |
|
| 90 | + ]; |
|
| 91 | + } |
|
| 92 | + return false; |
|
| 93 | + })->all(); |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - /** |
|
| 97 | - * Generate headers for the given route. |
|
| 98 | - * |
|
| 99 | - * @param array &$route |
|
| 100 | - * @param string $method |
|
| 101 | - * @param array $skipLoginCheck |
|
| 102 | - * @return void |
|
| 103 | - */ |
|
| 104 | - protected function getHeaders(&$route, $method, $skipLoginCheck) |
|
| 105 | - { |
|
| 106 | - $route['headers'] = [ |
|
| 107 | - 'Accept' => 'application/json', |
|
| 108 | - 'Content-Type' => 'application/json', |
|
| 109 | - 'locale' => 'The language of the returned data: ar, en or all.', |
|
| 110 | - 'time-zone-diff' => 'Timezone difference between UTC and Local Time', |
|
| 111 | - ]; |
|
| 96 | + /** |
|
| 97 | + * Generate headers for the given route. |
|
| 98 | + * |
|
| 99 | + * @param array &$route |
|
| 100 | + * @param string $method |
|
| 101 | + * @param array $skipLoginCheck |
|
| 102 | + * @return void |
|
| 103 | + */ |
|
| 104 | + protected function getHeaders(&$route, $method, $skipLoginCheck) |
|
| 105 | + { |
|
| 106 | + $route['headers'] = [ |
|
| 107 | + 'Accept' => 'application/json', |
|
| 108 | + 'Content-Type' => 'application/json', |
|
| 109 | + 'locale' => 'The language of the returned data: ar, en or all.', |
|
| 110 | + 'time-zone-diff' => 'Timezone difference between UTC and Local Time', |
|
| 111 | + ]; |
|
| 112 | 112 | |
| 113 | 113 | |
| 114 | - if (! $skipLoginCheck || ! in_array($method, $skipLoginCheck)) |
|
| 115 | - { |
|
| 116 | - $route['headers']['Authorization'] = 'Bearer {token}'; |
|
| 117 | - } |
|
| 118 | - } |
|
| 114 | + if (! $skipLoginCheck || ! in_array($method, $skipLoginCheck)) |
|
| 115 | + { |
|
| 116 | + $route['headers']['Authorization'] = 'Bearer {token}'; |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * Generate description and params for the given route |
|
| 122 | - * based on the docblock. |
|
| 123 | - * |
|
| 124 | - * @param array &$route |
|
| 125 | - * @param object $reflectionMethod] |
|
| 126 | - * @return void |
|
| 127 | - */ |
|
| 128 | - protected function processDocBlock(&$route, $reflectionMethod) |
|
| 129 | - { |
|
| 130 | - $factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance(); |
|
| 131 | - $docblock = $factory->create($reflectionMethod->getDocComment()); |
|
| 132 | - $route['description'] = trim(preg_replace('/\s+/', ' ', $docblock->getSummary())); |
|
| 133 | - $params = $docblock->getTagsByName('param'); |
|
| 134 | - $route['returnDocBlock'] = $docblock->getTagsByName('return')[0]->getType()->getFqsen()->getName(); |
|
| 135 | - foreach ($params as $param) |
|
| 136 | - { |
|
| 137 | - $name = $param->getVariableName(); |
|
| 138 | - if ($name !== 'request') |
|
| 139 | - { |
|
| 140 | - $route['parametars'][$param->getVariableName()] = $param->getDescription()->render(); |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - } |
|
| 120 | + /** |
|
| 121 | + * Generate description and params for the given route |
|
| 122 | + * based on the docblock. |
|
| 123 | + * |
|
| 124 | + * @param array &$route |
|
| 125 | + * @param object $reflectionMethod] |
|
| 126 | + * @return void |
|
| 127 | + */ |
|
| 128 | + protected function processDocBlock(&$route, $reflectionMethod) |
|
| 129 | + { |
|
| 130 | + $factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance(); |
|
| 131 | + $docblock = $factory->create($reflectionMethod->getDocComment()); |
|
| 132 | + $route['description'] = trim(preg_replace('/\s+/', ' ', $docblock->getSummary())); |
|
| 133 | + $params = $docblock->getTagsByName('param'); |
|
| 134 | + $route['returnDocBlock'] = $docblock->getTagsByName('return')[0]->getType()->getFqsen()->getName(); |
|
| 135 | + foreach ($params as $param) |
|
| 136 | + { |
|
| 137 | + $name = $param->getVariableName(); |
|
| 138 | + if ($name !== 'request') |
|
| 139 | + { |
|
| 140 | + $route['parametars'][$param->getVariableName()] = $param->getDescription()->render(); |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - /** |
|
| 146 | - * Generate post body for the given route. |
|
| 147 | - * |
|
| 148 | - * @param array &$route |
|
| 149 | - * @param object $reflectionMethod |
|
| 150 | - * @param array $validationRules |
|
| 151 | - * @return void |
|
| 152 | - */ |
|
| 153 | - protected function getPostData(&$route, $reflectionMethod, $validationRules) |
|
| 154 | - { |
|
| 155 | - if ($route['method'] == 'POST') |
|
| 156 | - { |
|
| 157 | - $body = $this->getMethodBody($reflectionMethod); |
|
| 145 | + /** |
|
| 146 | + * Generate post body for the given route. |
|
| 147 | + * |
|
| 148 | + * @param array &$route |
|
| 149 | + * @param object $reflectionMethod |
|
| 150 | + * @param array $validationRules |
|
| 151 | + * @return void |
|
| 152 | + */ |
|
| 153 | + protected function getPostData(&$route, $reflectionMethod, $validationRules) |
|
| 154 | + { |
|
| 155 | + if ($route['method'] == 'POST') |
|
| 156 | + { |
|
| 157 | + $body = $this->getMethodBody($reflectionMethod); |
|
| 158 | 158 | |
| 159 | - preg_match('/\$this->validate\(\$request,([^#]+)\);/iU', $body, $match); |
|
| 160 | - if (count($match)) |
|
| 161 | - { |
|
| 162 | - if ($match[1] == '$this->validationRules') |
|
| 163 | - { |
|
| 164 | - $route['body'] = $validationRules; |
|
| 165 | - } |
|
| 166 | - else |
|
| 167 | - { |
|
| 168 | - $route['body'] = eval('return ' . str_replace(',\'.$request->get(\'id\')', ',{id}\'', $match[1]) . ';'); |
|
| 169 | - } |
|
| 159 | + preg_match('/\$this->validate\(\$request,([^#]+)\);/iU', $body, $match); |
|
| 160 | + if (count($match)) |
|
| 161 | + { |
|
| 162 | + if ($match[1] == '$this->validationRules') |
|
| 163 | + { |
|
| 164 | + $route['body'] = $validationRules; |
|
| 165 | + } |
|
| 166 | + else |
|
| 167 | + { |
|
| 168 | + $route['body'] = eval('return ' . str_replace(',\'.$request->get(\'id\')', ',{id}\'', $match[1]) . ';'); |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | - foreach ($route['body'] as &$rule) |
|
| 172 | - { |
|
| 173 | - if(strpos($rule, 'unique')) |
|
| 174 | - { |
|
| 175 | - $rule = substr($rule, 0, strpos($rule, 'unique') + 6); |
|
| 176 | - } |
|
| 177 | - elseif(strpos($rule, 'exists')) |
|
| 178 | - { |
|
| 179 | - $rule = substr($rule, 0, strpos($rule, 'exists') - 1); |
|
| 180 | - } |
|
| 181 | - } |
|
| 182 | - } |
|
| 183 | - else |
|
| 184 | - { |
|
| 185 | - $route['body'] = 'conditions'; |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - } |
|
| 171 | + foreach ($route['body'] as &$rule) |
|
| 172 | + { |
|
| 173 | + if(strpos($rule, 'unique')) |
|
| 174 | + { |
|
| 175 | + $rule = substr($rule, 0, strpos($rule, 'unique') + 6); |
|
| 176 | + } |
|
| 177 | + elseif(strpos($rule, 'exists')) |
|
| 178 | + { |
|
| 179 | + $rule = substr($rule, 0, strpos($rule, 'exists') - 1); |
|
| 180 | + } |
|
| 181 | + } |
|
| 182 | + } |
|
| 183 | + else |
|
| 184 | + { |
|
| 185 | + $route['body'] = 'conditions'; |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + } |
|
| 189 | 189 | |
| 190 | - /** |
|
| 191 | - * Generate application errors. |
|
| 192 | - * |
|
| 193 | - * @return array |
|
| 194 | - */ |
|
| 195 | - protected function getErrors() |
|
| 196 | - { |
|
| 197 | - $errors = []; |
|
| 198 | - $reflectionClass = new \ReflectionClass('App\Modules\V1\Core\Utl\ErrorHandler'); |
|
| 199 | - foreach ($reflectionClass->getMethods() as $method) |
|
| 200 | - { |
|
| 201 | - $methodName = $method->getName(); |
|
| 202 | - $reflectionMethod = $reflectionClass->getMethod($methodName); |
|
| 203 | - $body = $this->getMethodBody($reflectionMethod); |
|
| 190 | + /** |
|
| 191 | + * Generate application errors. |
|
| 192 | + * |
|
| 193 | + * @return array |
|
| 194 | + */ |
|
| 195 | + protected function getErrors() |
|
| 196 | + { |
|
| 197 | + $errors = []; |
|
| 198 | + $reflectionClass = new \ReflectionClass('App\Modules\V1\Core\Utl\ErrorHandler'); |
|
| 199 | + foreach ($reflectionClass->getMethods() as $method) |
|
| 200 | + { |
|
| 201 | + $methodName = $method->getName(); |
|
| 202 | + $reflectionMethod = $reflectionClass->getMethod($methodName); |
|
| 203 | + $body = $this->getMethodBody($reflectionMethod); |
|
| 204 | 204 | |
| 205 | - preg_match('/\$error=\[\'status\'=>([^#]+)\,/iU', $body, $match); |
|
| 205 | + preg_match('/\$error=\[\'status\'=>([^#]+)\,/iU', $body, $match); |
|
| 206 | 206 | |
| 207 | - if (count($match)) |
|
| 208 | - { |
|
| 209 | - $errors[$match[1]][] = $methodName; |
|
| 210 | - } |
|
| 211 | - } |
|
| 207 | + if (count($match)) |
|
| 208 | + { |
|
| 209 | + $errors[$match[1]][] = $methodName; |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | 212 | |
| 213 | - return $errors; |
|
| 214 | - } |
|
| 213 | + return $errors; |
|
| 214 | + } |
|
| 215 | 215 | |
| 216 | - /** |
|
| 217 | - * Get the given method body code. |
|
| 218 | - * |
|
| 219 | - * @param object $reflectionMethod |
|
| 220 | - * @return string |
|
| 221 | - */ |
|
| 222 | - protected function getMethodBody($reflectionMethod) |
|
| 223 | - { |
|
| 224 | - $filename = $reflectionMethod->getFileName(); |
|
| 225 | - $start_line = $reflectionMethod->getStartLine() - 1; |
|
| 226 | - $end_line = $reflectionMethod->getEndLine(); |
|
| 227 | - $length = $end_line - $start_line; |
|
| 228 | - $source = file($filename); |
|
| 229 | - $body = implode("", array_slice($source, $start_line, $length)); |
|
| 230 | - $body = trim(preg_replace('/\s+/', '', $body)); |
|
| 216 | + /** |
|
| 217 | + * Get the given method body code. |
|
| 218 | + * |
|
| 219 | + * @param object $reflectionMethod |
|
| 220 | + * @return string |
|
| 221 | + */ |
|
| 222 | + protected function getMethodBody($reflectionMethod) |
|
| 223 | + { |
|
| 224 | + $filename = $reflectionMethod->getFileName(); |
|
| 225 | + $start_line = $reflectionMethod->getStartLine() - 1; |
|
| 226 | + $end_line = $reflectionMethod->getEndLine(); |
|
| 227 | + $length = $end_line - $start_line; |
|
| 228 | + $source = file($filename); |
|
| 229 | + $body = implode("", array_slice($source, $start_line, $length)); |
|
| 230 | + $body = trim(preg_replace('/\s+/', '', $body)); |
|
| 231 | 231 | |
| 232 | - return $body; |
|
| 233 | - } |
|
| 232 | + return $body; |
|
| 233 | + } |
|
| 234 | 234 | |
| 235 | - /** |
|
| 236 | - * Get example object of all availble models. |
|
| 237 | - * |
|
| 238 | - * @param string $modelName |
|
| 239 | - * @param array $docData |
|
| 240 | - * @return string |
|
| 241 | - */ |
|
| 242 | - protected function getModels($modelName, &$docData) |
|
| 243 | - { |
|
| 244 | - if ($modelName && ! array_key_exists($modelName, $docData['models'])) |
|
| 245 | - { |
|
| 246 | - $modelClass = call_user_func_array("\Core::{$modelName}", [])->modelClass; |
|
| 247 | - $model = factory($modelClass)->make(); |
|
| 248 | - $modelArr = $model->toArray(); |
|
| 235 | + /** |
|
| 236 | + * Get example object of all availble models. |
|
| 237 | + * |
|
| 238 | + * @param string $modelName |
|
| 239 | + * @param array $docData |
|
| 240 | + * @return string |
|
| 241 | + */ |
|
| 242 | + protected function getModels($modelName, &$docData) |
|
| 243 | + { |
|
| 244 | + if ($modelName && ! array_key_exists($modelName, $docData['models'])) |
|
| 245 | + { |
|
| 246 | + $modelClass = call_user_func_array("\Core::{$modelName}", [])->modelClass; |
|
| 247 | + $model = factory($modelClass)->make(); |
|
| 248 | + $modelArr = $model->toArray(); |
|
| 249 | 249 | |
| 250 | - if ( $model->trans && ! $model->trans->count()) |
|
| 251 | - { |
|
| 252 | - $modelArr['trans'] = [ |
|
| 253 | - 'en' => factory($modelClass . 'Translation')->make()->toArray() |
|
| 254 | - ]; |
|
| 255 | - } |
|
| 250 | + if ( $model->trans && ! $model->trans->count()) |
|
| 251 | + { |
|
| 252 | + $modelArr['trans'] = [ |
|
| 253 | + 'en' => factory($modelClass . 'Translation')->make()->toArray() |
|
| 254 | + ]; |
|
| 255 | + } |
|
| 256 | 256 | |
| 257 | - $docData['models'][$modelName] = json_encode($modelArr, JSON_PRETTY_PRINT); |
|
| 258 | - } |
|
| 259 | - } |
|
| 257 | + $docData['models'][$modelName] = json_encode($modelArr, JSON_PRETTY_PRINT); |
|
| 258 | + } |
|
| 259 | + } |
|
| 260 | 260 | |
| 261 | - /** |
|
| 262 | - * Get the route response object type. |
|
| 263 | - * |
|
| 264 | - * @param string $modelName |
|
| 265 | - * @param string $method |
|
| 266 | - * @param string $returnDocBlock |
|
| 267 | - * @return array |
|
| 268 | - */ |
|
| 269 | - protected function getResponseObject($modelName, $method, $returnDocBlock) |
|
| 270 | - { |
|
| 271 | - $config = \CoreConfig::getConfig(); |
|
| 272 | - $relations = array_key_exists($modelName, $config['relations']) ? array_key_exists($method, $config['relations'][$modelName]) ? $config['relations'][$modelName] : false : false; |
|
| 273 | - $modelName = call_user_func_array("\Core::{$returnDocBlock}", []) ? $returnDocBlock : $modelName; |
|
| 261 | + /** |
|
| 262 | + * Get the route response object type. |
|
| 263 | + * |
|
| 264 | + * @param string $modelName |
|
| 265 | + * @param string $method |
|
| 266 | + * @param string $returnDocBlock |
|
| 267 | + * @return array |
|
| 268 | + */ |
|
| 269 | + protected function getResponseObject($modelName, $method, $returnDocBlock) |
|
| 270 | + { |
|
| 271 | + $config = \CoreConfig::getConfig(); |
|
| 272 | + $relations = array_key_exists($modelName, $config['relations']) ? array_key_exists($method, $config['relations'][$modelName]) ? $config['relations'][$modelName] : false : false; |
|
| 273 | + $modelName = call_user_func_array("\Core::{$returnDocBlock}", []) ? $returnDocBlock : $modelName; |
|
| 274 | 274 | |
| 275 | - return $relations ? [$modelName => $relations && $relations[$method] ? $relations[$method] : []] : false; |
|
| 276 | - } |
|
| 275 | + return $relations ? [$modelName => $relations && $relations[$method] ? $relations[$method] : []] : false; |
|
| 276 | + } |
|
| 277 | 277 | } |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | $route['response'] = $this->getResponseObject($classProperties['model'], $route['name'], $route['returnDocBlock']); |
| 63 | 63 | |
| 64 | 64 | preg_match('/api\/v1\/([^#]+)\//iU', $route['uri'], $module); |
| 65 | - $docData['modules'][$module[1]][substr($route['prefix'], strlen('/api/v1/' . $module[1] . '/') - 1)][] = $route; |
|
| 65 | + $docData['modules'][$module[1]][substr($route['prefix'], strlen('/api/v1/'.$module[1].'/') - 1)][] = $route; |
|
| 66 | 66 | |
| 67 | 67 | $this->getModels($classProperties['model'], $docData); |
| 68 | 68 | } |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | */ |
| 80 | 80 | protected function getRoutes() |
| 81 | 81 | { |
| 82 | - return collect(\Route::getRoutes())->map(function ($route) { |
|
| 82 | + return collect(\Route::getRoutes())->map(function($route) { |
|
| 83 | 83 | if (strpos($route->uri(), 'api/v') !== false) |
| 84 | 84 | { |
| 85 | 85 | return [ |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | ]; |
| 112 | 112 | |
| 113 | 113 | |
| 114 | - if (! $skipLoginCheck || ! in_array($method, $skipLoginCheck)) |
|
| 114 | + if ( ! $skipLoginCheck || ! in_array($method, $skipLoginCheck)) |
|
| 115 | 115 | { |
| 116 | 116 | $route['headers']['Authorization'] = 'Bearer {token}'; |
| 117 | 117 | } |
@@ -165,16 +165,16 @@ discard block |
||
| 165 | 165 | } |
| 166 | 166 | else |
| 167 | 167 | { |
| 168 | - $route['body'] = eval('return ' . str_replace(',\'.$request->get(\'id\')', ',{id}\'', $match[1]) . ';'); |
|
| 168 | + $route['body'] = eval('return '.str_replace(',\'.$request->get(\'id\')', ',{id}\'', $match[1]).';'); |
|
| 169 | 169 | } |
| 170 | 170 | |
| 171 | 171 | foreach ($route['body'] as &$rule) |
| 172 | 172 | { |
| 173 | - if(strpos($rule, 'unique')) |
|
| 173 | + if (strpos($rule, 'unique')) |
|
| 174 | 174 | { |
| 175 | 175 | $rule = substr($rule, 0, strpos($rule, 'unique') + 6); |
| 176 | 176 | } |
| 177 | - elseif(strpos($rule, 'exists')) |
|
| 177 | + elseif (strpos($rule, 'exists')) |
|
| 178 | 178 | { |
| 179 | 179 | $rule = substr($rule, 0, strpos($rule, 'exists') - 1); |
| 180 | 180 | } |
@@ -247,10 +247,10 @@ discard block |
||
| 247 | 247 | $model = factory($modelClass)->make(); |
| 248 | 248 | $modelArr = $model->toArray(); |
| 249 | 249 | |
| 250 | - if ( $model->trans && ! $model->trans->count()) |
|
| 250 | + if ($model->trans && ! $model->trans->count()) |
|
| 251 | 251 | { |
| 252 | 252 | $modelArr['trans'] = [ |
| 253 | - 'en' => factory($modelClass . 'Translation')->make()->toArray() |
|
| 253 | + 'en' => factory($modelClass.'Translation')->make()->toArray() |
|
| 254 | 254 | ]; |
| 255 | 255 | } |
| 256 | 256 | |
@@ -6,76 +6,76 @@ |
||
| 6 | 6 | |
| 7 | 7 | class SettingsTableSeeder extends Seeder |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Run the database seeds. |
|
| 11 | - * |
|
| 12 | - * @return void |
|
| 13 | - */ |
|
| 14 | - public function run() |
|
| 15 | - { |
|
| 16 | - /** |
|
| 17 | - * Insert the permissions related to settings table. |
|
| 18 | - */ |
|
| 19 | - \DB::table('permissions')->insert( |
|
| 20 | - [ |
|
| 21 | - /** |
|
| 22 | - * Settings model permissions. |
|
| 23 | - */ |
|
| 24 | - [ |
|
| 25 | - 'name' => 'save', |
|
| 26 | - 'model' => 'settings', |
|
| 27 | - 'created_at' => \DB::raw('NOW()'), |
|
| 28 | - 'updated_at' => \DB::raw('NOW()') |
|
| 29 | - ], |
|
| 30 | - [ |
|
| 31 | - 'name' => 'find', |
|
| 32 | - 'model' => 'settings', |
|
| 33 | - 'created_at' => \DB::raw('NOW()'), |
|
| 34 | - 'updated_at' => \DB::raw('NOW()') |
|
| 35 | - ], |
|
| 36 | - [ |
|
| 37 | - 'name' => 'search', |
|
| 38 | - 'model' => 'settings', |
|
| 39 | - 'created_at' => \DB::raw('NOW()'), |
|
| 40 | - 'updated_at' => \DB::raw('NOW()') |
|
| 41 | - ], |
|
| 42 | - [ |
|
| 43 | - 'name' => 'list', |
|
| 44 | - 'model' => 'settings', |
|
| 45 | - 'created_at' => \DB::raw('NOW()'), |
|
| 46 | - 'updated_at' => \DB::raw('NOW()') |
|
| 47 | - ], |
|
| 48 | - [ |
|
| 49 | - 'name' => 'findby', |
|
| 50 | - 'model' => 'settings', |
|
| 51 | - 'created_at' => \DB::raw('NOW()'), |
|
| 52 | - 'updated_at' => \DB::raw('NOW()') |
|
| 53 | - ], |
|
| 54 | - [ |
|
| 55 | - 'name' => 'first', |
|
| 56 | - 'model' => 'settings', |
|
| 57 | - 'created_at' => \DB::raw('NOW()'), |
|
| 58 | - 'updated_at' => \DB::raw('NOW()') |
|
| 59 | - ], |
|
| 60 | - [ |
|
| 61 | - 'name' => 'paginate', |
|
| 62 | - 'model' => 'settings', |
|
| 63 | - 'created_at' => \DB::raw('NOW()'), |
|
| 64 | - 'updated_at' => \DB::raw('NOW()') |
|
| 65 | - ], |
|
| 66 | - [ |
|
| 67 | - 'name' => 'paginateby', |
|
| 68 | - 'model' => 'settings', |
|
| 69 | - 'created_at' => \DB::raw('NOW()'), |
|
| 70 | - 'updated_at' => \DB::raw('NOW()') |
|
| 71 | - ], |
|
| 72 | - [ |
|
| 73 | - 'name' => 'saveMany', |
|
| 74 | - 'model' => 'settings', |
|
| 75 | - 'created_at' => \DB::raw('NOW()'), |
|
| 76 | - 'updated_at' => \DB::raw('NOW()') |
|
| 77 | - ] |
|
| 78 | - ] |
|
| 79 | - ); |
|
| 80 | - } |
|
| 9 | + /** |
|
| 10 | + * Run the database seeds. |
|
| 11 | + * |
|
| 12 | + * @return void |
|
| 13 | + */ |
|
| 14 | + public function run() |
|
| 15 | + { |
|
| 16 | + /** |
|
| 17 | + * Insert the permissions related to settings table. |
|
| 18 | + */ |
|
| 19 | + \DB::table('permissions')->insert( |
|
| 20 | + [ |
|
| 21 | + /** |
|
| 22 | + * Settings model permissions. |
|
| 23 | + */ |
|
| 24 | + [ |
|
| 25 | + 'name' => 'save', |
|
| 26 | + 'model' => 'settings', |
|
| 27 | + 'created_at' => \DB::raw('NOW()'), |
|
| 28 | + 'updated_at' => \DB::raw('NOW()') |
|
| 29 | + ], |
|
| 30 | + [ |
|
| 31 | + 'name' => 'find', |
|
| 32 | + 'model' => 'settings', |
|
| 33 | + 'created_at' => \DB::raw('NOW()'), |
|
| 34 | + 'updated_at' => \DB::raw('NOW()') |
|
| 35 | + ], |
|
| 36 | + [ |
|
| 37 | + 'name' => 'search', |
|
| 38 | + 'model' => 'settings', |
|
| 39 | + 'created_at' => \DB::raw('NOW()'), |
|
| 40 | + 'updated_at' => \DB::raw('NOW()') |
|
| 41 | + ], |
|
| 42 | + [ |
|
| 43 | + 'name' => 'list', |
|
| 44 | + 'model' => 'settings', |
|
| 45 | + 'created_at' => \DB::raw('NOW()'), |
|
| 46 | + 'updated_at' => \DB::raw('NOW()') |
|
| 47 | + ], |
|
| 48 | + [ |
|
| 49 | + 'name' => 'findby', |
|
| 50 | + 'model' => 'settings', |
|
| 51 | + 'created_at' => \DB::raw('NOW()'), |
|
| 52 | + 'updated_at' => \DB::raw('NOW()') |
|
| 53 | + ], |
|
| 54 | + [ |
|
| 55 | + 'name' => 'first', |
|
| 56 | + 'model' => 'settings', |
|
| 57 | + 'created_at' => \DB::raw('NOW()'), |
|
| 58 | + 'updated_at' => \DB::raw('NOW()') |
|
| 59 | + ], |
|
| 60 | + [ |
|
| 61 | + 'name' => 'paginate', |
|
| 62 | + 'model' => 'settings', |
|
| 63 | + 'created_at' => \DB::raw('NOW()'), |
|
| 64 | + 'updated_at' => \DB::raw('NOW()') |
|
| 65 | + ], |
|
| 66 | + [ |
|
| 67 | + 'name' => 'paginateby', |
|
| 68 | + 'model' => 'settings', |
|
| 69 | + 'created_at' => \DB::raw('NOW()'), |
|
| 70 | + 'updated_at' => \DB::raw('NOW()') |
|
| 71 | + ], |
|
| 72 | + [ |
|
| 73 | + 'name' => 'saveMany', |
|
| 74 | + 'model' => 'settings', |
|
| 75 | + 'created_at' => \DB::raw('NOW()'), |
|
| 76 | + 'updated_at' => \DB::raw('NOW()') |
|
| 77 | + ] |
|
| 78 | + ] |
|
| 79 | + ); |
|
| 80 | + } |
|
| 81 | 81 | } |
@@ -4,716 +4,716 @@ |
||
| 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 object |
|
| 202 | - */ |
|
| 203 | - public function save(array $data) |
|
| 204 | - { |
|
| 205 | - $model = false; |
|
| 206 | - $modelClass = $this->model; |
|
| 207 | - $relations = []; |
|
| 208 | - |
|
| 209 | - \DB::transaction(function () use (&$model, &$relations, $data, $modelClass) { |
|
| 210 | - /** |
|
| 211 | - * If the id is present in the data then select the model for updating, |
|
| 212 | - * else create new model. |
|
| 213 | - * @var array |
|
| 214 | - */ |
|
| 215 | - $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
| 216 | - if ( ! $model) |
|
| 217 | - { |
|
| 218 | - \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * Construct the model object with the given data, |
|
| 223 | - * and if there is a relation add it to relations array, |
|
| 224 | - * then save the model. |
|
| 225 | - */ |
|
| 226 | - foreach ($data as $key => $value) |
|
| 227 | - { |
|
| 228 | - /** |
|
| 229 | - * If the attribute is a relation. |
|
| 230 | - */ |
|
| 231 | - $relation = camel_case($key); |
|
| 232 | - if (method_exists($model, $relation) && \Core::$relation()) |
|
| 233 | - { |
|
| 234 | - /** |
|
| 235 | - * Check if the relation is a collection. |
|
| 236 | - */ |
|
| 237 | - if (class_basename($model->$relation) == 'Collection') |
|
| 238 | - { |
|
| 239 | - /** |
|
| 240 | - * If the relation has no value then marke the relation data |
|
| 241 | - * related to the model to be deleted. |
|
| 242 | - */ |
|
| 243 | - if ( ! $value || ! count($value)) |
|
| 244 | - { |
|
| 245 | - $relations[$relation] = 'delete'; |
|
| 246 | - } |
|
| 247 | - } |
|
| 248 | - if (is_array($value)) |
|
| 249 | - { |
|
| 250 | - /** |
|
| 251 | - * Loop through the relation data. |
|
| 252 | - */ |
|
| 253 | - foreach ($value as $attr => $val) |
|
| 254 | - { |
|
| 255 | - /** |
|
| 256 | - * Get the relation model. |
|
| 257 | - */ |
|
| 258 | - $relationBaseModel = \Core::$relation()->model; |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * Check if the relation is a collection. |
|
| 262 | - */ |
|
| 263 | - if (class_basename($model->$relation) == 'Collection') |
|
| 264 | - { |
|
| 265 | - /** |
|
| 266 | - * If the id is present in the data then select the relation model for updating, |
|
| 267 | - * else create new model. |
|
| 268 | - */ |
|
| 269 | - $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
| 270 | - |
|
| 271 | - /** |
|
| 272 | - * If model doesn't exists. |
|
| 273 | - */ |
|
| 274 | - if ( ! $relationModel) |
|
| 275 | - { |
|
| 276 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * Loop through the relation attributes. |
|
| 281 | - */ |
|
| 282 | - foreach ($val as $attr => $val) |
|
| 283 | - { |
|
| 284 | - /** |
|
| 285 | - * Prevent the sub relations or attributes not in the fillable. |
|
| 286 | - */ |
|
| 287 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
| 288 | - { |
|
| 289 | - $relationModel->$attr = $val; |
|
| 290 | - } |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - $relations[$relation][] = $relationModel; |
|
| 294 | - } |
|
| 295 | - /** |
|
| 296 | - * If not collection. |
|
| 297 | - */ |
|
| 298 | - else |
|
| 299 | - { |
|
| 300 | - /** |
|
| 301 | - * Prevent the sub relations. |
|
| 302 | - */ |
|
| 303 | - if (gettype($val) !== 'object' && gettype($val) !== 'array') |
|
| 304 | - { |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * If the id is present in the data then select the relation model for updating, |
|
| 308 | - * else create new model. |
|
| 309 | - */ |
|
| 310 | - $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * If model doesn't exists. |
|
| 314 | - */ |
|
| 315 | - if ( ! $relationModel) |
|
| 316 | - { |
|
| 317 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - foreach ($value as $relationAttribute => $relationValue) |
|
| 321 | - { |
|
| 322 | - /** |
|
| 323 | - * Prevent attributes not in the fillable. |
|
| 324 | - */ |
|
| 325 | - if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) |
|
| 326 | - { |
|
| 327 | - $relationModel->$relationAttribute = $relationValue; |
|
| 328 | - } |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - $relations[$relation] = $relationModel; |
|
| 332 | - } |
|
| 333 | - } |
|
| 334 | - } |
|
| 335 | - } |
|
| 336 | - } |
|
| 337 | - /** |
|
| 338 | - * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
| 339 | - */ |
|
| 340 | - else if (array_search($key, $model->getFillable(), true) !== false) |
|
| 341 | - { |
|
| 342 | - $model->$key = $value; |
|
| 343 | - } |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - /** |
|
| 347 | - * Loop through the relations array. |
|
| 348 | - */ |
|
| 349 | - foreach ($relations as $key => $value) |
|
| 350 | - { |
|
| 351 | - /** |
|
| 352 | - * If the relation is marked for delete then delete it. |
|
| 353 | - */ |
|
| 354 | - if ($value == 'delete' && $model->$key()->count()) |
|
| 355 | - { |
|
| 356 | - $model->$key()->delete(); |
|
| 357 | - } |
|
| 358 | - /** |
|
| 359 | - * If the relation is an array. |
|
| 360 | - */ |
|
| 361 | - else if (gettype($value) == 'array') |
|
| 362 | - { |
|
| 363 | - /** |
|
| 364 | - * Save the model. |
|
| 365 | - */ |
|
| 366 | - $model->save(); |
|
| 367 | - $ids = []; |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * Loop through the relations. |
|
| 371 | - */ |
|
| 372 | - foreach ($value as $val) |
|
| 373 | - { |
|
| 374 | - switch (class_basename($model->$key())) |
|
| 375 | - { |
|
| 376 | - /** |
|
| 377 | - * If the relation is one to many then update it's foreign key with |
|
| 378 | - * the model id and save it then add its id to ids array to delete all |
|
| 379 | - * relations who's id isn't in the ids array. |
|
| 380 | - */ |
|
| 381 | - case 'HasMany': |
|
| 382 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 383 | - $val->$foreignKeyName = $model->id; |
|
| 384 | - $val->save(); |
|
| 385 | - $ids[] = $val->id; |
|
| 386 | - break; |
|
| 387 | - |
|
| 388 | - /** |
|
| 389 | - * If the relation is many to many then add it's id to the ids array to |
|
| 390 | - * attache these ids to the model. |
|
| 391 | - */ |
|
| 392 | - case 'BelongsToMany': |
|
| 393 | - $val->save(); |
|
| 394 | - $ids[] = $val->id; |
|
| 395 | - break; |
|
| 396 | - } |
|
| 397 | - } |
|
| 398 | - switch (class_basename($model->$key())) |
|
| 399 | - { |
|
| 400 | - /** |
|
| 401 | - * If the relation is one to many then delete all |
|
| 402 | - * relations who's id isn't in the ids array. |
|
| 403 | - */ |
|
| 404 | - case 'HasMany': |
|
| 405 | - $model->$key()->whereNotIn('id', $ids)->delete(); |
|
| 406 | - break; |
|
| 407 | - |
|
| 408 | - /** |
|
| 409 | - * If the relation is many to many then |
|
| 410 | - * detach the previous data and attach |
|
| 411 | - * the ids array to the model. |
|
| 412 | - */ |
|
| 413 | - case 'BelongsToMany': |
|
| 414 | - $model->$key()->detach(); |
|
| 415 | - $model->$key()->attach($ids); |
|
| 416 | - break; |
|
| 417 | - } |
|
| 418 | - } |
|
| 419 | - /** |
|
| 420 | - * If the relation isn't array. |
|
| 421 | - */ |
|
| 422 | - else |
|
| 423 | - { |
|
| 424 | - switch (class_basename($model->$key())) |
|
| 425 | - { |
|
| 426 | - /** |
|
| 427 | - * If the relation is one to one. |
|
| 428 | - */ |
|
| 429 | - case 'HasOne': |
|
| 430 | - /** |
|
| 431 | - * Save the model. |
|
| 432 | - */ |
|
| 433 | - $model->save(); |
|
| 434 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 435 | - $value->$foreignKeyName = $model->id; |
|
| 436 | - $value->save(); |
|
| 437 | - break; |
|
| 438 | - case 'BelongsTo': |
|
| 439 | - /** |
|
| 440 | - * Save the model. |
|
| 441 | - */ |
|
| 442 | - $value->save(); |
|
| 443 | - $model->$key()->associate($value); |
|
| 444 | - break; |
|
| 445 | - } |
|
| 446 | - } |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - /** |
|
| 450 | - * Save the model. |
|
| 451 | - */ |
|
| 452 | - $model->save(); |
|
| 453 | - }); |
|
| 197 | + /** |
|
| 198 | + * Save the given model to the storage. |
|
| 199 | + * |
|
| 200 | + * @param array $data |
|
| 201 | + * @return object |
|
| 202 | + */ |
|
| 203 | + public function save(array $data) |
|
| 204 | + { |
|
| 205 | + $model = false; |
|
| 206 | + $modelClass = $this->model; |
|
| 207 | + $relations = []; |
|
| 208 | + |
|
| 209 | + \DB::transaction(function () use (&$model, &$relations, $data, $modelClass) { |
|
| 210 | + /** |
|
| 211 | + * If the id is present in the data then select the model for updating, |
|
| 212 | + * else create new model. |
|
| 213 | + * @var array |
|
| 214 | + */ |
|
| 215 | + $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
| 216 | + if ( ! $model) |
|
| 217 | + { |
|
| 218 | + \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * Construct the model object with the given data, |
|
| 223 | + * and if there is a relation add it to relations array, |
|
| 224 | + * then save the model. |
|
| 225 | + */ |
|
| 226 | + foreach ($data as $key => $value) |
|
| 227 | + { |
|
| 228 | + /** |
|
| 229 | + * If the attribute is a relation. |
|
| 230 | + */ |
|
| 231 | + $relation = camel_case($key); |
|
| 232 | + if (method_exists($model, $relation) && \Core::$relation()) |
|
| 233 | + { |
|
| 234 | + /** |
|
| 235 | + * Check if the relation is a collection. |
|
| 236 | + */ |
|
| 237 | + if (class_basename($model->$relation) == 'Collection') |
|
| 238 | + { |
|
| 239 | + /** |
|
| 240 | + * If the relation has no value then marke the relation data |
|
| 241 | + * related to the model to be deleted. |
|
| 242 | + */ |
|
| 243 | + if ( ! $value || ! count($value)) |
|
| 244 | + { |
|
| 245 | + $relations[$relation] = 'delete'; |
|
| 246 | + } |
|
| 247 | + } |
|
| 248 | + if (is_array($value)) |
|
| 249 | + { |
|
| 250 | + /** |
|
| 251 | + * Loop through the relation data. |
|
| 252 | + */ |
|
| 253 | + foreach ($value as $attr => $val) |
|
| 254 | + { |
|
| 255 | + /** |
|
| 256 | + * Get the relation model. |
|
| 257 | + */ |
|
| 258 | + $relationBaseModel = \Core::$relation()->model; |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * Check if the relation is a collection. |
|
| 262 | + */ |
|
| 263 | + if (class_basename($model->$relation) == 'Collection') |
|
| 264 | + { |
|
| 265 | + /** |
|
| 266 | + * If the id is present in the data then select the relation model for updating, |
|
| 267 | + * else create new model. |
|
| 268 | + */ |
|
| 269 | + $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
| 270 | + |
|
| 271 | + /** |
|
| 272 | + * If model doesn't exists. |
|
| 273 | + */ |
|
| 274 | + if ( ! $relationModel) |
|
| 275 | + { |
|
| 276 | + \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * Loop through the relation attributes. |
|
| 281 | + */ |
|
| 282 | + foreach ($val as $attr => $val) |
|
| 283 | + { |
|
| 284 | + /** |
|
| 285 | + * Prevent the sub relations or attributes not in the fillable. |
|
| 286 | + */ |
|
| 287 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
| 288 | + { |
|
| 289 | + $relationModel->$attr = $val; |
|
| 290 | + } |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + $relations[$relation][] = $relationModel; |
|
| 294 | + } |
|
| 295 | + /** |
|
| 296 | + * If not collection. |
|
| 297 | + */ |
|
| 298 | + else |
|
| 299 | + { |
|
| 300 | + /** |
|
| 301 | + * Prevent the sub relations. |
|
| 302 | + */ |
|
| 303 | + if (gettype($val) !== 'object' && gettype($val) !== 'array') |
|
| 304 | + { |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * If the id is present in the data then select the relation model for updating, |
|
| 308 | + * else create new model. |
|
| 309 | + */ |
|
| 310 | + $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * If model doesn't exists. |
|
| 314 | + */ |
|
| 315 | + if ( ! $relationModel) |
|
| 316 | + { |
|
| 317 | + \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + foreach ($value as $relationAttribute => $relationValue) |
|
| 321 | + { |
|
| 322 | + /** |
|
| 323 | + * Prevent attributes not in the fillable. |
|
| 324 | + */ |
|
| 325 | + if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) |
|
| 326 | + { |
|
| 327 | + $relationModel->$relationAttribute = $relationValue; |
|
| 328 | + } |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + $relations[$relation] = $relationModel; |
|
| 332 | + } |
|
| 333 | + } |
|
| 334 | + } |
|
| 335 | + } |
|
| 336 | + } |
|
| 337 | + /** |
|
| 338 | + * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
| 339 | + */ |
|
| 340 | + else if (array_search($key, $model->getFillable(), true) !== false) |
|
| 341 | + { |
|
| 342 | + $model->$key = $value; |
|
| 343 | + } |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + /** |
|
| 347 | + * Loop through the relations array. |
|
| 348 | + */ |
|
| 349 | + foreach ($relations as $key => $value) |
|
| 350 | + { |
|
| 351 | + /** |
|
| 352 | + * If the relation is marked for delete then delete it. |
|
| 353 | + */ |
|
| 354 | + if ($value == 'delete' && $model->$key()->count()) |
|
| 355 | + { |
|
| 356 | + $model->$key()->delete(); |
|
| 357 | + } |
|
| 358 | + /** |
|
| 359 | + * If the relation is an array. |
|
| 360 | + */ |
|
| 361 | + else if (gettype($value) == 'array') |
|
| 362 | + { |
|
| 363 | + /** |
|
| 364 | + * Save the model. |
|
| 365 | + */ |
|
| 366 | + $model->save(); |
|
| 367 | + $ids = []; |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * Loop through the relations. |
|
| 371 | + */ |
|
| 372 | + foreach ($value as $val) |
|
| 373 | + { |
|
| 374 | + switch (class_basename($model->$key())) |
|
| 375 | + { |
|
| 376 | + /** |
|
| 377 | + * If the relation is one to many then update it's foreign key with |
|
| 378 | + * the model id and save it then add its id to ids array to delete all |
|
| 379 | + * relations who's id isn't in the ids array. |
|
| 380 | + */ |
|
| 381 | + case 'HasMany': |
|
| 382 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 383 | + $val->$foreignKeyName = $model->id; |
|
| 384 | + $val->save(); |
|
| 385 | + $ids[] = $val->id; |
|
| 386 | + break; |
|
| 387 | + |
|
| 388 | + /** |
|
| 389 | + * If the relation is many to many then add it's id to the ids array to |
|
| 390 | + * attache these ids to the model. |
|
| 391 | + */ |
|
| 392 | + case 'BelongsToMany': |
|
| 393 | + $val->save(); |
|
| 394 | + $ids[] = $val->id; |
|
| 395 | + break; |
|
| 396 | + } |
|
| 397 | + } |
|
| 398 | + switch (class_basename($model->$key())) |
|
| 399 | + { |
|
| 400 | + /** |
|
| 401 | + * If the relation is one to many then delete all |
|
| 402 | + * relations who's id isn't in the ids array. |
|
| 403 | + */ |
|
| 404 | + case 'HasMany': |
|
| 405 | + $model->$key()->whereNotIn('id', $ids)->delete(); |
|
| 406 | + break; |
|
| 407 | + |
|
| 408 | + /** |
|
| 409 | + * If the relation is many to many then |
|
| 410 | + * detach the previous data and attach |
|
| 411 | + * the ids array to the model. |
|
| 412 | + */ |
|
| 413 | + case 'BelongsToMany': |
|
| 414 | + $model->$key()->detach(); |
|
| 415 | + $model->$key()->attach($ids); |
|
| 416 | + break; |
|
| 417 | + } |
|
| 418 | + } |
|
| 419 | + /** |
|
| 420 | + * If the relation isn't array. |
|
| 421 | + */ |
|
| 422 | + else |
|
| 423 | + { |
|
| 424 | + switch (class_basename($model->$key())) |
|
| 425 | + { |
|
| 426 | + /** |
|
| 427 | + * If the relation is one to one. |
|
| 428 | + */ |
|
| 429 | + case 'HasOne': |
|
| 430 | + /** |
|
| 431 | + * Save the model. |
|
| 432 | + */ |
|
| 433 | + $model->save(); |
|
| 434 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 435 | + $value->$foreignKeyName = $model->id; |
|
| 436 | + $value->save(); |
|
| 437 | + break; |
|
| 438 | + case 'BelongsTo': |
|
| 439 | + /** |
|
| 440 | + * Save the model. |
|
| 441 | + */ |
|
| 442 | + $value->save(); |
|
| 443 | + $model->$key()->associate($value); |
|
| 444 | + break; |
|
| 445 | + } |
|
| 446 | + } |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + /** |
|
| 450 | + * Save the model. |
|
| 451 | + */ |
|
| 452 | + $model->save(); |
|
| 453 | + }); |
|
| 454 | 454 | |
| 455 | - return $model; |
|
| 456 | - } |
|
| 455 | + return $model; |
|
| 456 | + } |
|
| 457 | 457 | |
| 458 | - /** |
|
| 459 | - * Update record in the storage based on the given |
|
| 460 | - * condition. |
|
| 461 | - * |
|
| 462 | - * @param var $value condition value |
|
| 463 | - * @param array $data |
|
| 464 | - * @param string $attribute condition column name |
|
| 465 | - * @return void |
|
| 466 | - */ |
|
| 467 | - public function update($value, array $data, $attribute = 'id') |
|
| 468 | - { |
|
| 469 | - if ($attribute == 'id') |
|
| 470 | - { |
|
| 471 | - $model = $this->model->lockForUpdate()->find($value); |
|
| 472 | - $model ? $model->update($data) : 0; |
|
| 473 | - } |
|
| 474 | - else |
|
| 475 | - { |
|
| 476 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data){ |
|
| 477 | - $model->update($data); |
|
| 478 | - }); |
|
| 479 | - } |
|
| 480 | - } |
|
| 481 | - |
|
| 482 | - /** |
|
| 483 | - * Delete record from the storage based on the given |
|
| 484 | - * condition. |
|
| 485 | - * |
|
| 486 | - * @param var $value condition value |
|
| 487 | - * @param string $attribute condition column name |
|
| 488 | - * @return void |
|
| 489 | - */ |
|
| 490 | - public function delete($value, $attribute = 'id') |
|
| 491 | - { |
|
| 492 | - if ($attribute == 'id') |
|
| 493 | - { |
|
| 494 | - \DB::transaction(function () use ($value, $attribute, &$result) { |
|
| 495 | - $model = $this->model->lockForUpdate()->find($value); |
|
| 496 | - if ( ! $model) |
|
| 497 | - { |
|
| 498 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
| 499 | - } |
|
| 458 | + /** |
|
| 459 | + * Update record in the storage based on the given |
|
| 460 | + * condition. |
|
| 461 | + * |
|
| 462 | + * @param var $value condition value |
|
| 463 | + * @param array $data |
|
| 464 | + * @param string $attribute condition column name |
|
| 465 | + * @return void |
|
| 466 | + */ |
|
| 467 | + public function update($value, array $data, $attribute = 'id') |
|
| 468 | + { |
|
| 469 | + if ($attribute == 'id') |
|
| 470 | + { |
|
| 471 | + $model = $this->model->lockForUpdate()->find($value); |
|
| 472 | + $model ? $model->update($data) : 0; |
|
| 473 | + } |
|
| 474 | + else |
|
| 475 | + { |
|
| 476 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data){ |
|
| 477 | + $model->update($data); |
|
| 478 | + }); |
|
| 479 | + } |
|
| 480 | + } |
|
| 481 | + |
|
| 482 | + /** |
|
| 483 | + * Delete record from the storage based on the given |
|
| 484 | + * condition. |
|
| 485 | + * |
|
| 486 | + * @param var $value condition value |
|
| 487 | + * @param string $attribute condition column name |
|
| 488 | + * @return void |
|
| 489 | + */ |
|
| 490 | + public function delete($value, $attribute = 'id') |
|
| 491 | + { |
|
| 492 | + if ($attribute == 'id') |
|
| 493 | + { |
|
| 494 | + \DB::transaction(function () use ($value, $attribute, &$result) { |
|
| 495 | + $model = $this->model->lockForUpdate()->find($value); |
|
| 496 | + if ( ! $model) |
|
| 497 | + { |
|
| 498 | + \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
| 499 | + } |
|
| 500 | 500 | |
| 501 | - $model->delete(); |
|
| 502 | - }); |
|
| 503 | - } |
|
| 504 | - else |
|
| 505 | - { |
|
| 506 | - \DB::transaction(function () use ($value, $attribute, &$result) { |
|
| 507 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ |
|
| 508 | - $model->delete(); |
|
| 509 | - }); |
|
| 510 | - }); |
|
| 511 | - } |
|
| 512 | - } |
|
| 501 | + $model->delete(); |
|
| 502 | + }); |
|
| 503 | + } |
|
| 504 | + else |
|
| 505 | + { |
|
| 506 | + \DB::transaction(function () use ($value, $attribute, &$result) { |
|
| 507 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ |
|
| 508 | + $model->delete(); |
|
| 509 | + }); |
|
| 510 | + }); |
|
| 511 | + } |
|
| 512 | + } |
|
| 513 | 513 | |
| 514 | - /** |
|
| 515 | - * Fetch records from the storage based on the given |
|
| 516 | - * id. |
|
| 517 | - * |
|
| 518 | - * @param integer $id |
|
| 519 | - * @param array $relations |
|
| 520 | - * @param array $columns |
|
| 521 | - * @return object |
|
| 522 | - */ |
|
| 523 | - public function find($id, $relations = [], $columns = array('*')) |
|
| 524 | - { |
|
| 525 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
| 526 | - } |
|
| 514 | + /** |
|
| 515 | + * Fetch records from the storage based on the given |
|
| 516 | + * id. |
|
| 517 | + * |
|
| 518 | + * @param integer $id |
|
| 519 | + * @param array $relations |
|
| 520 | + * @param array $columns |
|
| 521 | + * @return object |
|
| 522 | + */ |
|
| 523 | + public function find($id, $relations = [], $columns = array('*')) |
|
| 524 | + { |
|
| 525 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
| 526 | + } |
|
| 527 | 527 | |
| 528 | - /** |
|
| 529 | - * Fetch records from the storage based on the given |
|
| 530 | - * condition. |
|
| 531 | - * |
|
| 532 | - * @param array $conditions array of conditions |
|
| 533 | - * @param array $relations |
|
| 534 | - * @param string $sortBy |
|
| 535 | - * @param boolean $desc |
|
| 536 | - * @param array $columns |
|
| 537 | - * @return collection |
|
| 538 | - */ |
|
| 539 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 540 | - { |
|
| 541 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 542 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 543 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - /** |
|
| 547 | - * Fetch the first record from the storage based on the given |
|
| 548 | - * condition. |
|
| 549 | - * |
|
| 550 | - * @param array $conditions array of conditions |
|
| 551 | - * @param array $relations |
|
| 552 | - * @param array $columns |
|
| 553 | - * @return object |
|
| 554 | - */ |
|
| 555 | - public function first($conditions, $relations = [], $columns = array('*')) |
|
| 556 | - { |
|
| 557 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 558 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - /** |
|
| 562 | - * Return the deleted models in pages based on the given conditions. |
|
| 563 | - * |
|
| 564 | - * @param array $conditions array of conditions |
|
| 565 | - * @param integer $perPage |
|
| 566 | - * @param string $sortBy |
|
| 567 | - * @param boolean $desc |
|
| 568 | - * @param array $columns |
|
| 569 | - * @return collection |
|
| 570 | - */ |
|
| 571 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 572 | - { |
|
| 573 | - unset($conditions['page']); |
|
| 574 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 575 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 576 | - $model = $this->model->onlyTrashed(); |
|
| 577 | - |
|
| 578 | - if (count($conditions['conditionValues'])) |
|
| 579 | - { |
|
| 580 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 581 | - } |
|
| 582 | - |
|
| 583 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);; |
|
| 584 | - } |
|
| 585 | - |
|
| 586 | - /** |
|
| 587 | - * Restore the deleted model. |
|
| 588 | - * |
|
| 589 | - * @param integer $id |
|
| 590 | - * @return void |
|
| 591 | - */ |
|
| 592 | - public function restore($id) |
|
| 593 | - { |
|
| 594 | - $model = $this->model->onlyTrashed()->find($id); |
|
| 595 | - |
|
| 596 | - if ( ! $model) |
|
| 597 | - { |
|
| 598 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id); |
|
| 599 | - } |
|
| 600 | - |
|
| 601 | - $model->restore(); |
|
| 602 | - } |
|
| 603 | - |
|
| 604 | - /** |
|
| 605 | - * Build the conditions recursively for the retrieving methods. |
|
| 606 | - * @param array $conditions |
|
| 607 | - * @return array |
|
| 608 | - */ |
|
| 609 | - protected function constructConditions($conditions, $model) |
|
| 610 | - { |
|
| 611 | - $conditionString = ''; |
|
| 612 | - $conditionValues = []; |
|
| 613 | - foreach ($conditions as $key => $value) |
|
| 614 | - { |
|
| 615 | - if (str_contains($key, '->')) |
|
| 616 | - { |
|
| 617 | - $key = $this->wrapJsonSelector($key); |
|
| 618 | - } |
|
| 619 | - |
|
| 620 | - if ($key == 'and') |
|
| 621 | - { |
|
| 622 | - $conditions = $this->constructConditions($value, $model); |
|
| 623 | - $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
|
| 624 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 625 | - } |
|
| 626 | - else if ($key == 'or') |
|
| 627 | - { |
|
| 628 | - $conditions = $this->constructConditions($value, $model); |
|
| 629 | - $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
|
| 630 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 631 | - } |
|
| 632 | - else |
|
| 633 | - { |
|
| 634 | - if (is_array($value)) |
|
| 635 | - { |
|
| 636 | - $operator = $value['op']; |
|
| 637 | - if (strtolower($operator) == 'between') |
|
| 638 | - { |
|
| 639 | - $value1 = $value['val1']; |
|
| 640 | - $value2 = $value['val2']; |
|
| 641 | - } |
|
| 642 | - else |
|
| 643 | - { |
|
| 644 | - $value = array_key_exists('val', $value) ? $value['val'] : ''; |
|
| 645 | - } |
|
| 646 | - } |
|
| 647 | - else |
|
| 648 | - { |
|
| 649 | - $operator = '='; |
|
| 650 | - } |
|
| 528 | + /** |
|
| 529 | + * Fetch records from the storage based on the given |
|
| 530 | + * condition. |
|
| 531 | + * |
|
| 532 | + * @param array $conditions array of conditions |
|
| 533 | + * @param array $relations |
|
| 534 | + * @param string $sortBy |
|
| 535 | + * @param boolean $desc |
|
| 536 | + * @param array $columns |
|
| 537 | + * @return collection |
|
| 538 | + */ |
|
| 539 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 540 | + { |
|
| 541 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 542 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 543 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + /** |
|
| 547 | + * Fetch the first record from the storage based on the given |
|
| 548 | + * condition. |
|
| 549 | + * |
|
| 550 | + * @param array $conditions array of conditions |
|
| 551 | + * @param array $relations |
|
| 552 | + * @param array $columns |
|
| 553 | + * @return object |
|
| 554 | + */ |
|
| 555 | + public function first($conditions, $relations = [], $columns = array('*')) |
|
| 556 | + { |
|
| 557 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 558 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + /** |
|
| 562 | + * Return the deleted models in pages based on the given conditions. |
|
| 563 | + * |
|
| 564 | + * @param array $conditions array of conditions |
|
| 565 | + * @param integer $perPage |
|
| 566 | + * @param string $sortBy |
|
| 567 | + * @param boolean $desc |
|
| 568 | + * @param array $columns |
|
| 569 | + * @return collection |
|
| 570 | + */ |
|
| 571 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 572 | + { |
|
| 573 | + unset($conditions['page']); |
|
| 574 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 575 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 576 | + $model = $this->model->onlyTrashed(); |
|
| 577 | + |
|
| 578 | + if (count($conditions['conditionValues'])) |
|
| 579 | + { |
|
| 580 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 581 | + } |
|
| 582 | + |
|
| 583 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);; |
|
| 584 | + } |
|
| 585 | + |
|
| 586 | + /** |
|
| 587 | + * Restore the deleted model. |
|
| 588 | + * |
|
| 589 | + * @param integer $id |
|
| 590 | + * @return void |
|
| 591 | + */ |
|
| 592 | + public function restore($id) |
|
| 593 | + { |
|
| 594 | + $model = $this->model->onlyTrashed()->find($id); |
|
| 595 | + |
|
| 596 | + if ( ! $model) |
|
| 597 | + { |
|
| 598 | + \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id); |
|
| 599 | + } |
|
| 600 | + |
|
| 601 | + $model->restore(); |
|
| 602 | + } |
|
| 603 | + |
|
| 604 | + /** |
|
| 605 | + * Build the conditions recursively for the retrieving methods. |
|
| 606 | + * @param array $conditions |
|
| 607 | + * @return array |
|
| 608 | + */ |
|
| 609 | + protected function constructConditions($conditions, $model) |
|
| 610 | + { |
|
| 611 | + $conditionString = ''; |
|
| 612 | + $conditionValues = []; |
|
| 613 | + foreach ($conditions as $key => $value) |
|
| 614 | + { |
|
| 615 | + if (str_contains($key, '->')) |
|
| 616 | + { |
|
| 617 | + $key = $this->wrapJsonSelector($key); |
|
| 618 | + } |
|
| 619 | + |
|
| 620 | + if ($key == 'and') |
|
| 621 | + { |
|
| 622 | + $conditions = $this->constructConditions($value, $model); |
|
| 623 | + $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
|
| 624 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 625 | + } |
|
| 626 | + else if ($key == 'or') |
|
| 627 | + { |
|
| 628 | + $conditions = $this->constructConditions($value, $model); |
|
| 629 | + $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
|
| 630 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 631 | + } |
|
| 632 | + else |
|
| 633 | + { |
|
| 634 | + if (is_array($value)) |
|
| 635 | + { |
|
| 636 | + $operator = $value['op']; |
|
| 637 | + if (strtolower($operator) == 'between') |
|
| 638 | + { |
|
| 639 | + $value1 = $value['val1']; |
|
| 640 | + $value2 = $value['val2']; |
|
| 641 | + } |
|
| 642 | + else |
|
| 643 | + { |
|
| 644 | + $value = array_key_exists('val', $value) ? $value['val'] : ''; |
|
| 645 | + } |
|
| 646 | + } |
|
| 647 | + else |
|
| 648 | + { |
|
| 649 | + $operator = '='; |
|
| 650 | + } |
|
| 651 | 651 | |
| 652 | - if (strtolower($operator) == 'between') |
|
| 653 | - { |
|
| 654 | - $conditionString .= $key . ' >= ? and '; |
|
| 655 | - $conditionValues[] = $value1; |
|
| 656 | - |
|
| 657 | - $conditionString .= $key . ' <= ? {op} '; |
|
| 658 | - $conditionValues[] = $value2; |
|
| 659 | - } |
|
| 660 | - elseif (strtolower($operator) == 'in') |
|
| 661 | - { |
|
| 662 | - $conditionValues = array_merge($conditionValues, $value); |
|
| 663 | - $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
| 664 | - $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
|
| 665 | - } |
|
| 666 | - elseif (strtolower($operator) == 'null') |
|
| 667 | - { |
|
| 668 | - $conditionString .= $key . ' is null {op} '; |
|
| 669 | - } |
|
| 670 | - elseif (strtolower($operator) == 'not null') |
|
| 671 | - { |
|
| 672 | - $conditionString .= $key . ' is not null {op} '; |
|
| 673 | - } |
|
| 674 | - elseif (strtolower($operator) == 'has') |
|
| 675 | - { |
|
| 676 | - $sql = $model->withTrashed()->has($key)->toSql(); |
|
| 677 | - $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
| 678 | - $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
|
| 679 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 680 | - } |
|
| 681 | - else |
|
| 682 | - { |
|
| 683 | - $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
|
| 684 | - $conditionValues[] = $value; |
|
| 685 | - } |
|
| 686 | - } |
|
| 687 | - } |
|
| 688 | - $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
| 689 | - return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
| 690 | - } |
|
| 691 | - |
|
| 692 | - /** |
|
| 693 | - * Wrap the given JSON selector. |
|
| 694 | - * |
|
| 695 | - * @param string $value |
|
| 696 | - * @return string |
|
| 697 | - */ |
|
| 698 | - protected function wrapJsonSelector($value) |
|
| 699 | - { |
|
| 700 | - $removeLast = strpos($value, ')'); |
|
| 701 | - $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
| 702 | - $path = explode('->', $value); |
|
| 703 | - $field = array_shift($path); |
|
| 704 | - $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
| 705 | - return '"'.$part.'"'; |
|
| 706 | - })->implode('.')); |
|
| 652 | + if (strtolower($operator) == 'between') |
|
| 653 | + { |
|
| 654 | + $conditionString .= $key . ' >= ? and '; |
|
| 655 | + $conditionValues[] = $value1; |
|
| 656 | + |
|
| 657 | + $conditionString .= $key . ' <= ? {op} '; |
|
| 658 | + $conditionValues[] = $value2; |
|
| 659 | + } |
|
| 660 | + elseif (strtolower($operator) == 'in') |
|
| 661 | + { |
|
| 662 | + $conditionValues = array_merge($conditionValues, $value); |
|
| 663 | + $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
| 664 | + $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
|
| 665 | + } |
|
| 666 | + elseif (strtolower($operator) == 'null') |
|
| 667 | + { |
|
| 668 | + $conditionString .= $key . ' is null {op} '; |
|
| 669 | + } |
|
| 670 | + elseif (strtolower($operator) == 'not null') |
|
| 671 | + { |
|
| 672 | + $conditionString .= $key . ' is not null {op} '; |
|
| 673 | + } |
|
| 674 | + elseif (strtolower($operator) == 'has') |
|
| 675 | + { |
|
| 676 | + $sql = $model->withTrashed()->has($key)->toSql(); |
|
| 677 | + $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
| 678 | + $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
|
| 679 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 680 | + } |
|
| 681 | + else |
|
| 682 | + { |
|
| 683 | + $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
|
| 684 | + $conditionValues[] = $value; |
|
| 685 | + } |
|
| 686 | + } |
|
| 687 | + } |
|
| 688 | + $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
| 689 | + return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
| 690 | + } |
|
| 691 | + |
|
| 692 | + /** |
|
| 693 | + * Wrap the given JSON selector. |
|
| 694 | + * |
|
| 695 | + * @param string $value |
|
| 696 | + * @return string |
|
| 697 | + */ |
|
| 698 | + protected function wrapJsonSelector($value) |
|
| 699 | + { |
|
| 700 | + $removeLast = strpos($value, ')'); |
|
| 701 | + $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
| 702 | + $path = explode('->', $value); |
|
| 703 | + $field = array_shift($path); |
|
| 704 | + $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
| 705 | + return '"'.$part.'"'; |
|
| 706 | + })->implode('.')); |
|
| 707 | 707 | |
| 708 | - return $removeLast === false ? $result : $result . ')'; |
|
| 709 | - } |
|
| 710 | - |
|
| 711 | - /** |
|
| 712 | - * Abstract method that return the necessary |
|
| 713 | - * information (full model namespace) |
|
| 714 | - * needed to preform the previous actions. |
|
| 715 | - * |
|
| 716 | - * @return string |
|
| 717 | - */ |
|
| 718 | - abstract protected function getModel(); |
|
| 708 | + return $removeLast === false ? $result : $result . ')'; |
|
| 709 | + } |
|
| 710 | + |
|
| 711 | + /** |
|
| 712 | + * Abstract method that return the necessary |
|
| 713 | + * information (full model namespace) |
|
| 714 | + * needed to preform the previous actions. |
|
| 715 | + * |
|
| 716 | + * @return string |
|
| 717 | + */ |
|
| 718 | + abstract protected function getModel(); |
|
| 719 | 719 | } |
| 720 | 720 | \ No newline at end of file |
@@ -6,282 +6,282 @@ |
||
| 6 | 6 | |
| 7 | 7 | class BaseApiController extends Controller |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * The config implementation. |
|
| 11 | - * |
|
| 12 | - * @var array |
|
| 13 | - */ |
|
| 14 | - protected $config; |
|
| 9 | + /** |
|
| 10 | + * The config implementation. |
|
| 11 | + * |
|
| 12 | + * @var array |
|
| 13 | + */ |
|
| 14 | + protected $config; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * The relations implementation. |
|
| 18 | - * |
|
| 19 | - * @var array |
|
| 20 | - */ |
|
| 21 | - protected $relations; |
|
| 16 | + /** |
|
| 17 | + * The relations implementation. |
|
| 18 | + * |
|
| 19 | + * @var array |
|
| 20 | + */ |
|
| 21 | + protected $relations; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * The repo implementation. |
|
| 25 | - * |
|
| 26 | - * @var object |
|
| 27 | - */ |
|
| 28 | - protected $repo; |
|
| 23 | + /** |
|
| 24 | + * The repo implementation. |
|
| 25 | + * |
|
| 26 | + * @var object |
|
| 27 | + */ |
|
| 28 | + protected $repo; |
|
| 29 | 29 | |
| 30 | - public function __construct() |
|
| 31 | - { |
|
| 32 | - $this->config = \CoreConfig::getConfig(); |
|
| 33 | - $this->model = property_exists($this, 'model') ? $this->model : false; |
|
| 34 | - $this->validationRules = property_exists($this, 'validationRules') ? $this->validationRules : false; |
|
| 35 | - $this->skipPermissionCheck = property_exists($this, 'skipPermissionCheck') ? $this->skipPermissionCheck : []; |
|
| 36 | - $this->skipLoginCheck = property_exists($this, 'skipLoginCheck') ? $this->skipLoginCheck : []; |
|
| 37 | - $route = explode('@',\Route::currentRouteAction())[1]; |
|
| 30 | + public function __construct() |
|
| 31 | + { |
|
| 32 | + $this->config = \CoreConfig::getConfig(); |
|
| 33 | + $this->model = property_exists($this, 'model') ? $this->model : false; |
|
| 34 | + $this->validationRules = property_exists($this, 'validationRules') ? $this->validationRules : false; |
|
| 35 | + $this->skipPermissionCheck = property_exists($this, 'skipPermissionCheck') ? $this->skipPermissionCheck : []; |
|
| 36 | + $this->skipLoginCheck = property_exists($this, 'skipLoginCheck') ? $this->skipLoginCheck : []; |
|
| 37 | + $route = explode('@',\Route::currentRouteAction())[1]; |
|
| 38 | 38 | |
| 39 | - $this->middleware(function ($request, $next) { |
|
| 39 | + $this->middleware(function ($request, $next) { |
|
| 40 | 40 | |
| 41 | - $this->repo = call_user_func_array("\Core::{$this->model}", []); |
|
| 42 | - return $next($request); |
|
| 43 | - }); |
|
| 41 | + $this->repo = call_user_func_array("\Core::{$this->model}", []); |
|
| 42 | + return $next($request); |
|
| 43 | + }); |
|
| 44 | 44 | |
| 45 | - $this->checkPermission($route); |
|
| 46 | - $this->setRelations($route); |
|
| 47 | - $this->setSessions(); |
|
| 48 | - } |
|
| 45 | + $this->checkPermission($route); |
|
| 46 | + $this->setRelations($route); |
|
| 47 | + $this->setSessions(); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Fetch all records with relations from storage. |
|
| 52 | - * |
|
| 53 | - * @param string $sortBy The name of the column to sort by. |
|
| 54 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
| 55 | - * @return \Illuminate\Http\Response |
|
| 56 | - */ |
|
| 57 | - public function index($sortBy = 'created_at', $desc = 1) |
|
| 58 | - { |
|
| 59 | - return \Response::json($this->repo->all($this->relations, $sortBy, $desc), 200); |
|
| 60 | - } |
|
| 50 | + /** |
|
| 51 | + * Fetch all records with relations from storage. |
|
| 52 | + * |
|
| 53 | + * @param string $sortBy The name of the column to sort by. |
|
| 54 | + * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
| 55 | + * @return \Illuminate\Http\Response |
|
| 56 | + */ |
|
| 57 | + public function index($sortBy = 'created_at', $desc = 1) |
|
| 58 | + { |
|
| 59 | + return \Response::json($this->repo->all($this->relations, $sortBy, $desc), 200); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Fetch the single object with relations from storage. |
|
| 64 | - * |
|
| 65 | - * @param integer $id Id of the requested model. |
|
| 66 | - * @return \Illuminate\Http\Response |
|
| 67 | - */ |
|
| 68 | - public function find($id) |
|
| 69 | - { |
|
| 70 | - return \Response::json($this->repo->find($id, $this->relations), 200); |
|
| 71 | - } |
|
| 62 | + /** |
|
| 63 | + * Fetch the single object with relations from storage. |
|
| 64 | + * |
|
| 65 | + * @param integer $id Id of the requested model. |
|
| 66 | + * @return \Illuminate\Http\Response |
|
| 67 | + */ |
|
| 68 | + public function find($id) |
|
| 69 | + { |
|
| 70 | + return \Response::json($this->repo->find($id, $this->relations), 200); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * Paginate all records with relations from storage |
|
| 75 | - * that matche the given query. |
|
| 76 | - * |
|
| 77 | - * @param string $query The search text. |
|
| 78 | - * @param integer $perPage Number of rows per page default 15. |
|
| 79 | - * @param string $sortBy The name of the column to sort by. |
|
| 80 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
| 81 | - * @return \Illuminate\Http\Response |
|
| 82 | - */ |
|
| 83 | - public function search($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
| 84 | - { |
|
| 85 | - return \Response::json($this->repo->search($query, $perPage, $this->relations, $sortBy, $desc), 200); |
|
| 86 | - } |
|
| 73 | + /** |
|
| 74 | + * Paginate all records with relations from storage |
|
| 75 | + * that matche the given query. |
|
| 76 | + * |
|
| 77 | + * @param string $query The search text. |
|
| 78 | + * @param integer $perPage Number of rows per page default 15. |
|
| 79 | + * @param string $sortBy The name of the column to sort by. |
|
| 80 | + * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
| 81 | + * @return \Illuminate\Http\Response |
|
| 82 | + */ |
|
| 83 | + public function search($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
| 84 | + { |
|
| 85 | + return \Response::json($this->repo->search($query, $perPage, $this->relations, $sortBy, $desc), 200); |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * Fetch records from the storage based on the given |
|
| 90 | - * condition. |
|
| 91 | - * |
|
| 92 | - * @param \Illuminate\Http\Request $request |
|
| 93 | - * @param string $sortBy The name of the column to sort by. |
|
| 94 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
| 95 | - * @return \Illuminate\Http\Response |
|
| 96 | - */ |
|
| 97 | - public function findby(Request $request, $sortBy = 'created_at', $desc = 1) |
|
| 98 | - { |
|
| 99 | - return \Response::json($this->repo->findBy($request->all(), $this->relations, $sortBy, $desc), 200); |
|
| 100 | - } |
|
| 88 | + /** |
|
| 89 | + * Fetch records from the storage based on the given |
|
| 90 | + * condition. |
|
| 91 | + * |
|
| 92 | + * @param \Illuminate\Http\Request $request |
|
| 93 | + * @param string $sortBy The name of the column to sort by. |
|
| 94 | + * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
| 95 | + * @return \Illuminate\Http\Response |
|
| 96 | + */ |
|
| 97 | + public function findby(Request $request, $sortBy = 'created_at', $desc = 1) |
|
| 98 | + { |
|
| 99 | + return \Response::json($this->repo->findBy($request->all(), $this->relations, $sortBy, $desc), 200); |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * Fetch the first record from the storage based on the given |
|
| 104 | - * condition. |
|
| 105 | - * |
|
| 106 | - * @param \Illuminate\Http\Request $request |
|
| 107 | - * @return \Illuminate\Http\Response |
|
| 108 | - */ |
|
| 109 | - public function first(Request $request) |
|
| 110 | - { |
|
| 111 | - return \Response::json($this->repo->first($request->all(), $this->relations), 200); |
|
| 112 | - } |
|
| 102 | + /** |
|
| 103 | + * Fetch the first record from the storage based on the given |
|
| 104 | + * condition. |
|
| 105 | + * |
|
| 106 | + * @param \Illuminate\Http\Request $request |
|
| 107 | + * @return \Illuminate\Http\Response |
|
| 108 | + */ |
|
| 109 | + public function first(Request $request) |
|
| 110 | + { |
|
| 111 | + return \Response::json($this->repo->first($request->all(), $this->relations), 200); |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - /** |
|
| 115 | - * Paginate all records with relations from storage. |
|
| 116 | - * |
|
| 117 | - * @param integer $perPage Number of rows per page default 15. |
|
| 118 | - * @param string $sortBy The name of the column to sort by. |
|
| 119 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
| 120 | - * @return \Illuminate\Http\Response |
|
| 121 | - */ |
|
| 122 | - public function paginate($perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
| 123 | - { |
|
| 124 | - return \Response::json($this->repo->paginate($perPage, $this->relations, $sortBy, $desc), 200); |
|
| 125 | - } |
|
| 114 | + /** |
|
| 115 | + * Paginate all records with relations from storage. |
|
| 116 | + * |
|
| 117 | + * @param integer $perPage Number of rows per page default 15. |
|
| 118 | + * @param string $sortBy The name of the column to sort by. |
|
| 119 | + * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
| 120 | + * @return \Illuminate\Http\Response |
|
| 121 | + */ |
|
| 122 | + public function paginate($perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
| 123 | + { |
|
| 124 | + return \Response::json($this->repo->paginate($perPage, $this->relations, $sortBy, $desc), 200); |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | - /** |
|
| 128 | - * Fetch all records with relations based on |
|
| 129 | - * the given condition from storage in pages. |
|
| 130 | - * |
|
| 131 | - * @param \Illuminate\Http\Request $request |
|
| 132 | - * @param integer $perPage Number of rows per page default 15. |
|
| 133 | - * @param string $sortBy The name of the column to sort by. |
|
| 134 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
| 135 | - * @return \Illuminate\Http\Response |
|
| 136 | - */ |
|
| 137 | - public function paginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
| 138 | - { |
|
| 139 | - return \Response::json($this->repo->paginateBy($request->all(), $perPage, $this->relations, $sortBy, $desc), 200); |
|
| 140 | - } |
|
| 127 | + /** |
|
| 128 | + * Fetch all records with relations based on |
|
| 129 | + * the given condition from storage in pages. |
|
| 130 | + * |
|
| 131 | + * @param \Illuminate\Http\Request $request |
|
| 132 | + * @param integer $perPage Number of rows per page default 15. |
|
| 133 | + * @param string $sortBy The name of the column to sort by. |
|
| 134 | + * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
| 135 | + * @return \Illuminate\Http\Response |
|
| 136 | + */ |
|
| 137 | + public function paginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
| 138 | + { |
|
| 139 | + return \Response::json($this->repo->paginateBy($request->all(), $perPage, $this->relations, $sortBy, $desc), 200); |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - /** |
|
| 143 | - * Save the given model to storage. |
|
| 144 | - * |
|
| 145 | - * @param \Illuminate\Http\Request $request |
|
| 146 | - * @return \Illuminate\Http\Response |
|
| 147 | - */ |
|
| 148 | - public function save(Request $request) |
|
| 149 | - { |
|
| 150 | - foreach ($this->validationRules as &$rule) |
|
| 151 | - { |
|
| 152 | - if (strpos($rule, 'exists') && ! strpos($rule, 'deleted_at,NULL')) |
|
| 153 | - { |
|
| 154 | - $rule .= ',deleted_at,NULL'; |
|
| 155 | - } |
|
| 142 | + /** |
|
| 143 | + * Save the given model to storage. |
|
| 144 | + * |
|
| 145 | + * @param \Illuminate\Http\Request $request |
|
| 146 | + * @return \Illuminate\Http\Response |
|
| 147 | + */ |
|
| 148 | + public function save(Request $request) |
|
| 149 | + { |
|
| 150 | + foreach ($this->validationRules as &$rule) |
|
| 151 | + { |
|
| 152 | + if (strpos($rule, 'exists') && ! strpos($rule, 'deleted_at,NULL')) |
|
| 153 | + { |
|
| 154 | + $rule .= ',deleted_at,NULL'; |
|
| 155 | + } |
|
| 156 | 156 | |
| 157 | - if ($request->has('id')) |
|
| 158 | - { |
|
| 159 | - $rule = str_replace('{id}', $request->get('id'), $rule); |
|
| 160 | - } |
|
| 161 | - else |
|
| 162 | - { |
|
| 163 | - $rule = str_replace(',{id}', '', $rule); |
|
| 164 | - } |
|
| 165 | - } |
|
| 157 | + if ($request->has('id')) |
|
| 158 | + { |
|
| 159 | + $rule = str_replace('{id}', $request->get('id'), $rule); |
|
| 160 | + } |
|
| 161 | + else |
|
| 162 | + { |
|
| 163 | + $rule = str_replace(',{id}', '', $rule); |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - $this->validate($request, $this->validationRules); |
|
| 167 | + $this->validate($request, $this->validationRules); |
|
| 168 | 168 | |
| 169 | - return \Response::json($this->repo->save($request->all()), 200); |
|
| 170 | - } |
|
| 169 | + return \Response::json($this->repo->save($request->all()), 200); |
|
| 170 | + } |
|
| 171 | 171 | |
| 172 | - /** |
|
| 173 | - * Delete by the given id from storage. |
|
| 174 | - * |
|
| 175 | - * @param integer $id Id of the deleted model. |
|
| 176 | - * @return \Illuminate\Http\Response |
|
| 177 | - */ |
|
| 178 | - public function delete($id) |
|
| 179 | - { |
|
| 180 | - return \Response::json($this->repo->delete($id), 200); |
|
| 181 | - } |
|
| 172 | + /** |
|
| 173 | + * Delete by the given id from storage. |
|
| 174 | + * |
|
| 175 | + * @param integer $id Id of the deleted model. |
|
| 176 | + * @return \Illuminate\Http\Response |
|
| 177 | + */ |
|
| 178 | + public function delete($id) |
|
| 179 | + { |
|
| 180 | + return \Response::json($this->repo->delete($id), 200); |
|
| 181 | + } |
|
| 182 | 182 | |
| 183 | - /** |
|
| 184 | - * Return the deleted models in pages based on the given conditions. |
|
| 185 | - * |
|
| 186 | - * @param \Illuminate\Http\Request $request |
|
| 187 | - * @param integer $perPage Number of rows per page default 15. |
|
| 188 | - * @param string $sortBy The name of the column to sort by. |
|
| 189 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
| 190 | - * @return \Illuminate\Http\Response |
|
| 191 | - */ |
|
| 192 | - public function deleted(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
| 193 | - { |
|
| 194 | - return \Response::json($this->repo->deleted($request->all(), $perPage, $sortBy, $desc), 200); |
|
| 195 | - } |
|
| 183 | + /** |
|
| 184 | + * Return the deleted models in pages based on the given conditions. |
|
| 185 | + * |
|
| 186 | + * @param \Illuminate\Http\Request $request |
|
| 187 | + * @param integer $perPage Number of rows per page default 15. |
|
| 188 | + * @param string $sortBy The name of the column to sort by. |
|
| 189 | + * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
| 190 | + * @return \Illuminate\Http\Response |
|
| 191 | + */ |
|
| 192 | + public function deleted(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
| 193 | + { |
|
| 194 | + return \Response::json($this->repo->deleted($request->all(), $perPage, $sortBy, $desc), 200); |
|
| 195 | + } |
|
| 196 | 196 | |
| 197 | - /** |
|
| 198 | - * Restore the deleted model. |
|
| 199 | - * |
|
| 200 | - * @param integer $id Id of the restored model. |
|
| 201 | - * @return \Illuminate\Http\Response |
|
| 202 | - */ |
|
| 203 | - public function restore($id) |
|
| 204 | - { |
|
| 205 | - return \Response::json($this->repo->restore($id), 200); |
|
| 206 | - } |
|
| 197 | + /** |
|
| 198 | + * Restore the deleted model. |
|
| 199 | + * |
|
| 200 | + * @param integer $id Id of the restored model. |
|
| 201 | + * @return \Illuminate\Http\Response |
|
| 202 | + */ |
|
| 203 | + public function restore($id) |
|
| 204 | + { |
|
| 205 | + return \Response::json($this->repo->restore($id), 200); |
|
| 206 | + } |
|
| 207 | 207 | |
| 208 | - /** |
|
| 209 | - * Check if the logged in user can do the given permission. |
|
| 210 | - * |
|
| 211 | - * @param string $permission |
|
| 212 | - * @return void |
|
| 213 | - */ |
|
| 214 | - private function checkPermission($permission) |
|
| 215 | - { |
|
| 216 | - \Auth::shouldUse('api'); |
|
| 217 | - $this->middleware('auth:api', ['except' => $this->skipLoginCheck]); |
|
| 208 | + /** |
|
| 209 | + * Check if the logged in user can do the given permission. |
|
| 210 | + * |
|
| 211 | + * @param string $permission |
|
| 212 | + * @return void |
|
| 213 | + */ |
|
| 214 | + private function checkPermission($permission) |
|
| 215 | + { |
|
| 216 | + \Auth::shouldUse('api'); |
|
| 217 | + $this->middleware('auth:api', ['except' => $this->skipLoginCheck]); |
|
| 218 | 218 | |
| 219 | - if ($user = \Auth::user()) |
|
| 220 | - { |
|
| 221 | - $permission = $permission !== 'index' ? $permission : 'list'; |
|
| 222 | - $isPasswordClient = $user->token()->client->password_client; |
|
| 219 | + if ($user = \Auth::user()) |
|
| 220 | + { |
|
| 221 | + $permission = $permission !== 'index' ? $permission : 'list'; |
|
| 222 | + $isPasswordClient = $user->token()->client->password_client; |
|
| 223 | 223 | |
| 224 | - if ($user->blocked) |
|
| 225 | - { |
|
| 226 | - \ErrorHandler::userIsBlocked(); |
|
| 227 | - } |
|
| 224 | + if ($user->blocked) |
|
| 225 | + { |
|
| 226 | + \ErrorHandler::userIsBlocked(); |
|
| 227 | + } |
|
| 228 | 228 | |
| 229 | - if ($isPasswordClient && (in_array($permission, $this->skipPermissionCheck) || \Core::users()->can($permission, $this->model))) |
|
| 230 | - {} |
|
| 231 | - elseif ( ! $isPasswordClient && $user->tokenCan($this->model . '-' . $permission)) |
|
| 232 | - {} |
|
| 233 | - else |
|
| 234 | - { |
|
| 229 | + if ($isPasswordClient && (in_array($permission, $this->skipPermissionCheck) || \Core::users()->can($permission, $this->model))) |
|
| 230 | + {} |
|
| 231 | + elseif ( ! $isPasswordClient && $user->tokenCan($this->model . '-' . $permission)) |
|
| 232 | + {} |
|
| 233 | + else |
|
| 234 | + { |
|
| 235 | 235 | |
| 236 | - \ErrorHandler::noPermissions(); |
|
| 237 | - } |
|
| 238 | - } |
|
| 239 | - } |
|
| 236 | + \ErrorHandler::noPermissions(); |
|
| 237 | + } |
|
| 238 | + } |
|
| 239 | + } |
|
| 240 | 240 | |
| 241 | - /** |
|
| 242 | - * Set sessions based on the given headers in the request. |
|
| 243 | - * |
|
| 244 | - * @return void |
|
| 245 | - */ |
|
| 246 | - private function setSessions() |
|
| 247 | - { |
|
| 248 | - \Session::put('timeZoneDiff', \Request::header('time-zone-diff') ?: 0); |
|
| 241 | + /** |
|
| 242 | + * Set sessions based on the given headers in the request. |
|
| 243 | + * |
|
| 244 | + * @return void |
|
| 245 | + */ |
|
| 246 | + private function setSessions() |
|
| 247 | + { |
|
| 248 | + \Session::put('timeZoneDiff', \Request::header('time-zone-diff') ?: 0); |
|
| 249 | 249 | |
| 250 | - $locale = \Request::header('locale'); |
|
| 251 | - switch ($locale) |
|
| 252 | - { |
|
| 253 | - case 'en': |
|
| 254 | - \App::setLocale('en'); |
|
| 255 | - \Session::put('locale', 'en'); |
|
| 256 | - break; |
|
| 250 | + $locale = \Request::header('locale'); |
|
| 251 | + switch ($locale) |
|
| 252 | + { |
|
| 253 | + case 'en': |
|
| 254 | + \App::setLocale('en'); |
|
| 255 | + \Session::put('locale', 'en'); |
|
| 256 | + break; |
|
| 257 | 257 | |
| 258 | - case 'ar': |
|
| 259 | - \App::setLocale('ar'); |
|
| 260 | - \Session::put('locale', 'ar'); |
|
| 261 | - break; |
|
| 258 | + case 'ar': |
|
| 259 | + \App::setLocale('ar'); |
|
| 260 | + \Session::put('locale', 'ar'); |
|
| 261 | + break; |
|
| 262 | 262 | |
| 263 | - case 'all': |
|
| 264 | - \App::setLocale('en'); |
|
| 265 | - \Session::put('locale', 'all'); |
|
| 266 | - break; |
|
| 263 | + case 'all': |
|
| 264 | + \App::setLocale('en'); |
|
| 265 | + \Session::put('locale', 'all'); |
|
| 266 | + break; |
|
| 267 | 267 | |
| 268 | - default: |
|
| 269 | - \App::setLocale('en'); |
|
| 270 | - \Session::put('locale', 'en'); |
|
| 271 | - break; |
|
| 272 | - } |
|
| 273 | - } |
|
| 268 | + default: |
|
| 269 | + \App::setLocale('en'); |
|
| 270 | + \Session::put('locale', 'en'); |
|
| 271 | + break; |
|
| 272 | + } |
|
| 273 | + } |
|
| 274 | 274 | |
| 275 | - /** |
|
| 276 | - * Set relation based on the called api. |
|
| 277 | - * |
|
| 278 | - * @param string $route |
|
| 279 | - * @return void |
|
| 280 | - */ |
|
| 281 | - private function setRelations($route) |
|
| 282 | - { |
|
| 283 | - $route = $route !== 'index' ? $route : 'list'; |
|
| 284 | - $relations = array_key_exists($this->model, $this->config['relations']) ? $this->config['relations'][$this->model] : false; |
|
| 285 | - $this->relations = $relations && isset($relations[$route]) ? $relations[$route] : []; |
|
| 286 | - } |
|
| 275 | + /** |
|
| 276 | + * Set relation based on the called api. |
|
| 277 | + * |
|
| 278 | + * @param string $route |
|
| 279 | + * @return void |
|
| 280 | + */ |
|
| 281 | + private function setRelations($route) |
|
| 282 | + { |
|
| 283 | + $route = $route !== 'index' ? $route : 'list'; |
|
| 284 | + $relations = array_key_exists($this->model, $this->config['relations']) ? $this->config['relations'][$this->model] : false; |
|
| 285 | + $this->relations = $relations && isset($relations[$route]) ? $relations[$route] : []; |
|
| 286 | + } |
|
| 287 | 287 | } |
@@ -34,9 +34,9 @@ discard block |
||
| 34 | 34 | $this->validationRules = property_exists($this, 'validationRules') ? $this->validationRules : false; |
| 35 | 35 | $this->skipPermissionCheck = property_exists($this, 'skipPermissionCheck') ? $this->skipPermissionCheck : []; |
| 36 | 36 | $this->skipLoginCheck = property_exists($this, 'skipLoginCheck') ? $this->skipLoginCheck : []; |
| 37 | - $route = explode('@',\Route::currentRouteAction())[1]; |
|
| 37 | + $route = explode('@', \Route::currentRouteAction())[1]; |
|
| 38 | 38 | |
| 39 | - $this->middleware(function ($request, $next) { |
|
| 39 | + $this->middleware(function($request, $next) { |
|
| 40 | 40 | |
| 41 | 41 | $this->repo = call_user_func_array("\Core::{$this->model}", []); |
| 42 | 42 | return $next($request); |
@@ -228,7 +228,7 @@ discard block |
||
| 228 | 228 | |
| 229 | 229 | if ($isPasswordClient && (in_array($permission, $this->skipPermissionCheck) || \Core::users()->can($permission, $this->model))) |
| 230 | 230 | {} |
| 231 | - elseif ( ! $isPasswordClient && $user->tokenCan($this->model . '-' . $permission)) |
|
| 231 | + elseif ( ! $isPasswordClient && $user->tokenCan($this->model.'-'.$permission)) |
|
| 232 | 232 | {} |
| 233 | 233 | else |
| 234 | 234 | { |
@@ -8,31 +8,31 @@ |
||
| 8 | 8 | |
| 9 | 9 | class SettingsController 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 = 'settings'; |
|
| 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 = 'settings'; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * The validations rules used by the base api controller |
|
| 20 | - * to check before add. |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - protected $validationRules = [ |
|
| 24 | - 'name' => 'required|string|max:100', |
|
| 25 | - 'value' => 'required|string' |
|
| 26 | - ]; |
|
| 18 | + /** |
|
| 19 | + * The validations rules used by the base api controller |
|
| 20 | + * to check before add. |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + protected $validationRules = [ |
|
| 24 | + 'name' => 'required|string|max:100', |
|
| 25 | + 'value' => 'required|string' |
|
| 26 | + ]; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Save list of settings. |
|
| 30 | - * |
|
| 31 | - * @param \Illuminate\Http\Request $request |
|
| 32 | - * @return \Illuminate\Http\Response |
|
| 33 | - */ |
|
| 34 | - public function saveMany(Request $request) |
|
| 35 | - { |
|
| 36 | - return \Response::json($this->repo->saveMany($request->all()), 200); |
|
| 37 | - } |
|
| 28 | + /** |
|
| 29 | + * Save list of settings. |
|
| 30 | + * |
|
| 31 | + * @param \Illuminate\Http\Request $request |
|
| 32 | + * @return \Illuminate\Http\Response |
|
| 33 | + */ |
|
| 34 | + public function saveMany(Request $request) |
|
| 35 | + { |
|
| 36 | + return \Response::json($this->repo->saveMany($request->all()), 200); |
|
| 37 | + } |
|
| 38 | 38 | } |