From 3de7b4c58bcddd119b31c52a4d9cad9afce5561b Mon Sep 17 00:00:00 2001 From: Vance Lucas Date: Tue, 12 Jul 2011 14:54:52 -0500 Subject: [PATCH] Page: Enforcing 'main' region availability and using it for detail views for blog posts, etc. --- app/Module/Page/Controller.php | 27 +++++++++++++++++-- app/Module/Page/Template.php | 47 +++++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/app/Module/Page/Controller.php b/app/Module/Page/Controller.php index da296c2..ebc3067 100755 --- a/app/Module/Page/Controller.php +++ b/app/Module/Page/Controller.php @@ -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; @@ -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 @@ -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']; } @@ -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); diff --git a/app/Module/Page/Template.php b/app/Module/Page/Template.php index 6c14cc5..79f20df 100755 --- a/app/Module/Page/Template.php +++ b/app/Module/Page/Template.php @@ -27,6 +27,7 @@ class Template extends Alloy\View\Template protected $_tokenRegionType = 'region'; protected $_regions = array(); protected $_regionsType = array(); + protected $_regionMain; /** @@ -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:
(" . \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
Parsing (" . \htmlentities($region->saveHTML()) . ")"); } // Ouput array $token = array( + 'name' => $regionName, 'element' => $region, 'type' => $this->_tokenRegionType, 'namespace' => 'cms', @@ -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); @@ -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; + } /**