|
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 Zend\Form\FieldsetInterface; |
|
14
|
|
|
use Zend\Form\FormInterface; |
|
15
|
|
|
use Zend\Form\View\Helper\Form; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* ${CARET} |
|
19
|
|
|
* |
|
20
|
|
|
* @author Mathias Gelhausen <[email protected]> |
|
21
|
|
|
* @todo write test |
|
22
|
|
|
*/ |
|
23
|
|
|
class SearchForm extends Form |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Invoke as function |
|
28
|
|
|
* |
|
29
|
|
|
* @param null|FormInterface $form |
|
30
|
|
|
* @param array $colMap |
|
31
|
|
|
* @return Form|string |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __invoke(FormInterface $form = null, $colMap=[]) |
|
34
|
|
|
{ |
|
35
|
|
|
if (!$form) { |
|
36
|
|
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return $this->render($form, $colMap); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function render(FormInterface $form, $colMap=[]) |
|
43
|
|
|
{ |
|
44
|
|
|
if (method_exists($form, 'prepare')) { |
|
45
|
|
|
$form->prepare(); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$params = $this->getView()->params()->fromQuery(); |
|
|
|
|
|
|
49
|
|
|
$form->setAttribute('data-search-params', \Zend\Json\Json::encode($params)); |
|
50
|
|
|
|
|
51
|
|
|
$formContent = '<div class="row" style="padding: 0 15px;">'; $buttonsRendered = false; |
|
52
|
|
|
$buttonsContent = '<input type="submit" class="btn btn-primary" name="submit" value="' . $this->getView()->translate('Search') . '">' |
|
|
|
|
|
|
53
|
|
|
. '<input type="reset" class="btn btn-default" name="clear" value="' . $this->getView()->translate('Clear') . '">'; |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
if (empty($colMap)) { |
|
57
|
|
|
$c = count($form); |
|
58
|
|
|
$r = floor($c / 3); |
|
59
|
|
|
|
|
60
|
|
|
if (0 != $r) { |
|
61
|
|
|
for ($i=0; $i<$r; $i+=1) { |
|
62
|
|
|
$colMap[] = 4; |
|
63
|
|
|
$colMap[] = 4; |
|
64
|
|
|
$colMap[] = 4; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
if ($l = $c % 3) { |
|
68
|
|
|
for ($i=0; $i<$l; $i+=1) { |
|
69
|
|
|
$colMap[] = 12 / $l; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$i = 0; |
|
75
|
|
|
foreach ($form as $element) { |
|
76
|
|
|
if ($element instanceof FieldsetInterface) { |
|
77
|
|
|
trigger_error('Fieldsets are not allowed in a search form.', E_USER_NOTICE); |
|
78
|
|
|
continue; |
|
79
|
|
|
} else { |
|
80
|
|
|
$col = isset($colMap[$element->getName()]) |
|
81
|
|
|
? $colMap[$element->getName()] |
|
82
|
|
|
: (isset($colMap[$i]) ? $colMap[$i] : 4); |
|
83
|
|
|
$i += 1; |
|
84
|
|
|
|
|
85
|
|
|
if ($element->getName() == $form->getOption('button_element')) { |
|
86
|
|
|
$formContent.='<div class="input-group col-md-' . $col . '">' |
|
87
|
|
|
. $this->getView()->formElement($element) |
|
|
|
|
|
|
88
|
|
|
. '<div class="input-group-btn search-form-buttons" style="width: 0px;">' . $buttonsContent . '</div>' |
|
89
|
|
|
. '</div>'; |
|
90
|
|
|
$buttonsRendered = true; |
|
91
|
|
|
} else { |
|
92
|
|
|
$formContent .= '<div class="input-group col-md-' . $col . '">' . $this->getView()->formElement($element) . '</div>'; |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if (!$buttonsRendered) { |
|
98
|
|
|
$c = count($form); |
|
99
|
|
|
$col = isset($colMap[$c]) ? $colMap[$c]: 12; |
|
100
|
|
|
$formContent .= '<div class="input-group search-form-buttons col-md-' . $col . ' text-right">' |
|
101
|
|
|
. '<div class="btn-group">' . $buttonsContent .'</div></div>'; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $this->openTag($form) . $formContent . $this->closeTag(); |
|
105
|
|
|
} |
|
106
|
|
|
} |
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: