[Pieforms-commit] SF.net SVN: pieforms: [85] pieforms-php5/trunk/src/pieform.php
Status: Alpha
Brought to you by:
oracleshinoda
From: <ora...@us...> - 2006-12-08 02:11:56
|
Revision: 85 http://svn.sourceforge.net/pieforms/?rev=85&view=rev Author: oracleshinoda Date: 2006-12-07 18:11:55 -0800 (Thu, 07 Dec 2006) Log Message: ----------- Standardised how plugins are included Modified Paths: -------------- pieforms-php5/trunk/src/pieform.php Modified: pieforms-php5/trunk/src/pieform.php =================================================================== --- pieforms-php5/trunk/src/pieform.php 2006-12-08 02:09:46 UTC (rev 84) +++ pieforms-php5/trunk/src/pieform.php 2006-12-08 02:11:55 UTC (rev 85) @@ -72,7 +72,6 @@ // - Collapsible js for fieldsets // - Grippie for textareas // - javascript validation - // - handle multiple submit buttons // - handle multipage forms? // - handle a tabbed interface type of form? // @@ -203,6 +202,13 @@ 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 @@ '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 @@ 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 @@ // 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 @@ // @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 @@ $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 @@ } // 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 @@ */ 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 @@ // 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 . '"'); } @@ -963,7 +970,7 @@ $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 . '"'); } @@ -1146,6 +1153,37 @@ } /** + * 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 * * Returns english by default. @@ -1272,7 +1310,7 @@ 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 . '"'); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |