Commit ed97a31e authored by Nigel McNie's avatar Nigel McNie Committed by Nigel McNie
Browse files

Standardised how plugins are included. They can now come from an array of

configuration directories as well as the default location.
parent 679d8980
......@@ -72,7 +72,6 @@ function pieform($data) {
// - Collapsible js for fieldsets
// - Grippie for textareas
// - javascript validation
// - handle multiple submit buttons
// - handle multipage forms?
// - handle a tabbed interface type of form?
//
......@@ -202,6 +201,13 @@ class Pieform {
*/
private $tabindex = 1;
/**
* Directories to look for elements, renderers and rules
*
* @var array
*/
private $configdirs = array();
/**
* Whether to autofocus fields in this form, and if so, optionally which
* field to focus.
......@@ -332,11 +338,12 @@ class Pieform {
'postajaxsubmitcallback' => '',
'ajaxsuccessfunction' => '',
'ajaxfailurefunction' => '',
'autofocus' => false,
'language' => 'en.utf8',
'validate' => true,
'submit' => true,
'elements' => array(),
'configdirs' => array(),
'autofocus' => false,
'language' => 'en.utf8',
'validate' => true,
'submit' => true,
'elements' => array(),
'submitfunction' => '',
'validatefunction' => '',
);
......@@ -348,12 +355,15 @@ class Pieform {
if ($data['method'] != 'post') {
$data['method'] = 'get';
}
$this->method = $data['method'];
$this->action = $data['action'];
$this->validate = $data['validate'];
$this->submit = $data['submit'];
$this->autofocus = $data['autofocus'];
$this->language = $data['language'];
$this->method = $data['method'];
$this->action = $data['action'];
$this->validate = $data['validate'];
$this->submit = $data['submit'];
$this->configdirs = array_map(
create_function('$a', 'return substr($a, -1) == "/" ? substr($a, 0, -1) : $a;'),
(array) $data['configdirs']);
$this->autofocus = $data['autofocus'];
$this->language = $data['language'];
if ($data['submitfunction']) {
$this->submitfunction = $data['submitfunction'];
......@@ -480,7 +490,7 @@ class Pieform {
// Let each element set and override attributes if necessary
if ($subelement['type'] != 'markup') {
$function = 'pieform_render_' . $subelement['type'] . '_set_attributes';
require_once('pieform/elements/' . $subelement['type'] . '.php');
$this->include_plugin('element', $subelement['type']);
if (function_exists($function)) {
$subelement = $function($subelement);
}
......@@ -506,7 +516,7 @@ class Pieform {
// @todo here, all elements are loaded that will be used, so no
// need to include files for them later (like in pieform_render_element)
// Also, don't use require_once so nicer errors can be thrown
require_once('pieform/elements/' . $element['type'] . '.php');
$this->include_plugin('element', $element['type']);
if (function_exists($function)) {
$element = $function($element);
}
......@@ -683,7 +693,7 @@ class Pieform {
$result .= ">\n";
// @todo masks attempts in pieform_render_element, including the error handling there
@include_once('pieform/renderers/' . $this->renderer . '.php');
$this->include_plugin('renderer', $this->renderer);
// Form header
$function = 'pieform_renderer_' . $this->renderer . '_header';
......@@ -705,7 +715,7 @@ class Pieform {
}
// Hidden elements
require_once('pieform/elements/hidden.php');
$this->include_plugin('element', 'hidden');
foreach ($this->get_elements() as $element) {
if ($element['type'] == 'hidden') {
$result .= pieform_render_hidden($element, $this);
......@@ -737,9 +747,6 @@ class Pieform {
*/
public function get_value($element) {
$function = 'pieform_get_value_' . $element['type'];
if (!function_exists($function)) {
@include_once('pieform/elements/' . $element['type'] . '.php');
}
// @todo for consistency, reverse parameter order - always a Form object first
if (function_exists($function)) {
return $function($element, $this);
......@@ -843,7 +850,7 @@ class Pieform {
// Get the rule
$function = 'pieform_rule_' . $rule;
if (!function_exists($function)) {
@include_once('pieform/rules/' . $rule . '.php');
$this->include_plugin('rule', $rule);
if (!function_exists($function)) {
throw new PieformException('No such form rule "' . $rule . '"');
}
......@@ -864,6 +871,7 @@ class Pieform {
*/
private function submit_js() {
// @todo nigel should disable all buttons on this form while the submit is happening
$result = <<<EOF
connect($('{$this->name}'), 'onsubmit', function (e) {
......@@ -903,6 +911,13 @@ EOF;
// Add the hidden element for detecting form submission
$result .= " data['pieform_{$this->name}'] = '';\n";
//if ($this->ajaxpostcallback) {
// $result .= " $this->ajaxpostcallback(data);\n}\n";
// return $result;
//}
$action = ($this->action) ? $this->action : basename($_SERVER['PHP_SELF']);
$method = ($this->get_method() == 'get') ? 'GET' : 'POST';
$result .= <<<EOF
......@@ -963,7 +978,7 @@ EOF;
$js_messages_function = 'pieform_renderer_' . $this->renderer . '_messages_js';
if (!function_exists($js_messages_function)) {
@include_once('pieform/renderers/' . $this->renderer . '.php');
$this->include_plugin('renderer', $this->renderer);
if (!function_exists($js_messages_function)) {
throw new PieformException('No renderer message function "' . $js_messages_function . '"');
}
......@@ -1145,6 +1160,37 @@ EOF;
return false;
}
/**
* Includes a plugin file, checking any configured plugin directories.
*
* @param string $type The type of plugin to include: 'element', 'renderer' or 'rule'
* @param string $name The name of the plugin to include
* @throws PieformException If the given type or plugin could not be found
*/
public function include_plugin($type, $name) {
if (!in_array($type, array('element', 'renderer', 'rule'))) {
throw new PieformException("The type \"$type\" is not allowed for an include plugin");
}
// Check the configured include paths if they are specified
foreach ($this->configdirs as $directory) {
$file = "$directory/{$type}s/$name.php";
if (is_readable($file)) {
include_once($file);
return;
}
}
// Check the default include path
$file = dirname(__FILE__) . "/pieform/{$type}s/{$name}.php";
if (is_readable($file)) {
include_once($file);
return;
}
throw new PieformException("Could not find $type \"$name\"");
}
/**
* Return an internationalised string based on the passed input key
*
......@@ -1272,7 +1318,7 @@ function pieform_render_element($element, Pieform $form) {
if ($renderer = $form->get_renderer()) {
$rendererfunction = 'pieform_renderer_' . $renderer;
if (!function_exists($rendererfunction)) {
include('pieform/renderers/' . $renderer . '.php');
$form->include_plugin('pieform/renderers/' . $renderer . '.php');
if (!function_exists($rendererfunction)) {
throw new PieformException('No such form renderer: "' . $renderer . '"');
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment