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.
Completed
Pull Request — 2.6.x (#2512)
by Nicolas
04:52
created

FieldInput::appendFormattedElement()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 21
rs 9.0534
cc 4
eloc 12
nc 4
nop 5
1
<?php
2
3
/**
4
 * @package toolkit
5
 */
6
7
/**
8
 * A simple Input field that essentially maps to HTML's `<input type='text'/>`.
9
 */
10
class FieldInput extends Field implements ExportableField, ImportableField
11
{
12 View Code Duplication
    public function __construct()
13
    {
14
        parent::__construct();
15
        $this->_name = __('Text Input');
0 ignored issues
show
Bug introduced by
The property _name does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
16
        $this->_required = true;
17
18
        $this->set('required', 'no');
19
    }
20
21
    /*-------------------------------------------------------------------------
22
        Definition:
23
    -------------------------------------------------------------------------*/
24
25
    public function canFilter()
26
    {
27
        return true;
28
    }
29
30
    public function canPrePopulate()
31
    {
32
        return true;
33
    }
34
35
    public function isSortable()
36
    {
37
        return true;
38
    }
39
40
    public function allowDatasourceOutputGrouping()
41
    {
42
        return true;
43
    }
44
45
    public function allowDatasourceParamOutput()
46
    {
47
        return true;
48
    }
49
50
    /*-------------------------------------------------------------------------
51
        Setup:
52
    -------------------------------------------------------------------------*/
53
54
    public function createTable()
55
    {
56
        return Symphony::Database()->query(
57
            "CREATE TABLE IF NOT EXISTS `tbl_entries_data_" . $this->get('id') . "` (
58
              `id` int(11) unsigned NOT null auto_increment,
59
              `entry_id` int(11) unsigned NOT null,
60
              `handle` varchar(255) default null,
61
              `value` varchar(255) default null,
62
              PRIMARY KEY  (`id`),
63
              UNIQUE KEY `entry_id` (`entry_id`),
64
              KEY `handle` (`handle`),
65
              KEY `value` (`value`)
66
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"
67
        );
68
    }
69
70
    /*-------------------------------------------------------------------------
71
        Utilities:
72
    -------------------------------------------------------------------------*/
73
74
    private function __applyValidationRules($data)
75
    {
76
        $rule = $this->get('validator');
77
78
        return ($rule ? General::validateString($data, $rule) : true);
79
    }
80
81
    private function __replaceAmpersands($value)
82
    {
83
        return preg_replace('/&(?!(#[0-9]+|#x[0-9a-f]+|amp|lt|gt);)/i', '&amp;', trim($value));
84
    }
85
86
    /*-------------------------------------------------------------------------
87
        Settings:
88
    -------------------------------------------------------------------------*/
89
90
    public function setFromPOST(array $settings = array())
91
    {
92
        parent::setFromPOST($settings);
93
94
        if ($this->get('validator') == '') {
95
            $this->remove('validator');
96
        }
97
    }
98
99
    public function displaySettingsPanel(XMLElement &$wrapper, $errors = null)
100
    {
101
        parent::displaySettingsPanel($wrapper, $errors);
102
103
        // Validation rule
104
        $this->buildValidationSelect($wrapper, $this->get('validator'), 'fields['.$this->get('sortorder').'][validator]', 'input', $errors);
105
106
        // Requirements and table display
107
        $this->appendStatusFooter($wrapper);
108
    }
109
110 View Code Duplication
    public function commit()
111
    {
112
        if (!parent::commit()) {
113
            return false;
114
        }
115
116
        $id = $this->get('id');
117
118
        if ($id === false) {
119
            return false;
120
        }
121
122
        $fields = array('validator'=>null);
123
124
        $fields['validator'] = ($fields['validator'] == 'custom' ? null : $this->get('validator'));
125
126
        return FieldManager::saveSettings($id, $fields);
127
    }
128
129
    /*-------------------------------------------------------------------------
130
        Publish:
131
    -------------------------------------------------------------------------*/
132
133
    public function displayPublishPanel(XMLElement &$wrapper, $data = null, $flagWithError = null, $fieldnamePrefix = null, $fieldnamePostfix = null, $entry_id = null)
134
    {
135
        $value = General::sanitize(isset($data['value']) ? $data['value'] : null);
136
        $label = Widget::Label($this->get('label'));
137
138
        if ($this->get('required') !== 'yes') {
139
            $label->appendChild(new XMLElement('i', __('Optional')));
140
        }
141
142
        $label->appendChild(Widget::Input('fields'.$fieldnamePrefix.'['.$this->get('element_name').']'.$fieldnamePostfix, (strlen($value) != 0 ? $value : null)));
143
144
        if ($flagWithError != null) {
145
            $wrapper->appendChild(Widget::Error($label, $flagWithError));
146
        } else {
147
            $wrapper->appendChild($label);
148
        }
149
    }
150
151
    public function checkPostFieldData($data, &$message, $entry_id = null)
152
    {
153
        $message = null;
154
155
        if (is_array($data) && isset($data['value'])) {
156
            $data = $data['value'];
157
        }
158
159 View Code Duplication
        if ($this->get('required') === 'yes' && strlen(trim($data)) == 0) {
160
            $message = __('‘%s’ is a required field.', array($this->get('label')));
161
            return self::__MISSING_FIELDS__;
162
        }
163
164 View Code Duplication
        if (!$this->__applyValidationRules($data)) {
165
            $message = __('‘%s’ contains invalid data. Please check the contents.', array($this->get('label')));
166
            return self::__INVALID_FIELDS__;
167
        }
168
169
        return self::__OK__;
170
    }
171
172
    public function processRawFieldData($data, &$status, &$message = null, $simulate = false, $entry_id = null)
173
    {
174
        $status = self::__OK__;
175
176
        if (strlen(trim($data)) == 0) {
177
            return array();
178
        }
179
180
        $result = array(
181
            'value' => $data
182
        );
183
184
        $result['handle'] = Lang::createHandle($result['value']);
185
186
        return $result;
187
    }
188
189
    /*-------------------------------------------------------------------------
190
        Output:
191
    -------------------------------------------------------------------------*/
192
193
    public function appendFormattedElement(XMLElement &$wrapper, $data, $encode = false, $mode = null, $entry_id = null)
194
    {
195
        $value = $data['value'];
196
197
        if ($encode === true) {
198
            $value = General::sanitize($value);
199
        } else {
200
            if (!General::validateXML($data['value'], $errors, false, new XsltProcess)) {
201
                $value = html_entity_decode($data['value'], ENT_QUOTES, 'UTF-8');
202
                $value = $this->__replaceAmpersands($value);
203
204
                if (!General::validateXML($value, $errors, false, new XsltProcess)) {
205
                    $value = General::sanitize($data['value']);
206
                }
207
            }
208
        }
209
210
        $wrapper->appendChild(
211
            new XMLElement($this->get('element_name'), $value, array('handle' => $data['handle']))
212
        );
213
    }
214
215
    /*-------------------------------------------------------------------------
216
        Import:
217
    -------------------------------------------------------------------------*/
218
219
    public function getImportModes()
220
    {
221
        return array(
222
            'getValue' =>       ImportableField::STRING_VALUE,
223
            'getPostdata' =>    ImportableField::ARRAY_VALUE
224
        );
225
    }
226
227 View Code Duplication
    public function prepareImportValue($data, $mode, $entry_id = null)
228
    {
229
        $message = $status = null;
230
        $modes = (object)$this->getImportModes();
231
232
        if ($mode === $modes->getValue) {
233
            return $data;
234
        } elseif ($mode === $modes->getPostdata) {
235
            return $this->processRawFieldData($data, $status, $message, true, $entry_id);
236
        }
237
238
        return null;
239
    }
240
241
    /*-------------------------------------------------------------------------
242
        Export:
243
    -------------------------------------------------------------------------*/
244
245
    /**
246
     * Return a list of supported export modes for use with `prepareExportValue`.
247
     *
248
     * @return array
249
     */
250
    public function getExportModes()
251
    {
252
        return array(
253
            'getHandle' =>      ExportableField::HANDLE,
254
            'getUnformatted' => ExportableField::UNFORMATTED,
255
            'getPostdata' =>    ExportableField::POSTDATA
256
        );
257
    }
258
259
    /**
260
     * Give the field some data and ask it to return a value using one of many
261
     * possible modes.
262
     *
263
     * @param mixed $data
264
     * @param integer $mode
265
     * @param integer $entry_id
266
     * @return string|null
267
     */
268
    public function prepareExportValue($data, $mode, $entry_id = null)
269
    {
270
        $modes = (object)$this->getExportModes();
271
272
        // Export handles:
273
        if ($mode === $modes->getHandle) {
274 View Code Duplication
            if (isset($data['handle'])) {
275
                return $data['handle'];
276
            } elseif (isset($data['value'])) {
277
                return Lang::createHandle($data['value']);
278
            }
279
280
            // Export unformatted:
281 View Code Duplication
        } elseif ($mode === $modes->getUnformatted || $mode === $modes->getPostdata) {
282
            return isset($data['value'])
283
                ? $data['value']
284
                : null;
285
        }
286
287
        return null;
288
    }
289
290
    /*-------------------------------------------------------------------------
291
        Filtering:
292
    -------------------------------------------------------------------------*/
293
294 View Code Duplication
    public function buildDSRetrievalSQL($data, &$joins, &$where, $andOperation = false)
295
    {
296
        $field_id = $this->get('id');
297
298
        if (self::isFilterRegex($data[0])) {
299
            $this->buildRegexSQL($data[0], array('value', 'handle'), $joins, $where);
300
        } elseif ($andOperation) {
301
            foreach ($data as $value) {
302
                $this->_key++;
303
                $value = $this->cleanValue($value);
304
                $joins .= "
305
                    LEFT JOIN
306
                        `tbl_entries_data_{$field_id}` AS t{$field_id}_{$this->_key}
307
                        ON (e.id = t{$field_id}_{$this->_key}.entry_id)
308
                ";
309
                $where .= "
310
                    AND (
311
                        t{$field_id}_{$this->_key}.value = '{$value}'
312
                        OR t{$field_id}_{$this->_key}.handle = '{$value}'
313
                    )
314
                ";
315
            }
316
        } else {
317
            if (!is_array($data)) {
318
                $data = array($data);
319
            }
320
321
            foreach ($data as &$value) {
322
                $value = $this->cleanValue($value);
323
            }
324
325
            $this->_key++;
326
            $data = implode("', '", $data);
327
            $joins .= "
328
                LEFT JOIN
329
                    `tbl_entries_data_{$field_id}` AS t{$field_id}_{$this->_key}
330
                    ON (e.id = t{$field_id}_{$this->_key}.entry_id)
331
            ";
332
            $where .= "
333
                AND (
334
                    t{$field_id}_{$this->_key}.value IN ('{$data}')
335
                    OR t{$field_id}_{$this->_key}.handle IN ('{$data}')
336
                )
337
            ";
338
        }
339
340
        return true;
341
    }
342
343
    /*-------------------------------------------------------------------------
344
        Sorting:
345
    -------------------------------------------------------------------------*/
346
347
    public function buildSortingSQL(&$joins, &$where, &$sort, $order = 'ASC')
348
    {
349
        if (in_array(strtolower($order), array('random', 'rand'))) {
350
            $sort = 'ORDER BY RAND()';
351
        } else {
352
            $sort = sprintf(
353
                'ORDER BY (
354
                    SELECT %s
355
                    FROM tbl_entries_data_%d AS `ed`
356
                    WHERE entry_id = e.id
357
                ) %s',
358
                '`ed`.value',
359
                $this->get('id'),
360
                $order
361
            );
362
        }
363
    }
364
365
    /*-------------------------------------------------------------------------
366
        Grouping:
367
    -------------------------------------------------------------------------*/
368
369 View Code Duplication
    public function groupRecords($records)
370
    {
371
        if (!is_array($records) || empty($records)) {
372
            return;
373
        }
374
375
        $groups = array($this->get('element_name') => array());
376
377
        foreach ($records as $r) {
378
            $data = $r->getData($this->get('id'));
379
            $value = General::sanitize($data['value']);
380
381
            if (!isset($groups[$this->get('element_name')][$data['handle']])) {
382
                $groups[$this->get('element_name')][$data['handle']] = array(
383
                    'attr' => array('handle' => $data['handle'], 'value' => $value),
384
                    'records' => array(),
385
                    'groups' => array()
386
                );
387
            }
388
389
            $groups[$this->get('element_name')][$data['handle']]['records'][] = $r;
390
        }
391
392
        return $groups;
393
    }
394
}
395