Skip to content
Snippets Groups Projects
Commit 40a6902a authored by Jared Hancock's avatar Jared Hancock
Browse files

plugins: Custom configuration

Allow plugins to define their own custom configuration pages. This is
provided so that plugins can define more sophisticated configurations above
the standard single-form configuration previously provided.
parent b4729d30
Branches
Tags
No related merge requests found
...@@ -22,6 +22,10 @@ class PluginConfig extends Config { ...@@ -22,6 +22,10 @@ class PluginConfig extends Config {
return array(); return array();
} }
function hasCustomConfig() {
return $this instanceof PluginCustomConfig;
}
/** /**
* Retreive a Form instance for the configurable options offered in * Retreive a Form instance for the configurable options offered in
* ::getOptions * ::getOptions
...@@ -55,6 +59,15 @@ class PluginConfig extends Config { ...@@ -55,6 +59,15 @@ class PluginConfig extends Config {
* array. * array.
*/ */
function commit(&$errors=array()) { function commit(&$errors=array()) {
global $msg;
if ($this->hasCustomConfig())
return $this->saveCustomConfig($errors);
return $this->commitForm($errors);
}
function commitForm(&$errors=array()) {
$f = $this->getForm(); $f = $this->getForm();
$commit = false; $commit = false;
if ($f->isValid()) { if ($f->isValid()) {
...@@ -68,7 +81,11 @@ class PluginConfig extends Config { ...@@ -68,7 +81,11 @@ class PluginConfig extends Config {
$field = $f->getField($name); $field = $f->getField($name);
$dbready[$name] = $field->to_database($val); $dbready[$name] = $field->to_database($val);
} }
return $this->updateAll($dbready); if ($this->updateAll($dbready)) {
if (!$msg)
$msg = 'Successfully updated configuration';
return true;
}
} }
return false; return false;
} }
...@@ -93,6 +110,20 @@ class PluginConfig extends Config { ...@@ -93,6 +110,20 @@ class PluginConfig extends Config {
} }
} }
/**
* Interface: PluginCustomConfig
*
* Allows a plugin to specify custom configuration pages. If the
* configuration cannot be suited by a single page, single form, then
* the plugin can use the ::renderCustomConfig() method to trigger
* rendering the page, and use ::saveCustomConfig() to trigger
* validating and saving the custom configuration.
*/
interface PluginCustomConfig {
function renderCustomConfig();
function saveCustomConfig();
}
class PluginManager { class PluginManager {
static private $plugin_info = array(); static private $plugin_info = array();
static private $plugin_list = array(); static private $plugin_list = array();
......
...@@ -3,10 +3,12 @@ ...@@ -3,10 +3,12 @@
$info=array(); $info=array();
if($plugin && $_REQUEST['a']!='add') { if($plugin && $_REQUEST['a']!='add') {
$config = $plugin->getConfig(); $config = $plugin->getConfig();
if ($config) if (!($page = $config->hasCustomConfig())) {
$form = $config->getForm(); if ($config)
if ($_POST) $form = $config->getForm();
$form->isValid(); if ($form && $_POST)
$form->isValid();
}
$title = __('Update Plugin'); $title = __('Update Plugin');
$action = 'update'; $action = 'update';
$submit_text = __('Save Changes'); $submit_text = __('Save Changes');
...@@ -23,19 +25,23 @@ $info=Format::htmlchars(($errors && $_POST)?$_POST:$info); ...@@ -23,19 +25,23 @@ $info=Format::htmlchars(($errors && $_POST)?$_POST:$info);
<br/><small><?php echo $plugin->getName(); ?></small></h2> <br/><small><?php echo $plugin->getName(); ?></small></h2>
<h3><?php echo __('Configuration'); ?></h3> <h3><?php echo __('Configuration'); ?></h3>
<?php
if ($page)
$config->renderCustomConfig();
elseif ($form) { ?>
<table class="form_table" width="940" border="0" cellspacing="0" cellpadding="2"> <table class="form_table" width="940" border="0" cellspacing="0" cellpadding="2">
<tbody> <tbody>
<?php $form->render(); ?>
</tbody></table>
<?php <?php
if ($form) }
$form->render();
else { ?> else { ?>
<tr><th><?php echo __('This plugin has no configurable settings'); ?><br> <tr><th><?php echo __('This plugin has no configurable settings'); ?><br>
<em><?php echo __('Every plugin should be so easy to use.'); ?></em></th></tr> <em><?php echo __('Every plugin should be so easy to use.'); ?></em></th></tr>
<?php } <?php }
?> ?>
</tbody></table>
<p class="centered"> <p class="centered">
<?php if ($form) { ?> <?php if ($page || $form) { ?>
<input type="submit" name="submit" value="<?php echo $submit_text; ?>"> <input type="submit" name="submit" value="<?php echo $submit_text; ?>">
<input type="reset" name="reset" value="<?php echo __('Reset'); ?>"> <input type="reset" name="reset" value="<?php echo __('Reset'); ?>">
<?php } ?> <?php } ?>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment