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
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
00078 parent::__construct($className);
00079
00080
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
00103 $this->groups['header']['content'] = (string) trim($content);
00104 }
00105
00112 protected function addFooterContent ($content) {
00113
00114 $this->groups['footer']['content'] = (string) trim($content);
00115 }
00116
00125 protected final function addContentToPreviousGroup ($newContent) {
00126
00127 if ($this->ifSubGroupOpenedPreviously()) {
00128
00129 $subGroupId = $this->getPreviousSubGroupId();
00130
00131
00132 $this->subGroups[$subGroupId]['content'] .= $newContent;
00133 } elseif ($this->ifGroupOpenedPreviously()) {
00134
00135 $groupId = $this->getPreviousGroupId();
00136
00137
00138 $this->groups[$groupId]['content'] .= $newContent;
00139 } else {
00140
00141 $this->addContent($newContent);
00142 }
00143 }
00144
00150 protected final function getContent () {
00151 return $this->content;
00152 }
00153
00160 public function assignField ($fieldName) {
00161
00162 $fieldValue = $this->getValueField($fieldName);
00163
00164
00165 $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $fieldValue);
00166 }
00167
00177 public function assignFieldWithFilter ($fieldName, $filterMethod) {
00178
00179 $fieldValue = $this->getValueField($fieldName);
00180
00181
00182 $filteredValue = call_user_func_array(array($this, 'doFilter' . $this->convertToClassName($filterMethod)), array($fieldValue));
00183
00184
00185 $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $filteredValue);
00186 }
00187
00196 public function prefetchValueInstance ($registryKey, $extraKey = null) {
00197
00198 $this->valueInstance = Registry::getRegistry()->getInstance($registryKey);
00199
00200
00201 if (is_null($this->valueInstance)) {
00202
00203 $extraInstance = null;
00204
00205
00206 if (!is_null($extraKey)) {
00207
00208 $extraInstance = Registry::getRegistry()->getInstance($extraKey);
00209 }
00210
00211
00212 try {
00213 $this->valueInstance = ObjectFactory::createObjectByConfiguredName($registryKey . '_class', array($extraInstance));
00214
00215 } catch (FrameworkException $e) {
00216
00217 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
00218 }
00219 }
00220 }
00221
00233 protected function openGroupByIdContent ($groupId, $content, $tag) {
00234
00235
00236 if (isset($this->groups[$groupId])) {
00237
00238 throw new HelperGroupAlreadyCreatedException(array($this, $groupId), self::EXCEPTION_GROUP_ALREADY_FOUND);
00239 }
00240
00241
00242 $this->totalCounter++;
00243
00244
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
00251 $this->setPreviousGroupId($groupId);
00252 }
00253
00262 public function closePreviousGroupByContent ($content = "") {
00263
00264 if ($this->ifSubGroupOpenedPreviously()) {
00265
00266 $this->closePreviousSubGroupByContent();
00267 }
00268
00269
00270 if ($this->ifGroupOpenedPreviously() === false) {
00271
00272 throw new HelperNoPreviousOpenedGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED);
00273 }
00274
00275
00276 $groupId = $this->getPreviousGroupId();
00277
00278
00279 if ((empty($content)) && (!empty($this->groups[$groupId]['tag']))) {
00280
00281 $content = sprintf("<!-- group %s auto-closed //--></%s>", $groupId, $this->groups[$groupId]['tag']);
00282 }
00283
00284
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
00289 $this->setPreviousGroupId("");
00290
00291 }
00292
00304 protected function openSubGroupByIdContent ($subGroupId, $content, $tag) {
00305
00306
00307 if (isset($this->subGroups[$subGroupId])) {
00308
00309 throw new HelperSubGroupAlreadyCreatedException(array($this, $subGroupId), self::EXCEPTION_SUB_GROUP_ALREADY_FOUND);
00310 }
00311
00312
00313 $this->totalCounter++;
00314
00315
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
00322 $this->setPreviousSubGroupId($subGroupId);
00323 }
00324
00333 public function closePreviousSubGroupByContent ($content = "") {
00334
00335 if ($this->ifSubGroupOpenedPreviously() === false) {
00336
00337 throw new HelperNoPreviousOpenedSubGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED);
00338 }
00339
00340
00341 $subGroupId = $this->getPreviousSubGroupId();
00342
00343
00344 if ((empty($content)) && (!empty($this->subGroups[$subGroupId]['tag']))) {
00345
00346 $content = sprintf("<!-- sub-group %s auto-closed //--></%s>", $subGroupId, $this->subGroups[$subGroupId]['tag']);
00347 }
00348
00349
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
00354 $this->setPreviousSubGroupId("");
00355
00356 }
00357
00363 public function renderContent () {
00364
00365 $content = "";
00366
00367
00368 if (isset($this->groups['header'])) {
00369
00370 $content .= $this->groups['header']['content']."\n";
00371 }
00372
00373
00374 $content .= $this->getContent();
00375
00376
00377 for ($idx = 1; $idx <= $this->totalCounter; $idx++) {
00378
00379 if ((isset($this->groups[$idx])) && ($this->groups[$this->groups[$idx]]['opened'] === false)) {
00380
00381 $groupContent = trim($this->groups[$this->groups[$idx]]['content']);
00382
00383 $content .= $groupContent;
00384 } elseif ((isset($this->subGroups[$idx])) && ($this->subGroups[$this->subGroups[$idx]]['opened'] === false)) {
00385
00386 $subGroupContent = $this->subGroups[$this->subGroups[$idx]]['content'];
00387
00388 $content .= trim($subGroupContent);
00389 } else {
00390
00391 $this->debugInstance(__METHOD__."(): Something unexpected happened here.");
00392 }
00393 }
00394
00395
00396 if (isset($this->groups['footer'])) {
00397
00398 $content .= $this->groups['footer']['content']."\n";
00399 }
00400
00401
00402
00403 return $content;
00404 }
00405
00412 protected function ifGroupIsOpened ($groupId) {
00413
00414 $isOpened = ((isset($this->groups[$groupId])) && ($this->groups[$groupId]['opened'] === true));
00415
00416
00417 return $isOpened;
00418 }
00419
00426 public function getValueField ($fieldName) {
00427
00428 $fieldValue = call_user_func_array(array($this->valueInstance, 'getField'), array($fieldName));
00429
00430
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
00503 ?>