Xataface 2.0
Xataface Application Framework
actions/delete_file.php
Go to the documentation of this file.
00001 <?php
00021 class dataface_actions_delete_file {
00022         function handle(&$params){
00023                 if ( @$_POST['--field'] ){
00024                         return $this->handlePost($params);
00025                 } else {
00026                         return $this->handleGet($params);
00027                 }
00028         }
00029         
00030         function handleGet(&$params){
00031                 return PEAR::raiseError("No implemented yet.  Please use this only via POST method.");
00032         }
00033         
00034         function handlePost(&$params){
00035                 $app =& Dataface_Application::getInstance();
00036                 $query =& $app->getQuery();
00037                 
00038                 if ( !@$_POST['--field'] ) return PEAR::raiseError('No field specified');
00039                 $record =& $app->getRecord();
00040                 
00041                 if ( !$record ) return PEAR::raiseError('No record found');
00042                 
00043                 $fieldDef =& $record->_table->getField($_POST['--field']);
00044                 if ( PEAR::isError($fieldDef) ) return $fieldDef;
00045                 
00046                 if ( !$record->checkPermission('edit', array('field'=>$fieldDef['Field'])) ){
00047                         return Dataface_Error::permissionDenied('You don\'t have permission to edit this field.');
00048                 }
00049                 
00050                 
00051                 
00052                 if ( $fieldDef['Type'] == 'container' ){
00053                         $fileName = $record->val($fieldDef['Field']);
00054                         if ( !$fileName ) return PEAR::raiseError("This record does not contain a file in the $fieldDef[Field] field.");
00055                         
00056                         // We need to delete the file from the file system.
00057                         $path = $fieldDef['savepath'];
00058                         $filePath = $path.'/'.basename($fileName);
00059                         @unlink($filePath);
00060                         
00061                         $record->setValue($fieldDef['Field'], null);
00062                         if ( @$fieldDef['mimetype'] ){
00063                                 $mimeTypeField =& $record->_table->getField($fieldDef['mimetype']);
00064                                 if ( !PEAR::isError($mimeTypeField) ){
00065                                         $record->setValue($fieldDef['mimetype'], null);
00066                                 }
00067                         }
00068                         $res = $record->save();
00069                         if ( PEAR::isError($res) ) return $res;
00070                                                 
00071                 } else if ( $record->_table->isBlob($fieldDef['Field']) ){
00072                         $record->setValue($fieldDef['Field'], 'dummy');
00073                         $record->setValue($fieldDef['Field'], null);
00074                         if ( @$fieldDef['mimetype'] ){
00075                                 $mimetypeField =& $record->_table->getField($fieldDef['mimetype']);
00076                                 if ( !PEAR::isError($mimetypeField) ){
00077                                         $record->setValue($fieldDef['mimetype'], null);
00078                                 }
00079                         }
00080                         
00081                         if ( @$fieldDef['filename'] ){
00082                                 $filenameField =& $record->_table->getField($fieldDef['filename']);
00083                                 if ( !PEAR::isError($filenameField) ){
00084                                         $record->setValue($fieldDef['filename'], null);
00085                                 }
00086                         }
00087                         $res = $record->save();
00088                         if ( PEAR::isError($res) ) return $res;
00089                         
00090                 }
00091                 
00092                 // Now that we have been successful, let's return a success reply.
00093                 if ( @$query['--format'] == 'json' ){
00094                         import('Services/JSON.php');
00095                         $json = new Services_JSON;
00096                         header('Content-type: text/json; charset='.$app->_conf['oe']);
00097                         echo $json->encode(
00098                                 array(
00099                                         'success'=>1,
00100                                         '--msg' => 'Successfully deleted file'
00101                                 )
00102                         );
00103                         return;
00104                 } else {
00105                         $redirect = '';
00106                         if ( !$redirect ) $redirect = @$query['-redirect'];
00107                         if ( !$redirect ) $redirect = @$_SERVER['HTTP_REFERER'];
00108                         if ( !$redirect ) $redirect = $record->getURL('-action=edit');
00109                         
00110                         if ( !$redirect or PEAR::isError($redirect) ){
00111                                 $redirect = DATAFACE_SITE_HREF;
00112                         }
00113                         
00114                         if ( strpos($redirect, '?') === false ) $redirect .= '?';
00115                         $redirect .= '&--msg='.urlencode("File successfully deleted.");
00116                         $app->redirect("$redirect");
00117                 }
00118                 
00119         }
00120 }
 All Data Structures Namespaces Files Functions Variables Enumerations