Skip to content

armgit / Vvveb

Public Imported from GitHub
0

Powerful and easy to use cms to build websites, blogs or ecommerce stores.

Crud controllers trait refactoring

givanz3e62ea5a23c9
diff --git a/admin/controller/crud.php b/admin/controller/crud.php
index 60564cb..5eb15e4 100755
--- a/admin/controller/crud.php
+++ b/admin/controller/crud.php
@@ -23,99 +23,21 @@
namespace Vvveb\Controller;
use function Vvveb\__;
-use function Vvveb\humanReadable;
-use function Vvveb\model;
-use Vvveb\System\Core\View;
-use Vvveb\System\Images;
+use Vvveb\System\Traits\Crud as CrudTrait;
class Crud extends Base {
- protected $module = '';
-
- protected $type = '';
-
- protected $redirect = true;
-
- function save() {
- $type = $this->type;
- $type_id = "{$type}_id";
- $module = $this->module;
- $controller = $this->controller ?? $type;
-
- $data_id = $this->request->get[$type_id] ?? false;
- $data = $this->request->post[$type] ?? false;
- $model = model($type);
-
- if ($data) {
- $model = model($type);
-
- if (! $data_id) {
- $data['created_at'] = $data['created_at'] ?? date('Y-m-d H:i:s');
- }
- $data['updated_at'] = $data['updated_at'] ?? date('Y-m-d H:i:s');
- $options = [$type => $data] + $this->global;
-
- if ($data_id) {
- $options[$type_id] = $data_id;
- $this->$type_id = $data_id;
- $result = $model->edit($options);
- } else {
- $result = $model->add($options);
- $this->$type_id = $result[$type] ?? false;
- }
-
- if ($result && isset($result[$type])) {
- $successMessage = humanReadable(__($type)) . __(' saved!');
- $this->view->success[] = $successMessage;
-
- $this->session->set('success', $successMessage);
-
- if (! $data_id && $this->redirect) {
- $this->redirect(['module' => "$module/$controller", $type_id => $result[$type]]);
- }
- } else {
- $this->view->errors[] = __('Error saving!');
- }
- }
-
- if ($this->redirect) {
- return $this->index();
- }
- }
+ use CrudTrait;
function index() {
- $type = $this->type;
- $type_id = "{$type}_id";
- $view = View :: getInstance();
- $data_id = $this->request->get[$type_id] ?? false;
- $admin_path = \Vvveb\adminPath();
- $data = [];
-
- if ($data_id) {
- if (isset($this->model)) {
- $modelName = $this->model;
- } else {
- $modelName = $type;
- }
-
- $model = model($modelName);
-
- $options = [
- $type_id => $data_id,
- ] + $this->global;
- unset($options['user_id']);
-
- $data = $model->get($options);
+ $result = $this->get();
- if (isset($data['image'])) {
- $data['image_url'] = Images::image($data['image'], $type);
- }
- }
+ $this->view->{$this->type} = $this->data;
- $controllerPath = $admin_path . 'index.php?module=media/media';
- $view->scanUrl = "$controllerPath&action=scan";
- $view->uploadUrl = "$controllerPath&action=upload";
+ $admin_path = \Vvveb\adminPath();
+ $controllerPath = $admin_path . 'index.php?module=media/media';
+ $this->view->scanUrl = "$controllerPath&action=scan";
+ $this->uploadUrl = "$controllerPath&action=upload";
$this->view->status = [0 => __('Inactive'), 1 => __('Active')];
- $this->view->$type = $data;
}
}
diff --git a/admin/controller/listing.php b/admin/controller/listing.php
index b42511b..c617c9f 100755
--- a/admin/controller/listing.php
+++ b/admin/controller/listing.php
@@ -23,113 +23,19 @@
namespace Vvveb\Controller;
use function Vvveb\__;
-use function Vvveb\humanReadable;
-use function Vvveb\model;
-use Vvveb\System\Core\View;
-use Vvveb\System\Images;
+use Vvveb\System\Traits\Listing as ListingTrait;
class Listing extends Base {
- protected $module = '';
-
- protected $type = '';
-
- function init() {
- if (isset($this->request->get['type'])) {
- //$this->type = $this->request->get['type'];
- }
-
- return parent::init();
- }
-
- function delete() {
- $type = $this->type;
- $type_id = "{$type}_id";
-
- $data_id = $this->request->post[$type_id] ?? $this->request->get[$type_id] ?? false;
-
- if ($data_id) {
- if (is_numeric($data_id)) {
- $data_id = [$data_id];
- }
-
- if (isset($this->model)) {
- $modelName = $this->model;
- } else {
- $modelName = $type;
- }
-
- $model = model($modelName);
- $options = [$type_id => $data_id] + $this->global;
- $result = $model->delete($options);
- $name = ucfirst(__($type));
-
- if ($result && isset($result[$type])) {
- $this->view->success[] = sprintf(__('%s(s) deleted!'), humanReadable($name));
- } else {
- $this->view->errors[] = sprintf(__('Error deleting %s!'), humanReadable($name));
- }
- }
-
- return $this->index();
- }
+ use ListingTrait;
function index() {
- $view = View :: getInstance();
-
- $type = $this->type;
- $controller = $this->controller ?? $type;
- $listController = $this->listController ?? $type;
- $type_id = "{$type}_id";
- $list = $this->list;
- $module = $this->module;
- $this->filter = $this->request->get['filter'] ?? [];
-
- if (isset($this->model)) {
- $modelName = $this->model;
- } else {
- $modelName = $type;
- }
- $model = model($modelName);
-
- $page = max($this->request->get['page'] ?? 1, 1);
- $limit = (int)($this->request->get['limit'] ?? 10);
- $start = ($page - 1) * $limit;
-
- $options = [
- 'start' => $start,
- 'limit' => $limit,
- //'type' => $this->type,
- ] + $this->global + $this->filter;
- unset($options['user_id']);
-
- $results = $model->getAll($options);
-
- if (isset($results[$type])) {
- foreach ($results[$type] as $id => &$row) {
- if (isset($row['images'])) {
- $row['images'] = json_decode($row['images'], 1);
-
- foreach ($row['images'] as &$image) {
- $image = Images::image($image, $type);
- }
- } else {
- if (isset($row['image'])) {
- $row['image'] = Images::image($row['image'], $type);
- }
- }
+ $results = $this->get();
- $params = ['module' => "$module/$controller", $type_id => $row[$type_id]];
- $paramsList = ['module' => "$module/$listController", $type_id => $row[$type_id]];
- $row['url'] = \Vvveb\url($params);
- $row['edit-url'] = \Vvveb\url($params);
- $row['delete-url'] = \Vvveb\url($paramsList + ['action' => 'delete', $type_id . '[]' => $row[$type_id]]);
- }
- }
+ $this->view->{$this->list} = $results[$this->type] ?? [];
- $view->status = [0 => 'Disabled', 1 => 'Enabled'];
- $view->filter = $this->filter;
- $view->$list = $results[$type] ?? [];
- $view->count = $results['count'] ?? 0;
- $view->limit = $limit;
+ $this->view->status = [0 => __('Disabled'), 1 => __('Enabled')];
+ $this->view->filter = $this->filter;
+ $this->view->count = $results['count'] ?? 0;
+ $this->view->limit = $results['limit'] ?? 0;
}
}
diff --git a/system/traits/crud.php b/system/traits/crud.php
new file mode 100644
index 0000000..a8f29f2
--- /dev/null
+++ b/system/traits/crud.php
@@ -0,0 +1,162 @@
+<?php
+
+/**
+ * Vvveb
+ *
+ * Copyright (C) 2022 Ziadin Givan
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Vvveb\System\Traits;
+
+use function Vvveb\__;
+use function Vvveb\humanReadable;
+use function Vvveb\model;
+use Vvveb\System\Images;
+
+trait Crud {
+ protected $module = '';
+
+ protected $type = '';
+
+ protected $controller = '';
+
+ protected $redirect = true;
+
+ protected $data_id = false;
+
+ protected $type_id;
+
+ protected $options = [];
+
+ protected $data = [];
+
+ function delete() {
+ $type = $this->type;
+ $type_id = $this->type_id ?? "{$type}_id";
+
+ $data_id = $this->request->post[$type_id] ?? $this->request->get[$type_id] ?? false;
+
+ if ($data_id) {
+ if (is_numeric($data_id)) {
+ $data_id = [$data_id];
+ }
+
+ if (! isset($this->modelName)) {
+ $modelName = $type;
+ }
+
+ $model = model($modelName);
+ $options = [$type_id => $data_id] + $this->global;
+ $result = $model->delete($options);
+ $name = ucfirst(__($type));
+
+ if ($result && isset($result[$type])) {
+ if ($result[$type]) {
+ $this->view->success[] = sprintf(__('%s(s) deleted!'), humanReadable($name));
+ } else {
+ $this->view->info[] = sprintf(__('No rows affected!'), humanReadable($name));
+ }
+ } else {
+ $this->view->errors[] = sprintf(__('Error deleting %s!'), humanReadable($name));
+ }
+ }
+
+ return $this->index();
+ }
+
+ function save() {
+ $type = $this->type;
+ $type_id = "{$type}_id";
+ $module = $this->module;
+ $controller = $this->controller ?? $type;
+
+ $this->data_id = $this->request->get[$type_id] ?? false;
+ $this->data = $this->request->post[$type] ?? false;
+
+ if ($this->data) {
+ if (! isset($this->modelName)) {
+ $this->modelName = $type;
+ }
+
+ $this->model = model($this->modelName);
+
+ if (! $this->data_id) {
+ $this->data['created_at'] = $this->data['created_at'] ?? date('Y-m-d H:i:s');
+ }
+ $this->data['updated_at'] = $this->data['updated_at'] ?? date('Y-m-d H:i:s');
+ $options = [$type => $this->data] + $this->global;
+
+ if ($this->data_id) {
+ $options[$type_id] = $this->data_id;
+ $this->$type_id = $this->data_id;
+ $result = $this->model->edit($options);
+ } else {
+ $result = $this->model->add($options);
+ $this->$type_id = $result[$type] ?? false;
+ }
+
+ if ($result && isset($result[$type])) {
+ $successMessage = humanReadable(__($type)) . __(' saved!');
+ $this->view->success[] = $successMessage;
+
+ $this->session->set('success', $successMessage);
+
+ if (! $this->data_id && $this->redirect) {
+ $this->redirect(['module' => "$module/$controller", $type_id => $result[$type]]);
+ }
+ } else {
+ $this->view->errors[] = __('Error saving!');
+ }
+ }
+
+ if ($this->redirect) {
+ return $this->index();
+ }
+ }
+
+ protected function get() {
+ $type = $this->type;
+ $type_id = $this->type_id ?? "{$type}_id";
+ $this->data_id = $this->request->get[$type_id] ?? false;
+ $this->data = [];
+
+ if ($this->data_id) {
+ if (! isset($this->modelName)) {
+ $this->modelName = $type;
+ }
+
+ $this->model = model($this->modelName);
+
+ $this->options += [
+ $type_id => $this->data_id,
+ ] + $this->global;
+ unset($this->options['user_id']);
+
+ $result = $this->model->get($this->options);
+
+ if ($result) {
+ $this->data = $result;
+
+ if (isset($this->data['image'])) {
+ $this->data['image_url'] = Images::image($this->data['image'], $type);
+ }
+ }
+ }
+
+ return $this->data;
+ }
+}
diff --git a/system/traits/listing.php b/system/traits/listing.php
new file mode 100644
index 0000000..ddf78be
--- /dev/null
+++ b/system/traits/listing.php
@@ -0,0 +1,145 @@
+<?php
+
+/**
+ * Vvveb
+ *
+ * Copyright (C) 2022 Ziadin Givan
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Vvveb\System\Traits;
+
+use function Vvveb\__;
+use function Vvveb\humanReadable;
+use function Vvveb\model;
+use Vvveb\System\Images;
+
+trait Listing {
+ protected $module;
+
+ protected $type;
+
+ protected $list;
+
+ protected $type_id;
+
+ protected $data_id;
+
+ protected $options = [];
+
+ function init() {
+ if (isset($this->request->get['type'])) {
+ //$this->type = $this->request->get['type'];
+ }
+
+ return parent::init();
+ }
+
+ function delete() {
+ $type = $this->type;
+ $type_id = $this->type_id ?? "{$type}_id";
+
+ $data_id = $this->request->post[$type_id] ?? $this->request->get[$type_id] ?? false;
+
+ if ($data_id) {
+ if (is_numeric($data_id)) {
+ $data_id = [$data_id];
+ }
+
+ if (! isset($this->modelName)) {
+ $modelName = $type;
+ }
+
+ $model = model($modelName);
+ $options = [$type_id => $data_id] + $this->global;
+ $result = $model->delete($options);
+ $name = ucfirst(__($type));
+
+ if ($result && isset($result[$type])) {
+ if ($result[$type]) {
+ $this->view->success[] = sprintf(__('%s(s) deleted!'), humanReadable($name));
+ } else {
+ $this->view->info[] = sprintf(__('No rows affected!'), humanReadable($name));
+ }
+ } else {
+ $this->view->errors[] = sprintf(__('Error deleting %s!'), humanReadable($name));
+ }
+ }
+
+ return $this->index();
+ }
+
+ function get() {
+ $type = $this->type;
+ $controller = $this->controller ?? $type;
+ $listController = $this->listController ?? $type;
+ $type_id = $this->type_id ?? "{$type}_id";
+ $data_id = $this->data_id ?? "{$type}_id";
+ $list = $this->list ?? $type;
+ $module = $this->module;
+ $this->filter = $this->request->get['filter'] ?? [];
+
+ if (isset($this->modelName)) {
+ $modelName = $this->modelName;
+ } else {
+ $modelName = $type;
+ }
+ $model = model($modelName);
+
+ $page = max($this->request->get['page'] ?? 1, 1);
+ $limit = (int)($this->request->get['limit'] ?? 10);
+ $start = ($page - 1) * $limit;
+
+ $options = [
+ 'start' => $start,
+ 'limit' => $limit,
+ //'type' => $this->type,
+ ] + $this->global + $this->filter;
+ unset($options['user_id']);
+
+ if ($this->data_id && ($id = $this->request->get[$this->data_id] ?? false)) {
+ $options[$this->data_id] = $id;
+ }
+
+ $results = $model->getAll($options);
+
+ if (isset($results[$type])) {
+ foreach ($results[$type] as $id => &$row) {
+ if (isset($row['images'])) {
+ $row['images'] = json_decode($row['images'], 1);
+
+ foreach ($row['images'] as &$image) {
+ $image = Images::image($image, $type);
+ }
+ } else {
+ if (isset($row['image'])) {
+ $row['image'] = Images::image($row['image'], $type);
+ }
+ }
+
+ $params = ['module' => "$module/$controller", $type_id => $row[$type_id]];
+ $paramsList = ['module' => "$module/$listController", $type_id => $row[$type_id]];
+ $row['url'] = \Vvveb\url($params);
+ $row['edit-url'] = \Vvveb\url($params);
+ $row['delete-url'] = \Vvveb\url($paramsList + ['action' => 'delete', $type_id . '[]' => $row[$type_id]]);
+ }
+ }
+
+ $options['page'] = $page;
+
+ return $results + $options;
+ }
+}