Completed
Push — develop ( bfc0af...88d3fd )
by
unknown
08:28
created

FormCollectionContainer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 6
c 1
b 1
f 0
lcom 1
cbo 2
dl 0
loc 84
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 8 2
A setDisplayRemoveButton() 0 6 1
B render() 0 37 3
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
namespace Core\Form\View\Helper;
11
12
use Core\Form\CollectionContainer;
13
use Zend\Form\View\Helper\AbstractHelper;
14
15
/**
16
 * Helper for rendering form collection containers
17
 *
18
 * @author fedys
19
 */
20
class FormCollectionContainer extends AbstractHelper
21
{
22
    
23
    /**
24
     * @var bool
25
     */
26
    protected $displayRemoveButton = true;
27
28
    /**
29
     * Invoke as function.
30
     *
31
     * Proxies to {@link render()} or returns self.
32
     *
33
     * @param  null|CollectionContainer $container
34
     * @param string $layout
35
     * @param array $parameter
36
     * @return FormCollectionContainer|string
37
     */
38
    public function __invoke(CollectionContainer $container = null, $layout = Form::LAYOUT_HORIZONTAL, $parameter = [])
39
    {
40
        if (!$container) {
41
            return $this;
42
        }
43
        
44
        return $this->render($container, $layout, $parameter);
45
    }
46
    
47
    /**
48
     * Renders the forms of a container.
49
     *
50
     * @param CollectionContainer $container
51
     * @param string $layout
52
     * @param array $parameter
53
     * @return string
54
     */
55
    public function render(CollectionContainer $container, $layout = Form::LAYOUT_HORIZONTAL, $parameter = [])
56
    {
57
        $view = $this->getView();
58
        $view->headscript()
59
            ->appendFile($view->basePath('Core/js/jquery.formcollection-container.js'));
60
        $translator = $this->getTranslator();
61
        $formContainerHelper = $view->formContainer();
0 ignored issues
show
Bug introduced by
The method formContainer() does not seem to exist on object<Zend\View\Renderer\RendererInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
        $formsMarkup = '';
63
		$formTemplateWrapper = '<div class="form-collection-container-form">
64
            '. ($this->displayRemoveButton ? '<button type="button" class="btn btn-sm btn-danger pull-right form-collection-container-remove-button">' . $translator->translate('Remove') . '</button>' : '') . '
65
            %s
66
        </div>';
67
        
68
        foreach ($container as $form) /* @var $form \Zend\Form\Form */
69
        {
70
            $formsMarkup .= sprintf($formTemplateWrapper, $formContainerHelper->renderElement($form, $layout, $parameter));
71
        }
72
        
73
        $templateForm = $container->getTemplateForm();
74
		$templateMarkup = sprintf(
75
            $view->formCollection()->getTemplateWrapper(),
0 ignored issues
show
Bug introduced by
The method formCollection() does not seem to exist on object<Zend\View\Renderer\RendererInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
            $view->escapeHtmlAttr(sprintf($formTemplateWrapper, $formContainerHelper->renderElement($templateForm, $layout, $parameter)))
77
        );
78
        
79
		return sprintf('<div class="form-collection-container" data-new-entry-key="%s" data-remove-action="%s" data-remove-question="%s">
80
                <h3>%s</h3>
81
                %s%s%s
82
            </div>',
83
		    CollectionContainer::NEW_ENTRY,
84
            $container->formatAction('remove'),
85
            $translator->translate('Really remove?'),
86
            $container->getLabel(),
87
            $formsMarkup,
88
            $templateMarkup,
89
            '<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>'
90
        );
91
    }
92
    /**
93
	 * @param boolean $displayRemoveButton
94
	 * @return FormCollectionContainer
95
     * @since 0.26
96
	 */
97
	public function setDisplayRemoveButton($displayRemoveButton)
98
	{
99
		$this->displayRemoveButton = (bool)$displayRemoveButton;
100
		
101
		return $this;
102
	}
103
}
104