/framework/core/zone/Zone.php

http://zoop.googlecode.com/ · PHP · 200 lines · 151 code · 36 blank · 13 comment · 30 complexity · 72e80d0b32aef07ee8576d01470ece96 MD5 · raw file

  1. <?php
  2. abstract class Zone extends Object
  3. {
  4. private $requestInfo;
  5. private $params;
  6. function init($requestInfo = NULL, $params = array())
  7. {
  8. if(!$requestInfo)
  9. {
  10. $this->requestInfo = array();
  11. $this->requestInfo[] = array('zone' => '');
  12. }
  13. else
  14. {
  15. $this->requestInfo = $requestInfo;
  16. $this->requestInfo[] = array('zone' => strtolower(substr(get_class($this), 4)));
  17. }
  18. $this->params = array();
  19. }
  20. public function initZone($p, $z) {}
  21. //
  22. // methods to override
  23. //
  24. function getParamNames()
  25. {
  26. return array();
  27. }
  28. // the main functionality of zone
  29. function handleRequest($pathParts)
  30. {
  31. $originalPart = array_shift($pathParts);
  32. $thisPart = ucfirst($originalPart);
  33. $zoneName = "Zone$thisPart";
  34. $pageName = "page$thisPart";
  35. $postName = "post$thisPart";
  36. $defaultName = "pageDefault";
  37. $defaultPostName = "postDefault";
  38. // first try to find a matching page
  39. if( RequestIsPost() && $this->_methodExists($postName) )
  40. $foundPage = $postName;
  41. else
  42. {
  43. if( $this->_methodExists($pageName) )
  44. $foundPage = $pageName;
  45. }
  46. // next look for an existing zone that is not this class
  47. if( !isset($foundPage) )
  48. {
  49. if( $thisPart && class_exists($zoneName) )
  50. {
  51. $newZone = new $zoneName();
  52. $newZone->init($this->requestInfo, $this->params);
  53. // grab the zone params from $pathParts
  54. foreach($newZone->getParamNames() as $thisParamName)
  55. {
  56. if(count($pathParts) == 0)
  57. trigger_error("$zoneName requires more paramaters than are available");
  58. $paramValue = array_shift($pathParts);
  59. $newZone->requestInfo[count($newZone->requestInfo) - 1]['params'][] = $thisParamName;
  60. $newZone->params[$thisParamName] = $paramValue;
  61. }
  62. $this->initZone($pathParts, $this->params);
  63. $newZone->handleRequest($pathParts);
  64. }
  65. else
  66. {
  67. // now look for a default post page
  68. if( RequestIsPost() && $this->_methodExists($defaultPostName) )
  69. {
  70. $foundPage = $defaultPostName;
  71. }
  72. else
  73. {
  74. // finally look for a defaultpage
  75. if( $this->_methodExists($defaultName) )
  76. {
  77. $foundPage = $defaultName;
  78. }
  79. else
  80. {
  81. trigger_error("no valid page for $thisPart in " . get_class($this));
  82. }
  83. }
  84. }
  85. }
  86. // if we found a page then run it
  87. if( isset($foundPage) )
  88. {
  89. $postfix = $foundPage == 'pageDefault' || $foundPage == 'postDefault' ? array(0 => 'default', 1 => $originalPart) : array(0 => $originalPart);
  90. $pageParams = array_merge($postfix, $pathParts);
  91. $this->initZone($pageParams, $this->params);
  92. if( $this->_methodExists('initPages') )
  93. $this->initPages($pageParams, $this->params);
  94. $this->$foundPage($pageParams, $this->params);
  95. if( RequestIsGet() && $this->_methodExists('closePages') )
  96. $this->closePages($pageParams, $this->params);
  97. if( RequestIsPost() && $this->_methodExists('closePosts') )
  98. $this->closePosts($pageParams, $this->params);
  99. }
  100. }
  101. //
  102. // path manipulation functions
  103. //
  104. function setParam($name, $value)
  105. {
  106. $this->params[$name] = $value;
  107. }
  108. function getRequestPath($numLevels = 0)
  109. {
  110. $path = '';
  111. $maxLevel = count($this->requestInfo) - 1;
  112. if($numLevels < 0)
  113. $maxLevel += $numLevels;
  114. foreach($this->requestInfo as $index => $thisZoneInfo)
  115. {
  116. $path .= $thisZoneInfo['zone'];
  117. if(isset($thisZoneInfo['params']))
  118. {
  119. foreach($thisZoneInfo['params'] as $paramName)
  120. $path .= '/' . $this->params[$paramName];
  121. }
  122. if($maxLevel == $index)
  123. break;
  124. }
  125. return $path;
  126. }
  127. function getUrl($extra = NULL, $numLevels = 0)
  128. {
  129. $url = script_url;
  130. if($this->getRequestPath($numLevels))
  131. $url .= '/' . $this->getRequestPath($numLevels);
  132. if($extra)
  133. $url .= '/' . $extra;
  134. return $url;
  135. }
  136. function getBackUrl($extra = NULL, $numLevels = 0)
  137. {
  138. $url = back_script_url;
  139. if($this->getRequestPath($numLevels))
  140. $url .= '/' . $this->getRequestPath($numLevels);
  141. if($extra)
  142. $url .= '/' . $extra;
  143. return $url;
  144. }
  145. function redirect($extra)
  146. {
  147. $url = $this->getUrl($extra);
  148. Redirect($url);
  149. }
  150. function pushZone($zonePlusExtra)
  151. {
  152. $url = $this->getUrl($zonePlusExtra);
  153. Redirect($url);
  154. }
  155. function popZone()
  156. {
  157. trigger_error('not done yet');
  158. }
  159. function switchZone($extra)
  160. {
  161. $url = $this->getUrl('', -1) . '/' . $extra;
  162. Redirect($url);
  163. }
  164. }