Source for file url_helper.php
Documentation is available at url_helper.php
* File containing the UrlHelper class and support functions
* @version $Id: url_helper.php 240 2006-08-02 09:21:48Z 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
* Creates a link tag of the given +name+ using an URL created by
* It's also possible to pass a string instead of an options hash
* to get a link tag that just points without consideration. If
* null is passed as a name, the link itself will become the
* The $html_options have a special feature for creating
* javascript confirm alerts where if you pass ":confirm" => 'Are
* the link will be guarded with a JS popup asking that
* question. If the user accepts, the link is processed, otherwise
* link_to("Delete this page", array(":action" => "delete",
* ":id" => $page->id), array(":confirm" => "Are you sure?"))
* @uses convert_confirm_option_to_javascript()
function link_to($name, $options = array(), $html_options = array()) {
$href = array("href" => $options);
if(count($html_options) > 0) {
$href = array("href" => $url);
if(count($html_options) > 0) {
* @todo Document this method
* @param string[] Options
$html_options['onclick'] =
"return confirm('". addslashes($html_options['confirm']). "');";
unset ($html_options['confirm']);
* @todo Document this method
foreach($bool_attrs as $x) {
* @todo Document this method
* @uses convert_boolean_attributes()
* @uses convert_confirm_option_to_javascript()
function button_to($name, $options = array(), $html_options = null) {
$html_options = (!is_null($html_options) ? $html_options : array());
$name = (!is_null($name) ? $name : $options);
array("type" => "submit", "value" => $name));
. "\" class=\"button-to\"><div>"
. $this->tag("input", $html_options) . "</div></form>";
* This tag is deprecated. Combine the link_to and
* AssetTagHelper::image_tag yourself instead, like:
* link_to(image_tag("rss", array("size" => "30x45"),
* array("border" => 0)), "http://www.example.com")
* @todo Document this method
$html_options = array()) {
$image_options = array("src" => (ereg("/", $src) ? $src : "/images/$src"));
if (!ereg(".", $image_options["src"])) $image_options["src"] .= ".png";
if (isset ($html_options["alt"])) {
$image_options["alt"] = $html_options["alt"];
unset ($html_options["alt"]);
if (isset ($html_options["size"])) {
$image_options["width"] = current(explode("x", $html_options["size"]));
$image_options["height"] = end(explode("x", $html_options["size"]));
unset ($html_options["size"]);
if(isset ($html_options["width"])) {
$image_options["width"] = $html_options["width"];
unset ($html_options["width"]);
} elseif(isset ($html_options["height"])) {
$image_options["height"] = $html_options["height"];
unset ($html_options["height"]);
if (isset ($html_options["border"])) {
$image_options["border"] = $html_options["border"];
unset ($html_options["border"]);
$image_options["border"] = 0;
if (isset ($html_options["align"])) {
$image_options["align"] = $html_options["align"];
unset ($html_options["align"]);
return $this->link_to($this->tag("img", $image_options), $options, $html_options);
* Generate URL based on current URL and optional arguments
* Output a URL with controller and optional action and id.
* The output URL has the same method, host and
* <samp>TRAX_URL_PREFIX</samp> as
* the current URL. Controller is either the current controller
* or a controller specified in $options. Action and ID are
* optionally specified in $options, or omitted. The
* <samp>':id'</samp> option will be ignored if the <samp>':action'</samp>
* <li><b>string:</b><br />
* The string value is returned immediately with no
* <li><samp>':controller'=></samp><i>controller value</i></li>
* <li><samp>':action'=></samp><i>action value</i></li>
* <li><samp>':id'=></samp><i>id value</i></li>
function url_for($options = array()) {
// Argument is a string, just return it
// Argument is a (possibly empty) array
// Start forming URL with this host
$url_base = $_SERVER['HTTP_HOST'];
if(substr($url_base, - 1) == "/") {
# remove the ending slash
$url_base = substr($url_base, 0, - 1);
// Method is same as was used by the current URL
if($_SERVER['SERVER_PORT'] == 443) {
$url_base = "https://". $url_base;
$url_base = "http://". $url_base;
// Insert value of Trax::$url_prefix
$prefix = Trax::$url_prefix;
// Get controller from $options or $controller_path
if(array_key_exists(":controller", $options)) {
if($controller = $options[":controller"]) {
if(substr($controller, 0, 1) == "/") {
# remove the beginning slash
$controller = substr($controller, 1);
// If controller found, get action from $options
if($action = $options[":action"]) {
// If controller and action found, get id from $actions
if($id = $options[":id"]->id) {
if($id = $options[":id"]) {
foreach($options as $key => $value) {
$extra_params[$key] = $value;
return $url_base . implode("/", $url)
* Make a new UrlHelper object and call its link_to() method
* @uses UrlHelper::link_to()
function link_to($name, $options = array(), $html_options = array()) {
return $url_helper->link_to($name, $options, $html_options);
* Make a new UrlHelper object and call its link_image_to() method
* @uses UrlHelper::link_image_to()
function link_image_to($src, $options = array(), $html_options = array()) {
return $url_helper->link_image_to($src, $options, $html_options);
* Make a new UrlHelper object and call its button_to() method
* @uses UrlHelper::button_to()
function button_to($name, $options = array(), $html_options = null) {
return $url_helper->button_to($name, $options, $html_options);
* Make a new UrlHelper object and call its url_for() method
* @uses UrlHelper::url_for()
function url_for($options = array()) {
return $url_helper->url_for($options);
// -- set Emacs parameters --
// c-hanging-comment-ender-p: nil
|