1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* extends Blog |
5
|
|
|
* |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class BlogSharedCategoriesExtension extends DataExtension |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
public function updateCMSFields(FieldList $fields) |
12
|
|
|
{ |
13
|
|
|
$fields->removeByName("Categories"); |
14
|
|
|
$fields->removeByName("Tags"); |
15
|
|
|
$fields->addFieldsToTab( |
16
|
|
|
"Root.Categorisation", |
17
|
|
|
array( |
18
|
|
|
GridField::create( |
19
|
|
|
"Categories", |
20
|
|
|
"Categories", |
21
|
|
|
$this->owner->Categories(), |
22
|
|
|
GridFieldConfig_RecordEditor ::create() |
23
|
|
|
) |
24
|
|
|
) |
25
|
|
|
); |
26
|
|
|
$fields->addFieldsToTab( |
27
|
|
|
"Root.Tags", |
28
|
|
|
array( |
29
|
|
|
GridField::create( |
30
|
|
|
"Tags", |
31
|
|
|
"Tags", |
32
|
|
|
$this->owner->Tags(), |
33
|
|
|
GridFieldConfig_RecordEditor ::create() |
34
|
|
|
) |
35
|
|
|
) |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* |
41
|
|
|
* @return DataList |
42
|
|
|
*/ |
43
|
|
|
public function UsedCategories() |
44
|
|
|
{ |
45
|
|
|
$categories = BlogCategory::get() |
46
|
|
|
->filter(array('BlogID' => $this->owner->ID)) |
47
|
|
|
->sort(array('Title' => 'ASC')); |
48
|
|
|
foreach ($categories as $category) { |
49
|
|
|
if ($category->BlogPosts()->count() == 0) { |
50
|
|
|
$categories = $categories->exclude(array('BlogCategory.ID' => $category->ID)); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $categories; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* overrules has_many method |
59
|
|
|
* to return ALL categories |
60
|
|
|
* |
61
|
|
|
* @return DataList |
62
|
|
|
*/ |
63
|
|
|
public function UsedTags() |
64
|
|
|
{ |
65
|
|
|
$tags = BlogTag::get(); |
66
|
|
|
foreach ($tags as $tag) { |
67
|
|
|
if ($tag->BlogPosts()->count() == 0) { |
68
|
|
|
$tags = $tags->exclude(array('BlogTag.ID' => $tag->ID)); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
return $tags; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* overrules has_many method |
77
|
|
|
* to return ALL categories |
78
|
|
|
* |
79
|
|
|
* @return DataList |
80
|
|
|
*/ |
81
|
|
|
public function Tags() |
82
|
|
|
{ |
83
|
|
|
return BlogTag::get(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* overrules has_many method |
88
|
|
|
* to return ALL categories |
89
|
|
|
* |
90
|
|
|
* @return DataList |
91
|
|
|
*/ |
92
|
|
|
public function Categories() |
93
|
|
|
{ |
94
|
|
|
return BlogCategory::get(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* overrules has_many method |
99
|
|
|
* to return ALL categories |
100
|
|
|
* |
101
|
|
|
* @return DataList |
102
|
|
|
*/ |
103
|
|
|
public function getTags() |
104
|
|
|
{ |
105
|
|
|
return BlogTag::get(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* overrules has_many method |
110
|
|
|
* to return ALL categories |
111
|
|
|
* |
112
|
|
|
* @return DataList |
113
|
|
|
*/ |
114
|
|
|
public function getCategories() |
115
|
|
|
{ |
116
|
|
|
return BlogCategory::get(); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
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.