|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace MauticPlugin\CustomObjectsBundle\Helper; |
| 6 | + |
| 7 | +use Doctrine\DBAL\Connection; |
| 8 | +use Mautic\EmailBundle\EventListener\MatchFilterForLeadTrait; |
| 9 | +use Mautic\LeadBundle\Entity\CompanyRepository; |
| 10 | +use Mautic\LeadBundle\Entity\LeadListRepository; |
| 11 | +use MauticPlugin\CustomObjectsBundle\DTO\TableConfig; |
| 12 | +use MauticPlugin\CustomObjectsBundle\Entity\CustomItem; |
| 13 | +use MauticPlugin\CustomObjectsBundle\Entity\CustomObject; |
| 14 | +use MauticPlugin\CustomObjectsBundle\Exception\InvalidCustomObjectFormatListException; |
| 15 | +use MauticPlugin\CustomObjectsBundle\Exception\NotFoundException; |
| 16 | +use MauticPlugin\CustomObjectsBundle\Model\CustomFieldModel; |
| 17 | +use MauticPlugin\CustomObjectsBundle\Model\CustomItemModel; |
| 18 | +use MauticPlugin\CustomObjectsBundle\Model\CustomObjectModel; |
| 19 | +use MauticPlugin\CustomObjectsBundle\Polyfill\EventListener\MatchFilterForLeadTrait as MatchFilterForLeadTraitPolyfill; |
| 20 | + |
| 21 | +if (method_exists(MatchFilterForLeadTrait::class, 'transformFilterDataForLead')) { |
| 22 | + class_alias(MatchFilterForLeadTrait::class, '\MauticPlugin\CustomObjectsBundle\Helper\MatchFilterForLeadTraitAlias'); |
| 23 | +} else { |
| 24 | + class_alias(MatchFilterForLeadTraitPolyfill::class, '\MauticPlugin\CustomObjectsBundle\Helper\MatchFilterForLeadTraitAlias'); |
| 25 | +} |
| 26 | + |
| 27 | +class ContactFilterMatcher |
| 28 | +{ |
| 29 | + use MatchFilterForLeadTraitAlias { |
| 30 | + transformFilterDataForLead as transformFilterDataForLeadAlias; |
| 31 | + } |
| 32 | + |
| 33 | + private CustomFieldModel $customFieldModel; |
| 34 | + private CustomObjectModel $customObjectModel; |
| 35 | + private CustomItemModel $customItemModel; |
| 36 | + private CompanyRepository $companyRepository; |
| 37 | + private Connection $connection; |
| 38 | + private int $leadCustomItemFetchLimit; |
| 39 | + |
| 40 | + public function __construct( |
| 41 | + CustomFieldModel $customFieldModel, |
| 42 | + CustomObjectModel $customObjectModel, |
| 43 | + CustomItemModel $customItemModel, |
| 44 | + LeadListRepository $segmentRepository, |
| 45 | + CompanyRepository $companyRepository, |
| 46 | + Connection $connection, |
| 47 | + int $leadCustomItemFetchLimit |
| 48 | + ) { |
| 49 | + $this->customFieldModel = $customFieldModel; |
| 50 | + $this->customObjectModel = $customObjectModel; |
| 51 | + $this->customItemModel = $customItemModel; |
| 52 | + $this->segmentRepository = $segmentRepository; |
| 53 | + $this->companyRepository = $companyRepository; |
| 54 | + $this->connection = $connection; |
| 55 | + $this->leadCustomItemFetchLimit = $leadCustomItemFetchLimit; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @param mixed[] $filters |
| 60 | + * @param mixed[] $lead |
| 61 | + */ |
| 62 | + public function match(array $filters, array $lead, bool &$hasCustomFields = false): bool |
| 63 | + { |
| 64 | + $leadId = (string) $lead['id']; |
| 65 | + $customFieldValues = $this->getCustomFieldDataForLead($filters, $leadId); |
| 66 | + |
| 67 | + if (!$customFieldValues) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + $hasCustomFields = true; |
| 72 | + $lead = array_merge($lead, $customFieldValues); |
| 73 | + |
| 74 | + if (!isset($lead['companies']) && $this->doFiltersContainCompanyFilter($filters)) { |
| 75 | + $lead['companies'] = $this->companyRepository->getCompaniesByLeadId($leadId); |
| 76 | + } |
| 77 | + |
| 78 | + if (!isset($lead['tags']) && $this->doFiltersContainTagsFilter($filters)) { |
| 79 | + $lead['tags'] = $this->getTagIdsByLeadId($leadId); |
| 80 | + } |
| 81 | + |
| 82 | + return $this->matchFilterForLead($filters, $lead); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @param mixed[] $filters |
| 87 | + * |
| 88 | + * @return mixed[] |
| 89 | + */ |
| 90 | + private function getCustomFieldDataForLead(array $filters, string $leadId): array |
| 91 | + { |
| 92 | + $customFieldValues = $cachedCustomItems = []; |
| 93 | + |
| 94 | + foreach ($filters as $condition) { |
| 95 | + try { |
| 96 | + if ('custom_object' !== $condition['object']) { |
| 97 | + continue; |
| 98 | + } |
| 99 | + |
| 100 | + if ('cmf_' === substr($condition['field'], 0, 4)) { |
| 101 | + $customField = $this->customFieldModel->fetchEntity( |
| 102 | + (int) explode('cmf_', $condition['field'])[1] |
| 103 | + ); |
| 104 | + $customObject = $customField->getCustomObject(); |
| 105 | + $fieldAlias = $customField->getAlias(); |
| 106 | + } elseif ('cmo_' === substr($condition['field'], 0, 4)) { |
| 107 | + $customObject = $this->customObjectModel->fetchEntity( |
| 108 | + (int) explode('cmo_', $condition['field'])[1] |
| 109 | + ); |
| 110 | + $fieldAlias = 'name'; |
| 111 | + } else { |
| 112 | + continue; |
| 113 | + } |
| 114 | + |
| 115 | + $key = $customObject->getId().'-'.$leadId; |
| 116 | + if (!isset($cachedCustomItems[$key])) { |
| 117 | + $cachedCustomItems[$key] = $this->getCustomItems($customObject, $leadId); |
| 118 | + } |
| 119 | + |
| 120 | + $result = $this->getCustomFieldValue($customObject, $fieldAlias, $cachedCustomItems[$key]); |
| 121 | + |
| 122 | + $customFieldValues[$condition['field']] = $result; |
| 123 | + } catch (NotFoundException|InvalidCustomObjectFormatListException $e) { |
| 124 | + continue; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return $customFieldValues; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @param mixed[] $customItems |
| 133 | + * |
| 134 | + * @return mixed[] |
| 135 | + */ |
| 136 | + private function getCustomFieldValue( |
| 137 | + CustomObject $customObject, |
| 138 | + string $customFieldAlias, |
| 139 | + array $customItems |
| 140 | + ): array { |
| 141 | + $fieldValues = []; |
| 142 | + |
| 143 | + foreach ($customItems as $customItemData) { |
| 144 | + // Name is known from the CI data array. |
| 145 | + if ('name' === $customFieldAlias) { |
| 146 | + $fieldValues[] = $customItemData['name']; |
| 147 | + |
| 148 | + continue; |
| 149 | + } |
| 150 | + |
| 151 | + // Custom Field values are handled like this. |
| 152 | + $customItem = new CustomItem($customObject); |
| 153 | + $customItem->populateFromArray($customItemData); |
| 154 | + $customItem = $this->customItemModel->populateCustomFields($customItem); |
| 155 | + |
| 156 | + try { |
| 157 | + $fieldValue = $customItem->findCustomFieldValueForFieldAlias($customFieldAlias); |
| 158 | + // If the CO item doesn't have a value, get the default value |
| 159 | + if (empty($fieldValue->getValue())) { |
| 160 | + $fieldValue->setValue($fieldValue->getCustomField()->getDefaultValue()); |
| 161 | + } |
| 162 | + |
| 163 | + if (in_array($fieldValue->getCustomField()->getType(), ['multiselect', 'select'])) { |
| 164 | + $fieldValues[] = $fieldValue->getValue(); |
| 165 | + } else { |
| 166 | + $fieldValues[] = $fieldValue->getCustomField()->getTypeObject()->valueToString($fieldValue); |
| 167 | + } |
| 168 | + } catch (NotFoundException $e) { |
| 169 | + // Custom field not found. |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + return $fieldValues; |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * @return array<mixed> |
| 178 | + */ |
| 179 | + private function getCustomItems(CustomObject $customObject, string $leadId): array |
| 180 | + { |
| 181 | + $orderBy = CustomItem::TABLE_ALIAS.'.id'; |
| 182 | + $orderDir = 'DESC'; |
| 183 | + |
| 184 | + $tableConfig = new TableConfig($this->leadCustomItemFetchLimit, 1, $orderBy, $orderDir); |
| 185 | + $tableConfig->addParameter('customObjectId', $customObject->getId()); |
| 186 | + $tableConfig->addParameter('filterEntityType', 'contact'); |
| 187 | + $tableConfig->addParameter('filterEntityId', $leadId); |
| 188 | + |
| 189 | + return $this->customItemModel->getArrayTableData($tableConfig); |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * @param mixed[] $data |
| 194 | + * @param mixed[] $lead |
| 195 | + * |
| 196 | + * @return ?mixed[] |
| 197 | + */ |
| 198 | + private function transformFilterDataForLead(array $data, array $lead): ?array |
| 199 | + { |
| 200 | + if ('custom_object' === $data['object']) { |
| 201 | + return $lead[$data['field']]; |
| 202 | + } |
| 203 | + |
| 204 | + return $this->transformFilterDataForLeadAlias($data, $lead); |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * @return string[] |
| 209 | + */ |
| 210 | + public function getTagIdsByLeadId(string $leadId): array |
| 211 | + { |
| 212 | + return $this->connection->createQueryBuilder() |
| 213 | + ->select('tag_id') |
| 214 | + ->from(MAUTIC_TABLE_PREFIX.'lead_tags_xref', 'x') |
| 215 | + ->where('x.lead_id = :leadId') |
| 216 | + ->setParameter('leadId', $leadId) |
| 217 | + ->execute() |
| 218 | + ->fetchFirstColumn(); |
| 219 | + } |
| 220 | +} |
0 commit comments