From: Andrey Kutejko Date: Mon, 1 Sep 2014 18:05:55 +0000 (+0300) Subject: simplify html generation X-Git-Tag: 0.6~152 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=67c0c06389af6361b0928e904f97f5aa6eaa54bb;p=ipf.git simplify html generation --- diff --git a/composer.lock b/composer.lock index 8b3108b..c5bf582 100644 --- a/composer.lock +++ b/composer.lock @@ -74,7 +74,7 @@ "source": { "type": "git", "url": "git://git.andy128k.net/missing-tools.git", - "reference": "c02b63b393ec05d0fa55c9eff64023618bf3ebfa" + "reference": "f1558fc778b002767a5dde8428b4ddb366697638" }, "require": { "andy128k/pegp": "0.1.*@dev", @@ -99,7 +99,7 @@ } ], "description": "Miscellaneous utilities", - "time": "2014-08-24 18:59:36" + "time": "2014-09-01 17:54:03" }, { "name": "andy128k/pegp", @@ -533,45 +533,44 @@ }, { "name": "phpunit/php-token-stream", - "version": "1.2.2", + "version": "1.3.0", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-token-stream.git", - "reference": "ad4e1e23ae01b483c16f600ff1bebec184588e32" + "reference": "f8d5d08c56de5cfd592b3340424a81733259a876" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/ad4e1e23ae01b483c16f600ff1bebec184588e32", - "reference": "ad4e1e23ae01b483c16f600ff1bebec184588e32", + "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/f8d5d08c56de5cfd592b3340424a81733259a876", + "reference": "f8d5d08c56de5cfd592b3340424a81733259a876", "shasum": "" }, "require": { "ext-tokenizer": "*", "php": ">=5.3.3" }, + "require-dev": { + "phpunit/phpunit": "~4.2" + }, "type": "library", "extra": { "branch-alias": { - "dev-master": "1.2-dev" + "dev-master": "1.3-dev" } }, "autoload": { "classmap": [ - "PHP/" + "src/" ] }, "notification-url": "https://packagist.org/downloads/", - "include-path": [ - "" - ], "license": [ "BSD-3-Clause" ], "authors": [ { "name": "Sebastian Bergmann", - "email": "sb@sebastian-bergmann.de", - "role": "lead" + "email": "sebastian@phpunit.de" } ], "description": "Wrapper around PHP's tokenizer extension.", @@ -579,7 +578,7 @@ "keywords": [ "tokenizer" ], - "time": "2014-03-03 05:10:30" + "time": "2014-08-31 06:12:13" }, { "name": "phpunit/phpunit", diff --git a/ipf/admin/legacymodel.php b/ipf/admin/legacymodel.php index 10c17c1..40572fd 100644 --- a/ipf/admin/legacymodel.php +++ b/ipf/admin/legacymodel.php @@ -1,5 +1,6 @@ '; case 'timestamp': - return htmlspecialchars(IPF_Utils::formatDate($value), ENT_COMPAT, 'UTF-8'); + return Text::escape(IPF_Utils::formatDate($value)); default: - return htmlspecialchars($value, ENT_COMPAT, 'UTF-8'); + return Text::escape($value); } } diff --git a/ipf/admin/model.php b/ipf/admin/model.php index 03730be..778f0f4 100644 --- a/ipf/admin/model.php +++ b/ipf/admin/model.php @@ -107,31 +107,18 @@ abstract class IPF_Admin_Component public abstract function list_display(); - protected function columnTitle($column) + public function list_display_links() { - return IPF_Utils::humanTitle($column); + return null; } - public function ListItemsHeader() + protected function columnTitle($column) { - $this->header = array(); - $this->names = $this->list_display(); - - foreach ($this->names as $name) { - if (is_array($name)) { - list($name, $title) = $name; - } else { - $title = $this->columnTitle($name); - } - - $this->header[$name] = array( - 'title' => $title, - 'name' => $name, - ); - } - return $this->header; + return IPF_Utils::humanTitle($column); } + public abstract function renderCell($object, $column); + protected abstract function getItems($searchValue, $filters, $page, $pageSize); protected abstract function itemsCount($searchValue, $filters); public abstract function getObjectByID($id); @@ -143,45 +130,6 @@ abstract class IPF_Admin_Component return false; } - public abstract function renderCell($object, $column); - - public function ListRow($o, $id) - { - $row = array(); - - foreach ($this->header as &$h) { - $listMethod = 'column_'.$h['name']; - if (method_exists($this,$listMethod)) { - $str = $this->$listMethod($o); - } else { - $str = $this->renderCell($o, $h['name']); - } - $row[$h['name']] = $str; - } - $this->linksRow($row, $o, $id); - return $row; - } - - protected function linksRow(&$row, $o, $id) - { - if (method_exists($this, 'list_display_links')) { - $links_display = $this->list_display_links(); - } else { - $links_display = null; - $i = 1; - } - foreach ($row as $name => &$v) { - if ($links_display) { - if (array_search($name, $links_display) !== false) - $v = ''.$v.''; - } else { - if ($i == 1) - $v = ''.$v.''; - $i++; - } - } - } - protected abstract function _getForm($obj, $data); protected function _getEditForm($obj, $data) @@ -353,7 +301,47 @@ abstract class IPF_Admin_Component $count = $this->itemsCount($searchValue, $filters); $objects = $this->getItems($searchValue, $filters, $currentPage, $this->perPage); - $this->ListItemsHeader(); + + $header = array(); + foreach ($this->list_display() as $name) { + if (is_array($name)) { + list($name, $title) = $name; + } else { + $title = $this->columnTitle($name); + } + + $header[] = array( + 'title' => $title, + 'name' => $name, + ); + } + + $links_display = $this->list_display_links(); + if (!$links_display && count($header)) + $links_display = array($header[0]['name']); + + $rows = array(); + foreach ($objects as $id => $o) { + $cells = array(); + foreach ($header as $h) { + $listMethod = 'column_'.$h['name']; + if (method_exists($this,$listMethod)) { + $cell = $this->$listMethod($o); + } else { + $cell = $this->renderCell($o, $h['name']); + } + + if (array_search($h['name'], $links_display) !== false) + $cell = ''.$cell.''; + + $cells[] = $cell; + } + $rows[] = array( + 'cells' => $cells, + 'object' => $o, + 'object_id' => $id, + ); + } $pagerLayout = new IPF_Pager_Layout; $pages = $pagerLayout->layout($currentPage, ceil($count / $this->perPage)); @@ -361,8 +349,8 @@ abstract class IPF_Admin_Component $context = array( 'orderable'=>$this->_orderable(), 'page_title'=>$this->titleList(), - 'header'=>$this->header, - 'objects' => $objects, + 'header' => $header, + 'rows' => $rows, 'count' => $count, 'pages' => $pages, 'current_page' => $currentPage, diff --git a/ipf/admin/templates/admin/items.html b/ipf/admin/templates/admin/items.html index 6f53572..3227943 100644 --- a/ipf/admin/templates/admin/items.html +++ b/ipf/admin/templates/admin/items.html @@ -63,10 +63,10 @@ - {foreach $objects as $id => $o} - - {foreach $component->ListRow($o, $id) as $v} - {$v|safe} + {foreach $rows as $row} + + {foreach $row['cells'] as $cell} + {$cell|safe} {/foreach} {/foreach} diff --git a/ipf/form/widget/radioinput.php b/ipf/form/widget/radioinput.php index e6b4997..3a0a1e0 100644 --- a/ipf/form/widget/radioinput.php +++ b/ipf/form/widget/radioinput.php @@ -24,12 +24,15 @@ class IPF_Form_Widget_RadioInput extends IPF_Form_Widget ->attrs($this->attrs) ->attrs($extra_attrs); foreach ($this->choices as $option_label => $option_value) { - $radio = Tag::input(array('type' => 'radio', 'name' => $name, 'value' => $option_value)); - if ($option_value == $value) - $radio->attr('checked', 'checked'); - $output->append(Tag::li(null, - Tag::label(null, $radio, ' ', $option_label))); + Tag::label(null, + Tag::input() + ->attr('type', 'radio') + ->attr('name', $name) + ->attr('value', $option_value) + ->toggleAttr('checked', 'checked', $option_value == $value), + ' ', + $option_label))); } return $output->html(); } diff --git a/ipf/form/widget/selectinput.php b/ipf/form/widget/selectinput.php index 21d8def..b39a12f 100644 --- a/ipf/form/widget/selectinput.php +++ b/ipf/form/widget/selectinput.php @@ -26,12 +26,10 @@ class IPF_Form_Widget_SelectInput extends IPF_Form_Widget ->attr('name', $name); $choices = $this->choices + $choices; foreach ($choices as $option_label => $option_value) { - $option = Tag::option() + $select->append(Tag::option() ->attr('value', $option_value) - ->append($option_label); - if ($option_value == $value) - $option->attr('selected', 'selected'); - $select->append($option); + ->toggleAttr('selected', 'selected', $option_value == $value) + ->append($option_label)); } return $select->html(); } diff --git a/ipf/form/widget/selectmultipleinput.php b/ipf/form/widget/selectmultipleinput.php index 0b36c23..d64c94a 100644 --- a/ipf/form/widget/selectmultipleinput.php +++ b/ipf/form/widget/selectmultipleinput.php @@ -15,22 +15,20 @@ class IPF_Form_Widget_SelectMultipleInput extends IPF_Form_Widget parent::__construct($attrs); } - public function render($name, $value, $extra_attrs=array(), $choices=array()) + public function render($name, $value, $extra_attrs=array()) { - $output = array(); - if ($value === null) { + if ($value === null) $value = array(); - } + $select = Tag::select($this->attrs) ->attrs($extra_attrs) ->attr('name', $name.'[]') ->attr('multiple', 'multiple'); - $choices = array_merge($this->choices, $choices); - foreach ($choices as $option_label => $option_value) { - $option = Tag::option()->attr('value', $option_value)->append($option_label); - if (@in_array($option_value, $value)) - $option->attr('selected', 'selected'); - $select->append($option); + foreach ($this->choices as $option_label => $option_value) { + $select->append(Tag::option() + ->attr('value', $option_value) + ->toggleAttr('selected', 'selected', @in_array($option_value, $value)) + ->append($option_label)); } return $select->html(); } diff --git a/ipf/form/widget/selectmultipleinputcheckbox.php b/ipf/form/widget/selectmultipleinputcheckbox.php index 7ce464c..1970f15 100644 --- a/ipf/form/widget/selectmultipleinputcheckbox.php +++ b/ipf/form/widget/selectmultipleinputcheckbox.php @@ -13,18 +13,15 @@ class IPF_Form_Widget_SelectMultipleInputCheckbox extends IPF_Form_Widget_Select ->attrs($this->attrs) ->attrs($extra_attrs); foreach ($this->choices as $option_label => $option_value) { - $selected = in_array($option_value, $value); - - $checkbox = Tag::input() - ->attr('type', 'checkbox') - ->attr('name', "{$name}[]") - ->attr('value', $option_value); - - if ($selected) - $checkbox->attr('checked', 'checked'); - $output->append(Tag::li(null, - Tag::label(null, $checkbox, ' ', $option_label))); + Tag::label(null, + Tag::input() + ->attr('type', 'checkbox') + ->attr('name', "{$name}[]") + ->attr('value', $option_value) + ->toggleAttr('checked', 'checked', in_array($option_value, $value)), + ' ', + $option_label))); } return $output->html(); }