View source
<?php
namespace Drupal\content_kanban;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\content_calendar\ContentTypeConfigService;
use Drupal\content_kanban\Form\SettingsForm;
use Drupal\content_moderation\Entity\ContentModerationState;
use Drupal\content_moderation\ModerationInformationInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Database\Driver\mysql\Connection;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\content_kanban\Form\KanbanFilterForm;
class KanbanService {
protected $configFactory;
protected $database;
protected $moderationInformation;
protected $definedColors = [
'#0074bd',
'#D66611',
'#27E834',
'#FF3D2A',
'purple',
'#22FFA0',
'black',
'#37C2FF',
];
protected $entityTypeManager;
protected $moduleHandler;
protected $contentTypConfigService;
public function __construct(ConfigFactoryInterface $config_factory, Connection $database, ModerationInformationInterface $moderation_information, EntityTypeManager $entityTypeManager, ModuleHandlerInterface $moduleHandler, ContentTypeConfigService $contentTypConfigService) {
$this->configFactory = $config_factory;
$this->database = $database;
$this->moderationInformation = $moderation_information;
$this->entityTypeManager = $entityTypeManager;
$this->moduleHandler = $moduleHandler;
$this->contentTypConfigService = $contentTypConfigService;
}
public function contentCalendarModuleIsEnabled() {
return $this->moduleHandler
->moduleExists('content_planner');
}
public function getKanbanSettings() {
return $this->configFactory
->get(SettingsForm::CONFIG_NAME);
}
public function useContentCalendarColors() {
if ($this
->contentCalendarModuleIsEnabled()) {
$settings = $this
->getKanbanSettings();
if ($value = $settings
->get('use_content_calendar_colors')) {
return (bool) $value;
}
}
return FALSE;
}
public function getEntityTypeConfigs(array $entityTypes = []) {
$entityTypeConfigs = [];
$color_index = 0;
foreach ($entityTypes as $entityTypeId => $entityBundles) {
foreach ($entityBundles as $bundle) {
$entityTypeConfigs[$bundle] = new EntityTypeConfig($entityTypeId, $bundle, ucfirst($bundle), $this
->getColor($color_index));
$color_index++;
}
}
if ($this
->useContentCalendarColors()) {
$content_type_configs = $this->contentTypConfigService
->loadAllEntities();
foreach ($content_type_configs as $content_type => $content_type_config) {
if (array_key_exists($content_type, $entityTypeConfigs)) {
$entityTypeConfigs[$content_type]
->setColor($content_type_config
->getColor());
}
}
}
return $entityTypeConfigs;
}
protected function getColor($index) {
if ($index + 1 > count($this->definedColors)) {
$index = 0;
}
return $this->definedColors[$index];
}
public function getEntityContentModerationEntities($workflow, array $filters = [], array $entities = []) {
$result = [];
try {
$query = $this->entityTypeManager
->getStorage('content_moderation_state')
->getQuery();
if (!empty(array_keys($entities))) {
$query
->condition('workflow', $workflow);
$query
->condition('content_entity_type_id', array_keys($entities), 'in');
}
if (array_key_exists('moderation_state', $filters) && $filters['moderation_state']) {
$query
->condition('moderation_state', $filters['moderation_state']);
}
if (array_key_exists('uid', $filters) && $filters['uid']) {
$query
->condition('uid', $filters['uid']);
}
$result = $query
->execute();
} catch (InvalidPluginDefinitionException $e) {
watchdog_exception('content_kanban', $e);
} catch (PluginNotFoundException $e) {
watchdog_exception('content_kanban', $e);
}
if ($result) {
return ContentModerationState::loadMultiple($result);
}
return $result;
}
public function getEntityIdsFromContentModerationEntities($workflow, array $filters = [], array $entities = []) {
$entityIds = [];
if ($content_moderation_states = $this
->getEntityContentModerationEntities($workflow, $filters, $entities)) {
foreach ($content_moderation_states as $content_moderation_state) {
$content_entity_id_property = $content_moderation_state->content_entity_id;
$content_entity_id_value = $content_entity_id_property
->getValue();
$entity_type_id_value = $content_moderation_state
->get('content_entity_type_id')
->getValue();
$entity_type_id = $entity_type_id_value[0]['value'];
$entityIds[$entity_type_id][] = $content_entity_id_value[0]['value'];
}
}
return $entityIds;
}
public function getEntitiesByEntityIds(array $entityIds = [], $filters = []) {
$result = [];
if (!empty($entityIds)) {
$query = [];
foreach ($entityIds as $entityTypeName => $entityId) {
try {
$entityStorage = $this->entityTypeManager
->getStorage($entityTypeName);
$entityKeys = $entityStorage
->getEntityType()
->getKeys();
$query[$entityTypeName] = $this->database
->select($entityTypeName . '_field_data', 'nfd');
$query[$entityTypeName]
->addField('nfd', $entityKeys['id']);
$query[$entityTypeName]
->addField('nfd', $entityKeys['label']);
$query[$entityTypeName]
->addField('nfd', 'created');
$query[$entityTypeName]
->addField('nfd', 'status');
$query[$entityTypeName]
->addField('nfd', 'type');
if ($filters['content_type']) {
$query[$entityTypeName]
->condition('nfd.type', $filters['content_type']);
}
$query[$entityTypeName]
->addField('ufd', 'name', 'username');
$query[$entityTypeName]
->addField('nfd', $entityKeys['uid']);
$query[$entityTypeName]
->condition('nfd.' . $entityKeys['id'], $entityIds[$entityTypeName], 'in');
$query[$entityTypeName]
->innerJoin('users_field_data', 'ufd', 'nfd.' . $entityKeys['uid'] . ' = ufd.uid');
if (KanbanFilterForm::getDateRangeFilter()) {
$searchFromTime = time() - 86400 * KanbanFilterForm::getDateRangeFilter();
$query[$entityTypeName]
->condition('nfd.created', $searchFromTime, '>=');
}
if ($this->database
->schema()
->fieldExists($entityTypeName . '_field_data', 'publish_on') && $this
->contentCalendarModuleIsEnabled()) {
$query[$entityTypeName]
->orderBy('nfd.publish_on', 'ASC');
}
else {
$query[$entityTypeName]
->orderBy('nfd.created', 'ASC');
}
$result[$entityTypeName] = $query[$entityTypeName]
->execute()
->fetchAll();
} catch (InvalidPluginDefinitionException $e) {
watchdog_exception('content_kanban', $e);
} catch (PluginNotFoundException $e) {
watchdog_exception('content_kanban', $e);
}
}
}
if ($result) {
return $result;
}
return [];
}
}