Removes trailing spaces.
[platal.git] / classes / pltableeditor.php
index d364d0e..f61dd4f 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2008 Polytechnique.org                              *
+ *  Copyright (C) 2003-2010 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -27,6 +27,21 @@ class PLTableEditor
     public $table;
     // joint tables to delete when deleting an entry
     public $jtables = array();
+    /** joint tables to add optional infos
+     * associative array : keys are the tables names, values are the joint
+     * clauses
+     * @see add_option_fields
+     */
+    public $otables = array();
+    /** optional fields
+     * These fields are used to display additionnal infos in listing (an only
+     * there). The additionnal infos are retreived from other tables. This var
+     * is an associative array : keys are the sql name of the fields
+     * (table.field) where table must be in $otables, values are a list of the
+     * name used in $vars, the description and the type of the field, and the
+     * var it should precede.
+     */
+    public $ofields = array();
     // sorting field
     public $sort = array();
     // the id field
@@ -164,6 +179,32 @@ class PLTableEditor
             $this->jtables[$name] = array("joinid" => $joinid,"joinextra" => $joinextra?(" AND ".$joinextra):"");
     }
 
+    /** Add optional table
+     * @see add_option_field
+     * @param $name the table sql name
+     * @param $jointclause the full joint clause. Use t as main table alias
+     * name.
+     */
+    public function add_option_table($name, $jointclause)
+    {
+        $this->otables[$name] = $jointclause;
+    }
+
+    /** Add optional field
+     * These fields are used to display additionnal infos in listing (and only
+     * there). The additionnal infos are retreived from other tables.
+     * @param $sqlname is the full sql name (table.field) where table must be
+     * added with add_option_table
+     * @param $name the name used for sort (please make it different from
+     * other fields in main table or subtables)
+     * @param $desc the description displayed as column header
+     * @param $type the typed used for display
+     */
+    public function add_option_field($sqlname, $name, $desc, $type = null, $nextvar = null)
+    {
+        $this->ofields[$sqlname] = array($name, $desc, $type, $nextvar);
+    }
+
     // add a sort key
     public function add_sort_field($key, $desc = false, $default = false)
     {
@@ -179,7 +220,7 @@ class PLTableEditor
     {
         $this->whereclause = $whereclause;
     }
-    
+
     // set an action when trying to delete row
     public function on_delete($action = NULL, $message = NULL)
     {
@@ -190,9 +231,9 @@ class PLTableEditor
     // call when done
     public function apply(PlPage &$page, $action, $id = false)
     {
-        $page->changeTpl('core/table-editor.tpl');
+        $page->coreTpl('table-editor.tpl');
         $list = true;
-        if ($action == 'delete') {
+        if ($action == 'delete' && $id !== false) {
             S::assert_xsrf_token();
 
             if (!isset($this->delete_action)) {
@@ -211,7 +252,7 @@ class PLTableEditor
                 $page->trigError("Impossible de supprimer l'entrée.");
             }
         }
-        if ($action == 'edit') {
+        if ($action == 'edit' && $id !== false) {
             $r = XDB::query("SELECT * FROM {$this->table} WHERE {$this->idfield} = {?} AND {$this->whereclause}",$id);
             $entry = $r->fetchOneAssoc();
             $page->assign('entry', $this->prepare_edit($entry));
@@ -233,8 +274,6 @@ class PLTableEditor
         }
         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;
@@ -246,9 +285,13 @@ class PLTableEditor
             $cancel = false;
             foreach ($this->vars as $field => $descr) {
                 if ($values) $values .= ',';
-                if (($field == $this->idfield) && !$this->idfield_editable)
-                    $val = "'".addslashes($id)."'";
-                elseif ($descr['Type'] == 'set') {
+                if (($field == $this->idfield) && !$this->idfield_editable) {
+                    if ($id === false || $id === null) {
+                        $val = "'".addslashes(XDB::fetchOneCell("SELECT MAX( {$field} ) + 1 FROM {$this->table}"))."'";
+                    } else {
+                        $val = "'".addslashes($id)."'";
+                    }
+                } elseif ($descr['Type'] == 'set') {
                     $val = "";
                     if (Post::has($field)) foreach (Post::v($field) as $option) {
                         if ($val) $val .= ',';
@@ -276,12 +319,12 @@ class PLTableEditor
                 $values .= $val;
             }
             if (!$cancel) {
-                if ($this->idfield_editable && ($id != Post::v($this->idfield)) && $action != 'new')
+                if ($this->idfield_editable && $id != Post::v($this->idfield))
                     XDB::execute("UPDATE {$this->table} SET {$this->idfield} = {?} WHERE {$this->idfield} = {?} AND {$this->whereclause}", Post::v($this->idfield), $id);
                 XDB::execute("REPLACE INTO {$this->table} VALUES ($values)");
-                if ($id !== false)
+                if ($id !== false && $id !== null) {
                     $page->trigSuccess("L'entrée ".$id." a été mise à jour.");
-                else {
+                else {
                     $page->trigSuccess("Une nouvelle entrée a été créée.");
                     $id = XDB::insertId();
                 }
@@ -310,7 +353,46 @@ class PLTableEditor
             if (count($this->sort) > 0) {
                 $sort = 'ORDER BY ' . join($this->sort, ',');
             }
-            $it = XDB::iterator("SELECT * FROM {$this->table} WHERE {$this->whereclause} $sort");
+            // optional infos columns
+            $optional_fields = '';
+            if (count($this->ofields)) {
+                $order = array();
+                foreach ($this->vars as $i => $aliasname) {
+                    $order[sprintf('%f',count($order))] = $i;
+                }
+                // delta for indexing optional columns between existing ones
+                $changeorder = 0.5;
+                foreach ($this->ofields as $sqlname => $ofieldvalues) {
+                    list($aliasname, $desc, $type, $nextvar) = $ofieldvalues;
+                    $optional_fields .= ', '.$sqlname.' AS '.$aliasname;
+                    $this->describe($aliasname, $desc, true);
+                    $this->vars[$aliasname]['optional'] = true;
+                    if (isset($type)) {
+                        $this->vars[$aliasname]['Type'] = $type;
+                    }
+                    if (isset($nextvar) && isset($this->vars[$nextvar]) && $nextvar != $aliasname) {
+                        $nextkey = array_search($nextvar, $order);
+                        $order[sprintf('%f',$nextkey - $changeorder)] = $aliasname;
+                        $changeorder = $changeorder / 2;
+                    } else {
+                        $order[sprintf('%f',count($order))] = $aliasname;
+                    }
+                    $this->vars[$aliasname]['Field'] = $aliasname;
+                }
+                if ($changeorder != 0.5) {
+                    ksort($order);
+                    $orderedvars = array();
+                    foreach ($order as $aliasname) {
+                        $orderedvars[$aliasname] = $this->vars[$aliasname];
+                    }
+                    $this->vars = $orderedvars;
+                }
+            }
+            $optional_joints = '';
+            foreach ($this->otables as $tablename => $jointclause) {
+                $optional_joints .= ' LEFT JOIN '.$tablename.' ON ('.$jointclause.')';
+            }
+            $it = XDB::iterator("SELECT t.* {$optional_fields} FROM {$this->table} AS t {$optional_joints} WHERE {$this->whereclause} $sort");
             $this->nbfields = 0;
             foreach ($this->vars as $field => $descr)
                 if ($descr['display']) $this->nbfields++;