PHPonTrax
[ class tree: PHPonTrax ] [ index: PHPonTrax ] [ all elements ]

Source for file asset_tag_helper.php

Documentation is available at asset_tag_helper.php

  1. <?php
  2. /**
  3.  *  File containing the AssetTagHelper class and support functions
  4.  *
  5.  *  (PHP 5)
  6.  *
  7.  *  @package PHPonTrax
  8.  *  @version $Id: asset_tag_helper.php 228 2006-07-16 15:53:11Z john $
  9.  *  @copyright (c) 2005 John Peterson
  10.  *
  11.  *   Permission is hereby granted, free of charge, to any person obtaining
  12.  *   a copy of this software and associated documentation files (the
  13.  *   "Software"), to deal in the Software without restriction, including
  14.  *   without limitation the rights to use, copy, modify, merge, publish,
  15.  *   distribute, sublicense, and/or sell copies of the Software, and to
  16.  *   permit persons to whom the Software is furnished to do so, subject to
  17.  *   the following conditions:
  18.  *
  19.  *   The above copyright notice and this permission notice shall be
  20.  *   included in all copies or substantial portions of the Software.
  21.  *
  22.  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23.  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24.  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25.  *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26.  *   LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27.  *   OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28.  *   WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29.  */
  30.  
  31. /**
  32.  *  Utility to help build HTML/XML link tags for public assets
  33.  */
  34. class AssetTagHelper extends Helpers {
  35.  
  36.     /**
  37.      *  @var string[] 
  38.      */
  39.     var $javascript_default_sources = null;
  40.  
  41.     /**
  42.      *  @todo Document this method
  43.      *
  44.      *  @uses javascript_default_sources
  45.      */
  46.     function __construct({
  47.         parent::__construct();
  48.         $this->javascript_default_sources =
  49.             array_key_exists('JAVASCRIPT_DEFAULT_SOURCES',$GLOBALS)
  50.             ? $GLOBALS['JAVASCRIPT_DEFAULT_SOURCES']
  51.             : array('prototype''effects''controls''dragdrop');    
  52.     }
  53.     
  54.     /**
  55.      *  Compute public path to an asset
  56.      *
  57.      *  Build the public path, suitable for inclusion in a URL, to an
  58.      *  asset.  Arguments are the filename, directory and extension of
  59.      *  the asset.
  60.      *  @param string  Filename of asset
  61.      *  @param string  Default directory name, if none in $source
  62.      *  @param string  Default file extension, if none in $source
  63.      *  @return string Public path to the asset
  64.      *  @uses controller_object
  65.      *  @uses ActionController::asset_host
  66.      */
  67.     private function compute_public_path($source$dir$ext{
  68.  
  69.         //  Test whether source is a URL, ie. starts something://
  70.         if(!preg_match('/^[-a-z]+:\/\//'$source)) {
  71.  
  72.             //  Source is not a URL.
  73.             //  If path doesn't start with '/', prefix /$dir/
  74.             if($source{0!= '/'{
  75.                 $source "/{$dir}/{$source}";
  76.             }
  77.  
  78.             //  If no '.' in source file name, add '.ext'
  79.             if(!strstr($source'.')) {
  80.                 $source "{$source}.{$ext}";
  81.             }
  82.  
  83.             //  If Trax::$url_prefix non-null, prefix it to path
  84.             if(!is_null(Trax::$url_prefix)) {
  85.                 $prefix Trax::$url_prefix;
  86.                 if($prefix{0!= "/"{
  87.                     $prefix "/$prefix";
  88.                 }
  89.                 $source $prefix ((substr($prefix-1== "/")
  90.                                      ? substr($source1$source);
  91.             }            
  92.         }
  93.  
  94.         //  If controller defined and has asset_host value
  95.         //  prefix that to path
  96.         //  FIXME: won't this cause a problem if $source was http://...?
  97.         if isset($this->controller_object)
  98.              && isset($this->controller_object->asset_host) ) {
  99.             $source $this->controller_object->asset_host $source;
  100.         }
  101.         return $source;
  102.     }
  103.     
  104.     /**
  105.      *  Compute public path to a javascript asset
  106.      *
  107.      *  Build the public path, suitable for inclusion in a URL, to a
  108.      *  javascript asset.  Argument is the filename of the asset.
  109.      *  Default directory to 'javascripts', extension to '.js'
  110.      *  @param string  Filename of asset, in one of the formats
  111.      *                  accepted as the $filename argument of
  112.      *                  {@link compute_public_path()}
  113.      *  @return string Public path to the javascript asset
  114.      *  @uses compute_public_path()
  115.      */
  116.     function javascript_path($source{
  117.         return $this->compute_public_path($source'javascripts''js');
  118.     }
  119.     
  120.     /**
  121.      *  Return script include tag for one or more javascript assets
  122.      *
  123.      *  javascript_include_tag("xmlhr"); =>
  124.      *   <script type="text/javascript" src="/javascripts/xmlhr.js"></script>
  125.      *
  126.      *  javascript_include_tag("common.javascript", "/elsewhere/cools"); =>
  127.      *   <script type="text/javascript" src="/javascripts/common.javascript"></script>
  128.      *   <script type="text/javascript" src="/elsewhere/cools.js"></script>
  129.      *
  130.      *  javascript_include_tag("defaults"); =>
  131.      *   <script type="text/javascript" src="/javascripts/prototype.js"></script>
  132.      *   <script type="text/javascript" src="/javascripts/effects.js"></script>
  133.      *   <script type="text/javascript" src="/javascripts/controls.js"></script>
  134.      *   <script type="text/javascript" src="/javascripts/dragdrop.js"></script>
  135.      *  @param mixed  The arguments are zero or more strings, followed
  136.      *                 by an optional array containing options
  137.      *  @return string 
  138.      *  @uses content_tag()
  139.      *  @uses javascript_default_sources
  140.      *  @uses javascript_path()
  141.      */
  142.     function javascript_include_tag({
  143.         if(func_num_args(0{
  144.             $sources func_get_args();     
  145.             $options (is_array(end($sources))
  146.                         ? array_pop($sourcesarray());          
  147.             if(in_array('defaults'$sources)) {
  148.                 if(is_array($this->javascript_default_sources)) {
  149.                     $sources array_merge($this->javascript_default_sources,
  150.                                            $sources);    
  151.                 }                  
  152.                 if(file_exists(Trax::$public_path"/javascripts/application.js")) {
  153.                     $sources['application';
  154.                 }
  155.                 # remove defaults from array
  156.                 unset($sources[array_search('defaults'$sources)]);  
  157.             }
  158.             $contents array();
  159.             foreach($sources as $source{
  160.                 $source $this->javascript_path($source);
  161.                 $contents[$this->content_tag("script""",
  162.                      array_merge(array("type" => "text/javascript",
  163.                                        "src" => $source)$options));
  164.             }
  165.             return implode(""$contents);
  166.         }
  167.     }
  168.     
  169.     /**
  170.      *  Compute public path to a stylesheet asset
  171.      *
  172.      *  Build the public path, suitable for inclusion in a URL, to a
  173.      *  stylesheet asset.  Argument is the filename of the asset.
  174.      *  Default directory to 'stylesheets', extension to '.css'
  175.      *  @param string  Filename of asset, in one of the formats
  176.      *                  accepted as the $filename argument of
  177.      *                  {@link compute_public_path()}
  178.      *  @return string Public path to the stylesheet asset
  179.      *  @uses compute_public_path()
  180.      */
  181.     function stylesheet_path($source{
  182.         return $this->compute_public_path($source'stylesheets''css');
  183.     }
  184.     
  185.     /**
  186.      *  Build link tags to one or more stylesheet assets
  187.      *
  188.      *  @param mixed  One or more assets, optionally followed by an
  189.      *                 array describing options to apply to the tags
  190.      *                 generated for these assets.<br>  Each asset is a
  191.      *                 string in one of the formats accepted as value
  192.      *                 of the $source argument of
  193.      *                 {@link stylesheet_path()}.<br>  The optional last
  194.      *                 argument is an array whose keys are names of
  195.      *                 attributes of the link tag and whose corresponding
  196.      *                 values are the values assigned to each
  197.      *                 attribute.  If omitted, options default to:
  198.      *                 <ul>
  199.      *                   <li>"rel" => "Stylesheet"</li>
  200.      *                   <li>"type" => "text/css"</li>
  201.      *                   <li>"media" => "screen"</li>
  202.      *                   <li>"href" => <i>path-to-source</i></li>
  203.      *                 </ul>
  204.      *  @return string  A link tag for each asset in the argument list
  205.      *  @uses stylesheet_path()
  206.      *  @uses tag()
  207.      */
  208.     function stylesheet_link_tag({
  209.         if(func_num_args(0{
  210.             $sources func_get_args();     
  211.             $options (is_array(end($sources))
  212.                         ? array_pop($sourcesarray());
  213.             $contents array();
  214.             foreach($sources as $source{
  215.                 $source $this->stylesheet_path($source);
  216.                 $contents[$this->tag("link",
  217.                    array_merge(array("rel" => "Stylesheet",
  218.                                      "type" => "text/css",
  219.                                      "media" => "screen",
  220.                                      "href" => $source)$options));
  221.             }
  222.             return implode(""$contents);
  223.         }
  224.     }
  225.     
  226.     /**
  227.      *  Compute public path to a image asset
  228.      *
  229.      *  Build the public path, suitable for inclusion in a URL, to a
  230.      *  image asset.  Argument is the filename of the asset.
  231.      *  Default directory to 'images', extension to '.png'
  232.      *  @param string  Filename of asset, in one of the formats
  233.      *                  accepted as the $filename argument of
  234.      *                  {@link compute_public_path()}
  235.      *  @return string Public path to the image asset
  236.      *  @uses compute_public_path()
  237.      */
  238.     function image_path($source{
  239.         return $this->compute_public_path($source'images''png');
  240.     }
  241.     
  242.     /**
  243.      *  Build image tags to an image asset
  244.      *
  245.      *  @param mixed  An image asset optionally followed by an
  246.      *                 array describing options to apply to the tag
  247.      *                 generated for this asset.<br>The asset is a
  248.      *                 string in one of the formats accepted as value
  249.      *                 of the $source argument of
  250.      *                 {@link image_path()}.<br>  The optional second
  251.      *                 argument is an array whose keys are names of
  252.      *                 attributes of the image tag and whose corresponding
  253.      *                 values are the values assigned to each
  254.      *                 attribute.  The image size can be specified in
  255.      *                 two ways: by specifying option values "width" =>
  256.      *                 <i>width</i> and "height" => <i>height</i>, or
  257.      *                 by specifying option "size" => "<i>width</i>
  258.      *                 x <i>height</i>".  If omitted, options default to:
  259.      *                 <ul>
  260.      *                   <li>"alt" => <i>humanized filename</i></li>
  261.      *                   <li>"width" and "height" value computed from
  262.      *                        value of "size"</li>
  263.      *                 </ul>
  264.      *  @return string  A image tag for each asset in the argument list
  265.      *  @uses image_path()
  266.      *  @uses tag()
  267.      */
  268.     function image_tag($source$options array()) {
  269.         $options['src'$this->image_path($source);
  270.         $options['alt'array_key_exists('alt',$options)
  271.             ? $options['alt']
  272.             : Inflector::capitalize(reset($file_array =
  273.                              explode('.'basename($options['src']))));
  274.         if(isset($options['size'])) {
  275.             $size explode('x'$options["size"]);         
  276.             $options['width'reset($size);
  277.             $options['height'end($size);
  278.             unset($options['size']);
  279.         }
  280.         return $this->tag("img"$options);
  281.     }
  282.     
  283.     /**
  284.      *  Returns a link tag that browsers and news readers can use to
  285.      *  auto-detect a RSS or ATOM feed for this page. The $type can
  286.      *  either be <tt>:rss</tt> (default) or <tt>:atom</tt> and the
  287.      *  $options follow the url_for() style of declaring a link
  288.      *  target.
  289.      *
  290.      *  Examples:
  291.      *  auto_discovery_link_tag  =>
  292.      *   <link rel="alternate" type="application/rss+xml" title="RSS"
  293.      *  href="http://www.curenthost.com/controller/action" />
  294.      *  auto_discovery_link_tag(:atom) =>
  295.      *   <link rel="alternate" type="application/atom+xml"
  296.      *  title="ATOM"
  297.      *  href="http://www.curenthost.com/controller/action" />
  298.      *  auto_discovery_link_tag(:rss, {:action => "feed"}) =>
  299.      *   <link rel="alternate" type="application/rss+xml" title="RSS"
  300.      *  href="http://www.curenthost.com/controller/feed" />
  301.      *  auto_discovery_link_tag(:rss, {:action => "feed"}, {:title =>
  302.      *  "My RSS"})  =>
  303.      *   <link rel="alternate" type="application/rss+xml" title="My
  304.      *  RSS" href="http://www.curenthost.com/controller/feed" />
  305.      *  @uses tag()
  306.      *  @uses url_for()
  307.      */
  308.     function auto_discovery_link_tag($type 'rss'$options array(),
  309.                                      $tag_options array()) {
  310.         return $this->tag(
  311.           "link"array(
  312.                         "rel" => (array_key_exists('rel',$tag_options)
  313.                                   ? $tag_options['rel'"alternate"),
  314.                         "type" => (array_key_exists('type',$tag_options)
  315.                                    ? $tag_options['type']
  316.                                    : "application/{$type}+xml"),
  317.                         "title" => (array_key_exists('title',$tag_options)
  318.                                     ? $tag_options['title']
  319.                                     : strtoupper($type)),
  320.                         "href" => url_for(array_merge($options,
  321.                                          array('only_path' => false))))
  322.           );
  323.     }    
  324. }
  325.  
  326. /**
  327.  *  Make a new AssetTagHelper object and call its auto_discovery_link_tag() method
  328.  *  @uses AssetTagHelper::auto_discovery_link_tag()
  329.  */
  330.     $asset_helper new AssetTagHelper();
  331.     $args func_get_args();
  332.     return call_user_func_array(array($asset_helper'auto_discovery_link_tag')$args);
  333. }
  334.  
  335. /**
  336.  *  Make a new AssetTagHelper object and call its image_tag() method
  337.  *  @uses AssetTagHelper::image_tag()
  338.  */
  339. function image_tag({
  340.     $asset_helper new AssetTagHelper();
  341.     $args func_get_args();
  342.     return call_user_func_array(array($asset_helper'image_tag')$args);
  343. }
  344.  
  345. /**
  346.  *  Make a new AssetTagHelper object and call its stylesheet_link_tag() method
  347.  *  @uses AssetTagHelper::stylesheet_link_tag()
  348.  */
  349. function stylesheet_link_tag({
  350.     $asset_helper new AssetTagHelper();
  351.     $args func_get_args();
  352.     return call_user_func_array(array($asset_helper'stylesheet_link_tag')$args);
  353. }
  354.  
  355. /**
  356.  *  Make a new AssetTagHelper object and call its javascript_include_tag() method
  357.  *  @uses AssetTagHelper::javascript_include_tag()
  358.  */
  359. function javascript_include_tag({
  360.     $asset_helper new AssetTagHelper();
  361.     $args func_get_args();
  362.     return call_user_func_array(array($asset_helper'javascript_include_tag')$args);
  363. }
  364.  
  365. // -- set Emacs parameters --
  366. // Local variables:
  367. // tab-width: 4
  368. // c-basic-offset: 4
  369. // c-hanging-comment-ender-p: nil
  370. // indent-tabs-mode: nil
  371. // End:
  372. ?>

Documentation generated on Mon, 21 May 2007 22:28:09 -0600 by phpDocumentor 1.3.2