aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilly Micieli <micieli@laposte.net>2019-11-01 19:21:02 +0100
committerWilly Micieli <micieli@laposte.net>2019-11-01 19:21:02 +0100
commit98a14da2fb445b0a8c5064bdbd15f4d0c7e3e23e (patch)
tree94042d0f97436b5b46c00f4875ffd050267f6211
parent1dc65c2bafd1af724daa56762fc2008f9cccc524 (diff)
downloadimperium-98a14da2fb445b0a8c5064bdbd15f4d0c7e3e23e.zip
imperium-98a14da2fb445b0a8c5064bdbd15f4d0c7e3e23e.tar.gz
rewrite form class
-rw-r--r--config/form.yaml18
-rw-r--r--config/pagination.yaml2
-rw-r--r--config/todo.yaml4
-rw-r--r--db/migrations/20191023162136_countries.php1
-rw-r--r--helpers/web.php38
-rw-r--r--imperium/Action/Crud.php29
-rw-r--r--imperium/App.php11
-rw-r--r--imperium/Controller/Task.php29
-rw-r--r--imperium/Html/Form/Form.php1971
-rw-r--r--imperium/Management.php32
-rw-r--r--imperium/Model/Model.php1
-rw-r--r--imperium/Routing/Router.php2
-rw-r--r--imperium/Tables/Table.php6
-rw-r--r--tests/AppTest.php2
-rw-r--r--tests/FormTests.php80
-rw-r--r--tests/Model/CrudTest.php60
16 files changed, 556 insertions, 1730 deletions
diff --git a/config/form.yaml b/config/form.yaml
index 0d6b704..33ef1d2 100644
--- a/config/form.yaml
+++ b/config/form.yaml
@@ -1,5 +1,7 @@
class:
- submit: 'btn-outline-primary text-uppercase'
+ base: 'form-control'
+ separator: 'form-group'
+ submit: 'btn-outline-primary btn mb-5 text-uppercase'
reset: 'btn-danger'
remove: 'btn-danger'
button: 'btn-secondary'
@@ -9,14 +11,6 @@ class:
edit: 'btn btn-primary'
delete : 'btn btn-danger'
table: 'table table-bordered table-hover'
-textarea:
- col: 10
- row: 5
-save: true
-padding: 2
-margin: 2
-validate: false
-large: true
-small: false
-pagination_step: 10
-space: true \ No newline at end of file
+update_text: 'update'
+create_text: 'create'
+choice_option: 'choose a value'
diff --git a/config/pagination.yaml b/config/pagination.yaml
index 9a6d0cd..9ed7542 100644
--- a/config/pagination.yaml
+++ b/config/pagination.yaml
@@ -1,4 +1,4 @@
url: '?page='
link_class: 'page-link'
-ul_class: 'pagination mt-5'
+ul_class: 'd-flex justify-content-end pagination mt-5'
li_class: 'page-item' \ No newline at end of file
diff --git a/config/todo.yaml b/config/todo.yaml
index f013e81..efc8136 100644
--- a/config/todo.yaml
+++ b/config/todo.yaml
@@ -3,4 +3,6 @@ todo_created: 'The task has been created'
todo_updated: 'The task has been updated'
todo_not_updated: 'The task has not been updated'
toto_not_close: 'The task has not been closed'
-toto_closed: 'The task is now closed' \ No newline at end of file
+toto_closed: 'The task is now closed'
+task_name: 'The name of the task'
+task_description: 'Describe the task to to' \ No newline at end of file
diff --git a/db/migrations/20191023162136_countries.php b/db/migrations/20191023162136_countries.php
index d0b1f32..9201157 100644
--- a/db/migrations/20191023162136_countries.php
+++ b/db/migrations/20191023162136_countries.php
@@ -30,6 +30,7 @@ class Countries extends AbstractMigration
{
$this->table('countries')
->addColumn('name','string')
+ ->addTimestamps()
->create();
}
}
diff --git a/helpers/web.php b/helpers/web.php
index f7b4c3d..73efa57 100644
--- a/helpers/web.php
+++ b/helpers/web.php
@@ -373,6 +373,7 @@ use Imperium\Model\Web;
switch ($db)
{
case 'admin':
+
$x = Admin::where('name',EQUAL,$route)->fetch(true)->all();
break;
case 'web':
@@ -386,6 +387,9 @@ use Imperium\Model\Web;
break;
}
+ is_true(not_def($x),true,"The $route route was not found inside $db base");
+
+
if (def($args))
{
@@ -534,7 +538,39 @@ use Imperium\Model\Web;
}
}
-
+ if (!function_exists('redirect_select'))
+ {
+
+ /**
+ *
+ * Generate a redirect select
+ *
+ * @param array $options
+ *
+ * @return string
+ *
+ * @throws Kedavra
+ *
+ */
+ function redirect_select(array $options): string
+ {
+ $html = '';
+
+ append($html, '<div class="' . collect(config('form','class'))->get('separator').'">');
+
+ append($html, '<select class="' . collect(config('form','class'))->get('base').'" onChange="location = this.options[this.selectedIndex].value">');
+
+ append($html,'<option value="'.root().'"> ' . config('form','choice_option') . '</option>');
+
+ foreach($options as $k => $option)
+ is_integer($k) ? append($html,'<option value="' . $option . '"> ' . $option . '</option>'):append($httml, '<option value="' . $k . '"> ' . $option . '</option>');
+
+ append($html, '</select></div></div>');
+
+ return $html;
+ }
+ }
+
if (!function_exists('append'))
{
diff --git a/imperium/Action/Crud.php b/imperium/Action/Crud.php
index ead86dc..e299227 100644
--- a/imperium/Action/Crud.php
+++ b/imperium/Action/Crud.php
@@ -7,7 +7,6 @@
use DI\NotFoundException;
use Imperium\Controller\Controller;
use Imperium\Exception\Kedavra;
- use Imperium\Html\Form\Form;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Twig\Error\LoaderError;
@@ -227,7 +226,7 @@
*/
public function edit(string $table,int $id): Response
{
- $form = $this->form()->start('admin','update',[$table,$id])->generate($table,$this->config('crud','update_text'),'',Form::EDIT,$id);
+ $form = $this->form(POST,'admin','update',$table,$id)->edit($table,$id);
return $this->view('@crud/edit',compact('form','table'));
}
@@ -249,7 +248,7 @@
*/
public function create(string $table): Response
{
- $form = $this->form()->start('admin','create',[$table])->generate($table,$this->config('crud','create_text'));
+ $form = $this->form(POST,'admin','create',$table)->generate($table);
return $this->view('@crud/create',compact('form','table'));
}
@@ -271,7 +270,6 @@
public function refresh(string $table,int $id): RedirectResponse
{
-
$this->init();
$sql = $this->sql($table);
@@ -280,7 +278,7 @@
$columns = collect();
- $values = collect($this->request()->request->all())->del(CSRF_TOKEN,'method','__table__')->for('htmlentities')->all();
+ $values = collect($this->request()->request->all())->del(CSRF_TOKEN,'_method')->for('htmlentities')->all();
foreach ($values as $k => $value)
{
@@ -320,32 +318,34 @@
/**
*
+ * Crud home
*
* @return Response
- * @throws DependencyException
+ *
* @throws Kedavra
* @throws LoaderError
- * @throws NotFoundException
* @throws RuntimeError
* @throws SyntaxError
*/
- public function home():Response
+ public function home(): Response
{
- $x = collect(['/' => $this->config('crud','select_table_text')]);
- foreach ($this->tables() as $table)
- $x->put(route('admin','show',[$table,1]),$table);
- $tables = $x->all();
- $form = $this->form()->redirect('table',$tables)->get();
- return $this->view('@crud/home',compact('tables','form'));
+ $form =redirect_select($this->tables());
+ return $this->view('@crud/home',compact('form'));
}
/**
+ *
+ * Generate a row in the table
+ *
* @param $key
* @param $value
+ *
* @return string
+ *
* @throws DependencyException
* @throws Kedavra
* @throws NotFoundException
+ *
*/
public function display($key,$value): string
{
@@ -360,6 +360,7 @@
append($html,'<td><a href="'.route('admin','remove',[$this->current,$value->id]).'" class="'.$this->config('crud','remove_class').'" '.$sure.'> '.$this->config('crud','remove_text').'</a></td>');
return $html . '</tr>';
}
+
/**
*
* @throws Kedavra
diff --git a/imperium/App.php b/imperium/App.php
index b1ec985..1a74dde 100644
--- a/imperium/App.php
+++ b/imperium/App.php
@@ -200,11 +200,18 @@ namespace Imperium {
}
/**
+ * @param string $method
+ * @param string $db
+ * @param string $route
+ * @param array $route_args
* @return Form
+ * @throws DependencyException
+ * @throws Kedavra
+ * @throws NotFoundException
*/
- public function form(): Form
+ public function form(string $method,string $db, string $route,...$route_args): Form
{
- return new Form();
+ return new Form($method,$db,$route,$route_args);
}
/**
diff --git a/imperium/Controller/Task.php b/imperium/Controller/Task.php
index 7a9d048..cde35d1 100644
--- a/imperium/Controller/Task.php
+++ b/imperium/Controller/Task.php
@@ -47,7 +47,7 @@ namespace Imperium\Controller {
*/
public function update(): RedirectResponse
{
- return Todo::update($this->request()->request->get('id'),$this->collect($this->request()->request->all())->del(CSRF_TOKEN,'method')->all()) ? $this->back($this->config('todo','todo_updated')) : $this->back($this->config('todo','todo_not_updated'),false);
+ return Todo::update($this->request()->request->get('id'),$this->collect($this->request()->request->all())->del(CSRF_TOKEN,'_method')->all()) ? $this->back($this->config('todo','todo_updated')) : $this->back($this->config('todo','todo_not_updated'),false);
}
/**
@@ -84,19 +84,22 @@ namespace Imperium\Controller {
public function home(): Response
{
$todo = Todo::query()->by('due',self::ASC)->all();
- $form = $this->form()->start('task','add')
+ $form = $this->form(POST,'task','add')
->row()
- ->input(Form::TEXT,'task','task')
- ->end_row_and_new()
- ->textarea('description','description')
- ->end_row_and_new()
- ->input(Form::DATE,'due','The due date')
- ->end_row_and_new()
- ->select(false,'priority',['none','low','medium','high'])
- ->end_row_and_new()
- ->submit(fa('fa','fa-plus'))
- ->end_row()
- ->get();
+ ->add('task','text',['placeholder'=> $this->config('todo','task_name'),'required' => 'required'])
+ ->end()
+ ->row()
+ ->add('description','textarea',['required'=> 'required','cols'=> 10,'placeholder'=> $this->config('todo','task_description')])
+ ->end()
+ ->row()
+ ->add('due','date',['required'=>'required'])
+ ->end()
+ ->row()
+ ->select('priority',['none','low','medium','high'])
+ ->end()
+ ->get(fa('fa','fa-plus'));
+
+
return $this->view('@todo/home',compact('todo','form'));
}
}
diff --git a/imperium/Html/Form/Form.php b/imperium/Html/Form/Form.php
index c09c5e6..dbf1149 100644
--- a/imperium/Html/Form/Form.php
+++ b/imperium/Html/Form/Form.php
@@ -1,1667 +1,330 @@
<?php
-
- namespace Imperium\Html\Form
- {
- use DI\DependencyException;
- use DI\NotFoundException;
- use Imperium\App;
- use Imperium\Exception\Kedavra;
-
- /**
- * Class Form
- *
- * @author Willy Micieli
- *
- * @package Imperium\Html\Form
- *
- * @license GPL
- *
- * @version 10
- *
- */
- class Form
- {
-
- /**
- *
- * The class name to represent a new row
- *
- * @var string
- *
- */
- const GRID_ROW = 'row';
-
- /**
- *
- * The class name to generate auto spaced columns
- *
- * @var string
- *
- */
- const AUTO_COL = 'col';
-
- /**
- *
- * The basic form class
- *
- * @var string
- *
- */
- const BASIC_CLASS = 'form-control';
-
- /**
- *
- * The basic large class
- *
- * @var string
- *
- */
- const LARGE_CLASS = 'form-control-lg';
-
- /**
- *
- * The basic small class
- *
- * @var string
- *
- */
- const SMALL_CLASS = 'form-control-sm';
-
- /**
- *
- * The basic form separator class
- *
- * @var string
- *
- */
- const FORM_SEPARATOR = 'form-group';
-
- /**
- *
- * The custom select class
- *
- * @var string
- *
- */
- const CUSTOM_SELECT_CLASS = 'form-control';
-
- /**
- *
- * The class to hide elements
- *
- * @var string
- *
- */
- const HIDE_CLASS = 'd-none';
-
- /**
- *
- * To create a reset button
- *
- * @var string
- *
- */
- const RESET = 'reset';
-
- /**
- *
- * To create a submit button
- *
- * @var string
- *
- */
- const SUBMIT = 'submit';
-
- /**
- *
- * To create a form with post method
- *
- * @var string
- *
- */
- const POST = 'post';
-
- /**
- *
- * To create a form with get method
- *
- * @var string
- *
- */
- const GET = 'get';
-
- /**
- *
- * To create a button
- *
- * @var string
- *
- */
- const BUTTON = 'button';
-
- /**
- *
- * To create a number input
- *
- * @var string
- *
- */
- const NUMBER = 'number';
-
- /**
- *
- * To create a hidden input
- *
- * @var string
- *
- */
- const HIDDEN = 'hidden';
-
- /**
- *
- * To create a text input
- *
- * @var string
- *
- */
- const TEXT = 'text';
-
- /**
- *
- * To create a password input
- *
- * @var string
- *
- */
- const PASSWORD = 'password';
-
- /**
- *
- * To create a email input
- *
- * @var string
- *
- */
- const EMAIL = 'email';
-
- /**
- *
- * To create a date input
- *
- * @var string
- *
- */
- const DATE = 'date';
-
- /**
- *
- * To create a datetime input
- *
- * @var string
- *
- */
- const DATETIME = 'datetime';
-
- /**
- *
- * To create a phone input
- *
- * @var string
- *
- */
- const TEL = 'tel';
-
- /**
- *
- * To create a url input
- *
- * @var string
- *
- */
- const URL = 'url';
-
- /**
- *
- * To create a time input
- *
- * @var string
- *
- */
- const TIME = 'time';
-
- /**
- *
- * To create a range input
- *
- * @var string
- *
- */
- const RANGE = 'range';
-
- /**
- *
- * To create a color input
- *
- * @var string
- *
- */
- const COLOR = 'color';
-
- /**
- *
- * To create a search input
- *
- * @var string
- *
- */
- const SEARCH = 'search';
-
- /**
- *
- * To create a week input
- *
- * @var string
- *
- */
- const WEEK = 'week';
-
- /**
- *
- * To create a checkbox input
- *
- * @var string
- *
- */
- const CHECKBOX = 'checkbox';
-
- /**
- *
- * To create a radio input
- *
- * @var string
- *
- */
- const RADIO = 'radio';
-
- /**
- *
- * To create a file input
- *
- * @var string
- *
- */
- const FILE = 'file';
-
- /**
- *
- * To create a datetime local input
- *
- * @var string
- *
- */
- const DATETIME_LOCAL = 'datetime-local';
-
- /**
- *
- * To create an image input
- *
- * @var string
- *
- */
- const IMAGE = 'image';
-
- /**
- *
- * To create a month input
- *
- * @var string
- *
- */
- const MONTH = 'month';
-
- /**
- *
- * Option to get the datetime result
- *
- * @var int
- *
- */
- const GET_TIME = 0;
-
- /**
- *
- * Option to get the datetime input
- *
- * @var int
- *
- */
- const GET_DATETIME = 1;
-
- /**
- *
- * Option to generate a form
- * to edit a record
- *
- * @var int
- *
- */
- const EDIT = 3;
-
- /**
- *
- * Option to generate a form to create a record
- *
- * @var int
- *
- */
- const CREATE = 4;
-
- /**
- *
- * Basic button class
- *
- * @var string
- *
- */
- const BTN_BASIC_CLASS = 'btn';
-
- /**
- *
- * Button large class
- *
- * @var string
- *
- */
- const BTN_LARGE_CLASS = 'btn-lg';
-
- /**
- *
- * Button small class
- *
- * @var string
- *
- */
- const BTN_SMALL_CLASS = 'btn-sm';
-
- /**
- *
- * The class to see validation
- *
- * @var string
- *
- */
- const VALIDATE = 'was-validated';
-
- /**
- *
- * The form
- *
- * @var string
- *
- */
- private $form;
-
- /**
- *
- * Option to activate validation
- *
- * @var bool
- *
- */
- private $validate = false;
-
- /**
- *
- * Option to save data after submit
- *
- * @var bool
- *
- */
- private $save = false;
-
- /**
- *
- * The button size class
- *
- * @var string
- *
- */
- private $btn_size;
-
- /**
- *
- * The input size class
- *
- * @var string
- *
- */
- private $input_size;
-
- /**
- *
- * The margin class between elements
- *
- * @var string
- *
- */
- private $margin = '';
-
- /**
- *
- * The padding class between elements
- *
- * @var string
- *
- */
- private $padding = '';
-
- /**
- *
- * The form method
- *
- * @var string
- *
- */
- private $method;
-
- /**
- *
- * Form config
- *
- * @var string
- */
- private $file = 'form';
-
- /**
- *
- * Define the padding between elements
- *
- * @method padding
- *
- *
- * @throws Kedavra
- * @return Form
- *
- */
- public function padding() : Form
- {
-
- $length = config($this->file, 'padding');
- not_in([ 1, 2, 3, 4, 5 ], $length, true, "The padding number must be an integer between 1 and 5");
- $this->padding = "pt-$length pb-$length";
-
- return $this;
- }
-
- /**
- *
- * Define the margin between elements
- *
- * @method margin
- *
- * @throws Kedavra
- *
- * @return Form
- *
- */
- public function margin() : Form
- {
-
- $length = config($this->file, 'margin');
- not_in([ 1, 2, 3, 4, 5 ], $length, true, "The margin number must be an integer between 1 and 5");
- $this->margin = "mt-$length mb-$length";
-
- return $this;
- }
-
- /**
- *
- * Return the defined margin class
- *
- * @method get_margin
- *
- * @return string
- *
- */
- public function get_margin() : string
- {
-
- return def($this->margin) ? $this->margin : '';
- }
-
- /**
- *
- * Return the defined padding class
- *
- * @method get_padding
- *
- * @return string
- *
- */
- public function get_padding() : string
- {
-
- return def($this->padding) ? $this->padding : '';
- }
-
- /**
- *
- * Return the complete separator class
- *
- * @method separator
- *
- * @return string
- *
- */
- private function separator() : string
- {
-
- return self::FORM_SEPARATOR;
- }
+namespace Imperium\Html\Form {
- /**
- *
- * Open the form
- *
- * @method start
- *
- * @param string $db
- * @param string $route
- * @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 $db,string $route,array $args= [],string $confirm = '', string $class = '', bool $enctype = false, string $charset = 'utf-8') : Form
- {
-
- $this->method = detect_method($db,$route);
+ use DateTime;
+ use DI\DependencyException;
+ use DI\NotFoundException;
+ use Imperium\App;
+ use Imperium\Collection\Collect;
+ use Imperium\Exception\Kedavra;
+ use function DI\value;
+ class Form
+ {
- $method = POST;
- if($this->validate)
- {
- equal($confirm, '', true, "The confirm message must not be empty");
- if($enctype)
- {
- if(not_def($class))
- append($this->form, '<form action="' . route($db,$route,$args) . '" method="' . $method . '" class="' . self::VALIDATE . '" accept-charset="' . $charset . '" enctype="multipart/form-data" onsubmit="return confirm(' . "'" . $confirm . "'" . ')" >');
- else
- append($this->form, '<form action="' . route($db,$route,$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($db,$route,$args) . '" method="' . $method . '" class="' . self::VALIDATE . '" accept-charset="' . $charset . '" onsubmit="return confirm(' . "'" . $confirm . "'" . ')">');
- else
- append($this->form, '<form action="' . route($db,$route,$args) . '" method="' . $method . '" accept-charset="' . $charset . '" class="' . $class . ' ' . self::VALIDATE . '" onsubmit="return confirm(' . "'" . $confirm . "'" . ')" >');
- }
- }
- else
- {
- if($enctype)
- {
- if(not_def($class))
- append($this->form, '<form action="' . route($db,$route,$args) . '" method="' . $method . '" accept-charset="' . $charset . '" enctype="multipart/form-data">');
- else
- append($this->form, '<form action="' . route($db,$route,$args) . '" method="' . $method . '" accept-charset="' . $charset . '" class="' . $class . '" enctype="multipart/form-data">');
- }
- else
- {
- if(not_def($class))
- append($this->form, '<form action="' . route($db,$route,$args) . '" method="' . $method . '" accept-charset="' . $charset . '" >');
- else
- append($this->form, '<form action="' . route($db,$route,$args) . '" method="' . $method . '" accept-charset="' . $charset . '" class="' . $class . '" >');
- }
- }
- if(config($this->file, 'large'))
- $this->large();
- if(config($this->file, 'small'))
- $this->small();
- if(config($this->file, 'save'))
- $this->save();
- append($this->form, '<input type="hidden" value="' . $this->method . '" name="method">');
-
- return $this->csrf(csrf_field());
- }
-
- /**
- *
- * Open a div to hide contents
- *
- * @method hide
- *
- * @return Form
- *
- */
- public function hide() : Form
- {
-
- append($this->form, '<div class="' . self::HIDE_CLASS . '">');
-
- return $this;
- }
-
- /**
- *
- * Add js code
- *
- * @param string $code
- *
- * @return Form
- *
- */
- public function js(string $code) : Form
- {
-
- append($this->form, $code);
-
- return $this;
- }
-
- /**
- *
- * Close the div created to hide contents
- *
- * @method end_hide
- *
- * @return Form
- *
- */
- public function end_hide() : Form
- {
-
- append($this->form, '</div>');
-
- return $this;
- }
-
- /**
- *
- * Get the input size
- *
- * @method get_input_class
- *
- * @return string [description]
- */
- private function get_input_class() : string
- {
-
- return def($this->input_size) ? $this->input_size : self::BASIC_CLASS;
- }
-
- /**
- *
- * Get input complete input size
- *
- * @method get_input_complete_class
- *
- * @throws Kedavra
- *
- * @return string
- *
- */
- private function get_input_complete_class() : string
- {
-
- $x = $this->get_input_class();
-
- return different($x, self::BASIC_CLASS) ? $x . ' ' . self::BASIC_CLASS : $x;
- }
-
- /**
- *
- * Get the complete button class
- *
- * @method get_btn_class
- *
- * @return string
- *
- */
- private function get_btn_class() : string
- {
-
- return def($this->btn_size) ? self::BTN_BASIC_CLASS . ' ' . $this->btn_size : self::BTN_BASIC_CLASS;
- }
-
- /**
- *
- * Generate a file input
- *
- * @method file
- *
- * @param string $name The name attribute
- * @param string $text The text to explain
- * @param string $ico The input icon
- * @param string $locale The locale to use
- *
- * @return Form
- *
- */
- public function file(string $name, string $text, string $ico = '', string $locale = 'en') : Form
- {
-
- if(not_def($ico))
- append($this->form, '<div class="' . self::AUTO_COL . '"><div class="' . $this->separator() . '"><div class="custom-file"><input type="file" name="' . $name . '" class="custom-file-input ' . $this->get_input_class() . '" lang="' . $locale . '"><label class="custom-file-label" for="' . $name . '">' . $text . '</label></div></div></div>');
- else
- append($this->form, '<div class="' . self::AUTO_COL . '"><div class="' . $this->separator() . '"><div class="input-group"><div class="input-group-prepend"><span class="input-group-text" >' . $ico . '</span></div><div class="custom-file"><input type="file" name="' . $name . '" class="custom-file-input ' . $this->get_input_class() . ' " lang="' . $locale . '"><label class="custom-file-label" for="' . $name . '">' . $text . '</label></div></div></div></div>');
-
- return $this;
- }
-
- /**
- * @param string $start
- * @param string $end
- * @param string $input
- * @param string $name
- * @param string $placeholder
- * @param string $value
- * @param bool $required
- * @param bool $autofocus
- * @param bool $autoComplete
- * @param string $success_text
- * @param string $error_text
- *
- * @throws Kedavra
- * @return string
- *
- */
- private function generateInput(string $start, string $end, string $input, string $name, string $placeholder, string $value, bool $required, bool $autofocus, bool $autoComplete, string $success_text = '', string $error_text = '')
- {
-
- $class = equal($input, Form::FILE) ? $this->get_input_complete_class() . ' form-control-file' : $this->get_input_complete_class();
- if($this->validate)
- {
- if(not_def($error_text, $success_text))
- throw new Kedavra('missing validation text');
- else
- $validation = $this->valid($success_text, $error_text);
- }
- else
- {
- $validation = '';
- }
- if($this->save)
- {
- $val = equal($this->method, self::POST) ? post($name) : get($name);
- $value = def($val) ? $val : $value;
- }
- if($required) // WITH REQUIRED
- {
- if($autofocus)
- {
- if($autoComplete)
- {
- return '' . $start . ' <input type="' . $input . '" class="' . $class . '" required="required" placeholder="' . $placeholder . '" name="' . $name . '" value="' . $value . '" autofocus="autofocus" autocomplete="on" > ' . $validation . $end . '';
- }
-
- return '' . $start . ' <input type="' . $input . '" class="' . $class . '" required="required" placeholder="' . $placeholder . '" name="' . $name . '" value="' . $value . '" autofocus="autofocus" autocomplete="off" > ' . $validation . $end . '';
- }
- if($autoComplete)
- return '' . $start . ' <input type="' . $input . '" class="' . $class . '" required="required" placeholder="' . $placeholder . '" name="' . $name . '" value="' . $value . '" autocomplete="on" > ' . $end . $validation . '';
- else
- return '' . $start . ' <input type="' . $input . '" class="' . $class . '" required="required" placeholder="' . $placeholder . '" name="' . $name . '" value="' . $value . '" autocomplete="off" > ' . $validation . $end . '';
- }
- else
- {
- // WITHOUT REQUIRED
- if($autofocus) // WITH AUTO FOCUS
- {
- if($autoComplete) // AUTO FOCUS , AND AUTO COMPLETE
- {
- return '' . $start . ' <input type="' . $input . '" class="' . $class . '" autofocus="autofocus" autocomplete="on" placeholder="' . $placeholder . '" name="' . $name . '" value="' . $value . '" > ' . $validation . $end . '';
- }
-
- return '' . $start . ' <input type="' . $input . '" class="' . $class . '" autofocus="autofocus" placeholder="' . $placeholder . '" autocomplete="off" name="' . $name . '" value="' . $value . '" > ' . $validation . $end . '';
- }
- else
- { // WITHOUT AUTO FOCUS
- if($autoComplete) // AUTO FOCUS , AND AUTO COMPLETE
- {
- return '' . $start . ' <input type="' . $input . '" class="' . $class . '" autocomplete="on" placeholder="' . $placeholder . '" name="' . $name . '" value="' . $value . '" > ' . $validation . $end . '';
- } // AUTO FOCUS , WITHOUT AUTO COMPLETE
-
- return '' . $start . ' <input type="' . $input . '" class="' . $class . '" autocomplete="off" placeholder="' . $placeholder . '" name="' . $name . '" value="' . $value . '" > ' . $validation . $end . '';
- }
- }
- }
-
- /**
- *
- * Generate an input
- *
- * @method input
- *
- * @param string $type The input type
- * @param string $name The input attr name
- * @param string $placeholder The placeholder
- * @param string $icon The input icon
- * @param string $success_text The success validation text
- * @param string $error_text The error validation text
- * @param string $value The default value
- * @param bool $required To generate a required input
- * @param bool $autofocus To add autofocus for the input
- * @param bool $autoComplete To configure autocomplete
- *
- * @throws Kedavra
- *
- * @return Form
- *
- */
- public function input(string $type, string $name, string $placeholder, string $icon = '', string $success_text = '', string $error_text = '', string $value = '', bool $required = true, bool $autofocus = false, bool $autoComplete = false) : Form
- {
-
- if(not_def($icon))
- {
- $start = '<div class="' . self::AUTO_COL . '"><div class="' . $this->separator() . '">';
- $end = "</div></div>";
- append($this->form, $this->generateInput($start, $end, $type, $name, $placeholder, $value, $required, $autofocus, $autoComplete, $success_text, $error_text));
- }
- else
- {
- $start = '<div class="' . self::AUTO_COL . '"><div class="' . $this->separator() . '"><div class="input-group"><div class="input-group-prepend"><div class="input-group-text">' . $icon . '</div></div> ';
- $end = "</div></div></div> ";
- append($this->form, $this->generateInput($start, $end, $type, $name, $placeholder, $value, $required, $autofocus, $autoComplete, $success_text, $error_text));
- }
-
- return $this;
- }
-
- /**
- *
- * To generate a button
- *
- * @method button
- *
- * @param string $type The button type
- * @param string $text The button text
- * @param string $icon The button icon
- *
- * @throws Kedavra
- *
- * @return Form
- *
- */
- public function button(string $type, string $text, string $icon = '') : Form
- {
-
- $class = collect(config($this->file, 'class'))->get($type);
- switch($type)
- {
- case Form::BUTTON:
- append($this->form, '<div class="' . self::AUTO_COL . '"><div class="' . $this->separator() . '"><button class="' . $class . ' ' . $this->get_btn_class() . ' " type="button"> ' . $icon . ' ' . $text . '</button></div></div>');
- break;
- case Form::RESET:
- append($this->form, '<div class="' . self::AUTO_COL . '"><div class="' . $this->separator() . '"><button class="' . $class . ' ' . $this->get_btn_class() . ' " type="reset"> ' . $icon . ' ' . $text . '</button></div></div>');
- break;
- case Form::SUBMIT:
- append($this->form, '<div class="' . self::AUTO_COL . '"><div class="' . $this->separator() . '"><button class="' . $class . ' ' . $this->get_btn_class() . '" type="submit"> ' . $icon . ' ' . $text . '</button></div></div>');
- break;
- }
-
- return $this;
- }
-
- /**
- *
- * Append to the form the csrf token
- *
- * @method csrf
- *
- * @param string $csrf The csrf token input
- *
- * @return Form
- *
- */
- private function csrf(string $csrf) : Form
- {
-
- append($this->form, $csrf);
-
- return $this;
- }
-
- /**
- *
- * Generate a reset button
- *
- * @method reset
- *
- * @param string $text The reset button text
- * @param string $icon The reset button icon
- *
- * @throws Kedavra
- *
- * @return Form
- *
- */
- public function reset(string $text, string $icon = '') : Form
- {
-
- $class = collect(config($this->file, 'class'))->get('reset');
- append($this->form, '<div class="' . self::AUTO_COL . '"><div class="' . $this->separator() . '"><button class="' . $this->get_btn_class() . ' ' . $class . '" type="reset"> ' . $icon . ' ' . ' ' . $text . '</button></div></div>');
-
- return $this;
- }
-
- /**
- *
- * Generate a textarea
- *
- * @method textarea
- *
- * @param string $name The input name
- * @param string $placeholder The placeholder
- * @param string $validation_success_text The success validation text
- * @param string $validation_error_text The error validation text
- * @param bool $autofocus Option to add autofocus
- * @param string $value The value
- *
- * @throws Kedavra
- * @return Form
- *
- */
- public function textarea(string $name, string $placeholder, string $validation_success_text = '', string $validation_error_text = '', bool $autofocus = false, string $value = '') : Form
- {
-
- $row = collect(config($this->file, 'textarea'))->get('row');
- $col = collect(config($this->file, 'textarea'))->get('col');
- if($this->validate)
- {
- if(not_def($validation_success_text, $validation_error_text))
- throw new Kedavra('missing validation text');
- else
- $validation = $this->valid($validation_success_text, $validation_error_text);
- }
- else
- {
- $validation = '';
- }
- $class = $this->get_input_complete_class();
- if($autofocus)
- append($this->form, ' <div class="' . self::AUTO_COL . '"> <div class="' . $this->separator() . '"><textarea rows="' . $row . '" cols="' . $col . '" placeholder="' . $placeholder . '" autofocus="autofocus" class="' . $class . '" required="required" name="' . $name . '" >' . $value . '</textarea>' . $validation . '</div></div>');
- else
- append($this->form, '<div class="' . self::AUTO_COL . '"><div class="' . $this->separator() . '"><textarea rows="' . $row . '" cols="' . $col . '" placeholder="' . $placeholder . '" class="' . $class . '" required="required" name="' . $name . '" >' . $value . '</textarea> ' . $validation . '</div></div>');
-
- return $this;
- }
-
- /**
- *
- * Enable save data after submit
- *
- * @method save
- *
- * @return Form
- *
- */
- public function save() : Form
- {
-
- $this->save = true;
-
- return $this;
- }
-
- /**
- *
- * Generate a submit button
- *
- * @method submit
- *
- * @param string $text The submit button text
- * @param string $function
- * @param string $icon The submit button id
- *
- * @throws Kedavra
- * @return Form
- *
- */
- public function submit(string $text, string $icon = '', string $function = '') : Form
- {
-
- $class = collect(config($this->file, 'class'))->get('submit');
- $x = "$function()";
- if(def($function))
- append($this->form, '<div class="' . self::AUTO_COL . '"> <div class="' . $this->separator() . '"><button onclick="' . $x . '" type="submit" class="' . $this->get_btn_class() . ' ' . $class . '">' . $icon . ' ' . $text . '</button></div></div>');
- else
- append($this->form, '<div class="' . self::AUTO_COL . '"> <div class="' . $this->separator() . '"><button type="submit" class="' . $this->get_btn_class() . ' ' . $class . '">' . $icon . ' ' . $text . '</button></div></div>');
-
- return $this;
- }
-
- /**
- *
- * Generate a group
- *
- * @param array $text
- * @param string ...$href
- *
- * @throws Kedavra
- * @return Form
- *
- */
- public function group(array $text, string ...$href) : Form
- {
-
- $class = collect(config($this->file, 'class'))->get('group');
- append($this->form, '<div class="' . self::AUTO_COL . '"> <div class="' . $this->separator() . '"> <div class="btn-group " role="group">');
- foreach($href as $k => $value)
- append($this->form, '<a href="' . $value . '" class="' . $class . ' ' . $this->get_btn_class() . '"> ' . collect($text)->get($k) . '</a> ');
- append($this->form, '<div></div></div>');
-
- return $this;
- }
-
- /**
- *
- * Generate a link
- *
- * @method link
- *
- * @param string $url The url link
- * @param string $text The button text
- * @param string $icon The button icon
- *
- * @throws Kedavra
- *
- * @return Form
- *
- */
- public function link(string $url, string $text, string $icon = '') : Form
- {
-
- $class = collect(config($this->file, 'class'))->get('link');
- append($this->form, '<div class="' . self::AUTO_COL . '"> <div class="' . $this->separator() . '"><a href="' . $url . '" class="' . $this->get_btn_class() . ' ' . $class . '"> ' . $icon . ' ' . $text . '</a></div></div>');
-
- return $this;
- }
-
- /**
- *
- * Generate validation text
- *
- * @method valid
- *
- * @param string $success The success text
- * @param string $error The error text
- *
- * @return string
- *
- */
- private function valid(string $success, string $error) : string
- {
-
- return '<div class="valid-feedback text-right"> ' . $success . '</div><div class="invalid-feedback text-center">' . $error . '</div>';
- }
-
- /**
- * Generate a select input
- *
- * @method select
- *
- * @param bool $use_index
- * @param string $name The select name
- * @param array $options The select options
- * @param string $success_text The validation success text
- * @param string $error_text The validation error text
- * @param string $icon The select icon
- * @param bool $multiple The option to create a multiple select
- * @param bool $required The option to add require
- *
- * @throws Kedavra
- * @return Form
- *
- */
- public function select(bool $use_index, string $name, array $options, string $icon = '', string $success_text = '', string $error_text = '', bool $multiple = false, bool $required = true) : Form
- {
-
- $class = $this->get_input_complete_class();
- if($this->validate)
- $validation = $this->valid($success_text, $error_text);
- else
- $validation = '';
- if($required)
- {
- if(not_def($icon))
- {
- if($multiple)
- append($this->form, '<div class="' . self::AUTO_COL . '"><div class="' . $this->separator() . '"><select class="' . $class . '" name="' . $name . '" multiple required="required">');
- else
- append($this->form, '<div class="' . self::AUTO_COL . '"><div class="' . $this->separator() . '"><select class="' . $class . '" name="' . $name . '" required="required">');
- foreach($options as $k => $v)
- $use_index ? append($this->form, '<option value="' . $k . '">' . $v . '</option>') : append($this->form, '<option value="' . $v . '">' . $v . '</option>');
- append($this->form, '</select>' . $validation . '</div></div>');
- }
- else
- {
- if($multiple)
- append($this->form, '<div class="' . self::AUTO_COL . '"><div class="' . $this->separator() . '"><div class="input-group"><div class="input-group-prepend"><span class="input-group-text">' . $icon . '</span></div> <select name="' . $name . '" class="' . $class . '" multiple required="required">');
- else
- append($this->form, '<div class="' . self::AUTO_COL . '"><div class="' . $this->separator() . '"><div class="input-group"><div class="input-group-prepend"><span class="input-group-text">' . $icon . '</span></div> <select name="' . $name . '" class="' . $class . '" required="required">');
- foreach($options as $k => $v)
- {
- $use_index ? append($this->form, '<option value="' . $k . '">' . $v . '</option>') : append($this->form, '<option value="' . $v . '">' . $v . '</option>');
- }
- append($this->form, '</select>' . $validation . '</div></div></div>');
- }
- }
- else
- {
- if(not_def($icon))
- {
- if($multiple)
- append($this->form, '<div class="' . self::AUTO_COL . '"><div class="' . $this->separator() . '"><select class="' . $class . '" name="' . $name . '" multiple>');
- else
- append($this->form, '<div class="' . self::AUTO_COL . '"><div class="' . $this->separator() . '"><select class="' . $class . '" name="' . $name . '">');
- foreach($options as $k => $v)
- {
- $use_index ? append($this->form, '<option value="' . $k . '">' . $v . '</option>') : append($this->form, '<option value="' . $v . '">' . $v . '</option>');
- }
- $this->form .= '</select>' . $validation . '</div></div>';
- }
- else
- {
- if($multiple)
- append($this->form, '<div class="' . self::AUTO_COL . '"><div class="' . $this->separator() . '"><div class="input-group"><div class="input-group-prepend"><span class="input-group-text">' . $icon . '</span></div> <select name="' . $name . '" class="' . $class . '" multiple>');
- else
- append($this->form, '<div class="' . self::AUTO_COL . '"><div class="' . $this->separator() . '"><div class="input-group"><div class="input-group-prepend"><span class="input-group-text">' . $icon . '</span></div> <select name="' . $name . '" class="' . $class . '">');
- foreach($options as $k => $v)
- {
- $use_index ? append($this->form, '<option value="' . $k . '">' . $v . '</option>') : append($this->form, '<option value="' . $v . '">' . $v . '</option>');
- }
- append($this->form, '</select>' . $validation . '</div></div></div>');
- }
- }
-
- return $this;
- }
-
- /**
- *
- * Generate a checkbox
- *
- * @method checkbox
- *
- * @param string $name The checkbox name
- * @param string $text The checkbox text
- * @param bool $checked To add checked by default
- *
- * @throws Kedavra
- * @return Form
- *
- *
- */
- public function checkbox(string $name, string $text, bool $checked = false) : Form
- {
-
- $class = collect(config($this->file, 'class'))->get('checkbox');
- if($checked)
- append($this->form, '<div class="' . self::AUTO_COL . '"><div class="' . $this->separator() . '"> <div class="custom-control custom-checkbox"><input type="checkbox" checked="checked" class="custom-control-input ' . $class . '" id="' . $name . '" name="' . $name . '"><label class="custom-control-label" for="' . $name . '">' . $text . '</label></div> </div></div> ');
- else
- append($this->form, '<div class="' . self::AUTO_COL . '"><div class="' . $this->separator() . '"> <div class="custom-control custom-checkbox"><input type="checkbox" class="custom-control-input ' . $class . '" id="' . $name . '" name="' . $name . '"><label class="custom-control-label" for="' . $name . '">' . $text . '</label></div> </div> </div> ');
-
- return $this;
- }
-
- /**
- *
- * Add a search input
- *
- * @param $search_placeholder
- * @param string $icon
- * @param string $id
- *
- * @throws Kedavra
- * @return Form
- *
- */
- public function search($search_placeholder, string $icon, $id = 'search') : Form
- {
-
- $url = request()->getRequestUri();
- $x = strstr($url, '&q=');
- $url = str_replace($x, "", $url);
- $class = $this->get_input_complete_class();
- append($this->form, '<div class="' . self::AUTO_COL . '"><div class="' . $this->separator() . '"><div class="input-group"><div class="input-group-prepend"><div class="input-group-text">' . $icon . '</div></div> ');
- append($this->form, '<input placeholder="' . $search_placeholder . '" class="' . $class . '" id="' . $id . '" onchange="location = this.attributes[4].value + this.value" data-url="' . $url . '&q=" value="' . get('q') . '" autofocus="autofocus" type="search"></div></div></div>');
-
- return $this;
- }
-
- /**
- * @param string $icon
- * @param string $url
- *
- * @throws Kedavra
- * @return Form
- *
- */
- public function pagination(string $icon, string $url) : Form
- {
-
- $step = config('form', 'pagination_step');
- $class = $this->get_input_complete_class();
- append($this->form, '<div class="' . self::AUTO_COL . '"><div class="' . $this->separator() . '"><div class="input-group"><div class="input-group-prepend"><div class="input-group-text">' . $icon . '</div></div> ');
- append($this->form, '<input class="' . $class . '" onchange="location = this.attributes[2].value + this.value" data-url="' . $url . '" value="' . session('limit', 10) . '" step="' . $step . '" min="1" type="number"></div></div></div>');
-
- return $this;
- }
-
- /**
- *
- * Generate a radio
- *
- * @method radio
- *
- * @param string $name The radio name
- * @param string $text The radio text
- * @param string $id
- * @param bool $checked To defined checked by default
- *
- * @return Form
- */
- public function radio(string $name, string $text, string $id, bool $checked = false) : Form
- {
-
- if($checked)
- {
- append($this->form, '<div class="' . self::AUTO_COL . '"><div class="' . $this->separator() . '""> <div class="custom-control custom-radio">
- <input type="radio" id="' . $id . '" name="' . $name . '" class="custom-control-input" checked="checked">
- <label class="custom-control-label" for="' . $name . '">' . $text . '</label>
- </div></div></div>');
- }
- else
- {
- append($this->form, '<div class="' . self::AUTO_COL . '"><div class="' . $this->separator() . '"><div class="custom-control custom-radio">
- <input type="radio" id="' . $id . '" name="' . $name . '" class="custom-control-input">
- <label class="custom-control-label" for="' . $name . '">' . $text . '</label>
- </div></div></div>');
- }
-
- return $this;
- }
-
- /**
- * Close the form and return it
- *
- * @method end
- *
- * @return string
- *
- */
- public function end() : string
- {
-
- append($this->form, '</form>');
-
- return $this->form;
- }
-
- /**
- * Generate a redirect select
- *
- * @method redirect
- *
- * @param string $name The select name
- * @param array $options The select options
- * @param string $icon The select icon
- *
- * @return Form
- *
- */
- public function redirect(string $name, array $options, string $icon = '') : Form
- {
-
- if(def($icon))
- append($this->form, '<div class="' . self::AUTO_COL . '"><div class="' . $this->separator() . '"><div class="input-group"><div class="input-group-prepend"><div class="input-group-text">' . $icon . '</div></div>');
- else
- 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)
- 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
- append($this->form, '</select></div></div>');
-
- return $this;
- }
-
- /**
- * @param string $column
- * @param string $value
- * @param string $table
- *
- * @return Form
- *
- */
- public function primary(string $column, string $value, string $table) : Form
- {
-
- append($this->form, '<input type="hidden" name="' . $column . '" value="' . $value . '"> <input type="hidden" name="__table__" value="' . $table . '"> ');
-
- return $this;
- }
+ /**
+ * @var string
+ */
+ private $form;
- /**
- *
- * 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);
- $types = app()->table()->column()->for($table)->types();
- $columns = app()->table()->column()->for($table)->show();
+ /**
+ * @var Collect
+ */
+ private $fields;
- $primary = $instance->primary_key();
- not_in([ Form::EDIT, Form::CREATE ], $mode, true, "The mode used is not a valid mode");
+ /**
+ * Form constructor.
+ *
+ * @param string $method
+ * @param string $db
+ * @param string $route
+ * @param array $route_args
+ * @throws Kedavra
+ * @throws DependencyException
+ * @throws NotFoundException
+ */
+ public function __construct(string $method,string $db, string $route,array $route_args)
+ {
+ $this->fields = collect();
+ $method = strtoupper($method);
+ $this->append('<form action="'.route($db,$route,$route_args).'" method="POST">');
+ $this->append(csrf_field());
- if(equal($mode, Form::EDIT))
- {
- $data = app()->table()->from($table)->select_or_fail($id);
- $numeric = collect();
- $date = collect();
- $text = collect();
- $columns = collect($instance->columns_with_types());
- $values = collect();
- foreach($data as $x)
- {
- foreach($columns->all() as $k => $v)
- {
- $type = $v;
- if(has($type, App::NUMERIC_TYPES) && different($primary, $k))
- $numeric->put($k, $v);
- if(has($type, App::TEXT_TYPES))
- $text->put($k, $v);
- if(has($type, App::DATE_TYPES))
- $date->put($k, $v);
- $values->put($k, $x->$k);
- }
- }
- $all_num = $numeric->sum();
- $all_date = $date->sum();
- $all_text = $text->sum();
- $this->row();
- foreach($text->all() as $k => $t)
- {
- $value = $values->get($k);
- if(is_pair($all_text))
- {
- if(is_pair($all_text))
- $this->textarea($k, $k, '', '', false, $value);
- else
- $this->textarea($k, $k, '', '', false, $value)->end_row_and_new();
- }
- else
- {
- if(equal($all_text % 3, 0))
- $this->textarea($k, $k, '', '', false, $value);
- else
- $this->textarea($k, $k, '', '', '', $value)->end_row_and_new();
- }
- }
- $this->end_row_and_new();
- foreach($numeric->all() as $k => $n)
- {
- $value = $values->get($k);
- if(is_pair($all_num))
- {
- if(is_pair($all_num))
- $this->input(Form::NUMBER, $k, $k, '', '', '', $value);
- else
- $this->input(Form::NUMBER, $k, $k, '', '', '', $value)->end_row_and_new();
- }
- else
- {
- if(equal($all_num % 3, 0))
- $this->input(Form::NUMBER, $k, $k, '', '', '', $value);
- else
- $this->input(Form::NUMBER, $k, $k, '', '', '', $value)->end_row_and_new();
- }
- }
- $this->end_row_and_new();
- foreach($date->all() as $k => $n)
- {
- $value = $values->get($k);
- if(is_pair($all_date))
- {
- if(is_pair($all_date))
- $this->input(Form::DATETIME, $k, $k, '', '', '', $value);
- else
- $this->input(Form::DATETIME, $k, $k, '', '', '', $value)->end_row_and_new();
- }
- else
- {
- if(equal($all_date % 3, 0))
- $this->input(Form::DATETIME, $k, $k, '', '', '', $value);
- else
- $this->input(Form::DATETIME, $k, $k, '', '', '', $value)->end_row_and_new();
- }
- }
-
- return $this->end_row_and_new()->primary($primary, $id, $table)->submit($submit_text, $submit_icon)->end_row()->get();
- }
- else
- {
- $current = date('Y-m-d');
- $numeric = App::NUMERIC_TYPES;
- $date = App::DATE_TYPES;
- $text = App::TEXT_TYPES;
- $this->row();
- foreach($types as $k => $t)
- {
- $x = collect($columns)->get($k);
- if ($x !== $primary)
- {
+ $this->add('_method','hidden',['value' => $method]);
- 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();
- }
+ /**
+ *
+ * @param string $name
+ * @param string $type
+ * @param array $options
+ *
+ * @return Form
+ *
+ * @throws Kedavra
+ *
+ */
+ public function add(string $name,string $type,array $options = []): Form
+ {
+ if ($this->has($name))
+ return $this;
+ else
+ $this->fields->push($name);
- }
- }
- }
- $this->end_row_and_new();
- $id = app()->connect()->postgresql() ? 'DEFAULT' : 'NULL';
-
- return $this->primary($primary, $id, $table)->submit($submit_text, $submit_icon)->end_row()->get();
- }
- }
-
- /**
- *
- * To display large input
- *
- * @method large
- *
- * @param bool $large
- *
- * @return Form
- *
- */
- public function large(bool $large = true) : Form
- {
-
- if($large)
- {
- $this->input_size = self::LARGE_CLASS;
- $this->btn_size = self::BTN_LARGE_CLASS;
- }
- else
- {
- $this->input_size = self::BASIC_CLASS;
- $this->btn_size = self::BTN_BASIC_CLASS;
- }
-
- return $this;
- }
-
- /**
- *
- * To display small input
- *
- * @method small
- *
- * @param bool $small
- *
- * @return Form
- *
- */
- public function small(bool $small = true) : Form
- {
-
- if($small)
- {
- $this->input_size = self::SMALL_CLASS;
- $this->btn_size = self::BTN_SMALL_CLASS;
- }
- else
- {
- $this->input_size = self::BASIC_CLASS;
- $this->btn_size = self::BTN_BASIC_CLASS;
- }
-
- return $this;
- }
-
- /**
- *
- * Start a new row
- *
- * @method row
- *
- * @return Form
- *
- */
- public function row() : Form
- {
-
- $this->form .= '<div class="' . self::GRID_ROW . '">';
-
- return $this;
- }
-
- /**
- *
- * Close the row
- *
- * @method end_row
- *
- * @return Form
- *
- */
- public function end_row() : Form
- {
-
- $this->form .= '</div>';
-
- return $this;
- }
-
- /**
- *
- * Close the row and open a new row
- *
- * @method end_row_and_new
- *
- * @return Form
- *
- */
- public function end_row_and_new() : Form
- {
-
- $this->end_row();
- $this->row();
-
- return $this;
- }
-
- /**
- *
- * Close the form and return the form
- *
- * Alias to end
- *
- * @method get
- *
- * @return string
- *
- */
- public function get() : string
- {
-
- return $this->end();
- }
-
- /**
- *
- * Add validation
- *
- * @method validate
- *
- * @return Form
- *
- */
- public function validate() : Form
- {
-
- $this->validate = true;
-
- return $this;
- }
-
- }
- }
+ switch ($type)
+ {
+ case 'text':
+ case 'button':
+ case 'checkbox':
+ case 'color':
+ case 'date':
+ case 'datetime-local':
+ case 'email':
+ case 'hidden':
+ case 'image':
+ case 'month':
+ case 'number':
+ case 'password':
+ case 'range':
+ case 'search':
+ case 'submit':
+ case 'tel':
+ case 'time':
+ case 'url':
+ case 'week':
+ case 'datetime':
+ $this->append('<div class="'.$this->class('separator').'">');
+ $input = '<input type="'.$type.'" name="'.$name.'" class="'.$this->class('base').'" ';
+ foreach ($options as $k => $v)
+ append($input,$k,'=','"'.$v.'" ');
+
+ append($input,'>');
+
+ $this->append($input);
+ $this->end();
+ break;
+ case 'textarea':
+ $this->append('<div class="'.$this->class('separator').'">');
+ $input = '<textarea name="'.$name.'" class="'.$this->class('base').'" ';
+
+ foreach ($options as $k => $v)
+ if (different($k,'value'))
+ append($input,$k,'=','"'.$v.'" ');
+
+ collect($options)->has('value') ? append($input,'>'.$options["value"].'</textarea>') : append($input,'></textarea>') ;
+
+ $this->append($input);
+ $this->end();
+ break;
+ default:
+ return $this;
+ break;
+ }
+ return $this;
+ }
+
+ /**
+ *
+ * Check if form has field.
+ *
+ * @param string $name
+ *
+ * @return bool
+ *
+ */
+ public function has(string $name): bool
+ {
+ return $this->fields->exist($name);
+ }
+
+ /**
+ *
+ * Add a field by a condition
+ *
+ * @param bool $condition
+ * @param string $name
+ * @param string $type
+ * @param array $options
+ *
+ * @return Form
+ *
+ * @throws Kedavra
+ *
+ */
+ public function only(bool $condition,string $name,string $type,array $options =[]): Form
+ {
+ return $condition ? $this->add($name,$type,$options) : $this;
+ }
+
+ /**
+ *
+ * Generate a row
+ *
+ * @return Form
+ *
+ * @throws Kedavra
+ *
+ */
+ public function row(): Form
+ {
+ return $this->append('<div class="'.$this->class('row').'">');
+ }
+
+
+ /**
+ *
+ * Close the div
+ *
+ * @return Form
+ *
+ */
+ public function end(): Form
+ {
+ return $this->append('</div>');
+ }
+
+
+ /**
+ *
+ * Return the form
+ *
+ * @param string $submit_text
+ *
+ * @return string
+ *
+ * @throws Kedavra
+ *
+ */
+ public function get(string $submit_text ='submit'): string
+ {
+
+ $this->append('<button type="submit" class="'.$this->class('submit').'">'.$submit_text.'</button>');
+
+ return def($this->form) ? $this->form . '</form>' : '</form>';
+ }
+
+ /**
+ *
+ * Append data to the form
+ *
+ * @param string $x
+ *
+ * @return Form
+ *
+ */
+ private function append(string $x): Form
+ {
+ append($this->form,$x);
+ return $this;
+ }
+
+ /**
+ *
+ * Found the class
+ *
+ * @param string $x
+ *
+ * @return string
+ *
+ * @throws Kedavra
+ *
+ */
+ private function class(string $x):string
+ {
+ return collect(config('form','class'))->get($x);
+ }
+
+ /**
+ * @param string $table
+ * @param int $id
+ * @return string
+ * @throws DependencyException
+ * @throws Kedavra
+ * @throws NotFoundException
+ */
+ public function edit(string $table,int $id): string
+ {
+
+ $x = app()->table()->column()->for($table);
+
+ $primary = $x->primary_key();
+
+ $values = collect(app()->table()->from($table)->select_or_fail($id));
+
+ $columns = collect($x->columns_with_types());
+
+
+ foreach ($values->all() as $k => $v)
+ {
+ foreach ($columns->all() as $column => $type )
+ {
+ if(has($type,App::NUMERIC_TYPES))
+ equal($column,$primary) ? $this->add($column,'hidden',['value'=> $v->$column]) : $this->add($column,'number',['value'=> $v->$column]);
+ elseif(has($type,App::TEXT_TYPES))
+ $this->add($column,'textarea',['value' => $v->$column,'rows'=> 10]);
+ elseif(has($type,App::DATE_TYPES))
+ $this->add($column,'datetime',['value'=> $v->$column]);
+ else
+ $this->add($column,'textarea',['value' => $v->$column,'rows'=> 10]);
+ }
+ }
+
+ return $this->get(config('form','update_text'));
+ }
+
+ /**
+ *
+ * Generate a form to create a new record
+ *
+ * @param string $table
+ *
+ * @return string
+ *
+ * @throws DependencyException
+ * @throws Kedavra
+ * @throws NotFoundException
+ *
+ */
+ public function generate(string $table)
+ {
+ $x = app()->table()->column()->for($table);
+
+ $primary = $x->primary_key();
+
+ $columns = collect($x->columns_with_types());
+
+ foreach ($columns->all() as $column => $type )
+ {
+ if(has($type,App::NUMERIC_TYPES))
+ equal($column,$primary) ? app()->connect()->postgresql() ? $this->add($column,'hidden',['value'=> 'DEFAULT']) : $this->add($column,'hidden',['value'=> 'NULL']) : $this->add($column,'number',['placeholder'=> $column,'required'=>'required']);
+ elseif(has($type,App::TEXT_TYPES))
+ $this->add($column,'textarea',['placeholder' => $column,'rows'=> 10,'required'=>'required']);
+ elseif(has($type,App::DATE_TYPES))
+ $this->add($column,'datetime',['value'=> date("Y-m-d H:i:s")]);
+ else
+ $this->add($column,'textarea',['placeholder' => $column,'rows'=> 10,'required'=>'required']);
+
+ }
+ return $this->get(config('form','create_text'));
+ }
+
+ /**
+ *
+ * Generate a select
+ *
+ * @param string $name
+ * @param array $options
+ *
+ * @return Form
+ *
+ * @throws Kedavra
+ *
+ */
+ public function select(string $name,array $options): Form
+ {
+
+ $this->append( '<div class="' . $this->class('separator').'"><select class="' . $this->class('base').'" name="'.$name.'" required="required"><option value=""> ' . config('form','choice_option') . '</option>');
+
+ foreach($options as $k => $option)
+ is_integer($k) ? append($this->form,'<option value="' . $option . '"> ' . $option . '</option>'): append($this->form, '<option value="' . $k . '"> ' . $option . '</option>');
+
+ append($this->form, '</select></div>');
+
+ return $this;
+ }
+ }
+} \ No newline at end of file
diff --git a/imperium/Management.php b/imperium/Management.php
index f8a3824..7c9d693 100644
--- a/imperium/Management.php
+++ b/imperium/Management.php
@@ -138,19 +138,22 @@
*
*/
public function truncate(string $table) : bool;
-
- /**
- * @return Form
- */
- public function form() : Form;
- /**
- *
- * Get an instance of table
- *
- * @return Table
- *
- */
+ /**
+ * @param string $method
+ * @param string $db
+ * @param string $route
+ * @param array $route_args
+ * @return Form
+ */
+ public function form(string $method,string $db, string $route,...$route_args) : Form;
+
+ /**
+ *
+ * Get an instance of table
+ *
+ * @return Table
+ */
public function table() : Table;
/**
@@ -391,13 +394,12 @@
*
* Generate url string
*
+ * @param string $db
* @param string $route
- * @param bool $web
- * @param bool $admin
- * @param bool $task
* @param mixed $args
*
* @return string
+ *
*/
public function url(string $db,string $route,...$args) : string;
diff --git a/imperium/Model/Model.php b/imperium/Model/Model.php
index 1168f83..b38966d 100644
--- a/imperium/Model/Model.php
+++ b/imperium/Model/Model.php
@@ -118,6 +118,7 @@
*
* @throws DependencyException
* @throws NotFoundException
+ * @throws Kedavra
*
*/
public static function between(int $begin, int $end, string $column = '') : Query
diff --git a/imperium/Routing/Router.php b/imperium/Routing/Router.php
index 1de250e..1a65199 100644
--- a/imperium/Routing/Router.php
+++ b/imperium/Routing/Router.php
@@ -65,7 +65,7 @@
public function __construct(ServerRequestInterface $request)
{
- $this->method = $request->getMethod() !== GET ? def($request->getParsedBody()) ? strtoupper(collect($request->getParsedBody())->get('method')) : $request->getMethod() : GET;
+ $this->method = $request->getMethod() !== GET ? def($request->getParsedBody()) ? strtoupper(collect($request->getParsedBody())->get('_method')) : $request->getMethod() : GET;
$this->url = $request->getUri()->getPath();
diff --git a/imperium/Tables/Table.php b/imperium/Tables/Table.php
index a4f7f07..15a9a53 100644
--- a/imperium/Tables/Table.php
+++ b/imperium/Tables/Table.php
@@ -473,7 +473,7 @@
else
{
if(has($type, self::BOOL))
- $x->put(true_or_false($this->driver), $column);
+ $x->put(rand(0,1), $column);
if(has($type, self::JSONS))
{
$data = collect();
@@ -488,11 +488,11 @@
$x->put($data->json(), $column);
}
if(has($type, self::DATE_TYPES))
- $x->put($column, $this->connexion->instance()->quote(faker()->date()));
+ $x->put($column, $this->connexion->pdo()->quote(faker()->date()));
if(has($type, self::NUMERIC_TYPES))
$x->put($column, faker()->numberBetween(1, 100));
if(has($type, self::TEXT_TYPES))
- $x->put($column, $this->connexion->instance()->quote(faker()->text(50)));
+ $x->put($column, $this->connexion->pdo()->quote(faker()->text(50)));
}
}
$value = '(' . $x->join(', ') . '),';
diff --git a/tests/AppTest.php b/tests/AppTest.php
index 61175c7..438545d 100644
--- a/tests/AppTest.php
+++ b/tests/AppTest.php
@@ -26,7 +26,7 @@
$this->assertInstanceOf(Collect::class,app()->collect());
$this->assertInstanceOf(File::class,app()->file('README.md'));
$this->assertInstanceOf(Table::class,app()->table());
- $this->assertInstanceOf(Form::class,app()->form());
+ $this->assertInstanceOf(Form::class,app()->form('post','admin','create','users'));
$this->assertInstanceOf(Query::class,app()->sql('users'));
$this->assertInstanceOf(Request::class,app()->request());
$this->assertInstanceOf(Oauth::class,app()->auth());
diff --git a/tests/FormTests.php b/tests/FormTests.php
new file mode 100644
index 0000000..bf53b53
--- /dev/null
+++ b/tests/FormTests.php
@@ -0,0 +1,80 @@
+<?php
+
+
+namespace Testing;
+
+
+use DI\DependencyException;
+use DI\NotFoundException;
+use Imperium\Exception\Kedavra;
+use Imperium\Html\Form\Form;
+use Imperium\Testing\Unit;
+
+class FormTests extends Unit
+{
+ /**
+ * @var Form
+ */
+ private $form;
+
+ /**
+ * @throws Kedavra
+ * @throws DependencyException
+ * @throws NotFoundException
+ */
+ public function setUp(): void
+ {
+ $this->form = new Form('POST','admin','create',['users']);
+ }
+
+ /**
+ * @throws Kedavra
+ */
+ public function test_only()
+ {
+ $this->assertStringNotContainsString('username',$this->form->only(false,'username','text')->get());
+ $this->assertStringContainsString('username',$this->form->only(true,'username','text')->get());
+ }
+
+ /**
+ * @throws Kedavra
+ */
+ public function test_add()
+ {
+ $form = $this->form->add('username','text')->add('email','email')->get();
+ $this->assertStringContainsString('username',$form);
+ $this->assertStringContainsString('email',$form);
+ }
+
+ public function test_edit()
+ {
+ $this->assertNotEmpty($this->form->edit('users',50));
+ $this->assertStringContainsString('50',$this->form->edit('users',50));
+ }
+
+
+ /**
+ * @throws Kedavra
+ */
+ public function test_add_with_rules()
+ {
+ $form = $this->form->add('username','text',['min'=> 5])->add('email','email',['max'=> 10])->get();
+ $this->assertStringContainsString('max="10"',$form);
+ $this->assertStringContainsString('min="5"',$form);
+ }
+
+ /**
+ * @throws Kedavra
+ */
+ public function tests_globals()
+ {
+ $form = $this->form->add('username','text',['min'=> 3])->add('bio','textarea',['required' =>'required','cols'=> 10])->add('phone','phone')->get('send');
+ $this->assertStringContainsString('send',$form);
+ $this->assertStringContainsString('submit',$form);
+ $this->assertStringContainsString('name="_method"',$form);
+ $this->assertStringContainsString('value="POST"',$form);
+ $this->assertStringContainsString('min="3"',$form);
+ $this->assertStringContainsString('required="required"',$form);
+ $this->assertStringContainsString('cols="10"',$form);
+ }
+} \ No newline at end of file
diff --git a/tests/Model/CrudTest.php b/tests/Model/CrudTest.php
index 313b2ae..f522cb1 100644
--- a/tests/Model/CrudTest.php
+++ b/tests/Model/CrudTest.php
@@ -5,12 +5,11 @@ 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;
+use Twig\Error\LoaderError;
+use Twig\Error\RuntimeError;
+use Twig\Error\SyntaxError;
class CrudTest extends Unit
{
@@ -27,8 +26,13 @@ class CrudTest extends Unit
}
/**
- * @throws Kedavra
- */
+ * @throws DependencyException
+ * @throws Kedavra
+ * @throws NotFoundException
+ * @throws LoaderError
+ * @throws RuntimeError
+ * @throws SyntaxError
+ */
public function test_show()
{
$this->assertTrue($this->crud->show('users')->isOk());
@@ -36,18 +40,25 @@ class CrudTest extends Unit
}
/**
- * @throws Kedavra
- */
+ * @throws DependencyException
+ * @throws Kedavra
+ * @throws LoaderError
+ * @throws NotFoundException
+ * @throws RuntimeError
+ * @throws SyntaxError
+ */
public function test_home()
{
$this->assertTrue($this->crud->home()->isOk());
$this->assertTrue($this->crud->clear('countries')->isRedirect('/countries'));
}
-
-/**
- * @throws Kedavra
- */
+
+ /**
+ * @throws DependencyException
+ * @throws Kedavra
+ * @throws NotFoundException
+ */
public function test_delete()
{
$this->assertTrue($this->crud->destroy('users',20)->isRedirect('/'));
@@ -55,22 +66,47 @@ class CrudTest extends Unit
}
/**
+ * @throws DependencyException
* @throws Kedavra
+ * @throws NotFoundException
*/
public function test_create()
{
$this->assertTrue($this->crud->add('users')->isRedirect('/'));
}
+
+ /**
+ * @throws DependencyException
+ * @throws Kedavra
+ * @throws LoaderError
+ * @throws NotFoundException
+ * @throws RuntimeError
+ * @throws SyntaxError
+ */
public function test_generate_form()
{
$this->assertTrue($this->crud->create('users')->isOk());
}
+ /**
+ * @throws DependencyException
+ * @throws Kedavra
+ * @throws LoaderError
+ * @throws NotFoundException
+ * @throws RuntimeError
+ * @throws SyntaxError
+ */
public function test_edit()
{
$this->assertTrue($this->crud->edit('users',30)->isOk());
}
+
+ /**
+ * @throws DependencyException
+ * @throws Kedavra
+ * @throws NotFoundException
+ */
public function test_refresh()
{
$this->expectException(Kedavra::class);