array(),
'_css' => array(),
);
var $output;
var $js = array();
var $css = array();
var $parser = 'parser';
var $parser_method = 'parse';
var $parse_template = FALSE;
/**
* Constructor
*
* Loads master configuration, master regions, and validates existence of
* default template
*
* @access public
*/
function CI_Master()
{
// Copy an instance of CI so we can use the entire framework.
$this->CI =& get_instance();
// Load the master config file and setup our default master template and regions (? not really sure it's also regions...)
include(dirname(__DIR__) . '/config/master'.EXT);
if (isset($master))
{
$this->config = $master;
$this->set_master($master['active_template']);
}
}
// --------------------------------------------------------------------
/**
* Use given master settings
*
* @access public
* @param string array key to access master settings
* @return void
*/
function set_master($group)
{
if (isset($this->config[$group]))
{
$this->master = $this->config[$group];
$this->js = array();
$this->css = array();
}
else
{
show_error('The "'. $group .'" master group does not exist. Provide a valid group name or add the group first.');
}
$this->initialize($this->master);
}
// --------------------------------------------------------------------
/**
* Set masters default template
*
* @access public
* @param string filename of new master template file
* @return void
*/
function set_default_template($filename)
{
if (file_exists(APPPATH .'views/'. $filename) or file_exists(APPPATH .'views/'. $filename . EXT))
{
$this->default_template = $filename;
}
else
{
show_error('The filename provided does not exist in '. APPPATH .'views. Remember to include the extension if other than ".php"');
}
}
// --------------------------------------------------------------------
/**
* Dynamically add a master and optionally switch to it
*
* @access public
* @param string array key to access master settings
* @param array properly formed
* @return void
*/
function add_master($group, $master, $activate = FALSE)
{
if ( ! isset($this->config[$group]))
{
$this->config[$group] = $master;
if ($activate === TRUE)
{
$this->initialize($master);
}
}
else
{
show_error('The "'. $group .'" master group already exists. Use a different group name.');
}
}
// --------------------------------------------------------------------
/**
* Initialize class settings using config settings
*
* @access public
* @param array configuration array
* @return void
*/
function initialize($props)
{
// Set default template
if (isset($props['template'])
&& (file_exists(APPPATH .'views/'. $props['template']) or file_exists(APPPATH .'views/'. $props['template'] . EXT)))
{
$this->default_template = $props['template'];
}
else
{
// Default template must exist. Throw error.
show_error('Either you have not provided a default template for the master or the one provided does not exist in '. APPPATH .'views. Remember to include the extension if other than ".php"');
}
// Load our regions
if (isset($props['regions']))
{
$this->set_regions($props['regions']);
}
// LM: Load any styles
if (isset($props['css'])) {
foreach($props['css'] as $s) {
self::add_css($s, 'link', FALSE);
}
}
// LM: Load any scripts
if (isset($props['js'])) {
foreach($props['js'] as $s) {
self::add_js($s, 'import', FALSE);
}
}
// Set parser and parser method
if (isset($props['parser']))
{
$this->set_parser($props['parser']);
}
if (isset($props['parser_method']))
{
$this->set_parser_method($props['parser_method']);
}
// Set template parser instructions
$this->parse_template = isset($props['parse_template']) ? $props['parse_template'] : FALSE;
}
// --------------------------------------------------------------------
/**
* Set regions for writing to
*
* @access public
* @param array properly formed regions array
* @return void
*/
function set_regions($regions)
{
if (count($regions))
{
$this->regions = array(
'_js' => array(),
'_css' => array(),
);
foreach ($regions as $key => $region)
{
// Regions must be arrays, but we take the burden off the master
// developer and insure it here
if ( ! is_array($region))
{
$this->add_region($region);
}
else {
$this->add_region($key, $region);
}
}
}
}
// --------------------------------------------------------------------
/**
* Dynamically add region to the currently set master
*
* @access public
* @param string Name to identify the region
* @param array Optional array with region defaults
* @return void
*/
function add_region($name, $props = array())
{
if ( ! is_array($props))
{
$props = array();
}
if ( ! isset($this->regions[$name]))
{
$this->regions[$name] = $props;
}
else
{
show_error('The "'. $name .'" region has already been defined.');
}
}
// --------------------------------------------------------------------
/**
* Empty a region's content
*
* @access public
* @param string Name to identify the region
* @return void
*/
function empty_region($name)
{
if (isset($this->regions[$name]['content']))
{
$this->regions[$name]['content'] = array();
}
else
{
show_error('The "'. $name .'" region is undefined.');
}
}
// --------------------------------------------------------------------
/**
* Set parser
*
* @access public
* @param string name of parser class to load and use for parsing methods
* @return void
*/
function set_parser($parser, $method = NULL)
{
$this->parser = $parser;
$this->CI->load->library($parser);
if ($method)
{
$this->set_parser_method($method);
}
}
// --------------------------------------------------------------------
/**
* Set parser method
*
* @access public
* @param string name of parser class member function to call when parsing
* @return void
*/
function set_parser_method($method)
{
$this->parser_method = $method;
}
// --------------------------------------------------------------------
/**
* Write contents to a region
*
* @access public
* @param string region to write to
* @param string what to write
* @param boolean FALSE to append to region, TRUE to overwrite region
* @return void
*/
function write($region, $content, $overwrite = FALSE)
{
if (isset($this->regions[$region]))
{
if ($overwrite === TRUE) // Should we append the content or overwrite it
{
$this->regions[$region]['content'] = array($content);
} else {
$this->regions[$region]['content'][] = $content;
}
}
// Regions MUST be defined
else
{
show_error("Cannot write to the '{$region}' region. The region is undefined.");
}
}
// --------------------------------------------------------------------
/**
* Write content from a View to a region. 'Views within views'
*
* @access public
* @param string region to write to
* @param string view file to use
* @param array variables to pass into view
* @param boolean FALSE to append to region, TRUE to overwrite region
* @return void
*/
function write_view($region, $view, $data = NULL, $overwrite = FALSE)
{
$args = func_get_args();
// Get rid of non-views
unset($args[0], $args[2], $args[3]);
// Do we have more view suggestions?
if (count($args) > 1)
{
foreach ($args as $suggestion)
{
if (file_exists(APPPATH .'views/'. $suggestion . EXT) or file_exists(APPPATH .'views/'. $suggestion))
{
// Just change the $view arg so the rest of our method works as normal
$view = $suggestion;
break;
}
}
}
$content = $this->CI->load->view($view, $data, TRUE);
$this->write($region, $content, $overwrite);
}
// --------------------------------------------------------------------
/**
* Parse content from a View to a region with the Parser Class
*
* @access public
* @param string region to write to
* @param string view file to parse
* @param array variables to pass into view for parsing
* @param boolean FALSE to append to region, TRUE to overwrite region
* @return void
*/
function parse_view($region, $view, $data = NULL, $overwrite = FALSE)
{
$this->CI->load->library('parser');
$args = func_get_args();
// Get rid of non-views
unset($args[0], $args[2], $args[3]);
// Do we have more view suggestions?
if (count($args) > 1)
{
foreach ($args as $suggestion)
{
if (file_exists(APPPATH .'views/'. $suggestion . EXT) or file_exists(APPPATH .'views/'. $suggestion))
{
// Just change the $view arg so the rest of our method works as normal
$view = $suggestion;
break;
}
}
}
$content = $this->CI->{$this->parser}->{$this->parser_method}($view, $data, TRUE);
$this->write($region, $content, $overwrite);
}
// --------------------------------------------------------------------
/**
* Dynamically include javascript in the master
*
* NOTE: This function does NOT check for existence of .js file
*
* @access public
* @param string script to import or embed
* @param string 'import' to load external file or 'embed' to add as-is
* @param boolean TRUE to use 'defer' attribute, FALSE to exclude it
* @return TRUE on success, FALSE otherwise
*/
function add_js($script, $type = 'import', $defer = FALSE)
{
$success = TRUE;
$js = NULL;
$js_folder = $this->CI->config->item('js_folder');
$this->CI->load->helper('url');
switch ($type)
{
case 'import':
$filepath = base_url($js_folder . '/' . $script . '.js');
$js = "\t