Merge remote branch 'origin/core/1.1.1/maint' into core/master
[platal.git] / classes / pltableeditor.php
index 616ce6c..88a4133 100644 (file)
@@ -38,7 +38,8 @@ class PLTableEditor
      * 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.
+     * name used in $vars, the description and the type of the field, and the
+     * var it should precede.
      */
     public $ofields = array();
     // sorting field
@@ -199,9 +200,9 @@ class PLTableEditor
      * @param $desc the description displayed as column header
      * @param $type the typed used for display
      */
-    public function add_option_field($sqlname, $name, $desc, $type)
+    public function add_option_field($sqlname, $name, $desc, $type = null, $nextvar = null)
     {
-        $this->ofields[$sqlname] = array($name, $desc, $type);
+        $this->ofields[$sqlname] = array($name, $desc, $type, $nextvar);
     }
 
     // add a sort key
@@ -219,7 +220,7 @@ class PLTableEditor
     {
         $this->whereclause = $whereclause;
     }
-    
+
     // set an action when trying to delete row
     public function on_delete($action = NULL, $message = NULL)
     {
@@ -232,7 +233,7 @@ class PLTableEditor
     {
         $page->coreTpl('table-editor.tpl');
         $list = true;
-        if ($action == 'delete') {
+        if ($action == 'delete' && $id !== false) {
             S::assert_xsrf_token();
 
             if (!isset($this->delete_action)) {
@@ -251,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));
@@ -280,51 +281,69 @@ class PLTableEditor
         if ($action == 'update') {
             S::assert_xsrf_token();
 
-            $values = "";
             $cancel = false;
+            $values = array();
+            $new = false;
             foreach ($this->vars as $field => $descr) {
-                if ($values) $values .= ',';
-                if (($field == $this->idfield) && !$this->idfield_editable)
-                    $val = "'".addslashes($id)."'";
-                elseif ($descr['Type'] == 'set') {
-                    $val = "";
-                    if (Post::has($field)) foreach (Post::v($field) as $option) {
-                        if ($val) $val .= ',';
-                        $val .= $option;
+                $val = null;
+                $new = ($id === false || $id === null);
+                if ($field == $this->idfield && !$this->idfield_editable) {
+                    if ($new) {
+                        $val = XDB::fetchOneCell("SELECT MAX({$field}) + 1
+                                                    FROM {$this->table}");
+                    } else {
+                        continue;
+                    }
+                } elseif ($descr['Type'] == 'set') {
+                    $val = new PlFlagset();
+                    if (Post::has($field)) {
+                        foreach (Post::v($field) as $option) {
+                            $val->addFlag($option);
+                        }
                     }
-                    $val = "'".addslashes($val)."'";
                 } elseif ($descr['Type'] == 'checkbox') {
-                    $val = Post::has($field)?"'".addslashes($descr['Value'])."'":"''";
+                    $val = Post::has($field)? $descr['Value'] : "";
                 } elseif (Post::has($field)) {
                     $val = Post::v($field);
                     if ($descr['Type'] == 'timestamp') {
                         $val = preg_replace('/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})/', '\3\2\1\4\5\6', $val);
-                    }
-                    elseif ($descr['Type'] == 'date') {
+                    } else if ($descr['Type'] == 'date') {
                         $val = preg_replace('/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4})/', '\3-\2-\1', $val);
-                    }
-                    elseif ($descr['Type'] == 'ip_address') {
+                    } elseif ($descr['Type'] == 'ip_address') {
                         $val = ip2long($val);
                     }
-                    $val = "'".addslashes($val)."'";
                 } else {
                     $cancel = true;
                     $page->trigError("Il manque le champ ".$field);
                 }
-                $values .= $val;
+                $values[$field] = XDB::escape($val);
             }
             if (!$cancel) {
-                if ($this->idfield_editable && ($id != Post::v($this->idfield)) && $action != 'new')
-                    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 (!$new) {
+                    $update = array();
+                    foreach ($values as $field => $value) {
+                        $update[] = $field . ' = ' . $value;
+                    }
+                    $update = implode(', ', $update);
+                    XDB::rawExecute("UPDATE {$this->table}
+                                        SET {$update}
+                                      WHERE {$this->idfield} = " . XDB::escape($id) . "
+                                            AND {$this->whereclause}");
+                } else {
+                    $fields = implode(', ', array_keys($values));
+                    $values = implode(', ', $values);
+                    XDB::rawExecute("INSERT INTO  {$this->table} ({$fields})
+                                          VALUES  ({$values})");
+                }
                 if ($id !== false && $id !== null) {
                     $page->trigSuccess("L'entrée ".$id." a été mise à jour.");
                 } else {
                     $page->trigSuccess("Une nouvelle entrée a été créée.");
                     $id = XDB::insertId();
                 }
-            } else
+            } else {
                 $page->trigError("Impossible de mettre à jour.");
+            }
             if (!$this->auto_return) {
                 return $this->apply($page, 'edit', $id);
             }
@@ -350,13 +369,38 @@ class PLTableEditor
             }
             // optional infos columns
             $optional_fields = '';
-            foreach ($this->ofields as $sqlname => $ofieldvalues) {
-                list($aliasname, $desc, $type) = $ofieldvalues;
-                $optional_fields .= ', '.$sqlname.' AS '.$aliasname;
-                $this->describe($aliasname, $desc, true);
-                $this->vars[$aliasname]['optional'] = true;
-                $this->vars[$aliasname]['Type'] = $type;
-                $this->vars[$aliasname]['Field'] = $aliasname;
+            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) {