/** * Registers the phone number provider for the given class * * @param string $className * @param PhoneProviderInterface $provider */ public function addPhoneProvider($className, PhoneProviderInterface $provider) { if ($provider instanceof RootPhoneProviderAwareInterface) { $provider->setRootProvider($this); } $this->phoneProviders[$className][] = $provider; }
/** * Process form * * @param Call $entity * * @return bool True on successful processing, false otherwise */ public function process(Call $entity) { $targetEntityClass = $this->request->get('entityClass'); $targetEntityId = $this->request->get('entityId'); $options = []; if ($targetEntityClass && $this->request->getMethod() === 'GET') { $targetEntity = $this->entityRoutingHelper->getEntity($targetEntityClass, $targetEntityId); if (!$entity->getId()) { $entity->setPhoneNumber($this->phoneProvider->getPhoneNumber($targetEntity)); } $options = ['phone_suggestions' => array_unique(array_map(function ($item) { return $item[0]; }, $this->phoneProvider->getPhoneNumbers($targetEntity)))]; } $this->form = $this->formFactory->createNamed($this->formName, $this->formType, $entity, $options); $this->form->setData($entity); if (in_array($this->request->getMethod(), array('POST', 'PUT'))) { $this->form->submit($this->request); if ($this->form->isValid()) { if ($targetEntityClass) { $targetEntity = $this->entityRoutingHelper->getEntity($targetEntityClass, $targetEntityId); $this->callActivityManager->addAssociation($entity, $targetEntity); $phones = $this->phoneProvider->getPhoneNumbers($targetEntity); foreach ($phones as $phone) { if ($entity->getPhoneNumber() === $phone[0]) { $this->callActivityManager->addAssociation($entity, $phone[1]); } } } $this->onSuccess($entity); return true; } } return false; }
/** * Gets a list of all phone numbers available for the given Customer object * * @param Customer $object * * @return array of [phone number, phone owner] */ public function getPhoneNumbers($object) { $contact = $object->getContact(); if (!$contact) { return []; } return $this->rootProvider->getPhoneNumbers($contact); }
/** * Gets a list of all phone numbers available for the given Account object * * @param Account $object * * @return array of [phone number, phone owner] */ public function getPhoneNumbers($object) { $defaultContact = $object->getDefaultContact(); $result = $defaultContact ? $this->rootProvider->getPhoneNumbers($defaultContact) : []; foreach ($object->getContacts() as $contact) { if ($contact !== $defaultContact) { $result = array_merge($result, $this->rootProvider->getPhoneNumbers($contact)); } } return $result; }
/** * Process form * * @param Call $entity * * @return bool True on successful processing, false otherwise */ public function process(Call $entity) { $targetEntityClass = $this->request->get('entityClass'); $targetEntityId = $this->request->get('entityId'); $options = []; if ($targetEntityClass && $this->request->getMethod() === 'GET') { $targetEntity = $this->entityRoutingHelper->getEntity($targetEntityClass, $targetEntityId); if (!$entity->getId()) { $phone = $this->request->query->get('phone'); if (!$phone) { $phone = $this->phoneProvider->getPhoneNumber($targetEntity); } $entity->setPhoneNumber($phone); } $options = ['phone_suggestions' => array_unique(array_map(function ($item) { return $item[0]; }, $this->phoneProvider->getPhoneNumbers($targetEntity)))]; } $this->form = $this->formFactory->createNamed($this->formName, $this->formType, $entity, $options); $this->form->setData($entity); if (in_array($this->request->getMethod(), array('POST', 'PUT'))) { $this->form->submit($this->request); if ($this->form->isValid()) { // TODO: should be refactored after finishing BAP-8722 // Contexts handling should be moved to common for activities form handler if ($this->form->has('contexts')) { $contexts = $this->form->get('contexts')->getData(); $this->activityManager->setActivityTargets($entity, $contexts); } elseif ($targetEntityClass) { // if we don't have "contexts" form field // we should save association between activity and target manually $targetEntity = $this->entityRoutingHelper->getEntity($targetEntityClass, $targetEntityId); $this->callActivityManager->addAssociation($entity, $targetEntity); $phones = $this->phoneProvider->getPhoneNumbers($targetEntity); foreach ($phones as $phone) { if ($entity->getPhoneNumber() === $phone[0]) { $this->callActivityManager->addAssociation($entity, $phone[1]); } } } $this->onSuccess($entity); return true; } } return false; }
/** * Adds phone number owner to default contexts * * @param FormEvent $event */ public function addPhoneContextListener(FormEvent $event) { /** @var Call $entity */ $entity = $event->getData(); $form = $event->getForm(); if (!is_object($entity) || $entity->getId()) { return; } $contexts = $form->get('contexts')->getData(); $phoneContexts = []; foreach ($contexts as $targetEntity) { $phones = $this->phoneProvider->getPhoneNumbers($targetEntity); foreach ($phones as $phone) { if ($entity->getPhoneNumber() === $phone[0]) { $phoneContexts[] = $phone[1]; } } } $form->get('contexts')->setData(array_merge($contexts, $phoneContexts)); }
/** * @dataProvider supportedMethods * * @param string $method */ public function testProcessValidDataWithTargetEntity($method) { $this->entity->setPhoneNumber('phone1'); $targetEntity = new TestTarget(123); $targetEntity1 = new TestTarget(456); $this->request->query->set('entityClass', get_class($targetEntity)); $this->request->query->set('entityId', $targetEntity->getId()); $this->formFactory->expects($this->once())->method('createNamed')->with('orocrm_call_form', 'orocrm_call_form', $this->entity, [])->will($this->returnValue($this->form)); $this->form->expects($this->once())->method('submit')->with($this->request); $this->form->expects($this->once())->method('isValid')->will($this->returnValue(true)); $this->phoneProvider->expects($this->never())->method('getPhoneNumber'); $this->phoneProvider->expects($this->once())->method('getPhoneNumbers')->with($this->identicalTo($targetEntity))->will($this->returnValue([['phone1', $targetEntity], ['phone2', $targetEntity], ['phone1', $targetEntity1]])); $this->entityRoutingHelper->expects($this->once())->method('getEntity')->with(get_class($targetEntity), $targetEntity->getId())->will($this->returnValue($targetEntity)); // phone1, $targetEntity $this->callActivityManager->expects($this->at(0))->method('addAssociation')->with($this->identicalTo($this->entity), $this->identicalTo($targetEntity)); // phone2, $targetEntity $this->callActivityManager->expects($this->at(1))->method('addAssociation')->with($this->identicalTo($this->entity), $this->identicalTo($targetEntity)); // phone1, $targetEntity1 $this->callActivityManager->expects($this->at(2))->method('addAssociation')->with($this->identicalTo($this->entity), $this->identicalTo($targetEntity1)); $this->manager->expects($this->once())->method('persist')->with($this->entity); $this->manager->expects($this->once())->method('flush'); $this->request->setMethod($method); $this->assertTrue($this->handler->process($this->entity)); }