oc-resizer-plugin/formwidgets/Responsiveimage.php

129 lines
3.0 KiB
PHP
Raw Normal View History

2020-10-21 00:02:20 +02:00
<?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;
2020-10-25 20:53:10 +01:00
use Aweos\Resizer\Classes\UploadStorage;
2020-10-21 00:02:20 +02:00
/**
* 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;
2020-10-21 00:02:20 +02:00
public $aspectRatio;
2020-10-21 00:02:20 +02:00
protected $defaultAlias = 'responsiveimage';
2020-10-21 00:02:20 +02:00
protected $configFormWidget;
/**
* @inheritDoc
*/
public function init()
{
$this->fillFromConfig([
'minWidth',
'aspectRatio'
2020-10-21 00:02:20 +02:00
]);
/*
2020-10-21 00:02:20 +02:00
if ($this->formField->disabled) {
$this->previewMode = true;
}
//$this->getConfigFormWidget();
*/
2020-10-21 00:02:20 +02:00
}
/**
* @inheritDoc
*/
public function render()
{
$this->prepareVars();
return $this->makePartial('responsiveimage');
}
/**
* Prepares the view data
*/
protected function prepareVars()
{
2020-10-25 23:02:13 +01:00
$this->vars['aspectRatio'] = $this->aspectRatio ?: 'null';
$this->vars['minWidth'] = $this->minWidth ?: '0';
$this->vars['name'] = $this->formField->getName();
2020-10-27 01:49:52 +01:00
$this->vars['value'] = $this->getLoadValue() ?: 'null';
$this->vars['meta'] = $this->getLoadValue()
? str_replace('"', "'", json_encode(app(UploadStorage::class)->getFileData($this->getLoadValue())))
: 'null';
2020-10-25 23:02:13 +01:00
$this->vars['model'] = $this->model;
2020-10-21 00:02:20 +02:00
}
/**
* @inheritDoc
*/
protected function loadAssets()
{
$this->addCss('css/responsiveimage.build.css', 'core');
$this->addJs('js/responsiveimage.build.js', 'core');
2020-10-21 00:02:20 +02:00
}
/**
* @inheritDoc
*/
public function getSaveValue($value)
{
2020-10-27 01:49:52 +01:00
return $value;
2020-10-21 00:02:20 +02:00
}
/**
* Upload handler for the server-side processing of uploaded files
*/
public function onUpload()
{
2020-10-25 20:53:10 +01:00
if (!Input::hasFile('file_data')) {
throw new ApplicationException('File missing from request');
}
2020-10-21 00:02:20 +02:00
2020-10-25 20:53:10 +01:00
$data = json_decode(Input::get('extraData'), true);
$uploadedFile = Input::file('file_data');
2020-10-21 00:02:20 +02:00
2020-10-25 20:53:10 +01:00
if (!$uploadedFile->isValid()) {
throw new ApplicationException('File is not valid');
2020-10-21 00:02:20 +02:00
}
2020-10-27 01:49:52 +01:00
$attachment = app(UploadStorage::class)->storeFileFromUpload($uploadedFile, $data);
2020-10-25 23:02:13 +01:00
2020-10-27 01:49:52 +01:00
return Response::make($attachment->id);
2020-10-25 23:02:13 +01:00
}
2020-10-25 20:53:10 +01:00
2020-10-25 23:02:13 +01:00
public function onDelete() {
2020-10-27 01:49:52 +01:00
app(UploadStorage::class)->removeAttachment(Input::get('file_id'));
2020-10-21 00:02:20 +02:00
}
}