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