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\GridFieldFilterHeader; |
||||||
13 | use SilverStripe\Forms\TextField; |
||||||
14 | use SilverStripe\ORM\ArrayList; |
||||||
15 | use SilverStripe\ORM\SS_List; |
||||||
16 | use SilverStripe\View\ArrayData; |
||||||
17 | |||||||
18 | /** |
||||||
19 | * Extension to the build in SilverStripe {@link GridField} to allow for |
||||||
20 | * filtering {@link SubmittedForm} objects in the submissions tab by |
||||||
21 | * entering the value of a field |
||||||
22 | * |
||||||
23 | * @package userforms |
||||||
24 | */ |
||||||
25 | class UserFormsGridFieldFilterHeader extends GridFieldFilterHeader |
||||||
26 | { |
||||||
27 | /** |
||||||
28 | * A map of name => value of columns from all submissions |
||||||
29 | * @var array |
||||||
30 | */ |
||||||
31 | protected $columns; |
||||||
32 | |||||||
33 | public function setColumns($columns) |
||||||
34 | { |
||||||
35 | $this->columns = $columns; |
||||||
36 | } |
||||||
37 | |||||||
38 | public function handleAction(GridField $gridField, $actionName, $arguments, $data) |
||||||
39 | { |
||||||
40 | if (!$this->checkDataType($gridField->getList())) { |
||||||
41 | return; |
||||||
42 | } |
||||||
43 | |||||||
44 | if ($actionName === 'filter') { |
||||||
45 | $gridField->State->UserFormsGridField = array( |
||||||
46 | 'filter' => isset($data['FieldNameFilter']) ? $data['FieldNameFilter'] : null, |
||||||
47 | 'value' => isset($data['FieldValue']) ? $data['FieldValue'] : null, |
||||||
48 | 'start' => isset($data['StartFilter']) ? $data['StartFilter'] : null, |
||||||
49 | 'end' => isset($data['EndFilter']) ? $data['EndFilter'] : null |
||||||
50 | ); |
||||||
51 | } |
||||||
52 | } |
||||||
53 | |||||||
54 | |||||||
55 | public function getHTMLFragments($gridField) |
||||||
56 | { |
||||||
57 | $fields = ArrayList::create(); |
||||||
58 | $state = $gridField->State->UserFormsGridField; |
||||||
59 | |||||||
60 | $selectedField = $state->filter; |
||||||
61 | $selectedValue = $state->value; |
||||||
62 | |||||||
63 | // show dropdown of all the fields available from the submitted form fields |
||||||
64 | // that have been saved. Takes the titles from the currently live form. |
||||||
65 | $columnField = DropdownField::create('FieldNameFilter', ''); |
||||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
66 | $columnField->setSource($this->columns); |
||||||
67 | $columnField->setEmptyString(_t(__CLASS__.'.FILTERSUBMISSIONS', 'Filter Submissions..')); |
||||||
68 | $columnField->setHasEmptyDefault(true); |
||||||
69 | $columnField->setValue($selectedField); |
||||||
70 | |||||||
71 | $valueField = TextField::create('FieldValue', '', $selectedValue); |
||||||
72 | |||||||
73 | $columnField->addExtraClass('ss-gridfield-sort'); |
||||||
74 | $columnField->addExtraClass('no-change-track'); |
||||||
75 | |||||||
76 | $valueField->addExtraClass('ss-gridfield-sort'); |
||||||
77 | $valueField->addExtraClass('no-change-track'); |
||||||
78 | $valueField->setAttribute( |
||||||
79 | 'placeholder', |
||||||
80 | _t(__CLASS__.'.WHEREVALUEIS', 'where value is..') |
||||||
81 | ); |
||||||
82 | |||||||
83 | $fields->push(FieldGroup::create(CompositeField::create( |
||||||
84 | $columnField, |
||||||
85 | $valueField |
||||||
86 | ))); |
||||||
87 | |||||||
88 | $fields->push(FieldGroup::create(CompositeField::create( |
||||||
89 | $start = DateField::create('StartFilter', _t(__CLASS__.'.FROM', 'From')), |
||||||
90 | $end = DateField::create('EndFilter', _t(__CLASS__.'.TILL', 'Till')) |
||||||
91 | ))); |
||||||
92 | |||||||
93 | foreach (array($start, $end) as $date) { |
||||||
94 | $date->setDateFormat('y-mm-dd'); |
||||||
95 | $date->addExtraClass('no-change-track'); |
||||||
96 | } |
||||||
97 | |||||||
98 | $end->setValue($state->end); |
||||||
99 | $start->setValue($state->start); |
||||||
100 | |||||||
101 | |||||||
102 | $fields->push($actions = FieldGroup::create( |
||||||
103 | GridField_FormAction::create($gridField, 'filter', false, 'filter', null) |
||||||
0 ignored issues
–
show
false of type false is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
104 | ->addExtraClass('ss-gridfield-button-filter') |
||||||
105 | ->setAttribute('title', _t('SilverStripe\\Forms\\GridField\\GridField.Filter', "Filter")) |
||||||
106 | ->setAttribute('id', 'action_filter_' . $gridField->getModelClass() . '_' . $columnField), |
||||||
107 | GridField_FormAction::create($gridField, 'reset', false, 'reset', null) |
||||||
108 | ->addExtraClass('ss-gridfield-button-close') |
||||||
109 | ->setAttribute('title', _t('SilverStripe\\Forms\\GridField\\GridField.ResetFilter', "Reset")) |
||||||
110 | ->setAttribute('id', 'action_reset_' . $gridField->getModelClass() . '_' . $columnField) |
||||||
111 | )); |
||||||
112 | |||||||
113 | $actions->addExtraClass('filter-buttons'); |
||||||
114 | $actions->addExtraClass('no-change-track'); |
||||||
115 | |||||||
116 | $forTemplate = ArrayData::create(array( |
||||||
117 | 'Fields' => $fields |
||||||
118 | )); |
||||||
119 | |||||||
120 | |||||||
121 | return array( |
||||||
122 | 'header' => $forTemplate->renderWith(GridFieldFilterHeader::class . '_Row') |
||||||
123 | ); |
||||||
124 | } |
||||||
125 | |||||||
126 | public function getManipulatedData(GridField $gridField, SS_List $dataList) |
||||||
127 | { |
||||||
128 | $state = $gridField->State; |
||||||
129 | |||||||
130 | if ($filter = $state->UserFormsGridField->toArray()) { |
||||||
131 | if (isset($filter['filter']) && $filter['filter'] && isset($filter['value']) && $filter['value']) { |
||||||
132 | $dataList = $dataList->where(sprintf( |
||||||
0 ignored issues
–
show
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
Loading history...
|
|||||||
133 | " |
||||||
134 | SELECT COUNT(*) FROM SubmittedFormField |
||||||
135 | WHERE ( |
||||||
136 | ParentID = SubmittedForm.ID AND |
||||||
137 | Name = '%s' AND |
||||||
138 | Value LIKE '%s' |
||||||
139 | ) > 0", |
||||||
140 | Convert::raw2sql($filter['filter']), |
||||||
141 | Convert::raw2sql($filter['value']) |
||||||
142 | )); |
||||||
143 | } |
||||||
144 | |||||||
145 | if (isset($filter['start']) && $filter['start']) { |
||||||
146 | $dataList = $dataList->filter(array( |
||||||
147 | 'Created:GreaterThan' => $filter['start'] |
||||||
148 | )); |
||||||
149 | } |
||||||
150 | |||||||
151 | if (isset($filter['end']) && $filter['end']) { |
||||||
152 | $dataList = $dataList->filter(array( |
||||||
153 | 'Created:LessThan' => $filter['end'] |
||||||
154 | )); |
||||||
155 | } |
||||||
156 | } |
||||||
157 | |||||||
158 | return $dataList; |
||||||
159 | } |
||||||
160 | } |
||||||
161 |