PageRenderTime 47ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/registry/registry.class.php

https://bitbucket.org/pooshonk/esw
PHP | 335 lines | 202 code | 63 blank | 70 comment | 31 complexity | bde9048a783b760171f8b1a87f384aff MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * The PCARegistry object
  4. * Implements the Registry and Singleton design patterns
  5. * Building a PHP Ecommerce Framework
  6. *
  7. * @version 1.0
  8. * @author Michael Peacock
  9. */
  10. class PeacockCarterFrameworkRegistry {
  11. /**
  12. * The array of objects being stored within the registry
  13. * @access private
  14. */
  15. private static $objects = array();
  16. /**
  17. * The array of settings being stored within the registry
  18. * @access private
  19. */
  20. private static $settings = array();
  21. /**
  22. * The instance of the registry
  23. * @access private
  24. */
  25. private static $instance;
  26. private static $urlPath;
  27. private static $urlBits = array();
  28. /**
  29. * Private constructor to prevent it being created directly
  30. * @access private
  31. */
  32. private function __construct()
  33. {
  34. }
  35. /**
  36. * singleton method used to access the object
  37. * @access public
  38. * @return
  39. */
  40. public static function singleton()
  41. {
  42. if( !isset( self::$instance ) )
  43. {
  44. $obj = __CLASS__;
  45. self::$instance = new $obj;
  46. }
  47. return self::$instance;
  48. }
  49. /**
  50. * prevent cloning of the object: issues an E_USER_ERROR if this is attempted
  51. */
  52. public function __clone()
  53. {
  54. trigger_error( 'Cloning the registry is not permitted', E_USER_ERROR );
  55. }
  56. /**
  57. * Stores an object in the registry
  58. * @param String $object the name of the object
  59. * @param String $key the key for the array
  60. * @return void
  61. */
  62. public function storeObject( $object, $key )
  63. {
  64. if( strpos( $object, 'database' ) !== false )
  65. {
  66. $object_a = str_replace( '.database', 'database', $object);
  67. $object = str_replace( '.database', '', $object);
  68. require_once('databaseobjects/' . $object . '.database.class.php');
  69. $object = $object_a;
  70. }
  71. else
  72. {
  73. require_once('objects/' . $object . '.class.php');
  74. }
  75. self::$objects[ $key ] = new $object( self::$instance );
  76. }
  77. /**
  78. * Gets an object from within the registry
  79. * @param String $key the array key used to store the object
  80. * @return object - the object
  81. */
  82. public function getObject( $key )
  83. {
  84. if( is_object ( self::$objects[ $key ] ) )
  85. {
  86. return self::$objects[ $key ];
  87. }
  88. }
  89. /**
  90. * Stores a setting in the registry
  91. * @param String $data the setting we wish to store
  92. * @param String $key the key for the array to access the setting
  93. * @return void
  94. */
  95. public function storeSetting( $data, $key )
  96. {
  97. self::$settings[ $key ] = $data;
  98. }
  99. /**
  100. * Gets a setting from the registry
  101. * @param String $key the key used to store the setting
  102. * @return String the setting
  103. */
  104. public function getSetting( $key )
  105. {
  106. if( ! isset( self::$settings[ $key ] ) )
  107. {
  108. $keycleaned = self::getObject('db')->sanitizeData( $key );
  109. $sql = "SELECT * FROM settings WHERE `key`='{$keycleaned}' LIMIT 1";#
  110. self::getObject('db')->executeQuery( $sql );
  111. if( self::getObject('db')->numRows() == 1 )
  112. {
  113. $data = self::getObject('db')->getRows();
  114. self::$settings[ $key ] = $data['value'];
  115. }
  116. }
  117. return self::$settings[ $key ];
  118. }
  119. public function getSettings()
  120. {
  121. return self::$settings;
  122. }
  123. public function setURLPath($path)
  124. {
  125. self::$urlPath = $path;
  126. }
  127. /**
  128. * Gets data from the current URL
  129. * @return void
  130. */
  131. public function getURLData()
  132. {
  133. $urldata = (isset($_GET['page'])) ? $_GET['page'] : '' ;
  134. self::$urlPath = $urldata;
  135. if( $urldata == '' )
  136. {
  137. self::$urlBits[] = '';
  138. self::$urlPath = '';
  139. }
  140. else
  141. {
  142. $data = explode( '/', $urldata );
  143. while ( !empty( $data ) && strlen( reset( $data ) ) === 0 )
  144. {
  145. array_shift( $data );
  146. }
  147. while ( !empty( $data ) && strlen( end( $data ) ) === 0)
  148. {
  149. array_pop($data);
  150. }
  151. self::$urlBits = $this->array_trim( $data );
  152. }
  153. }
  154. public function getURLBits()
  155. {
  156. return self::$urlBits;
  157. }
  158. public function getURLBit( $whichBit )
  159. {
  160. return ( isset( self::$urlBits[ $whichBit ] ) ) ? self::$urlBits[ $whichBit ] : 0 ;
  161. }
  162. public function getURLPath()
  163. {
  164. return self::$urlPath;
  165. }
  166. public function redirectUser( $url, $messageHeading, $message, $adminArea = false )
  167. {
  168. //echo "<pre>" . print_r( self::getObject('template')->getPage()->getTags(), true) . "</pre>";
  169. if( $adminArea == true )
  170. {
  171. if( self::getObject('authenticate')->isAuthorised('global_auto_approval', self::getObject('authenticate')->getUserID() ) == false )
  172. {
  173. $approvalMessage = "<b><i>Your changes have been submitted for approval.</i></b><br/><br/>";
  174. $message = $approvalMessage . $message;
  175. }
  176. }
  177. $url = $this->buildURL( $url, '', $adminArea );
  178. self::getObject('template')->buildFromTemplates('bounce.tpl.php');
  179. self::getObject('template')->getPage()->addTag('heading', $messageHeading);
  180. self::getObject('template')->getPage()->addTag('message', $message);
  181. self::getObject('template')->getPage()->addTag('url', $url);
  182. self::getObject('template')->parseOutput();
  183. print self::getObject('template')->getPage()->getContentToPrint();
  184. exit();
  185. }
  186. public function errorPage( $h, $m, $admin = false )
  187. {
  188. $this->displayErrorPage( $h, $m, $admin );
  189. }
  190. public function displayErrorPage( $errorHeading, $errorMessage, $admin = false )
  191. {
  192. // get the home page
  193. $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";
  194. self::getObject('db')->executeQuery( $sql );
  195. $p = self::getObject('db')->getRows();
  196. if( $admin != false)
  197. {
  198. self::getObject('menubuilder')->buildMenu( $p['ID'] );
  199. }
  200. self::getObject('template')->getPage()->addTag( 'admin_link_name', '' );
  201. self::getObject('template')->buildFromTemplates('header.tpl.php', 'message.tpl.php', 'footer.tpl.php');
  202. self::getObject('template')->getPage()->addTag('heading', $errorHeading);
  203. self::getObject('template')->getPage()->addTag('message', $errorMessage);
  204. }
  205. private function array_trim( $array )
  206. {
  207. while ( ! empty( $array ) && strlen( reset( $array ) ) === 0)
  208. {
  209. array_shift( $array );
  210. }
  211. while ( !empty( $array ) && strlen( end( $array ) ) === 0)
  212. {
  213. array_pop( $array );
  214. }
  215. return $array;
  216. }
  217. public function buildURL( $bits, $qs, $admin )
  218. {
  219. $admin = ( $admin == 1 ) ? $this->getSetting('admin_folder') . '/' : '';
  220. $the_rest = '';
  221. foreach( $bits as $bit )
  222. {
  223. $the_rest .= $bit . '/';
  224. }
  225. $the_rest = ( $qs != '' ) ? $the_rest . '?&' .$qs : $the_rest;
  226. return $this->getSetting('siteurl') . $admin . $the_rest;
  227. }
  228. public function pagination( $query, $limit, $offset, $method )
  229. {
  230. // method is the decision maker
  231. // - cache the query and return the cache id
  232. // - do the query and return the array?
  233. // - something else which is not implemented?
  234. // first thing first - let's build up Mr Query
  235. $temp_pg_query = $query;
  236. self::getObject('db')->executeQuery( $temp_pg_query );
  237. $num = self::getObject('db')->numRows();
  238. $limit_str = " LIMIT ";
  239. $limit_str .= ($offset * $limit) . ", " . $limit;
  240. $temp_pg_query .= $limit_str;
  241. if( $method == 'cache' )
  242. {
  243. $cache_id = self::getObject('db')->cacheQuery( $temp_pg_query );
  244. $tor = array( 'num' => $num, 'cache' => $cache_id );
  245. }
  246. elseif ( $method == 'do' )
  247. {
  248. self::getObject('db')->executeQuery( $temp_pg_query );
  249. $results = self::getObject('db')->getRows();
  250. $tor = array( 'num' => $num, 'results' => $results );
  251. }
  252. else
  253. {
  254. $tor = null;
  255. }
  256. // be nice...do some calculations - so modules don't have to!
  257. // num pages
  258. $num_pages = ceil($num/$limit);
  259. // is first
  260. $is_first = ( $offset == 0 ) ? true : false;
  261. // is last
  262. $is_last = ( ( $offset + 1 ) == $num_pages ) ? true : false;
  263. // cur page
  264. $cur_page = $offset;
  265. $tor['num_pages'] = $num_pages;
  266. $tor['is_first'] = $is_first;
  267. $tor['is_last'] = $is_last;
  268. $tor['cur_page'] = ( $num_pages == 0 ) ? 0 : $cur_page +1;
  269. return $tor;
  270. }
  271. }
  272. ?>