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

Merge branch '#218' into dev

This commit is contained in:
Awilum
2019-08-28 22:31:35 +03:00
10 changed files with 417 additions and 159 deletions

View File

@@ -101,7 +101,6 @@ class Entries
{
// If requested entry file founded then process it
if ($_entry = $this->read($id)) {
// Create unique entry cache_id
// Entry Cache ID = entry + entry file + entry file time stamp
if ($timestamp = Filesystem::getTimestamp($_entry['file_path'])) {
@@ -466,7 +465,7 @@ class Entries
/**
* Read entry
*
* @param string $id Entry ID
* @param string $id Entry ID
* @param bool $raw Return raw or decoded content
*
* @return bool|array Array of file path, decoded file data and file parser,

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

@@ -0,0 +1,371 @@
<?php
declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use Flextype\Component\Arr\Arr;
use Flextype\Component\Form\Form;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use function count;
use function Flextype\Component\I18n\__;
use function str_replace;
use function strlen;
use function strpos;
use function substr_replace;
class Forms
{
/**
* Flextype Dependency Container
*
* @var
* @access private
*/
private $flextype;
/**
* Sizes
*
* @var array
* @access private
*/
private $sizes = [
'1/12' => 'col-1',
'2/12' => 'col-2',
'3/12' => 'col-3',
'4/12' => 'col-4',
'5/12' => 'col-5',
'6/12' => 'col-6',
'7/12' => 'col-7',
'8/12' => 'col-8',
'9/12' => 'col-9',
'10/12' => 'col-19',
'12/12' => 'col-11',
'12' => 'col-12',
];
/**
* Field class
*
* @var string
* @access private
*/
private $field_class = 'form-control';
/**
* Constructor
*
* @access public
*/
public function __construct($flextype)
{
$this->flextype = $flextype;
}
/**
* Render form
*
* @param array $fieldset Fieldset
* @param array $values Fieldset values
* @param Request $request PSR7 request
* @param Response $response PSR7 response
*
* @return string Returns form based on fieldsets
*
* @access public
*/
public function render(array $fieldset, array $values = [], Request $request, Response $response) : string
{
$form = Form::open(null, ['id' => 'form']);
$form .= $this->_csrfHiddenField();
$form .= $this->_actionHiddenField();
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') ? $this->field_class . ' ' . $property['attributes']['class'] : $this->field_class;
// Create attribute size
$property['size'] = Arr::keyExists($property, 'size') ? $this->sizes[$property['size']] : $this->sizes['12'];
// Create attribute value
$property['value'] = Arr::keyExists($property, 'value') ? $property['value'] : '';
// Create attribute value
$property['label'] = Arr::keyExists($property, 'label') ? $property['label'] : true;
// Set element name
$element_name = $this->getElementName($element);
// Set element value
$form_value = Arr::keyExists($values, $element) ? Arr::get($values, $element) : $property['value'];
// Set form element
$form_element = '';
// Form elements
switch ($property['type']) {
// Simple text-input, for multi-line fields.
case 'textarea':
$form_element = $this->textareaField($element_name, $form_value, $property);
break;
// The hidden field is like the text field, except it's hidden from the content editor.
case 'hidden':
$form_element = $this->hiddenField($element_name, $form_value, $property);
break;
// A WYSIWYG HTML field.
case 'html':
$form_element = $this->htmlField($element_name, $form_value, $property);
break;
// Selectbox field
case 'select':
$form_element = $this->selectField($element_name, $property['options'], $form_value, $property);
break;
// Template select field for selecting entry template
case 'template_select':
$form_element = $this->templateSelectField($element_name, [], $form_value, $property);
break;
// Visibility select field for selecting entry visibility state
case 'visibility_select':
$form_element = $this->visibilitySelectField($element_name, ['draft' => __('admin_entries_draft'), 'visible' => __('admin_entries_visible'), 'hidden' => __('admin_entries_hidden')], (! empty($form_value) ? $form_value : 'visible'), $property);
break;
// Simple text-input, for single-line fields.
default:
$form_element = $this->textField($element_name, $form_value, $property);
break;
}
if ($property['label'] === true) {
$form_label = Form::label($element, __($property['title']));
} else {
$form_label = '';
}
$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;
}
/**
* Get element name
*
* @param string $element Element
*
* @return string Returns form element name
*
* @access protected
*/
protected function getElementName(string $element) : string
{
$pos = strpos($element, '.');
if ($pos === false) {
$element_name = $element;
} else {
$element_name = str_replace('.', '][', "$element") . ']';
}
$pos = strpos($element_name, ']');
if ($pos !== false) {
$element_name = substr_replace($element_name, '', $pos, strlen(']'));
}
return $element_name;
}
/**
* Template select field
*
* @param string $name Field name
* @param array $options Field options
* @param string $value Field value
* @param array $property Field property
*
* @return string Returns field
*
* @access protected
*/
protected function templateSelectField($name, $options, $value, $property)
{
$_templates_list = $this->flextype['themes']->getTemplates($this->flextype['registry']->get('settings.theme'));
$options = [];
if (count($_templates_list) > 0) {
foreach ($_templates_list as $template) {
if ($template['type'] !== 'file' || $template['extension'] !== 'html') {
continue;
}
$options[$template['basename']] = $template['basename'];
}
}
return Form::select($name, $options, $value, $property['attributes']);
}
/**
* Select field
*
* @param string $name Field name
* @param array $options Field options
* @param string $value Field value
* @param array $property Field property
*
* @return string Returns field
*
* @access protected
*/
protected function selectField($name, $options, $value, $property)
{
return Form::select($name, $options, $value, $property['attributes']);
}
/**
* Html field
*
* @param string $name Field name
* @param string $value Field value
* @param array $property Field property
*
* @return string Returns field
*
* @access protected
*/
protected function htmlField($name, $value, $property)
{
$property['attributes']['class'] .= ' js-html-editor';
return Form::textarea($name, $value, $property['attributes']);
}
/**
* Hidden field
*
* @param string $name Field name
* @param string $value Field value
* @param array $property Field property
*
* @return string Returns field
*
* @access protected
*/
protected function hiddenField($name, $value, $property)
{
return Form::hidden($name, $value, $property['attributes']);
}
/**
* Textarea field
*
* @param string $name Field name
* @param string $value Field value
* @param array $property Field property
*
* @return string Returns field
*
* @access protected
*/
protected function textareaField($name, $value, $property)
{
return Form::textarea($name, $value, $property['attributes']);
}
/**
* Visibility field
*
* @param string $name Field name
* @param array $options Field options
* @param string $value Field value
* @param array $property Field property
*
* @return string Returns field
*
* @access protected
*/
protected function visibilitySelectField($name, $options, $value, $property)
{
return Form::select($name, $options, $value, $property['attributes']);
}
/**
* Text field
*
* @param string $name Field name
* @param string $value Field value
* @param array $property Field property
*
* @return string Returns field
*
* @access protected
*/
protected function textField($name, $value, $property)
{
return Form::input($name, $value, $property['attributes']);
}
/**
* _csrfHiddenField
*
* @return string Returns field
*
* @access protected
*/
protected function _csrfHiddenField()
{
$field = '<input type="hidden" name="' . $this->flextype['csrf']->getTokenNameKey() . '" value="' . $this->flextype['csrf']->getTokenName() . '">';
$field .= '<input type="hidden" name="' . $this->flextype['csrf']->getTokenValueKey() . '" value="' . $this->flextype['csrf']->getTokenValue() . '">';
return $field;
}
/**
* _actionHiddenField
*
* @return string Returns field
*
* @access protected
*/
protected function _actionHiddenField()
{
return Form::hidden('action', 'save-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
*/

View File

@@ -7,33 +7,33 @@ sections:
title:
title: admin_title
type: text
size: col-12
size: 12
content:
title: admin_content
type: html
size: col-12
size: 12
image:
title: admin_media
type: media_select
size: col-3
size: 3/12
seo:
title: Seo
fields:
description:
title: admin_description
type: textarea
size: col-12
size: 12
menu:
title: Menu
fields:
menu_item_title:
title: admin_menu_item_title
type: text
size: col-4
size: 4/12
menu_item_url:
title: admin_menu_item_url
type: text
size: col-4
size: 4/12
menu_item_target:
title: admin_menu_item_target
type: select
@@ -42,8 +42,8 @@ sections:
_blank: _blank
_parent: _parent
_top: _top
size: col-4
size: 4/12
menu_item_order:
title: admin_menu_item_order
type: text
size: col-4
size: 4/12

View File

@@ -7,19 +7,19 @@ sections:
title:
title: admin_title
type: text
size: col-12
size: 12
summary:
title: admin_summary
type: html
size: col-12
size: 12
content:
title: admin_content
type: html
size: col-12
size: 12
seo:
title: Seo
fields:
description:
title: admin_description
type: textarea
size: col-12
size: 12

View File

@@ -7,29 +7,29 @@ sections:
title:
title: admin_title
type: text
size: col-12
size: 12
entries_limit:
title: admin_entries_limit
type: text
size: col-4
size: 4/12
seo:
title: Seo
fields:
description:
title: admin_description
type: textarea
size: col-12
size: 4/12
menu:
title: Menu
fields:
menu_item_title:
title: admin_menu_item_title
type: text
size: col-4
size: 4/12
menu_item_url:
title: admin_menu_item_url
type: text
size: col-4
size: 4/12
menu_item_target:
title: admin_menu_item_target
type: select
@@ -38,8 +38,8 @@ sections:
_blank: _blank
_parent: _parent
_top: _top
size: col-4
size: 4/12
menu_item_order:
title: admin_menu_item_order
type: text
size: col-4
size: 4/12

View File

@@ -7,37 +7,37 @@ sections:
title:
title: admin_title
type: text
size: col-12
size: 12
content:
title: admin_content
type: html
size: col-12
size: 12
template:
title: admin_template
type: template_select
size: col-4
size: 4/12
visibility:
title: admin_visibility
type: visibility_select
size: col-4
size: 4/12
seo:
title: Seo
fields:
description:
title: admin_description
type: textarea
size: col-12
size: 12
menu:
title: Menu
fields:
menu_item_title:
title: admin_menu_item_title
type: text
size: col-4
size: 4/12
menu_item_url:
title: admin_menu_item_url
type: text
size: col-4
size: 4/12
menu_item_target:
title: admin_menu_item_target
type: select
@@ -46,8 +46,8 @@ sections:
_blank: _blank
_parent: _parent
_top: _top
size: col-4
size: 4/12
menu_item_order:
title: admin_menu_item_order
type: text
size: col-4
size: 4/12

View File

@@ -7,15 +7,15 @@ sections:
title:
title: admin_title
type: text
size: col-12
size: 12
gallery_img:
title: admin_gallery_img
type: media_select
size: col-4
size: 4/12
seo:
title: Seo
fields:
description:
title: admin_description
type: textarea
size: col-12
size: 12

View File

@@ -7,25 +7,25 @@ sections:
title:
title: admin_title
type: text
size: col-12
size: 12
seo:
title: Seo
fields:
description:
title: admin_description
type: textarea
size: col-12
size: 12
menu:
title: Menu
fields:
menu_item_title:
title: admin_menu_item_title
type: text
size: col-4
size: 4/12
menu_item_url:
title: admin_menu_item_url
type: text
size: col-4
size: 4/12
menu_item_target:
title: admin_menu_item_target
type: select
@@ -34,8 +34,8 @@ sections:
_blank: _blank
_parent: _parent
_top: _top
size: col-4
size: 4/12
menu_item_order:
title: admin_menu_item_order
type: text
size: col-4
size: 4/12

View File

@@ -567,125 +567,6 @@ class EntriesController extends Controller
return $response->withRedirect($this->router->pathFor('admin.entries.index') . '?id=' . implode('/', array_slice(explode("/", $id), 0, -1)));
}
/**
* Fetch Fieldset form
*
* @access public
* @param array $fieldset Fieldset
* @param string $values Fieldset values
* @return string Returns form based on fieldsets
*/
public function fetchForm(array $fieldset, array $values = [], Request $request) : string
{
$form = '';
$form .= Form::open(null, ['id' => 'form']);
$form .= '<input type="hidden" name="' . $this->csrf->getTokenNameKey() . '" value="' . $this->csrf->getTokenName() . '">' .
$form .= '<input type="hidden" name="' . $this->csrf->getTokenValueKey() . '" value="' . $this->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->registry->has('settings.theme')) {
$_templates_list = $this->themes->getTemplates($this->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;
}
/**
* Edit entry
*
@@ -816,7 +697,7 @@ class EntriesController extends Controller
'parts' => $parts,
'i' => count($parts),
'last' => Arr::last($parts),
'form' => $this->fetchForm($fieldsets, $entry, $request),
'form' => $this->forms->render($fieldsets, $entry, $request, $response),
'menu_item' => 'entries',
'links' => [
'entries' => [