Skip to content

Commit 7525a66

Browse files
committed
Crud and Listing traits update to work for both admin and REST
1 parent aff6325 commit 7525a66

File tree

4 files changed

+80
-28
lines changed

4 files changed

+80
-28
lines changed

admin/controller/crud.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
use Vvveb\System\Traits\Crud as CrudTrait;
2727

2828
class Crud extends Base {
29-
use CrudTrait;
29+
use CrudTrait {
30+
CrudTrait::index as get;
31+
}
3032

3133
function index() {
3234
$result = $this->get();

admin/controller/listing.php

+22-1
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,32 @@
2626
use Vvveb\System\Traits\Listing as ListingTrait;
2727

2828
class Listing extends Base {
29-
use ListingTrait;
29+
use ListingTrait {
30+
ListingTrait::index as get;
31+
}
3032

3133
function index() {
3234
$results = $this->get();
3335

36+
$type = $this->type;
37+
38+
if (isset($results[$type])) {
39+
$controller = $this->controller ?? $type;
40+
$listController = $this->listController ?? $type;
41+
$type_id = $this->type_id ?? "{$type}_id";
42+
$data_id = $this->data_id ?? "{$type}_id";
43+
$list = $this->list ?? $type;
44+
$module = $this->module;
45+
46+
foreach ($results[$type] as $id => &$row) {
47+
$params = ['module' => "$module/$controller", $type_id => $row[$type_id]];
48+
$paramsList = ['module' => "$module/$listController", $type_id => $row[$type_id]];
49+
$row['url'] = \Vvveb\url($params);
50+
$row['edit-url'] = \Vvveb\url($params);
51+
$row['delete-url'] = \Vvveb\url($paramsList + ['action' => 'delete', $type_id . '[]' => $row[$type_id]]);
52+
}
53+
}
54+
3455
$this->view->{$this->list} = $results[$this->type] ?? [];
3556

3657
$this->view->status = [0 => __('Disabled'), 1 => __('Enabled')];

system/traits/crud.php

+42-11
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,30 @@
2525
use function Vvveb\__;
2626
use function Vvveb\humanReadable;
2727
use function Vvveb\model;
28+
use function Vvveb\sanitizeHTML;
2829
use Vvveb\System\Images;
2930

3031
trait Crud {
31-
protected $module = '';
32+
protected $module;
3233

33-
protected $type = '';
34+
protected $type;
3435

35-
protected $controller = '';
36+
protected $controller;
3637

3738
protected $redirect = true;
3839

39-
protected $data_id = false;
40+
protected $data_id;
4041

4142
protected $type_id;
4243

44+
protected $model;
45+
4346
protected $options = [];
4447

4548
protected $data = [];
4649

50+
//protected $fullPost;
51+
4752
function delete() {
4853
$type = $this->type;
4954
$type_id = $this->type_id ?? "{$type}_id";
@@ -83,9 +88,15 @@ function save() {
8388
$type_id = "{$type}_id";
8489
$module = $this->module;
8590
$controller = $this->controller ?? $type;
91+
$result = [];
8692

8793
$this->data_id = $this->request->get[$type_id] ?? false;
88-
$this->data = $this->request->post[$type] ?? false;
94+
95+
if (isset($this->fullPost) && $this->fullPost) {
96+
$this->data = $this->request->post ?? [];
97+
} else {
98+
$this->data = $this->request->post[$type] ?? [];
99+
}
89100

90101
if ($this->data) {
91102
if (! isset($this->modelName)) {
@@ -98,7 +109,18 @@ function save() {
98109
$this->data['created_at'] = $this->data['created_at'] ?? date('Y-m-d H:i:s');
99110
}
100111
$this->data['updated_at'] = $this->data['updated_at'] ?? date('Y-m-d H:i:s');
101-
$options = [$type => $this->data] + $this->global;
112+
113+
if (isset($this->fullPost) && $this->fullPost) {
114+
$options = $this->data + $this->global;
115+
} else {
116+
$options = [$type => $this->data] + $this->global;
117+
}
118+
119+
foreach (['content', 'name', 'excerpt'] as $field) {
120+
if (isset($this->data[$field])) {
121+
$this->data[$field] = sanitizeHTML($this->data[$field]);
122+
}
123+
}
102124

103125
if ($this->data_id) {
104126
$options[$type_id] = $this->data_id;
@@ -126,24 +148,33 @@ function save() {
126148
if ($this->redirect) {
127149
return $this->index();
128150
}
151+
152+
return $result;
129153
}
130154

131-
protected function get() {
155+
protected function index() {
132156
$type = $this->type;
133157
$type_id = $this->type_id ?? "{$type}_id";
134158
$this->data_id = $this->request->get[$type_id] ?? false;
159+
$this->slug = $this->request->get['slug'] ?? false;
135160
$this->data = [];
136161

137-
if ($this->data_id) {
162+
if ($this->data_id || $this->slug) {
138163
if (! isset($this->modelName)) {
139164
$this->modelName = $type;
140165
}
141166

142167
$this->model = model($this->modelName);
143168

144-
$this->options += [
145-
$type_id => $this->data_id,
146-
] + $this->global;
169+
if ($this->data_id) {
170+
$this->options[$type_id] = $this->data_id;
171+
}
172+
173+
if ($this->slug) {
174+
$this->options['slug'] = $this->slug;
175+
}
176+
177+
$this->options += $this->global + $this->request->get;
147178
unset($this->options['user_id']);
148179

149180
$result = $this->model->get($this->options);

system/traits/listing.php

+13-15
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ trait Listing {
3838

3939
protected $data_id;
4040

41+
protected $model;
42+
43+
protected $modelName;
44+
4145
protected $options = [];
4246

4347
function init() {
@@ -60,10 +64,10 @@ function delete() {
6064
}
6165

6266
if (! isset($this->modelName)) {
63-
$modelName = $type;
67+
$this->modelName = $type;
6468
}
6569

66-
$model = model($modelName);
70+
$model = model($this->modelName);
6771
$options = [$type_id => $data_id] + $this->global;
6872
$result = $model->delete($options);
6973
$name = ucfirst(__($type));
@@ -82,7 +86,7 @@ function delete() {
8286
return $this->index();
8387
}
8488

85-
function get() {
89+
function index() {
8690
$type = $this->type;
8791
$controller = $this->controller ?? $type;
8892
$listController = $this->listController ?? $type;
@@ -92,12 +96,11 @@ function get() {
9296
$module = $this->module;
9397
$this->filter = $this->request->get['filter'] ?? [];
9498

95-
if (isset($this->modelName)) {
96-
$modelName = $this->modelName;
97-
} else {
98-
$modelName = $type;
99+
if (! isset($this->modelName)) {
100+
$this->modelName = $type;
99101
}
100-
$model = model($modelName);
102+
103+
$model = model($this->modelName);
101104

102105
$page = max($this->request->get['page'] ?? 1, 1);
103106
$limit = (int)($this->request->get['limit'] ?? 10);
@@ -107,7 +110,7 @@ function get() {
107110
'start' => $start,
108111
'limit' => $limit,
109112
//'type' => $this->type,
110-
] + $this->global + $this->filter;
113+
] + $this->global + $this->filter + $this->request->get;
111114
unset($options['user_id']);
112115

113116
if ($this->data_id && ($id = $this->request->get[$this->data_id] ?? false)) {
@@ -129,14 +132,9 @@ function get() {
129132
$row['image'] = Images::image($row['image'], $type);
130133
}
131134
}
132-
133-
$params = ['module' => "$module/$controller", $type_id => $row[$type_id]];
134-
$paramsList = ['module' => "$module/$listController", $type_id => $row[$type_id]];
135-
$row['url'] = \Vvveb\url($params);
136-
$row['edit-url'] = \Vvveb\url($params);
137-
$row['delete-url'] = \Vvveb\url($paramsList + ['action' => 'delete', $type_id . '[]' => $row[$type_id]]);
138135
}
139136
}
137+
$results[$type] = array_values($results[$type] ?? []);
140138

141139
$options['page'] = $page;
142140

0 commit comments

Comments
 (0)