where([ [ 'name' => $numberList, 'hash' => null, ] ]) ->find(); $ids = []; $exist = []; foreach ($phoneNumbers as $phoneNumber) { $ids[] = $phoneNumber->id; $exist[] = $phoneNumber->get('name'); } foreach ($numberList as $number) { $number = trim($number); if (empty($number)) { continue; } if (!in_array($number, $exist)) { $phoneNumber = $this->get(); $phoneNumber->set('name', $number); $this->save($phoneNumber); $ids[] = $phoneNumber->id; } } } return $ids; } public function getPhoneNumberData(Entity $entity): array { $dataList = []; $numberList = $this ->select(['name', 'type', 'invalid', 'optOut', ['en.primary', 'primary']]) ->join( 'EntityPhoneNumber', 'en', [ 'en.phoneNumberId:' => 'id', ] ) ->where([ 'en.entityId' => $entity->id, 'en.entityType' => $entity->getEntityType(), 'en.deleted' => false, ]) ->order('en.primary', true) ->find(); foreach ($numberList as $number) { $item = (object) [ 'phoneNumber' => $number->get('name'), 'type' => $number->get('type'), 'primary' => $number->get('primary'), 'optOut' => $number->get('optOut'), 'invalid' => $number->get('invalid'), ]; $dataList[] = $item; } return $dataList; } public function getByNumber(string $number): ?PhoneNumberEntity { return $this->where(['name' => $number])->findOne(); } public function getEntityListByPhoneNumberId(string $phoneNumberId, ?Entity $exceptionEntity = null): array { $entityList = []; $where = [ 'phoneNumberId' => $phoneNumberId, ]; if ($exceptionEntity) { $where[] = [ 'OR' => [ 'entityType!=' => $exceptionEntity->getEntityType(), 'entityId!=' => $exceptionEntity->id, ] ]; } $itemList = $this->entityManager ->getRepository('EntityPhoneNumber') ->sth() ->select(['entityType', 'entityId']) ->where($where) ->find(); foreach ($itemList as $item) { $itemEntityType = $item->get('entityType'); $itemEntityId = $item->get('entityId'); if (!$itemEntityType || !$itemEntityId) { continue; } if (!$this->entityManager->hasRepository($itemEntityType)) { continue; } $entity = $this->entityManager->getEntity($itemEntityType, $itemEntityId); if (!$entity) { continue; } $entityList[] = $entity; } return $entityList; } public function getEntityByPhoneNumberId(string $phoneNumberId, ?string $entityType = null): ?Entity { $where = [ 'phoneNumberId' => $phoneNumberId, ]; if ($entityType) { $where[] = ['entityType' => $entityType]; } $itemList = $this->entityManager->getRepository('EntityPhoneNumber') ->sth() ->select(['entityType', 'entityId']) ->where($where) ->limit(0, 20) ->order([ ['primary', 'DESC'], ['LIST:entityType:User,Contact,Lead,Account'], ]) ->find(); foreach ($itemList as $item) { $itemEntityType = $item->get('entityType'); $itemEntityId = $item->get('entityId'); if (!$itemEntityType || !$itemEntityId) { continue; } if (!$this->entityManager->hasRepository($itemEntityType)) { continue; } $entity = $this->entityManager->getEntity($itemEntityType, $itemEntityId); if ($entity) { if ($entity->getEntityType() === 'User') { if (!$entity->get('isActive')) { continue; } } return $entity; } } return null; } protected function beforeSave(Entity $entity, array $options = []) { parent::beforeSave($entity, $options); if ($entity->has('name')) { $number = $entity->get('name'); if (is_string($number) && strpos($number, self::ERASED_PREFIX) !== 0) { $numeric = preg_replace('/[^0-9]/', '', $number); } else { $numeric = null; } $entity->set('numeric', $numeric); } } public function markNumberOptedOut(string $number, bool $isOptedOut = true): void { $phoneNumber = $this->getByNumber($number); if (!$phoneNumber) { return; } $phoneNumber->set('optOut', $isOptedOut); $this->save($phoneNumber); } }