@@ -2,14 +2,14 @@ discard block |
||
2 | 2 | |
3 | 3 | class CoreConfig |
4 | 4 | { |
5 | - public function getConfig() |
|
6 | - { |
|
7 | - $customSettings = []; |
|
8 | - Settings::get(['key', 'value'])->each(function ($setting) use (&$customSettings){ |
|
9 | - $customSettings[$setting['key']] = $setting['value']; |
|
10 | - }); |
|
5 | + public function getConfig() |
|
6 | + { |
|
7 | + $customSettings = []; |
|
8 | + Settings::get(['key', 'value'])->each(function ($setting) use (&$customSettings){ |
|
9 | + $customSettings[$setting['key']] = $setting['value']; |
|
10 | + }); |
|
11 | 11 | |
12 | - return array_merge($customSettings, [ |
|
12 | + return array_merge($customSettings, [ |
|
13 | 13 | /** |
14 | 14 | * Specify what relations should be used for every model. |
15 | 15 | */ |
@@ -56,5 +56,5 @@ discard block |
||
56 | 56 | ], |
57 | 57 | ] |
58 | 58 | ]); |
59 | - } |
|
59 | + } |
|
60 | 60 | } |
61 | 61 | \ No newline at end of file |
@@ -2,18 +2,18 @@ |
||
2 | 2 | |
3 | 3 | class Log |
4 | 4 | { |
5 | - public function saveLog($action, $item_name, $item_type, $item_id, $model = false) |
|
6 | - { |
|
7 | - if (\Core::logs() && $item_name !== 'Log') |
|
8 | - { |
|
9 | - $item_name = $item_name; |
|
10 | - \Core::logs()->save([ |
|
11 | - 'action' => $action, |
|
12 | - 'item_name' => $item_name, |
|
13 | - 'item_type' => $item_type, |
|
14 | - 'item_id' => $item_id, |
|
15 | - 'user_id' => \Auth::user()->id, |
|
16 | - ], false, false); |
|
17 | - } |
|
18 | - } |
|
5 | + public function saveLog($action, $item_name, $item_type, $item_id, $model = false) |
|
6 | + { |
|
7 | + if (\Core::logs() && $item_name !== 'Log') |
|
8 | + { |
|
9 | + $item_name = $item_name; |
|
10 | + \Core::logs()->save([ |
|
11 | + 'action' => $action, |
|
12 | + 'item_name' => $item_name, |
|
13 | + 'item_type' => $item_type, |
|
14 | + 'item_id' => $item_id, |
|
15 | + 'user_id' => \Auth::user()->id, |
|
16 | + ], false, false); |
|
17 | + } |
|
18 | + } |
|
19 | 19 | } |
20 | 20 | \ No newline at end of file |
@@ -3,127 +3,127 @@ |
||
3 | 3 | interface RepositoryInterface |
4 | 4 | { |
5 | 5 | /** |
6 | - * Fetch all records with relations from the storage. |
|
7 | - * |
|
8 | - * @param array $relations |
|
9 | - * @param array $sortBy |
|
10 | - * @param array $desc |
|
11 | - * @param array $columns |
|
12 | - * @return collection |
|
13 | - */ |
|
6 | + * Fetch all records with relations from the storage. |
|
7 | + * |
|
8 | + * @param array $relations |
|
9 | + * @param array $sortBy |
|
10 | + * @param array $desc |
|
11 | + * @param array $columns |
|
12 | + * @return collection |
|
13 | + */ |
|
14 | 14 | public function all($relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); |
15 | 15 | |
16 | - /** |
|
17 | - * Fetch all records with relations from storage in pages |
|
18 | - * that matche the given query. |
|
19 | - * |
|
20 | - * @param string $query |
|
21 | - * @param integer $perPage |
|
22 | - * @param array $relations |
|
23 | - * @param array $sortBy |
|
24 | - * @param array $desc |
|
25 | - * @param array $columns |
|
26 | - * @return collection |
|
27 | - */ |
|
28 | - public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); |
|
16 | + /** |
|
17 | + * Fetch all records with relations from storage in pages |
|
18 | + * that matche the given query. |
|
19 | + * |
|
20 | + * @param string $query |
|
21 | + * @param integer $perPage |
|
22 | + * @param array $relations |
|
23 | + * @param array $sortBy |
|
24 | + * @param array $desc |
|
25 | + * @param array $columns |
|
26 | + * @return collection |
|
27 | + */ |
|
28 | + public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); |
|
29 | 29 | |
30 | 30 | /** |
31 | - * Fetch all records with relations from storage in pages. |
|
32 | - * |
|
33 | - * @param integer $perPage |
|
34 | - * @param array $relations |
|
35 | - * @param array $sortBy |
|
36 | - * @param array $desc |
|
37 | - * @param array $columns |
|
38 | - * @return collection |
|
39 | - */ |
|
40 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); |
|
31 | + * Fetch all records with relations from storage in pages. |
|
32 | + * |
|
33 | + * @param integer $perPage |
|
34 | + * @param array $relations |
|
35 | + * @param array $sortBy |
|
36 | + * @param array $desc |
|
37 | + * @param array $columns |
|
38 | + * @return collection |
|
39 | + */ |
|
40 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); |
|
41 | 41 | |
42 | 42 | /** |
43 | - * Fetch all records with relations based on |
|
44 | - * the given condition from storage in pages. |
|
45 | - * |
|
46 | - * @param array $conditions array of conditions |
|
47 | - * @param integer $perPage |
|
48 | - * @param array $relations |
|
49 | - * @param array $sortBy |
|
50 | - * @param array $desc |
|
51 | - * @param array $columns |
|
52 | - * @return collection |
|
53 | - */ |
|
54 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); |
|
43 | + * Fetch all records with relations based on |
|
44 | + * the given condition from storage in pages. |
|
45 | + * |
|
46 | + * @param array $conditions array of conditions |
|
47 | + * @param integer $perPage |
|
48 | + * @param array $relations |
|
49 | + * @param array $sortBy |
|
50 | + * @param array $desc |
|
51 | + * @param array $columns |
|
52 | + * @return collection |
|
53 | + */ |
|
54 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); |
|
55 | 55 | |
56 | - /** |
|
57 | - * Save the given model/models to the storage. |
|
58 | - * |
|
59 | - * @param array $data |
|
60 | - * @param boolean $saveLog |
|
61 | - * @return object |
|
62 | - */ |
|
63 | - public function save(array $data, $saveLog = true); |
|
56 | + /** |
|
57 | + * Save the given model/models to the storage. |
|
58 | + * |
|
59 | + * @param array $data |
|
60 | + * @param boolean $saveLog |
|
61 | + * @return object |
|
62 | + */ |
|
63 | + public function save(array $data, $saveLog = true); |
|
64 | 64 | |
65 | - /** |
|
66 | - * Insert multiple records to the storage. |
|
67 | - * |
|
68 | - * @param array $data |
|
69 | - * @return object |
|
70 | - */ |
|
71 | - public function saveMany(array $data); |
|
65 | + /** |
|
66 | + * Insert multiple records to the storage. |
|
67 | + * |
|
68 | + * @param array $data |
|
69 | + * @return object |
|
70 | + */ |
|
71 | + public function saveMany(array $data); |
|
72 | 72 | |
73 | 73 | /** |
74 | - * Update record in the storage based on the given |
|
75 | - * condition. |
|
76 | - * |
|
77 | - * @param [type] $value condition value |
|
78 | - * @param array $data |
|
79 | - * @param string $attribute condition column name |
|
80 | - * @return integer affected rows |
|
81 | - */ |
|
82 | - public function update($value, array $data, $attribute = 'id'); |
|
74 | + * Update record in the storage based on the given |
|
75 | + * condition. |
|
76 | + * |
|
77 | + * @param [type] $value condition value |
|
78 | + * @param array $data |
|
79 | + * @param string $attribute condition column name |
|
80 | + * @return integer affected rows |
|
81 | + */ |
|
82 | + public function update($value, array $data, $attribute = 'id'); |
|
83 | 83 | |
84 | 84 | /** |
85 | - * Delete record from the storage based on the given |
|
86 | - * condition. |
|
87 | - * |
|
88 | - * @param [type] $value condition value |
|
89 | - * @param string $attribute condition column name |
|
90 | - * @return integer affected rows |
|
91 | - */ |
|
92 | - public function delete($value, $attribute = 'id'); |
|
85 | + * Delete record from the storage based on the given |
|
86 | + * condition. |
|
87 | + * |
|
88 | + * @param [type] $value condition value |
|
89 | + * @param string $attribute condition column name |
|
90 | + * @return integer affected rows |
|
91 | + */ |
|
92 | + public function delete($value, $attribute = 'id'); |
|
93 | 93 | |
94 | 94 | /** |
95 | - * Fetch records from the storage based on the given |
|
96 | - * id. |
|
97 | - * |
|
98 | - * @param integer $id |
|
99 | - * @param array $relations |
|
100 | - * @param array $columns |
|
101 | - * @return object |
|
102 | - */ |
|
103 | - public function find($id, $relations = [], $columns = array('*')); |
|
95 | + * Fetch records from the storage based on the given |
|
96 | + * id. |
|
97 | + * |
|
98 | + * @param integer $id |
|
99 | + * @param array $relations |
|
100 | + * @param array $columns |
|
101 | + * @return object |
|
102 | + */ |
|
103 | + public function find($id, $relations = [], $columns = array('*')); |
|
104 | 104 | |
105 | 105 | /** |
106 | - * Fetch records from the storage based on the given |
|
107 | - * condition. |
|
108 | - * |
|
109 | - * @param array $conditions array of conditions |
|
110 | - * @param array $relations |
|
111 | - * @param array $sortBy |
|
112 | - * @param array $desc |
|
113 | - * @param array $columns |
|
114 | - * @return collection |
|
115 | - */ |
|
116 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); |
|
106 | + * Fetch records from the storage based on the given |
|
107 | + * condition. |
|
108 | + * |
|
109 | + * @param array $conditions array of conditions |
|
110 | + * @param array $relations |
|
111 | + * @param array $sortBy |
|
112 | + * @param array $desc |
|
113 | + * @param array $columns |
|
114 | + * @return collection |
|
115 | + */ |
|
116 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); |
|
117 | 117 | |
118 | - /** |
|
119 | - * Fetch the first record fro the storage based on the given |
|
120 | - * condition. |
|
121 | - * |
|
122 | - * @param array $conditions array of conditions |
|
123 | - * @param [type] $value condition value |
|
124 | - * @param array $relations |
|
125 | - * @param array $columns |
|
126 | - * @return object |
|
127 | - */ |
|
128 | - public function first($conditions, $relations = [], $columns = array('*')); |
|
118 | + /** |
|
119 | + * Fetch the first record fro the storage based on the given |
|
120 | + * condition. |
|
121 | + * |
|
122 | + * @param array $conditions array of conditions |
|
123 | + * @param [type] $value condition value |
|
124 | + * @param array $relations |
|
125 | + * @param array $columns |
|
126 | + * @return object |
|
127 | + */ |
|
128 | + public function first($conditions, $relations = [], $columns = array('*')); |
|
129 | 129 | } |
130 | 130 | \ No newline at end of file |
@@ -4,13 +4,13 @@ |
||
4 | 4 | { |
5 | 5 | /** |
6 | 6 | * Construct the repository class name based on |
7 | - * the method name called, search in the |
|
8 | - * given namespaces for the class and |
|
9 | - * return an instance. |
|
10 | - * |
|
11 | - * @param string $name the called method name |
|
12 | - * @param array $arguments the method arguments |
|
13 | - * @return object |
|
14 | - */ |
|
7 | + * the method name called, search in the |
|
8 | + * given namespaces for the class and |
|
9 | + * return an instance. |
|
10 | + * |
|
11 | + * @param string $name the called method name |
|
12 | + * @param array $arguments the method arguments |
|
13 | + * @return object |
|
14 | + */ |
|
15 | 15 | public function __call($name, $arguments); |
16 | 16 | } |
17 | 17 | \ No newline at end of file |
@@ -2,33 +2,33 @@ |
||
2 | 2 | |
3 | 3 | class ErrorHandler |
4 | 4 | { |
5 | - public function unAuthorized() |
|
6 | - { |
|
7 | - return ['status' => 401, 'message' => 'Please login before any action']; |
|
8 | - } |
|
5 | + public function unAuthorized() |
|
6 | + { |
|
7 | + return ['status' => 401, 'message' => 'Please login before any action']; |
|
8 | + } |
|
9 | 9 | |
10 | - public function loginFailed() |
|
11 | - { |
|
12 | - return ['status' => 400, 'message' => 'Wrong mail or password']; |
|
13 | - } |
|
10 | + public function loginFailed() |
|
11 | + { |
|
12 | + return ['status' => 400, 'message' => 'Wrong mail or password']; |
|
13 | + } |
|
14 | 14 | |
15 | - public function noPermissions() |
|
16 | - { |
|
17 | - return ['status' => 401, 'message' => 'No permissions']; |
|
18 | - } |
|
15 | + public function noPermissions() |
|
16 | + { |
|
17 | + return ['status' => 401, 'message' => 'No permissions']; |
|
18 | + } |
|
19 | 19 | |
20 | - public function cannotCreateSetting() |
|
21 | - { |
|
22 | - return ['status' => 400, 'message' => 'Can\'t create setting']; |
|
23 | - } |
|
20 | + public function cannotCreateSetting() |
|
21 | + { |
|
22 | + return ['status' => 400, 'message' => 'Can\'t create setting']; |
|
23 | + } |
|
24 | 24 | |
25 | - public function cannotUpdateSettingKey() |
|
26 | - { |
|
27 | - return ['status' => 400, 'message' => 'Can\'t update setting key']; |
|
28 | - } |
|
25 | + public function cannotUpdateSettingKey() |
|
26 | + { |
|
27 | + return ['status' => 400, 'message' => 'Can\'t update setting key']; |
|
28 | + } |
|
29 | 29 | |
30 | - public function notFound($text) |
|
31 | - { |
|
32 | - return ['status' => 404, 'message' => 'The requested ' . $text . ' not found']; |
|
33 | - } |
|
30 | + public function notFound($text) |
|
31 | + { |
|
32 | + return ['status' => 404, 'message' => 'The requested ' . $text . ' not found']; |
|
33 | + } |
|
34 | 34 | } |
35 | 35 | \ No newline at end of file |
@@ -9,242 +9,242 @@ |
||
9 | 9 | |
10 | 10 | class BaseApiController extends Controller |
11 | 11 | { |
12 | - /** |
|
13 | - * The errorHandler implementation. |
|
14 | - * |
|
15 | - * @var errorHandler |
|
16 | - */ |
|
17 | - protected $errorHandler; |
|
18 | - |
|
19 | - /** |
|
20 | - * The config implementation. |
|
21 | - * |
|
22 | - * @var config |
|
23 | - */ |
|
24 | - protected $config; |
|
25 | - |
|
26 | - /** |
|
27 | - * The model implementation. |
|
28 | - * |
|
29 | - * @var model |
|
30 | - */ |
|
31 | - protected $model; |
|
32 | - |
|
33 | - /** |
|
34 | - * Create new BaseApiController instance. |
|
35 | - * |
|
36 | - * @param errorHandler |
|
37 | - */ |
|
38 | - public function __construct(ErrorHandler $errorHandler, CoreConfig $config) |
|
39 | - { |
|
40 | - \Session::set('timeZoneDiff', \Request::header('time-zone-diff') ?: 0); |
|
41 | - $this->errorHandler = $errorHandler; |
|
42 | - $this->config = $config->getConfig(); |
|
12 | + /** |
|
13 | + * The errorHandler implementation. |
|
14 | + * |
|
15 | + * @var errorHandler |
|
16 | + */ |
|
17 | + protected $errorHandler; |
|
18 | + |
|
19 | + /** |
|
20 | + * The config implementation. |
|
21 | + * |
|
22 | + * @var config |
|
23 | + */ |
|
24 | + protected $config; |
|
25 | + |
|
26 | + /** |
|
27 | + * The model implementation. |
|
28 | + * |
|
29 | + * @var model |
|
30 | + */ |
|
31 | + protected $model; |
|
32 | + |
|
33 | + /** |
|
34 | + * Create new BaseApiController instance. |
|
35 | + * |
|
36 | + * @param errorHandler |
|
37 | + */ |
|
38 | + public function __construct(ErrorHandler $errorHandler, CoreConfig $config) |
|
39 | + { |
|
40 | + \Session::set('timeZoneDiff', \Request::header('time-zone-diff') ?: 0); |
|
41 | + $this->errorHandler = $errorHandler; |
|
42 | + $this->config = $config->getConfig(); |
|
43 | 43 | |
44 | - $this->model = property_exists($this, 'model') ? $this->model : false; |
|
45 | - $this->validationRules = property_exists($this, 'validationRules') ? $this->validationRules : false; |
|
46 | - $this->skipPermissionCheck = property_exists($this, 'skipPermissionCheck') ? $this->skipPermissionCheck : []; |
|
47 | - $this->skipLoginCheck = property_exists($this, 'skipLoginCheck') ? $this->skipLoginCheck : []; |
|
44 | + $this->model = property_exists($this, 'model') ? $this->model : false; |
|
45 | + $this->validationRules = property_exists($this, 'validationRules') ? $this->validationRules : false; |
|
46 | + $this->skipPermissionCheck = property_exists($this, 'skipPermissionCheck') ? $this->skipPermissionCheck : []; |
|
47 | + $this->skipLoginCheck = property_exists($this, 'skipLoginCheck') ? $this->skipLoginCheck : []; |
|
48 | 48 | |
49 | - $this->relations = array_key_exists($this->model, $this->config['relations']) ? $this->config['relations'][$this->model] : false; |
|
50 | - $route = explode('@',\Route::currentRouteAction())[1]; |
|
51 | - $this->checkPermission(explode('_', snake_case($route))[1]); |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * Fetch all records with relations from model repository. |
|
56 | - * |
|
57 | - * @param string $sortBy |
|
58 | - * @param boolean $desc |
|
59 | - * @return \Illuminate\Http\Response |
|
60 | - */ |
|
61 | - public function getIndex() |
|
62 | - { |
|
63 | - if ($this->model) |
|
64 | - { |
|
65 | - $relations = $this->relations && $this->relations['all'] ? $this->relations['all'] : []; |
|
66 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->all($relations), 200); |
|
67 | - } |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Fetch the single object with relations from model repository. |
|
72 | - * |
|
73 | - * @param integer $id |
|
74 | - * @return \Illuminate\Http\Response |
|
75 | - */ |
|
76 | - public function getFind($id) |
|
77 | - { |
|
78 | - if ($this->model) |
|
79 | - { |
|
80 | - $relations = $this->relations && $this->relations['find'] ? $this->relations['find'] : []; |
|
81 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->find($id, $relations), 200); |
|
82 | - } |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Paginate all records with relations from model repository |
|
87 | - * that matche the given query. |
|
88 | - * |
|
89 | - * @param string $query |
|
90 | - * @param integer $perPage |
|
91 | - * @param string $sortBy |
|
92 | - * @param boolean $desc |
|
93 | - * @return \Illuminate\Http\Response |
|
94 | - */ |
|
95 | - public function getSearch($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
96 | - { |
|
97 | - if ($this->model) |
|
98 | - { |
|
99 | - $relations = $this->relations && $this->relations['paginate'] ? $this->relations['paginate'] : []; |
|
100 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->search($query, $perPage, $relations, $sortBy, $desc), 200); |
|
101 | - } |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Fetch records from the storage based on the given |
|
106 | - * condition. |
|
107 | - * |
|
108 | - * @param \Illuminate\Http\Request $request |
|
109 | - * @param string $sortBy |
|
110 | - * @param boolean $desc |
|
111 | - * @return \Illuminate\Http\Response |
|
112 | - */ |
|
113 | - public function postFindby(Request $request, $sortBy = 'created_at', $desc = 1) |
|
114 | - { |
|
115 | - if ($this->model) |
|
116 | - { |
|
117 | - $relations = $this->relations && $this->relations['findBy'] ? $this->relations['findBy'] : []; |
|
118 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->findBy($request->all(), $relations, $sortBy, $desc), 200); |
|
119 | - } |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Fetch the first record from the storage based on the given |
|
124 | - * condition. |
|
125 | - * |
|
126 | - * @param \Illuminate\Http\Request $request |
|
127 | - * @return \Illuminate\Http\Response |
|
128 | - */ |
|
129 | - public function postFirst(Request $request) |
|
130 | - { |
|
131 | - if ($this->model) |
|
132 | - { |
|
133 | - $relations = $this->relations && $this->relations['first'] ? $this->relations['first'] : []; |
|
134 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->first($request->all(), $relations), 200); |
|
135 | - } |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * Paginate all records with relations from model repository. |
|
140 | - * |
|
141 | - * @param integer $perPage |
|
142 | - * @param string $sortBy |
|
143 | - * @param boolean $desc |
|
144 | - * @return \Illuminate\Http\Response |
|
145 | - */ |
|
146 | - public function getPaginate($perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
147 | - { |
|
148 | - if ($this->model) |
|
149 | - { |
|
150 | - $relations = $this->relations && $this->relations['paginate'] ? $this->relations['paginate'] : []; |
|
151 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginate($perPage, $relations, $sortBy, $desc), 200); |
|
152 | - } |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * Fetch all records with relations based on |
|
157 | - * the given condition from storage in pages. |
|
158 | - * |
|
159 | - * @param \Illuminate\Http\Request $request |
|
160 | - * @param integer $perPage |
|
161 | - * @param string $sortBy |
|
162 | - * @param boolean $desc |
|
163 | - * @return \Illuminate\Http\Response |
|
164 | - */ |
|
165 | - public function postPaginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
166 | - { |
|
167 | - if ($this->model) |
|
168 | - { |
|
169 | - $relations = $this->relations && $this->relations['paginateBy'] ? $this->relations['paginateBy'] : []; |
|
170 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginateBy($request->all(), $perPage, $relations, $sortBy, $desc), 200); |
|
171 | - } |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * Save the given model to repository. |
|
176 | - * |
|
177 | - * @param \Illuminate\Http\Request $request |
|
178 | - * @return \Illuminate\Http\Response |
|
179 | - */ |
|
180 | - public function postSave(Request $request) |
|
181 | - { |
|
182 | - foreach ($this->validationRules as &$rule) |
|
183 | - { |
|
184 | - if (strpos($rule, 'exists') && ! strpos($rule, 'deleted_at,NULL')) |
|
185 | - { |
|
186 | - $rule .= ',deleted_at,NULL'; |
|
187 | - } |
|
188 | - |
|
189 | - if ($request->has('id')) |
|
190 | - { |
|
191 | - $rule = str_replace('{id}', $request->get('id'), $rule); |
|
192 | - } |
|
193 | - else |
|
194 | - { |
|
195 | - $rule = str_replace(',{id}', '', $rule); |
|
196 | - } |
|
197 | - } |
|
49 | + $this->relations = array_key_exists($this->model, $this->config['relations']) ? $this->config['relations'][$this->model] : false; |
|
50 | + $route = explode('@',\Route::currentRouteAction())[1]; |
|
51 | + $this->checkPermission(explode('_', snake_case($route))[1]); |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * Fetch all records with relations from model repository. |
|
56 | + * |
|
57 | + * @param string $sortBy |
|
58 | + * @param boolean $desc |
|
59 | + * @return \Illuminate\Http\Response |
|
60 | + */ |
|
61 | + public function getIndex() |
|
62 | + { |
|
63 | + if ($this->model) |
|
64 | + { |
|
65 | + $relations = $this->relations && $this->relations['all'] ? $this->relations['all'] : []; |
|
66 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->all($relations), 200); |
|
67 | + } |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Fetch the single object with relations from model repository. |
|
72 | + * |
|
73 | + * @param integer $id |
|
74 | + * @return \Illuminate\Http\Response |
|
75 | + */ |
|
76 | + public function getFind($id) |
|
77 | + { |
|
78 | + if ($this->model) |
|
79 | + { |
|
80 | + $relations = $this->relations && $this->relations['find'] ? $this->relations['find'] : []; |
|
81 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->find($id, $relations), 200); |
|
82 | + } |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Paginate all records with relations from model repository |
|
87 | + * that matche the given query. |
|
88 | + * |
|
89 | + * @param string $query |
|
90 | + * @param integer $perPage |
|
91 | + * @param string $sortBy |
|
92 | + * @param boolean $desc |
|
93 | + * @return \Illuminate\Http\Response |
|
94 | + */ |
|
95 | + public function getSearch($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
96 | + { |
|
97 | + if ($this->model) |
|
98 | + { |
|
99 | + $relations = $this->relations && $this->relations['paginate'] ? $this->relations['paginate'] : []; |
|
100 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->search($query, $perPage, $relations, $sortBy, $desc), 200); |
|
101 | + } |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Fetch records from the storage based on the given |
|
106 | + * condition. |
|
107 | + * |
|
108 | + * @param \Illuminate\Http\Request $request |
|
109 | + * @param string $sortBy |
|
110 | + * @param boolean $desc |
|
111 | + * @return \Illuminate\Http\Response |
|
112 | + */ |
|
113 | + public function postFindby(Request $request, $sortBy = 'created_at', $desc = 1) |
|
114 | + { |
|
115 | + if ($this->model) |
|
116 | + { |
|
117 | + $relations = $this->relations && $this->relations['findBy'] ? $this->relations['findBy'] : []; |
|
118 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->findBy($request->all(), $relations, $sortBy, $desc), 200); |
|
119 | + } |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Fetch the first record from the storage based on the given |
|
124 | + * condition. |
|
125 | + * |
|
126 | + * @param \Illuminate\Http\Request $request |
|
127 | + * @return \Illuminate\Http\Response |
|
128 | + */ |
|
129 | + public function postFirst(Request $request) |
|
130 | + { |
|
131 | + if ($this->model) |
|
132 | + { |
|
133 | + $relations = $this->relations && $this->relations['first'] ? $this->relations['first'] : []; |
|
134 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->first($request->all(), $relations), 200); |
|
135 | + } |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * Paginate all records with relations from model repository. |
|
140 | + * |
|
141 | + * @param integer $perPage |
|
142 | + * @param string $sortBy |
|
143 | + * @param boolean $desc |
|
144 | + * @return \Illuminate\Http\Response |
|
145 | + */ |
|
146 | + public function getPaginate($perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
147 | + { |
|
148 | + if ($this->model) |
|
149 | + { |
|
150 | + $relations = $this->relations && $this->relations['paginate'] ? $this->relations['paginate'] : []; |
|
151 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginate($perPage, $relations, $sortBy, $desc), 200); |
|
152 | + } |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * Fetch all records with relations based on |
|
157 | + * the given condition from storage in pages. |
|
158 | + * |
|
159 | + * @param \Illuminate\Http\Request $request |
|
160 | + * @param integer $perPage |
|
161 | + * @param string $sortBy |
|
162 | + * @param boolean $desc |
|
163 | + * @return \Illuminate\Http\Response |
|
164 | + */ |
|
165 | + public function postPaginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
166 | + { |
|
167 | + if ($this->model) |
|
168 | + { |
|
169 | + $relations = $this->relations && $this->relations['paginateBy'] ? $this->relations['paginateBy'] : []; |
|
170 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginateBy($request->all(), $perPage, $relations, $sortBy, $desc), 200); |
|
171 | + } |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * Save the given model to repository. |
|
176 | + * |
|
177 | + * @param \Illuminate\Http\Request $request |
|
178 | + * @return \Illuminate\Http\Response |
|
179 | + */ |
|
180 | + public function postSave(Request $request) |
|
181 | + { |
|
182 | + foreach ($this->validationRules as &$rule) |
|
183 | + { |
|
184 | + if (strpos($rule, 'exists') && ! strpos($rule, 'deleted_at,NULL')) |
|
185 | + { |
|
186 | + $rule .= ',deleted_at,NULL'; |
|
187 | + } |
|
188 | + |
|
189 | + if ($request->has('id')) |
|
190 | + { |
|
191 | + $rule = str_replace('{id}', $request->get('id'), $rule); |
|
192 | + } |
|
193 | + else |
|
194 | + { |
|
195 | + $rule = str_replace(',{id}', '', $rule); |
|
196 | + } |
|
197 | + } |
|
198 | 198 | |
199 | - $this->validate($request, $this->validationRules); |
|
200 | - |
|
201 | - if ($this->model) |
|
202 | - { |
|
203 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->save($request->all()), 200); |
|
204 | - } |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * Delete by the given id from model repository. |
|
209 | - * |
|
210 | - * @param integer $id |
|
211 | - * @return \Illuminate\Http\Response |
|
212 | - */ |
|
213 | - public function getDelete($id) |
|
214 | - { |
|
215 | - if ($this->model) |
|
216 | - { |
|
217 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->delete($id), 200); |
|
218 | - } |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * Check if the logged in user can do the given permission. |
|
223 | - * |
|
224 | - * @param string $permission |
|
225 | - * @return void |
|
226 | - */ |
|
227 | - private function checkPermission($permission) |
|
228 | - { |
|
229 | - $permission = $permission !== 'index' ? $permission : 'list'; |
|
230 | - if ($permission == 'method') |
|
231 | - { |
|
232 | - $error = $this->errorHandler->notFound('method'); |
|
233 | - abort($error['status'], $error['message']); |
|
234 | - } |
|
235 | - else if ( ! in_array($permission, $this->skipLoginCheck)) |
|
236 | - { |
|
237 | - if ( ! \Auth::check()) |
|
238 | - { |
|
239 | - $error = $this->errorHandler->unAuthorized(); |
|
240 | - abort($error['status'], $error['message']); |
|
241 | - } |
|
242 | - else if ( ! in_array($permission, $this->skipPermissionCheck) && |
|
243 | - ! \Core::users()->can($permission, $this->model)) |
|
244 | - { |
|
245 | - $error = $this->errorHandler->noPermissions(); |
|
246 | - abort($error['status'], $error['message']); |
|
247 | - } |
|
248 | - } |
|
249 | - } |
|
199 | + $this->validate($request, $this->validationRules); |
|
200 | + |
|
201 | + if ($this->model) |
|
202 | + { |
|
203 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->save($request->all()), 200); |
|
204 | + } |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * Delete by the given id from model repository. |
|
209 | + * |
|
210 | + * @param integer $id |
|
211 | + * @return \Illuminate\Http\Response |
|
212 | + */ |
|
213 | + public function getDelete($id) |
|
214 | + { |
|
215 | + if ($this->model) |
|
216 | + { |
|
217 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->delete($id), 200); |
|
218 | + } |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * Check if the logged in user can do the given permission. |
|
223 | + * |
|
224 | + * @param string $permission |
|
225 | + * @return void |
|
226 | + */ |
|
227 | + private function checkPermission($permission) |
|
228 | + { |
|
229 | + $permission = $permission !== 'index' ? $permission : 'list'; |
|
230 | + if ($permission == 'method') |
|
231 | + { |
|
232 | + $error = $this->errorHandler->notFound('method'); |
|
233 | + abort($error['status'], $error['message']); |
|
234 | + } |
|
235 | + else if ( ! in_array($permission, $this->skipLoginCheck)) |
|
236 | + { |
|
237 | + if ( ! \Auth::check()) |
|
238 | + { |
|
239 | + $error = $this->errorHandler->unAuthorized(); |
|
240 | + abort($error['status'], $error['message']); |
|
241 | + } |
|
242 | + else if ( ! in_array($permission, $this->skipPermissionCheck) && |
|
243 | + ! \Core::users()->can($permission, $this->model)) |
|
244 | + { |
|
245 | + $error = $this->errorHandler->noPermissions(); |
|
246 | + abort($error['status'], $error['message']); |
|
247 | + } |
|
248 | + } |
|
249 | + } |
|
250 | 250 | } |
@@ -13,13 +13,13 @@ |
||
13 | 13 | public function up() |
14 | 14 | { |
15 | 15 | Schema::create('settings', function (Blueprint $table) { |
16 | - $table->increments('id'); |
|
17 | - $table->string('name',100); |
|
18 | - $table->string('key',100)->unique(); |
|
19 | - $table->string('value',100); |
|
20 | - $table->softDeletes(); |
|
21 | - $table->timestamps(); |
|
22 | - }); |
|
16 | + $table->increments('id'); |
|
17 | + $table->string('name',100); |
|
18 | + $table->string('key',100)->unique(); |
|
19 | + $table->string('value',100); |
|
20 | + $table->softDeletes(); |
|
21 | + $table->timestamps(); |
|
22 | + }); |
|
23 | 23 | } |
24 | 24 | |
25 | 25 | /** |
@@ -5,40 +5,40 @@ |
||
5 | 5 | |
6 | 6 | class Log extends Model{ |
7 | 7 | |
8 | - use SoftDeletes; |
|
9 | - protected $table = 'logs'; |
|
10 | - protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
11 | - protected $hidden = ['deleted_at', 'item_type']; |
|
12 | - protected $guarded = ['id']; |
|
13 | - protected $fillable = ['action', 'item_name', 'item_type', 'item_id', 'user_id']; |
|
14 | - |
|
15 | - public function getCreatedAtAttribute($value) |
|
16 | - { |
|
17 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
18 | - } |
|
19 | - |
|
20 | - public function getUpdatedAtAttribute($value) |
|
21 | - { |
|
22 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
23 | - } |
|
24 | - |
|
25 | - public function getDeletedAtAttribute($value) |
|
26 | - { |
|
27 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
28 | - } |
|
8 | + use SoftDeletes; |
|
9 | + protected $table = 'logs'; |
|
10 | + protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
11 | + protected $hidden = ['deleted_at', 'item_type']; |
|
12 | + protected $guarded = ['id']; |
|
13 | + protected $fillable = ['action', 'item_name', 'item_type', 'item_id', 'user_id']; |
|
14 | + |
|
15 | + public function getCreatedAtAttribute($value) |
|
16 | + { |
|
17 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
18 | + } |
|
19 | + |
|
20 | + public function getUpdatedAtAttribute($value) |
|
21 | + { |
|
22 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
23 | + } |
|
24 | + |
|
25 | + public function getDeletedAtAttribute($value) |
|
26 | + { |
|
27 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
28 | + } |
|
29 | 29 | |
30 | - public function user() |
|
31 | - { |
|
32 | - return $this->belongsTo('App\Modules\Acl\AclUser'); |
|
33 | - } |
|
34 | - |
|
35 | - public function item() |
|
36 | - { |
|
37 | - return $this->morphTo(); |
|
38 | - } |
|
39 | - |
|
40 | - public static function boot() |
|
41 | - { |
|
42 | - parent::boot(); |
|
43 | - } |
|
30 | + public function user() |
|
31 | + { |
|
32 | + return $this->belongsTo('App\Modules\Acl\AclUser'); |
|
33 | + } |
|
34 | + |
|
35 | + public function item() |
|
36 | + { |
|
37 | + return $this->morphTo(); |
|
38 | + } |
|
39 | + |
|
40 | + public static function boot() |
|
41 | + { |
|
42 | + parent::boot(); |
|
43 | + } |
|
44 | 44 | } |
@@ -15,14 +15,14 @@ |
||
15 | 15 | Schema::create('notifications', function (Blueprint $table) { |
16 | 16 | $table->increments('id'); |
17 | 17 | $table->string('name',100); |
18 | - $table->text('description')->nullable(); |
|
18 | + $table->text('description')->nullable(); |
|
19 | 19 | $table->string('item_name',100); |
20 | 20 | $table->string('item_type',100); |
21 | 21 | $table->integer('item_id'); |
22 | 22 | $table->boolean('notified'); |
23 | 23 | $table->softDeletes(); |
24 | 24 | $table->timestamps(); |
25 | - }); |
|
25 | + }); |
|
26 | 26 | } |
27 | 27 | |
28 | 28 | /** |