2006 => 2007 Happy New Year\!
[platal.git] / classes / csvimporter.php
index 45a0dc1..5f1b4af 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2006 Polytechnique.org                              *
+ *  Copyright (C) 2003-2007 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -34,8 +34,10 @@ class CSVImporter
     private $data = array();
 
     private $user_functions = array();
+    private $field_desc = array();
+    private $field_value = array();
 
-    public function CSVImporter($table, $key = 'id', $do_sql = true)
+    public function __construct($table, $key = 'id', $do_sql = true)
     {
         $this->table     = $table;
         $this->key       = $key;
@@ -44,9 +46,9 @@ class CSVImporter
 
     private function processLine($line)
     {
-        $array = split($this->separator, $line);
+        $array = explode($this->separator, $line);
         if (is_null($this->index)) {
-            $this->index = $array;
+            $this->index = array_map('strtolower', $array);
             return true;
         }
 
@@ -63,21 +65,26 @@ class CSVImporter
         return true;
     }
 
+    private function getValue($line, $key, $action)
+    {
+        if (@array_key_exists($action, $line)) {
+            $value = $line[$action];
+        } elseif (is_callable($action, false)) {
+            $value = call_user_func($action, $line, $key);
+        } else {
+            $value = $action;
+        }
+        if (is_null($value) || $value == 'NULL') {
+            $value = 'NULL';
+        }
+        return $value;
+    }
+
     private function makeAssoc($line, $relation)
     {
         $ops = array();
         foreach ($relation as $key=>$ref) {
-            if (@array_key_exists($ref, $line)) {
-                $value = $line[$ref];
-            } elseif (is_callable($ref, false)) {
-                $value = call_user_func($ref, $line, $key);
-            } else {
-                $value = $ref;
-            }
-            if (is_null($value) || $value == 'NULL') {
-                $value = 'NULL';
-            }
-            $ops[$key] = $value; 
+            $ops[$key] = $this->getValue($line, $key, $ref);
         }
         return $ops;
     }
@@ -86,16 +93,8 @@ class CSVImporter
     {
         $ops = array();
         foreach ($relation as $key=>$ref) {
-            if (@array_key_exists($ref, $line)) {
-                $value = $line[$ref];
-            } elseif (is_callable($ref, false)) {
-                $value = call_user_func($ref, $line, $key);
-            } else {
-                $value = $ref;
-            }
-            if (is_null($value) || $value == 'NULL') {
-                $value = 'NULL';
-            } else {
+            $value = $this->getValue($line, $key, $ref);
+            if (!is_null($value) && $value != 'NULL') {
                 $value = "'" . addslashes($value) . "'";
             }
             $ops[$key] = "$key = $value";
@@ -207,13 +206,23 @@ class CSVImporter
 
     public function registerFunction($name, $desc, $callback)
     {
-        if (is_callable($callback)) {
+        if (is_callable($callback, false, $ref)) {
             $this->user_functions['func_' . $name] = array('desc' => $desc, 'callback' => $callback);
             return true;
         }
         return false;
     }
 
+    public function describe($name, $desc)
+    {
+        $this->field_desc[$name] = $desc;
+    }
+
+    public function forceValue($name, $value)
+    {
+        $this->field_value[$name] = $value;
+    }
+
     /** Handle insertion form
      * @param $page  PlatalPage to process
      * @param $url   URI of the page
@@ -221,12 +230,15 @@ class CSVImporter
      */
     public function apply(&$page, $url, $fields = null)
     {
-        if (is_null($fields)) {
+        if (is_null($fields) || empty($fields)) {
             $fields = $this->getFieldList();
         }
         if (is_null($fields)) {
             return false;
         }
+        foreach ($this->field_value as $key=>$value) {
+            unset($fields[$key]);
+        }
 
         $current = Env::v('csv_page');
         if (empty($current)) {
@@ -242,7 +254,12 @@ class CSVImporter
             $next = 'values';
         }
         if ($csv) {
-            $this->setCSV($csv);
+            $sep = Env::v('csv_separator');
+            if (empty($sep)) {
+                $sep = ';';
+            }
+            echo $sep;
+            $this->setCSV($csv, null, $sep);
         }
         if ($current == 'values' && Env::has('csv_valid')) {
             $next = 'valid';
@@ -268,6 +285,9 @@ class CSVImporter
                     $update[$key] = $insert[$key];
                 }
             }
+            foreach ($this->field_value as $key=>$value) {
+                $insert[$key] = $value;
+            }
             if ($current == 'valid' && Env::has('csv_valid')) {
                 $this->run(Env::v('csv_action'), $insert, $update);
                 $page->assign('csv_done', true);
@@ -280,7 +300,8 @@ class CSVImporter
             }
         }
         $page->assign('csv_index', $this->index);
-        $page->assign('csv_funtions', $this->user_functions);
+        $page->assign('csv_functions', $this->user_functions);
+        $page->assign('csv_field_desc', $this->field_desc);
         $page->assign('csv_page', $next);
         $page->assign('csv_path', $url);
         $page->assign('csv_fields', $fields);