Skip to content
This repository has been archived by the owner on Oct 15, 2023. It is now read-only.

Commit

Permalink
[FEATURE #34623] Extend Extension
Browse files Browse the repository at this point in the history
- Editing of Comments by Author
- Deleting of Comments by Author
- Send Mail to News Author on Comment Submit
  • Loading branch information
David Chalupsky committed Sep 12, 2023
1 parent ebe1d7a commit 2a765b9
Show file tree
Hide file tree
Showing 28 changed files with 800 additions and 103 deletions.
208 changes: 198 additions & 10 deletions Classes/Controller/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* | 2015 Dennis Roemmich <[email protected]>
* | 2016-2017 Christian Wolfram <[email protected]>
*/

use GeorgRinger\News\Domain\Repository\NewsRepository;
use T3\PwComments\Domain\Model\Comment;
use T3\PwComments\Domain\Model\Vote;
use T3\PwComments\Domain\Repository\CommentRepository;
Expand All @@ -17,10 +19,14 @@
use T3\PwComments\Utility\Mail;
use T3\PwComments\Utility\Settings;
use T3\PwComments\Utility\StringUtility;
use TYPO3\CMS\Core\DataHandling\DataHandler;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Domain\Model\FrontendUser;
use TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository;
use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException;
use TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException;
use TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException;
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
use TYPO3\CMS\Extbase\Persistence\Generic\QueryResult;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
Expand Down Expand Up @@ -208,12 +214,14 @@ public function indexAction(Comment $commentToReplyTo = null)
}
}
}

$this->view->assign('upvotedCommentUids', $upvotedCommentUids);
$this->view->assign('downvotedCommentUids', $downvotedCommentUids);

$this->view->assign('comments', $comments);
$this->view->assign('commentCount', $this->calculateCommentCount($comments));
$this->view->assign('commentToReplyTo', $commentToReplyTo);
$this->view->assign('userIdent', $this->currentAuthorIdent);
}

/**
Expand Down Expand Up @@ -254,8 +262,8 @@ public function createAction(Comment $newComment = null)
$newComment->setAuthor($author);
} else {
$newComment->setAuthor(null);
$GLOBALS['TSFE']->fe_user->setKey('ses', 'tx_pwcomments_unregistredUserName', $newComment->getAuthorName());
$GLOBALS['TSFE']->fe_user->setKey('ses', 'tx_pwcomments_unregistredUserMail', $newComment->getAuthorMail());
$GLOBALS['TSFE']->fe_user->setKey('ses', 'tx_pwcomments_unregisteredUserName', $newComment->getAuthorName());
$GLOBALS['TSFE']->fe_user->setKey('ses', 'tx_pwcomments_unregisteredUserMail', $newComment->getAuthorMail());
}
$GLOBALS['TSFE']->fe_user->setKey('ses', 'tx_pwcomments_lastComment', time());

Expand All @@ -280,6 +288,16 @@ public function createAction(Comment $newComment = null)
$this->commentRepository->add($newComment);
$this->getPersistenceManager()->persistAll();

if (isset($this->settings['sendMailOnNewCommentsToNewsAuthor']) && $this->settings['sendMailOnNewCommentsToNewsAuthor']) {
$newsRepository = GeneralUtility::makeInstance(NewsRepository::class);
$news = $newsRepository->findByUid($newComment->getEntryUid());
$this->mailUtility->setFluidTemplate($this->makeFluidTemplateObject());
$this->mailUtility->setControllerContext($this->controllerContext);
$this->mailUtility->setReceivers($news->getAuthorEmail());
$this->mailUtility->setTemplatePath($this->settings['sendMailNewsAuthorTemplate']);
$this->mailUtility->sendMail($newComment, HashEncryptionUtility::createHashForComment($newComment));
}

if (isset($this->settings['sendMailOnNewCommentsTo']) && $this->settings['sendMailOnNewCommentsTo']) {
$this->mailUtility->setFluidTemplate($this->makeFluidTemplateObject());
$this->mailUtility->setControllerContext($this->controllerContext);
Expand Down Expand Up @@ -326,22 +344,192 @@ public function newAction(Comment $newComment = null, Comment $commentToReplyTo
}
$this->view->assign('commentToReplyTo', $commentToReplyTo);

// Get name of unregistred user
// Get name of unregistered user
if ($newComment !== null && $newComment->getAuthorName()) {
$unregistredUserName = $newComment->getAuthorName();
$unregisteredUserName = $newComment->getAuthorName();
} else {
$unregistredUserName = $GLOBALS['TSFE']->fe_user->getKey('ses', 'tx_pwcomments_unregistredUserName');
$unregisteredUserName = $GLOBALS['TSFE']->fe_user->getKey('ses', 'tx_pwcomments_unregisteredUserName');
}

// Get mail of unregistred user
// Get mail of unregistered user
if ($newComment !== null && $newComment->getAuthorMail()) {
$unregistredUserMail = $newComment->getAuthorMail();
$unregisteredUserMail = $newComment->getAuthorMail();
} else {
$unregisteredUserMail = $GLOBALS['TSFE']->fe_user->getKey('ses', 'tx_pwcomments_unregisteredUserMail');
}

$this->view->assign('unregisteredUserName', $unregisteredUserName);
$this->view->assign('unregisteredUserMail', $unregisteredUserMail);
}

/**
* Edit action
*
* @param Comment|null $updateComment Comment
* @return void
*
* @TYPO3\CMS\Extbase\Annotation\IgnoreValidation("updateComment")
*/
public function editAction(Comment $updateComment = null): void
{
if ($updateComment !== null) {
$this->view->assign('updateComment', $updateComment);
}

// Get name of unregistered user
if ($updateComment !== null && $updateComment->getAuthorName()) {
$unregisteredUserName = $updateComment->getAuthorName();
} else {
$unregistredUserMail = $GLOBALS['TSFE']->fe_user->getKey('ses', 'tx_pwcomments_unregistredUserMail');
$unregisteredUserName = $GLOBALS['TSFE']->fe_user->getKey('ses', 'tx_pwcomments_unregisteredUserName');
}

$this->view->assign('unregistredUserName', $unregistredUserName);
$this->view->assign('unregistredUserMail', $unregistredUserMail);
// Get mail of unregistered user
if ($updateComment !== null && $updateComment->getAuthorMail()) {
$unregisteredUserMail = $updateComment->getAuthorMail();
} else {
$unregisteredUserMail = $GLOBALS['TSFE']->fe_user->getKey('ses', 'tx_pwcomments_unregisteredUserMail');
}

$this->view->assign('unregisteredUserName', $unregisteredUserName);
$this->view->assign('unregisteredUserMail', $unregisteredUserMail);
}

/**
* Update action
*
* @param Comment|null $updateComment
* @return bool
* @throws StopActionException
* @throws IllegalObjectTypeException
* @throws UnknownObjectException
* @TYPO3\CMS\Extbase\Annotation\Validate("T3\PwComments\Domain\Validator\CommentValidator", param="updateComment")
*/
public function updateAction(Comment $updateComment = null): bool
{
// Hidden field Spam-Protection
if (isset($this->settings['hiddenFieldSpamProtection']) && $this->settings['hiddenFieldSpamProtection']
&& $this->request->hasArgument($this->settings['hiddenFieldName'])
&& $this->request->getArgument($this->settings['hiddenFieldName'])) {
$this->redirectToUri($this->buildUriByUid($this->pageUid) . '#' . $this->settings['updateCommentAnchor']);
return false;
}
if ($updateComment === null) {
$this->redirectToUri($this->buildUriByUid($this->pageUid));
return false;
}
$this->createAuthorIdent();

$updateComment->setMessage(
StringUtility::prepareCommentMessage($updateComment->getMessage(), $this->settings['linkUrlsInComments'])
);

$GLOBALS['TSFE']->fe_user->setKey('ses', 'tx_pwcomments_lastComment', time());

$translateArguments = [
'name' => $updateComment->getAuthorName(),
'email' => $updateComment->getCommentAuthorMailAddress(),
'message' => $updateComment->getMessage(),
];

// Modify comment if moderation is active
if (isset($this->settings['moderateNewComments']) && $this->settings['moderateNewComments']) {
$updateComment->setHidden(true);
$this->addFlashMessage(
LocalizationUtility::translate('tx_pwcomments.moderationNotice', 'PwComments', $translateArguments)
);
} else {
$this->addFlashMessage(
LocalizationUtility::translate('tx_pwcomments.updateSuccess', 'PwComments', $translateArguments)
);
}

$this->commentRepository->update($updateComment);
$this->getPersistenceManager()->persistAll();
$newsRepository = GeneralUtility::makeInstance(NewsRepository::class);
$news = $newsRepository->findByUid($updateComment->getEntryUid());


if (isset($this->settings['sendMailOnNewCommentsToNewsAuthor']) && $this->settings['sendMailOnNewCommentsToNewsAuthor']) {
$newsRepository = GeneralUtility::makeInstance(NewsRepository::class);
$news = $newsRepository->findByUid($updateComment->getEntryUid());
$this->mailUtility->setFluidTemplate($this->makeFluidTemplateObject());
$this->mailUtility->setControllerContext($this->controllerContext);
$this->mailUtility->setReceivers($news->getAuthorEmail());
$this->mailUtility->setTemplatePath($this->settings['sendMailNewsAuthorUpdateTemplate']);
$this->mailUtility->setSubjectLocallangKey('tx_pwcomments.notificationMail.update.subject');
$this->mailUtility->sendMail($updateComment, HashEncryptionUtility::createHashForComment($updateComment));
}

if (isset($this->settings['sendMailOnNewCommentsTo']) && $this->settings['sendMailOnNewCommentsTo']) {
$this->mailUtility->setFluidTemplate($this->makeFluidTemplateObject());
$this->mailUtility->setControllerContext($this->controllerContext);
$this->mailUtility->setReceivers($this->settings['sendMailOnNewCommentsTo']);
$this->mailUtility->setTemplatePath($news === null ? $this->settings['sendMailUpdateTemplate'] : $this->settings['sendMailNewsAuthorUpdateTemplate']);
$this->mailUtility->setSubjectLocallangKey('tx_pwcomments.notificationMail.update.subject');
$this->mailUtility->sendMail($updateComment, HashEncryptionUtility::createHashForComment($updateComment));
}

if (isset($this->settings['sendMailToAuthorAfterSubmit']) &&
$this->settings['sendMailToAuthorAfterSubmit'] &&
$updateComment->hasCommentAuthorMailAddress()
) {
$this->mailUtility->setFluidTemplate($this->makeFluidTemplateObject());
$this->mailUtility->setControllerContext($this->controllerContext);
$this->mailUtility->setReceivers($updateComment->getCommentAuthorMailAddress());
$this->mailUtility->setTemplatePath($news === null ? $this->settings['sendMailToAuthorAfterSubmitUpdateTemplate'] : $this->settings['sendMailNewsAuthorUpdateTemplate']);
$this->mailUtility->setSubjectLocallangKey('tx_pwcomments.notificationMail.update.subject');
$this->mailUtility->sendMail($updateComment);
}

if (isset($this->settings['moderateNewComments']) && $this->settings['moderateNewComments']) {
$anchor = '#' . $this->settings['successfulAnchor'];
} else {
$anchor = '#' . $this->settings['commentAnchorPrefix'] . $updateComment->getUid();
}

$this->redirectToUri($this->buildUriByUid($this->pageUid, true) . $anchor);
}

/**
* Delete action
*
* @param string $commentUid Comment
* @return void
*
* @TYPO3\CMS\Extbase\Annotation\IgnoreValidation("commentUid")
*/
public function deleteAction(string $commentUid): void
{
$commentWithReplies = $this->commentRepository->findByCommentUid($commentUid);
$dataHandler = GeneralUtility::makeInstance(DataHandler::class);
if (null !== $commentWithReplies) {
$votes = $commentWithReplies->getVotes()->toArray();
if (!empty($votes)) {
foreach ($votes as $vote) {
$cmd['tx_pwcomments_domain_model_vote'][$vote->getUid()]['delete'] = 1;
}
}
$replies = $commentWithReplies->getReplies()->toArray();
if ($replies !== null) {
foreach ($replies as $reply) {
$cmd['tx_pwcomments_domain_model_comment'][$reply->getUid()]['delete'] = 1;
$replyVotes = $reply->getVotes()->toArray();
if (!empty($replyVotes)) {
foreach ($replyVotes as $replyVote) {
$cmd['tx_pwcomments_domain_model_vote'][$replyVote->getUid()]['delete'] = 1;
}
}
}
}
$cmd['tx_pwcomments_domain_model_comment'][$commentWithReplies->getUid()]['delete'] = 1;
$dataHandler->start([], $cmd);
$dataHandler->process_cmdmap();
$this->addFlashMessage(
LocalizationUtility::translate('tx_pwcomments.custom.deletedComment', 'PwComments')
);
}

$this->redirectToUri($this->buildUriByUid($this->pageUid, true));
}

/**
Expand Down
29 changes: 29 additions & 0 deletions Classes/Domain/Model/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ class Comment extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
*/
protected $crdate;

/**
* tstamp as unix timestamp
*
* @var int
*/
protected $tstamp;

/**
* hidden state
*
Expand Down Expand Up @@ -182,6 +189,27 @@ public function setCrdate($crdate)
$this->crdate = $crdate;
}

/**
* Getter for tstamp
*
* @return int tstamp
*/

public function getTstamp()
{
return $this->tstamp;
}
/**
* Setter for tstamp
*
* @param int $tstamp tstamp
* @return void
*/
public function setTstamp($tstamp)
{
$this->tstamp = $tstamp;
}

/**
* Getter for crdate
*
Expand All @@ -192,6 +220,7 @@ public function getCrdate()
return $this->crdate;
}


/**
* Setter for hidden state
*
Expand Down
26 changes: 18 additions & 8 deletions Classes/Utility/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* | 2015 Dennis Roemmich <[email protected]>
* | 2016-2017 Christian Wolfram <[email protected]>
*/

use GeorgRinger\News\Domain\Repository\NewsRepository;
use TYPO3\CMS\Core\Mail\MailMessage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
Expand Down Expand Up @@ -156,15 +158,23 @@ protected function getMailMessage(Comment $comment, $hash)
if (!file_exists($mailTemplate)) {
throw new \Exception('Mail template (' . $mailTemplate . ') not found. ');
}

$this->fluidTemplate->setTemplatePathAndFilename($mailTemplate);
$this->fluidTemplate->assignMultiple(
[
'hash' => $hash,
'comment' => $comment,
'settings' => $this->settings
]
);
$params = [
'hash' => $hash,
'comment' => $comment,
'settings' => $this->settings
];

if (isset($this->settings['sendMailOnNewCommentsToNewsAuthor'])) {
$newsRepository = GeneralUtility::makeInstance(NewsRepository::class);
$news = $newsRepository->findByUid($comment->getEntryUid());
if (null !== $news) {
$params['news'] = $news;
$params['site'] = GeneralUtility::getIndpEnv('HTTP_HOST');
}
}
$this->fluidTemplate->assignMultiple($params);

return $this->fluidTemplate->render();
}

Expand Down
6 changes: 3 additions & 3 deletions Configuration/FlexForms/Plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
<items>
<numIndex index="1">
<numIndex index="0">LLL:EXT:pw_comments/Resources/Private/Language/locallang_db.xlf:plugin.mode.index</numIndex>
<numIndex index="1">Comment->index;Comment->upvote;Comment->downvote</numIndex>
<numIndex index="1">Comment->index;Comment->upvote;Comment->downvote;Comment->delete</numIndex>
</numIndex>
<numIndex index="2">
<numIndex index="0">LLL:EXT:pw_comments/Resources/Private/Language/locallang_db.xlf:plugin.mode.new</numIndex>
<numIndex index="1">Comment->new;Comment->create;Comment->confirmComment</numIndex>
<numIndex index="0">LLL:EXT:pw_comments/Resources/Private/Language/locallang_db.xlf:plugin.mode.new</numIndex>
<numIndex index="1">Comment->new;Comment->create;Comment->edit;Comment->update;Comment->confirmComment</numIndex>
</numIndex>
</items>
</config>
Expand Down
7 changes: 7 additions & 0 deletions Configuration/TCA/tx_pwcomments_domain_model_comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@
'type' => 'input'
]
],
'tstamp' => [
'exclude' => 0,
'label' => $ll . 'general.tstamp',
'config' => [
'type' => 'input'
]
],
'hidden' => [
'exclude' => 0,
'label' => $ll . 'general.hidden',
Expand Down
Loading

0 comments on commit 2a765b9

Please sign in to comment.