pics/templates/FormView.php

160 lines
5.7 KiB
PHP

<?php
/*****************************************************************************
* FormView.php
* Contains the form template.
*
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
*****************************************************************************/
class FormView extends SubTemplate
{
public function __construct(Form $form, $title = '')
{
$this->title = $title;
$this->request_url = $form->request_url;
$this->request_method = $form->request_method;
$this->fields = $form->getFields();
$this->missing = $form->getMissing();
$this->data = $form->getData();
$this->content_above = $form->content_above;
$this->content_below = $form->content_below;
}
protected function html_content($exclude = [], $include = [])
{
if (!empty($this->title))
echo '
<div class="admin_box">
<h2>', htmlspecialchars($this->title), '</h2>';
foreach ($this->_subtemplates as $template)
$template->html_main();
echo '
<form action="', $this->request_url, '" method="', $this->request_method, '" enctype="multipart/form-data">';
if (isset($this->content_above))
echo $this->content_above;
echo '
<dl>';
foreach ($this->fields 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);
}
echo '
</dl>
<input type="hidden" name="', Session::getSessionTokenKey(), '" value="', Session::getSessionToken(), '">
<div style="clear: both">
<button type="submit" class="btn btn-primary">Save information</button>';
if (isset($this->content_below))
echo '
', $this->content_below;
echo '
</div>
</form>';
if (!empty($this->title))
echo '
</div>';
}
protected function renderField($field_id, $field)
{
if (isset($field['before_html']))
echo '</dl>
', $field['before_html'], '
<dl>';
if ($field['type'] != 'checkbox' && isset($field['label']))
echo '
<dt class="cont_', $field_id, isset($field['tab_class']) ? ' target target-' . $field['tab_class'] : '', '"', in_array($field_id, $this->missing) ? ' style="color: red"' : '', '>', $field['label'], '</dt>';
elseif ($field['type'] == 'checkbox' && isset($field['header']))
echo '
<dt class="cont_', $field_id, isset($field['tab_class']) ? ' target target-' . $field['tab_class'] : '', '"', in_array($field_id, $this->missing) ? ' style="color: red"' : '', '>', $field['header'], '</dt>';
echo '
<dd class="cont_', $field_id, isset($field['dd_class']) ? ' ' . $field['dd_class'] : '', isset($field['tab_class']) ? ' target target-' . $field['tab_class'] : '', '">';
if (isset($field['before']))
echo $field['before'];
switch ($field['type'])
{
case 'select':
echo '
<select name="', $field_id, '" id="', $field_id, '"', !empty($field['disabled']) ? ' disabled' : '', '>';
if (isset($field['placeholder']))
echo '
<option value="">', $field['placeholder'], '</option>';
foreach ($field['options'] as $value => $option)
echo '
<option value="', $value, '"', $this->data[$field_id] == $value ? ' selected' : '', '>', htmlentities($option), '</option>';
echo '
</select>';
break;
case 'radio':
foreach ($field['options'] as $value => $option)
echo '
<input type="radio" name="', $field_id, '" value="', $value, '"', $this->data[$field_id] == $value ? ' checked' : '', !empty($field['disabled']) ? ' disabled' : '', '> ', htmlentities($option);
break;
case 'checkbox':
echo '
<label><input type="checkbox"', $this->data[$field_id] ? ' checked' : '', !empty($field['disabled']) ? ' disabled' : '', ' name="', $field_id, '"> ', htmlentities($field['label']), '</label>';
break;
case 'textarea':
echo '
<textarea name="', $field_id, '" id="', $field_id, '" cols="', isset($field['columns']) ? $field['columns'] : 40, '" rows="', isset($field['rows']) ? $field['rows'] : 4, '"', !empty($field['disabled']) ? ' disabled' : '', '>', $this->data[$field_id], '</textarea>';
break;
case 'color':
echo '
<input type="color" name="', $field_id, '" id="', $field_id, '" value="', htmlentities($this->data[$field_id]), '"', !empty($field['disabled']) ? ' disabled' : '', '>';
break;
case 'numeric':
echo '
<input type="number"', isset($field['step']) ? ' step="' . $field['step'] . '"' : '', ' min="', isset($field['min_value']) ? $field['min_value'] : '0', '" max="', isset($field['max_value']) ? $field['max_value'] : '9999', '" name="', $field_id, '" id="', $field_id, '"', isset($field['size']) ? ' size="' . $field['size'] . '"' : '', isset($field['maxlength']) ? ' maxlength="' . $field['maxlength'] . '"' : '', ' value="', htmlentities($this->data[$field_id]), '"', !empty($field['disabled']) ? ' disabled' : '', '>';
break;
case 'file':
if (!empty($this->data[$field_id]))
echo '<img src="', $this->data[$field_id], '" alt=""><br>';
echo '
<input type="file" name="', $field_id, '" id="', $field_id, '"', !empty($field['disabled']) ? ' disabled' : '', '>';
break;
case 'text':
case 'password':
default:
echo '
<input type="', $field['type'], '" name="', $field_id, '" id="', $field_id, '"', isset($field['size']) ? ' size="' . $field['size'] . '"' : '', isset($field['maxlength']) ? ' maxlength="' . $field['maxlength'] . '"' : '', ' value="', htmlentities($this->data[$field_id]), '"', !empty($field['disabled']) ? ' disabled' : '', isset($field['trigger']) ? ' class="trigger-' . $field['trigger'] . '"' : '', '>';
}
if (isset($field['after']))
echo ' ', $field['after'];
echo '
</dd>';
}
}