Backend function to provide some details
authorJoey Schulze <joey@infodrom.org>
Wed, 20 Jan 2010 14:05:56 +0000 (15:05 +0100)
committerJoey Schulze <joey@infodrom.org>
Wed, 20 Jan 2010 14:05:56 +0000 (15:05 +0100)
ajax/ajax.php

index 2256e9d..1dc5c35 100644 (file)
@@ -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);
+
+?>