PageRenderTime 31ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/RD/RapiDev/Module/Page.php

https://github.com/Getty/historical-php-rapidev
PHP | 273 lines | 203 code | 44 blank | 26 comment | 26 complexity | 88490c5bd7676cfde0d0833eaa413bcf MD5 | raw file
  1. <?
  2. /**
  3. * RapiDev, Rapid Development PHP Application Framework
  4. *
  5. * PHP version 5
  6. *
  7. * Page Class (the classic Router)
  8. *
  9. * LICENSE:
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * @author Torsten Raudssus <torsten@raudssus.de>
  21. * @copyright 2007 Torsten Raudssus
  22. * @license GPL-2
  23. *
  24. */
  25. class RD_RapiDev_Module_Page extends RDM {
  26. public $Page;
  27. public $Pages;
  28. public $DefaultPage = 'default';
  29. public $DefaultTemplate = 'structure.tpl';
  30. public $GotoPage;
  31. public $PagesAllowed = true;
  32. public $PageCache = Array();
  33. public static $FunctionSeparator = '_';
  34. public static $FunctionPrefix = 'Page_';
  35. public static $Directory = 'pages';
  36. public static $ClassPrefix = 'RD_Page';
  37. public static $PageStart = 'start';
  38. public static $PageEnd = 'end';
  39. public static $RD_Functions = Array(
  40. 'SetPageDefaultTemplate',
  41. 'UpdatePages',
  42. 'SetGotoPage',
  43. 'SetPageDefault',
  44. 'GetPageDefault',
  45. 'SetPagesAllowed',
  46. 'GetPagesAllowed',
  47. 'GetPages',
  48. 'PageExist',
  49. 'GetPage'
  50. );
  51. public static $RD_Depencies = Array();
  52. public function Setup() {
  53. RD::RequireClass('RD_Page'); return;
  54. }
  55. public function HookPostInit() {
  56. if (!($this->Page = $this->Get('page'))) {
  57. $this->Page = $this->DefaultPage;
  58. }
  59. $this->UpdatePages();
  60. return;
  61. }
  62. public function SetPageDefault($Value) {
  63. $this->DefaultPage = $Value;
  64. return;
  65. }
  66. public function SetPageDefaultTemplate($Value) {
  67. $this->DefaultTemplate = $Value;
  68. return;
  69. }
  70. public function GetPageDefault() {
  71. return $this->DefaultPage;
  72. }
  73. public function SetPagesAllowed($Pages) {
  74. $this->PagesAllowed = $Pages;
  75. return;
  76. }
  77. public function GetPagesAllowed() {
  78. return $this->PagesAllowed;
  79. }
  80. public function GetPages($key = null) {
  81. if($key !== null) {
  82. if(!array_key_exists($key, $this->Pages) or !is_array($this->Pages)) {
  83. return false;
  84. }
  85. return $this->Pages[$key];
  86. }
  87. return $this->Pages;
  88. }
  89. public function GetPage() {
  90. return $this->Page;
  91. }
  92. public function SetGotoPage($Page) {
  93. $this->GotoPage = $Page;
  94. return;
  95. }
  96. public function UpdatePages($Page = NULL) {
  97. $this->Hook('PrePageUpdate');
  98. if ($Page !== NULL && is_string($Page) && !empty($Page)) {
  99. $this->Page = $Page;
  100. } elseif (isset($this->main->POST['page'])) {
  101. $this->Page = $this->main->POST['page'];
  102. } elseif (isset($this->main->GET['page'])) {
  103. $this->Page = $this->main->GET['page'];
  104. } elseif (isset($this->main->SESSION['page'])) {
  105. $this->Page = $this->main->SESSION['page'];
  106. }
  107. RDD::Log('Resulting Page: '.$this->Page,TRACE,1211);
  108. $this->Pages = explode('_',$this->Page);
  109. RDD::Log('Resulting Pages:',TRACE,1212);
  110. RDD::Log($this->Pages,TRACE,1212);
  111. $this->Hook('PostPageUpdate');
  112. return;
  113. }
  114. protected function GoToCheck() {
  115. if(isset($this->GotoPage)) {
  116. RDD::Log('Page module found a goto to '.$this->GotoPage,TRACE,1210);
  117. $this->UpdatePages($this->GotoPage);
  118. unset($this->GotoPage);
  119. $this->CallPage();
  120. return false;
  121. } else {
  122. return true;
  123. }
  124. }
  125. public function PageExist($Pages) {
  126. if (!is_array($Pages)) {
  127. $Pages = explode('_'.$Pages);
  128. }
  129. foreach($Pages as $Key => $Page) {
  130. $Pages[$Key] = ucfirst($Page);
  131. }
  132. }
  133. /**
  134. * Creates needed object and loads all needed methods for the object
  135. **/
  136. public function CallPage() {
  137. if ($this->PagesAllowed === false) {
  138. return;
  139. }
  140. if(!$this->GoToCheck()) {
  141. return;
  142. }
  143. if ($this->PagesAllowed !== true && is_array($this->PagesAllowed)) {
  144. $Access = false;
  145. foreach($this->PagesAllowed as $PageAllowed) {
  146. $Length = strlen($PageAllowed);
  147. if (substr($this->Page,0,$Length) == $PageAllowed) {
  148. $Access = true;
  149. break;
  150. }
  151. }
  152. if (!$Access) {
  153. throw new Exception('RDM_Page: not allowed to access the Page: "'.$this->Page.'"');
  154. }
  155. }
  156. $this->Hook('PageStart');
  157. $RunCache = Array();
  158. foreach($this->Pages as $Key => $PagePart) {
  159. $FileName = self::$Directory;
  160. $ClassName = self::$ClassPrefix;
  161. for($i = 0 ; $i <= $Key ; $i++) {
  162. $FileName .= DIR_SEP.$this->Pages[$i];
  163. $ClassName .= '_'.ucfirst($this->Pages[$i]);
  164. }
  165. if (!isset($this->PageCache[$ClassName])) {
  166. try {
  167. RD::RequireClass($ClassName);
  168. } catch (Exception $e) {
  169. $FileName .= '.php';
  170. if ($File = $this->File($FileName)) {
  171. require_once($File);
  172. }
  173. }
  174. if (class_exists($ClassName)) {
  175. RDD::Log('Page class '.$ClassName.' loaded into RunCache',TRACE,1221);
  176. $this->PageCache[$ClassName] = new $ClassName($this->main);
  177. } else {
  178. RDD::Log('Page class '.$ClassName.' or file '.$FileName.' not exist',WARN);
  179. continue;
  180. }
  181. }
  182. $RunCache[$ClassName] = $this->PageCache[$ClassName];
  183. }
  184. if (!empty($RunCache)) {
  185. $MethodStack = Array(self::$PageStart);
  186. $PageDepth = count($this->Pages);
  187. $TempPageStack = Array();
  188. for($i = 0 ; $i < $PageDepth-1 ; $i++) {
  189. $TempPageStack[] = $this->Pages[$i];
  190. $MethodStack[] = implode(self::$FunctionSeparator,$TempPageStack).self::$FunctionSeparator.self::$PageStart;
  191. }
  192. $MethodStack[] = implode(self::$FunctionSeparator,$this->Pages);
  193. while (!empty($TempPageStack)) {
  194. $MethodStack[] = implode(self::$FunctionSeparator,$TempPageStack).self::$FunctionSeparator.self::$PageEnd;
  195. array_pop($TempPageStack);
  196. }
  197. $MethodStack[] = self::$PageEnd;
  198. foreach($MethodStack as $Method) {
  199. foreach($RunCache as $PageClass) {
  200. $PageMethod = self::$FunctionPrefix.$Method;
  201. RDD::Log('Trying PageMethod: "'.$PageMethod.'" on "'.get_class($PageClass).'"',TRACE,1211);
  202. if ($PageClass->MethodExists($PageMethod)) {
  203. $PageClass->$PageMethod();
  204. if(!$this->GoToCheck()) {
  205. return;
  206. }
  207. }
  208. }
  209. }
  210. }
  211. }
  212. public function PageStart() {
  213. $this->View('RD_Page');
  214. $this->SetTemplate($this->DefaultTemplate);
  215. $this->CallPage();
  216. $this->Assign('page',$this->Page);
  217. $this->Assign('pages',$this->Pages);
  218. }
  219. public function PageFinish() {
  220. $this->ViewFinish();
  221. }
  222. public function HookFinish() {
  223. $this->PageFinish();
  224. return;
  225. }
  226. public function HookStart() {
  227. $this->PageStart();
  228. }
  229. }