miniMVC/sys/core/page.php

541 lines
10 KiB
PHP
Raw Normal View History

2012-01-13 11:58:06 -05:00
<?php
/**
* MiniMVC
*
* Convention-based micro-framework for PHP
*
* @package miniMVC
2012-01-13 11:58:06 -05:00
* @author Timothy J. Warren
* @copyright Copyright (c) 2011 - 2012
* @link https://github.com/timw4mail/miniMVC
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
/**
* Class for building pages
*
* All methods are chainable, with the exception of the constructor,
* build_header(), build_footer(), and _headers() methods.
*
* @package miniMVC
* @subpackage System
2012-01-13 11:58:06 -05:00
*/
2012-05-16 08:29:52 -04:00
class MM_Page extends MM_Output {
/**
* Meta tags
*
* @var string
*/
private $meta;
/**
* JS tags for the header
*
* @var string
*/
private $head_js;
/**
* JS tags for the footer
*
* @var string
*/
private $foot_js;
/**
* CSS tags for the page
*
* @var string
*/
private $css;
2012-01-13 11:58:06 -05:00
/**
* Page title
*
* @var string
*/
private $title;
/**
* Additional header tags
*
* @var string
*/
private $head_tags;
/**
* Class(es) to apply to the main body tag
*
* @var string
*/
private $body_class;
/**
* Id to apply to the body tag
*
* @var string
*/
private $body_id;
/**
* Base tag
*
* @var string
*/
private $base;
/**
* Set up the page class
*
2012-04-27 16:28:25 -04:00
* @param object
* @return void
*/
2012-04-27 16:28:25 -04:00
public function __construct(&$controller)
2012-01-13 11:58:06 -05:00
{
$this->meta = "";
$this->head_js = "";
$this->foot_js = "";
$this->css = "";
$this->title = "";
$this->head_tags = "";
$this->body_class = "";
$this->body_id = "";
$this->base = "";
2012-04-27 16:28:25 -04:00
$this->mm = $controller;
2012-01-13 11:58:06 -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 $html5
* @return Page
*/
private function _headers($html5)
2012-01-13 11:58:06 -05:00
{
2012-05-16 08:29:52 -04:00
$this->set_header("Cache-Control", "must-revalidate, public");
2012-01-13 11:58:06 -05:00
$mime = "";
//Variable for accept keyword
$accept = ( ! empty($_SERVER['HTTP_ACCEPT'])) ? $_SERVER['HTTP_ACCEPT'] : "";
//Predefine doctype
$doctype_string = ($html5 == TRUE) ? '<!DOCTYPE html>' : '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">';
//Predefine charset
$charset = "UTF-8";
$mime = "text/html";
2012-01-13 11:58:06 -05:00
if ($html5 == FALSE)
2012-01-13 11:58:06 -05:00
{
$doctype_string = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">';
2012-01-13 11:58:06 -05:00
}
$doctype_string .= "<html lang='en'>";
2012-01-13 11:58:06 -05:00
// finally, output the mime type and prolog type
2012-05-16 08:29:52 -04:00
$this->set_header("Content-Type", "{$mime};charset={$charset}");
$this->set_header("X-UA-Compatible", "chrome=1, IE=edge");
$this->set_output($doctype_string);
2012-01-13 11:58:06 -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
*
2012-01-13 11:58:06 -05:00
* @param string $group
* @param bool $debug
* @return Page
*/
public function set_head_js_group($group, $debug = FALSE)
{
if ($group === FALSE)
{
return $this;
}
$file = SCRIPT_PATH . $group;
$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)
{
2012-05-14 14:35:57 -04:00
$link = [
2012-01-13 11:58:06 -05:00
'href' => STYLE_PATH . $group,
'rel' => 'stylesheet',
'type' => 'text/css'
2012-05-14 14:35:57 -04:00
];
2012-01-13 11:58:06 -05:00
$this->css .= $this->_link_tag($link);
return $this;
}
// --------------------------------------------------------------------------
/**
* Sets a minified javascript group for the page footer
*
2012-01-13 11:58:06 -05:00
* @param string $group
* @param bool $debug
2012-01-13 11:58:06 -05:00
* @return Page
*/
public function set_foot_js_group($group, $debug = FALSE)
{
$file = SCRIPT_PATH . $group;
$file .= ($debug == TRUE) ? "/debug/1" : "";
$this->foot_js .= $this->script_tag($file, FALSE);
return $this;
}
// --------------------------------------------------------------------------
/**
* Sets html title string
*
2012-01-13 11:58:06 -05:00
* @param string $title
* @return Page
*/
public function set_title($title = "")
{
$title = ($title == "") ? DEFAULT_TITLE : $title;
$this->title = $title;
return $this;
}
// --------------------------------------------------------------------------
/**
* Sets custom body class
*
2012-01-13 11:58:06 -05:00
* @param string $class
* @return Page
*/
public function set_body_class($class = "")
{
$this->body_class = $class;
return $this;
}
// --------------------------------------------------------------------------
/**
* Sets custom body id
*
2012-01-13 11:58:06 -05:00
* @param string $id
* @return Page
*/
public function set_body_id($id = "")
{
$this->body_id = $id;
return $this;
}
// --------------------------------------------------------------------------
/**
* Sets custom base href
*
2012-01-13 11:58:06 -05:00
* @param string href
* @return Page
*/
public function set_base($href)
{
$this->base = $href;
return $this;
}
// --------------------------------------------------------------------------
/**
* Sets custom css tags
*
2012-01-13 11:58:06 -05:00
* @param string $name
* @param bool $domain
2012-01-13 11:58:06 -05:00
* @param string $media
* @return Page
*/
public function set_css_tag($name, $domain = TRUE, $media = "all")
{
$path = CONTENT_DOMAIN;
2012-05-16 08:29:52 -04:00
$css_file = "{$path}/css/{$name}.css";
2012-01-13 11:58:06 -05:00
if ($domain == FALSE)
{
$css_file = $name;
}
2012-05-14 14:35:57 -04:00
$this->css_tags .= $this->_link_tag([
2012-01-13 11:58:06 -05:00
'rel' => 'stylesheet',
'type' => 'text/css',
'media' => $media,
'href' => $css_file,
2012-05-14 14:35:57 -04:00
]);
2012-01-13 11:58:06 -05:00
return $this;
}
// --------------------------------------------------------------------------
/**
* Sets a custom tag in the header
*
2012-01-13 11:58:06 -05:00
* @param string $tag
* @return Page
*/
public function set_head_tag($tag)
{
$this->head_tags .= $tag;
return $this;
}
// --------------------------------------------------------------------------
/**
* Sets custom page header
*
2012-01-13 11:58:06 -05:00
* @param bool $html5
* @return Page
*/
public function build_header($html5 = TRUE)
2012-01-13 11:58:06 -05:00
{
2012-05-14 14:35:57 -04:00
$data = [];
2012-01-13 11:58:06 -05:00
//Set Meta Tags
$this->meta = ($html5 == TRUE)
? '<meta charset="utf-8" />'. $this->meta
2012-05-14 14:35:57 -04:00
: $this->_meta([
2012-01-13 11:58:06 -05:00
'http-equiv' => 'Content-Type',
'content' => 'text/html; charset=utf-8',
2012-05-14 14:35:57 -04:00
]) . $this->meta;
2012-01-13 11:58:06 -05:00
$data['meta'] = $this->meta;
//Set CSS
if ($this->css !== "")
{
$data['css'] = $this->css;
}
else
{
//Set default CSS group
$this->set_css_group(DEFAULT_CSS_GROUP);
$data['css'] = $this->css;
}
//Set head javascript
$data['head_js'] = ( ! empty($this->head_js)) ? $this->head_js : "";
//Set Page Title
$data['title'] = ($this->title !== '') ? $this->title : DEFAULT_TITLE;
//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($html5);
2012-01-13 11:58:06 -05:00
//Output Header
$this->mm->load_view('header', $data);
return $this;
}
// --------------------------------------------------------------------------
/**
* Builds common footer with any additional js
*/
public function build_footer()
{
2012-05-14 14:35:57 -04:00
$data = [];
2012-01-13 11:58:06 -05:00
$data['foot_js'] = ($this->foot_js != "") ? $this->foot_js : '';
$this->mm->load_view('footer', $data);
}
// --------------------------------------------------------------------------
/**
* Script Tag
*
* Helper function for making script tags
*
* @param string $js
* @param bool $domain
* @return string
*/
private function script_tag($js, $domain = TRUE)
{
$path = CONTENT_DOMAIN;
2012-05-16 08:29:52 -04:00
$js_file = "{$path}/js/{$js}.js";
2012-01-13 11:58:06 -05:00
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
* @param bool $return
2012-01-13 11:58:06 -05:00
* @return void
*/
public function set_message($type, $message, $return=FALSE)
{
$data['stat_class'] = $type;
$data['message'] = $message;
return $this->mm->load_view('message', $data, $return);
}
// --------------------------------------------------------------------------
/**
* Redirect 303
*
* Shortcut function for 303 redirect
* @param string $url
*/
function redirect_303($url)
{
2012-05-16 08:29:52 -04:00
$this->set_header("HTTP/1.1 303 See Other");
$this->set_header("Location:" . $url);
2012-01-13 11:58:06 -05:00
}
// --------------------------------------------------------------------------
/**
* Render
*
* Shortcut function for building a page
* @param string $view
* @param array $data
*/
2012-05-14 14:35:57 -04:00
function render($view, $data=[])
2012-01-13 11:58:06 -05:00
{
$this->build_header();
$this->mm->load_view($view, $data);
$this->build_footer();
}
// --------------------------------------------------------------------------
/**
* 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();
2012-05-16 08:29:52 -04:00
$this->append_output($string);
2012-01-13 11:58:06 -05:00
$this->build_footer();
}
// --------------------------------------------------------------------------
2012-05-15 16:53:10 -04:00
/**
* Private helper function to generate meta tags
*
* @param array $params
* @return string
*/
2012-01-13 11:58:06 -05:00
private function _meta($params)
{
$string = "<meta ";
2012-05-14 14:35:57 -04:00
foreach($params as $k => &$v)
2012-01-13 11:58:06 -05:00
{
$string .= $k.'="'.$v.'" ';
}
$string .= " />";
return $string;
}
// --------------------------------------------------------------------------
2012-01-13 11:58:06 -05:00
2012-05-15 16:53:10 -04:00
/**
* Private helper function to generate link tags
*
* @param array $params
* @return string
*/
private function _link_tag($params)
2012-01-13 11:58:06 -05:00
{
$string = "<link ";
2012-05-14 14:35:57 -04:00
foreach($params as $k => &$v)
2012-01-13 11:58:06 -05:00
{
$string .= $k . '="'.$v.'" ';
}
2012-05-03 13:26:09 -04:00
$string .= "/>";
2012-01-13 11:58:06 -05:00
return $string;
}
}
2012-05-17 16:22:39 -04:00
2012-01-13 11:58:06 -05:00
// End of page.php