Convert existing AJAX functions to new framework
authorJoey Schulze <joey@infodrom.org>
Sun, 9 Jul 2017 11:50:29 +0000 (13:50 +0200)
committerJoey Schulze <joey@infodrom.org>
Mon, 10 Jul 2017 20:48:29 +0000 (22:48 +0200)
class/ajaxbackend.class.php [deleted file]
class/ajaxbackendbase.class.php [deleted file]
class/sales.class.php
class/spritlog.class.php
class/spritmachine.class.php [new file with mode: 0644]
src/InfoCon/sprit/index.wml
src/InfoCon/sprit/list.wml
src/InfoCon/sprit/moduleajaxbackend.class.php [deleted file]
src/InfoCon/stempel/status.wml

diff --git a/class/ajaxbackend.class.php b/class/ajaxbackend.class.php
deleted file mode 100644 (file)
index 234974b..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-<?php
-
-class AJAXBackend extends AJAXBackendBase {
-  public function salesText()
-  {
-    $sales = new Sales($_POST['id']);
-    $sales->setDescription($_POST['text']);    
-  }
-}
-
diff --git a/class/ajaxbackendbase.class.php b/class/ajaxbackendbase.class.php
deleted file mode 100644 (file)
index cd12b17..0000000
+++ /dev/null
@@ -1,27 +0,0 @@
-<?php
-
-class AJAXBackendBase {
-  protected $db;
-  protected $relativeRootPath;
-
-  public function __construct()
-  {
-    global $db;
-    $this->db = $db;
-
-    $path = substr($_SERVER['HTTP_REFERER'], strpos($_SERVER['HTTP_REFERER'], $_SERVER['SERVER_NAME'])+strlen($_SERVER['SERVER_NAME'])+1);
-
-    $pos = 0;
-    $this->relativeRootPath = '';
-    while (($pos = strpos($path, '/', $pos)) !== false) {
-      $this->relativeRootPath .= '../';
-      $pos++;
-    }
-  }
-
-  public function relativeRootPath()
-  {
-    return $this->relativeRootPath;
-  }
-
-}
index 68e9987..e432746 100644 (file)
@@ -18,11 +18,10 @@ class Sales extends DatabaseTable {
     return $this->modify('paid', 0);
   }
 
-  public function setDescription($text)
+  public function ajaxText(Array $data)
   {
-    return $this->modify('description', $text);
+    return $this->modify('description', $data['text']);
   }
 
 }
 
-?>
index 2e501e4..e0b8f70 100644 (file)
@@ -9,7 +9,7 @@ class SpritLog extends DatabaseTable {
 
   public function distinctYears()
   {
-    $sql = "SELECT DISTINCT EXTRACT(YEAR from date) AS year FROM sprit_log ORDER BY year DESC";
+    $sql = "SELECT EXTRACT(YEAR from date) AS year,sum(price) AS sum,sum(km) AS km FROM sprit_log GROUP BY year ORDER BY year DESC";
     return $this->db->fetchObjectList($sql);
   }
 
@@ -35,12 +35,12 @@ class SpritLog extends DatabaseTable {
     }
 
     if (strlen($out)) {
-      $out = '<table class="smallfont" width="100%">' .
+      $out = '<table class="smallfont spritlog" width="100%">' .
        '<thead><tr>' .
        '<th width="70">Datum</th><th width="130" class="left">Ort</th><th class="left">Tankstelle</th>' .
        '<th width="40">EUR/l</th><th width="40">l</th><th width="40">EUR</th><th width="40">km</th><th width="40">gesamt</th>' .
        '</tr></thead>' .
-       $out .
+       '<tbody>' . $out . '</tbody>' .
        '<tfoot><tr>' .
        '<th colspan="4" class="left">Summe</th>' .
        sprintf('<th class="right">%.2f</th><th class="right">%.2f</th><th class="right">%d</th><th>&nbsp;</th>',
@@ -51,5 +51,37 @@ class SpritLog extends DatabaseTable {
     return $out;
   }
 
-}
+  public function ajaxList(Array $data)
+  {
+    return array('html' => array('list_'.$data['year'] => $this->formatYear($data['year'])));
+  }
+
+  public function ajaxAdd(Array $data)
+  {
+    $data = array('machine' => intval($data['machine']),
+                  'date' => assert_iso_date($data['date']),
+                  'city' => $data['city'],
+                  'tankstelle' => $data['tankstelle'],
+                  'price_liter' => str_replace(',','.',$data['price_liter']),
+                  'liter' => str_replace(',','.',$data['liter']),
+                  'price' => str_replace(',','.',$data['price']),
+                  'km' => intval($data['km']),
+                  'km_total' => intval($data['km_total']),
+                  'sys_edit' => 'now()',
+                  'sys_user' => $_SERVER['REMOTE_USER']);
+
+    foreach ($data as $k => $v)
+      if (empty($v))
+        return ajax_error(sprintf('Field %s must not be empty', $k));
 
+    if (empty($data['id'])) {
+      $ok = $this->db->insertInto('sprit_log', $data);
+    } else {
+      $ok = $this->db->update('sprit_log', $data, 'id = ' . intval($data['id']));
+    }
+
+    $d = explode('-', $data['date']);
+    return array('status' => $ok, 'year' => $d[0]);
+
+  }
+}
diff --git a/class/spritmachine.class.php b/class/spritmachine.class.php
new file mode 100644 (file)
index 0000000..df5047f
--- /dev/null
@@ -0,0 +1,45 @@
+<?php
+
+class SpritMachine extends DatabaseTable {
+
+  public function __construct($id=false)
+  {
+    parent::__construct('sprit_machine', $id);
+  }
+
+  public function ajaxList(Array $data)
+  {
+    $out = '';
+    $sql = "SELECT id,name FROM sprit_machine WHERE active = 1 ORDER BY name";
+    foreach ($this->fetchObjectList($sql) as $row) {
+      $out .= sprintf('<li>%s ', $row->name);
+      $out .= '<form action="list.php" method="POST" style="display:inline;">';
+      $out .= sprintf('<input type="hidden" name="machine" value="%d">', $row->id);
+      $out .= sprintf('<img src="%spix/arrowrightmonth.gif" onclick="machine_list(this);" style="margin-bottom:-4px;" title="Liste zeigen">',
+                      $this->rootPath);
+      $out .= '</form>';
+      $out .= '</li>';
+    }
+
+    return array('html' => array('machines' => $out));
+  }
+
+  public function ajaxSave(Array $data)
+  {
+    if ($this->id) {
+      $sql = sprintf("UPDATE sprit_machine SET name=%s,sys_edit=now(),sys_user=%s WHERE id = %d",
+                     $this->quote(utf8_decode($data['name'])),
+                     $this->quote($_SERVER['REMOTE_USER']),
+                     $this->id);
+    } else {
+      $sql = sprintf("INSERT INTO sprit_machine (name,active,sys_user,sys_edit) " .
+                     "VALUES (%s,1,%s,now())",
+                     $this->quote(utf8_decode($data['name'])),
+                     $this->quote($_SERVER['REMOTE_USER']));
+    }
+
+    return $this->execute($sql);
+  }
+
+}
+
index 8c00f34..abdd417 100644 (file)
@@ -34,10 +34,7 @@ $(function(){
 
 function load_machines()
 {
-    ajax_request('machines', null,
-                function(data){
-                    $('#machines').html(data.list);
-                });
+    $.invoke('SpritMachine/List');
 }
 
 function machine_new()
@@ -49,11 +46,11 @@ function machine_new()
 
 function machine_new_save()
 {
-    ajax_request('savemachine', $('#details input').serialize(),
-          function(data){
-              load_machines();
-              $('#details').hide();
-          });
+    $.invoke('SpritMachine/Save', $('#details input').serialize(), function(data){
+       load_machines();
+       $('#details').hide();
+    });
+
     return false;
 }
 
index c66cbed..8580a12 100644 (file)
@@ -8,6 +8,27 @@
 div#details {
     width: 270px;
 }
+div.bar {
+    background-color: #98c5e5;
+    border-bottom: 1px solid #AAA;
+    font-size: 14px;
+    font-weight: bold;
+}
+div.year {
+    float: left;
+}
+table.spritlog tbody td {
+    border-bottom: 1px solid #CCC;
+}
+table.spritlog thead th {
+    border-bottom: 1px solid #AAA;
+}
+table.spritlog tfoot th {
+    border-top: 1px solid #AAA;
+}
+table.spritlog tbody tr:hover {
+    background-color: yellow;
+}
 </style>
 
 <div id="details" class="popup" style="margin-top: 5px;display:none;">
@@ -40,32 +61,32 @@ div#details {
 &nbsp;&nbsp;&nbsp;
 <input type="submit" onclick="$('#details').hide();return false" value="Abbrechen">
 </div>
-</div>
+ </div>
 </div>
 
 <?php
   $log = new SpritLog();
   $list = $log->distinctYears();
   foreach ($list as $row) {
-    printf('<h3 year="%d" class="bar year">%d</h3>', $row->year, $row->year);
-    printf('<div id="list_%d"></div>', $row->year);
+    echo '<div class="bar">';
+    printf('<div year="%d" class="year">%d</div>', $row->year, $row->year);
+    printf('<div style="float:right;">&euro; %.2f&nbsp;&nbsp;%d km</div>', $row->sum, $row->km);
+    echo '<div style="clear:both;"></div></div>';
+    printf('<div id="list_%d" style="display:none;"></div>', $row->year);
   }
   if (count($list)) {
     $out = <<<EOT
 <script type="text/javascript">
 $(function(){
     load_year({$list[0]->year});
-    $('h3.year').click(function(e){
-       var year = \$(this).attr('year');
-       load_year(year);
-    });
+    $('div.bar').click(toggle_year);
 });
 </script>
 EOT;
 echo $out;
   }
 ?>
-
+<div style="height:10px;"></div>
 </page>
 <protect><script text="text/javascript">
 var sprit_machine = <?=intval($_POST['machine'])?>;
@@ -81,23 +102,27 @@ function log_new()
 
 function log_save()
 {
-    ajax_request('savelog', $('#details input').serialize(),
-          function(data){
-              load_year(data.year);
-              $('#details').hide();
-          });
+    $.invoke('SpritLog/Add', $('#details input').serialize(), function(data){
+       load_year(data.year);
+       $('#details').hide();
+    });
+
     return false;
 }
 
+function toggle_year(event)
+{
+    var year = $(this).find('div.year').attr('year');
+
+    if ($('div#list_'+year).is(':visible'))
+       $('div#list_'+year).hide();
+    else
+       load_year(year);
+}
+
 function load_year(year)
 {
-    ajax_request('loadyear', {year: year},
-                function(data){
-                    var div = $('div#list_'+data.year);
-                    if (div.length)
-                        div.html(data.list);
-                    else
-                        window.location.reload();
-                });
+    $('div#list_'+year).show();
+    $.invoke('SpritLog/List', {year: year});
 }
 </script></protect>
diff --git a/src/InfoCon/sprit/moduleajaxbackend.class.php b/src/InfoCon/sprit/moduleajaxbackend.class.php
deleted file mode 100644 (file)
index 5ccc0b3..0000000
+++ /dev/null
@@ -1,73 +0,0 @@
-<?php
-
-class ModuleAJAXBackend extends AJAXBackendBase {
-
-  public function ajax_machines()
-  {
-    $out = '';
-    $sql = "SELECT id,name FROM sprit_machine WHERE active = 1 ORDER BY name";
-    foreach ($this->db->fetchObjectList($sql) as $row) {
-      $out .= sprintf('<li>%s ', $row->name);
-      $out .= '<form action="list.php" method="POST" style="display:inline;">';
-      $out .= sprintf('<input type="hidden" name="machine" value="%d">', $row->id);
-      $out .= sprintf('<img src="%spix/arrowrightmonth.gif" onclick="machine_list(this);" style="margin-bottom:-4px;" title="Liste zeigen">',
-                     $this->relativeRootPath());;
-      $out .= '</form>';
-      $out .= '</li>';
-    }
-    return array('status' => true, 'list' => $out);;
-  }
-
-  public function ajax_savemachine()
-  {
-    if (empty($_POST['id'])) {
-      $sql = sprintf("INSERT INTO sprit_machine (name,active,sys_user,sys_edit) " .
-                    "VALUES (%s,1,%s,now())",
-                    $this->db->quote(utf8_decode($_POST['name'])),
-                    $this->db->quote($_SERVER['REMOTE_USER']));
-    } else {
-      $sql = sprintf("UPDATE sprit_machine SET name=%s,sys_edit=now(),sys_user=%s WHERE id = %d",
-                    $this->db->quote(utf8_decode($_POST['name'])),
-                    $this->db->quote($_SERVER['REMOTE_USER']),
-                    $_POST['id']);
-    }
-    return array('status' => $this->db->execute($sql));
-  }
-
-  public function ajax_savelog()
-  {
-    $data = array('machine' => intval($_POST['machine']),
-                 'date' => assert_iso_date($_POST['date']),
-                 'city' => $_POST['city'],
-                 'tankstelle' => $_POST['tankstelle'],
-                 'price_liter' => str_replace(',','.',$_POST['price_liter']),
-                 'liter' => str_replace(',','.',$_POST['liter']),
-                 'price' => str_replace(',','.',$_POST['price']),
-                 'km' => intval($_POST['km']),
-                 'km_total' => intval($_POST['km_total']),
-                 'sys_edit' => 'now()',
-                 'sys_user' => $_SERVER['REMOTE_USER']);
-
-    foreach ($data as $k => $v)
-      if (empty($v))
-       json_error(sprintf('Field %s must not be empty', $k));
-
-    if (empty($_POST['id'])) {
-      $ok = $this->db->insertInto('sprit_log', $data);
-    } else {
-      $ok = $this->db->update('sprit_log', $data, 'id = ' . intval($_POST['id']));
-    }
-
-    $d = explode('-', $data['date']);
-    return array('status' => $ok, 'year' => $d[0]);
-  }
-
-  public function ajax_loadyear()
-  {
-    $log = new SpritLog();
-    return array('status' => true,
-                'list' => $log->formatYear($_POST['year']),
-                'year' => $_POST['year']);
-  }
-
-}
index d52c94d..0da5bd8 100644 (file)
@@ -264,9 +264,7 @@ function edit_task(obj)
        task_oid = obj.parentNode.children[0].children[0].value;
 
        var input = $('<input>');
-<protect>
        input.val(obj.innerHTML.replace('&gt;', '>').replace('&lt;', '<').replace('&amp;', '&'));
-</protect>
        input.css('fontSize', '100%').css('width', '100%').css('background', 'yellow');
        obj.innerHTML = '';
        $(obj).append(input);
@@ -279,7 +277,7 @@ function edit_task_save()
     if (!task_parent) return;
 
     if (task_parent.children[0].value != task_title)
-       ajax_request('save','oid='+task_oid+'&task='+encodeURIComponent(task_parent.children[0].value));
+       $.invoke('Sales/Text', {id: task_oid, text: task_parent.children[0].value});
 
     task_parent.innerHTML = task_parent.children[0].value;
     task_parent = null;