PageRenderTime 17ms CodeModel.GetById 10ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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