Skip to content

Commit 6a31f3b

Browse files
committed
refactor: moved queries for comment class into repository class
1 parent ebb5917 commit 6a31f3b

File tree

7 files changed

+425
-172
lines changed

7 files changed

+425
-172
lines changed

phpmyfaq/src/phpMyFAQ/Application.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
declare(strict_types=1);
4-
53
/**
64
* The main Application class
75
*
@@ -17,6 +15,8 @@
1715
* @since 2023-10-24
1816
*/
1917

18+
declare(strict_types=1);
19+
2020
namespace phpMyFAQ;
2121

2222
use phpMyFAQ\Core\Exception;
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
<?php
2+
3+
/**
4+
* Repository for comments-related database operations
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public License,
7+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
8+
* obtain one at https://mozilla.org/MPL/2.0/.
9+
*
10+
* @package phpMyFAQ
11+
* @author Thorsten Rinne <[email protected]>
12+
* @copyright 2025 phpMyFAQ Team
13+
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14+
* @link https://www.phpmyfaq.de
15+
* @since 2025-11-04
16+
*/
17+
18+
declare(strict_types=1);
19+
20+
namespace phpMyFAQ\Comment;
21+
22+
use phpMyFAQ\Configuration as CoreConfiguration;
23+
use phpMyFAQ\Database;
24+
use phpMyFAQ\Entity\Comment;
25+
use phpMyFAQ\Entity\CommentType;
26+
27+
readonly class CommentsRepository
28+
{
29+
public function __construct(
30+
private CoreConfiguration $configuration,
31+
) {
32+
}
33+
34+
/**
35+
* @return array<int, object>
36+
*/
37+
public function fetchByReferenceIdAndType(int $referenceId, string $type): array
38+
{
39+
$sql = <<<SQL
40+
SELECT
41+
id_comment, id, usr, email, comment, datum
42+
FROM
43+
%sfaqcomments
44+
WHERE
45+
type = '%s'
46+
AND
47+
id = %d
48+
SQL;
49+
50+
$query = sprintf($sql, Database::getTablePrefix(), $this->configuration->getDb()->escape($type), $referenceId);
51+
52+
$result = $this->configuration->getDb()->query($query);
53+
$rows = $this->configuration->getDb()->fetchAll($result);
54+
return is_array($rows) ? $rows : [];
55+
}
56+
57+
public function insert(Comment $comment): bool
58+
{
59+
$helpedValue = $comment->hasHelped();
60+
$helpedSql = $helpedValue === null ? 'NULL' : "'" . ($helpedValue ? 'y' : 'n') . "'";
61+
62+
$sql = <<<SQL
63+
INSERT INTO
64+
%sfaqcomments (id_comment, id, type, usr, email, comment, datum, helped)
65+
VALUES
66+
(%d, %d, '%s', '%s', '%s', '%s', '%s', %s)
67+
SQL;
68+
69+
$query = sprintf(
70+
$sql,
71+
Database::getTablePrefix(),
72+
$this->configuration->getDb()->nextId(Database::getTablePrefix() . 'faqcomments', 'id_comment'),
73+
$comment->getRecordId(),
74+
$this->configuration->getDb()->escape($comment->getType()),
75+
$this->configuration->getDb()->escape($comment->getUsername()),
76+
$this->configuration->getDb()->escape($comment->getEmail()),
77+
$this->configuration->getDb()->escape($comment->getComment()),
78+
$this->configuration->getDb()->escape($comment->getDate()),
79+
$helpedSql,
80+
);
81+
82+
return (bool) $this->configuration->getDb()->query($query);
83+
}
84+
85+
public function deleteByTypeAndId(string $type, int $commentId): bool
86+
{
87+
$sql = <<<SQL
88+
DELETE FROM
89+
%sfaqcomments
90+
WHERE
91+
type = '%s'
92+
AND
93+
id_comment = %d
94+
SQL;
95+
96+
$query = sprintf($sql, Database::getTablePrefix(), $this->configuration->getDb()->escape($type), $commentId);
97+
98+
return (bool) $this->configuration->getDb()->query($query);
99+
}
100+
101+
/**
102+
* @return array<int, object>
103+
*/
104+
public function countByTypeGroupedByRecordId(string $type = CommentType::FAQ): array
105+
{
106+
$sql = <<<SQL
107+
SELECT
108+
COUNT(id) AS anz,
109+
id
110+
FROM
111+
%sfaqcomments
112+
WHERE
113+
type = '%s'
114+
GROUP BY id
115+
ORDER BY id
116+
SQL;
117+
118+
$query = sprintf($sql, Database::getTablePrefix(), $this->configuration->getDb()->escape($type));
119+
120+
$result = $this->configuration->getDb()->query($query);
121+
$rows = $this->configuration->getDb()->fetchAll($result);
122+
return is_array($rows) ? $rows : [];
123+
}
124+
125+
/**
126+
* @return array<int, object>
127+
*/
128+
public function countByCategoryForFaq(): array
129+
{
130+
$sql = <<<SQL
131+
SELECT
132+
COUNT(fc.id) AS number,
133+
fcg.category_id AS category_id
134+
FROM
135+
%sfaqcomments fc
136+
LEFT JOIN
137+
%sfaqcategoryrelations fcg
138+
ON
139+
fc.id = fcg.record_id
140+
WHERE
141+
fc.type = '%s'
142+
GROUP BY fcg.category_id
143+
ORDER BY fcg.category_id
144+
SQL;
145+
146+
$query = sprintf($sql, Database::getTablePrefix(), Database::getTablePrefix(), CommentType::FAQ);
147+
148+
$result = $this->configuration->getDb()->query($query);
149+
$rows = $this->configuration->getDb()->fetchAll($result);
150+
return is_array($rows) ? $rows : [];
151+
}
152+
153+
/**
154+
* @return array<int, object>
155+
*/
156+
public function fetchAllWithCategories(string $type = CommentType::FAQ): array
157+
{
158+
$prefix = Database::getTablePrefix();
159+
$escapedType = $this->configuration->getDb()->escape($type);
160+
161+
if ($type === CommentType::FAQ) {
162+
$query = sprintf(
163+
'SELECT fc.id_comment AS comment_id, fc.id AS record_id, fcg.category_id, fc.usr AS username, '
164+
. 'fc.email AS email, fc.comment AS comment, fc.datum AS comment_date FROM %sfaqcomments fc '
165+
. "LEFT JOIN %sfaqcategoryrelations fcg ON fc.id = fcg.record_id WHERE type = '%s'",
166+
$prefix,
167+
$prefix,
168+
$escapedType,
169+
);
170+
} else {
171+
$query = sprintf(
172+
'SELECT fc.id_comment AS comment_id, fc.id AS record_id, fc.usr AS username, fc.email AS email, '
173+
. "fc.comment AS comment, fc.datum AS comment_date FROM %sfaqcomments fc WHERE type = '%s'",
174+
$prefix,
175+
$escapedType,
176+
);
177+
}
178+
179+
$result = $this->configuration->getDb()->query($query);
180+
$rows = $this->configuration->getDb()->fetchAll($result);
181+
return is_array($rows) ? $rows : [];
182+
}
183+
184+
public function isCommentAllowed(int $recordId, string $recordLang, string $commentType = 'faq'): bool
185+
{
186+
$table = 'news' === $commentType ? 'faqnews' : 'faqdata';
187+
188+
$sql = <<<SQL
189+
SELECT
190+
comment
191+
FROM
192+
%s%s
193+
WHERE
194+
id = %d
195+
AND
196+
lang = '%s'
197+
SQL;
198+
199+
$query = sprintf(
200+
$sql,
201+
Database::getTablePrefix(),
202+
$table,
203+
$recordId,
204+
$this->configuration->getDb()->escape($recordLang),
205+
);
206+
207+
$result = $this->configuration->getDb()->query($query);
208+
if ($row = $this->configuration->getDb()->fetchObject($result)) {
209+
return $row->comment === 'y';
210+
}
211+
212+
return false;
213+
}
214+
}

0 commit comments

Comments
 (0)