Skip to content

Commit

Permalink
Merge pull request #261 into develop. Implements #258
Browse files Browse the repository at this point in the history
  • Loading branch information
cbleek committed Jul 26, 2016
2 parents bfc0afe + d2db541 commit 88d3fd6
Show file tree
Hide file tree
Showing 25 changed files with 499 additions and 78 deletions.
4 changes: 2 additions & 2 deletions less/include/forms.less
Expand Up @@ -2,14 +2,14 @@
.sf-summary {
position: relative;

.sf-edit {
.sf-controls {
position: absolute;
top: 0;
right: 0;
display: none;
}
}
.sf-summary:hover .sf-edit {
.sf-summary:hover .sf-controls {
display: block;
}
}
Expand Down
3 changes: 1 addition & 2 deletions module/Auth/src/Auth/Form/UserInfo.php
Expand Up @@ -10,7 +10,6 @@
/** */
namespace Auth\Form;

use Core\Form\EmptySummaryAwareInterface;
use Core\Form\SummaryForm;

/**
Expand All @@ -31,7 +30,7 @@ class UserInfo extends SummaryForm

public function init()
{
$this->setLabel(/*@translate*/ 'personal informations');
$this->setLabel(/*@translate*/ 'Personal informations');
parent::init();
}
}
40 changes: 5 additions & 35 deletions module/Auth/src/Auth/Form/UserInfoFieldset.php
Expand Up @@ -11,6 +11,7 @@

use Core\Entity\Hydrator\EntityHydrator;
use Core\Form\EmptySummaryAwareInterface;
use Core\Form\EmptySummaryAwareTrait;
use Zend\Form\Fieldset;
use Core\Form\ViewPartialProviderInterface;
use Zend\InputFilter\InputFilterProviderInterface;
Expand All @@ -25,18 +26,14 @@ class UserInfoFieldset extends Fieldset implements
InputFilterProviderInterface
{

use EmptySummaryAwareTrait;

/**
* View script for rendering
*
* @var string
*/
protected $viewPartial = 'form/auth/contact';
/**
* The empty summary notice.
*
* @var string
*/
protected $emptySummaryNotice = /*@translate*/ 'Click here to enter contact informations.';

/**
* @param String $partial
Expand Down Expand Up @@ -236,38 +233,11 @@ public function getInputFilterSpecification()
);
}

/**
* If all elements have empty values, the form will be displayed collapsed with the EmptySummaryNotice
*
* @return bool
*/
public function isSummaryEmpty()
{
foreach ($this as $element) { /* @var $element \Zend\Form\ElementInterface */
if ('' != $element->getValue()) {
return false;
}
}
return true;
}

/**
* @param $message
*
* @return $this
*/
public function setEmptySummaryNotice($message)
{
$this->emptySummaryNotice = $message;
return $this;
}

/**
* @return string
*/
public function getEmptySummaryNotice()
protected function getDefaultEmptySummaryNotice()
{
return $this->emptySummaryNotice;
return /*@translate*/ 'Click here to enter contact informations.';
}

}
20 changes: 14 additions & 6 deletions module/Core/public/js/jquery.formcollection-container.js
Expand Up @@ -19,11 +19,11 @@
.fadeIn();

// set key in case of valid insertion
$form.on('done.yk.core.forms', function(event, data) {
$form.on('yk:forms:success', function(event, data) {
if ('valid' in data.data && data.data.valid) {
var $newForm = $(data.data.content).find('form[data-entry-key]'),
key = $newForm.attr('data-entry-key');
$form.attr('action', $newForm.attr('action'))
var key = $('<div>' + data.data.content + '</div>').find('*[data-entry-key]')
.attr('data-entry-key');
$form.attr('action', $form.attr('action').replace(new RegExp($collectionContainer.data('new-entry-key')), key))
.attr('data-entry-key', key)
.data('entry-key', key);
}
Expand Down Expand Up @@ -82,8 +82,16 @@
};

var initRemoveButtons = function (collection) {
collection.find('.form-collection-container-remove-button')
.on('click.formcollectionContainer', removeButtonClickListener);
var event = 'click.formcollectionContainer',
buttonSelector = '.form-collection-container-remove-button';
collection.find(buttonSelector)
.on(event, removeButtonClickListener);
collection.find('form').on('yk:forms:success', function () {
$(this).closest('.form-collection-container-form')
.find(buttonSelector)
.off(event)
.on(event, removeButtonClickListener);
})
};

$.fn.formcollectionContainer = function( ) {
Expand Down
7 changes: 6 additions & 1 deletion module/Core/src/Core/Form/CollectionContainer.php
Expand Up @@ -222,6 +222,11 @@ protected function buildForm($key, $entry = null)
protected function setupForm(CoreForm $form, $key)
{
$form->setAttribute('action', sprintf('?form=%s', $this->formatAction($key)))
->setAttribute('data-entry-key', $key);
->setAttribute('data-entry-key', $key)
->setOption('control_buttons', [[
'class' => 'btn-danger form-collection-container-remove-button',
'icon' => 'delete-o',
'label' => /*@translate */ 'Remove',
]]);
}
}
67 changes: 67 additions & 0 deletions module/Core/src/Core/Form/EmptySummaryAwareTrait.php
@@ -0,0 +1,67 @@
<?php
/**
* YAWIK
*
* @filesource
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
* @license MIT
*/
namespace Core\Form;

/**
*
* @author fedys
* @since 0.26
*/
trait EmptySummaryAwareTrait
{

/**
* The empty summary notice.
*
* @var string
*/
protected $emptySummaryNotice;

/**
* @see \Core\Form\EmptySummaryAwareInterface::isSummaryEmpty()
*/
public function isSummaryEmpty()
{
foreach ($this as $element) { /* @var $element \Zend\Form\ElementInterface */
if ($element->getValue()) {
return false;
}
}
return true;
}

/**
* @see \Core\Form\EmptySummaryAwareInterface::setEmptySummaryNotice()
*/
public function setEmptySummaryNotice($message)
{
$this->emptySummaryNotice = $message;
return $this;
}

/**
* @see \Core\Form\EmptySummaryAwareInterface::getEmptySummaryNotice()
*/
public function getEmptySummaryNotice()
{
if (! isset($this->emptySummaryNotice)) {
$this->emptySummaryNotice = $this->getDefaultEmptySummaryNotice();
}

return $this->emptySummaryNotice;
}

/**
* @return string
*/
protected function getDefaultEmptySummaryNotice()
{
return '';
}
}
20 changes: 18 additions & 2 deletions module/Core/src/Core/Form/View/Helper/FormCollectionContainer.php
Expand Up @@ -19,6 +19,11 @@
*/
class FormCollectionContainer extends AbstractHelper
{

/**
* @var bool
*/
protected $displayRemoveButton = true;

/**
* Invoke as function.
Expand Down Expand Up @@ -56,7 +61,7 @@ public function render(CollectionContainer $container, $layout = Form::LAYOUT_HO
$formContainerHelper = $view->formContainer();
$formsMarkup = '';
$formTemplateWrapper = '<div class="form-collection-container-form">
<button type="button" class="btn btn-sm btn-danger pull-right form-collection-container-remove-button">' . $translator->translate('Remove') . '</button>
'. ($this->displayRemoveButton ? '<button type="button" class="btn btn-sm btn-danger pull-right form-collection-container-remove-button">' . $translator->translate('Remove') . '</button>' : '') . '
%s
</div>';

Expand All @@ -81,7 +86,18 @@ public function render(CollectionContainer $container, $layout = Form::LAYOUT_HO
$container->getLabel(),
$formsMarkup,
$templateMarkup,
'<div class="form-collection-container-add-wrapper"><button type="button" class="btn btn-success form-collection-container-add-button">' . sprintf($translator->translate('Add %s'), $container->getLabel()) . '</button></div>'
'<div class="form-collection-container-add-wrapper"><button type="button" class="btn btn-success form-collection-container-add-button"><span class="yk-icon yk-icon-plus"></span> ' . sprintf($translator->translate('Add %s'), $container->getLabel()) . '</button></div>'
);
}
/**
* @param boolean $displayRemoveButton
* @return FormCollectionContainer
* @since 0.26
*/
public function setDisplayRemoveButton($displayRemoveButton)
{
$this->displayRemoveButton = (bool)$displayRemoveButton;

return $this;
}
}
6 changes: 4 additions & 2 deletions module/Core/src/Core/Form/View/Helper/FormRow.php
Expand Up @@ -12,9 +12,7 @@
use Zend\Form\View\Helper\FormRow as ZendFormRow;
use Zend\Form\ElementInterface;
use Core\Form\ViewPartialProviderInterface;
use Core\Form\Element\ViewhelperProviderInterface;
use Zend\Form\Element\Button;
use Zend\Form\Element\Select;

class FormRow extends ZendFormRow
{
Expand Down Expand Up @@ -74,6 +72,10 @@ public function render(ElementInterface $element, $ignoreViewPartial = false)
$form_row_class = 'form-group';
}

if (($elementRowClass = $element->getOption('rowClass')) != '') {
$form_row_class .= ' '.$elementRowClass;
}

// Does this element have errors ?
if (!empty($elementErrors) && !empty($inputErrorClass) && $this->layout != Form::LAYOUT_BARE) {
$classAttributes = ($element->hasAttribute('class') ? $element->getAttribute('class') . ' ' : '');
Expand Down
38 changes: 31 additions & 7 deletions module/Core/src/Core/Form/View/Helper/SummaryForm.php
Expand Up @@ -163,20 +163,42 @@ public function renderForm(SummaryFormInterface $form, $layout = Form::LAYOUT_HO
*/
public function renderSummary(SummaryFormInterface $form)
{
$form->prepare();
$baseFieldset = $form->getBaseFieldset();
if (!isset($baseFieldset)) {
throw new \InvalidArgumentException('For the Form ' . get_class($form) . ' there is no Basefieldset');
}

$markup = '<div class="panel panel-default" style="min-height: 100px;">
<div class="panel-body">%s%s</div></div>';
$dataAttributesMarkup = '';

foreach ($form->getAttributes() as $dataKey => $dataValue)
{
if (preg_match('/^data-/', $dataKey))
{
$dataAttributesMarkup .= sprintf(' %s="%s"', $dataKey, $dataValue);
}
}

$markup = '<div class="panel panel-default" style="min-height: 100px;"' . $dataAttributesMarkup . '>
<div class="panel-body"><div class="sf-controls">%s</div>%s</div></div>';

$view = $this->getView();
$buttonMarkup = false === $form->getOption('editable')
? ''
: '<button type="button" class="pull-right btn btn-default btn-xs sf-edit">'
: '<button type="button" class="btn btn-default btn-xs sf-edit">'
. '<span class="yk-icon yk-icon-edit"></span> '
. $this->getView()->translate('Edit')
. $view->translate('Edit')
. '</button>';

if (($controlButtons = $form->getOption('control_buttons')) !== null)
{
$buttonMarkup .= PHP_EOL . implode(PHP_EOL, array_map(function (array $buttonSpec) use ($view) {
return '<button type="button" class="btn btn-default btn-xs' . (isset($buttonSpec['class']) ? ' ' . $buttonSpec['class'] : '') . '">'
. (isset($buttonSpec['icon']) ? '<span class="yk-icon yk-icon-' . $buttonSpec['icon'] . '"></span> ' : '')
. $view->translate($buttonSpec['label'])
. '</button>';
}, $controlButtons));
}

$elementMarkup = $this->renderSummaryElement($baseFieldset);

Expand All @@ -190,7 +212,7 @@ public function renderSummary(SummaryFormInterface $form)
* @param ElementInterface $element
* @return string
*/
protected function renderSummaryElement(ElementInterface $element)
public function renderSummaryElement(ElementInterface $element)
{
if ($element instanceof Hidden || false === $element->getOption('render_summary')) {
return '';
Expand Down Expand Up @@ -244,14 +266,15 @@ protected function renderSummaryElement(ElementInterface $element)
: $element->getValue();

if ('' != $elementValue && $element instanceof \Zend\Form\Element\Select) {
$generalOptGroupName = '__general__';
$options = $element->getValueOptions();
$translator = $this->getTranslator();
if (true == $element->getAttribute('multiple')) {

$multiOptions = [];
foreach ($elementValue as $optionKey) {
if (isset($options[$optionKey])) {
$multiOptions['__general__'][] = $translator->translate($options[$optionKey]);
$multiOptions[$generalOptGroupName][] = $translator->translate($options[$optionKey]);
continue;
}

Expand All @@ -264,8 +287,9 @@ protected function renderSummaryElement(ElementInterface $element)
}

$elementValue = [];
$numberOfmultiOptions = count($multiOptions);
foreach ($multiOptions as $optGroupLabel => $vals) {
$elementValue[] = "<b>$optGroupLabel</b><br>" . join(', ', $vals);
$elementValue[] = ($numberOfmultiOptions > 1 || $optGroupLabel !== $generalOptGroupName ? "<b>$optGroupLabel</b><br>" : '') . join(', ', $vals);
}
$elementValue = join('<br>', $elementValue) . '<br>';

Expand Down

0 comments on commit 88d3fd6

Please sign in to comment.