1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20: 21: 22:
23: abstract class NControl extends NPresenterComponent implements IPartiallyRenderable
24: {
25:
26: private $template;
27:
28:
29: private $invalidSnippets = array();
30:
31:
32:
33:
34:
35:
36:
37: 38: 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: 57:
58: protected function createTemplate()
59: {
60: $template = new NTemplate;
61: $presenter = $this->getPresenter(FALSE);
62: $template->onPrepareFilters[] = array($this, 'templatePrepareFilters');
63:
64:
65: $template->component = $this;
66: $template->control = $this;
67: $template->presenter = $presenter;
68: $template->baseUri = NEnvironment::getVariable('baseUri');
69: $template->basePath = rtrim($template->baseUri, '/');
70:
71:
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:
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: 98: 99: 100:
101: public function templatePrepareFilters($template)
102: {
103:
104: $template->registerFilter(new NLatteFilter);
105: }
106:
107:
108:
109: 110: 111: 112: 113:
114: public function getWidget($name)
115: {
116: return $this->getComponent($name);
117: }
118:
119:
120:
121: 122: 123: 124: 125: 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:
143:
144:
145:
146: 147: 148: 149: 150:
151: public function invalidateControl($snippet = NULL)
152: {
153: $this->invalidSnippets[$snippet] = TRUE;
154: }
155:
156:
157:
158: 159: 160: 161: 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: 177: 178: 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:
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: 205: 206: 207:
208: public function getSnippetId($name = NULL)
209: {
210:
211: return 'snippet-' . $this->getUniqueId() . '-' . $name;
212: }
213:
214: }
215: