Simplify framework classes, add some category functionality

This commit is contained in:
Timothy Warren 2012-06-14 16:33:16 -04:00
parent 054ea4f908
commit 4125c033fc
22 changed files with 595 additions and 512 deletions

View File

@ -22,16 +22,6 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/*
|--------------------------------------------------------------------------
| Display Debug backtrace
|--------------------------------------------------------------------------
|
| If set to TRUE, a backtrace will be displayed along with php errors.
|
*/
define('SHOW_DEBUG_BACKTRACE', TRUE);
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Base Url | Base Url

View File

@ -35,6 +35,9 @@ return array(
'default_module' => 'meta', 'default_module' => 'meta',
'genre' => 'meta/genre/index', 'genre' => 'meta/genre/index',
'genre/add' => 'meta/genre/add', 'genre/add' => 'meta/genre/add',
'category' => 'meta/category/index',
'category/add' => 'meta/category/add',
'category/detail' => 'meta/category/detail',
'404_route' => '', '404_route' => '',
); );

View File

@ -0,0 +1,93 @@
<?php
/**
* meta
*
* Hierarchial data tool
*
* @package meta
* @author Timothy J. Warren
* @copyright Copyright (c) 2012
* @link https://github.com/aviat4ion/meta
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
/**
* Category controller
*
* @package meta
*/
class Category extends miniMVC\Controller {
/**
* Initialize the Controller
*/
public function __construct()
{
parent::__construct();
$this->load_model('meta\model');
$this->page->build_header();
}
/**
* Default controller method
*/
public function index()
{
$id = (int) miniMVC\get_last_segment();
if ($id === 0)
{
return miniMVC\show_404();
}
$this->detail();
}
/**
* Adds a new category
*/
public function add()
{
// Strip away tags for the sake of security
$name = strip_tags($_POST['category']);
$id = (int) $_POST['genre_id'];
// Make sure the name doesn't already exist. If it does, show an error.
$res = $this->model->add_category($name, $id);
if ($res === TRUE)
{
$this->page->set_message('success', 'Added new category');
}
else
{
$this->page->set_message('error', 'Category already exists for this genre');
}
// Render the basic page
$this->index();
}
/**
* Returns the sections / editing options for a category
*/
public function detail()
{
$id = (int) miniMVC\get_last_segment();
$data = array(
'category' => $this->model->get_category_by_id($id),
'sections' => $this->model->get_sections($id),
'category_id' => $id
);
$this->load_view('category_detail', $data);
$this->page->build_footer();
}
}
// End of genre.php

View File

@ -0,0 +1,30 @@
<?php
/**
* meta
*
* Hierarchial data tool
*
* @package meta
* @author Timothy J. Warren
* @copyright Copyright (c) 2012
* @link https://github.com/aviat4ion/meta
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
/**
* Section Controller
*/
class Section extends \miniMVC\Controller {
/**
* Constructor
*/
public function __construct()
{
}
}
// End of section.php

View File

@ -50,6 +50,9 @@ class Welcome extends miniMVC\Controller {
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/**
* Authenticate a user
*/
public function login() public function login()
{ {

View File

@ -138,12 +138,30 @@ class Model extends \miniMVC\Model {
* *
* @param string * @param string
* @param int * @param int
* @return bool
*/ */
public function add_category($cat, $genre_id) public function add_category($cat, $genre_id)
{ {
$this->db->set('category', $cat) // Check for duplicates
->set('genre_id', $genre_id) $query = $this->db->from('category')
->insert('category'); ->where('genre_id', $genre_id)
->where('category', $cat)
->get();
// Fetch the data as a workaround
// for databases that do not support
// grabbing result counts (SQLite / Firebird)
$array = $query->fetchAll();
if (count($array) === 0)
{
$this->db->set('category', $cat)
->set('genre_id', $genre_id)
->insert('category');
return TRUE;
}
return FALSE;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -253,6 +271,8 @@ class Model extends \miniMVC\Model {
return $genres; return $genres;
} }
// --------------------------------------------------------------------------
/** /**
* Gets the name of the genre from its id * Gets the name of the genre from its id
* *
@ -273,6 +293,46 @@ class Model extends \miniMVC\Model {
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/**
* Gets the name of the category from its id
*
* @param int
* @return string
*/
public function get_category_by_id($id)
{
$query = $this->db->select('category')
->from('category')
->where('id', (int) $id)
->get();
$row = $query->fetch(\PDO::FETCH_ASSOC);
return $row['category'];
}
// --------------------------------------------------------------------------
/**
* Gets the name of the section from its id
*
* @param int
* @return string
*/
public function get_section_by_id($id)
{
$query = $this->db->select('section')
->from('section')
->where('id', (int) $id)
->get();
$row = $query->fetch(\PDO::FETCH_ASSOC);
return $row['section'];
}
// --------------------------------------------------------------------------
/** /**
* Get the categories for the specified genre * Get the categories for the specified genre
* *

View File

@ -22,6 +22,9 @@ namespace meta;
*/ */
class User_model extends \miniMVC\Model { class User_model extends \miniMVC\Model {
/**
* Initialize the User model
*/
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();

View File

@ -0,0 +1,23 @@
<h1>meta</h1>
<h3><?= $category ?></h3>
<h4>Category Sections</h4>
<ul class="list">
<?php foreach($sections as $id => $section): ?>
<li><a href="<?= miniMVC\site_url("section/detail/{$id}") ?>"><?= $section ?></a></li>
<?php endforeach ?>
</ul>
<form action="<?= miniMVC\site_url("section/add") ?>" method="post">
<fieldset>
<lengend>Add Section</lengend>
<dl>
<!-- Weird tag wrapping is intentional for display: inline-block -->
<dt><label for="section">Section name:</label></dt><dd>
<input type="text" name="section" id="section" /></dd>
<dt><input type="hidden" name="category_id" value="<?= $category_id ?>" /></dt><dd>
<button type="submit">Add Section</button></dd>
</dl>
</fieldset>
</form>

View File

@ -4,7 +4,7 @@
<h4>Genre Categories</h4> <h4>Genre Categories</h4>
<ul class="list"> <ul class="list">
<?php foreach($categories as $id => $cat): ?> <?php foreach($categories as $id => $cat): ?>
<li><a href="<?= miniMVC\site_url("category/{$id}") ?>"><?= $cat ?></a></li> <li><a href="<?= miniMVC\site_url("category/detail/{$id}") ?>"><?= $cat ?></a></li>
<?php endforeach ?> <?php endforeach ?>
</ul> </ul>

View File

@ -0,0 +1,15 @@
<h1>meta</h1>
<h3>Login</h3>
<form action="<?= miniMVC\site_url('welcome/login') ?>" method="post">
<dl>
<dt><label for="user">Username:</label></dt>
<dd><input type="text" id="user" name="user" /></dd>
<dt><label for="pass">Password:</label></dt>
<dd><input type="password" id="pass" name="pass" /></dd>
<dt>&nbsp;</dt>
<dd><button type="submit">Login</button></dd>
</dl>
</form>

View File

@ -8,6 +8,14 @@ html, body {
margin: 0 auto; margin: 0 auto;
} }
a {
text-decoration:none;
}
a:hover {
text-decoration:underline;
}
/* form styles */ /* form styles */
form dt, form dd { form dt, form dd {
display:inline-block; display:inline-block;

View File

@ -8,7 +8,7 @@
* @author Timothy J. Warren * @author Timothy J. Warren
* @copyright Copyright (c) 2011 - 2012 * @copyright Copyright (c) 2011 - 2012
* @link https://github.com/aviat4ion/miniMVC * @link https://github.com/aviat4ion/miniMVC
* @license http://philsturgeon.co.uk/code/dbad-license * @license http://philsturgeon.co.uk/code/dbad-license
*/ */
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -19,24 +19,12 @@
* @package miniMVC * @package miniMVC
* @subpackage App * @subpackage App
*/ */
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
namespace miniMVC; namespace miniMVC;
// Set as either DEVELOPMENT or PRODUCTION
// DEVELOPMENT enables error reporting
// PRODUCTION disables error reporting
define('ENVIRONMENT', 'DEVELOPMENT');
if(ENVIRONMENT == 'DEVELOPMENT') error_reporting(-1);
{
error_reporting(-1);
}
else if(ENVIRONMENT == 'PRODUCTION')
{
error_reporting(0);
}
// Set the default paths // Set the default paths
define('MM_BASE_PATH', __DIR__); define('MM_BASE_PATH', __DIR__);
@ -53,4 +41,4 @@ require(MM_SYS_PATH . 'common.php');
// And away we go! // And away we go!
init(); init();
// End of index.php // End of index.php

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<phpdoc> <phpdoc>
<title>miniMVC</title> <title>meta</title>
<parser> <parser>
<target>docs</target> <target>docs</target>
</parser> </parser>
@ -16,11 +16,12 @@
<directory>assets</directory> <directory>assets</directory>
<directory>app/config</directory> <directory>app/config</directory>
<directory>app/views</directory> <directory>app/views</directory>
<directory>sys/db/tests</directory> <directory>sys/db</directory>
<ignore>tests/*</ignore> <ignore>tests/*</ignore>
<ignore>assets/*</ignore> <ignore>assets/*</ignore>
<ignore>sys/db/tests/*</ignore> <ignore>sys/db/*</ignore>
<ignore>app/config/*</ignore> <ignore>app/config/*</ignore>
<ignore>app/views/*</ignore> <ignore>app/views/*</ignore>
<ignore>*/views/*</ignore>
</files> </files>
</phpdoc> </phpdoc>

View File

@ -79,7 +79,7 @@ function shutdown()
$error = error_get_last(); $error = error_get_last();
// types of errors that are fatal // types of errors that are fatal
$fatal = [E_ERROR, E_PARSE, E_RECOVERABLE_ERROR]; $fatal = array(E_ERROR, E_PARSE, E_RECOVERABLE_ERROR);
// Display pretty error page // Display pretty error page
if (in_array($error['type'], $fatal)) if (in_array($error['type'], $fatal))
@ -308,6 +308,9 @@ function init()
// Load Database classes // Load Database classes
require_once(MM_SYS_PATH . 'db/autoload.php'); require_once(MM_SYS_PATH . 'db/autoload.php');
// Load the page class
$GLOBALS['page'] = new \miniMVC\Page();
// Map to the appropriate module/controller/function // Map to the appropriate module/controller/function
route(); route();
} }
@ -327,6 +330,40 @@ function get_last_segment()
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/**
* Call a method in another controller
*
* @param string
* @param string
* @param args
*/
function call_controller_method($controller, $method="index", $args=array())
{
// Load the routes config file
$routes = include(MM_APP_PATH . 'config/routes.php');
// Set the default route
$module = $routes['default_module'];
$class = $routes['default_controller'];
// Split the controller into module/controller if possible
$parts = explode('/', $controller);
if (count($parts) === 2)
{
list($module, $class) = $parts;
}
else
{
$class = $parts[0];
}
// Call the method
run($module, $class, $method, $args);
}
// --------------------------------------------------------------------------
/** /**
* Gets an array of the segments of the current url * Gets an array of the segments of the current url
* *
@ -463,14 +500,17 @@ function route()
return; return;
} }
// --------------------------------------------------------------------------
/** /**
* Instantiate the appropriate controller * Instantiate the appropriate controller
* *
* @param string * @param string
* @param string * @param string
* @param string * @param string
* @param array
*/ */
function run($module, $controller, $func) function run($module, $controller, $func, $args = array())
{ {
$path = MM_MOD_PATH . "{$module}/controllers/{$controller}.php"; $path = MM_MOD_PATH . "{$module}/controllers/{$controller}.php";
@ -484,10 +524,13 @@ function run($module, $controller, $func)
if (in_array($func, $methods)) if (in_array($func, $methods))
{ {
// Define the name of the current module for file loading // Define the name of the current module for file loading
define('MM_MOD', $module); if ( ! defined('MM_MOD'))
{
define('MM_MOD', $module);
}
$class = new $controller(); $class = new $controller();
return call_user_func([&$class, $func]); return call_user_func_array(array(&$class, $func), $args);
} }
} }
@ -495,5 +538,4 @@ function run($module, $controller, $func)
show_404(); show_404();
} }
// End of common.php // End of common.php

View File

@ -21,7 +21,7 @@ namespace miniMVC;
* @package miniMVC * @package miniMVC
* @subpackage System * @subpackage System
*/ */
class Controller extends miniMVC { class Controller {
/** /**
* Instance of Page class * Instance of Page class
@ -37,10 +37,8 @@ class Controller extends miniMVC {
*/ */
public function __construct() public function __construct()
{ {
parent::__construct();
// Create the page object // Create the page object
$this->page = new Page($this); $this->page = $GLOBALS['page'];
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -52,7 +50,7 @@ class Controller extends miniMVC {
* @param array $args * @param array $args
* @return void * @return void
*/ */
public function load_model($file, $args=[]) public function load_model($file, $args=array())
{ {
$segments = explode('\\', $file); $segments = explode('\\', $file);
$file_name = end($segments); $file_name = end($segments);
@ -87,41 +85,9 @@ class Controller extends miniMVC {
* @param bool $return * @param bool $return
* @return mixed * @return mixed
*/ */
public function load_view($file, array $data=[], $return=FALSE) public function load_view($file, array $data=array(), $return=FALSE)
{ {
$path = ""; return $this->page->load_view($file, $data, $return);
// The module is set via the router
$module = strtolower(MM_MOD);
$path = MM_MOD_PATH . "{$module}/views/{$file}.php";
// If it's not a module, or doesn't exist in the module view folder
// look in the app view folder
if ( ! is_file($path))
{
$path = MM_APP_PATH . "views/{$file}.php";
}
// Contain the content for buffering
ob_start();
// Extract the data array
extract($data);
// Include the file
include($path);
$buffer = ob_get_contents();
ob_end_clean();
if ($return == TRUE)
{
return $buffer;
}
else
{
$this->page->append_output($buffer);
}
} }
} }

View File

@ -1,70 +0,0 @@
<?php
/**
* MiniMVC
*
* Convention-based micro-framework for PHP
*
* @package miniMVC
* @author Timothy J. Warren
* @copyright Copyright (c) 2011 - 2012
* @link https://github.com/aviat4ion/miniMVC
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
namespace miniMVC;
/**
* Class for standalone JSObject objects
*
* @package miniMVC
* @subpackage System
*/
class MM extends \ArrayObject {
/**
* Create the ArrayObject hybrid object
*
* @param array
*/
public function __construct($members = array())
{
parent::__construct($members);
// Add the passed parameters to the object
foreach ($members as $name => &$value)
{
$this->$name = $value;
}
}
// --------------------------------------------------------------------------
/**
* Allow calling of array methods on the object and
* dynamic methods
*
* @param string $name
* @param array $params
* @return mixed
*/
public function __call($name, $params = array())
{
// Allow array operations on the object
if (substr($name, 0, 6) === 'array_' && is_callable($name))
{
$args = array_merge($this->getArrayCopy(), $args);
return call_user_func_array($name, $args);
}
// Allow dynamic method calls
if (is_callable($this->$name))
{
//Call the dynamic function
return call_user_func_array($this->$name, $params);
}
}
}
// End of MM.php

View File

@ -8,7 +8,7 @@
* @author Timothy J. Warren * @author Timothy J. Warren
* @copyright Copyright (c) 2011 - 2012 * @copyright Copyright (c) 2011 - 2012
* @link https://github.com/aviat4ion/miniMVC * @link https://github.com/aviat4ion/miniMVC
* @license http://philsturgeon.co.uk/code/dbad-license * @license http://philsturgeon.co.uk/code/dbad-license
*/ */
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -21,7 +21,7 @@ namespace miniMVC;
* @package miniMVC * @package miniMVC
* @subpackage System * @subpackage System
*/ */
class Model extends miniMVC { class Model extends \ArrayObject {
/** /**
* Initialize the model class * Initialize the model class
@ -31,7 +31,7 @@ class Model extends miniMVC {
*/ */
public function __construct(array $args = array()) public function __construct(array $args = array())
{ {
parent::__construct($args); parent::__construct($args, \ArrayObject::STD_PROP_LIST | \ArrayObject::ARRAY_AS_PROPS);
} }
} }

View File

@ -1,156 +0,0 @@
<?php
/**
* MiniMVC
*
* Convention-based micro-framework for PHP
*
* @package miniMVC
* @author Timothy J. Warren
* @copyright Copyright (c) 2011 - 2012
* @link https://github.com/aviat4ion/miniMVC
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
namespace miniMVC;
/**
* Class for displaying output and setting http headers
*
* @package miniMVC
* @subpackage System
*/
class Output extends MM {
/**
* Content for outputting
*
* @var string
*/
private $buffer;
/**
* HTTP headers to send
*
* @var array
*/
private $headers;
/**
* Initialize the output class
*
* @return void
*/
public function __construct()
{
$this->buffer = "";
$this->headers = array();
}
// --------------------------------------------------------------------------
/**
* PHP magic method called when ending the script
* Used for outputing HTML
*
* @return void
*/
public function __destruct()
{
if ( ! empty($this->headers))
{
// Set headers
foreach($this->headers as $key => $val)
{
if ( ! isset($val))
{
@header($key);
}
else
{
@header("$key: $val");
}
}
}
if ( ! empty($this->buffer))
{
if (is_null(error_get_last()))
{
// Compression is good!
ob_start("ob_gzhandler");
}
else
{
ob_start();
}
echo $this->buffer;
ob_end_flush();
}
}
// --------------------------------------------------------------------------
/**
* Sets a header for later output
*
* @param string $key
* @param string $val
*/
public function set_header($key, $val)
{
$this->headers[$key] = $val;
}
// --------------------------------------------------------------------------
/**
* Adds text to the output buffer
*
* @param string $string
*/
public function append_output($string)
{
$this->buffer .= $string;
}
// --------------------------------------------------------------------------
/**
* Sets the output buffer
*
* @param string $string
*/
public function set_output($string)
{
$this->buffer = $string;
}
// --------------------------------------------------------------------------
/**
* Sends headers and then removes them
*/
public function flush_headers()
{
// Set headers
foreach ($this->headers as $key => &$val)
{
if ( ! isset($val))
{
@header($key);
}
else
{
@header("{$key}: {$val}");
}
}
// Empty headers
$this->headers = array();
}
}
// End of Output.php

View File

@ -8,9 +8,9 @@
* @author Timothy J. Warren * @author Timothy J. Warren
* @copyright Copyright (c) 2011 - 2012 * @copyright Copyright (c) 2011 - 2012
* @link https://github.com/aviat4ion/miniMVC * @link https://github.com/aviat4ion/miniMVC
* @license http://philsturgeon.co.uk/code/dbad-license * @license http://philsturgeon.co.uk/code/dbad-license
*/ */
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
namespace miniMVC; namespace miniMVC;
@ -24,7 +24,7 @@ namespace miniMVC;
* @package miniMVC * @package miniMVC
* @subpackage System * @subpackage System
*/ */
class Page extends Output { class Page {
/** /**
* Meta tags * Meta tags
@ -32,70 +32,83 @@ class Page extends Output {
* @var string * @var string
*/ */
private $meta; private $meta;
/** /**
* JS tags for the header * JS tags for the header
* *
* @var string * @var string
*/ */
private $head_js; private $head_js;
/** /**
* JS tags for the footer * JS tags for the footer
* *
* @var string * @var string
*/ */
private $foot_js; private $foot_js;
/** /**
* CSS tags for the page * CSS tags for the page
* *
* @var string * @var string
*/ */
private $css; private $css;
/** /**
* Page title * Page title
* *
* @var string * @var string
*/ */
private $title; private $title;
/** /**
* Additional header tags * Additional header tags
* *
* @var string * @var string
*/ */
private $head_tags; private $head_tags;
/** /**
* Class(es) to apply to the main body tag * Class(es) to apply to the main body tag
* *
* @var string * @var string
*/ */
private $body_class; private $body_class;
/** /**
* Id to apply to the body tag * Id to apply to the body tag
* *
* @var string * @var string
*/ */
private $body_id; private $body_id;
/** /**
* Base tag * Base tag
* *
* @var string * @var string
*/ */
private $base; private $base;
/**
* Content for outputting
*
* @var string
*/
private $buffer;
/**
* HTTP headers to send
*
* @var array
*/
private $headers;
/** /**
* Set up the page class * Set up the page class
* *
* @param object
* @return void * @return void
*/ */
public function __construct(&$controller) public function __construct()
{ {
$this->meta = ""; $this->meta = "";
$this->head_js = ""; $this->head_js = "";
@ -106,22 +119,69 @@ class Page extends Output {
$this->body_class = ""; $this->body_class = "";
$this->body_id = ""; $this->body_id = "";
$this->base = ""; $this->base = "";
$this->buffer = "";
$this->mm = $controller; $this->headers = array();
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* call the parent destructor * PHP magic method called when ending the script
* Used for outputing HTML
*
* @return void
*/ */
public function __destruct() public function __destruct()
{ {
parent::__destruct(); if ( ! empty($this->headers))
{
// Set headers
foreach($this->headers as $key => $val)
{
if ( ! isset($val))
{
@header($key);
}
else
{
@header("$key: $val");
}
}
}
if ( ! empty($this->buffer))
{
$errors = error_get_last();
if (empty($errors))
{
// Compression is good!
ob_start("ob_gzhandler");
}
else
{
ob_start();
}
echo $this->buffer;
// Check if a buffer exists
// so that it doesn't throw a notice
if (ob_get_level > 0)
{
ob_end_flush();
}
}
else
{
echo 'No content';
}
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Sets server headers and doctype * Sets server headers and doctype
* *
@ -136,40 +196,40 @@ class Page extends Output {
{ {
$this->set_header("Cache-Control", "must-revalidate, public"); $this->set_header("Cache-Control", "must-revalidate, public");
$mime = ""; $mime = "";
//Variable for accept keyword //Variable for accept keyword
$accept = ( ! empty($_SERVER['HTTP_ACCEPT'])) ? $_SERVER['HTTP_ACCEPT'] : ""; $accept = ( ! empty($_SERVER['HTTP_ACCEPT'])) ? $_SERVER['HTTP_ACCEPT'] : "";
//Predefine doctype //Predefine doctype
$doctype_string = ($html5 == TRUE) ? '<!DOCTYPE html>' : '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">'; $doctype_string = ($html5 == TRUE) ? '<!DOCTYPE html>' : '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">';
//Predefine charset //Predefine charset
$charset = "UTF-8"; $charset = "UTF-8";
$mime = "text/html"; $mime = "text/html";
if ($html5 == FALSE) if ($html5 == FALSE)
{ {
$doctype_string = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">'; $doctype_string = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">';
} }
$doctype_string .= "<html lang='en'>"; $doctype_string .= "<html lang='en'>";
// finally, output the mime type and prolog type // finally, output the mime type and prolog type
$this->set_header("Content-Type", "{$mime};charset={$charset}"); $this->set_header("Content-Type", "{$mime};charset={$charset}");
$this->set_header("X-UA-Compatible", "chrome=1, IE=edge"); $this->set_header("X-UA-Compatible", "chrome=1, IE=edge");
$this->set_output($doctype_string); $this->set_output($doctype_string);
return $this; return $this;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Set Meta * Set Meta
* *
* Sets meta tags, with codeigniter native meta tag helper * Sets meta tags, with codeigniter native meta tag helper
* *
* @param array $meta * @param array $meta
* @return Page * @return Page
*/ */
@ -178,9 +238,9 @@ class Page extends Output {
$this->meta .= $this->_meta($meta); $this->meta .= $this->_meta($meta);
return $this; return $this;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Sets minified javascript group in header * Sets minified javascript group in header
* *
@ -194,15 +254,15 @@ class Page extends Output {
{ {
return $this; return $this;
} }
$file = SCRIPT_PATH . $group; $file = SCRIPT_PATH . $group;
$file .= ($debug == TRUE) ? "/debug/1" : ""; $file .= ($debug == TRUE) ? "/debug/1" : "";
$this->head_js .= $this->script_tag($file, FALSE); $this->head_js .= $this->script_tag($file, FALSE);
return $this; return $this;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Sets a minified css group * Sets a minified css group
* @param string $group * @param string $group
@ -210,18 +270,18 @@ class Page extends Output {
*/ */
public function set_css_group($group) public function set_css_group($group)
{ {
$link = [ $link = array(
'href' => STYLE_PATH . $group, 'href' => STYLE_PATH . $group,
'rel' => 'stylesheet', 'rel' => 'stylesheet',
'type' => 'text/css' 'type' => 'text/css'
]; );
$this->css .= $this->_link_tag($link); $this->css .= $this->_link_tag($link);
return $this; return $this;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Sets a minified javascript group for the page footer * Sets a minified javascript group for the page footer
* *
@ -236,9 +296,9 @@ class Page extends Output {
$this->foot_js .= $this->script_tag($file, FALSE); $this->foot_js .= $this->script_tag($file, FALSE);
return $this; return $this;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Sets html title string * Sets html title string
* *
@ -248,14 +308,14 @@ class Page extends Output {
public function set_title($title = "") public function set_title($title = "")
{ {
$title = ($title == "") ? DEFAULT_TITLE : $title; $title = ($title == "") ? DEFAULT_TITLE : $title;
$this->title = $title; $this->title = $title;
return $this; return $this;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Sets custom body class * Sets custom body class
* *
@ -267,9 +327,9 @@ class Page extends Output {
$this->body_class = $class; $this->body_class = $class;
return $this; return $this;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Sets custom body id * Sets custom body id
* *
@ -281,9 +341,9 @@ class Page extends Output {
$this->body_id = $id; $this->body_id = $id;
return $this; return $this;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Sets custom base href * Sets custom base href
* *
@ -295,9 +355,9 @@ class Page extends Output {
$this->base = $href; $this->base = $href;
return $this; return $this;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Sets custom css tags * Sets custom css tags
* *
@ -310,24 +370,24 @@ class Page extends Output {
{ {
$path = CONTENT_DOMAIN; $path = CONTENT_DOMAIN;
$css_file = "{$path}/css/{$name}.css"; $css_file = "{$path}/css/{$name}.css";
if ($domain == FALSE) if ($domain == FALSE)
{ {
$css_file = $name; $css_file = $name;
} }
$this->css_tags .= $this->_link_tag([ $this->css_tags .= $this->_link_tag(array(
'rel' => 'stylesheet', 'rel' => 'stylesheet',
'type' => 'text/css', 'type' => 'text/css',
'media' => $media, 'media' => $media,
'href' => $css_file, 'href' => $css_file,
]); ));
return $this; return $this;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Sets a custom tag in the header * Sets a custom tag in the header
* *
@ -339,9 +399,9 @@ class Page extends Output {
$this->head_tags .= $tag; $this->head_tags .= $tag;
return $this; return $this;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Sets custom page header * Sets custom page header
* *
@ -351,17 +411,17 @@ class Page extends Output {
public function build_header($html5 = TRUE) public function build_header($html5 = TRUE)
{ {
$data = array(); $data = array();
//Set Meta Tags //Set Meta Tags
$this->meta = ($html5 == TRUE) $this->meta = ($html5 == TRUE)
? '<meta charset="utf-8" />'. $this->meta ? '<meta charset="utf-8" />'. $this->meta
: $this->_meta([ : $this->_meta(array(
'http-equiv' => 'Content-Type', 'http-equiv' => 'Content-Type',
'content' => 'text/html; charset=utf-8', 'content' => 'text/html; charset=utf-8',
]) . $this->meta; )) . $this->meta;
$data['meta'] = $this->meta; $data['meta'] = $this->meta;
//Set CSS //Set CSS
if ($this->css !== "") if ($this->css !== "")
{ {
@ -373,50 +433,50 @@ class Page extends Output {
$this->set_css_group(DEFAULT_CSS_GROUP); $this->set_css_group(DEFAULT_CSS_GROUP);
$data['css'] = $this->css; $data['css'] = $this->css;
} }
//Set head javascript //Set head javascript
$data['head_js'] = ( ! empty($this->head_js)) ? $this->head_js : ""; $data['head_js'] = ( ! empty($this->head_js)) ? $this->head_js : "";
//Set Page Title //Set Page Title
$data['title'] = ($this->title !== '') ? $this->title : DEFAULT_TITLE; $data['title'] = ($this->title !== '') ? $this->title : DEFAULT_TITLE;
//Set Body Class //Set Body Class
$data['body_class'] = $this->body_class; $data['body_class'] = $this->body_class;
//Set Body Id //Set Body Id
$data['body_id'] = $this->body_id; $data['body_id'] = $this->body_id;
//Set Base HREF //Set Base HREF
$data['base'] = $this->base; $data['base'] = $this->base;
//Set individual head tags //Set individual head tags
$data['head_tags'] = $this->head_tags; $data['head_tags'] = $this->head_tags;
//Set Server Headers and Doctype //Set Server Headers and Doctype
$this->_headers($html5); $this->_headers($html5);
//Output Header //Output Header
$this->mm->load_view('header', $data); $this->load_view('header', $data);
return $this; return $this;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Builds common footer with any additional js * Builds common footer with any additional js
*/ */
public function build_footer() public function build_footer()
{ {
$data = array(); $data = array();
$data['foot_js'] = ($this->foot_js != "") ? $this->foot_js : ''; $data['foot_js'] = ($this->foot_js != "") ? $this->foot_js : '';
$this->mm->load_view('footer', $data); $this->load_view('footer', $data);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Script Tag * Script Tag
* *
@ -430,19 +490,19 @@ class Page extends Output {
{ {
$path = CONTENT_DOMAIN; $path = CONTENT_DOMAIN;
$js_file = "{$path}/js/{$js}.js"; $js_file = "{$path}/js/{$js}.js";
if ($domain == FALSE) if ($domain == FALSE)
{ {
$js_file = $js; $js_file = $js;
} }
$tag = '<script src="' . $js_file . '"></script>'; $tag = '<script src="' . $js_file . '"></script>';
return $tag; return $tag;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Set Message * Set Message
* *
@ -456,26 +516,26 @@ class Page extends Output {
{ {
$data['stat_class'] = $type; $data['stat_class'] = $type;
$data['message'] = $message; $data['message'] = $message;
return $this->mm->load_view('message', $data, $return); return $this->load_view('message', $data, $return);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Redirect 303 * Redirect 303
* *
* Shortcut function for 303 redirect * Shortcut function for 303 redirect
* @param string $url * @param string $url
*/ */
function redirect_303($url) public function redirect_303($url)
{ {
$this->set_header("HTTP/1.1 303 See Other"); $this->set_header("HTTP/1.1 303 See Other");
$this->set_header("Location:" . $url); $this->set_header("Location:" . $url);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Render * Render
* *
@ -483,30 +543,30 @@ class Page extends Output {
* @param string $view * @param string $view
* @param array $data * @param array $data
*/ */
function render($view, $data=array()) public function render($view, $data=array())
{ {
$this->build_header(); $this->build_header();
$this->mm->load_view($view, $data); $this->load_view($view, $data);
$this->build_footer(); $this->build_footer();
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Output String * Output String
* *
* Similar to render(), this is a shortcut * Similar to render(), this is a shortcut
* to output a string in the body of the * to output a string in the body of the
* page. * page.
* @param string $string * @param string $string
*/ */
function output_string($string) public function output_string($string)
{ {
$this->build_header(); $this->build_header();
$this->append_output($string); $this->append_output($string);
$this->build_footer(); $this->build_footer();
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
@ -518,17 +578,17 @@ class Page extends Output {
private function _meta($params) private function _meta($params)
{ {
$string = "<meta "; $string = "<meta ";
foreach ($params as $k => &$v) foreach ($params as $k => &$v)
{ {
$string .= $k.'="'.$v.'" '; $string .= $k.'="'.$v.'" ';
} }
$string .= " />"; $string .= " />";
return $string; return $string;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
@ -536,20 +596,128 @@ class Page extends Output {
* *
* @param array $params * @param array $params
* @return string * @return string
*/ */
private function _link_tag($params) private function _link_tag($params)
{ {
$string = "<link "; $string = "<link ";
foreach ($params as $k => &$v) foreach ($params as $k => &$v)
{ {
$string .= $k . '="'.$v.'" '; $string .= $k . '="'.$v.'" ';
} }
$string .= "/>"; $string .= "/>";
return $string; return $string;
} }
// --------------------------------------------------------------------------
/**
* Function for loading a view
*
* @param string $file
* @param array $data
* @param bool $return
* @return mixed
*/
public function load_view($file, array $data=array(), $return=FALSE)
{
$path = "";
// The module is set via the router
$module = strtolower(MM_MOD);
$path = MM_MOD_PATH . "{$module}/views/{$file}.php";
// If it's not a module, or doesn't exist in the module view folder
// look in the app view folder
if ( ! is_file($path))
{
$path = MM_APP_PATH . "views/{$file}.php";
}
// Contain the content for buffering
ob_start();
// Extract the data array
extract($data);
// Include the file
include($path);
$buffer = ob_get_contents();
ob_end_clean();
if ($return == TRUE)
{
return $buffer;
}
else
{
$this->append_output($buffer);
}
}
// --------------------------------------------------------------------------
/**
* Sets a header for later output
*
* @param string $key
* @param string $val
*/
public function set_header($key, $val)
{
$this->headers[$key] = $val;
}
// --------------------------------------------------------------------------
/**
* Adds text to the output buffer
*
* @param string $string
*/
public function append_output($string)
{
$this->buffer .= $string;
}
// --------------------------------------------------------------------------
/**
* Sets the output buffer
*
* @param string $string
*/
public function set_output($string)
{
$this->buffer = $string;
}
// --------------------------------------------------------------------------
/**
* Sends headers and then removes them
*/
public function flush_headers()
{
// Set headers
foreach ($this->headers as $key => &$val)
{
if ( ! isset($val))
{
@header($key);
}
else
{
@header("{$key}: {$val}");
}
}
// Empty headers
$this->headers = array();
}
} }
// End of page.php // End of page.php

View File

@ -1,84 +0,0 @@
<?php
/**
* MiniMVC
*
* Convention-based micro-framework for PHP
*
* @package miniMVC
* @author Timothy J. Warren
* @copyright Copyright (c) 2011 - 2012
* @link https://github.com/aviat4ion/miniMVC
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
namespace miniMVC;
/**
* Base class for the framework
*
* @package miniMVC
* @subpackage System
*/
class miniMVC extends MM {
/**
* Constructor - Any classes loaded here become subclasses of miniMVC
*
* @param array $members
*/
public function __construct($members = array())
{
// Allow the class to be used like an array
parent::__construct($members);
}
// --------------------------------------------------------------------------
/**
* Convenience function to load config files
*
* @param string $name
*/
public function load_config($name)
{
$path = MM_APP_PATH . "config/{$name}.php";
if (is_file($path))
{
require_once($path);
}
}
// --------------------------------------------------------------------------
/**
* Singleton getter function
*
* @return self
*/
public static function &get_instance()
{
if ( ! isset(self::$instance))
{
$class = __CLASS__;
self::$instance = new $class;
}
return self::$instance;
}
// --------------------------------------------------------------------------
/**
* Magic function called when cloning an object
*/
public function __clone()
{
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
}
// End of miniMVC.php

Binary file not shown.