From a3e5ae149d94ca1ace8eeab51ee7d6fe7dde01cc Mon Sep 17 00:00:00 2001 From: Joey Schulze Date: Sun, 9 Jul 2017 13:50:29 +0200 Subject: [PATCH] Convert existing AJAX functions to new framework --- class/ajaxbackend.class.php | 10 --- class/ajaxbackendbase.class.php | 27 ------- class/sales.class.php | 5 +- class/spritlog.class.php | 40 +++++++++- class/spritmachine.class.php | 45 ++++++++++++ src/InfoCon/sprit/index.wml | 15 ++-- src/InfoCon/sprit/list.wml | 67 +++++++++++------ src/InfoCon/sprit/moduleajaxbackend.class.php | 73 ------------------- src/InfoCon/stempel/status.wml | 4 +- 9 files changed, 136 insertions(+), 150 deletions(-) delete mode 100644 class/ajaxbackend.class.php delete mode 100644 class/ajaxbackendbase.class.php create mode 100644 class/spritmachine.class.php delete mode 100644 src/InfoCon/sprit/moduleajaxbackend.class.php diff --git a/class/ajaxbackend.class.php b/class/ajaxbackend.class.php deleted file mode 100644 index 234974b..0000000 --- a/class/ajaxbackend.class.php +++ /dev/null @@ -1,10 +0,0 @@ -setDescription($_POST['text']); - } -} - diff --git a/class/ajaxbackendbase.class.php b/class/ajaxbackendbase.class.php deleted file mode 100644 index cd12b17..0000000 --- a/class/ajaxbackendbase.class.php +++ /dev/null @@ -1,27 +0,0 @@ -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; - } - -} diff --git a/class/sales.class.php b/class/sales.class.php index 68e9987..e432746 100644 --- a/class/sales.class.php +++ b/class/sales.class.php @@ -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']); } } -?> diff --git a/class/spritlog.class.php b/class/spritlog.class.php index 2e501e4..e0b8f70 100644 --- a/class/spritlog.class.php +++ b/class/spritlog.class.php @@ -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 = '' . + $out = '
' . '' . '' . '' . '' . - $out . + '' . $out . '' . '' . '' . sprintf('', @@ -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 index 0000000..df5047f --- /dev/null +++ b/class/spritmachine.class.php @@ -0,0 +1,45 @@ +fetchObjectList($sql) as $row) { + $out .= sprintf('
  • %s ', $row->name); + $out .= '
    '; + $out .= sprintf('', $row->id); + $out .= sprintf('', + $this->rootPath); + $out .= ''; + $out .= '
  • '; + } + + 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); + } + +} + diff --git a/src/InfoCon/sprit/index.wml b/src/InfoCon/sprit/index.wml index 8c00f34..abdd417 100644 --- a/src/InfoCon/sprit/index.wml +++ b/src/InfoCon/sprit/index.wml @@ -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; } diff --git a/src/InfoCon/sprit/list.wml b/src/InfoCon/sprit/list.wml index c66cbed..8580a12 100644 --- a/src/InfoCon/sprit/list.wml +++ b/src/InfoCon/sprit/list.wml @@ -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; +} - + distinctYears(); foreach ($list as $row) { - printf('

    %d

    ', $row->year, $row->year); - printf('
    ', $row->year); + echo '
    '; + printf('
    %d
    ', $row->year, $row->year); + printf('
    € %.2f  %d km
    ', $row->sum, $row->km); + echo '
    '; + printf('', $row->year); } if (count($list)) { $out = << $(function(){ load_year({$list[0]->year}); - $('h3.year').click(function(e){ - var year = \$(this).attr('year'); - load_year(year); - }); + $('div.bar').click(toggle_year); }); EOT; echo $out; } ?> - +
    diff --git a/src/InfoCon/sprit/moduleajaxbackend.class.php b/src/InfoCon/sprit/moduleajaxbackend.class.php deleted file mode 100644 index 5ccc0b3..0000000 --- a/src/InfoCon/sprit/moduleajaxbackend.class.php +++ /dev/null @@ -1,73 +0,0 @@ -db->fetchObjectList($sql) as $row) { - $out .= sprintf('
  • %s ', $row->name); - $out .= '
    '; - $out .= sprintf('', $row->id); - $out .= sprintf('', - $this->relativeRootPath());; - $out .= ''; - $out .= '
  • '; - } - 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']); - } - -} diff --git a/src/InfoCon/stempel/status.wml b/src/InfoCon/stempel/status.wml index d52c94d..0da5bd8 100644 --- a/src/InfoCon/stempel/status.wml +++ b/src/InfoCon/stempel/status.wml @@ -264,9 +264,7 @@ function edit_task(obj) task_oid = obj.parentNode.children[0].children[0].value; var input = $(''); - input.val(obj.innerHTML.replace('>', '>').replace('<', '<').replace('&', '&')); - 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; -- 2.20.1
    DatumOrtTankstelleEUR/llEURkmgesamt
    Summe%.2f%.2f%d