/system/pyrocms/libraries/Asset.php

https://github.com/daK76/pyrocms · PHP · 329 lines · 119 code · 47 blank · 163 comment · 15 complexity · fe69eaade5e37af6507b1243204feea1 MD5 · raw file

  1. <?php defined('BASEPATH') OR 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. class Asset {
  25. private $theme = NULL;
  26. private $_ci;
  27. function __construct()
  28. {
  29. $this->_ci = &get_instance();
  30. $this->_ci->load->config('asset');
  31. }
  32. // ------------------------------------------------------------------------
  33. /**
  34. * CSS
  35. *
  36. * Helps generate CSS asset HTML.
  37. *
  38. * @access public
  39. * @param string the name of the file or asset
  40. * @param string optional, module name
  41. * @param string optional, extra attributes
  42. * @return string HTML code for JavaScript asset
  43. */
  44. public function css($asset_name, $module_name = NULL, $attributes = array())
  45. {
  46. $attribute_str = $this->_parse_asset_html($attributes);
  47. if ( ! preg_match('/rel="([^\"]+)"/', $attribute_str))
  48. {
  49. $attribute_str .= ' rel="stylesheet"';
  50. }
  51. return '<link href="' . $this->css_path($asset_name, $module_name) . '" type="text/css"' . $attribute_str . ' />' . "\n";
  52. }
  53. // ------------------------------------------------------------------------
  54. /**
  55. * CSS Path
  56. *
  57. * Generate CSS asset path locations.
  58. *
  59. * @access public
  60. * @param string the name of the file or asset
  61. * @param string optional, module name
  62. * @return string full url to css asset
  63. */
  64. public function css_path($asset_name, $module_name = NULL)
  65. {
  66. return $this->_asset_path($asset_name, $module_name, config_item('asset_css_dir'));
  67. }
  68. // ------------------------------------------------------------------------
  69. /**
  70. * CSS URL
  71. *
  72. * Generate CSS asset URLs.
  73. *
  74. * @access public
  75. * @param string the name of the file or asset
  76. * @param string optional, module name
  77. * @return string full url to css asset
  78. */
  79. public function css_url($asset_name, $module_name = NULL)
  80. {
  81. return $this->_asset_url($asset_name, $module_name, config_item('asset_css_dir'));
  82. }
  83. // ------------------------------------------------------------------------
  84. /**
  85. * Image
  86. *
  87. * Helps generate image HTML.
  88. *
  89. * @access public
  90. * @param string the name of the image
  91. * @param string optional, module name
  92. * @param string optional, extra attributes
  93. * @return string HTML code for image asset
  94. */
  95. public function image($asset_name, $module_name = '', $attributes = array())
  96. {
  97. // No alternative text given? Use the filename, better than nothing!
  98. if (empty($attributes['alt']))
  99. {
  100. list($attributes['alt']) = explode('.', $asset_name);
  101. }
  102. $attribute_str = $this->_parse_asset_html($attributes);
  103. return '<img src="' . $this->image_path($asset_name, $module_name) . '"' . $attribute_str . ' />' . "\n";
  104. }
  105. // ------------------------------------------------------------------------
  106. /**
  107. * Image Path
  108. *
  109. * Helps generate image paths.
  110. *
  111. * @access public
  112. * @param string the name of the file or asset
  113. * @param string optional, module name
  114. * @return string full url to image asset
  115. */
  116. public function image_path($asset_name, $module_name = NULL)
  117. {
  118. return $this->_asset_path($asset_name, $module_name, config_item('asset_img_dir'), 'path');
  119. }
  120. // ------------------------------------------------------------------------
  121. /**
  122. * Image URL
  123. *
  124. * Helps generate image URLs.
  125. *
  126. * @access public
  127. * @param string the name of the file or asset
  128. * @param string optional, module name
  129. * @return string full url to image asset
  130. */
  131. public function image_url($asset_name, $module_name = NULL)
  132. {
  133. return $this->_asset_url($asset_name, $module_name, config_item('asset_img_dir'));
  134. }
  135. // ------------------------------------------------------------------------
  136. /**
  137. * JS
  138. *
  139. * Helps generate JavaScript asset HTML.
  140. *
  141. * @access public
  142. * @param string the name of the file or asset
  143. * @param string optional, module name
  144. * @return string HTML code for JavaScript asset
  145. */
  146. public function js($asset_name, $module_name = NULL)
  147. {
  148. return '<script type="text/javascript" src="' . $this->js_path($asset_name, $module_name) . '"></script>' . "\n";
  149. }
  150. // ------------------------------------------------------------------------
  151. /**
  152. * JS Path
  153. *
  154. * Helps generate JavaScript asset paths.
  155. *
  156. * @access public
  157. * @param string the name of the file or asset
  158. * @param string optional, module name
  159. * @return string web root path to JavaScript asset
  160. */
  161. public function js_path($asset_name, $module_name = NULL)
  162. {
  163. return $this->_asset_path($asset_name, $module_name, config_item('asset_js_dir'));
  164. }
  165. // ------------------------------------------------------------------------
  166. /**
  167. * JS URL
  168. *
  169. * Helps generate JavaScript asset locations.
  170. *
  171. * @access public
  172. * @param string the name of the file or asset
  173. * @param string optional, module name
  174. * @return string full url to JavaScript asset
  175. */
  176. public function js_url($asset_name, $module_name = NULL)
  177. {
  178. return $this->_asset_url($asset_name, $module_name, config_item('asset_js_dir'));
  179. }
  180. // ------------------------------------------------------------------------
  181. /**
  182. * General Asset HTML Helper
  183. *
  184. * The main asset location generator
  185. *
  186. * @access private
  187. * @param string the name of the file or asset
  188. * @param string optional, module name
  189. * @return string HTML code for JavaScript asset
  190. */
  191. private function _asset_path($asset_name, $module_name = NULL, $asset_type = NULL)
  192. {
  193. return $this->_other_asset_location($asset_name, $module_name, $asset_type, 'path');
  194. }
  195. public function _asset_url($asset_name, $module_name = NULL, $asset_type = NULL)
  196. {
  197. return $this->_other_asset_location($asset_name, $module_name, $asset_type, 'url');
  198. }
  199. private function _other_asset_location($asset_name, $module_name = NULL, $asset_type = NULL, $location_type = 'url')
  200. {
  201. // Given a full URL
  202. if (strpos($asset_name, '://') !== FALSE)
  203. {
  204. return $asset_name;
  205. }
  206. $base_location = config_item($location_type == 'url' ? 'asset_url' : 'asset_dir');
  207. // If they are using a direct path, take them to it
  208. if (strpos($asset_name, 'assets/') !== FALSE)
  209. {
  210. $asset_location = $base_location . $asset_name;
  211. }
  212. // If they have just given a filename, not an asset path, and its in a theme
  213. elseif ($module_name == '_theme_' && $this->theme != NULL)
  214. {
  215. $asset_location = base_url() . ltrim(config_item('theme_asset_dir'), '/')
  216. . $this->theme . '/'
  217. . $asset_type . '/' . $asset_name;
  218. }
  219. // Normal file (that might be in a module)
  220. else
  221. {
  222. $asset_location = $base_location;
  223. // Its in a module, ignore the current
  224. if ($module_name)
  225. {
  226. foreach (Modules::$locations as $path => $offset)
  227. {
  228. if (is_dir($path . $module_name))
  229. {
  230. // TODO: Fix this fucking mess
  231. $asset_location = base_url() . $path . $module_name . '/';
  232. break;
  233. }
  234. }
  235. }
  236. $asset_location .= ( $asset_type == '' ? '' : $asset_type . '/') . $asset_name;
  237. }
  238. return $asset_location;
  239. }
  240. // ------------------------------------------------------------------------
  241. /**
  242. * Parse HTML Attributes
  243. *
  244. * Turns an array of attributes into a string
  245. *
  246. * @access private
  247. * @param array attributes to be parsed
  248. * @return string string of html attributes
  249. */
  250. private function _parse_asset_html($attributes = NULL)
  251. {
  252. $attribute_str = '';
  253. if (is_string($attributes))
  254. {
  255. $attribute_str = $attributes;
  256. }
  257. else if (is_array($attributes) || is_object($attributes))
  258. {
  259. foreach ($attributes as $key => $value)
  260. {
  261. $attribute_str .= ' ' . $key . '="' . $value . '"';
  262. }
  263. }
  264. return $attribute_str;
  265. }
  266. // ------------------------------------------------------------------------
  267. /**
  268. * Set theme
  269. *
  270. * If you use some sort of theme system, this method stores the theme name
  271. *
  272. * @access public
  273. * @param string theme name
  274. */
  275. public function set_theme($theme)
  276. {
  277. $this->theme = $theme;
  278. }
  279. }
  280. // END Asset Class
  281. /* End of file Asset.php */
  282. /* Location: ./application/libraries/Asset.php */