Reorganise variables as array so that postcall works again
[misc/kostenrechnung] / ajax / ajax.php
index bbef0e4..5f2e3c9 100644 (file)
@@ -6,7 +6,9 @@ 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')
+    if ($info['type'] == 'date')
+      $fields[] = sprintf("to_char(%s,'DD.MM.YYYY') AS %s", $field, $field);
+    elseif ($info['type'] != 'passwd')
       $fields[] = $field;
 
   $sql = sprintf('SELECT id,%s FROM %s WHERE id = %d',
@@ -67,10 +69,14 @@ 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') {
+    } elseif ($info['type'] == 'number' || ($info['type'] == 'select' && $info['options_string'] !== true)) {
       if (empty($_POST[$field]) && $info['null'] === true)
         $update[] = sprintf("%s=NULL", $field);
       else
@@ -89,6 +95,7 @@ function save($mask)
       else
         $update[] = sprintf("%s='%s'", $field, pg_escape_string($_POST[$field]));
     }
+  }
 
   $sql = 'UPDATE ' . $mask['table'] . ' SET ';
   $sql .= implode(', ', $update);
@@ -110,11 +117,15 @@ 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'] == 'select' && $info['options_string'] !== true)) {
       $fields[] = $field;
       if (empty($_POST[$field]) && $info['null'] === true)
        $values[] = 'NULL';
@@ -138,6 +149,7 @@ function insert($mask)
       else
        $values[] = sprintf("'%s'", pg_escape_string($_POST[$field]));
     }
+  }
 
   $sql = 'INSERT INTO ' . $mask['table'] . ' (' . implode(',', $fields) . ') ';
   $sql .= 'VALUES (' . implode(',', $values) . ')';