View source
<?php
namespace Drupal\comment;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\PagerSelectExtender;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CommentStorage extends SqlContentEntityStorage implements CommentStorageInterface {
protected $currentUser;
public function __construct(EntityTypeInterface $entity_info, Connection $database, EntityFieldManagerInterface $entity_field_manager, AccountInterface $current_user, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, MemoryCacheInterface $memory_cache, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($entity_info, $database, $entity_field_manager, $cache, $language_manager, $memory_cache, $entity_type_bundle_info, $entity_type_manager);
$this->currentUser = $current_user;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_info) {
return new static($entity_info, $container
->get('database'), $container
->get('entity_field.manager'), $container
->get('current_user'), $container
->get('cache.entity'), $container
->get('language_manager'), $container
->get('entity.memory_cache'), $container
->get('entity_type.bundle.info'), $container
->get('entity_type.manager'));
}
public function getMaxThread(CommentInterface $comment) {
$query = $this->database
->select($this
->getDataTable(), 'c')
->condition('entity_id', $comment
->getCommentedEntityId())
->condition('field_name', $comment
->getFieldName())
->condition('entity_type', $comment
->getCommentedEntityTypeId())
->condition('default_langcode', 1);
$query
->addExpression('MAX([thread])', 'thread');
return $query
->execute()
->fetchField();
}
public function getMaxThreadPerThread(CommentInterface $comment) {
$query = $this->database
->select($this
->getDataTable(), 'c')
->condition('entity_id', $comment
->getCommentedEntityId())
->condition('field_name', $comment
->getFieldName())
->condition('entity_type', $comment
->getCommentedEntityTypeId())
->condition('thread', $comment
->getParentComment()
->getThread() . '.%', 'LIKE')
->condition('default_langcode', 1);
$query
->addExpression('MAX([thread])', 'thread');
return $query
->execute()
->fetchField();
}
public function getDisplayOrdinal(CommentInterface $comment, $comment_mode, $divisor = 1) {
$data_table = $this
->getDataTable();
$query = $this->database
->select($data_table, 'c1');
$query
->innerJoin($data_table, 'c2', '[c2].[entity_id] = [c1].[entity_id] AND [c2].[entity_type] = [c1].[entity_type] AND [c2].[field_name] = [c1].[field_name]');
$query
->addExpression('COUNT(*)', 'count');
$query
->condition('c2.cid', $comment
->id());
if (!$this->currentUser
->hasPermission('administer comments')) {
$query
->condition('c1.status', CommentInterface::PUBLISHED);
}
if ($comment_mode == CommentManagerInterface::COMMENT_MODE_FLAT) {
$query
->condition('c1.cid', $comment
->id(), '<');
}
else {
$query
->where('SUBSTRING([c1].[thread], 1, (LENGTH([c1].[thread]) - 1)) < SUBSTRING([c2].[thread], 1, (LENGTH([c2].[thread]) - 1))');
}
$query
->condition('c1.default_langcode', 1);
$query
->condition('c2.default_langcode', 1);
$ordinal = $query
->execute()
->fetchField();
return $divisor > 1 ? floor($ordinal / $divisor) : $ordinal;
}
public function getNewCommentPageNumber($total_comments, $new_comments, FieldableEntityInterface $entity, $field_name) {
$field = $entity
->getFieldDefinition($field_name);
$comments_per_page = $field
->getSetting('per_page');
$data_table = $this
->getDataTable();
if ($total_comments <= $comments_per_page) {
$count = 0;
}
elseif ($field
->getSetting('default_mode') == CommentManagerInterface::COMMENT_MODE_FLAT) {
$count = $total_comments - $new_comments;
}
else {
$unread_threads_query = $this->database
->select($data_table, 'comment')
->fields('comment', [
'thread',
])
->condition('entity_id', $entity
->id())
->condition('entity_type', $entity
->getEntityTypeId())
->condition('field_name', $field_name)
->condition('status', CommentInterface::PUBLISHED)
->condition('default_langcode', 1)
->orderBy('created', 'DESC')
->orderBy('cid', 'DESC')
->range(0, $new_comments);
$first_thread_query = $this->database
->select($unread_threads_query, 'thread');
$first_thread_query
->addExpression('SUBSTRING([thread], 1, (LENGTH([thread]) - 1))', 'torder');
$first_thread = $first_thread_query
->fields('thread', [
'thread',
])
->orderBy('torder')
->range(0, 1)
->execute()
->fetchField();
$first_thread = substr($first_thread, 0, -1);
$count = $this->database
->query('SELECT COUNT(*) FROM {' . $data_table . '} WHERE [entity_id] = :entity_id
AND [entity_type] = :entity_type
AND [field_name] = :field_name
AND [status] = :status
AND SUBSTRING([thread], 1, (LENGTH([thread]) - 1)) < :thread
AND [default_langcode] = 1', [
':status' => CommentInterface::PUBLISHED,
':entity_id' => $entity
->id(),
':field_name' => $field_name,
':entity_type' => $entity
->getEntityTypeId(),
':thread' => $first_thread,
])
->fetchField();
}
return $comments_per_page > 0 ? (int) ($count / $comments_per_page) : 0;
}
public function getChildCids(array $comments) {
return $this->database
->select($this
->getDataTable(), 'c')
->fields('c', [
'cid',
])
->condition('pid', array_keys($comments), 'IN')
->condition('default_langcode', 1)
->execute()
->fetchCol();
}
public function loadThread(EntityInterface $entity, $field_name, $mode, $comments_per_page = 0, $pager_id = 0) {
$data_table = $this
->getDataTable();
$query = $this->database
->select($data_table, 'c');
$query
->addField('c', 'cid');
$query
->condition('c.entity_id', $entity
->id())
->condition('c.entity_type', $entity
->getEntityTypeId())
->condition('c.field_name', $field_name)
->condition('c.default_langcode', 1)
->addTag('entity_access')
->addTag('comment_filter')
->addMetaData('base_table', 'comment')
->addMetaData('entity', $entity)
->addMetaData('field_name', $field_name);
if ($comments_per_page) {
$query = $query
->extend(PagerSelectExtender::class)
->limit($comments_per_page);
if ($pager_id) {
$query
->element($pager_id);
}
$count_query = $this->database
->select($data_table, 'c');
$count_query
->addExpression('COUNT(*)');
$count_query
->condition('c.entity_id', $entity
->id())
->condition('c.entity_type', $entity
->getEntityTypeId())
->condition('c.field_name', $field_name)
->condition('c.default_langcode', 1)
->addTag('entity_access')
->addTag('comment_filter')
->addMetaData('base_table', 'comment')
->addMetaData('entity', $entity)
->addMetaData('field_name', $field_name);
$query
->setCountQuery($count_query);
}
if (!$this->currentUser
->hasPermission('administer comments')) {
$query
->condition('c.status', CommentInterface::PUBLISHED);
if ($comments_per_page) {
$count_query
->condition('c.status', CommentInterface::PUBLISHED);
}
}
if ($mode == CommentManagerInterface::COMMENT_MODE_FLAT) {
$query
->orderBy('c.cid', 'ASC');
}
else {
$query
->addExpression('SUBSTRING([c].[thread], 1, (LENGTH([c].[thread]) - 1))', 'torder');
$query
->orderBy('torder', 'ASC');
}
$cids = $query
->execute()
->fetchCol();
$comments = [];
if ($cids) {
$comments = $this
->loadMultiple($cids);
}
return $comments;
}
public function getUnapprovedCount() {
return $this->database
->select($this
->getDataTable(), 'c')
->condition('status', CommentInterface::NOT_PUBLISHED, '=')
->condition('default_langcode', 1)
->countQuery()
->execute()
->fetchField();
}
}