class_BaseHelper.php

Go to the documentation of this file.
00001 <?php
00024 class BaseHelper extends BaseFrameworkSystem {
00028         private $valueInstance = null;
00029 
00033         private $content = "";
00034 
00038         private $groups = array();
00039 
00043         private $subGroups = array();
00044 
00048         private $previousGroupId = "";
00049 
00053         private $previousSubGroupId = "";
00054 
00058         private $totalCounter = 0;
00059 
00060         // Exception constants
00061         const EXCEPTION_XML_PARSER_ERROR             = 0x1e0;
00062         const EXCEPTION_XML_NODE_UNKNOWN             = 0x1e1;
00063         const EXCEPTION_XML_NODE_MISMATCH            = 0x1e2;
00064         const EXCEPTION_GROUP_NOT_OPENED             = 0x1e3;
00065         const EXCEPTION_GROUP_ALREADY_FOUND          = 0x1e4;
00066         const EXCEPTION_SUB_GROUP_ALREADY_FOUND      = 0x1e5;
00067         const EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED = 0x1e6;
00068         const EXCEPTION_NO_PREVIOUS_GROUP_OPENED     = 0x1e7;
00069 
00076         protected function __construct ($className) {
00077                 // Call parent constructor
00078                 parent::__construct($className);
00079 
00080                 // Clean up a little
00081                 $this->removeNumberFormaters();
00082                 $this->removeSystemArray();
00083         }
00084 
00091         protected final function addContent ($newContent) {
00092                 $this->content .= (string) trim($newContent)."\n";
00093         }
00094 
00101         protected function addHeaderContent ($content) {
00102                 // Add the header content
00103                 $this->groups['header']['content'] = (string) trim($content);
00104         }
00105 
00112         protected function addFooterContent ($content) {
00113                 // Add the footer content
00114                 $this->groups['footer']['content'] = (string) trim($content);
00115         }
00116 
00125         protected final function addContentToPreviousGroup ($newContent) {
00126                 // Check for sub/group
00127                 if ($this->ifSubGroupOpenedPreviously()) {
00128                         // Get sub group id
00129                         $subGroupId = $this->getPreviousSubGroupId();
00130 
00131                         // Add the content
00132                         $this->subGroups[$subGroupId]['content'] .= $newContent;
00133                 } elseif ($this->ifGroupOpenedPreviously()) {
00134                         // Get group id
00135                         $groupId = $this->getPreviousGroupId();
00136 
00137                         // Add the content
00138                         $this->groups[$groupId]['content'] .= $newContent;
00139                 } else {
00140                         // Add it directly
00141                         $this->addContent($newContent);
00142                 }
00143         }
00144 
00150         protected final function getContent () {
00151                 return $this->content;
00152         }
00153 
00160         public function assignField ($fieldName) {
00161                 // Get the value from value instance
00162                 $fieldValue = $this->getValueField($fieldName);
00163 
00164                 // Assign it with a template variable
00165                 $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $fieldValue);
00166         }
00167 
00177         public function assignFieldWithFilter ($fieldName, $filterMethod) {
00178                 // Get the value
00179                 $fieldValue = $this->getValueField($fieldName);
00180 
00181                 // Now filter it through the value through the filter method
00182                 $filteredValue = call_user_func_array(array($this, 'doFilter' . $this->convertToClassName($filterMethod)), array($fieldValue));
00183 
00184                 // Assign it with a template variable
00185                 $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $filteredValue);
00186         }
00187 
00196         public function prefetchValueInstance ($registryKey, $extraKey = null) {
00197                 // Get the required instance
00198                 $this->valueInstance = Registry::getRegistry()->getInstance($registryKey);
00199 
00200                 // Is the value instance valid?
00201                 if (is_null($this->valueInstance)) {
00202                         // Try to create it "from scratch", by first init extra instance
00203                         $extraInstance = null;
00204 
00205                         // Shall we get an extra instance?
00206                         if (!is_null($extraKey)) {
00207                                 // Get the extra instance.
00208                                 $extraInstance = Registry::getRegistry()->getInstance($extraKey);
00209                         } // END - if
00210 
00211                         // Get the requested instance
00212                         try {
00213                                 $this->valueInstance = ObjectFactory::createObjectByConfiguredName($registryKey . '_class', array($extraInstance));
00214 
00215                         } catch (FrameworkException $e) {
00216                                 // Okay, nothing found so throw a null pointer exception here
00217                                 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
00218                         }
00219                 } // END - if
00220         }
00221 
00233         protected function openGroupByIdContent ($groupId, $content, $tag) {
00234                 //* DEBUG: */ echo "OPEN:groupId={$groupId},content=<pre>".htmlentities($content)."</pre>\n";
00235                 // Is the group already there?
00236                 if (isset($this->groups[$groupId])) {
00237                         // Then throw an exception here
00238                         throw new HelperGroupAlreadyCreatedException(array($this, $groupId), self::EXCEPTION_GROUP_ALREADY_FOUND);
00239                 } // END - if
00240 
00241                 // Count one up
00242                 $this->totalCounter++;
00243 
00244                 // Add the group to the stack
00245                 $this->groups[$this->totalCounter] = $groupId;
00246                 $this->groups[$groupId]['opened']  = true;
00247                 $this->groups[$groupId]['content'] = sprintf("<!-- group %s opened (length: %s, tag: %s) //-->%s\n", $groupId, strlen($content), $tag, $content);
00248                 $this->groups[$groupId]['tag'] = $tag;
00249 
00250                 // Mark this group as previously opened
00251                 $this->setPreviousGroupId($groupId);
00252         }
00253 
00262         public function closePreviousGroupByContent ($content = "") {
00263                 // Check if any sub group was opened before
00264                 if ($this->ifSubGroupOpenedPreviously()) {
00265                         // Close it automatically
00266                         $this->closePreviousSubGroupByContent();
00267                 } // END - if
00268 
00269                 // Check if any group was opened before
00270                 if ($this->ifGroupOpenedPreviously() === false) {
00271                         // Then throw an exception
00272                         throw new HelperNoPreviousOpenedGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED);
00273                 } // END - if
00274 
00275                 // Get previous group
00276                 $groupId = $this->getPreviousGroupId();
00277 
00278                 // Is the content empty?
00279                 if ((empty($content)) && (!empty($this->groups[$groupId]['tag']))) {
00280                         // Get it from opener
00281                         $content = sprintf("<!-- group %s auto-closed //--></%s>", $groupId, $this->groups[$groupId]['tag']);
00282                 } // END - if
00283 
00284                 // Add content to it and mark it as closed
00285                 $this->groups[$groupId]['content'] .= sprintf("<!-- group %s closed (length: %s, tag: %s) //-->%s\n", $groupId, strlen($content), $this->groups[$groupId]['tag'], $content);
00286                 $this->groups[$groupId]['opened'] = false;
00287 
00288                 // Mark previous group as closed
00289                 $this->setPreviousGroupId("");
00290                 //* DEBUG: */ echo "CLOSE:groupId={$groupId}<br />\n";
00291         }
00292 
00304         protected function openSubGroupByIdContent ($subGroupId, $content, $tag) {
00305                 //* DEBUG: */ echo "OPEN:subGroupId={$subGroupId},content=".htmlentities($content)."<br />\n";
00306                 // Is the group already there?
00307                 if (isset($this->subGroups[$subGroupId])) {
00308                         // Then throw an exception here
00309                         throw new HelperSubGroupAlreadyCreatedException(array($this, $subGroupId), self::EXCEPTION_SUB_GROUP_ALREADY_FOUND);
00310                 } // END - if
00311 
00312                 // Count one up
00313                 $this->totalCounter++;
00314 
00315                 // Add the group to the stack
00316                 $this->subGroups[$this->totalCounter] = $subGroupId;
00317                 $this->subGroups[$subGroupId]['opened']  = true;
00318                 $this->subGroups[$subGroupId]['content'] = sprintf("<!-- sub-group %s opened (length: %s, tag: %s) //-->%s\n", $subGroupId, strlen($content), $tag, $content);
00319                 $this->subGroups[$subGroupId]['tag'] = $tag;
00320 
00321                 // Mark this group as previously opened
00322                 $this->setPreviousSubGroupId($subGroupId);
00323         }
00324 
00333         public function closePreviousSubGroupByContent ($content = "") {
00334                 // Check if any sub group was opened before
00335                 if ($this->ifSubGroupOpenedPreviously() === false) {
00336                         // Then throw an exception
00337                         throw new HelperNoPreviousOpenedSubGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED);
00338                 } // END - if
00339 
00340                 // Get previous sub group
00341                 $subGroupId = $this->getPreviousSubGroupId();
00342 
00343                 // Is the content empty?
00344                 if ((empty($content)) && (!empty($this->subGroups[$subGroupId]['tag']))) {
00345                         // Get it from opener
00346                         $content = sprintf("<!-- sub-group %s auto-closed //--></%s>", $subGroupId, $this->subGroups[$subGroupId]['tag']);
00347                 } // END - if
00348 
00349                 // Add content to it and mark it as closed
00350                 $this->subGroups[$subGroupId]['content'] .= sprintf("<!-- sub-group %s closed (length: %s, tag: %s) //-->%s\n", $subGroupId, strlen($content), $this->subGroups[$subGroupId]['tag'], $content);
00351                 $this->subGroups[$subGroupId]['opened'] = false;
00352 
00353                 // Mark previous sub group as closed
00354                 $this->setPreviousSubGroupId("");
00355                 //* DEBUG: */ echo "CLOSE:subGroupId={$subGroupId}<br />\n";
00356         }
00357 
00363         public function renderContent () {
00364                 // Initialize content
00365                 $content = "";
00366 
00367                 // Is header content there?
00368                 if (isset($this->groups['header'])) {
00369                         // Then add it
00370                         $content .= $this->groups['header']['content']."\n";
00371                 } // END - if
00372 
00373                 // Initiate content
00374                 $content .= $this->getContent();
00375 
00376                 // Now "walk" through all groups and sub-groups
00377                 for ($idx = 1; $idx <= $this->totalCounter; $idx++) {
00378                         // Is this a group and is it closed?
00379                         if ((isset($this->groups[$idx])) && ($this->groups[$this->groups[$idx]]['opened'] === false)) {
00380                                 // Then add it's content
00381                                 $groupContent = trim($this->groups[$this->groups[$idx]]['content']);
00382                                 //* DEBUG: */ echo "group={$this->groups[$idx]},content=<pre>".htmlentities($groupContent)."</pre><br />\n";
00383                                 $content .= $groupContent;
00384                         } elseif ((isset($this->subGroups[$idx])) && ($this->subGroups[$this->subGroups[$idx]]['opened'] === false)) {
00385                                 // Then add it's content
00386                                 $subGroupContent = $this->subGroups[$this->subGroups[$idx]]['content'];
00387                                 //* DEBUG: */ echo "subgroup={$this->subGroups[$idx]},content=<pre>".htmlentities($subGroupContent)."</pre><br />\n";
00388                                 $content .= trim($subGroupContent);
00389                         } else {
00390                                 // Something went wrong
00391                                 $this->debugInstance(__METHOD__."(): Something unexpected happened here.");
00392                         }
00393                 } // END - for
00394 
00395                 // Is footer content there?
00396                 if (isset($this->groups['footer'])) {
00397                         // Then add it
00398                         $content .= $this->groups['footer']['content']."\n";
00399                 } // END - if
00400 
00401                 // Return it
00402                 //* DEBUG: */ echo "content=<pre>".htmlentities($content)."</pre> (".strlen($content).")<br />\n";
00403                 return $content;
00404         }
00405 
00412         protected function ifGroupIsOpened ($groupId) {
00413                 // Is the group open?
00414                 $isOpened = ((isset($this->groups[$groupId])) && ($this->groups[$groupId]['opened'] === true));
00415 
00416                 // Return status
00417                 return $isOpened;
00418         }
00419 
00426         public function getValueField ($fieldName) {
00427                 // Get the field value
00428                 $fieldValue = call_user_func_array(array($this->valueInstance, 'getField'), array($fieldName));
00429 
00430                 // Return it
00431                 return $fieldValue;
00432         }
00433 
00439         public final function getValueInstance () {
00440                 return $this->valueInstance;
00441         }
00442 
00448         protected final function ifGroupOpenedPreviously () {
00449                 $groupOpened = (!empty($this->previousGroupId));
00450                 return $groupOpened;
00451         }
00452 
00458         protected final function ifSubGroupOpenedPreviously () {
00459                 $subGroupOpened = (!empty($this->previousSubGroupId));
00460                 return $subGroupOpened;
00461         }
00462 
00468         protected final function getPreviousGroupId () {
00469                 return $this->previousGroupId;
00470         }
00471 
00478         protected final function setPreviousGroupId ($previousGroupId) {
00479                 $this->previousGroupId = (string) $previousGroupId;
00480         }
00481 
00487         protected final function getPreviousSubGroupId () {
00488                 return $this->previousSubGroupId;
00489         }
00490 
00497         protected final function setPreviousSubGroupId ($previousSubGroupId) {
00498                 $this->previousSubGroupId = (string) $previousSubGroupId;
00499         }
00500 }
00501 
00502 // [EOF]
00503 ?>

Generated on Mon Dec 8 01:06:46 2008 for Ship-Simulator by  doxygen 1.5.6