Simplify and clarify Forms and FormViews #54
@ -122,7 +122,7 @@ class EditAlbum extends HTMLController
 | 
			
		||||
 | 
			
		||||
		$this->form = new Form([
 | 
			
		||||
			'request_url' => BASEURL . '/editalbum/?' . ($id_tag ? 'id=' . $id_tag : 'add'),
 | 
			
		||||
			'content_below' => $after_form,
 | 
			
		||||
			'buttons_extra' => $after_form,
 | 
			
		||||
			'fields' => $fields,
 | 
			
		||||
		]);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -106,7 +106,7 @@ class EditTag extends HTMLController
 | 
			
		||||
 | 
			
		||||
		$form = new Form([
 | 
			
		||||
			'request_url' => BASEURL . '/edittag/?' . ($id_tag ? 'id=' . $id_tag : 'add'),
 | 
			
		||||
			'content_below' => $after_form,
 | 
			
		||||
			'buttons_extra' => $after_form,
 | 
			
		||||
			'fields' => $fields,
 | 
			
		||||
		]);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -69,7 +69,7 @@ class EditUser extends HTMLController
 | 
			
		||||
 | 
			
		||||
		$form = new Form([
 | 
			
		||||
			'request_url' => BASEURL . '/edituser/?' . ($id_user ? 'id=' . $id_user : 'add'),
 | 
			
		||||
			'content_below' => $after_form,
 | 
			
		||||
			'buttons_extra' => $after_form,
 | 
			
		||||
			'fields' => [
 | 
			
		||||
				'first_name' => [
 | 
			
		||||
					'type' => 'text',
 | 
			
		||||
 | 
			
		||||
@ -10,24 +10,36 @@ class Form
 | 
			
		||||
{
 | 
			
		||||
	public $request_method;
 | 
			
		||||
	public $request_url;
 | 
			
		||||
	public $content_above;
 | 
			
		||||
	public $content_below;
 | 
			
		||||
 | 
			
		||||
	private $fields = [];
 | 
			
		||||
	public $before_fields;
 | 
			
		||||
	public $after_fields;
 | 
			
		||||
 | 
			
		||||
	private $submit_caption;
 | 
			
		||||
	public $buttons_extra;
 | 
			
		||||
	private $trim_inputs;
 | 
			
		||||
 | 
			
		||||
	private $data = [];
 | 
			
		||||
	private $missing = [];
 | 
			
		||||
	private $submit_caption;
 | 
			
		||||
	private $trim_inputs;
 | 
			
		||||
 | 
			
		||||
	// NOTE: this class does not verify the completeness of form options.
 | 
			
		||||
	public function __construct($options)
 | 
			
		||||
	{
 | 
			
		||||
		$this->request_method = !empty($options['request_method']) ? $options['request_method'] : 'POST';
 | 
			
		||||
		$this->request_url = !empty($options['request_url']) ? $options['request_url'] : BASEURL;
 | 
			
		||||
		$this->fields = !empty($options['fields']) ? $options['fields'] : [];
 | 
			
		||||
		$this->content_below = !empty($options['content_below']) ? $options['content_below'] : null;
 | 
			
		||||
		$this->content_above = !empty($options['content_above']) ? $options['content_above'] : null;
 | 
			
		||||
		$this->submit_caption = !empty($options['submit_caption']) ? $options['submit_caption'] : 'Save information';
 | 
			
		||||
		$this->trim_inputs = !empty($options['trim_inputs']);
 | 
			
		||||
		static $optionKeys = [
 | 
			
		||||
			'request_method' => 'POST',
 | 
			
		||||
			'request_url' => BASEURL,
 | 
			
		||||
 | 
			
		||||
			'fields' => [],
 | 
			
		||||
			'before_fields' => null,
 | 
			
		||||
			'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()
 | 
			
		||||
 | 
			
		||||
@ -19,7 +19,7 @@ class FormView extends SubTemplate
 | 
			
		||||
		$this->title = $title;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	protected function html_content($exclude = [], $include = [])
 | 
			
		||||
	protected function html_content()
 | 
			
		||||
	{
 | 
			
		||||
		if (!empty($this->title))
 | 
			
		||||
			echo '
 | 
			
		||||
@ -31,34 +31,29 @@ class FormView extends SubTemplate
 | 
			
		||||
		echo '
 | 
			
		||||
			<form action="', $this->form->request_url, '" method="', $this->form->request_method, '" enctype="multipart/form-data">';
 | 
			
		||||
 | 
			
		||||
		if (isset($this->form->content_above))
 | 
			
		||||
			echo $this->form->content_above;
 | 
			
		||||
		if (isset($this->form->before_fields))
 | 
			
		||||
			echo $this->form->before_fields;
 | 
			
		||||
 | 
			
		||||
		$this->missing = $this->form->getMissing();
 | 
			
		||||
		$this->data = $this->form->getData();
 | 
			
		||||
 | 
			
		||||
		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);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (isset($this->form->after_fields))
 | 
			
		||||
			echo $this->form->after_fields;
 | 
			
		||||
 | 
			
		||||
		echo '
 | 
			
		||||
				<input type="hidden" name="', Session::getSessionTokenKey(), '" value="', Session::getSessionToken(), '">
 | 
			
		||||
				<div class="form-group">
 | 
			
		||||
					<div class="offset-sm-2 col-sm-10">
 | 
			
		||||
						<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 '
 | 
			
		||||
						', $this->form->content_below;
 | 
			
		||||
						', $this->form->buttons_extra;
 | 
			
		||||
 | 
			
		||||
		echo '
 | 
			
		||||
					</div>
 | 
			
		||||
@ -75,10 +70,8 @@ class FormView extends SubTemplate
 | 
			
		||||
		echo '
 | 
			
		||||
				<div class="row mb-2">';
 | 
			
		||||
 | 
			
		||||
		if (isset($field['before']))
 | 
			
		||||
			echo $field['before'];
 | 
			
		||||
 | 
			
		||||
		if ($field['type'] !== 'checkbox')
 | 
			
		||||
		{
 | 
			
		||||
			if (isset($field['label']))
 | 
			
		||||
				echo '
 | 
			
		||||
					<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
 | 
			
		||||
				echo '
 | 
			
		||||
					<div class="offset-sm-2 ', isset($field['class']) ? $field['class'] : 'col-sm-6', '">';
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		switch ($field['type'])
 | 
			
		||||
		{
 | 
			
		||||
@ -127,15 +121,16 @@ class FormView extends SubTemplate
 | 
			
		||||
				$this->renderText($field_id, $field);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (isset($field['after']))
 | 
			
		||||
			echo ' ', $field['after'];
 | 
			
		||||
 | 
			
		||||
		if ($field['type'] !== 'checkbox')
 | 
			
		||||
			echo '
 | 
			
		||||
					</div>';
 | 
			
		||||
 | 
			
		||||
		echo '
 | 
			
		||||
				</div>';
 | 
			
		||||
 | 
			
		||||
		if (isset($field['after_html']))
 | 
			
		||||
			echo '
 | 
			
		||||
				', $field['after_html'];
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private function renderCaptcha($field_id, array $field)
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user