Created
July 18, 2019 16:21
-
-
Save faizanakram99/d5597d33be83ff1b38bed7cf1157560a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// ..... | |
class blaBla | |
{ | |
/** | |
* @Route("/suggestdestinations") | |
* | |
* @return \Symfony\Component\HttpFoundation\Response | |
*/ | |
public function suggestDestinationsAction(Request $request) | |
{ | |
$this->translator = $this->get('translator'); | |
$this->entityManager = $this->getDoctrine()->getManager(); | |
$email = json_decode($request->getContent(), JSON_OBJECT_AS_ARRAY); | |
if ($request->isMethod(Request::METHOD_OPTIONS)) { | |
return $this->preflightResponse(); | |
} | |
if (false === $email) { | |
throw new BadRequestHttpException('Unable to decode JSON payload'); | |
} | |
$parameters = ['email' => $email['sender']['email']]; | |
$suggestedRelations = $this->entityManager->createQuery('SELECT r FROM '.(Relatie::class).' r LEFT JOIN r.addresses a LEFT JOIN r.contacts c WHERE (c.email=:email OR r.email=:email OR a.email=:email)')->setParameters($parameters)->getResult(); | |
$attachmentsWithKeys = array_combine(array_map(function ($attachment) { | |
return $attachment['name']; | |
}, $email['attachments']), $email['attachments']); | |
$findIdentifiers = function ($hints, $string) use ($email) { | |
if (!preg_match_all($regex = '/(?:'.implode('|', $hints).")[\t ]*([A-Z0-9\-]+)/i", $string.' '.$email['subject'], $matches)) { | |
return []; | |
} | |
return $matches[1]; | |
}; | |
$findSimpleIdentifiers = function ($string) use ($email) { | |
if (!preg_match_all($regex = "/(?:[\t ]|^)([A-Z0-9]*[0-9\-]+)/i", $string.' '.$email['subject'], $matches)) { | |
return []; | |
} | |
return $matches[1]; | |
}; | |
$suggestions = array_map(function ($attachment) use ($suggestedRelations, $findIdentifiers, $findSimpleIdentifiers) { | |
$attachmentName = $attachment['name']; | |
if (!count($suggestedRelations)) { | |
$orders = array_filter(array_map(function ($identifier) { | |
return $this->entityManager->find(Afroep::class, $identifier); | |
}, $findIdentifiers($this->translateCue('order'), $attachmentName))); | |
$contracts = array_filter(array_map(function ($identifier) { | |
return $this->entityManager->getRepository(Contract::class)->findOneBy(['weergavecontractnummer' => $identifier]); | |
}, $findIdentifiers($this->translateCue('contract'), $attachmentName))); | |
} else { | |
$identifiers = $findSimpleIdentifiers($attachmentName); | |
$contracts = $this->entityManager->createQuery('SELECT c FROM '.(Contract::class).' c JOIN c.adres a WHERE a.relatie IN (:relations) AND REGEXP(c.weergavecontractnummer, :identifiers) = TRUE') | |
->setParameters(['relations' => $suggestedRelations, 'identifiers' => '(^|[A-Za-z]+)('.implode('|', $identifiers).')'])->getResult(); | |
$orders = $this->entityManager->createQuery('SELECT a FROM '.(Afroep::class).' a LEFT JOIN a.inslagen i LEFT JOIN i.contractline icl LEFT JOIN icl.contract ic LEFT JOIN ic.adres ia LEFT JOIN a.uitslagen u LEFT JOIN u.contractline ucl LEFT JOIN ucl.contract uc LEFT JOIN uc.adres ua LEFT JOIN a.transportorder t LEFT JOIN t.transporter ta WHERE (ia.relatie IN (:relations) OR ua.relatie IN (:relations) OR ta.relatie IN (:relations)) AND a.id IN (:identifiers)') | |
->setParameters(['relations' => $suggestedRelations, 'identifiers' => $identifiers])->getResult(); | |
} | |
$attachmentSuggestions = array_merge( | |
array_map(function (Relatie $relation) use ($attachmentName) { | |
$alreadyExists = $this->hasExistingAttachment($relation->attachments, $attachmentName); | |
return ['label' => ucfirst($this->translator->trans('relatie')).' '.$relation->bedrijfsnaam, 'code' => 'relation-'.$relation->id, 'duplicate' => $alreadyExists]; | |
}, $suggestedRelations), | |
array_map(function (Contract $contract) use ($attachmentName) { | |
$alreadyExists = $this->hasExistingAttachment($contract->attachments, $attachmentName); | |
return ['label' => ucfirst($this->translator->trans('contract')).' '.$contract->weergavecontractnummer.' ('.$contract->adres->relatie->bedrijfsnaam.')', 'code' => 'contract-'.$contract->id, 'duplicate' => $alreadyExists]; | |
}, $contracts), | |
array_map(function (Afroep $order) use ($attachmentName) { | |
$alreadyExists = $this->hasExistingAttachment($order->attachments, $attachmentName); | |
return ['label' => ucfirst($this->translator->trans('order')).' '.$order->id, 'code' => 'order-'.$order->id, 'duplicate' => $alreadyExists]; | |
}, $orders) | |
); | |
return array_map(function ($suggestion) { | |
if ($suggestion['duplicate']) { | |
$suggestion['label'] .= ' - '.$this->translator->trans('duplicate'); | |
} | |
return $suggestion; | |
}, $attachmentSuggestions); | |
}, $attachmentsWithKeys); | |
return new JsonResponse($suggestions, Response::HTTP_OK, self::HEADERS); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment