aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilly Micieli <micieli@laposte.net>2019-10-22 18:59:13 +0200
committerWilly Micieli <micieli@laposte.net>2019-10-22 18:59:13 +0200
commit51b53f7f6316c86efa9001111dc1464cfc81bb1f (patch)
tree263b3b7ba44d6f661df68d0e4cdf8bcee8c245a1
parenteee1cc553f9d316ea995b2a0653ba8e07e9b9017 (diff)
downloadimperium-51b53f7f6316c86efa9001111dc1464cfc81bb1f.zip
imperium-51b53f7f6316c86efa9001111dc1464cfc81bb1f.tar.gz
add new functionalities
-rw-r--r--config/crud.yaml2
-rw-r--r--helpers/admin.php13
-rw-r--r--helpers/web.php23
-rw-r--r--imperium/Action/Crud.php111
-rw-r--r--imperium/Html/Form/Form.php53
-rw-r--r--imperium/Html/Table/Table.php26
-rw-r--r--imperium/View/View.php4
-rw-r--r--tests/Model/CrudTest.php59
-rw-r--r--tests/Model/ModelTest.php4
9 files changed, 227 insertions, 68 deletions
diff --git a/config/crud.yaml b/config/crud.yaml
index d2199d3..f82fc30 100644
--- a/config/crud.yaml
+++ b/config/crud.yaml
@@ -4,3 +4,5 @@ updated: 'The record has been updated successfully'
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
diff --git a/helpers/admin.php b/helpers/admin.php
index e4e8fab..cc57310 100644
--- a/helpers/admin.php
+++ b/helpers/admin.php
@@ -610,6 +610,19 @@
return request()->server->get($key,$value);
}
}
+
+ if (!function_exists('obj'))
+ {
+ function obj($obj): array
+ {
+ $x = collect();
+ foreach ($obj as $k => $v)
+ {
+ $x->put($k,$v);
+ }
+ return $x->all();
+ }
+ }
if( ! function_exists('post'))
{
/**
diff --git a/helpers/web.php b/helpers/web.php
index 5197fb5..25e0ec7 100644
--- a/helpers/web.php
+++ b/helpers/web.php
@@ -358,19 +358,22 @@
* @method route
*
* @param mixed $name
- * @param bool $admin
* @param mixed $args
*
* @return string
*
+ * @throws DependencyException
* @throws Kedavra
+ * @throws NotFoundException
*/
- function route(string $name,bool $admin = false,array $args = []): string
+ function route(string $name,array $args = []): string
{
-
- $x = $admin ? Admin::where('name', EQUAL, $name)->fetch(true)->all() : Web::where('name', EQUAL, $name)->fetch(true)->all();
- is_true(not_def($x),true,"The $name route name was not found");
+ $admin = Admin::where('name', EQUAL, $name)->fetch(true)->all();
+ $web = Web::where('name', EQUAL, $name)->fetch(true)->all();
+
+ $x = def($web) ? $web : $admin;
+
if (def($args))
{
@@ -410,6 +413,16 @@
}
}
+
+ if (!function_exists('detect_method'))
+ {
+ function detect_method(string $route)
+ {
+ $web = Web::where('name',EQUAL,$route)->fetch(true)->all();
+ $admin = Admin::where('name',EQUAL,$route)->fetch(true)->all();
+ return def($web) ? $web->method : $admin->method;
+ }
+ }
if (!function_exists('url'))
{
diff --git a/imperium/Action/Crud.php b/imperium/Action/Crud.php
index 0d279b9..980d8b8 100644
--- a/imperium/Action/Crud.php
+++ b/imperium/Action/Crud.php
@@ -7,6 +7,8 @@
use DI\NotFoundException;
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;
@@ -86,15 +88,6 @@
/**
*
- * The table name
- *
- * @var string
- *
- */
- protected static $table = '';
-
- /**
- *
* The pagination limit
*
* @var int
@@ -107,29 +100,27 @@
*
* Create a new record
*
- * @param array $values
+ * @param string $table
*
* @return RedirectResponse
*
* @throws DependencyException
- * @throws NotFoundException
* @throws Kedavra
+ * @throws NotFoundException
*
*/
- public function create(array $values): RedirectResponse
+ public function add(string $table): RedirectResponse
{
$this->init();
- $table = static::$table;
-
$sql = $this->sql($table);
$columns = $sql->columns();
$x = collect($columns)->join();
- $values = collect($values)->for('htmlentities')->all();
+ $values = collect($this->request()->request->all())->del(CSRF_TOKEN)->for('htmlentities')->all();
$id = $sql->key();
@@ -150,7 +141,7 @@
*
* Read all record with a pagination
*
- * @param callable $callable
+ * @param string $table
* @param int $current_page
*
* @return Response
@@ -163,20 +154,65 @@
* @throws SyntaxError
*
*/
- public function read($callable,int $current_page): Response
+ public function show(string $table,int $current_page): Response
{
- $all = $this->sql(static::$table)->paginate($callable,$current_page,static::$limit);
+ $all = $this->sql($table)->paginate([$this,'home'],$current_page,static::$limit);
- return $this->view('@crud/read',compact('all'));
+ return $this->view('@crud/show',compact('all'));
}
+ /**
+ *
+ * Generate a form to update a record
+ *
+ * @param string $table
+ * @param int $id
+ *
+ * @return Response
+ *
+ * @throws DependencyException
+ * @throws Kedavra
+ * @throws LoaderError
+ * @throws NotFoundException
+ * @throws RuntimeError
+ * @throws SyntaxError
+ *
+ */
+ 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'));
+ }
+
+ /**
+ *
+ * Create a form to add content
+ *
+ * @param string $table
+ *
+ * @return Response
+ *
+ * @throws DependencyException
+ * @throws Kedavra
+ * @throws LoaderError
+ * @throws NotFoundException
+ * @throws RuntimeError
+ * @throws SyntaxError
+ *
+ */
+ public function create(string $table): Response
+ {
+ $form = $this->form()->start('create')->generate($table,$this->config('crud','create_text'));
+
+ return $this->view('@crud/create',compact('form'));
+ }
/**
*
* Update a record
*
+ * @param string $table
* @param int $id
- * @param array $values
*
* @return RedirectResponse
*
@@ -185,19 +221,19 @@
* @throws NotFoundException
*
*/
- public function update(int $id,array $values):RedirectResponse
+ public function refresh(string $table,int $id): RedirectResponse
{
$this->init();
- $table = static::$table;
-
$sql = $this->sql($table);
$primary = $sql->key();
$columns = collect();
+ $values = collect($this->request()->request->all())->del(CSRF_TOKEN)->all();
+
foreach ($values as $k => $value)
{
if (different($k,$primary))
@@ -217,6 +253,7 @@
*
* Destroy a record
*
+ * @param string $table
* @param int $id
*
* @return RedirectResponse
@@ -226,11 +263,30 @@
* @throws NotFoundException
*
*/
- public function delete(int $id): RedirectResponse
+ public function destroy(string $table,int $id): RedirectResponse
{
$this->init();
- return $this->sql(static::$table)->destroy($id) ? $this->back($this->deleted) : $this->back($this->no_deleted,false);
+ return $this->sql($table)->destroy($id) ? $this->back($this->deleted) : $this->back($this->no_deleted,false);
+ }
+
+ /**
+ *
+ *
+ * @param $key
+ * @param $value
+ *
+ * @return string
+ */
+ public function home($key, $value): string
+ {
+
+
+ $html = '<td>';
+ foreach ($this->collect(obj($value))->keys()->all() as $key)
+ append($html,"<tr>$key</tr>");
+
+ return $html .'</td>';
}
/**
@@ -255,5 +311,10 @@
$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/Html/Form/Form.php b/imperium/Html/Form/Form.php
index c3b2641..9958307 100644
--- a/imperium/Html/Form/Form.php
+++ b/imperium/Html/Form/Form.php
@@ -2,8 +2,10 @@
namespace Imperium\Html\Form
{
-
- use Imperium\App;
+
+ use DI\DependencyException;
+ use DI\NotFoundException;
+ use Imperium\App;
use Imperium\Exception\Kedavra;
/**
@@ -579,6 +581,7 @@
{
$this->method = detect_method($route);
+
$method = POST;
if($this->validate)
{
@@ -1344,33 +1347,35 @@
return $this;
}
-
- /**
- *
- * Generate a form to edit or create a record
- *
- *
- * @method generate
- *
- * @param int $form_grid The number to modify the form generation output
- * @param string $table The current table
- * @param string $submit_text The submit button text
- * @param string $submit_icon The submit icon
- * @param int $mode Define the mode edit or create
- * @param int $id The record id
- *
- * @throws Kedavra
- *
- * @return string
- *
- */
- public function generate(int $form_grid, string $table, string $submit_text, string $submit_icon = '', int $mode = Form::CREATE, int $id = 0) : string
+
+ /**
+ *
+ * Generate a form to edit or create a record
+ *
+ *
+ * @method generate
+ *
+ * @param string $table The current table
+ * @param string $submit_text The submit button text
+ * @param string $submit_icon The submit icon
+ * @param int $mode Define the mode edit or create
+ * @param int $id The record id
+ *
+ * @return string
+ * @throws DependencyException
+ * @throws Kedavra
+ * @throws NotFoundException
+ *
+ */
+ public function generate(string $table, string $submit_text, string $submit_icon = '', int $mode = Form::CREATE, int $id = 0) : string
{
$instance = app()->table()->column()->for($table);
+
$primary = $instance->primary_key();
- equal($form_grid, 0, true, "Zero is not a valid number");
+
not_in([ Form::EDIT, Form::CREATE ], $mode, true, "The mode used is not a valid mode");
+
if(equal($mode, Form::EDIT))
{
$data = app()->table()->from($table)->select_or_fail($id);
diff --git a/imperium/Html/Table/Table.php b/imperium/Html/Table/Table.php
index 5c75806..f632548 100644
--- a/imperium/Html/Table/Table.php
+++ b/imperium/Html/Table/Table.php
@@ -4,7 +4,9 @@
{
use Carbon\Carbon;
- use Imperium\Collection\Collect;
+ use DI\DependencyException;
+ use DI\NotFoundException;
+ use Imperium\Collection\Collect;
use Imperium\Exception\Kedavra;
/**
@@ -343,16 +345,18 @@
return $this;
}
-
- /**
- *
- * Generate the table
- *
- * @param string $class
- *
- * @throws Kedavra
- * @return string
- */
+
+ /**
+ *
+ * Generate the table
+ *
+ * @param string $class
+ *
+ * @return string
+ * @throws Kedavra
+ * @throws DependencyException
+ * @throws NotFoundException
+ */
public function generate(string $class = '') : string
{
diff --git a/imperium/View/View.php b/imperium/View/View.php
index ad94a8b..434cba4 100644
--- a/imperium/View/View.php
+++ b/imperium/View/View.php
@@ -99,7 +99,9 @@
$this->loader()->addPath($views_path . DIRECTORY_SEPARATOR . 'Admin', 'admin');
$this->loader()->addPath($views_path . DIRECTORY_SEPARATOR . 'Users', 'users');
-
+
+ $this->loader()->addPath($views_path . DIRECTORY_SEPARATOR . 'Crud', 'crud');
+
if(def($this->namespaces))
{
foreach($this->namespaces as $k => $v)
diff --git a/tests/Model/CrudTest.php b/tests/Model/CrudTest.php
new file mode 100644
index 0000000..3bd7050
--- /dev/null
+++ b/tests/Model/CrudTest.php
@@ -0,0 +1,59 @@
+<?php
+
+namespace Testing\Model;
+
+use App\Controllers\AdminController;
+use DI\DependencyException;
+use DI\NotFoundException;
+use Imperium\Dump\Dump;
+use Imperium\Exception\Kedavra;
+use Imperium\Import\Import;
+use Imperium\Testing\Unit;
+use App\Models\Users;
+use stdClass;
+
+class CrudTest extends Unit
+{
+
+
+ /**
+ * @var AdminController
+ */
+ private $crud;
+
+ public function setUp(): void
+ {
+ $this->crud = new AdminController();
+ }
+
+ /**
+ * @throws Kedavra
+ */
+ public function test_show()
+ {
+ $this->assertTrue($this->crud->show('users',1)->isOk());
+
+ }
+
+/**
+ * @throws Kedavra
+ */
+ public function test_delete()
+ {
+ $this->assertTrue($this->crud->destroy('users',20)->isRedirect('/'));
+
+ }
+
+ /**
+ * @throws Kedavra
+ */
+ public function test_create()
+ {
+ $this->assertTrue($this->crud->add('users')->isRedirect('/'));
+
+ }
+ public function test_generate_form()
+ {
+ $this->assertTrue($this->crud->create('users')->isOk());
+ }
+} \ No newline at end of file
diff --git a/tests/Model/ModelTest.php b/tests/Model/ModelTest.php
index bb6c723..ea79efb 100644
--- a/tests/Model/ModelTest.php
+++ b/tests/Model/ModelTest.php
@@ -20,7 +20,7 @@ class ModelTest extends Unit
{
$this->assertNotEmpty(Users::all());
$this->assertNotEmpty(Users::paginate([$this,'records'],1));
- $this->assertEquals(99,Users::count());
+ $this->assertEquals(100,Users::count());
$this->assertNotEmpty(Users::only('id'));
$this->assertInstanceOf(stdClass::class,Users::get(1));
$this->assertNotEmpty(Users::search('a'));
@@ -32,7 +32,7 @@ class ModelTest extends Unit
$this->assertContains('password',$columns);
$this->assertTrue(Users::destroy(1));
- $this->assertEquals(98,Users::count());
+ $this->assertEquals(99,Users::count());
$this->assertEquals(10,Users::different(5)->take(10,2)->sum());
$this->assertEquals(48,Users::where('id',INFERIOR_OR_EQUAL,50)->sum());