Add support for secondary tables for editing
authorJoey Schulze <joey@infodrom.org>
Thu, 4 Mar 2010 21:15:47 +0000 (22:15 +0100)
committerJoey Schulze <joey@infodrom.org>
Thu, 4 Mar 2010 21:15:47 +0000 (22:15 +0100)
ajax/ajax.php

index 4ab5aa2..2b1acac 100644 (file)
@@ -101,9 +101,10 @@ function save($mask)
     }
   }
 
-  $sql = 'UPDATE ' . $mask['table'] . ' SET ';
-  $sql .= implode(', ', $update);
-  $sql .= ' WHERE id = ' . intval($_POST['id']);
+  $sql = sprintf('UPDATE %s SET %s WHERE id = %d',
+                empty($mask['edit_table']) ? $mask['table'] : $mask['edit_table'],
+                implode(', ', $update),
+                intval($_POST['id']));
 
   $sth = pg_query($sql);
 
@@ -155,8 +156,10 @@ function insert($mask)
     }
   }
 
-  $sql = 'INSERT INTO ' . $mask['table'] . ' (' . implode(',', $fields) . ') ';
-  $sql .= 'VALUES (' . implode(',', $values) . ')';
+  $sql = sprintf('INSERT INTO %s (%s) VALUES (%s)',
+                 empty($mask['edit_table']) ? $mask['table'] : $mask['edit_table'],
+                implode(',', $fields),
+                implode(',', $values));
 
   $sth = pg_query($sql);
 
@@ -174,6 +177,9 @@ function delete_or_copy($mask)
   if (empty($_POST['id']))
     return array('error' => 'Missing ID');
 
+  if (!empty($mask['edit_table']))
+    return array('error' => 'Cannot handle deletion for secondary table');
+
   if (DELETE_COPY === true) {
     $sql = sprintf("INSERT INTO %s_deleted SELECT * FROM %s WHERE id = %d",
                   $mask['table'], $mask['table'], $_POST['id']);