Packages

  • Nette
    • Application
      • Application\Diagnostics
      • Application\Responses
      • Application\Routers
      • Application\UI
    • Caching
      • Caching\Storages
    • ComponentModel
    • Config
    • Database
      • Database\Diagnostics
      • Database\Drivers
      • Database\Reflection
      • Database\Table
    • DI
    • Diagnostics
    • Forms
      • Forms\Controls
      • Forms\Rendering
    • Http
    • Iterators
    • Latte
      • Latte\Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • None
  • PHP

Classes

  • NAppForm
  • NControl
  • NPresenter
  • NPresenterComponent

Interfaces

  • IPartiallyRenderable
  • IRenderable
  • ISignalReceiver
  • IStatePersistent

Exceptions

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