3 namespace Drupal\forum;
5 use Drupal\comment\CommentInterface;
6 use Drupal\Core\Database\Connection;
7 use Drupal\node\NodeInterface;
10 * Handles CRUD operations to {forum_index} table.
12 class ForumIndexStorage implements ForumIndexStorageInterface {
15 * The active database connection.
17 * @var \Drupal\Core\Database\Connection
22 * Constructs a ForumIndexStorage object.
24 * @param \Drupal\Core\Database\Connection $database
25 * The current database connection.
27 public function __construct(Connection $database) {
28 $this->database = $database;
34 public function getOriginalTermId(NodeInterface $node) {
35 return $this->database->queryRange("SELECT f.tid FROM {forum} f INNER JOIN {node} n ON f.vid = n.vid WHERE n.nid = :nid ORDER BY f.vid DESC", 0, 1, [':nid' => $node->id()])->fetchField();
41 public function create(NodeInterface $node) {
42 $this->database->insert('forum')
44 'tid' => $node->forum_tid,
45 'vid' => $node->getRevisionId(),
54 public function read(array $vids) {
55 return $this->database->select('forum', 'f')
56 ->fields('f', ['nid', 'tid'])
57 ->condition('f.vid', $vids, 'IN')
64 public function delete(NodeInterface $node) {
65 $this->database->delete('forum')
66 ->condition('nid', $node->id())
73 public function deleteRevision(NodeInterface $node) {
74 $this->database->delete('forum')
75 ->condition('nid', $node->id())
76 ->condition('vid', $node->getRevisionId())
83 public function update(NodeInterface $node) {
84 $this->database->update('forum')
85 ->fields(['tid' => $node->forum_tid])
86 ->condition('vid', $node->getRevisionId())
93 public function updateIndex(NodeInterface $node) {
95 $count = $this->database->query("SELECT COUNT(cid) FROM {comment_field_data} c INNER JOIN {forum_index} i ON c.entity_id = i.nid WHERE c.entity_id = :nid AND c.field_name = 'comment_forum' AND c.entity_type = 'node' AND c.status = :status AND c.default_langcode = 1", [
97 ':status' => CommentInterface::PUBLISHED,
102 $last_reply = $this->database->queryRange("SELECT cid, name, created, uid FROM {comment_field_data} WHERE entity_id = :nid AND field_name = 'comment_forum' AND entity_type = 'node' AND status = :status AND default_langcode = 1 ORDER BY cid DESC", 0, 1, [
104 ':status' => CommentInterface::PUBLISHED,
106 $this->database->update('forum_index')
108 'comment_count' => $count,
109 'last_comment_timestamp' => $last_reply->created,
111 ->condition('nid', $nid)
115 // Comments do not exist.
116 // @todo This should be actually filtering on the desired node language
117 $this->database->update('forum_index')
119 'comment_count' => 0,
120 'last_comment_timestamp' => $node->getCreatedTime(),
122 ->condition('nid', $nid)
130 public function createIndex(NodeInterface $node) {
131 $query = $this->database->insert('forum_index')
132 ->fields(['nid', 'title', 'tid', 'sticky', 'created', 'comment_count', 'last_comment_timestamp']);
133 foreach ($node->getTranslationLanguages() as $langcode => $language) {
134 $translation = $node->getTranslation($langcode);
135 foreach ($translation->taxonomy_forums as $item) {
137 'nid' => $node->id(),
138 'title' => $translation->label(),
139 'tid' => $item->target_id,
140 'sticky' => (int) $node->isSticky(),
141 'created' => $node->getCreatedTime(),
142 'comment_count' => 0,
143 'last_comment_timestamp' => $node->getCreatedTime(),
148 // The logic for determining last_comment_count is fairly complex, so
149 // update the index too.
150 if ($node->isNew()) {
151 $this->updateIndex($node);
158 public function deleteIndex(NodeInterface $node) {
159 $this->database->delete('forum_index')
160 ->condition('nid', $node->id())