Source for file Control.php

Documentation is available at Control.php

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