Arrange templates by module
[platal.git] / classes / pltableeditor.php
index 955ac30..d1dca33 100644 (file)
@@ -49,7 +49,8 @@ class PLTableEditor {
      * $idfield : the field of the table which is the id, ex: id
      * $editid : is the id editable or not (if not, it is considered as an int)
      */
-    function PLTableEditor($plname, $table, $idfield, $editid=false) {
+    function PLTableEditor($plname, $table, $idfield, $editid=false)
+    {
         $this->pl = $plname;
         $this->table = $table;
         $this->idfield = $idfield;
@@ -74,7 +75,12 @@ class PLTableEditor {
             elseif (substr($a['Type'],0,4) == 'set(') {
                 // get the list of options
                 $a['List'] = explode('§',str_replace("','","§",substr($a['Type'], 5, strlen($a['Type']) - 7)));
-                $a['Type'] = 'set';
+                if (count($a['List']) == 1) {
+                    $a['Type'] = 'checkbox';
+                    $a['Value'] = $a['List'][0];
+                } else {
+                    $a['Type'] = 'set';
+                } 
             }
             elseif (substr($a['Type'],0,5) == 'enum(') {
                 // get the list of options
@@ -89,6 +95,15 @@ class PLTableEditor {
         }
         $this->vars[$idfield]['desc'] = 'id';
     }
+    // called before creating a new entry
+    function prepare_new()
+    {
+        $entry = array();
+        foreach ($this->vars as $field => $descr) {
+            $entry[$field] = $descr['Default'];
+        }
+        return $this->prepare_edit($entry);
+    }
     // called before editing $entry
     function prepare_edit(&$entry) {
         foreach ($this->vars as $field => $descr) {
@@ -138,7 +153,7 @@ class PLTableEditor {
     }
     // call when done
     function apply(&$page, $action, $id = false) {
-        $page->changeTpl('table-editor.tpl');
+        $page->changeTpl('core/table-editor.tpl');
         $list = true;
         if ($action == 'delete') {
                if (!isset($this->delete_action)) {
@@ -164,10 +179,24 @@ 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}");
                 $page->assign('id', $r->fetchOneCell());
+                $page->assign('entry', $this->prepare_new());
             }
             $list = false;
         }
@@ -185,6 +214,8 @@ class PLTableEditor {
                         $val .= $option;
                     }
                     $val = "'".addslashes($val)."'";
+                } elseif ($descr['Type'] == 'checkbox') {
+                    $val = Post::has($field)?"'".addslashes($descr['Value'])."'":"''";
                 } elseif (Post::has($field)) {
                     $val = Post::v($field);
                     if ($descr['Type'] == 'timestamp') {