Example #1
0
 /**
  * @Route("/import", name="massimport")
  */
 public function massImportAction(Request $request)
 {
     $form = $this->createFormBuilder(null, array('action' => $this->generateUrl('massimport')))->add('file', 'file')->getForm();
     if ($request->getMethod() === 'POST') {
         $request = $this->getRequest();
         $form->bind($request);
         // It always gets overwritten
         $form['file']->getData()->move('/tmp', 'example.tmp');
         $lines = explode(PHP_EOL, file_get_contents('/tmp/example.tmp'));
         // since we don't care about first line or last line
         array_shift($lines);
         array_pop($lines);
         foreach ($lines as $line) {
             $data = explode("\t", $line);
             $product = new Product();
             $product->setTitle($data[0]);
             $product->setDescription($this->sanitize($data[1]));
             $product->setPrice($data[2]);
             // I'm not dealing with timezones as I'd need another field for it to work with doctrine and I didn't notice until the end of the exam
             $product->setInitDate(new \DateTime($data[3]));
             $product->setExpiryDate(new \DateTime($data[4]));
             $product->setMerchantAddress($data[5]);
             $product->setMerchantName($this->sanitize($data[6]));
             // I'm currently just enabling all of them
             $product->setStatus(1);
             $this->saveProduct($product);
         }
     }
     return $this->render('default/success.html.twig');
 }