Fix timestamps in carnet RSS feed
[platal.git] / classes / pltableeditor.php
index 58894d6..955ac30 100644 (file)
@@ -38,6 +38,11 @@ class PLTableEditor {
     var $nbfields;
     // the field for sorting entries
     var $sortfield;
+    var $sortdesc = false;
+    // action to do to delete row:
+       // null => delete effectively, false => no deletion, SQL
+    var $delete_action;
+    var $delete_message;
     /* table editor for platal
      * $plname : the PLname of the page, ex: admin/payments
      * $table : the table to edit, ex: profile_medals
@@ -56,7 +61,7 @@ class PLTableEditor {
             // desc will be the title of the column
             $a['desc'] = $a['Field'];
             $a['display'] = true;
-            
+
             if (substr($a['Type'],0,8) == 'varchar(') {
                 // limit editing box size
                 $a['Size'] = $a['Maxlength'] = substr($a['Type'], 8, strlen($a['Type']) - 9);
@@ -97,7 +102,7 @@ class PLTableEditor {
             if ($descr['Type'] == 'timestamp') {
                 // set readable timestamp
                 $date =& $entry[$field];
-                $date = preg_replace('/([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/', '\3/\2/\1 \4:\5:\6', $date); 
+                $date = preg_replace('/([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/', '\3/\2/\1 \4:\5:\6', $date);
             }
             if ($descr['Type'] == 'date') {
                 $date =& $entry[$field];
@@ -111,27 +116,46 @@ class PLTableEditor {
         $this->vars[$name]['desc'] = $desc;
         $this->vars[$name]['display'] = $display;
     }
-    // add a join table, when deleting a row corresponding entries will be deleted in these tables 
+    // add a join table, when deleting a row corresponding entries will be deleted in these tables
     function add_join_table($name,$joinid,$joindel,$joinextra="") {
         if ($joindel)
             $this->jtables[$name] = array("joinid" => $joinid,"joinextra" => $joinextra?(" AND ".$joinextra):"");
     }
-
     // add a sort key
-    function add_sort_field($key, $desc = false)
+    function add_sort_field($key, $desc = false, $default = false)
     {
-        $this->sort[] = $key . ($desc ? ' DESC' : '');
+       if ($default) {
+                       $this->sortfield = $key . ($desc ? ' DESC' : '');
+               } else {
+               $this->sort[] = $key . ($desc ? ' DESC' : '');
+        }
+    }
+    // set an action when trying to delete row
+    function on_delete($action = NULL, $message = NULL)
+    {
+       $this->delete_action = $action;
+       $this->delete_message = $message;
     }
-
     // call when done
     function apply(&$page, $action, $id = false) {
         $page->changeTpl('table-editor.tpl');
         $list = true;
         if ($action == 'delete') {
-            foreach ($this->jtables as $table => $j)
-                XDB::execute("DELETE FROM {$table} WHERE {$j['joinid']} = {?}{$j['joinextra']}", $id);
-            XDB::execute("DELETE FROM {$this->table} WHERE {$this->idfield} = {?}",$id);
-            $page->trig("L'entrée ".$id." a été supprimée.");
+               if (!isset($this->delete_action)) {
+                   foreach ($this->jtables as $table => $j)
+                       XDB::execute("DELETE FROM {$table} WHERE {$j['joinid']} = {?}{$j['joinextra']}", $id);
+                   XDB::execute("DELETE FROM {$this->table} WHERE {$this->idfield} = {?}",$id);
+                   $page->trig("L'entrée ".$id." a été supprimée.");
+               } else if ($this->delete_action) {
+                       XDB::execute($this->delete_action, $id);
+                       if (isset($this->delete_message)) {
+                       $page->trig($this->delete_message);
+                       } else {
+                       $page->trig("L'entrée ".$id." a été supprimée.");
+                               }                       
+               } else {
+                   $page->trig("Impossible de supprimer l'entrée.");
+               }
         }
         if ($action == 'edit') {
             $r = XDB::query("SELECT * FROM {$this->table} WHERE {$this->idfield} = {?}",$id);
@@ -162,9 +186,9 @@ class PLTableEditor {
                     }
                     $val = "'".addslashes($val)."'";
                 } elseif (Post::has($field)) {
-                    $val = Post::v($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); 
+                        $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') {
                         $val = preg_replace('/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4})/', '\3-\2-\1', $val);
@@ -172,7 +196,7 @@ class PLTableEditor {
                     $val = "'".addslashes($val)."'";
                 } else {
                     $cancel = true;
-                    $page->trig("Il manque le champ ".$field); 
+                    $page->trig("Il manque le champ ".$field);
                 }
                 $values .= $val;
             }
@@ -187,19 +211,21 @@ class PLTableEditor {
             } else
                 $page->trig("Impossible de mette à jour.");
         }
+        if ($action == 'sort') {
+               $this->sortfield = $id;
+        }
+        if ($action == 'sortdesc') {
+               $this->sortfield = $id.' DESC';
+        }
         if ($list) {
             // user can sort by field by clicking the title of the column
-            if ($action == 'sort' || $action == 'sortdesc') {
-                if (isset($this->vars[$id])) {
-                    // add this sort order after the others (chosen by dev)
-                    $this->add_sort_field($id, $action == 'sortdesc');
-                    // set as sortfield to reverse sort when clicking again
-                    if ($action == 'sort') {
-                        $this->sortfield = $id;
-                    } else {
-                        $this->sortfield = '';
-                    }
-                } 
+            if (isset($this->sortfield)) {
+                // add this sort order after the others (chosen by dev)
+                $this->add_sort_field($this->sortfield);
+                if (substr($this->sortfield,-5) == ' DESC') {
+                    $this->sortfield = substr($this->sortfield,0,-5);
+                    $this->sortdesc = true;
+                }
             }
             if (count($this->sort) > 0) {
                 $sort = 'ORDER BY ' . join($this->sort, ',');