Convert script to strict mode
[infodrom.org/service.infodrom.org] / class / spritlog.class.php
1 <?php
2
3 class SpritLog extends DatabaseTable {
4
5   public function __construct($id=false)
6   {
7     parent::__construct('sprit_log', $id);
8   }
9
10   public function distinctYears()
11   {
12     $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";
13     return $this->db->fetchObjectList($sql);
14   }
15
16   public function formatYear($year)
17   {
18     $out = '';
19     $sql = sprintf("SELECT * FROM sprit_log WHERE EXTRACT(YEAR from date) = %d ORDER BY date ASC", $year);;
20
21     $total_km = 0;
22     $total_liter = 0;
23     $total_price = 0;
24     foreach ($this->db->fetchObjectList($sql) as $row) {
25       $out .= sprintf('<tr id="%d"><td>%s</td><td>%s</td><td><span route="SpritLog/EditTankstelle" item_id="%d">%s</span></td>' .
26                       '<td class="right">%.2f</td><td class="right">%.2f</td><td class="right">%.2f</td>' .
27                       '<td class="right">%d</td><td class="right">%d</td></tr>',
28                       $row->id, assert_german_date($row->date),
29                       $row->city, $row->id, $row->tankstelle,
30                       $row->price_liter, $row->liter,$row->price,
31                       $row->km, $row->km_total);
32       $total_km += $row->km;
33       $total_liter += $row->liter;
34       $total_price += $row->price;
35     }
36
37     if (strlen($out)) {
38       $out = '<table class="smallfont spritlog" width="100%">' .
39         '<thead><tr>' .
40         '<th width="70">Datum</th><th width="130" class="left">Ort</th><th class="left">Tankstelle</th>' .
41         '<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>' .
42         '</tr></thead>' .
43         '<tbody>' . $out . '</tbody>' .
44         '<tfoot><tr>' .
45         '<th colspan="4" class="left">Summe</th>' .
46         sprintf('<th class="right">%.2f</th><th class="right">%.2f</th><th class="right">%d</th><th>&nbsp;</th>',
47                 $total_liter, $total_price, $total_km).
48         '</tr></tfoot>' .
49         '</table>';
50     }
51     return $out;
52   }
53
54   public function ajaxList(Array $data)
55   {
56     return array('html' => array('list_'.$data['year'] => $this->formatYear($data['year'])));
57   }
58
59   public function ajaxAdd(Array $data)
60   {
61     $data = array('machine' => intval($data['machine']),
62                   'date' => assert_iso_date($data['date']),
63                   'city' => $data['city'],
64                   'tankstelle' => $data['tankstelle'],
65                   'price_liter' => str_replace(',','.',$data['price_liter']),
66                   'liter' => str_replace(',','.',$data['liter']),
67                   'price' => str_replace(',','.',$data['price']),
68                   'km' => intval($data['km']),
69                   'km_total' => intval($data['km_total']),
70                   'sys_edit' => 'now()',
71                   'sys_user' => $_SERVER['REMOTE_USER']);
72
73     foreach ($data as $k => $v)
74       if (empty($v))
75         return ajax_error(sprintf('Field %s must not be empty', $k));
76
77     if (empty($data['id'])) {
78       $ok = $this->db->insertInto('sprit_log', $data);
79     } else {
80       $ok = $this->db->update('sprit_log', $data, 'id = ' . intval($data['id']));
81     }
82
83     $d = explode('-', $data['date']);
84     return array('status' => $ok, 'year' => $d[0]);
85   }
86
87   public function ajaxSuggestCity(Array $data)
88   {
89     $data['query'] .= '%';
90     $sql = sprintf("SELECT DISTINCT city FROM sprit_log WHERE lower(city) LIKE %s ORDER BY city",
91                    $this->quote(strtolower($data['query'])));
92     $list = array();
93     foreach ($this->fetchObjectList($sql) as $row)
94       $list[] = array('value' => $row->city, 'data' => $row->city);
95     return array('suggestions' => $list);
96   }
97
98   public function ajaxSuggestTankstelle(Array $data)
99   {
100     $data['query'] .= '%';
101     $sql = sprintf("SELECT DISTINCT tankstelle FROM sprit_log WHERE city = %s AND lower(tankstelle) LIKE %s ORDER BY tankstelle",
102                    $this->quote($data['city']),
103                    $this->quote(strtolower($data['query'])));
104     $list = array();
105     foreach ($this->fetchObjectList($sql) as $row)
106       $list[] = array('value' => $row->tankstelle, 'data' => $row->tankstelle);
107     return array('suggestions' => $list);
108   }
109
110   public function ajaxEditTankstelle(Array $data)
111   {
112     if (!strlen($data['content'])) return false;
113     return $this->modify('tankstelle', $data['content']);
114   }
115
116 }