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..85127c248f 100644 --- a/demos/form3.php +++ b/demos/form3.php @@ -31,8 +31,11 @@ $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/demos/grid.php b/demos/grid.php index 4252fbefe3..6a85d6c856 100644 --- a/demos/grid.php +++ b/demos/grid.php @@ -11,6 +11,9 @@ $g->menu->addItem(['Re-Import', 'icon'=>'power'], new \atk4\ui\jsReload($g)); $g->menu->addItem(['Delete All', 'icon'=>'trash', 'red active']); +$g->addColumn(['Template', 'helloworld']); +$g->addFormatter('name', ['Link', 'page2']); + $g->addAction('Say HI', function ($j, $id) use ($g) { return 'Loaded "'.$g->model->load($id)['name'].'" from ID='.$id; }); diff --git a/src/Form.php b/src/Form.php index a0e320f858..ae1e5f1aa5 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->refModel()); + + return $dd; + } + switch ($f->type) { case 'boolean': return new FormField\Checkbox($arg); @@ -346,6 +356,25 @@ 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..4a6652e60f --- /dev/null +++ b/src/FormField/Calendar.php @@ -0,0 +1,39 @@ +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..8cc78176e1 100644 --- a/src/FormField/Dropdown.php +++ b/src/FormField/Dropdown.php @@ -13,6 +13,8 @@ class Dropdown extends Input public $values = []; + public $empty = '...'; + public function init() { parent::init(); @@ -27,14 +29,31 @@ 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 ($this->empty) { + $item = ['option', 'value'=>'', $this->empty]; $options[] = $item; } + 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 == $key) { + $item['selected'] = true; + } + $options[] = $item; + } + } + return $this->app->getTag('select', [ 'name' => $this->short_name, 'type' => $this->inputType, diff --git a/src/FormField/Input.php b/src/FormField/Input.php index 4638bfff33..be443b0720 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..94a5ff146d --- /dev/null +++ b/src/FormField/Money.php @@ -0,0 +1,31 @@ +field ? $this->field->get() : ($this->content ?: null); + + if (is_null($v)) { + return; + } + + return number_format($v, 2); + } + + public 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/src/TableColumn/Template.php b/src/TableColumn/Template.php index 3620a6c35b..e1a70332be 100644 --- a/src/TableColumn/Template.php +++ b/src/TableColumn/Template.php @@ -21,11 +21,18 @@ class Template extends Generic */ public function __construct($template) { - $this->template = $template; + $this->template = is_object($template) ? $template : new \atk4\ui\Template($template); } public function getDataCellHTML(\atk4\data\Field $f = null) { - return $this->getTag('body', $this->template); + return $this->getTag('body', '{$c_'.$this->short_name.'}'); + } + + public function getHtmlTags($row, $field) + { + $this->table->add($this->template); + + return ['c_'.$this->short_name => $this->template->set($row)->render()]; } } 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} - + + +