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