winter/modules/cms/formwidgets/MediaFinder.php

119 lines
2.4 KiB
PHP
Raw Normal View History

<?php namespace Cms\FormWidgets;
use Lang;
use ApplicationException;
use Cms\Classes\MediaLibrary;
use Backend\Classes\FormField;
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
*
* @package october\cms
* @author Alexey Bobkov, Samuel Georges
*/
class MediaFinder extends FormWidgetBase
{
//
// Configurable properties
//
/**
* @var string Prompt to display if no record is selected.
*/
public $prompt = 'cms::lang.mediafinder.default_prompt';
/**
* @var string Display mode for the selection. Values: file, image.
*/
public $mode = 'file';
/**
* @var int Preview image width
*/
public $imageWidth = null;
/**
* @var int Preview image height
*/
public $imageHeight = null;
//
// Object properties
//
/**
2017-03-16 06:26:14 +11:00
* @inheritDoc
*/
protected $defaultAlias = 'media';
/**
2017-03-16 06:26:14 +11:00
* @inheritDoc
*/
public function init()
{
$this->fillFromConfig([
'mode',
'prompt',
'imageWidth',
'imageHeight'
]);
if ($this->formField->disabled) {
$this->previewMode = true;
}
}
/**
2017-03-16 06:26:14 +11:00
* @inheritDoc
*/
public function render()
{
$this->prepareVars();
return $this->makePartial('mediafinder');
}
/**
* Prepares the list data
*/
public function prepareVars()
{
$value = $this->getLoadValue();
$this->vars['value'] = $value;
$this->vars['imageUrl'] = $value ? MediaLibrary::url($value) : '';
$this->vars['field'] = $this->formField;
$this->vars['prompt'] = str_replace('%s', '<i class="icon-folder"></i>', trans($this->prompt));
$this->vars['mode'] = $this->mode;
$this->vars['imageWidth'] = $this->imageWidth;
$this->vars['imageHeight'] = $this->imageHeight;
}
/**
2017-03-16 06:26:14 +11:00
* @inheritDoc
*/
public function getSaveValue($value)
{
if ($this->formField->disabled || $this->formField->hidden) {
return FormField::NO_SAVE_DATA;
}
return $value;
}
/**
2017-03-16 06:26:14 +11:00
* @inheritDoc
*/
protected function loadAssets()
{
$this->addJs('js/mediafinder.js', 'core');
$this->addCss('css/mediafinder.css', 'core');
}
}