bookmark-pages/Classes/Domain/Model/Vote.php

243 lines
6.9 KiB
PHP
Raw Permalink Normal View History

2021-08-17 19:45:38 +02:00
<?php
/*
* This file is part of the package thucke/th-rating.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/
namespace WapplerSystems\BookmarksLikesRatings\Domain\Model;
use WapplerSystems\BookmarksLikesRatings\Service\ExtensionHelperService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Annotation as Extbase;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException;
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
/**
* Model for rating votes
*
* @copyright Copyright belongs to the respective authors
* @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
* @entity
*/
class Vote extends AbstractEntity
{
/**
* @Extbase\Validate("\WapplerSystems\BookmarksLikesRatings\Domain\Validator\RatingValidator")
* @Extbase\Validate("NotEmpty")
* @var \WapplerSystems\BookmarksLikesRatings\Domain\Model\Rating
*/
protected $rating;
/**
* The voter of this object
*
* @Extbase\Validate("NotEmpty")
* @var \WapplerSystems\BookmarksLikesRatings\Domain\Model\Voter
*/
protected $voter;
/**
* The actual voting of this object
*
* @Extbase\Validate("\WapplerSystems\BookmarksLikesRatings\Domain\Validator\StepconfValidator")
* @Extbase\Validate("NotEmpty")
* @var \WapplerSystems\BookmarksLikesRatings\Domain\Model\Stepconf
*/
protected $vote;
/**
* @var array
*/
protected $settings;
/**
* @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
*/
protected $objectManager;
/**
* @var \TYPO3\CMS\Core\Log\Logger
*/
protected $logger;
/**
* @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
*/
public function injectObjectManager(ObjectManagerInterface $objectManager)
{
$this->objectManager = $objectManager;
}
/**
* @var \WapplerSystems\BookmarksLikesRatings\Service\ExtensionHelperService
*/
protected $extensionHelperService;
/**
* @param \WapplerSystems\BookmarksLikesRatings\Service\ExtensionHelperService $extensionHelperService
*/
public function injectExtensionHelperService(ExtensionHelperService $extensionHelperService): void
{
$this->extensionHelperService = $extensionHelperService;
}
/**
* Constructs a new rating object
*
* @param \WapplerSystems\BookmarksLikesRatings\Domain\Model\Rating|null $rating
* @param \WapplerSystems\BookmarksLikesRatings\Domain\Model\Voter|null $voter
* @param \WapplerSystems\BookmarksLikesRatings\Domain\Model\Stepconf|null $vote
* @throws InvalidConfigurationTypeException
*/
/** @noinspection PhpUnused */
public function __construct(
Rating $rating = null,
Voter $voter = null,
Stepconf $vote = null
) {
if ($rating) {
$this->setRating($rating);
}
if ($voter) {
$this->setVoter($voter);
}
if ($vote) {
$this->setVote($vote);
}
$this->initializeObject();
}
/**
* Initializes the new vote object
* @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
*/
public function initializeObject()
{
if (empty($this->objectManager)) {
$this->objectManager = GeneralUtility::makeInstance(ObjectManager::class);
}
if (empty($this->extensionHelperService)) {
$this->extensionHelperService = GeneralUtility::makeInstance(ExtensionHelperService::class);
}
$this->logger = $this->extensionHelperService->getLogger(__CLASS__);
$this->settings = $this->objectManager->get(ConfigurationManager::class)->getConfiguration(
'Settings',
'thRating',
'pi1'
);
//\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($this,get_class($this).' initializeObject');
}
/**
* Sets the rating this vote is part of
*
* @param \WapplerSystems\BookmarksLikesRatings\Domain\Model\Rating $rating The Rating
*/
public function setRating(Rating $rating)
{
$this->rating = $rating;
$this->setPid($rating->getPid());
}
/**
* Returns the rating this vote is part of
*
* @return \WapplerSystems\BookmarksLikesRatings\Domain\Model\Rating The rating this vote is part of
*/
public function getRating(): Rating
{
return $this->rating;
}
/**
* Sets the frontenduser of this vote
*
* @param \WapplerSystems\BookmarksLikesRatings\Domain\Model\Voter $voter The frontenduser
*/
public function setVoter(Voter $voter)
{
$this->voter = $voter;
}
/**
* Returns the frontenduser of this vote
*
* @return \WapplerSystems\BookmarksLikesRatings\Domain\Model\Voter|null
*/
public function getVoter(): ?Voter
{
return $this->voter;
}
/**
* Sets the choosen stepconfig
*
* @param \WapplerSystems\BookmarksLikesRatings\Domain\Model\Stepconf $vote
*/
public function setVote($vote)
{
$this->vote = $vote;
}
/**
* Gets the rating object uid
*
* @return \WapplerSystems\BookmarksLikesRatings\Domain\Model\Stepconf|null Reference to selected stepconfig
*/
public function getVote()
{
return $this->vote;
}
/**
* Sets the rating this vote is part of
*
* @return bool
*/
/** @noinspection PhpUnused */
public function hasRated()
{
return $this->getVote() !== null && ($this->getVote() instanceof Stepconf);
}
/**
* Checks if vote is done by anonymous user
*
* @return bool
*/
public function isAnonymous(): bool
{
return !empty($this->settings['mapAnonymous']) && !empty($this->getVoter()) &&
$this->getVoter()->getUid() === (int)$this->settings['mapAnonymous'];
}
/**
* Checks cookie if anonymous vote is already done
* always false if cookie checks is deactivated
*
* @param string $prefixId Extension prefix to identify cookie
* @return bool
*/
public function hasAnonymousVote($prefixId = 'DummyPrefix'): bool
{
$anonymousRating = GeneralUtility::makeInstance(\WapplerSystems\BookmarksLikesRatings\Service\JsonService::class)
->decodeJsonToArray($_COOKIE[$prefixId . '_AnonymousRating_' . $this->getRating()->getUid()]);
return !empty($anonymousRating['voteUid']);
}
/**
* Method to use Object as plain string
*
* @return string
*/
public function __toString(): string
{
return (string)$this->getVote();
}
}