X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=classes%2Fcsvimporter.php;h=5f1b4af4b65931fc524b9335604210ab949d5a89;hb=5ddeb07cc787dd1dc3630a31f1528f5cc7c4d9b9;hp=45a0dc119c04d57a58f79f38b62ee8817799613f;hpb=212aae73878f5c54dbbacb5d2e5c13c6a15dc52b;p=platal.git diff --git a/classes/csvimporter.php b/classes/csvimporter.php index 45a0dc1..5f1b4af 100644 --- a/classes/csvimporter.php +++ b/classes/csvimporter.php @@ -1,6 +1,6 @@ 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);