1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-19 11:21:30 +02:00

feat(core): refactor Forms class #218 #186

This commit is contained in:
Awilum
2019-08-23 16:30:44 +03:00
parent 0783b3a402
commit 71ebc5388d

View File

@@ -9,20 +9,15 @@ declare(strict_types=1);
namespace Flextype;
use Flextype\Component\Filesystem\Filesystem;
use Flextype\Component\Date\Date;
use Flextype\Component\Form\Form;
use Flextype\Component\Arr\Arr;
use Flextype\Component\Text\Text;
use Flextype\Component\Registry\Registry;
use function Flextype\Component\I18n\__;
use Respect\Validation\Validator as v;
use Intervention\Image\ImageManagerStatic as Image;
use Psr\Http\Message\ResponseInterface as Response;
use Flextype\Component\Form\Form;
use Psr\Http\Message\ServerRequestInterface as Request;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
use function count;
use function Flextype\Component\I18n\__;
use function str_replace;
use function strlen;
use function strpos;
use function substr_replace;
class Forms
{
@@ -44,29 +39,31 @@ class Forms
/**
* Fetch Fieldset form
*
* @access public
* @param array $fieldset Fieldset
* @param string $values Fieldset values
*
* @return string Returns form based on fieldsets
*
* @access public
*/
public function fetch(array $fieldset, array $values = [], Request $request) : string
{
$form = '';
$form = '';
$form .= Form::open(null, ['id' => 'form']);
$form .= '<input type="hidden" name="' . $this->flextype['csrf']->getTokenNameKey() . '" value="' . $this->flextype['csrf']->getTokenName() . '">' .
$form .= '<input type="hidden" name="' . $this->flextype['csrf']->getTokenValueKey() . '" value="' .$this->flextype['csrf']->getTokenValue() . '">';
$form .= '<input type="hidden" name="' . $this->flextype['csrf']->getTokenValueKey() . '" value="' . $this->flextype['csrf']->getTokenValue() . '">';
$form .= Form::hidden('action', 'save-form');
if (count($fieldset['sections']) > 0) {
$form .= '<ul class="nav nav-pills nav-justified" id="pills-tab" role="tablist">';
foreach ($fieldset['sections'] as $key => $section) {
$form .= '<li class="nav-item">
<a class="nav-link '.(($key == 'main') ? 'active' : '') . '" id="pills-' . $key . '-tab" data-toggle="pill" href="#pills-' . $key . '" role="tab" aria-controls="pills-' . $key . '" aria-selected="true">' . $section['title'] . '</a>
<a class="nav-link ' . ($key === 'main' ? 'active' : '') . '" id="pills-' . $key . '-tab" data-toggle="pill" href="#pills-' . $key . '" role="tab" aria-controls="pills-' . $key . '" aria-selected="true">' . $section['title'] . '</a>
</li>';
}
$form .= '</ul>';
$form .= '<div class="tab-content" id="pills-tabContent">';
foreach ($fieldset['sections'] as $key => $section) {
$form .= '<div class="tab-pane fade show ' . (($key == 'main') ? 'active' : '') . '" id="pills-' . $key . '" role="tabpanel" aria-labelledby="pills-' . $key . '-tab">';
$form .= '<div class="tab-pane fade show ' . ($key === 'main' ? 'active' : '') . '" id="pills-' . $key . '" role="tabpanel" aria-labelledby="pills-' . $key . '-tab">';
$form .= '<div class="row">';
foreach ($section['fields'] as $element => $property) {
// Create attributes
@@ -77,11 +74,11 @@ class Forms
$property['size'] = Arr::keyExists($property, 'size') ? $property['size'] : 'col-12';
// Create attribute value
$property['value'] = Arr::keyExists($property, 'value') ? $property['value'] : '';
$pos = strpos($element, '.');
$pos = strpos($element, '.');
if ($pos === false) {
$form_element_name = $element;
} else {
$form_element_name = str_replace(".", "][", "$element") . ']';
$form_element_name = str_replace('.', '][', "$element") . ']';
}
$pos = strpos($form_element_name, ']');
if ($pos !== false) {
@@ -96,54 +93,55 @@ class Forms
// Simple text-input, for multi-line fields.
case 'textarea':
$form_element = Form::textarea($element, $form_value, $property['attributes']);
break;
break;
// The hidden field is like the text field, except it's hidden from the content editor.
case 'hidden':
$form_element = Form::hidden($element, $form_value);
break;
break;
// A WYSIWYG HTML field.
case 'html':
$property['attributes']['class'] .= ' js-html-editor';
$form_element = Form::textarea($element, $form_value, $property['attributes']);
break;
$form_element = Form::textarea($element, $form_value, $property['attributes']);
break;
// Selectbox field
case 'select':
$form_element = Form::select($form_element_name, $property['options'], $form_value, $property['attributes']);
break;
break;
// Template select field for selecting entry template
case 'template_select':
if ($this->flextype['registry']->has('settings.theme')) {
$_templates_list = $this->flextype['themes']->getTemplates($this->flextype['registry']->get('settings.theme'));
$templates_list = [];
if (count($_templates_list) > 0) {
foreach ($_templates_list as $template) {
if ($template['type'] == 'file' && $template['extension'] == 'html') {
$templates_list[$template['basename']] = $template['basename'];
if ($template['type'] !== 'file' || $template['extension'] !== 'html') {
continue;
}
$templates_list[$template['basename']] = $template['basename'];
}
}
$form_element = Form::select($form_element_name, $templates_list, $form_value, $property['attributes']);
}
break;
break;
// Visibility select field for selecting entry visibility state
case 'visibility_select':
$form_element = Form::select($form_element_name, ['draft' => __('admin_entries_draft'), 'visible' => __('admin_entries_visible'), 'hidden' => __('admin_entries_hidden')], (!empty($form_value) ? $form_value : 'visible'), $property['attributes']);
break;
$form_element = Form::select($form_element_name, ['draft' => __('admin_entries_draft'), 'visible' => __('admin_entries_visible'), 'hidden' => __('admin_entries_hidden')], (! empty($form_value) ? $form_value : 'visible'), $property['attributes']);
break;
// Media select field
case 'media_select':
//$form_element = Form::select($form_element_name, $this->getMediaList($request->getQueryParams()['id'], false), $form_value, $property['attributes']);
break;
break;
// Simple text-input, for single-line fields.
default:
$form_element = Form::input($form_element_name, $form_value, $property['attributes']);
break;
break;
}
// Render form elments with labels
if ($property['type'] == 'hidden') {
if ($property['type'] === 'hidden') {
$form .= $form_element;
} else {
$form .= '<div class="form-group ' . $property['size'] . '">';
@@ -157,6 +155,7 @@ class Forms
$form .= '</div>';
}
$form .= Form::close();
return $form;
}
}