/** * Method used to process actions. * 1) Log in file first request * 2) Save in DB * * @Route("/post", name="_client_post_form") * @param Request $request * * @throws \Exception * @return Response */ public function postLeadsAction(Request $request) { $logger = $this->get('logger'); $formUtils = $this->get("form_utils"); $fields = $request->get("lffield"); $exportUtils = $this->get('export_utils'); $searchUtils = $this->get('search.utils'); // if ( !$formUtils->checkFormKey( $request->get("lfFormKey"), $request->get("lfFormId") ) ) // throw new \Exception ("Form Key is not allowed"); try { $formTypeObject = $this->get('leadsfactory.form_type_repository')->find((string) $request->get("lfFormType")); $formId = $request->get("lfFormId"); $logger->info("Id FORM Posted : " . $formId); $formObject = $this->get('leadsfactory.form_repository')->find($formId); // Read configuration to map attributes correctly $config = $formObject->getConfig(); $redirectUrlSuccess = isset($config['redirect']['url_success']) ? $config['redirect']['url_success'] : ''; $redirectUrlError = isset($config['redirect']['url_error']) ? $config['redirect']['url_error'] : ''; if (array_key_exists('configuration', $config)) { if (array_key_exists('lastname', $config["configuration"])) { $fields["lastname"] = ucfirst($fields[$config["configuration"]["lastname"]]); } if (array_key_exists('firstname', $config["configuration"])) { $fields["firstname"] = ucfirst($fields[$config["configuration"]["firstname"]]); } } // On vérifie s'il y a des fichiers uploadés $form_dir_path = $this->container->getParameter('kernel.root_dir') . '/../datas/'; if (isset($config['upload_files']) && $config['upload_files'] == 'OK') { // On vérifie l'extension $all_files = $request->files->all(); $fs = new Filesystem(); $filesToSave = array(); $i = 0; foreach ($all_files['lffield'] as $field_name => $file) { // Fichier pesant 1Mo max if (isset($file) && $file->getClientSize() != 0 && $file->getClientSize() <= 1048576) { // On créé (s'il n'existe pas) un répertoire portant le nom de l'ID form Leads if (!$fs->exists($form_dir_path . $formId)) { $fs->mkdir($form_dir_path . $formId, 0760); } else { // Droits en écriture ? if (!is_writable($form_dir_path . $formId)) { $fs->chmod($form_dir_path . $formId, 0760); } } // On récupère la liste des fichiers à uploader $filesToSave[$i] = array(); $filesToSave[$i]['field_name'] = $field_name; $filesToSave[$i]['file'] = $file; $filesToSave[$i]['extension'] = $file->getClientOriginalExtension(); // On sauvegarde les noms originaux des fichiers $logger->info("Field name : " . $field_name); $fields[$field_name] = $file->getClientOriginalName(); } $i++; } } $fields = $this->get('form_utils')->preProcessData($formId, $fields); $json = json_encode($fields); // Create new Leads Entity Object $leads = new Leads(); $leads->setIpadress($this->get('request')->getClientIp()); $leads->setUserAgent($this->get('request')->server->get("HTTP_USER_AGENT")); $leads->setFirstname(@$fields["firstname"]); $leads->setLastname(@$fields["lastname"]); $leads->setData($json); $leads->setLog("leads importée le : " . date('Y-m-d h:s')); $leads->setUtmcampaign(@$fields["utmcampaign"]); $leads->setFormType($formTypeObject); $leads->setForm($formObject); $leads->setTelephone(@$fields["phone"]); if (array_key_exists('email', $fields)) { $leads->setEmail($fields['email']); } // Assignation de la leads si la configuration est presente if (array_key_exists('configuration', $config)) { if (array_key_exists('assign', $config["configuration"])) { $assign = trim($config["configuration"]["assign"]); $user = $this->getDoctrine()->getRepository('TellawLeadsFactoryBundle:Users')->findOneByEmail($assign); if ($user != null) { $leads->setUser($user); } else { $logger->info("Frontcontroller : Assign tu a User that does not exists! " . $assign); } } if (array_key_exists('status', $config["configuration"])) { $status = trim($config["configuration"]["status"]); $leads->setWorkflowStatus($status); } if (array_key_exists('type', $config["configuration"])) { $type = trim($config["configuration"]["type"]); $leads->setWorkflowType($type); } if (array_key_exists('theme', $config["configuration"])) { $theme = trim($config["configuration"]["theme"]); $leads->setWorkflowTheme($theme); } } // Assignation de la leads si l'information est contenue dans les données de la leads if (array_key_exists('lf-assign', $fields)) { $assign = trim($fields["lf-assign"]); $user = $this->getDoctrine()->getRepository('TellawLeadsFactoryBundle:Users')->findOneByEmail($assign); if ($user != null) { $leads->setUser($user); } else { $logger->info("Frontcontroller : Assign tu a User that does not exists! " . $assign); } } if (array_key_exists('lf-status', $fields) && trim($fields["lf-status"] != "")) { $status = trim($fields["lf-status"]); $leads->setWorkflowStatus($status); } if (array_key_exists('lf-type', $fields) && trim($fields["lf-type"] != "")) { $type = trim($fields["lf-type"]); $leads->setWorkflowType($type); } if (array_key_exists('lf-theme', $fields) && trim($fields["lf-theme"] != "")) { $theme = trim($fields["lf-theme"]); $leads->setWorkflowTheme($theme); } $status = $exportUtils->hasScheduledExport($formObject->getConfig()) ? $exportUtils::$_EXPORT_NOT_PROCESSED : $exportUtils::$_EXPORT_NOT_SCHEDULED; $leads->setStatus($status); $leads->setCreatedAt(new \DateTime()); $em = $this->getDoctrine()->getManager(); $em->persist($leads); $em->flush(); // On déplace le(s) fichier(s) uploadé(s) vers le répertoire final if (isset($filesToSave) && !empty($filesToSave)) { foreach ($filesToSave as $file_item) { $field_name = $file_item['field_name']; // Nom du champ dans la Lead $file = $file_item['file']; // Objet permettant de gérer le fichier à uploader $extension = $file_item['extension']; $filename = $leads->getId() . '_' . $field_name . '.' . $extension; $file->move($form_dir_path . $formId, $filename); $logger->info("Fichier : " . $filename . " uploadés :)"); } } // Create export job(s) if ($status == $exportUtils::$_EXPORT_NOT_PROCESSED) { $exportUtils->createJob($leads); } // Index leads on search engine $leads_array = $this->get('leadsfactory.leads_repository')->getLeadsArrayById($leads->getId()); $searchUtils->indexLeadObject($leads_array, $leads->getForm()->getScope()->getCode()); //Send notification if (isset($config['notification'])) { $this->sendNotification($config['notification'], $leads); } //Send confirmation email if (isset($config['confirmation_email'])) { $this->sendConfirmationEmail($config['confirmation_email'], $leads); } //Redirect to success page if (!empty($redirectUrlSuccess)) { if ($redirectUrlSuccess == 'redirect_url') { $logger->info('redirect url : ' . $redirectUrlSuccess); $redirectUrlSuccess = $fields['redirect_url']; } if (isset($config['redirect']['redirect_with_id']) && $config['redirect']['redirect_with_id'] == true) { if (strpos($redirectUrlSuccess, '?')) { $paramsSep = '&'; } else { $paramsSep = '?'; } $redirectUrlSuccess = $redirectUrlSuccess . $paramsSep . 'lead_id=' . $leads->getId() . '&key=' . $formUtils->getApiKey($formObject); } return $this->redirect($redirectUrlSuccess); } } catch (Exception $e) { $logger->error('postLeadsAction Error '); return $this->redirect($redirectUrlError); } return new Response('Done'); }
/** * Enregistre une DI * @Route("/lead/post") * @Method("POST") */ public function postLeadAction(Request $request) { $exportUtils = $this->get('export_utils'); $logger = $this->get('logger'); $searchUtils = $this->get('search.utils'); $logger->info('API post lead'); $data = $request->getcontent(); $logger->info($data); $data = json_decode($data, true); try { $form = $this->getDoctrine()->getRepository('TellawLeadsFactoryBundle:Form')->findOneByCode($data['formCode']); // Get the Json configuration of the form $config = $form->getConfig(); $data = $this->get('form_utils')->preProcessData($form->getId(), $data); $jsonContent = json_encode($data); $leads = new Leads(); $leads->setIpadress($this->get('request')->getClientIp()); $leads->setUserAgent($this->get('request')->server->get("HTTP_USER_AGENT")); $leads->setFirstname(@$data['firstName']); $leads->setLastname(@$data['lastName']); $leads->setData($jsonContent); $leads->setLog("leads importée le : " . date('Y-m-d h:s')); $leads->setUtmcampaign(@$data["utmcampaign"]); $leads->setForm($form); $leads->setTelephone(@$data["phone"]); $leads->setEmail(@$data['email']); // Assignation de la leads si la configuration est presente if (array_key_exists('configuration', $config)) { if (array_key_exists('assign', $config["configuration"])) { $assign = trim($config["configuration"]["assign"]); $user = $this->getDoctrine()->getRepository('TellawLeadsFactoryBundle:Users')->findOneByEmail($assign); if ($user != null) { $leads->setUser($user); } else { $logger->info("Frontcontroller : Assign tu a User that does not exists! " . $assign); } } if (array_key_exists('status', $config["configuration"])) { $status = trim($config["configuration"]["status"]); $leads->setWorkflowStatus($status); } if (array_key_exists('type', $config["configuration"])) { $type = trim($config["configuration"]["type"]); $leads->setWorkflowType($type); } if (array_key_exists('theme', $config["configuration"])) { $theme = trim($config["configuration"]["theme"]); $leads->setWorkflowTheme($theme); } } $status = $exportUtils->hasScheduledExport($form->getConfig()) ? $exportUtils::$_EXPORT_NOT_PROCESSED : $exportUtils::$_EXPORT_NOT_SCHEDULED; $leads->setStatus($status); $leads->setCreatedAt(new \DateTime()); $em = $this->getDoctrine()->getManager(); $em->persist($leads); $em->flush(); // Index leads on search engine $leads_array = $this->get('leadsfactory.leads_repository')->getLeadsArrayById($leads->getId()); $searchUtils->indexLeadObject($leads_array, $leads->getForm()->getScope()->getCode()); // Create export job(s) if ($status == $exportUtils::$_EXPORT_NOT_PROCESSED) { $exportUtils->createJob($leads); } if (array_key_exists('enableApiNotificationEmail', $config["configuration"]) && $config["configuration"]["enableApiNotificationEmail"] == true) { //Send notification if (isset($config['notification'])) { $this->sendNotification($config['notification'], $leads); $logger->info("API : Envoi de notifications"); } else { $logger->info("API : Le bloc de configuration de Notification n'existe pas en config"); } } else { $logger->info("API : Le formulaire refuse l'envoi de mail par notification"); } if (array_key_exists('enableApiConfirmationEmail', $config["configuration"]) && $config["configuration"]["enableApiConfirmationEmail"] == true) { //Send confirmation email if (isset($config['confirmation_email'])) { $this->sendConfirmationEmail($config['confirmation_email'], $leads); } } return new Response(1); } catch (Exception $e) { $logger->error($e->getMessage()); return new Response(0); } }