/** * 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; }
/** * @param ObjectManager $om */ protected function persistDemoCalls(ObjectManager $om) { $accounts = $om->getRepository('OroCRMAccountBundle:Account')->findAll(); $contacts = $om->getRepository('OroCRMContactBundle:Contact')->findAll(); $callStatus = $om->getRepository('OroCRMCallBundle:CallStatus')->findOneBy(['name' => 'completed']); $directions = ['incoming' => $om->getRepository('OroCRMCallBundle:CallDirection')->findOneBy(['name' => 'incoming']), 'outgoing' => $om->getRepository('OroCRMCallBundle:CallDirection')->findOneBy(['name' => 'outgoing'])]; $contactCount = count($contacts); $accountCount = count($accounts); for ($i = 0; $i < 100; ++$i) { $contactRandom = rand(0, $contactCount - 1); $accountRandom = rand(0, $accountCount - 1); /** @var Contact $contact */ $contact = $contacts[$contactRandom]; /** @var Account $account */ $account = $accounts[$accountRandom]; $call = new Call(); $call->setCallStatus($callStatus); $call->setOrganization($this->organization); $call->setOwner($contact->getOwner()); $call->setSubject($this->subjects[array_rand($this->subjects)]); $call->setDuration(new \DateTime(rand(0, 1) . ':' . rand(0, 59) . ':' . rand(0, 59), new \DateTimeZone('UTC'))); if ($call->supportActivityTarget(get_class($contact->getOwner()))) { $call->addActivityTarget($contact->getOwner()); } $randomPath = rand(1, 10); if ($randomPath > 2) { if ($call->supportActivityTarget(get_class($contact))) { $this->setSecurityContext($contact->getOwner()); $call->addActivityTarget($contact); } $contactPrimaryPhone = $contact->getPrimaryPhone(); if ($contactPrimaryPhone) { $call->setPhoneNumber($contactPrimaryPhone->getPhone()); } $call->setDirection($directions['outgoing']); } if ($randomPath > 3) { /** @var Contact[] $relatedContacts */ $relatedContacts = $call->getActivityTargets('OroCRM\\Bundle\\ContactBundle\\Entity\\Contact'); if ($relatedContacts) { if ($call->supportActivityTarget(get_class($relatedContacts[0]->getAccounts()[0]))) { $call->addActivityTarget($relatedContacts[0]->getAccounts()[0]); } } else { if ($call->supportActivityTarget(get_class($account))) { $call->addActivityTarget($account); } } } $phone = $call->getPhoneNumber(); if (empty($phone)) { $phone = rand(1000000000, 9999999999); $phone = sprintf("%s-%s-%s", substr($phone, 0, 3), substr($phone, 3, 3), substr($phone, 6)); $call->setPhoneNumber($phone); $call->setDirection($directions['incoming']); } $om->persist($call); } }
/** * 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; }
public function testRemoveContactFromContextShouldDecreaseActivityCounter() { $firstContact = $this->findContact(LoadContactEntitiesData::FIRST_ENTITY_NAME); $secondContact = $this->findContact(LoadContactEntitiesData::SECOND_ENTITY_NAME); $firstContacted = $firstContact->getAcContactCount(); $secondContacted = $firstContact->getAcContactCount(); $call = new Call(); $call->setPhoneNumber('3058304958')->setSubject('subj')->setDirection($this->findCallDirection(DirectionProviderInterface::DIRECTION_OUTGOING)); $this->getActivityManager()->addActivityTargets($call, [$firstContact, $secondContact]); $this->getEntityManager()->persist($call); $this->getEntityManager()->flush(); $this->assertEquals($firstContacted + 1, $firstContact->getAcContactCount()); $this->assertEquals($secondContacted + 1, $secondContact->getAcContactCount()); $this->getActivityManager()->removeActivityTarget($call, $secondContact); $this->getEntityManager()->flush(); $this->assertEquals($firstContacted + 1, $firstContact->getAcContactCount()); $this->assertEquals($secondContacted, $secondContact->getAcContactCount()); }
/** * @param ObjectManager $om */ protected function persistDemoCalls(ObjectManager $om) { $accounts = $om->getRepository('OroCRMAccountBundle:Account')->findAll(); $contacts = $om->getRepository('OroCRMContactBundle:Contact')->findAll(); $directions = array('incoming' => $om->getRepository('OroCRMCallBundle:CallDirection')->findOneBy(array('name' => 'incoming')), 'outgoing' => $om->getRepository('OroCRMCallBundle:CallDirection')->findOneBy(array('name' => 'outgoing'))); $contactCount = count($contacts); $accountCount = count($accounts); for ($i = 0; $i < 100; ++$i) { $contactRandom = rand(0, $contactCount - 1); $accountRandom = rand(0, $accountCount - 1); /** @var Contact $contact */ $contact = $contacts[$contactRandom]; /** @var Account $account */ $account = $accounts[$accountRandom]; $call = new Call(); $call->setOrganization($this->organization); $call->setOwner($contact->getOwner()); $call->setSubject($this->subjects[array_rand($this->subjects)]); $call->setDuration(new \DateTime(rand(0, 1) . ':' . rand(0, 59) . ':' . rand(0, 59), new \DateTimeZone('UTC'))); $randomPath = rand(1, 10); if ($randomPath > 2) { $call->setRelatedContact($contact); $call->setContactPhoneNumber($contact->getPrimaryPhone()); $call->setDirection($directions['outgoing']); } if ($randomPath > 3) { if ($call->getRelatedContact()) { $call->setRelatedAccount($call->getRelatedContact()->getAccounts()[0]); } else { $call->setRelatedAccount($account); } } if (is_null($call->getContactPhoneNumber())) { $phone = rand(1000000000, 9999999999); $phone = sprintf("%s-%s-%s", substr($phone, 0, 3), substr($phone, 3, 3), substr($phone, 6)); $call->setPhoneNumber($phone); $call->setDirection($directions['incoming']); } $om->persist($call); } }
/** * @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)); }