Xataface 2.0
Xataface Application Framework
actions/rest_delete.php
Go to the documentation of this file.
00001 <?php
00002 class dataface_actions_rest_delete {
00003         
00004         const PERMISSION_DENIED = 401;
00005         const NOT_FOUND=404;
00006         const SERVER_ERROR=500;
00007         const BAD_REQUEST = 400;
00008 
00009 
00010         function handle($params){
00011                 $app = Dataface_Application::getInstance();
00012                 $query = $app->getQuery();
00013                 $record_id = @$_POST['--record_id'];
00014                 
00015                 try {
00016                         if ( !$record_id ){
00017                                 throw new Exception(
00018                                         df_translate('Bad Request', 'Bad Request.  Missing parameter.'),
00019                                         self::BAD_REQUEST
00020                                 );
00021                         }
00022                         $record = df_get_record_by_id($record_id);
00023                         if ( PEAR::isError($record) ){
00024                                 error_log($record->getMessage());
00025                                 throw new Exception(
00026                                         df_translate('Bad Request', 'Bad Request - invalid ID.'),
00027                                         self::BAD_REQUEST
00028                                 );
00029                         }
00030                         if ( !$record ){
00031                                 throw new Exception(
00032                                         df_translate('No records matched request','No records matched the request'),
00033                                         self::NOT_FOUND
00034                                 );
00035                         }
00036                         if ( !$record->checkPermission('delete') ){
00037                                 throw new Exception(
00038                                         df_translate('scripts.GLOBAL.MESSAGE.PERMISSION_DENIED','Permission Denied'),
00039                                         self::PERMISSION_DENIED
00040                                 );
00041                         }
00042                         
00043                         $res = $record->delete(false); // We've already done a security check...
00044                         if ( PEAR::isError($res) ){
00045                                 error_log($res->getMessage());
00046                                 throw new Exception(
00047                                         df_translate('actions.rest_delete.messages.SERVER_ERROR', 'Failed to delete record due to a server error.  See error log for details.'), 
00048                                         self::SERVER_ERROR
00049                                 );
00050                                         
00051                         }
00052                         
00053                         $this->out(array(
00054                                 'code'=>200,
00055                                 'message'=>df_translate('actions.rest_delete.messages.SUCCESS', 'Successfully deleted record.'),
00056                                 'record_id'=>$record->getId()
00057                         ));
00058                         exit;
00059                 } catch (Exception $ex){
00060                         switch ($ex->getCode() ){
00061                                 case self::PERMISSION_DENIED:
00062                                 case self::NOT_FOUND:
00063                                 case self::SERVER_ERROR:
00064                                         $msg = $ex->getMessage();
00065                                         $code = $ex->getCode();
00066                                         break;
00067                                 default:
00068                                         $msg = df_translate('actions.rest_delete.messages.SUCCESS', 'Successfully deleted record.');
00069                                         $code = self::SERVER_ERROR;
00070                                         error_log($ex->getMessage());
00071                                         break;
00072                         }
00073                         $this->out(array(
00074                                 'code' => $code,
00075                                 'message' => $msg
00076                         ));
00077                         exit;
00078                 }
00079         }
00080         
00081         
00082         function out($params){
00083                 header('Content-type: text/json; charset="'.Dataface_Application::getInstance()->_conf['oe'].'"');
00084                 echo json_encode($params);
00085         }
00086 }
 All Data Structures Namespaces Files Functions Variables Enumerations