forked from matt-bailey/silverstripe-widget-categories
-
Notifications
You must be signed in to change notification settings - Fork 0
/
categoriesWidget.php
59 lines (52 loc) · 1.85 KB
/
categoriesWidget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
class CategoriesWidget extends Widget
{
static $title = "Blog Categories";
static $cmsTitle = 'Blog Category Widget';
static $description = 'Show a list of blog categories as links';
static $db = array(
"WidgetTitle" => "Varchar(255)",
"ShowCount" => "Enum('Yes, No')"
);
static $defaults = array(
"WidgetTitle" => "Blog Categories",
"ShowCount" => "Yes"
);
public function getCMSFields()
{
return new FieldList(
new TextField('WidgetTitle', 'Widget Title'),
new DropdownField('ShowCount','Show Article Count', singleton('CategoriesWidget')->dbObject('ShowCount')->enumValues())
);
}
public function Title()
{
return $this->WidgetTitle ? $this->WidgetTitle : self::$title;
}
/**
* Count the categories
* @return string
*/
public function totalEntries($catID) {
$count = DB::query("SELECT COUNT(*) FROM SiteTree_Live LEFT JOIN BlogEntry ON SiteTree_Live.ID = BlogEntry.ID LEFT JOIN BlogEntry_BlogCategories ON BlogEntry_BlogCategories.BlogEntryID = BlogEntry.ID LEFT JOIN BlogCategory ON BlogEntry_BlogCategories.BlogCategoryID = BlogCategory.ID WHERE BlogCategory.ID LIKE '" . $catID . "'")->value();
return $count;
}
/**
* Get the blog categories
* @return ArrayList
*/
public function getCategories()
{
$categories = new ArrayList;
foreach(BlogCategory::get() as $category) {
$data = array(
'Title' => $category->Title,
'Link' => $category->Parent()->Link('category/'.$category->URLSegment),
'ShowCount' => $this->ShowCount,
'Count' => $this->totalEntries($category->ID)
);
$categories->push(new ArrayData($data));
}
return $categories;
}
}