autcomplete for city and poste too
[platal.git] / classes / csvimporter.php
index bf5cfe2..2b76a50 100644 (file)
@@ -175,11 +175,11 @@ class CSVImporter
         static $fields, $conds, $values, $thens, $elses;
 
         if (!isset($fields)) {
-            $fields = Env::v('csv_cond_field');
-            $conds  = Env::v('csv_cond');
-            $values = Env::v('csv_cond_value');
-            $thens  = Env::v('csv_cond_then');
-            $elses  = Env::v('csv_cond_else');
+            $fields = $_SESSION['csv_cond_field'];
+            $conds  = $_SESSION['csv_cond'];
+            $values = $_SESSION['csv_cond_value'];
+            $thens  = $_SESSION['csv_cond_then'];
+            $elses  = $_SESSION['csv_cond_else'];
         }
         $field = $line[$fields[$key]];
         $cond  = $conds[$key];
@@ -225,6 +225,13 @@ class CSVImporter
         $this->field_value[$name] = $value;
     }
 
+    private function cleanSession($fields)
+    {
+        foreach ($fields as $field) {
+            unset($_SESSION[$field]);
+        }
+    }
+
     /** Handle insertion form
      * @param $page  PlatalPage to process
      * @param $url   URI of the page
@@ -232,6 +239,15 @@ class CSVImporter
      */
     public function apply(&$page, $url, $fields = null)
     {
+        $sesfields = array('csv_value', 'csv_user_value', 'csv_cond_field',
+                           'csv_update', 'csv_action', 'csv_cond_field',
+                           'csv_cond', 'csv_cond_value', 'csv_cond_then',
+                           'csv_cond_else', 'csv', 'csv_separator', 'csv_url');
+        if ($url != @$_SESSION['csv_url']) {
+            $this->cleanSession($sesfields);
+            $_SESSION['csv_url'] = $url;
+        }
+
         if (is_null($fields) || empty($fields)) {
             $fields = $this->getFieldList();
         }
@@ -251,17 +267,21 @@ class CSVImporter
         if (empty($next)) {
             $next = $current;
         }
-        $csv  = Env::v('csv');
+        $csv  = @$_SESSION['csv'];
         if ($current == 'source' && Env::has('csv_valid')) {
             $csv = Env::v('csv_source');
+            $_SESSION['csv'] = $csv;
             $next = 'values';
         }
         if ($csv) {
-            $sep = Env::v('csv_separator');
-            if (empty($sep)) {
-                $sep = ';';
+            if (Env::has('csv_separator')) {
+                $sep = Env::v('csv_separator');
+                if (empty($sep)) {
+                    $sep = ';';
+                }
+                $_SESSION['csv_separator'] = $sep;
             }
-            $this->setCSV($csv, null, $sep);
+            $this->setCSV($csv, null, $_SESSION['csv_separator']);
         }
         if ($current == 'values' && Env::has('csv_valid')) {
             $next = 'valid';
@@ -269,10 +289,22 @@ class CSVImporter
         if (empty($csv)) {
             $next = 'source';
         }
+        if (Env::has('csv_action')) {
+            $_SESSION['csv_action'] = Env::v('csv_action');
+        } 
         if ($next == 'valid') {
-            $insert   = Env::v('csv_value');
-            $values   = Env::v('csv_user_value');
-            $update   = Env::v('csv_update');
+            if ($current != 'valid') {
+                $cpyfields = array('csv_value', 'csv_user_value', 'csv_cond_field',
+                                   'csv_update', 'csv_action', 'csv_cond_field',
+                                   'csv_cond', 'csv_cond_value', 'csv_cond_then',
+                                   'csv_cond_else');
+                foreach ($cpyfields as $field) {
+                    $_SESSION[$field] = Env::v($field);
+                }
+            }
+            $insert   = $_SESSION['csv_value'];
+            $values   = $_SESSION['csv_user_value'];
+            $update   = $_SESSION['csv_update'];
             foreach ($insert as $key=>$value) {
                 if (empty($value)) {
                     $insert[$key] = null;
@@ -292,8 +324,9 @@ class CSVImporter
                 $fields[]     = $key;
             }
             if ($current == 'valid' && Env::has('csv_valid')) {
-                $this->run(Env::v('csv_action'), $insert, $update);
+                $this->run($_SESSION['csv_action'], $insert, $update);
                 $page->assign('csv_done', true);
+                $this->cleanSession($sesfields);
             } else {
                 $preview = array();
                 foreach ($this->data as $line) {
@@ -308,8 +341,8 @@ class CSVImporter
         $page->assign('csv_page', $next);
         $page->assign('csv_path', $url);
         $page->assign('csv_fields', $fields);  
-        $page->assign('csv', $csv);
     }
 }
 
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
 ?>