Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
delete_file.php
Go to the documentation of this file.
1 <?php
22  function handle(&$params){
23  if ( @$_POST['--field'] ){
24  return $this->handlePost($params);
25  } else {
26  return $this->handleGet($params);
27  }
28  }
29 
30  function handleGet(&$params){
31  return PEAR::raiseError("No implemented yet. Please use this only via POST method.");
32  }
33 
34  function handlePost(&$params){
36  $query =& $app->getQuery();
37 
38  if ( !@$_POST['--field'] ) return PEAR::raiseError('No field specified');
39  $record =& $app->getRecord();
40 
41  if ( !$record ) return PEAR::raiseError('No record found');
42 
43  $fieldDef =& $record->_table->getField($_POST['--field']);
44  if ( PEAR::isError($fieldDef) ) return $fieldDef;
45 
46  if ( !$record->checkPermission('edit', array('field'=>$fieldDef['Field'])) ){
47  return Dataface_Error::permissionDenied('You don\'t have permission to edit this field.');
48  }
49 
50 
51 
52  if ( $fieldDef['Type'] == 'container' ){
53  $fileName = $record->val($fieldDef['Field']);
54  if ( !$fileName ) return PEAR::raiseError("This record does not contain a file in the $fieldDef[Field] field.");
55 
56  // We need to delete the file from the file system.
57  $path = $fieldDef['savepath'];
58  $filePath = $path.'/'.basename($fileName);
59  @unlink($filePath);
60 
61  $record->setValue($fieldDef['Field'], null);
62  if ( @$fieldDef['mimetype'] ){
63  $mimeTypeField =& $record->_table->getField($fieldDef['mimetype']);
64  if ( !PEAR::isError($mimeTypeField) ){
65  $record->setValue($fieldDef['mimetype'], null);
66  }
67  }
68  $res = $record->save();
69  if ( PEAR::isError($res) ) return $res;
70 
71  } else if ( $record->_table->isBlob($fieldDef['Field']) ){
72  $record->setValue($fieldDef['Field'], 'dummy');
73  $record->setValue($fieldDef['Field'], null);
74  if ( @$fieldDef['mimetype'] ){
75  $mimetypeField =& $record->_table->getField($fieldDef['mimetype']);
76  if ( !PEAR::isError($mimetypeField) ){
77  $record->setValue($fieldDef['mimetype'], null);
78  }
79  }
80 
81  if ( @$fieldDef['filename'] ){
82  $filenameField =& $record->_table->getField($fieldDef['filename']);
83  if ( !PEAR::isError($filenameField) ){
84  $record->setValue($fieldDef['filename'], null);
85  }
86  }
87  $res = $record->save();
88  if ( PEAR::isError($res) ) return $res;
89 
90 
91 
92  }
93 
94  // Now that we have been successful, let's return a success reply.
95  if ( @$query['--format'] == 'json' ){
96  import('Services/JSON.php');
97  $json = new Services_JSON;
98  header('Content-type: application/json; charset='.$app->_conf['oe']);
99  echo $json->encode(
100  array(
101  'success'=>1,
102  '--msg' => 'Successfully deleted file'
103  )
104  );
105  return;
106  } else {
107  $redirect = '';
108  if ( !$redirect ) $redirect = @$query['-redirect'];
109  if ( !$redirect ) $redirect = @$_SERVER['HTTP_REFERER'];
110  if ( !$redirect ) $redirect = $record->getURL('-action=edit');
111 
112  if ( !$redirect or PEAR::isError($redirect) ){
113  $redirect = DATAFACE_SITE_HREF;
114  }
115 
116  if ( strpos($redirect, '?') === false ) $redirect .= '?';
117  $redirect .= '&--msg='.urlencode("File successfully deleted.");
118  $app->redirect("$redirect");
119  }
120 
121  }
122 }