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