silverstripe-blog/code/widgets/BlogCategoriesWidget.php

98 lines
2.3 KiB
PHP
Raw Normal View History

2013-08-04 18:38:26 +02:00
<?php
2015-05-09 16:33:12 +02:00
if(!class_exists("Widget")) {
return;
}
2013-08-04 18:38:26 +02:00
2015-05-09 16:33:12 +02:00
/**
* @method Blog Blog()
*/
class BlogCategoriesWidget extends Widget {
/**
* @var string
*/
private static $title = 'Categories';
2013-08-04 18:38:26 +02:00
2015-05-09 16:33:12 +02:00
/**
* @var string
*/
private static $cmsTitle = 'Blog Categories';
2013-08-04 18:38:26 +02:00
2015-05-09 16:33:12 +02:00
/**
* @var string
*/
private static $description = 'Displays a list of blog categories.';
2013-08-04 18:38:26 +02:00
2015-05-09 16:33:12 +02:00
/**
* @var array
*/
2015-07-08 03:07:08 +02:00
private static $db = array(
'Limit' => 'Int',
'Order' => 'Varchar',
'Direction' => 'Varchar',
);
2013-08-04 18:38:26 +02:00
2015-05-09 16:33:12 +02:00
/**
* @var array
*/
private static $has_one = array(
'Blog' => 'Blog',
);
2013-10-10 00:09:28 +02:00
2015-05-09 16:33:12 +02:00
/**
* {@inheritdoc}
*/
public function getCMSFields() {
2015-07-08 03:07:08 +02:00
$this->beforeUpdateCMSFields(function (FieldList $fields) {
$fields[] = DropdownField::create(
'BlogID', _t('BlogCategoriesWidget.Blog', 'Blog'), Blog::get()->map()
2015-05-09 16:33:12 +02:00
);
2015-07-08 03:07:08 +02:00
$fields[] = NumericField::create(
'Limit', _t('BlogCategoriesWidget.Limit.Label', 'Limit'), 0
)
->setDescription(_t('BlogCategoriesWidget.Limit.Description', 'Limit the number of categories shown by this widget (set to 0 to show all categories).'))
->setMaxLength(3);
$fields[] = DropdownField::create(
'Order', _t('BlogCategoriesWidget.Sort.Label', 'Sort'), array('Title' => 'Title', 'Created' => 'Created', 'LastUpdated' => 'Updated')
)
->setDescription(_t('BlogCategoriesWidget.Sort.Description', 'Change the order of categories shown by this widget.'));
$fields[] = DropdownField::create(
'Direction', _t('BlogCategoriesWidget.Direction.Label', 'Direction'), array('ASC' => 'Ascending', 'DESC' => 'Descending')
)
->setDescription(_t('BlogCategoriesWidget.Direction.Description', 'Change the direction of ordering of categories shown by this widget.'));
2015-05-09 16:33:12 +02:00
});
return parent::getCMSFields();
}
2013-08-04 18:38:26 +02:00
2015-05-09 16:33:12 +02:00
/**
2015-07-08 03:07:08 +02:00
* @return DataList
2015-05-09 16:33:12 +02:00
*/
public function getCategories() {
2015-07-08 03:07:08 +02:00
$blog = $this->Blog();
if (!$blog) {
return array();
}
$query = $blog->Categories();
if ($this->Limit) {
$query = $query->limit(Convert::raw2sql($this->Limit));
}
if ($this->Order && $this->Direction) {
$query = $query->sort(Convert::raw2sql($this->Order), Convert::raw2sql($this->Direction));
2013-08-04 18:38:26 +02:00
}
2013-10-10 00:09:28 +02:00
2015-07-08 03:07:08 +02:00
return $query;
2013-08-04 18:38:26 +02:00
}
2015-05-09 16:33:12 +02:00
}
2013-08-04 18:38:26 +02:00
2015-05-09 16:33:12 +02:00
class BlogCategoriesWidget_Controller extends Widget_Controller {
2013-08-04 18:38:26 +02:00
2013-10-10 00:09:28 +02:00
}