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