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 http://nettephp.com
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    http://nettephp.com/license  Nette license
  15. 15:  * @link       http://nettephp.com
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Application
  18. 18:  */
  19. 19:  
  20. 20:  
  21. 21:  
  22. 22: require_once dirname(__FILE__'/../Application/PresenterComponent.php';
  23. 23:  
  24. 24: require_once dirname(__FILE__'/../Application/IRenderable.php';
  25. 25:  
  26. 26:  
  27. 27:  
  28. 28: /**
  29. 29:  * Control is renderable component.
  30. 30:  *
  31. 31:  * @author     David Grudl
  32. 32:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  33. 33:  * @package    Nette\Application
  34. 34:  *
  35. 35:  * @property-read ITemplate $template 
  36. 36:  */
  37. 37: abstract class Control extends PresenterComponent implements IPartiallyRenderable
  38. 38: {
  39. 39:     /** @var ITemplate */
  40. 40:     private $template;
  41. 41:  
  42. 42:     /** @var array */
  43. 43:     private $invalidSnippets array();
  44. 44:  
  45. 45:  
  46. 46:  
  47. 47:     /********************* template factory ****************d*g**/
  48. 48:  
  49. 49:  
  50. 50:  
  51. 51:     /**
  52. 52:      * @return ITemplate 
  53. 53:      */
  54. 54:     final public function getTemplate()
  55. 55:     {
  56. 56:         if ($this->template === NULL{
  57. 57:             $value $this->createTemplate();
  58. 58:             if (!($value instanceof ITemplate || $value === NULL)) {
  59. 59:                 $class get_class($value);
  60. 60:                 throw new UnexpectedValueException("Object returned by $this->class::createTemplate() must be instance of Nette\\Templates\\ITemplate, '$class' given.");
  61. 61:             }
  62. 62:             $this->template $value;
  63. 63:         }
  64. 64:         return $this->template;
  65. 65:     }
  66. 66:  
  67. 67:  
  68. 68:  
  69. 69:     /**
  70. 70:      * @return ITemplate 
  71. 71:      */
  72. 72:     protected function createTemplate()
  73. 73:     {
  74. 74:         $template new Template;
  75. 75:         $presenter $this->getPresenter(FALSE);
  76. 76:         $template->onPrepareFilters[array($this'templatePrepareFilters');
  77. 77:  
  78. 78:         // default parameters
  79. 79:         $template->component $this// DEPRECATED!
  80. 80:         $template->control $this;
  81. 81:         $template->presenter $presenter;
  82. 82:         $template->baseUri Environment::getVariable('baseUri');
  83. 83:         $template->basePath rtrim($template->baseUri'/');
  84. 84:  
  85. 85:         // flash message
  86. 86:         if ($presenter !== NULL && $presenter->hasFlashSession()) {
  87. 87:             $id $this->getParamId('flash');
  88. 88:             $template->flashes $presenter->getFlashSession()->$id;
  89. 89:         }
  90. 90:         if (!isset($template->flashes|| !is_array($template->flashes)) {
  91. 91:             $template->flashes array();
  92. 92:         }
  93. 93:  
  94. 94:         // default helpers
  95. 95:         $template->registerHelper('escape''Nette\Templates\TemplateHelpers::escapeHtml');
  96. 96:         $template->registerHelper('escapeUrl''rawurlencode');
  97. 97:         $template->registerHelper('stripTags''strip_tags');
  98. 98:         $template->registerHelper('nl2br''nl2br');
  99. 99:         $template->registerHelper('substr''iconv_substr');
  100. 100:         $template->registerHelper('repeat''str_repeat');
  101. 101:         $template->registerHelper('implode''implode');
  102. 102:         $template->registerHelper('number''number_format');
  103. 103:         $template->registerHelperLoader('Nette\Templates\TemplateHelpers::loader');
  104. 104:  
  105. 105:         return $template;
  106. 106:     }
  107. 107:  
  108. 108:  
  109. 109:  
  110. 110:     /**
  111. 111:      * Descendant can override this method to customize template compile-time filters.
  112. 112:      * @param  Template 
  113. 113:      * @return void 
  114. 114:      */
  115. 115:     public function templatePrepareFilters($template)
  116. 116:     {
  117. 117:         // default filters
  118. 118:         $template->registerFilter(new LatteFilter);
  119. 119:     }
  120. 120:  
  121. 121:  
  122. 122:  
  123. 123:     /**
  124. 124:      * Returns widget component specified by name.
  125. 125:      * @param  string 
  126. 126:      * @return IComponent 
  127. 127:      */
  128. 128:     public function getWidget($name)
  129. 129:     {
  130. 130:         return $this->getComponent($name);
  131. 131:     }
  132. 132:  
  133. 133:  
  134. 134:  
  135. 135:     /**
  136. 136:      * Saves the message to template, that can be displayed after redirect.
  137. 137:      * @param  string 
  138. 138:      * @param  string 
  139. 139:      * @return stdClass 
  140. 140:      */
  141. 141:     public function flashMessage($message$type 'info')
  142. 142:     {
  143. 143:         $id $this->getParamId('flash');
  144. 144:         $messages $this->getPresenter()->getFlashSession()->$id;
  145. 145:         $messages[$flash = (object) array(
  146. 146:             'message' => $message,
  147. 147:             'type' => $type,
  148. 148:         );
  149. 149:         $this->getTemplate()->flashes $messages;
  150. 150:         $this->getPresenter()->getFlashSession()->$id $messages;
  151. 151:         return $flash;
  152. 152:     }
  153. 153:  
  154. 154:  
  155. 155:  
  156. 156:     /********************* rendering ****************d*g**/
  157. 157:  
  158. 158:  
  159. 159:  
  160. 160:     /**
  161. 161:      * Forces control or its snippet to repaint.
  162. 162:      * @param  string 
  163. 163:      * @return void 
  164. 164:      */
  165. 165:     public function invalidateControl($snippet NULL)
  166. 166:     {
  167. 167:         $this->invalidSnippets[$snippetTRUE;
  168. 168:     }
  169. 169:  
  170. 170:  
  171. 171:  
  172. 172:     /**
  173. 173:      * Allows control or its snippet to not repaint.
  174. 174:      * @param  string 
  175. 175:      * @return void 
  176. 176:      */
  177. 177:     public function validateControl($snippet NULL)
  178. 178:     {
  179. 179:         if ($snippet === NULL{
  180. 180:             $this->invalidSnippets array();
  181. 181:  
  182. 182:         else {
  183. 183:             unset($this->invalidSnippets[$snippet]);
  184. 184:         }
  185. 185:     }
  186. 186:  
  187. 187:  
  188. 188:  
  189. 189:     /**
  190. 190:      * Is required to repaint the control or its snippet?
  191. 191:      * @param  string  snippet name
  192. 192:      * @return bool 
  193. 193:      */
  194. 194:     public function isControlInvalid($snippet NULL)
  195. 195:     {
  196. 196:         if ($snippet === NULL{
  197. 197:             if (count($this->invalidSnippets0{
  198. 198:                 return TRUE;
  199. 199:  
  200. 200:             else {
  201. 201:                 foreach ($this->getComponents(as $component{
  202. 202:                     if ($component instanceof IRenderable && $component->isControlInvalid()) {
  203. 203:                         // $this->invalidSnippets['__child'] = TRUE; // as cache
  204. 204:                         return TRUE;
  205. 205:                     }
  206. 206:                 }
  207. 207:                 return FALSE;
  208. 208:             }
  209. 209:  
  210. 210:         else {
  211. 211:             return isset($this->invalidSnippets[NULL]|| isset($this->invalidSnippets[$snippet]);
  212. 212:         }
  213. 213:     }
  214. 214:  
  215. 215:  
  216. 216:  
  217. 217:     /**
  218. 218:      * Returns snippet HTML ID.
  219. 219:      * @param  string  snippet name
  220. 220:      * @return string 
  221. 221:      */
  222. 222:     public function getSnippetId($name NULL)
  223. 223:     {
  224. 224:         // HTML 4 ID & NAME: [A-Za-z][A-Za-z0-9:_.-]*
  225. 225:         return 'snippet-' $this->getUniqueId('-' $name;
  226. 226:     }
  227. 227: