GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

FieldSelect::toggleFieldData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 6
rs 10
1
<?php
2
3
/**
4
 * @package toolkit
5
 */
6
7
/**
8
 * A simple Select field that essentially maps to HTML's `<select/>`. The
9
 * options for this field can be static, or feed from another field.
10
 */
11
class FieldSelect extends FieldTagList implements ExportableField, ImportableField
12
{
13
    public function __construct()
14
    {
15
        parent::__construct();
16
        $this->_name = __('Select Box');
0 ignored issues
show
Bug Best Practice introduced by
The property _name does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
17
        $this->_required = true;
18
        $this->_showassociation = true;
19
20
        // Set default
21
        $this->set('show_column', 'yes');
22
        $this->set('location', 'sidebar');
23
        $this->set('required', 'no');
24
    }
25
26
/*-------------------------------------------------------------------------
27
    Definition:
28
-------------------------------------------------------------------------*/
29
30
    public function canToggle()
31
    {
32
        return ($this->get('allow_multiple_selection') === 'yes' ? false : true);
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
33
    }
34
35
    public function getToggleStates()
36
    {
37
        $values = preg_split('/,\s*/i', $this->get('static_options'), -1, PREG_SPLIT_NO_EMPTY);
0 ignored issues
show
Bug introduced by
It seems like $this->get('static_options') can also be of type array; however, parameter $subject of preg_split() 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

37
        $values = preg_split('/,\s*/i', /** @scrutinizer ignore-type */ $this->get('static_options'), -1, PREG_SPLIT_NO_EMPTY);
Loading history...
38
39
        if ($this->get('dynamic_options') != '') {
40
            $this->findAndAddDynamicOptions($values);
41
        }
42
43
        $values = array_map('trim', $values);
44
        // Fixes issues on PHP5.3. RE: #1773 ^BA
45
        if (empty($values)) {
46
            return $values;
47
        }
48
49
        $states = array_combine($values, $values);
50
51
        if ($this->get('sort_options') === 'yes') {
52
            natsort($states);
0 ignored issues
show
Bug introduced by
It seems like $states can also be of type false; however, parameter $array of natsort() does only seem to accept array, 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

52
            natsort(/** @scrutinizer ignore-type */ $states);
Loading history...
53
        }
54
55
        return $states;
56
    }
57
58
    public function toggleFieldData(array $data, $newState, $entry_id = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$entry_id" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$entry_id"; expected 0 but found 1
Loading history...
59
    {
60
        $data['value'] = $newState;
61
        $data['handle'] = Lang::createHandle($newState);
62
63
        return $data;
64
    }
65
66
    public function canFilter()
67
    {
68
        return true;
69
    }
70
71
    public function canPrePopulate()
72
    {
73
        return true;
74
    }
75
76
    public function isSortable()
77
    {
78
        return true;
79
    }
80
81
    public function allowDatasourceOutputGrouping()
82
    {
83
        // Grouping follows the same rule as toggling.
84
        return $this->canToggle();
85
    }
86
87
    public function allowDatasourceParamOutput()
88
    {
89
        return true;
90
    }
91
92
    public function requiresSQLGrouping()
93
    {
94
        // SQL grouping follows the opposite rule as toggling.
95
        return !$this->canToggle();
96
    }
97
98
    public function fetchSuggestionTypes()
99
    {
100
        return array('association', 'static');
101
    }
102
103
    /*-------------------------------------------------------------------------
104
        Setup:
105
    -------------------------------------------------------------------------*/
106
107
    public function createTable()
108
    {
109
        return Symphony::Database()->query(
110
            "CREATE TABLE IF NOT EXISTS `tbl_entries_data_" . $this->get('id') . "` (
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal CREATE TABLE IF NOT EXISTS `tbl_entries_data_ does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal ` (\n `id` ...OLLATE=utf8_unicode_ci; does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Bug introduced by
Are you sure $this->get('id') of type array|mixed|null can be used in concatenation? ( Ignorable by Annotation )

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

110
            "CREATE TABLE IF NOT EXISTS `tbl_entries_data_" . /** @scrutinizer ignore-type */ $this->get('id') . "` (
Loading history...
111
              `id` int(11) unsigned NOT null auto_increment,
112
              `entry_id` int(11) unsigned NOT null,
113
              `handle` varchar(255) default null,
114
              `value` varchar(255) default null,
115
              PRIMARY KEY  (`id`),
116
              KEY `entry_id` (`entry_id`),
117
              KEY `handle` (`handle`),
118
              KEY `value` (`value`)
119
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"
120
        );
121
    }
122
123
    /*-------------------------------------------------------------------------
124
        Utilities:
125
    -------------------------------------------------------------------------*/
126
127
    public function findAndAddDynamicOptions(&$values)
128
    {
129
        if (!is_array($values)) {
130
            $values = array();
131
        }
132
133
        $results = false;
134
135
        // Fixes #1802
136
        if (!Symphony::Database()->tableExists('tbl_entries_data_' . $this->get('dynamic_options'))) {
0 ignored issues
show
Bug introduced by
Are you sure $this->get('dynamic_options') of type array|mixed|null can be used in concatenation? ( Ignorable by Annotation )

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

136
        if (!Symphony::Database()->tableExists('tbl_entries_data_' . /** @scrutinizer ignore-type */ $this->get('dynamic_options'))) {
Loading history...
137
            return;
138
        }
139
140
        // Ensure that the table has a 'value' column
141
        if ((boolean)Symphony::Database()->fetchVar('Field', 0, sprintf(
142
            "SHOW COLUMNS FROM `tbl_entries_data_%d` LIKE '%s'",
143
            $this->get('dynamic_options'),
0 ignored issues
show
Bug introduced by
It seems like $this->get('dynamic_options') 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

143
            /** @scrutinizer ignore-type */ $this->get('dynamic_options'),
Loading history...
144
            'value'
145
        ))) {
146
            $results = Symphony::Database()->fetchCol('value', sprintf(
147
                "SELECT DISTINCT `value`
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal SELECT DISTINCT `value`\... ORDER BY `value` ASC does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
148
                FROM `tbl_entries_data_%d`
149
                ORDER BY `value` ASC",
150
                $this->get('dynamic_options')
151
            ));
152
        }
153
154
        // In the case of a Upload field, use 'file' instead of 'value'
155
        if (($results == false) && (boolean)Symphony::Database()->fetchVar('Field', 0, sprintf(
156
            "SHOW COLUMNS FROM `tbl_entries_data_%d` LIKE '%s'",
157
            $this->get('dynamic_options'),
158
            'file'
159
        ))) {
160
            $results = Symphony::Database()->fetchCol('file', sprintf(
161
                "SELECT DISTINCT `file`
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal SELECT DISTINCT `file`\n... ORDER BY `file` ASC does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
162
                FROM `tbl_entries_data_%d`
163
                ORDER BY `file` ASC",
164
                $this->get('dynamic_options')
165
            ));
166
        }
167
168
        if ($results) {
169
            if ($this->get('sort_options') == 'no') {
170
                natsort($results);
171
            }
172
173
            $values = array_merge($values, $results);
174
        }
175
    }
176
177
    /*-------------------------------------------------------------------------
178
        Settings:
179
    -------------------------------------------------------------------------*/
180
181
    public function findDefaults(array &$settings)
182
    {
183
        if (!isset($settings['allow_multiple_selection'])) {
184
            $settings['allow_multiple_selection'] = 'no';
185
        }
186
187
        if (!isset($settings['show_association'])) {
188
            $settings['show_association'] = 'no';
189
        }
190
191
        if (!isset($settings['sort_options'])) {
192
            $settings['sort_options'] = 'no';
193
        }
194
    }
195
196
    public function displaySettingsPanel(XMLElement &$wrapper, $errors = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$errors" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$errors"; expected 0 but found 1
Loading history...
197
    {
198
        Field::displaySettingsPanel($wrapper, $errors);
199
200
        $div = new XMLElement('div', null, array('class' => 'two columns'));
201
202
        // Predefined Values
203
        $label = Widget::Label(__('Static Values'));
204
        $label->setAttribute('class', 'column');
205
        $label->appendChild(new XMLElement('i', __('Optional')));
206
        $input = Widget::Input('fields['.$this->get('sortorder').'][static_options]', General::sanitize($this->get('static_options')));
0 ignored issues
show
Bug introduced by
Are you sure $this->get('sortorder') of type array|mixed|null can be used in concatenation? ( Ignorable by Annotation )

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

206
        $input = Widget::Input('fields['./** @scrutinizer ignore-type */ $this->get('sortorder').'][static_options]', General::sanitize($this->get('static_options')));
Loading history...
Bug introduced by
It seems like $this->get('static_options') can also be of type array; however, parameter $source of General::sanitize() 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

206
        $input = Widget::Input('fields['.$this->get('sortorder').'][static_options]', General::sanitize(/** @scrutinizer ignore-type */ $this->get('static_options')));
Loading history...
207
        $label->appendChild($input);
208
        $div->appendChild($label);
209
210
        // Dynamic Values
211
        // Only append selected ids, load full section information asynchronously
212
        $label = Widget::Label(__('Dynamic Values'));
213
        $label->setAttribute('class', 'column');
214
        $label->appendChild(new XMLElement('i', __('Optional')));
215
216
        $options = array(
217
            array('', false, __('None'))
218
        );
219
220
        if ($this->get('dynamic_options')) {
221
            $options[] = array($this->get('dynamic_options'));
222
        }
223
224
        $label->appendChild(
225
            Widget::Select('fields['.$this->get('sortorder').'][dynamic_options]', $options, array(
226
                'class' => 'js-fetch-sections'
227
            ))
228
        );
229
230
        if (isset($errors['dynamic_options'])) {
231
            $div->appendChild(Widget::Error($label, $errors['dynamic_options']));
232
        } else {
233
            $div->appendChild($label);
234
        }
235
236
        $wrapper->appendChild($div);
237
238
        // Other settings
239
        $div = new XMLElement('div', null, array('class' => 'two columns'));
240
241
        // Allow selection of multiple items
242
        $this->createCheckboxSetting($div, 'allow_multiple_selection', __('Allow selection of multiple options'));
243
244
        // Sort options?
245
        $this->createCheckboxSetting($div, 'sort_options', __('Sort all options alphabetically'));
246
247
        $wrapper->appendChild($div);
248
249
        // Associations
250
        $fieldset = new XMLElement('fieldset');
251
        $this->appendAssociationInterfaceSelect($fieldset);
252
        $this->appendShowAssociationCheckbox($fieldset);
253
        $wrapper->appendChild($fieldset);
254
255
        // Requirements and table display
256
        $this->appendStatusFooter($wrapper);
257
    }
258
259
    public function checkFields(array &$errors, $checkForDuplicates = true)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$checkForDuplicates" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$checkForDuplicates"; expected 0 but found 1
Loading history...
260
    {
261
        if (!is_array($errors)) {
0 ignored issues
show
introduced by
The condition is_array($errors) is always true.
Loading history...
262
            $errors = array();
263
        }
264
265
        if ($this->get('static_options') == '' && ($this->get('dynamic_options') == '' || $this->get('dynamic_options') == 'none')) {
266
            $errors['dynamic_options'] = __('At least one source must be specified, dynamic or static.');
267
        }
268
269
        Field::checkFields($errors, $checkForDuplicates);
270
    }
271
272
    public function commit()
273
    {
274
        if (!Field::commit()) {
275
            return false;
276
        }
277
278
        $id = $this->get('id');
279
280
        if ($id === false) {
281
            return false;
282
        }
283
284
        $fields = array();
285
286
        if ($this->get('static_options') != '') {
287
            $fields['static_options'] = $this->get('static_options');
288
        }
289
290
        if ($this->get('dynamic_options') != '') {
291
            $fields['dynamic_options'] = $this->get('dynamic_options');
292
        }
293
294
        $fields['allow_multiple_selection'] = ($this->get('allow_multiple_selection') ? $this->get('allow_multiple_selection') : 'no');
295
        $fields['sort_options'] = $this->get('sort_options') === 'yes' ? 'yes' : 'no';
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
296
297
        if (!FieldManager::saveSettings($id, $fields)) {
0 ignored issues
show
Bug introduced by
It seems like $id can also be of type array; however, parameter $field_id of FieldManager::saveSettings() does only seem to accept integer, 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

297
        if (!FieldManager::saveSettings(/** @scrutinizer ignore-type */ $id, $fields)) {
Loading history...
298
            return false;
299
        }
300
301
        SectionManager::removeSectionAssociation($id);
0 ignored issues
show
Bug introduced by
It seems like $id can also be of type array; however, parameter $field_id of SectionManager::removeSectionAssociation() does only seem to accept integer, 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

301
        SectionManager::removeSectionAssociation(/** @scrutinizer ignore-type */ $id);
Loading history...
302
303
        // Dynamic Options isn't an array like in Select Box Link
304
        $field_id = $this->get('dynamic_options');
305
306
        if (!is_null($field_id) && is_numeric($field_id)) {
307
            SectionManager::createSectionAssociation(null, $id, (int)$field_id, $this->get('show_association') === 'yes' ? true : false, $this->get('association_ui'), $this->get('association_editor'));
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
Bug introduced by
It seems like $id can also be of type array; however, parameter $child_field_id of SectionManager::createSectionAssociation() does only seem to accept integer, 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

307
            SectionManager::createSectionAssociation(null, /** @scrutinizer ignore-type */ $id, (int)$field_id, $this->get('show_association') === 'yes' ? true : false, $this->get('association_ui'), $this->get('association_editor'));
Loading history...
308
        }
309
310
        return true;
311
    }
312
313
    /*-------------------------------------------------------------------------
314
        Publish:
315
    -------------------------------------------------------------------------*/
316
317
    public function displayPublishPanel(XMLElement &$wrapper, $data = null, $flagWithError = null, $fieldnamePrefix = null, $fieldnamePostfix = null, $entry_id = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$data" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$data"; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$flagWithError" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$flagWithError"; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$fieldnamePrefix" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$fieldnamePrefix"; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$fieldnamePostfix" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$fieldnamePostfix"; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$entry_id" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$entry_id"; expected 0 but found 1
Loading history...
318
    {
319
        $states = $this->getToggleStates();
320
        $value = isset($data['value']) ? $data['value'] : null;
321
322
        if (!is_array($value)) {
323
            $value = array($value);
324
        }
325
326
        $options = array();
327
        if ($this->get('required') !== 'yes') {
328
            $options[] = array(null, false, null);
329
        }
330
331
        foreach ($states as $handle => $v) {
332
            $options[] = array(General::sanitize($v), in_array($v, $value), General::sanitize($v));
333
        }
334
335
        $fieldname = 'fields'.$fieldnamePrefix.'['.$this->get('element_name').']'.$fieldnamePostfix;
0 ignored issues
show
Bug introduced by
Are you sure $this->get('element_name') of type array|mixed|null can be used in concatenation? ( Ignorable by Annotation )

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

335
        $fieldname = 'fields'.$fieldnamePrefix.'['./** @scrutinizer ignore-type */ $this->get('element_name').']'.$fieldnamePostfix;
Loading history...
336
337
        if ($this->get('allow_multiple_selection') === 'yes') {
338
            $fieldname .= '[]';
339
        }
340
341
        $label = Widget::Label($this->get('label'));
0 ignored issues
show
Bug introduced by
It seems like $this->get('label') can also be of type array; however, parameter $name of Widget::Label() 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

341
        $label = Widget::Label(/** @scrutinizer ignore-type */ $this->get('label'));
Loading history...
342
343
        if ($this->get('required') !== 'yes') {
344
            $label->appendChild(new XMLElement('i', __('Optional')));
345
        }
346
347
        $label->appendChild(Widget::Select($fieldname, $options, ($this->get('allow_multiple_selection') === 'yes' ? array('multiple' => 'multiple', 'size' => count($options)) : null)));
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
348
349
        if ($flagWithError != null) {
350
            $wrapper->appendChild(Widget::Error($label, $flagWithError));
351
        } else {
352
            $wrapper->appendChild($label);
353
        }
354
    }
355
356
    public function checkPostFieldData($data, &$message, $entry_id = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$entry_id" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$entry_id"; expected 0 but found 1
Loading history...
357
    {
358
        return Field::checkPostFieldData($data, $message, $entry_id);
359
    }
360
361
    public function processRawFieldData($data, &$status, &$message = null, $simulate = false, $entry_id = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$message" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$message"; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$simulate" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$simulate"; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$entry_id" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$entry_id"; expected 0 but found 1
Loading history...
362
    {
363
        $status = self::__OK__;
364
365
        if (!is_array($data)) {
366
            return array(
367
                'value' => $data,
368
                'handle' => Lang::createHandle($data)
369
            );
370
        }
371
372
        if (empty($data)) {
373
            return null;
374
        }
375
376
        $result = array(
377
            'value' => array(),
378
            'handle' => array()
379
        );
380
381
        foreach ($data as $value) {
382
            $result['value'][] = $value;
383
            $result['handle'][] = Lang::createHandle($value);
384
        }
385
386
        return $result;
387
    }
388
389
    /*-------------------------------------------------------------------------
390
        Output:
391
    -------------------------------------------------------------------------*/
392
393
    public function prepareTextValue($data, $entry_id = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$entry_id" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$entry_id"; expected 0 but found 1
Loading history...
394
    {
395
        $value = $this->prepareExportValue($data, ExportableField::LIST_OF + ExportableField::VALUE, $entry_id);
396
397
        return implode(', ', $value);
398
    }
399
400
    /*-------------------------------------------------------------------------
401
        Import:
402
    -------------------------------------------------------------------------*/
403
404
    public function prepareImportValue($data, $mode, $entry_id = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$entry_id" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$entry_id"; expected 0 but found 1
Loading history...
405
    {
406
        $message = $status = null;
407
        $modes = (object)$this->getImportModes();
408
409
        if (!is_array($data)) {
410
            $data = array($data);
411
        }
412
413
        if ($mode === $modes->getValue) {
414
            if ($this->get('allow_multiple_selection') === 'no') {
415
                $data = array(implode('', $data));
416
            }
417
418
            return $data;
419
        } elseif ($mode === $modes->getPostdata) {
420
            return $this->processRawFieldData($data, $status, $message, true, $entry_id);
421
        }
422
423
        return null;
424
    }
425
426
/*-------------------------------------------------------------------------
427
    Export:
428
-------------------------------------------------------------------------*/
429
430
    /**
431
     * Return a list of supported export modes for use with `prepareExportValue`.
432
     *
433
     * @return array
434
     */
435
    public function getExportModes()
436
    {
437
        return array(
438
            'listHandle' =>         ExportableField::LIST_OF
439
                                    + ExportableField::HANDLE,
440
            'listValue' =>          ExportableField::LIST_OF
441
                                    + ExportableField::VALUE,
442
            'listHandleToValue' =>  ExportableField::LIST_OF
443
                                    + ExportableField::HANDLE
444
                                    + ExportableField::VALUE,
445
            'getPostdata' =>        ExportableField::POSTDATA
446
        );
447
    }
448
449
    /**
450
     * Give the field some data and ask it to return a value using one of many
451
     * possible modes.
452
     *
453
     * @param mixed $data
454
     * @param integer $mode
455
     * @param integer $entry_id
456
     * @return array
457
     */
458
    public function prepareExportValue($data, $mode, $entry_id = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$entry_id" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$entry_id"; expected 0 but found 1
Loading history...
459
    {
460
        $modes = (object)$this->getExportModes();
461
462
        if (isset($data['handle']) && is_array($data['handle']) === false) {
463
            $data['handle'] = array(
464
                $data['handle']
465
            );
466
        }
467
468
        if (isset($data['value']) && is_array($data['value']) === false) {
469
            $data['value'] = array(
470
                $data['value']
471
            );
472
        }
473
474
        // Handle => Value pairs:
475
        if ($mode === $modes->listHandleToValue) {
476
            return isset($data['handle'], $data['value'])
0 ignored issues
show
Bug Best Practice introduced by
The expression return IssetNode ? array...ata['value']) : array() could also return false which is incompatible with the documented return type array. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
477
                ? array_combine($data['handle'], $data['value'])
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement must be declared on a single line
Loading history...
478
                : array();
479
480
            // Array of handles:
481
        } elseif ($mode === $modes->listHandle) {
482
            return isset($data['handle'])
483
                ? $data['handle']
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement must be declared on a single line
Loading history...
484
                : array();
485
486
            // Array of values:
487
        } elseif ($mode === $modes->listValue || $mode === $modes->getPostdata) {
488
            return isset($data['value'])
489
                ? $data['value']
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement must be declared on a single line
Loading history...
490
                : array();
491
        }
492
    }
493
494
    /*-------------------------------------------------------------------------
495
        Filtering:
496
    -------------------------------------------------------------------------*/
497
498
    public function displayFilteringOptions(XMLElement &$wrapper)
499
    {
500
        $existing_options = $this->getToggleStates();
501
502
        if (is_array($existing_options) && !empty($existing_options)) {
503
            $optionlist = new XMLElement('ul');
504
            $optionlist->setAttribute('class', 'tags');
505
            $optionlist->setAttribute('data-interactive', 'data-interactive');
506
507
            foreach ($existing_options as $option) {
508
                $optionlist->appendChild(
509
                    new XMLElement('li', General::sanitize($option))
510
                );
511
            };
512
513
            $wrapper->appendChild($optionlist);
514
        }
515
    }
516
517
    /*-------------------------------------------------------------------------
518
        Grouping:
519
    -------------------------------------------------------------------------*/
520
521
    public function groupRecords($records)
522
    {
523
        if (!is_array($records) || empty($records)) {
524
            return;
525
        }
526
527
        $groups = array($this->get('element_name') => array());
528
529
        foreach ($records as $r) {
530
            $data = $r->getData($this->get('id'));
531
            $value = General::sanitize($data['value']);
532
533
            if (!isset($groups[$this->get('element_name')][$data['handle']])) {
534
                $groups[$this->get('element_name')][$data['handle']] = array(
535
                    'attr' => array('handle' => $data['handle'], 'value' => $value),
536
                    'records' => array(),
537
                    'groups' => array()
538
                );
539
            }
540
541
            $groups[$this->get('element_name')][$data['handle']]['records'][] = $r;
542
        }
543
544
        return $groups;
545
    }
546
547
    /*-------------------------------------------------------------------------
548
        Events:
549
    -------------------------------------------------------------------------*/
550
551
    public function getExampleFormMarkup()
552
    {
553
        $states = $this->getToggleStates();
554
555
        $options = array();
556
557
        foreach ($states as $handle => $v) {
558
            $options[] = array($v, null, $v);
559
        }
560
561
        $fieldname = 'fields['.$this->get('element_name').']';
0 ignored issues
show
Bug introduced by
Are you sure $this->get('element_name') of type array|mixed|null can be used in concatenation? ( Ignorable by Annotation )

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

561
        $fieldname = 'fields['./** @scrutinizer ignore-type */ $this->get('element_name').']';
Loading history...
562
563
        if ($this->get('allow_multiple_selection') === 'yes') {
564
            $fieldname .= '[]';
565
        }
566
567
        $label = Widget::Label($this->get('label'));
0 ignored issues
show
Bug introduced by
It seems like $this->get('label') can also be of type array; however, parameter $name of Widget::Label() 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

567
        $label = Widget::Label(/** @scrutinizer ignore-type */ $this->get('label'));
Loading history...
568
        $label->appendChild(Widget::Select($fieldname, $options, ($this->get('allow_multiple_selection') === 'yes' ? array('multiple' => 'multiple') : null)));
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
569
570
        return $label;
571
    }
572
}
573