Source for file active_record_helper.php
Documentation is available at active_record_helper.php
* File containing ActiveRecordHelper class and support functions
* @version $Id: active_record_helper.php 277 2007-01-22 11:55:57Z 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.
* @todo Document this class
* Whether to generate scaffolding HTML
* Set to true in {@link form_scaffolding.phtml}. If true
* generate HTML scaffold otherwise generate final HTML
* Returns a default input tag for the type of object returned by the method. Example
* (title is a VARCHAR column and holds "Hello World"):
* input("post", "title") =>
* <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
function input($object_name, $attribute_name, $options = array()) {
return $this->to_tag($object_name, $attribute_name, $options);
* @todo Document this method
* @uses to_scaffold_tag()
* Returns an entire form with input tags and everything for a specified Active Record object. Example
* (post is a new record that has a title using VARCHAR and a body using TEXT):
* <form action='/post/create' method='post'>
* <label for="post_title">Title</label><br />
* <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
* <label for="post_body">Body</label><br />
* <textarea cols="40" id="post_body" name="post[body]" rows="20">
* Back to the hill and over it again!
* <input type='submit' value='Create' />
* It's possible to specialize the form builder by using a different action name and by supplying another
* block renderer. Example (entry is a new record that has a message attribute using VARCHAR):
* form("entry", array('action' => "sign", 'input_block' =>
* 'foreach($record->content_columns() as $column_name => $column) $contents .= Inflector::humanize($column_name) . ": " . input($record, $column) . "<br />"')) =>
* <form action='/post/sign' method='post'>
* <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /><br />
* <input type='submit' value='Sign' />
* It's also possible to add additional content to the form by giving it a block, such as:
* form("entry", array('action' => "sign", 'block' =>
* content_tag("b", "Department") .
* collection_select("department", "id", $departments, "id", "name"))
* @uses Helpers::object()
function form($record_name, $options = array()) {
$record = $this->object($record_name);
$options["action"] = $options[":action"] ? $options[":action"] : $record->is_new_record() ? "add" : "save";
$action = url_for(array(':action' => $options[':action'], ':id' => $record->id));
$submit_value = (isset ($options['submit_value']) ? $options['submit_value'] : ucfirst(preg_replace('/[^\w]/', '', $options[':action'])));
if(!$record->is_new_record()) $contents .= hidden_field($record_name, 'id');
if(isset ($options['block'])) $contents .= eval ($options['block']);
$contents .= "<br>". submit_tag($submit_value). "<br><br>";
return $this->content_tag('form', $contents, array('action' => $action, 'method' => 'post'));
* Returns a string containing the error message attached to the +method+ on the +object+, if one exists.
* This error message is wrapped in a DIV tag, which can be specialized to include both a +prepend_text+ and +append_text+
* to properly introduce the error and a +css_class+ to style it accordingly. Examples (post has an error message
* "can't be empty" on the title attribute):
* <?= error_message_on("post", "title") ?> =>
* <div class="formError">can't be empty</div>
* <?= error_message_on "post", "title", "Title simply ", " (or it won't work)", "inputError" ?> =>
* <div class="inputError">Title simply can't be empty (or it won't work)</div>
* @uses controller_object
function error_message_on($object_name, $attribute_name, $prepend_text = "", $append_text = "", $css_class = "formError") {
if($errors = $object->errors[$attribute_name]) {
return $this->content_tag("div", $prepend_text . (is_array($errors) ? current($errors) : $errors) . $append_text, array('class' => $css_class));
* Returns a string with a div containing all the error messages for the object located as an instance variable by the name
* of <tt>object_name</tt>. This div can be tailored by the following options:
* <tt>header_tag</tt> - Used for the header of the error div (default: h2)
* <tt>id</tt> - The id of the error div (default: errorExplanation)
* <tt>class</tt> - The class of the error div (default: errorExplanation)
* @param mixed object_name The name of a PHP class, or
* an object instance of that class
* @param string[] options Set of options: 'header_tag', 'id', 'class', 'header_message', 'header_sub_message'
* @uses Inflector::humanize()
//echo "object name:".$object_name;
if(!empty($object->errors)) {
$id = isset ($options['id']) ? $options['id'] : "ErrorExplanation";
$class = isset ($options['class']) ? $options['class'] : "ErrorExplanation";
$header_tag = isset ($options['header_tag']) ? $options['header_tag'] : "h2";
$header_message = isset ($options['header_message']) ?
$options['header_message'] : "%s prohibited this %s from being saved";
$header_sub_message = isset ($options['header_sub_message']) ?
$options['header_sub_message'] : "There were problems with the following fields:";
array("id" => $id, "class" => $class)
* @todo Document this method
* @uses default_input_block()
//if($record_name) $this->object_name = $record_name;
$input_block = (isset ($options['input_block']) ? $options['input_block'] : $this->default_input_block());
if(is_array($record->content_columns)) {
foreach($record->content_columns as $column) {
//$contents .= "<p><label for=\"".$record_name."_".$column['name']."\">";
//$contents .= Inflector::humanize($column['name']) . ":</label><br />";
//$contents .= input($record_name, $column['name']) . "</p>\n";
if(!in_array($column['name'], $record->primary_keys)) {
eval ($input_block) . "\n";
* @todo Document this method
* @uses input_scaffolding()
return '$contents .= "<p><label for=\"{$record_name}_{$column[\'name\']}\">" . Inflector::humanize($column[\'name\']) . ":</label><br/>\n<?= " . input_scaffolding($record_name, $column[\'name\']) . " ?></p>\n";';
return '$contents .= "<p><label for=\"{$record_name}_{$column[\'name\']}\">" . Inflector::humanize($column[\'name\']) . ":</label><br/>\n" . input($record_name, $column[\'name\']) . "</p>\n";';
* @todo Document this method
* @param string object_name Name of an ActiveRecord subclass
* @param string attribute_name Name of an attribute of $object_name
* @param string[] options
* @uses DateHelper::to_date_select_tag()
* @uses FormHelper::to_boolean_select_tag()
* @uses FormHelper::to_input_field_tag()
* @uses FormHelper::to_text_area_tag()
* @uses to_datetime_select_tag()
function to_tag($object_name, $attribute_name, $options = array()) {
$form = new FormHelper($object_name, $attribute_name);
$results = $form->to_input_field_tag($field_type, $options);
$results = $form->to_text_area_tag($options);
$results = $form->to_input_field_tag("text", $options);
$form = new DateHelper($object_name, $attribute_name);
$results = $form->to_date_select_tag($options);
$results = $form->to_boolean_select_tag($options);
* @todo Document this method
function to_scaffold_tag($object_name, $attribute_name, $options = array()) {
$results = $field_type. "_field(\"$object_name\", \"$attribute_name\")";
$results = "text_area(\"$object_name\", \"$attribute_name\")";
$results = "text_field(\"$object_name\", \"$attribute_name\")";
$results = "date_select(\"$object_name\", \"$attribute_name\")";
$results = "year_select(\"$object_name\", \"$attribute_name\")";
$results = "datetime_select(\"$object_name\", \"$attribute_name\")";
$results = "time_select(\"$object_name\", \"$attribute_name\")";
$results = "boolean_select(\"$object_name\", \"$attribute_name\")";
echo "No case statement for ". $this->column_type(). "\n";
* @todo Document this method
* @todo Document this method
* @uses tag_without_error_wrapping()
function tag($name, $options = array()) {
* @todo Document this method
* @todo Document this method
* @uses content_tag_without_error_wrapping()
function content_tag($name, $value, $options = array()) {
* @todo Document this method
* @uses DateHelper::to_date_select_tag()
* @todo Document this method
* @todo Document this method
* @uses DateHelper::to_datetime_select_tag()
* @todo Document this method
* @uses to_datetime_select_tag_without_error_wrapping
* @todo Document this method
* @todo Document this method
* @uses ActiveRecord::column_type()
* @todo Document this API
$object = $object_name_or_object;
$object = $this->object($object_name_or_object);
<select name=\"per_page\" onChange=\"document.location = '?". escape_javascript($object->paging_extra_params). "&per_page=' + this.options[this.selectedIndex].value;\">
<option value=\"$object->rows_per_page\" selected>$default_text</option>
<option value=10>10</option>
<option value=20>20</option>
<option value=50>50</option>
<option value=100>100</option>
<option value=999999999>ALL</option>
* @todo Document this API
* @return string HTML to link to previous and next pages
* @uses $paging_extra_params
$object = $object_name_or_object;
$object = $this->object($object_name_or_object);
//$html = "<pre>".print_r($object,1);
/* Print the first and previous page links if necessary */
if(($object->page != 1) && ($object->page)) {
"?$object->paging_extra_params&page=1&per_page=$object->rows_per_page",
if(($object->page- 1) > 0) {
"?$object->paging_extra_params&page=". ($object->page- 1). "&per_page=$object->rows_per_page",
"title" => "Previous Page"
if($object->pages < $object->display) {
$object->display = $object->pages;
if($object->page == $object->pages) {
if(($object->pages - $object->display) == 0) {
$start = $object->pages - $object->display;
if($object->page >= $object->display) {
$start = $object->page - ($object->display / 2);
$end = $object->page + (($object->display / 2) - 1);
if($end >= $object->pages) {
/* Print the numeric page list; make the current page unlinked and bold */
for($i= $start; $i<= $end; $i++ ) {
if($i == $object->page) {
$html .= "<span class=\"pageList\"><b>". $i. "</b></span>";
"?$object->paging_extra_params&page=$i&per_page=$object->rows_per_page",
/* Print the Next and Last page links if necessary */
if(($object->page+ 1) <= $object->pages) {
"?$object->paging_extra_params&page=". ($object->page+ 1). "&per_page=$object->rows_per_page",
if(($object->page != $object->pages) && ($object->pages != 0)) {
"?$object->paging_extra_params&page=". $object->pages. "&per_page=$object->rows_per_page",
$object = $object_name_or_object;
$object = $this->object($object_name_or_object);
$end = $object->rows_per_page * $object->page;
$start = $end - ($object->rows_per_page - 1);
if($end >= $object->pagination_count) {
$end = $object->pagination_count;
return $object->pagination_count ? sprintf($format, $start, $end, $object->pagination_count) : null;
* Avialble functions for use in views
* error_message_on($object, $attribute_name, $prepend_text = "", $append_text = "", $css_class = "formError")
* @uses ActiveRecordHelper::error_message_on()
* error_messages_for($object_name, $options = array())
* @uses ActiveRecordHelper::error_messages_for()
* form($record_name, $options = array())
* @uses ActiveRecordHelper::form()
* Returns a default input tag for the type of object returned by the method. Example
* (title is a VARCHAR column and holds "Hello World"):
* input("post", "title") =>
* <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
* @uses ActiveRecordHelper::input()
* @uses ActiveRecordHelper::input_scaffolding()
* @uses ActiveRecordHelper::pagination_limit_select()
* @uses ActiveRecordHelper::pagination_links()
* @uses ActiveRecordHelper::pagination_range_text()
// -- set Emacs parameters --
// c-hanging-comment-ender-p: nil
|