|
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
|
|
|
/** Core forms view helpers */ |
|
11
|
|
|
namespace Core\Form\View\Helper; |
|
12
|
|
|
|
|
13
|
|
|
use Core\Form\ViewPartialProviderInterface; |
|
14
|
|
|
use Core\Form\ExplicitParameterProviderInterface; |
|
15
|
|
|
use Core\Form\Element\ViewHelperProviderInterface; |
|
16
|
|
|
use Core\Form\Container; |
|
17
|
|
|
use Zend\Form\View\Helper\AbstractHelper; |
|
18
|
|
|
use Core\Form\SummaryForm as CoreSummaryForm; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Helper for rendering form containers |
|
22
|
|
|
* |
|
23
|
|
|
* @author Mathias Gelhausen <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class FormContainer extends AbstractHelper |
|
26
|
|
|
{ |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Invoke as function. |
|
30
|
|
|
* |
|
31
|
|
|
* Proxies to {@link render()} or returns self. |
|
32
|
|
|
* |
|
33
|
|
|
* @param null|Container $container |
|
34
|
|
|
* @param string $layout |
|
35
|
|
|
* @param array $parameter |
|
36
|
|
|
* @return FormContainer|string |
|
37
|
|
|
*/ |
|
38
|
|
View Code Duplication |
public function __invoke(Container $container = null, $layout = Form::LAYOUT_HORIZONTAL, $parameter = array()) |
|
|
|
|
|
|
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 Container $container |
|
51
|
|
|
* @param string $layout |
|
52
|
|
|
* @param array $parameter |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
public function render(Container $container, $layout = Form::LAYOUT_HORIZONTAL, $parameter = array()) |
|
56
|
|
|
{ |
|
57
|
|
|
|
|
58
|
|
|
$content = ''; |
|
59
|
|
|
|
|
60
|
|
|
$content .= $container->renderPre($this->getView()); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
if ($container instanceof ViewPartialProviderInterface) { |
|
63
|
|
|
return $this->getView()->partial($container->getViewPartial(), array('element' => $container)); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if (isset($parameter['render_label']) && $parameter['render_label'] && ($label = $container->getLabel())) { |
|
67
|
|
|
$content .= '<div class="container-headline"><h3>' . $this->getView()->translate($label) . '</h3></div>'; |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
foreach ($container as $element) { |
|
70
|
|
|
$content .= $this->renderElement($element, $layout, $parameter); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$content .= $container->renderPost($this->getView()); |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
return $content; |
|
76
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function renderElement($element, $layout, $parameter) |
|
80
|
|
|
{ |
|
81
|
|
|
$parameterPartial = $parameter; |
|
82
|
|
|
$content = ''; |
|
83
|
|
|
if ($element instanceof ExplicitParameterProviderInterface) { |
|
84
|
|
|
$parameterPartial = array_merge($element->getParams(), $parameterPartial); |
|
85
|
|
|
} |
|
86
|
|
|
if ($element instanceof ViewPartialProviderInterface) { |
|
87
|
|
|
$parameterPartial = array_merge(array('element' => $element, 'layout' => $layout), $parameterPartial); |
|
88
|
|
|
$content .= $this->getView()->partial( |
|
|
|
|
|
|
89
|
|
|
$element->getViewPartial(), |
|
90
|
|
|
$parameterPartial |
|
91
|
|
|
); |
|
92
|
|
|
|
|
93
|
|
|
} elseif ($element instanceof ViewHelperProviderInterface) { |
|
94
|
|
|
$helper = $element->getViewHelper(); |
|
95
|
|
|
if (is_string($helper)) { |
|
96
|
|
|
$helper = $this->getView()->plugin($helper); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
$content .= $helper($element); |
|
99
|
|
|
} elseif ($element instanceof CoreSummaryForm) { |
|
100
|
|
|
$content .= $this->getView()->summaryForm($element); |
|
|
|
|
|
|
101
|
|
|
} elseif ($element instanceof Container) { |
|
102
|
|
|
$content.= $this->render($element, $layout, $parameter); |
|
103
|
|
|
} else { |
|
104
|
|
|
$content.= $this->getView()->form($element, $layout, $parameter); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $content; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.