oc-resizer-plugin/formwidgets/Responsiveimage.php

129 lines
3.0 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;
/**
* 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 ?: 'null';
$this->vars['minWidth'] = $this->minWidth ?: '0';
$this->vars['name'] = $this->formField->getName();
$this->vars['value'] = $this->getLoadValue() ?: 'null';
$this->vars['meta'] = $this->getLoadValue()
? str_replace('"', "'", json_encode(app(UploadStorage::class)->getFileData($this->getLoadValue())))
: 'null';
$this->vars['model'] = $this->model;
}
/**
* @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;
}
/**
* 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'));
}
}