Improve asset library
This commit is contained in:
@@ -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 */
|
||||
|
||||
@@ -1,101 +0,0 @@
|
||||
<?php defined('BASEPATH') || exit('No direct script access allowed');
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Eike Foken <kontakt@eikefoken.de>
|
||||
*/
|
||||
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 = '<img src="' . $link . '"';
|
||||
|
||||
foreach ($attributes as $key => $value) {
|
||||
$output .= " $key=\"$value\"";
|
||||
}
|
||||
|
||||
$output .= ' />';
|
||||
break;
|
||||
case 'css':
|
||||
$output = '<link href="' . $link . '" rel="stylesheet" type="text/css"';
|
||||
foreach ($attributes as $key => $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 = '<script src="' . $link . '" language="javascript" type="text/javascript"></script>';
|
||||
$output .= "\n";
|
||||
break;
|
||||
default:
|
||||
$output = '';
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
||||
/* End of file Assets.php */
|
||||
/* Location: ./application/libraries/Assets.php */
|
||||
146
application/libraries/Assets.php
Normal file
146
application/libraries/Assets.php
Normal file
@@ -0,0 +1,146 @@
|
||||
<?php defined('BASEPATH') || exit('No direct script access allowed');
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Eike Foken <kontakt@eikefoken.de>
|
||||
*/
|
||||
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 = '<img src="' . $this->CI->config->slash_item('base_url') . $filepath . '"' . $this->parseAttributes($attributes) . ' />';
|
||||
break;
|
||||
case 'css':
|
||||
$output = '<link href="' . $this->CI->config->slash_item('base_url') . $filepath . '" rel="stylesheet" type="text/css"' . $this->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 = '<script src="' . $this->CI->config->slash_item('base_url') . $filepath . '" language="javascript" type="text/javascript"></script>';
|
||||
$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 */
|
||||
@@ -7,15 +7,16 @@
|
||||
|
||||
<title>ScattPort</title>
|
||||
|
||||
<?=asset('css', 'style.css');?>
|
||||
<?=asset('css', 'table.css');?>
|
||||
<?=asset('css', 'form.css');?>
|
||||
<?=css_asset('style.css');?>
|
||||
<?=css_asset('table.css');?>
|
||||
<?=css_asset('form.css');?>
|
||||
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" language="javascript" type="text/javascript"></script>
|
||||
<?=asset('js', 'tablednd.jquery.js');?>
|
||||
<?=asset('js', 'jsc3d.min.js');?>
|
||||
<?=asset('js', 'scattport.js');?>
|
||||
<?=asset('js', 'jtip.js');?>
|
||||
<?=script_tag('https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js');?>
|
||||
|
||||
<?=js_asset('tablednd.jquery.js');?>
|
||||
<?=js_asset('jsc3d.min.js');?>
|
||||
<?=js_asset('scattport.js');?>
|
||||
<?=js_asset('jtip.js');?>
|
||||
|
||||
<script type="text/javascript">
|
||||
var SITE_URL = '<?=site_url();?>';
|
||||
@@ -27,7 +28,7 @@
|
||||
<body>
|
||||
|
||||
<div id="header">
|
||||
<h1><?=anchor('', asset('image', 'logo.png'));?></h1>
|
||||
<h1><?=anchor('', image_asset('logo.png'));?></h1>
|
||||
<div class="status">
|
||||
<select name="activeProject">
|
||||
<?php
|
||||
|
||||
Reference in New Issue
Block a user