| [ Index ] |
PHP Cross Reference of Akelos Framework |
[Summary view] [Print] [Text view]
1 <?php 2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ 3 4 // +----------------------------------------------------------------------+ 5 // | Akelos Framework - http://www.akelos.org | 6 // +----------------------------------------------------------------------+ 7 // | Copyright (c) 2002-2006, Akelos Media, S.L. & Bermi Ferrer Martinez | 8 // | Released under the GNU Lesser General Public License, see LICENSE.txt| 9 // +----------------------------------------------------------------------+ 10 11 /** 12 * @package ActionView 13 * @subpackage Helpers 14 * @author Bermi Ferrer <bermi a.t akelos c.om> 15 * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org 16 * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html> 17 */ 18 19 20 require_once (AK_LIB_DIR.DS.'AkActionView'.DS.'helpers'.DS.'tag_helper.php'); 21 require_once (AK_LIB_DIR.DS.'AkActionView'.DS.'helpers'.DS.'url_helper.php'); 22 require_once (AK_LIB_DIR.DS.'AkActionView'.DS.'AkActionViewHelper.php'); 23 24 if(!defined('JAVASCRIPT_DEFAULT_SOURCES')){ 25 define('JAVASCRIPT_DEFAULT_SOURCES','prototype,event_selectors,scriptaculous'); 26 } 27 28 /** 29 * Provides methods for linking a HTML page together with other assets, such as javascripts, stylesheets, and feeds. 30 */ 31 class AssetTagHelper extends AkActionViewHelper 32 { 33 34 function setController(&$controller) 35 { 36 $this->_controller =& $controller; 37 } 38 39 /** 40 * Returns a link tag that browsers and news readers can use to auto-detect a RSS or ATOM feed for this page. The +type+ can 41 * either be <tt>'rss'</tt> (default) or <tt>'atom'</tt> and the +options+ follow the $controller->urlFor style of declaring a link target. 42 * 43 * Examples: 44 * $asset_tag_helper->auto_discovery_link_tag(); # => 45 * <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.curenthost.com/controller/action" /> 46 * $asset_tag_helper->auto_discovery_link_tag('atom'); # => 47 * <link rel="alternate" type="application/atom+xml" title="ATOM" href="http://www.curenthost.com/controller/action" /> 48 * $asset_tag_helper->auto_discovery_link_tag('rss', array('action' => 'feed')); # => 49 * <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.curenthost.com/controller/feed" /> 50 * $asset_tag_helper->auto_discovery_link_tag('rss', array('action'=>'feed'), array('title'=>'My RSS')); # => 51 * <link rel="alternate" type="application/rss+xml" title="My RSS" href="http://www.curenthost.com/controller/feed" /> 52 */ 53 function auto_discovery_link_tag($type = 'rss', $url_options = array(), $tag_options = array()) 54 { 55 return TagHelper::tag( 56 'link', 57 array( 58 'rel' => empty($tag_options['rel']) ? 'alternate' : $tag_options['rel'], 59 'type' => empty($tag_options['type']) ? "application/$type+xml" : $tag_options['type'], 60 'title' => empty($tag_options['title']) ? strtoupper($type) : $tag_options['title'], 61 'href' => is_array($url_options) ? $this->_controller->urlFor(array_merge($url_options,array('only_path'=>false))) : $url_options 62 ) 63 ); 64 } 65 66 /** 67 * Returns path to a javascript asset. Example: 68 * 69 * $asset_tag_helper->javascript_path('xmlhr'); # => /javascripts/xmlhr.js 70 */ 71 function javascript_path($source) 72 { 73 return $this->_compute_public_path($source, 'javascripts', 'js'); 74 } 75 76 77 /** 78 * Returns a script include tag per source given as argument. Examples: 79 * 80 * $asset_tag_helper->javascript_include_tag ("xmlhr"); # => 81 * <script type="text/javascript" src="/javascripts/xmlhr.js"></script> 82 * 83 * $asset_tag_helper->javascript_include_tag('common.javascript', '/elsewhere/cools'); # => 84 * <script type="text/javascript" src="/javascripts/common.javascript"></script> 85 * <script type="text/javascript" src="/elsewhere/cools.js"></script> 86 * 87 * $asset_tag_helper->javascript_include_tag('defaults'); # => 88 * <script type="text/javascript" src="/javascripts/prototype.js"></script> 89 * <script type="text/javascript" src="/javascripts/effects.js"></script> 90 * ... 91 * <script type="text/javascript" src="/javascripts/application.js"></script> *see below 92 * 93 * If there's an <tt>application.js</tt> file in your <tt>public/javascripts</tt> directory, 94 * <tt>$asset_tag_helper->javascript_include_tag('defaults')</tt> will automatically include it. This file 95 * facilitates the inclusion of small snippets of JavaScript code, along the lines of 96 * <tt>controllers/application.php</tt> and <tt>helpers/application_helper.php</tt>. 97 */ 98 function javascript_include_tag() 99 { 100 $sources = func_get_args(); 101 $num_args = func_num_args(); 102 $options = !empty($sources[$num_args-1]) && is_array($sources[$num_args-1]) ? array_pop($sources) : array(); 103 if(empty($sources) || $sources[0] == 'defaults'){ 104 $sources = $this->_get_javascript_included_defaults(); 105 } 106 $javascript_include_tags = ''; 107 foreach ($sources as $source){ 108 $source = $this->javascript_path($source); 109 $javascript_include_tags .= TagHelper::content_tag('script', '', array_merge($options,array('type'=>'text/javascript','src'=>$source)))."\n"; 110 } 111 return $javascript_include_tags; 112 } 113 114 function _get_javascript_included_defaults() 115 { 116 static $defaults, $plugin_defaults = array(); 117 if(empty($defaults)){ 118 $defaults = array_unique(array_diff(array_filter(explode(',',JAVASCRIPT_DEFAULT_SOURCES. 119 ','.(file_exists(AK_PUBLIC_DIR.DS.'javascript'.DS.'application.js') ? 'application' : '' ) 120 ),'trim'),array(''))); 121 } 122 if(func_num_args()){ 123 $plugin_defaults = func_get_arg(0) === false ? array() : func_get_args(); 124 } 125 return array_merge($defaults, $plugin_defaults); 126 } 127 128 /** 129 * Register one or more additional JavaScript files to be included when 130 * 131 * javascript_include_tag :defaults 132 * 133 * is called. This method is intended to be called only from plugin initialization 134 * to register extra .js files the plugin installed in <tt>public/javascripts</tt>. 135 */ 136 function register_javascript_include_default($sources) 137 { 138 $this->_get_javascript_included_defaults($sources); 139 } 140 141 function reset_javascript_include_default() 142 { 143 $this->_get_javascript_included_defaults(false); 144 } 145 146 /** 147 * Returns path to a stylesheet asset. Example: 148 * 149 * $asset_tag_helper->stylesheet_path('style'); # => /stylesheets/style.css 150 */ 151 function stylesheet_path($source) 152 { 153 return $this->_compute_public_path($source, 'stylesheets', 'css'); 154 } 155 156 /** 157 * Returns a css link tag per source given as argument. Examples: 158 * 159 * $asset_tag_helper->stylesheet_link_tag('style'); # => 160 * <link href="/stylesheets/style.css" media="screen" rel="Stylesheet" type="text/css" /> 161 * 162 * $asset_tag_helper->stylesheet_link_tag('style', array('media'=>'all')); # => 163 * <link href="/stylesheets/style.css" media="all" rel="Stylesheet" type="text/css" /> 164 * 165 * $asset_tag_helper->stylesheet_link_tag('random.styles', '/css/stylish'); # => 166 * <link href="/stylesheets/random.styles" media="screen" rel="Stylesheet" type="text/css" /> 167 * <link href="/css/stylish.css" media="screen" rel="Stylesheet" type="text/css" /> 168 */ 169 function stylesheet_link_tag() 170 { 171 $sources = func_get_args(); 172 $num_args = func_num_args(); 173 174 $options = (!empty($sources[$num_args-1]) && is_array($sources[$num_args-1])) ? array_pop($sources) : array(); 175 176 $style_include_tags = ''; 177 foreach ($sources as $source){ 178 $source = $this->stylesheet_path($source); 179 $style_include_tags .= TagHelper::tag('link', array_merge(array('rel'=>'Stylesheet','type'=>'text/css','media'=>'screen','href'=>$source),$options))."\n"; 180 } 181 return $style_include_tags; 182 } 183 184 /** 185 * Returns path to an image asset. Example: 186 * 187 * The +src+ can be supplied as a... 188 * * full path, like "/my_images/image.gif" 189 * * file name, like "rss.gif", that gets expanded to "/images/rss.gif" 190 * * file name without extension, like "logo", that gets expanded to "/images/logo.png" 191 */ 192 function image_path($source) 193 { 194 return $this->_compute_public_path($source, 'images', 'png'); 195 } 196 197 /** 198 * Returns an image tag converting the +options+ into html options on the tag, but with these special cases: 199 * 200 * * <tt>alt</tt> - If no alt text is given, the file name part of the +src+ is used (capitalized and without the extension) 201 * * <tt>size</tt> - Supplied as "XxY", so "30x45" becomes width="30" and height="45" 202 * 203 * The +src+ can be supplied as a... 204 * * full path, like "/my_images/image.gif" 205 * * file name, like "rss.gif", that gets expanded to "/images/rss.gif" 206 * * file name without extension, like "logo", that gets expanded to "/images/logo.png" 207 */ 208 function image_tag($source, $options = array()) 209 { 210 if(!empty($options['size'])){ 211 list($options['width'], $options['height']) = split('x|X| ',trim(str_replace(' ','',$options['size']))); 212 unset($options['size']); 213 } 214 $options['src'] = $this->image_path($source); 215 $options['alt'] = !empty($options['alt']) ? $options['alt'] : AkInflector::titleize(substr(basename($options['src']),0,strpos(basename($options['src']),'.')),'first'); 216 217 return TagHelper::tag('img', $options); 218 } 219 220 function _compute_public_path($source, $dir = '', $ext = '') 221 { 222 $source = $source[0] != '/' && !strstr($source,':') ? "/$dir/$source" : $source; 223 $source = !strstr($source,'.') ? "$source.$ext" : $source; 224 $source = !preg_match('/^[-a-z]+:\/\//',$source) ? AK_ASSET_URL_PREFIX.$source : $source; 225 $source = strstr($source,':') ? $source : $this->_controller->asset_host.$source; 226 $source = substr($source,0,2) == '//' ? substr($source,1) : $source; 227 228 return $source; 229 } 230 231 function stylesheet_for_current_controller() 232 { 233 $stylesheet = AkInflector::underscore($this->_controller->getControllerName()).'.css'; 234 if(file_exists(AK_PUBLIC_DIR.DS.'stylesheets'.DS.$stylesheet)){ 235 return $this->stylesheet_link_tag($stylesheet); 236 } 237 return ''; 238 } 239 240 function javascript_for_current_controller() 241 { 242 $js_file = AkInflector::underscore($this->_controller->getControllerName()).'.js'; 243 if(file_exists(AK_PUBLIC_DIR.DS.'javascripts'.DS.$js_file)){ 244 return $this->javascript_include_tag($js_file); 245 } 246 return ''; 247 } 248 } 249 250 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Oct 27 12:43:49 2008 | Cross-referenced by PHPXref 0.6 |