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 — master (#111)
by
unknown
03:16
created

GridFieldConfig_BlockManager::__construct()   C

Complexity

Conditions 10
Paths 96

Size

Total Lines 90
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 90
rs 5.1578
c 1
b 0
f 0
cc 10
eloc 59
nc 96
nop 5

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
namespace Blocks\Forms;
4
5
use Blocks\Model\Block;
6
7
use SilverStripe\Control\Controller;
8
9
use SilverStripe\Forms\DropdownField;
10
use SilverStripe\Forms\GridField\GridFieldConfig;
11
use SilverStripe\Forms\GridField\GridFieldDataColumns;
12
use SilverStripe\Forms\GridField\GridFieldButtonRow;
13
use SilverStripe\Forms\GridField\GridFieldToolbarHeader;
14
use SilverStripe\Forms\GridField\GridFieldSortableHeader;
15
use SilverStripe\Forms\GridField\GridFieldFilterHeader;
16
use SilverStripe\Forms\GridField\GridFieldDetailForm;
17
use SilverStripe\Forms\GridField\GridFieldCopyButton;
18
use SilverStripe\Forms\GridField\GridFieldEditButton;
19
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
20
21
/**
22
 * GridFieldConfig_BlockManager
23
 * Provides a reusable GridFieldConfig for managing Blocks.
24
 *
25
 * @author Shea Dawson <[email protected]>
26
 */
27
class GridFieldConfig_BlockManager extends GridFieldConfig
28
{
29
    public $blockManager;
30
31
    public function __construct($canAdd = true, $canEdit = true, $canDelete = true, $editableRows = false, $aboveOrBelow = false)
32
    {
33
        parent::__construct();
34
35
        $this->blockManager = Injector::inst()->get('BlockManager');
36
        $controllerClass = Controller::curr()->class;
37
        // Get available Areas (for page) or all in case of ModelAdmin
38
        if ($controllerClass == 'CMSPageEditController') {
39
            $currentPage = Controller::curr()->currentPage();
40
            $areasFieldSource = $this->blockManager->getAreasForPageType($currentPage->ClassName);
41
        } else {
42
            $areasFieldSource = $this->blockManager->getAreasForTheme();
43
        }
44
45
        // EditableColumns only makes sense on Saveable parenst (eg Page), or inline changes won't be saved
46
        if ($editableRows) {
47
            $this->addComponent($editable = new GridFieldEditableColumns());
48
            $displayfields = array(
49
                'TypeForGridfield' => array('title' => _t('Block.BlockType', 'Block Type'), 'field' => 'LiteralField'),
50
                'Title' => array('title' => _t('Block.Title', 'Title'), 'field' => 'ReadonlyField'),
51
                'BlockArea' => array(
52
                    'title' => _t('Block.BlockArea', 'Block Area').'
53
						&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
54
                        // the &nbsp;s prevent wrapping of dropdowns
55
                    'callback' => function () use ($areasFieldSource) {
56
                            $areasField = DropdownField::create('BlockArea', 'Block Area', $areasFieldSource);
57
                            if (count($areasFieldSource) > 1) {
58
                                $areasField->setHasEmptyDefault(true);
59
                            }
60
                            return $areasField;
61
                        },
62
                ),
63
                'isPublishedIcon' => array('title' => _t('Block.IsPublishedField', 'Published'), 'field' => 'LiteralField'),
64
                'UsageListAsString' => array('title' => _t('Block.UsageListAsString', 'Used on'), 'field' => 'LiteralField'),
65
            );
66
67
            if ($aboveOrBelow) {
68
                $displayfields['AboveOrBelow'] = array(
69
                    'title' => _t('GridFieldConfigBlockManager.AboveOrBelow', 'Above or Below'),
70
                    'callback' => function () {
71
                        return DropdownField::create('AboveOrBelow', _t('GridFieldConfigBlockManager.AboveOrBelow', 'Above or Below'), BlockSet::config()->get('above_or_below_options'));
72
                    },
73
                );
74
            }
75
            $editable->setDisplayFields($displayfields);
76
        } else {
77
            $this->addComponent($dcols = new GridFieldDataColumns());
78
79
            $displayfields = array(
80
                'TypeForGridfield' => array('title' => _t('Block.BlockType', 'Block Type'), 'field' => 'LiteralField'),
81
                'Title' => _t('Block.Title', 'Title'),
82
                'BlockArea' => _t('Block.BlockArea', 'Block Area'),
83
                'isPublishedIcon' => array('title' => _t('Block.IsPublishedField', 'Published'), 'field' => 'LiteralField'),
84
                'UsageListAsString' => _t('Block.UsageListAsString', 'Used on'),
85
            );
86
            $dcols->setDisplayFields($displayfields);
87
            $dcols->setFieldCasting(array('UsageListAsString' => 'HTMLText->Raw'));
88
        }
89
90
        $this->addComponent(new GridFieldButtonRow('before'));
91
        $this->addComponent(new GridFieldToolbarHeader());
92
        $this->addComponent(new GridFieldDetailForm());
93
        $this->addComponent($sort = new GridFieldSortableHeader());
94
        $this->addComponent($filter = new GridFieldFilterHeader());
95
        $this->addComponent(new GridFieldDetailForm());
96
        if ($controllerClass == 'BlockAdmin' && class_exists('GridFieldCopyButton')) {
97
            $this->addComponent(new GridFieldCopyButton());
98
        }
99
100
        $filter->setThrowExceptionOnBadDataType(false);
101
        $sort->setThrowExceptionOnBadDataType(false);
102
103
        if ($canAdd) {
104
            $multiClass = new GridFieldAddNewMultiClass();
105
            $classes = $this->blockManager->getBlockClasses();
106
            $multiClass->setClasses($classes);
107
            $this->addComponent($multiClass);
108
            //$this->addComponent(new GridFieldAddNewButton());
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
109
        }
110
111
        if ($canEdit) {
112
            $this->addComponent(new GridFieldEditButton());
113
        }
114
115
        if ($canDelete) {
116
            $this->addComponent(new GridFieldDeleteAction(true));
117
        }
118
119
        return $this;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
120
    }
121
122
    /**
123
     * Add the GridFieldAddExistingSearchButton component to this grid config.
124
     *
125
     * @return $this
126
     **/
127
    public function addExisting()
128
    {
129
        $classes = $this->blockManager->getBlockClasses();
130
131
        $this->addComponent($add = new GridFieldAddExistingSearchButton());
132
        $add->setSearchList(Block::get()->filter(array(
133
            'ClassName' => array_keys($classes),
134
        )));
135
136
        return $this;
137
    }
138
139
    /**
140
     * Add the GridFieldBulkManager component to this grid config.
141
     *
142
     * @return $this
143
     **/
144
    public function addBulkEditing()
145
    {
146
        if (class_exists('GridFieldBulkManager')) {
147
            $this->addComponent(new GridFieldBulkManager());
148
        }
149
150
        return $this;
151
    }
152
}
153