| Total Complexity | 83 | 
| Total Lines | 710 | 
| Duplicated Lines | 4.23 % | 
| Changes | 4 | ||
| Bugs | 0 | Features | 0 | 
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 BaseElement 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.
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 BaseElement, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 44 | class BaseElement extends DataObject implements CMSPreviewable  | 
            ||
| 45 | { | 
            ||
| 46 | /**  | 
            ||
| 47 | * Override this on your custom elements to specify a cms icon  | 
            ||
| 48 | *  | 
            ||
| 49 | * @var string  | 
            ||
| 50 | */  | 
            ||
| 51 | private static $icon = 'dnadesign/silverstripe-elemental:images/base.svg';  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 52 | |||
| 53 | private static $db = [  | 
            ||
| 54 | 'Title' => 'Varchar(255)',  | 
            ||
| 55 | 'ShowTitle' => 'Boolean',  | 
            ||
| 56 | 'Sort' => 'Int',  | 
            ||
| 57 | 'ExtraClass' => 'Varchar(255)',  | 
            ||
| 58 | 'Style' => 'Varchar(255)'  | 
            ||
| 59 | ];  | 
            ||
| 60 | |||
| 61 | private static $has_one = [  | 
            ||
| 62 | 'Parent' => ElementalArea::class  | 
            ||
| 63 | ];  | 
            ||
| 64 | |||
| 65 | private static $extensions = [  | 
            ||
| 66 | Versioned::class  | 
            ||
| 67 | ];  | 
            ||
| 68 | |||
| 69 | private static $versioned_gridfield_extensions = true;  | 
            ||
| 70 | |||
| 71 | private static $table_name = 'Element';  | 
            ||
| 72 | |||
| 73 | /**  | 
            ||
| 74 | * @var string  | 
            ||
| 75 | */  | 
            ||
| 76 | private static $controller_class = ElementController::class;  | 
            ||
| 77 | |||
| 78 | /**  | 
            ||
| 79 | * @var string  | 
            ||
| 80 | */  | 
            ||
| 81 | private static $controller_template = 'ElementHolder';  | 
            ||
| 82 | |||
| 83 | /**  | 
            ||
| 84 | * @var ElementController  | 
            ||
| 85 | */  | 
            ||
| 86 | protected $controller;  | 
            ||
| 87 | |||
| 88 | private static $default_sort = 'Sort';  | 
            ||
| 89 | |||
| 90 | private static $singular_name = 'block';  | 
            ||
| 91 | |||
| 92 | private static $plural_name = 'blocks';  | 
            ||
| 93 | |||
| 94 | private static $summary_fields = [  | 
            ||
| 95 | 'EditorPreview' => 'Summary'  | 
            ||
| 96 | ];  | 
            ||
| 97 | |||
| 98 | /**  | 
            ||
| 99 | * @var array  | 
            ||
| 100 | */  | 
            ||
| 101 | private static $styles = [];  | 
            ||
| 102 | |||
| 103 | private static $searchable_fields = [  | 
            ||
| 104 | 'ID' => [  | 
            ||
| 105 | 'field' => NumericField::class,  | 
            ||
| 106 | ],  | 
            ||
| 107 | 'Title',  | 
            ||
| 108 | 'LastEdited'  | 
            ||
| 109 | ];  | 
            ||
| 110 | |||
| 111 | /**  | 
            ||
| 112 | * Enable for backwards compatibility  | 
            ||
| 113 | *  | 
            ||
| 114 | * @var boolean  | 
            ||
| 115 | */  | 
            ||
| 116 | private static $disable_pretty_anchor_name = false;  | 
            ||
| 117 | |||
| 118 | /**  | 
            ||
| 119 | * Store used anchor names, this is to avoid title clashes  | 
            ||
| 120 | * when calling 'getAnchor'  | 
            ||
| 121 | *  | 
            ||
| 122 | * @var array  | 
            ||
| 123 | */  | 
            ||
| 124 | protected static $_used_anchors = [];  | 
            ||
| 125 | |||
| 126 | /**  | 
            ||
| 127 | * For caching 'getAnchor'  | 
            ||
| 128 | *  | 
            ||
| 129 | * @var string  | 
            ||
| 130 | */  | 
            ||
| 131 | protected $_anchor = null;  | 
            ||
| 132 | |||
| 133 | /**  | 
            ||
| 134 | * Basic permissions, defaults to page perms where possible.  | 
            ||
| 135 | *  | 
            ||
| 136 | * @param Member $member  | 
            ||
| 137 | *  | 
            ||
| 138 | * @return boolean  | 
            ||
| 139 | */  | 
            ||
| 140 | View Code Duplication | public function canView($member = null)  | 
            |
| 141 |     { | 
            ||
| 142 |         if ($this->hasMethod('getPage')) { | 
            ||
| 143 |             if ($page = $this->getPage()) { | 
            ||
| 144 | return $page->canView($member);  | 
            ||
| 145 | }  | 
            ||
| 146 | }  | 
            ||
| 147 | |||
| 148 |         return (Permission::check('CMS_ACCESS', 'any', $member)) ? true : null; | 
            ||
| 149 | }  | 
            ||
| 150 | |||
| 151 | /**  | 
            ||
| 152 | * Basic permissions, defaults to page perms where possible.  | 
            ||
| 153 | *  | 
            ||
| 154 | * @param Member $member  | 
            ||
| 155 | *  | 
            ||
| 156 | * @return boolean  | 
            ||
| 157 | */  | 
            ||
| 158 | View Code Duplication | public function canEdit($member = null)  | 
            |
| 159 |     { | 
            ||
| 160 |         if ($this->hasMethod('getPage')) { | 
            ||
| 161 |             if ($page = $this->getPage()) { | 
            ||
| 162 | return $page->canEdit($member);  | 
            ||
| 163 | }  | 
            ||
| 164 | }  | 
            ||
| 165 | |||
| 166 |         return (Permission::check('CMS_ACCESS', 'any', $member)) ? true : null; | 
            ||
| 167 | }  | 
            ||
| 168 | |||
| 169 | /**  | 
            ||
| 170 | * Basic permissions, defaults to page perms where possible.  | 
            ||
| 171 | *  | 
            ||
| 172 | * Uses archive not delete so that current stage is respected i.e if a  | 
            ||
| 173 | * element is not published, then it can be deleted by someone who doesn't  | 
            ||
| 174 | * have publishing permissions.  | 
            ||
| 175 | *  | 
            ||
| 176 | * @param Member $member  | 
            ||
| 177 | *  | 
            ||
| 178 | * @return boolean  | 
            ||
| 179 | */  | 
            ||
| 180 | View Code Duplication | public function canDelete($member = null)  | 
            |
| 181 |     { | 
            ||
| 182 |         if ($this->hasMethod('getPage')) { | 
            ||
| 183 |             if ($page = $this->getPage()) { | 
            ||
| 184 | return $page->canArchive($member);  | 
            ||
| 185 | }  | 
            ||
| 186 | }  | 
            ||
| 187 | |||
| 188 |         return (Permission::check('CMS_ACCESS', 'any', $member)) ? true : null; | 
            ||
| 189 | }  | 
            ||
| 190 | |||
| 191 | /**  | 
            ||
| 192 | * Basic permissions, defaults to page perms where possible.  | 
            ||
| 193 | *  | 
            ||
| 194 | * @param Member $member  | 
            ||
| 195 | * @param array $context  | 
            ||
| 196 | *  | 
            ||
| 197 | * @return boolean  | 
            ||
| 198 | */  | 
            ||
| 199 | public function canCreate($member = null, $context = array())  | 
            ||
| 200 |     { | 
            ||
| 201 |         return (Permission::check('CMS_ACCESS', 'any', $member)) ? true : null; | 
            ||
| 202 | }  | 
            ||
| 203 | |||
| 204 | /**  | 
            ||
| 205 | *  | 
            ||
| 206 | */  | 
            ||
| 207 | public function onBeforeWrite()  | 
            ||
| 208 |     { | 
            ||
| 209 | parent::onBeforeWrite();  | 
            ||
| 210 | |||
| 211 |         if ($areaID = $this->ParentID) { | 
            ||
| 212 |             if ($elementalArea = ElementalArea::get()->byID($areaID)) { | 
            ||
| 213 | $elementalArea->write();  | 
            ||
| 214 | }  | 
            ||
| 215 | }  | 
            ||
| 216 | |||
| 217 |         if (!$this->Sort) { | 
            ||
| 218 | $parentID = ($this->ParentID) ? $this->ParentID : 0;  | 
            ||
| 219 | |||
| 220 |             $this->Sort = static::get()->max('Sort') + 1; | 
            ||
| 221 | }  | 
            ||
| 222 | }  | 
            ||
| 223 | |||
| 224 | public function getCMSFields()  | 
            ||
| 225 |     { | 
            ||
| 226 |         $this->beforeUpdateCMSFields(function (FieldList $fields) { | 
            ||
| 227 | // Remove relationship fields  | 
            ||
| 228 |             $fields->removeByName('ParentID'); | 
            ||
| 229 |             $fields->removeByName('Sort'); | 
            ||
| 230 | |||
| 231 | $fields->addFieldToTab(  | 
            ||
| 232 | 'Root.Settings',  | 
            ||
| 233 |                 TextField::create('ExtraClass', _t(__CLASS__ . '.ExtraCssClassesLabel', 'Custom CSS classes')) | 
            ||
| 234 | ->setAttribute(  | 
            ||
| 235 | 'placeholder',  | 
            ||
| 236 | _t(__CLASS__ . '.ExtraCssClassesPlaceholder', 'my_class another_class')  | 
            ||
| 237 | )  | 
            ||
| 238 | );  | 
            ||
| 239 | |||
| 240 | // Add a combined field for "Title" and "Displayed" checkbox in a Bootstrap input group  | 
            ||
| 241 |             $fields->removeByName('ShowTitle'); | 
            ||
| 242 | $fields->replaceField(  | 
            ||
| 243 | 'Title',  | 
            ||
| 244 | FieldGroup::create(  | 
            ||
| 245 |                     TextField::create('Title', ''), | 
            ||
| 246 |                     CheckboxField::create('ShowTitle', _t(__CLASS__ . '.ShowTitleLabel', 'Displayed')) | 
            ||
| 247 | )  | 
            ||
| 248 | ->setTemplate(__CLASS__ . '\\FieldGroup')  | 
            ||
| 249 | ->setTitle(_t(__CLASS__ . '.TitleLabel', 'Title (not displayed unless specified)'))  | 
            ||
| 250 | );  | 
            ||
| 251 | |||
| 252 | // Rename the "Main" tab  | 
            ||
| 253 |             $fields->fieldByName('Root.Main') | 
            ||
| 254 | ->setTitle(_t(__CLASS__ . '.MainTabLabel', 'Content'));  | 
            ||
| 255 | |||
| 256 | // Remove divider lines on all block forms  | 
            ||
| 257 |             $fields->fieldByName('Root')->addExtraClass('form--no-dividers'); | 
            ||
| 258 | |||
| 259 |             $fields->addFieldsToTab('Root.Main', [ | 
            ||
| 260 |                 HiddenField::create('AbsoluteLink', false, Director::absoluteURL($this->PreviewLink())), | 
            ||
| 261 |                 HiddenField::create('LiveLink', false, Director::absoluteURL($this->Link())), | 
            ||
| 262 |                 HiddenField::create('StageLink', false, Director::absoluteURL($this->PreviewLink())), | 
            ||
| 263 | ]);  | 
            ||
| 264 | |||
| 265 |             $styles = $this->config()->get('styles'); | 
            ||
| 266 | |||
| 267 |             if ($styles && count($styles) > 0) { | 
            ||
| 268 | $styleDropdown = new DropdownField(  | 
            ||
| 269 | 'Style',  | 
            ||
| 270 | _t(__CLASS__.'.STYLE', 'Style variation'),  | 
            ||
| 271 | $styles  | 
            ||
| 272 | );  | 
            ||
| 273 | |||
| 274 | $fields->insertBefore($styleDropdown, 'ExtraClass');  | 
            ||
| 275 | |||
| 276 | $styleDropdown->setEmptyString(_t(__CLASS__.'.CUSTOM_STYLES', 'Select a style..'));  | 
            ||
| 277 |             } else { | 
            ||
| 278 |                 $fields->removeByName('Style'); | 
            ||
| 279 | }  | 
            ||
| 280 | |||
| 281 | $history = $this->getHistoryFields();  | 
            ||
| 282 | |||
| 283 |             if ($history) { | 
            ||
| 284 |                 $fields->addFieldsToTab('Root.History', $history); | 
            ||
| 285 | }  | 
            ||
| 286 | });  | 
            ||
| 287 | |||
| 288 | return parent::getCMSFields();  | 
            ||
| 289 | }  | 
            ||
| 290 | |||
| 291 | /**  | 
            ||
| 292 | * Returns the history fields for this element.  | 
            ||
| 293 | *  | 
            ||
| 294 | * @return FieldList  | 
            ||
| 295 | */  | 
            ||
| 296 | public function getHistoryFields()  | 
            ||
| 297 |     { | 
            ||
| 298 |         if (!$this->isLatestVersion()) { | 
            ||
| 299 | // if viewing the history of the of page then don't show the history  | 
            ||
| 300 | // fields as then we have recursion.  | 
            ||
| 301 | return null;  | 
            ||
| 302 | }  | 
            ||
| 303 | |||
| 304 | $config = GridFieldConfig_RecordViewer::create();  | 
            ||
| 305 | $config->removeComponentsByType(GridFieldPageCount::class);  | 
            ||
| 306 | |||
| 307 | $config  | 
            ||
| 308 | ->getComponentByType(GridFieldDetailForm::class)  | 
            ||
| 309 | ->setItemRequestClass(HistoricalVersionedGridFieldItemRequest::class);  | 
            ||
| 310 | |||
| 311 | $config->getComponentByType(GridFieldDataColumns::class)->setDisplayFields([  | 
            ||
| 312 | 'Version' => '#',  | 
            ||
| 313 | 'LastEdited' => _t(__CLASS__ . '.LastEdited', 'Last Edited'),  | 
            ||
| 314 | 'getAuthor.Name' => _t(__CLASS__ . '.Author', 'Author')  | 
            ||
| 315 | ]);  | 
            ||
| 316 | |||
| 317 | $config->removeComponentsByType(GridFieldViewButton::class);  | 
            ||
| 318 | $config->addComponent(new ElementalGridFieldHistoryButton());  | 
            ||
| 319 | |||
| 320 | $history = Versioned::get_all_versions(__CLASS__, $this->ID)  | 
            ||
| 321 |             ->sort('Version', 'DESC'); | 
            ||
| 322 | |||
| 323 | return FieldList::create(  | 
            ||
| 324 | GridField::create(  | 
            ||
| 325 | 'History',  | 
            ||
| 326 | '',  | 
            ||
| 327 | $history,  | 
            ||
| 328 | $config  | 
            ||
| 329 | )  | 
            ||
| 330 | );  | 
            ||
| 331 | }  | 
            ||
| 332 | |||
| 333 | /**  | 
            ||
| 334 | * Get the type of the current block, for use in GridField summaries, block  | 
            ||
| 335 | * type dropdowns etc. Examples are "Content", "File", "Media", etc.  | 
            ||
| 336 | *  | 
            ||
| 337 | * @return string  | 
            ||
| 338 | */  | 
            ||
| 339 | public function getType()  | 
            ||
| 340 |     { | 
            ||
| 341 | return _t(__CLASS__ . '.BlockType', 'Block');  | 
            ||
| 342 | }  | 
            ||
| 343 | |||
| 344 | /**  | 
            ||
| 345 | * @param ElementController  | 
            ||
| 346 | *  | 
            ||
| 347 | * @return $this  | 
            ||
| 348 | */  | 
            ||
| 349 | public function setController($controller)  | 
            ||
| 350 |     { | 
            ||
| 351 | $this->controller = $controller;  | 
            ||
| 352 | |||
| 353 | return $this;  | 
            ||
| 354 | }  | 
            ||
| 355 | |||
| 356 | /**  | 
            ||
| 357 | * @throws Exception  | 
            ||
| 358 | *  | 
            ||
| 359 | * @return ElementController  | 
            ||
| 360 | */  | 
            ||
| 361 | public function getController()  | 
            ||
| 362 |     { | 
            ||
| 363 |         if ($this->controller) { | 
            ||
| 364 | return $this->controller;  | 
            ||
| 365 | }  | 
            ||
| 366 | |||
| 367 | $controllerClass = self::config()->controller_class;  | 
            ||
| 368 | |||
| 369 |         if (!class_exists($controllerClass)) { | 
            ||
| 370 |             throw new Exception('Could not find controller class ' . $controllerClass . ' as defined in ' . static::class); | 
            ||
| 371 | }  | 
            ||
| 372 | |||
| 373 | $this->controller = Injector::inst()->create($controllerClass, $this);  | 
            ||
| 374 | $this->controller->doInit();  | 
            ||
| 375 | |||
| 376 | return $this->controller;  | 
            ||
| 377 | }  | 
            ||
| 378 | |||
| 379 | /**  | 
            ||
| 380 | * @return Controller  | 
            ||
| 381 | */  | 
            ||
| 382 | public function Top()  | 
            ||
| 383 |     { | 
            ||
| 384 | return (Controller::has_curr()) ? Controller::curr() : null;  | 
            ||
| 385 | }  | 
            ||
| 386 | |||
| 387 | /**  | 
            ||
| 388 | * Default way to render element in templates. Note that all blocks should  | 
            ||
| 389 |      * be rendered through their {@link ElementController} class as this | 
            ||
| 390 | * contains the holder styles.  | 
            ||
| 391 | *  | 
            ||
| 392 | * @return string HTML  | 
            ||
| 393 | */  | 
            ||
| 394 | public function forTemplate($holder = true)  | 
            ||
| 395 |     { | 
            ||
| 396 | $templates = $this->getRenderTemplates();  | 
            ||
| 397 | |||
| 398 |         if ($templates) { | 
            ||
| 399 | return $this->renderWith($templates);  | 
            ||
| 400 | }  | 
            ||
| 401 | }  | 
            ||
| 402 | |||
| 403 | /**  | 
            ||
| 404 | * @param string $suffix  | 
            ||
| 405 | *  | 
            ||
| 406 | * @return array  | 
            ||
| 407 | */  | 
            ||
| 408 | public function getRenderTemplates($suffix = '')  | 
            ||
| 409 |     { | 
            ||
| 410 | $classes = ClassInfo::ancestry($this->ClassName);  | 
            ||
| 411 | $classes[static::class] = static::class;  | 
            ||
| 412 | $classes = array_reverse($classes);  | 
            ||
| 413 | $templates = array();  | 
            ||
| 414 | |||
| 415 |         foreach ($classes as $key => $value) { | 
            ||
| 416 |             if ($value == BaseElement::class) { | 
            ||
| 417 | continue;  | 
            ||
| 418 | }  | 
            ||
| 419 | |||
| 420 |             if ($value == DataObject::class) { | 
            ||
| 421 | break;  | 
            ||
| 422 | }  | 
            ||
| 423 | |||
| 424 | $templates[] = $value . $suffix;  | 
            ||
| 425 | }  | 
            ||
| 426 | |||
| 427 | return $templates;  | 
            ||
| 428 | }  | 
            ||
| 429 | |||
| 430 | /**  | 
            ||
| 431 | * Strip all namespaces from class namespace.  | 
            ||
| 432 | *  | 
            ||
| 433 | * @param string $classname e.g. "\Fully\Namespaced\Class"  | 
            ||
| 434 | *  | 
            ||
| 435 | * @return string following the param example, "Class"  | 
            ||
| 436 | */  | 
            ||
| 437 | protected function stripNamespacing($classname)  | 
            ||
| 438 |     { | 
            ||
| 439 |         $classParts = explode('\\', $classname); | 
            ||
| 440 | return array_pop($classParts);  | 
            ||
| 441 | }  | 
            ||
| 442 | |||
| 443 | /**  | 
            ||
| 444 | * @return string  | 
            ||
| 445 | */  | 
            ||
| 446 | public function getSimpleClassName()  | 
            ||
| 447 |     { | 
            ||
| 448 | return strtolower($this->sanitiseClassName($this->ClassName, '__'));  | 
            ||
| 449 | }  | 
            ||
| 450 | |||
| 451 | /**  | 
            ||
| 452 | * @return SiteTree  | 
            ||
| 453 | */  | 
            ||
| 454 | public function getPage()  | 
            ||
| 455 |     { | 
            ||
| 456 | $area = $this->Parent();  | 
            ||
| 457 | |||
| 458 |         if ($area instanceof ElementalArea && $area->exists()) { | 
            ||
| 459 | return $area->getOwnerPage();  | 
            ||
| 460 | }  | 
            ||
| 461 | |||
| 462 | return null;  | 
            ||
| 463 | }  | 
            ||
| 464 | |||
| 465 | /**  | 
            ||
| 466 | * Get a unique anchor name  | 
            ||
| 467 | *  | 
            ||
| 468 | * @return string  | 
            ||
| 469 | */  | 
            ||
| 470 | public function getAnchor()  | 
            ||
| 471 |     { | 
            ||
| 472 |         if ($this->_anchor !== null) { | 
            ||
| 473 | return $this->_anchor;  | 
            ||
| 474 | }  | 
            ||
| 475 | |||
| 476 | $anchorTitle = '';  | 
            ||
| 477 | |||
| 478 |         if (!$this->config()->disable_pretty_anchor_name) { | 
            ||
| 479 |             if ($this->hasMethod('getAnchorTitle')) { | 
            ||
| 480 | $anchorTitle = $this->getAnchorTitle();  | 
            ||
| 481 |             } elseif ($this->config()->enable_title_in_template) { | 
            ||
| 482 |                 $anchorTitle = $this->getField('Title'); | 
            ||
| 483 | }  | 
            ||
| 484 | }  | 
            ||
| 485 | |||
| 486 |         if (!$anchorTitle) { | 
            ||
| 487 | $anchorTitle = 'e'.$this->ID;  | 
            ||
| 488 | }  | 
            ||
| 489 | |||
| 490 | $filter = URLSegmentFilter::create();  | 
            ||
| 491 | $titleAsURL = $filter->filter($anchorTitle);  | 
            ||
| 492 | |||
| 493 | // Ensure that this anchor name isn't already in use  | 
            ||
| 494 | // ie. If two elemental blocks have the same title, it'll append '-2', '-3'  | 
            ||
| 495 | $result = $titleAsURL;  | 
            ||
| 496 | $count = 1;  | 
            ||
| 497 |         while (isset(self::$_used_anchors[$result]) && self::$_used_anchors[$result] !== $this->ID) { | 
            ||
| 498 | ++$count;  | 
            ||
| 499 | $result = $titleAsURL.'-'.$count;  | 
            ||
| 500 | }  | 
            ||
| 501 | self::$_used_anchors[$result] = $this->ID;  | 
            ||
| 502 | return $this->_anchor = $result;  | 
            ||
| 503 | }  | 
            ||
| 504 | |||
| 505 | /**  | 
            ||
| 506 | * @param string $action  | 
            ||
| 507 | *  | 
            ||
| 508 | * @return string  | 
            ||
| 509 | */  | 
            ||
| 510 | public function AbsoluteLink($action = null)  | 
            ||
| 511 |     { | 
            ||
| 512 |         if ($page = $this->getPage()) { | 
            ||
| 513 | $link = $page->AbsoluteLink($action) . '#' . $this->getAnchor();  | 
            ||
| 514 | |||
| 515 | return $link;  | 
            ||
| 516 | }  | 
            ||
| 517 | }  | 
            ||
| 518 | |||
| 519 | /**  | 
            ||
| 520 | * @param string $action  | 
            ||
| 521 | *  | 
            ||
| 522 | * @return string  | 
            ||
| 523 | */  | 
            ||
| 524 | public function Link($action = null)  | 
            ||
| 525 |     { | 
            ||
| 526 |         if ($page = $this->getPage()) { | 
            ||
| 527 | $link = $page->Link($action) . '#' . $this->getAnchor();  | 
            ||
| 528 | |||
| 529 |             $this->extend('updateLink', $link); | 
            ||
| 530 | |||
| 531 | return $link;  | 
            ||
| 532 | }  | 
            ||
| 533 | }  | 
            ||
| 534 | |||
| 535 | /**  | 
            ||
| 536 | * @param string $action  | 
            ||
| 537 | *  | 
            ||
| 538 | * @return string  | 
            ||
| 539 | */  | 
            ||
| 540 | public function PreviewLink($action = null)  | 
            ||
| 541 |     { | 
            ||
| 542 | $action = $action . '?ElementalPreview=' . mt_rand();  | 
            ||
| 543 | $link = $this->Link($action);  | 
            ||
| 544 |         $this->extend('updatePreviewLink', $link); | 
            ||
| 545 | |||
| 546 | return $link;  | 
            ||
| 547 | }  | 
            ||
| 548 | |||
| 549 | /**  | 
            ||
| 550 | * @return boolean  | 
            ||
| 551 | */  | 
            ||
| 552 | public function isCMSPreview()  | 
            ||
| 553 |     { | 
            ||
| 554 |         if (Controller::has_curr()) { | 
            ||
| 555 | $c = Controller::curr();  | 
            ||
| 556 | |||
| 557 |             if ($c->getRequest()->requestVar('CMSPreview')) { | 
            ||
| 558 | return true;  | 
            ||
| 559 | }  | 
            ||
| 560 | }  | 
            ||
| 561 | |||
| 562 | return false;  | 
            ||
| 563 | }  | 
            ||
| 564 | |||
| 565 | /**  | 
            ||
| 566 | * @return string  | 
            ||
| 567 | */  | 
            ||
| 568 | public function CMSEditLink()  | 
            ||
| 569 |     { | 
            ||
| 570 | $relationName = $this->getAreaRelationName();  | 
            ||
| 571 | $page = $this->getPage(true);  | 
            ||
| 572 | |||
| 573 |         if (!$page) { | 
            ||
| 574 | return null;  | 
            ||
| 575 | }  | 
            ||
| 576 | |||
| 577 | $link = Controller::join_links(  | 
            ||
| 578 |             singleton(CMSPageEditController::class)->Link('EditForm'), | 
            ||
| 579 | $page->ID,  | 
            ||
| 580 | 'field/' . $relationName . '/item/',  | 
            ||
| 581 | $this->ID  | 
            ||
| 582 | );  | 
            ||
| 583 | |||
| 584 | return Controller::join_links(  | 
            ||
| 585 | $link,  | 
            ||
| 586 | 'edit'  | 
            ||
| 587 | );  | 
            ||
| 588 | }  | 
            ||
| 589 | |||
| 590 | /**  | 
            ||
| 591 | * Retrieve a elemental area relation for creating cms links  | 
            ||
| 592 | *  | 
            ||
| 593 | * @return string - the name of a valid elemental area relation  | 
            ||
| 594 | */  | 
            ||
| 595 | public function getAreaRelationName()  | 
            ||
| 596 |     { | 
            ||
| 597 | $page = $this->getPage(true);  | 
            ||
| 598 | |||
| 599 |         if ($page) { | 
            ||
| 600 |             $has_one = $page->config()->get('has_one'); | 
            ||
| 601 | $area = $this->Parent();  | 
            ||
| 602 | |||
| 603 |             foreach ($has_one as $relationName => $relationClass) { | 
            ||
| 604 |                 if ($relationClass === $area->ClassName) { | 
            ||
| 605 | return $relationName;  | 
            ||
| 606 | }  | 
            ||
| 607 | }  | 
            ||
| 608 | }  | 
            ||
| 609 | |||
| 610 | return 'ElementalArea';  | 
            ||
| 611 | }  | 
            ||
| 612 | |||
| 613 | /**  | 
            ||
| 614 | * Sanitise a model class' name for inclusion in a link.  | 
            ||
| 615 | *  | 
            ||
| 616 | * @return string  | 
            ||
| 617 | */  | 
            ||
| 618 | public function sanitiseClassName($class, $delimiter = '-')  | 
            ||
| 619 |     { | 
            ||
| 620 |         return str_replace('\\', $delimiter, $class); | 
            ||
| 621 | }  | 
            ||
| 622 | |||
| 623 | public function unsanitiseClassName($class, $delimiter = '-')  | 
            ||
| 624 |     { | 
            ||
| 625 | return str_replace($delimiter, '\\', $class);  | 
            ||
| 626 | }  | 
            ||
| 627 | |||
| 628 | /**  | 
            ||
| 629 | * @return string  | 
            ||
| 630 | */  | 
            ||
| 631 | public function getEditLink()  | 
            ||
| 632 |     { | 
            ||
| 633 | return $this->CMSEditLink();  | 
            ||
| 634 | }  | 
            ||
| 635 | |||
| 636 | /**  | 
            ||
| 637 | * @return HTMLText  | 
            ||
| 638 | */  | 
            ||
| 639 | public function PageCMSEditLink()  | 
            ||
| 640 |     { | 
            ||
| 641 |         if ($page = $this->getPage()) { | 
            ||
| 642 |             return DBField::create_field('HTMLText', sprintf( | 
            ||
| 643 | '<a href="%s">%s</a>',  | 
            ||
| 644 | $page->CMSEditLink(),  | 
            ||
| 645 | $page->Title  | 
            ||
| 646 | ));  | 
            ||
| 647 | }  | 
            ||
| 648 | }  | 
            ||
| 649 | |||
| 650 | /**  | 
            ||
| 651 | * @return string  | 
            ||
| 652 | */  | 
            ||
| 653 | public function getMimeType()  | 
            ||
| 654 |     { | 
            ||
| 655 | return 'text/html';  | 
            ||
| 656 | }  | 
            ||
| 657 | |||
| 658 | /**  | 
            ||
| 659 | * This can be overridden on child elements to create a summary for display  | 
            ||
| 660 | * in GridFields.  | 
            ||
| 661 | *  | 
            ||
| 662 | * @return string  | 
            ||
| 663 | */  | 
            ||
| 664 | public function getSummary()  | 
            ||
| 665 |     { | 
            ||
| 666 | return '';  | 
            ||
| 667 | }  | 
            ||
| 668 | |||
| 669 | |||
| 670 | /**  | 
            ||
| 671 | * Generate markup for element type icons suitable for use in GridFields.  | 
            ||
| 672 | *  | 
            ||
| 673 | * @return DBField  | 
            ||
| 674 | */  | 
            ||
| 675 | public function getIcon()  | 
            ||
| 676 |     { | 
            ||
| 677 |         $icon = $this->config()->get('icon'); | 
            ||
| 678 | |||
| 679 |         if ($icon) { | 
            ||
| 680 | $icon = ModuleResourceLoader::resourceURL($icon);  | 
            ||
| 681 | |||
| 682 |             return DBField::create_field('HTMLVarchar', '<img width="16px" src="' . Director::absoluteBaseURL() . $icon . '" alt="" />'); | 
            ||
| 683 | }  | 
            ||
| 684 | }  | 
            ||
| 685 | |||
| 686 | /**  | 
            ||
| 687 | * Generate markup for element type, with description suitable for use in  | 
            ||
| 688 | * GridFields.  | 
            ||
| 689 | *  | 
            ||
| 690 | * @return DBField  | 
            ||
| 691 | */  | 
            ||
| 692 | public function getTypeNice()  | 
            ||
| 693 |     { | 
            ||
| 694 |         $description = $this->config()->get('description'); | 
            ||
| 695 | $desc = ($description) ? ' <span class="el-description"> — ' . $description . '</span>' : '';  | 
            ||
| 696 | |||
| 697 | return DBField::create_field(  | 
            ||
| 698 | 'HTMLVarchar',  | 
            ||
| 699 | $this->getType() . $desc  | 
            ||
| 700 | );  | 
            ||
| 701 | }  | 
            ||
| 702 | |||
| 703 | /**  | 
            ||
| 704 | * @return HTMLText  | 
            ||
| 705 | */  | 
            ||
| 706 | public function getEditorPreview()  | 
            ||
| 707 |     { | 
            ||
| 708 |         $templates = $this->getRenderTemplates('_EditorPreview'); | 
            ||
| 709 | $templates[] = BaseElement::class . '_EditorPreview';  | 
            ||
| 710 | |||
| 711 | return $this->renderWith($templates);  | 
            ||
| 712 | }  | 
            ||
| 713 | |||
| 714 | /**  | 
            ||
| 715 | * @return Member  | 
            ||
| 716 | */  | 
            ||
| 717 | public function getAuthor()  | 
            ||
| 721 | }  | 
            ||
| 722 | }  | 
            ||
| 723 | |||
| 724 | /**  | 
            ||
| 725 | * @return string  | 
            ||
| 726 | */  | 
            ||
| 727 | public function getStyleVariant()  | 
            ||
| 728 |     { | 
            ||
| 729 | $style = $this->Style;  | 
            ||
| 730 | |||
| 731 |         if (isset($styles[$style])) { | 
            ||
| 732 | $style = strtolower($styles[$style]);  | 
            ||
| 733 |         } else { | 
            ||
| 734 | $style = '';  | 
            ||
| 735 | }  | 
            ||
| 736 | |||
| 737 |         $this->extend('updateStyleVariant', $style); | 
            ||
| 738 | |||
| 739 | return $style;  | 
            ||
| 740 | }  | 
            ||
| 741 | |||
| 742 | /**  | 
            ||
| 743 | *  | 
            ||
| 744 | */  | 
            ||
| 745 | public function getPageTitle()  | 
            ||
| 754 | }  | 
            ||
| 755 | }  | 
            ||
| 756 | 
This check marks private properties in classes that are never used. Those properties can be removed.