View source
<?php
namespace Drupal\tagclouds;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Cache\CacheBackendInterface;
class TagService implements TagServiceInterface {
protected $configFactory;
protected $languageManager;
protected $cacheStore;
public function __construct(ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager, CacheBackendInterface $cache_store) {
$this->configFactory = $config_factory;
$this->languageManager = $language_manager;
$this->cacheStore = $cache_store;
}
public function sortTags(array $tags, $sort_order = NULL) {
if ($sort_order == NULL) {
$config = $this->configFactory
->get('tagclouds.settings');
$sort_order = $config
->get('sort_order');
}
list($sort, $order) = explode(',', $sort_order);
switch ($sort) {
case 'title':
usort($tags, "static::sortByTitle");
break;
case 'count':
usort($tags, "static::sortByCount");
break;
case 'random':
shuffle($tags);
break;
}
if ($order == 'desc') {
$tags = array_reverse($tags);
}
return $tags;
}
public function getTags(array $vids, $steps = 6, $size = 60, $display = NULL) {
$language = $this->languageManager
->getCurrentLanguage();
$options = implode('_', $vids) . '_' . $language
->getId() . '_' . $steps . '_' . $size . "_" . $display;
$cache_name = 'tagclouds_cache_' . $options;
$cache = $this->cacheStore
->get($cache_name);
$tags = [];
if (!empty($cache->data)) {
$tags = $cache->data;
}
else {
if (count($vids) == 0) {
return [];
}
$config = $this->configFactory
->get('tagclouds.settings');
$query = \Drupal::database()
->select('taxonomy_term_data', 'td');
$query
->addExpression('COUNT(td.tid)', 'count');
$query
->fields('tfd', [
'name',
'description__value',
]);
$query
->fields('td', [
'tid',
'vid',
]);
$query
->addExpression('MIN(tn.nid)', 'nid');
$query
->join('taxonomy_index', 'tn', 'td.tid = tn.tid');
$query
->join('node_field_data', 'n', 'tn.nid = n.nid');
$query
->join('taxonomy_term_field_data', 'tfd', 'tfd.tid = tn.tid');
if ($config
->get('language_separation')) {
$query
->condition('n.langcode', $language
->getId());
}
$query
->condition('td.vid', $vids);
$query
->condition('n.status', 1);
$query
->groupBy('td.tid')
->groupBy('td.vid')
->groupBy('tfd.name');
$query
->groupBy('tfd.description__value');
$query
->having('COUNT(td.tid)>0');
$query
->orderBy('count', 'DESC');
if ($size > 0) {
$query
->range(0, $size);
}
$result = $query
->execute()
->fetchAll();
foreach ($result as $tag) {
$tags[$tag->tid] = $tag;
}
if ($display == NULL) {
$display = $config
->get('display_type');
}
$tags = $this
->buildWeightedTags($tags, $steps);
$this->cacheStore
->set($cache_name, $tags, CacheBackendInterface::CACHE_PERMANENT, [
'node_list',
'taxonomy_term_list',
'config:tagclouds.settings',
]);
}
return $tags;
}
private function buildWeightedTags($tags, $steps = 6) {
$tags_tmp = [];
$min = 1000000000.0;
$max = -1000000000.0;
foreach ($tags as $id => $tag) {
$tag->number_of_posts = $tag->count;
$tag->weightcount = log($tag->count);
$min = min($min, $tag->weightcount);
$max = max($max, $tag->weightcount);
$tags_tmp[$id] = $tag;
}
$range = max(0.01, $max - $min) * 1.0001;
foreach ($tags_tmp as $key => $value) {
$tags[$key]->weight = 1 + floor($steps * ($value->weightcount - $min) / $range);
}
return $tags;
}
private static function sortByTitle($a, $b) {
return strnatcasecmp($a->name, $b->name);
}
private static function sortByCount($a, $b) {
return $a->count > $b->count;
}
}