Skip to content

Commit 3e62ea5

Browse files
committed
Crud controllers trait refactoring
1 parent 2300670 commit 3e62ea5

File tree

4 files changed

+323
-188
lines changed

4 files changed

+323
-188
lines changed

admin/controller/crud.php

+8-86
Original file line numberDiff line numberDiff line change
@@ -23,99 +23,21 @@
2323
namespace Vvveb\Controller;
2424

2525
use function Vvveb\__;
26-
use function Vvveb\humanReadable;
27-
use function Vvveb\model;
28-
use Vvveb\System\Core\View;
29-
use Vvveb\System\Images;
26+
use Vvveb\System\Traits\Crud as CrudTrait;
3027

3128
class Crud extends Base {
32-
protected $module = '';
33-
34-
protected $type = '';
35-
36-
protected $redirect = true;
37-
38-
function save() {
39-
$type = $this->type;
40-
$type_id = "{$type}_id";
41-
$module = $this->module;
42-
$controller = $this->controller ?? $type;
43-
44-
$data_id = $this->request->get[$type_id] ?? false;
45-
$data = $this->request->post[$type] ?? false;
46-
$model = model($type);
47-
48-
if ($data) {
49-
$model = model($type);
50-
51-
if (! $data_id) {
52-
$data['created_at'] = $data['created_at'] ?? date('Y-m-d H:i:s');
53-
}
54-
$data['updated_at'] = $data['updated_at'] ?? date('Y-m-d H:i:s');
55-
$options = [$type => $data] + $this->global;
56-
57-
if ($data_id) {
58-
$options[$type_id] = $data_id;
59-
$this->$type_id = $data_id;
60-
$result = $model->edit($options);
61-
} else {
62-
$result = $model->add($options);
63-
$this->$type_id = $result[$type] ?? false;
64-
}
65-
66-
if ($result && isset($result[$type])) {
67-
$successMessage = humanReadable(__($type)) . __(' saved!');
68-
$this->view->success[] = $successMessage;
69-
70-
$this->session->set('success', $successMessage);
71-
72-
if (! $data_id && $this->redirect) {
73-
$this->redirect(['module' => "$module/$controller", $type_id => $result[$type]]);
74-
}
75-
} else {
76-
$this->view->errors[] = __('Error saving!');
77-
}
78-
}
79-
80-
if ($this->redirect) {
81-
return $this->index();
82-
}
83-
}
29+
use CrudTrait;
8430

8531
function index() {
86-
$type = $this->type;
87-
$type_id = "{$type}_id";
88-
$view = View :: getInstance();
89-
$data_id = $this->request->get[$type_id] ?? false;
90-
$admin_path = \Vvveb\adminPath();
91-
$data = [];
92-
93-
if ($data_id) {
94-
if (isset($this->model)) {
95-
$modelName = $this->model;
96-
} else {
97-
$modelName = $type;
98-
}
99-
100-
$model = model($modelName);
101-
102-
$options = [
103-
$type_id => $data_id,
104-
] + $this->global;
105-
unset($options['user_id']);
106-
107-
$data = $model->get($options);
32+
$result = $this->get();
10833

109-
if (isset($data['image'])) {
110-
$data['image_url'] = Images::image($data['image'], $type);
111-
}
112-
}
34+
$this->view->{$this->type} = $this->data;
11335

114-
$controllerPath = $admin_path . 'index.php?module=media/media';
115-
$view->scanUrl = "$controllerPath&action=scan";
116-
$view->uploadUrl = "$controllerPath&action=upload";
36+
$admin_path = \Vvveb\adminPath();
37+
$controllerPath = $admin_path . 'index.php?module=media/media';
38+
$this->view->scanUrl = "$controllerPath&action=scan";
39+
$this->uploadUrl = "$controllerPath&action=upload";
11740

11841
$this->view->status = [0 => __('Inactive'), 1 => __('Active')];
119-
$this->view->$type = $data;
12042
}
12143
}

admin/controller/listing.php

+8-102
Original file line numberDiff line numberDiff line change
@@ -23,113 +23,19 @@
2323
namespace Vvveb\Controller;
2424

2525
use function Vvveb\__;
26-
use function Vvveb\humanReadable;
27-
use function Vvveb\model;
28-
use Vvveb\System\Core\View;
29-
use Vvveb\System\Images;
26+
use Vvveb\System\Traits\Listing as ListingTrait;
3027

3128
class Listing extends Base {
32-
protected $module = '';
33-
34-
protected $type = '';
35-
36-
function init() {
37-
if (isset($this->request->get['type'])) {
38-
//$this->type = $this->request->get['type'];
39-
}
40-
41-
return parent::init();
42-
}
43-
44-
function delete() {
45-
$type = $this->type;
46-
$type_id = "{$type}_id";
47-
48-
$data_id = $this->request->post[$type_id] ?? $this->request->get[$type_id] ?? false;
49-
50-
if ($data_id) {
51-
if (is_numeric($data_id)) {
52-
$data_id = [$data_id];
53-
}
54-
55-
if (isset($this->model)) {
56-
$modelName = $this->model;
57-
} else {
58-
$modelName = $type;
59-
}
60-
61-
$model = model($modelName);
62-
$options = [$type_id => $data_id] + $this->global;
63-
$result = $model->delete($options);
64-
$name = ucfirst(__($type));
65-
66-
if ($result && isset($result[$type])) {
67-
$this->view->success[] = sprintf(__('%s(s) deleted!'), humanReadable($name));
68-
} else {
69-
$this->view->errors[] = sprintf(__('Error deleting %s!'), humanReadable($name));
70-
}
71-
}
72-
73-
return $this->index();
74-
}
29+
use ListingTrait;
7530

7631
function index() {
77-
$view = View :: getInstance();
78-
79-
$type = $this->type;
80-
$controller = $this->controller ?? $type;
81-
$listController = $this->listController ?? $type;
82-
$type_id = "{$type}_id";
83-
$list = $this->list;
84-
$module = $this->module;
85-
$this->filter = $this->request->get['filter'] ?? [];
86-
87-
if (isset($this->model)) {
88-
$modelName = $this->model;
89-
} else {
90-
$modelName = $type;
91-
}
92-
$model = model($modelName);
93-
94-
$page = max($this->request->get['page'] ?? 1, 1);
95-
$limit = (int)($this->request->get['limit'] ?? 10);
96-
$start = ($page - 1) * $limit;
97-
98-
$options = [
99-
'start' => $start,
100-
'limit' => $limit,
101-
//'type' => $this->type,
102-
] + $this->global + $this->filter;
103-
unset($options['user_id']);
104-
105-
$results = $model->getAll($options);
106-
107-
if (isset($results[$type])) {
108-
foreach ($results[$type] as $id => &$row) {
109-
if (isset($row['images'])) {
110-
$row['images'] = json_decode($row['images'], 1);
111-
112-
foreach ($row['images'] as &$image) {
113-
$image = Images::image($image, $type);
114-
}
115-
} else {
116-
if (isset($row['image'])) {
117-
$row['image'] = Images::image($row['image'], $type);
118-
}
119-
}
32+
$results = $this->get();
12033

121-
$params = ['module' => "$module/$controller", $type_id => $row[$type_id]];
122-
$paramsList = ['module' => "$module/$listController", $type_id => $row[$type_id]];
123-
$row['url'] = \Vvveb\url($params);
124-
$row['edit-url'] = \Vvveb\url($params);
125-
$row['delete-url'] = \Vvveb\url($paramsList + ['action' => 'delete', $type_id . '[]' => $row[$type_id]]);
126-
}
127-
}
34+
$this->view->{$this->list} = $results[$this->type] ?? [];
12835

129-
$view->status = [0 => 'Disabled', 1 => 'Enabled'];
130-
$view->filter = $this->filter;
131-
$view->$list = $results[$type] ?? [];
132-
$view->count = $results['count'] ?? 0;
133-
$view->limit = $limit;
36+
$this->view->status = [0 => __('Disabled'), 1 => __('Enabled')];
37+
$this->view->filter = $this->filter;
38+
$this->view->count = $results['count'] ?? 0;
39+
$this->view->limit = $results['limit'] ?? 0;
13440
}
13541
}

system/traits/crud.php

+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<?php
2+
3+
/**
4+
* Vvveb
5+
*
6+
* Copyright (C) 2022 Ziadin Givan
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as
10+
* published by the Free Software Foundation, either version 3 of the
11+
* License, or (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
namespace Vvveb\System\Traits;
24+
25+
use function Vvveb\__;
26+
use function Vvveb\humanReadable;
27+
use function Vvveb\model;
28+
use Vvveb\System\Images;
29+
30+
trait Crud {
31+
protected $module = '';
32+
33+
protected $type = '';
34+
35+
protected $controller = '';
36+
37+
protected $redirect = true;
38+
39+
protected $data_id = false;
40+
41+
protected $type_id;
42+
43+
protected $options = [];
44+
45+
protected $data = [];
46+
47+
function delete() {
48+
$type = $this->type;
49+
$type_id = $this->type_id ?? "{$type}_id";
50+
51+
$data_id = $this->request->post[$type_id] ?? $this->request->get[$type_id] ?? false;
52+
53+
if ($data_id) {
54+
if (is_numeric($data_id)) {
55+
$data_id = [$data_id];
56+
}
57+
58+
if (! isset($this->modelName)) {
59+
$modelName = $type;
60+
}
61+
62+
$model = model($modelName);
63+
$options = [$type_id => $data_id] + $this->global;
64+
$result = $model->delete($options);
65+
$name = ucfirst(__($type));
66+
67+
if ($result && isset($result[$type])) {
68+
if ($result[$type]) {
69+
$this->view->success[] = sprintf(__('%s(s) deleted!'), humanReadable($name));
70+
} else {
71+
$this->view->info[] = sprintf(__('No rows affected!'), humanReadable($name));
72+
}
73+
} else {
74+
$this->view->errors[] = sprintf(__('Error deleting %s!'), humanReadable($name));
75+
}
76+
}
77+
78+
return $this->index();
79+
}
80+
81+
function save() {
82+
$type = $this->type;
83+
$type_id = "{$type}_id";
84+
$module = $this->module;
85+
$controller = $this->controller ?? $type;
86+
87+
$this->data_id = $this->request->get[$type_id] ?? false;
88+
$this->data = $this->request->post[$type] ?? false;
89+
90+
if ($this->data) {
91+
if (! isset($this->modelName)) {
92+
$this->modelName = $type;
93+
}
94+
95+
$this->model = model($this->modelName);
96+
97+
if (! $this->data_id) {
98+
$this->data['created_at'] = $this->data['created_at'] ?? date('Y-m-d H:i:s');
99+
}
100+
$this->data['updated_at'] = $this->data['updated_at'] ?? date('Y-m-d H:i:s');
101+
$options = [$type => $this->data] + $this->global;
102+
103+
if ($this->data_id) {
104+
$options[$type_id] = $this->data_id;
105+
$this->$type_id = $this->data_id;
106+
$result = $this->model->edit($options);
107+
} else {
108+
$result = $this->model->add($options);
109+
$this->$type_id = $result[$type] ?? false;
110+
}
111+
112+
if ($result && isset($result[$type])) {
113+
$successMessage = humanReadable(__($type)) . __(' saved!');
114+
$this->view->success[] = $successMessage;
115+
116+
$this->session->set('success', $successMessage);
117+
118+
if (! $this->data_id && $this->redirect) {
119+
$this->redirect(['module' => "$module/$controller", $type_id => $result[$type]]);
120+
}
121+
} else {
122+
$this->view->errors[] = __('Error saving!');
123+
}
124+
}
125+
126+
if ($this->redirect) {
127+
return $this->index();
128+
}
129+
}
130+
131+
protected function get() {
132+
$type = $this->type;
133+
$type_id = $this->type_id ?? "{$type}_id";
134+
$this->data_id = $this->request->get[$type_id] ?? false;
135+
$this->data = [];
136+
137+
if ($this->data_id) {
138+
if (! isset($this->modelName)) {
139+
$this->modelName = $type;
140+
}
141+
142+
$this->model = model($this->modelName);
143+
144+
$this->options += [
145+
$type_id => $this->data_id,
146+
] + $this->global;
147+
unset($this->options['user_id']);
148+
149+
$result = $this->model->get($this->options);
150+
151+
if ($result) {
152+
$this->data = $result;
153+
154+
if (isset($this->data['image'])) {
155+
$this->data['image_url'] = Images::image($this->data['image'], $type);
156+
}
157+
}
158+
}
159+
160+
return $this->data;
161+
}
162+
}

0 commit comments

Comments
 (0)