229 lines
6.4 KiB
PHP
Raw Normal View History

2014-05-14 23:24:20 +10:00
<?php namespace Backend\FormWidgets;
use App;
use File;
2015-06-25 19:26:47 +10:00
use Event;
use Request;
2014-05-14 23:24:20 +10:00
use Backend\Classes\FormWidgetBase;
use Backend\Models\EditorSetting;
2014-05-14 23:24:20 +10:00
/**
* Rich Editor
* Renders a rich content editor field.
*
* @package october\backend
* @author Alexey Bobkov, Samuel Georges
*/
class RichEditor extends FormWidgetBase
{
//
// Configurable properties
//
/**
* @var boolean Determines whether content has HEAD and HTML tags.
*/
public $fullPage = false;
/**
* @var boolean Determines whether content has HEAD and HTML tags.
*/
public $toolbarButtons = null;
//
// Object properties
//
2014-05-14 23:24:20 +10:00
/**
* {@inheritDoc}
*/
2015-02-28 12:43:53 +11:00
protected $defaultAlias = 'richeditor';
2014-05-14 23:24:20 +10:00
/**
* {@inheritDoc}
*/
public function init()
{
$this->fillFromConfig([
'fullPage',
'toolbarButtons',
]);
}
2014-05-14 23:24:20 +10:00
/**
* {@inheritDoc}
*/
public function render()
{
$this->prepareVars();
return $this->makePartial('richeditor');
}
/**
* Prepares the list data
*/
public function prepareVars()
{
$this->vars['field'] = $this->formField;
$this->vars['editorLang'] = $this->getValidEditorLang();
$this->vars['fullPage'] = $this->fullPage;
2014-05-14 23:24:20 +10:00
$this->vars['stretch'] = $this->formField->stretch;
$this->vars['size'] = $this->formField->size;
$this->vars['name'] = $this->formField->getName();
2015-01-05 09:45:04 +11:00
$this->vars['value'] = $this->getLoadValue();
$this->vars['toolbarButtons'] = $this->evalToolbarButtons();
$this->vars['allowEmptyTags'] = EditorSetting::getConfigured('html_allow_empty_tags');
$this->vars['allowTags'] = EditorSetting::getConfigured('html_allow_tags');
$this->vars['noWrapTags'] = EditorSetting::getConfigured('html_no_wrap_tags');
$this->vars['removeTags'] = EditorSetting::getConfigured('html_remove_tags');
$this->vars['imageStyles'] = EditorSetting::getConfiguredStyles('html_style_image');
$this->vars['linkStyles'] = EditorSetting::getConfiguredStyles('html_style_link');
$this->vars['paragraphStyles'] = EditorSetting::getConfiguredStyles('html_style_paragraph');
$this->vars['tableStyles'] = EditorSetting::getConfiguredStyles('html_style_table');
$this->vars['tableCellStyles'] = EditorSetting::getConfiguredStyles('html_style_table_cell');
}
/**
* Determine the toolbar buttons to use based on config.
* @return string
*/
protected function evalToolbarButtons()
{
$buttons = $this->toolbarButtons;
if (is_string($buttons)) {
$buttons = array_map(function($button) {
return strlen($button) ? $button : '|';
}, explode('|', $buttons));
}
return $buttons;
2014-05-14 23:24:20 +10:00
}
2015-06-25 19:26:47 +10:00
/**
* Returns a single collection of available page links.
* This implementation has room to place links under
* different groups based on the link type.
* @return array
*/
public function onGetPageLinks()
{
2015-06-25 19:26:47 +10:00
$links = [];
$types = $this->getPageLinkTypes();
$links[] = ['name' => 'Select a page...', 'url' => false];
$iterator = function($links, $level = 0) use (&$iterator) {
$result = [];
foreach ($links as $linkUrl => $link) {
/*
* Remove scheme and host from URL
*/
$baseUrl = Request::getSchemeAndHttpHost();
if (strpos($linkUrl, $baseUrl) === 0) {
$linkUrl = substr($linkUrl, strlen($baseUrl));
}
$linkName = str_repeat('&nbsp;', $level * 4);
$linkName .= is_array($link) ? array_get($link, 'title', '') : $link;
$result[] = ['name' => $linkName, 'url' => $linkUrl];
if (is_array($link)) {
$result = array_merge(
$result,
$iterator(array_get($link, 'links', []), $level + 1)
);
}
}
return $result;
};
foreach ($types as $typeCode => $typeName) {
$links = array_merge($links, $iterator($this->getPageLinks($typeCode)));
}
return ['links' => $links];
}
2014-05-14 23:24:20 +10:00
/**
* {@inheritDoc}
*/
protected function loadAssets()
2014-05-14 23:24:20 +10:00
{
2014-09-14 10:57:17 +10:00
$this->addCss('css/richeditor.css', 'core');
2015-02-14 16:40:50 +11:00
$this->addJs('js/build-min.js', 'core');
2016-05-15 06:00:58 +10:00
$this->addJs('/modules/backend/formwidgets/codeeditor/assets/js/build-min.js', 'core');
if ($lang = $this->getValidEditorLang()) {
$this->addJs('vendor/froala/js/languages/'.$lang.'.js', 'core');
}
}
/**
* Returns a valid language code for Redactor.
* @return string|mixed
*/
protected function getValidEditorLang()
{
$locale = App::getLocale();
// English is baked in
if ($locale == 'en') {
return null;
}
$locale = str_replace('-', '_', strtolower($locale));
$path = base_path('modules/backend/formwidgets/richeditor/assets/vendor/froala/js/languages/'.$locale.'.js');
return File::exists($path) ? $locale : false;
2014-09-14 10:57:17 +10:00
}
2015-06-25 19:26:47 +10:00
/**
* Returns a list of registered page link types.
* This is reserved functionality for separating the links by type.
* @return array Returns an array of registered page link types
*/
protected function getPageLinkTypes()
{
$result = [];
$apiResult = Event::fire('backend.richeditor.listTypes');
if (is_array($apiResult)) {
foreach ($apiResult as $typeList) {
if (!is_array($typeList)) {
continue;
}
foreach ($typeList as $typeCode => $typeName) {
$result[$typeCode] = $typeName;
}
}
}
return $result;
}
protected function getPageLinks($type)
{
$result = [];
$apiResult = Event::fire('backend.richeditor.getTypeInfo', [$type]);
if (is_array($apiResult)) {
foreach ($apiResult as $typeInfo) {
if (!is_array($typeInfo)) {
continue;
}
foreach ($typeInfo as $name => $value) {
$result[$name] = $value;
}
}
}
return $result;
}
2014-10-10 23:50:05 +02:00
}