PageRenderTime 25ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/application/libraries/Assets.php

https://github.com/mydesignivan/Bottino
PHP | 459 lines | 228 code | 75 blank | 156 comment | 57 complexity | d20a25fee05f893e72b76864fe8f9dfa MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class Assets {
  3. private $ci;
  4. private $host;
  5. private $combine;
  6. // Javascript variables
  7. private $inline_scripts = array();
  8. private $external_scripts = array();
  9. public $external_scripts_default = array();
  10. // Styles
  11. private $styles = array();
  12. public $styles_default = array();
  13. private $assets_css_group = array();
  14. private $assets_js_group = array();
  15. private $tmp_js = array();
  16. private $tmp_css = array();
  17. //---------------------------------------------------------------
  18. /**
  19. * Constructor.
  20. *
  21. * Load the assets config file, and inserts the base
  22. * css and js into our array for later use. This ensures
  23. * that these files will be processed first, in the order
  24. * the user is expecting, prior to and later-added files.
  25. *
  26. * @access public
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. $this->ci =& get_instance();
  32. $this->ci->config->load('assets');
  33. $this->ci->config->load('assets_js');
  34. $this->ci->config->load('assets_css');
  35. $this->styles_default = $this->ci->config->item('assets_css_default');
  36. $this->external_scripts_default = $this->ci->config->item('assets_js_default');
  37. $this->assets_css_group = $this->ci->config->item('assets_css_group');
  38. $this->assets_js_group = $this->ci->config->item('assets_js_group');
  39. // Setup our host
  40. $this->host = $this->ci->config->item('asset_host');
  41. $this->host = empty($this->host) ? base_url() : $this->host;
  42. $this->combine = $this->ci->config->item('combine_on_'.devmode());
  43. }
  44. //---------------------------------------------------------------
  45. //---------------------------------------------------------------
  46. // !STYLESHEET FUNCTIONS
  47. //---------------------------------------------------------------
  48. /**
  49. * add_css function.
  50. *
  51. * accepts either an array or a string with a single css file name
  52. * and appends them to the base styles in $this->styles;
  53. *
  54. * The file names should NOT have the .css added on to them.
  55. *
  56. * @access public
  57. * @param mixed $styles. (default: null)
  58. * @return void
  59. */
  60. public function add_css($styles=null, $combine=null){
  61. $combine = is_null($combine) ? $this->combine : $combine;
  62. if (is_string($styles) && !empty($styles)){
  63. $this->styles[] = array($combine, $styles);
  64. }else if (is_array($styles) && count($styles) != 0){
  65. foreach ($styles as $style){
  66. $this->styles[] = array($combine, $style);
  67. }
  68. }
  69. }
  70. public function add_css_group($styles=null, $combine=null, $get=false){
  71. $combine = is_null($combine) ? $this->combine : $combine;
  72. $arr = $this->assets_css_group;
  73. if (is_string($styles) && !empty($styles)) $styles = array($styles);
  74. foreach( $styles as $style ){
  75. if( isset($arr[$style]) ){
  76. if( is_array($arr[$style]) ){
  77. foreach( $arr[$style] as $val ){
  78. if(!$get) $this->styles[] = array($combine, $val);
  79. else $this->tmp_css[] = array($combine, $val);
  80. }
  81. }elseif( is_string($arr[$style]) && !empty($arr[$style]) ){
  82. if(!$get) $this->styles[] = array($combine, $arr[$style]);
  83. else $this->tmp_css[] = array($combine, $arr[$style]);
  84. }
  85. }
  86. }
  87. }
  88. //---------------------------------------------------------------
  89. /**
  90. * css function.
  91. *
  92. * Creates the proper links for inserting into the HTML head,
  93. * depending on whether devmode is 'dev' or other (test/production).
  94. *
  95. * Accepts an array of styles to be used in place of the base files
  96. * set in the config file. This allows you to completely replace
  97. * the styles being used in one area of your site, like the admin section.
  98. *
  99. * If you need additional styles than the base, you should make a call
  100. * to add_css(), above.
  101. *
  102. * The file names should NOT have the .css extension. They will be added.
  103. *
  104. * @access public
  105. * @param mixed $new_styles. (default: null)
  106. * @return void
  107. */
  108. public function css($new_styles=null) {
  109. $styles = array();
  110. if (is_array($new_styles)){
  111. $styles = $new_styles;
  112. } else {
  113. $styles = $this->styles;
  114. }
  115. $folder = $this->ci->config->item('css_folder') . '/';
  116. $path = $this->ci->config->item('asset_folder');
  117. $this->_show_tag($this->styles_default, 'css', $path, $folder, true);
  118. }
  119. //---------------------------------------------------------------
  120. //---------------------------------------------------------------
  121. // !JAVASCRIPT FUNCTIONS
  122. //---------------------------------------------------------------
  123. /**
  124. * add_js function.
  125. *
  126. * accepts either an array or a string with a single js file name
  127. * and appends them to the base scripts in $this->js;
  128. *
  129. * The file names should NOT have the .js added on to them.
  130. *
  131. * @access public
  132. * @param mixed $scripts. (default: null)
  133. * @return void
  134. */
  135. public function add_js($scripts=null, $combine=null){
  136. $combine = is_null($combine) ? $this->combine : $combine;
  137. if (is_string($scripts) && !empty($scripts)){
  138. $this->external_scripts[] = array($combine, $scripts);
  139. }else if (is_array($scripts) && count($scripts) != 0){
  140. foreach ($scripts as $script){
  141. $this->external_scripts[] = array($combine, $script);
  142. }
  143. }
  144. }
  145. public function add_js_group($scripts=null, $combine=null, $get=false){
  146. $combine = is_null($combine) ? $this->combine : $combine;
  147. $arr = $this->assets_js_group;
  148. if (is_string($scripts) && !empty($scripts)) $scripts = array($scripts);
  149. $this->add_css_group($scripts);
  150. foreach( $scripts as $script ){
  151. if( isset($arr[$script]) ){
  152. if( is_array($arr[$script]) ){
  153. foreach( $arr[$script] as $val ){
  154. if( !$get ) $this->external_scripts[] = array($combine, $val);
  155. else $this->tmp_js[] = array($combine, $val);
  156. }
  157. }elseif( is_string($arr[$script]) && !empty($arr[$script]) ){
  158. if( !$get ) $this->external_scripts[] = array($combine, $arr[$script]);
  159. else $this->tmp_js[] = array($combine, $arr[$script]);
  160. }
  161. }
  162. }
  163. }
  164. //---------------------------------------------------------------
  165. public function add_js_default($data){
  166. $this->external_scripts_default = array_merge($this->external_scripts_default, $data);
  167. }
  168. public function add_css_default($data){
  169. $this->styles = array_merge($this->styles, $data);
  170. }
  171. /**
  172. * add_inline_js function.
  173. *
  174. * Adds scripts to be rendered on just that page, inline.
  175. *
  176. * @access public
  177. * @param mixed $scripts. (default: null)
  178. * @return void
  179. */
  180. public function add_inline_js($scripts=null)
  181. {
  182. if (is_array($scripts) && count($scripts) != 0)
  183. {
  184. foreach ($scripts as $js)
  185. {
  186. $this->inline_scripts[] = $js;
  187. }
  188. } else if (!empty($scripts))
  189. {
  190. $this->inline_scripts[] = $scripts;
  191. }
  192. }
  193. //---------------------------------------------------------------
  194. /**
  195. * js function.
  196. *
  197. * Creates the proper links for inserting into the HTML head,
  198. * depending on whether devmode is 'dev' or other (test/production).
  199. *
  200. * Accepts an array of scripts to be used in place of the base files
  201. * set in the config file. This allows you to completely replace
  202. * the javascript being used in one area of your site, like the admin section.
  203. *
  204. * If you need additional scripts than the base, you should make a call
  205. * to add_js(), above.
  206. *
  207. * The file names should NOT have the .js extension. They will be added.
  208. *
  209. * @access public
  210. * @param mixed $new_js. (default: null)
  211. * @return void
  212. */
  213. public function js($new_js=null)
  214. {
  215. $js = array();
  216. if (is_array($new_js))
  217. {
  218. $js = $new_js;
  219. } else
  220. {
  221. $js = $this->external_scripts;
  222. }
  223. $folder = $this->ci->config->item('js_folder') . '/';
  224. $path = $this->ci->config->item('asset_folder');
  225. $this->_show_tag($this->external_scripts_default, 'js', $path, $folder, true);
  226. //$this->_inline_js();
  227. }
  228. //---------------------------------------------------------------
  229. /**
  230. * _show_tag function.
  231. *
  232. */
  233. private function _show_tag($arrLinks, $type, $path, $folder, $def){
  234. $files = $files_combine = array();
  235. eval('$this->tmp_'.$type.'=array();');
  236. foreach( $arrLinks as $val ){
  237. if( is_array($val) ){
  238. if( $def && count($val)==2 ){
  239. $arr = explode("/", $val[0]);
  240. $val[0] = !is_numeric(array_search("combine", $arr)) ? $this->combine : false;
  241. if( array_search("group", $arr)!==FALSE ) {
  242. eval('$this->add_'.$type.'_group($val[1], $val[0], true);');
  243. }
  244. }
  245. $combine = $val[0];
  246. $name = $val[1];
  247. }else{
  248. $combine = $this->combine;
  249. $name = $val;
  250. }
  251. if( is_string($name) ){
  252. if( !$combine ) {
  253. if( $type=="js" ){
  254. echo '<script type="text/javascript" src="'. $this->host . $path . $folder . $name .'.js" ></script>' . "\n";
  255. }else{
  256. echo '<link rel="stylesheet" type="text/css" href="' . $this->host . $path . $folder . $name . '.css" media="screen, projection" />' . "\n";
  257. }
  258. }
  259. else $files_combine[] = $folder . $name;
  260. }
  261. }
  262. if( count($files_combine)>0 ){
  263. $files_combine = implode('.'.$type.',', $files_combine) . '.'.$type;
  264. if( $type=="js" ){
  265. echo '<script type="text/javascript" src="' . $this->host . $path . $files_combine.'" ></script>' . "\n";
  266. }else{
  267. echo '<link rel="stylesheet" type="text/css" href="' . $this->host . $path . $files_combine . '" />' . "\n";
  268. }
  269. }
  270. if( $def ) {
  271. if( $type=="js" ){
  272. $this->external_scripts = array_merge($this->tmp_js, $this->external_scripts);
  273. $this->_show_tag($this->external_scripts, $type, $path, $folder, false);
  274. }else{
  275. $this->styles = array_merge($this->tmp_css, $this->styles);
  276. $this->_show_tag($this->styles, $type, $path, $folder, false);
  277. }
  278. }
  279. }
  280. //---------------------------------------------------------------
  281. /**
  282. * _inline_js function.
  283. *
  284. * This private method does the actual work of generating the
  285. * inline js code. All code is wrapped by open and close tags
  286. * specified in the config file, so that you can modify it to
  287. * use your favorite js library.
  288. *
  289. * It is called by the js() method.
  290. *
  291. * @access private
  292. * @return void
  293. */
  294. private function _inline_js()
  295. {
  296. // Are there any scripts to include?
  297. if (count($this->inline_scripts) == 0)
  298. {
  299. return false;
  300. }
  301. // Create our shell opening
  302. echo '<script type="text/javascript">' . "\n";
  303. echo $this->ci->config->item('inline_js_opener') ."\n\n";
  304. // Loop through all available scripts
  305. // inserting them inside the shell.
  306. foreach($this->inline_scripts as $script)
  307. {
  308. echo $script . "\n";
  309. }
  310. // Close the shell.
  311. echo "\n" . $this->ci->config->item('inline_js_closer') . "\n";
  312. echo '</script>' . "\n";
  313. }
  314. //---------------------------------------------------------------
  315. //---------------------------------------------------------------
  316. // !IMAGE FUNCTIONS
  317. //---------------------------------------------------------------
  318. public function image($path='', $extras=array())
  319. {
  320. if (empty($path)) return;
  321. // Build our extra attributes string
  322. $attributes = '';
  323. foreach ($extras as $key => $value)
  324. {
  325. $attributes .= " $key='$value'";
  326. }
  327. echo '<img src="'. $this->host . $path .'"'. $attributes .' />';
  328. }
  329. //---------------------------------------------------------------
  330. }
  331. /*
  332. | -------------------------------------------------------------------
  333. | ABout devMode
  334. | -------------------------------------------------------------------
  335. | This helper provides a simple, efficient, and site-wide way of
  336. | knowing whether your app is running on the development, test, or
  337. | production server.
  338. |
  339. | For many smaller sites, this helper will not be necessary, though
  340. | it can still prove useful, even if it's only used to set the active
  341. | database group.
  342. |
  343. | When building your $servers array, do not include the http:// or
  344. | https://. Also, make sure that your production server is listed last
  345. | within the array, so that any subdomain searches will be caught
  346. | prior to finding the main site.
  347. |
  348. */
  349. function devmode($test_mode=null)
  350. {
  351. $ci =& get_instance();
  352. $servers = $ci->config->item('servers');
  353. // To make testing more accurate, get rid of the http://, etc.
  354. $current_server = strtolower(trim(base_url(), ' /'));
  355. $current_server = str_replace('http://', '', $current_server);
  356. $current_server = str_replace('https://', '', $current_server);
  357. //$current_mode = array_search($current_server, $servers);
  358. $current_mode = '';
  359. // Because the server name could contain www. or subdomains,
  360. // we need to search each item to see if it contains the string.
  361. foreach ($servers as $name => $domain)
  362. {
  363. if (!empty($domain))
  364. {
  365. if (strpos($current_server, $domain) !== FALSE) {
  366. $current_mode = $name;
  367. break;
  368. }
  369. }
  370. }
  371. // Time to figure out what to return.
  372. if (empty($test_mode))
  373. {
  374. // Not performing a check, so just return the current value
  375. return $current_mode;
  376. } else
  377. {
  378. return $current_mode == $test_mode;
  379. }
  380. }
  381. //---------------------------------------------------------------
  382. // END Assets class
  383. /* End of file Assets.php */
  384. /* Location: ./application/libraries/Assets.php */