From a6613b2efb9256648907e6a7d0427b518d995ccc Mon Sep 17 00:00:00 2001 From: x2003bruneau Date: Sun, 17 Dec 2006 14:20:24 +0000 Subject: [PATCH] Table editor integrates CSVImporter and minor code cleaning git-svn-id: svn+ssh://murphy/home/svn/platal/trunk@1284 839d8a87-29fc-0310-9880-83ba4fa771e5 --- classes/csvimporter.php | 48 +++++++++++++++++++++----------------- classes/pltableeditor.php | 13 +++++++++++ templates/include/csv-importer.tpl | 6 +++-- templates/table-editor.tpl | 17 +++++++++++--- 4 files changed, 57 insertions(+), 27 deletions(-) diff --git a/classes/csvimporter.php b/classes/csvimporter.php index 45a0dc1..606f444 100644 --- a/classes/csvimporter.php +++ b/classes/csvimporter.php @@ -34,6 +34,7 @@ class CSVImporter private $data = array(); private $user_functions = array(); + private $field_desc = array(); public function CSVImporter($table, $key = 'id', $do_sql = true) { @@ -63,21 +64,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 +92,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"; @@ -214,6 +212,11 @@ class CSVImporter return false; } + public function describe($name, $desc) + { + $this->field_desc[$name] = $desc; + } + /** Handle insertion form * @param $page PlatalPage to process * @param $url URI of the page @@ -221,7 +224,7 @@ 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)) { @@ -281,6 +284,7 @@ class CSVImporter } $page->assign('csv_index', $this->index); $page->assign('csv_funtions', $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); diff --git a/classes/pltableeditor.php b/classes/pltableeditor.php index 3fe9dce..cad848a 100644 --- a/classes/pltableeditor.php +++ b/classes/pltableeditor.php @@ -179,6 +179,19 @@ class PLTableEditor { $page->assign('id', $id); $list = false; } + if ($action == 'massadd') { + $importer = new CSVImporter($this->table, $this->idfield_editable ? $this->idfield : null); + $fields = array(); + foreach ($this->vars as $field=>$descr) { + if ($this->idfield_editable || $field != $this->idfield) { + $fields[] = $field; + $importer->describe($field, @$descr['desc']); + } + } + $page->assign('massadd', true); + $importer->apply($page, $this->pl . '/massadd', $fields); + $list = false; + } if ($action == 'new') { if (!$this->idfield_editable) { $r = XDB::query("SELECT MAX({$this->idfield})+1 FROM {$this->table}"); diff --git a/templates/include/csv-importer.tpl b/templates/include/csv-importer.tpl index 5765dc7..d023d88 100644 --- a/templates/include/csv-importer.tpl +++ b/templates/include/csv-importer.tpl @@ -105,9 +105,11 @@ val3_1;val3_2;... + {if $csv_key} + {/if} @@ -120,7 +122,7 @@ val3_1;val3_2;... {foreach from=$csv_fields item=f} - +
{$f}{$csv_field_desc[$f]|default:$f} {foreach from=$csv_fields item=f} - + {/foreach} {foreach from=$csv_preview item=assoc} diff --git a/templates/table-editor.tpl b/templates/table-editor.tpl index 4c6b7d6..fed0d38 100644 --- a/templates/table-editor.tpl +++ b/templates/table-editor.tpl @@ -42,9 +42,13 @@ {if !$readonly} - - + {/if} @@ -84,6 +88,13 @@

{/if} +{elseif $massadd} +{include file="include/csv-importer.tpl"} + +

+back +

+ {else} -- 2.1.4
{$f}{$csv_field_desc[$f]|default:$f}
nouvelle entrée - créer{icon name=add title='nouvelle entrée'} + + + Nouvelles entrées : ManuellementDepuis un CSV + + + {icon name=add title='nouvelle entrée'}