Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
rest_delete.php
Go to the documentation of this file.
1 <?php
3 
4  const PERMISSION_DENIED = 401;
5  const NOT_FOUND=404;
6  const SERVER_ERROR=500;
7  const BAD_REQUEST = 400;
8 
9 
10  function handle($params){
12  $query = $app->getQuery();
13  $record_id = @$_POST['--record_id'];
14 
15  try {
16  if ( !$record_id ){
17  throw new Exception(
18  df_translate('Bad Request', 'Bad Request. Missing parameter.'),
19  self::BAD_REQUEST
20  );
21  }
22  $record = df_get_record_by_id($record_id);
23  if ( PEAR::isError($record) ){
24  error_log($record->getMessage());
25  throw new Exception(
26  df_translate('Bad Request', 'Bad Request - invalid ID.'),
27  self::BAD_REQUEST
28  );
29  }
30  if ( !$record ){
31  throw new Exception(
32  df_translate('No records matched request','No records matched the request'),
33  self::NOT_FOUND
34  );
35  }
36  if ( !$record->checkPermission('delete') ){
37  throw new Exception(
38  df_translate('scripts.GLOBAL.MESSAGE.PERMISSION_DENIED','Permission Denied'),
39  self::PERMISSION_DENIED
40  );
41  }
42 
43  $res = $record->delete(false); // We've already done a security check...
44  if ( PEAR::isError($res) ){
45  error_log($res->getMessage());
46  throw new Exception(
47  df_translate('actions.rest_delete.messages.SERVER_ERROR', 'Failed to delete record due to a server error. See error log for details.'),
48  self::SERVER_ERROR
49  );
50 
51  }
52 
53  $this->out(array(
54  'code'=>200,
55  'message'=>df_translate('actions.rest_delete.messages.SUCCESS', 'Successfully deleted record.'),
56  'record_id'=>$record->getId()
57  ));
58  exit;
59  } catch (Exception $ex){
60  switch ($ex->getCode() ){
61  case self::PERMISSION_DENIED:
62  case self::NOT_FOUND:
63  case self::SERVER_ERROR:
64  $msg = $ex->getMessage();
65  $code = $ex->getCode();
66  break;
67  default:
68  $msg = df_translate('actions.rest_delete.messages.SUCCESS', 'Successfully deleted record.');
69  $code = self::SERVER_ERROR;
70  error_log($ex->getMessage());
71  break;
72  }
73  $this->out(array(
74  'code' => $code,
75  'message' => $msg
76  ));
77  exit;
78  }
79  }
80 
81 
82  function out($params){
83  header('Content-type: application/json; charset="'.Dataface_Application::getInstance()->_conf['oe'].'"');
84  echo json_encode($params);
85  }
86 }