2011-12-27 16:47:27 -05:00
|
|
|
<?php
|
2011-12-28 19:33:41 -05:00
|
|
|
|
2011-12-27 16:47:27 -05:00
|
|
|
/**
|
|
|
|
* Class for building pages
|
|
|
|
*
|
|
|
|
* All methods are chainable, with the exception of the constructor,
|
|
|
|
* build_header(), build_footer(), build_page() and _headers() methods.
|
|
|
|
*/
|
2011-12-28 19:33:41 -05:00
|
|
|
class Page
|
2011-12-27 16:47:27 -05:00
|
|
|
{
|
2011-12-28 19:33:41 -05:00
|
|
|
private $meta, $head_js, $foot_js, $css, $title, $head_tags, $body_class, $body_id, $base;
|
2011-12-27 16:47:27 -05:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->meta = "";
|
|
|
|
$this->head_js = "";
|
|
|
|
$this->foot_js = "";
|
|
|
|
$this->css = "";
|
|
|
|
$this->title = "";
|
|
|
|
$this->head_tags = "";
|
|
|
|
$this->body_class = "";
|
|
|
|
$this->body_id = "";
|
|
|
|
$this->base = "";
|
2011-12-28 19:33:41 -05:00
|
|
|
|
2011-12-29 10:31:04 -05:00
|
|
|
$this->mm = miniMVC::get_instance();
|
2011-12-27 16:47:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets server headers and doctype
|
|
|
|
*
|
|
|
|
* Also sets page mime type, based on if sent as
|
|
|
|
* html or xhtml, and what the target browser
|
|
|
|
* supports
|
|
|
|
*
|
|
|
|
* @param bool $xhtml
|
|
|
|
* @param bool $html5
|
|
|
|
* @return Page
|
|
|
|
*/
|
|
|
|
private function _headers($xhtml, $html5)
|
|
|
|
{
|
2011-12-28 19:33:41 -05:00
|
|
|
$this->mm->output->set_header("Cache-Control", "must-revalidate, public");
|
2011-12-27 16:47:27 -05:00
|
|
|
$mime = "";
|
|
|
|
|
|
|
|
//Variable for accept keyword
|
2011-12-29 10:31:04 -05:00
|
|
|
$accept = ( ! empty($_SERVER['HTTP_ACCEPT'])) ? $_SERVER['HTTP_ACCEPT'] : "";
|
2011-12-27 16:47:27 -05:00
|
|
|
|
|
|
|
//Predefine doctype
|
|
|
|
$doctype_string = ($html5 == TRUE) ? '<!DOCTYPE html>' : '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">';
|
|
|
|
|
|
|
|
//Predefine charset
|
|
|
|
$charset = "UTF-8";
|
|
|
|
|
|
|
|
//If xhtml flag is false, set html5 header
|
|
|
|
if ($xhtml == TRUE)
|
|
|
|
{
|
|
|
|
//Check that the user agent accepts application/xhtml+xml, or if it's the W3C Validator
|
|
|
|
if (stristr($accept, "application/xhtml+xml") || stristr($_SERVER["HTTP_USER_AGENT"], "W3C_Validator"))
|
|
|
|
{
|
|
|
|
$mime = "application/xhtml+xml";
|
|
|
|
}
|
|
|
|
//Or if it supports application/xml
|
|
|
|
else if (stristr($accept, "application/xml"))
|
|
|
|
{
|
|
|
|
$mime = "application/xml";
|
|
|
|
}
|
|
|
|
//Or if it supports text/xml
|
|
|
|
else if (stristr($accept, "text/xml"))
|
|
|
|
{
|
|
|
|
$mime = "text/xml";
|
|
|
|
}
|
|
|
|
else //Otherwise, it's tag soup
|
|
|
|
{
|
|
|
|
$mime = "text/html";
|
|
|
|
|
|
|
|
if ($html5 == FALSE) //If it's not HTML5, it's HTML4
|
|
|
|
{
|
|
|
|
$doctype_string = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$mime = "text/html";
|
|
|
|
|
|
|
|
if ($html5 == FALSE)
|
|
|
|
{
|
|
|
|
$doctype_string = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the doctype according to the mime type which was determined
|
|
|
|
if ($mime == "application/xhtml+xml" || $mime == "text/xml" || $mime == "application/xml")
|
|
|
|
{
|
|
|
|
if ($html5 == TRUE)
|
|
|
|
{
|
|
|
|
$doctype_string = '';
|
|
|
|
}
|
|
|
|
|
2011-12-29 11:24:10 -05:00
|
|
|
$doctype_string = "<?xml version='1.0' encoding='$charset' ?>"
|
2011-12-27 16:47:27 -05:00
|
|
|
. $doctype_string
|
2011-12-29 11:24:10 -05:00
|
|
|
. "<html xmlns='http://www.w3.org/1999/xhtml'" . " xml:lang='en'>";
|
2011-12-27 16:47:27 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-29 10:31:04 -05:00
|
|
|
$doctype_string .= "<html lang='en'>";
|
2011-12-27 16:47:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// finally, output the mime type and prolog type
|
2011-12-29 10:31:04 -05:00
|
|
|
$this->mm->output->set_header("Content-Type", "{$mime};charset={$charset}");
|
2011-12-28 19:33:41 -05:00
|
|
|
$this->mm->output->set_header("X-UA-Compatible", "chrome=1, IE=edge");
|
|
|
|
$this->mm->output->set_output($doctype_string);
|
2011-12-27 16:47:27 -05:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set Meta
|
|
|
|
*
|
|
|
|
* Sets meta tags, with codeigniter native meta tag helper
|
|
|
|
*
|
|
|
|
* @param array $meta
|
|
|
|
* @return Page
|
|
|
|
*/
|
|
|
|
public function set_meta($meta)
|
|
|
|
{
|
|
|
|
$this->meta .= $this->_meta($meta);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets minified javascript group in header
|
|
|
|
* @param string $group
|
|
|
|
* @param bool $debug
|
|
|
|
* @return Page
|
|
|
|
*/
|
|
|
|
public function set_head_js_group($group, $debug = FALSE)
|
|
|
|
{
|
|
|
|
if ($group === FALSE)
|
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2011-12-29 10:31:04 -05:00
|
|
|
$file = SCRIPT_PATH . $group;
|
2011-12-27 16:47:27 -05:00
|
|
|
$file .= ($debug == TRUE) ? "/debug/1" : "";
|
|
|
|
$this->head_js .= $this->script_tag($file, FALSE);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a minified css group
|
|
|
|
* @param string $group
|
|
|
|
* @return Page
|
|
|
|
*/
|
|
|
|
public function set_css_group($group)
|
|
|
|
{
|
|
|
|
$link = array(
|
2011-12-29 10:31:04 -05:00
|
|
|
'href' => STYLE_PATH . $group,
|
2011-12-27 16:47:27 -05:00
|
|
|
'rel' => 'stylesheet',
|
|
|
|
'type' => 'text/css'
|
|
|
|
);
|
2011-12-28 19:33:41 -05:00
|
|
|
$this->css .= $this->_link_tag($link);
|
2011-12-27 16:47:27 -05:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a minified javascript group for the page footer
|
|
|
|
* @param string $group
|
|
|
|
* @return Page
|
|
|
|
*/
|
|
|
|
public function set_foot_js_group($group, $debug = FALSE)
|
|
|
|
{
|
2011-12-29 10:31:04 -05:00
|
|
|
$file = SCRIPT_PATH . $group;
|
2011-12-27 16:47:27 -05:00
|
|
|
$file .= ($debug == TRUE) ? "/debug/1" : "";
|
|
|
|
$this->foot_js .= $this->script_tag($file, FALSE);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets html title string
|
|
|
|
* @param string $title
|
|
|
|
* @return Page
|
|
|
|
*/
|
|
|
|
public function set_title($title = "")
|
|
|
|
{
|
2011-12-29 10:31:04 -05:00
|
|
|
$title = ($title == "") ? DEFAULT_TITLE : $title;
|
2011-12-27 16:47:27 -05:00
|
|
|
|
|
|
|
$this->title = $title;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets custom body class
|
|
|
|
* @param string $class
|
|
|
|
* @return Page
|
|
|
|
*/
|
|
|
|
public function set_body_class($class = "")
|
|
|
|
{
|
|
|
|
$this->body_class = $class;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets custom body id
|
|
|
|
* @param string $id
|
|
|
|
* @return Page
|
|
|
|
*/
|
|
|
|
public function set_body_id($id = "")
|
|
|
|
{
|
|
|
|
$this->body_id = $id;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets custom base href
|
|
|
|
* @param string href
|
|
|
|
* @return Page
|
|
|
|
*/
|
|
|
|
public function set_base($href)
|
|
|
|
{
|
|
|
|
$this->base = $href;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets custom css tags
|
|
|
|
* @param string $name
|
|
|
|
* @param string $media
|
|
|
|
* @return Page
|
|
|
|
*/
|
|
|
|
public function set_css_tag($name, $domain = TRUE, $media = "all")
|
|
|
|
{
|
2011-12-28 19:33:41 -05:00
|
|
|
$path = CONTENT_DOMAIN;
|
2011-12-27 16:47:27 -05:00
|
|
|
$css_file = $path . "/css/" . $name . ".css";
|
|
|
|
|
|
|
|
if ($domain == FALSE)
|
2011-12-28 19:33:41 -05:00
|
|
|
{
|
2011-12-27 16:47:27 -05:00
|
|
|
$css_file = $name;
|
2011-12-28 19:33:41 -05:00
|
|
|
}
|
2011-12-27 16:47:27 -05:00
|
|
|
|
2011-12-28 19:33:41 -05:00
|
|
|
$this->css_tags .= $this->_link_tag(array(
|
|
|
|
'rel' => 'stylesheet',
|
|
|
|
'type' => 'text/css',
|
|
|
|
'media' => $media,
|
|
|
|
'href' => $css_file,
|
|
|
|
));
|
2011-12-27 16:47:27 -05:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a custom tag in the header
|
|
|
|
* @param string $tag
|
|
|
|
* @return Page
|
|
|
|
*/
|
|
|
|
public function set_head_tag($tag)
|
|
|
|
{
|
2011-12-29 11:24:10 -05:00
|
|
|
$this->head_tags .= $tag;
|
2011-12-27 16:47:27 -05:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets custom page header
|
|
|
|
* @param mixed $xhtml
|
|
|
|
* @param bool $html5
|
|
|
|
* @param bool $fbml
|
2011-12-29 10:31:04 -05:00
|
|
|
* @return Page
|
2011-12-27 16:47:27 -05:00
|
|
|
*/
|
|
|
|
public function build_header($xhtml = FALSE, $html5 = TRUE)
|
|
|
|
{
|
|
|
|
$data = array();
|
|
|
|
|
|
|
|
//Set Meta Tags
|
|
|
|
$this->meta = ($html5 == TRUE)
|
|
|
|
? '<meta charset="utf-8" />'. $this->meta
|
2011-12-28 19:33:41 -05:00
|
|
|
: $this->_meta(array(
|
|
|
|
'http-equiv' => 'Content-Type',
|
|
|
|
'content' => 'text/html; charset=utf-8',
|
|
|
|
)) . $this->meta;
|
2011-12-27 16:47:27 -05:00
|
|
|
|
|
|
|
$data['meta'] = $this->meta;
|
|
|
|
|
|
|
|
//Set CSS
|
2011-12-28 19:33:41 -05:00
|
|
|
if ($this->css !== "")
|
2011-12-27 16:47:27 -05:00
|
|
|
{
|
|
|
|
$data['css'] = $this->css;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Set default CSS group
|
2011-12-29 10:31:04 -05:00
|
|
|
$this->set_css_group(DEFAULT_CSS_GROUP);
|
2011-12-27 16:47:27 -05:00
|
|
|
$data['css'] = $this->css;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Set head javascript
|
2011-12-28 19:33:41 -05:00
|
|
|
$data['head_js'] = ( ! empty($this->head_js)) ? $this->head_js : "";
|
2011-12-27 16:47:27 -05:00
|
|
|
|
|
|
|
//Set Page Title
|
2011-12-29 10:31:04 -05:00
|
|
|
$data['title'] = ($this->title !== '') ? $this->title : DEFAULT_TITLE;
|
2011-12-27 16:47:27 -05:00
|
|
|
|
|
|
|
//Set Body Class
|
|
|
|
$data['body_class'] = $this->body_class;
|
|
|
|
|
|
|
|
//Set Body Id
|
|
|
|
$data['body_id'] = $this->body_id;
|
|
|
|
|
|
|
|
//Set Base HREF
|
|
|
|
$data['base'] = $this->base;
|
|
|
|
|
|
|
|
//Set individual head tags
|
|
|
|
$data['head_tags'] = $this->head_tags;
|
|
|
|
|
|
|
|
//Set Server Headers and Doctype
|
|
|
|
$this->_headers($xhtml, $html5);
|
|
|
|
|
|
|
|
//Output Header
|
2011-12-28 19:33:41 -05:00
|
|
|
$this->mm->load_view('header', $data);
|
2011-12-27 16:47:27 -05:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds common footer with any additional js
|
|
|
|
*/
|
|
|
|
public function build_footer()
|
|
|
|
{
|
|
|
|
$data = array();
|
|
|
|
|
|
|
|
$data['foot_js'] = ($this->foot_js != "") ? $this->foot_js : '';
|
|
|
|
|
2011-12-29 10:31:04 -05:00
|
|
|
$this->mm->load_view('footer', $data);
|
2011-12-27 16:47:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Script Tag
|
|
|
|
*
|
|
|
|
* Helper function for making script tags
|
|
|
|
*
|
|
|
|
* @param string $js
|
|
|
|
* @param bool $domain
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function script_tag($js, $domain = TRUE)
|
|
|
|
{
|
2011-12-28 19:33:41 -05:00
|
|
|
$path = CONTENT_DOMAIN;
|
2011-12-27 16:47:27 -05:00
|
|
|
$js_file = $path . "/js/" . $js . ".js";
|
|
|
|
|
|
|
|
if ($domain == FALSE)
|
|
|
|
$js_file = $js;
|
|
|
|
|
|
|
|
$tag = '<script src="' . $js_file . '"></script>';
|
|
|
|
|
|
|
|
return $tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set Message
|
|
|
|
*
|
|
|
|
* Adds a message to the page
|
|
|
|
* @param string $type
|
|
|
|
* @param string $message
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-12-30 16:41:25 -05:00
|
|
|
public function set_message($type, $message, $return=FALSE)
|
2011-12-27 16:47:27 -05:00
|
|
|
{
|
|
|
|
$data['stat_class'] = $type;
|
|
|
|
$data['message'] = $message;
|
|
|
|
|
2011-12-30 16:41:25 -05:00
|
|
|
return $this->mm->load_view('message', $data, $return);
|
2011-12-27 16:47:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Redirect 303
|
|
|
|
*
|
|
|
|
* Shortcut function for 303 redirect
|
|
|
|
* @param string $url
|
|
|
|
*/
|
|
|
|
function redirect_303($url)
|
|
|
|
{
|
2011-12-28 19:33:41 -05:00
|
|
|
$this->mm->output->set_header("HTTP/1.1 303 See Other");
|
|
|
|
$this->mm->output->set_header("Location:" . $url);
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2011-12-29 10:31:04 -05:00
|
|
|
* Render
|
2011-12-28 19:33:41 -05:00
|
|
|
*
|
|
|
|
* Shortcut function for building a page
|
|
|
|
* @param string $view
|
|
|
|
* @param array $data
|
|
|
|
*/
|
2011-12-29 10:31:04 -05:00
|
|
|
function render($view, $data=array())
|
2011-12-28 19:33:41 -05:00
|
|
|
{
|
|
|
|
$this->build_header();
|
2011-12-29 11:24:10 -05:00
|
|
|
$this->mm->load_view($view, $data);
|
2011-12-28 19:33:41 -05:00
|
|
|
$this->build_footer();
|
2011-12-27 16:47:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2011-12-29 16:57:28 -05:00
|
|
|
/**
|
|
|
|
* Output String
|
|
|
|
*
|
|
|
|
* Similar to render(), this is a shortcut
|
|
|
|
* to output a string in the body of the
|
|
|
|
* page.
|
|
|
|
* @param string $string
|
|
|
|
*/
|
|
|
|
function output_string($string)
|
|
|
|
{
|
|
|
|
$this->build_header();
|
|
|
|
$this->mm->output->append_output($string);
|
|
|
|
$this->build_footer();
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
2012-01-03 21:54:09 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Private helper function to generate meta tags
|
|
|
|
*
|
|
|
|
* @param array $params
|
|
|
|
* @return string
|
|
|
|
*/
|
2011-12-27 16:47:27 -05:00
|
|
|
private function _meta($params)
|
|
|
|
{
|
|
|
|
$string = "<meta ";
|
|
|
|
|
|
|
|
foreach($params as $k => $v)
|
|
|
|
{
|
|
|
|
$string .= $k.'="'.$v.'" ';
|
|
|
|
}
|
|
|
|
|
|
|
|
$string .= " />";
|
|
|
|
|
|
|
|
return $string;
|
|
|
|
}
|
2012-01-03 21:54:09 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Private helper function to generate link tags
|
|
|
|
*
|
|
|
|
* @param array $params
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function _link_tag($params)
|
2011-12-28 19:33:41 -05:00
|
|
|
{
|
|
|
|
$string = "<link ";
|
|
|
|
|
|
|
|
foreach($params as $k => $v)
|
|
|
|
{
|
|
|
|
$string .= $k . '="'.$v.'" ';
|
|
|
|
}
|
|
|
|
|
|
|
|
$string .= " />";
|
|
|
|
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// End of page.php
|