From 153927546886cacb36669951336d471cc5849990 Mon Sep 17 00:00:00 2001 From: Antonio de la Rosa Date: Mon, 20 Apr 2015 04:31:09 +0200 Subject: [PATCH] Fixes on view --- src/View.php | 306 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 247 insertions(+), 59 deletions(-) diff --git a/src/View.php b/src/View.php index c58c89f..0dd16cb 100644 --- a/src/View.php +++ b/src/View.php @@ -1,6 +1,7 @@ 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 * @@ -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. */ - 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 @@ -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($yes_cache==1) @@ -163,6 +154,8 @@ class View { $func_view=$this->cache_template[$template]; //Load function from loaded view with his parameters + + array_unshift($arr_template_values, $this); call_user_func_array($func_view, $arr_template_values); @@ -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; + + } + + } + + } + }