/application/libraries/Asset.php

https://github.com/wuts/starflower · PHP · 338 lines · 109 code · 67 blank · 162 comment · 10 complexity · a7e18d2d80e1630055d7a7d40aa43fe4 MD5 · raw file

  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Code Igniter
  4. *
  5. * An open source application development framework for PHP 4.3.2 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author Rick Ellis
  9. * @copyright Copyright (c) 2006, pMachine, Inc.
  10. * @license http://www.codeignitor.com/user_guide/license.html
  11. * @link http://www.codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * CodeIgniter Asset Library
  18. *
  19. * @package CodeIgniter
  20. * @subpackage Libraries
  21. * @category Libraries
  22. * @author Philip Sturgeon < email@philsturgeon.co.uk >
  23. */
  24. // ------------------------------------------------------------------------
  25. class Asset
  26. {
  27. var $CI;
  28. var $theme = NULL;
  29. function Asset()
  30. {
  31. $this->CI =& get_instance();
  32. $this->CI->load->config('asset');
  33. }
  34. // ------------------------------------------------------------------------
  35. /**
  36. * CSS
  37. *
  38. * Helps generate CSS asset HTML.
  39. *
  40. * @access public
  41. * @param string the name of the file or asset
  42. * @param string optional, module name
  43. * @param string optional, extra attributes
  44. * @return string HTML code for JavaScript asset
  45. */
  46. function css($asset_name, $module_name = NULL, $attributes = array())
  47. {
  48. $attribute_str = $this->_parse_asset_html($attributes);
  49. return '<link href="'.$this->css_path($asset_name, $module_name).'" rel="stylesheet" type="text/css"'.$attribute_str.' />'."\n";
  50. }
  51. // ------------------------------------------------------------------------
  52. /**
  53. * CSS Path
  54. *
  55. * Generate CSS asset path locations.
  56. *
  57. * @access public
  58. * @param string the name of the file or asset
  59. * @param string optional, module name
  60. * @return string full url to css asset
  61. */
  62. function css_path($asset_name, $module_name = NULL)
  63. {
  64. return $this->other_asset_path($asset_name, $module_name, config_item('asset_css_dir'));
  65. }
  66. // ------------------------------------------------------------------------
  67. /**
  68. * CSS URL
  69. *
  70. * Generate CSS asset URLs.
  71. *
  72. * @access public
  73. * @param string the name of the file or asset
  74. * @param string optional, module name
  75. * @return string full url to css asset
  76. */
  77. function css_url($asset_name, $module_name = NULL)
  78. {
  79. return $this->other_asset_url($asset_name, $module_name, config_item('asset_css_dir'));
  80. }
  81. // ------------------------------------------------------------------------
  82. /**
  83. * Image
  84. *
  85. * Helps generate image HTML.
  86. *
  87. * @access public
  88. * @param string the name of the image
  89. * @param string optional, module name
  90. * @param string optional, extra attributes
  91. * @return string HTML code for image asset
  92. */
  93. function image($asset_name, $module_name = '', $attributes = array())
  94. {
  95. // No alternative text given? Use the filename, better than nothing!
  96. if(empty($attributes['alt'])) list($attributes['alt']) = explode('.', $asset_name);
  97. $attribute_str = $this->_parse_asset_html($attributes);
  98. return '<img src="'.$this->image_path($asset_name, $module_name).'"'.$attribute_str.' />'."\n";
  99. }
  100. // ------------------------------------------------------------------------
  101. /**
  102. * Image Path
  103. *
  104. * Helps generate image paths.
  105. *
  106. * @access public
  107. * @param string the name of the file or asset
  108. * @param string optional, module name
  109. * @return string full url to image asset
  110. */
  111. function image_path($asset_name, $module_name = NULL)
  112. {
  113. return $this->other_asset_path($asset_name, $module_name, config_item('asset_img_dir'), 'path');
  114. }
  115. // ------------------------------------------------------------------------
  116. /**
  117. * Image URL
  118. *
  119. * Helps generate image URLs.
  120. *
  121. * @access public
  122. * @param string the name of the file or asset
  123. * @param string optional, module name
  124. * @return string full url to image asset
  125. */
  126. function image_url($asset_name, $module_name = NULL)
  127. {
  128. return $this->other_asset_url($asset_name, $module_name, config_item('asset_img_dir'));
  129. }
  130. // ------------------------------------------------------------------------
  131. /**
  132. * JS
  133. *
  134. * Helps generate JavaScript asset HTML.
  135. *
  136. * @access public
  137. * @param string the name of the file or asset
  138. * @param string optional, module name
  139. * @return string HTML code for JavaScript asset
  140. */
  141. function js($asset_name, $module_name = NULL)
  142. {
  143. return '<script type="text/javascript" src="'.$this->js_path($asset_name, $module_name).'"></script>'."\n";
  144. }
  145. // ------------------------------------------------------------------------
  146. /**
  147. * JS Path
  148. *
  149. * Helps generate JavaScript asset paths.
  150. *
  151. * @access public
  152. * @param string the name of the file or asset
  153. * @param string optional, module name
  154. * @return string web root path to JavaScript asset
  155. */
  156. function js_path($asset_name, $module_name = NULL)
  157. {
  158. return $this->other_asset_path($asset_name, $module_name, config_item('asset_js_dir'));
  159. }
  160. // ------------------------------------------------------------------------
  161. /**
  162. * JS URL
  163. *
  164. * Helps generate JavaScript asset locations.
  165. *
  166. * @access public
  167. * @param string the name of the file or asset
  168. * @param string optional, module name
  169. * @return string full url to JavaScript asset
  170. */
  171. function js_url($asset_name, $module_name = NULL)
  172. {
  173. return $this->other_asset_url($asset_name, $module_name, config_item('asset_js_dir'));
  174. }
  175. // ------------------------------------------------------------------------
  176. /**
  177. * General Asset HTML Helper
  178. *
  179. * The main asset location generator
  180. *
  181. * @access public
  182. * @param string the name of the file or asset
  183. * @param string optional, module name
  184. * @return string HTML code for JavaScript asset
  185. */
  186. function other_asset_path($asset_name, $module_name = NULL, $asset_type = NULL)
  187. {
  188. return $this->_other_asset_location($asset_name, $module_name, $asset_type, 'path');
  189. }
  190. function other_asset_url($asset_name, $module_name = NULL, $asset_type = NULL)
  191. {
  192. return $this->_other_asset_location($asset_name, $module_name, $asset_type, 'url');
  193. }
  194. function _other_asset_location($asset_name, $module_name = NULL, $asset_type = NULL, $location_type = 'url')
  195. {
  196. $base_location = $this->CI->config->item( $location_type == 'url' ? 'asset_url' : 'asset_dir' );
  197. // If they are using a direct path, take them to it
  198. if(strpos($asset_name, 'assets/') !== FALSE)
  199. {
  200. $asset_location = $base_location.$asset_name;
  201. }
  202. // If they have just given a filename, not an asset path, and its in a theme
  203. elseif($module_name == '_theme_' && $this->theme != NULL)
  204. {
  205. $asset_location = $base_location.'themes/'
  206. . $this->theme.'/'
  207. . $asset_type.'/'.$asset_name;
  208. }
  209. // Normal file (that might be in a module)
  210. else
  211. {
  212. $asset_location = $base_location;
  213. // Its in a module, ignore the current
  214. if($module_name)
  215. {
  216. foreach( array_keys(Modules::$locations) as $path)
  217. {
  218. if(is_dir($path . $module_name))
  219. {
  220. $asset_location .= str_replace(APPPATH, '', $path).$module_name.'/';
  221. break;
  222. }
  223. }
  224. }
  225. else
  226. {
  227. $asset_location .= 'assets/';
  228. }
  229. $asset_location .= $asset_type.'/'.$asset_name;
  230. }
  231. return $asset_location;
  232. }
  233. // ------------------------------------------------------------------------
  234. /**
  235. * Parse HTML Attributes
  236. *
  237. * Turns an array of attributes into a string
  238. *
  239. * @access public
  240. * @param array attributes to be parsed
  241. * @return string string of html attributes
  242. */
  243. function _parse_asset_html($attributes = NULL)
  244. {
  245. $attribute_str = '';
  246. if(is_array($attributes))
  247. {
  248. foreach($attributes as $key => $value)
  249. {
  250. $attribute_str .= ' '.$key.'="'.$value.'"';
  251. }
  252. }
  253. return $attribute_str;
  254. }
  255. // ------------------------------------------------------------------------
  256. /**
  257. * Set theme
  258. *
  259. * If you use some sort of theme system, this method stores the theme name
  260. *
  261. * @access public
  262. * @param string theme name
  263. */
  264. function set_theme($theme)
  265. {
  266. $this->theme = $theme;
  267. }
  268. }
  269. // END Asset Class
  270. /* End of file Asset.php */
  271. /* Location: ./application/libraries/Asset.php */