<?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 *
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;
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;
}
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;
}
{
$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";
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
*/
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)) {
$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';
$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);
}
}
$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);