Removed Cache class.

* File was little more than just a stub file for the Cache class.

Removed Mailer class.

 * Attempting to remove non-critical classes from the system.

 * Class will be re-added / re-factored at a later date once the core of PICKLES is done.

Removed Security class.

 * Removing non-critcal classes from the system.

 * Security class was not very generic and will be replaced down the road once the security scheme is full realized.

Removed Form class.

 * Removing non-critcal classes from PICKLES to help the rewrite efforts.

 * Form class would take a database table and convert it into a webform.  Nice script, but really had no place in PICKLES.

 * Eventually will replace with a generic HTML form element generation class.

Added INSTALL file.

Updated derivation of the hostname.

 * Hostname is now defaulted in the class variable definition.

Refactoring Controller and Module
This commit is contained in:
Josh Sherman 2010-03-11 23:19:33 -05:00
parent 67c12593bc
commit c2445d3a62
8 changed files with 150 additions and 709 deletions

View file

@ -3,261 +3,126 @@
/**
* Module Class File for PICKLES
*
* PICKLES is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
* PHP version 5
*
* PICKLES is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* Licensed under the GNU General Public License Version 3
* Redistribution of these files must retain the above copyright notice.
*
* You should have received a copy of the GNU Lesser General Public
* License along with PICKLES. If not, see
* <http://www.gnu.org/licenses/>.
*
* @author Joshua John Sherman <josh@phpwithpickles.org>
* @copyright Copyright 2007, 2008, 2009 Joshua John Sherman
* @package pickles
* @author Josh Sherman <josh@phpwithpickles.org>
* @copyright Copyright 2007-2010, Gravity Boulevard, LLC
* @license http://www.gnu.org/licenses/gpl.html GPL v3
* @link http://phpwithpickles.org
* @license http://www.gnu.org/copyleft/lesser.html
* @package PICKLES
*/
/**
* Module Class
*
* Every module (page) in PICKLES at both the core and site levels should
* extend this class. It handles the getting of common module variables
* (auth, data and view) as well as making sure that every module has a
* database object available.
* This is a parent class that all PICKLES modules should be extending.
* Each module can specify it's own meta data and whether or not a user
* must be properly authenticated to view the page. Currently any pages
* without a template are treated as pages being requested via AJAX and the
* return will be JSON encoded. In the future this may need to be changed
* out for logic that allows the requested module to specify what display
* type(s) it can use.
*/
class Module extends Object {
class Module extends Object
{
/**
* Page title
*
* @var string
*/
public $title;
/**
* Array of public variables to be available by the display
* Meta description
*
* @var string
*/
protected $public = array();
public $description;
/**
* Passed objects
* Meta keywords (comma separated)
*
* @var string
*/
protected $config = null;
protected $db = null;
protected $mailer = null;
protected $error = null;
public $keywords;
/**
* Template file for the module
* Access level of the page
*
* Defaults to false which is everybody, even anonymous
*
* @access protected
* @var boolean
*/
protected $template = null;
protected $name = null;
/**
* Module defaults
*/
protected $authentication = false;
protected $caching = false;
protected $display = false;
protected $session = false;
protected $access = false;
private $smarty;
private $cache_id;
/**
* Secure
*
* Whether or not the page should be loaded via SSL. Not currently
* being used. Defaults to false, non-SSL.
*
* @access protected
* @var boolean
*/
protected $secure = false;
/**
* AJAX
*
* Whether or not the page must be loaded via AJAX and if so, what
* pages are allowed to access it and the request method.
*
* @access protected
* @var array
*/
protected $ajax = false;
/**
* Default template
*
* Defaults to index.tpl but could be set to any valid template.
*
* @var string
*/
public $template = 'index';
/**
* Constructor
*
* Handles calling the parent constructor and sets up the module's
* internal config and database object
* The constructor does nothing by default but can be passed a boolean
* variable to tell it to automatically run the __default() method.
* This is typically used when a module is called outside of the scope
* of the controller (the registration page calls the login page in
* this manner.
*
* @param object $config Instance of the Config class
* @param object $db Instance of the DB class
* @param object $mailer Instance of the Mailer class
* @param boolean $autorun optional flag to autorun __default()
*/
public function __construct(Config $config, DB $db, Mailer $mailer, Error $error) {
public function __construct($autorun = false)
{
parent::__construct();
$this->config = $config;
$this->db = $db;
$this->mailer = $mailer;
$this->error = $error;
}
/**
* Gets the authentication value
*
* Order of precedence:
* Module, Config, Guess (guess is always false)
*
* @return boolean Whether or not user authentication is required
*/
public function getAuthentication() {
if ($this->authentication != null) {
return $this->authentication;
}
else if (is_bool($this->config->getAuthentication())) {
return $this->config->getAuthentication();
}
return false;
}
/**
* Gets the caching value
*
* Order of precedence:
* POSTed, Module, Config, Guess (guess is always false)
*
* @return boolean Whether or not user authentication is required
*/
public function getCaching() {
/*
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
return false;
}
*/
if ($this->caching != null) {
return $this->caching;
}
else if ($this->config->getCaching()) {
return $this->config->getCaching();
}
return false;
}
/**
* Gets the session value
*
* Order of precedence:
* Auth On, Module, Config, Guess (guess is always false)
*
* @return boolean Whether or not the session needs to be started
*/
public function getSession() {
if ($this->authentication === true) {
return true;
}
else if ($this->session != null) {
return $this->session;
}
else if (is_bool($this->config->getSession())) {
return $this->config->getSession();
}
return false;
}
/**
* Gets the requested Display
*
* Order of precedence:
* Module, Config, Guess (guess is always Smarty)
*
* @return string The display that the module has requested to be used
*/
public function getDisplay() {
// Checks if the module has a display tyoe
if (isset($this->display)) {
// Checks if multiple display types are supported
if (is_array($this->display)) {
$display = $this->display[0];
}
else {
$display = $this->display;
}
if (in_array($display, array(DISPLAY_JSON, DISPLAY_PHP, DISPLAY_RSS, DISPLAY_SMARTY))) {
return $display;
}
}
// Checks for a display type in the config
if (isset($this->config->modules->display)) {
return (string)$this->config->modules->display;
}
else {
$this->error->addWarning('Invalid display specified, DISPLAY_PHP used by default (' . $this->display . ')');
return DISPLAY_PHP;
if ($autorun === true)
{
$this->__default();
}
}
/**
* Alias for $module->data
* Default "Magic" Method
*
* @return array Associative array of data that was set by the module
* This function is overloaded by the module. The __default() method is
* where you want to place any code that needs to be executed at
* runtime. The reason the code isn't in the constructor is because the
* module must be instantiated before the code is executed so that the
* controller script is aware of the authentication requirements.
*/
public function getData() {
if (isset($this->data)) {
return $this->data;
}
public function __default()
{
return null;
}
public function setPublic($variable, $value) {
$this->public[$variable] = $value;
return true;
}
public function getPublic($variable) {
if (isset($this->public[$variable])) {
return $this->public[$variable];
}
else {
return null;
}
}
public function setSmartyObject(Smarty $smarty) {
$this->smarty = $smarty;
}
public function isCached($id = null) {
if ($id == null) {
$id = get_class($this);
}
switch ($this->getDisplay()) {
case DISPLAY_PHP:
break;
case DISPLAY_SMARTY:
if (is_object($this->smarty)) {
if ($this->smarty->template_exists('index.tpl')) {
return $this->smarty->is_cached('index.tpl', $id);
}
else {
return $this->smarty->is_cached($template, $id);
}
}
break;
}
return false;
}
public function setCacheID($id) {
$this->cache_id = $id;
}
public function getCacheID() {
return $this->cache_id;
}
/**
* Default function
*
* This function is overloaded by the module. The __default() function
* is where any code that needs to be executed at run time needs to be
* placed. It's not in the constructor as the module needs to be
* instantiated first so that the authorization requirements can be
* checked without running code it's potentially not supposed to have
* been executed.
*/
public function __default() {
}
}