oc-resizer-plugin/formwidgets/Responsiveimage.php

179 lines
4.6 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-28 01:28:36 +01:00
use Aweos\Resizer\Models\SourceFile;
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-27 02:18:30 +01:00
$this->vars['aspectRatio'] = $this->aspectRatio
? str_replace('"', "'", json_encode(explode(' / ', $this->aspectRatio)))
: 'null';
2020-10-25 23:02:13 +01:00
$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
}
2020-10-28 01:28:36 +01:00
public function getLoadValue() {
return is_numeric(parent::getLoadValue()) ? parent::getLoadValue() : null;
}
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
}
2020-10-28 01:28:36 +01:00
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
]);
}
2020-10-21 00:02:20 +02:00
}