2014-05-14 13:24:20 +00:00
|
|
|
<?php namespace Backend\FormWidgets;
|
|
|
|
|
|
2016-02-13 01:59:49 +00:00
|
|
|
use App;
|
|
|
|
|
use File;
|
2015-06-25 09:26:47 +00:00
|
|
|
use Event;
|
2016-11-09 20:39:29 +00:00
|
|
|
use Lang;
|
2015-06-25 09:26:47 +00:00
|
|
|
use Request;
|
2019-05-10 17:59:57 +00:00
|
|
|
use BackendAuth;
|
2014-05-14 13:24:20 +00:00
|
|
|
use Backend\Classes\FormWidgetBase;
|
2016-05-23 19:33:32 +00:00
|
|
|
use Backend\Models\EditorSetting;
|
2014-05-14 13:24:20 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Rich Editor
|
|
|
|
|
* Renders a rich content editor field.
|
|
|
|
|
*
|
|
|
|
|
* @package october\backend
|
|
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
|
|
|
*/
|
|
|
|
|
class RichEditor extends FormWidgetBase
|
|
|
|
|
{
|
2015-02-28 03:43:34 +00:00
|
|
|
//
|
|
|
|
|
// Configurable properties
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var boolean Determines whether content has HEAD and HTML tags.
|
|
|
|
|
*/
|
|
|
|
|
public $fullPage = false;
|
|
|
|
|
|
2016-05-19 18:35:40 +00:00
|
|
|
/**
|
|
|
|
|
* @var boolean Determines whether content has HEAD and HTML tags.
|
|
|
|
|
*/
|
2018-08-15 16:33:24 +00:00
|
|
|
public $toolbarButtons;
|
2016-05-19 18:35:40 +00:00
|
|
|
|
2017-03-13 20:38:02 +00:00
|
|
|
/**
|
|
|
|
|
* @var boolean If true, the editor is set to read-only mode
|
|
|
|
|
*/
|
|
|
|
|
public $readOnly = false;
|
|
|
|
|
|
2015-02-28 03:43:34 +00:00
|
|
|
//
|
|
|
|
|
// Object properties
|
|
|
|
|
//
|
|
|
|
|
|
2014-05-14 13:24:20 +00:00
|
|
|
/**
|
2017-03-15 19:26:14 +00:00
|
|
|
* @inheritDoc
|
2014-05-14 13:24:20 +00:00
|
|
|
*/
|
2015-02-28 01:43:53 +00:00
|
|
|
protected $defaultAlias = 'richeditor';
|
2014-05-14 13:24:20 +00:00
|
|
|
|
2014-06-06 11:38:34 +00:00
|
|
|
/**
|
2017-03-15 19:26:14 +00:00
|
|
|
* @inheritDoc
|
2014-06-06 11:38:34 +00:00
|
|
|
*/
|
2015-02-28 03:43:34 +00:00
|
|
|
public function init()
|
|
|
|
|
{
|
2017-03-13 20:38:02 +00:00
|
|
|
if ($this->formField->disabled) {
|
|
|
|
|
$this->readOnly = true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-28 03:43:34 +00:00
|
|
|
$this->fillFromConfig([
|
|
|
|
|
'fullPage',
|
2017-03-13 20:38:02 +00:00
|
|
|
'readOnly',
|
2016-05-19 18:35:40 +00:00
|
|
|
'toolbarButtons',
|
2015-02-28 03:43:34 +00:00
|
|
|
]);
|
|
|
|
|
}
|
2014-06-06 11:38:34 +00:00
|
|
|
|
2014-05-14 13:24:20 +00:00
|
|
|
/**
|
2017-03-15 19:26:14 +00:00
|
|
|
* @inheritDoc
|
2014-05-14 13:24:20 +00:00
|
|
|
*/
|
|
|
|
|
public function render()
|
|
|
|
|
{
|
|
|
|
|
$this->prepareVars();
|
|
|
|
|
return $this->makePartial('richeditor');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Prepares the list data
|
|
|
|
|
*/
|
|
|
|
|
public function prepareVars()
|
|
|
|
|
{
|
2016-05-19 18:35:40 +00:00
|
|
|
$this->vars['field'] = $this->formField;
|
2016-02-13 01:59:49 +00:00
|
|
|
$this->vars['editorLang'] = $this->getValidEditorLang();
|
2014-06-06 11:38:34 +00:00
|
|
|
$this->vars['fullPage'] = $this->fullPage;
|
2014-05-14 13:24:20 +00:00
|
|
|
$this->vars['stretch'] = $this->formField->stretch;
|
|
|
|
|
$this->vars['size'] = $this->formField->size;
|
2017-03-13 20:38:02 +00:00
|
|
|
$this->vars['readOnly'] = $this->readOnly;
|
2016-11-27 20:50:06 +00:00
|
|
|
$this->vars['name'] = $this->getFieldName();
|
2015-01-04 22:45:04 +00:00
|
|
|
$this->vars['value'] = $this->getLoadValue();
|
2016-05-19 18:35:40 +00:00
|
|
|
$this->vars['toolbarButtons'] = $this->evalToolbarButtons();
|
2019-05-10 17:59:57 +00:00
|
|
|
$this->vars['useMediaManager'] = BackendAuth::getUser()->hasAccess('media.manage_media');
|
2016-05-23 19:33:32 +00:00
|
|
|
|
2017-07-04 14:10:58 +00:00
|
|
|
$this->vars['globalToolbarButtons'] = EditorSetting::getConfigured('html_toolbar_buttons');
|
2016-05-23 19:33:32 +00:00
|
|
|
$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');
|
2018-08-11 19:33:34 +00:00
|
|
|
$this->vars['lineBreakerTags'] = EditorSetting::getConfigured('html_line_breaker_tags');
|
2016-05-23 19:33:32 +00:00
|
|
|
|
|
|
|
|
$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');
|
2016-05-19 18:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine the toolbar buttons to use based on config.
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected function evalToolbarButtons()
|
|
|
|
|
{
|
|
|
|
|
$buttons = $this->toolbarButtons;
|
|
|
|
|
|
|
|
|
|
if (is_string($buttons)) {
|
2017-04-24 11:38:19 +00:00
|
|
|
$buttons = array_map(function ($button) {
|
2016-05-19 18:35:40 +00:00
|
|
|
return strlen($button) ? $button : '|';
|
|
|
|
|
}, explode('|', $buttons));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $buttons;
|
2014-05-14 13:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-24 19:19:12 +00:00
|
|
|
public function onLoadPageLinksForm()
|
2015-06-24 08:14:36 +00:00
|
|
|
{
|
2016-05-24 19:19:12 +00:00
|
|
|
$this->vars['links'] = $this->getPageLinksArray();
|
|
|
|
|
return $this->makePartial('page_links_form');
|
2015-06-24 08:14:36 +00:00
|
|
|
}
|
|
|
|
|
|
2014-05-14 13:24:20 +00:00
|
|
|
/**
|
2017-03-15 19:26:14 +00:00
|
|
|
* @inheritDoc
|
2014-05-14 13:24:20 +00:00
|
|
|
*/
|
2015-08-04 09:32:51 +00:00
|
|
|
protected function loadAssets()
|
2014-05-14 13:24:20 +00:00
|
|
|
{
|
2014-09-14 00:57:17 +00:00
|
|
|
$this->addCss('css/richeditor.css', 'core');
|
2015-02-14 05:40:50 +00:00
|
|
|
$this->addJs('js/build-min.js', 'core');
|
2019-04-30 06:24:00 +00:00
|
|
|
$this->addJs('js/build-plugins-min.js', 'core');
|
2016-05-14 20:00:58 +00:00
|
|
|
$this->addJs('/modules/backend/formwidgets/codeeditor/assets/js/build-min.js', 'core');
|
2016-02-13 01:59:49 +00:00
|
|
|
|
2016-05-21 21:41:38 +00:00
|
|
|
if ($lang = $this->getValidEditorLang()) {
|
|
|
|
|
$this->addJs('vendor/froala/js/languages/'.$lang.'.js', 'core');
|
|
|
|
|
}
|
2016-02-13 01:59:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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));
|
2016-05-21 21:41:38 +00:00
|
|
|
$path = base_path('modules/backend/formwidgets/richeditor/assets/vendor/froala/js/languages/'.$locale.'.js');
|
2016-02-13 01:59:49 +00:00
|
|
|
|
|
|
|
|
return File::exists($path) ? $locale : false;
|
2014-09-14 00:57:17 +00:00
|
|
|
}
|
2015-06-25 09:26:47 +00: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;
|
|
|
|
|
}
|
2016-05-24 19:19:12 +00: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
|
|
|
|
|
*/
|
|
|
|
|
protected function getPageLinksArray()
|
|
|
|
|
{
|
|
|
|
|
$links = [];
|
|
|
|
|
$types = $this->getPageLinkTypes();
|
|
|
|
|
|
2016-11-09 20:39:29 +00:00
|
|
|
$links[] = ['name' => Lang::get('backend::lang.pagelist.select_page'), 'url' => false];
|
2016-05-24 19:19:12 +00:00
|
|
|
|
2017-04-24 11:38:19 +00:00
|
|
|
$iterator = function ($links, $level = 0) use (&$iterator) {
|
2016-05-24 19:19:12 +00:00
|
|
|
$result = [];
|
|
|
|
|
|
2019-07-18 14:50:37 +00:00
|
|
|
foreach ($links as $linkUrl => $link) {
|
2016-05-24 19:19:12 +00:00
|
|
|
/*
|
|
|
|
|
* Remove scheme and host from URL
|
|
|
|
|
*/
|
|
|
|
|
$baseUrl = Request::getSchemeAndHttpHost();
|
|
|
|
|
if (strpos($linkUrl, $baseUrl) === 0) {
|
|
|
|
|
$linkUrl = substr($linkUrl, strlen($baseUrl));
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-19 18:46:13 +00:00
|
|
|
/*
|
|
|
|
|
* Root page fallback.
|
|
|
|
|
*/
|
|
|
|
|
if (strlen($linkUrl) === 0) {
|
|
|
|
|
$linkUrl = '/';
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-24 19:19:12 +00:00
|
|
|
$linkName = str_repeat(' ', $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;
|
|
|
|
|
}
|
2014-10-10 21:50:05 +00:00
|
|
|
}
|