2015-11-21 07:17:29 +01:00
|
|
|
<?php
|
|
|
|
|
2016-12-15 04:41:49 +01:00
|
|
|
namespace SilverStripe\Blog\Widgets;
|
|
|
|
|
2017-01-18 05:12:06 +01:00
|
|
|
if (!class_exists('\\SilverStripe\\Widgets\\Model\\Widget')) {
|
2015-11-21 07:17:29 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-18 05:12:06 +01:00
|
|
|
use SilverStripe\Blog\Model\Blog;
|
|
|
|
use SilverStripe\Core\Convert;
|
|
|
|
use SilverStripe\Forms\DropdownField;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\NumericField;
|
2017-09-14 00:27:40 +02:00
|
|
|
use SilverStripe\ORM\DataList;
|
2017-01-18 05:12:06 +01:00
|
|
|
use SilverStripe\Widgets\Model\Widget;
|
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* @method Blog Blog()
|
|
|
|
*/
|
|
|
|
class BlogCategoriesWidget extends Widget
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $title = 'Categories';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $cmsTitle = 'Blog Categories';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $description = 'Displays a list of blog categories.';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-09-14 00:59:01 +02:00
|
|
|
private static $db = [
|
2015-11-21 07:17:29 +01:00
|
|
|
'Limit' => 'Int',
|
|
|
|
'Order' => 'Varchar',
|
|
|
|
'Direction' => 'Varchar',
|
2017-09-14 00:59:01 +02:00
|
|
|
];
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-09-14 00:59:01 +02:00
|
|
|
private static $has_one = [
|
2017-01-18 05:12:06 +01:00
|
|
|
'Blog' => Blog::class,
|
2017-09-14 00:59:01 +02:00
|
|
|
];
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getCMSFields()
|
|
|
|
{
|
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) {
|
|
|
|
$fields[] = DropdownField::create(
|
2017-01-18 05:12:06 +01:00
|
|
|
'BlogID',
|
2017-09-14 00:27:40 +02:00
|
|
|
_t(__CLASS__ . '.Blog', 'Blog'),
|
2017-01-18 05:12:06 +01:00
|
|
|
Blog::get()->map()
|
2015-11-21 07:17:29 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$fields[] = NumericField::create(
|
2017-01-18 05:12:06 +01:00
|
|
|
'Limit',
|
2017-09-14 00:27:40 +02:00
|
|
|
_t(__CLASS__ . '.Limit', 'Limit'),
|
2017-01-18 05:12:06 +01:00
|
|
|
0
|
2015-11-21 07:17:29 +01:00
|
|
|
)
|
2017-01-18 05:12:06 +01:00
|
|
|
->setDescription(
|
|
|
|
_t(
|
2017-09-14 00:27:40 +02:00
|
|
|
__CLASS__ . '.Limit_Description',
|
2017-01-18 05:12:06 +01:00
|
|
|
'Limit the number of categories shown by this widget (set to 0 to show all categories).'
|
|
|
|
)
|
|
|
|
)
|
2015-11-21 07:17:29 +01:00
|
|
|
->setMaxLength(3);
|
|
|
|
|
|
|
|
$fields[] = DropdownField::create(
|
2017-01-18 05:12:06 +01:00
|
|
|
'Order',
|
2017-09-14 00:27:40 +02:00
|
|
|
_t(__CLASS__ . '.Sort', 'Sort'),
|
2017-09-14 00:59:01 +02:00
|
|
|
['Title' => 'Title', 'Created' => 'Created', 'LastEdited' => 'Updated']
|
2015-11-21 07:17:29 +01:00
|
|
|
)
|
2017-01-18 05:12:06 +01:00
|
|
|
->setDescription(
|
2017-09-14 00:27:40 +02:00
|
|
|
_t(__CLASS__ . '.Sort_Description', 'Change the order of categories shown by this widget.')
|
2017-01-18 05:12:06 +01:00
|
|
|
);
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
$fields[] = DropdownField::create(
|
2017-01-18 05:12:06 +01:00
|
|
|
'Direction',
|
2017-09-14 00:27:40 +02:00
|
|
|
_t(__CLASS__ . '.Direction', 'Direction'),
|
2017-09-14 00:59:01 +02:00
|
|
|
['ASC' => 'Ascending', 'DESC' => 'Descending']
|
2015-11-21 07:17:29 +01:00
|
|
|
)
|
2017-01-18 05:12:06 +01:00
|
|
|
->setDescription(
|
|
|
|
_t(
|
2017-09-14 00:27:40 +02:00
|
|
|
__CLASS__ . '.Direction_Description',
|
2017-01-18 05:12:06 +01:00
|
|
|
'Change the direction of ordering of categories shown by this widget.'
|
|
|
|
)
|
|
|
|
);
|
2015-11-21 07:17:29 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return parent::getCMSFields();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return DataList
|
|
|
|
*/
|
|
|
|
public function getCategories()
|
|
|
|
{
|
|
|
|
$blog = $this->Blog();
|
|
|
|
|
|
|
|
if (!$blog) {
|
2017-09-14 00:59:01 +02:00
|
|
|
return [];
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$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));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
}
|