2015-05-19 02:35:35 +02:00
|
|
|
<?php
|
|
|
|
|
2016-12-15 04:41:49 +01:00
|
|
|
namespace SilverStripe\Blog\Widgets;
|
|
|
|
|
2017-01-18 05:12:06 +01:00
|
|
|
use SilverStripe\Blog\Model\Blog;
|
|
|
|
use SilverStripe\Core\Convert;
|
|
|
|
use SilverStripe\Forms\DropdownField;
|
|
|
|
use SilverStripe\ORM\ArrayList;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\DB;
|
|
|
|
use SilverStripe\Widgets\Model\Widget;
|
2023-01-24 03:41:55 +01:00
|
|
|
use SilverStripe\Control\Controller;
|
2017-01-18 05:12:06 +01:00
|
|
|
|
2018-04-06 00:22:58 +02:00
|
|
|
if (!class_exists(Widget::class)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-19 02:35:35 +02:00
|
|
|
/**
|
|
|
|
* @method Blog Blog()
|
|
|
|
*/
|
|
|
|
class BlogTagsCloudWidget extends Widget
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $title = 'Tags Cloud';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $cmsTitle = 'Blog Tags Cloud';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $description = 'Displays a tag cloud for this blog.';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-09-14 00:59:01 +02:00
|
|
|
private static $db = [];
|
2015-05-19 02:35:35 +02: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-05-19 02:35:35 +02:00
|
|
|
|
2017-12-15 05:24:20 +01:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $table_name = 'BlogTagsCloudWidget';
|
|
|
|
|
2015-05-19 02:35:35 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getCMSFields()
|
|
|
|
{
|
|
|
|
$this->beforeUpdateCMSFields(function ($fields) {
|
|
|
|
/*
|
|
|
|
* @var FieldList $fields
|
|
|
|
*/
|
|
|
|
$fields->push(
|
2017-01-18 05:12:06 +01:00
|
|
|
DropdownField::create(
|
|
|
|
'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-05-19 02:35:35 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return parent::getCMSFields();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getTags()
|
|
|
|
{
|
|
|
|
if ($blog = $this->Blog()) {
|
|
|
|
$escapedID = Convert::raw2sql($blog->ID);
|
|
|
|
$sql = 'SELECT DISTINCT "BlogTag"."URLSegment","BlogTag"."Title",Count("BlogTagID") AS "TagCount"
|
|
|
|
from "BlogPost_Tags"
|
|
|
|
INNER JOIN "BlogPost"
|
|
|
|
ON "BlogPost"."ID" = "BlogPost_Tags"."BlogPostID"
|
|
|
|
INNER JOIN "BlogTag"
|
|
|
|
ON "BlogTag"."ID" = "BlogPost_Tags"."BlogTagID"
|
|
|
|
WHERE "BlogID" = ' . $escapedID
|
|
|
|
. ' GROUP By "BlogTag"."URLSegment","BlogTag"."Title"
|
|
|
|
ORDER BY "Title"';
|
|
|
|
|
|
|
|
$records = DB::query($sql);
|
|
|
|
$bloglink = $blog->Link();
|
|
|
|
$maxTagCount = 0;
|
|
|
|
|
|
|
|
// create DataObjects that can be used to render the tag cloud
|
2017-09-14 01:49:27 +02:00
|
|
|
$tags = ArrayList::create();
|
2015-05-19 02:35:35 +02:00
|
|
|
foreach ($records as $record) {
|
2017-09-14 01:49:27 +02:00
|
|
|
$tag = DataObject::create();
|
2015-05-19 02:35:35 +02:00
|
|
|
$tag->TagName = $record['Title'];
|
2023-01-24 03:41:55 +01:00
|
|
|
$link = Controller::join_links($bloglink, 'tag', $record['URLSegment']);
|
2015-05-19 02:35:35 +02:00
|
|
|
$tag->Link = $link;
|
|
|
|
if ($record['TagCount'] > $maxTagCount) {
|
|
|
|
$maxTagCount = $record['TagCount'];
|
|
|
|
}
|
|
|
|
$tag->TagCount = $record['TagCount'];
|
|
|
|
$tags->push($tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
// normalize the tag counts from 1 to 10
|
2016-02-24 16:01:24 +01:00
|
|
|
if ($maxTagCount) {
|
|
|
|
$tagfactor = 10 / $maxTagCount;
|
|
|
|
foreach ($tags->getIterator() as $tag) {
|
|
|
|
$normalized = round($tagfactor * ($tag->TagCount));
|
|
|
|
$tag->NormalizedTag = $normalized;
|
|
|
|
}
|
2015-05-19 02:35:35 +02:00
|
|
|
}
|
|
|
|
|
2016-02-24 16:01:24 +01:00
|
|
|
|
2015-05-19 02:35:35 +02:00
|
|
|
return $tags;
|
|
|
|
}
|
|
|
|
|
2017-09-14 00:59:01 +02:00
|
|
|
return [];
|
2015-05-19 02:35:35 +02:00
|
|
|
}
|
|
|
|
}
|