]> git.andy128k.dev Git - ipf.git/commitdiff
simplify html generation
authorAndrey Kutejko <andy128k@gmail.com>
Mon, 1 Sep 2014 18:05:55 +0000 (21:05 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Mon, 1 Sep 2014 18:05:55 +0000 (21:05 +0300)
composer.lock
ipf/admin/legacymodel.php
ipf/admin/model.php
ipf/admin/templates/admin/items.html
ipf/form/widget/radioinput.php
ipf/form/widget/selectinput.php
ipf/form/widget/selectmultipleinput.php
ipf/form/widget/selectmultipleinputcheckbox.php

index 8b3108bbbe507e89dd972b74224abf04972e917e..c5bf58236581e358bc5934279c1ebd23a29c9ee5 100644 (file)
@@ -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",
         },
         {
             "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.",
             "keywords": [
                 "tokenizer"
             ],
-            "time": "2014-03-03 05:10:30"
+            "time": "2014-08-31 06:12:13"
         },
         {
             "name": "phpunit/phpunit",
index 10c17c180ae7c68c1eb9a4acc05b5f7c0f0f0821..40572fd5740584eba45c52adb6722ba0963027dc 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 
+use \PFF\HtmlBuilder\Text as Text;
 use \PFF\HtmlBuilder\Tag as Tag;
 
 class IPF_Admin_Model extends IPF_Admin_Component
@@ -104,9 +105,9 @@ class IPF_Admin_Model extends IPF_Admin_Component
                 else
                     return '<img src="'.IPF::get('static_url').'admin/img/icon-no.gif" alt="False" />';
             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);
         }
     }
 
index 03730be8b57bc17a7f1b8c9c6053b2c0f1024128..778f0f43051df1fb656417283e6cdcbf1c6a3b7b 100644 (file)
@@ -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 = '<a href="'.$id.'/">'.$v.'</a>';
-            } else {
-                if ($i == 1)
-                    $v = '<a href="'.$id.'/">'.$v.'</a>';
-                $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 = '<a href="'.$id.'/">'.$cell.'</a>';
+
+                $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,
index 6f535722f8594d6884df8e326d1510f014c278e0..322794326c245968b57a31027741c869ab5664f1 100644 (file)
             </tr>
           </thead>
           <tbody>
-            {foreach $objects as $id => $o}
-            <tr class="trsort" id="{$id}">
-              {foreach $component->ListRow($o, $id) as $v}
-              <td>{$v|safe}</td>
+            {foreach $rows as $row}
+            <tr class="trsort" id="{$row['object_id']}">
+              {foreach $row['cells'] as $cell}
+              <td>{$cell|safe}</td>
               {/foreach}
             </tr>
             {/foreach}
index e6b499735ed93bb7efc8259f35a17326d6538f5f..3a0a1e09d73f8657e3bc32d630ba525755158843 100644 (file)
@@ -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();
     }
index 21d8def95f471735347bae2af680bcd2c3a05d60..b39a12f57084e80f74dae27e18dcc8416e52e246 100644 (file)
@@ -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();
     }
index 0b36c2368ee8eb3d31df1a2581fdf9d049214c4e..d64c94a13312656f12f26a3f910d3dadb6a6859e 100644 (file)
@@ -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();
     }
index 7ce464cb0e42dd488991ad7b011f90d5760887ee..1970f159f4204b00c777c8fe62d573068fcb30ed 100644 (file)
@@ -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();
     }