From: Joey Schulze Date: Wed, 20 Jan 2010 14:05:56 +0000 (+0100) Subject: Backend function to provide some details X-Git-Tag: 2010-06-02_customer~259 X-Git-Url: https://git.infodrom.org/?p=misc%2Fkostenrechnung;a=commitdiff_plain;h=e047820afaee83f756061bb26ff17ae20021420f Backend function to provide some details --- diff --git a/ajax/ajax.php b/ajax/ajax.php index 2256e9d..1dc5c35 100644 --- a/ajax/ajax.php +++ b/ajax/ajax.php @@ -13,6 +13,30 @@ function fetch($mask) $sth = pg_query($sql); } +function details($mask) +{ + if (empty($_POST['id'])) + return array('error' => 'Missing ID'); + + $sql = sprintf('SELECT id,%s FROM %s WHERE id = %d', + implode(',', array_keys($mask['details']['list'])), + $mask['table'], $_POST['id']); + error_log($sql); + $sth = pg_query($sql); + + if (!$sth) + return array('error' => pg_last_error(), + 'sql' => $sql); + + $row = pg_fetch_assoc($sth); + + foreach ($mask['details']['list'] as $field => $info) + if (array_key_exists('format', $info)) + $row[$field] = sprintf($info['format'], $row[$field]); + + return $row; +} + function format_ajax($data) { header('Content-type: application/json; charset=UTF-8'); @@ -29,10 +53,16 @@ if (empty($_POST['mask'])) connect_db(); if (load_mask($_POST['mask'], '../') === false) exit; +$data = array('error' => 'Unknown function'); + if ($_POST['func'] == 'fetch') { - format_ajax(fetch($mask)); + $data = fetch($mask); +} elseif ($_POST['func'] == 'details') { + $data = details($mask); } elseif ($_POST['func'] == 'save') { error_log('save'); } -?> \ No newline at end of file +format_ajax($data); + +?>