src/Contact/Repository/DoctrineContactRepository.php line 162
<?phpdeclare(strict_types=1);/*** Copyright (c) 2019 TECLA Consulting Group oü.* All rights reserved.** This unpublished material is proprietary to TECLA Consulting Group oü.* All rights reserved. The methods and* techniques described herein are considered trade secrets* and/or confidential. Reproduction or distribution, in whole* or in part, is forbidden except by express written permission* of TECLA Consulting Group oü.** @author Matúš Sýkorjak <matus@tecla.no>* @copyright 2019 TECLA Consulting Group oü*/namespace App\Contact\Repository;use App\Contact\Doctrine\DBAL\Types\ContactIdType;use App\Contact\Doctrine\DBAL\Types\GenderType;use App\Contact\Doctrine\DBAL\Types\GroupIdType;use App\Contact\Doctrine\DBAL\Types\IndustrySectorType;use App\Contact\Doctrine\DBAL\Types\MaritalStatusType;use App\Contact\Doctrine\DBAL\Types\WorkOccupationType;use App\Contact\Model\Contact;use App\Contact\Model\ContactAddressInterface;use App\Contact\Model\ContactInterface;use App\Contact\Model\ContactPermissionInterface;use App\Contact\Model\ContactRelationshipCustomerSupplierInterface;use App\Contact\Model\GroupItemInterface;use App\Contact\Model\Identity\ContactIdInterface;use App\Contact\Model\Relationship\AccountantInterface;use App\Contact\Model\Relationship\AuthorizedAccountantInterface;use App\Contact\Model\Relationship\ManagingDirectorInterface;use App\Contact\Model\Relationship\EmploymentInterface;use App\Invoice\Model\InvoicePaymentNotificationInterface;use App\Note\Model\NoteInterface;use App\Note\Model\NotePermissionInterface;use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;use Doctrine\ORM\Query\Expr\Join;use Doctrine\ORM\QueryBuilder;use Doctrine\Persistence\ManagerRegistry;class DoctrineContactRepository extends ServiceEntityRepository{public function __construct(ManagerRegistry $registry){parent::__construct($registry, Contact::class);}/*** Returns contact with $authorizeId if contact with $id has permission to access it.*/public function findOneAuthorizedById(ContactIdInterface $contactId, ContactIdInterface $authorizedContactId): ?ContactInterface{$qb = $this->createQueryBuilder('c');return $qb->leftJoin(ContactPermissionInterface::class, 'cp', Join::WITH, $qb->expr()->eq('cp.contact', 'c.id'))->andWhere('c.id = :authorizedId')->andWhere($qb->expr()->eq('cp.authorizedContact', ':id'))->addGroupBy('c.id')->setParameter('id', $contactId, ContactIdType::NAME)->setParameter('authorizedId', $authorizedContactId, ContactIdType::NAME)->getQuery()->getOneOrNullResult();}/*** Returns all contacts for what is contact with given ID authorized.** @param ?int $limit* @param ?int $offset** @return ContactInterface[]|null*/public function findAllAuthorized(ContactIdInterface $id, array $criteria = null, array $orderBy = null, ?int $limit = null, ?int $offset = null): ?array{return $this->createQueryBuilderAllAuthorized(false, $id, $criteria, $orderBy, $limit, $offset)->getQuery()->getResult();}/*** Counts all contacts for what is contact with given ID authorized.*/public function countAllAuthorized(ContactIdInterface $id, array $criteria = null): int{return (int) ($this->createQueryBuilderAllAuthorized(true, $id, $criteria)->getQuery()->getSingleScalarResult());}/*** Returns Contact of person type with given unique information.*/public function findOnePersonByUniqueInformation(string $email): ?ContactInterface{$qb = $this->createQueryBuilder('c');return $qb->where('c.type = :type')->andWhere('c.email = :email')->setParameter('type', ContactInterface::TYPE_PERSON)->setParameter('email', $email)->getQuery()->getOneOrNullResult();}/*** Returns Contact of company type with given unique information.*/public function findOneCompanyByUniqueInformation(string $registrationNumber, string $country): ?ContactInterface{$qb = $this->createQueryBuilder('c');return $qb->where('c.type = :type')->andWhere('c.registrationNumber = :registrationNumber')->andWhere('c.country = :country')->setParameter('type', ContactInterface::TYPE_COMPANY)->setParameter('registrationNumber', $registrationNumber)->setParameter('country', $country)->getQuery()->getOneOrNullResult();}public function createAddContactAwareQueryBuilder(ContactIdInterface $authorizedContactId): QueryBuilder{$qb = $this->createQueryBuilder('c');return $qb->innerJoin(ContactPermissionInterface::class, 'cp', Join::WITH, $qb->expr()->eq('c.id', 'cp.contact'))->where($qb->expr()->eq('cp.authorizedContact', ':id'))->setParameter('id', $authorizedContactId, ContactIdType::NAME);}public function createAddContactAccessAccessorQueryBuilder(ContactIdInterface $authorizedContactId){$qb = $this->createQueryBuilder('c');return $qb->innerJoin(ContactPermissionInterface::class, 'cp', Join::WITH, $qb->expr()->eq('c.id', 'cp.contact'))->where($qb->expr()->eq('cp.authorizedContact', ':id'))->andWhere($qb->expr()->eq('c.type', ':personType'))->setParameter('id', $authorizedContactId, ContactIdType::NAME)->setParameter('personType', ContactInterface::TYPE_PERSON);}/*** Create query builder for all contacts for what is contact with given ID authorized.** @param ?int $limit* @param ?int $offset*/private function createQueryBuilderAllAuthorized(bool $isCount, ContactIdInterface $id, array $criteria = null, array $orderBy = null, ?int $limit = null, ?int $offset = null): QueryBuilder{$addFullNameQuery = false;$addContactEmailQuery = false;$addGroupQuery = false;$addManagingDirectorQuery = false;$addAccountantQuery = false;$addAuthorizedAccountantQuery = false;$addEmploymentRelationshipQuery = false;$addRelationshipCustomerSupplierQuery = false;$addNotificationInvoicePaymentQuery = false;$addAddressQuery = false;$addNoteTitleQuery = false;$qb = $this->createQueryBuilder('c');$groupsWhereExpr = $qb->expr()->orX();$managingDirectorsWhereExpr = $qb->expr()->orX();$accountantsWhereExpr = $qb->expr()->orX();$authorizedAccountantsWhereExpr = $qb->expr()->orX();if (true === $isCount) {$qb->select($qb->expr()->countDistinct('c.id'));} else {$qb->groupBy('c.id');if (null !== $limit) {$qb->setMaxResults($limit);}if (null !== $offset) {$qb->setFirstResult($offset);}$addContactEmailQuery = true;}if (false === empty($criteria)) {foreach ($criteria as $criterion => $values) {if (true === empty($values)) {continue;}if ('search' === $criterion) {$searchTokens = array_filter(explode(' ', $values), function (mixed $value) {return ' ' !== $value;});$expression = $qb->expr()->orX($qb->expr()->like('c.name', ':search'),$qb->expr()->like('c.oldName', ':search'),$qb->expr()->like('c.phone', ':search'),$qb->expr()->like('c.secondaryPhone', ':search'),$qb->expr()->like('ce.email', ':search'),$qb->expr()->like('ce.secondaryEmail', ':search'),$qb->expr()->like('c.registrationNumber', ':search'),$qb->expr()->like('c.idNumber', ':search'),$qb->expr()->like('c.norwegianIdNumber', ':search'),$qb->expr()->like('c.norwegianTemporaryIdNumber', ':search'),);$nameExpression = $qb->expr()->andX();foreach ($searchTokens as $key => $searchToken) {$paramKey = 'search_' . $key;$nameExpression->add($qb->expr()->orX($qb->expr()->like('c.firstName', ':' . $paramKey),$qb->expr()->like('c.lastName', ':' . $paramKey)));$qb->setParameter($paramKey, '%' . $searchToken . '%');}$expression->add($nameExpression);if (true === $isCount) {$qb->andWhere($expression);} else {$qb->having($expression);}$qb->setParameter('search', '%' . $values . '%');$addFullNameQuery = true;$addContactEmailQuery = true;}if ('type' === $criterion) {if (true === \is_array($values)) {$qb->andWhere($qb->expr()->in('c.type', ':type'));} else {$qb->andWhere($qb->expr()->eq('c.type', ':type'));}$qb->setParameter('type', $values);}if ('relationshipCustomerSupplier' === $criterion) {$addRelationshipCustomerSupplierQuery = true;}if ('employmentRelationship' === $criterion) {$addEmploymentRelationshipQuery = true;}if ('notificationInvoicePayment' === $criterion) {$addNotificationInvoicePaymentQuery = true;}if ('forSale' === $criterion) {$qb->andWhere($qb->expr()->eq('c.forSale', ':forSale'));$qb->setParameter('forSale', true);}if ('country' === $criterion) {$countryWhereExpr = $qb->expr()->orX();foreach ($values as $key => $value) {$paramKey = 'country_' . $key;$countryWhereExpr->add($qb->expr()->eq('ca.country', ':' . $paramKey));$qb->setParameter($paramKey, $value);}$qb->andWhere($countryWhereExpr);$addAddressQuery = true;}if ('gender' === $criterion) {$genderWhereExpr = $qb->expr()->orX();foreach ($values as $key => $value) {$paramKey = 'gender_' . $key;$genderWhereExpr->add($qb->expr()->eq('c.gender', ':' . $paramKey));$qb->setParameter($paramKey, $value, GenderType::NAME);}$qb->andWhere($genderWhereExpr);}if ('group' === $criterion) {foreach ($values as $key => $value) {$paramKey = 'groupId_' . $key;$groupsWhereExpr->add($qb->expr()->eq('cgi.group', ':' . $paramKey));$qb->setParameter($paramKey, $value, GroupIdType::NAME);}$qb->andWhere($groupsWhereExpr);$addGroupQuery = true;}if ('managingDirector' === $criterion) {foreach ($values as $key => $value) {$paramKey = 'managingDirectorId_' . $key;$managingDirectorsWhereExpr->add($qb->expr()->eq('cr_md.managingDirector', ':' . $paramKey));$qb->setParameter($paramKey, $value, ContactIdType::NAME);}$qb->andWhere($managingDirectorsWhereExpr);$addManagingDirectorQuery = true;}if ('accountant' === $criterion) {foreach ($values as $key => $value) {$paramKey = 'accountantId_' . $key;$accountantsWhereExpr->add($qb->expr()->eq('cr_a.accountant', ':' . $paramKey));$qb->setParameter($paramKey, $value, ContactIdType::NAME);}$qb->andWhere($accountantsWhereExpr);$addAccountantQuery = true;}if ('authorizedAccountant' === $criterion) {foreach ($values as $key => $value) {$paramKey = 'authorizedAccountantId_' . $key;$authorizedAccountantsWhereExpr->add($qb->expr()->eq('cr_aa.authorizedAccountant', ':' . $paramKey));$qb->setParameter($paramKey, $value, ContactIdType::NAME);}$qb->andWhere($authorizedAccountantsWhereExpr);$addAuthorizedAccountantQuery = true;}if ('language' === $criterion) {$languageWhereExpr = $qb->expr()->orX();foreach ($values as $key => $value) {$paramKey = 'language_' . $key;$languageWhereExpr->add($qb->expr()->eq('c.language', ':' . $paramKey));$qb->setParameter($paramKey, $value);}$qb->andWhere($languageWhereExpr);}if ('workOccupation' === $criterion) {$workOccupationWhereExpr = $qb->expr()->orX();foreach ($values as $key => $value) {$paramKey = 'workOccupationId_' . $key;$workOccupationWhereExpr->add($qb->expr()->eq('c.workOccupation', ':' . $paramKey));$qb->setParameter($paramKey, $value, WorkOccupationType::NAME);}$qb->andWhere($workOccupationWhereExpr);}if ('industrySector' === $criterion) {$industrySectorWhereExpr = $qb->expr()->orX();foreach ($values as $key => $value) {$paramKey = 'industrySector_' . $key;$industrySectorWhereExpr->add($qb->expr()->eq('c.industrySector', ':' . $paramKey));$qb->setParameter($paramKey, $value, IndustrySectorType::NAME);}$qb->andWhere($industrySectorWhereExpr);}if ('maritalStatus' === $criterion) {$maritalStatusWhereExpr = $qb->expr()->orX();foreach ($values as $key => $value) {$paramKey = 'maritalStatus_' . $key;$maritalStatusWhereExpr->add($qb->expr()->eq('c.maritalStatus', ':' . $paramKey));$qb->setParameter($paramKey, $value, MaritalStatusType::NAME);}$qb->andWhere($maritalStatusWhereExpr);}if ('nationality' === $criterion) {$nationalityWhereExpr = $qb->expr()->orX();foreach ($values as $key => $value) {$paramKey = 'nationality_' . $key;$nationalityWhereExpr->add($qb->expr()->eq('c.nationality', ':' . $paramKey));$qb->setParameter($paramKey, $value);}$qb->andWhere($nationalityWhereExpr);}if ('vatActivity' === $criterion) {if (true === $values) {$qb->andWhere($qb->expr()->isNotNull('c.vatin'));}}if ('vatActivityNone' === $criterion) {if (true === $values) {$qb->andWhere($qb->expr()->isNull('c.vatin'));}}if ('noteTitle' === $criterion) {$noteTitleWhereExpr = $qb->expr()->orX();foreach ($values as $key => $value) {$paramKey = 'note_title_' . $key;$noteTitleWhereExpr->add($qb->expr()->eq('n.title', ':' . $paramKey));$qb->setParameter($paramKey, $value);}$qb->andWhere($noteTitleWhereExpr);$addNoteTitleQuery = true;}}}if (false === $isCount) {if (false === empty($orderBy)) {foreach ($orderBy as $column => $direction) {if ('fullName' === $column) {$qb->addOrderBy('fullName', $direction);$addFullNameQuery = true;}if ('phone' === $column) {$qb->addOrderBy('c.phone', $direction);}if ('email' === $column) {$qb->addOrderBy('ce.email', $direction);$addContactEmailQuery = true;}}}}if (true === $addFullNameQuery && false === $isCount) {$qb->addSelect("CASE WHEN c.type = 1 THEN c.name WHEN c.type = 2 THEN CONCAT(c.lastName, ' ', c.firstName) ELSE '' END AS HIDDEN fullName");}if (true === $addContactEmailQuery) {if (false === $isCount) {$qb->addSelect('ce');}$qb->innerJoin('c.contactEmail', 'ce', Join::WITH, $qb->expr()->eq('c.contactEmail', 'ce.id'));}$qb->innerJoin(ContactPermissionInterface::class, 'cp', Join::WITH, $qb->expr()->andX($qb->expr()->eq('cp.contact', 'c.id'),$qb->expr()->eq('cp.authorizedContact', ':id')));if (true === $addAddressQuery) {$qb->innerJoin(ContactAddressInterface::class, 'ca', Join::WITH, $qb->expr()->andX($qb->expr()->eq('ca.id', 'c.contactAddress')));}if (true === $addGroupQuery) {$qb->innerJoin(GroupItemInterface::class, 'cgi', Join::WITH, $qb->expr()->andX($qb->expr()->eq('cgi.contact', 'c.id')));}if (true === $addManagingDirectorQuery) {$qb->innerJoin(ManagingDirectorInterface::class, 'cr_md', Join::WITH, $qb->expr()->andX($qb->expr()->eq('cr_md.contact', 'c.id')));}if (true === $addAccountantQuery) {$qb->innerJoin(AccountantInterface::class, 'cr_a', Join::WITH, $qb->expr()->andX($qb->expr()->eq('cr_a.contact', 'c.id')));}if (true === $addAuthorizedAccountantQuery) {$qb->innerJoin(AuthorizedAccountantInterface::class, 'cr_aa', Join::WITH, $qb->expr()->andX($qb->expr()->eq('cr_aa.contact', 'c.id')));}if (true === $addEmploymentRelationshipQuery) {$qb->innerJoin(EmploymentInterface::class, 'cer', Join::WITH, $qb->expr()->andX($qb->expr()->eq('cer.employer', 'cp.authorizedContact'),$qb->expr()->eq('cer.employee', 'cp.contact')));}if (true === $addRelationshipCustomerSupplierQuery) {$qb->innerJoin(ContactRelationshipCustomerSupplierInterface::class, 'ccr', Join::WITH, $qb->expr()->andX($qb->expr()->eq('ccr.supplier', 'cp.authorizedContact'),$qb->expr()->eq('ccr.customer', 'cp.contact')));}if (true === $addNotificationInvoicePaymentQuery) {$qb->innerJoin(InvoicePaymentNotificationInterface::class, 'ipn', Join::WITH, $qb->expr()->andX($qb->expr()->eq('ipn.supplier', ':id'),$qb->expr()->eq('ipn.customer', 'c.id')));}if (true === $addNoteTitleQuery) {$qb->innerJoin(NoteInterface::class, 'n', Join::WITH, $qb->expr()->andX($qb->expr()->eq('n.contact', 'c.id')))->innerJoin(NotePermissionInterface::class, 'np', Join::WITH, $qb->expr()->andX($qb->expr()->eq('n.id', 'np.note'),$qb->expr()->eq('np.contact', ':id')));}$qb->setParameter('id', $id, ContactIdType::NAME);return $qb;}}