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