1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class Abbreviation extends DataObject implements Shortcodable |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
|
6
|
|
|
private static $db = array( |
|
|
|
|
7
|
|
|
'Title' => 'Varchar(255)', |
8
|
|
|
'Description' => 'Varchar(255)', |
9
|
|
|
'URLSlug' => 'Varchar(255)', |
10
|
|
|
'Explanation' => 'HTMLText' |
11
|
|
|
); |
12
|
|
|
|
13
|
|
|
private static $has_one = array( |
|
|
|
|
14
|
|
|
'Page' => 'Page' |
15
|
|
|
); |
16
|
|
|
|
17
|
|
|
private static $singular_name = 'Abbreviation'; |
|
|
|
|
18
|
|
|
|
19
|
|
|
private static $plural_name = 'Abbreviations'; |
|
|
|
|
20
|
|
|
|
21
|
|
|
private static $summary_fields = array('Title', 'Description'); |
|
|
|
|
22
|
|
|
|
23
|
|
|
private static $searchable_fields = array('Title', 'Description'); |
|
|
|
|
24
|
|
|
|
25
|
|
|
public function getCMSFields() |
26
|
|
|
{ |
27
|
|
|
$fields = parent::getCMSFields(); |
28
|
|
|
$fields->removeByName('URLSlug'); |
29
|
|
|
return $fields; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Taken from https://github.com/NightJar/ssrigging-slug/blob/master/code/Slug.php |
34
|
|
|
*/ |
35
|
|
|
public function Slug($regen=false) |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$existing = $this->URLSlug; |
|
|
|
|
38
|
|
|
return $existing && !$regen ? $existing : URLSegmentFilter::create()->filter($this->Title); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Taken from https://github.com/NightJar/ssrigging-slug/blob/master/code/Slug.php |
43
|
|
|
*/ |
44
|
|
|
public function onBeforeWrite() |
45
|
|
|
{ |
46
|
|
|
parent::onBeforeWrite(); |
47
|
|
|
if ($this->isChanged('URLSlug') || !$this->URLSlug || $this->isChanged($this->Title)) { |
|
|
|
|
48
|
|
|
$this->URLSlug = $this->Slug(); |
|
|
|
|
49
|
|
|
$class = $this->class; |
50
|
|
|
$filter = array('URLSlug' => $this->URLSlug); |
|
|
|
|
51
|
|
|
if ($parent = $this->parentRel) { |
|
|
|
|
52
|
|
|
$filter[$parent] = $this->$parent; |
53
|
|
|
} |
54
|
|
|
$count = 1; |
55
|
|
|
while ($exists = $class::get()->filter($filter)->exclude('ID', $this->ID)->exists()) { |
|
|
|
|
56
|
|
|
$this->URLSlug = $this->URLSlug.$count++; |
|
|
|
|
57
|
|
|
$filter['URLSlug'] = $this->URLSlug; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function Link() |
63
|
|
|
{ |
64
|
|
|
return Controller::join_links($this->Page()->Link(), $this->URLSlug); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function AbsoluteLink() |
68
|
|
|
{ |
69
|
|
|
return Director::absoluteURL($this->Link()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function forTemplate() |
73
|
|
|
{ |
74
|
|
|
$template = new SSViewer('Abbreviation'); |
75
|
|
|
return $template->process($this); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Used for Breadcrumbs |
81
|
|
|
* |
82
|
|
|
* @return DBField |
83
|
|
|
*/ |
84
|
|
|
public function getMenuTitle() |
85
|
|
|
{ |
86
|
|
|
return $this->dbObject('Title'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns the first letter of the module title, used for grouping. |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function getTitleFirstLetter() |
94
|
|
|
{ |
95
|
|
|
return strtoupper($this->Title[0]); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Parse the shortcode and render as a string, probably with a template |
100
|
|
|
* @param array $arguments the list of attributes of the shortcode |
101
|
|
|
* @param string $content the shortcode content |
102
|
|
|
* @param ShortcodeParser $parser the ShortcodeParser instance |
103
|
|
|
* @param string $shortcode the raw shortcode being parsed |
104
|
|
|
* @return String |
|
|
|
|
105
|
|
|
**/ |
106
|
|
View Code Duplication |
public static function parse_shortcode($arguments, $content, $parser, $shortcode) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
if (empty($arguments['id'])) { |
109
|
|
|
return; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (array_key_exists('id', $arguments) && $arguments['id']) { |
113
|
|
|
$abbreviation = Abbreviation::get()->byID($arguments['id']); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (!$abbreviation) { |
|
|
|
|
117
|
|
|
return; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if (array_key_exists('title', $arguments) && $arguments['title']) { |
121
|
|
|
$abbreviation->Title = $arguments['title']; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$template = new SSViewer('AbbreviationShortcode'); |
125
|
|
|
return $template->process($abbreviation); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* returns a list of fields for editing the shortcode's attributes |
130
|
|
|
* @return Fieldlist |
|
|
|
|
131
|
|
|
**/ |
132
|
|
|
public static function shortcode_attribute_fields() |
133
|
|
|
{ |
134
|
|
|
//@todo use addnew field to add a new abbr. on the fly |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.