'crop', 'extension' => 'auto' ]; /** * @var boolean Allow the user to set a caption. */ public $useCaption = true; /** * @var boolean Automatically attaches the uploaded file on upload if the parent record exists instead of using deferred binding to attach on save of the parent record. Defaults to false. */ public $attachOnUpload = false; // // Object properties // /** * @inheritDoc */ protected $defaultAlias = 'responsiveimage'; /** * @var Backend\Widgets\Form The embedded form for modifying the properties of the selected file */ protected $configFormWidget; /** * @inheritDoc */ public function init() { $this->maxFilesize = $this->getUploadMaxFilesize(); $this->fillFromConfig([ 'minWidth', 'aspectRatio' ]); /* if ($this->formField->disabled) { $this->previewMode = true; } //$this->getConfigFormWidget(); */ } /** * @inheritDoc */ public function render() { $this->prepareVars(); return $this->makePartial('responsiveimage'); } /** * Prepares the view data */ protected function prepareVars() { $this->vars['aspectRatio'] = $this->aspectRatio ?: 'null'; $this->vars['minWidth'] = $this->minWidth ?: '0'; $this->vars['name'] = $this->formField->getName(); $this->vars['value'] = str_replace('"', "'", json_encode($this->getLoadValue())); $this->vars['model'] = $this->model; } /** * Get the file record for this request, returns false if none available * * @return System\Models\File|false */ protected function getFileRecord() { $record = false; return $record; } /** * Returns the escaped and translated prompt text to display according to the type. * @return string */ protected function getPromptText() { if ($this->prompt === null) { $isMulti = ends_with($this->getDisplayMode(), 'multi'); $this->prompt = $isMulti ? 'backend::lang.fileupload.upload_file' : 'backend::lang.fileupload.default_prompt'; } return str_replace('%s', '', e(trans($this->prompt))); } /** * Returns the CSS dimensions for the uploaded image, * uses auto where no dimension is provided. * @param string $mode * @return string */ protected function getCssDimensions($mode = null) { if (!$this->imageWidth && !$this->imageHeight) { return ''; } $cssDimensions = ''; if ($mode == 'block') { $cssDimensions .= $this->imageWidth ? 'width: '.$this->imageWidth.'px;' : 'width: '.$this->imageHeight.'px;'; $cssDimensions .= ($this->imageHeight) ? 'max-height: '.$this->imageHeight.'px;' : 'height: auto;'; } else { $cssDimensions .= $this->imageWidth ? 'width: '.$this->imageWidth.'px;' : 'width: auto;'; $cssDimensions .= ($this->imageHeight) ? 'max-height: '.$this->imageHeight.'px;' : 'height: auto;'; } return $cssDimensions; } /** * Removes a file attachment. */ public function onRemoveAttachment() { // } /** * Loads the configuration form for an attachment, allowing title and description to be set. */ public function onLoadAttachmentConfig() { // } /** * Commit the changes of the attachment configuration form. */ public function onSaveAttachmentConfig() { try { $formWidget = $this->getConfigFormWidget(); if ($file = $formWidget->model) { $modelsToSave = $this->prepareModelsToSave($file, $formWidget->getSaveData()); Db::transaction(function () use ($modelsToSave, $formWidget) { foreach ($modelsToSave as $modelToSave) { $modelToSave->save(null, $formWidget->getSessionKey()); } }); return ['displayName' => $file->title ?: $file->file_name]; } throw new ApplicationException('Unable to find file, it may no longer exist'); } catch (Exception $ex) { return json_encode(['error' => $ex->getMessage()]); } } /** * @inheritDoc */ protected function loadAssets() { $this->addCss('css/responsiveimage.build.css', 'core'); $this->addJs('js/responsiveimage.build.js', 'core'); } /** * @inheritDoc */ public function getSaveValue($value) { return $value ? json_decode($value) : null; } /** * Upload handler for the server-side processing of uploaded files */ public function onUpload() { if (!Input::hasFile('file_data')) { throw new ApplicationException('File missing from request'); } $data = json_decode(Input::get('extraData'), true); $uploadedFile = Input::file('file_data'); if (!$uploadedFile->isValid()) { throw new ApplicationException('File is not valid'); } $filename = app(UploadStorage::class)->storeFileFromUpload($uploadedFile, $data); return Response::json([ 'file' => $filename, 'data' => array_only($data, ['title', 'description']) ], 200); } public function onDelete() { app(UploadStorage::class)->removeFile(Input::get('fileName')); } /** * Adds the bespoke attributes used internally by this widget. * - thumbUrl * - pathUrl * @return System\Models\File */ protected function decorateFileAttributes($file) { $path = $thumb = $file->getPath(); if ($this->imageWidth || $this->imageHeight) { $thumb = $file->getThumb($this->imageWidth, $this->imageHeight, $this->thumbOptions); } $file->pathUrl = $path; $file->thumbUrl = $thumb; return $file; } /** * Return max upload filesize in Mb * @return integer */ protected function getUploadMaxFilesize() { $size = ini_get('upload_max_filesize'); if (preg_match('/^([\d\.]+)([KMG])$/i', $size, $match)) { $pos = array_search($match[2], ['K', 'M', 'G']); if ($pos !== false) { $size = $match[1] * pow(1024, $pos + 1); } } return floor($size / 1024 / 1024); } }