Happy New Year
[platal.git] / classes / csvimporter.php
index 2b76a50..70aa377 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2007 Polytechnique.org                              *
+ *  Copyright (C) 2003-2009 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************/
 
-define('CSV_INSERT',  'insert'); // INSERT IGNORE
-define('CSV_REPLACE', 'replace'); // REPLACE
-define('CSV_UPDATE',  'update'); // INSERT and UPDATE on error
+define('CSV_INSERT',     'insert'); // INSERT IGNORE
+define('CSV_REPLACE',    'replace'); // REPLACE
+define('CSV_UPDATE',     'update'); // INSERT and UPDATE on error
+define('CSV_UPDATEONLY', 'updateonly'); // UPDATE
 
 class CSVImporter
 {
@@ -39,7 +40,7 @@ class CSVImporter
     public function __construct($table, $key = 'id', $do_sql = true)
     {
         $this->table     = $table;
-        $this->key       = $key;
+        $this->key       = explode(',', $key);
         $this->do_sql    = $do_sql;
     }
 
@@ -133,7 +134,7 @@ class CSVImporter
         VarStream::init();
         global $csv_source;
         $this->index     = null;
-        
+
         $csv_source = $csv;
         $res        = fopen('var://csv_source', 'r');
 
@@ -159,11 +160,16 @@ class CSVImporter
               case CSV_REPLACE:
                 $this->execute("REPLACE INTO {$this->table} SET $set");
                 break;
-              case CSV_UPDATE:
-                if (!$this->execute("INSERT INTO {$this->table} SET $set")) {
+              case CSV_UPDATE: case CSV_UPDATEONLY:
+                if ($action == CSV_UPDATEONLY || !$this->execute("INSERT INTO {$this->table} SET $set")) {
                     $ops = $this->makeRequestArgs($line, $update_relation);
                     $set = join(', ', $ops);
-                    $this->execute("UPDATE {$this->table} SET $set WHERE {$ops[$this->key]}");
+                    $where = array();
+                    foreach ($this->key as $key) {
+                        $where[] = $ops[$key];
+                    }
+                    $where = join(' AND ', $where);
+                    $this->execute("UPDATE {$this->table} SET $set WHERE $where");
                 }
                 break;
             }
@@ -233,7 +239,7 @@ class CSVImporter
     }
 
     /** Handle insertion form
-     * @param $page  PlatalPage to process
+     * @param $page  PlPage to process
      * @param $url   URI of the page
      * @param $field Editable fields
      */
@@ -285,13 +291,13 @@ class CSVImporter
         }
         if ($current == 'values' && Env::has('csv_valid')) {
             $next = 'valid';
-        }    
+        }
         if (empty($csv)) {
             $next = 'source';
         }
         if (Env::has('csv_action')) {
             $_SESSION['csv_action'] = Env::v('csv_action');
-        } 
+        }
         if ($next == 'valid') {
             if ($current != 'valid') {
                 $cpyfields = array('csv_value', 'csv_user_value', 'csv_cond_field',
@@ -324,6 +330,7 @@ class CSVImporter
                 $fields[]     = $key;
             }
             if ($current == 'valid' && Env::has('csv_valid')) {
+                S::assert_xsrf_token();
                 $this->run($_SESSION['csv_action'], $insert, $update);
                 $page->assign('csv_done', true);
                 $this->cleanSession($sesfields);
@@ -340,7 +347,7 @@ class CSVImporter
         $page->assign('csv_field_desc', $this->field_desc);
         $page->assign('csv_page', $next);
         $page->assign('csv_path', $url);
-        $page->assign('csv_fields', $fields);  
+        $page->assign('csv_fields', $fields);
     }
 }