Handle hidden fields as if they were integers
[misc/kostenrechnung] / ajax / ajax.php
index 3431c03..4ab5aa2 100644 (file)
@@ -5,9 +5,15 @@ require_once('../init.php');
 function fetch($mask)
 {
   $fields = array("to_char(sys_edit,'DD.MM.YYYY HH24:mm') AS sys_edit", 'sys_user');
-  foreach ($mask['edit'] as $field => $info)
-    if ($info['type'] != 'passwd')
-      $fields[] = $field;
+  foreach ($mask['edit'] as $field => $info) {
+    if ($info['sql'] === false) continue;
+    if ($info['type'] == 'date')
+      $fields[] = sprintf("to_char(%s,'DD.MM.YYYY') AS %s",
+                         empty($info['sql']) ? $field : $info['sql'],
+                         $field);
+    elseif ($info['type'] != 'passwd')
+      $fields[] = empty($info['sql']) ? $field : $info['sql'] . ' AS ' . $field;
+  }
 
   $sql = sprintf('SELECT id,%s FROM %s WHERE id = %d',
                 implode(',', $fields),
@@ -53,6 +59,12 @@ function details($mask)
   return $row;
 }
 
+function format_decimal($value)
+{
+  $value = str_replace(',','.',$value);
+  return sprintf("%.2f", $value);
+}
+
 function save($mask)
 {
   if (empty($_POST['id']))
@@ -61,17 +73,33 @@ function save($mask)
   $update = array(sprintf("sys_user = '%s'", pg_escape_string($_SESSION['sys']['login'])),
                  "sys_edit = now()");
 
-  foreach ($mask['edit'] as $field => $info)
+  foreach ($mask['edit'] as $field => $info) {
+    if ($info['required'] === true && empty($_POST[$field]))
+      return array('error' => sprintf('Pflichtfeld %s nicht ausgefüllt', $info['name']),
+                  'errormsg' => 'Pflichtfelder nicht ausgefüllt');
+
     if ($info['type'] == 'boolean') {
       $update[] = sprintf("%s=%d", $field, $_POST[$field] == 'on'?1:0);
-    } elseif ($info['type'] == 'number') {
-      $update[] = sprintf("%s=%d", $field, $_POST[$field]);
+    } elseif ($info['type'] == 'number' || $info['type'] == 'hidden' || ($info['type'] == 'select' && $info['options_string'] !== true)) {
+      if (empty($_POST[$field]) && $info['null'] === true)
+        $update[] = sprintf("%s=NULL", $field);
+      else
+       $update[] = sprintf("%s=%d", $field, $_POST[$field]);
+    } elseif ($info['type'] == 'decimal') {
+      if (empty($_POST[$field]) && $info['null'] === true)
+        $update[] = sprintf("%s=NULL", $field);
+      else
+       $update[] = sprintf("%s=%s", $field, format_decimal($_POST[$field]));
     } elseif ($info['type'] == 'passwd') {
       if (!empty($_POST[$field]))
        $update[] = sprintf("%s='%s'", $field, pg_escape_string(passwd($_SESSION['sys']['login'],$_POST[$field])));
     } else {
-      $update[] = sprintf("%s='%s'", $field, pg_escape_string($_POST[$field]));
+      if (empty($_POST[$field]) && $info['null'] === true)
+        $update[] = sprintf("%s=NULL", $field);
+      else
+        $update[] = sprintf("%s='%s'", $field, pg_escape_string($_POST[$field]));
     }
+  }
 
   $sql = 'UPDATE ' . $mask['table'] . ' SET ';
   $sql .= implode(', ', $update);
@@ -93,13 +121,26 @@ function insert($mask)
   $fields = array('sys_user','sys_edit');
   $values = array("'".pg_escape_string($_SESSION['sys']['login'])."'", 'now()');
 
-  foreach ($mask['edit'] as $field => $info)
+  foreach ($mask['edit'] as $field => $info) {
+    if ($info['required'] === true && empty($_POST[$field]))
+      return array('error' => sprintf('Pflichtfeld %s nicht ausgefüllt', $info['name']),
+                  'errormsg' => 'Pflichtfelder nicht ausgefüllt');
+
     if ($info['type'] == 'boolean') {
       $fields[] = $field;
       $values[] = $_POST[$field] == 'on'?1:0;
-    } elseif ($info['type'] == 'number') {
+    } elseif ($info['type'] == 'number' || $info['type'] == 'hidden' || ($info['type'] == 'select' && $info['options_string'] !== true)) {
+      $fields[] = $field;
+      if (empty($_POST[$field]) && $info['null'] === true)
+       $values[] = 'NULL';
+      else
+       $values[] = intval($_POST[$field]);
+    } elseif ($info['type'] == 'decimal') {
       $fields[] = $field;
-      $values[] = intval($_POST[$field]);
+      if (empty($_POST[$field]) && $info['null'] === true)
+       $values[] = 'NULL';
+      else
+       $values[] = format_decimal($_POST[$field]);
     } elseif ($info['type'] == 'passwd') {
       if (!empty($_POST[$field])) {
        $fields[] = $field;
@@ -107,8 +148,12 @@ function insert($mask)
       }
     } else {
       $fields[] = $field;
-      $values[] = sprintf("'%s'", pg_escape_string($_POST[$field]));
+      if (empty($_POST[$field]) && $info['null'] === true)
+       $values[] = 'NULL';
+      else
+       $values[] = sprintf("'%s'", pg_escape_string($_POST[$field]));
     }
+  }
 
   $sql = 'INSERT INTO ' . $mask['table'] . ' (' . implode(',', $fields) . ') ';
   $sql .= 'VALUES (' . implode(',', $values) . ')';
@@ -166,6 +211,41 @@ function delete_or_copy($mask)
   return array('status' => true);
 }
 
+function set_variable($name,$mask)
+{
+  if (!array_key_exists('variables',$mask))
+    return array('error' => 'Unknown variable ' . htmlspecialchars($_POST['name']));
+
+  if (!array_key_exists($_POST['name'],$mask['variables']))
+    return array('error' => 'Unknown variable ' . htmlspecialchars($_POST['name']));
+
+  $_SESSION[$name . '.' . $_POST['name']] = $_POST['value'];
+
+  if (array_key_exists('postcall',$mask['variables'][$_POST['name']]))
+    $mask['variables'][$_POST['name']]['postcall']();
+
+  return array('status' => true);
+}
+
+function get_infos($mask)
+{
+  if (!array_key_exists('info',$mask))
+    return array('error' => 'Unknown callback ' . htmlspecialchars($_POST['name']));
+
+  if (!array_key_exists($_POST['name'],$mask['info']))
+    return array('error' => 'Unknown callback ' . htmlspecialchars($_POST['name']));
+
+  if (!array_key_exists('sql',$mask['info'][$_POST['name']]))
+    return array('error' => 'Unknown callback ' . htmlspecialchars($_POST['name']));
+
+  $sql = $mask['info'][$_POST['name']]['sql'];
+
+  while (preg_match('/\{([^\}]*)\}/', $sql, $matches))
+    $sql = str_replace('{'.$matches[1].'}', $_POST[$matches[1]], $sql);
+
+  return array('info' => query_db($sql));
+}
+
 function format_ajax($data)
 {
   header('Content-type: application/json; charset=UTF-8');
@@ -194,6 +274,10 @@ if ($_POST['func'] == 'fetch') {
   $data = insert($mask);
 } elseif ($_POST['func'] == 'delete') {
   $data = delete_or_copy($mask);
+} elseif ($_POST['func'] == 'setvar') {
+  $data = set_variable($_POST['source'],$mask);
+} elseif ($_POST['func'] == 'info') {
+  $data = get_infos($mask);
 }
 
 format_ajax($data);