Skip to content

Commit

Permalink
Page: Enforcing 'main' region availability and using it for detail vi…
Browse files Browse the repository at this point in the history
…ews for blog posts, etc.
  • Loading branch information
vlucas committed Jul 12, 2011
1 parent 01d0b40 commit 3de7b4c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
27 changes: 25 additions & 2 deletions app/Module/Page/Controller.php
Expand Up @@ -55,6 +55,7 @@ public function viewUrl($pageUrl)

// Single module call?
// @todo Check against matched route name instead of general request params (? - may restict query string params from being used)
$mainContent = false;
if($request->module_name && $request->module_action) {
$moduleId = (int) $request->module_id;
$moduleName = $request->module_name;
Expand Down Expand Up @@ -105,8 +106,13 @@ public function viewUrl($pageUrl)
}
$moduleResponse = $kernel->dispatch($moduleObject, $moduleAction, array($request, $page, $module));

// Return content immediately, currently not wrapped in template
return $this->regionModuleFormat($request, $page, $module, $user, $moduleResponse);
// Set content as main content (detail view)
$mainContent = $this->regionModuleFormat($request, $page, $module, $user, $moduleResponse);

// Return content immediately if ajax request
if($request->isAjax()) {
return $mainContent;
}
}

// Load page template
Expand Down Expand Up @@ -137,6 +143,8 @@ public function viewUrl($pageUrl)

// Template Region Defaults
$regionModules = array();
$mainRegion = $template->regionMain();
$mainRegionName = $mainRegion['name'];
foreach($template->regions() as $regionName => $regionData) {
$regionModules[$regionName] = $regionData['content'];
}
Expand All @@ -155,8 +163,23 @@ public function viewUrl($pageUrl)
if(!isset($regionModules[$module->region]) || !is_array($regionModules[$module->region])) {
$regionModules[$module->region] = array();
}

// If we have a 'main' module render, don't dispatch/render other content in main region
if(false !== $mainContent) {
// If module goes in 'main' region, skip it
if($mainRegionName == $module->region) {
continue;
}
}

// Dispatch to content modules inside regions to render their contents
$regionModules[$module->region][] = $this->regionModuleFormat($request, $page, $module, $user, $moduleResponse);
}

// Replace main region content if set
if(false !== $mainContent) {
$regionModules[$mainRegionName] = array($mainContent);
}

// Replace region content
$regionModules = $kernel->events('cms')->filter('module_page_template_regions', $regionModules);
Expand Down
47 changes: 46 additions & 1 deletion app/Module/Page/Template.php
Expand Up @@ -27,6 +27,7 @@ class Template extends Alloy\View\Template
protected $_tokenRegionType = 'region';
protected $_regions = array();
protected $_regionsType = array();
protected $_regionMain;


/**
Expand Down Expand Up @@ -62,13 +63,32 @@ public function parse()

$regionName = $region->getAttribute('id');
$regionClass = $region->getAttribute('class');
$regionType = (false !== strpos($regionClass, 'cms_region_global')) ? 'global' : 'page';
$regionType = 'page';

// Global and main region types
if(false !== strpos($regionClass, 'cms_region_global')) {
$regionType = 'global';
// Add the standard 'cms_region' class in addition
$regionClass = $region->setAttribute('class', $region->getAttribute('class') . ' cms_region');
} elseif(false !== strpos($regionClass, 'cms_region_main')) {
$regionType = 'main';
// Add the standard 'cms_region' class in addition
$regionClass = $region->setAttribute('class', $region->getAttribute('class') . ' cms_region');

// Ensure there is only ONE main region
if(null !== $this->_regionMain) {
throw new Template\Exception("Template can only have one main region. Second one encountered at:<br />(" . \htmlentities($region->saveHTML()) . ")");
}
}

// Ensure region has a name (id attribute)
if(!$regionName) {
throw new Template\Exception("Template region does not have an id attribute.\n<br /> Parsing (" . \htmlentities($region->saveHTML()) . ")");
}

// Ouput array
$token = array(
'name' => $regionName,
'element' => $region,
'type' => $this->_tokenRegionType,
'namespace' => 'cms',
Expand All @@ -78,10 +98,19 @@ public function parse()

$this->_regions[$regionName] = $token;
$this->_regionsType[$regionType][] = $regionName;

// Store main region
if('main' == $regionType) {
$this->_regionMain = $token;
}

$tokens[] = $token;
}

// Ensure a main region exists
if(null === $this->_regionMain) {
throw new Template\Exception("Template must have a main region maked with the CSS class 'cms_region_main'.");
}

// TAGS
$xpath = new \DOMXPath($dom);
Expand Down Expand Up @@ -169,6 +198,22 @@ public function regionsType($type)

return isset($this->_regionsType[$type]) ? $this->_regionsType[$type] : array();
}


/**
* Get all found global regions
*
* @return array
*/
public function regionMain()
{
// Parse template if is has not been parsed already
if(!$this->_regionMain) {
$this->parse();
}

return $this->_regionMain;
}


/**
Expand Down

0 comments on commit 3de7b4c

Please sign in to comment.