/** @return PntRequestHandler appropriate to handle a request.
* First a class named <pntType><pntHandler> is tried.
* If it can not be included, Object<pntHandler> will be used.
* If pntHandler param is missing in $requestData a default will be used, see implementation fo this method.
* A special case is if pntHandler=(MtoN)PropertyPage: then <pntType>Property<pntProperty>Page is tried first,
* @param array $requestData associative, of string like $_REQUEST,
* used to decide which class to include and instantiate,
* and passed to the PntRequestHandler as the request data.
* @param string $dir appears to be ignored. $thisfunction (->() will be used.
* For each class name inclusion is first tried from $this->(),
* If the class is not found, a class from the classes root folder is tried
* @throws PntValidationException is no class could be included
*/
function getRequestHandler($requestData, $dir=null) {
if (!$dir) $dir = $this->();
$id = isSet($requestData["id"]) ? $requestData["id"] : null;
$specifiedHandler = $handler = isSet($requestData["pntHandler"])
? $requestData["pntHandler"] : null;
$property = ucFirst(isSet($requestData["pntProperty"]) ? $requestData["pntProperty"] : '');
$type = isSet($requestData["pntType"]) ? $requestData["pntType"] : null;
if ($handler=='PropertyPage' || $handler=='MtoNPropertyPage')
$handler = "Property$property".'Page';
elseif (!$handler) { //no pntHandler param, use default:
if ($property == 'pntList')
$handler = 'IndexPage';
elseif ($id !== null)
$handler = 'EditDetailsPage';
else
$handler = 'IndexPage';
$specifiedHandler = $handler;
}
$attempted = array();
$info = null;
$handlerClass = $type
? $this->("$type$handler", $attempted)
: null;
if (!$handlerClass && $property) {
//there is no specific handler for this type and property, try type-handler
$handler = $specifiedHandler;
$handlerClass = $this->("$type$handler", $attempted);
}
if (!$handlerClass)
//there is no specific handler for this type, try generic handler from same dir
$handlerClass = $this->controller->($this, $requestData, $handler, $attempted);
if (!$handlerClass) {
$name = $this->();
$errorMessage = "$name - handler not found: $handler, tried: <BR>\n";
$errorMessage .= $this->($attempted);
throw new PntValidationException($errorMessage);
}
// print "<BR>Handler: $handlerClass $included";
if (!is_subclassOr($handlerClass, 'PntRequestHandler'))
throw new PntValidationException($handlerClass. ' does not inherit from PntRequestHandler');
$result = new $handlerClass($this, $requestData);
if ($this->() == 'verbose') {
$info = 'Handlers tried<BR>(one of last two succeeded): ';
$info .= $this->($attempted);
$result->($info);
}
return $result;
}
|