Source for file helpers.php
Documentation is available at helpers.php
* File containing the Helpers class and associated functions
* @version $Id: helpers.php 280 2007-01-30 06:55:13Z john $
* @copyright (c) 2005 John Peterson
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* A collection of methods used to generate basic HTML/XML.
* @todo Document this variable
* @todo Document this variable
* @todo Document this variable
* Current controller object
* Local copy of Trax::$current_controller_object<br />
* <b>NB:</b> {@link object()} faults if this does not contain a
* valid instance of ActionController.
* Current controller name
* Local copy of Trax::$current_controller_name
* Current controller path
* Local copy of Trax::$current_controller_path
* Construct a Helpers object
* @param string Name of ActiveRecord subclass
* @param string Attribute of ActiveRecord subclass
* @uses controller_object
function __construct($object_name = null, $attribute_name = null) {
if(substr($object_name, - 2) == "[]") {
// Copy controller information from $GLOBALS
? Trax::$current_controller_name : null;
? Trax::$current_controller_path : null;
&& is_object(Trax::$current_controller_object))
? Trax::$current_controller_object : null;
$index = $object->index_on; # should be primary key (usually id field)
* Get value of current attribute in the current ActiveRecord object
* If there is a value in $_REQUEST[][], return it.
* Otherwise fetch the value from the database.
* @uses ActiveRecord::send()
protected function value() {
// Attribute value not found in $_REQUEST. Find the
// ActiveRecord subclass instance and query it.
//$value = $object->send($this->attribute_name);
* Given the name of an ActiveRecord subclass, find an instance
* Finds the AR instance from the ActionController instance.
* Assumes that if a $object_name is defined either as the
* argument or an instance variable, then there must be
* a controller object instance which points to a single instance
* <b>FIXME:</b> Handle errors better.
* @param string Name of an ActiveRecord subclass or null
* @return mixed Instance of the subclass, or null if
* @uses controller_object
protected function object($object_name = null) {
$object_name = $object_name ? $object_name : $this->object_name;
* Convert array of tag attribute names and values to string
* @param string[] $options
foreach($options as $key => $value) {
* Convert selected attributes to proper XML boolean form
* @uses boolean_attribute()
* @param string[] $options
* @return string[] Input argument with selected attributes converted
* to proper XML boolean form
foreach(array('disabled', 'readonly', 'multiple') as $a) {
* Convert an attribute to proper XML boolean form
* @param string[] $options
* @param string $attribute
* @return void Contents of $options have been converted
&& $options[$attribute]) {
$options[$attribute] = $attribute;
unset ($options[$attribute]);
* Wrap CDATA begin and end tags around argument
* Returns a CDATA section for the given content. CDATA sections
* are used to escape blocks of text containing characters which would
* otherwise be recognized as markup. CDATA sections begin with the string
* <samp><![CDATA[</samp> and end with (and may not contain) the string
* @param string $content Content to wrap
* @return string Wrapped argument
return "<![CDATA[". $content. "]]>";
* Generate an HTML or XML tag with optional attributes and self-ending
* <li>Example: <samp>tag("br");</samp><br>
* Returns: <samp><br />\n</samp></li>
* <li> Example: <samp>tag("div", array("class" => "warning"), true);</samp><br>
* Returns: <samp><div class="warning">\n</samp></li>
* @param string $name Tag name
* @param string[] $options Tag attributes to apply, specified as
* array('attr1' => 'value1'[, 'attr2' => 'value2']...)
* <li>true => make opening tag (end with '>')</li>
* <li>false => make self-terminating tag (end with ' \>')</li>
* @return string The generated tag, followed by "\n"
function tag($name, $options = array(), $open = false) {
$html .= $open ? ">" : " />";
* Generate an open/close pair of tags with optional attributes and content between
* <li>Example: <samp>content_tag("p", "Hello world!");</samp><br />
* Returns: <samp><p>Hello world!</p>\n</samp><li>
* <samp>content_tag("div",
* content_tag("p", "Hello world!"),
* array("class" => "strong"));</samp><br />
* <samp><div class="strong"><p>Hello world!</p></div>\n</samp></li>
* @param string $name Tag to wrap around $content
* @param string $content Text to put between tags
* @param string[] $options Tag attributes to apply, specified as
* array('attr1' => 'value1'[, 'attr2' => 'value2']...)
* @return string Text wrapped with tag and attributes,
function content_tag($name, $content, $options = array()) {
if(isset ($options['strip_slashes'])) {
$html .= ">$content</$name>";
* If this tag has an error, wrap it with a visual indicator
* @param string HTML to be wrapped
* @param boolean true=>error, false=>no error
return ($has_error ? '<span class="fieldWithErrors">' . eregi_replace("[\n\r]", '', $html_tag) . '</span>' : $html_tag);
* Create a Helpers object and call its content_tag() method
* @see Helpers::content_tag()
* @param string $name Tag to wrap around $content
* @param string $content Text to put between tags
* @param string[] $options Tag attributes to apply
* @return string Text wrapped with tag and attributes,
* Create a Helpers object and call its tag() method
* @param string $name Tag name
* @param string[] $options Tag attributes to apply
* <li>true => make opening tag (end with '>')</li>
* <li>false => make self-terminating tag (end with ' \>')</li>
* @return string The tag, followed by "\n"
* Create a Helpers object and call its cdata_section() method
// -- set Emacs parameters --
// c-hanging-comment-ender-p: nil
|