Support select fields, add missing curley brackets
[misc/kostenrechnung] / ajax / ajax.php
index bbef0e4..a5a8a12 100644 (file)
@@ -67,10 +67,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') {
       if (empty($_POST[$field]) && $info['null'] === true)
         $update[] = sprintf("%s=NULL", $field);
       else
@@ -89,6 +93,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 +115,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') {
       $fields[] = $field;
       if (empty($_POST[$field]) && $info['null'] === true)
        $values[] = 'NULL';
@@ -138,6 +147,7 @@ function insert($mask)
       else
        $values[] = sprintf("'%s'", pg_escape_string($_POST[$field]));
     }
+  }
 
   $sql = 'INSERT INTO ' . $mask['table'] . ' (' . implode(',', $fields) . ') ';
   $sql .= 'VALUES (' . implode(',', $values) . ')';