示例#1
0
 protected function processImportForm(sfWebRequest $request, sfForm $form)
 {
     $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
     if ($form->isValid()) {
         $notice = 'Import réussi';
         try {
             $file = $form->getValue('file');
             $filename = $file->getTempName();
             ini_set('auto_detect_line_endings', TRUE);
             $filestream = fopen($filename, "r");
             $collection = new Doctrine_Collection('Rfid');
             while (($data = fgetcsv($filestream, 1000, ";")) !== FALSE) {
                 $rfid = new Rfid();
                 $rfid->setUid($data[0]);
                 if (isset($data[1]) && !empty($data[1])) {
                     $rfid->setGroupeId($data[1]);
                 }
                 if (isset($data[2]) && !empty($data[2])) {
                     $rfid->setType($data[2]);
                 } else {
                     $rfid->setType('visiteur');
                 }
                 if (isset($data[3])) {
                     $rfid->setValeur1($data[3]);
                 }
                 if (isset($data[4])) {
                     $rfid->setValeur2($data[4]);
                 }
                 if (isset($data[5])) {
                     $rfid->setValeur3($data[5]);
                 }
                 if (isset($data[6])) {
                     $rfid->setIsActive($data[6]);
                 }
                 if (isset($data[7])) {
                     $rfid->setIsResettable($data[7]);
                 }
                 $collection->add($rfid);
             }
             ini_set('auto_detect_line_endings', FALSE);
             fclose($filestream);
             $collection->save();
         } catch (Exception $e) {
             $message = "Erreur import : " . $e->getMessage();
             $this->getUser()->setFlash('error', sprintf("error : %s", $message));
             return sfView::SUCCESS;
         }
         $this->getUser()->setFlash('notice', $notice);
     } else {
         $this->getUser()->setFlash('error', 'The import has not been saved due to some errors.', false);
     }
 }