[eluser]Matthew Potter[/eluser]
I'd love to hear what you all think of the following:
It's a library for creating links within the <head> of the document.
(sorry about the spaces instead of tabs but it was over 6000 characters and CI forum converts tabs to 4 spaces automatically)
./application/library/html_header_links.php
Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* HTML Header Links Codeigniter Library
*
* Description
*
* @license GNU General Public License
* @author Matthew Potter
* @link http://askmatthewpotter.com
* @email [email protected]
*
* @file html_header_links
* @version 1.0
* @date 03/14/2010
*
* @settings: Change the default root paths for each file type
* $this->html_header_links->css_root_path = 'path/from/root/'
* $this->html_header_links->js_root_path = 'path/from/root/'
*
* @function: Add multiple files via array input
* $this->html_header_links->add_files( array() );
* @input:
* Array of filenames that include both the path
* from the root folder of the file type and
* also the file type:
* array(
* 'filename.ext'
* [, 'filename.ext']
* );
*
* Array of named arrays where the array key
* "name" is required but you may also pass "type"
* for file type and "path" as file path from root
* folder for the file type.
* *If the optional keys are not defined, the name
* value must inlcude a file extension:
* array(
* array( "name" => filename[.ext]*
* [, "type" => "ext"
* [, "path" => "path/to/file"]
* )
* );
*
* You may also mix the input types:
* array(
* array( "name" => filename.ext),
* array(
* "name" => "filename",
* "type" => "ext"
* ),
* "path/to/file/filename.ext"
* )
*
* *if path to file includes "http://" the base url and
* default path for file type is dropped to allow for
* external links.
*
* @function: Return links as string
* $this->html_header_links->render( ['css' || 'js'] );
*
** Copyright (c) 2010 **/
class HTML_Header_Links {
var $css_files = array();
var $js_files = array();
var $css_root_path = "interface/css/";
var $js_root_path = "interface/javascript/";
var $default_links = array();
function HTML_Header_Links(){
$CI =& get_instance();
$CI->config->item('base_url');
$this->add_files($this->default_links);
}
function add_files($files){
if(!is_array($files)) { show_error('The add_files function requires an array'); }
foreach($files as $file){
if(is_array($file)){
$file_name = $file["name"];
$file_type = $file["type"];
$file_sub_path = $file["path"];
$extension_check = split("\.", $file_name);
if(count($extension_check) > 1 ){
$file_type = array_pop($extension_check);
$file_name = implode(".", $extension_check);
}
}else{
$extension_check = split("\.", $file);
$file_type = array_pop($extension_check);
$file_name = implode(".", $extension_check);
$path_check = split("/", $file_name);
$file_sub_path = "";
if($file_type != "css" && $file_type != "js") {
show_error('You must define a file extension in the add_files funtion. Please read the instructions for details on different methods.<br /><br /> Ref: '.$file.'');
}
if(count($path_check) > 1){
$file_name = array_pop($path_check);
$file_sub_path = implode("/", $path_check) . "/";
}
}
if($file_type == "css"){
$this->add_css_file($file_name . "." . $file_type, $file_sub_path);
}
if($file_type == "js"){
$this->add_js_file($file_name . "." . $file_type, $file_sub_path);
}
} // End foreach $files as $file
}
function add_css_file($file_name, $sub_path = ""){
if(is_array($file_name)){
foreach($file_name as $file){
$this->add_css_file($file, $sub_path);
}
}else{
if(substr($file_name, strlen($file_name)-4) != ".css"){
$file_name .= ".css";
}
if($sub_path != ""){
if(substr($sub_path, strlen($sub_path)-1) != "/"){
$sub_path .= "/";
}
}
$this->css_files[] = array(
"filename" => $file_name,
"sub_path" => $sub_path
);
}
}
function add_js_file($file_name, $sub_path = ""){
if(is_array($file_name)){
foreach($file_name as $file){
$this->add_js_file($file, $sub_path);
}
}else{
if(substr($file_name, strlen($file_name)-3) != ".js"){
$file_name .= ".js";
}
if($sub_path != ""){
if(substr($sub_path, strlen($sub_path)-1) != "/"){
$sub_path .= "/";
}
}
$this->js_files[] = array(
"filename" => $file_name,
"sub_path" => $sub_path
);
}
}
function render($return_items = "all"){
$css_links = "";
$js_links = "";
if($return_items == "all" || $return_items == "css"){
foreach($this->css_files as $css){
$css_links .= "\t\t<link type='text/css' href='" . base_url() . $this->css_root_path . $css["sub_path"] . $css["filename"] . "' rel='Stylesheet' />\n";
}
}
if($return_items == "all" || $return_items == "js"){
foreach($this->js_files as $js){
if(strpos($js["sub_path"], "http://") === 0){
$js_links .= "\t\t[removed][removed]\n";
}else{
$js_links .= "\t\t[removed]js_root_path . $js["sub_path"] . $js["filename"] . "'>[removed]\n";
}
}
}
return $css_links . $js_links;
}
}
/* End of file html_header_links.php */
/* Location: ./application/libraries/html_header_links.php */
[eluser]Sbioko[/eluser]
Sorry, but I don't think, that this will help somebody. I just can say, that your library is *almost* well documented. I see, that you're trying to write code according to the CI standarts. This is good, but not necessary.
[eluser]Matthew Potter[/eluser]
Any recommendation on fixing the *almost*? What would you suggest?
[eluser]Sbioko[/eluser]
You'll need to comment each method. What it does. Look at any CI library. Every method is well documented. Also, you need to comment your code. For example:
Code: $match = preg_match('/[0-9]/', 'test124'); // Here we search for a numbers
[eluser]Matthew Potter[/eluser]
Makes sense. And doing so, I think I'll move the comments regarding each function from the top to the function position too.
Thank you.
[eluser]Sbioko[/eluser]
You're welcome :-) You can always ask me about everything!
|