KumbiaPHP  beta2
Framework PHP
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Páginas
kumbia_view.php
Ir a la documentación de este archivo.
1 <?php
28 {
29 
35  protected static $_content;
41  protected static $_view;
47  protected static $_template = 'default';
53  protected static $_response;
59  protected static $_path;
68  protected static $_cache = array('type' => FALSE, 'time' => FALSE, 'group' => FALSE);
69 
75  protected static $_controller;
76 
83  public static function select($view, $template = FALSE)
84  {
85  self::$_view = $view;
86 
87  // verifica si se indico template
88  if ($template !== FALSE) {
89  self::$_template = $template;
90  }
91  }
92 
98  public static function template($template)
99  {
100  self::$_template = $template;
101  }
102 
112  public static function response($response, $template = FALSE)
113  {
114  if ($response == 'view') { //se mantiene pero ya esta deprecated
115  self::$_template = NULL;
116  } else {
117  self::$_response = $response;
118  }
119 
120  // verifica si se indico template
121  if ($template !== FALSE) {
122  self::$_template = $template;
123  }
124  }
125 
131  public static function setPath($path)
132  {
133  self::$_path = $path . '/';
134  }
135 
141  public static function getPath()
142  {
143  if (self::$_response)
144  return self::$_path . self::$_view . '.' . self::$_response . '.phtml';
145 
146  return self::$_path . self::$_view . '.phtml';
147  }
148 
154  public static function get($atribute)
155  {
156  return self::${"_$atribute"};
157  }
158 
168  public static function cache($time, $type='view', $group='kumbia.view')
169  {
170  if ($time === FALSE) { //TODO borrar cache
171  self::$_cache['type'] = FALSE;
172  return;
173  }
174  self::$_cache['type'] = $type;
175  self::$_cache['time'] = $time;
176  self::$_cache['group'] = $group;
177  //Si está en producción para view
178  if (PRODUCTION && $type === 'view') {
179  return getCache(); //TRUE si está cacheada
180  }
181  }
182 
188  protected static function getCache()
189  {
190  // el contenido permanece nulo si no hay nada cacheado o la cache expiro
191  self::$_content = Cache::driver()->get(Router::get('route'), self::$_cache['group']);
192  return self::$_content !== NULL;
193  }
194 
200  public static function render($controller)
201  {
202  if (!self::$_view && !self::$_template)
203  return ob_end_flush();
204 
205  // Guarda el controlador
206  self::$_controller = $controller;
207 
208  // Mapea los atributos del controller en el scope
209  extract(get_object_vars($controller), EXTR_OVERWRITE);
210 
211  // carga la vista si tiene view y no esta cacheada
212  if (($__view = self::$_view) && self::$_content === NULL) {
213  // Carga el contenido del buffer de salida
214  self::$_content = ob_get_clean();
215 
216  // Renderizar vista
217  ob_start();
218 
219  $__file = APP_PATH . 'views/' . self::getPath();
220  //Si no existe el view y es scaffold
221  if (!is_file($__file) && $scaffold) {
222  $__file = APP_PATH . "views/_shared/scaffolds/$scaffold/$__view.phtml";
223  }
224 
225  // carga la vista
226  if (!include $__file)
227  throw new KumbiaException('Vista "' . self::getPath() . '" no encontrada', 'no_view');
228 
229  // si esta en produccion y se cachea la vista
230  if (PRODUCTION && self::$_cache['type'] == 'view') {
231  Cache::driver()->save(ob_get_contents(), self::$_cache['time'], Router::get('route'), self::$_cache['group']);
232  }
233 
234  // Verifica si hay template
235  if (!self::$_template) {
236  ob_end_flush();
237  return;
238  }
239 
240  self::$_content = ob_get_clean();
241 
242  } //else {ob_clean()}
243 
244  // Renderizar template
245  if ($__template = self::$_template) {
246  ob_start();
247 
248  // carga el template
249  if (!include APP_PATH . "views/_shared/templates/$__template.phtml")
250  throw new KumbiaException("Template $__template no encontrado");
251 
252  // si esta en produccion y se cachea template
253  if (PRODUCTION && self::$_cache['type'] == 'template') {
254  Cache::driver()->save(ob_get_contents(), self::$_cache['time'], Router::get('route'), self::$_cache['group']);
255  }
256 
257  return ob_end_flush();
258  }
259 
260  echo self::$_content;
261  }
262 
267  public static function content()
268  {
269  if (isset($_SESSION['KUMBIA.CONTENT'])) {
270  echo $_SESSION['KUMBIA.CONTENT'];
271  unset($_SESSION['KUMBIA.CONTENT']);
272  }
273  echo self::$_content;
274  }
275 
286  public static function partial($partial, $__time=FALSE, $params=NULL, $group ='kumbia.partials')
287  {
288  if (PRODUCTION && $__time && !Cache::driver()->start($__time, $partial, $group)) {
289  return;
290  }
291 
292  //Verificando el partials en el dir app
293  $__file = APP_PATH . "views/_shared/partials/$partial.phtml";
294 
295  if (!is_file($__file)) {
296  //Verificando el partials en el dir core
297  $__file = CORE_PATH . "views/partials/$partial.phtml";
298  }
299 
300  if($params){
301  if (is_string($params)) {
302  $params = Util::getParams(explode(',', $params));
303  }
304 
305  // carga los parametros en el scope
306  extract($params, EXTR_OVERWRITE);
307  }
308 
309  // carga la vista parcial
310  if (!include $__file) {
311  throw new KumbiaException('Vista Parcial "' . $__file . '" no se encontro');
312  }
313 
314  // se guarda en la cache de ser requerido
315  if (PRODUCTION && $__time) {
316  Cache::driver()->end();
317  }
318  }
319 
327  public static function helpers($helper)
328  {
329  $helper = Util::smallcase($helper);
330  $path = "extensions/helpers/$helper.php";
331  $file = APP_PATH . $path;
332 
333  if (!is_file($file)) {
334  if (!include_once CORE_PATH . $path)
335  throw new KumbiaException("Helpers $helper no encontrado");
336  return;
337  }
338 
339  require_once $file;
340  }
341 
348  public static function getVar($var = NULL)
349  {
350  if(!$var) return get_object_vars(self::$_controller);
351 
352  return isset(self::$_controller->$var) ? self::$_controller->$var : NULL;
353  }
354 }
355 
364 function h($s, $charset = APP_CHARSET)
365 {
366  return htmlspecialchars($s, ENT_QUOTES, $charset);
367 }
368 
377 function eh($s, $charset = APP_CHARSET)
378 {
379  echo htmlspecialchars($s, ENT_QUOTES, $charset);
380 }