Source of file FlagCommentExtension.php
Size: 4,866 Bytes - Last Modified: 2021-12-23T10:09:08+00:00
/var/www/docs.ssmods.com/process/src/src/extensions/FlagCommentExtension.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 | <?php namespace NZTA\FlagComments\Extensions; use SilverStripe\ORM\DataExtension; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\CheckboxField; use SilverStripe\Security\Member; use SilverStripe\Security\SecurityToken; use SilverStripe\Control\HTTP; use Psr\Log\LoggerInterface; use SilverStripe\Core\Injector\Injector; use SilverStripe\Control\Controller; use SilverStripe\Security\RandomGenerator; use SilverStripe\ORM\ValidationException; use SilverStripe\ORM\SS_List; class FlagCommentExtension extends DataExtension { /** * @var array */ private static $db = [ 'Flagged' => 'Boolean', 'FlaggedAndRemoved' => 'Boolean', 'FlaggedSecurityToken' => 'Varchar(255)', ]; /** * @param FieldList $fields * * @return void */ public function updateCMSFields(FieldList $fields) { $optionField = null; foreach ($fields as $field) { if (get_class($field) == 'FieldGroup' && $field->Name() == 'Options') { $field->push(CheckboxField::create('Flagged', 'Flagged?')); break; } } } /** * Checks if the given user can flag a comment * * @param Member $member * * @return bool */ public function canFlag(Member $member = null) { if ($this->owner->Flagged) { return false; } $parent = $this->owner->Parent(); $comments = $parent->config()->comments; return isset($comments['can_flag']) && $comments['can_flag'] && $parent->canPostComment($member); } /** * Returns a link to flag the current comment * * @return string */ public function FlagLink() { $link = Controller::join_links( 'comments', 'flagcomment', $this->owner->ID ); return HTTP::setGetVar('SecurityID', SecurityToken::inst()->getValue(), $link); } /** * Returns a link to remove a flagged comment * * @return string */ public function RemoveFlaggedCommentLink() { $link = Controller::join_links( 'comments', 'removeflaggedcomment', $this->owner->ID ); return HTTP::setGetVar('token', $this->owner->FlaggedSecurityToken, $link); } /** * Returns a link to unflag a comment * * @return string */ public function UnflagLink() { $link = Controller::join_links( 'comments', 'unflagcomment', $this->owner->ID ); return HTTP::setGetVar('token', $this->owner->FlaggedSecurityToken, $link); } /** * Flags the current comment * * @return bool */ public function doFlag() { if (!$this->owner->canFlag()) { return false; } $this->owner->Flagged = true; $this->owner->FlaggedAndRemoved = false; $random = new RandomGenerator(); $this->owner->FlaggedSecurityToken = $random->randomToken(); try { $this->owner->write(); } catch (ValidationException $e) { Injector::inst()->get(LoggerInterface::class)->error($e->getMessage()); return false; } $this->owner->extend('afterFlag'); return true; } /** * Remove the flag on a comment * * @return bool */ public function doUnflag() { if (!$this->owner->canEdit()) { return false; } $this->owner->Flagged = false; $this->owner->FlaggedAndRemoved = false; $this->owner->FlaggedSecurityToken = null; try { $this->owner->write(); } catch (ValidationException $e) { Injector::inst()->get(LoggerInterface::class)->error($e->getMessage()); return false; } $this->owner->extend('afterUnflag'); return true; } /** * Remove a comment which has been flagged * * @return bool */ public function doRemoveFlaggedComment() { if (!$this->owner->canEdit()) { return false; } if (!$this->owner->Flagged) { return false; } $this->owner->FlaggedAndRemoved = true; $this->owner->FlaggedSecurityToken = null; try { $this->owner->write(); } catch (ValidationException $e) { Injector::inst()->get(LoggerInterface::class)->error($e->getMessage()); return false; } return true; } /** * Filters flagged and removed replies * * @param SS_List $list */ public function updateReplies(SS_List &$list) { $list = $list->filter('FlaggedAndRemoved', false); } } |