PageRenderTime 106ms CodeModel.GetById 37ms RepoModel.GetById 2ms app.codeStats 0ms

/Nette/Application/Control.php

https://github.com/DocX/nette
PHP | 225 lines | 96 code | 51 blank | 78 comment | 16 complexity | 7343c5e7c7f4a72339a3c9ed1a1c3a0d MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Nette Framework
  4. *
  5. * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  6. *
  7. * This source file is subject to the "Nette license" that is bundled
  8. * with this package in the file license.txt.
  9. *
  10. * For more information please see http://nettephp.com
  11. *
  12. * @copyright Copyright (c) 2004, 2009 David Grudl
  13. * @license http://nettephp.com/license Nette license
  14. * @link http://nettephp.com
  15. * @category Nette
  16. * @package Nette\Application
  17. */
  18. /*namespace Nette\Application;*/
  19. require_once dirname(__FILE__) . '/../Application/PresenterComponent.php';
  20. require_once dirname(__FILE__) . '/../Application/IRenderable.php';
  21. /**
  22. * Control is renderable component.
  23. *
  24. * @author David Grudl
  25. * @copyright Copyright (c) 2004, 2009 David Grudl
  26. * @package Nette\Application
  27. *
  28. * @property-read Nette\Templates\ITemplate $template
  29. */
  30. abstract class Control extends PresenterComponent implements IPartiallyRenderable
  31. {
  32. /** @var Nette\Templates\ITemplate */
  33. private $template;
  34. /** @var array */
  35. private $invalidSnippets = array();
  36. /********************* template factory ****************d*g**/
  37. /**
  38. * @return Nette\Templates\ITemplate
  39. */
  40. final public function getTemplate()
  41. {
  42. if ($this->template === NULL) {
  43. $value = $this->createTemplate();
  44. if (!($value instanceof /*Nette\Templates\*/ITemplate || $value === NULL)) {
  45. $class = get_class($value);
  46. throw new /*\*/UnexpectedValueException("Object returned by $this->class::createTemplate() must be instance of Nette\\Templates\\ITemplate, '$class' given.");
  47. }
  48. $this->template = $value;
  49. }
  50. return $this->template;
  51. }
  52. /**
  53. * @return Nette\Templates\ITemplate
  54. */
  55. protected function createTemplate()
  56. {
  57. $template = new /*Nette\Templates\*/Template;
  58. $presenter = $this->getPresenter(FALSE);
  59. $template->onPrepareFilters[] = array($this, 'templatePrepareFilters');
  60. // default parameters
  61. $template->component = $this; // DEPRECATED!
  62. $template->control = $this;
  63. $template->presenter = $presenter;
  64. $template->baseUri = /*Nette\*/Environment::getVariable('baseUri');
  65. // flash message
  66. if ($presenter !== NULL && $presenter->hasFlashSession()) {
  67. $id = $this->getParamId('flash');
  68. $template->flashes = $presenter->getFlashSession()->$id;
  69. }
  70. if (!isset($template->flashes) || !is_array($template->flashes)) {
  71. $template->flashes = array();
  72. }
  73. // default helpers
  74. $template->registerHelper('escape', 'Nette\Templates\TemplateHelpers::escapeHtml');
  75. $template->registerHelper('escapeUrl', 'rawurlencode');
  76. $template->registerHelper('stripTags', 'strip_tags');
  77. $template->registerHelper('nl2br', 'nl2br');
  78. $template->registerHelperLoader('Nette\Templates\TemplateHelpers::loader');
  79. return $template;
  80. }
  81. /**
  82. * Descendant can override this method to customize template compile-time filters.
  83. * @param Nette\Templates\Template
  84. * @return void
  85. */
  86. public function templatePrepareFilters($template)
  87. {
  88. // default filters
  89. $template->registerFilter(new /*Nette\Templates\*/LatteFilter);
  90. }
  91. /**
  92. * Returns widget component specified by name.
  93. * @param string
  94. * @return IComponent
  95. */
  96. public function getWidget($name)
  97. {
  98. return $this->getComponent($name);
  99. }
  100. /**
  101. * Saves the message to template, that can be displayed after redirect.
  102. * @param string
  103. * @param string
  104. * @return stdClass
  105. */
  106. public function flashMessage($message, $type = 'info')
  107. {
  108. $id = $this->getParamId('flash');
  109. $messages = $this->getPresenter()->getFlashSession()->$id;
  110. $messages[] = $flash = (object) array(
  111. 'message' => $message,
  112. 'type' => $type,
  113. );
  114. $this->getTemplate()->flashes = $messages;
  115. $this->getPresenter()->getFlashSession()->$id = $messages;
  116. return $flash;
  117. }
  118. /********************* rendering ****************d*g**/
  119. /**
  120. * Forces control or its snippet to repaint.
  121. * @param string
  122. * @return void
  123. */
  124. public function invalidateControl($snippet = NULL)
  125. {
  126. $this->invalidSnippets[$snippet] = TRUE;
  127. }
  128. /**
  129. * Allows control or its snippet to not repaint.
  130. * @param string
  131. * @return void
  132. */
  133. public function validateControl($snippet = NULL)
  134. {
  135. if ($snippet === NULL) {
  136. $this->invalidSnippets = array();
  137. } else {
  138. unset($this->invalidSnippets[$snippet]);
  139. }
  140. }
  141. /**
  142. * Is required to repaint the control or its snippet?
  143. * @param string snippet name
  144. * @return bool
  145. */
  146. public function isControlInvalid($snippet = NULL)
  147. {
  148. if ($snippet === NULL) {
  149. if (count($this->invalidSnippets) > 0) {
  150. return TRUE;
  151. } else {
  152. foreach ($this->getComponents() as $component) {
  153. if ($component instanceof IRenderable && $component->isControlInvalid()) {
  154. // $this->invalidSnippets['__child'] = TRUE; // as cache
  155. return TRUE;
  156. }
  157. }
  158. return FALSE;
  159. }
  160. } else {
  161. return isset($this->invalidSnippets[NULL]) || isset($this->invalidSnippets[$snippet]);
  162. }
  163. }
  164. /**
  165. * Returns snippet HTML ID.
  166. * @param string snippet name
  167. * @return string
  168. */
  169. public function getSnippetId($name = NULL)
  170. {
  171. // HTML 4 ID & NAME: [A-Za-z][A-Za-z0-9:_.-]*
  172. return $this->getUniqueId() . '__' . $name;
  173. }
  174. }