Skip to content

Commit

Permalink
refactor: extracted method into correct helper class
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Apr 6, 2024
1 parent c07240f commit 7672332
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 91 deletions.
9 changes: 8 additions & 1 deletion phpmyfaq/open-questions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* @since 2002-09-17
*/

use phpMyFAQ\Configuration;
use phpMyFAQ\Helper\QuestionHelper;
use phpMyFAQ\Translation;

if (!defined('IS_VALID_PHPMYFAQ')) {
Expand All @@ -24,6 +26,11 @@

$faqSession->userTracking('open_questions', 0);

$faqConfig = Configuration::getConfigurationInstance();

$questionHelper = new QuestionHelper($faqConfig);
$questionHelper->setCategory($category);

try {
$template->parse(
'mainPageContent',
Expand All @@ -32,7 +39,7 @@
'msgQuestionText' => Translation::get('msgQuestionText'),
'msgDate_User' => Translation::get('msgDate_User'),
'msgQuestion2' => Translation::get('msgQuestion2'),
'renderOpenQuestionTable' => $faq->renderOpenQuestions()
'renderOpenQuestionTable' => $questionHelper->renderOpenQuestions()
]
);
} catch (Exception) {
Expand Down
90 changes: 0 additions & 90 deletions phpmyfaq/src/phpMyFAQ/Faq.php
Original file line number Diff line number Diff line change
Expand Up @@ -2072,96 +2072,6 @@ public function getRecordsWithoutPagingByCategoryId(int $categoryId): string
return $output;
}

/**
* Prints the open questions as a HTML table.
*
* @todo needs to be moved to a QuestionHelper class
* @throws Exception
*/
public function renderOpenQuestions(): string
{
global $sids, $category;

$date = new Date($this->configuration);
$mail = new Mail($this->configuration);

$query = sprintf(
"SELECT COUNT(id) AS num FROM %sfaqquestions WHERE lang = '%s' AND is_visible != 'Y'",
Database::getTablePrefix(),
$this->configuration->getLanguage()->getLanguage()
);

$result = $this->configuration->getDb()->query($query);
$row = $this->configuration->getDb()->fetchObject($result);
$numOfInvisibles = $row->num;

if ($numOfInvisibles > 0) {
$extraout = sprintf(
'<tr><td colspan="3"><small>%s %s</small></td></tr>',
Translation::get('msgQuestionsWaiting'),
$numOfInvisibles
);
} else {
$extraout = '';
}

$query = sprintf(
"SELECT * FROM %sfaqquestions WHERE lang = '%s' AND is_visible = 'Y' ORDER BY created ASC",
Database::getTablePrefix(),
$this->configuration->getLanguage()->getLanguage()
);

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

if ($result && $this->configuration->getDb()->numRows($result) > 0) {
while ($row = $this->configuration->getDb()->fetchObject($result)) {
$output .= '<tr class="openquestions">';
$output .= sprintf(
'<td><small>%s</small><br><a href="mailto:%s">%s</a></td>',
$date->format(Date::createIsoDate($row->created)),
$mail->safeEmail($row->email),
Strings::htmlentities($row->username)
);
$output .= sprintf(
'<td><strong>%s:</strong><br>%s</td>',
isset($category->categoryName[$row->category_id]['name']) ?
Strings::htmlentities($category->categoryName[$row->category_id]['name']) :
'',
Strings::htmlentities($row->question)
);
if ($this->configuration->get('records.enableCloseQuestion') && $row->answer_id) {
$output .= sprintf(
'<td><a id="PMF_openQuestionAnswered" href="?%saction=faq&amp;cat=%d&amp;id=%d">%s</a></td>',
$sids,
$row->category_id,
$row->answer_id,
Translation::get('msg2answerFAQ')
);
} else {
$output .= sprintf(
'<td class="text-end">' .
'<a class="btn btn-primary" href="?%saction=add&amp;question=%d&amp;cat=%d">%s</a></td>',
$sids,
$row->id,
$row->category_id,
Translation::get('msg2answer')
);
}

$output .= '</tr>';
}
} else {
$output = sprintf(
'<tr><td colspan="3">%s</td></tr>',
Translation::get('msgNoQuestionsAvailable')
);
}

return $output . $extraout;
}


/**
* Prepares and returns the sticky records for the frontend.
*/
Expand Down
99 changes: 99 additions & 0 deletions phpmyfaq/src/phpMyFAQ/Helper/QuestionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
use phpMyFAQ\Category;
use phpMyFAQ\Configuration;
use phpMyFAQ\Core\Exception;
use phpMyFAQ\Database;
use phpMyFAQ\Date;
use phpMyFAQ\Language\Plurals;
use phpMyFAQ\Link;
use phpMyFAQ\Mail;
use phpMyFAQ\Search\SearchResultSet;
use phpMyFAQ\Strings;
use phpMyFAQ\Translation;
use phpMyFAQ\User;
use phpMyFAQ\Utils;
Expand All @@ -35,6 +38,8 @@
*/
readonly class QuestionHelper
{
private Category $category;

/**
* QuestionHelper constructor.
*/
Expand Down Expand Up @@ -74,4 +79,98 @@ public function generateSmartAnswer(SearchResultSet $faqSearchResult): string

return $smartAnswer;
}

public function renderOpenQuestions(): string
{
global $sids;

$date = new Date($this->configuration);
$mail = new Mail($this->configuration);

$query = sprintf(
"SELECT COUNT(id) AS num FROM %sfaqquestions WHERE lang = '%s' AND is_visible != 'Y'",
Database::getTablePrefix(),
$this->configuration->getLanguage()->getLanguage()
);

$result = $this->configuration->getDb()->query($query);
$row = $this->configuration->getDb()->fetchObject($result);
$numOfInvisibles = $row->num;

if ($numOfInvisibles > 0) {
$extraout = sprintf(
'<tr><td colspan="3"><small>%s %s</small></td></tr>',
Translation::get('msgQuestionsWaiting'),
$numOfInvisibles
);
} else {
$extraout = '';
}

$query = sprintf(
"SELECT * FROM %sfaqquestions WHERE lang = '%s' AND is_visible = 'Y' ORDER BY created ASC",
Database::getTablePrefix(),
$this->configuration->getLanguage()->getLanguage()
);

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

if ($result && $this->configuration->getDb()->numRows($result) > 0) {
while ($row = $this->configuration->getDb()->fetchObject($result)) {
$output .= '<tr class="openquestions">';
$output .= sprintf(
'<td><small>%s</small><br><a href="mailto:%s">%s</a></td>',
$date->format(Date::createIsoDate($row->created)),
$mail->safeEmail($row->email),
Strings::htmlentities($row->username)
);
$output .= sprintf(
'<td><strong>%s:</strong><br>%s</td>',
isset($this->category->categoryName[$row->category_id]['name']) ?
Strings::htmlentities($this->category->categoryName[$row->category_id]['name']) :
'',
Strings::htmlentities($row->question)
);
if ($this->configuration->get('records.enableCloseQuestion') && $row->answer_id) {
$output .= sprintf(
'<td><a id="PMF_openQuestionAnswered" href="?%saction=faq&amp;cat=%d&amp;id=%d">%s</a></td>',
$sids,
$row->category_id,
$row->answer_id,
Translation::get('msg2answerFAQ')
);
} else {
$output .= sprintf(
'<td class="text-end">' .
'<a class="btn btn-primary" href="?%saction=add&amp;question=%d&amp;cat=%d">%s</a></td>',
$sids,
$row->id,
$row->category_id,
Translation::get('msg2answer')
);
}

$output .= '</tr>';
}
} else {
$output = sprintf(
'<tr><td colspan="3">%s</td></tr>',
Translation::get('msgNoQuestionsAvailable')
);
}

return $output . $extraout;
}

public function setCategory(Category $category): QuestionHelper
{
$this->category = $category;
return $this;
}

public function getCategory(): Category
{
return $this->category;
}
}

0 comments on commit 7672332

Please sign in to comment.