Chris@0: currentUser = $current_user; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_info) { Chris@0: return new static( Chris@0: $entity_info, Chris@0: $container->get('database'), Chris@18: $container->get('entity_field.manager'), Chris@0: $container->get('current_user'), Chris@0: $container->get('cache.entity'), Chris@17: $container->get('language_manager'), Chris@18: $container->get('entity.memory_cache'), Chris@18: $container->get('entity_type.bundle.info'), Chris@18: $container->get('entity_type.manager'), Chris@18: $container->get('entity.last_installed_schema.repository') Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getMaxThread(CommentInterface $comment) { Chris@17: $query = $this->database->select($this->getDataTable(), 'c') Chris@0: ->condition('entity_id', $comment->getCommentedEntityId()) Chris@0: ->condition('field_name', $comment->getFieldName()) Chris@0: ->condition('entity_type', $comment->getCommentedEntityTypeId()) Chris@0: ->condition('default_langcode', 1); Chris@0: $query->addExpression('MAX(thread)', 'thread'); Chris@0: return $query->execute() Chris@0: ->fetchField(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getMaxThreadPerThread(CommentInterface $comment) { Chris@17: $query = $this->database->select($this->getDataTable(), 'c') Chris@0: ->condition('entity_id', $comment->getCommentedEntityId()) Chris@0: ->condition('field_name', $comment->getFieldName()) Chris@0: ->condition('entity_type', $comment->getCommentedEntityTypeId()) Chris@0: ->condition('thread', $comment->getParentComment()->getThread() . '.%', 'LIKE') Chris@0: ->condition('default_langcode', 1); Chris@0: $query->addExpression('MAX(thread)', 'thread'); Chris@0: return $query->execute() Chris@0: ->fetchField(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getDisplayOrdinal(CommentInterface $comment, $comment_mode, $divisor = 1) { Chris@0: // Count how many comments (c1) are before $comment (c2) in display order. Chris@0: // This is the 0-based display ordinal. Chris@17: $data_table = $this->getDataTable(); Chris@17: $query = $this->database->select($data_table, 'c1'); Chris@17: $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'); Chris@0: $query->addExpression('COUNT(*)', 'count'); Chris@0: $query->condition('c2.cid', $comment->id()); Chris@0: if (!$this->currentUser->hasPermission('administer comments')) { Chris@0: $query->condition('c1.status', CommentInterface::PUBLISHED); Chris@0: } Chris@0: Chris@0: if ($comment_mode == CommentManagerInterface::COMMENT_MODE_FLAT) { Chris@0: // For rendering flat comments, cid is used for ordering comments due to Chris@0: // unpredictable behavior with timestamp, so we make the same assumption Chris@0: // here. Chris@0: $query->condition('c1.cid', $comment->id(), '<'); Chris@0: } Chris@0: else { Chris@0: // For threaded comments, the c.thread column is used for ordering. We can Chris@0: // use the sorting code for comparison, but must remove the trailing Chris@0: // slash. Chris@0: $query->where('SUBSTRING(c1.thread, 1, (LENGTH(c1.thread) - 1)) < SUBSTRING(c2.thread, 1, (LENGTH(c2.thread) - 1))'); Chris@0: } Chris@0: Chris@0: $query->condition('c1.default_langcode', 1); Chris@0: $query->condition('c2.default_langcode', 1); Chris@0: Chris@0: $ordinal = $query->execute()->fetchField(); Chris@0: Chris@0: return ($divisor > 1) ? floor($ordinal / $divisor) : $ordinal; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getNewCommentPageNumber($total_comments, $new_comments, FieldableEntityInterface $entity, $field_name) { Chris@0: $field = $entity->getFieldDefinition($field_name); Chris@0: $comments_per_page = $field->getSetting('per_page'); Chris@17: $data_table = $this->getDataTable(); Chris@0: Chris@0: if ($total_comments <= $comments_per_page) { Chris@0: // Only one page of comments. Chris@0: $count = 0; Chris@0: } Chris@0: elseif ($field->getSetting('default_mode') == CommentManagerInterface::COMMENT_MODE_FLAT) { Chris@0: // Flat comments. Chris@0: $count = $total_comments - $new_comments; Chris@0: } Chris@0: else { Chris@0: // Threaded comments. Chris@0: Chris@0: // 1. Find all the threads with a new comment. Chris@17: $unread_threads_query = $this->database->select($data_table, 'comment') Chris@0: ->fields('comment', ['thread']) Chris@0: ->condition('entity_id', $entity->id()) Chris@0: ->condition('entity_type', $entity->getEntityTypeId()) Chris@0: ->condition('field_name', $field_name) Chris@0: ->condition('status', CommentInterface::PUBLISHED) Chris@0: ->condition('default_langcode', 1) Chris@0: ->orderBy('created', 'DESC') Chris@0: ->orderBy('cid', 'DESC') Chris@0: ->range(0, $new_comments); Chris@0: Chris@0: // 2. Find the first thread. Chris@0: $first_thread_query = $this->database->select($unread_threads_query, 'thread'); Chris@0: $first_thread_query->addExpression('SUBSTRING(thread, 1, (LENGTH(thread) - 1))', 'torder'); Chris@0: $first_thread = $first_thread_query Chris@0: ->fields('thread', ['thread']) Chris@0: ->orderBy('torder') Chris@0: ->range(0, 1) Chris@0: ->execute() Chris@0: ->fetchField(); Chris@0: Chris@0: // Remove the final '/'. Chris@0: $first_thread = substr($first_thread, 0, -1); Chris@0: Chris@0: // Find the number of the first comment of the first unread thread. Chris@17: $count = $this->database->query('SELECT COUNT(*) FROM {' . $data_table . '} WHERE entity_id = :entity_id Chris@0: AND entity_type = :entity_type Chris@0: AND field_name = :field_name Chris@0: AND status = :status Chris@0: AND SUBSTRING(thread, 1, (LENGTH(thread) - 1)) < :thread Chris@0: AND default_langcode = 1', [ Chris@0: ':status' => CommentInterface::PUBLISHED, Chris@0: ':entity_id' => $entity->id(), Chris@0: ':field_name' => $field_name, Chris@0: ':entity_type' => $entity->getEntityTypeId(), Chris@0: ':thread' => $first_thread, Chris@0: ])->fetchField(); Chris@0: } Chris@0: Chris@0: return $comments_per_page > 0 ? (int) ($count / $comments_per_page) : 0; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getChildCids(array $comments) { Chris@17: return $this->database->select($this->getDataTable(), 'c') Chris@0: ->fields('c', ['cid']) Chris@0: ->condition('pid', array_keys($comments), 'IN') Chris@0: ->condition('default_langcode', 1) Chris@0: ->execute() Chris@0: ->fetchCol(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: * Chris@0: * To display threaded comments in the correct order we keep a 'thread' field Chris@0: * and order by that value. This field keeps this data in Chris@0: * a way which is easy to update and convenient to use. Chris@0: * Chris@0: * A "thread" value starts at "1". If we add a child (A) to this comment, Chris@0: * we assign it a "thread" = "1.1". A child of (A) will have "1.1.1". Next Chris@0: * brother of (A) will get "1.2". Next brother of the parent of (A) will get Chris@0: * "2" and so on. Chris@0: * Chris@0: * First of all note that the thread field stores the depth of the comment: Chris@0: * depth 0 will be "X", depth 1 "X.X", depth 2 "X.X.X", etc. Chris@0: * Chris@0: * Now to get the ordering right, consider this example: Chris@0: * Chris@0: * 1 Chris@0: * 1.1 Chris@0: * 1.1.1 Chris@0: * 1.2 Chris@0: * 2 Chris@0: * Chris@0: * If we "ORDER BY thread ASC" we get the above result, and this is the Chris@0: * natural order sorted by time. However, if we "ORDER BY thread DESC" Chris@0: * we get: Chris@0: * Chris@0: * 2 Chris@0: * 1.2 Chris@0: * 1.1.1 Chris@0: * 1.1 Chris@0: * 1 Chris@0: * Chris@0: * Clearly, this is not a natural way to see a thread, and users will get Chris@0: * confused. The natural order to show a thread by time desc would be: Chris@0: * Chris@0: * 2 Chris@0: * 1 Chris@0: * 1.2 Chris@0: * 1.1 Chris@0: * 1.1.1 Chris@0: * Chris@0: * which is what we already did before the standard pager patch. To achieve Chris@0: * this we simply add a "/" at the end of each "thread" value. This way, the Chris@0: * thread fields will look like this: Chris@0: * Chris@0: * 1/ Chris@0: * 1.1/ Chris@0: * 1.1.1/ Chris@0: * 1.2/ Chris@0: * 2/ Chris@0: * Chris@0: * we add "/" since this char is, in ASCII, higher than every number, so if Chris@0: * now we "ORDER BY thread DESC" we get the correct order. However this would Chris@0: * spoil the reverse ordering, "ORDER BY thread ASC" -- here, we do not need Chris@0: * to consider the trailing "/" so we use a substring only. Chris@0: */ Chris@0: public function loadThread(EntityInterface $entity, $field_name, $mode, $comments_per_page = 0, $pager_id = 0) { Chris@17: $data_table = $this->getDataTable(); Chris@17: $query = $this->database->select($data_table, 'c'); Chris@0: $query->addField('c', 'cid'); Chris@0: $query Chris@0: ->condition('c.entity_id', $entity->id()) Chris@0: ->condition('c.entity_type', $entity->getEntityTypeId()) Chris@0: ->condition('c.field_name', $field_name) Chris@0: ->condition('c.default_langcode', 1) Chris@0: ->addTag('entity_access') Chris@0: ->addTag('comment_filter') Chris@0: ->addMetaData('base_table', 'comment') Chris@0: ->addMetaData('entity', $entity) Chris@0: ->addMetaData('field_name', $field_name); Chris@0: Chris@0: if ($comments_per_page) { Chris@0: $query = $query->extend('Drupal\Core\Database\Query\PagerSelectExtender') Chris@0: ->limit($comments_per_page); Chris@0: if ($pager_id) { Chris@0: $query->element($pager_id); Chris@0: } Chris@0: Chris@17: $count_query = $this->database->select($data_table, 'c'); Chris@0: $count_query->addExpression('COUNT(*)'); Chris@0: $count_query Chris@0: ->condition('c.entity_id', $entity->id()) Chris@0: ->condition('c.entity_type', $entity->getEntityTypeId()) Chris@0: ->condition('c.field_name', $field_name) Chris@0: ->condition('c.default_langcode', 1) Chris@0: ->addTag('entity_access') Chris@0: ->addTag('comment_filter') Chris@0: ->addMetaData('base_table', 'comment') Chris@0: ->addMetaData('entity', $entity) Chris@0: ->addMetaData('field_name', $field_name); Chris@0: $query->setCountQuery($count_query); Chris@0: } Chris@0: Chris@0: if (!$this->currentUser->hasPermission('administer comments')) { Chris@0: $query->condition('c.status', CommentInterface::PUBLISHED); Chris@0: if ($comments_per_page) { Chris@0: $count_query->condition('c.status', CommentInterface::PUBLISHED); Chris@0: } Chris@0: } Chris@0: if ($mode == CommentManagerInterface::COMMENT_MODE_FLAT) { Chris@0: $query->orderBy('c.cid', 'ASC'); Chris@0: } Chris@0: else { Chris@0: // See comment above. Analysis reveals that this doesn't cost too Chris@0: // much. It scales much much better than having the whole comment Chris@0: // structure. Chris@0: $query->addExpression('SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))', 'torder'); Chris@0: $query->orderBy('torder', 'ASC'); Chris@0: } Chris@0: Chris@0: $cids = $query->execute()->fetchCol(); Chris@0: Chris@0: $comments = []; Chris@0: if ($cids) { Chris@0: $comments = $this->loadMultiple($cids); Chris@0: } Chris@0: Chris@0: return $comments; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getUnapprovedCount() { Chris@17: return $this->database->select($this->getDataTable(), 'c') Chris@0: ->condition('status', CommentInterface::NOT_PUBLISHED, '=') Chris@0: ->condition('default_langcode', 1) Chris@0: ->countQuery() Chris@0: ->execute() Chris@0: ->fetchField(); Chris@0: } Chris@0: Chris@0: }