Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
ajax_save.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['--record_id'] ){
35  throw new Exception("No record ID specified");
36  }
37 
38  $record = df_get_record_by_id($_POST['--record_id']);
39  if ( !$record ) throw new Exception("Record could not be found.", 404);
40 
41  $vals = array();
42  foreach ($query as $k=>$v){
43  if ( $k and $k{0} != '-' ) $vals[$k] = $v;
44  }
45 
46  $record->setValues($vals);
47  //print_r($record->getPermissions());exit;
48  if ( !$record->checkPermission('ajax_save') ){
49  throw new Exception("Permission Denied", 502);
50  }
51  if ( $record->recordChanged() ){
52  $res = $record->save(null, true);
53  if ( PEAR::isError($res) ){
54  error_log($res->getMessage(), $res->getCode());
55  throw new Exception("Failed to save record due to a server error. See log for details.");
56  }
57  $msg = 'Successfully saved record.';
58  } else {
59  $msg = 'Record is unchanged.';
60  }
61 
62  $this->out(array(
63  'code' => 200,
64  'message' => $msg,
65  'recordId' => $record->getId()
66  ));
67 
68  } catch (Exception $ex){
69  $this->out(array(
70  'code' => $ex->getCode(),
71  'message' => $ex->getMessage()
72  ));
73 
74  }
75 
76  }
77 
78 
79  function out($params){
80  header('Content-type: application/json; charset="'.Dataface_Application::getInstance()->_conf['oe'].'"');
81  $out = json_encode($params);
82  header('Content-Length: '.strlen($out));
83  header('Connection: close');
84  echo $out;
85  flush();
86  }
87 
88 }