Issues (281)

Branch: master

src/Backend/Modules/FormBuilder/Actions/Data.php (3 issues)

1
<?php
2
3
namespace Backend\Modules\FormBuilder\Actions;
4
5
use Backend\Core\Engine\Authentication as BackendAuthentication;
6
use Backend\Core\Engine\Base\ActionIndex as BackendBaseActionIndex;
7
use Backend\Core\Engine\DataGridDatabase as BackendDataGridDatabase;
8
use Backend\Core\Engine\Form;
9
use Backend\Core\Engine\Form as BackendForm;
10
use Backend\Core\Language\Language as BL;
11
use Backend\Core\Engine\Model as BackendModel;
12
use Backend\Modules\FormBuilder\Engine\Model as BackendFormBuilderModel;
13
14
/**
15
 * This is the data-action it will display the overview of sent data
16
 */
17
class Data extends BackendBaseActionIndex
18
{
19
    /**
20
     * Filter variables
21
     *
22
     * @var array
23
     */
24
    private $filter;
25
26
    /**
27
     * The form instance
28
     *
29
     * @var Form
30
     */
31
    protected $form;
32
33
    /**
34
     * Form id.
35
     *
36
     * @var int
37
     */
38
    private $id;
39
40
    /**
41
     * @var array
42
     */
43
    private $record;
44
45
    /**
46
     * Builds the query for this datagrid
47
     *
48
     * @return array An array with two arguments containing the query and its parameters.
49
     */
50
    private function buildQuery(): array
51
    {
52
        $parameters = [$this->id];
53
54
        // start query, as you can see this query is build in the wrong place,
55
        // because of the filter it is a special case
56
        // wherein we allow the query to be in the actionfile itself
57
        $query =
58
            'SELECT i.id, UNIX_TIMESTAMP(i.sent_on) AS sent_on
59
             FROM forms_data AS i
60
             WHERE i.form_id = ?';
61
62
        // add start date
63
        if ($this->filter['start_date'] !== '') {
64
            // explode date parts
65
            $chunks = explode('/', $this->filter['start_date']);
66
67
            // add condition
68
            $query .= ' AND i.sent_on >= ?';
69
            $parameters[] = BackendModel::getUTCDate(null, gmmktime(23, 59, 59, $chunks[1], $chunks[0], $chunks[2]));
0 ignored issues
show
$chunks[2] of type string is incompatible with the type integer expected by parameter $year of gmmktime(). ( Ignorable by Annotation )

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

69
            $parameters[] = BackendModel::getUTCDate(null, gmmktime(23, 59, 59, $chunks[1], $chunks[0], /** @scrutinizer ignore-type */ $chunks[2]));
Loading history...
$chunks[0] of type string is incompatible with the type integer expected by parameter $day of gmmktime(). ( Ignorable by Annotation )

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

69
            $parameters[] = BackendModel::getUTCDate(null, gmmktime(23, 59, 59, $chunks[1], /** @scrutinizer ignore-type */ $chunks[0], $chunks[2]));
Loading history...
$chunks[1] of type string is incompatible with the type integer expected by parameter $month of gmmktime(). ( Ignorable by Annotation )

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

69
            $parameters[] = BackendModel::getUTCDate(null, gmmktime(23, 59, 59, /** @scrutinizer ignore-type */ $chunks[1], $chunks[0], $chunks[2]));
Loading history...
70
        }
71
72
        // add end date
73
        if ($this->filter['end_date'] !== '') {
74
            // explode date parts
75
            $chunks = explode('/', $this->filter['end_date']);
76
77
            // add condition
78
            $query .= ' AND i.sent_on <= ?';
79
            $parameters[] = BackendModel::getUTCDate(null, gmmktime(23, 59, 59, $chunks[1], $chunks[0], $chunks[2]));
80
        }
81
82
        // new query
83
        return [$query, $parameters];
84
    }
85
86
    public function execute(): void
87
    {
88
        // get parameters
89
        $this->id = $this->getRequest()->query->getInt('id');
90
91
        // does the item exist
92
        if ($this->id !== 0 && BackendFormBuilderModel::exists($this->id)) {
93
            parent::execute();
94
            $this->setFilter();
95
            $this->loadForm();
96
            $this->getData();
97
            $this->loadDataGrid();
98
            $this->parse();
99
            $this->display();
100
        } else {
101
            // no item found, throw an exceptions, because somebody is fucking with our url
102
            $this->redirect(BackendModel::createUrlForAction('Index') . '&error=non-existing');
103
        }
104
    }
105
106
    private function getData(): void
107
    {
108
        $this->record = BackendFormBuilderModel::get($this->id);
109
110
        if ($this->record['method'] === 'email') {
111
            $this->redirect(BackendModel::createUrlForAction('Index') . '&error=non-existing');
112
        }
113
    }
114
115
    private function loadDataGrid(): void
116
    {
117
        list($query, $parameters) = $this->buildQuery();
118
119
        // create datagrid
120
        $this->dataGrid = new BackendDataGridDatabase($query, $parameters);
121
122
        // overrule default URL
123
        $this->dataGrid->setURL(
124
            BackendModel::createUrlForAction(
125
                null,
126
                null,
127
                null,
128
                [
129
                    'offset' => '[offset]',
130
                    'order' => '[order]',
131
                    'sort' => '[sort]',
132
                    'start_date' => $this->filter['start_date'],
133
                    'end_date' => $this->filter['end_date'],
134
                ],
135
                false
136
            ) . '&amp;id=' . $this->id
137
        );
138
139
        // sorting columns
140
        $this->dataGrid->setSortingColumns(['sent_on'], 'sent_on');
141
        $this->dataGrid->setSortParameter('desc');
142
143
        // check if this action is allowed
144
        if (BackendAuthentication::isAllowedAction('DataDetails')) {
145
            // set colum URLs
146
            $this->dataGrid->setColumnURL(
147
                'sent_on',
148
                BackendModel::createUrlForAction(
149
                    'DataDetails',
150
                    null,
151
                    null,
152
                    [
153
                        'start_date' => $this->filter['start_date'],
154
                        'end_date' => $this->filter['end_date'],
155
                    ],
156
                    false
157
                ) . '&amp;id=[id]'
158
            );
159
160
            // add edit column
161
            $this->dataGrid->addColumn(
162
                'details',
163
                null,
164
                BL::getLabel('Details'),
165
                BackendModel::createUrlForAction(
166
                    'DataDetails',
167
                    null,
168
                    null,
169
                    [
170
                        'start_date' => $this->filter['start_date'],
171
                        'end_date' => $this->filter['end_date'],
172
                    ]
173
                ) . '&amp;id=[id]',
174
                BL::getLabel('Details')
175
            );
176
        }
177
178
        // date
179
        $this->dataGrid->setColumnFunction(
180
            [new BackendFormBuilderModel(), 'calculateTimeAgo'],
181
            '[sent_on]',
182
            'sent_on',
183
            false
184
        );
185
        $this->dataGrid->setColumnFunction('ucfirst', '[sent_on]', 'sent_on', false);
186
187
        // add the multicheckbox column
188
        $this->dataGrid->setMassActionCheckboxes('check', '[id]');
189
190
        // mass action
191
        $ddmMassAction = new \SpoonFormDropdown('action', ['delete' => BL::getLabel('Delete')], 'delete');
192
        $ddmMassAction->setOptionAttributes('delete', ['data-target' => '#confirmDelete']);
193
        $this->dataGrid->setMassAction($ddmMassAction);
194
    }
195
196
    private function loadForm(): void
197
    {
198
        $startDate = '';
199
        $endDate = '';
200
201
        if (isset($this->filter['start_date']) && $this->filter['start_date'] != '') {
202
            $chunks = explode('/', $this->filter['start_date']);
203
            $startDate = (int) mktime(0, 0, 0, (int) $chunks[1], (int) $chunks[0], (int) $chunks[2]);
204
            if ($startDate == 0) {
205
                $startDate = '';
206
            }
207
        }
208
209
        if (isset($this->filter['end_date']) && $this->filter['end_date'] != '') {
210
            $chunks = explode('/', $this->filter['end_date']);
211
            $endDate = (int) mktime(0, 0, 0, (int) $chunks[1], (int) $chunks[0], (int) $chunks[2]);
212
            if ($endDate == 0) {
213
                $endDate = '';
214
            }
215
        }
216
217
        $this->form = new BackendForm('filter', BackendModel::createUrlForAction(), 'get');
218
        $this->form->addText('id', $this->id, 255, 'hidden');
219
        $this->form->addDate('start_date', $startDate);
220
        $this->form->addDate('end_date', $endDate);
221
222
        // manually parse fields
223
        $this->form->parse($this->template);
224
    }
225
226
    protected function parse(): void
227
    {
228
        parent::parse();
229
230
        // datagrid
231
        $this->template->assign('dataGrid', ($this->dataGrid->getNumResults() != 0) ? $this->dataGrid->getContent() : false);
232
233
        // form info
234
        $this->template->assign('name', $this->record['name']);
235
        $this->template->assign('id', $this->record['id']);
236
        $this->template->assignArray($this->filter);
237
    }
238
239
    /**
240
     * Sets the filter based on the $_GET array.
241
     */
242
    private function setFilter(): void
243
    {
244
        // start date is set
245
        if ($this->getRequest()->query->has('start_date') && $this->getRequest()->query->get('start_date', '') !== '') {
246
            // redefine
247
            $startDate = $this->getRequest()->query->get('start_date', '');
248
249
            // explode date parts
250
            $chunks = explode('/', $startDate);
251
252
            // valid date
253
            if (count($chunks) == 3 && checkdate((int) $chunks[1], (int) $chunks[0], (int) $chunks[2])) {
254
                $this->filter['start_date'] = $startDate;
255
            } else {
256
                // invalid date
257
                $this->filter['start_date'] = '';
258
            }
259
        } else {
260
            // not set
261
            $this->filter['start_date'] = '';
262
        }
263
264
        // end date is set
265
        if ($this->getRequest()->query->has('end_date') && $this->getRequest()->query->get('end_date', '') !== '') {
266
            // redefine
267
            $endDate = $this->getRequest()->query->get('end_date');
268
269
            // explode date parts
270
            $chunks = explode('/', $endDate);
271
272
            // valid date
273
            if (count($chunks) == 3 && checkdate((int) $chunks[1], (int) $chunks[0], (int) $chunks[2])) {
274
                $this->filter['end_date'] = $endDate;
275
            } else {
276
                // invalid date
277
                $this->filter['end_date'] = '';
278
            }
279
        } else {
280
            // not set
281
            $this->filter['end_date'] = '';
282
        }
283
    }
284
}
285