aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilly Micieli <micieli@laposte.net>2019-10-23 15:47:34 +0200
committerWilly Micieli <micieli@laposte.net>2019-10-23 15:47:34 +0200
commit2dbf7b8d7afae56883313c7f6ffa5a74ab1d9515 (patch)
tree5c925f7795f32ce6385659a02f3fae02bbb2dd28
parent51b53f7f6316c86efa9001111dc1464cfc81bb1f (diff)
downloadimperium-2dbf7b8d7afae56883313c7f6ffa5a74ab1d9515.zip
imperium-2dbf7b8d7afae56883313c7f6ffa5a74ab1d9515.tar.gz
finish crud
-rw-r--r--config/crud.yaml7
-rw-r--r--helpers/web.php7
-rw-r--r--imperium/Action/Crud.php72
-rw-r--r--imperium/App.php12
-rw-r--r--imperium/Flash/Flash.php4
-rw-r--r--imperium/Html/Form/Form.php155
-rw-r--r--imperium/Query/Query.php2
-rw-r--r--imperium/Tables/Column.php18
-rw-r--r--tests/Model/CrudTest.php10
9 files changed, 152 insertions, 135 deletions
diff --git a/config/crud.yaml b/config/crud.yaml
index f82fc30..fa2d741 100644
--- a/config/crud.yaml
+++ b/config/crud.yaml
@@ -5,4 +5,9 @@ no_updated: 'The record has not been updated'
deleted: 'The record has been deleted successfully'
no_deleted: 'The record has not been deleted'
update_text: 'update'
-create_text: 'create' \ No newline at end of file
+create_text: 'create'
+select_table_text: 'select a table'
+edit_text: 'edit'
+remove_text: 'remove'
+edit_class: 'btn btn-outline-primary'
+remove_class: 'btn btn-outline-danger' \ No newline at end of file
diff --git a/helpers/web.php b/helpers/web.php
index 25e0ec7..011970c 100644
--- a/helpers/web.php
+++ b/helpers/web.php
@@ -358,6 +358,7 @@
* @method route
*
* @param mixed $name
+ * @param bool $admin
* @param mixed $args
*
* @return string
@@ -366,13 +367,13 @@
* @throws Kedavra
* @throws NotFoundException
*/
- function route(string $name,array $args = []): string
+ function route(string $name,bool $admin = false,array $args = []): string
{
- $admin = Admin::where('name', EQUAL, $name)->fetch(true)->all();
+ $admin_r = Admin::where('name', EQUAL, $name)->fetch(true)->all();
$web = Web::where('name', EQUAL, $name)->fetch(true)->all();
- $x = def($web) ? $web : $admin;
+ $x = $admin ? $admin_r : $web;
if (def($args))
diff --git a/imperium/Action/Crud.php b/imperium/Action/Crud.php
index 980d8b8..9c6c3ed 100644
--- a/imperium/Action/Crud.php
+++ b/imperium/Action/Crud.php
@@ -8,7 +8,6 @@
use Imperium\Controller\Controller;
use Imperium\Exception\Kedavra;
use Imperium\Html\Form\Form;
- use Imperium\Html\Table\Table;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Twig\Error\LoaderError;
@@ -94,6 +93,10 @@
*
*/
protected static $limit = 20;
+ /**
+ * @var string
+ */
+ private $current;
/**
@@ -142,8 +145,7 @@
* Read all record with a pagination
*
* @param string $table
- * @param int $current_page
- *
+ *
* @return Response
*
* @throws DependencyException
@@ -152,13 +154,19 @@
* @throws NotFoundException
* @throws RuntimeError
* @throws SyntaxError
- *
*/
- public function show(string $table,int $current_page): Response
+ public function show(string $table): Response
{
- $all = $this->sql($table)->paginate([$this,'home'],$current_page,static::$limit);
+ $this->current = $table;
+ $all = '<div class="table-responsive"><table class="table table-bordered"><thead>';
+ foreach ($this->sql($table)->columns() as $column)
+ append($all,"<th>$column</th>");
+
+ append($all,'</thead><tbody>');
+ append($all,$this->sql($table)->paginate([$this,'display'],$this->get('page',1),static::$limit));
+ append($all,'</tbody></table></div>');
- return $this->view('@crud/show',compact('all'));
+ return $this->view('@crud/show',compact('all','table'));
}
/**
@@ -180,8 +188,8 @@
*/
public function edit(string $table,int $id): Response
{
- $form = $this->form()->generate($table,$this->config('crud','update_text'),'',Form::EDIT,$id);
- return $this->view('@crud/edit',compact('form'));
+ $form = $this->form()->start('update',true,[$table,$id])->generate($table,$this->config('crud','update_text'),'',Form::EDIT,$id);
+ return $this->view('@crud/edit',compact('form','table'));
}
/**
@@ -202,9 +210,9 @@
*/
public function create(string $table): Response
{
- $form = $this->form()->start('create')->generate($table,$this->config('crud','create_text'));
+ $form = $this->form()->start('create',true,[$table])->generate($table,$this->config('crud','create_text'));
- return $this->view('@crud/create',compact('form'));
+ return $this->view('@crud/create',compact('form','table'));
}
/**
@@ -224,6 +232,7 @@
public function refresh(string $table,int $id): RedirectResponse
{
+
$this->init();
$sql = $this->sql($table);
@@ -232,7 +241,7 @@
$columns = collect();
- $values = collect($this->request()->request->all())->del(CSRF_TOKEN)->all();
+ $values = collect($this->request()->request->all())->del(CSRF_TOKEN,'method','__table__')->for('htmlentities')->all();
foreach ($values as $k => $value)
{
@@ -273,22 +282,34 @@
/**
*
*
- * @param $key
- * @param $value
- *
- * @return string
+ * @return Response
+ * @throws DependencyException
+ * @throws Kedavra
+ * @throws LoaderError
+ * @throws NotFoundException
+ * @throws RuntimeError
+ * @throws SyntaxError
*/
- public function home($key, $value): string
+ public function home():Response
{
+ $x = collect(['/' => $this->config('crud','select_table_text')]);
+ foreach ($this->tables() as $table)
+ $x->put(route('show',true,[$table,1]),$table);
+ $tables = $x->all();
+ $form = $this->form()->redirect('table',$tables)->get();
+ return $this->view('@crud/home',compact('tables','form'));
+ }
+ public function display($key,$value)
+ {
+ $html = '<tr>';
+ foreach ($value as $v)
+ append($html,'<td>'.substr($v,0,50).'</td>');
- $html = '<td>';
- foreach ($this->collect(obj($value))->keys()->all() as $key)
- append($html,"<tr>$key</tr>");
-
- return $html .'</td>';
+ append($html,'<td><a href="'.route('edit',true,[$this->current,$value->id]).'" class="'.$this->config('crud','edit_class').'">'.$this->config('crud','edit_text').'</a></td>');
+ append($html,'<td><a href="'.route('remove',true,[$this->current,$value->id]).'" class="'.$this->config('crud','remove_class').'"> '.$this->config('crud','remove_text').'</a></td>');
+ return $html . '</tr>';
}
-
/**
*
* @throws Kedavra
@@ -311,10 +332,7 @@
$this->no_deleted = config($file,'no_deleted');
}
- private function all($table)
- {
- return $this->sql($table)->take(static::$limit)->all();
- }
+
}
} \ No newline at end of file
diff --git a/imperium/App.php b/imperium/App.php
index ff01dae..f303475 100644
--- a/imperium/App.php
+++ b/imperium/App.php
@@ -310,7 +310,17 @@ namespace Imperium {
*/
public function run(): Response
{
- if (equal(config('mode', 'mode'), 'up') || equal(config('mode', 'mode'), 'admin')) {
+ if (equal(config('mode', 'mode'), 'up'))
+ {
+
+ $x = $this->router(ServerRequest::fromGlobals())->search();
+
+ return $x instanceof RedirectResponse ? $x->send() : $x->call()->send();
+ }
+
+ if (equal(config('mode', 'mode'), 'admin') && equal($this->request()->getClientIp(),'127.0.0.1'))
+ {
+
$x = $this->router(ServerRequest::fromGlobals())->search();
return $x instanceof RedirectResponse ? $x->send() : $x->call()->send();
diff --git a/imperium/Flash/Flash.php b/imperium/Flash/Flash.php
index 53d13af..31993a3 100644
--- a/imperium/Flash/Flash.php
+++ b/imperium/Flash/Flash.php
@@ -124,13 +124,11 @@
$failure_class = config('flash','failure_class');
- $column_class = config('flash','column_class');
-
$message = $this->get($key);
if(def($message))
{
- return $success ? '<div class="row"><div class="'.$column_class.'"><div class="'.$success_class.'" role="alert"><div class="flex"><div>' . $message . '</div></div></div></div></div>' : '<div class="row"><div class="'.$column_class.'"><div class="'.$failure_class.'" role="alert"><div class="flex"><div class="font-bold">' . $message . '</div></div></div></div></div>';
+ return $success ? '<div class="'.$success_class.'" role="alert">'.$message.'</div>':'<div class="'.$failure_class.'" role="alert">'.$message.'<div>';
}
return '';
diff --git a/imperium/Html/Form/Form.php b/imperium/Html/Form/Form.php
index 9958307..0a1a1a5 100644
--- a/imperium/Html/Form/Form.php
+++ b/imperium/Html/Form/Form.php
@@ -559,25 +559,27 @@
return self::FORM_SEPARATOR;
}
-
- /**
- *
- * Open the form
- *
- * @method start
- *
- * @param string $route
- * @param string $confirm The confirm text
- * @param string $class The form class
- * @param bool $enctype Configuration to support upload
- * @param string $charset The form charset
- *
- * @throws Kedavra
- *
- * @return Form
- *
- */
- public function start(string $route, string $confirm = '', string $class = '', bool $enctype = false, string $charset = 'utf-8') : Form
+
+ /**
+ *
+ * Open the form
+ *
+ * @method start
+ *
+ * @param string $route
+ * @param bool $admin
+ * @param array $args
+ * @param string $confirm The confirm text
+ * @param string $class The form class
+ * @param bool $enctype Configuration to support upload
+ * @param string $charset The form charset
+ *
+ * @return Form
+ * @throws DependencyException
+ * @throws Kedavra
+ * @throws NotFoundException
+ */
+ public function start(string $route,bool $admin,array $args,string $confirm = '', string $class = '', bool $enctype = false, string $charset = 'utf-8') : Form
{
$this->method = detect_method($route);
@@ -589,16 +591,16 @@
if($enctype)
{
if(not_def($class))
- append($this->form, '<form action="' . route($route) . '" method="' . $method . '" class="' . self::VALIDATE . '" accept-charset="' . $charset . '" enctype="multipart/form-data" onsubmit="return confirm(' . "'" . $confirm . "'" . ')" >');
+ append($this->form, '<form action="' . route($route,$admin,$args) . '" method="' . $method . '" class="' . self::VALIDATE . '" accept-charset="' . $charset . '" enctype="multipart/form-data" onsubmit="return confirm(' . "'" . $confirm . "'" . ')" >');
else
- append($this->form, '<form action="' . route($route) . '" method="' . $method . '" accept-charset="' . $charset . '" class="' . $class . ' ' . self::VALIDATE . '" enctype="multipart/form-data" onsubmit="return confirm(' . "'" . $confirm . "'" . ')">');
+ append($this->form, '<form action="' . route($route,$admin,$args) . '" method="' . $method . '" accept-charset="' . $charset . '" class="' . $class . ' ' . self::VALIDATE . '" enctype="multipart/form-data" onsubmit="return confirm(' . "'" . $confirm . "'" . ')">');
}
else
{
if(not_def($class))
- append($this->form, '<form action="' . route($route) . '" method="' . $method . '" class="' . self::VALIDATE . '" accept-charset="' . $charset . '" onsubmit="return confirm(' . "'" . $confirm . "'" . ')">');
+ append($this->form, '<form action="' . route($route,$admin,$args) . '" method="' . $method . '" class="' . self::VALIDATE . '" accept-charset="' . $charset . '" onsubmit="return confirm(' . "'" . $confirm . "'" . ')">');
else
- append($this->form, '<form action="' . route($route) . '" method="' . $method . '" accept-charset="' . $charset . '" class="' . $class . ' ' . self::VALIDATE . '" onsubmit="return confirm(' . "'" . $confirm . "'" . ')" >');
+ append($this->form, '<form action="' . route($route,$admin,$args) . '" method="' . $method . '" accept-charset="' . $charset . '" class="' . $class . ' ' . self::VALIDATE . '" onsubmit="return confirm(' . "'" . $confirm . "'" . ')" >');
}
}
else
@@ -606,16 +608,16 @@
if($enctype)
{
if(not_def($class))
- append($this->form, '<form action="' . route($route) . '" method="' . $method . '" accept-charset="' . $charset . '" enctype="multipart/form-data">');
+ append($this->form, '<form action="' . route($route,$admin,$args) . '" method="' . $method . '" accept-charset="' . $charset . '" enctype="multipart/form-data">');
else
- append($this->form, '<form action="' . route($route) . '" method="' . $method . '" accept-charset="' . $charset . '" class="' . $class . '" enctype="multipart/form-data">');
+ append($this->form, '<form action="' . route($route,$admin,$args) . '" method="' . $method . '" accept-charset="' . $charset . '" class="' . $class . '" enctype="multipart/form-data">');
}
else
{
if(not_def($class))
- append($this->form, '<form action="' . route($route) . '" method="' . $method . '" accept-charset="' . $charset . '" >');
+ append($this->form, '<form action="' . route($route,$admin,$args) . '" method="' . $method . '" accept-charset="' . $charset . '" >');
else
- append($this->form, '<form action="' . route($route) . '" method="' . $method . '" accept-charset="' . $charset . '" class="' . $class . '" >');
+ append($this->form, '<form action="' . route($route,$admin,$args) . '" method="' . $method . '" accept-charset="' . $charset . '" class="' . $class . '" >');
}
}
if(config($this->file, 'large'))
@@ -1323,7 +1325,7 @@
append($this->form, '<div class="' . self::AUTO_COL . '"><div class="' . $this->separator() . '">');
append($this->form, '<select class="' . self::CUSTOM_SELECT_CLASS . ' ' . $this->get_input_class() . '" name="' . $name . '" onChange="location = this.options[this.selectedIndex].value">');
foreach($options as $k => $option)
- append($this->form, '<option value="' . $k . '"> ' . $option . '</option>');
+ is_integer($k) ? append($this->form,'<option value="' . $option . '"> ' . $option . '</option>'):append($this->form, '<option value="' . $k . '"> ' . $option . '</option>');
if(def($icon))
append($this->form, '</select></div></div></div>');
else
@@ -1371,6 +1373,8 @@
{
$instance = app()->table()->column()->for($table);
+ $types = app()->table()->column()->for($table)->types();
+ $columns = app()->table()->column()->for($table)->show();
$primary = $instance->primary_key();
@@ -1467,63 +1471,48 @@
$numeric = App::NUMERIC_TYPES;
$date = App::DATE_TYPES;
$text = App::TEXT_TYPES;
- $all_num = collect($numeric)->sum();
- $all_date = collect($date)->sum();
- $all_text = collect($text)->sum();
$this->row();
- foreach($text as $k => $t)
- {
- if(is_pair($all_text))
- {
- if(is_pair($all_text))
- $this->textarea($t, $t);
- else
- $this->textarea($t, $t)->end_row_and_new();
- }
- else
- {
- if(equal($k % 3, 0))
- $this->textarea($t, $t)->end_row_and_new();
- else
- $this->textarea($t, $t);
- }
- }
- $this->end_row_and_new();
- foreach($numeric as $k => $n)
- {
- if(different($n, $primary))
- {
- if(is_pair($all_num))
- {
- if(is_pair($all_num))
- $this->input(Form::NUMBER, $n, $n);
- else
- $this->input(Form::NUMBER, $n, $n)->end_row_and_new();
- }
- else
- {
- $this->input(Form::NUMBER, $n, $n);
- }
- }
- }
- $this->end_row_and_new();
- foreach($date as $k => $d)
+ foreach($types as $k => $t)
{
- if(is_pair($all_date))
- {
- if(is_pair($all_date))
- $this->input(Form::DATE, $d, $d, '', '', '', $current);
- else
- $this->input(Form::DATE, $d, $d, '', '', '', $current)->end_row_and_new();
- }
- else
- {
- if(equal($k % 3, 0))
- $this->input(Form::DATE, $d, $d, '', '', '', $current)->end_row_and_new();
- else
- $this->input(Form::DATE, $d, $d, '', '', '', $current);
- }
- }
+ $x = collect($columns)->get($k);
+ if ($x !== $primary)
+ {
+
+ if (is_pair($k)) {
+
+ if (in_array($t, $text))
+ $this->textarea($x, $x)->end_row_and_new();
+ elseif (in_array($t, $numeric))
+ $this->input(Form::NUMBER, $x, $x)->end_row_and_new();
+ elseif (in_array($t, $date))
+ $this->input(Form::DATE, $x, $x)->end_row_and_new();
+ else
+ $this->textarea($x, $x)->end_row_and_new();
+ } else {
+ if (equal($k % 3, 0)) {
+ if (in_array($t, $text))
+ $this->textarea($x, $x)->end_row_and_new();
+ elseif (in_array($t, $numeric))
+ $this->input(Form::NUMBER, $x, $x)->end_row_and_new();
+ elseif (in_array($t, $date))
+ $this->input(Form::DATE, $x, $x)->end_row_and_new();
+ else
+ $this->textarea($x, $x)->end_row_and_new();
+ } else {
+ if (in_array($t, $text))
+ $this->textarea($x, $x)->end_row_and_new();
+ elseif (in_array($t, $numeric))
+ $this->input(Form::NUMBER, $x, $x)->end_row_and_new();
+ elseif (in_array($t, $date))
+ $this->input(Form::DATE, $x, $x)->end_row_and_new();
+ else
+ $this->textarea($x, $x)->end_row_and_new();
+ }
+
+ }
+ }
+ }
+
$this->end_row_and_new();
$id = app()->connect()->postgresql() ? 'DEFAULT' : 'NULL';
diff --git a/imperium/Query/Query.php b/imperium/Query/Query.php
index 78d893f..b45fdbf 100644
--- a/imperium/Query/Query.php
+++ b/imperium/Query/Query.php
@@ -946,7 +946,7 @@
$pagination = (new Pagination($page, $limit, $this->sum()))->paginate();
- return collect($this->take($limit, (($page) - 1) * $limit)->all())->each($callable)->join('') . ' ' . $pagination;
+ return collect($this->take($limit, (($page) - 1) * $limit)->by('id')->all())->each($callable)->join('') . ' ' . $pagination;
}
/**
diff --git a/imperium/Tables/Column.php b/imperium/Tables/Column.php
index 7101077..7f90f26 100644
--- a/imperium/Tables/Column.php
+++ b/imperium/Tables/Column.php
@@ -1009,21 +1009,7 @@
return true;
}
- /**
- *
- * Return the column type
- *
- * @param string $column
- *
- * @throws Kedavra
- *
- * @return mixed
- *
- */
- public function column_type(string $column)
- {
- return collect($this->show())->search($column)->set_new_data($this->types())->result();
- }
+
/**
*
@@ -1042,7 +1028,7 @@
foreach ($this->show() as $k => $v)
{
- $data->put($$v, $this->column_type($v));
+ $data->put($v, collect($this->types())->get($k) );
}
return $data->all();
}
diff --git a/tests/Model/CrudTest.php b/tests/Model/CrudTest.php
index 3bd7050..5529d2d 100644
--- a/tests/Model/CrudTest.php
+++ b/tests/Model/CrudTest.php
@@ -56,4 +56,14 @@ class CrudTest extends Unit
{
$this->assertTrue($this->crud->create('users')->isOk());
}
+
+ public function test_edit()
+ {
+ $this->assertTrue($this->crud->edit('users',30)->isOk());
+ }
+ public function test_refresh()
+ {
+ $this->expectException(Kedavra::class);
+ $this->crud->refresh('users',30);
+ }
} \ No newline at end of file