Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like AssetAdmin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AssetAdmin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class AssetAdmin extends LeftAndMain implements PermissionProvider{ |
||
| 13 | |||
| 14 | private static $url_segment = 'assets'; |
||
|
|
|||
| 15 | |||
| 16 | private static $url_rule = '/$Action/$ID'; |
||
| 17 | |||
| 18 | private static $menu_title = 'Files'; |
||
| 19 | |||
| 20 | private static $tree_class = 'Folder'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Amount of results showing on a single page. |
||
| 24 | * |
||
| 25 | * @config |
||
| 26 | * @var int |
||
| 27 | */ |
||
| 28 | private static $page_length = 15; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @config |
||
| 32 | * @see Upload->allowedMaxFileSize |
||
| 33 | * @var int |
||
| 34 | */ |
||
| 35 | private static $allowed_max_file_size; |
||
| 36 | |||
| 37 | private static $allowed_actions = array( |
||
| 38 | 'addfolder', |
||
| 39 | 'delete', |
||
| 40 | 'AddForm', |
||
| 41 | 'SearchForm', |
||
| 42 | 'getsubtree' |
||
| 43 | ); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Return fake-ID "root" if no ID is found (needed to upload files into the root-folder) |
||
| 47 | */ |
||
| 48 | public function currentPageID() { |
||
| 49 | if(is_numeric($this->getRequest()->requestVar('ID'))) { |
||
| 50 | return $this->getRequest()->requestVar('ID'); |
||
| 51 | } elseif (isset($this->urlParams['ID']) && is_numeric($this->urlParams['ID'])) { |
||
| 52 | return $this->urlParams['ID']; |
||
| 53 | } elseif(Session::get("{$this->class}.currentPage")) { |
||
| 54 | return Session::get("{$this->class}.currentPage"); |
||
| 55 | } else { |
||
| 56 | return 0; |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Set up the controller |
||
| 62 | */ |
||
| 63 | public function init() { |
||
| 64 | parent::init(); |
||
| 65 | |||
| 66 | Versioned::set_stage(Versioned::DRAFT); |
||
| 67 | |||
| 68 | Requirements::javascript(CMS_DIR . "/client/dist/js/AssetAdmin.js"); |
||
| 69 | Requirements::add_i18n_javascript(CMS_DIR . '/client/lang', false, true); |
||
| 70 | Requirements::css(CMS_DIR . '/client/dist/styles/bundle.css'); |
||
| 71 | CMSBatchActionHandler::register('delete', 'AssetAdmin_DeleteBatchAction', 'Folder'); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Returns the files and subfolders contained in the currently selected folder, |
||
| 76 | * defaulting to the root node. Doubles as search results, if any search parameters |
||
| 77 | * are set through {@link SearchForm()}. |
||
| 78 | * |
||
| 79 | * @return SS_List |
||
| 80 | */ |
||
| 81 | public function getList() { |
||
| 82 | $folder = $this->currentPage(); |
||
| 83 | $context = $this->getSearchContext(); |
||
| 84 | // Overwrite name filter to search both Name and Title attributes |
||
| 85 | $context->removeFilterByName('Name'); |
||
| 86 | $params = $this->getRequest()->requestVar('q'); |
||
| 87 | $list = $context->getResults($params); |
||
| 88 | |||
| 89 | // Don't filter list when a detail view is requested, |
||
| 90 | // to avoid edge cases where the filtered list wouldn't contain the requested |
||
| 91 | // record due to faulty session state (current folder not always encoded in URL, see #7408). |
||
| 92 | if(!$folder->ID |
||
| 93 | && $this->getRequest()->requestVar('ID') === null |
||
| 94 | && ($this->getRequest()->param('ID') == 'field') |
||
| 95 | ) { |
||
| 96 | return $list; |
||
| 97 | } |
||
| 98 | |||
| 99 | // Re-add previously removed "Name" filter as combined filter |
||
| 100 | // TODO Replace with composite SearchFilter once that API exists |
||
| 101 | if(!empty($params['Name'])) { |
||
| 102 | $list = $list->filterAny(array( |
||
| 103 | 'Name:PartialMatch' => $params['Name'], |
||
| 104 | 'Title:PartialMatch' => $params['Name'] |
||
| 105 | )); |
||
| 106 | } |
||
| 107 | |||
| 108 | // Always show folders at the top |
||
| 109 | $list = $list->sort('(CASE WHEN "File"."ClassName" = \'Folder\' THEN 0 ELSE 1 END), "Name"'); |
||
| 110 | |||
| 111 | // If a search is conducted, check for the "current folder" limitation. |
||
| 112 | // Otherwise limit by the current folder as denoted by the URL. |
||
| 113 | if(empty($params) || !empty($params['CurrentFolderOnly'])) { |
||
| 114 | $list = $list->filter('ParentID', $folder->ID); |
||
| 115 | } |
||
| 116 | |||
| 117 | // Category filter |
||
| 118 | if(!empty($params['AppCategory']) |
||
| 119 | && !empty(File::config()->app_categories[$params['AppCategory']]) |
||
| 120 | ) { |
||
| 121 | $exts = File::config()->app_categories[$params['AppCategory']]; |
||
| 122 | $list = $list->filter('Name:PartialMatch', $exts); |
||
| 123 | } |
||
| 124 | |||
| 125 | // Date filter |
||
| 126 | View Code Duplication | if(!empty($params['CreatedFrom'])) { |
|
| 127 | $fromDate = new DateField(null, null, $params['CreatedFrom']); |
||
| 128 | $list = $list->filter("Created:GreaterThanOrEqual", $fromDate->dataValue().' 00:00:00'); |
||
| 129 | } |
||
| 130 | View Code Duplication | if(!empty($params['CreatedTo'])) { |
|
| 131 | $toDate = new DateField(null, null, $params['CreatedTo']); |
||
| 132 | $list = $list->filter("Created:LessThanOrEqual", $toDate->dataValue().' 23:59:59'); |
||
| 133 | } |
||
| 134 | |||
| 135 | return $list; |
||
| 136 | } |
||
| 137 | |||
| 138 | public function getEditForm($id = null, $fields = null) { |
||
| 139 | Requirements::javascript(FRAMEWORK_DIR . '/client/dist/js/AssetUploadField.js'); |
||
| 140 | Requirements::css(FRAMEWORK_DIR . '/client/dist/styles/AssetUploadField.css'); |
||
| 141 | |||
| 142 | $form = parent::getEditForm($id, $fields); |
||
| 143 | $folder = ($id && is_numeric($id)) ? DataObject::get_by_id('Folder', $id, false) : $this->currentPage(); |
||
| 144 | $fields = $form->Fields(); |
||
| 145 | $title = ($folder && $folder->isInDB()) ? $folder->Title : _t('AssetAdmin.FILES', 'Files'); |
||
| 146 | $fields->push(new HiddenField('ID', false, $folder ? $folder->ID : null)); |
||
| 147 | |||
| 148 | // Remove legacy previewable behaviour. |
||
| 149 | $form->removeExtraClass('cms-previewable'); |
||
| 150 | $form->Fields()->removeByName('SilverStripeNavigator'); |
||
| 151 | |||
| 152 | // File listing |
||
| 153 | $gridFieldConfig = GridFieldConfig::create()->addComponents( |
||
| 154 | new GridFieldToolbarHeader(), |
||
| 155 | new GridFieldSortableHeader(), |
||
| 156 | new GridFieldFilterHeader(), |
||
| 157 | new GridFieldDataColumns(), |
||
| 158 | new GridFieldPaginator(self::config()->page_length), |
||
| 159 | new GridFieldEditButton(), |
||
| 160 | new GridFieldDeleteAction(), |
||
| 161 | new GridFieldDetailForm(), |
||
| 162 | GridFieldLevelup::create($folder->ID)->setLinkSpec('admin/assets/show/%d') |
||
| 163 | ); |
||
| 164 | |||
| 165 | $gridField = GridField::create('File', $title, $this->getList(), $gridFieldConfig); |
||
| 166 | $columns = $gridField->getConfig()->getComponentByType('GridFieldDataColumns'); |
||
| 167 | $columns->setDisplayFields(array( |
||
| 168 | 'StripThumbnail' => '', |
||
| 169 | 'Title' => _t('File.Title', 'Title'), |
||
| 170 | 'Created' => _t('AssetAdmin.CREATED', 'Date'), |
||
| 171 | 'Size' => _t('AssetAdmin.SIZE', 'Size'), |
||
| 172 | )); |
||
| 173 | $columns->setFieldCasting(array( |
||
| 174 | 'Created' => 'SS_Datetime->Nice' |
||
| 175 | )); |
||
| 176 | $gridField->setAttribute( |
||
| 177 | 'data-url-folder-template', |
||
| 178 | Controller::join_links($this->Link('show'), '%s') |
||
| 179 | ); |
||
| 180 | |||
| 181 | if(!$folder->hasMethod('canAddChildren') || ($folder->hasMethod('canAddChildren') && $folder->canAddChildren())) { |
||
| 182 | // TODO Will most likely be replaced by GridField logic |
||
| 183 | $addFolderBtn = new LiteralField( |
||
| 184 | 'AddFolderButton', |
||
| 185 | sprintf( |
||
| 186 | '<a class="ss-ui-button font-icon-folder-add no-text cms-add-folder-link" title="%s" data-icon="add" data-url="%s" href="%s"></a>', |
||
| 187 | _t('Folder.AddFolderButton', 'Add folder'), |
||
| 188 | Controller::join_links($this->Link('AddForm'), '?' . http_build_query(array( |
||
| 189 | 'action_doAdd' => 1, |
||
| 190 | 'ParentID' => $folder->ID, |
||
| 191 | 'SecurityID' => $form->getSecurityToken()->getValue() |
||
| 192 | ))), |
||
| 193 | Controller::join_links($this->Link('addfolder'), '?ParentID=' . $folder->ID) |
||
| 194 | ) |
||
| 195 | ); |
||
| 196 | } else { |
||
| 197 | $addFolderBtn = ''; |
||
| 198 | } |
||
| 199 | |||
| 200 | // Move existing fields to a "details" tab, unless they've already been tabbed out through extensions. |
||
| 201 | // Required to keep Folder->getCMSFields() simple and reuseable, |
||
| 202 | // without any dependencies into AssetAdmin (e.g. useful for "add folder" views). |
||
| 203 | if(!$fields->hasTabset()) { |
||
| 204 | $tabs = new TabSet('Root', |
||
| 205 | $tabList = new Tab('ListView', _t('AssetAdmin.ListView', 'List View')), |
||
| 206 | $tabTree = new Tab('TreeView', _t('AssetAdmin.TreeView', 'Tree View')) |
||
| 207 | ); |
||
| 208 | $tabList->addExtraClass("content-listview cms-tabset-icon list"); |
||
| 209 | $tabTree->addExtraClass("content-treeview cms-tabset-icon tree"); |
||
| 210 | if($fields->Count() && $folder && $folder->isInDB()) { |
||
| 211 | $tabs->push($tabDetails = new Tab('DetailsView', _t('AssetAdmin.DetailsView', 'Details'))); |
||
| 212 | $tabDetails->addExtraClass("content-galleryview cms-tabset-icon edit"); |
||
| 213 | foreach($fields as $field) { |
||
| 214 | $fields->removeByName($field->getName()); |
||
| 215 | $tabDetails->push($field); |
||
| 216 | } |
||
| 217 | } |
||
| 218 | $fields->push($tabs); |
||
| 219 | } |
||
| 220 | |||
| 221 | // we only add buttons if they're available. User might not have permission and therefore |
||
| 222 | // the button shouldn't be available. Adding empty values into a ComposteField breaks template rendering. |
||
| 223 | $actionButtonsComposite = CompositeField::create()->addExtraClass('cms-actions-row'); |
||
| 224 | if($addFolderBtn) $actionButtonsComposite->push($addFolderBtn); |
||
| 225 | |||
| 226 | // Add the upload field for new media |
||
| 227 | if($currentPageID = $this->currentPageID()){ |
||
| 228 | Session::set("{$this->class}.currentPage", $currentPageID); |
||
| 229 | } |
||
| 230 | |||
| 231 | $folder = $this->currentPage(); |
||
| 232 | |||
| 233 | $uploadField = UploadField::create('AssetUploadField', ''); |
||
| 234 | $uploadField->setConfig('previewMaxWidth', 40); |
||
| 235 | $uploadField->setConfig('previewMaxHeight', 30); |
||
| 236 | $uploadField->setConfig('changeDetection', false); |
||
| 237 | $uploadField->addExtraClass('ss-assetuploadfield'); |
||
| 238 | $uploadField->removeExtraClass('ss-uploadfield'); |
||
| 239 | $uploadField->setTemplate('AssetUploadField'); |
||
| 240 | |||
| 241 | if($folder->exists()) { |
||
| 242 | $path = $folder->getFilename(); |
||
| 243 | $uploadField->setFolderName($path); |
||
| 244 | } else { |
||
| 245 | $uploadField->setFolderName('/'); // root of the assets |
||
| 246 | } |
||
| 247 | |||
| 248 | $exts = $uploadField->getValidator()->getAllowedExtensions(); |
||
| 249 | asort($exts); |
||
| 250 | $uploadField->Extensions = implode(', ', $exts); |
||
| 251 | |||
| 252 | // List view |
||
| 253 | $fields->addFieldsToTab('Root.ListView', array( |
||
| 254 | $actionsComposite = CompositeField::create( |
||
| 255 | $actionButtonsComposite |
||
| 256 | )->addExtraClass('cms-content-toolbar field'), |
||
| 257 | $uploadField, |
||
| 258 | new HiddenField('ID'), |
||
| 259 | $gridField |
||
| 260 | )); |
||
| 261 | |||
| 262 | // Tree view |
||
| 263 | $fields->addFieldsToTab('Root.TreeView', array( |
||
| 264 | clone $actionsComposite, |
||
| 265 | // TODO Replace with lazy loading on client to avoid performance hit of rendering potentially unused views |
||
| 266 | new LiteralField( |
||
| 267 | 'Tree', |
||
| 268 | FormField::create_tag( |
||
| 269 | 'div', |
||
| 270 | array( |
||
| 271 | 'class' => 'cms-tree', |
||
| 272 | 'data-url-tree' => $this->Link('getsubtree'), |
||
| 273 | 'data-url-savetreenode' => $this->Link('savetreenode') |
||
| 274 | ), |
||
| 275 | $this->SiteTreeAsUL() |
||
| 276 | ) |
||
| 277 | ) |
||
| 278 | )); |
||
| 279 | |||
| 280 | // Move actions to "details" tab (they don't make sense on list/tree view) |
||
| 281 | $actions = $form->Actions(); |
||
| 282 | $saveBtn = $actions->fieldByName('action_save'); |
||
| 283 | $deleteBtn = $actions->fieldByName('action_delete'); |
||
| 284 | $actions->removeByName('action_save'); |
||
| 285 | $actions->removeByName('action_delete'); |
||
| 286 | if(($saveBtn || $deleteBtn) && $fields->fieldByName('Root.DetailsView')) { |
||
| 287 | $fields->addFieldToTab( |
||
| 288 | 'Root.DetailsView', |
||
| 289 | CompositeField::create($saveBtn,$deleteBtn)->addExtraClass('Actions') |
||
| 290 | ); |
||
| 291 | } |
||
| 292 | |||
| 293 | |||
| 294 | $fields->setForm($form); |
||
| 295 | $form->setTemplate($this->getTemplatesWithSuffix('_EditForm')); |
||
| 296 | // TODO Can't merge $FormAttributes in template at the moment |
||
| 297 | $form->addExtraClass('cms-edit-form ' . $this->BaseCSSClasses()); |
||
| 298 | $form->setAttribute('data-pjax-fragment', 'CurrentForm'); |
||
| 299 | $form->Fields()->findOrMakeTab('Root')->setTemplate('CMSTabSet'); |
||
| 300 | |||
| 301 | // Optionally handle form submissions with 'X-Formschema-Request' |
||
| 302 | // which rely on having validation errors returned as structured data |
||
| 303 | $form->setValidationResponseCallback(function() use ($form) { |
||
| 304 | $request = $this->getRequest(); |
||
| 305 | if($request->getHeader('X-Formschema-Request')) { |
||
| 306 | $data = $this->getSchemaForForm($form); |
||
| 307 | $response = new SS_HTTPResponse(Convert::raw2json($data)); |
||
| 308 | $response->addHeader('Content-Type', 'application/json'); |
||
| 309 | return $response; |
||
| 310 | |||
| 311 | } |
||
| 312 | }); |
||
| 313 | |||
| 314 | |||
| 315 | $this->extend('updateEditForm', $form); |
||
| 316 | |||
| 317 | return $form; |
||
| 318 | } |
||
| 319 | |||
| 320 | public function addfolder($request) { |
||
| 334 | |||
| 335 | public function delete($data, $form) { |
||
| 336 | $className = $this->stat('tree_class'); |
||
| 337 | |||
| 353 | |||
| 354 | /** |
||
| 355 | * Get the search context |
||
| 356 | * |
||
| 357 | * @return SearchContext |
||
| 358 | */ |
||
| 359 | public function getSearchContext() { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Returns a form for filtering of files and assets gridfield. |
||
| 410 | * Result filtering takes place in {@link getList()}. |
||
| 411 | * |
||
| 412 | * @return Form |
||
| 413 | * @see AssetAdmin.js |
||
| 414 | */ |
||
| 415 | public function SearchForm() { |
||
| 436 | |||
| 437 | public function AddForm() { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Add a new group and return its details suitable for ajax. |
||
| 474 | * |
||
| 475 | * @todo Move logic into Folder class, and use LeftAndMain->doAdd() default implementation. |
||
| 476 | */ |
||
| 477 | public function doAdd($data, $form) { |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Get an asset renamer for the given filename. |
||
| 537 | * |
||
| 538 | * @param string $filename Path name |
||
| 539 | * @return AssetNameGenerator |
||
| 540 | */ |
||
| 541 | protected function getNameGenerator($filename){ |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Custom currentPage() method to handle opening the 'root' folder |
||
| 548 | */ |
||
| 549 | public function currentPage() { |
||
| 560 | |||
| 561 | public function getSiteTreeFor($className, $rootID = null, $childrenMethod = null, $numChildrenMethod = null, $filterFunction = null, $minNodeCount = 30) { |
||
| 566 | |||
| 567 | public function getCMSTreeTitle() { |
||
| 570 | |||
| 571 | public function SiteTreeAsUL() { |
||
| 574 | |||
| 575 | /** |
||
| 576 | * @param bool $unlinked |
||
| 577 | * @return ArrayList |
||
| 578 | */ |
||
| 579 | public function Breadcrumbs($unlinked = false) { |
||
| 605 | |||
| 606 | public static function menu_title($class = null, $localised = true) { |
||
| 614 | |||
| 615 | public function providePermissions() { |
||
| 624 | |||
| 625 | } |
||
| 626 | /** |
||
| 660 |