PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/application/helpers/asset_helper.php

https://bitbucket.org/davfive/easyorgchart
PHP | 381 lines | 173 code | 27 blank | 181 comment | 22 complexity | 7f438fcab2bf76297c473c3354eff75e MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Sekati CodeIgniter Asset Helper
  4. *
  5. * @package Sekati
  6. * @author Jason M Horwitz
  7. * @copyright Copyright (c) 2012, Sekati LLC.
  8. * @license http://www.opensource.org/licenses/mit-license.php
  9. * @link http://sekati.com
  10. * https://github.com/sekati/codeigniter-asset-helper
  11. * @version v1.2.5
  12. * @filesource
  13. *
  14. * @usage $autoload['config'] = array('asset');
  15. * $autoload['helper'] = array('asset');
  16. * @example <img src="<?=asset_url();?>imgs/photo.jpg" />
  17. * @example <?=img('photo.jpg')?>
  18. *
  19. * @install Copy config/asset.php to your CI application/config directory
  20. * & helpers/asset_helper.php to your application/helpers/ directory.
  21. * Then add both files as autoloads in application/autoload.php:
  22. *
  23. * $autoload['config'] = array('asset');
  24. * $autoload['helper'] = array('asset');
  25. *
  26. * Autoload CodeIgniter's url_helper in application/config/autoload.php:
  27. * $autoload['helper'] = array('url');
  28. *
  29. * @notes Organized assets in the top level of your CodeIgniter 2.x app:
  30. * - assets/
  31. * -- css/
  32. * -- download/
  33. * -- img/
  34. * -- js/
  35. * -- less/
  36. * -- swf/
  37. * -- upload/
  38. * -- xml/
  39. * - application/
  40. * -- config/asset.php
  41. * -- helpers/asset_helper.php
  42. */
  43. // ------------------------------------------------------------------------
  44. // PATH HELPERS
  45. /**
  46. * Get asset URL
  47. *
  48. * @access public
  49. * @return string
  50. */
  51. if ( ! function_exists('asset_url'))
  52. {
  53. function asset_url()
  54. {
  55. //get an instance of CI so we can access our configuration
  56. $CI =& get_instance();
  57. //return the full asset path
  58. return base_url() . $CI->config->item('asset_path');
  59. }
  60. }
  61. /**
  62. * Get css URL
  63. *
  64. * @access public
  65. * @return string
  66. */
  67. if ( ! function_exists('css_url'))
  68. {
  69. function css_url()
  70. {
  71. $CI =& get_instance();
  72. return base_url() . $CI->config->item('css_path');
  73. }
  74. }
  75. /**
  76. * Get less URL
  77. *
  78. * @access public
  79. * @return string
  80. */
  81. if ( ! function_exists('less_url'))
  82. {
  83. function less_url()
  84. {
  85. $CI =& get_instance();
  86. return base_url() . $CI->config->item('less_path');
  87. }
  88. }
  89. /**
  90. * Get js URL
  91. *
  92. * @access public
  93. * @return string
  94. */
  95. if ( ! function_exists('js_url'))
  96. {
  97. function js_url()
  98. {
  99. $CI =& get_instance();
  100. return base_url() . $CI->config->item('js_path');
  101. }
  102. }
  103. /**
  104. * Get image URL
  105. *
  106. * @access public
  107. * @return string
  108. */
  109. if ( ! function_exists('img_url'))
  110. {
  111. function img_url()
  112. {
  113. $CI =& get_instance();
  114. return base_url() . $CI->config->item('img_path');
  115. }
  116. }
  117. /**
  118. * Get SWF URL
  119. *
  120. * @access public
  121. * @return string
  122. */
  123. if ( ! function_exists('swf_url'))
  124. {
  125. function swf_url()
  126. {
  127. $CI =& get_instance();
  128. return base_url() . $CI->config->item('swf_path');
  129. }
  130. }
  131. /**
  132. * Get Upload URL
  133. *
  134. * @access public
  135. * @return string
  136. */
  137. if ( ! function_exists('upload_url'))
  138. {
  139. function upload_url()
  140. {
  141. $CI =& get_instance();
  142. return base_url() . $CI->config->item('upload_path');
  143. }
  144. }
  145. /**
  146. * Get Download URL
  147. *
  148. * @access public
  149. * @return string
  150. */
  151. if ( ! function_exists('download_url'))
  152. {
  153. function download_url()
  154. {
  155. $CI =& get_instance();
  156. return base_url() . $CI->config->item('download_path');
  157. }
  158. }
  159. /**
  160. * Get XML URL
  161. *
  162. * @access public
  163. * @return string
  164. */
  165. if ( ! function_exists('xml_url'))
  166. {
  167. function xml_url()
  168. {
  169. $CI =& get_instance();
  170. return base_url() . $CI->config->item('xml_path');
  171. }
  172. }
  173. // ------------------------------------------------------------------------
  174. // PATH HELPERS
  175. /**
  176. * Get the Absolute Upload Path
  177. *
  178. * @access public
  179. * @return string
  180. */
  181. if ( ! function_exists('upload_path'))
  182. {
  183. function upload_path()
  184. {
  185. $CI =& get_instance();
  186. return FCPATH . $CI->config->item('upload_path');
  187. }
  188. }
  189. /**
  190. * Get the Relative (to app root) Upload Path
  191. *
  192. * @access public
  193. * @return string
  194. */
  195. if ( ! function_exists('upload_path_relative'))
  196. {
  197. function upload_path_relative()
  198. {
  199. $CI =& get_instance();
  200. return './' . $CI->config->item('upload_path');
  201. }
  202. }
  203. /**
  204. * Get the Absolute Download Path
  205. *
  206. * @access public
  207. * @return string
  208. */
  209. if ( ! function_exists('download_path'))
  210. {
  211. function download_path()
  212. {
  213. $CI =& get_instance();
  214. return FCPATH . $CI->config->item('download_path');
  215. }
  216. }
  217. /**
  218. * Get the Relative (to app root) Download Path
  219. *
  220. * @access public
  221. * @return string
  222. */
  223. if ( ! function_exists('download_path_relative'))
  224. {
  225. function download_path_relative()
  226. {
  227. $CI =& get_instance();
  228. return './' . $CI->config->item('download_path');
  229. }
  230. }
  231. // ------------------------------------------------------------------------
  232. // EMBED HELPERS
  233. /**
  234. * Load CSS
  235. * Creates the <link> tag that links all requested css file
  236. * @access public
  237. * @param string
  238. * @return string
  239. */
  240. if ( ! function_exists('css'))
  241. {
  242. function css($file, $media='all')
  243. {
  244. return '<link rel="stylesheet" type="text/css" href="' . css_url() . $file . '" media="' . $media . '">'."\n";
  245. }
  246. }
  247. /**
  248. * Load LESS
  249. * Creates the <link> tag that links all requested LESS file
  250. * @access public
  251. * @param string
  252. * @return string
  253. */
  254. if ( ! function_exists('less'))
  255. {
  256. function less($file)
  257. {
  258. return '<link rel="stylesheet/less" type="text/css" href="' . less_url() . $file . '">'."\n";
  259. }
  260. }
  261. /**
  262. * Load JS
  263. * Creates the <script> tag that links all requested js file
  264. * @access public
  265. * @param string
  266. * @return string
  267. */
  268. if ( ! function_exists('js'))
  269. {
  270. function js($file)
  271. {
  272. return '<script type="text/javascript" src="' . js_url() . $file . '"></script>'."\n";
  273. }
  274. }
  275. /**
  276. * Load Image
  277. * Creates the <script> tag that links all requested js file
  278. * @access public
  279. * @param string
  280. * @param array $atts Optional, additional key/value attributes to include in the IMG tag
  281. * @return string
  282. */
  283. if ( ! function_exists('img'))
  284. {
  285. function img($file, $atts = array())
  286. {
  287. $url = '<img src="' . img_url() . $file . '"';
  288. foreach ( $atts as $key => $val )
  289. $url .= ' ' . $key . '="' . $val . '"';
  290. $url .= " />\n";
  291. return $url;
  292. }
  293. }
  294. /**
  295. * Load Minified JQuery CDN w/ failover
  296. * Creates the <script> tag that links all requested js file
  297. * @access public
  298. * @param string
  299. * @return string
  300. */
  301. if ( ! function_exists('jquery'))
  302. {
  303. function jquery($version='')
  304. {
  305. // Grab Google CDN's jQuery, with a protocol relative URL; fall back to local if offline
  306. $out = '<script src="//ajax.googleapis.com/ajax/libs/jquery/'.$version.'/jquery.min.js"></script>'."\n";
  307. $out .= '<script>window.jQuery || document.write(\'<script src="'.js_url().'jquery-'.$version.'.min.js"><\/script>\')</script>'."\n";
  308. return $out;
  309. }
  310. }
  311. /**
  312. * Load Minified JQuery-UI CDN w/ failover
  313. * Creates the <script> tag that links all requested js file
  314. * @access public
  315. * @param string
  316. * @return string
  317. */
  318. if ( ! function_exists('jqueryui'))
  319. {
  320. function jqueryui($version='')
  321. {
  322. // Grab Google CDN's jQuery, with a protocol relative URL; fall back to local if offline
  323. $out = '<script src="//ajax.googleapis.com/ajax/libs/jqueryui/'.$version.'/jquery-ui.min.js"></script>'."\n";
  324. $out .= '<script>window.jQuery || document.write(\'<script src="'.js_url().'jquery-ui-'.$version.'.min.js"><\/script>\')</script>'."\n";
  325. return $out;
  326. }
  327. }
  328. /**
  329. * Load Google Analytics
  330. * Creates the <script> tag that links all requested js file
  331. * @access public
  332. * @param string
  333. * @return string
  334. */
  335. if ( ! function_exists('google_analytics'))
  336. {
  337. function google_analytics($ua='')
  338. {
  339. // Change UA-XXXXX-X to be your site's ID
  340. $out = "<!-- Google Webmaster Tools & Analytics -->\n";
  341. $out .='<script type="text/javascript">';
  342. $out .=' var _gaq = _gaq || [];';
  343. $out .=" _gaq.push(['_setAccount', '$ua']);";
  344. $out .=" _gaq.push(['_trackPageview']);";
  345. $out .=' (function() {';
  346. $out .=" var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;";
  347. $out .=" ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';";
  348. $out .=" var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);";
  349. $out .=" })();";
  350. $out .="</script>";
  351. return $out;
  352. }
  353. }
  354. /* End of file asset_helper.php */