silverstripe-blog/src/Widgets/BlogTagsWidget.php

132 lines
3.1 KiB
PHP
Raw Permalink Normal View History

2015-11-21 07:17:29 +01:00
<?php
namespace SilverStripe\Blog\Widgets;
use SilverStripe\Blog\Model\Blog;
use SilverStripe\Core\Convert;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\NumericField;
use SilverStripe\ORM\DataList;
use SilverStripe\Widgets\Model\Widget;
if (!class_exists(Widget::class)) {
return;
}
2015-11-21 07:17:29 +01:00
/**
* @method Blog Blog()
*/
class BlogTagsWidget extends Widget
{
/**
* @var string
*/
private static $title = 'Tags';
/**
* @var string
*/
private static $cmsTitle = 'Blog Tags';
/**
* @var string
*/
private static $description = 'Displays a list of blog tags.';
/**
* @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 = [
'Blog' => Blog::class
2017-09-14 00:59:01 +02:00
];
2015-11-21 07:17:29 +01:00
/**
* @var string
*/
private static $table_name = 'BlogTagsWidget';
2015-11-21 07:17:29 +01:00
/**
* {@inheritdoc}
*/
public function getCMSFields()
{
$this->beforeUpdateCMSFields(function (Fieldlist $fields) {
$fields[] = DropdownField::create(
'BlogID',
2017-09-14 00:27:40 +02:00
_t(__CLASS__ . '.Blog', 'Blog'),
Blog::get()->map()
2015-11-21 07:17:29 +01:00
);
$fields[] = NumericField::create(
'Limit',
2017-09-14 00:27:40 +02:00
_t(__CLASS__ . '.Limit', 'Limit'),
0
2015-11-21 07:17:29 +01:00
)
->setDescription(
_t(
2017-09-14 00:27:40 +02:00
__CLASS__ . '.Limit_Description',
'Limit the number of tags shown by this widget (set to 0 to show all tags).'
)
)
2015-11-21 07:17:29 +01:00
->setMaxLength(3);
$fields[] = DropdownField::create(
'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
)
->setDescription(
2017-09-14 00:27:40 +02:00
_t(__CLASS__ . '.Sort_Description', 'Change the order of tags shown by this widget.')
);
2015-11-21 07:17:29 +01:00
$fields[] = DropdownField::create(
'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
)
->setDescription(
_t(
2017-09-14 00:27:40 +02:00
__CLASS__ . '.Direction_Description',
'Change the direction of ordering of tags shown by this widget.'
)
);
2015-11-21 07:17:29 +01:00
});
return parent::getCMSFields();
}
/**
* @return DataList
*/
public function getTags()
{
$blog = $this->Blog();
if (!$blog) {
2017-09-14 00:59:01 +02:00
return [];
2015-11-21 07:17:29 +01:00
}
$query = $blog->Tags();
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;
}
}