Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
ajax_insert.php
Go to the documentation of this file.
1 <?php
26 
27  function handle($params){
28 
29 
31  $query = $app->getQuery();
32  try {
33 
34  if ( !@$_POST['-table'] ){
35  throw new Exception("No table was specified");
36  }
37 
38 
39  $vals = array();
40  foreach ($query as $k=>$v){
41  if ( $k and $k{0} != '-' ) $vals[$k] = $v;
42  }
43 
44  $record = new Dataface_Record($_POST['-table'], array());
45 
46  $record->setValues($vals);
47  if ( !$record->checkPermission('ajax_save') ){
48  throw new Exception("Permission Denied", 502);
49  }
50  $res = $record->save(null, true);
51  if ( PEAR::isError($res) ){
52  error_log($res->getMessage(), $res->getCode());
53  throw new Exception("Failed to save record due to a server error. See log for details.");
54  }
55 
56  $this->out(array(
57  'code' => 200,
58  'message' => 'Successfully inserted record.',
59  'recordId' => $record->getId()
60  ));
61 
62  } catch (Exception $ex){
63  $this->out(array(
64  'code' => $ex->getCode(),
65  'message' => $ex->getMessage()
66  ));
67 
68  }
69 
70  }
71 
72 
73  function out($params){
74  header('Content-type: application/json; charset="'.Dataface_Application::getInstance()->_conf['oe'].'"');
75  $out = json_encode($params);
76  header('Content-Length: '.strlen($out));
77  header('Connection: close');
78  echo $out;
79  flush();
80  }
81 
82 }