@@ -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\V1\Reporting\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\V1\Reporting\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('reporting', '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('reporting', '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('reporting', '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('reporting', 'Routes/api.php'); |
|
77 | + }); |
|
78 | + } |
|
79 | 79 | } |
@@ -54,7 +54,7 @@ discard block |
||
54 | 54 | Route::group([ |
55 | 55 | 'middleware' => 'web', |
56 | 56 | 'namespace' => $this->namespace, |
57 | - ], function ($router) { |
|
57 | + ], function($router) { |
|
58 | 58 | require module_path('reporting', 'Routes/web.php'); |
59 | 59 | }); |
60 | 60 | } |
@@ -72,7 +72,7 @@ discard block |
||
72 | 72 | 'middleware' => 'api', |
73 | 73 | 'namespace' => $this->namespace, |
74 | 74 | 'prefix' => 'api', |
75 | - ], function ($router) { |
|
75 | + ], function($router) { |
|
76 | 76 | require module_path('reporting', 'Routes/api.php'); |
77 | 77 | }); |
78 | 78 | } |
@@ -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\V1\Acl\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\V1\Acl\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('acl', '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('acl', '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('acl', '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('acl', 'Routes/api.php'); |
|
77 | + }); |
|
78 | + } |
|
79 | 79 | } |
@@ -54,7 +54,7 @@ discard block |
||
54 | 54 | Route::group([ |
55 | 55 | 'middleware' => 'web', |
56 | 56 | 'namespace' => $this->namespace, |
57 | - ], function ($router) { |
|
57 | + ], function($router) { |
|
58 | 58 | require module_path('acl', 'Routes/web.php'); |
59 | 59 | }); |
60 | 60 | } |
@@ -72,7 +72,7 @@ discard block |
||
72 | 72 | 'middleware' => 'api', |
73 | 73 | 'namespace' => $this->namespace, |
74 | 74 | 'prefix' => 'api', |
75 | - ], function ($router) { |
|
75 | + ], function($router) { |
|
76 | 76 | require module_path('acl', 'Routes/api.php'); |
77 | 77 | }); |
78 | 78 | } |
@@ -2,25 +2,25 @@ |
||
2 | 2 | |
3 | 3 | return [ |
4 | 4 | |
5 | - /** |
|
6 | - * List of error messages used in core module. |
|
7 | - */ |
|
8 | - 'unAuthorized' => 'من فضلك قم بتسجيل الدخول', |
|
9 | - 'tokenExpired' => 'انتهت صلاحية الدخول', |
|
10 | - 'noPermissions' => 'لا توجد صلاحية', |
|
11 | - 'loginFailed' => 'خطأ في البريد لاكتروني او كلمة المرور', |
|
12 | - 'noSocialEmail' => 'لا يمكن الحصول علي تابريد الاكتروني', |
|
13 | - 'userAlreadyRegistered' => 'المستخد مسجل بالفعل.سجل الدخول بالبريد الاكتروني و كلمة السر', |
|
14 | - 'connectionError' => 'خطأ في الاتصال', |
|
15 | - 'redisNotRunning' => 'سيرفير الاشعارات لايعمل', |
|
16 | - 'dbQueryError' => 'خطا في البيانات', |
|
17 | - 'cannotCreateSetting' => 'لا يمكن اضافة اعدادات', |
|
18 | - 'cannotUpdateSettingKey' => 'لا يمكن تعديل اعدادات', |
|
19 | - 'userIsBlocked' => 'لقد تم حظرك', |
|
20 | - 'invalidResetToken' => 'رمز تعديل كلمة المرور خطا', |
|
21 | - 'invalidResetPassword' => 'خطا في نعديل كلمة المرور', |
|
22 | - 'invalidOldPassword' => 'كلمة السر القديمه خطا', |
|
23 | - 'notFound' => 'ال :replace المطلوب غير موجود', |
|
24 | - 'generalError' => 'حدث خطا ما', |
|
5 | + /** |
|
6 | + * List of error messages used in core module. |
|
7 | + */ |
|
8 | + 'unAuthorized' => 'من فضلك قم بتسجيل الدخول', |
|
9 | + 'tokenExpired' => 'انتهت صلاحية الدخول', |
|
10 | + 'noPermissions' => 'لا توجد صلاحية', |
|
11 | + 'loginFailed' => 'خطأ في البريد لاكتروني او كلمة المرور', |
|
12 | + 'noSocialEmail' => 'لا يمكن الحصول علي تابريد الاكتروني', |
|
13 | + 'userAlreadyRegistered' => 'المستخد مسجل بالفعل.سجل الدخول بالبريد الاكتروني و كلمة السر', |
|
14 | + 'connectionError' => 'خطأ في الاتصال', |
|
15 | + 'redisNotRunning' => 'سيرفير الاشعارات لايعمل', |
|
16 | + 'dbQueryError' => 'خطا في البيانات', |
|
17 | + 'cannotCreateSetting' => 'لا يمكن اضافة اعدادات', |
|
18 | + 'cannotUpdateSettingKey' => 'لا يمكن تعديل اعدادات', |
|
19 | + 'userIsBlocked' => 'لقد تم حظرك', |
|
20 | + 'invalidResetToken' => 'رمز تعديل كلمة المرور خطا', |
|
21 | + 'invalidResetPassword' => 'خطا في نعديل كلمة المرور', |
|
22 | + 'invalidOldPassword' => 'كلمة السر القديمه خطا', |
|
23 | + 'notFound' => 'ال :replace المطلوب غير موجود', |
|
24 | + 'generalError' => 'حدث خطا ما', |
|
25 | 25 | |
26 | 26 | ]; |
27 | 27 | \ No newline at end of file |
@@ -2,25 +2,25 @@ |
||
2 | 2 | |
3 | 3 | return [ |
4 | 4 | |
5 | - /** |
|
6 | - * List of error messages used in core module. |
|
7 | - */ |
|
8 | - 'unAuthorized' => 'Please login before any action', |
|
9 | - 'tokenExpired' => 'Login token expired', |
|
10 | - 'noPermissions' => 'No permissions', |
|
11 | - 'loginFailed' => 'Wrong mail or password', |
|
12 | - 'noSocialEmail' => 'Couldn\'t retrieve email', |
|
13 | - 'userAlreadyRegistered' => 'User already registered. Please login using email and password', |
|
14 | - 'connectionError' => 'Connection error', |
|
15 | - 'redisNotRunning' => 'Your redis notification server is\'t running', |
|
16 | - 'dbQueryError' => 'Please check the given inputes', |
|
17 | - 'cannotCreateSetting' => 'Can\'t create setting', |
|
18 | - 'cannotUpdateSettingKey' => 'Can\'t update setting key', |
|
19 | - 'userIsBlocked' => 'You have been blocked', |
|
20 | - 'invalidResetToken' => 'Reset password token is invalid', |
|
21 | - 'invalidResetPassword' => 'Reset password is invalid', |
|
22 | - 'invalidOldPassword' => 'Old password is invalid', |
|
23 | - 'notFound' => 'The requested :replace not found', |
|
24 | - 'generalError' => 'Something went wrong', |
|
5 | + /** |
|
6 | + * List of error messages used in core module. |
|
7 | + */ |
|
8 | + 'unAuthorized' => 'Please login before any action', |
|
9 | + 'tokenExpired' => 'Login token expired', |
|
10 | + 'noPermissions' => 'No permissions', |
|
11 | + 'loginFailed' => 'Wrong mail or password', |
|
12 | + 'noSocialEmail' => 'Couldn\'t retrieve email', |
|
13 | + 'userAlreadyRegistered' => 'User already registered. Please login using email and password', |
|
14 | + 'connectionError' => 'Connection error', |
|
15 | + 'redisNotRunning' => 'Your redis notification server is\'t running', |
|
16 | + 'dbQueryError' => 'Please check the given inputes', |
|
17 | + 'cannotCreateSetting' => 'Can\'t create setting', |
|
18 | + 'cannotUpdateSettingKey' => 'Can\'t update setting key', |
|
19 | + 'userIsBlocked' => 'You have been blocked', |
|
20 | + 'invalidResetToken' => 'Reset password token is invalid', |
|
21 | + 'invalidResetPassword' => 'Reset password is invalid', |
|
22 | + 'invalidOldPassword' => 'Old password is invalid', |
|
23 | + 'notFound' => 'The requested :replace not found', |
|
24 | + 'generalError' => 'Something went wrong', |
|
25 | 25 | |
26 | 26 | ]; |
27 | 27 | \ No newline at end of file |
@@ -13,16 +13,16 @@ |
||
13 | 13 | public function up() |
14 | 14 | { |
15 | 15 | Schema::create('users', function (Blueprint $table) { |
16 | - $table->increments('id'); |
|
17 | - $table->string('name', 100)->nullable(); |
|
18 | - $table->string('email')->unique(); |
|
19 | - $table->string('password', 60)->nullable(); |
|
20 | - $table->boolean('blocked')->default(0); |
|
16 | + $table->increments('id'); |
|
17 | + $table->string('name', 100)->nullable(); |
|
18 | + $table->string('email')->unique(); |
|
19 | + $table->string('password', 60)->nullable(); |
|
20 | + $table->boolean('blocked')->default(0); |
|
21 | 21 | $table->date('last_change_password')->nullable(); |
22 | - $table->softDeletes(); |
|
23 | - $table->rememberToken(); |
|
24 | - $table->timestamps(); |
|
25 | - }); |
|
22 | + $table->softDeletes(); |
|
23 | + $table->rememberToken(); |
|
24 | + $table->timestamps(); |
|
25 | + }); |
|
26 | 26 | } |
27 | 27 | |
28 | 28 | /** |
@@ -12,7 +12,7 @@ |
||
12 | 12 | */ |
13 | 13 | public function up() |
14 | 14 | { |
15 | - Schema::create('users', function (Blueprint $table) { |
|
15 | + Schema::create('users', function(Blueprint $table) { |
|
16 | 16 | $table->increments('id'); |
17 | 17 | $table->string('name', 100)->nullable(); |
18 | 18 | $table->string('email')->unique(); |
@@ -170,8 +170,7 @@ |
||
170 | 170 | if ($request->has('id')) |
171 | 171 | { |
172 | 172 | $rule = str_replace('{id}', $request->get('id'), $rule); |
173 | - } |
|
174 | - else |
|
173 | + } else |
|
175 | 174 | { |
176 | 175 | $rule = str_replace(',{id}', '', $rule); |
177 | 176 | } |
@@ -7,250 +7,250 @@ |
||
7 | 7 | |
8 | 8 | class UsersController extends BaseApiController |
9 | 9 | { |
10 | - /** |
|
11 | - * The name of the model that is used by the base api controller |
|
12 | - * to preform actions like (add, edit ... etc). |
|
13 | - * @var string |
|
14 | - */ |
|
15 | - protected $model = 'users'; |
|
10 | + /** |
|
11 | + * The name of the model that is used by the base api controller |
|
12 | + * to preform actions like (add, edit ... etc). |
|
13 | + * @var string |
|
14 | + */ |
|
15 | + protected $model = 'users'; |
|
16 | 16 | |
17 | - /** |
|
18 | - * List of all route actions that the base api controller |
|
19 | - * will skip permissions check for them. |
|
20 | - * @var array |
|
21 | - */ |
|
22 | - protected $skipPermissionCheck = ['account', 'logout', 'changePassword']; |
|
17 | + /** |
|
18 | + * List of all route actions that the base api controller |
|
19 | + * will skip permissions check for them. |
|
20 | + * @var array |
|
21 | + */ |
|
22 | + protected $skipPermissionCheck = ['account', 'logout', 'changePassword']; |
|
23 | 23 | |
24 | - /** |
|
25 | - * List of all route actions that the base api controller |
|
26 | - * will skip login check for them. |
|
27 | - * @var array |
|
28 | - */ |
|
29 | - protected $skipLoginCheck = ['login', 'loginSocial', 'register', 'sendreset', 'resetpassword', 'refreshtoken']; |
|
24 | + /** |
|
25 | + * List of all route actions that the base api controller |
|
26 | + * will skip login check for them. |
|
27 | + * @var array |
|
28 | + */ |
|
29 | + protected $skipLoginCheck = ['login', 'loginSocial', 'register', 'sendreset', 'resetpassword', 'refreshtoken']; |
|
30 | 30 | |
31 | - /** |
|
32 | - * The validations rules used by the base api controller |
|
33 | - * to check before add. |
|
34 | - * @var array |
|
35 | - */ |
|
36 | - protected $validationRules = [ |
|
37 | - 'name' => 'nullable|string|unique:users,name,{id}', |
|
38 | - 'email' => 'required|email|unique:users,email,{id}', |
|
39 | - 'password' => 'nullable|min:6' |
|
40 | - ]; |
|
31 | + /** |
|
32 | + * The validations rules used by the base api controller |
|
33 | + * to check before add. |
|
34 | + * @var array |
|
35 | + */ |
|
36 | + protected $validationRules = [ |
|
37 | + 'name' => 'nullable|string|unique:users,name,{id}', |
|
38 | + 'email' => 'required|email|unique:users,email,{id}', |
|
39 | + 'password' => 'nullable|min:6' |
|
40 | + ]; |
|
41 | 41 | |
42 | - /** |
|
43 | - * Return the logged in user account. |
|
44 | - * |
|
45 | - * @return \Illuminate\Http\Response |
|
46 | - */ |
|
47 | - public function account() |
|
48 | - { |
|
49 | - return \Response::json(\Core::users()->account($this->relations), 200); |
|
50 | - } |
|
42 | + /** |
|
43 | + * Return the logged in user account. |
|
44 | + * |
|
45 | + * @return \Illuminate\Http\Response |
|
46 | + */ |
|
47 | + public function account() |
|
48 | + { |
|
49 | + return \Response::json(\Core::users()->account($this->relations), 200); |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * Block the user. |
|
54 | - * |
|
55 | - * @param integer $id Id of the user. |
|
56 | - * @return \Illuminate\Http\Response |
|
57 | - */ |
|
58 | - public function block($id) |
|
59 | - { |
|
60 | - return \Response::json(\Core::users()->block($id), 200); |
|
61 | - } |
|
52 | + /** |
|
53 | + * Block the user. |
|
54 | + * |
|
55 | + * @param integer $id Id of the user. |
|
56 | + * @return \Illuminate\Http\Response |
|
57 | + */ |
|
58 | + public function block($id) |
|
59 | + { |
|
60 | + return \Response::json(\Core::users()->block($id), 200); |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * Unblock the user. |
|
65 | - * |
|
66 | - * @param integer $id Id of the user. |
|
67 | - * @return \Illuminate\Http\Response |
|
68 | - */ |
|
69 | - public function unblock($id) |
|
70 | - { |
|
71 | - return \Response::json(\Core::users()->unblock($id), 200); |
|
72 | - } |
|
63 | + /** |
|
64 | + * Unblock the user. |
|
65 | + * |
|
66 | + * @param integer $id Id of the user. |
|
67 | + * @return \Illuminate\Http\Response |
|
68 | + */ |
|
69 | + public function unblock($id) |
|
70 | + { |
|
71 | + return \Response::json(\Core::users()->unblock($id), 200); |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * Logout the user. |
|
76 | - * |
|
77 | - * @return \Illuminate\Http\Response |
|
78 | - */ |
|
79 | - public function logout() |
|
80 | - { |
|
81 | - return \Response::json(\Core::users()->logout(), 200); |
|
82 | - } |
|
74 | + /** |
|
75 | + * Logout the user. |
|
76 | + * |
|
77 | + * @return \Illuminate\Http\Response |
|
78 | + */ |
|
79 | + public function logout() |
|
80 | + { |
|
81 | + return \Response::json(\Core::users()->logout(), 200); |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * Handle a registration request. |
|
86 | - * |
|
87 | - * @param \Illuminate\Http\Request $request |
|
88 | - * @return \Illuminate\Http\Response |
|
89 | - */ |
|
90 | - public function register(Request $request) |
|
91 | - { |
|
92 | - $this->validate($request, [ |
|
93 | - 'name' => 'nullable|string|unique:users,name,{id}', |
|
94 | - 'email' => 'required|email|unique:users,email,{id}', |
|
95 | - 'password' => 'required|min:6' |
|
96 | - ]); |
|
84 | + /** |
|
85 | + * Handle a registration request. |
|
86 | + * |
|
87 | + * @param \Illuminate\Http\Request $request |
|
88 | + * @return \Illuminate\Http\Response |
|
89 | + */ |
|
90 | + public function register(Request $request) |
|
91 | + { |
|
92 | + $this->validate($request, [ |
|
93 | + 'name' => 'nullable|string|unique:users,name,{id}', |
|
94 | + 'email' => 'required|email|unique:users,email,{id}', |
|
95 | + 'password' => 'required|min:6' |
|
96 | + ]); |
|
97 | 97 | |
98 | - return \Response::json(\Core::users()->register($request->only('email', 'password')), 200); |
|
99 | - } |
|
98 | + return \Response::json(\Core::users()->register($request->only('email', 'password')), 200); |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * Handle a login request to the application. |
|
103 | - * |
|
104 | - * @param \Illuminate\Http\Request $request |
|
105 | - * @return \Illuminate\Http\Response |
|
106 | - */ |
|
107 | - public function login(Request $request) |
|
108 | - { |
|
109 | - $this->validate($request, [ |
|
110 | - 'email' => 'required|email', |
|
111 | - 'password' => 'required|min:6', |
|
112 | - 'admin' => 'boolean' |
|
113 | - ]); |
|
101 | + /** |
|
102 | + * Handle a login request to the application. |
|
103 | + * |
|
104 | + * @param \Illuminate\Http\Request $request |
|
105 | + * @return \Illuminate\Http\Response |
|
106 | + */ |
|
107 | + public function login(Request $request) |
|
108 | + { |
|
109 | + $this->validate($request, [ |
|
110 | + 'email' => 'required|email', |
|
111 | + 'password' => 'required|min:6', |
|
112 | + 'admin' => 'boolean' |
|
113 | + ]); |
|
114 | 114 | |
115 | - return \Response::json(\Core::users()->login($request->only('email', 'password'), $request->get('admin')), 200); |
|
116 | - } |
|
115 | + return \Response::json(\Core::users()->login($request->only('email', 'password'), $request->get('admin')), 200); |
|
116 | + } |
|
117 | 117 | |
118 | - /** |
|
119 | - * Handle a social login request of the none admin to the application. |
|
120 | - * |
|
121 | - * @param \Illuminate\Http\Request $request |
|
122 | - * @return \Illuminate\Http\Response |
|
123 | - */ |
|
124 | - public function loginSocial(Request $request) |
|
125 | - { |
|
126 | - $this->validate($request, [ |
|
127 | - 'auth_code' => 'required_without:access_token', |
|
128 | - 'access_token' => 'required_without:auth_code', |
|
129 | - 'type' => 'required|in:facebook,google' |
|
130 | - ]); |
|
118 | + /** |
|
119 | + * Handle a social login request of the none admin to the application. |
|
120 | + * |
|
121 | + * @param \Illuminate\Http\Request $request |
|
122 | + * @return \Illuminate\Http\Response |
|
123 | + */ |
|
124 | + public function loginSocial(Request $request) |
|
125 | + { |
|
126 | + $this->validate($request, [ |
|
127 | + 'auth_code' => 'required_without:access_token', |
|
128 | + 'access_token' => 'required_without:auth_code', |
|
129 | + 'type' => 'required|in:facebook,google' |
|
130 | + ]); |
|
131 | 131 | |
132 | - return \Response::json(\Core::users()->loginSocial($request->only('auth_code', 'access_token', 'type')), 200); |
|
133 | - } |
|
132 | + return \Response::json(\Core::users()->loginSocial($request->only('auth_code', 'access_token', 'type')), 200); |
|
133 | + } |
|
134 | 134 | |
135 | - /** |
|
136 | - * Assign the given groups to the given user. |
|
137 | - * |
|
138 | - * @param \Illuminate\Http\Request $request |
|
139 | - * @return \Illuminate\Http\Response |
|
140 | - */ |
|
141 | - public function assigngroups(Request $request) |
|
142 | - { |
|
143 | - $this->validate($request, [ |
|
144 | - 'group_ids' => 'required|exists:groups,id', |
|
145 | - 'user_id' => 'required|exists:users,id' |
|
146 | - ]); |
|
135 | + /** |
|
136 | + * Assign the given groups to the given user. |
|
137 | + * |
|
138 | + * @param \Illuminate\Http\Request $request |
|
139 | + * @return \Illuminate\Http\Response |
|
140 | + */ |
|
141 | + public function assigngroups(Request $request) |
|
142 | + { |
|
143 | + $this->validate($request, [ |
|
144 | + 'group_ids' => 'required|exists:groups,id', |
|
145 | + 'user_id' => 'required|exists:users,id' |
|
146 | + ]); |
|
147 | 147 | |
148 | - return \Response::json(\Core::users()->assignGroups($request->get('user_id'), $request->get('group_ids')), 200); |
|
149 | - } |
|
148 | + return \Response::json(\Core::users()->assignGroups($request->get('user_id'), $request->get('group_ids')), 200); |
|
149 | + } |
|
150 | 150 | |
151 | - /** |
|
152 | - * Send a reset link to the given user. |
|
153 | - * |
|
154 | - * @param \Illuminate\Http\Request $request |
|
155 | - * @return \Illuminate\Http\Response |
|
156 | - */ |
|
157 | - public function sendreset(Request $request) |
|
158 | - { |
|
159 | - $this->validate($request, ['email' => 'required|email']); |
|
151 | + /** |
|
152 | + * Send a reset link to the given user. |
|
153 | + * |
|
154 | + * @param \Illuminate\Http\Request $request |
|
155 | + * @return \Illuminate\Http\Response |
|
156 | + */ |
|
157 | + public function sendreset(Request $request) |
|
158 | + { |
|
159 | + $this->validate($request, ['email' => 'required|email']); |
|
160 | 160 | |
161 | - return \Response::json(\Core::users()->sendReset($request->only('email')), 200); |
|
162 | - } |
|
161 | + return \Response::json(\Core::users()->sendReset($request->only('email')), 200); |
|
162 | + } |
|
163 | 163 | |
164 | - /** |
|
165 | - * Reset the given user's password. |
|
166 | - * |
|
167 | - * @param \Illuminate\Http\Request $request |
|
168 | - * @return \Illuminate\Http\Response |
|
169 | - */ |
|
170 | - public function resetpassword(Request $request) |
|
171 | - { |
|
172 | - $this->validate($request, [ |
|
173 | - 'token' => 'required', |
|
174 | - 'email' => 'required|email', |
|
175 | - 'password' => 'required|confirmed|min:6', |
|
176 | - 'password_confirmation' => 'required', |
|
177 | - ]); |
|
164 | + /** |
|
165 | + * Reset the given user's password. |
|
166 | + * |
|
167 | + * @param \Illuminate\Http\Request $request |
|
168 | + * @return \Illuminate\Http\Response |
|
169 | + */ |
|
170 | + public function resetpassword(Request $request) |
|
171 | + { |
|
172 | + $this->validate($request, [ |
|
173 | + 'token' => 'required', |
|
174 | + 'email' => 'required|email', |
|
175 | + 'password' => 'required|confirmed|min:6', |
|
176 | + 'password_confirmation' => 'required', |
|
177 | + ]); |
|
178 | 178 | |
179 | - return \Response::json(\Core::users()->resetPassword($request->only('email', 'password', 'password_confirmation', 'token')), 200); |
|
180 | - } |
|
179 | + return \Response::json(\Core::users()->resetPassword($request->only('email', 'password', 'password_confirmation', 'token')), 200); |
|
180 | + } |
|
181 | 181 | |
182 | - /** |
|
183 | - * Change the logged in user password. |
|
184 | - * |
|
185 | - * @param \Illuminate\Http\Request $request |
|
186 | - * @return \Illuminate\Http\Response |
|
187 | - */ |
|
188 | - public function changePassword(Request $request) |
|
189 | - { |
|
190 | - $this->validate($request, [ |
|
191 | - 'old_password' => 'required', |
|
192 | - 'password' => 'required|confirmed|min:6', |
|
193 | - 'password_confirmation' => 'required', |
|
194 | - ]); |
|
182 | + /** |
|
183 | + * Change the logged in user password. |
|
184 | + * |
|
185 | + * @param \Illuminate\Http\Request $request |
|
186 | + * @return \Illuminate\Http\Response |
|
187 | + */ |
|
188 | + public function changePassword(Request $request) |
|
189 | + { |
|
190 | + $this->validate($request, [ |
|
191 | + 'old_password' => 'required', |
|
192 | + 'password' => 'required|confirmed|min:6', |
|
193 | + 'password_confirmation' => 'required', |
|
194 | + ]); |
|
195 | 195 | |
196 | - return \Response::json(\Core::users()->changePassword($request->only('old_password', 'password', 'password_confirmation')), 200); |
|
197 | - } |
|
196 | + return \Response::json(\Core::users()->changePassword($request->only('old_password', 'password', 'password_confirmation')), 200); |
|
197 | + } |
|
198 | 198 | |
199 | - /** |
|
200 | - * Refresh the expired login token. |
|
201 | - * |
|
202 | - * @return \Illuminate\Http\Response |
|
203 | - */ |
|
204 | - public function refreshtoken() |
|
205 | - { |
|
206 | - return \Response::json(\Core::users()->refreshtoken(), 200); |
|
207 | - } |
|
199 | + /** |
|
200 | + * Refresh the expired login token. |
|
201 | + * |
|
202 | + * @return \Illuminate\Http\Response |
|
203 | + */ |
|
204 | + public function refreshtoken() |
|
205 | + { |
|
206 | + return \Response::json(\Core::users()->refreshtoken(), 200); |
|
207 | + } |
|
208 | 208 | |
209 | - /** |
|
210 | - * Paginate all users with inthe given group. |
|
211 | - * |
|
212 | - * @param \Illuminate\Http\Request $request |
|
213 | - * @param string $groupName The name of the requested group. |
|
214 | - * @param integer $perPage Number of rows per page default 15. |
|
215 | - * @param string $sortBy The name of the column to sort by. |
|
216 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
217 | - * @return \Illuminate\Http\Response |
|
218 | - */ |
|
219 | - public function group(Request $request, $groupName, $perPage = false, $sortBy = 'created_at', $desc = 1) |
|
220 | - { |
|
221 | - return \Response::json(\Core::users()->group($request->all(), $groupName, $this->relations, $perPage, $sortBy, $desc), 200); |
|
222 | - } |
|
209 | + /** |
|
210 | + * Paginate all users with inthe given group. |
|
211 | + * |
|
212 | + * @param \Illuminate\Http\Request $request |
|
213 | + * @param string $groupName The name of the requested group. |
|
214 | + * @param integer $perPage Number of rows per page default 15. |
|
215 | + * @param string $sortBy The name of the column to sort by. |
|
216 | + * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
217 | + * @return \Illuminate\Http\Response |
|
218 | + */ |
|
219 | + public function group(Request $request, $groupName, $perPage = false, $sortBy = 'created_at', $desc = 1) |
|
220 | + { |
|
221 | + return \Response::json(\Core::users()->group($request->all(), $groupName, $this->relations, $perPage, $sortBy, $desc), 200); |
|
222 | + } |
|
223 | 223 | |
224 | - /** |
|
225 | - * Save the given data to the logged in user. |
|
226 | - * |
|
227 | - * @param \Illuminate\Http\Request $request |
|
228 | - * @return \Illuminate\Http\Response |
|
229 | - */ |
|
230 | - public function saveProfile(Request $request) |
|
231 | - { |
|
232 | - foreach ($this->validationRules as &$rule) |
|
233 | - { |
|
234 | - if (strpos($rule, 'exists') && ! strpos($rule, 'deleted_at,NULL')) |
|
235 | - { |
|
236 | - $rule .= ',deleted_at,NULL'; |
|
237 | - } |
|
224 | + /** |
|
225 | + * Save the given data to the logged in user. |
|
226 | + * |
|
227 | + * @param \Illuminate\Http\Request $request |
|
228 | + * @return \Illuminate\Http\Response |
|
229 | + */ |
|
230 | + public function saveProfile(Request $request) |
|
231 | + { |
|
232 | + foreach ($this->validationRules as &$rule) |
|
233 | + { |
|
234 | + if (strpos($rule, 'exists') && ! strpos($rule, 'deleted_at,NULL')) |
|
235 | + { |
|
236 | + $rule .= ',deleted_at,NULL'; |
|
237 | + } |
|
238 | 238 | |
239 | - if ($request->has('id')) |
|
240 | - { |
|
241 | - $rule = str_replace('{id}', $request->get('id'), $rule); |
|
242 | - } |
|
243 | - else |
|
244 | - { |
|
245 | - $rule = str_replace(',{id}', '', $rule); |
|
246 | - } |
|
247 | - } |
|
239 | + if ($request->has('id')) |
|
240 | + { |
|
241 | + $rule = str_replace('{id}', $request->get('id'), $rule); |
|
242 | + } |
|
243 | + else |
|
244 | + { |
|
245 | + $rule = str_replace(',{id}', '', $rule); |
|
246 | + } |
|
247 | + } |
|
248 | 248 | |
249 | - $this->validate($request, $this->validationRules); |
|
249 | + $this->validate($request, $this->validationRules); |
|
250 | 250 | |
251 | - if ($this->model) |
|
252 | - { |
|
253 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->saveProfile($request->all()), 200); |
|
254 | - } |
|
255 | - } |
|
251 | + if ($this->model) |
|
252 | + { |
|
253 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->saveProfile($request->all()), 200); |
|
254 | + } |
|
255 | + } |
|
256 | 256 | } |
@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | * to preform actions like (add, edit ... etc). |
13 | 13 | * @var string |
14 | 14 | */ |
15 | - protected $model = 'users'; |
|
15 | + protected $model = 'users'; |
|
16 | 16 | |
17 | 17 | /** |
18 | 18 | * List of all route actions that the base api controller |
@@ -26,14 +26,14 @@ discard block |
||
26 | 26 | * will skip login check for them. |
27 | 27 | * @var array |
28 | 28 | */ |
29 | - protected $skipLoginCheck = ['login', 'loginSocial', 'register', 'sendreset', 'resetpassword', 'refreshtoken']; |
|
29 | + protected $skipLoginCheck = ['login', 'loginSocial', 'register', 'sendreset', 'resetpassword', 'refreshtoken']; |
|
30 | 30 | |
31 | 31 | /** |
32 | 32 | * The validations rules used by the base api controller |
33 | 33 | * to check before add. |
34 | 34 | * @var array |
35 | 35 | */ |
36 | - protected $validationRules = [ |
|
36 | + protected $validationRules = [ |
|
37 | 37 | 'name' => 'nullable|string|unique:users,name,{id}', |
38 | 38 | 'email' => 'required|email|unique:users,email,{id}', |
39 | 39 | 'password' => 'nullable|min:6' |
@@ -2,8 +2,8 @@ |
||
2 | 2 | |
3 | 3 | return [ |
4 | 4 | |
5 | - /** |
|
6 | - * Here goes your notification messages. |
|
7 | - */ |
|
5 | + /** |
|
6 | + * Here goes your notification messages. |
|
7 | + */ |
|
8 | 8 | |
9 | 9 | ]; |
10 | 10 | \ No newline at end of file |
@@ -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 | */ |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | * @param integer $perPage |
140 | 140 | * @param array $relations |
141 | 141 | * @param string $sortBy |
142 | - * @param boolean $desc |
|
142 | + * @param integer $desc |
|
143 | 143 | * @param array $columns |
144 | 144 | * @return collection |
145 | 145 | */ |
@@ -157,7 +157,7 @@ discard block |
||
157 | 157 | * @param integer $perPage |
158 | 158 | * @param array $relations |
159 | 159 | * @param string $sortBy |
160 | - * @param boolean $desc |
|
160 | + * @param integer $desc |
|
161 | 161 | * @param array $columns |
162 | 162 | * @return collection |
163 | 163 | */ |
@@ -418,7 +418,7 @@ discard block |
||
418 | 418 | * Update record in the storage based on the given |
419 | 419 | * condition. |
420 | 420 | * |
421 | - * @param var $value condition value |
|
421 | + * @param boolean $value condition value |
|
422 | 422 | * @param array $data |
423 | 423 | * @param string $attribute condition column name |
424 | 424 | * @return void |
@@ -479,7 +479,7 @@ discard block |
||
479 | 479 | * id. |
480 | 480 | * |
481 | 481 | * @param integer $id |
482 | - * @param array $relations |
|
482 | + * @param string[] $relations |
|
483 | 483 | * @param array $columns |
484 | 484 | * @return object |
485 | 485 | */ |
@@ -495,7 +495,7 @@ discard block |
||
495 | 495 | * @param array $conditions array of conditions |
496 | 496 | * @param array $relations |
497 | 497 | * @param string $sortBy |
498 | - * @param boolean $desc |
|
498 | + * @param integer $desc |
|
499 | 499 | * @param array $columns |
500 | 500 | * @return collection |
501 | 501 | */ |
@@ -527,7 +527,7 @@ discard block |
||
527 | 527 | * @param array $conditions array of conditions |
528 | 528 | * @param integer $perPage |
529 | 529 | * @param string $sortBy |
530 | - * @param boolean $desc |
|
530 | + * @param integer $desc |
|
531 | 531 | * @param array $columns |
532 | 532 | * @return collection |
533 | 533 | */ |
@@ -4,657 +4,657 @@ |
||
4 | 4 | |
5 | 5 | abstract class AbstractRepository implements RepositoryInterface |
6 | 6 | { |
7 | - /** |
|
8 | - * The model implementation. |
|
9 | - * |
|
10 | - * @var model |
|
11 | - */ |
|
12 | - public $model; |
|
7 | + /** |
|
8 | + * The model implementation. |
|
9 | + * |
|
10 | + * @var model |
|
11 | + */ |
|
12 | + public $model; |
|
13 | 13 | |
14 | - /** |
|
15 | - * The config implementation. |
|
16 | - * |
|
17 | - * @var config |
|
18 | - */ |
|
19 | - protected $config; |
|
14 | + /** |
|
15 | + * The config implementation. |
|
16 | + * |
|
17 | + * @var config |
|
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 | - /** |
|
71 | - * Use the first element in the model columns to construct the first condition. |
|
72 | - */ |
|
73 | - $q->where(\DB::raw('LOWER(' . array_shift($conditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Loop through the rest of the columns to construct or where conditions. |
|
78 | - */ |
|
79 | - foreach ($conditionColumns as $column) |
|
80 | - { |
|
81 | - $q->orWhere(\DB::raw('LOWER(' . $column . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Loop through the model relations. |
|
86 | - */ |
|
87 | - foreach ($relations as $relation) |
|
88 | - { |
|
89 | - /** |
|
90 | - * Remove the sub relation if exists. |
|
91 | - */ |
|
92 | - $relation = explode('.', $relation)[0]; |
|
93 | - |
|
94 | - /** |
|
95 | - * Try to fetch the relation repository from the core. |
|
96 | - */ |
|
97 | - if (\Core::$relation()) |
|
98 | - { |
|
99 | - /** |
|
100 | - * Construct the relation condition. |
|
101 | - */ |
|
102 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
103 | - |
|
104 | - $subModel->where(function ($q) use ($query, $relation){ |
|
105 | - |
|
106 | - /** |
|
107 | - * Get columns of the relation. |
|
108 | - */ |
|
109 | - $subConditionColumns = \Core::$relation()->model->searchable; |
|
110 | - |
|
111 | - if (count($subConditionColumns)) |
|
112 | - { |
|
113 | - /** |
|
114 | - * Use the first element in the relation model columns to construct the first condition. |
|
115 | - */ |
|
116 | - $q->where(\DB::raw('LOWER(' . array_shift($subConditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * Loop through the rest of the columns to construct or where conditions. |
|
121 | - */ |
|
122 | - foreach ($subConditionColumns as $subConditionColumn) |
|
123 | - { |
|
124 | - $q->orWhere(\DB::raw('LOWER(' . $subConditionColumn . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
125 | - } |
|
126 | - }); |
|
127 | - |
|
128 | - }); |
|
129 | - } |
|
130 | - } |
|
131 | - }); |
|
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 | + /** |
|
71 | + * Use the first element in the model columns to construct the first condition. |
|
72 | + */ |
|
73 | + $q->where(\DB::raw('LOWER(' . array_shift($conditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Loop through the rest of the columns to construct or where conditions. |
|
78 | + */ |
|
79 | + foreach ($conditionColumns as $column) |
|
80 | + { |
|
81 | + $q->orWhere(\DB::raw('LOWER(' . $column . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Loop through the model relations. |
|
86 | + */ |
|
87 | + foreach ($relations as $relation) |
|
88 | + { |
|
89 | + /** |
|
90 | + * Remove the sub relation if exists. |
|
91 | + */ |
|
92 | + $relation = explode('.', $relation)[0]; |
|
93 | + |
|
94 | + /** |
|
95 | + * Try to fetch the relation repository from the core. |
|
96 | + */ |
|
97 | + if (\Core::$relation()) |
|
98 | + { |
|
99 | + /** |
|
100 | + * Construct the relation condition. |
|
101 | + */ |
|
102 | + $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
103 | + |
|
104 | + $subModel->where(function ($q) use ($query, $relation){ |
|
105 | + |
|
106 | + /** |
|
107 | + * Get columns of the relation. |
|
108 | + */ |
|
109 | + $subConditionColumns = \Core::$relation()->model->searchable; |
|
110 | + |
|
111 | + if (count($subConditionColumns)) |
|
112 | + { |
|
113 | + /** |
|
114 | + * Use the first element in the relation model columns to construct the first condition. |
|
115 | + */ |
|
116 | + $q->where(\DB::raw('LOWER(' . array_shift($subConditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * Loop through the rest of the columns to construct or where conditions. |
|
121 | + */ |
|
122 | + foreach ($subConditionColumns as $subConditionColumn) |
|
123 | + { |
|
124 | + $q->orWhere(\DB::raw('LOWER(' . $subConditionColumn . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
125 | + } |
|
126 | + }); |
|
127 | + |
|
128 | + }); |
|
129 | + } |
|
130 | + } |
|
131 | + }); |
|
132 | 132 | |
133 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
134 | - } |
|
133 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
134 | + } |
|
135 | 135 | |
136 | - /** |
|
137 | - * Fetch all records with relations from storage in pages. |
|
138 | - * |
|
139 | - * @param integer $perPage |
|
140 | - * @param array $relations |
|
141 | - * @param string $sortBy |
|
142 | - * @param boolean $desc |
|
143 | - * @param array $columns |
|
144 | - * @return collection |
|
145 | - */ |
|
146 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
147 | - { |
|
148 | - $sort = $desc ? 'desc' : 'asc'; |
|
149 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Fetch all records with relations based on |
|
154 | - * the given condition from storage in pages. |
|
155 | - * |
|
156 | - * @param array $conditions array of conditions |
|
157 | - * @param integer $perPage |
|
158 | - * @param array $relations |
|
159 | - * @param string $sortBy |
|
160 | - * @param boolean $desc |
|
161 | - * @param array $columns |
|
162 | - * @return collection |
|
163 | - */ |
|
164 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
165 | - { |
|
166 | - unset($conditions['page']); |
|
167 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
168 | - $sort = $desc ? 'desc' : 'asc'; |
|
169 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
170 | - } |
|
136 | + /** |
|
137 | + * Fetch all records with relations from storage in pages. |
|
138 | + * |
|
139 | + * @param integer $perPage |
|
140 | + * @param array $relations |
|
141 | + * @param string $sortBy |
|
142 | + * @param boolean $desc |
|
143 | + * @param array $columns |
|
144 | + * @return collection |
|
145 | + */ |
|
146 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
147 | + { |
|
148 | + $sort = $desc ? 'desc' : 'asc'; |
|
149 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Fetch all records with relations based on |
|
154 | + * the given condition from storage in pages. |
|
155 | + * |
|
156 | + * @param array $conditions array of conditions |
|
157 | + * @param integer $perPage |
|
158 | + * @param array $relations |
|
159 | + * @param string $sortBy |
|
160 | + * @param boolean $desc |
|
161 | + * @param array $columns |
|
162 | + * @return collection |
|
163 | + */ |
|
164 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
165 | + { |
|
166 | + unset($conditions['page']); |
|
167 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
168 | + $sort = $desc ? 'desc' : 'asc'; |
|
169 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
170 | + } |
|
171 | 171 | |
172 | - /** |
|
173 | - * Save the given model to the storage. |
|
174 | - * |
|
175 | - * @param array $data |
|
176 | - * @param boolean $saveLog |
|
177 | - * @return void |
|
178 | - */ |
|
179 | - public function save(array $data, $saveLog = true) |
|
180 | - { |
|
181 | - $model = false; |
|
182 | - $modelClass = $this->model; |
|
183 | - $relations = []; |
|
184 | - |
|
185 | - \DB::transaction(function () use (&$model, &$relations, $data, $saveLog, $modelClass) { |
|
186 | - /** |
|
187 | - * If the id is present in the data then select the model for updating, |
|
188 | - * else create new model. |
|
189 | - * @var array |
|
190 | - */ |
|
191 | - $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
192 | - if ( ! $model) |
|
193 | - { |
|
194 | - \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * Construct the model object with the given data, |
|
199 | - * and if there is a relation add it to relations array, |
|
200 | - * then save the model. |
|
201 | - */ |
|
202 | - foreach ($data as $key => $value) |
|
203 | - { |
|
204 | - /** |
|
205 | - * If the attribute is a relation. |
|
206 | - */ |
|
207 | - $relation = camel_case($key); |
|
208 | - if (method_exists($model, $relation) && \Core::$relation()) |
|
209 | - { |
|
210 | - /** |
|
211 | - * Check if the relation is a collection. |
|
212 | - */ |
|
213 | - if (class_basename($model->$relation) == 'Collection') |
|
214 | - { |
|
215 | - /** |
|
216 | - * If the relation has no value then marke the relation data |
|
217 | - * related to the model to be deleted. |
|
218 | - */ |
|
219 | - if ( ! $value || ! count($value)) |
|
220 | - { |
|
221 | - $relations[$relation] = 'delete'; |
|
222 | - } |
|
223 | - } |
|
224 | - if (is_array($value)) |
|
225 | - { |
|
226 | - /** |
|
227 | - * Loop through the relation data. |
|
228 | - */ |
|
229 | - foreach ($value as $attr => $val) |
|
230 | - { |
|
231 | - /** |
|
232 | - * Get the relation model. |
|
233 | - */ |
|
234 | - $relationBaseModel = \Core::$relation()->model; |
|
235 | - |
|
236 | - /** |
|
237 | - * Check if the relation is a collection. |
|
238 | - */ |
|
239 | - if (class_basename($model->$relation) == 'Collection') |
|
240 | - { |
|
241 | - /** |
|
242 | - * If the id is present in the data then select the relation model for updating, |
|
243 | - * else create new model. |
|
244 | - */ |
|
245 | - $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
246 | - |
|
247 | - /** |
|
248 | - * If model doesn't exists. |
|
249 | - */ |
|
250 | - if ( ! $relationModel) |
|
251 | - { |
|
252 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
253 | - } |
|
254 | - |
|
255 | - /** |
|
256 | - * Loop through the relation attributes. |
|
257 | - */ |
|
258 | - foreach ($val as $attr => $val) |
|
259 | - { |
|
260 | - /** |
|
261 | - * Prevent the sub relations or attributes not in the fillable. |
|
262 | - */ |
|
263 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
264 | - { |
|
265 | - $relationModel->$attr = $val; |
|
266 | - } |
|
267 | - } |
|
268 | - |
|
269 | - $relations[$relation][] = $relationModel; |
|
270 | - } |
|
271 | - /** |
|
272 | - * If not collection. |
|
273 | - */ |
|
274 | - else |
|
275 | - { |
|
276 | - /** |
|
277 | - * Prevent the sub relations. |
|
278 | - */ |
|
279 | - if (gettype($val) !== 'object' && gettype($val) !== 'array') |
|
280 | - { |
|
281 | - |
|
282 | - /** |
|
283 | - * If the id is present in the data then select the relation model for updating, |
|
284 | - * else create new model. |
|
285 | - */ |
|
286 | - $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
287 | - |
|
288 | - /** |
|
289 | - * If model doesn't exists. |
|
290 | - */ |
|
291 | - if ( ! $relationModel) |
|
292 | - { |
|
293 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
294 | - } |
|
295 | - |
|
296 | - foreach ($value as $relationAttribute => $relationValue) |
|
297 | - { |
|
298 | - /** |
|
299 | - * Prevent attributes not in the fillable. |
|
300 | - */ |
|
301 | - if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) |
|
302 | - { |
|
303 | - $relationModel->$relationAttribute = $relationValue; |
|
304 | - } |
|
305 | - } |
|
306 | - |
|
307 | - $relations[$relation] = $relationModel; |
|
308 | - } |
|
309 | - } |
|
310 | - } |
|
311 | - } |
|
312 | - } |
|
313 | - /** |
|
314 | - * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
315 | - */ |
|
316 | - else if (array_search($key, $model->getFillable(), true) !== false) |
|
317 | - { |
|
318 | - $model->$key = $value; |
|
319 | - } |
|
320 | - } |
|
321 | - /** |
|
322 | - * Save the model. |
|
323 | - */ |
|
324 | - $model->save(); |
|
325 | - |
|
326 | - /** |
|
327 | - * Loop through the relations array. |
|
328 | - */ |
|
329 | - foreach ($relations as $key => $value) |
|
330 | - { |
|
331 | - /** |
|
332 | - * If the relation is marked for delete then delete it. |
|
333 | - */ |
|
334 | - if ($value == 'delete' && $model->$key()->count()) |
|
335 | - { |
|
336 | - $model->$key()->delete(); |
|
337 | - } |
|
338 | - /** |
|
339 | - * If the relation is an array. |
|
340 | - */ |
|
341 | - else if (gettype($value) == 'array') |
|
342 | - { |
|
343 | - $ids = []; |
|
344 | - /** |
|
345 | - * Loop through the relations. |
|
346 | - */ |
|
347 | - foreach ($value as $val) |
|
348 | - { |
|
349 | - switch (class_basename($model->$key())) |
|
350 | - { |
|
351 | - /** |
|
352 | - * If the relation is one to many then update it's foreign key with |
|
353 | - * the model id and save it then add its id to ids array to delete all |
|
354 | - * relations who's id isn't in the ids array. |
|
355 | - */ |
|
356 | - case 'HasMany': |
|
357 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
358 | - $val->$foreignKeyName = $model->id; |
|
359 | - $val->save(); |
|
360 | - $ids[] = $val->id; |
|
361 | - break; |
|
362 | - |
|
363 | - /** |
|
364 | - * If the relation is many to many then add it's id to the ids array to |
|
365 | - * attache these ids to the model. |
|
366 | - */ |
|
367 | - case 'BelongsToMany': |
|
368 | - $val->save(); |
|
369 | - $ids[] = $val->id; |
|
370 | - break; |
|
371 | - } |
|
372 | - } |
|
373 | - switch (class_basename($model->$key())) |
|
374 | - { |
|
375 | - /** |
|
376 | - * If the relation is one to many then delete all |
|
377 | - * relations who's id isn't in the ids array. |
|
378 | - */ |
|
379 | - case 'HasMany': |
|
380 | - $model->$key()->whereNotIn('id', $ids)->delete(); |
|
381 | - break; |
|
382 | - |
|
383 | - /** |
|
384 | - * If the relation is many to many then |
|
385 | - * detach the previous data and attach |
|
386 | - * the ids array to the model. |
|
387 | - */ |
|
388 | - case 'BelongsToMany': |
|
389 | - $model->$key()->detach(); |
|
390 | - $model->$key()->attach($ids); |
|
391 | - break; |
|
392 | - } |
|
393 | - } |
|
394 | - /** |
|
395 | - * If the relation isn't array. |
|
396 | - */ |
|
397 | - else |
|
398 | - { |
|
399 | - switch (class_basename($model->$key())) |
|
400 | - { |
|
401 | - /** |
|
402 | - * If the relation is one to many or one to one. |
|
403 | - */ |
|
404 | - case 'HasOne': |
|
405 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
406 | - $value->$foreignKeyName = $model->id; |
|
407 | - $value->save(); |
|
408 | - break; |
|
409 | - } |
|
410 | - } |
|
411 | - } |
|
412 | - |
|
413 | - $saveLog ? \Logging::saveLog(array_key_exists('id', $data) ? 'update' : 'create', class_basename($modelClass), $this->getModel(), $model->id, $model) : false; |
|
414 | - }); |
|
172 | + /** |
|
173 | + * Save the given model to the storage. |
|
174 | + * |
|
175 | + * @param array $data |
|
176 | + * @param boolean $saveLog |
|
177 | + * @return void |
|
178 | + */ |
|
179 | + public function save(array $data, $saveLog = true) |
|
180 | + { |
|
181 | + $model = false; |
|
182 | + $modelClass = $this->model; |
|
183 | + $relations = []; |
|
184 | + |
|
185 | + \DB::transaction(function () use (&$model, &$relations, $data, $saveLog, $modelClass) { |
|
186 | + /** |
|
187 | + * If the id is present in the data then select the model for updating, |
|
188 | + * else create new model. |
|
189 | + * @var array |
|
190 | + */ |
|
191 | + $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
192 | + if ( ! $model) |
|
193 | + { |
|
194 | + \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * Construct the model object with the given data, |
|
199 | + * and if there is a relation add it to relations array, |
|
200 | + * then save the model. |
|
201 | + */ |
|
202 | + foreach ($data as $key => $value) |
|
203 | + { |
|
204 | + /** |
|
205 | + * If the attribute is a relation. |
|
206 | + */ |
|
207 | + $relation = camel_case($key); |
|
208 | + if (method_exists($model, $relation) && \Core::$relation()) |
|
209 | + { |
|
210 | + /** |
|
211 | + * Check if the relation is a collection. |
|
212 | + */ |
|
213 | + if (class_basename($model->$relation) == 'Collection') |
|
214 | + { |
|
215 | + /** |
|
216 | + * If the relation has no value then marke the relation data |
|
217 | + * related to the model to be deleted. |
|
218 | + */ |
|
219 | + if ( ! $value || ! count($value)) |
|
220 | + { |
|
221 | + $relations[$relation] = 'delete'; |
|
222 | + } |
|
223 | + } |
|
224 | + if (is_array($value)) |
|
225 | + { |
|
226 | + /** |
|
227 | + * Loop through the relation data. |
|
228 | + */ |
|
229 | + foreach ($value as $attr => $val) |
|
230 | + { |
|
231 | + /** |
|
232 | + * Get the relation model. |
|
233 | + */ |
|
234 | + $relationBaseModel = \Core::$relation()->model; |
|
235 | + |
|
236 | + /** |
|
237 | + * Check if the relation is a collection. |
|
238 | + */ |
|
239 | + if (class_basename($model->$relation) == 'Collection') |
|
240 | + { |
|
241 | + /** |
|
242 | + * If the id is present in the data then select the relation model for updating, |
|
243 | + * else create new model. |
|
244 | + */ |
|
245 | + $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
246 | + |
|
247 | + /** |
|
248 | + * If model doesn't exists. |
|
249 | + */ |
|
250 | + if ( ! $relationModel) |
|
251 | + { |
|
252 | + \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
253 | + } |
|
254 | + |
|
255 | + /** |
|
256 | + * Loop through the relation attributes. |
|
257 | + */ |
|
258 | + foreach ($val as $attr => $val) |
|
259 | + { |
|
260 | + /** |
|
261 | + * Prevent the sub relations or attributes not in the fillable. |
|
262 | + */ |
|
263 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
264 | + { |
|
265 | + $relationModel->$attr = $val; |
|
266 | + } |
|
267 | + } |
|
268 | + |
|
269 | + $relations[$relation][] = $relationModel; |
|
270 | + } |
|
271 | + /** |
|
272 | + * If not collection. |
|
273 | + */ |
|
274 | + else |
|
275 | + { |
|
276 | + /** |
|
277 | + * Prevent the sub relations. |
|
278 | + */ |
|
279 | + if (gettype($val) !== 'object' && gettype($val) !== 'array') |
|
280 | + { |
|
281 | + |
|
282 | + /** |
|
283 | + * If the id is present in the data then select the relation model for updating, |
|
284 | + * else create new model. |
|
285 | + */ |
|
286 | + $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
287 | + |
|
288 | + /** |
|
289 | + * If model doesn't exists. |
|
290 | + */ |
|
291 | + if ( ! $relationModel) |
|
292 | + { |
|
293 | + \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
294 | + } |
|
295 | + |
|
296 | + foreach ($value as $relationAttribute => $relationValue) |
|
297 | + { |
|
298 | + /** |
|
299 | + * Prevent attributes not in the fillable. |
|
300 | + */ |
|
301 | + if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) |
|
302 | + { |
|
303 | + $relationModel->$relationAttribute = $relationValue; |
|
304 | + } |
|
305 | + } |
|
306 | + |
|
307 | + $relations[$relation] = $relationModel; |
|
308 | + } |
|
309 | + } |
|
310 | + } |
|
311 | + } |
|
312 | + } |
|
313 | + /** |
|
314 | + * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
315 | + */ |
|
316 | + else if (array_search($key, $model->getFillable(), true) !== false) |
|
317 | + { |
|
318 | + $model->$key = $value; |
|
319 | + } |
|
320 | + } |
|
321 | + /** |
|
322 | + * Save the model. |
|
323 | + */ |
|
324 | + $model->save(); |
|
325 | + |
|
326 | + /** |
|
327 | + * Loop through the relations array. |
|
328 | + */ |
|
329 | + foreach ($relations as $key => $value) |
|
330 | + { |
|
331 | + /** |
|
332 | + * If the relation is marked for delete then delete it. |
|
333 | + */ |
|
334 | + if ($value == 'delete' && $model->$key()->count()) |
|
335 | + { |
|
336 | + $model->$key()->delete(); |
|
337 | + } |
|
338 | + /** |
|
339 | + * If the relation is an array. |
|
340 | + */ |
|
341 | + else if (gettype($value) == 'array') |
|
342 | + { |
|
343 | + $ids = []; |
|
344 | + /** |
|
345 | + * Loop through the relations. |
|
346 | + */ |
|
347 | + foreach ($value as $val) |
|
348 | + { |
|
349 | + switch (class_basename($model->$key())) |
|
350 | + { |
|
351 | + /** |
|
352 | + * If the relation is one to many then update it's foreign key with |
|
353 | + * the model id and save it then add its id to ids array to delete all |
|
354 | + * relations who's id isn't in the ids array. |
|
355 | + */ |
|
356 | + case 'HasMany': |
|
357 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
358 | + $val->$foreignKeyName = $model->id; |
|
359 | + $val->save(); |
|
360 | + $ids[] = $val->id; |
|
361 | + break; |
|
362 | + |
|
363 | + /** |
|
364 | + * If the relation is many to many then add it's id to the ids array to |
|
365 | + * attache these ids to the model. |
|
366 | + */ |
|
367 | + case 'BelongsToMany': |
|
368 | + $val->save(); |
|
369 | + $ids[] = $val->id; |
|
370 | + break; |
|
371 | + } |
|
372 | + } |
|
373 | + switch (class_basename($model->$key())) |
|
374 | + { |
|
375 | + /** |
|
376 | + * If the relation is one to many then delete all |
|
377 | + * relations who's id isn't in the ids array. |
|
378 | + */ |
|
379 | + case 'HasMany': |
|
380 | + $model->$key()->whereNotIn('id', $ids)->delete(); |
|
381 | + break; |
|
382 | + |
|
383 | + /** |
|
384 | + * If the relation is many to many then |
|
385 | + * detach the previous data and attach |
|
386 | + * the ids array to the model. |
|
387 | + */ |
|
388 | + case 'BelongsToMany': |
|
389 | + $model->$key()->detach(); |
|
390 | + $model->$key()->attach($ids); |
|
391 | + break; |
|
392 | + } |
|
393 | + } |
|
394 | + /** |
|
395 | + * If the relation isn't array. |
|
396 | + */ |
|
397 | + else |
|
398 | + { |
|
399 | + switch (class_basename($model->$key())) |
|
400 | + { |
|
401 | + /** |
|
402 | + * If the relation is one to many or one to one. |
|
403 | + */ |
|
404 | + case 'HasOne': |
|
405 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
406 | + $value->$foreignKeyName = $model->id; |
|
407 | + $value->save(); |
|
408 | + break; |
|
409 | + } |
|
410 | + } |
|
411 | + } |
|
412 | + |
|
413 | + $saveLog ? \Logging::saveLog(array_key_exists('id', $data) ? 'update' : 'create', class_basename($modelClass), $this->getModel(), $model->id, $model) : false; |
|
414 | + }); |
|
415 | 415 | |
416 | - return $model->id; |
|
417 | - } |
|
416 | + return $model->id; |
|
417 | + } |
|
418 | 418 | |
419 | - /** |
|
420 | - * Update record in the storage based on the given |
|
421 | - * condition. |
|
422 | - * |
|
423 | - * @param var $value condition value |
|
424 | - * @param array $data |
|
425 | - * @param string $attribute condition column name |
|
426 | - * @return void |
|
427 | - */ |
|
428 | - public function update($value, array $data, $attribute = 'id', $saveLog = true) |
|
429 | - { |
|
430 | - if ($attribute == 'id') |
|
431 | - { |
|
432 | - $model = $this->model->lockForUpdate()->find($value); |
|
433 | - $model ? $model->update($data) : 0; |
|
434 | - $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
435 | - } |
|
436 | - else |
|
437 | - { |
|
438 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){ |
|
439 | - $model->update($data); |
|
440 | - $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
441 | - }); |
|
442 | - } |
|
443 | - } |
|
444 | - |
|
445 | - /** |
|
446 | - * Delete record from the storage based on the given |
|
447 | - * condition. |
|
448 | - * |
|
449 | - * @param var $value condition value |
|
450 | - * @param string $attribute condition column name |
|
451 | - * @return void |
|
452 | - */ |
|
453 | - public function delete($value, $attribute = 'id', $saveLog = true) |
|
454 | - { |
|
455 | - if ($attribute == 'id') |
|
456 | - { |
|
457 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
458 | - $model = $this->model->lockForUpdate()->find($value); |
|
459 | - if ( ! $model) |
|
460 | - { |
|
461 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
462 | - } |
|
419 | + /** |
|
420 | + * Update record in the storage based on the given |
|
421 | + * condition. |
|
422 | + * |
|
423 | + * @param var $value condition value |
|
424 | + * @param array $data |
|
425 | + * @param string $attribute condition column name |
|
426 | + * @return void |
|
427 | + */ |
|
428 | + public function update($value, array $data, $attribute = 'id', $saveLog = true) |
|
429 | + { |
|
430 | + if ($attribute == 'id') |
|
431 | + { |
|
432 | + $model = $this->model->lockForUpdate()->find($value); |
|
433 | + $model ? $model->update($data) : 0; |
|
434 | + $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
435 | + } |
|
436 | + else |
|
437 | + { |
|
438 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){ |
|
439 | + $model->update($data); |
|
440 | + $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
441 | + }); |
|
442 | + } |
|
443 | + } |
|
444 | + |
|
445 | + /** |
|
446 | + * Delete record from the storage based on the given |
|
447 | + * condition. |
|
448 | + * |
|
449 | + * @param var $value condition value |
|
450 | + * @param string $attribute condition column name |
|
451 | + * @return void |
|
452 | + */ |
|
453 | + public function delete($value, $attribute = 'id', $saveLog = true) |
|
454 | + { |
|
455 | + if ($attribute == 'id') |
|
456 | + { |
|
457 | + \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
458 | + $model = $this->model->lockForUpdate()->find($value); |
|
459 | + if ( ! $model) |
|
460 | + { |
|
461 | + \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
462 | + } |
|
463 | 463 | |
464 | - $model->delete(); |
|
465 | - $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
466 | - }); |
|
467 | - } |
|
468 | - else |
|
469 | - { |
|
470 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
471 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($saveLog){ |
|
472 | - $model->delete(); |
|
473 | - $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
474 | - }); |
|
475 | - }); |
|
476 | - } |
|
477 | - } |
|
464 | + $model->delete(); |
|
465 | + $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
466 | + }); |
|
467 | + } |
|
468 | + else |
|
469 | + { |
|
470 | + \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
471 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($saveLog){ |
|
472 | + $model->delete(); |
|
473 | + $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
474 | + }); |
|
475 | + }); |
|
476 | + } |
|
477 | + } |
|
478 | 478 | |
479 | - /** |
|
480 | - * Fetch records from the storage based on the given |
|
481 | - * id. |
|
482 | - * |
|
483 | - * @param integer $id |
|
484 | - * @param array $relations |
|
485 | - * @param array $columns |
|
486 | - * @return object |
|
487 | - */ |
|
488 | - public function find($id, $relations = [], $columns = array('*')) |
|
489 | - { |
|
490 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
491 | - } |
|
479 | + /** |
|
480 | + * Fetch records from the storage based on the given |
|
481 | + * id. |
|
482 | + * |
|
483 | + * @param integer $id |
|
484 | + * @param array $relations |
|
485 | + * @param array $columns |
|
486 | + * @return object |
|
487 | + */ |
|
488 | + public function find($id, $relations = [], $columns = array('*')) |
|
489 | + { |
|
490 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
491 | + } |
|
492 | 492 | |
493 | - /** |
|
494 | - * Fetch records from the storage based on the given |
|
495 | - * condition. |
|
496 | - * |
|
497 | - * @param array $conditions array of conditions |
|
498 | - * @param array $relations |
|
499 | - * @param string $sortBy |
|
500 | - * @param boolean $desc |
|
501 | - * @param array $columns |
|
502 | - * @return collection |
|
503 | - */ |
|
504 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
505 | - { |
|
506 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
507 | - $sort = $desc ? 'desc' : 'asc'; |
|
508 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
509 | - } |
|
510 | - |
|
511 | - /** |
|
512 | - * Fetch the first record from the storage based on the given |
|
513 | - * condition. |
|
514 | - * |
|
515 | - * @param array $conditions array of conditions |
|
516 | - * @param array $relations |
|
517 | - * @param array $columns |
|
518 | - * @return object |
|
519 | - */ |
|
520 | - public function first($conditions, $relations = [], $columns = array('*')) |
|
521 | - { |
|
522 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
523 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
524 | - } |
|
525 | - |
|
526 | - /** |
|
527 | - * Return the deleted models in pages based on the given conditions. |
|
528 | - * |
|
529 | - * @param array $conditions array of conditions |
|
530 | - * @param integer $perPage |
|
531 | - * @param string $sortBy |
|
532 | - * @param boolean $desc |
|
533 | - * @param array $columns |
|
534 | - * @return collection |
|
535 | - */ |
|
536 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
537 | - { |
|
538 | - unset($conditions['page']); |
|
539 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
540 | - $sort = $desc ? 'desc' : 'asc'; |
|
541 | - $model = $this->model->onlyTrashed(); |
|
542 | - |
|
543 | - if (count($conditions['conditionValues'])) |
|
544 | - { |
|
545 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
546 | - } |
|
547 | - |
|
548 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);; |
|
549 | - } |
|
550 | - |
|
551 | - /** |
|
552 | - * Restore the deleted model. |
|
553 | - * |
|
554 | - * @param integer $id |
|
555 | - * @return void |
|
556 | - */ |
|
557 | - public function restore($id) |
|
558 | - { |
|
559 | - $model = $this->model->onlyTrashed()->find($id); |
|
560 | - |
|
561 | - if ( ! $model) |
|
562 | - { |
|
563 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id); |
|
564 | - } |
|
565 | - |
|
566 | - $model->restore(); |
|
567 | - } |
|
568 | - |
|
569 | - /** |
|
570 | - * Build the conditions recursively for the retrieving methods. |
|
571 | - * @param array $conditions |
|
572 | - * @return array |
|
573 | - */ |
|
574 | - protected function constructConditions($conditions, $model) |
|
575 | - { |
|
576 | - $conditionString = ''; |
|
577 | - $conditionValues = []; |
|
578 | - foreach ($conditions as $key => $value) |
|
579 | - { |
|
580 | - if ($key == 'and') |
|
581 | - { |
|
582 | - $conditions = $this->constructConditions($value, $model); |
|
583 | - $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
|
584 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
585 | - } |
|
586 | - else if ($key == 'or') |
|
587 | - { |
|
588 | - $conditions = $this->constructConditions($value, $model); |
|
589 | - $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
|
590 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
591 | - } |
|
592 | - else |
|
593 | - { |
|
594 | - if (is_array($value)) |
|
595 | - { |
|
596 | - $operator = $value['op']; |
|
597 | - if (strtolower($operator) == 'between') |
|
598 | - { |
|
599 | - $value1 = $value['val1']; |
|
600 | - $value2 = $value['val2']; |
|
601 | - } |
|
602 | - else |
|
603 | - { |
|
604 | - $value = array_key_exists('val', $value) ? $value['val'] : ''; |
|
605 | - } |
|
606 | - } |
|
607 | - else |
|
608 | - { |
|
609 | - $operator = '='; |
|
610 | - } |
|
493 | + /** |
|
494 | + * Fetch records from the storage based on the given |
|
495 | + * condition. |
|
496 | + * |
|
497 | + * @param array $conditions array of conditions |
|
498 | + * @param array $relations |
|
499 | + * @param string $sortBy |
|
500 | + * @param boolean $desc |
|
501 | + * @param array $columns |
|
502 | + * @return collection |
|
503 | + */ |
|
504 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
505 | + { |
|
506 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
507 | + $sort = $desc ? 'desc' : 'asc'; |
|
508 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
509 | + } |
|
510 | + |
|
511 | + /** |
|
512 | + * Fetch the first record from the storage based on the given |
|
513 | + * condition. |
|
514 | + * |
|
515 | + * @param array $conditions array of conditions |
|
516 | + * @param array $relations |
|
517 | + * @param array $columns |
|
518 | + * @return object |
|
519 | + */ |
|
520 | + public function first($conditions, $relations = [], $columns = array('*')) |
|
521 | + { |
|
522 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
523 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
524 | + } |
|
525 | + |
|
526 | + /** |
|
527 | + * Return the deleted models in pages based on the given conditions. |
|
528 | + * |
|
529 | + * @param array $conditions array of conditions |
|
530 | + * @param integer $perPage |
|
531 | + * @param string $sortBy |
|
532 | + * @param boolean $desc |
|
533 | + * @param array $columns |
|
534 | + * @return collection |
|
535 | + */ |
|
536 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
537 | + { |
|
538 | + unset($conditions['page']); |
|
539 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
540 | + $sort = $desc ? 'desc' : 'asc'; |
|
541 | + $model = $this->model->onlyTrashed(); |
|
542 | + |
|
543 | + if (count($conditions['conditionValues'])) |
|
544 | + { |
|
545 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
546 | + } |
|
547 | + |
|
548 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);; |
|
549 | + } |
|
550 | + |
|
551 | + /** |
|
552 | + * Restore the deleted model. |
|
553 | + * |
|
554 | + * @param integer $id |
|
555 | + * @return void |
|
556 | + */ |
|
557 | + public function restore($id) |
|
558 | + { |
|
559 | + $model = $this->model->onlyTrashed()->find($id); |
|
560 | + |
|
561 | + if ( ! $model) |
|
562 | + { |
|
563 | + \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id); |
|
564 | + } |
|
565 | + |
|
566 | + $model->restore(); |
|
567 | + } |
|
568 | + |
|
569 | + /** |
|
570 | + * Build the conditions recursively for the retrieving methods. |
|
571 | + * @param array $conditions |
|
572 | + * @return array |
|
573 | + */ |
|
574 | + protected function constructConditions($conditions, $model) |
|
575 | + { |
|
576 | + $conditionString = ''; |
|
577 | + $conditionValues = []; |
|
578 | + foreach ($conditions as $key => $value) |
|
579 | + { |
|
580 | + if ($key == 'and') |
|
581 | + { |
|
582 | + $conditions = $this->constructConditions($value, $model); |
|
583 | + $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
|
584 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
585 | + } |
|
586 | + else if ($key == 'or') |
|
587 | + { |
|
588 | + $conditions = $this->constructConditions($value, $model); |
|
589 | + $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
|
590 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
591 | + } |
|
592 | + else |
|
593 | + { |
|
594 | + if (is_array($value)) |
|
595 | + { |
|
596 | + $operator = $value['op']; |
|
597 | + if (strtolower($operator) == 'between') |
|
598 | + { |
|
599 | + $value1 = $value['val1']; |
|
600 | + $value2 = $value['val2']; |
|
601 | + } |
|
602 | + else |
|
603 | + { |
|
604 | + $value = array_key_exists('val', $value) ? $value['val'] : ''; |
|
605 | + } |
|
606 | + } |
|
607 | + else |
|
608 | + { |
|
609 | + $operator = '='; |
|
610 | + } |
|
611 | 611 | |
612 | - if (strtolower($operator) == 'between') |
|
613 | - { |
|
614 | - $conditionString .= $key . ' >= ? and '; |
|
615 | - $conditionValues[] = $value1; |
|
616 | - |
|
617 | - $conditionString .= $key . ' <= ? {op} '; |
|
618 | - $conditionValues[] = $value2; |
|
619 | - } |
|
620 | - elseif (strtolower($operator) == 'in') |
|
621 | - { |
|
622 | - $conditionValues = array_merge($conditionValues, $value); |
|
623 | - $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
624 | - $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
|
625 | - } |
|
626 | - elseif (strtolower($operator) == 'null') |
|
627 | - { |
|
628 | - $conditionString .= $key . ' is null {op} '; |
|
629 | - } |
|
630 | - elseif (strtolower($operator) == 'not null') |
|
631 | - { |
|
632 | - $conditionString .= $key . ' is not null {op} '; |
|
633 | - } |
|
634 | - elseif (strtolower($operator) == 'has') |
|
635 | - { |
|
636 | - $sql = $model->withTrashed()->has($key)->toSql(); |
|
637 | - $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
638 | - $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
|
639 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
640 | - } |
|
641 | - else |
|
642 | - { |
|
643 | - $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
|
644 | - $conditionValues[] = $value; |
|
645 | - } |
|
646 | - } |
|
647 | - } |
|
648 | - $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
649 | - return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
650 | - } |
|
651 | - |
|
652 | - /** |
|
653 | - * Abstract method that return the necessary |
|
654 | - * information (full model namespace) |
|
655 | - * needed to preform the previous actions. |
|
656 | - * |
|
657 | - * @return string |
|
658 | - */ |
|
659 | - abstract protected function getModel(); |
|
612 | + if (strtolower($operator) == 'between') |
|
613 | + { |
|
614 | + $conditionString .= $key . ' >= ? and '; |
|
615 | + $conditionValues[] = $value1; |
|
616 | + |
|
617 | + $conditionString .= $key . ' <= ? {op} '; |
|
618 | + $conditionValues[] = $value2; |
|
619 | + } |
|
620 | + elseif (strtolower($operator) == 'in') |
|
621 | + { |
|
622 | + $conditionValues = array_merge($conditionValues, $value); |
|
623 | + $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
624 | + $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
|
625 | + } |
|
626 | + elseif (strtolower($operator) == 'null') |
|
627 | + { |
|
628 | + $conditionString .= $key . ' is null {op} '; |
|
629 | + } |
|
630 | + elseif (strtolower($operator) == 'not null') |
|
631 | + { |
|
632 | + $conditionString .= $key . ' is not null {op} '; |
|
633 | + } |
|
634 | + elseif (strtolower($operator) == 'has') |
|
635 | + { |
|
636 | + $sql = $model->withTrashed()->has($key)->toSql(); |
|
637 | + $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
638 | + $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
|
639 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
640 | + } |
|
641 | + else |
|
642 | + { |
|
643 | + $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
|
644 | + $conditionValues[] = $value; |
|
645 | + } |
|
646 | + } |
|
647 | + } |
|
648 | + $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
649 | + return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
650 | + } |
|
651 | + |
|
652 | + /** |
|
653 | + * Abstract method that return the necessary |
|
654 | + * information (full model namespace) |
|
655 | + * needed to preform the previous actions. |
|
656 | + * |
|
657 | + * @return string |
|
658 | + */ |
|
659 | + abstract protected function getModel(); |
|
660 | 660 | } |
661 | 661 | \ No newline at end of file |
@@ -63,14 +63,14 @@ discard block |
||
63 | 63 | /** |
64 | 64 | * Construct the select conditions for the model. |
65 | 65 | */ |
66 | - $model->where(function ($q) use ($query, $conditionColumns, $relations){ |
|
66 | + $model->where(function($q) use ($query, $conditionColumns, $relations){ |
|
67 | 67 | |
68 | 68 | if (count($conditionColumns)) |
69 | 69 | { |
70 | 70 | /** |
71 | 71 | * Use the first element in the model columns to construct the first condition. |
72 | 72 | */ |
73 | - $q->where(\DB::raw('LOWER(' . array_shift($conditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
73 | + $q->where(\DB::raw('LOWER('.array_shift($conditionColumns).')'), 'LIKE', '%'.strtolower($query).'%'); |
|
74 | 74 | } |
75 | 75 | |
76 | 76 | /** |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | */ |
79 | 79 | foreach ($conditionColumns as $column) |
80 | 80 | { |
81 | - $q->orWhere(\DB::raw('LOWER(' . $column . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
81 | + $q->orWhere(\DB::raw('LOWER('.$column.')'), 'LIKE', '%'.strtolower($query).'%'); |
|
82 | 82 | } |
83 | 83 | |
84 | 84 | /** |
@@ -99,9 +99,9 @@ discard block |
||
99 | 99 | /** |
100 | 100 | * Construct the relation condition. |
101 | 101 | */ |
102 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
102 | + $q->orWhereHas($relation, function($subModel) use ($query, $relation){ |
|
103 | 103 | |
104 | - $subModel->where(function ($q) use ($query, $relation){ |
|
104 | + $subModel->where(function($q) use ($query, $relation){ |
|
105 | 105 | |
106 | 106 | /** |
107 | 107 | * Get columns of the relation. |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | /** |
114 | 114 | * Use the first element in the relation model columns to construct the first condition. |
115 | 115 | */ |
116 | - $q->where(\DB::raw('LOWER(' . array_shift($subConditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
116 | + $q->where(\DB::raw('LOWER('.array_shift($subConditionColumns).')'), 'LIKE', '%'.strtolower($query).'%'); |
|
117 | 117 | } |
118 | 118 | |
119 | 119 | /** |
@@ -121,7 +121,7 @@ discard block |
||
121 | 121 | */ |
122 | 122 | foreach ($subConditionColumns as $subConditionColumn) |
123 | 123 | { |
124 | - $q->orWhere(\DB::raw('LOWER(' . $subConditionColumn . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
124 | + $q->orWhere(\DB::raw('LOWER('.$subConditionColumn.')'), 'LIKE', '%'.strtolower($query).'%'); |
|
125 | 125 | } |
126 | 126 | }); |
127 | 127 | |
@@ -182,7 +182,7 @@ discard block |
||
182 | 182 | $modelClass = $this->model; |
183 | 183 | $relations = []; |
184 | 184 | |
185 | - \DB::transaction(function () use (&$model, &$relations, $data, $saveLog, $modelClass) { |
|
185 | + \DB::transaction(function() use (&$model, &$relations, $data, $saveLog, $modelClass) { |
|
186 | 186 | /** |
187 | 187 | * If the id is present in the data then select the model for updating, |
188 | 188 | * else create new model. |
@@ -191,7 +191,7 @@ discard block |
||
191 | 191 | $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
192 | 192 | if ( ! $model) |
193 | 193 | { |
194 | - \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
194 | + \ErrorHandler::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
195 | 195 | } |
196 | 196 | |
197 | 197 | /** |
@@ -249,7 +249,7 @@ discard block |
||
249 | 249 | */ |
250 | 250 | if ( ! $relationModel) |
251 | 251 | { |
252 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
252 | + \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
253 | 253 | } |
254 | 254 | |
255 | 255 | /** |
@@ -260,7 +260,7 @@ discard block |
||
260 | 260 | /** |
261 | 261 | * Prevent the sub relations or attributes not in the fillable. |
262 | 262 | */ |
263 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
263 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
264 | 264 | { |
265 | 265 | $relationModel->$attr = $val; |
266 | 266 | } |
@@ -290,7 +290,7 @@ discard block |
||
290 | 290 | */ |
291 | 291 | if ( ! $relationModel) |
292 | 292 | { |
293 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
293 | + \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
294 | 294 | } |
295 | 295 | |
296 | 296 | foreach ($value as $relationAttribute => $relationValue) |
@@ -435,7 +435,7 @@ discard block |
||
435 | 435 | } |
436 | 436 | else |
437 | 437 | { |
438 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){ |
|
438 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function($model) use ($data, $saveLog){ |
|
439 | 439 | $model->update($data); |
440 | 440 | $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
441 | 441 | }); |
@@ -454,11 +454,11 @@ discard block |
||
454 | 454 | { |
455 | 455 | if ($attribute == 'id') |
456 | 456 | { |
457 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
457 | + \DB::transaction(function() use ($value, $attribute, &$result, $saveLog) { |
|
458 | 458 | $model = $this->model->lockForUpdate()->find($value); |
459 | 459 | if ( ! $model) |
460 | 460 | { |
461 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
461 | + \ErrorHandler::notFound(class_basename($this->model).' with id : '.$value); |
|
462 | 462 | } |
463 | 463 | |
464 | 464 | $model->delete(); |
@@ -467,8 +467,8 @@ discard block |
||
467 | 467 | } |
468 | 468 | else |
469 | 469 | { |
470 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
471 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($saveLog){ |
|
470 | + \DB::transaction(function() use ($value, $attribute, &$result, $saveLog) { |
|
471 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function($model) use ($saveLog){ |
|
472 | 472 | $model->delete(); |
473 | 473 | $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
474 | 474 | }); |
@@ -505,7 +505,7 @@ discard block |
||
505 | 505 | { |
506 | 506 | $conditions = $this->constructConditions($conditions, $this->model); |
507 | 507 | $sort = $desc ? 'desc' : 'asc'; |
508 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
508 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
509 | 509 | } |
510 | 510 | |
511 | 511 | /** |
@@ -545,7 +545,7 @@ discard block |
||
545 | 545 | $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
546 | 546 | } |
547 | 547 | |
548 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);; |
|
548 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); ; |
|
549 | 549 | } |
550 | 550 | |
551 | 551 | /** |
@@ -560,7 +560,7 @@ discard block |
||
560 | 560 | |
561 | 561 | if ( ! $model) |
562 | 562 | { |
563 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id); |
|
563 | + \ErrorHandler::notFound(class_basename($this->model).' with id : '.$id); |
|
564 | 564 | } |
565 | 565 | |
566 | 566 | $model->restore(); |
@@ -580,13 +580,13 @@ discard block |
||
580 | 580 | if ($key == 'and') |
581 | 581 | { |
582 | 582 | $conditions = $this->constructConditions($value, $model); |
583 | - $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
|
583 | + $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
584 | 584 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
585 | 585 | } |
586 | 586 | else if ($key == 'or') |
587 | 587 | { |
588 | 588 | $conditions = $this->constructConditions($value, $model); |
589 | - $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
|
589 | + $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
590 | 590 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
591 | 591 | } |
592 | 592 | else |
@@ -611,41 +611,41 @@ discard block |
||
611 | 611 | |
612 | 612 | if (strtolower($operator) == 'between') |
613 | 613 | { |
614 | - $conditionString .= $key . ' >= ? and '; |
|
614 | + $conditionString .= $key.' >= ? and '; |
|
615 | 615 | $conditionValues[] = $value1; |
616 | 616 | |
617 | - $conditionString .= $key . ' <= ? {op} '; |
|
617 | + $conditionString .= $key.' <= ? {op} '; |
|
618 | 618 | $conditionValues[] = $value2; |
619 | 619 | } |
620 | 620 | elseif (strtolower($operator) == 'in') |
621 | 621 | { |
622 | 622 | $conditionValues = array_merge($conditionValues, $value); |
623 | 623 | $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
624 | - $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
|
624 | + $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
625 | 625 | } |
626 | 626 | elseif (strtolower($operator) == 'null') |
627 | 627 | { |
628 | - $conditionString .= $key . ' is null {op} '; |
|
628 | + $conditionString .= $key.' is null {op} '; |
|
629 | 629 | } |
630 | 630 | elseif (strtolower($operator) == 'not null') |
631 | 631 | { |
632 | - $conditionString .= $key . ' is not null {op} '; |
|
632 | + $conditionString .= $key.' is not null {op} '; |
|
633 | 633 | } |
634 | 634 | elseif (strtolower($operator) == 'has') |
635 | 635 | { |
636 | 636 | $sql = $model->withTrashed()->has($key)->toSql(); |
637 | 637 | $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
638 | - $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
|
638 | + $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')').' and '.$conditions['conditionString'].') {op} '; |
|
639 | 639 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
640 | 640 | } |
641 | 641 | else |
642 | 642 | { |
643 | - $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
|
643 | + $conditionString .= $key.' '.$operator.' ? {op} '; |
|
644 | 644 | $conditionValues[] = $value; |
645 | 645 | } |
646 | 646 | } |
647 | 647 | } |
648 | - $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
648 | + $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
649 | 649 | return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
650 | 650 | } |
651 | 651 |
@@ -432,8 +432,7 @@ discard block |
||
432 | 432 | $model = $this->model->lockForUpdate()->find($value); |
433 | 433 | $model ? $model->update($data) : 0; |
434 | 434 | $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $value, $model) : false; |
435 | - } |
|
436 | - else |
|
435 | + } else |
|
437 | 436 | { |
438 | 437 | call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){ |
439 | 438 | $model->update($data); |
@@ -464,8 +463,7 @@ discard block |
||
464 | 463 | $model->delete(); |
465 | 464 | $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false; |
466 | 465 | }); |
467 | - } |
|
468 | - else |
|
466 | + } else |
|
469 | 467 | { |
470 | 468 | \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
471 | 469 | call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($saveLog){ |
@@ -582,14 +580,12 @@ discard block |
||
582 | 580 | $conditions = $this->constructConditions($value, $model); |
583 | 581 | $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
584 | 582 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
585 | - } |
|
586 | - else if ($key == 'or') |
|
583 | + } else if ($key == 'or') |
|
587 | 584 | { |
588 | 585 | $conditions = $this->constructConditions($value, $model); |
589 | 586 | $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
590 | 587 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
591 | - } |
|
592 | - else |
|
588 | + } else |
|
593 | 589 | { |
594 | 590 | if (is_array($value)) |
595 | 591 | { |
@@ -598,13 +594,11 @@ discard block |
||
598 | 594 | { |
599 | 595 | $value1 = $value['val1']; |
600 | 596 | $value2 = $value['val2']; |
601 | - } |
|
602 | - else |
|
597 | + } else |
|
603 | 598 | { |
604 | 599 | $value = array_key_exists('val', $value) ? $value['val'] : ''; |
605 | 600 | } |
606 | - } |
|
607 | - else |
|
601 | + } else |
|
608 | 602 | { |
609 | 603 | $operator = '='; |
610 | 604 | } |
@@ -616,29 +610,24 @@ discard block |
||
616 | 610 | |
617 | 611 | $conditionString .= $key . ' <= ? {op} '; |
618 | 612 | $conditionValues[] = $value2; |
619 | - } |
|
620 | - elseif (strtolower($operator) == 'in') |
|
613 | + } elseif (strtolower($operator) == 'in') |
|
621 | 614 | { |
622 | 615 | $conditionValues = array_merge($conditionValues, $value); |
623 | 616 | $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
624 | 617 | $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
625 | - } |
|
626 | - elseif (strtolower($operator) == 'null') |
|
618 | + } elseif (strtolower($operator) == 'null') |
|
627 | 619 | { |
628 | 620 | $conditionString .= $key . ' is null {op} '; |
629 | - } |
|
630 | - elseif (strtolower($operator) == 'not null') |
|
621 | + } elseif (strtolower($operator) == 'not null') |
|
631 | 622 | { |
632 | 623 | $conditionString .= $key . ' is not null {op} '; |
633 | - } |
|
634 | - elseif (strtolower($operator) == 'has') |
|
624 | + } elseif (strtolower($operator) == 'has') |
|
635 | 625 | { |
636 | 626 | $sql = $model->withTrashed()->has($key)->toSql(); |
637 | 627 | $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
638 | 628 | $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
639 | 629 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
640 | - } |
|
641 | - else |
|
630 | + } else |
|
642 | 631 | { |
643 | 632 | $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
644 | 633 | $conditionValues[] = $value; |
@@ -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\V1\Core\Decorators\CachingDecorator(new $class, $app['cache.store']); |
|
27 | - }); |
|
26 | + return new \App\Modules\V1\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 |
@@ -18,10 +18,10 @@ |
||
18 | 18 | { |
19 | 19 | foreach ($this->getRepoNameSpace() as $repoNameSpace) |
20 | 20 | { |
21 | - $class = rtrim($repoNameSpace, '\\') . '\\' . ucfirst(str_singular($name)) . 'Repository'; |
|
21 | + $class = rtrim($repoNameSpace, '\\').'\\'.ucfirst(str_singular($name)).'Repository'; |
|
22 | 22 | if (class_exists($class)) |
23 | 23 | { |
24 | - \App::singleton($class, function ($app) use ($class) { |
|
24 | + \App::singleton($class, function($app) use ($class) { |
|
25 | 25 | |
26 | 26 | return new \App\Modules\V1\Core\Decorators\CachingDecorator(new $class, $app['cache.store']); |
27 | 27 | }); |