Zwischenstand

This commit is contained in:
Sven Wappler
2021-08-17 19:45:38 +02:00
parent fde2759722
commit ce6b9e38dc
71 changed files with 7886 additions and 809 deletions

View File

@@ -0,0 +1,89 @@
<?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\Repository;
use WapplerSystems\BookmarksLikesRatings\Domain\Model\Rating;
use WapplerSystems\BookmarksLikesRatings\Domain\Model\Ratingobject;
use WapplerSystems\BookmarksLikesRatings\Domain\Validator\RatingValidator;
use WapplerSystems\BookmarksLikesRatings\Service\ExtensionHelperService;
use TYPO3\CMS\Extbase\Annotation as Extbase;
use TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException;
use TYPO3\CMS\Extbase\Persistence\Repository;
/**
* A repository for ratings
*/
class RatingRepository extends Repository
{
/**
* Defines name for function parameter
*/
public const ADD_IF_NOT_FOUND = true;
/**
* @var \WapplerSystems\BookmarksLikesRatings\Service\ExtensionHelperService
*/
protected $extensionHelperService;
/**
* @param \WapplerSystems\BookmarksLikesRatings\Service\ExtensionHelperService $extensionHelperService
*/
public function injectExtensionHelperService(ExtensionHelperService $extensionHelperService): void
{
$this->extensionHelperService = $extensionHelperService;
}
/**
* Finds the specific rating by giving the object and row uid
*
* @Extbase\Validate("\WapplerSystems\BookmarksLikesRatings\Domain\Validator\RatingobjectValidator", param="ratingobject")
* @param \WapplerSystems\BookmarksLikesRatings\Domain\Model\Ratingobject $ratingobject The concerned ratingobject
* @Extbase\Validate("NumberRange", options={"minimum": 1}, param="ratedobjectuid")
* @param int $ratedobjectuid The Uid of the rated row
* @param bool $addIfNotFound Set to true if new objects should instantly be added
* @return Rating
* @throws IllegalObjectTypeException
*/
public function findMatchingObjectAndUid(
Ratingobject $ratingobject,
int $ratedobjectuid,
$addIfNotFound = false
): Rating {
/** @var \WapplerSystems\BookmarksLikesRatings\Domain\Model\Rating $foundRow */
$foundRow = $this->objectManager->get(Rating::class);
$query = $this->createQuery();
$query->matching($query->logicalAnd(
[
$query->equals('ratingobject', $ratingobject->getUid()),
$query->equals('ratedobjectuid', $ratedobjectuid)
]
))->setLimit(1);
/*$queryParser = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class);
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($queryParser->convertQueryToDoctrineQueryBuilder($query)->getSQL(), get_class($this).' SQL');
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($queryParser->convertQueryToDoctrineQueryBuilder($query)->getParameters(), get_class($this).' SQL Parameter');*/
$queryResult = $query->execute();
if ($queryResult->count() > 0) {
$foundRow = $queryResult->getFirst();
} elseif ($addIfNotFound) {
$foundRow->setRatingobject($ratingobject);
$foundRow->setRatedobjectuid($ratedobjectuid);
$validator = $this->objectManager->get(RatingValidator::class);
if (!$validator->validate($foundRow)->hasErrors()) {
$this->add($foundRow);
}
$this->extensionHelperService->persistRepository(__CLASS__, $foundRow);
$foundRow = $this->findMatchingObjectAndUid($ratingobject, $ratedobjectuid);
}
return $foundRow;
}
}

View File

@@ -0,0 +1,114 @@
<?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\Repository;
use WapplerSystems\BookmarksLikesRatings\Domain\Model\Ratingobject;
use WapplerSystems\BookmarksLikesRatings\Domain\Validator\RatingobjectValidator;
use WapplerSystems\BookmarksLikesRatings\Exception\RecordNotFoundException;
use WapplerSystems\BookmarksLikesRatings\Service\ExtensionHelperService;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Persistence\Repository;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
/**
* A repository for rating objects
*/
class RatingobjectRepository extends Repository
{
/**
* Defines name for function parameter
*/
public const ADD_IF_NOT_FOUND = true;
/**
* @var \WapplerSystems\BookmarksLikesRatings\Service\ExtensionHelperService
*/
protected $extensionHelperService;
/**
* @param \WapplerSystems\BookmarksLikesRatings\Service\ExtensionHelperService $extensionHelperService
*/
public function injectExtensionHelperService(ExtensionHelperService $extensionHelperService): void
{
$this->extensionHelperService = $extensionHelperService;
}
/**
* @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
*/
protected $configurationManager;
/**
* @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
*/
public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager): void
{
$this->configurationManager = $configurationManager;
}
/**
* Finds the specific ratingobject by giving table and fieldname
*
* @param string $ratetable The tablename of the ratingobject
* @param string $ratefield The fieldname of the ratingobject
* @param bool $addIfNotFound Set to true if new objects should instantly be added
* @return \WapplerSystems\BookmarksLikesRatings\Domain\Model\Ratingobject The ratingobject
* @throws RecordNotFoundException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
*/
public function findMatchingTableAndField(string $ratetable, string $ratefield, bool $addIfNotFound = false): Ratingobject
{
/** @var \WapplerSystems\BookmarksLikesRatings\Domain\Model\Ratingobject $foundRow */
$foundRow = $this->objectManager->get(Ratingobject::class);
$query = $this->createQuery();
$query->matching($query->logicalAnd([
$query->equals('ratetable', $ratetable),
$query->equals('ratefield', $ratefield),
]))->setLimit(1);
/*$queryParser = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class);
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($queryParser->convertQueryToDoctrineQueryBuilder($query)->getSQL(), get_class($this).' SQL');
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($queryParser->convertQueryToDoctrineQueryBuilder($query)->getParameters(), get_class($this).' SQL Parameter');*/
/** @var \TYPO3\CMS\Extbase\Persistence\QueryResultInterface $queryResult */
$queryResult = $query->execute();
if ($queryResult->count() > 0) {
$foundRow = $queryResult->getFirst();
} elseif ($addIfNotFound) {
$foundRow->setRatetable($ratetable);
$foundRow->setRatefield($ratefield);
if (!$this->objectManager->get(RatingobjectValidator::class)->validate($foundRow)->hasErrors()) {
$this->add($foundRow);
}
$this->extensionHelperService->persistRepository(self::class, $foundRow);
$foundRow = $this->findMatchingTableAndField($ratetable, $ratefield);
} else {
throw new RecordNotFoundException(LocalizationUtility::translate('recordNotFound', 'ThRating'), 1567962473);
}
return $foundRow;
}
/**
* Finds the specific ratingobject by giving table and fieldname
*
* @param bool Switch to fetch ALL entries regardless of their pid
* @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface|array All ratingobjects of the site
*/
/** @noinspection PhpMissingParentCallCommonInspection */
public function findAll($ignoreStoragePage = false)
{
$query = $this->createQuery();
$query->getQuerySettings()->setRespectStoragePage(!$ignoreStoragePage);
return $query->execute();
}
}

View File

@@ -0,0 +1,78 @@
<?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\Repository;
use WapplerSystems\BookmarksLikesRatings\Domain\Model\Ratingobject;
use WapplerSystems\BookmarksLikesRatings\Domain\Model\Stepconf;
use WapplerSystems\BookmarksLikesRatings\Domain\Validator\StepconfValidator;
use TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
use TYPO3\CMS\Extbase\Persistence\Repository;
/**
* A repository for ratingstep configurations
* @method findByRatingobject(Ratingobject $getRatingobject)
*/
class StepconfRepository extends Repository
{
protected $defaultOrderings = ['steporder' => QueryInterface::ORDER_ASCENDING];
/**
* Initialize this repository
*/
public function initializeObject(): void
{
//disable RespectStoragePage as pid is always bound to parent objects pid
/** @var \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $defaultQuerySettings */
$defaultQuerySettings = $this->objectManager->get(QuerySettingsInterface::class);
$defaultQuerySettings->setRespectStoragePage(false);
$this->setDefaultQuerySettings($defaultQuerySettings);
}
/**
* Finds the given stepconf object in the repository
*
* @param \WapplerSystems\BookmarksLikesRatings\Domain\Model\Stepconf $stepconf The ratingobject to look for
* @return \WapplerSystems\BookmarksLikesRatings\Domain\Model\Stepconf
*/
public function findStepconfObject(Stepconf $stepconf): Stepconf
{
$query = $this->createQuery();
/** @noinspection NullPointerExceptionInspection */
$query->matching($query->logicalAnd([
$query->equals('ratingobject', $stepconf->getRatingobject()->getUid()),
$query->equals('steporder', $stepconf->getSteporder()),
]))->setLimit(1);
$queryResult = $query->execute();
/** @var \WapplerSystems\BookmarksLikesRatings\Domain\Model\Stepconf $foundRow */
$foundRow = $this->objectManager->get(Stepconf::class);
if (count($queryResult) !== 0) {
$foundRow = $queryResult->getFirst();
}
return $foundRow;
}
/**
* Finds the ratingstep entry by giving ratingobjectUid
*
* @param \WapplerSystems\BookmarksLikesRatings\Domain\Model\Stepconf $stepconf The uid of the ratingobject
* @return bool true if stepconf object exists in repository
*/
public function existStepconf(Stepconf $stepconf): bool
{
$foundRow = $this->findStepconfObject($stepconf);
$stepconfValidator = $this->objectManager->get(StepconfValidator::class);
return !$stepconfValidator->validate($foundRow)->hasErrors();
}
}

View File

@@ -0,0 +1,255 @@
<?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\Repository;
use InvalidArgumentException;
use Psr\Http\Message\ServerRequestInterface;
use WapplerSystems\BookmarksLikesRatings\Domain\Model\Stepname;
use WapplerSystems\BookmarksLikesRatings\Service\ExtensionHelperService;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
use TYPO3\CMS\Extbase\Persistence\Repository;
/**
* A repository for ratingstep configurations
*/
class StepnameRepository extends Repository
{
protected const TABLE_NAME = 'tx_thrating_domain_model_stepname';
protected const STEPCONF_NAME = 'stepconf';
/**
* @var string $syslangUidLiteral
*/
protected $syslangUidLiteral;
/**
* @var array
*/
protected $defaultOrderings;
/**
* Initialize this repository
*/
public function initializeObject(): void
{
$this->syslangUidLiteral = $GLOBALS['TCA'][self::TABLE_NAME]['ctrl']['languageField'];
$this->defaultOrderings = [ $this->syslangUidLiteral => QueryInterface::ORDER_ASCENDING];
}
/**
* Checks if stepname got a valid language code
*
* @param Stepname $stepname The stepname object
* @return bool
*/
public function checkStepnameLanguage(Stepname $stepname): bool
{
$stepnameLang = $stepname->getSysLanguageUid();
if ($stepnameLang > 0) {
//check if given language exist
try {
// only get language and do not assign the result to check if it exists
$this->objectManager
->get(ExtensionHelperService::class)
->getStaticLanguageById($stepnameLang);
} catch (InvalidArgumentException $exception) {
//invalid language code -> NOK
return false;
}
}
//language code found -> OK
return true;
}
/**
* Finds the given stepconf object in the repository
*
* @param Stepname $stepname The ratingname to look for
* @return Stepname|null
*/
public function findStepnameObject(Stepname $stepname): ?Stepname
{
$query = $this->createQuery();
$query->getQuerySettings()->setRespectSysLanguage(false);
$query->getQuerySettings()->setLanguageOverlayMode(false);
$query->matching(
$query->logicalAnd(
[
$query->equals(self::STEPCONF_NAME, $stepname->getStepconf()),
$query->equals($this->syslangUidLiteral, $stepname->getSysLanguageUid()),
]
)
)->setLimit(1);
/** @var \TYPO3\CMS\Extbase\Persistence\QueryResultInterface $queryResult */
$queryResult = $query->execute();
/*
$queryParser = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class);
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($queryParser->convertQueryToDoctrineQueryBuilder($query)->getSQL(), get_class($this).' SQL');
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($queryParser->convertQueryToDoctrineQueryBuilder($query)->getParameters(), get_class($this).' SQL Parameter');
*/
/** @var \WapplerSystems\BookmarksLikesRatings\Domain\Model\Stepname $foundRow */
$foundRow = null;
if ($queryResult->count() > 0) {
$foundRow = $queryResult->getFirst();
}
return $foundRow;
}
/**
* Finds the given stepname object in the repository
*
* @param int $uid
* @return Stepname|null
*/
public function findStrictByUid(int $uid): ?Stepname
{
$query = $this->createQuery();
$query->getQuerySettings()->setRespectSysLanguage(false);
$query->getQuerySettings()->setRespectStoragePage(false);
$query->getQuerySettings()->setLanguageOverlayMode(false);
$query->matching(
$query->logicalAnd(
[$query->equals('uid', $uid)]
)
)->setLimit(1);
$queryResult = $query->execute();
/** @var Stepname $foundRow */
$foundRow = null;
if ($queryResult->count() > 0) {
$foundRow = $queryResult->getFirst();
}
return $foundRow;
}
/**
* Check on double language entries
*
* @param Stepname $stepname The ratingname to look for
* @return array return values false says OK
*/
public function checkConsistency(Stepname $stepname): array
{
$query = $this->createQuery();
$query ->getQuerySettings()->setRespectSysLanguage(false);
$query ->matching(
$query->equals(self::STEPCONF_NAME, $stepname->getStepconf()->getUid())
);
$queryResult = $query
->execute(true);
$checkConsistency = [];
if (count($queryResult) > 1) {
$websiteLanguagesArray = [];
$allWebsiteLanguages = $this->getCurrentSite()->getAllLanguages();
/** @var \TYPO3\CMS\Core\Site\Entity\SiteLanguage $language */
foreach (array_values($allWebsiteLanguages) as $language) {
$websiteLanguagesArray[] = $language->getLanguageId();
}
$languageCounter = [];
foreach (array_values($queryResult) as $value) {
$languageUid = $value[$this->syslangUidLiteral];
$languageCounter[$languageUid]++;
if ($languageCounter[$languageUid] > 1) {
$checkConsistency['doubleLang'] = true;
}
//check if language flag exists in current website
if (($languageUid > 0) && in_array($languageUid, $websiteLanguagesArray, true)) {
$checkConsistency['existLang'] = true;
}
}
}
return $checkConsistency;
}
/**
* Finds the default language stepconf by giving ratingobject and steporder
*
* @param \WapplerSystems\BookmarksLikesRatings\Domain\Model\Stepname $stepname The ratingname to look for
* @throws InvalidQueryException
* @return Stepname|null The stepname in default language
* @var Stepname $foundRow
*/
public function findDefaultStepname(Stepname $stepname): ?Stepname
{
$foundRow = $this->objectManager->get(Stepname::class);
$query = $this->createQuery();
$query->getQuerySettings()->setRespectSysLanguage(false);
$query->getQuerySettings()->setLanguageOverlayMode(false);
$query->matching(
$query->logicalAnd(
[
$query->equals(self::STEPCONF_NAME, $stepname->getStepconf()),
$query->in($this->syslangUidLiteral, [0, -1])
]
)
)->setLimit(1);
/** @var QueryResultInterface $queryResult */
$queryResult = $query->execute();
/*
$queryParser = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class);
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($queryParser->convertQueryToDoctrineQueryBuilder($query)->getSQL(), get_class($this).' SQL');
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($queryParser->convertQueryToDoctrineQueryBuilder($query)->getParameters(), get_class($this).' SQL Parameter');
*/
/** @var Stepname $foundRow */
$foundRow = null;
if ($queryResult->count() > 0) {
$foundRow = $queryResult->getFirst();
}
return $foundRow;
}
/**
* Finds the localized ratingstep entry by giving ratingobjectUid
*
* @param Stepname $stepname The ratingname to look for
* @return bool true if stepconf having same steporder and _languageUid exists
*/
public function existStepname(Stepname $stepname): bool
{
$lookForStepname = $this->findStepnameObject($stepname);
return !is_null($lookForStepname);
}
/**
* Set default query settings to find ALL records
*/
public function clearQuerySettings(): void
{
$querySettings = $this->createQuery()->getQuerySettings();
$querySettings->setRespectSysLanguage(true);
$querySettings->setIgnoreEnableFields(true);
$querySettings->setLanguageOverlayMode(false);
$this->setDefaultQuerySettings($querySettings);
}
protected function getCurrentSite(): ?Site
{
if ($GLOBALS['TYPO3_REQUEST'] instanceof ServerRequestInterface
&& $GLOBALS['TYPO3_REQUEST']->getAttribute('site') instanceof Site) {
return $GLOBALS['TYPO3_REQUEST']->getAttribute('site');
}
return null;
}
}

View File

@@ -0,0 +1,105 @@
<?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\Repository;
use WapplerSystems\BookmarksLikesRatings\Domain\Model\Rating;
use WapplerSystems\BookmarksLikesRatings\Domain\Model\Vote;
use WapplerSystems\BookmarksLikesRatings\Domain\Model\Voter;
use TYPO3\CMS\Extbase\Persistence\Repository;
/**
* A repository for votes
*/
class VoteRepository extends Repository
{
/**
* Defines name for function parameter
*/
public const /** @noinspection PhpUnused */
ADD_IF_NOT_FOUND = true;
/**
* Initialize this repository
*/
/** @noinspection PhpUnused */
public function initializeObject(): void
{
}
/**
* Finds the voting by giving the rating and voter objects
*
* @param Rating|null $rating The concerned ratingobject
* @param Voter|null $voter The Uid of the rated row
* @return Vote|object The voting
*/
public function findMatchingRatingAndVoter($rating = null, $voter = null)
{
/** @var \TYPO3\CMS\Extbase\Persistence\QueryInterface $query */
$query = $this->createQuery();
return $query->matching(
$query->logicalAnd(
[
$query->equals('rating', $rating),
$query->equals('voter', $voter)
]
)
)->execute()->getFirst();
}
/**
* Counts all votings by giving the rating and ratingstep
*
* @param Rating $rating The concerned ratingobject
* @param \WapplerSystems\BookmarksLikesRatings\Domain\Model\Stepconf $stepconf The stepconf object
* @return int
*/
public function countByMatchingRatingAndVote($rating = null, $stepconf = null): int
{
$query = $this->createQuery();
$query->matching($query->logicalAnd(
[
$query->equals('rating', $rating->getUid()),
$query->equals('vote', $stepconf->getUid())
]
));
return count($query->execute());
}
/**
* Counts all anonymous votings by giving the rating and ratingstep
*
* @param Rating $rating The concerned ratingobject
* @param \WapplerSystems\BookmarksLikesRatings\Domain\Model\Stepconf $stepconf The stepconf object
* @param int $anonymousVoter UID of the anonymous account
* @return int
*/
public function countAnonymousByMatchingRatingAndVote($rating = null, $stepconf = null, $anonymousVoter = null): int
{
/** @var int $count */
$count = 0;
if ($anonymousVoter !== null) {
$query = $this->createQuery();
$query->matching(
$query->logicalAnd([
$query->equals('rating', $rating->getUid()),
$query->equals('vote', $stepconf->getUid()),
$query->equals('voter', $anonymousVoter),
])
);
$count = count($query->execute());
}
return $count;
}
}

View File

@@ -0,0 +1,31 @@
<?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\Repository;
use TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository;
use TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface;
/**
* A repository for votes
*/
class VoterRepository extends FrontendUserRepository
{
/**
* Initialze this repository
*/
public function initializeObject()
{
//Even hidden or deleted FE Users should be found
/** @var \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $querySettings */
$querySettings = $this->objectManager->get(QuerySettingsInterface::class);
$querySettings->setIgnoreEnableFields(true);
$this->setDefaultQuerySettings($querySettings);
}
}