1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Application;
13:
14: use Nette;
15:
16:
17:
18: 19: 20: 21: 22: 23: 24:
25: abstract class Control extends PresenterComponent implements IPartiallyRenderable
26: {
27:
28: private $template;
29:
30:
31: private $invalidSnippets = array();
32:
33:
34:
35:
36:
37:
38:
39: 40: 41:
42: final public function getTemplate()
43: {
44: if ($this->template === NULL) {
45: $value = $this->createTemplate();
46: if (!($value instanceof Nette\Templates\ITemplate || $value === NULL)) {
47: $class = get_class($value);
48: throw new \UnexpectedValueException("Object returned by {$this->reflection->name}::createTemplate() must be instance of Nette\\Templates\\ITemplate, '$class' given.");
49: }
50: $this->template = $value;
51: }
52: return $this->template;
53: }
54:
55:
56:
57: 58: 59:
60: protected function createTemplate()
61: {
62: $template = new Nette\Templates\Template;
63: $presenter = $this->getPresenter(FALSE);
64: $template->onPrepareFilters[] = array($this, 'templatePrepareFilters');
65:
66:
67: $template->component = $this;
68: $template->control = $this;
69: $template->presenter = $presenter;
70: $template->baseUri = Nette\Environment::getVariable('baseUri');
71: $template->basePath = rtrim($template->baseUri, '/');
72:
73:
74: if ($presenter !== NULL && $presenter->hasFlashSession()) {
75: $id = $this->getParamId('flash');
76: $template->flashes = $presenter->getFlashSession()->$id;
77: }
78: if (!isset($template->flashes) || !is_array($template->flashes)) {
79: $template->flashes = array();
80: }
81:
82:
83: $template->registerHelper('escape', 'Nette\Templates\TemplateHelpers::escapeHtml');
84: $template->registerHelper('escapeUrl', 'rawurlencode');
85: $template->registerHelper('stripTags', 'strip_tags');
86: $template->registerHelper('nl2br', 'nl2br');
87: $template->registerHelper('substr', 'iconv_substr');
88: $template->registerHelper('repeat', 'str_repeat');
89: $template->registerHelper('implode', 'implode');
90: $template->registerHelper('number', 'number_format');
91: $template->registerHelperLoader('Nette\Templates\TemplateHelpers::loader');
92:
93: return $template;
94: }
95:
96:
97:
98: 99: 100: 101: 102:
103: public function templatePrepareFilters($template)
104: {
105:
106: $template->registerFilter(new Nette\Templates\LatteFilter);
107: }
108:
109:
110:
111: 112: 113: 114: 115:
116: public function getWidget($name)
117: {
118: return $this->getComponent($name);
119: }
120:
121:
122:
123: 124: 125: 126: 127: 128:
129: public function flashMessage($message, $type = 'info')
130: {
131: $id = $this->getParamId('flash');
132: $messages = $this->getPresenter()->getFlashSession()->$id;
133: $messages[] = $flash = (object) array(
134: 'message' => $message,
135: 'type' => $type,
136: );
137: $this->getTemplate()->flashes = $messages;
138: $this->getPresenter()->getFlashSession()->$id = $messages;
139: return $flash;
140: }
141:
142:
143:
144:
145:
146:
147:
148: 149: 150: 151: 152:
153: public function invalidateControl($snippet = NULL)
154: {
155: $this->invalidSnippets[$snippet] = TRUE;
156: }
157:
158:
159:
160: 161: 162: 163: 164:
165: public function validateControl($snippet = NULL)
166: {
167: if ($snippet === NULL) {
168: $this->invalidSnippets = array();
169:
170: } else {
171: unset($this->invalidSnippets[$snippet]);
172: }
173: }
174:
175:
176:
177: 178: 179: 180: 181:
182: public function isControlInvalid($snippet = NULL)
183: {
184: if ($snippet === NULL) {
185: if (count($this->invalidSnippets) > 0) {
186: return TRUE;
187:
188: } else {
189: foreach ($this->getComponents() as $component) {
190: if ($component instanceof IRenderable && $component->isControlInvalid()) {
191:
192: return TRUE;
193: }
194: }
195: return FALSE;
196: }
197:
198: } else {
199: return isset($this->invalidSnippets[NULL]) || isset($this->invalidSnippets[$snippet]);
200: }
201: }
202:
203:
204:
205: 206: 207: 208: 209:
210: public function getSnippetId($name = NULL)
211: {
212:
213: return 'snippet-' . $this->getUniqueId() . '-' . $name;
214: }
215:
216: }
217: