Skip to content

Commit

Permalink
Merge pull request #14 from pixelant/dev
Browse files Browse the repository at this point in the history
[FEATURE] New views added, completely rebuild extension
  • Loading branch information
VladVilya authored Nov 19, 2020
2 parents 1159866 + e6f2ff1 commit 82e9d82
Show file tree
Hide file tree
Showing 42 changed files with 699 additions and 250 deletions.
159 changes: 159 additions & 0 deletions Classes/Controller/AjaxFeedbackController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php

namespace Jp\Jpfaq\Controller;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use Jp\Jpfaq\Domain\Repository\QuestionRepository;
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;

/**
* AjaxFeedbackController
*/
class AjaxFeedbackController
{
/**
* questionRepository
*
* @var QuestionRepository
*/
protected $questionRepository;

/**
* objectManager
*
* @var ObjectManager
*/
private $objectManager;

/**
* frontendAuthentication
*
* @var FrontendUserAuthentication
*/
protected $frontendAuthentication;

/**
* configurationManager
*
* @var ConfigurationManagerInterface
*/
protected $configurationManager;


public function __construct()
{
$this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(ObjectManager::class);
$this->questionRepository = $this->objectManager->get(QuestionRepository::class);
$this->frontendAuthentication = $this->objectManager->get(FrontendUserAuthentication::class);
}

/**
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @return ResponseInterface
*/

public function processRequest(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
$params = $request->getQueryParams();
$helpfulness = $this->updateHelpful($params['question'], $params['helpful']);
$responseJson = json_encode(['helpfulness' => $helpfulness]);
$response->getBody()->write($responseJson);
return $response;
}

/**
* Update helpful or nothelpful of a question in db and session
*
* @param string $questionUid
* @param bool $helpful
*
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
*
* @return bool
*/
private function updateHelpful(string $questionUid, bool $helpful)
{
$questionUid = (int)$questionUid;
$question = $this->questionRepository->findByUid($questionUid);
$questionHelpful = $question->getHelpful();
$questionNotHelpful = $question->getNothelpful();
$queryBuilder = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)->getQueryBuilderForTable('tx_jpfaq_domain_model_question');
$userClickedHelpfulness = $this->frontendAuthentication->getKey('ses', 'tx_jpfaq_helpfulness_' . $questionUid);
$isHelpful = false;

// User already clicked helpful for this question this session
if (isset($userClickedHelpfulness)) {
// User changes helpful to nothelpful
if ($userClickedHelpfulness & !$helpful) {
$queryBuilder
->update('tx_jpfaq_domain_model_question')
->where(
$queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($questionUid))
)
->set('nothelpful', $questionNotHelpful + 1)
->execute();
if ($questionHelpful > 0) {
$queryBuilder
->update('tx_jpfaq_domain_model_question')
->where(
$queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($questionUid))
)
->set('nothelpful', $questionNotHelpful - 1)
->execute();
}
$isHelpful = false;
} elseif (!$userClickedHelpfulness & $helpful) {
// User changes nothelpful to helpful
$queryBuilder
->update('tx_jpfaq_domain_model_question')
->where(
$queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($questionUid))
)
->set('helpful', $questionNotHelpful + 1)
->execute();
if ($questionNotHelpful > 0) {
$queryBuilder
->update('tx_jpfaq_domain_model_question')
->where(
$queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($questionUid))
)
->set('helpful', $questionNotHelpful - 1)
->execute();
}
$isHelpful = true;
}
} else {
// User has not clicked helpful or nothelpful for this question this session
if ($helpful) {
$queryBuilder
->update('tx_jpfaq_domain_model_question')
->where(
$queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($questionUid))
)
->set('helpful', $questionNotHelpful + 1)
->execute();
$isHelpful = true;
} else {
$queryBuilder
->update('tx_jpfaq_domain_model_question')
->where(
$queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($questionUid))
)
->set('nothelpful', $questionNotHelpful + 1)
->execute();
$isHelpful = false;
}
}

// Store user interaction on helpfulness in session
$this->frontendAuthentication->setKey('ses', 'tx_jpfaq_helpfulness_' . $questionUid, $helpful);
$this->frontendAuthentication->storeSessionData();

return $isHelpful;
}
}
Empty file modified Classes/Controller/CategoryController.php
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion Classes/Controller/CategorycommentController.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function addCommentAction(Categorycomment $newCategorycomment, array $cat

$categories = [];
$categoryNames = '';
if ($catUids[0] !== 'no categories') {
if ($catUids[0] !== '') {
foreach ($catUids as $catUid) {
$catUid = intval($catUid);

Expand Down
148 changes: 69 additions & 79 deletions Classes/Controller/QuestionController.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public function initializeListAction()
{
# Avoid code injection
$this->settings['questions']['categories'] = [];
if ($this->settings['flexform']['selectCategory']) {
$categories = explode(',', $this->settings['flexform']['selectCategory']);
if ($this->settings['flexform']['selectedCategory']) {
$categories = explode(',', $this->settings['flexform']['selectedCategory']);
foreach ($categories as $category) {
$this->settings['questions']['categories'][] = (int)$category;
}
Expand All @@ -53,14 +53,32 @@ public function initializeListAction()
/**
* action list
*
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
*
* @param Question|null $question
* @param string $selectedCategory
* @param int $categoryDetail
* @param string $singleViewPid
* @param array $gtag
* @return void
* @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
*/
public function listAction()
public function listAction(Question $question = null, string $selectedCategory = '', int $categoryDetail = 0, string $singleViewPid = '0', array $gtag = [])
{
$restrictToCategories = $this->settings['questions']['categories'];
$excludeAlreadyDisplayedQuestions = intval($this->settings['excludeAlreadyDisplayedQuestions']);

if ($question !== null) {
$this->forward('detail', null, null, ['question' => $question]);
}

if ($this->settings['singleViewPid']) {
$singleViewPid = $this->settings['singleViewPid'];
}

if ($this->settings['gtag']) {
$gtag = $this->settings['gtag'];
}

$restrictToCategories = ($selectedCategory) ? [(int)$selectedCategory] : $this->settings['questions']['categories'];
$excludeAlreadyDisplayedQuestions = (int)$this->settings['excludeAlreadyDisplayedQuestions'];
$questions = $this->questionRepository->findQuestionsWithConstraints($restrictToCategories, $excludeAlreadyDisplayedQuestions);

$categories = [];
Expand All @@ -69,53 +87,72 @@ public function listAction()
}

if (!$restrictToCategories) {
$restrictToCategories = ['no categories'];
$restrictToCategories = [];
}

$currentUid = $this->getCurrentUid();

$this->view->assignMultiple(array(
'showSearchForm' => intval($this->settings['flexform']['showSearch']),
'showNumberOfResults' => intval($this->settings['flexform']['showNumberOfResults']),
'showQuestionCommentForm' => intval($this->settings['flexform']['showQuestionCommentForm']),
'showCategoriesCommentForm' => intval($this->settings['flexform']['showCategoriesCommentForm']),
'showSearchForm' => (int)$this->settings['flexform']['showSearch'],
'showNumberOfResults' => (int)$this->settings['flexform']['showNumberOfResults'],
'showQuestionCommentForm' => (int)$this->settings['flexform']['showQuestionCommentForm'],
'showCategoriesCommentForm' => (int)$this->settings['flexform']['showCategoriesCommentForm'],
'categories' => $categories,
'restrictToCategories' => $restrictToCategories,
'currentUid' => $currentUid,
'gtag' => $this->settings['gtag'],
'categoryDetail' => $categoryDetail,
'singleViewPid' => (int)$singleViewPid,
'gtag' => $gtag,
'questions' => $questions
));
}

/**
* Action helpfulness
* action detail
*
* @param Question $question
* @param bool $helpful
* @param int $pluginUid
* @param array $gtag
*
* @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
*
* @return void|bool
* @return void
*/
public function helpfulnessAction(Question $question, bool $helpful, int $pluginUid)
public function detailAction(Question $question, array $gtag)
{
$currentUid = $this->getCurrentUid();

if ($currentUid == $pluginUid) {
$this->updateHelpful($question, $helpful);
$restrictToCategories = $this->settings['questions']['categories'];

if (!$helpful) {
// Show comment form
$this->forward('comment', 'Questioncomment', null);
}
} else {
# Do not render helpfulness view
# When multiple plugins on a page we want action for the one who called it
return false;
$categories = [];
foreach ($restrictToCategories as $uid) {
$categories[] = $this->categoryRepository->findByUid($uid);
}

if (!$restrictToCategories) {
$restrictToCategories = [''];
}

$this->view->assignMultiple(array(
'question' => $question,
'showQuestionCommentForm' => (int)$this->settings['flexform']['showQuestionCommentForm'],
'currentUid' => $currentUid,
'gtag' => $gtag,
'restrictToCategories' => $restrictToCategories,
'categories' => $categories,
'questionDetail' => 1
));
}

/**
* action categoryDetail
*
* @param string $selectedCategory
* @param array $gtag
* @param int $categoryDetail
* @param string $singleViewPid
* @return void
*/
public function categoryDetailAction(string $selectedCategory, array $gtag, int $categoryDetail = 0, string $singleViewPid = '0'){
$this->forward('list', null, null,
['selectedCategory' => $selectedCategory, 'categoryDetail' => $categoryDetail, 'singleViewPid' => $singleViewPid, 'gtag' => $gtag]);
}

/**
Expand All @@ -131,51 +168,4 @@ private function getCurrentUid()
return $currentUid;
}

/**
* Update helpful or nothelpful of a question in db and session
*
* @param Question $question
* @param bool $helpful
*
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
*
* @return void
*/
private function updateHelpful(Question $question, bool $helpful)
{
$questionUid = $question->getUid();
$questionHelpful = $question->getHelpful();
$questionNotHelpful = $question->getNothelpful();
$userClickedHelpfulness = $GLOBALS['TSFE']->fe_user->getKey('ses', $questionUid);

// User already clicked helpful for this question this session
if (isset($userClickedHelpfulness)) {
// User changes helpful to nothelpful
if ($userClickedHelpfulness & !$helpful) {
$question->setNothelpful($questionNotHelpful + 1);
if ($questionHelpful > 0) {
$question->setHelpful($questionHelpful - 1);
}
} elseif (!$userClickedHelpfulness & $helpful) {
// User changes nothelpful to helpful
$question->setHelpful($questionHelpful + 1);
if ($questionNotHelpful > 0) {
$question->setNothelpful($questionNotHelpful - 1);
}
}
} else {
// User has not clicked helpful or nothelpful for this question this session
if ($helpful) {
$question->setHelpful($questionHelpful + 1);
} else {
$question->setNothelpful($questionNotHelpful + 1);
}
}

$this->questionRepository->update($question);

// Store user interaction on helpfulness in session
$GLOBALS['TSFE']->fe_user->setKey('ses', $questionUid, $helpful);
}
}
Loading

0 comments on commit 82e9d82

Please sign in to comment.