1: <?php
2: 3: 4: 5: 6: 7:
8:
9: namespace GLFramework;
10:
11:
12: use GLFramework\Module\ModuleManager;
13:
14: class View
15: {
16: private $filters = array();
17: private $twig;
18: 19: 20:
21: private $controller;
22:
23: 24: 25: 26:
27: public function __construct($controller)
28: {
29: $this->controller = $controller;
30: $directories = ModuleManager::getInstance()->getViews($controller->module);
31: $loader = new \Twig_Loader_Filesystem($directories);
32: $this->twig = new \Twig_Environment($loader, array());
33: Events::fire('onViewCreated', array(&$this->twig));
34: $this->twig->addGlobal('config', $this->controller->config);
35: $this->twig->addGlobal('_GET', $_GET);
36: $this->twig->addGlobal('_POST', $_POST);
37: $this->twig->addGlobal('_REQUEST', $_REQUEST);
38: $this->twig->addGlobal('_SERVER', $_SERVER);
39: $this->twig->addGlobal('this', $this->controller);
40: $this->twig->addGlobal('render', $this);
41: $this->twig->addGlobal('manager', ModuleManager::getInstance());
42: $this->twig->addFunction(new \Twig_SimpleFunction('fire', array($this, 'fireEvent')));
43: $this->twig->addFilter(new \Twig_SimpleFilter('active', array($this, 'isHrefActive')));
44: $this->twig->addFilter(new \Twig_SimpleFilter('fecha_hora', array($this, 'parseFechaHora')));
45: $this->twig->addFilter(new \Twig_SimpleFilter('fecha', array($this, 'parseFecha')));
46: $this->twig->addFilter(new \Twig_SimpleFilter('hora', array($this, 'parseHora')));
47: $this->twig->addFilter(new \Twig_SimpleFilter('debug', array($this, 'debug')));
48: $this->twig->addFilter(new \Twig_SimpleFilter('number', array($this, 'isNumber')));
49: $this->twig->addFilter(new \Twig_SimpleFilter('implode', array($this, 'implode')));
50: }
51:
52: 53: 54: 55: 56: 57:
58: public function render($data = null, $params = array())
59: {
60: if($this->controller->getTemplate() != null)
61: {
62: $data = $data == null?array():$data;
63: $this->twig->addGlobal('params', $params);
64: $template = $this->twig->loadTemplate($this->controller->getTemplate());
65: return $template->render($data);
66: }
67: return $data;
68: }
69:
70: 71: 72: 73: 74: 75:
76: public function display($template, $data = array())
77: {
78: return $this->getTwig()->render($template, $data);
79: }
80:
81: 82: 83: 84: 85: 86: 87:
88: public function mail($template, $data = array(), &$css = array())
89: {
90: $this->twig->addFunction(new \Twig_SimpleFunction('css', function($file) use(&$css)
91: {
92: $css[] = $file;
93: }));
94: $template = $this->twig->loadTemplate($template);
95: return $template->render($data);
96: }
97:
98: public function addFilter($filter)
99: {
100: $this->filters[] = $filter;
101: }
102:
103:
104: public function isHrefActive($url)
105: {
106: if(strpos($_SERVER['REQUEST_URI'], $url) !== FALSE)
107: {
108: return 'active';
109: }
110: return "";
111: }
112:
113: public function parseFechaHora($fecha)
114: {
115: return $this->parseFecha($fecha) . " " . $this->parseHora($fecha);
116: }
117: public function parseFecha($fecha)
118: {
119: if(!$fecha) return "";
120: $time = strtotime($fecha);
121: return date("d-m-Y", $time);
122: }
123:
124: public function parseHora($fecha)
125: {
126: if(!$fecha) return "";
127: $time = strtotime($fecha);
128: return date("H:i:s", $time);
129: }
130:
131: public function debug($data)
132: {
133: print_debug($data);
134: }
135:
136: public function isNumber($data)
137: {
138: return is_numeric($data);
139: }
140:
141: public function implode($array, $separator)
142: {
143: return implode($separator, $array);
144: }
145: 146: 147:
148: public function getTwig()
149: {
150: return $this->twig;
151: }
152:
153: public function fireEvent($name, $args = array())
154: {
155: Events::fire($name, $args);
156: }
157:
158: 159: 160:
161: public function getController()
162: {
163: return $this->controller;
164: }
165:
166: 167: 168:
169: public function setController($controller)
170: {
171: $this->controller = $controller;
172: }
173:
174:
175:
176:
177: }