From: Andrey Kutejko Date: Tue, 19 Aug 2014 16:42:33 +0000 (+0300) Subject: cleanup widgets X-Git-Tag: 0.6~191 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=555e8fb02294440fa0ded9c60bf6c3e3c4f9cc27;p=ipf.git cleanup widgets --- diff --git a/ipf/admin/app.php b/ipf/admin/app.php index a8651b4..ae4ae7a 100644 --- a/ipf/admin/app.php +++ b/ipf/admin/app.php @@ -1,7 +1,5 @@ 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 index 0000000..03bedbb --- /dev/null +++ b/ipf/admin/form.php @@ -0,0 +1,72 @@ +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); + } +} + diff --git a/ipf/admin/model.php b/ipf/admin/model.php index 2df9115..60e7a80 100644 --- a/ipf/admin/model.php +++ b/ipf/admin/model.php @@ -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, diff --git a/ipf/admin/modelinline.php b/ipf/admin/modelinline.php index 1e4e090..cb0b823 100644 --- a/ipf/admin/modelinline.php +++ b/ipf/admin/modelinline.php @@ -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() diff --git a/ipf/admin/templates/admin/change.html b/ipf/admin/templates/admin/change.html index e18b14b..9929925 100644 --- a/ipf/admin/templates/admin/change.html +++ b/ipf/admin/templates/admin/change.html @@ -31,7 +31,7 @@ {/if}
{block form} - {$form_html} + {$form->render()} {/block}
{if $inlineInstances} diff --git a/ipf/admin/templates/admin/changepassword.html b/ipf/admin/templates/admin/changepassword.html index 1d2246e..e43341f 100644 --- a/ipf/admin/templates/admin/changepassword.html +++ b/ipf/admin/templates/admin/changepassword.html @@ -19,7 +19,7 @@

Please correct the error below.

{/if}
- {$form_html} + {$form->render()}
diff --git a/ipf/admin/templates/admin/login.html b/ipf/admin/templates/admin/login.html index 18b5c94..f4ee222 100644 --- a/ipf/admin/templates/admin/login.html +++ b/ipf/admin/templates/admin/login.html @@ -13,7 +13,7 @@
- {$form_html} + {$form->render()}
diff --git a/ipf/admin/views.php b/ipf/admin/views.php index 845ad0e..eadbb9a 100644 --- a/ipf/admin/views.php +++ b/ipf/admin/views.php @@ -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); diff --git a/ipf/auth/models/Role.php b/ipf/auth/models/Role.php index b79bd59..6621bd0 100644 --- a/ipf/auth/models/Role.php +++ b/ipf/auth/models/Role.php @@ -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() { diff --git a/ipf/auth/models/User.php b/ipf/auth/models/User.php index e8151e3..ff6536b 100644 --- a/ipf/auth/models/User.php +++ b/ipf/auth/models/User.php @@ -1,6 +1,6 @@ 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; + } } diff --git a/ipf/form/boundfield.php b/ipf/form/boundfield.php index df6908f..db47a3d 100644 --- a/ipf/form/boundfield.php +++ b/ipf/form/boundfield.php @@ -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) diff --git a/ipf/form/layout.php b/ipf/form/layout.php index fab7043..8d87642 100644 --- a/ipf/form/layout.php +++ b/ipf/form/layout.php @@ -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)); } diff --git a/ipf/form/widget.php b/ipf/form/widget.php index 8c6a499..f58c5d0 100644 --- a/ipf/form/widget.php +++ b/ipf/form/widget.php @@ -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); -} - diff --git a/ipf/form/widget/fileinput.php b/ipf/form/widget/fileinput.php index f022016..321accf 100644 --- a/ipf/form/widget/fileinput.php +++ b/ipf/form/widget/fileinput.php @@ -1,5 +1,7 @@ view'; + 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: ' . + $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 .= ' |  '; + $sim .= ' | ' . + Tag::input(array('name' => "{$name}_remove", 'value' => 1, 'id' => "id_{$name}_remove", 'type' => 'checkbox')) . + ' ' . + Tag::label(array('class' => 'file_remove', 'for' => "id_{$name}_remove"), 'Remove'); + $sim .= ' Change:'; + return $sim; } else { - return 'Currently: '.$filename.'
Change: '; + return 'Currently: ' . Tag::b(null, $filename) . '
Change: '; } } diff --git a/ipf/form/widget/htmlinput.php b/ipf/form/widget/htmlinput.php index 5ca2f7c..cc488e4 100644 --- a/ipf/form/widget/htmlinput.php +++ b/ipf/form/widget/htmlinput.php @@ -1,5 +1,7 @@ buildAttrs(array('name' => $name), $extra_attrs); - return new IPF_Template_SafeString(''.htmlspecialchars($value, ENT_COMPAT, 'UTF-8').'', true); + + return Tag::textarea($final_attrs) + ->addClass($this->force_absolute_urls ? 'htmlEditorAbs' : 'htmlEditor') + ->append($value) + ->html(); } public function extra_js() diff --git a/ipf/form/widget/image.php b/ipf/form/widget/image.php index 2d957c4..b25678a 100644 --- a/ipf/form/widget/image.php +++ b/ipf/form/widget/image.php @@ -1,15 +1,20 @@  '; - } else { + if (!$filename) return ''; - } + + $url = IPF::getUploadUrl() . $filename; + return ' ' . + Tag::a(array('target' => '_blank', 'href' => $url), + Tag::img(array('src' => $url, 'style' => 'max-width:64px;max-height:64px'))) + ->html() . + ' '; } } diff --git a/ipf/form/widget/input.php b/ipf/form/widget/input.php index 81be6ba..9287c0f 100644 --- a/ipf/form/widget/input.php +++ b/ipf/form/widget/input.php @@ -1,17 +1,18 @@ 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('', true); + + return Tag::input($final_attrs); } -} \ No newline at end of file +} + diff --git a/ipf/form/widget/radioinput.php b/ipf/form/widget/radioinput.php index a28484e..56a9d6f 100644 --- a/ipf/form/widget/radioinput.php +++ b/ipf/form/widget/radioinput.php @@ -1,5 +1,7 @@ buildAttrs($extra_attrs); - $output[] = '
    '; // '.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('', - $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[] = '
'; - return new IPF_Template_SafeString(implode("\n", $output), true); + return $output->html(); } } diff --git a/ipf/form/widget/selectinput.php b/ipf/form/widget/selectinput.php index 67c3ed2..21d8def 100644 --- a/ipf/form/widget/selectinput.php +++ b/ipf/form/widget/selectinput.php @@ -1,5 +1,7 @@ buildAttrs(array('name' => $name), $extra_attrs); - $output[] = ''; + $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('', - 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[] = ''; - return new IPF_Template_SafeString(implode("\n", $output), true); + return $select->html(); } -} \ No newline at end of file +} + diff --git a/ipf/form/widget/selectmultipleinput.php b/ipf/form/widget/selectmultipleinput.php index 1ebc46d..2d2f55b 100644 --- a/ipf/form/widget/selectmultipleinput.php +++ b/ipf/form/widget/selectmultipleinput.php @@ -1,5 +1,7 @@ buildAttrs(array('name' => $name.'[]'), - $extra_attrs); - $output[] = ''; - return new IPF_Template_SafeString(implode("\n", $output), true); + return $select->html(); } public function valueFromFormData($name, &$data) diff --git a/ipf/form/widget/selectmultipleinputcheckbox.php b/ipf/form/widget/selectmultipleinputcheckbox.php index 3f4e614..612f0f7 100644 --- a/ipf/form/widget/selectmultipleinputcheckbox.php +++ b/ipf/form/widget/selectmultipleinputcheckbox.php @@ -1,29 +1,34 @@ buildAttrs($extra_attrs); - $output[] = '
    '; + $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('
  • ', - $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[] = '
'; - return new IPF_Template_SafeString(implode("\n", $output), true); + return $output->html(); } public function idForLabel($id) diff --git a/ipf/form/widget/textareainput.php b/ipf/form/widget/textareainput.php index bab0cf8..876c44f 100644 --- a/ipf/form/widget/textareainput.php +++ b/ipf/form/widget/textareainput.php @@ -1,23 +1,20 @@ 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('%s', - 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 index 2c13aab..0000000 --- a/ipf/form/widget/tupleinput.php +++ /dev/null @@ -1,68 +0,0 @@ -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 = ''; - if ($this->isHeadLabels()){ - $s .= ''; - foreach ($this->headers as &$h){ - $s .= ''; - } - $s .= ''; - } - for ($i=0; $i<$this->rows; $i++){ - $s .= ''; - for ($j=0; $jheaders); $j++){ - $v = @$data[$i][$j]; - $s .= ''; - } - $s .= ''; - } - $s .= '
'.$h.'
'; - 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; $jheaders); $j++){ - if ($j>0) $s.='|'; - $s .= @$data[$name.'_'.$i.'_'.$j]; - } - } - return $s; - } -} -