1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
<?php
class FilesController extends AppController {
var $helpers = array('Html');
var $uses = array('Stream','BEObject') ;
var $components = array('Transaction', 'BeUploadToObj', 'RequestHandler', 'BeSecurity');
function upload() {
try {
$this->Transaction->begin();
$id = $this->BeUploadToObj->upload();
$this->Transaction->commit();
$response = array('id' => $id);
} catch (BeditaException $ex) {
throw new BeditaAjaxException($ex->getMessage(), array(
'output' => 'json',
'headers' => array('HTTP/1.1 409 Conflict')
));
}
$this->layout = 'ajax';
$this->RequestHandler->respondAs('json');
$this->set('data', $response);
$this->view = 'View';
$this->render('/pages/json');
}
function uploadAjax($uploadSuffix = null) {
$this->layout = "ajax";
try {
$this->Transaction->begin() ;
$formUploadFields = 'streamUploaded';
$formFileName = 'Filedata';
if (!empty($uploadSuffix)) {
$formUploadFields .= $uploadSuffix;
unset($this->params["form"]["Filedata"]);
$formFileName .= $uploadSuffix;
}
$this->params['form'][$formUploadFields]['lang'] = $this->data["lang"];
$id = $this->BeUploadToObj->upload($this->params["form"][$formUploadFields],$formFileName) ;
$this->Transaction->commit();
$this->set("fileId", $id);
$this->set("fileUploaded", true);
} catch(BeditaException $ex) {
$errTrace = get_class($ex) . " - " . $ex->getMessage()."\nFile: ".$ex->getFile()." - line: ".$ex->getLine()."\nTrace:\n".$ex->getTraceAsString();
$this->handleError($ex->getMessage(), $ex->getMessage(), $errTrace);
$this->setResult(self::ERROR);
$this->set("errorMsg", $ex->getMessage());
}
}
function uploadAjaxMediaProvider() {
$this->layout = "ajax";
header("Content-Type: application/json");
try {
if (!isset($this->params['form']['uploadByUrl']['url']))
throw new BEditaException(__("Error during upload: missing url",true)) ;
$this->params['form']['uploadByUrl']['lang'] = $this->data["lang"];
$this->Transaction->begin() ;
$id = $this->BeUploadToObj->uploadFromURL($this->params['form']['uploadByUrl']) ;
$this->Transaction->commit();
$this->set("fileId", $id);
} catch(BeditaException $ex) {
$errTrace = get_class($ex) . " - " . $ex->getMessage()."\nFile: ".$ex->getFile()." - line: ".$ex->getLine()."\nTrace:\n".$ex->getTraceAsString();
$this->handleError($ex->getMessage(), $ex->getMessage(), $errTrace);
$this->setResult(self::ERROR);
$this->set("errorMsg", $ex->getMessage());
}
}
function deleteFile() {
if(!isset($this->params['form']['filename'])) throw new BeditaException(sprintf(__("No data", true), $id));
$this->Transaction->begin() ;
if(!($id = $this->Stream->getIdFromFilename($this->params['form']['filename']))) throw new BeditaException(sprintf(__("Error getting id object: %s", true), $this->params['form']['filename']));
if(!$this->BeFileHandler->del($id)) throw new BeditaException(sprintf(__("Error deleting object: %d", true), $id));
$this->Transaction->commit() ;
$this->layout = "empty" ;
}
protected function beforeCheckLogin() {
if ($this->RequestHandler->isFlash()) {
$this->skipCheck = true;
}
}
public function handleError($eventMsg, $userMsg, $errTrace) {
$this->log($errTrace);
if($this->Transaction->started()) {
$this->Transaction->rollback();
}
}
protected function forward($action, $result) {
$redirect = array(
'uploadAjax' => array(
'OK' => self::VIEW_FWD . 'upload_ajax_response',
'ERROR' => self::VIEW_FWD . 'upload_ajax_response'
),
'uploadAjaxMediaProvider' => array(
'OK' => self::VIEW_FWD . 'upload_ajax_response',
'ERROR' => self::VIEW_FWD . 'upload_ajax_response'
)
);
if (isset($redirect[$action][$result])) {
return $redirect[$action][$result];
}
return false;
}
}