コード例 #1
0
 private function exportSegment($segment, $output)
 {
     $result = "";
     $query = "";
     $fieldsToDisplayRaw = "";
     $fieldsToDisplay = array();
     $searchUtils = $this->getContainer()->get("search.utils");
     $exportUtils = $this->getContainer()->get('export_utils');
     $logger = $this->getContainer()->get('logger');
     if ($segment->getCode()) {
         $segmentation = $this->getContainer()->get('leadsfactory.mkgsegmentation_repository')->find($segment->getSegmentation());
         $savedSearch = $searchUtils->getKibanaSavedSearch($segmentation->getQueryCode());
         $query = $savedSearch->getQuery();
         SegmentUtils::addFilterConfig($query, $segment);
         $result = $searchUtils->request(ElasticSearchUtils::$PROTOCOL_POST, "/_search", $query);
         $fieldsToDisplayRaw = implode(";", $savedSearch->getColumns());
         $fieldsToDisplay = $savedSearch->getColumns();
     }
     if (!is_dir("datas/segments")) {
         mkdir("datas/segments");
     }
     $handle = fopen('datas/segments/segment-' . $segment->getId() . "-" . $segment->getCode() . ".csv", 'w');
     fputcsv($handle, $fieldsToDisplay, ";", "\"", "\\");
     $elements = $result->hits->hits;
     foreach ($elements as $row) {
         $leadsource = $row->_source;
         $content = array();
         foreach ($fieldsToDisplay as $fied) {
             try {
                 if (trim($fied) != "") {
                     if (strstr($fied, "content.")) {
                         $headerrow = str_replace("content.", "", $fied);
                         $obj = $leadsource->content;
                         $content[] = $obj->{$headerrow};
                     } else {
                         $content[] = $leadsource->{$fied};
                     }
                 }
             } catch (\Exception $e) {
                 $content[] = "";
             }
         }
         fputcsv($handle, $content, ";", "\"", "\\");
     }
     fclose($handle);
     if (trim($segment->getEmails()) != "") {
         $from = isset($params['from']) ? $params['from'] : $exportUtils::NOTIFICATION_DEFAULT_FROM;
         $emails = explode(";", $segment->getEmails());
         foreach ($emails as $email) {
             // Sending email
             $message = Swift_Message::newInstance()->setSubject($segment->getConfirmationemailssubjects())->setFrom($from)->setTo($email)->setBody($segment->getConfirmationEmailSource());
             $message->attach(\Swift_Attachment::fromPath('datas/segments/segment-' . $segment->getId() . "-" . $segment->getCode() . ".csv")->setFilename('segment-' . $segment->getId() . "-" . $segment->getCode() . ".csv"));
             try {
                 $output->writeln("Sending mail for segment " . $segment->getName() . " to " . $email);
                 $output->writeln('<info>Sending mail for segment ' . $segment->getName() . ' to ' . $email . '</info>');
                 $result = $this->getContainer()->get('mailer')->send($message);
             } catch (\Exception $e) {
                 $output->writeln($e->getMessage());
                 $logger->error($e->getMessage());
             }
         }
     }
 }
コード例 #2
0
 /**
  * @param Request $request
  * @return mixed
  * @Route("/kibana/segment/download/csv/{id}", name="_marketing_segment_download_csv")
  * @Secure(roles="ROLE_USER")
  */
 public function mkgSegmentDownloadCsvAction(Request $request, $id)
 {
     $searchUtils = $this->get("search.utils");
     if (!$searchUtils->isKibanaAlive()) {
         return $this->redirectToRoute('_marketing_kibana_error');
     }
     $segment = $this->get('leadsfactory.mkgsegment_repository')->find($id);
     $segmentation = $this->get('leadsfactory.mkgsegmentation_repository')->find($segment->getSegmentation());
     $result = "";
     $query = "";
     $fieldsToDisplayRaw = "";
     $fieldsToDisplay = array();
     if ($segmentation->getQueryCode()) {
         $savedSearch = $searchUtils->getKibanaSavedSearch($segmentation->getQueryCode());
         $query = $savedSearch->getQuery();
         SegmentUtils::addFilterConfig($query, $segment);
         $result = $searchUtils->request(ElasticSearchUtils::$PROTOCOL_POST, "/_search?size=10000&from=0", $query);
         $fieldsToDisplayRaw = implode(";", $savedSearch->getColumns());
         $fieldsToDisplay = $savedSearch->getColumns();
     }
     $handle = fopen('php://temp', 'w');
     fputcsv($handle, $fieldsToDisplay, ";", "\"", "\\");
     $elements = $result->hits->hits;
     foreach ($elements as $row) {
         $leadsource = $row->_source;
         $content = array();
         foreach ($fieldsToDisplay as $fied) {
             try {
                 if (trim($fied) != "") {
                     if (strstr($fied, "content.")) {
                         $headerrow = str_replace("content.", "", $fied);
                         $obj = $leadsource->content;
                         $content[] = $obj->{$headerrow};
                     } else {
                         $content[] = $leadsource->{$fied};
                     }
                 }
             } catch (\Exception $e) {
                 $content[] = "";
             }
         }
         fputcsv($handle, $content, ";", "\"", "\\");
     }
     rewind($handle);
     $content = stream_get_contents($handle);
     fclose($handle);
     $response = new Response($content);
     $response->headers->set('content-type', 'text/csv');
     $response->headers->set('Content-Disposition', 'attachment; filename=leads_report.csv');
     return $response;
 }