2015-06-13 10:31:31 +10:00
|
|
|
<?php namespace Cms\FormWidgets;
|
|
|
|
|
|
|
|
use Lang;
|
|
|
|
use ApplicationException;
|
2015-06-17 18:51:05 +10:00
|
|
|
use Cms\Classes\MediaLibrary;
|
2017-03-14 07:38:02 +11:00
|
|
|
use Backend\Classes\FormField;
|
2015-06-13 10:31:31 +10:00
|
|
|
use Backend\Classes\FormWidgetBase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Media Finder
|
|
|
|
* Renders a record finder field.
|
|
|
|
*
|
|
|
|
* image:
|
|
|
|
* label: Some image
|
|
|
|
* type: media
|
|
|
|
* prompt: Click the %s button to find a user
|
2017-05-25 15:30:22 +02:00
|
|
|
*
|
2015-06-13 10:31:31 +10:00
|
|
|
* @package october\cms
|
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
|
|
*/
|
|
|
|
class MediaFinder extends FormWidgetBase
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Configurable properties
|
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Prompt to display if no record is selected.
|
|
|
|
*/
|
2015-06-20 22:21:18 +10:00
|
|
|
public $prompt = 'cms::lang.mediafinder.default_prompt';
|
2015-06-13 10:31:31 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Display mode for the selection. Values: file, image.
|
|
|
|
*/
|
|
|
|
public $mode = 'file';
|
|
|
|
|
2017-05-25 15:30:22 +02:00
|
|
|
/**
|
|
|
|
* @var int Preview image width
|
|
|
|
*/
|
|
|
|
public $imageWidth = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int Preview image height
|
|
|
|
*/
|
|
|
|
public $imageHeight = null;
|
|
|
|
|
2015-06-13 10:31:31 +10:00
|
|
|
//
|
|
|
|
// Object properties
|
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
2017-03-16 06:26:14 +11:00
|
|
|
* @inheritDoc
|
2015-06-13 10:31:31 +10:00
|
|
|
*/
|
|
|
|
protected $defaultAlias = 'media';
|
|
|
|
|
|
|
|
/**
|
2017-03-16 06:26:14 +11:00
|
|
|
* @inheritDoc
|
2015-06-13 10:31:31 +10:00
|
|
|
*/
|
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
$this->fillFromConfig([
|
|
|
|
'mode',
|
2017-05-25 15:30:22 +02:00
|
|
|
'prompt',
|
|
|
|
'imageWidth',
|
|
|
|
'imageHeight'
|
2015-06-13 10:31:31 +10:00
|
|
|
]);
|
2017-03-14 07:38:02 +11:00
|
|
|
|
|
|
|
if ($this->formField->disabled) {
|
|
|
|
$this->previewMode = true;
|
|
|
|
}
|
2015-06-13 10:31:31 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-16 06:26:14 +11:00
|
|
|
* @inheritDoc
|
2015-06-13 10:31:31 +10:00
|
|
|
*/
|
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
$this->prepareVars();
|
2017-05-25 15:30:22 +02:00
|
|
|
|
2015-06-13 10:31:31 +10:00
|
|
|
return $this->makePartial('mediafinder');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepares the list data
|
|
|
|
*/
|
|
|
|
public function prepareVars()
|
|
|
|
{
|
2015-06-17 18:51:05 +10:00
|
|
|
$value = $this->getLoadValue();
|
|
|
|
$this->vars['value'] = $value;
|
|
|
|
$this->vars['imageUrl'] = $value ? MediaLibrary::url($value) : '';
|
2015-06-13 10:31:31 +10:00
|
|
|
$this->vars['field'] = $this->formField;
|
2015-06-20 22:21:18 +10:00
|
|
|
$this->vars['prompt'] = str_replace('%s', '<i class="icon-folder"></i>', trans($this->prompt));
|
2015-06-13 10:31:31 +10:00
|
|
|
$this->vars['mode'] = $this->mode;
|
2017-05-25 15:30:22 +02:00
|
|
|
$this->vars['imageWidth'] = $this->imageWidth;
|
|
|
|
$this->vars['imageHeight'] = $this->imageHeight;
|
2015-06-13 10:31:31 +10:00
|
|
|
}
|
|
|
|
|
2017-03-14 07:38:02 +11:00
|
|
|
/**
|
2017-03-16 06:26:14 +11:00
|
|
|
* @inheritDoc
|
2017-03-14 07:38:02 +11:00
|
|
|
*/
|
|
|
|
public function getSaveValue($value)
|
|
|
|
{
|
|
|
|
if ($this->formField->disabled || $this->formField->hidden) {
|
|
|
|
return FormField::NO_SAVE_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2015-06-13 10:31:31 +10:00
|
|
|
/**
|
2017-03-16 06:26:14 +11:00
|
|
|
* @inheritDoc
|
2015-06-13 10:31:31 +10:00
|
|
|
*/
|
2015-08-04 19:32:51 +10:00
|
|
|
protected function loadAssets()
|
2015-06-13 10:31:31 +10:00
|
|
|
{
|
|
|
|
$this->addJs('js/mediafinder.js', 'core');
|
|
|
|
$this->addCss('css/mediafinder.css', 'core');
|
|
|
|
}
|
2017-03-14 07:38:02 +11:00
|
|
|
}
|