fix URL accordingly to new documentation architecture
[platal.git] / classes / csvimporter.php
index 8ac5aee..816b9fa 100644 (file)
@@ -1,4 +1,23 @@
 <?php
+/***************************************************************************
+ *  Copyright (C) 2003-2006 Polytechnique.org                              *
+ *  http://opensource.polytechnique.org/                                   *
+ *                                                                         *
+ *  This program is free software; you can redistribute it and/or modify   *
+ *  it under the terms of the GNU General Public License as published by   *
+ *  the Free Software Foundation; either version 2 of the License, or      *
+ *  (at your option) any later version.                                    *
+ *                                                                         *
+ *  This program is distributed in the hope that it will be useful,        *
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
+ *  GNU General Public License for more details.                           *
+ *                                                                         *
+ *  You should have received a copy of the GNU General Public License      *
+ *  along with this program; if not, write to the Free Software            *
+ *  Foundation, Inc.,                                                      *
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
+ ***************************************************************************/
 
 define('CSV_INSERT',  'insert'); // INSERT IGNORE
 define('CSV_REPLACE', 'replace'); // REPLACE
@@ -15,6 +34,8 @@ 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)
     {
@@ -27,7 +48,7 @@ class CSVImporter
     {
         $array = split($this->separator, $line);
         if (is_null($this->index)) {
-            $this->index = $array;
+            $this->index = array_map('strtolower', $array);
             return true;
         }
 
@@ -44,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;
     }
@@ -67,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";
@@ -188,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
@@ -202,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)) {
@@ -249,6 +280,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);
@@ -261,7 +295,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);