Thread: [Pieforms-commit] SF.net SVN: pieforms: [2] pieforms/src/pieform.php
Status: Alpha
Brought to you by:
oracleshinoda
From: <ora...@us...> - 2006-11-18 09:30:45
|
Revision: 2 http://svn.sourceforge.net/pieforms/?rev=2&view=rev Author: oracleshinoda Date: 2006-11-18 01:30:45 -0800 (Sat, 18 Nov 2006) Log Message: ----------- * Fixed default method to be 'get' * Make sure forms have at least one element * Change hsc() calls to self::hsc, and implement that This commit is a bit messy, but I'm slowly getting it all working. Modified Paths: -------------- pieforms/src/pieform.php Modified: pieforms/src/pieform.php =================================================================== --- pieforms/src/pieform.php 2006-11-17 13:01:30 UTC (rev 1) +++ pieforms/src/pieform.php 2006-11-18 09:30:45 UTC (rev 2) @@ -287,10 +287,13 @@ } } } + else { + $formconfig = array(); + } // Assign defaults for the form $formdefaults = array( - 'method' => 'post', + 'method' => 'get', 'action' => '', 'onsubmit' => '', 'ajaxpost' => false, @@ -352,7 +355,7 @@ $this->iscancellable = (isset($data['iscancellable']) && !$data['iscancellable']) ? false : true; - if (!is_array($data['elements'])) { + if (!is_array($data['elements']) || count($data['elements']) == 0) { throw new PieformException('Forms must have a list of elements'); } $this->elements = $data['elements']; @@ -588,6 +591,7 @@ // @todo masks attempts in pieform_render_element, including the error handling there @include_once('pieform/renderers/' . $this->renderer . '.php'); + // Form header $function = 'pieform_renderer_' . $this->renderer . '_header'; if (function_exists($function)) { @@ -939,10 +943,10 @@ */ public static function make_id($element) { if (isset($element['id'])) { - return hsc($element['id']); + return self::hsc($element['id']); } if (isset($element['name'])) { - return hsc($element['name']); + return self::hsc($element['name']); } return substr(md5(mt_rand()), 0, 4); } @@ -1008,7 +1012,7 @@ $result = ''; foreach ($elementattributes as $attribute) { if (isset($element[$attribute]) && $element[$attribute] !== '') { - $result .= ' ' . $attribute . '="' . hsc($element[$attribute]) . '"'; + $result .= ' ' . $attribute . '="' . self::hsc($element[$attribute]) . '"'; } } @@ -1059,6 +1063,16 @@ } /** + * HTML-escapes the given value + * + * @param string $text The text to escape + * @return string The text, HTML escaped + */ + public static function hsc($text) { + return htmlspecialchars($text, ENT_COMPAT, 'UTF-8'); + } + + /** * Returns elements with errors on them * * @return array An array of elements with errors on them, the empty array @@ -1098,6 +1112,7 @@ } } } + } @@ -1126,18 +1141,18 @@ // Make sure that the function to render the element type is available $function = 'pieform_render_' . $element['type']; - if (!function_exists($function)) { - @include('pieform/elements/' . $element['type'] . '.php'); - if (!function_exists($function)) { - throw new PieformException('No such form element: ' . $element['type']); - } - } + //if (!function_exists($function)) { + // @include('pieform/elements/' . $element['type'] . '.php'); + // if (!function_exists($function)) { + // throw new PieformException('No such form element: ' . $element['type']); + // } + //} // Work out the renderer function required and make sure it exists if ($renderer = $form->get_renderer()) { $rendererfunction = 'pieform_renderer_' . $renderer; if (!function_exists($rendererfunction)) { - @include('pieform/renderers/' . $renderer . '.php'); + include('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. |
From: <ora...@us...> - 2006-11-18 13:04:34
|
Revision: 31 http://svn.sourceforge.net/pieforms/?rev=31&view=rev Author: oracleshinoda Date: 2006-11-18 05:04:32 -0800 (Sat, 18 Nov 2006) Log Message: ----------- * If the form method is 'get' and a file upload element is in the form, notify the user * Make sure the is_submitted() method is declared public * Added a method for displaying information, which allows a callback function to override its behaviour Modified Paths: -------------- pieforms/src/pieform.php Modified: pieforms/src/pieform.php =================================================================== --- pieforms/src/pieform.php 2006-11-18 12:52:08 UTC (rev 30) +++ pieforms/src/pieform.php 2006-11-18 13:04:32 UTC (rev 31) @@ -378,6 +378,10 @@ } if ($element['type'] == 'file') { $this->fileupload = true; + if ($this->method == 'get') { + $this->method = 'post'; + self::info("Your form '$this->name' had the method 'get' and also a file element - it has been converted to 'post'"); + } } if ($element['type'] == 'fieldset') { foreach ($element['elements'] as $subname => &$subelement) { @@ -393,6 +397,10 @@ } if ($subelement['type'] == 'file') { $this->fileupload = true; + if ($this->method == 'get') { + $this->method = 'post'; + self::info("Your form '$this->name' had the method 'get' and also a file element - it has been converted to 'post'"); + } } if (!$autofocusadded && $this->autofocus === true) { $subelement['autofocus'] = true; @@ -561,7 +569,7 @@ * * @return bool */ - function is_submitted() { + public function is_submitted() { return $this->submitted; } @@ -1073,6 +1081,19 @@ } /** + * Hook for giving information back to the developer + */ + public static function info($message) { + $function = 'pieform_info'; + if (function_exists($function)) { + $function($message); + } + else { + trigger_error($message, E_USER_NOTICE); + } + } + + /** * Returns elements with errors on them * * @return array An array of elements with errors on them, the empty array This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ora...@us...> - 2006-11-19 00:36:14
|
Revision: 32 http://svn.sourceforge.net/pieforms/?rev=32&view=rev Author: oracleshinoda Date: 2006-11-18 16:36:14 -0800 (Sat, 18 Nov 2006) Log Message: ----------- Generalised processingStart and processingStop to be configurable. Now validation of JS callback names is done. Properly i18n'd a couple of ajax related strings Modified Paths: -------------- pieforms/src/pieform.php Modified: pieforms/src/pieform.php =================================================================== --- pieforms/src/pieform.php 2006-11-18 13:04:32 UTC (rev 31) +++ pieforms/src/pieform.php 2006-11-19 00:36:14 UTC (rev 32) @@ -152,6 +152,21 @@ private $ajaxpost = false; /** + * A callback to call before submitting the form via AJAX + * + * @var string + */ + private $preajaxsubmitcallback = ''; + + /** + * A callback to call after submitting the form via AJAX, regardless of + * the result of the submission + * + * @var string + */ + private $postajaxsubmitcallback = ''; + + /** * Name of a javascript function to call on successful ajax submission * * @var string @@ -297,8 +312,10 @@ 'action' => '', 'onsubmit' => '', 'ajaxpost' => false, - 'ajaxsuccessfunction' => '', - 'ajaxfailurefunction' => '', + 'preajaxsubmitcallback' => '', + 'postajaxsubmitcallback' => '', + 'ajaxsuccessfunction' => '', + 'ajaxfailurefunction' => '', 'autofocus' => false, 'language' => 'en', 'validate' => true, @@ -338,8 +355,11 @@ if ($data['ajaxpost']) { $this->ajaxpost = true; - $this->ajaxsuccessfunction = $data['ajaxsuccessfunction']; - $this->ajaxfailurefunction = $data['ajaxfailurefunction']; + $this->preajaxsubmitcallback = self::validate_js_callback($data['preajaxsubmitcallback']); + $this->postajaxsubmitcallback = self::validate_js_callback($data['postajaxsubmitcallback']); + // @todo rename to *callback instead of *function for consistency + $this->ajaxsuccessfunction = self::validate_js_callback($data['ajaxsuccessfunction']); + $this->ajaxfailurefunction = self::validate_js_callback($data['ajaxfailurefunction']); } if (isset($data['renderer'])) { @@ -783,7 +803,11 @@ // Also should only save wysiwyg elements in the form, not all of them... if (typeof(tinyMCE) != 'undefined') { tinyMCE.triggerSave(); } - processingStart(); +EOF; + if (!empty($this->preajaxsubmitcallback)) { + $result .= " $this->preajaxsubmitcallback();\n"; + } + $result .= <<<EOF var data = {}; EOF; @@ -849,15 +873,14 @@ } } - $result .= <<<EOF + $result .= " }\n"; + if (!empty($this->postajaxsubmitcallback)) { + $result .= " $this->postajaxsubmitcallback();\n"; } - processingStop(); -EOF; - // @todo not with get_string! $this->i18n instead - $strunknownerror = get_string('unknownerror'); - $strprocessingform = get_string('processingform'); + $strunknownerror = $this->i18n('ajaxunknownerror'); + $strprocessingform = $this->i18n('processingform'); $result .= <<<EOF }, function() { @@ -1093,6 +1116,15 @@ } } + private static function validate_js_callback($name) { + if ($name == '') { + return ''; + } + if (!preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $name)) { + throw new PieformException("'$name' is not a valid javascript callback name"); + } + } + /** * Returns elements with errors on them * This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ora...@us...> - 2006-11-19 22:28:03
|
Revision: 41 http://svn.sourceforge.net/pieforms/?rev=41&view=rev Author: oracleshinoda Date: 2006-11-19 14:27:55 -0800 (Sun, 19 Nov 2006) Log Message: ----------- Store the form data in its own array. Eventually almost all fields will be merged into this. Added a method to get a property from this array. Added a todo about removing prefix and suffix support from core Modified Paths: -------------- pieforms/src/pieform.php Modified: pieforms/src/pieform.php =================================================================== --- pieforms/src/pieform.php 2006-11-19 12:25:31 UTC (rev 40) +++ pieforms/src/pieform.php 2006-11-19 22:27:55 UTC (rev 41) @@ -106,6 +106,14 @@ class Pieform { /** + * Data for the form + * + * @var array + * @todo move all of the member fields here into this field + */ + private $data = array(); + + /** * Maintains a tab index across all created forms, to make it easy for * people to forget about it and have it just work for all of their forms. * @@ -333,6 +341,7 @@ 'validatefunction' => '', ); $data = array_merge($formdefaults, $formconfig, $data); + $this->data = $data; // Set the method - only get/post allowed $data['method'] = strtolower($data['method']); @@ -557,6 +566,18 @@ } /** + * Returns a generic property. This can be used to retrieve any property + * set in the form data array, so developers can pass in random stuff and + * get access to it. + * + * @param string The key of the property to return + * @return mixed + */ + public function get_property($key) { + return $this->data[$key]; + } + + /** * Returns the form name * * @return string @@ -1229,6 +1250,7 @@ $builtelement = $function($element, $form); // Prepare the prefix and suffix + // @tod REMOVE THIS: each renderer should support it seperately $prefix = (isset($element['prefix'])) ? $element['prefix'] : ''; $suffix = (isset($element['suffix'])) ? $element['suffix'] : ''; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ora...@us...> - 2006-11-19 22:28:46
|
Revision: 42 http://svn.sourceforge.net/pieforms/?rev=42&view=rev Author: oracleshinoda Date: 2006-11-19 14:28:46 -0800 (Sun, 19 Nov 2006) Log Message: ----------- Changed the default language name to en.utf8 instead of en Modified Paths: -------------- pieforms/src/pieform.php Modified: pieforms/src/pieform.php =================================================================== --- pieforms/src/pieform.php 2006-11-19 22:27:55 UTC (rev 41) +++ pieforms/src/pieform.php 2006-11-19 22:28:46 UTC (rev 42) @@ -224,7 +224,7 @@ * * @var string */ - private $language = 'en'; + private $language = 'en.utf8'; /** * Language strings for rules @@ -232,7 +232,7 @@ * @var array */ private $language_strings = array( - 'en' => array( + 'en.utf8' => array( 'required' => 'This field is required', 'email' => 'E-mail address is invalid', 'maxlength' => 'This field must be at most %d characters long', @@ -333,7 +333,7 @@ 'ajaxsuccessfunction' => '', 'ajaxfailurefunction' => '', 'autofocus' => false, - 'language' => 'en', + 'language' => 'en.utf8', 'validate' => true, 'submit' => true, 'elements' => array(), This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |