Completed
Push — develop ( 4bf430...c74260 )
by
unknown
18:30
created

SearchForm   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 181
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 6
dl 0
loc 181
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setButtonElement() 0 4 1
A getButtonElement() 0 4 1
A setColumnMap() 0 6 1
A getColumnMap() 0 18 4
B init() 0 16 5
A addElements() 0 4 1
B addTextElement() 0 24 2
A getButtons() 0 4 1
B addButton() 0 30 3
A setSearchParams() 0 11 2
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2017 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Core\Form;
12
13
use Zend\Form\Form as ZfForm;
14
use Zend\Json\Json;
15
use Zend\Stdlib\ArrayUtils;
16
use Zend\Stdlib\PriorityList;
17
18
/**
19
 * ${CARET}
20
 * 
21
 * @author Mathias Gelhausen <[email protected]>
22
 * @todo write test 
23
 */
24
class SearchForm extends ZfForm
25
{
26
    protected $attributes = [
27
        'class'          => 'form-inline search-form',
28
        'data-handle-by' => 'script',
29
        'method'         => 'get',
30
    ];
31
32
    /**
33
     *
34
     *
35
     * @var \Zend\Stdlib\PriorityList
36
     */
37
    protected $buttonsIterator;
38
39
    public function __construct($name = null, $options = [])
40
    {
41
        parent::__construct($name, $options);
42
        $this->buttonsIterator = new PriorityList();
43
        $this->buttonsIterator->isLIFO(false);
44
    }
45
46
    public function setButtonElement($name)
47
    {
48
        return $this->setOption('button_element', $name);
49
    }
50
51
    public function getButtonElement()
52
    {
53
        return $this->getOption('button_element');
54
    }
55
56
    /**
57
     * Sets the column map.
58
     *
59
     * @param array $map
60
     *
61
     * @see \Core\Form\View\Helper\SearchForm
62
     * @return self
63
     */
64
    public function setColumnMap($map)
65
    {
66
        $this->setOption('column_map', $map);
67
68
        return $this;
69
    }
70
71
    /**
72
     * Gets the column map.
73
     *
74
     * Generates the column map from the element options,
75
     * if none is set.
76
     *
77
     * @return array
78
     */
79
    public function getColumnMap()
80
    {
81
        $map = $this->getOption('column_map');
82
83
        if (null === $map) {
84
            $map = [];
85
            foreach ($this as $element) {
86
                $col = $element->getOption('span');
87
                if (null !== $col) {
88
                    $map[$element->getName()] = $col;
89
                }
90
            }
91
92
            $this->setOption('column_map', $map);
93
        }
94
95
        return $map;
96
    }
97
98
99
    public function init()
100
    {
101
        $this->addTextElement(
102
            $this->getOption('text_name') ?: /*@translate*/ 'q',
103
            $this->getOption('text_label') ?: /*@translate*/ 'Search',
104
            $this->getOption('text_placeholder') ?: /*@translate*/ 'Search query',
105
            $this->getOption('text_span') ?: 12,
106
            50,
107
            true
108
        );
109
110
        $this->addButton(/*@translate*/ 'Search', -1000, 'submit');
111
        $this->addButton(/*@translate*/ 'Clear', -1001, 'reset');
112
113
        $this->addElements();
114
    }
115
116
    protected function addElements()
117
    {
118
119
    }
120
121
    public function addTextElement($name, $label, $placeholder, $span = 12, $priority = 0, $isButtonElement = false)
122
    {
123
        $this->add(
124
            [
125
                'type' => 'Text',
126
                'options' => [
127
                    'label' => $label,
128
                    'span' => $span,
129
                ],
130
                'attributes' => [
131
                    'placeholder' => $placeholder,
132
                    'class' => 'form-control',
133
                ],
134
            ],
135
            [
136
                'name' => $name,
137
                'priority' => $priority,
138
            ]
139
        );
140
141
        if ($isButtonElement) {
142
            $this->setOption('button_element', $name);
143
        }
144
    }
145
146
    public function getButtons()
147
    {
148
        return $this->buttonsIterator;
149
    }
150
151
    public function addButton($name, $priority = 0, $type = 'button')
152
    {
153
        if (is_array($name)) {
154
            $label = $name[1];
155
            $name = $name[0];
156
        } else {
157
            $label = $name;
158
            $name = strtolower($name);
159
        }
160
161
        $factory = $this->getFormFactory();
162
163
        $button = $factory->create(
164
            [
165
                'type' => 'Button',
166
                'options' => [
167
                    'label' => $label,
168
                ],
169
                'attributes' => [
170
                    'class' => 'btn btn-' . ('submit' == $type ? 'primary' : 'default'),
171
                    'type'  => $type,
172
                ],
173
            ]
174
        );
175
        $button->setName($name);
176
177
        $this->buttonsIterator->insert($name, $button, $priority);
178
179
        return $this;
180
    }
181
182
    /**
183
     * Sets the initial search params.
184
     *
185
     * That means, the values for the elements
186
     * which should be set, if the form resets.
187
     *
188
     * @param array|\Traversable $params
189
     *
190
     * @return self
191
     */
192
    public function setSearchParams($params)
193
    {
194
        if ($params instanceOf \Traversable) {
195
            $params = ArrayUtils::iteratorToArray($params);
196
        }
197
198
        $params = Json::encode($params);
199
        $this->setAttribute('data-search-params', $params);
200
201
        return $this;
202
    }
203
204
}