Passed
Push — master ( a5f649...f758ae )
by Daniel
06:48
created

UserFormsGridFieldFilterHeader::getHTMLFragments()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 78
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 50
nc 8
nop 1
dl 0
loc 78
rs 8.5839
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SilverStripe\UserForms\Form;
4
5
use SilverStripe\Core\Convert;
6
use SilverStripe\Forms\CompositeField;
7
use SilverStripe\Forms\DateField;
8
use SilverStripe\Forms\DropdownField;
9
use SilverStripe\Forms\FieldGroup;
10
use SilverStripe\Forms\GridField\GridField;
11
use SilverStripe\Forms\GridField\GridField_FormAction;
12
use SilverStripe\Forms\GridField\GridFieldDataColumns;
13
use SilverStripe\Forms\GridField\GridFieldFilterHeader;
14
use SilverStripe\Forms\TextField;
15
use SilverStripe\ORM\ArrayList;
16
use SilverStripe\ORM\FieldType\DBDate;
17
use SilverStripe\ORM\SS_List;
18
use SilverStripe\View\ArrayData;
19
20
/**
21
 * Extension to the build in SilverStripe {@link GridField} to allow for
22
 * filtering {@link SubmittedForm} objects in the submissions tab by
23
 * entering the value of a field
24
 *
25
 * @package userforms
26
 */
27
class UserFormsGridFieldFilterHeader extends GridFieldFilterHeader
28
{
29
    /**
30
     * A map of name => value of columns from all submissions
31
     * @var array
32
     */
33
    protected $columns;
34
35
    public function setColumns($columns)
36
    {
37
        $this->columns = $columns;
38
    }
39
40
    public function handleAction(GridField $gridField, $actionName, $arguments, $data)
41
    {
42
        if (!$this->checkDataType($gridField->getList())) {
43
            return;
44
        }
45
46
        if ($actionName === 'filter') {
47
            $gridField->State->UserFormsGridField = array(
48
                'filter' => isset($data['FieldNameFilter']) ? $data['FieldNameFilter'] : null,
49
                'value' => isset($data['FieldValue']) ? $data['FieldValue'] : null,
50
                'start' => isset($data['StartFilter']) ? $data['StartFilter'] : null,
51
                'end' => isset($data['EndFilter']) ? $data['EndFilter'] : null
52
            );
53
        }
54
    }
55
56
57
    public function getHTMLFragments($gridField)
58
    {
59
        $fields = ArrayList::create();
60
        $state = $gridField->State->UserFormsGridField;
61
62
        $selectedField = $state->filter;
63
        $selectedValue = $state->value;
64
65
        // show dropdown of all the fields available from the submitted form fields
66
        // that have been saved. Takes the titles from the currently live form.
67
        $columnField = DropdownField::create('FieldNameFilter', '');
68
        $columnField->setSource($this->columns);
69
        $columnField->setEmptyString(_t(__CLASS__.'.FILTERSUBMISSIONS', 'Filter Submissions..'));
70
        $columnField->setHasEmptyDefault(true);
71
        $columnField->setValue($selectedField);
72
73
        $valueField = TextField::create('FieldValue', '', $selectedValue);
74
75
        $columnField->addExtraClass('ss-gridfield-sort');
76
        $columnField->addExtraClass('no-change-track');
77
78
        $valueField->addExtraClass('ss-gridfield-sort');
79
        $valueField->addExtraClass('no-change-track');
80
        $valueField->setAttribute(
81
            'placeholder',
82
            _t(__CLASS__.'.WHEREVALUEIS', 'where value is..')
83
        );
84
85
        $fields->push(FieldGroup::create(CompositeField::create(
86
            $columnField,
87
            $valueField
88
        )));
89
90
        $fields->push(FieldGroup::create(CompositeField::create(
91
            $start = DateField::create('StartFilter', _t(__CLASS__.'.FROM', 'From')),
92
            $end = DateField::create('EndFilter', _t(__CLASS__.'.TILL', 'Till'))
93
        )));
94
95
        foreach (array($start, $end) as $date) {
96
            $date->setDateFormat(DBDate::ISO_DATE);
97
            $date->addExtraClass('no-change-track');
98
        }
99
100
        if ($state->end) {
101
            $end->setValue($state->end);
102
        }
103
        if ($state->start) {
104
            $start->setValue($state->start);
105
        }
106
107
108
        $fields->push($actions = FieldGroup::create(
109
            GridField_FormAction::create($gridField, 'filter', false, 'filter', null)
110
                ->addExtraClass('ss-gridfield-button-filter btn btn-primary')
111
                ->setTitle(_t(__CLASS__.'.FILTER', "Filter"))
112
                ->setAttribute('title', _t(__CLASS__.'.FILTER', "Filter"))
113
                ->setAttribute('id', 'action_filter_' . $gridField->getModelClass() . '_' . $columnField),
114
            GridField_FormAction::create($gridField, 'reset', false, 'reset', null)
115
                ->addExtraClass('ss-gridfield-button-close btn ')
116
                ->setTitle(_t(__CLASS__.'.RESET', "Reset"))
117
                ->setAttribute('title', _t(__CLASS__.'.RESET', "Reset"))
118
                ->setAttribute('id', 'action_reset_' . $gridField->getModelClass() . '_' . $columnField)
119
        ));
120
121
        $actions->addExtraClass('filter-buttons');
122
        $actions->addExtraClass('no-change-track');
123
124
        $colSpan = 2 + count($gridField->getConfig()->getComponentByType(GridFieldDataColumns::class)
125
            ->getDisplayFields($gridField));
126
127
        $forTemplate = ArrayData::create(array(
128
            'Fields'    => $fields,
129
            'ColSpan'   => $colSpan
130
        ));
131
132
133
        return array(
134
            'header' => $forTemplate->renderWith(UserFormsGridFieldFilterHeader::class . '_Row')
135
        );
136
    }
137
138
    public function getManipulatedData(GridField $gridField, SS_List $dataList)
139
    {
140
        $state = $gridField->State;
141
142
        if ($filter = $state->UserFormsGridField->toArray()) {
143
            if (isset($filter['filter']) && $filter['filter'] && isset($filter['value']) && $filter['value']) {
144
                $dataList = $dataList->where(sprintf(
0 ignored issues
show
Bug introduced by
The method where() does not exist on SilverStripe\ORM\SS_List. It seems like you code against a sub-type of said class. However, the method does not exist in SilverStripe\ORM\Sortable or SilverStripe\ORM\Filterable or SilverStripe\ORM\Relation or SilverStripe\ORM\Limitable or SilverStripe\ORM\Relation or SilverStripe\ORM\Relation or SilverStripe\ORM\Relation. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

144
                /** @scrutinizer ignore-call */ 
145
                $dataList = $dataList->where(sprintf(
Loading history...
145
                    "
146
					SELECT COUNT(*) FROM SubmittedFormField
147
					WHERE (
148
						ParentID = SubmittedForm.ID AND
149
						Name = '%s' AND
150
						Value LIKE '%s'
151
					) > 0",
152
                    Convert::raw2sql($filter['filter']),
0 ignored issues
show
Bug introduced by
It seems like SilverStripe\Core\Conver...2sql($filter['filter']) can also be of type array; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

152
                    /** @scrutinizer ignore-type */ Convert::raw2sql($filter['filter']),
Loading history...
153
                    Convert::raw2sql($filter['value'])
154
                ));
155
            }
156
157
            if (isset($filter['start']) && $filter['start']) {
158
                $dataList = $dataList->filter(array(
159
                    'Created:GreaterThan' => $filter['start']
160
                ));
161
            }
162
163
            if (isset($filter['end']) && $filter['end']) {
164
                $dataList = $dataList->filter(array(
165
                    'Created:LessThan' => $filter['end']
166
                ));
167
            }
168
        }
169
170
        return $dataList;
171
    }
172
}
173