|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @copyright 2013 - 2016 Cross Solution <http://cross-solution.de> |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** */ |
|
11
|
|
|
namespace Core\Form\View\Helper; |
|
12
|
|
|
|
|
13
|
|
|
use Core\Form\Element\ViewHelperProviderInterface; |
|
14
|
|
|
use Core\Form\TextSearchFormFieldset; |
|
15
|
|
|
use Core\Form\ViewPartialProviderInterface; |
|
16
|
|
|
use Zend\Form\FieldsetInterface; |
|
17
|
|
|
use Zend\Form\FormInterface; |
|
18
|
|
|
use Zend\Form\View\Helper\Form; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* ${CARET} |
|
22
|
|
|
* |
|
23
|
|
|
* @author Mathias Gelhausen <[email protected]> |
|
24
|
|
|
* @todo write test |
|
25
|
|
|
*/ |
|
26
|
|
|
class SearchForm extends Form |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Invoke as function |
|
31
|
|
|
* |
|
32
|
|
|
* @param null|FormInterface $form |
|
33
|
|
|
* @param array $colMap |
|
|
|
|
|
|
34
|
|
|
* @return Form|string |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __invoke(FormInterface $form = null, $colMap=null) |
|
37
|
|
|
{ |
|
38
|
|
|
if (!$form) { |
|
39
|
|
|
return $this; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $this->render($form, $colMap); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function render(FormInterface $form, $colMap=null, $buttonsSpan = null) |
|
46
|
|
|
{ |
|
47
|
|
|
$headscript = $this->getView()->plugin('headscript'); |
|
|
|
|
|
|
48
|
|
|
$basepath = $this->getView()->plugin('basepath'); |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
$headscript->appendFile($basepath('Core/js/core.searchform.js')); |
|
51
|
|
|
|
|
52
|
|
|
if (is_int($colMap)) { |
|
53
|
|
|
$buttonsSpan = $colMap; |
|
54
|
|
|
$colMap = null; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
View Code Duplication |
if ($form instanceOf ViewPartialProviderInterface) { |
|
|
|
|
|
|
58
|
|
|
return $this->getView()->partial($form->getViewPartial(), [ 'element' => $form, 'colMap' => $colMap, 'buttonsSpan' => $buttonsSpan ]); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$elements = $form->getElements(); |
|
62
|
|
|
$buttons = $form->getButtons(); |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
$content = $this->renderElements($elements, $buttons, $colMap, $buttonsSpan); |
|
65
|
|
|
|
|
66
|
|
|
return $this->openTag($form) |
|
67
|
|
|
. '<div class="row" style="padding: 0 15px;">' |
|
68
|
|
|
. $content . '</div>' . $this->closeTag(); |
|
69
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function renderButtons($fieldset) |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->renderElements($fieldset, true); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function renderElements($fieldset, $buttonsFieldset, $colMap = null, $buttonsSpan = null) |
|
78
|
|
|
{ |
|
79
|
|
View Code Duplication |
if ($fieldset instanceOf ViewPartialProviderInterface) { |
|
|
|
|
|
|
80
|
|
|
return $this->getView()->partial($fieldset->getViewPartial(), [ 'element' => $fieldset, 'colMap' => $colMap, 'buttonsSpan' => $buttonsSpan ]); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if (true !== $buttonsFieldset && null === $colMap) { |
|
84
|
|
|
$colMap = $fieldset->getColumnMap(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$formElement = $this->getView()->plugin('formElement'); |
|
|
|
|
|
|
88
|
|
|
$content = ''; $buttonsRendered = false; $i=0; |
|
89
|
|
|
foreach ($fieldset as $element) { |
|
90
|
|
|
if (true === $buttonsFieldset) { |
|
91
|
|
|
$content .= $formElement($element); |
|
92
|
|
|
continue; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if (isset($colMap[$element->getName()])) { |
|
96
|
|
|
$cols = $colMap[$element->getName()]; |
|
97
|
|
|
|
|
98
|
|
|
} else if (isset($colMap[$i])) { |
|
99
|
|
|
$cols = $colMap[$i]; |
|
100
|
|
|
|
|
101
|
|
|
} else { |
|
102
|
|
|
$cols = $element->getOption('span') ?: 12; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
if ($fieldset instanceOf TextSearchFormFieldset && $element->getName() == $fieldset->getButtonElement()) { |
|
106
|
|
|
$content.='<div class="input-group col-md-' . $cols . '">' |
|
107
|
|
|
. $formElement($element) |
|
108
|
|
|
. '<div class="input-group-btn search-form-buttons" style="width: 0px;">' |
|
109
|
|
|
. $this->renderElements($buttonsFieldset, true) . '</div>' |
|
110
|
|
|
. '</div>'; |
|
111
|
|
|
$buttonsRendered = true; |
|
112
|
|
|
} else { |
|
113
|
|
|
$content .= '<div class="input-group col-md-' . $cols . '">' |
|
114
|
|
|
. $formElement($element) |
|
115
|
|
|
. '</div>'; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$i += 1; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
if (true !== $buttonsFieldset && !$buttonsRendered) { |
|
122
|
|
|
if (null === $buttonsSpan) { |
|
123
|
|
|
$buttonsSpan = $buttonsFieldset->getSpan(); |
|
124
|
|
|
} |
|
125
|
|
|
$content .= '<div class="input-group search-form-buttons col-md-' . $buttonsSpan . ' text-right">' |
|
126
|
|
|
. '<div class="btn-group">' . $this->renderElements($buttonsFieldset, true) .'</div></div>'; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return $content; |
|
130
|
|
|
} |
|
131
|
|
|
} |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: