From f5f2cdfb44740cfc9dea738ca02b21b2401a73a4 Mon Sep 17 00:00:00 2001 From: Romans Malinovskis Date: Sat, 13 May 2017 20:16:49 +0100 Subject: [PATCH 1/4] Improve Money, Drop-downs display, Added Date, Time and DateTime support Added hasOne() reference support. --- demos/database.php | 2 +- demos/form3.php | 6 ++++- src/Form.php | 27 ++++++++++++++++++++++ src/FormField/Calendar.php | 42 ++++++++++++++++++++++++++++++++++ src/FormField/Dropdown.php | 23 +++++++++++++++---- src/FormField/Input.php | 10 +++++++- src/FormField/Money.php | 31 +++++++++++++++++++++++++ src/Persistence/UI.php | 7 +++--- template/semantic-ui/html.html | 4 +++- template/semantic-ui/html.pug | 6 ++++- 10 files changed, 145 insertions(+), 13 deletions(-) create mode 100644 src/FormField/Calendar.php create mode 100644 src/FormField/Money.php diff --git a/demos/database.php b/demos/database.php index b09dada8c8..668d708cd5 100644 --- a/demos/database.php +++ b/demos/database.php @@ -79,7 +79,7 @@ public function init() $this->addFields(['start_date', 'finish_date'], ['type'=>'date']); $this->addField('finish_time', ['type'=>'time']); - $this->addFields(['created', 'updated'], ['type'=>'datetime']); + $this->addFields(['created', 'updated'], ['type'=>'datetime', 'ui'=>['form'=>['FormField/Line', 'disabled'=>true]]]); } } diff --git a/demos/form3.php b/demos/form3.php index 8514cd7e2e..6218f12813 100644 --- a/demos/form3.php +++ b/demos/form3.php @@ -31,8 +31,12 @@ $form->onSubmit(function ($form) { $errors = []; foreach ($form->model->dirty as $field => $value) { + + if ($form->model->getElement($field)->never_persist) { + continue; + } $errors[] = $form->error($field, 'Value was changed, '.json_encode($value).' to '.json_encode($form->model[$field])); } - return $errors ?: $form->success('No changed fields'); + return $errors ?: 'No fields were changed'; }); diff --git a/src/Form.php b/src/Form.php index a0e320f858..d064aa7a85 100644 --- a/src/Form.php +++ b/src/Form.php @@ -336,6 +336,16 @@ public function _fieldFactory(\atk4\data\Field $f) return new FormField\Dropdown($arg); } + // Field values can be picked from the model. + if (isset($f->reference)) { + //$arg['values'] = $f->ref(); + + $dd = new FormField\Dropdown($arg); + $dd->setModel($f->reference->ref()); + return $dd; + } + + switch ($f->type) { case 'boolean': return new FormField\Checkbox($arg); @@ -346,6 +356,23 @@ public function _fieldFactory(\atk4\data\Field $f) case 'password': return new FormField\Password($arg); + + case 'datetime': + $arg['options']['ampm'] = false; + return new FormField\Calendar($arg); + + case 'date': + $arg['type'] = 'date'; + return new FormField\Calendar($arg); + + case 'time': + $arg['type'] = 'time'; + $arg['options']['ampm'] = false; + return new FormField\Calendar($arg); + + case 'money': + return new FormField\Money($arg); + case null: return new FormField\Line($arg); diff --git a/src/FormField/Calendar.php b/src/FormField/Calendar.php new file mode 100644 index 0000000000..2c2a443446 --- /dev/null +++ b/src/FormField/Calendar.php @@ -0,0 +1,42 @@ +icon) { + switch ($this->type) { + //case 'date': $this->icon = ' + } + } + + if ($this->type) { + $this->options['type'] = $this->type; + } + + $this->js(true)->calendar($this->options); + + + parent::renderView(); + } + +} diff --git a/src/FormField/Dropdown.php b/src/FormField/Dropdown.php index 5c12e25ea0..4bf6a15783 100644 --- a/src/FormField/Dropdown.php +++ b/src/FormField/Dropdown.php @@ -27,12 +27,25 @@ public function getInput() $value = isset($this->field) ? $this->app->ui_persistence->typecastSaveField($this->field, $this->field->get()) : $this->content ?: ''; $options = []; - foreach ($this->values as $key=>$val) { - $item = ['option', 'value'=>(string) $key, $val]; - if ($value == $val) { - $item['selected'] = true; + + if (isset($this->model)) { + foreach ($this->model as $key=>$row) { + $title = $row[$row->title_field]; + + $item = ['option', 'value'=>(string) $key, $title]; + if ($value == $key) { + $item['selected'] = true; + } + $options[] = $item; + } + } else { + foreach ($this->values as $key=>$val) { + $item = ['option', 'value'=>(string) $key, $val]; + if ($value == $val) { + $item['selected'] = true; + } + $options[] = $item; } - $options[] = $item; } return $this->app->getTag('select', [ diff --git a/src/FormField/Input.php b/src/FormField/Input.php index 4638bfff33..b0590a158e 100644 --- a/src/FormField/Input.php +++ b/src/FormField/Input.php @@ -51,6 +51,14 @@ public function jsInput($when = null, $action = null) return $this->js($when, $action, '#'.$this->id.'_input'); } + /** + * Returns presentable value to be inserted into input tag + */ + public function getValue() + { + return isset($this->field) ? $this->app->ui_persistence->typecastSaveField($this->field, $this->field->get()) : $this->content ?: ''; + } + /** * returns tag. */ @@ -61,7 +69,7 @@ public function getInput() 'type' => $this->inputType, 'placeholder'=> $this->placeholder, 'id' => $this->id.'_input', - 'value' => isset($this->field) ? $this->app->ui_persistence->typecastSaveField($this->field, $this->field->get()) : $this->content ?: '', + 'value' => $this->getValue() ]); //return ''; } diff --git a/src/FormField/Money.php b/src/FormField/Money.php new file mode 100644 index 0000000000..c6520d31e4 --- /dev/null +++ b/src/FormField/Money.php @@ -0,0 +1,31 @@ +field ? $this->field->get() : ($this->content ?: null); + + if (is_null($v)) { + return null; + } + + return number_format($v, 2); + } + + function renderView() + { + if ($this->label === null) { + $this->label = $this->app->ui_persistence->currency; + } + + parent::renderView(); + } + +} diff --git a/src/Persistence/UI.php b/src/Persistence/UI.php index b1c15d4eb0..75fcb6a3d7 100644 --- a/src/Persistence/UI.php +++ b/src/Persistence/UI.php @@ -17,11 +17,12 @@ */ class UI extends \atk4\data\Persistence { - public $date_format = 'd/m/Y'; + public $date_format = 'M d, Y'; - public $time_format = 'h:i:s'; + public $time_format = 'H:i'; - public $datetime_format = 'D, d M Y H:i:s O'; + public $datetime_format = 'M d, Y H:i:s'; + // 'D, d M Y H:i:s O'; public $currency = '€'; diff --git a/template/semantic-ui/html.html b/template/semantic-ui/html.html index 48377d5167..582a955799 100644 --- a/template/semantic-ui/html.html +++ b/template/semantic-ui/html.html @@ -3,10 +3,12 @@ {title}Agile UI - Untitled Project{/}{$meta} - + + +