Ejemplo n.º 1
0
 public function csvAction()
 {
     if ($this->getRequest()->isPost()) {
         if ($_FILES["file_csv"]["error"] == 0) {
             $fileName = $_FILES["file_csv"]["tmp_name"];
             $csvData = file_get_contents($fileName);
             $lines = explode(PHP_EOL, $csvData);
             $array = array();
             foreach ($lines as $line) {
                 $array[] = str_getcsv($line);
             }
             $size = sizeof($array);
             if ($size > 1) {
                 $modelCategory = new Application_Model_Category();
                 $modelTransaction = new Application_Model_Transaction();
                 $transactions_added = 0;
                 $error = array();
                 $session = new Zend_Session_Namespace('Zend_auth');
                 for ($i = 1; $i < $size; $i++) {
                     $category = $modelCategory->getByName($array[$i][1]);
                     if (isset($category->id_category)) {
                         $date_transaction = array('description' => $array[$i][0], 'amount' => $array[$i][2], 'id_category' => $category->id_category, 'id_user' => $session->user, 'date' => $array[$i][3]);
                         $modelTransaction->insertTransaction($date_transaction);
                         $transactions_added++;
                     } else {
                         $error[] = array("Message" => "Categorie inexistante", "Transaction" => $array[$i][1]);
                     }
                 }
                 $this->view->error = $error;
                 $this->view->success = $transactions_added > 1 ? $transactions_added . " transactions ont été ajoutées." : $transactions_added . " transaction a été ajoutée.";
                 if (sizeof($error) == 0) {
                     $this->_redirect('transaction/');
                 }
             }
         }
     }
 }