|
12 | 12 | use In2code\Lux\Domain\Model\Visitor; |
13 | 13 | use In2code\Lux\Domain\Service\BranchService; |
14 | 14 | use In2code\Lux\Domain\Service\CountryService; |
| 15 | +use In2code\Lux\Utility\ArrayUtility; |
15 | 16 | use In2code\Lux\Utility\DatabaseUtility; |
16 | 17 | use In2code\Lux\Utility\DateUtility; |
| 18 | +use In2code\Lux\Utility\StringUtility; |
17 | 19 | use TYPO3\CMS\Core\Utility\GeneralUtility; |
| 20 | +use TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException; |
18 | 21 | use TYPO3\CMS\Extbase\Persistence\QueryInterface; |
| 22 | +use TYPO3\CMS\Extbase\Persistence\QueryResultInterface; |
19 | 23 |
|
20 | 24 | class CompanyRepository extends AbstractRepository |
21 | 25 | { |
@@ -83,6 +87,36 @@ public function findAmountByFilter(FilterDto $filter): int |
83 | 87 | return (int)$connection->executeQuery($sql)->fetchOne(); |
84 | 88 | } |
85 | 89 |
|
| 90 | + /** |
| 91 | + * Find all companies by a given property. Property paths are allowed (e.g. "category.title") |
| 92 | + * |
| 93 | + * @param string $propertyName |
| 94 | + * @param string $propertyValue |
| 95 | + * @param bool $exactMatch |
| 96 | + * @param array $orderings |
| 97 | + * @param int $limit |
| 98 | + * @return QueryResultInterface |
| 99 | + * @throws InvalidQueryException |
| 100 | + */ |
| 101 | + public function findAllByProperty( |
| 102 | + string $propertyName, |
| 103 | + string $propertyValue, |
| 104 | + bool $exactMatch, |
| 105 | + array $orderings = ['uid' => 'DESC'], |
| 106 | + int $limit = 1000 |
| 107 | + ): QueryResultInterface { |
| 108 | + $query = $this->createQuery(); |
| 109 | + $propertyName = StringUtility::cleanString($propertyName, false, '._-'); |
| 110 | + $constraint = $query->equals($propertyName, $propertyValue); |
| 111 | + if ($exactMatch === false) { |
| 112 | + $constraint = $query->like($propertyName, '%' . $propertyValue . '%'); |
| 113 | + } |
| 114 | + $query->matching($constraint); |
| 115 | + $query->setOrderings(ArrayUtility::cleanStringForArrayKeys($orderings)); |
| 116 | + $query->setLimit($limit); |
| 117 | + return $query->execute(); |
| 118 | + } |
| 119 | + |
86 | 120 | public function findByTitleAndDomain(string $title, string $domain): ?Company |
87 | 121 | { |
88 | 122 | $query = $this->createQuery(); |
@@ -277,12 +311,16 @@ public function canCompanyBeReadBySites(Company $company, array $sites): bool |
277 | 311 | */ |
278 | 312 | public function removeCompany(Company $company, bool $removeVisitors): void |
279 | 313 | { |
280 | | - if ($removeVisitors) { |
281 | | - $visitorRepository = GeneralUtility::makeInstance(VisitorRepository::class); |
282 | | - foreach ($company->getVisitors() as $visitor) { |
283 | | - $visitorRepository->removeVisitor($visitor); |
| 314 | + $visitorRepository = GeneralUtility::makeInstance(VisitorRepository::class); |
| 315 | + if ($removeVisitors === true) { |
| 316 | + foreach ($visitorRepository->findAllUidsByCompany($company) as $visitorUid) { |
| 317 | + $visitor = $visitorRepository->findByUid($visitorUid); |
| 318 | + if ($visitor !== null) { |
| 319 | + $visitorRepository->removeVisitor($visitor); |
| 320 | + } |
284 | 321 | } |
285 | 322 | } |
| 323 | + $visitorRepository->removeCompanyRelation($company); |
286 | 324 |
|
287 | 325 | $connection = DatabaseUtility::getConnectionForTable(Company::TABLE_NAME); |
288 | 326 | $connection->executeQuery('delete from ' . Company::TABLE_NAME . ' where uid=' . (int)$company->getUid()); |
|
0 commit comments