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$
  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', 'dragdrop', 'controls');
  52. }
  53. /**
  54. * Compute public path to an asset
  55. *
  56. * Build the public path, suitable for inclusion in a URL, to an
  57. * asset. Arguments are the filename, directory and extension of
  58. * the asset.
  59. * @param string Filename of asset
  60. * @param string Default directory name, if none in $source
  61. * @param string Default file extension, if none in $source
  62. * @return string Public path to the asset
  63. * @uses controller_object
  64. * @uses ActionController::asset_host
  65. */
  66. private function compute_public_path($source, $dir, $ext) {
  67. // Test whether source is a URL, ie. starts something://
  68. if(!preg_match('/^[-a-z]+:\/\//', $source)) {
  69.  
  70. // Source is not a URL.
  71. // If path doesn't start with '/', prefix /$dir/
  72. if($source{0} != '/') {
  73. $source = "/{$dir}/{$source}";
  74. }
  75.  
  76. // If no '.' in source file name, add '.ext'
  77. if(!strstr($source, '.')) {
  78. $source = "{$source}.{$ext}";
  79. }
  80.  
  81. // If TRAX_URL_PREFIX non-null, prefix it to path
  82. if(!is_null(TRAX_URL_PREFIX)) {
  83. $prefix = TRAX_URL_PREFIX;
  84. if($prefix{0} != "/") {
  85. $prefix = "/$prefix";
  86. }
  87. $source = $prefix . ((substr($prefix, -1) == "/")
  88. ? substr($source, 1) : $source);
  89. }
  90. }
  91.  
  92. // If controller defined and has asset_host value
  93. // prefix that to path
  94. // FIXME: won't this cause a problem if $source was http://...?
  95. if ( isset($this->controller_object)
  96. && isset($this->controller_object->asset_host) ) {
  97. $source = $this->controller_object->asset_host . $source;
  98. }
  99. return $source;
  100. }
  101. /**
  102. * Compute public path to a javascript asset
  103. *
  104. * Build the public path, suitable for inclusion in a URL, to a
  105. * javascript asset. Argument is the filename of the asset.
  106. * Default directory to 'javascripts', extension to '.js'
  107. * @param string Filename of asset, in one of the formats
  108. * accepted as the $filename argument of
  109. * {@link compute_public_path()}
  110. * @return string Public path to the javascript asset
  111. * @uses compute_public_path()
  112. */
  113. function javascript_path($source) {
  114. return $this->compute_public_path($source, 'javascripts', 'js');
  115. }
  116. /**
  117. * Return script include tag for one or more javascript assets
  118. *
  119. * javascript_include_tag("xmlhr"); =>
  120. * <script type="text/javascript" src="/javascripts/xmlhr.js"></script>
  121. *
  122. * javascript_include_tag("common.javascript", "/elsewhere/cools"); =>
  123. * <script type="text/javascript" src="/javascripts/common.javascript"></script>
  124. * <script type="text/javascript" src="/elsewhere/cools.js"></script>
  125. *
  126. * javascript_include_tag("defaults"); =>
  127. * <script type="text/javascript" src="/javascripts/prototype.js"></script>
  128. * <script type="text/javascript" src="/javascripts/effects.js"></script>
  129. * <script type="text/javascript" src="/javascripts/controls.js"></script>
  130. * <script type="text/javascript" src="/javascripts/dragdrop.js"></script>
  131. * @param mixed The arguments are zero or more strings, followed
  132. * by an optional array containing options
  133. * @return string
  134. * @uses content_tag()
  135. * @uses javascript_default_sources
  136. * @uses javascript_path()
  137. */
  138. function javascript_include_tag() {
  139. if(func_num_args() > 0) {
  140. $sources = func_get_args();
  141. $options = (is_array(end($sources))
  142. ? array_pop($sources) : array());
  143. if(in_array('defaults', $sources)) {
  144. if(is_array($this->javascript_default_sources)) {
  145. $sources = array_merge($this->javascript_default_sources,
  146. $sources);
  147. }
  148. if(file_exists(TRAX_PUBLIC. "/javascripts/application.js")) {
  149. $sources[] = 'application';
  150. }
  151. # remove defaults from array
  152. unset($sources[array_search('defaults', $sources)]);
  153. }
  154. $contents = array();
  155. foreach($sources as $source) {
  156. $source = $this->javascript_path($source);
  157. $contents[] = $this->content_tag("script", "",
  158. array_merge(array("type" => "text/javascript",
  159. "src" => $source), $options));
  160. }
  161. return implode("", $contents);
  162. }
  163. }
  164. /**
  165. * Compute public path to a stylesheet asset
  166. *
  167. * Build the public path, suitable for inclusion in a URL, to a
  168. * stylesheet asset. Argument is the filename of the asset.
  169. * Default directory to 'stylesheets', extension to '.css'
  170. * @param string Filename of asset, in one of the formats
  171. * accepted as the $filename argument of
  172. * {@link compute_public_path()}
  173. * @return string Public path to the stylesheet asset
  174. * @uses compute_public_path()
  175. */
  176. function stylesheet_path($source) {
  177. return $this->compute_public_path($source, 'stylesheets', 'css');
  178. }
  179. /**
  180. * Build link tags to one or more stylesheet assets
  181. *
  182. * @param mixed One or more assets, optionally followed by an
  183. * array describing options to apply to the tags
  184. * generated for these assets.<br> Each asset is a
  185. * string in one of the formats accepted as value
  186. * of the $source argument of
  187. * {@link stylesheet_path()}.<br> The optional last
  188. * argument is an array whose keys are names of
  189. * attributes of the link tag and whose corresponding
  190. * values are the values assigned to each
  191. * attribute. If omitted, options default to:
  192. * <ul>
  193. * <li>"rel" => "Stylesheet"</li>
  194. * <li>"type" => "text/css"</li>
  195. * <li>"media" => "screen"</li>
  196. * <li>"href" => <i>path-to-source</i></li>
  197. * </ul>
  198. * @return string A link tag for each asset in the argument list
  199. * @uses stylesheet_path()
  200. * @uses tag()
  201. */
  202. function stylesheet_link_tag() {
  203. if(func_num_args() > 0) {
  204. $sources = func_get_args();
  205. $options = (is_array(end($sources))
  206. ? array_pop($sources) : array());
  207. $contents = array();
  208. foreach($sources as $source) {
  209. $source = $this->stylesheet_path($source);
  210. $contents[] = $this->tag("link",
  211. array_merge(array("rel" => "Stylesheet",
  212. "type" => "text/css",
  213. "media" => "screen",
  214. "href" => $source), $options));
  215. }
  216. return implode("", $contents);
  217. }
  218. }
  219. /**
  220. * Compute public path to a image asset
  221. *
  222. * Build the public path, suitable for inclusion in a URL, to a
  223. * image asset. Argument is the filename of the asset.
  224. * Default directory to 'images', extension to '.png'
  225. * @param string Filename of asset, in one of the formats
  226. * accepted as the $filename argument of
  227. * {@link compute_public_path()}
  228. * @return string Public path to the image asset
  229. * @uses compute_public_path()
  230. */
  231. function image_path($source) {
  232. return $this->compute_public_path($source, 'images', 'png');
  233. }
  234. /**
  235. * Build image tags to an image asset
  236. *
  237. * @param mixed An image asset optionally followed by an
  238. * array describing options to apply to the tag
  239. * generated for this asset.<br>The asset is a
  240. * string in one of the formats accepted as value
  241. * of the $source argument of
  242. * {@link image_path()}.<br> The optional second
  243. * argument is an array whose keys are names of
  244. * attributes of the image tag and whose corresponding
  245. * values are the values assigned to each
  246. * attribute. The image size can be specified in
  247. * two ways: by specifying option values "width" =>
  248. * <i>width</i> and "height" => <i>height</i>, or
  249. * by specifying option "size" => "<i>width</i>
  250. * x <i>height</i>". If omitted, options default to:
  251. * <ul>
  252. * <li>"alt" => <i>humanized filename</i></li>
  253. * <li>"width" and "height" value computed from
  254. * value of "size"</li>
  255. * </ul>
  256. * @return string A image tag for each asset in the argument list
  257. * @uses image_path()
  258. * @uses tag()
  259. */
  260. function image_tag($source, $options = array()) {
  261. $options['src'] = $this->image_path($source);
  262. $options['alt'] = array_key_exists('alt',$options)
  263. ? $options['alt']
  264. : Inflector::capitalize(reset($file_array =
  265. explode('.', basename($options['src']))));
  266. if(isset($options['size'])) {
  267. $size = explode('x', $options["size"]);
  268. $options['width'] = reset($size);
  269. $options['height'] = end($size);
  270. unset($options['size']);
  271. }
  272. return $this->tag("img", $options);
  273. }
  274. /**
  275. * Returns a link tag that browsers and news readers can use to
  276. * auto-detect a RSS or ATOM feed for this page. The $type can
  277. * either be <tt>:rss</tt> (default) or <tt>:atom</tt> and the
  278. * $options follow the url_for() style of declaring a link
  279. * target.
  280. *
  281. * Examples:
  282. * auto_discovery_link_tag =>
  283. * <link rel="alternate" type="application/rss+xml" title="RSS"
  284. * href="http://www.curenthost.com/controller/action" />
  285. * auto_discovery_link_tag(:atom) =>
  286. * <link rel="alternate" type="application/atom+xml"
  287. * title="ATOM"
  288. * href="http://www.curenthost.com/controller/action" />
  289. * auto_discovery_link_tag(:rss, {:action => "feed"}) =>
  290. * <link rel="alternate" type="application/rss+xml" title="RSS"
  291. * href="http://www.curenthost.com/controller/feed" />
  292. * auto_discovery_link_tag(:rss, {:action => "feed"}, {:title =>
  293. * "My RSS"}) =>
  294. * <link rel="alternate" type="application/rss+xml" title="My
  295. * RSS" href="http://www.curenthost.com/controller/feed" />
  296. * @uses tag()
  297. * @uses url_for()
  298. */
  299. function auto_discovery_link_tag($type = 'rss', $options = array(),
  300. $tag_options = array()) {
  301. return $this->tag(
  302. "link", array(
  303. "rel" => (array_key_exists('rel',$tag_options)
  304. ? $tag_options['rel'] : "alternate"),
  305. "type" => (array_key_exists('type',$tag_options)
  306. ? $tag_options['type']
  307. : "application/{$type}+xml"),
  308. "title" => (array_key_exists('title',$tag_options)
  309. ? $tag_options['title']
  310. : strtoupper($type)),
  311. "href" => url_for(array_merge($options,
  312. array('only_path' => false))))
  313. );
  314. }
  315. }
  316.  
  317. /**
  318. * Make a new AssetTagHelper object and call its auto_discovery_link_tag() method
  319. * @uses AssetTagHelper::auto_discovery_link_tag()
  320. */
  321. function auto_discovery_link_tag() {
  322. $asset_helper = new AssetTagHelper();
  323. $args = func_get_args();
  324. return call_user_func_array(array($asset_helper,
  325. 'auto_discovery_link_tag'), $args);
  326. }
  327.  
  328. /**
  329. * Make a new AssetTagHelper object and call its image_tag() method
  330. * @uses AssetTagHelper::image_tag()
  331. */
  332. function image_tag() {
  333. $asset_helper = new AssetTagHelper();
  334. $args = func_get_args();
  335. return call_user_func_array(array($asset_helper, 'image_tag'), $args);
  336. }
  337.  
  338. /**
  339. * Make a new AssetTagHelper object and call its stylesheet_link_tag() method
  340. * @uses AssetTagHelper::stylesheet_link_tag()
  341. */
  342. function stylesheet_link_tag() {
  343. $asset_helper = new AssetTagHelper();
  344. $args = func_get_args();
  345. return call_user_func_array(array($asset_helper,
  346. 'stylesheet_link_tag'), $args);
  347. }
  348.  
  349. /**
  350. * Make a new AssetTagHelper object and call its javascript_include_tag() method
  351. * @uses AssetTagHelper::javascript_include_tag()
  352. */
  353. function javascript_include_tag() {
  354. $asset_helper = new AssetTagHelper();
  355. $args = func_get_args();
  356. return call_user_func_array(array($asset_helper,
  357. 'javascript_include_tag'), $args);
  358. }
  359.  
  360. // -- set Emacs parameters --
  361. // Local variables:
  362. // tab-width: 4
  363. // c-basic-offset: 4
  364. // c-hanging-comment-ender-p: nil
  365. // indent-tabs-mode: nil
  366. // End:
  367. ?>

Documentation generated on Thu, 04 May 2006 19:47:18 -0600 by phpDocumentor 1.3.0RC4