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.
Passed
Push — master ( 2240c6...8ae5ca )
by Nicolas
04:17
created

FieldAuthor::displayPublishPanel()   F

Complexity

Conditions 17
Paths 1536

Size

Total Lines 65
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 33
c 0
b 0
f 0
nc 1536
nop 6
dl 0
loc 65
rs 2.7789

How to fix   Long Method    Complexity   

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
/**
4
 * @package toolkit
5
 */
6
7
/**
8
 * The Author field allows Symphony Authors to be selected in your entries.
9
 * It is a read only field, new Authors cannot be added from the Frontend using
10
 * events.
11
 *
12
 * The Author field allows filtering by Author ID or Username.
13
 * Sorting is done based on the Author's first name and last name.
14
 */
15
class FieldAuthor extends Field implements ExportableField
16
{
17
    public function __construct()
18
    {
19
        parent::__construct();
20
        $this->_name = __('Author');
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...
21
        $this->_required = true;
22
23
        $this->set('author_types', array());
24
    }
25
26
    /*-------------------------------------------------------------------------
27
        Definition:
28
    -------------------------------------------------------------------------*/
29
30
    public function canToggle()
31
    {
32
        return ($this->get('allow_multiple_selection') === 'yes' ? false : true);
33
    }
34
35
    public function getToggleStates()
36
    {
37
        $authors = AuthorManager::fetch();
38
39
        $states = array();
40
        foreach ($authors as $a) {
41
            $states[$a->get('id')] = $a->getFullName();
42
        }
43
44
        return $states;
45
    }
46
47
    public function toggleFieldData(array $data, $newState, $entry_id = null)
48
    {
49
        $data['author_id'] = $newState;
50
        return $data;
51
    }
52
53
    public function canFilter()
54
    {
55
        return true;
56
    }
57
58
    public function isSortable()
59
    {
60
        return $this->canToggle();
61
    }
62
63
    public function allowDatasourceOutputGrouping()
64
    {
65
        // Grouping follows the same rule as toggling.
66
        return $this->canToggle();
67
    }
68
69
    public function allowDatasourceParamOutput()
70
    {
71
        return true;
72
    }
73
74
    public function fetchSuggestionTypes()
75
    {
76
        return array('static');
77
    }
78
79
    /*-------------------------------------------------------------------------
80
        Setup:
81
    -------------------------------------------------------------------------*/
82
83
    public function createTable()
84
    {
85
        return Symphony::Database()->query(
86
            "CREATE TABLE IF NOT EXISTS `tbl_entries_data_" . $this->get('id') ."` (
0 ignored issues
show
Bug introduced by
Are you sure $this->get('id') of type null|array|mixed 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

86
            "CREATE TABLE IF NOT EXISTS `tbl_entries_data_" . /** @scrutinizer ignore-type */ $this->get('id') ."` (
Loading history...
87
              `id` int(11) unsigned NOT null auto_increment,
88
              `entry_id` int(11) unsigned NOT null,
89
              `author_id` int(11) unsigned null,
90
              PRIMARY KEY  (`id`),
91
              UNIQUE KEY `author` (`entry_id`, `author_id`),
92
              KEY `author_id` (`author_id`)
93
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"
94
        );
95
    }
96
97
    /*-------------------------------------------------------------------------
98
        Utilities:
99
    -------------------------------------------------------------------------*/
100
101
    public function set($field, $value)
102
    {
103
        if ($field === 'author_types' && !is_array($value)) {
104
            $value = explode(',', $value);
105
        }
106
107
        $this->_settings[$field] = $value;
108
    }
109
110
    /**
111
     * Determines based on the input value whether we want to filter the Author
112
     * field by ID or by the Author's Username
113
     *
114
     * @since Symphony 2.2
115
     * @param string $value
116
     * @return string
117
     *  Either `author_id` or `username`
118
     */
119
    private static function __parseFilter($value)
120
    {
121
        return is_numeric($value) ? 'author_id' : 'username';
122
    }
123
124
    /*-------------------------------------------------------------------------
125
        Settings:
126
    -------------------------------------------------------------------------*/
127
128
    public function findDefaults(array &$settings)
129
    {
130
        if (!isset($settings['allow_multiple_selection'])) {
131
            $settings['allow_multiple_selection'] = 'no';
132
        }
133
134
        if (!isset($settings['author_types'])) {
135
            $settings['author_types'] = array('developer', 'manager', 'author');
136
        }
137
    }
138
139
    public function displaySettingsPanel(XMLElement &$wrapper, $errors = null)
140
    {
141
        parent::displaySettingsPanel($wrapper, $errors);
142
143
        // Author types
144
        $label = Widget::Label(__('Author types'));
145
        $types = $this->get('author_types');
146
        $options = array(
147
            array('author', empty($types) ? true : in_array('author', $types), __('Author')),
148
            array('manager', empty($types) ? true : in_array('manager', $types), __('Manager')),
149
            array('developer', empty($types) ? true : in_array('developer', $types), __('Developer'))
150
        );
151
        $label->appendChild(
152
            Widget::Select('fields['.$this->get('sortorder').'][author_types][]', $options, array(
0 ignored issues
show
Bug introduced by
Are you sure $this->get('sortorder') of type null|array|mixed 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

152
            Widget::Select('fields['./** @scrutinizer ignore-type */ $this->get('sortorder').'][author_types][]', $options, array(
Loading history...
153
                'multiple' => 'multiple'
154
            ))
155
        );
156
157
        if (isset($errors['author_types'])) {
158
            $wrapper->appendChild(Widget::Error($label, $errors['author_types']));
159
        } else {
160
            $wrapper->appendChild($label);
161
        }
162
163
        // Options
164
        $div = new XMLElement('div', null, array('class' => 'two columns'));
165
166
        // Allow multiple selection
167
        $this->createCheckboxSetting($div, 'allow_multiple_selection', __('Allow selection of multiple authors'));
168
169
        // Default to current logged in user
170
        $this->createCheckboxSetting($div, 'default_to_current_user', __('Select current user by default'));
171
172
        // Requirements and table display
173
        $wrapper->appendChild($div);
174
        $this->appendStatusFooter($wrapper);
175
    }
176
177
    public function checkFields(array &$errors, $checkForDuplicates = true)
178
    {
179
        parent::checkFields($errors, $checkForDuplicates);
180
181
        $types = $this->get('author_types');
182
183
        if (empty($types)) {
184
            $errors['author_types'] = __('This is a required field.');
185
        }
186
187
        return (is_array($errors) && !empty($errors) ? self::__ERROR__ : self::__OK__);
188
    }
189
190
    public function commit()
191
    {
192
        if (!parent::commit()) {
193
            return false;
194
        }
195
196
        $id = $this->get('id');
197
198
        if ($id === false) {
199
            return false;
200
        }
201
202
        $fields = array();
203
204
        $fields['allow_multiple_selection'] = ($this->get('allow_multiple_selection') ? $this->get('allow_multiple_selection') : 'no');
205
        $fields['default_to_current_user'] = ($this->get('default_to_current_user') ? $this->get('default_to_current_user') : 'no');
206
207
        if ($this->get('author_types') != '') {
208
            $fields['author_types'] = implode(',', $this->get('author_types'));
209
        }
210
211
        return 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

211
        return FieldManager::saveSettings(/** @scrutinizer ignore-type */ $id, $fields);
Loading history...
212
    }
213
214
    /*-------------------------------------------------------------------------
215
        Publish:
216
    -------------------------------------------------------------------------*/
217
218
    public function displayPublishPanel(XMLElement &$wrapper, $data = null, $flagWithError = null, $fieldnamePrefix = null, $fieldnamePostfix = null, $entry_id = null)
219
    {
220
        $value = isset($data['author_id']) ? $data['author_id'] : null;
221
222
        if ($this->get('default_to_current_user') === 'yes' && empty($data) && empty($_POST)) {
223
            $value = array(Symphony::Author()->get('id'));
224
        }
225
226
        if (!is_array($value)) {
227
            $value = array($value);
228
        }
229
230
        $options = array();
231
232
        if ($this->get('required') !== 'yes') {
233
            $options[] = array(null, false, null);
234
        }
235
236
        // Custom where to only show Authors based off the Author Types setting
237
        $types = $this->get('author_types');
238
239
        if (!empty($types)) {
240
            $types = implode('","', $this->get('author_types'));
241
            $where = 'user_type IN ("' . $types . '")';
242
        }
243
244
        $authors = AuthorManager::fetch('id', 'ASC', null, null, $where);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $where does not seem to be defined for all execution paths leading up to this point.
Loading history...
245
        $found = false;
246
247
        foreach ($authors as $a) {
248
            if (in_array($a->get('id'), $value)) {
249
                $found = true;
250
            }
251
252
            $options[] = array($a->get('id'), in_array($a->get('id'), $value), $a->getFullName());
253
        }
254
255
        // Ensure the selected Author is included in the options (incase
256
        // the settings change after the original entry was created)
257
        if (!$found && !is_null($value)) {
258
            $authors = AuthorManager::fetchByID($value);
259
260
            foreach ($authors as $a) {
261
                $options[] = array($a->get('id'), in_array($a->get('id'), $value), $a->getFullName());
262
            }
263
        }
264
265
        $fieldname = 'fields'.$fieldnamePrefix.'['.$this->get('element_name').']'.$fieldnamePostfix;
0 ignored issues
show
Bug introduced by
Are you sure $this->get('element_name') of type null|array|mixed 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

265
        $fieldname = 'fields'.$fieldnamePrefix.'['./** @scrutinizer ignore-type */ $this->get('element_name').']'.$fieldnamePostfix;
Loading history...
266
267
        if ($this->get('allow_multiple_selection') === 'yes') {
268
            $fieldname .= '[]';
269
        }
270
271
        $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

271
        $label = Widget::Label(/** @scrutinizer ignore-type */ $this->get('label'));
Loading history...
272
273
        if ($this->get('required') !== 'yes') {
274
            $label->appendChild(new XMLElement('i', __('Optional')));
275
        }
276
277
        $label->appendChild(Widget::Select($fieldname, $options, ($this->get('allow_multiple_selection') === 'yes' ? array('multiple' => 'multiple') : null)));
278
279
        if ($flagWithError != null) {
280
            $wrapper->appendChild(Widget::Error($label, $flagWithError));
281
        } else {
282
            $wrapper->appendChild($label);
283
        }
284
    }
285
286
    public function processRawFieldData($data, &$status, &$message = null, $simulate = false, $entry_id = null)
287
    {
288
        $status = self::__OK__;
289
290
        if (!is_array($data) && !empty($data)) {
291
            return array('author_id' => $data);
292
        }
293
294
        if (empty($data)) {
295
            return null;
296
        }
297
298
        $result = array();
299
300
        foreach ($data as $id) {
301
            $result['author_id'][] = $id;
302
        }
303
304
        return $result;
305
    }
306
307
    /*-------------------------------------------------------------------------
308
        Output:
309
    -------------------------------------------------------------------------*/
310
311
    public function appendFormattedElement(XMLElement &$wrapper, $data, $encode = false, $mode = null, $entry_id = null)
312
    {
313
        if (!is_array($data['author_id'])) {
314
            $data['author_id'] = array($data['author_id']);
315
        }
316
317
        $list = new XMLElement($this->get('element_name'));
0 ignored issues
show
Bug introduced by
It seems like $this->get('element_name') can also be of type array; however, parameter $name of XMLElement::__construct() 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

317
        $list = new XMLElement(/** @scrutinizer ignore-type */ $this->get('element_name'));
Loading history...
318
        $authors = AuthorManager::fetchByID($data['author_id']);
319
320
        foreach ($authors as $author) {
321
            if (is_null($author)) {
322
                continue;
323
            }
324
325
            $list->appendChild(new XMLElement(
326
                'item',
327
                $author->getFullName(),
328
                array(
329
                    'id' => (string)$author->get('id'),
330
                    'handle' => Lang::createHandle($author->getFullName()),
331
                    'username' => General::sanitize($author->get('username'))
332
                )
333
            ));
334
        }
335
336
        $wrapper->appendChild($list);
337
    }
338
339
    public function prepareTextValue($data, $entry_id = null)
340
    {
341
        $value = $this->prepareExportValue($data, ExportableField::LIST_OF + ExportableField::VALUE, $entry_id);
342
        return General::sanitize(implode(', ', $value));
343
    }
344
345
    public function getParameterPoolValue(array $data, $entry_id = null)
346
    {
347
        return $this->prepareExportValue($data, ExportableField::LIST_OF + ExportableField::AUTHOR, $entry_id);
348
    }
349
350
    /*-------------------------------------------------------------------------
351
        Export:
352
    -------------------------------------------------------------------------*/
353
354
    /**
355
     * Return a list of supported export modes for use with `prepareExportValue`.
356
     *
357
     * @return array
358
     */
359
    public function getExportModes()
360
    {
361
        return array(
362
            'listAuthor' =>         ExportableField::LIST_OF
363
                                    + ExportableField::AUTHOR,
364
            'listAuthorObject' =>   ExportableField::LIST_OF
365
                                    + ExportableField::AUTHOR
366
                                    + ExportableField::OBJECT,
367
            'listAuthorToValue' =>  ExportableField::LIST_OF
368
                                    + ExportableField::AUTHOR
369
                                    + ExportableField::VALUE,
370
            'listValue' =>          ExportableField::LIST_OF
371
                                    + ExportableField::VALUE,
372
            'getPostdata' =>        ExportableField::POSTDATA
373
        );
374
    }
375
376
    /**
377
     * Give the field some data and ask it to return a value using one of many
378
     * possible modes.
379
     *
380
     * @param mixed $data
381
     * @param integer $mode
382
     * @param integer $entry_id
383
     * @return array|null
384
     */
385
    public function prepareExportValue($data, $mode, $entry_id = null)
386
    {
387
        $modes = (object)$this->getExportModes();
388
389
        // Make sure we have an array to work with:
390
        if (isset($data['author_id']) && is_array($data['author_id']) === false) {
391
            $data['author_id'] = array(
392
                $data['author_id']
393
            );
394
        }
395
396
        // Return the author IDs:
397
        if ($mode === $modes->listAuthor || $mode === $modes->getPostdata) {
398
            return isset($data['author_id'])
399
                ? $data['author_id']
400
                : array();
401
        }
402
403
        // All other modes require full data:
404
        $authors = isset($data['author_id'])
405
            ? AuthorManager::fetchByID($data['author_id'])
406
            : array();
407
        $items = array();
408
409
        foreach ($authors as $author) {
410
            if (is_null($author)) {
411
                continue;
412
            }
413
414
            if ($mode === $modes->listAuthorObject) {
415
                $items[] = $author;
416
            } elseif ($mode === $modes->listValue) {
417
                $items[] = $author->getFullName();
418
            } elseif ($mode === $modes->listAuthorToValue) {
419
                $items[$data['author_id']] = $author->getFullName();
420
            }
421
        }
422
423
        return $items;
424
    }
425
426
    /*-------------------------------------------------------------------------
427
        Filtering:
428
    -------------------------------------------------------------------------*/
429
430
    public function buildDSRetrievalSQL($data, &$joins, &$where, $andOperation = false)
431
    {
432
        $field_id = $this->get('id');
433
434
        if (self::isFilterRegex($data[0])) {
435
            $this->_key++;
436
437
            if (preg_match('/^regexp:/i', $data[0])) {
438
                $pattern = preg_replace('/^regexp:\s*/i', null, $this->cleanValue($data[0]));
439
                $regex = 'REGEXP';
440
            } else {
441
                $pattern = preg_replace('/^not-?regexp:\s*/i', null, $this->cleanValue($data[0]));
442
                $regex = 'NOT REGEXP';
443
            }
444
445
            if (strlen($pattern) == 0) {
446
                return;
447
            }
448
449
            $joins .= "
450
                LEFT JOIN
451
                    `tbl_entries_data_{$field_id}` AS t{$field_id}_{$this->_key}
452
                    ON (e.id = t{$field_id}_{$this->_key}.entry_id)
453
                JOIN
454
                    `tbl_authors` AS t{$field_id}_{$this->_key}_authors
455
                    ON (t{$field_id}_{$this->_key}.author_id = t{$field_id}_{$this->_key}_authors.id)
456
            ";
457
            $where .= "
458
                AND (
459
                    t{$field_id}_{$this->_key}.author_id {$regex} '{$pattern}'
460
                    OR t{$field_id}_{$this->_key}_authors.username {$regex} '{$pattern}'
461
                    OR CONCAT_WS(' ',
462
                        t{$field_id}_{$this->_key}_authors.first_name,
463
                        t{$field_id}_{$this->_key}_authors.last_name
464
                    ) {$regex} '{$pattern}'
465
                )
466
            ";
467
        } elseif (self::isFilterSQL($data[0])) {
468
            $this->buildFilterSQL($data[0], array('username', 'first_name', 'last_name'), $joins, $where);
469
        } elseif ($andOperation) {
470
            foreach ($data as $value) {
471
                $this->_key++;
472
                $value = $this->cleanValue($value);
473
474
                if (self::__parseFilter($value) == "author_id") {
475
                    $where .= "
476
                        AND t{$field_id}_{$this->_key}.author_id = '{$value}'
477
                    ";
478
                    $joins .= "
479
                        LEFT JOIN
480
                            `tbl_entries_data_{$field_id}` AS t{$field_id}_{$this->_key}
481
                            ON (e.id = t{$field_id}_{$this->_key}.entry_id)
482
                    ";
483
                } else {
484
                    $joins .= "
485
                        LEFT JOIN
486
                            `tbl_entries_data_{$field_id}` AS t{$field_id}_{$this->_key}
487
                            ON (e.id = t{$field_id}_{$this->_key}.entry_id)
488
                        JOIN
489
                            `tbl_authors` AS t{$field_id}_{$this->_key}_authors
490
                            ON (t{$field_id}_{$this->_key}.author_id = t{$field_id}_{$this->_key}_authors.id)
491
                    ";
492
                    $where .= "
493
                        AND (
494
                            t{$field_id}_{$this->_key}_authors.username = '{$value}'
495
                            OR CONCAT_WS(' ',
496
                                t{$field_id}_{$this->_key}_authors.first_name,
497
                                t{$field_id}_{$this->_key}_authors.last_name
498
                            ) = '{$value}'
499
                        )
500
                    ";
501
                }
502
            }
503
        } else {
504
            if (!is_array($data)) {
505
                $data = array($data);
506
            }
507
508
            foreach ($data as &$value) {
509
                $value = $this->cleanValue($value);
510
            }
511
512
            $this->_key++;
513
            $data = implode("', '", $data);
514
            $joins .= "
515
                LEFT JOIN
516
                    `tbl_entries_data_{$field_id}` AS t{$field_id}_{$this->_key}
517
                    ON (e.id = t{$field_id}_{$this->_key}.entry_id)
518
                JOIN
519
                    `tbl_authors` AS t{$field_id}_{$this->_key}_authors
520
                    ON (t{$field_id}_{$this->_key}.author_id = t{$field_id}_{$this->_key}_authors.id)
521
            ";
522
            $where .= "
523
                AND (
524
                    t{$field_id}_{$this->_key}.author_id IN ('{$data}')
525
                    OR
526
                    t{$field_id}_{$this->_key}_authors.username IN ('{$data}')
527
                    OR CONCAT_WS(' ',
528
                        t{$field_id}_{$this->_key}_authors.first_name,
529
                        t{$field_id}_{$this->_key}_authors.last_name
530
                    ) IN ('{$data}')
531
                )
532
            ";
533
        }
534
535
        return true;
536
    }
537
538
    /*-------------------------------------------------------------------------
539
        Sorting:
540
    -------------------------------------------------------------------------*/
541
542
    public function buildSortingSQL(&$joins, &$where, &$sort, $order = 'ASC')
543
    {
544
        if ($this->isRandomOrder($order)) {
545
            $sort = 'ORDER BY RAND()';
546
        } else {
547
            $joins .= "
548
                LEFT OUTER JOIN `tbl_entries_data_".$this->get('id')."` AS `ed` ON (`e`.`id` = `ed`.`entry_id`)
0 ignored issues
show
Bug introduced by
Are you sure $this->get('id') of type null|array|mixed 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

548
                LEFT OUTER JOIN `tbl_entries_data_"./** @scrutinizer ignore-type */ $this->get('id')."` AS `ed` ON (`e`.`id` = `ed`.`entry_id`)
Loading history...
549
                LEFT OUTER JOIN `tbl_authors` AS `a` ON (ed.author_id = a.id)
550
            ";
551
            $sort = sprintf('ORDER BY `a`.`first_name` %1$s, `a`.`last_name` %1$s', $order);
552
        }
553
    }
554
555
    public function buildSortingSelectSQL($sort, $order = 'ASC')
556
    {
557
        if ($this->isRandomOrder($order)) {
558
            return null;
559
        }
560
        return '`a`.`first_name`, `a`.`last_name`';
561
    }
562
563
    /*-------------------------------------------------------------------------
564
        Events:
565
    -------------------------------------------------------------------------*/
566
567
    public function getExampleFormMarkup()
568
    {
569
        $authors = AuthorManager::fetch();
570
        $options = array();
571
572
        foreach ($authors as $a) {
573
            $options[] = array($a->get('id'), null, $a->getFullName());
574
        }
575
576
        $fieldname = 'fields['.$this->get('element_name').']';
0 ignored issues
show
Bug introduced by
Are you sure $this->get('element_name') of type null|array|mixed 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

576
        $fieldname = 'fields['./** @scrutinizer ignore-type */ $this->get('element_name').']';
Loading history...
577
578
        if ($this->get('allow_multiple_selection') === 'yes') {
579
            $fieldname .= '[]';
580
        }
581
582
        $attr = array();
583
584
        if ($this->get('allow_multiple_selection') === 'yes') {
585
            $attr['multiple'] = 'multiple';
586
        }
587
588
        $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

588
        $label = Widget::Label(/** @scrutinizer ignore-type */ $this->get('label'));
Loading history...
589
        $label->appendChild(Widget::Select($fieldname, $options, $attr));
590
591
        return $label;
592
    }
593
594
    /*-------------------------------------------------------------------------
595
        Grouping:
596
    -------------------------------------------------------------------------*/
597
598
    public function groupRecords($records)
599
    {
600
        if (!is_array($records) || empty($records)) {
601
            return;
602
        }
603
604
        $groups = array($this->get('element_name') => array());
605
606
        foreach ($records as $r) {
607
            $data = $r->getData($this->get('id'));
608
            $author_id = !isset($data['author_id']) ? 0 : $data['author_id'];
609
610
            if (!isset($groups[$this->get('element_name')][$author_id])) {
611
                $author = AuthorManager::fetchByID($author_id);
612
                // If there is an author, use those values, otherwise just blank it.
613
                if ($author instanceof Author) {
614
                    $username = $author->get('username');
615
                    $full_name = $author->getFullName();
616
                } else {
617
                    $username = '';
618
                    $full_name = '';
619
                }
620
621
                $groups[$this->get('element_name')][$author_id] = array(
622
                    'attr' => array('author-id' => $author_id, 'username' => $username, 'full-name' => $full_name),
623
                    'records' => array(),
624
                    'groups' => array()
625
                );
626
            }
627
628
            $groups[$this->get('element_name')][$author_id]['records'][] = $r;
629
        }
630
631
        return $groups;
632
    }
633
}
634