1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-23 05:16:03 +02:00

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

This commit is contained in:
Awilum
2019-08-23 16:26:43 +03:00
parent 924e1b52ab
commit b309068482
2 changed files with 169 additions and 0 deletions

162
flextype/core/Forms.php Normal file
View File

@@ -0,0 +1,162 @@
<?php
declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
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 Psr\Http\Message\ServerRequestInterface as Request;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
class Forms
{
/**
* Flextype Dependency Container
*/
private $flextype;
/**
* Constructor
*
* @access public
*/
public function __construct($flextype)
{
$this->flextype = $flextype;
}
/**
* Fetch Fieldset form
*
* @access public
* @param array $fieldset Fieldset
* @param string $values Fieldset values
* @return string Returns form based on fieldsets
*/
public function fetch(array $fieldset, array $values = [], Request $request) : string
{
$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 .= 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>
</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="row">';
foreach ($section['fields'] as $element => $property) {
// Create attributes
$property['attributes'] = Arr::keyExists($property, 'attributes') ? $property['attributes'] : [];
// Create attribute class
$property['attributes']['class'] = Arr::keyExists($property, 'attributes.class') ? 'form-control ' . $property['attributes']['class'] : 'form-control';
// Create attribute size
$property['size'] = Arr::keyExists($property, 'size') ? $property['size'] : 'col-12';
// Create attribute value
$property['value'] = Arr::keyExists($property, 'value') ? $property['value'] : '';
$pos = strpos($element, '.');
if ($pos === false) {
$form_element_name = $element;
} else {
$form_element_name = str_replace(".", "][", "$element") . ']';
}
$pos = strpos($form_element_name, ']');
if ($pos !== false) {
$form_element_name = substr_replace($form_element_name, '', $pos, strlen(']'));
}
// Form value
$form_value = Arr::keyExists($values, $element) ? Arr::get($values, $element) : $property['value'];
// Form label
$form_label = Form::label($element, __($property['title']));
// Form elements
switch ($property['type']) {
// Simple text-input, for multi-line fields.
case 'textarea':
$form_element = Form::textarea($element, $form_value, $property['attributes']);
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;
// A WYSIWYG HTML field.
case 'html':
$property['attributes']['class'] .= ' js-html-editor';
$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;
// 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'];
}
}
}
$form_element = Form::select($form_element_name, $templates_list, $form_value, $property['attributes']);
}
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;
// Media select field
case 'media_select':
//$form_element = Form::select($form_element_name, $this->getMediaList($request->getQueryParams()['id'], false), $form_value, $property['attributes']);
break;
// Simple text-input, for single-line fields.
default:
$form_element = Form::input($form_element_name, $form_value, $property['attributes']);
break;
}
// Render form elments with labels
if ($property['type'] == 'hidden') {
$form .= $form_element;
} else {
$form .= '<div class="form-group ' . $property['size'] . '">';
$form .= $form_label . $form_element;
$form .= '</div>';
}
}
$form .= '</div>';
$form .= '</div>';
}
$form .= '</div>';
}
$form .= Form::close();
return $form;
}
}

View File

@@ -174,6 +174,13 @@ $flextype['fieldsets'] = static function ($container) use ($flextype) {
return new Fieldsets($flextype);
};
/**
* Add forms service to Flextype container
*/
$flextype['forms'] = static function ($container) use ($flextype) {
return new Forms($flextype);
};
/**
* Add snippets service to Flextype container
*/