| Conditions | 7 |
| Paths | 1 |
| Total Lines | 111 |
| Code Lines | 76 |
| Lines | 10 |
| Ratio | 9.01 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 68 | public function getCMSFields() |
||
| 69 | { |
||
| 70 | // PHP 5.3 only |
||
| 71 | $self = $this; |
||
| 72 | $this->beforeUpdateCMSFields(function (FieldList $fields) use ($self) { |
||
| 73 | $fields->removeFieldsFromTab( |
||
| 74 | 'Root.Main', |
||
| 75 | array('KeyValuePairs', 'SortBy', 'SortByDirection') |
||
| 76 | ); |
||
| 77 | // Don't put the GridField for documents in until the set has been created |
||
| 78 | if (!$self->isInDB()) { |
||
| 79 | $fields->addFieldToTab( |
||
| 80 | 'Root.Main', |
||
| 81 | LiteralField::create( |
||
| 82 | 'GridFieldNotice', |
||
| 83 | '<p class="message warning">' . _t( |
||
| 84 | 'DMSDocumentSet.GRIDFIELD_NOTICE', |
||
| 85 | 'Managing documents will be available once you have created this document set.' |
||
| 86 | ) . '</p>' |
||
| 87 | ), |
||
| 88 | 'Title' |
||
| 89 | ); |
||
| 90 | } else { |
||
| 91 | $fields->removeByName('DocumentSetSort'); |
||
| 92 | // Document listing |
||
| 93 | $gridFieldConfig = GridFieldConfig::create() |
||
| 94 | ->addComponents( |
||
| 95 | new GridFieldButtonRow('before'), |
||
| 96 | new GridFieldToolbarHeader(), |
||
| 97 | new GridFieldFilterHeader(), |
||
| 98 | new GridFieldSortableHeader(), |
||
| 99 | new GridFieldDataColumns(), |
||
| 100 | new GridFieldEditButton(), |
||
| 101 | // Special delete dialog to handle custom behaviour of unlinking and deleting |
||
| 102 | new GridFieldDeleteAction(true), |
||
| 103 | new GridFieldDetailForm() |
||
| 104 | ); |
||
| 105 | |||
| 106 | if (class_exists('GridFieldPaginatorWithShowAll')) { |
||
| 107 | $paginatorComponent = new GridFieldPaginatorWithShowAll(15); |
||
| 108 | } else { |
||
| 109 | $paginatorComponent = new GridFieldPaginator(15); |
||
| 110 | } |
||
| 111 | $gridFieldConfig->addComponent($paginatorComponent); |
||
| 112 | |||
| 113 | if (class_exists('GridFieldSortableRows')) { |
||
| 114 | $gridFieldConfig->addComponent(new GridFieldSortableRows('DocumentSort')); |
||
| 115 | } |
||
| 116 | |||
| 117 | // Don't show which page this is if we're already editing within a page context |
||
| 118 | View Code Duplication | if (Controller::curr() instanceof CMSPageEditController) { |
|
| 119 | $fields->removeByName('PageID'); |
||
| 120 | } else { |
||
| 121 | $fields->fieldByName('Root.Main.PageID')->setTitle(_t('DMSDocumentSet.SHOWONPAGE', 'Show on page')); |
||
| 122 | } |
||
| 123 | |||
| 124 | // Don't show which page this is if we're already editing within a page context |
||
| 125 | View Code Duplication | if (Controller::curr() instanceof CMSPageEditController) { |
|
| 126 | $fields->removeByName('PageID'); |
||
| 127 | } else { |
||
| 128 | $fields->fieldByName('Root.Main.PageID')->setTitle(_t('DMSDocumentSet.SHOWONPAGE', 'Show on page')); |
||
| 129 | } |
||
| 130 | |||
| 131 | $gridFieldConfig->getComponentByType('GridFieldDataColumns') |
||
| 132 | ->setDisplayFields($self->getDocumentDisplayFields()) |
||
| 133 | ->setFieldCasting(array('LastEdited' => 'Datetime->Ago')) |
||
| 134 | ->setFieldFormatting( |
||
| 135 | array( |
||
| 136 | 'FilenameWithoutID' => '<a target=\'_blank\' class=\'file-url\'' |
||
| 137 | . ' href=\'$Link\'>$FilenameWithoutID</a>', |
||
| 138 | 'ManuallyAdded' => function ($value) { |
||
| 139 | if ($value) { |
||
| 140 | return _t('DMSDocumentSet.MANUAL', 'Manually'); |
||
| 141 | } |
||
| 142 | return _t('DMSDocumentSet.QUERYBUILDER', 'Query Builder'); |
||
| 143 | } |
||
| 144 | ) |
||
| 145 | ); |
||
| 146 | |||
| 147 | // Override delete functionality with this class |
||
| 148 | $gridFieldConfig->getComponentByType('GridFieldDetailForm') |
||
| 149 | ->setItemRequestClass('DMSGridFieldDetailForm_ItemRequest'); |
||
| 150 | $gridField = GridField::create( |
||
| 151 | 'Documents', |
||
| 152 | false, |
||
| 153 | $self->Documents(), |
||
| 154 | $gridFieldConfig |
||
| 155 | ); |
||
| 156 | $gridField->setModelClass('DMSDocument'); |
||
| 157 | $gridField->addExtraClass('documents'); |
||
| 158 | |||
| 159 | $gridFieldConfig->addComponent( |
||
| 160 | $addNewButton = new DMSGridFieldAddNewButton('buttons-before-left'), |
||
| 161 | 'GridFieldExportButton' |
||
| 162 | ); |
||
| 163 | $addNewButton->setDocumentSetId($self->ID); |
||
| 164 | |||
| 165 | $fields->removeByName('Documents'); |
||
| 166 | $fields->addFieldsToTab( |
||
| 167 | 'Root.Main', |
||
| 168 | array( |
||
| 169 | $gridField, |
||
| 170 | HiddenField::create('DMSShortcodeHandlerKey', false, DMS::inst()->getShortcodeHandlerKey()) |
||
| 171 | ) |
||
| 172 | ); |
||
| 173 | $self->addQueryFields($fields); |
||
| 174 | } |
||
| 175 | }); |
||
| 176 | $this->addRequirements(); |
||
| 177 | return parent::getCMSFields(); |
||
| 178 | } |
||
| 179 | |||
| 420 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.