<?php
namespace addons\tinymce;
use app\common\library\Menu;
use think\Addons;
class Tinymce extends Addons
{
public function install()
{
$this->setTinymce();
return true;
}
public function uninstall()
{
return true;
}
public function enable()
{
$this->setTinymce();
return true;
}
public function disable()
{
return true;
}
public function wipecacheAfter($param)
{
$info = $this->getInfo();
if ($info['state'] == 0) {
return true;
}
$config = $this->getConfig();
$configBase = include(__DIR__ . '/configBase.php');
if ($config == $configBase) {
return true;
}
\think\addons\Service::enable($this->getName(), 0);
return true;
}
public function setTinymce()
{
$mustPlugins = [
'advlist' 'link', 'image' 'lists' 'charmap',
'hr' 'anchor' 'pagebreak' 'searchreplace',
'wordcount',
'visualblocks',
'visualchars',
'code' 'insertdatetime',
'nonbreaking',
'save' 'table' 'contextmenu',
'directionality',
'help' ];
$allConfigPlugins = [
'autolink' 'autosave' 'print' 'preview' 'spellchecker' 'fullscreen' 'media' 'emoticons' 'template' 'paste' 'textcolor' ];
$baseToolbar = 'insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | print preview media fullpage | forecolor backcolor emoticons | spellchecker help';
$config = $this->getConfig();
$jsContent = file_get_contents(__DIR__ . '/bootstrapBase.js');
$jsContent = str_replace("{language}", $config['language'], $jsContent);
if (!empty($config['plugins'])) {
$plugins = array_merge($mustPlugins, explode(',', $config['plugins']));
} else {
$plugins = $mustPlugins;
}
$jsContent = str_replace("{plugins}", implode(' ', $plugins), $jsContent);
if (in_array('spellchecker', $plugins)) {
$jsContent = str_replace("'{browser_spellcheck}'", 'true', $jsContent);
} else {
$jsContent = str_replace("'{browser_spellcheck}'", 'false', $jsContent);
}
if ($config['toolbar']) {
$arr = array_diff($allConfigPlugins, $plugins);
foreach ($arr as $value) {
$this->baseToolbar = str_replace($value, '', $baseToolbar);
}
$jsContent = str_replace("{toolbar}", $baseToolbar, $jsContent);
} else {
$jsContent = str_replace("'{toolbar}'", 'false', $jsContent);
}
$jsContent = str_replace("{subDom}", $config['subDom'], $jsContent);
file_put_contents(__DIR__ . '/bootstrap.js', $jsContent);
file_put_contents(__DIR__ . '/configBase.php', '<?php return ' . var_export($config, true) . ';');
return true;
}
}