/trunk/registry/registry.class.php
PHP | 335 lines | 202 code | 63 blank | 70 comment | 31 complexity | bde9048a783b760171f8b1a87f384aff MD5 | raw file
Possible License(s): LGPL-2.1
- <?php
- /**
- * The PCARegistry object
- * Implements the Registry and Singleton design patterns
- * Building a PHP Ecommerce Framework
- *
- * @version 1.0
- * @author Michael Peacock
- */
-
- class PeacockCarterFrameworkRegistry {
-
- /**
- * The array of objects being stored within the registry
- * @access private
- */
- private static $objects = array();
-
- /**
- * The array of settings being stored within the registry
- * @access private
- */
- private static $settings = array();
-
-
- /**
- * The instance of the registry
- * @access private
- */
- private static $instance;
-
- private static $urlPath;
- private static $urlBits = array();
-
-
- /**
- * Private constructor to prevent it being created directly
- * @access private
- */
- private function __construct()
- {
-
- }
-
- /**
- * singleton method used to access the object
- * @access public
- * @return
- */
- public static function singleton()
- {
- if( !isset( self::$instance ) )
- {
- $obj = __CLASS__;
- self::$instance = new $obj;
- }
-
- return self::$instance;
- }
-
- /**
- * prevent cloning of the object: issues an E_USER_ERROR if this is attempted
- */
- public function __clone()
- {
- trigger_error( 'Cloning the registry is not permitted', E_USER_ERROR );
- }
-
- /**
- * Stores an object in the registry
- * @param String $object the name of the object
- * @param String $key the key for the array
- * @return void
- */
- public function storeObject( $object, $key )
- {
- if( strpos( $object, 'database' ) !== false )
- {
- $object_a = str_replace( '.database', 'database', $object);
- $object = str_replace( '.database', '', $object);
- require_once('databaseobjects/' . $object . '.database.class.php');
- $object = $object_a;
- }
- else
- {
- require_once('objects/' . $object . '.class.php');
- }
-
- self::$objects[ $key ] = new $object( self::$instance );
- }
-
- /**
- * Gets an object from within the registry
- * @param String $key the array key used to store the object
- * @return object - the object
- */
- public function getObject( $key )
- {
- if( is_object ( self::$objects[ $key ] ) )
- {
- return self::$objects[ $key ];
- }
- }
-
- /**
- * Stores a setting in the registry
- * @param String $data the setting we wish to store
- * @param String $key the key for the array to access the setting
- * @return void
- */
- public function storeSetting( $data, $key )
- {
- self::$settings[ $key ] = $data;
- }
-
- /**
- * Gets a setting from the registry
- * @param String $key the key used to store the setting
- * @return String the setting
- */
- public function getSetting( $key )
- {
- if( ! isset( self::$settings[ $key ] ) )
- {
- $keycleaned = self::getObject('db')->sanitizeData( $key );
- $sql = "SELECT * FROM settings WHERE `key`='{$keycleaned}' LIMIT 1";#
- self::getObject('db')->executeQuery( $sql );
- if( self::getObject('db')->numRows() == 1 )
- {
- $data = self::getObject('db')->getRows();
- self::$settings[ $key ] = $data['value'];
- }
- }
- return self::$settings[ $key ];
-
- }
-
- public function getSettings()
- {
- return self::$settings;
- }
-
- public function setURLPath($path)
- {
- self::$urlPath = $path;
- }
-
- /**
- * Gets data from the current URL
- * @return void
- */
- public function getURLData()
- {
- $urldata = (isset($_GET['page'])) ? $_GET['page'] : '' ;
- self::$urlPath = $urldata;
- if( $urldata == '' )
- {
- self::$urlBits[] = '';
- self::$urlPath = '';
- }
- else
- {
- $data = explode( '/', $urldata );
- while ( !empty( $data ) && strlen( reset( $data ) ) === 0 )
- {
- array_shift( $data );
- }
- while ( !empty( $data ) && strlen( end( $data ) ) === 0)
- {
- array_pop($data);
- }
- self::$urlBits = $this->array_trim( $data );
- }
- }
-
- public function getURLBits()
- {
- return self::$urlBits;
- }
-
- public function getURLBit( $whichBit )
- {
- return ( isset( self::$urlBits[ $whichBit ] ) ) ? self::$urlBits[ $whichBit ] : 0 ;
- }
-
- public function getURLPath()
- {
- return self::$urlPath;
- }
-
- public function redirectUser( $url, $messageHeading, $message, $adminArea = false )
- {
- //echo "<pre>" . print_r( self::getObject('template')->getPage()->getTags(), true) . "</pre>";
-
- if( $adminArea == true )
- {
- if( self::getObject('authenticate')->isAuthorised('global_auto_approval', self::getObject('authenticate')->getUserID() ) == false )
- {
- $approvalMessage = "<b><i>Your changes have been submitted for approval.</i></b><br/><br/>";
- $message = $approvalMessage . $message;
- }
- }
-
- $url = $this->buildURL( $url, '', $adminArea );
- self::getObject('template')->buildFromTemplates('bounce.tpl.php');
- self::getObject('template')->getPage()->addTag('heading', $messageHeading);
- self::getObject('template')->getPage()->addTag('message', $message);
- self::getObject('template')->getPage()->addTag('url', $url);
- self::getObject('template')->parseOutput();
- print self::getObject('template')->getPage()->getContentToPrint();
- exit();
- }
-
- public function errorPage( $h, $m, $admin = false )
- {
- $this->displayErrorPage( $h, $m, $admin );
- }
-
-
- public function displayErrorPage( $errorHeading, $errorMessage, $admin = false )
- {
- // get the home page
- $sql = "SELECT c.ID FROM content c, content_types t, content_versions v, content_versions_pages p WHERE c.type=t.ID AND t.reference='page' AND p.version_id=v.ID AND v.ID=c.current_revision AND c.`order` >= 0 ORDER BY c.`order` ASC LIMIT 1";
- self::getObject('db')->executeQuery( $sql );
- $p = self::getObject('db')->getRows();
- if( $admin != false)
- {
- self::getObject('menubuilder')->buildMenu( $p['ID'] );
-
- }
- self::getObject('template')->getPage()->addTag( 'admin_link_name', '' );
-
- self::getObject('template')->buildFromTemplates('header.tpl.php', 'message.tpl.php', 'footer.tpl.php');
- self::getObject('template')->getPage()->addTag('heading', $errorHeading);
- self::getObject('template')->getPage()->addTag('message', $errorMessage);
- }
-
- private function array_trim( $array )
- {
- while ( ! empty( $array ) && strlen( reset( $array ) ) === 0)
- {
- array_shift( $array );
- }
-
- while ( !empty( $array ) && strlen( end( $array ) ) === 0)
- {
- array_pop( $array );
- }
-
- return $array;
- }
-
- public function buildURL( $bits, $qs, $admin )
- {
- $admin = ( $admin == 1 ) ? $this->getSetting('admin_folder') . '/' : '';
- $the_rest = '';
- foreach( $bits as $bit )
- {
- $the_rest .= $bit . '/';
- }
- $the_rest = ( $qs != '' ) ? $the_rest . '?&' .$qs : $the_rest;
- return $this->getSetting('siteurl') . $admin . $the_rest;
-
- }
-
-
- public function pagination( $query, $limit, $offset, $method )
- {
- // method is the decision maker
- // - cache the query and return the cache id
- // - do the query and return the array?
- // - something else which is not implemented?
-
- // first thing first - let's build up Mr Query
-
- $temp_pg_query = $query;
- self::getObject('db')->executeQuery( $temp_pg_query );
- $num = self::getObject('db')->numRows();
- $limit_str = " LIMIT ";
- $limit_str .= ($offset * $limit) . ", " . $limit;
- $temp_pg_query .= $limit_str;
-
-
- if( $method == 'cache' )
- {
-
- $cache_id = self::getObject('db')->cacheQuery( $temp_pg_query );
-
- $tor = array( 'num' => $num, 'cache' => $cache_id );
-
- }
- elseif ( $method == 'do' )
- {
-
- self::getObject('db')->executeQuery( $temp_pg_query );
- $results = self::getObject('db')->getRows();
- $tor = array( 'num' => $num, 'results' => $results );
-
-
- }
- else
- {
- $tor = null;
- }
-
- // be nice...do some calculations - so modules don't have to!
-
- // num pages
-
- $num_pages = ceil($num/$limit);
-
- // is first
-
- $is_first = ( $offset == 0 ) ? true : false;
-
- // is last
-
- $is_last = ( ( $offset + 1 ) == $num_pages ) ? true : false;
-
- // cur page
-
- $cur_page = $offset;
-
- $tor['num_pages'] = $num_pages;
- $tor['is_first'] = $is_first;
- $tor['is_last'] = $is_last;
- $tor['cur_page'] = ( $num_pages == 0 ) ? 0 : $cur_page +1;
-
- return $tor;
-
- }
-
- }
-
- ?>