184 lines
4.9 KiB
PHP
184 lines
4.9 KiB
PHP
<?php namespace Aweos\Resizer\FormWidgets;
|
|
|
|
use Db;
|
|
use Input;
|
|
use Request;
|
|
use Response;
|
|
use Validator;
|
|
use Backend\Widgets\Form;
|
|
use Backend\Classes\FormField;
|
|
use Backend\Classes\FormWidgetBase;
|
|
use October\Rain\Filesystem\Definitions as FileDefinitions;
|
|
use ApplicationException;
|
|
use ValidationException;
|
|
use Exception;
|
|
use Aweos\Resizer\Traits\ResponsiveSaver;
|
|
use Aweos\Resizer\Traits\ResponsiveWidget;
|
|
use Aweos\Resizer\Classes\UploadStorage;
|
|
use Aweos\Resizer\Models\SourceFile;
|
|
|
|
/**
|
|
* File upload field
|
|
* Renders a form file uploader field.
|
|
*
|
|
* Supported options:
|
|
* - mode: image-single, image-multi, file-single, file-multi
|
|
* - upload-label: Add file
|
|
* - empty-label: No file uploaded
|
|
*
|
|
* @package october\backend
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
*/
|
|
class Responsiveimage extends FormWidgetBase
|
|
{
|
|
|
|
public $minWidth;
|
|
|
|
public $aspectRatio;
|
|
|
|
protected $defaultAlias = 'responsiveimage';
|
|
|
|
protected $configFormWidget;
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function init()
|
|
{
|
|
$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
|
|
? $this->vueParam(explode(' / ', $this->aspectRatio))
|
|
: 'null';
|
|
$this->vars['minWidth'] = $this->minWidth ?: '0';
|
|
$this->vars['name'] = $this->formField->getName();
|
|
$this->vars['value'] = is_numeric($this->getLoadValue()) ? $this->getLoadValue() : 'null';
|
|
$this->vars['meta'] = is_numeric($this->getLoadValue())
|
|
? $this->vueParam(app(UploadStorage::class)->getFileData($this->getLoadValue()))
|
|
: 'null';
|
|
$this->vars['model'] = $this->model;
|
|
$this->vars['croppableTypes'] = $this->vueParam(SourceFile::$croppableTypes);
|
|
}
|
|
|
|
private function vueParam($config) {
|
|
return str_replace('"', "'", json_encode($config));
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
protected function loadAssets()
|
|
{
|
|
$this->addCss('css/responsiveimage.build.css', 'core');
|
|
$this->addJs('js/responsiveimage.build.js', 'core');
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function getSaveValue($value)
|
|
{
|
|
return is_numeric($value) ? $value : null;
|
|
}
|
|
|
|
public function getLoadValue() {
|
|
return is_numeric(parent::getLoadValue()) ? parent::getLoadValue() : '';
|
|
}
|
|
|
|
/**
|
|
* 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');
|
|
}
|
|
|
|
$attachment = app(UploadStorage::class)->storeFileFromUpload($uploadedFile, $data);
|
|
|
|
return Response::make($attachment->id);
|
|
}
|
|
|
|
public function onDelete() {
|
|
app(UploadStorage::class)->removeAttachment(Input::get('file_id'));
|
|
}
|
|
|
|
public function onOpenDatabase() {
|
|
$files = (new SourceFile)->newQuery();
|
|
|
|
if ($aspectRatio = Input::get('databasePopup.crop.aspectRatio', null)) {
|
|
$files->where('aspect_ratio', json_encode($aspectRatio));
|
|
}
|
|
if ($minWidth = Input::get('databasePopup.crop.minWidth', null)) {
|
|
$files->where('min_width', '>=', $minWidth);
|
|
}
|
|
|
|
return $this->makePartial('database', [
|
|
'images' => $files->get()
|
|
]);
|
|
}
|
|
|
|
public function onSelectDatabase() {
|
|
$v = Validator::make(Input::get(), [
|
|
'image' => 'required|exists:responsive_source_files,id',
|
|
'title' => 'required'
|
|
], [
|
|
'image.required' => 'Bitte ein Bild auswählen',
|
|
'title.required' => 'Bitte einen Titel angeben'
|
|
]);
|
|
|
|
if ($v->fails()) {
|
|
throw new ValidationException($v);
|
|
}
|
|
|
|
$attachment = app(UploadStorage::class)->storeAttachment(SourceFile::find(Input::get('image')), [
|
|
'title' => Input::get('title', ''),
|
|
'description' => Input::get('description', ''),
|
|
'crop' => null
|
|
]);
|
|
|
|
return Response::json([
|
|
'id' => $attachment->id,
|
|
'url' => $attachment->url,
|
|
'name' => $attachment->slug,
|
|
'type' => $attachment->source->type,
|
|
'size' => $attachment->source->size
|
|
]);
|
|
}
|
|
|
|
}
|