Source of file Abbreviation.php
Size: 3,792 Bytes - Last Modified: 2021-12-23T10:59:27+00:00
/var/www/docs.ssmods.com/process/src/code/model/Abbreviation.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 | <?php class Abbreviation extends DataObject implements Shortcodable { private static $db = array( 'Title' => 'Varchar(255)', 'Description' => 'Varchar(255)', 'URLSlug' => 'Varchar(255)', 'Explanation' => 'HTMLText' ); private static $has_one = array( 'Page' => 'Page' ); private static $singular_name = 'Abbreviation'; private static $plural_name = 'Abbreviations'; private static $summary_fields = array('Title', 'Description'); private static $searchable_fields = array('Title', 'Description'); public function getCMSFields() { $fields = parent::getCMSFields(); $fields->removeByName('URLSlug'); return $fields; } /** * Taken from https://github.com/NightJar/ssrigging-slug/blob/master/code/Slug.php */ public function Slug($regen=false) { $existing = $this->URLSlug; return $existing && !$regen ? $existing : URLSegmentFilter::create()->filter($this->Title); } /** * Taken from https://github.com/NightJar/ssrigging-slug/blob/master/code/Slug.php */ public function onBeforeWrite() { parent::onBeforeWrite(); if ($this->isChanged('URLSlug') || !$this->URLSlug || $this->isChanged($this->Title)) { $this->URLSlug = $this->Slug(); $class = $this->class; $filter = array('URLSlug' => $this->URLSlug); if ($parent = $this->parentRel) { $filter[$parent] = $this->$parent; } $count = 1; while ($exists = $class::get()->filter($filter)->exclude('ID', $this->ID)->exists()) { $this->URLSlug = $this->URLSlug.$count++; $filter['URLSlug'] = $this->URLSlug; } } } public function Link() { return Controller::join_links($this->Page()->Link(), $this->URLSlug); } public function AbsoluteLink() { return Director::absoluteURL($this->Link()); } public function forTemplate() { $template = new SSViewer('Abbreviation'); return $template->process($this); } /** * Used for Breadcrumbs * * @return DBField */ public function getMenuTitle() { return $this->dbObject('Title'); } /** * Returns the first letter of the module title, used for grouping. * @return string */ public function getTitleFirstLetter() { return strtoupper($this->Title[0]); } /** * Parse the shortcode and render as a string, probably with a template * @param array $arguments the list of attributes of the shortcode * @param string $content the shortcode content * @param ShortcodeParser $parser the ShortcodeParser instance * @param string $shortcode the raw shortcode being parsed * @return String **/ public static function parse_shortcode($arguments, $content, $parser, $shortcode) { if (empty($arguments['id'])) { return; } if (array_key_exists('id', $arguments) && $arguments['id']) { $abbreviation = Abbreviation::get()->byID($arguments['id']); } if (!$abbreviation) { return; } if (array_key_exists('title', $arguments) && $arguments['title']) { $abbreviation->Title = $arguments['title']; } $template = new SSViewer('AbbreviationShortcode'); return $template->process($abbreviation); } /** * returns a list of fields for editing the shortcode's attributes * @return Fieldlist **/ public static function shortcode_attribute_fields() { //@todo use addnew field to add a new abbr. on the fly } } |