1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class AbbreviationPage extends Page |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
|
6
|
|
|
public static $has_many = array( |
7
|
|
|
'Abbreviations' => 'Abbreviation' |
8
|
|
|
); |
9
|
|
|
|
10
|
|
View Code Duplication |
public function getCMSFields() |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
$fields = parent::getCMSFields(); |
13
|
|
|
|
14
|
|
|
$conf = GridFieldConfig_RecordEditor::create(); |
15
|
|
|
$grid = GridField::create('Abbreviations', $this->fieldLabel('Abbreviations'), $this->Abbreviations(), $conf); |
16
|
|
|
$tabTitle = $this->fieldLabel('Abbreviations'); |
17
|
|
|
$fields->addFieldsToTab('Root.' . $tabTitle, $grid); |
18
|
|
|
|
19
|
|
|
return $fields; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* If you add <% loop $AdditionalBreadcrumbsAfter %> to your BreadcrumbsTemplate you can |
24
|
|
|
* @return ArrayList |
25
|
|
|
*/ |
26
|
|
|
public function getAdditionalBreadcrumbsAfter() |
27
|
|
|
{ |
28
|
|
|
$pages = ArrayList::create(); |
29
|
|
|
$currentController = Controller::curr(); |
30
|
|
|
$slug = $currentController->request->param('Item'); |
31
|
|
|
$item = ($slug && $currentController->hasMethod('getItem')) ? $currentController->getItem($slug) : false; |
32
|
|
|
if ($slug && $item) { |
33
|
|
|
$pages->push($item); |
34
|
|
|
} |
35
|
|
|
return $pages; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
class AbbreviationPage_Controller extends ContentController |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
|
42
|
|
|
private static $url_handlers = array( |
|
|
|
|
43
|
|
|
'$Item!' => 'viewItem' |
44
|
|
|
); |
45
|
|
|
private static $allowed_actions = array( |
|
|
|
|
46
|
|
|
'viewItem' |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
public function index() |
50
|
|
|
{ |
51
|
|
|
return $this->renderWith(array('AbbreviationPage', 'Page')); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
public function viewItem() |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$item = $this->getItem($this->request->param('Item')); |
57
|
|
|
if (!$item) { |
58
|
|
|
$this->httpError(404); |
59
|
|
|
} |
60
|
|
|
return $this->customise(array('Item' => $item))->renderWith(array('AbbreviationPage_view', 'Page')); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getItem($slug) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
return Abbreviation::get()->filter(array('URLSlug' => $slug))->first(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getItems() |
69
|
|
|
{ |
70
|
|
|
return Abbreviation::get()->sort('Title'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getGroupedItems() |
74
|
|
|
{ |
75
|
|
|
return GroupedList::create($this->getItems()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getFirstLetterNavigation() |
79
|
|
|
{ |
80
|
|
|
$menu = array(); |
81
|
|
|
foreach (range('A', 'Z') as $char) { |
82
|
|
|
$menu[$char] = ArrayData::create(array('Title' => $char)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
foreach ($this->getGroupedItems()->GroupedBy('TitleFirstLetter') as $item) { |
86
|
|
|
$firstLetter = $item->TitleFirstLetter; |
87
|
|
|
|
88
|
|
|
if (array_key_exists($firstLetter, $menu)) { |
89
|
|
|
$menu[$firstLetter]->setField('hasItems', true); |
90
|
|
|
} else { |
91
|
|
|
$menu[$firstLetter] = ArrayData::create( |
92
|
|
|
array('Title' => $firstLetter, 'hasItems' => true) |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
ksort($menu); |
98
|
|
|
|
99
|
|
|
return ArrayList::create($menu); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
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.