<?php
class WymeditorController extends PluginController {
// Plugin information
const PLUGIN_ID = "wymeditor";
// Location of the view folder
const VIEW_FOLDER = "wymeditor/views/";
const PLUGIN_REL_VIEW_FOLDER = "../../plugins/wymeditor/views/";
// Error types
const ERROR_EMPTY = 'empty';
const ERROR_NOT_EXISTING = 'not_existing';
const ERROR_NOT_VALID = 'not_valid';
/**
* Create a new controller instance and apply the sidebar to the backend.
*/
public function __construct() {
AuthUser::load();
if (!(AuthUser::isLoggedIn())) {
redirect(get_url('login'));
}
$this->setLayout('backend');
$this->assignToLayout('sidebar', $this->create_view('sidebar'));
}
/**
* Settings function to delete the metadata table.
*/
public function settings() {
// TODO: There should be a better way to control access rights than copying every time
if (!AuthUser::hasPermission('administrator') && !AuthUser::hasPermission('developer')) {
redirect(get_url());
}
// Show the error in the view
$error = null;
// Previous settings
$stylesheet = Plugin::getSetting('stylesheet', self::PLUGIN_ID);
// Update request
if (get_request_method() == 'POST' &&
isset($_POST['stylesheet']) &&
!empty($_POST['stylesheet'])) {
// Give the user a second chance and preserve the value in the form
$stylesheet = $_POST['stylesheet'];
// Little security by only allowing '.css' files
if (strrpos($_POST['stylesheet'], '.css') == (strlen($_POST['stylesheet']) - 4)) {
Plugin::setSetting('stylesheet', $stylesheet, self::PLUGIN_ID);
}
else {
$error = self::ERROR_NOT_VALID;
}
}
if (!$error) {
if (empty($stylesheet)) {
$error = self::ERROR_EMPTY;
}
elseif (!is_readable(FROG_ROOT.$stylesheet)) {
$error = self::ERROR_NOT_EXISTING;
}
}
$this->display('settings', array(
'error' => $error,
'stylesheet' => $stylesheet
));
}
public function stylesheet() {
$stylesheet = Plugin::getSetting('stylesheet', self::PLUGIN_ID);
if (!empty($stylesheet) && is_readable(FROG_ROOT.$stylesheet)) {
header('content-type: text/css');
print file_get_contents(FROG_ROOT.$stylesheet);
}
else {
header("HTTP/1.0 404 Not Found");
exit();
}
}
public function environment() {
header("content-type: text/javascript");
$this->setLayout(null);
$this->display('environment', array(
'language' => I18n::getLocale(),
'stylesheet' => get_url('plugin/'.self::PLUGIN_ID.'/stylesheet'),
));
}
/**
* Private function that provide the default values for the view.
*
* @return default values
*/
private function get_default_view_vars() {
$vars = array();
$vars['plugin_id'] = self::PLUGIN_ID;
$vars['plugin_url'] = get_url('plugin/'.self::PLUGIN_ID.'/');
return $vars;
}
/**
* Overwrite the render function to enforce that some variables are
* available for the whole view artifacts.
* Simplify the view file handling by prefixing the file with the
* plugin directory.
*
* @param view the view file
* @param vars parameter for the views
* @return the view
*/
/*@overwrite('render')*/
public function render($view, $vars=array()) {
$vars = array_merge($this->get_default_view_vars(), $vars);
/* We only render views for this plugin. So add the prefix of the view folder to every view file. */
return parent::render(self::VIEW_FOLDER.$view, $vars);
}
/**
* View factory for the controller and the view.
*
* @param view the filename without the postfix
* @param vars the template vars
* @return a view object
*/
public function create_view($view, $vars=array()) {
$vars = array_merge($this->get_default_view_vars(), $vars);
return new View(self::PLUGIN_REL_VIEW_FOLDER.$view, $vars);
}
}
?>