Completed
Push — develop ( 7d6075...b57d28 )
by
unknown
15:48 queued 07:06
created

TextSearchFormFieldset::setOptions()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 24
rs 8.5125
cc 5
eloc 12
nc 9
nop 1
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;
12
13
use Zend\Form\Element;
14
use Zend\Form\ElementInterface;
15
use Zend\Form\Exception;
16
use Zend\Form\Fieldset;
17
18
/**
19
 * Fieldset for elements in a TextSearchForm
20
 *
21
 * @author Mathias Gelhausen <[email protected]>
22
 * @since  0.25
23
 */
24
class TextSearchFormFieldset extends Fieldset
25
{
26
27
    /**
28
     * Gets the name of the button element.
29
     *
30
     * @return string
31
     */
32
    public function getButtonElement()
33
    {
34
        $name = $this->getOption('button_element');
35
        if (null === $name) {
36
            $name = 'text';
37
            $this->setButtonElement($name);
38
        }
39
40
        return $name;
41
    }
42
43
    /**
44
     * Sets the name of the button element.
45
     *
46
     * This is the element, the searchForm view helper should
47
     * render the buttons in an input button group addon.
48
     *
49
     * @param string $name
50
     *
51
     * @return self
52
     */
53
    public function setButtonElement($name)
54
    {
55
        return $this->setOption('button_element', $name);
56
    }
57
58
    /**
59
     * Sets the column map.
60
     *
61
     * @param array $map
62
     *
63
     * @see \Core\Form\View\Helper\SearchForm
64
     * @return self
65
     */
66
    public function setColumnMap($map)
67
    {
68
        $this->setOption('column_map', $map);
69
70
        return $this;
71
    }
72
73
    /**
74
     * Gets the column map.
75
     *
76
     * Generates the column map from the element options,
77
     * if none is set.
78
     *
79
     * @return array
80
     */
81
    public function getColumnMap()
82
    {
83
        $map = $this->getOption('column_map');
84
85
        if (null === $map) {
86
            $map = [];
87
            foreach ($this as $element) {
88
                $col = $element->getOption('span');
89
                if (null !== $col) {
90
                    $map[$element->getName()] = $col;
91
                }
92
            }
93
94
            $this->setOption('column_map', $map);
95
        }
96
97
        return $map;
98
    }
99
100
    public function setOptions($options)
101
    {
102
        parent::setOptions($options);
103
104
        $options = $this->options; // assure array
105
106
        if ($this->has('text')) {
107
            $textElement = $this->get('text');
108
109
            if (isset($options['text_placeholder'])) {
110
                $textElement->setAttribute('placeholder', $options['text_placeholder']);
111
            }
112
113
            if (isset($options['text_span'])) {
114
                $textElement->setOption('span', $options['text_span']);
115
            }
116
117
            if (isset($options['text_label'])) {
118
                $textElement->setLabel($options['text_label']);
119
            }
120
        }
121
122
        return $this;
123
    }
124
125
126
    public function init()
127
    {
128
        $this->addTextElement(
129
             $this->getOption('text_label')
130
                 ? : /*@translate*/ 'Search',
131
             $this->getOption('text_placeholder')
132
                 ? : /*@translate*/ 'Search query',
133
             $this->getOption('text_span') ? : 12
134
        );
135
    }
136
137
    public function add($elementOrFieldset, array $flags = [])
138
    {
139
        if (is_array($elementOrFieldset)) {
140
            $class                                    =
141
                isset($elementOrFieldset['attributes']['class']) ? $elementOrFieldset['attributes']['class'] : '';
142
            $elementOrFieldset['attributes']['class'] = "$class form-control";
143
144
            if (isset($elementOrFieldset['options']['is_button_element'])
145
                && $elementOrFieldset['options']['is_button_element']
146
            ) {
147
                $this->setButtonElement(
148
                     isset($flags['name']) ? $flags['name'] : $elementOrFieldset['name']
149
                );
150
            }
151
152
        } else if ($elementOrFieldset instanceOf ElementInterface) {
153
            $class = $elementOrFieldset->hasAttribute('class') ? $elementOrFieldset->getAttribute('class') : '';
154
            $elementOrFieldset->setAttribute('class', "$class form-control");
155
156
            if (true === $elementOrFieldset->getOption('is_button_element')) {
157
                $this->setButtonElement($elementOrFieldset->getName());
158
            }
159
        }
160
161
        return parent::add($elementOrFieldset, $flags);
162
    }
163
164
    /**
165
     * Adds the search text element.
166
     *
167
     * @param string $label
168
     * @param string $placeholder
169
     * @param int    $span
170
     *
171
     * @return Fieldset|\Zend\Form\FieldsetInterface
172
     */
173
    protected function addTextElement($label, $placeholder = 'Search query', $span = 12)
174
    {
175
        return $this->add([
176
                              'type'       => 'Text',
177
                              'name'       => 'text',
178
                              'options'    => [
179
                                  'label' => $label,
180
                                  'span'  => $span,
181
                              ],
182
                              'attributes' => [
183
                                  'placeholder' => $placeholder,
184
                              ],
185
                          ]
186
        );
187
    }
188
}