Skip to content

Commit

Permalink
refactor: moved query helper methods into own class
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Mar 17, 2024
1 parent 0cbcdcf commit 5239c64
Show file tree
Hide file tree
Showing 6 changed files with 383 additions and 218 deletions.
40 changes: 38 additions & 2 deletions phpmyfaq/src/phpMyFAQ/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
* Class Comments
* @package phpMyFAQ
*/
class Comments
readonly class Comments
{
/**
* Constructor.
*/
public function __construct(private readonly Configuration $configuration)
public function __construct(private Configuration $configuration)
{
}

Expand Down Expand Up @@ -329,4 +329,40 @@ public function getAllComments(string $type = CommentType::FAQ): array

return $comments;
}
/**
* Checks, if comments are disabled for the FAQ record.
*
* @param int $recordId ID of FAQ or news entry
* @param string $recordLang Language
* @param string $commentType Type of comment: faq or news
* @return bool false, if comments are disabled
*/
public function isCommentAllowed(int $recordId, string $recordLang, string $commentType = 'faq'): bool
{
$table = 'news' === $commentType ? 'faqnews' : 'faqdata';

$query = sprintf(
"
SELECT
comment
FROM
%s%s
WHERE
id = %d
AND
lang = '%s'",
Database::getTablePrefix(),
$table,
$recordId,
$this->configuration->getDb()->escape($recordLang)
);

$result = $this->configuration->getDb()->query($query);

if ($row = $this->configuration->getDb()->fetchObject($result)) {
return $row->comment === 'y';
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function create(Request $request): JsonResponse

if (
!empty($username) && !empty($mailer) && !empty($commentText) && $stopWords->checkBannedWord($commentText) &&
!$faq->commentDisabled($id, $languageCode, $type) && !$faq->isActive($id, $languageCode, $type)
!$comment->isCommentAllowed($id, $languageCode, $type) && !$faq->isActive($id, $languageCode, $type)
) {
try {
$session->userTracking('save_comment', $id);
Expand Down
3 changes: 2 additions & 1 deletion phpmyfaq/src/phpMyFAQ/Controller/Frontend/FaqController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use phpMyFAQ\Enums\PermissionType;
use phpMyFAQ\Faq;
use phpMyFAQ\Faq\FaqMetaData;
use phpMyFAQ\Faq\QueryHelper;
use phpMyFAQ\Filter;
use phpMyFAQ\Helper\CategoryHelper;
use phpMyFAQ\Helper\FaqHelper;
Expand Down Expand Up @@ -112,7 +113,7 @@ public function create(Request $request): JsonResponse
$faqEntity
->setLanguage($languageCode)
->setQuestion($questionText)
->setActive((bool)($autoActivate ? FAQ_SQL_ACTIVE_YES : FAQ_SQL_ACTIVE_NO))
->setActive((bool)($autoActivate ? QueryHelper::FAQ_SQL_ACTIVE_YES : QueryHelper::FAQ_SQL_ACTIVE_NO))
->setSticky(false)
->setAnswer($answer)
->setKeywords($keywords)
Expand Down

0 comments on commit 5239c64

Please sign in to comment.