@@ -6,30 +6,30 @@ |
||
6 | 6 | |
7 | 7 | class CreateOauthAuthCodesTable extends Migration |
8 | 8 | { |
9 | - /** |
|
10 | - * Run the migrations. |
|
11 | - * |
|
12 | - * @return void |
|
13 | - */ |
|
14 | - public function up() |
|
15 | - { |
|
16 | - Schema::create('oauth_auth_codes', function (Blueprint $table) { |
|
17 | - $table->string('id', 100)->primary(); |
|
18 | - $table->integer('user_id'); |
|
19 | - $table->integer('client_id'); |
|
20 | - $table->text('scopes')->nullable(); |
|
21 | - $table->boolean('revoked'); |
|
22 | - $table->dateTime('expires_at')->nullable(); |
|
23 | - }); |
|
24 | - } |
|
9 | + /** |
|
10 | + * Run the migrations. |
|
11 | + * |
|
12 | + * @return void |
|
13 | + */ |
|
14 | + public function up() |
|
15 | + { |
|
16 | + Schema::create('oauth_auth_codes', function (Blueprint $table) { |
|
17 | + $table->string('id', 100)->primary(); |
|
18 | + $table->integer('user_id'); |
|
19 | + $table->integer('client_id'); |
|
20 | + $table->text('scopes')->nullable(); |
|
21 | + $table->boolean('revoked'); |
|
22 | + $table->dateTime('expires_at')->nullable(); |
|
23 | + }); |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * Reverse the migrations. |
|
28 | - * |
|
29 | - * @return void |
|
30 | - */ |
|
31 | - public function down() |
|
32 | - { |
|
33 | - Schema::drop('oauth_auth_codes'); |
|
34 | - } |
|
26 | + /** |
|
27 | + * Reverse the migrations. |
|
28 | + * |
|
29 | + * @return void |
|
30 | + */ |
|
31 | + public function down() |
|
32 | + { |
|
33 | + Schema::drop('oauth_auth_codes'); |
|
34 | + } |
|
35 | 35 | } |
@@ -2,105 +2,105 @@ |
||
2 | 2 | |
3 | 3 | class ErrorHandler |
4 | 4 | { |
5 | - public function unAuthorized() |
|
6 | - { |
|
7 | - $error = ['status' => 401, 'message' => trans('errors.unAuthorized')]; |
|
8 | - abort($error['status'], $error['message']); |
|
9 | - } |
|
10 | - |
|
11 | - public function invalidRefreshToken() |
|
12 | - { |
|
13 | - $error = ['status' => 401, 'message' => trans('errors.invalidRefreshToken')]; |
|
14 | - abort($error['status'], $error['message']); |
|
15 | - } |
|
16 | - |
|
17 | - public function noPermissions() |
|
18 | - { |
|
19 | - $error = ['status' => 403, 'message' => trans('errors.noPermissions')]; |
|
20 | - abort($error['status'], $error['message']); |
|
21 | - } |
|
22 | - |
|
23 | - public function loginFailed() |
|
24 | - { |
|
25 | - $error = ['status' => 400, 'message' => trans('errors.loginFailed')]; |
|
26 | - abort($error['status'], $error['message']); |
|
27 | - } |
|
28 | - |
|
29 | - public function noSocialEmail() |
|
30 | - { |
|
31 | - $error = ['status' => 400, 'message' => trans('errors.noSocialEmail')]; |
|
32 | - abort($error['status'], $error['message']); |
|
33 | - } |
|
34 | - |
|
35 | - public function userAlreadyRegistered() |
|
36 | - { |
|
37 | - $error = ['status' => 400, 'message' => trans('errors.userAlreadyRegistered')]; |
|
38 | - abort($error['status'], $error['message']); |
|
39 | - } |
|
40 | - |
|
41 | - public function connectionError() |
|
42 | - { |
|
43 | - $error = ['status' => 400, 'message' => trans('errors.connectionError')]; |
|
44 | - abort($error['status'], $error['message']); |
|
45 | - } |
|
46 | - |
|
47 | - public function redisNotRunning() |
|
48 | - { |
|
49 | - $error = ['status' => 400, 'message' => trans('errors.redisNotRunning')]; |
|
50 | - abort($error['status'], $error['message']); |
|
51 | - } |
|
52 | - |
|
53 | - public function dbQueryError() |
|
54 | - { |
|
55 | - $error = ['status' => 400, 'message' => trans('errors.dbQueryError')]; |
|
56 | - abort($error['status'], $error['message']); |
|
57 | - } |
|
58 | - |
|
59 | - public function cannotCreateSetting() |
|
60 | - { |
|
61 | - $error = ['status' => 400, 'message' => trans('errors.cannotCreateSetting')]; |
|
62 | - abort($error['status'], $error['message']); |
|
63 | - } |
|
64 | - |
|
65 | - public function cannotUpdateSettingKey() |
|
66 | - { |
|
67 | - $error = ['status' => 400, 'message' => trans('errors.cannotUpdateSettingKey')]; |
|
68 | - abort($error['status'], $error['message']); |
|
69 | - } |
|
70 | - |
|
71 | - public function userIsBlocked() |
|
72 | - { |
|
73 | - $error = ['status' => 403, 'message' => trans('errors.userIsBlocked')]; |
|
74 | - abort($error['status'], $error['message']); |
|
75 | - } |
|
76 | - |
|
77 | - public function invalidResetToken() |
|
78 | - { |
|
79 | - $error = ['status' => 400, 'message' => trans('errors.invalidResetToken')]; |
|
80 | - abort($error['status'], $error['message']); |
|
81 | - } |
|
82 | - |
|
83 | - public function invalidResetPassword() |
|
84 | - { |
|
85 | - $error = ['status' => 400, 'message' => trans('errors.invalidResetPassword')]; |
|
86 | - abort($error['status'], $error['message']); |
|
87 | - } |
|
88 | - |
|
89 | - public function invalidOldPassword() |
|
90 | - { |
|
91 | - $error = ['status' => 400, 'message' => trans('errors.invalidOldPassword')]; |
|
92 | - abort($error['status'], $error['message']); |
|
93 | - } |
|
94 | - |
|
95 | - public function notFound($text) |
|
96 | - { |
|
97 | - $error = ['status' => 404, 'message' => trans('errors.notFound', ['replace' => $text])]; |
|
98 | - abort($error['status'], $error['message']); |
|
99 | - } |
|
100 | - |
|
101 | - public function generalError() |
|
102 | - { |
|
103 | - $error = ['status' => 400, 'message' => trans('errors.generalError')]; |
|
104 | - abort($error['status'], $error['message']); |
|
105 | - } |
|
5 | + public function unAuthorized() |
|
6 | + { |
|
7 | + $error = ['status' => 401, 'message' => trans('errors.unAuthorized')]; |
|
8 | + abort($error['status'], $error['message']); |
|
9 | + } |
|
10 | + |
|
11 | + public function invalidRefreshToken() |
|
12 | + { |
|
13 | + $error = ['status' => 401, 'message' => trans('errors.invalidRefreshToken')]; |
|
14 | + abort($error['status'], $error['message']); |
|
15 | + } |
|
16 | + |
|
17 | + public function noPermissions() |
|
18 | + { |
|
19 | + $error = ['status' => 403, 'message' => trans('errors.noPermissions')]; |
|
20 | + abort($error['status'], $error['message']); |
|
21 | + } |
|
22 | + |
|
23 | + public function loginFailed() |
|
24 | + { |
|
25 | + $error = ['status' => 400, 'message' => trans('errors.loginFailed')]; |
|
26 | + abort($error['status'], $error['message']); |
|
27 | + } |
|
28 | + |
|
29 | + public function noSocialEmail() |
|
30 | + { |
|
31 | + $error = ['status' => 400, 'message' => trans('errors.noSocialEmail')]; |
|
32 | + abort($error['status'], $error['message']); |
|
33 | + } |
|
34 | + |
|
35 | + public function userAlreadyRegistered() |
|
36 | + { |
|
37 | + $error = ['status' => 400, 'message' => trans('errors.userAlreadyRegistered')]; |
|
38 | + abort($error['status'], $error['message']); |
|
39 | + } |
|
40 | + |
|
41 | + public function connectionError() |
|
42 | + { |
|
43 | + $error = ['status' => 400, 'message' => trans('errors.connectionError')]; |
|
44 | + abort($error['status'], $error['message']); |
|
45 | + } |
|
46 | + |
|
47 | + public function redisNotRunning() |
|
48 | + { |
|
49 | + $error = ['status' => 400, 'message' => trans('errors.redisNotRunning')]; |
|
50 | + abort($error['status'], $error['message']); |
|
51 | + } |
|
52 | + |
|
53 | + public function dbQueryError() |
|
54 | + { |
|
55 | + $error = ['status' => 400, 'message' => trans('errors.dbQueryError')]; |
|
56 | + abort($error['status'], $error['message']); |
|
57 | + } |
|
58 | + |
|
59 | + public function cannotCreateSetting() |
|
60 | + { |
|
61 | + $error = ['status' => 400, 'message' => trans('errors.cannotCreateSetting')]; |
|
62 | + abort($error['status'], $error['message']); |
|
63 | + } |
|
64 | + |
|
65 | + public function cannotUpdateSettingKey() |
|
66 | + { |
|
67 | + $error = ['status' => 400, 'message' => trans('errors.cannotUpdateSettingKey')]; |
|
68 | + abort($error['status'], $error['message']); |
|
69 | + } |
|
70 | + |
|
71 | + public function userIsBlocked() |
|
72 | + { |
|
73 | + $error = ['status' => 403, 'message' => trans('errors.userIsBlocked')]; |
|
74 | + abort($error['status'], $error['message']); |
|
75 | + } |
|
76 | + |
|
77 | + public function invalidResetToken() |
|
78 | + { |
|
79 | + $error = ['status' => 400, 'message' => trans('errors.invalidResetToken')]; |
|
80 | + abort($error['status'], $error['message']); |
|
81 | + } |
|
82 | + |
|
83 | + public function invalidResetPassword() |
|
84 | + { |
|
85 | + $error = ['status' => 400, 'message' => trans('errors.invalidResetPassword')]; |
|
86 | + abort($error['status'], $error['message']); |
|
87 | + } |
|
88 | + |
|
89 | + public function invalidOldPassword() |
|
90 | + { |
|
91 | + $error = ['status' => 400, 'message' => trans('errors.invalidOldPassword')]; |
|
92 | + abort($error['status'], $error['message']); |
|
93 | + } |
|
94 | + |
|
95 | + public function notFound($text) |
|
96 | + { |
|
97 | + $error = ['status' => 404, 'message' => trans('errors.notFound', ['replace' => $text])]; |
|
98 | + abort($error['status'], $error['message']); |
|
99 | + } |
|
100 | + |
|
101 | + public function generalError() |
|
102 | + { |
|
103 | + $error = ['status' => 400, 'message' => trans('errors.generalError')]; |
|
104 | + abort($error['status'], $error['message']); |
|
105 | + } |
|
106 | 106 | } |
107 | 107 | \ No newline at end of file |
@@ -4,699 +4,699 @@ |
||
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 | - $column = 'LOWER(' . array_shift($conditionColumns) . ')'; |
|
71 | - if (str_contains($column, '->')) |
|
72 | - { |
|
73 | - $column = $this->wrapJsonSelector($column); |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Use the first element in the model columns to construct the first condition. |
|
78 | - */ |
|
79 | - $q->where(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Loop through the rest of the columns to construct or where conditions. |
|
84 | - */ |
|
85 | - foreach ($conditionColumns as $column) |
|
86 | - { |
|
87 | - $column = 'LOWER(' . $column . ')'; |
|
88 | - if (str_contains($column, '->')) |
|
89 | - { |
|
90 | - $column = $this->wrapJsonSelector($column); |
|
91 | - } |
|
92 | - |
|
93 | - $q->orWhere(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Loop through the model relations. |
|
98 | - */ |
|
99 | - foreach ($relations as $relation) |
|
100 | - { |
|
101 | - /** |
|
102 | - * Remove the sub relation if exists. |
|
103 | - */ |
|
104 | - $relation = explode('.', $relation)[0]; |
|
105 | - |
|
106 | - /** |
|
107 | - * Try to fetch the relation repository from the core. |
|
108 | - */ |
|
109 | - if (\Core::$relation()) |
|
110 | - { |
|
111 | - /** |
|
112 | - * Construct the relation condition. |
|
113 | - */ |
|
114 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
115 | - |
|
116 | - $subModel->where(function ($q) use ($query, $relation){ |
|
117 | - |
|
118 | - /** |
|
119 | - * Get columns of the relation. |
|
120 | - */ |
|
121 | - $subConditionColumns = \Core::$relation()->model->searchable; |
|
122 | - |
|
123 | - if (count($subConditionColumns)) |
|
124 | - { |
|
125 | - |
|
126 | - $column = 'LOWER(' . array_shift($subConditionColumns) . ')'; |
|
127 | - if (str_contains($column, '->')) |
|
128 | - { |
|
129 | - $column = $this->wrapJsonSelector($column); |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Use the first element in the relation model columns to construct the first condition. |
|
134 | - */ |
|
135 | - $q->where(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * Loop through the rest of the columns to construct or where conditions. |
|
140 | - */ |
|
141 | - foreach ($subConditionColumns as $subConditionColumn) |
|
142 | - { |
|
143 | - $column = 'LOWER(' . $subConditionColumn . ')'; |
|
144 | - if (str_contains($column, '->')) |
|
145 | - { |
|
146 | - $column = $this->wrapJsonSelector($column); |
|
147 | - } |
|
21 | + /** |
|
22 | + * Create new AbstractRepository instance. |
|
23 | + */ |
|
24 | + public function __construct() |
|
25 | + { |
|
26 | + $this->config = \CoreConfig::getConfig(); |
|
27 | + $this->model = \App::make($this->getModel()); |
|
28 | + } |
|
29 | + |
|
30 | + /** |
|
31 | + * Fetch all records with relations from the storage. |
|
32 | + * |
|
33 | + * @param array $relations |
|
34 | + * @param string $sortBy |
|
35 | + * @param boolean $desc |
|
36 | + * @param array $columns |
|
37 | + * @return collection |
|
38 | + */ |
|
39 | + public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
40 | + { |
|
41 | + $sort = $desc ? 'desc' : 'asc'; |
|
42 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->get($columns); |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * Fetch all records with relations from storage in pages |
|
47 | + * that matche the given query. |
|
48 | + * |
|
49 | + * @param string $query |
|
50 | + * @param integer $perPage |
|
51 | + * @param array $relations |
|
52 | + * @param string $sortBy |
|
53 | + * @param boolean $desc |
|
54 | + * @param array $columns |
|
55 | + * @return collection |
|
56 | + */ |
|
57 | + public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
58 | + { |
|
59 | + $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
60 | + $conditionColumns = $this->model->searchable; |
|
61 | + $sort = $desc ? 'desc' : 'asc'; |
|
62 | + |
|
63 | + /** |
|
64 | + * Construct the select conditions for the model. |
|
65 | + */ |
|
66 | + $model->where(function ($q) use ($query, $conditionColumns, $relations){ |
|
67 | + |
|
68 | + if (count($conditionColumns)) |
|
69 | + { |
|
70 | + $column = 'LOWER(' . array_shift($conditionColumns) . ')'; |
|
71 | + if (str_contains($column, '->')) |
|
72 | + { |
|
73 | + $column = $this->wrapJsonSelector($column); |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Use the first element in the model columns to construct the first condition. |
|
78 | + */ |
|
79 | + $q->where(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Loop through the rest of the columns to construct or where conditions. |
|
84 | + */ |
|
85 | + foreach ($conditionColumns as $column) |
|
86 | + { |
|
87 | + $column = 'LOWER(' . $column . ')'; |
|
88 | + if (str_contains($column, '->')) |
|
89 | + { |
|
90 | + $column = $this->wrapJsonSelector($column); |
|
91 | + } |
|
92 | + |
|
93 | + $q->orWhere(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Loop through the model relations. |
|
98 | + */ |
|
99 | + foreach ($relations as $relation) |
|
100 | + { |
|
101 | + /** |
|
102 | + * Remove the sub relation if exists. |
|
103 | + */ |
|
104 | + $relation = explode('.', $relation)[0]; |
|
105 | + |
|
106 | + /** |
|
107 | + * Try to fetch the relation repository from the core. |
|
108 | + */ |
|
109 | + if (\Core::$relation()) |
|
110 | + { |
|
111 | + /** |
|
112 | + * Construct the relation condition. |
|
113 | + */ |
|
114 | + $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
115 | + |
|
116 | + $subModel->where(function ($q) use ($query, $relation){ |
|
117 | + |
|
118 | + /** |
|
119 | + * Get columns of the relation. |
|
120 | + */ |
|
121 | + $subConditionColumns = \Core::$relation()->model->searchable; |
|
122 | + |
|
123 | + if (count($subConditionColumns)) |
|
124 | + { |
|
125 | + |
|
126 | + $column = 'LOWER(' . array_shift($subConditionColumns) . ')'; |
|
127 | + if (str_contains($column, '->')) |
|
128 | + { |
|
129 | + $column = $this->wrapJsonSelector($column); |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Use the first element in the relation model columns to construct the first condition. |
|
134 | + */ |
|
135 | + $q->where(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * Loop through the rest of the columns to construct or where conditions. |
|
140 | + */ |
|
141 | + foreach ($subConditionColumns as $subConditionColumn) |
|
142 | + { |
|
143 | + $column = 'LOWER(' . $subConditionColumn . ')'; |
|
144 | + if (str_contains($column, '->')) |
|
145 | + { |
|
146 | + $column = $this->wrapJsonSelector($column); |
|
147 | + } |
|
148 | 148 | |
149 | - $q->orWhere(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
150 | - } |
|
151 | - }); |
|
152 | - |
|
153 | - }); |
|
154 | - } |
|
155 | - } |
|
156 | - }); |
|
149 | + $q->orWhere(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
150 | + } |
|
151 | + }); |
|
152 | + |
|
153 | + }); |
|
154 | + } |
|
155 | + } |
|
156 | + }); |
|
157 | 157 | |
158 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
159 | - } |
|
158 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
159 | + } |
|
160 | 160 | |
161 | - /** |
|
162 | - * Fetch all records with relations from storage in pages. |
|
163 | - * |
|
164 | - * @param integer $perPage |
|
165 | - * @param array $relations |
|
166 | - * @param string $sortBy |
|
167 | - * @param boolean $desc |
|
168 | - * @param array $columns |
|
169 | - * @return collection |
|
170 | - */ |
|
171 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
172 | - { |
|
173 | - $sort = $desc ? 'desc' : 'asc'; |
|
174 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * Fetch all records with relations based on |
|
179 | - * the given condition from storage in pages. |
|
180 | - * |
|
181 | - * @param array $conditions array of conditions |
|
182 | - * @param integer $perPage |
|
183 | - * @param array $relations |
|
184 | - * @param string $sortBy |
|
185 | - * @param boolean $desc |
|
186 | - * @param array $columns |
|
187 | - * @return collection |
|
188 | - */ |
|
189 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
190 | - { |
|
191 | - unset($conditions['page']); |
|
192 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
193 | - $sort = $desc ? 'desc' : 'asc'; |
|
194 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
195 | - } |
|
161 | + /** |
|
162 | + * Fetch all records with relations from storage in pages. |
|
163 | + * |
|
164 | + * @param integer $perPage |
|
165 | + * @param array $relations |
|
166 | + * @param string $sortBy |
|
167 | + * @param boolean $desc |
|
168 | + * @param array $columns |
|
169 | + * @return collection |
|
170 | + */ |
|
171 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
172 | + { |
|
173 | + $sort = $desc ? 'desc' : 'asc'; |
|
174 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * Fetch all records with relations based on |
|
179 | + * the given condition from storage in pages. |
|
180 | + * |
|
181 | + * @param array $conditions array of conditions |
|
182 | + * @param integer $perPage |
|
183 | + * @param array $relations |
|
184 | + * @param string $sortBy |
|
185 | + * @param boolean $desc |
|
186 | + * @param array $columns |
|
187 | + * @return collection |
|
188 | + */ |
|
189 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
190 | + { |
|
191 | + unset($conditions['page']); |
|
192 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
193 | + $sort = $desc ? 'desc' : 'asc'; |
|
194 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
195 | + } |
|
196 | 196 | |
197 | - /** |
|
198 | - * Save the given model to the storage. |
|
199 | - * |
|
200 | - * @param array $data |
|
201 | - * @return void |
|
202 | - */ |
|
203 | - public function save(array $data) |
|
204 | - { |
|
205 | - $model = false; |
|
206 | - $modelClass = $this->model; |
|
207 | - $relations = []; |
|
208 | - |
|
209 | - \DB::transaction(function () use (&$model, &$relations, $data, $modelClass) { |
|
210 | - /** |
|
211 | - * If the id is present in the data then select the model for updating, |
|
212 | - * else create new model. |
|
213 | - * @var array |
|
214 | - */ |
|
215 | - $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
216 | - if ( ! $model) |
|
217 | - { |
|
218 | - \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * Construct the model object with the given data, |
|
223 | - * and if there is a relation add it to relations array, |
|
224 | - * then save the model. |
|
225 | - */ |
|
226 | - foreach ($data as $key => $value) |
|
227 | - { |
|
228 | - /** |
|
229 | - * If the attribute is a relation. |
|
230 | - */ |
|
231 | - $relation = camel_case($key); |
|
232 | - if (method_exists($model, $relation) && \Core::$relation()) |
|
233 | - { |
|
234 | - /** |
|
235 | - * Check if the relation is a collection. |
|
236 | - */ |
|
237 | - if (class_basename($model->$relation) == 'Collection') |
|
238 | - { |
|
239 | - /** |
|
240 | - * If the relation has no value then marke the relation data |
|
241 | - * related to the model to be deleted. |
|
242 | - */ |
|
243 | - if ( ! $value || ! count($value)) |
|
244 | - { |
|
245 | - $relations[$relation] = 'delete'; |
|
246 | - } |
|
247 | - } |
|
248 | - if (is_array($value)) |
|
249 | - { |
|
250 | - /** |
|
251 | - * Loop through the relation data. |
|
252 | - */ |
|
253 | - foreach ($value as $attr => $val) |
|
254 | - { |
|
255 | - /** |
|
256 | - * Get the relation model. |
|
257 | - */ |
|
258 | - $relationBaseModel = \Core::$relation()->model; |
|
259 | - |
|
260 | - /** |
|
261 | - * Check if the relation is a collection. |
|
262 | - */ |
|
263 | - if (class_basename($model->$relation) == 'Collection') |
|
264 | - { |
|
265 | - /** |
|
266 | - * If the id is present in the data then select the relation model for updating, |
|
267 | - * else create new model. |
|
268 | - */ |
|
269 | - $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
270 | - |
|
271 | - /** |
|
272 | - * If model doesn't exists. |
|
273 | - */ |
|
274 | - if ( ! $relationModel) |
|
275 | - { |
|
276 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * Loop through the relation attributes. |
|
281 | - */ |
|
282 | - foreach ($val as $attr => $val) |
|
283 | - { |
|
284 | - /** |
|
285 | - * Prevent the sub relations or attributes not in the fillable. |
|
286 | - */ |
|
287 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
288 | - { |
|
289 | - $relationModel->$attr = $val; |
|
290 | - } |
|
291 | - } |
|
292 | - |
|
293 | - $relations[$relation][] = $relationModel; |
|
294 | - } |
|
295 | - /** |
|
296 | - * If not collection. |
|
297 | - */ |
|
298 | - else |
|
299 | - { |
|
300 | - /** |
|
301 | - * Prevent the sub relations. |
|
302 | - */ |
|
303 | - if (gettype($val) !== 'object' && gettype($val) !== 'array') |
|
304 | - { |
|
305 | - |
|
306 | - /** |
|
307 | - * If the id is present in the data then select the relation model for updating, |
|
308 | - * else create new model. |
|
309 | - */ |
|
310 | - $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
311 | - |
|
312 | - /** |
|
313 | - * If model doesn't exists. |
|
314 | - */ |
|
315 | - if ( ! $relationModel) |
|
316 | - { |
|
317 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
318 | - } |
|
319 | - |
|
320 | - foreach ($value as $relationAttribute => $relationValue) |
|
321 | - { |
|
322 | - /** |
|
323 | - * Prevent attributes not in the fillable. |
|
324 | - */ |
|
325 | - if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) |
|
326 | - { |
|
327 | - $relationModel->$relationAttribute = $relationValue; |
|
328 | - } |
|
329 | - } |
|
330 | - |
|
331 | - $relations[$relation] = $relationModel; |
|
332 | - } |
|
333 | - } |
|
334 | - } |
|
335 | - } |
|
336 | - } |
|
337 | - /** |
|
338 | - * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
339 | - */ |
|
340 | - else if (array_search($key, $model->getFillable(), true) !== false) |
|
341 | - { |
|
342 | - $model->$key = $value; |
|
343 | - } |
|
344 | - } |
|
345 | - /** |
|
346 | - * Save the model. |
|
347 | - */ |
|
348 | - $model->save(); |
|
349 | - |
|
350 | - /** |
|
351 | - * Loop through the relations array. |
|
352 | - */ |
|
353 | - foreach ($relations as $key => $value) |
|
354 | - { |
|
355 | - /** |
|
356 | - * If the relation is marked for delete then delete it. |
|
357 | - */ |
|
358 | - if ($value == 'delete' && $model->$key()->count()) |
|
359 | - { |
|
360 | - $model->$key()->delete(); |
|
361 | - } |
|
362 | - /** |
|
363 | - * If the relation is an array. |
|
364 | - */ |
|
365 | - else if (gettype($value) == 'array') |
|
366 | - { |
|
367 | - $ids = []; |
|
368 | - /** |
|
369 | - * Loop through the relations. |
|
370 | - */ |
|
371 | - foreach ($value as $val) |
|
372 | - { |
|
373 | - switch (class_basename($model->$key())) |
|
374 | - { |
|
375 | - /** |
|
376 | - * If the relation is one to many then update it's foreign key with |
|
377 | - * the model id and save it then add its id to ids array to delete all |
|
378 | - * relations who's id isn't in the ids array. |
|
379 | - */ |
|
380 | - case 'HasMany': |
|
381 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
382 | - $val->$foreignKeyName = $model->id; |
|
383 | - $val->save(); |
|
384 | - $ids[] = $val->id; |
|
385 | - break; |
|
386 | - |
|
387 | - /** |
|
388 | - * If the relation is many to many then add it's id to the ids array to |
|
389 | - * attache these ids to the model. |
|
390 | - */ |
|
391 | - case 'BelongsToMany': |
|
392 | - $val->save(); |
|
393 | - $ids[] = $val->id; |
|
394 | - break; |
|
395 | - } |
|
396 | - } |
|
397 | - switch (class_basename($model->$key())) |
|
398 | - { |
|
399 | - /** |
|
400 | - * If the relation is one to many then delete all |
|
401 | - * relations who's id isn't in the ids array. |
|
402 | - */ |
|
403 | - case 'HasMany': |
|
404 | - $model->$key()->whereNotIn('id', $ids)->delete(); |
|
405 | - break; |
|
406 | - |
|
407 | - /** |
|
408 | - * If the relation is many to many then |
|
409 | - * detach the previous data and attach |
|
410 | - * the ids array to the model. |
|
411 | - */ |
|
412 | - case 'BelongsToMany': |
|
413 | - $model->$key()->detach(); |
|
414 | - $model->$key()->attach($ids); |
|
415 | - break; |
|
416 | - } |
|
417 | - } |
|
418 | - /** |
|
419 | - * If the relation isn't array. |
|
420 | - */ |
|
421 | - else |
|
422 | - { |
|
423 | - switch (class_basename($model->$key())) |
|
424 | - { |
|
425 | - /** |
|
426 | - * If the relation is one to many or one to one. |
|
427 | - */ |
|
428 | - case 'HasOne': |
|
429 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
430 | - $value->$foreignKeyName = $model->id; |
|
431 | - $value->save(); |
|
432 | - break; |
|
433 | - } |
|
434 | - } |
|
435 | - } |
|
436 | - }); |
|
197 | + /** |
|
198 | + * Save the given model to the storage. |
|
199 | + * |
|
200 | + * @param array $data |
|
201 | + * @return void |
|
202 | + */ |
|
203 | + public function save(array $data) |
|
204 | + { |
|
205 | + $model = false; |
|
206 | + $modelClass = $this->model; |
|
207 | + $relations = []; |
|
208 | + |
|
209 | + \DB::transaction(function () use (&$model, &$relations, $data, $modelClass) { |
|
210 | + /** |
|
211 | + * If the id is present in the data then select the model for updating, |
|
212 | + * else create new model. |
|
213 | + * @var array |
|
214 | + */ |
|
215 | + $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
216 | + if ( ! $model) |
|
217 | + { |
|
218 | + \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * Construct the model object with the given data, |
|
223 | + * and if there is a relation add it to relations array, |
|
224 | + * then save the model. |
|
225 | + */ |
|
226 | + foreach ($data as $key => $value) |
|
227 | + { |
|
228 | + /** |
|
229 | + * If the attribute is a relation. |
|
230 | + */ |
|
231 | + $relation = camel_case($key); |
|
232 | + if (method_exists($model, $relation) && \Core::$relation()) |
|
233 | + { |
|
234 | + /** |
|
235 | + * Check if the relation is a collection. |
|
236 | + */ |
|
237 | + if (class_basename($model->$relation) == 'Collection') |
|
238 | + { |
|
239 | + /** |
|
240 | + * If the relation has no value then marke the relation data |
|
241 | + * related to the model to be deleted. |
|
242 | + */ |
|
243 | + if ( ! $value || ! count($value)) |
|
244 | + { |
|
245 | + $relations[$relation] = 'delete'; |
|
246 | + } |
|
247 | + } |
|
248 | + if (is_array($value)) |
|
249 | + { |
|
250 | + /** |
|
251 | + * Loop through the relation data. |
|
252 | + */ |
|
253 | + foreach ($value as $attr => $val) |
|
254 | + { |
|
255 | + /** |
|
256 | + * Get the relation model. |
|
257 | + */ |
|
258 | + $relationBaseModel = \Core::$relation()->model; |
|
259 | + |
|
260 | + /** |
|
261 | + * Check if the relation is a collection. |
|
262 | + */ |
|
263 | + if (class_basename($model->$relation) == 'Collection') |
|
264 | + { |
|
265 | + /** |
|
266 | + * If the id is present in the data then select the relation model for updating, |
|
267 | + * else create new model. |
|
268 | + */ |
|
269 | + $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
270 | + |
|
271 | + /** |
|
272 | + * If model doesn't exists. |
|
273 | + */ |
|
274 | + if ( ! $relationModel) |
|
275 | + { |
|
276 | + \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * Loop through the relation attributes. |
|
281 | + */ |
|
282 | + foreach ($val as $attr => $val) |
|
283 | + { |
|
284 | + /** |
|
285 | + * Prevent the sub relations or attributes not in the fillable. |
|
286 | + */ |
|
287 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
288 | + { |
|
289 | + $relationModel->$attr = $val; |
|
290 | + } |
|
291 | + } |
|
292 | + |
|
293 | + $relations[$relation][] = $relationModel; |
|
294 | + } |
|
295 | + /** |
|
296 | + * If not collection. |
|
297 | + */ |
|
298 | + else |
|
299 | + { |
|
300 | + /** |
|
301 | + * Prevent the sub relations. |
|
302 | + */ |
|
303 | + if (gettype($val) !== 'object' && gettype($val) !== 'array') |
|
304 | + { |
|
305 | + |
|
306 | + /** |
|
307 | + * If the id is present in the data then select the relation model for updating, |
|
308 | + * else create new model. |
|
309 | + */ |
|
310 | + $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
311 | + |
|
312 | + /** |
|
313 | + * If model doesn't exists. |
|
314 | + */ |
|
315 | + if ( ! $relationModel) |
|
316 | + { |
|
317 | + \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
318 | + } |
|
319 | + |
|
320 | + foreach ($value as $relationAttribute => $relationValue) |
|
321 | + { |
|
322 | + /** |
|
323 | + * Prevent attributes not in the fillable. |
|
324 | + */ |
|
325 | + if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) |
|
326 | + { |
|
327 | + $relationModel->$relationAttribute = $relationValue; |
|
328 | + } |
|
329 | + } |
|
330 | + |
|
331 | + $relations[$relation] = $relationModel; |
|
332 | + } |
|
333 | + } |
|
334 | + } |
|
335 | + } |
|
336 | + } |
|
337 | + /** |
|
338 | + * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
339 | + */ |
|
340 | + else if (array_search($key, $model->getFillable(), true) !== false) |
|
341 | + { |
|
342 | + $model->$key = $value; |
|
343 | + } |
|
344 | + } |
|
345 | + /** |
|
346 | + * Save the model. |
|
347 | + */ |
|
348 | + $model->save(); |
|
349 | + |
|
350 | + /** |
|
351 | + * Loop through the relations array. |
|
352 | + */ |
|
353 | + foreach ($relations as $key => $value) |
|
354 | + { |
|
355 | + /** |
|
356 | + * If the relation is marked for delete then delete it. |
|
357 | + */ |
|
358 | + if ($value == 'delete' && $model->$key()->count()) |
|
359 | + { |
|
360 | + $model->$key()->delete(); |
|
361 | + } |
|
362 | + /** |
|
363 | + * If the relation is an array. |
|
364 | + */ |
|
365 | + else if (gettype($value) == 'array') |
|
366 | + { |
|
367 | + $ids = []; |
|
368 | + /** |
|
369 | + * Loop through the relations. |
|
370 | + */ |
|
371 | + foreach ($value as $val) |
|
372 | + { |
|
373 | + switch (class_basename($model->$key())) |
|
374 | + { |
|
375 | + /** |
|
376 | + * If the relation is one to many then update it's foreign key with |
|
377 | + * the model id and save it then add its id to ids array to delete all |
|
378 | + * relations who's id isn't in the ids array. |
|
379 | + */ |
|
380 | + case 'HasMany': |
|
381 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
382 | + $val->$foreignKeyName = $model->id; |
|
383 | + $val->save(); |
|
384 | + $ids[] = $val->id; |
|
385 | + break; |
|
386 | + |
|
387 | + /** |
|
388 | + * If the relation is many to many then add it's id to the ids array to |
|
389 | + * attache these ids to the model. |
|
390 | + */ |
|
391 | + case 'BelongsToMany': |
|
392 | + $val->save(); |
|
393 | + $ids[] = $val->id; |
|
394 | + break; |
|
395 | + } |
|
396 | + } |
|
397 | + switch (class_basename($model->$key())) |
|
398 | + { |
|
399 | + /** |
|
400 | + * If the relation is one to many then delete all |
|
401 | + * relations who's id isn't in the ids array. |
|
402 | + */ |
|
403 | + case 'HasMany': |
|
404 | + $model->$key()->whereNotIn('id', $ids)->delete(); |
|
405 | + break; |
|
406 | + |
|
407 | + /** |
|
408 | + * If the relation is many to many then |
|
409 | + * detach the previous data and attach |
|
410 | + * the ids array to the model. |
|
411 | + */ |
|
412 | + case 'BelongsToMany': |
|
413 | + $model->$key()->detach(); |
|
414 | + $model->$key()->attach($ids); |
|
415 | + break; |
|
416 | + } |
|
417 | + } |
|
418 | + /** |
|
419 | + * If the relation isn't array. |
|
420 | + */ |
|
421 | + else |
|
422 | + { |
|
423 | + switch (class_basename($model->$key())) |
|
424 | + { |
|
425 | + /** |
|
426 | + * If the relation is one to many or one to one. |
|
427 | + */ |
|
428 | + case 'HasOne': |
|
429 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
430 | + $value->$foreignKeyName = $model->id; |
|
431 | + $value->save(); |
|
432 | + break; |
|
433 | + } |
|
434 | + } |
|
435 | + } |
|
436 | + }); |
|
437 | 437 | |
438 | - return $model->id; |
|
439 | - } |
|
438 | + return $model->id; |
|
439 | + } |
|
440 | 440 | |
441 | - /** |
|
442 | - * Update record in the storage based on the given |
|
443 | - * condition. |
|
444 | - * |
|
445 | - * @param var $value condition value |
|
446 | - * @param array $data |
|
447 | - * @param string $attribute condition column name |
|
448 | - * @return void |
|
449 | - */ |
|
450 | - public function update($value, array $data, $attribute = 'id') |
|
451 | - { |
|
452 | - if ($attribute == 'id') |
|
453 | - { |
|
454 | - $model = $this->model->lockForUpdate()->find($value); |
|
455 | - $model ? $model->update($data) : 0; |
|
456 | - } |
|
457 | - else |
|
458 | - { |
|
459 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data){ |
|
460 | - $model->update($data); |
|
461 | - }); |
|
462 | - } |
|
463 | - } |
|
464 | - |
|
465 | - /** |
|
466 | - * Delete record from the storage based on the given |
|
467 | - * condition. |
|
468 | - * |
|
469 | - * @param var $value condition value |
|
470 | - * @param string $attribute condition column name |
|
471 | - * @return void |
|
472 | - */ |
|
473 | - public function delete($value, $attribute = 'id') |
|
474 | - { |
|
475 | - if ($attribute == 'id') |
|
476 | - { |
|
477 | - \DB::transaction(function () use ($value, $attribute, &$result) { |
|
478 | - $model = $this->model->lockForUpdate()->find($value); |
|
479 | - if ( ! $model) |
|
480 | - { |
|
481 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
482 | - } |
|
441 | + /** |
|
442 | + * Update record in the storage based on the given |
|
443 | + * condition. |
|
444 | + * |
|
445 | + * @param var $value condition value |
|
446 | + * @param array $data |
|
447 | + * @param string $attribute condition column name |
|
448 | + * @return void |
|
449 | + */ |
|
450 | + public function update($value, array $data, $attribute = 'id') |
|
451 | + { |
|
452 | + if ($attribute == 'id') |
|
453 | + { |
|
454 | + $model = $this->model->lockForUpdate()->find($value); |
|
455 | + $model ? $model->update($data) : 0; |
|
456 | + } |
|
457 | + else |
|
458 | + { |
|
459 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data){ |
|
460 | + $model->update($data); |
|
461 | + }); |
|
462 | + } |
|
463 | + } |
|
464 | + |
|
465 | + /** |
|
466 | + * Delete record from the storage based on the given |
|
467 | + * condition. |
|
468 | + * |
|
469 | + * @param var $value condition value |
|
470 | + * @param string $attribute condition column name |
|
471 | + * @return void |
|
472 | + */ |
|
473 | + public function delete($value, $attribute = 'id') |
|
474 | + { |
|
475 | + if ($attribute == 'id') |
|
476 | + { |
|
477 | + \DB::transaction(function () use ($value, $attribute, &$result) { |
|
478 | + $model = $this->model->lockForUpdate()->find($value); |
|
479 | + if ( ! $model) |
|
480 | + { |
|
481 | + \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
482 | + } |
|
483 | 483 | |
484 | - $model->delete(); |
|
485 | - }); |
|
486 | - } |
|
487 | - else |
|
488 | - { |
|
489 | - \DB::transaction(function () use ($value, $attribute, &$result) { |
|
490 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ |
|
491 | - $model->delete(); |
|
492 | - }); |
|
493 | - }); |
|
494 | - } |
|
495 | - } |
|
484 | + $model->delete(); |
|
485 | + }); |
|
486 | + } |
|
487 | + else |
|
488 | + { |
|
489 | + \DB::transaction(function () use ($value, $attribute, &$result) { |
|
490 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ |
|
491 | + $model->delete(); |
|
492 | + }); |
|
493 | + }); |
|
494 | + } |
|
495 | + } |
|
496 | 496 | |
497 | - /** |
|
498 | - * Fetch records from the storage based on the given |
|
499 | - * id. |
|
500 | - * |
|
501 | - * @param integer $id |
|
502 | - * @param array $relations |
|
503 | - * @param array $columns |
|
504 | - * @return object |
|
505 | - */ |
|
506 | - public function find($id, $relations = [], $columns = array('*')) |
|
507 | - { |
|
508 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
509 | - } |
|
497 | + /** |
|
498 | + * Fetch records from the storage based on the given |
|
499 | + * id. |
|
500 | + * |
|
501 | + * @param integer $id |
|
502 | + * @param array $relations |
|
503 | + * @param array $columns |
|
504 | + * @return object |
|
505 | + */ |
|
506 | + public function find($id, $relations = [], $columns = array('*')) |
|
507 | + { |
|
508 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
509 | + } |
|
510 | 510 | |
511 | - /** |
|
512 | - * Fetch records from the storage based on the given |
|
513 | - * condition. |
|
514 | - * |
|
515 | - * @param array $conditions array of conditions |
|
516 | - * @param array $relations |
|
517 | - * @param string $sortBy |
|
518 | - * @param boolean $desc |
|
519 | - * @param array $columns |
|
520 | - * @return collection |
|
521 | - */ |
|
522 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
523 | - { |
|
524 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
525 | - $sort = $desc ? 'desc' : 'asc'; |
|
526 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
527 | - } |
|
528 | - |
|
529 | - /** |
|
530 | - * Fetch the first record from the storage based on the given |
|
531 | - * condition. |
|
532 | - * |
|
533 | - * @param array $conditions array of conditions |
|
534 | - * @param array $relations |
|
535 | - * @param array $columns |
|
536 | - * @return object |
|
537 | - */ |
|
538 | - public function first($conditions, $relations = [], $columns = array('*')) |
|
539 | - { |
|
540 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
541 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
542 | - } |
|
543 | - |
|
544 | - /** |
|
545 | - * Return the deleted models in pages based on the given conditions. |
|
546 | - * |
|
547 | - * @param array $conditions array of conditions |
|
548 | - * @param integer $perPage |
|
549 | - * @param string $sortBy |
|
550 | - * @param boolean $desc |
|
551 | - * @param array $columns |
|
552 | - * @return collection |
|
553 | - */ |
|
554 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
555 | - { |
|
556 | - unset($conditions['page']); |
|
557 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
558 | - $sort = $desc ? 'desc' : 'asc'; |
|
559 | - $model = $this->model->onlyTrashed(); |
|
560 | - |
|
561 | - if (count($conditions['conditionValues'])) |
|
562 | - { |
|
563 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
564 | - } |
|
565 | - |
|
566 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);; |
|
567 | - } |
|
568 | - |
|
569 | - /** |
|
570 | - * Restore the deleted model. |
|
571 | - * |
|
572 | - * @param integer $id |
|
573 | - * @return void |
|
574 | - */ |
|
575 | - public function restore($id) |
|
576 | - { |
|
577 | - $model = $this->model->onlyTrashed()->find($id); |
|
578 | - |
|
579 | - if ( ! $model) |
|
580 | - { |
|
581 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id); |
|
582 | - } |
|
583 | - |
|
584 | - $model->restore(); |
|
585 | - } |
|
586 | - |
|
587 | - /** |
|
588 | - * Build the conditions recursively for the retrieving methods. |
|
589 | - * @param array $conditions |
|
590 | - * @return array |
|
591 | - */ |
|
592 | - protected function constructConditions($conditions, $model) |
|
593 | - { |
|
594 | - $conditionString = ''; |
|
595 | - $conditionValues = []; |
|
596 | - foreach ($conditions as $key => $value) |
|
597 | - { |
|
598 | - if (str_contains($key, '->')) |
|
599 | - { |
|
600 | - $key = $this->wrapJsonSelector($key); |
|
601 | - } |
|
602 | - |
|
603 | - if ($key == 'and') |
|
604 | - { |
|
605 | - $conditions = $this->constructConditions($value, $model); |
|
606 | - $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
|
607 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
608 | - } |
|
609 | - else if ($key == 'or') |
|
610 | - { |
|
611 | - $conditions = $this->constructConditions($value, $model); |
|
612 | - $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
|
613 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
614 | - } |
|
615 | - else |
|
616 | - { |
|
617 | - if (is_array($value)) |
|
618 | - { |
|
619 | - $operator = $value['op']; |
|
620 | - if (strtolower($operator) == 'between') |
|
621 | - { |
|
622 | - $value1 = $value['val1']; |
|
623 | - $value2 = $value['val2']; |
|
624 | - } |
|
625 | - else |
|
626 | - { |
|
627 | - $value = array_key_exists('val', $value) ? $value['val'] : ''; |
|
628 | - } |
|
629 | - } |
|
630 | - else |
|
631 | - { |
|
632 | - $operator = '='; |
|
633 | - } |
|
511 | + /** |
|
512 | + * Fetch records from the storage based on the given |
|
513 | + * condition. |
|
514 | + * |
|
515 | + * @param array $conditions array of conditions |
|
516 | + * @param array $relations |
|
517 | + * @param string $sortBy |
|
518 | + * @param boolean $desc |
|
519 | + * @param array $columns |
|
520 | + * @return collection |
|
521 | + */ |
|
522 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
523 | + { |
|
524 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
525 | + $sort = $desc ? 'desc' : 'asc'; |
|
526 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
527 | + } |
|
528 | + |
|
529 | + /** |
|
530 | + * Fetch the first record from the storage based on the given |
|
531 | + * condition. |
|
532 | + * |
|
533 | + * @param array $conditions array of conditions |
|
534 | + * @param array $relations |
|
535 | + * @param array $columns |
|
536 | + * @return object |
|
537 | + */ |
|
538 | + public function first($conditions, $relations = [], $columns = array('*')) |
|
539 | + { |
|
540 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
541 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
542 | + } |
|
543 | + |
|
544 | + /** |
|
545 | + * Return the deleted models in pages based on the given conditions. |
|
546 | + * |
|
547 | + * @param array $conditions array of conditions |
|
548 | + * @param integer $perPage |
|
549 | + * @param string $sortBy |
|
550 | + * @param boolean $desc |
|
551 | + * @param array $columns |
|
552 | + * @return collection |
|
553 | + */ |
|
554 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
555 | + { |
|
556 | + unset($conditions['page']); |
|
557 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
558 | + $sort = $desc ? 'desc' : 'asc'; |
|
559 | + $model = $this->model->onlyTrashed(); |
|
560 | + |
|
561 | + if (count($conditions['conditionValues'])) |
|
562 | + { |
|
563 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
564 | + } |
|
565 | + |
|
566 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);; |
|
567 | + } |
|
568 | + |
|
569 | + /** |
|
570 | + * Restore the deleted model. |
|
571 | + * |
|
572 | + * @param integer $id |
|
573 | + * @return void |
|
574 | + */ |
|
575 | + public function restore($id) |
|
576 | + { |
|
577 | + $model = $this->model->onlyTrashed()->find($id); |
|
578 | + |
|
579 | + if ( ! $model) |
|
580 | + { |
|
581 | + \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id); |
|
582 | + } |
|
583 | + |
|
584 | + $model->restore(); |
|
585 | + } |
|
586 | + |
|
587 | + /** |
|
588 | + * Build the conditions recursively for the retrieving methods. |
|
589 | + * @param array $conditions |
|
590 | + * @return array |
|
591 | + */ |
|
592 | + protected function constructConditions($conditions, $model) |
|
593 | + { |
|
594 | + $conditionString = ''; |
|
595 | + $conditionValues = []; |
|
596 | + foreach ($conditions as $key => $value) |
|
597 | + { |
|
598 | + if (str_contains($key, '->')) |
|
599 | + { |
|
600 | + $key = $this->wrapJsonSelector($key); |
|
601 | + } |
|
602 | + |
|
603 | + if ($key == 'and') |
|
604 | + { |
|
605 | + $conditions = $this->constructConditions($value, $model); |
|
606 | + $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
|
607 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
608 | + } |
|
609 | + else if ($key == 'or') |
|
610 | + { |
|
611 | + $conditions = $this->constructConditions($value, $model); |
|
612 | + $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
|
613 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
614 | + } |
|
615 | + else |
|
616 | + { |
|
617 | + if (is_array($value)) |
|
618 | + { |
|
619 | + $operator = $value['op']; |
|
620 | + if (strtolower($operator) == 'between') |
|
621 | + { |
|
622 | + $value1 = $value['val1']; |
|
623 | + $value2 = $value['val2']; |
|
624 | + } |
|
625 | + else |
|
626 | + { |
|
627 | + $value = array_key_exists('val', $value) ? $value['val'] : ''; |
|
628 | + } |
|
629 | + } |
|
630 | + else |
|
631 | + { |
|
632 | + $operator = '='; |
|
633 | + } |
|
634 | 634 | |
635 | - if (strtolower($operator) == 'between') |
|
636 | - { |
|
637 | - $conditionString .= $key . ' >= ? and '; |
|
638 | - $conditionValues[] = $value1; |
|
639 | - |
|
640 | - $conditionString .= $key . ' <= ? {op} '; |
|
641 | - $conditionValues[] = $value2; |
|
642 | - } |
|
643 | - elseif (strtolower($operator) == 'in') |
|
644 | - { |
|
645 | - $conditionValues = array_merge($conditionValues, $value); |
|
646 | - $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
647 | - $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
|
648 | - } |
|
649 | - elseif (strtolower($operator) == 'null') |
|
650 | - { |
|
651 | - $conditionString .= $key . ' is null {op} '; |
|
652 | - } |
|
653 | - elseif (strtolower($operator) == 'not null') |
|
654 | - { |
|
655 | - $conditionString .= $key . ' is not null {op} '; |
|
656 | - } |
|
657 | - elseif (strtolower($operator) == 'has') |
|
658 | - { |
|
659 | - $sql = $model->withTrashed()->has($key)->toSql(); |
|
660 | - $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
661 | - $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
|
662 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
663 | - } |
|
664 | - else |
|
665 | - { |
|
666 | - $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
|
667 | - $conditionValues[] = $value; |
|
668 | - } |
|
669 | - } |
|
670 | - } |
|
671 | - $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
672 | - return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
673 | - } |
|
674 | - |
|
675 | - /** |
|
676 | - * Wrap the given JSON selector. |
|
677 | - * |
|
678 | - * @param string $value |
|
679 | - * @return string |
|
680 | - */ |
|
681 | - protected function wrapJsonSelector($value) |
|
682 | - { |
|
683 | - $removeLast = strpos($value, ')'); |
|
684 | - $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
685 | - $path = explode('->', $value); |
|
686 | - $field = array_shift($path); |
|
687 | - $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
688 | - return '"'.$part.'"'; |
|
689 | - })->implode('.')); |
|
635 | + if (strtolower($operator) == 'between') |
|
636 | + { |
|
637 | + $conditionString .= $key . ' >= ? and '; |
|
638 | + $conditionValues[] = $value1; |
|
639 | + |
|
640 | + $conditionString .= $key . ' <= ? {op} '; |
|
641 | + $conditionValues[] = $value2; |
|
642 | + } |
|
643 | + elseif (strtolower($operator) == 'in') |
|
644 | + { |
|
645 | + $conditionValues = array_merge($conditionValues, $value); |
|
646 | + $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
647 | + $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
|
648 | + } |
|
649 | + elseif (strtolower($operator) == 'null') |
|
650 | + { |
|
651 | + $conditionString .= $key . ' is null {op} '; |
|
652 | + } |
|
653 | + elseif (strtolower($operator) == 'not null') |
|
654 | + { |
|
655 | + $conditionString .= $key . ' is not null {op} '; |
|
656 | + } |
|
657 | + elseif (strtolower($operator) == 'has') |
|
658 | + { |
|
659 | + $sql = $model->withTrashed()->has($key)->toSql(); |
|
660 | + $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
661 | + $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
|
662 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
663 | + } |
|
664 | + else |
|
665 | + { |
|
666 | + $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
|
667 | + $conditionValues[] = $value; |
|
668 | + } |
|
669 | + } |
|
670 | + } |
|
671 | + $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
672 | + return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
673 | + } |
|
674 | + |
|
675 | + /** |
|
676 | + * Wrap the given JSON selector. |
|
677 | + * |
|
678 | + * @param string $value |
|
679 | + * @return string |
|
680 | + */ |
|
681 | + protected function wrapJsonSelector($value) |
|
682 | + { |
|
683 | + $removeLast = strpos($value, ')'); |
|
684 | + $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
685 | + $path = explode('->', $value); |
|
686 | + $field = array_shift($path); |
|
687 | + $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
688 | + return '"'.$part.'"'; |
|
689 | + })->implode('.')); |
|
690 | 690 | |
691 | - return $removeLast === false ? $result : $result . ')'; |
|
692 | - } |
|
693 | - |
|
694 | - /** |
|
695 | - * Abstract method that return the necessary |
|
696 | - * information (full model namespace) |
|
697 | - * needed to preform the previous actions. |
|
698 | - * |
|
699 | - * @return string |
|
700 | - */ |
|
701 | - abstract protected function getModel(); |
|
691 | + return $removeLast === false ? $result : $result . ')'; |
|
692 | + } |
|
693 | + |
|
694 | + /** |
|
695 | + * Abstract method that return the necessary |
|
696 | + * information (full model namespace) |
|
697 | + * needed to preform the previous actions. |
|
698 | + * |
|
699 | + * @return string |
|
700 | + */ |
|
701 | + abstract protected function getModel(); |
|
702 | 702 | } |
703 | 703 | \ No newline at end of file |
@@ -6,20 +6,20 @@ discard block |
||
6 | 6 | |
7 | 7 | class AssignRelationsSeeder extends Seeder |
8 | 8 | { |
9 | - /** |
|
10 | - * Run the database seeds. |
|
11 | - * |
|
12 | - * @return void |
|
13 | - */ |
|
14 | - public function run() |
|
15 | - { |
|
16 | - $adminGroupId = \DB::table('groups')->where('name', 'admin')->select('id')->first()->id; |
|
9 | + /** |
|
10 | + * Run the database seeds. |
|
11 | + * |
|
12 | + * @return void |
|
13 | + */ |
|
14 | + public function run() |
|
15 | + { |
|
16 | + $adminGroupId = \DB::table('groups')->where('name', 'admin')->select('id')->first()->id; |
|
17 | 17 | |
18 | - /** |
|
19 | - * Assign the permissions to the admin group. |
|
20 | - */ |
|
21 | - \DB::table('permissions')->orderBy('created_at', 'asc')->whereIn('model', ['settings'])->each(function ($permission) use ($adminGroupId) { |
|
22 | - \DB::table('groups_permissions')->insert( |
|
18 | + /** |
|
19 | + * Assign the permissions to the admin group. |
|
20 | + */ |
|
21 | + \DB::table('permissions')->orderBy('created_at', 'asc')->whereIn('model', ['settings'])->each(function ($permission) use ($adminGroupId) { |
|
22 | + \DB::table('groups_permissions')->insert( |
|
23 | 23 | [ |
24 | 24 | 'permission_id' => $permission->id, |
25 | 25 | 'group_id' => $adminGroupId, |
@@ -27,6 +27,6 @@ discard block |
||
27 | 27 | 'updated_at' => \DB::raw('NOW()') |
28 | 28 | ] |
29 | 29 | ); |
30 | - }); |
|
31 | - } |
|
30 | + }); |
|
31 | + } |
|
32 | 32 | } |
@@ -6,15 +6,15 @@ |
||
6 | 6 | |
7 | 7 | class ClearDataSeeder extends Seeder |
8 | 8 | { |
9 | - /** |
|
10 | - * Run the database seeds. |
|
11 | - * |
|
12 | - * @return void |
|
13 | - */ |
|
14 | - public function run() |
|
15 | - { |
|
16 | - $permissions = \DB::table('permissions')->whereIn('model', ['settings']); |
|
17 | - \DB::table('groups_permissions')->whereIn('permission_id', $permissions->pluck('id'))->delete(); |
|
18 | - $permissions->delete(); |
|
19 | - } |
|
9 | + /** |
|
10 | + * Run the database seeds. |
|
11 | + * |
|
12 | + * @return void |
|
13 | + */ |
|
14 | + public function run() |
|
15 | + { |
|
16 | + $permissions = \DB::table('permissions')->whereIn('model', ['settings']); |
|
17 | + \DB::table('groups_permissions')->whereIn('permission_id', $permissions->pluck('id'))->delete(); |
|
18 | + $permissions->delete(); |
|
19 | + } |
|
20 | 20 | } |
@@ -6,15 +6,15 @@ |
||
6 | 6 | |
7 | 7 | class CoreDatabaseSeeder extends Seeder |
8 | 8 | { |
9 | - /** |
|
10 | - * Run the database seeds. |
|
11 | - * |
|
12 | - * @return void |
|
13 | - */ |
|
14 | - public function run() |
|
15 | - { |
|
16 | - $this->call(ClearDataSeeder::class); |
|
17 | - $this->call(SettingsTableSeeder::class); |
|
18 | - $this->call(AssignRelationsSeeder::class); |
|
19 | - } |
|
9 | + /** |
|
10 | + * Run the database seeds. |
|
11 | + * |
|
12 | + * @return void |
|
13 | + */ |
|
14 | + public function run() |
|
15 | + { |
|
16 | + $this->call(ClearDataSeeder::class); |
|
17 | + $this->call(SettingsTableSeeder::class); |
|
18 | + $this->call(AssignRelationsSeeder::class); |
|
19 | + } |
|
20 | 20 | } |
@@ -7,33 +7,33 @@ |
||
7 | 7 | |
8 | 8 | class ReportsController 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 = 'reports'; |
|
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 = 'reports'; |
|
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 = ['getReport']; |
|
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 = ['getReport']; |
|
23 | 23 | |
24 | - /** |
|
25 | - * Render the given report name with the given conditions. |
|
26 | - * |
|
27 | - * @param \Illuminate\Http\Request $request |
|
28 | - * @param string $reportName Name of the requested report |
|
29 | - * @param integer $perPage Number of rows per page default all data. |
|
30 | - * @return \Illuminate\Http\Response |
|
31 | - */ |
|
32 | - public function getReport(Request $request, $reportName, $perPage = 0) |
|
33 | - { |
|
34 | - if ($this->model) |
|
35 | - { |
|
36 | - return \Response::json($this->repo->getReport($reportName, $request->all(), $perPage), 200); |
|
37 | - } |
|
38 | - } |
|
24 | + /** |
|
25 | + * Render the given report name with the given conditions. |
|
26 | + * |
|
27 | + * @param \Illuminate\Http\Request $request |
|
28 | + * @param string $reportName Name of the requested report |
|
29 | + * @param integer $perPage Number of rows per page default all data. |
|
30 | + * @return \Illuminate\Http\Response |
|
31 | + */ |
|
32 | + public function getReport(Request $request, $reportName, $perPage = 0) |
|
33 | + { |
|
34 | + if ($this->model) |
|
35 | + { |
|
36 | + return \Response::json($this->repo->getReport($reportName, $request->all(), $perPage), 200); |
|
37 | + } |
|
38 | + } |
|
39 | 39 | } |
@@ -5,48 +5,48 @@ |
||
5 | 5 | |
6 | 6 | class PushNotificationDevice extends Model{ |
7 | 7 | |
8 | - use SoftDeletes; |
|
9 | - protected $table = 'push_notifications_devices'; |
|
10 | - protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
11 | - protected $hidden = ['deleted_at', 'access_token']; |
|
12 | - protected $guarded = ['id']; |
|
13 | - protected $fillable = ['device_token', 'user_id', 'access_token']; |
|
14 | - public $searchable = ['device_token']; |
|
15 | - |
|
16 | - public function getCreatedAtAttribute($value) |
|
17 | - { |
|
18 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
19 | - } |
|
20 | - |
|
21 | - public function getUpdatedAtAttribute($value) |
|
22 | - { |
|
23 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
24 | - } |
|
25 | - |
|
26 | - public function getDeletedAtAttribute($value) |
|
27 | - { |
|
28 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
29 | - } |
|
8 | + use SoftDeletes; |
|
9 | + protected $table = 'push_notifications_devices'; |
|
10 | + protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
11 | + protected $hidden = ['deleted_at', 'access_token']; |
|
12 | + protected $guarded = ['id']; |
|
13 | + protected $fillable = ['device_token', 'user_id', 'access_token']; |
|
14 | + public $searchable = ['device_token']; |
|
15 | + |
|
16 | + public function getCreatedAtAttribute($value) |
|
17 | + { |
|
18 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
19 | + } |
|
20 | + |
|
21 | + public function getUpdatedAtAttribute($value) |
|
22 | + { |
|
23 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
24 | + } |
|
25 | + |
|
26 | + public function getDeletedAtAttribute($value) |
|
27 | + { |
|
28 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
29 | + } |
|
30 | 30 | |
31 | - public function user() |
|
32 | - { |
|
33 | - return $this->belongsTo('App\Modules\V1\Acl\AclUser'); |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * Encrypt the access_token attribute before |
|
38 | - * saving it in the storage. |
|
39 | - * |
|
40 | - * @param string $value |
|
41 | - */ |
|
42 | - public function setLoginTokenAttribute($value) |
|
43 | - { |
|
44 | - $this->attributes['access_token'] = encrypt($value); |
|
45 | - } |
|
46 | - |
|
47 | - public static function boot() |
|
48 | - { |
|
49 | - parent::boot(); |
|
50 | - parent::observe(\App::make('App\Modules\V1\Notifications\ModelObservers\PushNotificationDeviceObserver')); |
|
51 | - } |
|
31 | + public function user() |
|
32 | + { |
|
33 | + return $this->belongsTo('App\Modules\V1\Acl\AclUser'); |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * Encrypt the access_token attribute before |
|
38 | + * saving it in the storage. |
|
39 | + * |
|
40 | + * @param string $value |
|
41 | + */ |
|
42 | + public function setLoginTokenAttribute($value) |
|
43 | + { |
|
44 | + $this->attributes['access_token'] = encrypt($value); |
|
45 | + } |
|
46 | + |
|
47 | + public static function boot() |
|
48 | + { |
|
49 | + parent::boot(); |
|
50 | + parent::observe(\App::make('App\Modules\V1\Notifications\ModelObservers\PushNotificationDeviceObserver')); |
|
51 | + } |
|
52 | 52 | } |
@@ -7,42 +7,42 @@ |
||
7 | 7 | |
8 | 8 | class PushNotificationsDevicesController 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 = 'pushNotificationDevices'; |
|
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 = 'pushNotificationDevices'; |
|
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 = ['registerDevice']; |
|
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 = ['registerDevice']; |
|
23 | 23 | |
24 | - /** |
|
25 | - * The validations rules used by the base api controller |
|
26 | - * to check before add. |
|
27 | - * @var array |
|
28 | - */ |
|
29 | - protected $validationRules = [ |
|
30 | - 'device_token' => 'required|string|max:255', |
|
31 | - 'user_id' => 'required|exists:users,id' |
|
32 | - ]; |
|
24 | + /** |
|
25 | + * The validations rules used by the base api controller |
|
26 | + * to check before add. |
|
27 | + * @var array |
|
28 | + */ |
|
29 | + protected $validationRules = [ |
|
30 | + 'device_token' => 'required|string|max:255', |
|
31 | + 'user_id' => 'required|exists:users,id' |
|
32 | + ]; |
|
33 | 33 | |
34 | - /** |
|
35 | - * Register the given device to the logged in user. |
|
36 | - * |
|
37 | - * @param \Illuminate\Http\Request $request |
|
38 | - * @return \Illuminate\Http\Response |
|
39 | - */ |
|
40 | - public function registerDevice(Request $request) |
|
41 | - { |
|
42 | - $this->validate($request, [ |
|
43 | - 'device_token' => 'required|string|max:255' |
|
44 | - ]); |
|
34 | + /** |
|
35 | + * Register the given device to the logged in user. |
|
36 | + * |
|
37 | + * @param \Illuminate\Http\Request $request |
|
38 | + * @return \Illuminate\Http\Response |
|
39 | + */ |
|
40 | + public function registerDevice(Request $request) |
|
41 | + { |
|
42 | + $this->validate($request, [ |
|
43 | + 'device_token' => 'required|string|max:255' |
|
44 | + ]); |
|
45 | 45 | |
46 | - return \Response::json($this->repo->registerDevice($request->all()), 200); |
|
47 | - } |
|
46 | + return \Response::json($this->repo->registerDevice($request->all()), 200); |
|
47 | + } |
|
48 | 48 | } |