Completed
Push — develop ( bec80e...e58249 )
by
unknown
09:35
created

SearchForm::setData()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
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 Traversable;
14
use Zend\Form\Exception;
15
use Zend\Form\Form as ZfForm;
16
use Zend\Json\Json;
17
use Zend\Stdlib\ArrayUtils;
18
use Zend\Stdlib\PriorityList;
19
20
/**
21
 * ${CARET}
22
 * 
23
 * @author Mathias Gelhausen <[email protected]>
24
 * @todo write test 
25
 */
26
class SearchForm extends ZfForm
27
{
28
    protected $attributes = [
29
        'class'          => 'form-inline search-form',
30
        'data-handle-by' => 'script',
31
        'method'         => 'get',
32
    ];
33
34
    /**
35
     *
36
     *
37
     * @var \Zend\Stdlib\PriorityList
38
     */
39
    protected $buttonsIterator;
40
41
    protected $multiValueFields = [];
42
43
    public function __construct($name = null, $options = [])
44
    {
45
        parent::__construct($name, $options);
46
        $this->buttonsIterator = new PriorityList();
47
        $this->buttonsIterator->isLIFO(false);
48
    }
49
50
    public function setButtonElement($name)
51
    {
52
        return $this->setOption('button_element', $name);
53
    }
54
55
    public function getButtonElement()
56
    {
57
        return $this->getOption('button_element');
58
    }
59
60
    /**
61
     * Sets the column map.
62
     *
63
     * @param array $map
64
     *
65
     * @see \Core\Form\View\Helper\SearchForm
66
     * @return self
67
     */
68
    public function setColumnMap($map)
69
    {
70
        $this->setOption('column_map', $map);
71
72
        return $this;
73
    }
74
75
    /**
76
     * Gets the column map.
77
     *
78
     * Generates the column map from the element options,
79
     * if none is set.
80
     *
81
     * @return array
82
     */
83
    public function getColumnMap()
84
    {
85
        $map = $this->getOption('column_map');
86
87
        if (null === $map) {
88
            $map = [];
89
            foreach ($this as $element) {
90
                $col = $element->getOption('span');
91
                if (null !== $col) {
92
                    $map[$element->getName()] = $col;
93
                }
94
            }
95
96
            $this->setOption('column_map', $map);
97
        }
98
99
        return $map;
100
    }
101
102
    public function setMultiValueFields(array $fields)
103
    {
104
        $multiValues = [];
105
106
        foreach ($fields as $name => $separator) {
107
            if (is_numeric($name)) {
108
                $name = $separator;
109
                $separator = ',';
110
            }
111
112
            $multiValues[$name] = $separator;
113
        }
114
115
        $this->multiValuesFields = $multiValues;
0 ignored issues
show
Bug introduced by
The property multiValuesFields does not seem to exist. Did you mean multiValueFields?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
116
        $this->setAttribute('data-multivalues', Json::encode($multiValues));
117
118
        return $this;
119
    }
120
121
    public function setData($data)
122
    {
123
        foreach ($this->multiValueFields as $name => $separator) {
124
            if (array_key_exists($name, $data)) {
125
                $data[$name] = explode($separator, $data[$name]);
126
            }
127
        }
128
129
        return parent::setData($data); // TODO: Change the autogenerated stub
130
    }
131
132
133
    public function init()
134
    {
135
        $this->addTextElement(
136
            $this->getOption('text_name') ?: /*@translate*/ 'q',
137
            $this->getOption('text_label') ?: /*@translate*/ 'Search',
138
            $this->getOption('text_placeholder') ?: /*@translate*/ 'Search query',
139
            $this->getOption('text_span') ?: 12,
140
            50,
141
            true
142
        );
143
144
        $this->addButton(/*@translate*/ 'Search', -1000, 'submit');
145
        $this->addButton(/*@translate*/ 'Clear', -1001, 'reset');
146
147
        $this->addElements();
148
    }
149
150
    protected function addElements()
151
    {
152
153
    }
154
155
    public function addTextElement($name, $label, $placeholder, $span = 12, $priority = 0, $isButtonElement = false)
156
    {
157
        $this->add(
158
            [
159
                'type' => 'Text',
160
                'options' => [
161
                    'label' => $label,
162
                    'span' => $span,
163
                ],
164
                'attributes' => [
165
                    'placeholder' => $placeholder,
166
                    'class' => 'form-control',
167
                ],
168
            ],
169
            [
170
                'name' => $name,
171
                'priority' => $priority,
172
            ]
173
        );
174
175
        if ($isButtonElement) {
176
            $this->setOption('button_element', $name);
177
        }
178
    }
179
180
    public function getButtons()
181
    {
182
        return $this->buttonsIterator;
183
    }
184
185
    public function addButton($name, $priority = 0, $type = 'button')
186
    {
187
        if (is_array($name)) {
188
            $label = $name[1];
189
            $name = $name[0];
190
        } else {
191
            $label = $name;
192
            $name = strtolower($name);
193
        }
194
195
        $factory = $this->getFormFactory();
196
197
        $button = $factory->create(
198
            [
199
                'type' => 'Button',
200
                'options' => [
201
                    'label' => $label,
202
                ],
203
                'attributes' => [
204
                    'class' => 'btn btn-' . ('submit' == $type ? 'primary' : 'default'),
205
                    'type'  => $type,
206
                ],
207
            ]
208
        );
209
        $button->setName($name);
210
211
        $this->buttonsIterator->insert($name, $button, $priority);
212
213
        return $this;
214
    }
215
216
    /**
217
     * Sets the initial search params.
218
     *
219
     * That means, the values for the elements
220
     * which should be set, if the form resets.
221
     *
222
     * @param array|\Traversable $params
223
     *
224
     * @return self
225
     */
226
    public function setSearchParams($params)
227
    {
228
        if ($params instanceOf \Traversable) {
229
            $params = ArrayUtils::iteratorToArray($params);
230
        }
231
232
        $params = Json::encode($params);
233
        $this->setAttribute('data-search-params', $params);
234
235
        return $this;
236
    }
237
238
}