Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / forum / src / ForumIndexStorage.php
1 <?php
2
3 namespace Drupal\forum;
4
5 use Drupal\comment\CommentInterface;
6 use Drupal\Core\Database\Connection;
7 use Drupal\node\NodeInterface;
8
9 /**
10  * Handles CRUD operations to {forum_index} table.
11  */
12 class ForumIndexStorage implements ForumIndexStorageInterface {
13
14   /**
15    * The active database connection.
16    *
17    * @var \Drupal\Core\Database\Connection
18    */
19   protected $database;
20
21   /**
22    * Constructs a ForumIndexStorage object.
23    *
24    * @param \Drupal\Core\Database\Connection $database
25    *   The current database connection.
26    */
27   public function __construct(Connection $database) {
28     $this->database = $database;
29   }
30
31   /**
32    * {@inheritdoc}
33    */
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();
36   }
37
38   /**
39    * {@inheritdoc}
40    */
41   public function create(NodeInterface $node) {
42     $this->database->insert('forum')
43       ->fields([
44         'tid' => $node->forum_tid,
45         'vid' => $node->getRevisionId(),
46         'nid' => $node->id(),
47       ])
48       ->execute();
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public function read(array $vids) {
55     return $this->database->select('forum', 'f')
56       ->fields('f', ['nid', 'tid'])
57       ->condition('f.vid', $vids, 'IN')
58       ->execute();
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function delete(NodeInterface $node) {
65     $this->database->delete('forum')
66       ->condition('nid', $node->id())
67       ->execute();
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function deleteRevision(NodeInterface $node) {
74     $this->database->delete('forum')
75       ->condition('nid', $node->id())
76       ->condition('vid', $node->getRevisionId())
77       ->execute();
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   public function update(NodeInterface $node) {
84     $this->database->update('forum')
85       ->fields(['tid' => $node->forum_tid])
86       ->condition('vid', $node->getRevisionId())
87       ->execute();
88   }
89
90   /**
91    * {@inheritdoc}
92    */
93   public function updateIndex(NodeInterface $node) {
94     $nid = $node->id();
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", [
96       ':nid' => $nid,
97       ':status' => CommentInterface::PUBLISHED,
98     ])->fetchField();
99
100     if ($count > 0) {
101       // Comments exist.
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, [
103         ':nid' => $nid,
104         ':status' => CommentInterface::PUBLISHED,
105       ])->fetchObject();
106       $this->database->update('forum_index')
107         ->fields([
108           'comment_count' => $count,
109           'last_comment_timestamp' => $last_reply->created,
110         ])
111         ->condition('nid', $nid)
112         ->execute();
113     }
114     else {
115       // Comments do not exist.
116       // @todo This should be actually filtering on the desired node language
117       $this->database->update('forum_index')
118         ->fields([
119           'comment_count' => 0,
120           'last_comment_timestamp' => $node->getCreatedTime(),
121         ])
122         ->condition('nid', $nid)
123         ->execute();
124     }
125   }
126
127   /**
128    * {@inheritdoc}
129    */
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) {
136         $query->values([
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(),
144         ]);
145       }
146     }
147     $query->execute();
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);
152     }
153   }
154
155   /**
156    * {@inheritdoc}
157    */
158   public function deleteIndex(NodeInterface $node) {
159     $this->database->delete('forum_index')
160       ->condition('nid', $node->id())
161       ->execute();
162   }
163
164 }