Support calculation of details values via a function instead of an table column
authorJoey Schulze <joey@infodrom.org>
Thu, 4 Mar 2010 22:55:59 +0000 (23:55 +0100)
committerJoey Schulze <joey@infodrom.org>
Thu, 4 Mar 2010 22:55:59 +0000 (23:55 +0100)
ajax/ajax.php

index 2b1acac..e855674 100644 (file)
@@ -40,21 +40,37 @@ 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']);
+  $fields = array();
+  foreach ($mask['details']['list'] as $field => $info) {
+    if ($info['type'] == 'date')
+      $fields[] = sprintf("to_char(%s,'DD.MM.YYYY') AS %s",
+                         empty($info['sql']) ? $field : $info['sql'],
+                         $field);
+    elseif (!array_key_exists('fetch',$info))
+      $fields[] = empty($info['sql']) ? $field : $info['sql'] . ' AS ' . $field;
+  }
 
-  $sth = pg_query($sql);
+  if (count($fields)) {
+    $sql = sprintf('SELECT id,%s FROM %s WHERE id = %d',
+                  implode(',', $fields),
+                  $mask['table'], $_POST['id']);
 
-  if (!$sth)
-    return array('error' => pg_last_error(),
-                'sql' => $sql);
+    $sth = pg_query($sql);
 
-  $row = pg_fetch_assoc($sth);
+    if (!$sth)
+      return array('error' => pg_last_error(),
+                  'sql' => $sql);
+
+    $row = pg_fetch_assoc($sth);
+  } else {
+    $row = array();
+  }
 
   foreach ($mask['details']['list'] as $field => $info)
     if (array_key_exists('format', $info))
       $row[$field] = sprintf($info['format'], $row[$field]);
+    elseif (array_key_exists('fetch', $info))
+      $row[$field] = $info['fetch']();
 
   return $row;
 }