Filters::setDefaults()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 6
rs 10
ccs 3
cts 3
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace mQueue\Form;
4
5
use mQueue\Model\Status;
6
use mQueue\Model\UserMapper;
7
use Zend_Controller_Action_HelperBroker;
8
use Zend_Form;
9
10
class Filters extends Zend_Form
11
{
12 1
    public function init(): void
13
    {
14
        // Set the method for the display form to GET
15 1
        $this->setMethod('get');
16 1
        $this->setName('filters');
17
18
        // Add the submit button
19 1
        $this->addElement('submit', 'submit', [
20 1
            'ignore' => true,
21 1
            'label' => _tr('Apply'),
22
        ]);
23
24 1
        $this->addDecorator('Fieldset');
25
26 1
        $this->setDecorators([
27 1
            'FormElements',
28
            [['fieldset' => 'Fieldset'], ['legend' => 'Filter']],
29
            'Form',
30
        ]);
31
32 1
        $this->addDisplayGroup(['submit'], 'filters', ['legend' => _tr('Filter')]);
33
34 1
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
35 1
        $view = $viewRenderer->view;
36
37 1
        $this->addElement('image', 'addFilter', [
38 1
            'src' => $view->serverUrl() . $view->baseUrl('/images/add.png'),
39 1
            'imageValue' => '1',
40
        ]);
41 1
        $this->addDisplayGroup(['addFilter'], 'addFilterGroup', ['class' => 'addFilter']);
42
43 1
        $this->setDisplayGroupDecorators([
44 1
            'FormElements',
45
            [['row' => 'HtmlTag'], ['tag' => 'dl', 'class' => 'buttons']],
46
        ]);
47 1
    }
48
49
    /**
50
     * Overrides isValid to dynamically generate subforms which will be used for validation.
51
     *
52
     * @param array $data
53
     *
54
     * @return bool
55
     */
56
    public function isValid($data)
57
    {
58
        $data = $this->createSubForms($data);
59
60
        return parent::isValid($data);
61
    }
62
63
    /**
64
     * Override setDefaults to dynamically generate subforms.
65
     *
66
     * @param array $defaults
67
     *
68
     * @return Zend_Form
69
     */
70 1
    public function setDefaults(array $defaults)
71
    {
72 1
        $defaults = $this->createSubForms($defaults);
73
74
        // set defaults, which will propagate to newly created subforms
75 1
        return parent::setDefaults($defaults);
76
    }
77
78
    /**
79
     * Create actual filter as subforms according to filter values given.
80
     * It will at least create one subform. It may add a default subform
81
     * if 'addFilter_x' value is given.
82
     *
83
     * @param array $defaults values of form
84
     *
85
     * @return array $defaults modified values with additional filter
86
     */
87 1
    private function createSubForms(array $defaults)
88
    {
89
        // Find out the highest filter number
90 1
        $max = 0;
91 1
        foreach (array_keys($defaults) as $key) {
92
            if (preg_match('/^filter(\d+)$/', $key, $m)) {
93
                if ($m[1] > $max) {
94
                    $max = $m[1];
95
                }
96
            }
97
        }
98
99
        // If we specifically asked to add a filter or if there is none, then add a new filter with default value
100 1
        if ((isset($defaults['addFilter_x'])) || $max == 0) {
101 1
            $defaults['filter' . ($max + 1)] = [
102 1
                'user' => \mQueue\Model\User::getCurrent() ? 0 : UserMapper::fetchAll()->current()->id,
103 1
                'condition' => 'is',
104 1
                'status' => array_keys(Status::$ratings),
105
            ];
106
        }
107
108
        // Create all filters
109 1
        $position = 1;
110 1
        foreach (array_keys($defaults) as $key) {
111 1
            if (preg_match('/^filter(\d+)$/', $key, $m)) {
112 1
                $subform = new Filter();
113 1
                if ($position > 1) {
114
                    $subform->disableExtraFields();
115
                }
116 1
                $this->addSubForm($subform, $key, $position++);
117
            }
118
        }
119
120 1
        return $defaults;
121
    }
122
123
    /**
124
     * Returns values as readable text for end-user
125
     *
126
     * @return string
127
     */
128 1
    public function getValuesText()
129
    {
130 1
        $text = [];
131 1
        foreach ($this->getSubForms() as $subForm) {
132 1
            $text[] = $subForm->getValuesText();
133
        }
134
135 1
        return implode(' + ', $text);
136
    }
137
}
138