/** * Construct a upload input that will populate the fieldName given with the file database ID * * ### Accepted options: * * - `model` - The alternate model for the stored file (must be a model extended from SfilStoredFile) * - `callbacks` - An array with possible callbacks with Jodel Callbacks conven- * tion. * - `version`: The version of file that will be returned as URL for preview purposes (available on onSave callback) * - `error`: A list of possible errors and its texts to be passed for onReject callback * * @access public * @param array $options An array with non-defaults values * @return string The HTML well formated * @todo Trigger errors */ protected function _upload($gen_options, $file_input_options) { $View = $this->_getView(); $packed = SecureParams::pack(array($gen_options['version'], $file_input_options['fieldName'], $gen_options['model'])); list($model_plugin, $model_name) = pluginSplit($gen_options['model']); $gen_options['parameters'] += array($this->internalParam('layout_scheme') => $View->getVar('layout_scheme')); $gen_options['parameters'] += array($this->internalParam('data') => $packed); $out = ''; $this->sform(array(), array('url' => '')); $out .= $this->Bl->sdiv(array('id' => 'div' . $gen_options['baseID'])); $out .= $this->input(array('id' => 'mi' . $gen_options['baseID']), array('type' => 'file', 'container' => false, 'fieldName' => $model_name . '.file') + $file_input_options); $out .= $this->Bl->ediv(); $this->eform(); $out .= $this->input(array('id' => 'hi' . $gen_options['baseID']), array('type' => 'hidden', 'fieldName' => $file_input_options['fieldName'])); // JS class $out .= $this->BuroOfficeBoy->upload($gen_options); return $out; }
/** * Performs the logic of saving the upload data * * This method receive the POSTed data from each action (classic or ajax upload) * validates the upload and saves it. * The returned data is a array of the generated data (that will generally be * sent back to the view, through JSON object) * * @access protected * @param array $data The POSTed data to be analised and saved * @param string $forceModel When not null, will force a Model to be used, instead of the specified on POSTed data * @return array The array of data of generated data */ protected function saveUpload($data, $forceModel = null) { $saved = $error = false; $filename = ''; $validationErrors = array(); $version = $fieldName = $modelName = null; if (!empty($this->buroData['data'])) { list($version, $fieldName, $modelName) = SecureParams::unpack($this->buroData['data']); } if ($forceModel) { $modelName = $forceModel; } if (is_null($version) || is_null($fieldName) || is_null($modelName)) { $validationErrors['file'] = 'post_max_size'; } elseif (!$this->loadModel($modelName)) { $error = Configure::read() > 0 ? 'JjMediaController::upload - Model ' . $modelName . ' not found.' : true; } else { list($plugin, $modelName) = pluginSplit($modelName); /** @var AppModel $Model */ $Model =& $this->{$modelName}; $model_alias = $Model->alias; if (!empty($data)) { $scope = $Model->findTheScope($fieldName); if ($scope) { $Model->setScope($scope); } $Model->set($data); $validationErrors = $this->validateErrors($Model); if (empty($validationErrors) && $Model->save(null, false)) { $saved = $Model->id; if (isset($data[$model_alias]['file']['name'])) { $filename = $data[$model_alias]['file']['name']; } list($fieldModelName, $fieldName) = pluginSplit($fieldName); if (!empty($data[$fieldModelName][$fieldName])) { $Model->delete($data[$fieldModelName][$fieldName]); } if (Configure::read('JjMedia.asyncGeneration') == false) { App::import('Lib', array('JjUtils.SecureParams')); $packed_params = SecureParams::pack(array($saved, $version), true); $baseUrl = array('plugin' => 'jj_media', 'controller' => 'jj_media', 'action' => 'index'); $dlurl = Router::url($baseUrl + array('1', $packed_params)); $url = Router::url($baseUrl + array($packed_params)); } else { $dlurl = $url = $this->SfilStoredFile->webPath($saved, $version); } } } } return compact('error', 'validationErrors', 'saved', 'version', 'filename', 'url', 'dlurl'); }
/** * Returns a link for the requested image * * @access public * @param integer $id The file id of the image * @param string $version The filter version of image to be displayed * @return string|boolean The URL that points to the picture or false, if wasn´t possible to create the url. */ public function fileURL($id, $version = '', $force_download = false) { if (!$id) { return false; } $url = array('plugin' => 'jj_media', 'controller' => 'jj_media', 'action' => 'index'); if ($force_download) { array_push($url, '1'); } App::import('Lib', array('JjUtils.SecureParams')); $packed_params = SecureParams::pack(array($id, $version), true); array_push($url, $packed_params); return $this->url($url); }