diff --git a/application/helpers/asset_helper.php b/application/helpers/asset_helper.php index 4ec8fed..bbdb360 100644 --- a/application/helpers/asset_helper.php +++ b/application/helpers/asset_helper.php @@ -16,10 +16,9 @@ */ function asset_url($type, $filename) { $CI = &get_instance(); - $CI->load->config('assets'); + $CI->load->library('assets'); - return $CI->config->item('base_url') . $CI->config->item('assets_folder') - . $type . '/' . $filename; + return $CI->assets->url($type, $filename); } /** @@ -27,14 +26,46 @@ function asset_url($type, $filename) { * * @param string $type * @param string $filename - * @param array $attributes + * @param mixed $attributes * @return string */ -function asset($type, $filename, $attributes = array()) { +function asset($type, $filename, $attributes = '') { $CI = &get_instance(); - $CI->load->library('asset'); + $CI->load->library('assets'); - return $CI->asset->load($type, $filename, $attributes); + return $CI->assets->load($type, $filename, $attributes); +} + +/** + * Inserts CSS assets. + * + * @param string $filename + * @param mixed $attributes + * @return string + */ +function css_asset($filename, $attributes = '') { + return asset('css', $filename, $attributes); +} + +/** + * Inserts image assets. + * + * @param string $filename + * @param mixed $attributes + * @return string + */ +function image_asset($filename, $attributes = '') { + return asset('image', $filename, $attributes); +} + +/** + * Inserts javascript assets. + * + * @param string $filename + * @return string + */ +function js_asset($filename) { + return asset('js', $filename); } /* End of file asset_helper.php */ diff --git a/application/libraries/Asset.php b/application/libraries/Asset.php deleted file mode 100644 index b7e1b10..0000000 --- a/application/libraries/Asset.php +++ /dev/null @@ -1,101 +0,0 @@ - - */ -class Asset { - - private $assetsFolder; - private $cacheFolder; - - private $CI; - - /** - * Constructor. - */ - public function __construct() { - $this->CI =& get_instance(); - $this->CI->load->config('assets'); - - $this->assetsFolder = $this->CI->config->item('assets_folder'); - $this->cacheFolder = $this->CI->config->item('cache_folder'); - - log_message('debug', "Asset Class Initialized"); - } - - /** - * Loads an asset. - * - * @param string $type - * @param string $filename - */ - public function load($type, $filename, $attributes = array()) { - // validate type parameter - if (!empty($type)) { - $type = strtolower($type); - if (!in_array($type, array('image', 'images', 'icon', 'icons', 'css', 'js'))) { - log_message('error', "Invalid asset type '" . $type . "' used."); - } - if (in_array($type, array('image', 'icon'))) { - $type .= 's'; - } - } - - // convert filename to array - if (!empty($filename)) { - $link = $this->CI->config->slash_item('base_url') . $this->assetsFolder . $type . '/' . $filename; - $path = FCPATH . $this->assetsFolder . $type . '/' . $filename; - } - - switch ($type) { - case 'images': - case 'icons': - if (!isset($attributes['alt'])) { - $attributes['alt'] = ''; - } - - $output = ' $value) { - $output .= " $key=\"$value\""; - } - - $output .= ' />'; - break; - case 'css': - $output = ' $value) { - $output .= " $key=\"$value\""; - } - $output .= " />\n"; - break; - case 'js': -// $code = file_get_contents(FCPATH . $this->assetsFolder . $type . '/' . $filename); - -// if ($this->CI->config->item('enable_jsmin') == true) { -// $this->CI->load->library('jsmin'); -// $code = $this->CI->jsmin->minify($code); -// } - -// $httpHeaders = '<' . '?php header("Content-type: text/javascript; charset: UTF-8");?' . '>'; - -// if ($this->CI->config->item('compress_output') == true) { -// $httpHeaders .= ''; -// } -// $code = $httpHeaders . "\n" . $code; -// file_put_contents(FCPATH . $this->cacheFolder . $type . '_' . md5($code) . EXT, $code); - - $output = ''; - $output .= "\n"; - break; - default: - $output = ''; - } - - return $output; - } -} - -/* End of file Assets.php */ -/* Location: ./application/libraries/Assets.php */ diff --git a/application/libraries/Assets.php b/application/libraries/Assets.php new file mode 100644 index 0000000..c32f343 --- /dev/null +++ b/application/libraries/Assets.php @@ -0,0 +1,146 @@ + + */ +class Assets { + + private $assetsFolder; + private $cacheFolder; + + private $css = array(); + private $javascript = array(); + + private $CI; + + /** + * Constructor. + */ + public function __construct($config = array()) { + log_message('debug', "Asset Class Initialized"); + + // set the super object to a local variable + $this->CI =& get_instance(); + + // set the asset path + $this->assetsPath = ($config['assets_folder'] != '') ? $config['assets_folder'] : 'assets'; + + if (!is_dir($this->assetsPath)) { + log_message('error', "Asset folder does not exist"); + } + + // set the cache path + $this->cachePath = ($config['cache_folder'] != '') ? $config['cache_folder'] : 'assets/cache'; + + if (!is_dir($this->cachePath) || !is_really_writable($this->cachePath)) { + $this->cacheEnable = false; + } + } + + /** + * Loads an asset. + * + * @param string $type + * @param string $filename + * @param mixed $attributes + */ + public function load($type, $filename, $attributes = '') { + // validate type parameter + $type = strtolower($type); + if (!in_array($type, array('image', 'images', 'icon', 'icons', 'css', 'js'))) { + log_message('error', "Invalid asset type '" . $type . "' used."); + } + if (in_array($type, array('image', 'icon'))) { + $type .= 's'; + } + + // build the path strings + $filepath = $this->assetsPath . $type . '/' . $filename; + + if (!file_exists($filepath)) { + log_message('error', "Unable to load requested asset: $filename"); + return ''; + } + + switch ($type) { + case 'images': + case 'icons': + if (!isset($attributes['alt'])) { + $attributes['alt'] = ''; + } + + $output = 'parseAttributes($attributes) . ' />'; + break; + case 'css': + $output = 'parseAttributes($attributes) . " />\n"; + break; + case 'js': +// $this->javascript[] = array('filepath' => $filepath, 'attributes' => $attributes); + +// $code = file_get_contents(FCPATH . $this->assetsFolder . $type . '/' . $filename); + +// if ($this->CI->config->item('enable_jsmin') == true) { +// $this->CI->load->library('jsmin'); +// $code = $this->CI->jsmin->minify($code); +// } + +// $httpHeaders = '<' . '?php header("Content-type: text/javascript; charset: UTF-8");?' . '>'; + +// if ($this->CI->config->item('compress_output') == true) { +// $httpHeaders .= ''; +// } +// $code = $httpHeaders . "\n" . $code; +// file_put_contents(FCPATH . $this->cacheFolder . $type . '_' . md5($code) . EXT, $code); + $output = ''; + $output .= "\n"; + break; + default: + $output = ''; + } + + return $output; + } + + /** + * Generates an URL of the given asset. + * + * @param string $type + * @param string $filename + * @return string + */ + public function url($type, $filename) { + $type = strtolower($type); + if (!in_array($type, array('image', 'images', 'icon', 'icons', 'css', 'js'))) { + log_message('error', "'" . $type . "' is not a valid asset type"); + return; + } + if (in_array($type, array('image', 'icon'))) { + $type .= 's'; + } + + return $this->CI->config->slash_item('base_url') . $this->assetsPath . $type . '/' . $filename; + } + + /** + * Parses out the attributes. + * + * @param mixed $attributes + * @return string + */ + private function parseAttributes($attributes) { + if (is_string($attributes)) { + return ($attributes != '') ? ' '.$attributes : ''; + } + + $output = ''; + foreach ($attributes as $key => $value) { + $output .= ' ' . $key . '="' . $value . '"'; + } + + return $output; + } +} + +/* End of file Assets.php */ +/* Location: ./application/libraries/Assets.php */ diff --git a/application/views/header.php b/application/views/header.php index 0cfeec6..161c0e1 100644 --- a/application/views/header.php +++ b/application/views/header.php @@ -7,15 +7,16 @@ ScattPort - - - + + + - - - - - + + + + + +