From d1e3374a0e5620def2bf49a22988447b0b4b8365 Mon Sep 17 00:00:00 2001 From: andrevv Date: Thu, 2 Dec 2010 21:52:19 +0200 Subject: [PATCH] IPF_Form is added --- ipf/admin/media/css/forms.css | 7 ++ ipf/auth/models/User.php | 16 +++++ ipf/form.php | 129 +++++++++++++++++++++++----------- 3 files changed, 110 insertions(+), 42 deletions(-) diff --git a/ipf/admin/media/css/forms.css b/ipf/admin/media/css/forms.css index 0687c11..848321d 100644 --- a/ipf/admin/media/css/forms.css +++ b/ipf/admin/media/css/forms.css @@ -6,6 +6,13 @@ .form-row img, .form-row input { vertical-align:middle; } form .form-row p { padding-left:0; font-size:11px; } +/* FORM GROUP TITLE */ +.form-group-title { + background-color:#CFCFCF; + padding:2px 8px 3px 16px; font-size:12px; color:#006A95; border-top:1px solid #CCC; border-bottom:1px solid #CCC; text-align:left; + font-weight:bold; +} + /* FORM LABELS */ form h4 { margin:0 !important; padding:0 !important; border:none !important; } label { font-weight:normal !important; color:#666; font-size:12px; } diff --git a/ipf/auth/models/User.php b/ipf/auth/models/User.php index 5d3bd96..6bedd6c 100644 --- a/ipf/auth/models/User.php +++ b/ipf/auth/models/User.php @@ -9,6 +9,22 @@ class AdminUser extends IPF_Admin_Model{ protected function _setupForm($form){ $form->fields['username']->help_text = 'Required. 32 characters or less. Alphanumeric characters only (letters, digits and underscores).'; $form->fields['password']->help_text = "Use '[algo]$[salt]$[hexdigest]' or use the change password form."; + + $form->fields['email']->label = 'E-mail'; + + $form->fields['is_active']->label = 'Active'; + $form->fields['is_staff']->label = 'Staff status'; + $form->fields['is_superuser']->label = 'Superuser status'; + + $form->fields['is_active']->help_text = 'Designates whether this user should be treated as active. Unselect this instead of deleting accounts.'; + $form->fields['is_staff']->help_text = 'Designates whether the user can log into this admin site.'; + $form->fields['is_superuser']->help_text = 'Designates that this user has all permissions without explicitly assigning them.'; + + $form->field_groups = array( + array('fields'=>array('username', 'password')), + array('fields'=>array('email', 'first_name', 'last_name'), 'label'=>'Personal info'), + array('fields'=>array('is_active', 'is_staff', 'is_superuser'), 'label'=>'Permissions'), + ); } public function AddItem($request, $lapp, $lmodel){ diff --git a/ipf/form.php b/ipf/form.php index b8f6c45..29f9f61 100644 --- a/ipf/form.php +++ b/ipf/form.php @@ -3,6 +3,7 @@ class IPF_Form implements Iterator { public $fields = array(); + public $field_groups = array(); public $prefix = ''; public $id_fields = 'id_%s'; @@ -121,57 +122,100 @@ class IPF_Form implements Iterator { return (isset($this->errors['__all__'])) ? $this->errors['__all__'] : array(); } - + protected function htmlOutput($normal_row, $error_row, $row_ender, - $help_text_html, $errors_on_separate_row) + $help_text_html, $errors_on_separate_row, $group_title=null) { $top_errors = (isset($this->errors['__all__'])) ? $this->errors['__all__'] : array(); array_walk($top_errors, 'IPF_Form_htmlspecialcharsArray'); $output = array(); $hidden_fields = array(); - foreach ($this->fields as $name=>$field) { - $bf = new IPF_Form_BoundField($this, $field, $name); - $bf_errors = $bf->errors; - array_walk($bf_errors, 'IPF_Form_htmlspecialcharsArray'); - if ($field->widget->is_hidden) { - foreach ($bf_errors as $_e) { - $top_errors[] = sprintf(__('(Hidden field %1$s) %2$s'), - $name, $_e); - } - $hidden_fields[] = $bf; // Not rendered - } else { - if ($errors_on_separate_row and count($bf_errors)) { - $output[] = sprintf($error_row, IPF_Form_renderErrorsAsHTML($bf_errors)); - } - 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; - } + + $groups = array(); + + if (count($this->field_groups)) + { + foreach ($this->field_groups as $field_group) + { + if (array_key_exists('fields', $field_group) && is_array($field_group['fields'])) + { + $_fields = array(); + + foreach ($field_group['fields'] as $field_name) + { + if (array_key_exists($field_name, $this->fields)) + $_fields[$field_name] = $this->fields[$field_name]; + } + + if (count($_fields)) + { + $_group = array('fields'=>$_fields); + + if (array_key_exists('label', $field_group)) + $_group['label'] = $field_group['label']; + + $groups[] = $_group; } - if ($field->required) - $label_attrs = array('class'=>'required'); - else - $label_attrs = array(); - $label = $bf->labelTag($label,$label_attrs); - } else { - $label = ''; } - if ($bf->help_text) { - // $bf->help_text can contains HTML and is not - // escaped. - $help_text = sprintf($help_text_html, $bf->help_text); + } + } + + if (!count($groups)) + { + $groups = array(array('fields'=>$this->fields)); + $render_group_title = false; + } + else $render_group_title = $group_title ? true : false; + + foreach ($groups as $group) + { + if ($render_group_title && array_key_exists('label', $group)) + $output[] = sprintf($group_title, $group['label']); + + foreach ($group['fields'] as $name=>$field) { + $bf = new IPF_Form_BoundField($this, $field, $name); + $bf_errors = $bf->errors; + array_walk($bf_errors, 'IPF_Form_htmlspecialcharsArray'); + if ($field->widget->is_hidden) { + foreach ($bf_errors as $_e) { + $top_errors[] = sprintf(__('(Hidden field %1$s) %2$s'), + $name, $_e); + } + $hidden_fields[] = $bf; // Not rendered } else { - $help_text = ''; - } - $errors = ''; - if (!$errors_on_separate_row and count($bf_errors)) { - $errors = IPF_Form_renderErrorsAsHTML($bf_errors); + if ($errors_on_separate_row and count($bf_errors)) { + $output[] = sprintf($error_row, IPF_Form_renderErrorsAsHTML($bf_errors)); + } + 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->labelTag($label,$label_attrs); + } else { + $label = ''; + } + if ($bf->help_text) { + // $bf->help_text can contains HTML and is not + // escaped. + $help_text = sprintf($help_text_html, $bf->help_text); + } else { + $help_text = ''; + } + $errors = ''; + if (!$errors_on_separate_row and count($bf_errors)) { + $errors = IPF_Form_renderErrorsAsHTML($bf_errors); + } + $output[] = sprintf($normal_row, $errors, $label, + $bf->render_w(), $help_text); } - $output[] = sprintf($normal_row, $errors, $label, - $bf->render_w(), $help_text); } } if (count($top_errors)) { @@ -218,7 +262,8 @@ class IPF_Form implements Iterator return $this->htmlOutput( '
%2$s %1$s%3$s%4$s
', '
%s
', - '', '

%s

', true + '', '

%s

', true, + '
%s
' ); } -- 2.49.0