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 IPartiallyRenderableArrayAccess
  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:     /********************* component factory ****************d*g**/
  48. 48:  
  49. 49:  
  50. 50:  
  51. 51:     /**
  52. 52:      * Delegates the creation of components to a createComponent<Name> method.
  53. 53:      * @param  string  component name
  54. 54:      * @return void 
  55. 55:      */
  56. 56:     protected function createComponent($name)
  57. 57:     {
  58. 58:         $ucname ucfirst($name);
  59. 59:         $method 'createComponent' $ucname;
  60. 60:         if ($ucname !== $name && method_exists($this$method&& $this->getReflection()->getMethod($method)->getName(=== $method{
  61. 61:             $component $this->$method($name);
  62. 62:             if ($component instanceof IComponent && $component->getParent(=== NULL{
  63. 63:                 $this->addComponent($component$name);
  64. 64:             }
  65. 65:         }
  66. 66:     }
  67. 67:  
  68. 68:  
  69. 69:  
  70. 70:     /********************* template factory ****************d*g**/
  71. 71:  
  72. 72:  
  73. 73:  
  74. 74:     /**
  75. 75:      * @return ITemplate 
  76. 76:      */
  77. 77:     final public function getTemplate()
  78. 78:     {
  79. 79:         if ($this->template === NULL{
  80. 80:             $value $this->createTemplate();
  81. 81:             if (!($value instanceof ITemplate || $value === NULL)) {
  82. 82:                 $class get_class($value);
  83. 83:                 throw new UnexpectedValueException("Object returned by $this->class::createTemplate() must be instance of Nette\\Templates\\ITemplate, '$class' given.");
  84. 84:             }
  85. 85:             $this->template $value;
  86. 86:         }
  87. 87:         return $this->template;
  88. 88:     }
  89. 89:  
  90. 90:  
  91. 91:  
  92. 92:     /**
  93. 93:      * @return ITemplate 
  94. 94:      */
  95. 95:     protected function createTemplate()
  96. 96:     {
  97. 97:         $template new Template;
  98. 98:         $presenter $this->getPresenter(FALSE);
  99. 99:         $template->onPrepareFilters[array($this'templatePrepareFilters');
  100. 100:  
  101. 101:         // default parameters
  102. 102:         $template->component $this// DEPRECATED!
  103. 103:         $template->control $this;
  104. 104:         $template->presenter $presenter;
  105. 105:         $template->baseUri Environment::getVariable('baseUri');
  106. 106:  
  107. 107:         // flash message
  108. 108:         if ($presenter !== NULL && $presenter->hasFlashSession()) {
  109. 109:             $id $this->getParamId('flash');
  110. 110:             $template->flashes $presenter->getFlashSession()->$id;
  111. 111:         }
  112. 112:         if (!isset($template->flashes|| !is_array($template->flashes)) {
  113. 113:             $template->flashes array();
  114. 114:         }
  115. 115:  
  116. 116:         // default helpers
  117. 117:         $template->registerHelper('escape''Nette\Templates\TemplateHelpers::escapeHtml');
  118. 118:         $template->registerHelper('escapeUrl''rawurlencode');
  119. 119:         $template->registerHelper('stripTags''strip_tags');
  120. 120:         $template->registerHelper('nl2br''nl2br');
  121. 121:         $template->registerHelperLoader('Nette\Templates\TemplateHelpers::loader');
  122. 122:  
  123. 123:         return $template;
  124. 124:     }
  125. 125:  
  126. 126:  
  127. 127:  
  128. 128:     /**
  129. 129:      * Descendant can override this method to customize template compile-time filters.
  130. 130:      * @param  Template 
  131. 131:      * @return void 
  132. 132:      */
  133. 133:     public function templatePrepareFilters($template)
  134. 134:     {
  135. 135:         // default filters
  136. 136:         $template->registerFilter(new CurlyBracketsFilter);
  137. 137:     }
  138. 138:  
  139. 139:  
  140. 140:  
  141. 141:     /**
  142. 142:      * Returns widget component specified by name.
  143. 143:      * @param  string 
  144. 144:      * @return IComponent 
  145. 145:      */
  146. 146:     public function getWidget($name)
  147. 147:     {
  148. 148:         return $this->getComponent($name);
  149. 149:     }
  150. 150:  
  151. 151:  
  152. 152:  
  153. 153:     /**
  154. 154:      * Saves the message to template, that can be displayed after redirect.
  155. 155:      * @param  string 
  156. 156:      * @param  string 
  157. 157:      * @return stdClass 
  158. 158:      */
  159. 159:     public function flashMessage($message$type 'info')
  160. 160:     {
  161. 161:         $id $this->getParamId('flash');
  162. 162:         $messages $this->getPresenter()->getFlashSession()->$id;
  163. 163:         $messages[$flash = (object) array(
  164. 164:             'message' => $message,
  165. 165:             'type' => $type,
  166. 166:         );
  167. 167:         $this->getTemplate()->flashes $messages;
  168. 168:         $this->getPresenter()->getFlashSession()->$id $messages;
  169. 169:         return $flash;
  170. 170:     }
  171. 171:  
  172. 172:  
  173. 173:  
  174. 174:     /********************* rendering ****************d*g**/
  175. 175:  
  176. 176:  
  177. 177:  
  178. 178:     /**
  179. 179:      * Forces control or its snippet to repaint.
  180. 180:      * @param  string 
  181. 181:      * @return void 
  182. 182:      */
  183. 183:     public function invalidateControl($snippet NULL)
  184. 184:     {
  185. 185:         $this->invalidSnippets[$snippetTRUE;
  186. 186:     }
  187. 187:  
  188. 188:  
  189. 189:  
  190. 190:     /**
  191. 191:      * Allows control or its snippet to not repaint.
  192. 192:      * @param  string 
  193. 193:      * @return void 
  194. 194:      */
  195. 195:     public function validateControl($snippet NULL)
  196. 196:     {
  197. 197:         if ($snippet === NULL{
  198. 198:             $this->invalidSnippets array();
  199. 199:  
  200. 200:         else {
  201. 201:             unset($this->invalidSnippets[$snippet]);
  202. 202:         }
  203. 203:     }
  204. 204:  
  205. 205:  
  206. 206:  
  207. 207:     /**
  208. 208:      * Is required to repaint the control or its snippet?
  209. 209:      * @param  string  snippet name
  210. 210:      * @return bool 
  211. 211:      */
  212. 212:     public function isControlInvalid($snippet NULL)
  213. 213:     {
  214. 214:         if ($snippet === NULL{
  215. 215:             if (count($this->invalidSnippets0{
  216. 216:                 return TRUE;
  217. 217:  
  218. 218:             else {
  219. 219:                 foreach ($this->getComponents(as $component{
  220. 220:                     if ($component instanceof IRenderable && $component->isControlInvalid()) {
  221. 221:                         // $this->invalidSnippets['__child'] = TRUE; // as cache
  222. 222:                         return TRUE;
  223. 223:                     }
  224. 224:                 }
  225. 225:                 return FALSE;
  226. 226:             }
  227. 227:  
  228. 228:         else {
  229. 229:             return isset($this->invalidSnippets[NULL]|| isset($this->invalidSnippets[$snippet]);
  230. 230:         }
  231. 231:     }
  232. 232:  
  233. 233:  
  234. 234:  
  235. 235:     /**
  236. 236:      * Returns snippet HTML ID.
  237. 237:      * @param  string  snippet name
  238. 238:      * @return string 
  239. 239:      */
  240. 240:     public function getSnippetId($name NULL)
  241. 241:     {
  242. 242:         // HTML 4 ID & NAME: [A-Za-z][A-Za-z0-9:_.-]*
  243. 243:         return $this->getUniqueId('__' $name;
  244. 244:     }
  245. 245:  
  246. 246:  
  247. 247:  
  248. 248:     /********************* interface \ArrayAccess ****************d*g**/
  249. 249:  
  250. 250:  
  251. 251:  
  252. 252:     /**
  253. 253:      * Adds the component to the container.
  254. 254:      * @param  string  component name
  255. 255:      * @param  IComponent 
  256. 256:      * @return void. 
  257. 257:      */
  258. 258:     final public function offsetSet($name$component)
  259. 259:     {
  260. 260:         $this->addComponent($component$name);
  261. 261:     }
  262. 262:  
  263. 263:  
  264. 264:  
  265. 265:     /**
  266. 266:      * Returns component specified by name. Throws exception if component doesn't exist.
  267. 267:      * @param  string  component name
  268. 268:      * @return IComponent 
  269. 269:      * @throws InvalidArgumentException
  270. 270:      */
  271. 271:     final public function offsetGet($name)
  272. 272:     {
  273. 273:         return $this->getComponent($nameTRUE);
  274. 274:     }
  275. 275:  
  276. 276:  
  277. 277:  
  278. 278:     /**
  279. 279:      * Does component specified by name exists?
  280. 280:      * @param  string  component name
  281. 281:      * @return bool 
  282. 282:      */
  283. 283:     final public function offsetExists($name)
  284. 284:     {
  285. 285:         return $this->getComponent($nameFALSE!== NULL;
  286. 286:     }
  287. 287:  
  288. 288:  
  289. 289:  
  290. 290:     /**
  291. 291:      * Removes component from the container. Throws exception if component doesn't exist.
  292. 292:      * @param  string  component name
  293. 293:      * @return void 
  294. 294:      */
  295. 295:     final public function offsetUnset($name)
  296. 296:     {
  297. 297:         $component $this->getComponent($nameFALSE);
  298. 298:         if ($component !== NULL{
  299. 299:             $this->removeComponent($component);
  300. 300:         }
  301. 301:     }
  302. 302: