Completed
Push — develop ( 69bf64...c61561 )
by
unknown
09:01
created

FormCollectionContainer::render()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 37
rs 8.8571
cc 2
eloc 23
nc 2
nop 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
     * Invoke as function.
25
     *
26
     * Proxies to {@link render()} or returns self.
27
     *
28
     * @param  null|CollectionContainer $container
29
     * @param string $layout
30
     * @param array $parameter
31
     * @return FormCollectionContainer|string
32
     */
33
    public function __invoke(CollectionContainer $container = null, $layout = Form::LAYOUT_HORIZONTAL, $parameter = [])
34
    {
35
        if (!$container) {
36
            return $this;
37
        }
38
        
39
        return $this->render($container, $layout, $parameter);
40
    }
41
    
42
    /**
43
     * Renders the forms of a container.
44
     *
45
     * @param CollectionContainer $container
46
     * @param string $layout
47
     * @param array $parameter
48
     * @return string
49
     */
50
    public function render(CollectionContainer $container, $layout = Form::LAYOUT_HORIZONTAL, $parameter = [])
51
    {
52
        $view = $this->getView();
53
        $view->headscript()
54
            ->appendFile($view->basePath('Core/js/jquery.formcollection-container.js'));
55
        $translator = $this->getTranslator();
56
        $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...
57
        $formsMarkup = '';
58
		$formTemplateWrapper = '<div class="form-collection-container-form">
59
            <button type="button" class="btn btn-sm btn-danger pull-right form-collection-container-remove-button">' . $translator->translate('Remove') . '</button>
60
            %s
61
        </div>';
62
        
63
        foreach ($container as $form) /* @var $form \Zend\Form\Form */
64
        {
65
            $formsMarkup .= sprintf($formTemplateWrapper, $formContainerHelper->renderElement($form, $layout, $parameter));
66
        }
67
        
68
        $templateForm = $container->getTemplateForm();
69
		$templateMarkup = sprintf(
70
            $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...
71
            $view->escapeHtmlAttr(sprintf($formTemplateWrapper, $formContainerHelper->renderElement($templateForm, $layout, $parameter)))
72
        );
73
        
74
		return sprintf('<div class="form-collection-container" data-new-entry-key="%s" data-remove-action="%s" data-remove-question="%s">
75
                <h3>%s</h3>
76
                %s%s%s
77
            </div>',
78
		    CollectionContainer::NEW_ENTRY,
79
            $container->formatAction('remove'),
80
            $translator->translate('Really remove?'),
81
            $container->getLabel(),
82
            $formsMarkup,
83
            $templateMarkup,
84
            '<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>'
85
        );
86
    }
87
}
88