Skip to content

armgit / Vvveb

Public Imported from GitHub
0

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

Crud and Listing traits update to work for both admin and REST

givanz7525a660603a
diff --git a/admin/controller/crud.php b/admin/controller/crud.php
index 5eb15e4..ed8ab78 100755
--- a/admin/controller/crud.php
+++ b/admin/controller/crud.php
@@ -26,7 +26,9 @@ use function Vvveb\__;
use Vvveb\System\Traits\Crud as CrudTrait;
class Crud extends Base {
- use CrudTrait;
+ use CrudTrait {
+ CrudTrait::index as get;
+ }
function index() {
$result = $this->get();
diff --git a/admin/controller/listing.php b/admin/controller/listing.php
index c617c9f..673b6ca 100755
--- a/admin/controller/listing.php
+++ b/admin/controller/listing.php
@@ -26,11 +26,32 @@ use function Vvveb\__;
use Vvveb\System\Traits\Listing as ListingTrait;
class Listing extends Base {
- use ListingTrait;
+ use ListingTrait {
+ ListingTrait::index as get;
+ }
function index() {
$results = $this->get();
+ $type = $this->type;
+
+ if (isset($results[$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;
+
+ foreach ($results[$type] as $id => &$row) {
+ $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] ?? [];
$this->view->status = [0 => __('Disabled'), 1 => __('Enabled')];
diff --git a/system/traits/crud.php b/system/traits/crud.php
index a8f29f2..f54833b 100644
--- a/system/traits/crud.php
+++ b/system/traits/crud.php
@@ -25,25 +25,30 @@ namespace Vvveb\System\Traits;
use function Vvveb\__;
use function Vvveb\humanReadable;
use function Vvveb\model;
+use function Vvveb\sanitizeHTML;
use Vvveb\System\Images;
trait Crud {
- protected $module = '';
+ protected $module;
- protected $type = '';
+ protected $type;
- protected $controller = '';
+ protected $controller;
protected $redirect = true;
- protected $data_id = false;
+ protected $data_id;
protected $type_id;
+ protected $model;
+
protected $options = [];
protected $data = [];
+ //protected $fullPost;
+
function delete() {
$type = $this->type;
$type_id = $this->type_id ?? "{$type}_id";
@@ -83,9 +88,15 @@ trait Crud {
$type_id = "{$type}_id";
$module = $this->module;
$controller = $this->controller ?? $type;
+ $result = [];
$this->data_id = $this->request->get[$type_id] ?? false;
- $this->data = $this->request->post[$type] ?? false;
+
+ if (isset($this->fullPost) && $this->fullPost) {
+ $this->data = $this->request->post ?? [];
+ } else {
+ $this->data = $this->request->post[$type] ?? [];
+ }
if ($this->data) {
if (! isset($this->modelName)) {
@@ -98,7 +109,18 @@ trait Crud {
$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 (isset($this->fullPost) && $this->fullPost) {
+ $options = $this->data + $this->global;
+ } else {
+ $options = [$type => $this->data] + $this->global;
+ }
+
+ foreach (['content', 'name', 'excerpt'] as $field) {
+ if (isset($this->data[$field])) {
+ $this->data[$field] = sanitizeHTML($this->data[$field]);
+ }
+ }
if ($this->data_id) {
$options[$type_id] = $this->data_id;
@@ -126,24 +148,33 @@ trait Crud {
if ($this->redirect) {
return $this->index();
}
+
+ return $result;
}
- protected function get() {
+ protected function index() {
$type = $this->type;
$type_id = $this->type_id ?? "{$type}_id";
$this->data_id = $this->request->get[$type_id] ?? false;
+ $this->slug = $this->request->get['slug'] ?? false;
$this->data = [];
- if ($this->data_id) {
+ if ($this->data_id || $this->slug) {
if (! isset($this->modelName)) {
$this->modelName = $type;
}
$this->model = model($this->modelName);
- $this->options += [
- $type_id => $this->data_id,
- ] + $this->global;
+ if ($this->data_id) {
+ $this->options[$type_id] = $this->data_id;
+ }
+
+ if ($this->slug) {
+ $this->options['slug'] = $this->slug;
+ }
+
+ $this->options += $this->global + $this->request->get;
unset($this->options['user_id']);
$result = $this->model->get($this->options);
diff --git a/system/traits/listing.php b/system/traits/listing.php
index ddf78be..3fa8f14 100644
--- a/system/traits/listing.php
+++ b/system/traits/listing.php
@@ -38,6 +38,10 @@ trait Listing {
protected $data_id;
+ protected $model;
+
+ protected $modelName;
+
protected $options = [];
function init() {
@@ -60,10 +64,10 @@ trait Listing {
}
if (! isset($this->modelName)) {
- $modelName = $type;
+ $this->modelName = $type;
}
- $model = model($modelName);
+ $model = model($this->modelName);
$options = [$type_id => $data_id] + $this->global;
$result = $model->delete($options);
$name = ucfirst(__($type));
@@ -82,7 +86,7 @@ trait Listing {
return $this->index();
}
- function get() {
+ function index() {
$type = $this->type;
$controller = $this->controller ?? $type;
$listController = $this->listController ?? $type;
@@ -92,12 +96,11 @@ trait Listing {
$module = $this->module;
$this->filter = $this->request->get['filter'] ?? [];
- if (isset($this->modelName)) {
- $modelName = $this->modelName;
- } else {
- $modelName = $type;
+ if (! isset($this->modelName)) {
+ $this->modelName = $type;
}
- $model = model($modelName);
+
+ $model = model($this->modelName);
$page = max($this->request->get['page'] ?? 1, 1);
$limit = (int)($this->request->get['limit'] ?? 10);
@@ -107,7 +110,7 @@ trait Listing {
'start' => $start,
'limit' => $limit,
//'type' => $this->type,
- ] + $this->global + $this->filter;
+ ] + $this->global + $this->filter + $this->request->get;
unset($options['user_id']);
if ($this->data_id && ($id = $this->request->get[$this->data_id] ?? false)) {
@@ -129,14 +132,9 @@ trait Listing {
$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]]);
}
}
+ $results[$type] = array_values($results[$type] ?? []);
$options['page'] = $page;