Packages

  • Nette
    • Application
    • Caching
    • Collections
    • Config
    • Forms
    • IO
    • Loaders
    • Mail
    • Reflection
    • Security
    • Templates
    • Web
  • None
  • PHP

Classes

  • NAppForm
  • NApplication
  • NCliRouter
  • NControl
  • NDownloadResponse
  • NForwardingResponse
  • NJsonResponse
  • NLink
  • NMultiRouter
  • NPresenter
  • NPresenterComponent
  • NPresenterLoader
  • NPresenterRequest
  • NRedirectingResponse
  • NRenderResponse
  • NRoute
  • NSimpleRouter

Interfaces

  • IPartiallyRenderable
  • IPresenter
  • IPresenterLoader
  • IPresenterResponse
  • IRenderable
  • IRouter
  • ISignalReceiver
  • IStatePersistent

Exceptions

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