Table editor integrates CSVImporter and minor code cleaning
authorx2003bruneau <x2003bruneau@839d8a87-29fc-0310-9880-83ba4fa771e5>
Sun, 17 Dec 2006 14:20:24 +0000 (14:20 +0000)
committerx2003bruneau <x2003bruneau@839d8a87-29fc-0310-9880-83ba4fa771e5>
Sun, 17 Dec 2006 14:20:24 +0000 (14:20 +0000)
git-svn-id: svn+ssh://murphy/home/svn/platal/trunk@1284 839d8a87-29fc-0310-9880-83ba4fa771e5

classes/csvimporter.php
classes/pltableeditor.php
templates/include/csv-importer.tpl
templates/table-editor.tpl

index 45a0dc1..606f444 100644 (file)
@@ -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);  
index 3fe9dce..cad848a 100644 (file)
@@ -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}");
index 5765dc7..d023d88 100644 (file)
@@ -105,9 +105,11 @@ val3_1;val3_2;...</pre>
       <option value="replace" {if $smarty.request.csv_action eq 'replace'}selected="selected"{/if}>
         remplacer par la nouvelle entrée
       </option>
+      {if $csv_key}
       <option value="update" {if $smarty.request.csv_action eq 'update'}selected="selected"{/if}>
         mettre à jour les champs sélectionnés
       </option>
+      {/if}
     </select>
   </div>
   <table class="bicol">
@@ -120,7 +122,7 @@ val3_1;val3_2;...</pre>
     </tr>
     {foreach from=$csv_fields item=f}
     <tr class="{cycle values="pair,impair"}">
-      <td>{$f}</td>
+      <td>{$csv_field_desc[$f]|default:$f}</td>
       <td>
         <select name="csv_value[{$f}]" onchange="showValue('{$f}', this);">
           <option value="" {if !$smarty.request.csv_value[$f]}selected="selected"{/if}>
@@ -205,7 +207,7 @@ val3_1;val3_2;...</pre>
   <table class="bicol">
     <tr>
       {foreach from=$csv_fields item=f}
-      <th>{$f}</th>
+      <th>{$csv_field_desc[$f]|default:$f}</th>
       {/foreach}
     </tr>
     {foreach from=$csv_preview item=assoc}
index 4c6b7d6..fed0d38 100644 (file)
 </tr>
 {if !$readonly}
 <tr class="impair">
-  <td colspan="{$t->nbfields}"><strong>nouvelle entrée</strong></td>
-  <td class="action">
-    <a href="{$t->pl}/new">créer{icon name=add title='nouvelle entrée'}</a>
+  <td colspan="{$t->nbfields}">
+    <strong>
+      Nouvelles entrées : <a href="{$t->pl}/new">Manuellement</a> &bull; <a href="{$t->pl}/massadd">Depuis un CSV</a>
+    </strong>
+  </td>
+  <td class="right">
+    <a href="{$t->pl}/new">{icon name=add title='nouvelle entrée'}</a>
   </td>
 </tr>
 {/if}
 </p>
 {/if}
 
+{elseif $massadd}
+{include file="include/csv-importer.tpl"}
+
+<p>
+<a href="{$t->pl}">back</a>
+</p>
+
 {else}
 
 <form method="post" action="{$t->pl}/update/{$id}">