Commit 34268c722b4510054c371830319afcea085728ca
1 parent
e72fa263
Exists in
master
Adicionados novos métodos para {{{Core_Controller_Page_Abtract}}}:
* {{{append|prependOutput()}}}: permitem adicionar ou prefixar o conteúdo HTML a ser gerado por um {{{Core_Controller_Page_Abtract}}}, dando mais flexibilidade para a inclusão de JavaScript ou outros elementos HTML;
* Algumas classes subclassificam {{{clsConfig}}}, ao qual não está na árvore de herança de {{{Core_Controller_Page_Abstract}}}. As outras classes ({{{clsCadastro}}}, {{{clsDetalhe}}} e {{{clsListagem}}} além dos page controllers específicos) podem usar os métodos sem problemas;
* O conteúdo adicionado via estes métodos não são incluídos quando a página usa {{{$renderMenu = FALSE}}}. Isso ocorre em poucos casos no i-Educar, então verifique antes se o arquivo em que está trabalhando desabilita o menu (geralmente em arquivos que abram janelas modais ou pop-ups).
Showing
3 changed files
with
120 additions
and
2 deletions
Show diff stats
ieducar/intranet/include/clsBase.inc.php
| @@ -474,11 +474,26 @@ class clsBase extends clsConfig | @@ -474,11 +474,26 @@ class clsBase extends clsConfig | ||
| 474 | return $retorno; | 474 | return $retorno; |
| 475 | } | 475 | } |
| 476 | 476 | ||
| 477 | + /** | ||
| 478 | + * @see Core_Page_Controller_Abstract#getAppendedOutput() | ||
| 479 | + * @see Core_Page_Controller_Abstract#getPrependedOutput() | ||
| 480 | + */ | ||
| 477 | function MakeBody() | 481 | function MakeBody() |
| 478 | { | 482 | { |
| 479 | $corpo = ''; | 483 | $corpo = ''; |
| 480 | foreach ($this->clsForm as $form) { | 484 | foreach ($this->clsForm as $form) { |
| 481 | $corpo .= $form->RenderHTML(); | 485 | $corpo .= $form->RenderHTML(); |
| 486 | + | ||
| 487 | + // Prepend output. | ||
| 488 | + if (method_exists($form, 'getPrependedOutput')) { | ||
| 489 | + $corpo = $form->getPrependedOutput() . $corpo; | ||
| 490 | + } | ||
| 491 | + | ||
| 492 | + // Append output. | ||
| 493 | + if (method_exists($form, 'getAppendedOutput')) { | ||
| 494 | + $corpo = $corpo . $form->getAppendedOutput(); | ||
| 495 | + } | ||
| 496 | + | ||
| 482 | if (is_string($form->prog_alert) && $form->prog_alert) { | 497 | if (is_string($form->prog_alert) && $form->prog_alert) { |
| 483 | $this->prog_alert .= $form->prog_alert; | 498 | $this->prog_alert .= $form->prog_alert; |
| 484 | } | 499 | } |
| @@ -733,7 +748,7 @@ class clsBase extends clsConfig | @@ -733,7 +748,7 @@ class clsBase extends clsConfig | ||
| 733 | $saida_geral = $this->MakeHeadHtml(); | 748 | $saida_geral = $this->MakeHeadHtml(); |
| 734 | 749 | ||
| 735 | // @todo else ruim, colocar abre e fecha colchetes ao redor de foreach. | 750 | // @todo else ruim, colocar abre e fecha colchetes ao redor de foreach. |
| 736 | - if($this->renderMenu) { | 751 | + if ($this->renderMenu) { |
| 737 | $saida_geral .= $this->MakeBody(); | 752 | $saida_geral .= $this->MakeBody(); |
| 738 | } | 753 | } |
| 739 | else { | 754 | else { |
ieducar/lib/Core/Controller/Page/Abstract.php
| @@ -130,6 +130,11 @@ abstract class Core_Controller_Page_Abstract | @@ -130,6 +130,11 @@ abstract class Core_Controller_Page_Abstract | ||
| 130 | public $url_cancelar = NULL; | 130 | public $url_cancelar = NULL; |
| 131 | 131 | ||
| 132 | /** | 132 | /** |
| 133 | + * @var array | ||
| 134 | + */ | ||
| 135 | + private $_output = array(); | ||
| 136 | + | ||
| 137 | + /** | ||
| 133 | * Construtor. | 138 | * Construtor. |
| 134 | */ | 139 | */ |
| 135 | public function __construct() | 140 | public function __construct() |
| @@ -384,6 +389,70 @@ abstract class Core_Controller_Page_Abstract | @@ -384,6 +389,70 @@ abstract class Core_Controller_Page_Abstract | ||
| 384 | } | 389 | } |
| 385 | 390 | ||
| 386 | /** | 391 | /** |
| 392 | + * Adiciona conteúdo HTML após o conteúdo gerado por um | ||
| 393 | + * Core_Controller_Page_Abstract. | ||
| 394 | + * | ||
| 395 | + * @param string $data A string HTML a ser adiciona após o conteúdo. | ||
| 396 | + * @return Core_Controller_Page_Abstract Provê interface fluída | ||
| 397 | + */ | ||
| 398 | + public function appendOutput($data) | ||
| 399 | + { | ||
| 400 | + if (!empty($data) && is_string($data)) { | ||
| 401 | + $this->_output['append'][] = $data; | ||
| 402 | + } | ||
| 403 | + return $this; | ||
| 404 | + } | ||
| 405 | + | ||
| 406 | + /** | ||
| 407 | + * Retorna todo o conteúdo acrescentado como uma string. | ||
| 408 | + * @return string O conteúdo a ser acrescentado separado com uma quebra de linha. | ||
| 409 | + * @see clsBase#MakeBody() | ||
| 410 | + */ | ||
| 411 | + public function getAppendedOutput() | ||
| 412 | + { | ||
| 413 | + return $this->_getOutput('append'); | ||
| 414 | + } | ||
| 415 | + | ||
| 416 | + /** | ||
| 417 | + * Adiciona conteúdo HTML antes do conteúdo HTML gerado por um | ||
| 418 | + * Core_Controller_Page_Abstract. | ||
| 419 | + * | ||
| 420 | + * @param string $data A string HTML a ser adiciona após o conteúdo. | ||
| 421 | + * @return Core_Controller_Page_Abstract Provê interface fluída | ||
| 422 | + */ | ||
| 423 | + public function prependOutput($data) | ||
| 424 | + { | ||
| 425 | + if (!empty($data) && is_string($data)) { | ||
| 426 | + $this->_output['prepend'][] = $data; | ||
| 427 | + } | ||
| 428 | + return $this; | ||
| 429 | + } | ||
| 430 | + | ||
| 431 | + /** | ||
| 432 | + * Retorna todo o conteúdo prefixado como uma string. | ||
| 433 | + * @return string O conteúdo a ser prefixado separado com uma quebra de linha. | ||
| 434 | + * @see clsBase#MakeBody() | ||
| 435 | + */ | ||
| 436 | + public function getPrependedOutput() | ||
| 437 | + { | ||
| 438 | + return $this->_getOutput('prepend'); | ||
| 439 | + } | ||
| 440 | + | ||
| 441 | + /** | ||
| 442 | + * Retorna o conteúdo a ser adicionado a saída de acordo com a região. | ||
| 443 | + * @param string $pos Região para retornar o conteúdo a ser adicionado na saída. | ||
| 444 | + * @return string|NULL Conteúdo da região separado por uma quebra de linha ou | ||
| 445 | + * NULL caso a região não exista. | ||
| 446 | + */ | ||
| 447 | + private function _getOutput($pos = 'prepend') | ||
| 448 | + { | ||
| 449 | + if (isset($this->_output[$pos])) { | ||
| 450 | + return implode(PHP_EOL, $this->_output[$pos]); | ||
| 451 | + } | ||
| 452 | + return NULL; | ||
| 453 | + } | ||
| 454 | + | ||
| 455 | + /** | ||
| 387 | * @see CoreExt_Controller_Interface#dispatch() | 456 | * @see CoreExt_Controller_Interface#dispatch() |
| 388 | */ | 457 | */ |
| 389 | public function dispatch() | 458 | public function dispatch() |
ieducar/tests/unit/Core/Controller/Page/AbstractTest.php
| @@ -163,4 +163,38 @@ class Core_Controller_Page_AbstractTest extends UnitBaseTest | @@ -163,4 +163,38 @@ class Core_Controller_Page_AbstractTest extends UnitBaseTest | ||
| 163 | { | 163 | { |
| 164 | $this->_pageController->getBaseTitulo(); | 164 | $this->_pageController->getBaseTitulo(); |
| 165 | } | 165 | } |
| 166 | -} | ||
| 167 | \ No newline at end of file | 166 | \ No newline at end of file |
| 167 | + | ||
| 168 | + public function testAppendOutput() | ||
| 169 | + { | ||
| 170 | + $this->_pageController->appendOutput('string 1') | ||
| 171 | + ->appendOutput('string 2'); | ||
| 172 | + | ||
| 173 | + $this->assertEquals( | ||
| 174 | + 'string 1' . PHP_EOL . 'string 2', | ||
| 175 | + $this->_pageController->getAppendedOutput(), | ||
| 176 | + '->getAppendedOutput() retorna o conteúdo a ser adicionado como uma string separada por quebra de linha' | ||
| 177 | + ); | ||
| 178 | + } | ||
| 179 | + | ||
| 180 | + public function testGetApendedOutputRetornaNullQuandoNaoExisteConteudoASerAdicionado() | ||
| 181 | + { | ||
| 182 | + $this->assertNull($this->_pageController->getAppendedOutput()); | ||
| 183 | + } | ||
| 184 | + | ||
| 185 | + public function testPrependOutput() | ||
| 186 | + { | ||
| 187 | + $this->_pageController->prependOutput('string 1') | ||
| 188 | + ->prependOutput('string 2'); | ||
| 189 | + | ||
| 190 | + $this->assertEquals( | ||
| 191 | + 'string 1' . PHP_EOL . 'string 2', | ||
| 192 | + $this->_pageController->getPrependedOutput(), | ||
| 193 | + '->getPrependedOutput() retorna o conteúdo a ser adicionado como uma string separada por quebra de linha' | ||
| 194 | + ); | ||
| 195 | + } | ||
| 196 | + | ||
| 197 | + public function testGetPrependedOutputRetornaNullQuandoNaoExisteConteudoASerAdicionado() | ||
| 198 | + { | ||
| 199 | + $this->assertNull($this->_pageController->getPrependedOutput()); | ||
| 200 | + } | ||
| 201 | +} |