[Cs-content-commits] SF.net SVN: cs-content:[376] trunk/1.0
PHP Templating & Includes System
Brought to you by:
crazedsanity
From: <cra...@us...> - 2009-05-06 19:30:08
|
Revision: 376 http://cs-content.svn.sourceforge.net/cs-content/?rev=376&view=rev Author: crazedsanity Date: 2009-05-06 19:30:00 +0000 (Wed, 06 May 2009) Log Message: ----------- Fix PHP warning, allow multiple tab block rows in same set. NOTE::: cs_tabs needs some work to make it more useful and extensible. There is some stuff to figure out with logic in load_tabs_template()--i.e. is it still needed? NOTE2:: cs_tabs will overwrite existing tabs of the same name, so duplicate tab names aren't allowed... but some apps may require this. Consider adding. /cs_genericPage.class.php: * get_block_row_defs(): -- check if the index in templateVars is set before evaluating it. /cs_tabs.class.php: * MAIN::: -- set tabsArray as an array() initially. -- NEW (private) VAR: $defaultSuffix='tab' (default if no block row suffix is given (i.e. this would require "selected_tab" and "unselected_tab" to function). * load_tabs_template(): -- no longer throws exceptions, nor does it do any real checking -- rips block rows for the given template var... * add_tab_array(): -- ARG CHANGE: NEW ARG: #2 ($useSuffix=null) -- passes $useSuffix when calling add_tab(). * add_tab(): -- ARG CHANGE: NEW ARG: #2 ($useSuffix=null) -- Now stores an array of data instead of just the text of the url: the "suffix" denotes what block row to use, allowing multiple different rows to be used within the same tab set. -- if no suffix is given, the default suffix is used. * display_tabs(): -- code to automatically select a tab if one is not already selected. -- instead of parsing a known templateRow (block row), it is dynamic, determined based on the "suffix" given. -- can throw an exception if referenced templateRow doesn't exist. -- updated exception (when no tabs present) to use magic __METHOD__ constant. * tab_exists() [NEW]: -- method to determine if the named tab exists or not. * rename_tab() [NEW]: -- rename existing tab to a new one. -- throws an exception if the named tab doesn't exist or if a tab named as the new name already exists. Modified Paths: -------------- trunk/1.0/cs_genericPage.class.php trunk/1.0/cs_tabs.class.php Modified: trunk/1.0/cs_genericPage.class.php =================================================================== --- trunk/1.0/cs_genericPage.class.php 2009-05-06 15:23:00 UTC (rev 375) +++ trunk/1.0/cs_genericPage.class.php 2009-05-06 19:30:00 UTC (rev 376) @@ -551,8 +551,8 @@ //NOTE: the value 30 isn't just a randomly chosen length; it's the minimum // number of characters to have a block row. EG: "<!-- BEGIN x --><!-- END x -->" - $templateContents = $this->templateVars[$templateVar]; - if(strlen($templateContents) >= 30) { + if(isset($this->templateVars[$templateVar]) && strlen($this->templateVars[$templateVar]) >= 30) { + $templateContents = $this->templateVars[$templateVar]; //looks good to me. Run the regex... $flags = PREG_PATTERN_ORDER; $reg = "/<!-- BEGIN (\S{1,}) -->/"; Modified: trunk/1.0/cs_tabs.class.php =================================================================== --- trunk/1.0/cs_tabs.class.php 2009-05-06 15:23:00 UTC (rev 375) +++ trunk/1.0/cs_tabs.class.php 2009-05-06 19:30:00 UTC (rev 376) @@ -8,18 +8,15 @@ class cs_tabs extends cs_contentAbstract { - private $tabsArr; + private $tabsArr=array(); private $selectedTab; private $csPageObj; private $templateVar; - /** Block row with the "selected" tab */ - private $selectedTabContent; + /** This is the default suffix to use when none is given during the add_tab() call. */ + private $defaultSuffix='tab'; - /** Block row with the "unselected" tab */ - private $unselectedTabContent; - //--------------------------------------------------------------------------------------------- /** * Build the object, and parses the given template. Tabs must be added & selected manually. @@ -64,26 +61,21 @@ //now let's parse it for the proper block rows. $blockRows = $this->csPageObj->rip_all_block_rows($this->templateVar); - if(count($blockRows) < 2 || !isset($blockRows['selected_tab']) || !isset($blockRows['unselected_tab'])) { - //not enough blocks, or they're not properly named. - throw new exception("cs_tabs::load_tabs_template(): failed to retrieve the required block rows"); - } - else { - //got the rows. Yay! - $this->selectedTabContent = $blockRows['selected_tab']; - $this->unselectedTabContent = $blockRows['unselected_tab']; - } + #if(count($blockRows) < 2) { + # //not enough blocks, or they're not properly named. + # throw new exception("cs_tabs::load_tabs_template(): failed to retrieve the required block rows"); + #} }//end load_tabs_template() //--------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------- - public function add_tab_array(array $tabs) { + public function add_tab_array(array $tabs, $useSuffix=null) { $retval = 0; foreach($tabs as $name=>$url) { //call an internal method to do it. - $retval += $this->add_tab($name, $url); + $retval += $this->add_tab($name, $url, $useSuffix); } return($retval); @@ -107,9 +99,18 @@ //--------------------------------------------------------------------------------------------- - public function add_tab($tabName, $url) { + public function add_tab($tabName, $url, $useSuffix=null) { + + //set the default suffix. + if(is_null($useSuffix)) { + $useSuffix = $this->defaultSuffix; + } + //add it to an array. - $this->tabsArr[$tabName] = $url; + $this->tabsArr[$tabName] = array( + 'url' => $url, + 'suffix' => $useSuffix + ); }//end add_tab() //--------------------------------------------------------------------------------------------- @@ -120,16 +121,33 @@ * Call this to add the parsed tabs into the page. */ public function display_tabs() { + + if(!strlen($this->selectedTab)) { + $keys = array_keys($this->tabsArr); + $this->select_tab($keys[0]); + } + if(is_array($this->tabsArr) && count($this->tabsArr)) { $this->load_tabs_template(); $finalString = ""; //loop through the array. - foreach($this->tabsArr as $tabName=>$url) { - $useTabContent = $this->unselectedTabContent; - if(strtolower($tabName) === strtolower($this->selectedTab)) { - //it's selected. - $useTabContent = $this->selectedTabContent; + foreach($this->tabsArr as $tabName=>$tabData) { + + $url = $tabData['url']; + $suffix = $tabData['suffix']; + + $blockRowName = 'unselected_'. $suffix; + if(strtolower($tabName) == strtolower($this->selectedTab)) { + $blockRowName = 'selected_'. $suffix; } + + if(isset($this->csPageObj->templateRows[$blockRowName])) { + $useTabContent = $this->csPageObj->templateRows[$blockRowName]; + } + else { + throw new exception(__METHOD__ ."(): failed to load block row (". $blockRowName .") for tab (". $tabName .")". $this->csPageObj->gfObj->debug_print($this->csPageObj->templateRows,0)); + } + $parseThis = array( 'title' => $tabName, 'url' => $url @@ -142,11 +160,40 @@ } else { //something bombed. - throw new exception("cs_tabs::display_tabs(): no tabs to add"); + throw new exception(__METHOD__ ."(): no tabs to add"); } }//end display_tabs() //--------------------------------------------------------------------------------------------- + + //--------------------------------------------------------------------------------------------- + /** + * Determine if the given named tab exists (returns boolean true/false) + */ + public function tab_exists($tabName) { + $retval = false; + if(isset($this->tabsArr[$tabName])) { + $retval = true; + } + return($retval); + }//end tab_exists() + //--------------------------------------------------------------------------------------------- + + + + //--------------------------------------------------------------------------------------------- + public function rename_tab($tabName, $newTabName) { + if($this->tab_exists($tabName) && !$this->tab_exists($newTabName)) { + $tabContents = $this->tabsArr[$tabName]; + unset($this->tabsArr[$tabName]); + $this->tabsArr[$newTabName] = $tabContents; + } + else { + throw new exception(__METHOD__ .": tried to rename non-existent tab (". $tabName .") to (". $newTabName .")"); + } + }//end rename_tab(); + //--------------------------------------------------------------------------------------------- + } ?> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |