Convert existing AJAX functions to new framework
[infodrom.org/service.infodrom.org] / class / spritlog.class.php
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]);
+
+  }
+}