Fixes on view

This commit is contained in:
Antonio de la Rosa 2015-04-20 04:31:09 +02:00
parent 92e7b17f0e
commit 1539275468

View file

@ -1,6 +1,7 @@
<?php <?php
namespace PhaView; namespace PhaView;
use PhaUtils\Utils;
class View { class View {
@ -22,6 +23,54 @@ class View {
public $cache_template=array(); public $cache_template=array();
/**
* Path of static media files (javascript, images and css).
*/
public $path_media='media';
/**
* Url of static media files (javascript, images and css).
*/
public $url_media='media';
/**
* Media .php path.
*/
public $php_file='showmedia.php';
/**
* Internal property used for see if this media are in production
*/
protected $production=0;
/**
* Internal property used for define the method used for retrieve the media files.
*/
protected $func_media='dynamicGetMediaUrl';
/**
* An array where you can add new css in all views. For example, a view that use a special css can use this array and you can insert the value 'special.css' and the principal View can use loadCss method for load all css writed in the array by children views.
*/
public $css=array();
/**
* An array where you can add new js in all views. For example, a view that use a special js can use this array and you can insert the value 'special.js' in principal View using loadJs method for load all js writed in the array by children views.
*/
public $js=array();
/**
* An array where you can add new code in <header> tag. For example, a view that need a initialitation code in the principal view can use this array and you can insert the code in principal View using loadHeader method for load all header code writed in the array by children views.
*/
public $header=array();
/** /**
* The construct for create a view object * The construct for create a view object
* *
@ -54,7 +103,7 @@ class View {
* @param string $module_theme If the view are on a different theme and you don't want put the view on the theme, use this variable for go to the other theme. * @param string $module_theme If the view are on a different theme and you don't want put the view on the theme, use this variable for go to the other theme.
*/ */
function load_view($arr_template_values_values, $template) public function loadView($arr_template_values, $template)
{ {
//First see in controller/view/template, if not see in /views/template //First see in controller/view/template, if not see in /views/template
@ -82,64 +131,6 @@ class View {
} }
//Search view first on an theme
/*$theme_view=$this->root_path.$container_theme.'views/'.$theme.'/'.strtolower($template).'.php';
//Search view on the real module
$script_module_view=$this->root_path.'modules/'.$this->script_module.'/views/'.strtolower($template).'.php';
//Search view on other module specified.
$module_view=$this->root_path.'modules/'.$module_theme.'/views/'.strtolower($template).'.php';*/
/*if(!is_file($theme_view))
{
if(!is_file($script_module_view))
{
if(!is_file($module_view))
{
$output=ob_get_contents();
ob_clean();
$check_error_lang[0]='Error while loading template, check that the view exists...';
$check_error_lang[1]='Error while loading template library '.$template.' in path '.$theme_view.' ,'.$script_module_view.' and '.$module_view.', check config.php or that template library exists... ';
show_error($check_error_lang[0], $check_error_lang[1], $output);
ob_end_flush();
die;
}
else
{
include($module_view);
}
}
else
{
include($script_module_view);
}
}
else
{
include($theme_view);
}*/
//If load view, save function name for call write the html again without call include view too //If load view, save function name for call write the html again without call include view too
if($yes_cache==1) if($yes_cache==1)
@ -164,6 +155,8 @@ class View {
//Load function from loaded view with his parameters //Load function from loaded view with his parameters
array_unshift($arr_template_values, $this);
call_user_func_array($func_view, $arr_template_values); call_user_func_array($func_view, $arr_template_values);
$out_template=ob_get_contents(); $out_template=ob_get_contents();
@ -174,6 +167,201 @@ class View {
} }
/**
*
*/
public function dynamicGetMediaUrl($path_file)
{
return $this->php_file.'/'.$path_file;
}
/**
*
*/
public function staticGetMediaUrl($path_file)
{
return $this->url_media.'/'.$path_file;
}
/**
*
*/
public function setProduction($value=1)
{
if($value==1)
{
$production=1;
$this->func_media='staticGetMediaUrl';
}
else
{
$production=0;
$this->func_media='dynamicGetMediaUrl';
}
}
/**
*
*/
public function getMediaUrl($path_file)
{
$func_media=$this->func_media;
return $this->$func_media($path_file);
}
/**
* Method for load media files. Method for load simple media file, is only for development
*
* This method is used on php files for retrieve media files using a very simple url dispatcher.
*
* @warning NO USE THIS METHOD IN PRODUCTION.
*
*/
public function loadMediaFile($url)
{
//Check files origin.
if($this->production==0)
{
$yes_file=0;
$arr_url=explode($this->php_file.'/', $url);
$final_path='';
if(isset($arr_url[1]))
{
//Clean the path of undesirerable elements.
$arr_path=explode('/', $arr_url[1]);
$c=count($arr_path)-1;
//foreach($arr_path as $key_path => $item_path)
for($x=0;$x<$c-1;$x++)
{
$arr_path[$key_path]=Utils::slugify($item_path, $respect_upper=0, $replace_space='-', $replace_dot=1, $replace_barr=1);
}
$arr_path[$c]=Utils::slugify($arr_path[$c], $respect_upper=1, $replace_space='-', $replace_dot=0, $replace_barr=1);
$final_path=implode('/', $arr_path);
}
foreach($this->folder_env as $folder)
{
$file_path=$this->root_path.'/'.$folder.'/'.$this->path_media.'/'.$final_path;
if(is_file($file_path))
{
$yes_file=1;
break;
}
}
if($yes_file==1)
{
$ext_info=pathinfo($file_path);
settype($ext_info['extension'], 'string');
switch($ext_info['extension'])
{
default:
$type_mime='text/plain';
break;
case 'js':
$type_mime='application/javascript';
break;
case 'css':
$type_mime='text/css';
break;
case 'gif':
$type_mime='image/gif';
break;
case 'png':
$type_mime='image/png';
break;
case 'jpg':
$type_mime='image/jpg';
break;
}
header('Content-Type: '.$type_mime);
readfile($file_path);
die;
}
else
{
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
echo 'File not found...';
die;
}
}
}
} }