View source
<?php
namespace Drupal\onlyone;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class OnlyOne implements OnlyOneInterface {
use StringTranslationTrait;
protected $entityTypeManager;
protected $connection;
protected $languageManager;
protected $configFactory;
protected $formatter;
public function __construct(EntityTypeManagerInterface $entity_type_manager, Connection $connection, LanguageManagerInterface $language_manager, ConfigFactoryInterface $config_factory, TranslationInterface $string_translation) {
$this->entityTypeManager = $entity_type_manager;
$this->connection = $connection;
$this->languageManager = $language_manager;
$this->configFactory = $config_factory;
$this->stringTranslation = $string_translation;
$this->formatter = new OnlyOnePrintAdminPage();
}
private function getTemporaryContentTypesTableName() {
$query_content_types = "SELECT DISTINCT SUBSTR(name, 11) AS type\n FROM {config}\n WHERE name LIKE 'node.type.%'";
return $this->connection
->queryTemporary($query_content_types);
}
public function getContentTypesList() {
$content_types = $this->entityTypeManager
->getStorage('node_type')
->loadMultiple();
$content_types_list_label = [];
foreach ($content_types as $content_type) {
$content_types_list_label[$content_type
->id()] = $content_type
->label();
}
return $content_types_list_label;
}
public function existsNodesContentType($content_type, $language = NULL) {
$query = $this->entityTypeManager
->getStorage('node')
->getQuery();
$query
->condition('type', $content_type);
if ($this->languageManager
->isMultilingual()) {
if (empty($language)) {
$language = $this->languageManager
->getCurrentLanguage()
->getId();
}
$query
->condition('langcode', $language);
}
$nids = $query
->execute();
$nid = count($nids) ? array_pop($nids) : 0;
return $nid;
}
public function deleteContentTypeConfig($content_type) {
$config = $this->configFactory
->getEditable('onlyone.settings');
$onlyone_content_types = $config
->get('onlyone_node_types');
$index = array_search($content_type, $onlyone_content_types);
if ($index !== FALSE) {
unset($onlyone_content_types[$index]);
$config
->set('onlyone_node_types', $onlyone_content_types)
->save();
return TRUE;
}
return FALSE;
}
public function getLanguageLabel($language) {
switch ($language) {
case LanguageInterface::LANGCODE_NOT_SPECIFIED:
case '':
return $this->languageManager
->getLanguage(LanguageInterface::LANGCODE_NOT_SPECIFIED)
->getName();
case LanguageInterface::LANGCODE_NOT_APPLICABLE:
return $this->languageManager
->getLanguage(LanguageInterface::LANGCODE_NOT_APPLICABLE)
->getName();
default:
return ucfirst($language);
}
}
public function getAvailableContentTypes() {
$content_types_temporary_table_name = $this
->getTemporaryContentTypesTableName();
if ($this->languageManager
->isMultilingual()) {
$query = "SELECT type\n FROM {$content_types_temporary_table_name}\n WHERE type NOT IN\n (SELECT node.type\n FROM {node} node\n JOIN {node_field_data} node_field_data USING(nid)\n GROUP BY type,\n node_field_data.langcode\n HAVING COUNT(nid) > 1)\n ORDER BY type ASC";
}
else {
$query = "SELECT node_type.type\n FROM {$content_types_temporary_table_name} node_type\n LEFT JOIN {node} USING(type)\n GROUP BY type\n HAVING COUNT(nid) <= 1\n ORDER BY type ASC";
}
$result = $this->connection
->query($query);
$content_types = $result
->fetchCol();
return $content_types;
}
public function getNotAvailableContentTypes() {
if ($this->languageManager
->isMultilingual()) {
$query = 'SELECT DISTINCT node.type
FROM {node} node
JOIN {node_field_data} node_field_data USING(nid)
GROUP BY type,
node_field_data.langcode
HAVING COUNT(nid) > 1
ORDER BY node.type ASC';
}
else {
$query = "SELECT type\n FROM {node}\n GROUP BY type\n HAVING COUNT(nid) > 1\n ORDER BY type ASC";
}
$result = $this->connection
->query($query);
$content_types = $result
->fetchCol();
return $content_types;
}
public function getAvailableContentTypesSummarized() {
$content_types_temporary_table_name = $this
->getTemporaryContentTypesTableName();
if ($this->languageManager
->isMultilingual()) {
$query = "SELECT DISTINCT node_type.type,\n node_field_data.langcode AS language,\n COUNT(node.nid) AS total\n FROM {$content_types_temporary_table_name} node_type\n LEFT JOIN {node} node USING(type)\n LEFT JOIN {node_field_data} node_field_data USING(nid)\n WHERE node_type.type NOT IN\n (SELECT node.type\n FROM {node} node\n JOIN {node_field_data} node_field_data USING(nid)\n GROUP BY type,\n node_field_data.langcode\n HAVING COUNT(nid) > 1)\n GROUP BY type,\n node_field_data.langcode\n ORDER BY node_type.type ASC";
}
else {
$query = "SELECT type,\n COUNT(nid) AS total\n FROM {$content_types_temporary_table_name} node_type\n LEFT JOIN {node} USING(type)\n GROUP BY type\n HAVING COUNT(nid) <= 1\n ORDER BY type ASC";
}
$result = $this->connection
->query($query);
$content_types = $result
->fetchAll(\PDO::FETCH_GROUP);
$this
->addAditionalInfoToContentTypes($content_types);
uasort($content_types, function ($a, $b) {
return $a[0]->name <=> $b[0]->name;
});
return $content_types;
}
public function getNotAvailableContentTypesSummarized() {
if ($this->languageManager
->isMultilingual()) {
$query = "SELECT DISTINCT node.type,\n node_field_data.langcode AS language,\n COUNT(node.nid) AS total\n FROM {node} node\n JOIN {node_field_data} node_field_data USING(nid)\n WHERE node.type IN\n (SELECT DISTINCT node.type\n FROM {node} node\n JOIN {node_field_data} node_field_data USING(nid)\n GROUP BY type,\n node_field_data.langcode\n HAVING COUNT(nid) > 1)\n GROUP BY node.type,\n language\n ORDER BY node.type ASC";
}
else {
$query = "SELECT type,\n COUNT(nid) AS total\n FROM {node}\n GROUP BY type\n HAVING COUNT(nid) > 1\n ORDER BY type ASC";
}
$result = $this->connection
->query($query);
$content_types = $result
->fetchAll(\PDO::FETCH_GROUP);
$this
->addAditionalInfoToContentTypes($content_types);
return $content_types;
}
private function addAditionalInfoToContentTypes(array &$content_types) {
$content_types_list_label = $this
->getContentTypesList();
$configured_content_types = $this->configFactory
->get('onlyone.settings')
->get('onlyone_node_types');
foreach ($content_types as $conten_type => $content_type_info) {
$cant = count($content_type_info);
for ($i = 0; $i < $cant; $i++) {
$content_types[$conten_type][$i]->configured = in_array($conten_type, $configured_content_types) ? TRUE : FALSE;
$content_types[$conten_type][$i]->name = $content_types_list_label[$conten_type];
if ($this->languageManager
->isMultilingual()) {
$language = $this
->getLanguageLabel($content_type_info[$i]->language);
$total_nodes = $content_type_info[$i]->total ? $this
->formatPlural($content_type_info[$i]->total, '@language: @total Node', '@language: @total Nodes', [
'@language' => $language,
'@total' => $content_type_info[$i]->total,
]) : $this
->t('0 Nodes');
}
else {
$total_nodes = $content_type_info[$i]->total ? $this
->formatPlural($content_type_info[$i]->total, '@total Node', '@total Nodes', [
'@total' => $content_type_info[$i]->total,
]) : $this
->t('0 Nodes');
}
$content_types[$conten_type][$i]->total_nodes = $total_nodes;
}
}
}
public function setFormatter(OnlyOnePrintStrategyInterface $formatter) {
$this->formatter = $formatter;
return $this;
}
public function getAvailableContentTypesForPrint() {
return $this->formatter
->getContentTypesListForPrint($this
->getAvailableContentTypesSummarized());
}
public function getNotAvailableContentTypesForPrint() {
return $this->formatter
->getContentTypesListForPrint($this
->getNotAvailableContentTypesSummarized());
}
}