]> git.andy128k.dev Git - ipf.git/commitdiff
cleanup widgets
authorAndrey Kutejko <andy128k@gmail.com>
Tue, 19 Aug 2014 16:42:33 +0000 (19:42 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Tue, 19 Aug 2014 16:42:33 +0000 (19:42 +0300)
24 files changed:
ipf/admin/app.php
ipf/admin/form.php [new file with mode: 0644]
ipf/admin/model.php
ipf/admin/modelinline.php
ipf/admin/templates/admin/change.html
ipf/admin/templates/admin/changepassword.html
ipf/admin/templates/admin/login.html
ipf/admin/views.php
ipf/auth/models/Role.php
ipf/auth/models/User.php
ipf/form.php
ipf/form/boundfield.php
ipf/form/layout.php
ipf/form/widget.php
ipf/form/widget/fileinput.php
ipf/form/widget/htmlinput.php
ipf/form/widget/image.php
ipf/form/widget/input.php
ipf/form/widget/radioinput.php
ipf/form/widget/selectinput.php
ipf/form/widget/selectmultipleinput.php
ipf/form/widget/selectmultipleinputcheckbox.php
ipf/form/widget/textareainput.php
ipf/form/widget/tupleinput.php [deleted file]

index a8651b4c0b3a40b21911a5b5af8e866967607c22..ae4ae7a7ab46bc81c9ee69cc481e52357228cfe5 100644 (file)
@@ -1,7 +1,5 @@
 <?php
 
-use \PFF\HtmlBuilder\Tag as Tag;
-
 class IPF_Admin_App extends IPF_Application
 {
     public function __construct()
@@ -93,57 +91,5 @@ class IPF_Admin_App extends IPF_Application
         
         return $perms;
     }
-
-    public static function renderForm($form)
-    {
-        return $form->renderLayout(new IPF_Admin_Form_Layout, false);
-    }
-}
-
-class IPF_Admin_Form_Layout extends IPF_Form_LayoutAdapter
-{
-    public function startForm($form)
-    {
-        $errors = $this->commonErrors($form);
-        return $this->errors($errors) .
-            $this->hiddenWidgets($form);
-    }
-
-    public function startGroup($label)
-    {
-        if ($label)
-            return Tag::div(array('class' => 'form-group-title'), $label)->html();
-        else
-            return '';
-    }
-
-    public function field($boundField, $label)
-    {
-        if ($boundField->help_text) {
-            $help_text = Tag::p(array('class' => 'help'))
-                ->raw($boundField->help_text);
-        } else {
-            $help_text = '';
-        }
-
-        return $this->errors($boundField->errors) .
-            Tag::div(array('class' => 'form-row'),
-                Tag::div()
-                    ->raw($label)
-                    ->raw(' ')
-                    ->raw($boundField->render_w())
-                    ->append($help_text));
-    }
-
-    private function errors($errors)
-    {
-        if (!count($errors))
-            return '';
-
-        $ul = Tag::ul(array('class' => 'errorlist'));
-        foreach ($errors as $err)
-            $ul->append(Tag::li(null, $err));
-        return Tag::div(null, $ul);
-    }
 }
 
diff --git a/ipf/admin/form.php b/ipf/admin/form.php
new file mode 100644 (file)
index 0000000..03bedbb
--- /dev/null
@@ -0,0 +1,72 @@
+<?php
+
+use \PFF\HtmlBuilder\Tag as Tag;
+
+
+class IFP_Admin_ModelForm extends IPF_Form_Model
+{
+    public function unescape($html)
+    {
+        return new IPF_Template_SafeString($html, true);
+    }
+
+    public function render()
+    {
+        return $this->renderLayout(new IPF_Admin_Form_Layout, false);
+    }
+
+    public static function GetFormForModel($model, $data=null, $extra=array(), $label_suffix=null)
+    {
+        $extra['model'] = $model;
+        return new self($data, $extra, $label_suffix);
+    }
+}
+
+
+class IPF_Admin_Form_Layout extends IPF_Form_LayoutAdapter
+{
+    public function startForm($form)
+    {
+        $errors = $this->commonErrors($form);
+        return $this->errors($errors) .
+            $this->hiddenWidgets($form);
+    }
+
+    public function startGroup($label)
+    {
+        if ($label)
+            return Tag::div(array('class' => 'form-group-title'), $label)->html();
+        else
+            return '';
+    }
+
+    public function field($boundField, $label)
+    {
+        if ($boundField->help_text) {
+            $help_text = Tag::p(array('class' => 'help'))
+                ->raw($boundField->help_text);
+        } else {
+            $help_text = '';
+        }
+
+        return $this->errors($boundField->errors) .
+            Tag::div(array('class' => 'form-row'),
+                Tag::div()
+                    ->raw($label)
+                    ->raw(' ')
+                    ->raw($boundField->render())
+                    ->append($help_text));
+    }
+
+    private function errors($errors)
+    {
+        if (!count($errors))
+            return '';
+
+        $ul = Tag::ul(array('class' => 'errorlist'));
+        foreach ($errors as $err)
+            $ul->append(Tag::li(null, $err));
+        return Tag::div(null, $ul);
+    }
+}
+
index 2df9115b3a7c8647839182b45fea1b035f9aedee..60e7a80badc05a0fa43ab81b16c24eb32d7b77a7 100644 (file)
@@ -623,7 +623,7 @@ class IPF_Admin_Model
 
     protected function _getForm($model_obj, $data, $extra)
     {
-        return IPF_Shortcuts::GetFormForModel($model_obj,$data,$extra);
+        return IFP_Admin_ModelForm::GetFormForModel($model_obj, $data, $extra);
     }
 
     protected function _getEditForm($model_obj, $data, $extra)
@@ -743,7 +743,6 @@ class IPF_Admin_Model
             'page_title'=>$this->titleAdd(),
             'classname'=>$this->verbose_name(),
             'form' => $form,
-            'form_html' => IPF_Admin_App::renderForm($form),
             'extra_js' => $extraMedia['js'],
             'extra_css' => $extraMedia['css'],
             'inlineInstances'=>$this->inlineInstances,
@@ -842,7 +841,6 @@ class IPF_Admin_Model
             'classname'=>$this->verbose_name(),
             'object'=>$o,
             'form' => $form,
-            'form_html' => IPF_Admin_App::renderForm($form),
             'extra_js' => $extraMedia['js'],
             'extra_css' => $extraMedia['css'],
             'inlineInstances'=>$this->inlineInstances,
index 1e4e090e77a855e8a8278483d000019657e530e4..cb0b82303a918ae975808514e10908866de8d69c 100644 (file)
@@ -83,7 +83,7 @@ abstract class IPF_Admin_ModelInline
 
     protected function _getForm($model_obj, $data, $extra)
     {
-        return IPF_Shortcuts::GetFormForModel($model_obj, $data, $extra);
+        return IFP_Admin_ModelForm::GetFormForModel($model_obj, $data, $extra);
     }
 
     function getFkName()
index e18b14b8247968b5d99cf5dca89d53625906a358..992992585b5ba2573216193a62e02936b371573a 100644 (file)
@@ -31,7 +31,7 @@
             {/if}
             <fieldset class="module aligned">
               {block form}
-                {$form_html}
+                {$form->render()}
               {/block}
             </fieldset>
             {if $inlineInstances}
index 1d2246e749b939fcd55d6b64826052f75eee64e5..e43341fcbb4aadb5f595b47a6f0d0733642bdf21 100644 (file)
@@ -19,7 +19,7 @@
           <p class="errornote">Please correct the error below.</p>
         {/if}
         <fieldset class="module aligned">
-          {$form_html}
+          {$form->render()}
         </fieldset>
         <div class="submit-row">
           <input type="submit" value="Change password" class="default" />
index 18b5c94e4382e7a47672557c77b6366142ce0383..f4ee2220f31458d699d3af6d5ccc7bd0666cc9b6 100644 (file)
@@ -13,7 +13,7 @@
 <div id="content" class="colM">
   <div id="content-main">
     <form method="post">
-      {$form_html}
+      {$form->render()}
       <div class="submit-row">
         <input type="submit" value="Sign In" class="default" />
       </div>
index 845ad0ecbab495b4bfa8e2eb01ae7f9a98a26566..eadbb9aafad3e4662eba24cc640281f9f3026187 100644 (file)
@@ -256,7 +256,6 @@ function IPF_Admin_Views_ChangePassword($request, $match)
                 'classname'=>'User',
                 'object'=>$user,
                 'form' => $form,
-                'form_html' => IPF_Admin_App::renderForm($form),
                 'extra_js' => array(),
                 'lapp'=>$lapp,
                 'lmodel'=>$lmodel,
@@ -290,7 +289,6 @@ function IPF_Admin_Views_Login($request, $match)
     $context = array(
         'page_title' => IPF::get('admin_title'),
         'form' => $form,
-        'form_html' => IPF_Admin_App::renderForm($form),
         'extra_js' => $form->extra_js(),
     );
     return IPF_Shortcuts::RenderToResponse('admin/login.html', $context, $request);
index b79bd59399b6a8977726e42fce644f7e031c08d2..6621bd0f9722c90b8b1941728c26887d895fcfa3 100644 (file)
@@ -8,7 +8,7 @@ class Role extends BaseRole
     }
 }
 
-class IPFAdminRoleForm extends IPF_Form_Model
+class IPFAdminRoleForm extends IFP_Admin_ModelForm
 {
     public function add__Permissions__field()
     {
index e8151e30f99332155ccc699d6b3968049eb73b69..ff6536b85a4e85e2354dc1ed01de3bbdc28e21d6 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
-class IPFAuthAdminUserForm extends IPF_Form_Model
+class IPFAuthAdminUserForm extends IFP_Admin_ModelForm
 {
     function initFields($extra=array())
     {
@@ -64,7 +64,7 @@ class IPFAuthAdminUserForm extends IPF_Form_Model
         $profileTable = IPF::get('auth_profile_model');
         if ($profileTable) {
             $table = IPF_ORM::getTable($profileTable);
-            $profileFields = IPF_Form_Model::suggestFields($table);
+            $profileFields = self::suggestFields($table);
 
             $fieldGroup = array();
             foreach ($profileFields as $field) {
index f034d4317fd2f9fa125400c8865008b47a98e421..2770c08d5cac85602d6b91c5547f95b1ee5e20b7 100644 (file)
@@ -123,7 +123,7 @@ abstract class IPF_Form implements Iterator
         if ($raw)
             return $errors;
         else
-            return new IPF_Template_SafeString($errors, true);
+            return $this->unescape($errors);
     }
 
     public function get_top_errors()
@@ -162,36 +162,33 @@ abstract class IPF_Form implements Iterator
             $output .= $layout->startGroup($groupLabel);
 
             foreach ($group['fields'] as $name=>$field) {
+                if ($field->widget->is_hidden)
+                    continue;
+
                 $bf = new IPF_Form_BoundField($this, $field, $name);
 
-                if (!$field->widget->is_hidden) {
-
-                    if (strlen($bf->label) > 0) {
-                        $label = htmlspecialchars($bf->label, ENT_COMPAT, 'UTF-8');
-                        if ($this->label_suffix) {
-                            if (!in_array(mb_substr($label, -1, 1),
-                                        array(':','?','.','!'))) {
-                                $label .= $this->label_suffix;
-                            }
-                        }
-                        if ($field->required)
-                            $label_attrs = array('class'=>'required');
-                        else
-                            $label_attrs = array();
-                        $label = $bf->labelTagRaw($label, $label_attrs);
-                    } else {
-                        $label = '';
+                $label = $field->label;
+                if ($this->label_suffix) {
+                    if (!in_array(mb_substr($label, -1, 1),
+                                array(':','?','.','!'))) {
+                        $label .= $this->label_suffix;
                     }
-
-                    $output .= $layout->field($bf, $label);
                 }
+
+                if ($field->required)
+                    $label_attrs = array('class'=>'required');
+                else
+                    $label_attrs = null;
+                $label = $bf->labelTag($label, $label_attrs, true);
+
+                $output .= $layout->field($bf, $label);
             }
             $output .= $layout->endGroup($groupLabel);
         }
         $output .= $layout->endForm($this);
 
         if (!$raw)
-            $output = new IPF_Template_SafeString($output, true);
+            $output = $this->unescape($output);
 
         return $output;
     }
@@ -271,12 +268,13 @@ abstract class IPF_Form implements Iterator
         $ul = Tag::ul(array('class' => 'errorlist'));
         foreach ($errors as $err)
             $ul->append(Tag::li(null, $err));
-        return $ul;
+        return $ul->html();
     }
-}
 
-function IPF_Form_htmlspecialcharsArray(&$item, $key)
-{
-    $item = htmlspecialchars($item, ENT_COMPAT, 'UTF-8');
+    public function unescape($html)
+    {
+        // Do nothing
+        return $html;
+    }
 }
 
index df6908fb4ed9b7f2cbb4750277153275536394f5..db47a3dc48aeaa6c56058e79226190525a79df21 100644 (file)
@@ -37,6 +37,18 @@ class IPF_Form_BoundField
             return $this->form->initial($this->name);
     }
 
+    public function render()
+    {
+        $widget = $this->field->widget;
+
+        $extra_attrs = array();
+        if (array_key_exists('id', $widget->attrs)) {
+            $extra_attrs['id'] = $this->autoId();
+        }
+
+        return $widget->render($this->html_name, $this->value(), $extra_attrs);
+    }
+
     public function render_w($widget=null, $attrs=array())
     {
         if ($widget === null) {
@@ -47,10 +59,13 @@ class IPF_Form_BoundField
             and !array_key_exists('id', $widget->attrs)) {
             $attrs['id'] = $id;
         }
-        return $widget->render($this->html_name, $this->value(), $attrs);
+
+        $code = $widget->render($this->html_name, $this->value(), $attrs);
+
+        return $this->form->unescape($code);
     }
 
-    public function labelTag($contents=null, $attrs=array(), $raw=false)
+    public function labelTag($contents=null, $attrs=null, $raw=false)
     {
         $label = Tag::label($attrs);
 
@@ -64,13 +79,10 @@ class IPF_Form_BoundField
 
         $label->attr('for', $widget->idForLabel($id));
 
-        if ($this->field->required)
-            $label->addClass('req');
-
         if ($raw)
-            return $label;
+            return $label->html();
         else
-            return new IPF_Template_SafeString($label, true);
+            return $this->form->unescape($label->html());
     }
 
     public function autoId()
@@ -90,7 +102,7 @@ class IPF_Form_BoundField
         if ($raw)
             return $errors;
         else
-            return new IPF_Template_SafeString($errors, true);
+            return $this->form->unescape($errors);
     }
 
     public function __get($prop)
index fab7043c0f94db2e77cd960ad4f5e6c423b8edbc..8d87642412311defb1ad48ae5eddee9549745940 100644 (file)
@@ -31,7 +31,7 @@ abstract class IPF_Form_LayoutAdapter /*implements informal interface IPF_Form_L
     {
         $hiddenWidgets = '';
         foreach ($form->hiddenFields() as $field_name)
-            $hiddenWidgets .= $form->field($field_name)->render_w();
+            $hiddenWidgets .= $form->field($field_name)->render();
         return $hiddenWidgets;
     }
 
@@ -82,7 +82,7 @@ class IPF_Form_ParagraphLayout extends IPF_Form_LayoutAdapter
             Tag::p()
                 ->raw($label)
                 ->raw(' ')
-                ->raw($boundField->render_w())
+                ->raw($boundField->render())
                 ->raw(' ')
                 ->raw($boundField->help_text);
     }
@@ -116,7 +116,7 @@ class IPF_Form_ListLayout extends IPF_Form_LayoutAdapter
             ->raw($this->errorList($boundField->errors))
             ->raw($label)
             ->raw(' ')
-            ->raw($boundField->render_w())
+            ->raw($boundField->render())
             ->raw($help_text);
     }
 
@@ -160,7 +160,7 @@ class IPF_Form_TableLayout extends IPF_Form_LayoutAdapter
             Tag::td()
                 ->raw($this->takeDeferred())
                 ->raw($this->errorList($boundField->errors))
-                ->raw($boundField->render_w())
+                ->raw($boundField->render())
                 ->raw($help_text));
     }
 
index 8c6a499a6e2fe21f0f7db61356fa99b715a3ba71..f58c5d02baabb54ccf6605af409578554e251a6a 100644 (file)
@@ -46,12 +46,3 @@ abstract class IPF_Form_Widget
     }
 }
 
-function IPF_Form_Widget_Attrs($attrs)
-{
-    $_tmp = array();
-    foreach ($attrs as $attr=>$val) {
-        $_tmp[] = $attr.'="'.$val.'"';
-    }
-    return ' '.implode(' ', $_tmp);
-}
-
index f022016448f3bceae36c0a988fb236b7ebe6032b..321accfe660d2b9d530643c7231c84074f8888b5 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+use \PFF\HtmlBuilder\Tag as Tag;
+
 class IPF_Form_Widget_FileInput extends IPF_Form_Widget_Input
 {
     public $input_type = 'file';
@@ -10,7 +12,7 @@ class IPF_Form_Widget_FileInput extends IPF_Form_Widget_Input
     protected function viewCurrentValue($filename)
     {
         if ($filename)
-            return '<a target="_blank" href="'.IPF::getUploadUrl().$filename.'">view</a>';
+            return Tag::a(array('target' => '_blank', 'href' => IPF::getUploadUrl().$filename), 'view')->html();
         else
             return '';
     }
@@ -21,14 +23,22 @@ class IPF_Form_Widget_FileInput extends IPF_Form_Widget_Input
             return '';
 
         if ($this->allow_extended) {
-            $sim = 'Currently: <input name="'.$name.'_name" value="'.$filename.'" type="hidden" /><input name="'.$name.'_rename" value="'.$filename.'" id="id_'.$name.'_rename" type="text" style="width:150px;" /> ' .
+            $sim = 'Currently: ' .
+                Tag::input(array('name' => "{$name}_name", 'value' => $filename, 'type' => 'hidden')) .
+                Tag::input(array('name' => "{$name}_rename", 'value' => $filename, 'id' => "id_{$name}_rename", 'type' => 'text', 'style' => 'width:150px;')) .
                 $this->viewCurrentValue($filename);
+
             if ($this->allow_delete)
-                $sim .= '&nbsp;|&nbsp;<input name="'.$name.'_remove" value="1" id="id_'.$name.'_remove" type="checkbox" />&nbsp;<label class="file_remove" for="id_'.$name.'_remove">Remove</label>';
+                $sim .= '&nbsp;|&nbsp;' .
+                    Tag::input(array('name' => "{$name}_remove", 'value' => 1, 'id' => "id_{$name}_remove", 'type' => 'checkbox')) .
+                    '&nbsp;' .
+                    Tag::label(array('class' => 'file_remove', 'for' => "id_{$name}_remove"), 'Remove');
+
             $sim .= ' Change:';
+
             return $sim;
         } else {
-            return 'Currently: <b>'.$filename.'</b><br> Change: ';
+            return 'Currently: ' . Tag::b(null, $filename) . '<br> Change: ';
         }
     }
 
index 5ca2f7c6d9f94478af48b3cfa3a838431ca0ee30..cc488e45c31e5363cb9ae69a84f03d760143b09f 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+use \PFF\HtmlBuilder\Tag as Tag;
+
 class IPF_Form_Widget_HTMLInput extends IPF_Form_Widget
 {
     public $mode = 'textareas';
@@ -45,7 +47,11 @@ class IPF_Form_Widget_HTMLInput extends IPF_Form_Widget
             }
         }
         $final_attrs = $this->buildAttrs(array('name' => $name), $extra_attrs);
-        return new IPF_Template_SafeString('<textarea'.IPF_Form_Widget_Attrs($final_attrs).' class="htmlEditor'.($this->force_absolute_urls ? 'Abs' : '').'">'.htmlspecialchars($value, ENT_COMPAT, 'UTF-8').'</textarea>', true);
+
+        return Tag::textarea($final_attrs)
+            ->addClass($this->force_absolute_urls ? 'htmlEditorAbs' : 'htmlEditor')
+            ->append($value)
+            ->html();
     }
 
     public function extra_js()
index 2d957c4682f4eb561e4db2894b382132f630532e..b25678a0ed7545fd3027f34cb2240de757362247 100644 (file)
@@ -1,15 +1,20 @@
 <?php
 
+use \PFF\HtmlBuilder\Tag as Tag;
+
 class IPF_Form_Widget_Image extends IPF_Form_Widget_FileInput
 {
     protected function viewCurrentValue($filename)
     {
-        if ($filename) {
-            $url = IPF::getUploadUrl() . $filename;
-            return '&nbsp;<a target="_blank" href="'.$url.'"><img src="'.$url.'" style="max-width:64px;max-height:64px"/></a>&nbsp;';
-        } else {
+        if (!$filename)
             return '';
-        }
+
+        $url = IPF::getUploadUrl() . $filename;
+        return '&nbsp;' .
+            Tag::a(array('target' => '_blank', 'href' => $url),
+                Tag::img(array('src' => $url, 'style' => 'max-width:64px;max-height:64px')))
+                ->html() .
+            '&nbsp;';
     }
 }
 
index 81be6ba112a07fa8679d0e3a32ea8bfe9b6fd1cf..9287c0f3ccf67d13a53203afa74d76048cb33bf5 100644 (file)
@@ -1,17 +1,18 @@
 <?php
 
+use \PFF\HtmlBuilder\Tag as Tag;
+
 class IPF_Form_Widget_Input extends IPF_Form_Widget
 {
     public function render($name, $value, $extra_attrs=array())
     {
-        if ($value === null) $value = '';
         $final_attrs = $this->buildAttrs(array('name' => $name, 
                                                'type' => $this->input_type),
                                          $extra_attrs);
-        if ($value !== '') {
-            $value = htmlspecialchars($value, ENT_COMPAT, 'UTF-8');
+        if ($value)
             $final_attrs['value'] = $value;
-        }
-        return new IPF_Template_SafeString('<input'.IPF_Form_Widget_Attrs($final_attrs).' />', true);
+
+        return Tag::input($final_attrs);
     }
-}
\ No newline at end of file
+}
+
index a28484e83aa9360123e9bd2ea3f1891ba8280319..56a9d6fe6dd0fbebd5e8ef0bc9f2f5cbbb3d41aa 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+use \PFF\HtmlBuilder\Tag as Tag;
+
 class IPF_Form_Widget_RadioInput extends IPF_Form_Widget
 {
     public $choices = array();
@@ -13,32 +15,29 @@ class IPF_Form_Widget_RadioInput 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(), $choices=array())
     {
         $output = array();
         if ($value === null) {
             $value = '';
         }
-        // $final_attrs = $this->buildAttrs($extra_attrs);
-        $output[] = '<ul>'; // '.IPF_Form_Widget_Attrs($final_attrs).'
+        $output = Tag::ul(); // ->attrs($this->buildAttrs($extra_attrs));
         $choices = $this->choices + $choices;
         $index = 1;
-        foreach ($choices as $option_label=>$option_value) {
-            $selected = ($option_value == $value) ? ' checked="checked"':'';
-            $output[] = sprintf('<input type="radio" name="%s" id="id_%s-%s" value="%s"%s/><label for="id_%s-%s">%s</label>',
-                                $name,
-                                $name,
-                                $index,
-                                htmlspecialchars($option_value, ENT_COMPAT, 'UTF-8'),
-                                $selected,
-                                $name,
-                                $index,
-                                htmlspecialchars($option_label, ENT_COMPAT, 'UTF-8'));
+        foreach ($choices as $option_label => $option_value) {
+            $optionId = "id_$name-$index";
+
+            $radio = Tag::input(array('type' => 'radio', 'name' => $name, 'id' => $optionId, 'value' => $option_value));
+            if ($option_value == $value)
+                $radio->attr('checked', 'checked');
+
+            $output->append(Tag::li(null,
+                $radio,
+                Tag::label(array('for' => $optionId), $option_label)));
+
             $index++;
         }
-        $output[] = '</ul>';
-        return new IPF_Template_SafeString(implode("\n", $output), true);
+        return $output->html();
     }
 }
 
index 67c3ed27d80702b9d094bf3570f292e36711db9e..21d8def95f471735347bae2af680bcd2c3a05d60 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+use \PFF\HtmlBuilder\Tag as Tag;
+
 class IPF_Form_Widget_SelectInput extends IPF_Form_Widget
 {
     public $choices = array();
@@ -13,24 +15,25 @@ class IPF_Form_Widget_SelectInput 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(), $choices=array())
     {
         $output = array();
         if ($value === null) {
             $value = '';
         }
-        $final_attrs = $this->buildAttrs(array('name' => $name), $extra_attrs);
-        $output[] = '<select'.IPF_Form_Widget_Attrs($final_attrs).'>';
+        $select = Tag::select($this->attrs)
+            ->attrs($extra_attrs)
+            ->attr('name', $name);
         $choices = $this->choices + $choices;
-        foreach ($choices as $option_label=>$option_value) {
-            $selected = ($option_value == $value) ? ' selected="selected"':'';
-            $output[] = sprintf('<option value="%s"%s>%s</option>',
-                                htmlspecialchars($option_value, ENT_COMPAT, 'UTF-8'),
-                                $selected, 
-                                htmlspecialchars($option_label, ENT_COMPAT, 'UTF-8'));
+        foreach ($choices as $option_label => $option_value) {
+            $option = Tag::option()
+                ->attr('value', $option_value)
+                ->append($option_label);
+            if ($option_value == $value)
+                $option->attr('selected', 'selected');
+            $select->append($option);
         }
-        $output[] = '</select>';
-        return new IPF_Template_SafeString(implode("\n", $output), true);
+        return $select->html();
     }
-}
\ No newline at end of file
+}
+
index 1ebc46d43735878811a1bb7a011169ec63990b42..2d2f55bdde538643ba23b27d978c271bd0f8d111 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+use \PFF\HtmlBuilder\Tag as Tag;
+
 class IPF_Form_Widget_SelectMultipleInput extends IPF_Form_Widget
 {
     public $choices = array();
@@ -19,22 +21,18 @@ class IPF_Form_Widget_SelectMultipleInput extends IPF_Form_Widget
         if ($value === null) {
             $value = array();
         }
-        $final_attrs = $this->buildAttrs(array('name' => $name.'[]'),
-                                         $extra_attrs);
-        $output[] = '<select multiple="multiple"'
-            .IPF_Form_Widget_Attrs($final_attrs).'>';
+        $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) {
-            $selected = (@in_array($option_value, $value)) ? ' selected="selected"':'';
-            $output[] = sprintf('<option value="%s"%s>%s</option>',
-                                htmlspecialchars($option_value, ENT_COMPAT, 'UTF-8'),
-                                $selected,
-                                htmlspecialchars($option_label, ENT_COMPAT, 'UTF-8'));
-
+        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);
         }
-        $output[] = '</select>';
-        return new IPF_Template_SafeString(implode("\n", $output), true);
+        return $select->html();
     }
 
     public function valueFromFormData($name, &$data)
index 3f4e614516ead760aa9a2c4b5c7bbd9ddf8b2af6..612f0f7c1297f78f24347310518b31d2c7fd9b61 100644 (file)
@@ -1,29 +1,34 @@
 <?php
 
+use \PFF\HtmlBuilder\Tag as Tag;
+
 class IPF_Form_Widget_SelectMultipleInputCheckbox extends IPF_Form_Widget_SelectMultipleInput
 {
     public function render($name, $value, $extra_attrs=array(), $choices=array())
     {
-        $output = array();
         if ($value === null || $value == '')
             $value = array();
         $final_attrs = $this->buildAttrs($extra_attrs);
-        $output[] = '<ul>';
+        $output = Tag::ul();
         $choices = array_merge($this->choices, $choices);
         $i=0;
         $base_id = $final_attrs['id'];
-        foreach ($choices as $option_label=>$option_value) {
-            $final_attrs['id'] = $base_id.'_'.$i;
-            $final_attrs['value'] = htmlspecialchars($option_value, ENT_COMPAT, 'UTF-8');
-            $checkbox = new IPF_Form_Widget_CheckboxInput($final_attrs);
-            $rendered = $checkbox->render($name.'[]', in_array($option_value, $value), array('value'=>$option_value));
-            $output[] = sprintf('<li><label>%s %s</label></li>',
-                                $rendered,
-                                htmlspecialchars($option_label, ENT_COMPAT, 'UTF-8'));
+        foreach ($choices as $option_label => $option_value) {
+            $selected = in_array($option_value, $value);
+
+            $checkbox = Tag::input($final_attrs)
+                ->attr('type', 'checkbox')
+                ->attr('name', "{$name}[]")
+                ->attr('id', $base_id.'_'.$i)
+                ->attr('value', $option_value);
+            if ($selected)
+                $checkbox->attr('checked', 'checked');
+
+            $output->append(Tag::li(null,
+                Tag::label(null, $checkbox, ' ', $option_label)));
             $i++;
         }
-        $output[] = '</ul>';
-        return new IPF_Template_SafeString(implode("\n", $output), true);
+        return $output->html();
     }
 
     public function idForLabel($id)
index bab0cf8bd9e08b8f352c4068134df3a7e7e88a90..876c44ff73cef8ef7f23def64813b4f3c2442133 100644 (file)
@@ -1,23 +1,20 @@
 <?php
 
+use \PFF\HtmlBuilder\Tag as Tag;
+
 class IPF_Form_Widget_TextareaInput extends IPF_Form_Widget
 {
-
     public function __construct($attrs=array())
     {
-        $this->attrs = array_merge(array('cols' => '40', 'rows' => '10'), 
-                                   $attrs);
+        $this->attrs = array_merge(array('cols' => '40', 'rows' => '10'), $attrs);
     }
 
     public function render($name, $value, $extra_attrs=array())
     {
-        if ($value === null) $value = '';
-        $final_attrs = $this->buildAttrs(array('name' => $name),
-                                         $extra_attrs);
-        return new IPF_Template_SafeString(
-                       sprintf('<textarea%s>%s</textarea>',
-                               IPF_Form_Widget_Attrs($final_attrs),
-                               htmlspecialchars($value, ENT_COMPAT, 'UTF-8')),
-                       true);
+        if ($value === null)
+            $value = '';
+        $final_attrs = $this->buildAttrs(array('name' => $name), $extra_attrs);
+        return Tag::textarea($final_attrs, $value)->html();
     }
 }
+
diff --git a/ipf/form/widget/tupleinput.php b/ipf/form/widget/tupleinput.php
deleted file mode 100644 (file)
index 2c13aab..0000000
+++ /dev/null
@@ -1,68 +0,0 @@
-<?php
-
-class IPF_Form_Widget_TupleInput extends IPF_Form_Widget
-{
-    var $headers = array('');
-    var $rows = 3;
-
-    public function __construct($attrs=array()){
-        
-        parent::__construct($attrs);
-
-        if (isset($attrs['headers']))
-            $this->headers = $attrs['headers'];
-
-        if (isset($attrs['rows']))
-            $this->rows = $attrs['rows'];
-    }
-    
-    protected function isHeadLabels(){
-        foreach ($this->headers as &$h){
-            if ($h=='')
-                return false;
-        }
-        return true;
-    }
-
-    public function render($name, $value, $extra_attrs=array())
-    {
-        $data = array();
-        $lines = explode("\n",$value);
-        foreach ($lines as $line){
-            $data[] = explode('|', $line);
-        }
-        if ($value === null) $value = '';
-        $s = '<table class="tuplegrid">';
-        if ($this->isHeadLabels()){
-            $s .= '<tr>';
-            foreach ($this->headers as &$h){
-                $s .= '<th>'.$h.'</th>';
-            }
-            $s .= '</tr>';
-        }
-        for ($i=0; $i<$this->rows; $i++){
-            $s .= '<tr>';
-            for ($j=0; $j<count($this->headers); $j++){
-                $v = @$data[$i][$j];
-                $s .= '<td><input name="'.$name.'_'.$i.'_'.$j.'" value="'.$v.'"></td>';
-            }
-            $s .= '<tr>';
-        }
-        $s .= '</table>';
-        return new IPF_Template_SafeString($s,true);
-    }
-
-    public function valueFromFormData($name, &$data)
-    {
-        $s = '';
-        for ($i=0; $i<$this->rows; $i++){
-            if ($i>0) $s .= "\n";
-            for ($j=0; $j<count($this->headers); $j++){
-                if ($j>0) $s.='|';
-                $s .= @$data[$name.'_'.$i.'_'.$j];
-            }
-        }
-        return $s;
-    }
-}
-