16/05/2012 8:12pm

PHP | Problem - ProductPageExtension for having DataObject URLSegment in url with a Page Controller actions


<?php


/**
* This Extension will search for a Product before ContentController init();
* When a Product is found, a new Product_Controller will be instantiated.
* The URLSegment of the Product will be assigned to urlParam['ID']
* The Page Title, MetaKeywords and MetaDescription will set to the products Title, MetaKeywords and MetaDescription
* with $controller->setProduct($product);
* The Parent Page will be set with the $controller->setParent($page);
* So we have correct breadcrumbs. Note that we override the method BreadCrumbs in the Product_Controller
* for adding the Product Title in after the Page BreadCrumbs
**/

class ProductPageExtension extends Extension {

function onBeforeInit() {
if($request = $this->owner->getRequest()) {
if($params = $request->latestParams()) {
if(isset($params["Action"]) && $action = $params["Action"] && isset($params["ID"]) && $URLSegment = $params["ID"]) {
if($product = DataObject::get_one('Product', "URLSegment='".Convert::raw2sql($URLSegment)."'")) {
if($page = $product->SubNiveauPagina()) {
$controller = new Product_Controller($page);
$controller->setURLParams(array("ID" => $URLSegment));
$controller->setProduct($product);
$controller->setParent($page);
$controller->init();
echo $controller->render();
exit();
}
}
}
}
}
}
}


1 Comments 1 Solutions

16/05/2012 9:16pm

PHP | Solution - Anonymous

Added SS_HTTPResponse ans request to the Controller



/**
* This Extension will search for a Product before ContentController init();
* When a Product is found, a new Product_Controller will be instantiated.
* The URLSegment of the Product will be assigned to urlParam['ID']
* The Page Title, MetaKeywords and MetaDescription will set to the products Title, MetaKeywords and MetaDescription
* with $controller->setProduct($product);
* The Parent Page will be set with the $controller->setParent($page);
* So we have correct breadcrumbs. Note that we override the method BreadCrumbs in the Product_Controller
* for adding the Product Title in after the Page BreadCrumbs
**/

class ProductPageExtension extends Extension {

function onBeforeInit() {
if($request = $this->owner->getRequest()) {
if($params = $request->latestParams()) {
if(isset($params["Action"]) && $action = $params["Action"] && isset($params["ID"]) && $URLSegment = $params["ID"]) {
if($product = DataObject::get_one('Product', "URLSegment='".Convert::raw2sql($URLSegment)."'")) {
if($page = $product->SubNiveauPagina()) {

$controller = new Product_Controller($page);
$response = new SS_HTTPResponse(NULL, 200);

$controller->setURLParams(array("ID" => $URLSegment));
$controller->request = $request;
$controller->response = $response;

$controller->init();
$controller->setProduct($product);
$controller->setParent($page);

$body = $controller->render();

ContentNegotiator::process($controller->response);
HTTP::add_cache_headers($controller->response);

echo $body;
exit();

}
}
}
}
}
}
}

Post Comment