Compare commits

..

7 Commits

5 changed files with 40 additions and 33 deletions

View File

@ -122,7 +122,7 @@ class EditAlbum extends HTMLController
$this->form = new Form([ $this->form = new Form([
'request_url' => BASEURL . '/editalbum/?' . ($id_tag ? 'id=' . $id_tag : 'add'), 'request_url' => BASEURL . '/editalbum/?' . ($id_tag ? 'id=' . $id_tag : 'add'),
'content_below' => $after_form, 'buttons_extra' => $after_form,
'fields' => $fields, 'fields' => $fields,
]); ]);

View File

@ -106,7 +106,7 @@ class EditTag extends HTMLController
$form = new Form([ $form = new Form([
'request_url' => BASEURL . '/edittag/?' . ($id_tag ? 'id=' . $id_tag : 'add'), 'request_url' => BASEURL . '/edittag/?' . ($id_tag ? 'id=' . $id_tag : 'add'),
'content_below' => $after_form, 'buttons_extra' => $after_form,
'fields' => $fields, 'fields' => $fields,
]); ]);

View File

@ -69,7 +69,7 @@ class EditUser extends HTMLController
$form = new Form([ $form = new Form([
'request_url' => BASEURL . '/edituser/?' . ($id_user ? 'id=' . $id_user : 'add'), 'request_url' => BASEURL . '/edituser/?' . ($id_user ? 'id=' . $id_user : 'add'),
'content_below' => $after_form, 'buttons_extra' => $after_form,
'fields' => [ 'fields' => [
'first_name' => [ 'first_name' => [
'type' => 'text', 'type' => 'text',

View File

@ -10,24 +10,36 @@ class Form
{ {
public $request_method; public $request_method;
public $request_url; public $request_url;
public $content_above;
public $content_below;
private $fields = []; private $fields = [];
public $before_fields;
public $after_fields;
private $submit_caption;
public $buttons_extra;
private $trim_inputs;
private $data = []; private $data = [];
private $missing = []; private $missing = [];
private $submit_caption;
private $trim_inputs;
// NOTE: this class does not verify the completeness of form options. // NOTE: this class does not verify the completeness of form options.
public function __construct($options) public function __construct($options)
{ {
$this->request_method = !empty($options['request_method']) ? $options['request_method'] : 'POST'; static $optionKeys = [
$this->request_url = !empty($options['request_url']) ? $options['request_url'] : BASEURL; 'request_method' => 'POST',
$this->fields = !empty($options['fields']) ? $options['fields'] : []; 'request_url' => BASEURL,
$this->content_below = !empty($options['content_below']) ? $options['content_below'] : null;
$this->content_above = !empty($options['content_above']) ? $options['content_above'] : null; 'fields' => [],
$this->submit_caption = !empty($options['submit_caption']) ? $options['submit_caption'] : 'Save information'; 'before_fields' => null,
$this->trim_inputs = !empty($options['trim_inputs']); 'after_fields' => null,
'submit_caption' => 'Save information',
'buttons_extra' => null,
'trim_inputs' => true,
];
foreach ($optionKeys as $optionKey => $default)
$this->$optionKey = !empty($options[$optionKey]) ? $options[$optionKey] : $default;
} }
public function getFields() public function getFields()

View File

@ -19,7 +19,7 @@ class FormView extends SubTemplate
$this->title = $title; $this->title = $title;
} }
protected function html_content($exclude = [], $include = []) protected function html_content()
{ {
if (!empty($this->title)) if (!empty($this->title))
echo ' echo '
@ -31,34 +31,29 @@ class FormView extends SubTemplate
echo ' echo '
<form action="', $this->form->request_url, '" method="', $this->form->request_method, '" enctype="multipart/form-data">'; <form action="', $this->form->request_url, '" method="', $this->form->request_method, '" enctype="multipart/form-data">';
if (isset($this->form->content_above)) if (isset($this->form->before_fields))
echo $this->form->content_above; echo $this->form->before_fields;
$this->missing = $this->form->getMissing(); $this->missing = $this->form->getMissing();
$this->data = $this->form->getData(); $this->data = $this->form->getData();
foreach ($this->form->getFields() as $field_id => $field) foreach ($this->form->getFields() as $field_id => $field)
{ {
// Either we have a blacklist
if (!empty($exclude) && in_array($field_id, $exclude))
continue;
// ... or a whitelist
elseif (!empty($include) && !in_array($field_id, $include))
continue;
// ... or neither (ha)
$this->renderField($field_id, $field); $this->renderField($field_id, $field);
} }
if (isset($this->form->after_fields))
echo $this->form->after_fields;
echo ' echo '
<input type="hidden" name="', Session::getSessionTokenKey(), '" value="', Session::getSessionToken(), '"> <input type="hidden" name="', Session::getSessionTokenKey(), '" value="', Session::getSessionToken(), '">
<div class="form-group"> <div class="form-group">
<div class="offset-sm-2 col-sm-10"> <div class="offset-sm-2 col-sm-10">
<button type="submit" name="submit" class="btn btn-primary">', $this->form->getSubmitButtonCaption(), '</button>'; <button type="submit" name="submit" class="btn btn-primary">', $this->form->getSubmitButtonCaption(), '</button>';
if (isset($this->form->content_below)) if (isset($this->form->buttons_extra))
echo ' echo '
', $this->form->content_below; ', $this->form->buttons_extra;
echo ' echo '
</div> </div>
@ -75,10 +70,8 @@ class FormView extends SubTemplate
echo ' echo '
<div class="row mb-2">'; <div class="row mb-2">';
if (isset($field['before']))
echo $field['before'];
if ($field['type'] !== 'checkbox') if ($field['type'] !== 'checkbox')
{
if (isset($field['label'])) if (isset($field['label']))
echo ' echo '
<label class="col-sm-2 col-form-label" for="', $field_id, '"', in_array($field_id, $this->missing) ? ' style="color: red"' : '', '>', $field['label'], ':</label> <label class="col-sm-2 col-form-label" for="', $field_id, '"', in_array($field_id, $this->missing) ? ' style="color: red"' : '', '>', $field['label'], ':</label>
@ -86,6 +79,7 @@ class FormView extends SubTemplate
else else
echo ' echo '
<div class="offset-sm-2 ', isset($field['class']) ? $field['class'] : 'col-sm-6', '">'; <div class="offset-sm-2 ', isset($field['class']) ? $field['class'] : 'col-sm-6', '">';
}
switch ($field['type']) switch ($field['type'])
{ {
@ -127,15 +121,16 @@ class FormView extends SubTemplate
$this->renderText($field_id, $field); $this->renderText($field_id, $field);
} }
if (isset($field['after']))
echo ' ', $field['after'];
if ($field['type'] !== 'checkbox') if ($field['type'] !== 'checkbox')
echo ' echo '
</div>'; </div>';
echo ' echo '
</div>'; </div>';
if (isset($field['after_html']))
echo '
', $field['after_html'];
} }
private function renderCaptcha($field_id, array $field) private function renderCaptcha($field_id, array $field)