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    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:  * NControl is renderable component.
  30. 30:  *
  31. 31:  * @author     David Grudl
  32. 32:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  33. 33:  * @package    Application
  34. 34:  *
  35. 35:  * @property-read ITemplate $template 
  36. 36:  */
  37. 37: abstract class NControl extends NPresenterComponent 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("NObject returned by $this->class::createTemplate() must be instance of \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 NTemplate;
  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 NEnvironment::getVariable('baseUri');
  83. 83:  
  84. 84:         // flash message
  85. 85:         if ($presenter !== NULL && $presenter->hasFlashSession()) {
  86. 86:             $id $this->getParamId('flash');
  87. 87:             $template->flashes $presenter->getFlashSession()->$id;
  88. 88:         }
  89. 89:         if (!isset($template->flashes|| !is_array($template->flashes)) {
  90. 90:             $template->flashes array();
  91. 91:         }
  92. 92:  
  93. 93:         // default helpers
  94. 94:         $template->registerHelper('escape''NTemplateHelpers::escapeHtml');
  95. 95:         $template->registerHelper('escapeUrl''rawurlencode');
  96. 96:         $template->registerHelper('stripTags''strip_tags');
  97. 97:         $template->registerHelper('nl2br''nl2br');
  98. 98:         $template->registerHelperLoader('NTemplateHelpers::loader');
  99. 99:  
  100. 100:         return $template;
  101. 101:     }
  102. 102:  
  103. 103:  
  104. 104:  
  105. 105:     /**
  106. 106:      * Descendant can override this method to customize template compile-time filters.
  107. 107:      * @param  NTemplate 
  108. 108:      * @return void 
  109. 109:      */
  110. 110:     public function templatePrepareFilters($template)
  111. 111:     {
  112. 112:         // default filters
  113. 113:         $template->registerFilter(new LatteFilter);
  114. 114:     }
  115. 115:  
  116. 116:  
  117. 117:  
  118. 118:     /**
  119. 119:      * Returns widget component specified by name.
  120. 120:      * @param  string 
  121. 121:      * @return IComponent 
  122. 122:      */
  123. 123:     public function getWidget($name)
  124. 124:     {
  125. 125:         return $this->getComponent($name);
  126. 126:     }
  127. 127:  
  128. 128:  
  129. 129:  
  130. 130:     /**
  131. 131:      * Saves the message to template, that can be displayed after redirect.
  132. 132:      * @param  string 
  133. 133:      * @param  string 
  134. 134:      * @return stdClass 
  135. 135:      */
  136. 136:     public function flashMessage($message$type 'info')
  137. 137:     {
  138. 138:         $id $this->getParamId('flash');
  139. 139:         $messages $this->getPresenter()->getFlashSession()->$id;
  140. 140:         $messages[$flash = (object) array(
  141. 141:             'message' => $message,
  142. 142:             'type' => $type,
  143. 143:         );
  144. 144:         $this->getTemplate()->flashes $messages;
  145. 145:         $this->getPresenter()->getFlashSession()->$id $messages;
  146. 146:         return $flash;
  147. 147:     }
  148. 148:  
  149. 149:  
  150. 150:  
  151. 151:     /********************* rendering ****************d*g**/
  152. 152:  
  153. 153:  
  154. 154:  
  155. 155:     /**
  156. 156:      * Forces control or its snippet to repaint.
  157. 157:      * @param  string 
  158. 158:      * @return void 
  159. 159:      */
  160. 160:     public function invalidateControl($snippet NULL)
  161. 161:     {
  162. 162:         $this->invalidSnippets[$snippetTRUE;
  163. 163:     }
  164. 164:  
  165. 165:  
  166. 166:  
  167. 167:     /**
  168. 168:      * Allows control or its snippet to not repaint.
  169. 169:      * @param  string 
  170. 170:      * @return void 
  171. 171:      */
  172. 172:     public function validateControl($snippet NULL)
  173. 173:     {
  174. 174:         if ($snippet === NULL{
  175. 175:             $this->invalidSnippets array();
  176. 176:  
  177. 177:         else {
  178. 178:             unset($this->invalidSnippets[$snippet]);
  179. 179:         }
  180. 180:     }
  181. 181:  
  182. 182:  
  183. 183:  
  184. 184:     /**
  185. 185:      * Is required to repaint the control or its snippet?
  186. 186:      * @param  string  snippet name
  187. 187:      * @return bool 
  188. 188:      */
  189. 189:     public function isControlInvalid($snippet NULL)
  190. 190:     {
  191. 191:         if ($snippet === NULL{
  192. 192:             if (count($this->invalidSnippets0{
  193. 193:                 return TRUE;
  194. 194:  
  195. 195:             else {
  196. 196:                 foreach ($this->getComponents(as $component{
  197. 197:                     if ($component instanceof IRenderable && $component->isControlInvalid()) {
  198. 198:                         // $this->invalidSnippets['__child'] = TRUE; // as cache
  199. 199:                         return TRUE;
  200. 200:                     }
  201. 201:                 }
  202. 202:                 return FALSE;
  203. 203:             }
  204. 204:  
  205. 205:         else {
  206. 206:             return isset($this->invalidSnippets[NULL]|| isset($this->invalidSnippets[$snippet]);
  207. 207:         }
  208. 208:     }
  209. 209:  
  210. 210:  
  211. 211:  
  212. 212:     /**
  213. 213:      * Returns snippet HTML ID.
  214. 214:      * @param  string  snippet name
  215. 215:      * @return string 
  216. 216:      */
  217. 217:     public function getSnippetId($name NULL)
  218. 218:     {
  219. 219:         // HTML 4 ID & NAME: [A-Za-z][A-Za-z0-9:_.-]*
  220. 220:         return $this->getUniqueId('__' $name;
  221. 221:     }
  222. 222: