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. */ static public $header=array(); /** * An array with config_paths for load configurations. */ static public $config_path=['settings']; /** * A boolean property for set if views are escaped by default */ static public $escape=false; /** * The directory where formatted views are saved. */ static public $cache_directory='cache/templates'; /** * Debug */ static public $debug_tpl=false; /** * The construct for create a view object * * @param string $folder_base The folder used how base path for search view files */ /*public function __construct() { $arr_arg=func_get_args(); if(count($arr_arg)>0) { View::$folder_env=$arr_arg; } View::$root_path=getcwd(); }*/ static public function load_config() { //Load config here foreach(View::config_path as $config) { Utils::load_config("config_views", $config); } } /** * Very important function used for load views. Is the V in the MVC paradigm. Phango is an MVC framework and has separate code and html. * * load_view is used for load the views. Views in Phango are php files with a function that have a special name with "View" suffix. For example, if you create a view file with the name blog.php, inside you need create a php function called BlogView(). The arguments of this function can be that you want, how on any normal php function. The view files need to be saved on a "view" folders inside of a theme folder, or a "views/module_name" folder inside of a module being "module_name" the name of the module. * * @param array $arr_template_values Arguments for the view function of the view. * @param string $template Name of the view. Tipically views/$template.php or modules/name_module/views/name_module/$template.php * @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. */ static public function load_view($arr_template_values, $template, $module='', $escape=true) { //First see in controller/view/template, if not see in /views/template $yes_cache=0; $all_path=array(); if(!isset(View::$cache_template[$template])) { if($module!='') { //vendor/phangoapp/admin/views View::$folder_env[]='vendor/'.$module.'/views'; } foreach(View::$folder_env as $base_path) { $view_real_path=View::$root_path.'/'.$base_path.'/'.$template.'.php'; $view_path=View::$root_path.'/'.View::$cache_directory.'/'.$base_path.'/'.$template.'.php'; $all_path[]=$view_real_path; $all_cache_path[]=$view_path; if(is_file($view_real_path)) { /*if(View::$escape==true && $escape==true) { if(!is_file($view_path)) { $directory_cache=View::$root_path.'/'.View::$cache_directory.'/'.$base_path.'/'.dirname($template); if(!is_dir($directory_cache)) { if(!mkdir($directory_cache, 0755, true)) { throw new \Exception('Error: directory cache cant be created: '.$directory_cache); die; } } } }*/ } //If escape then get /*if(View::$escape==true && $escape==true) { if(!is_file($view_path)) { $directory_cache=View::$root_path.'/'.View::$cache_directory.'/'.$base_path.'/'.dirname($template); if(!is_dir($directory_cache)) { if(!mkdir($directory_cache, 0755, true)) { throw new \Exception('Error: directory cache cant be created: '.$directory_cache); die; } } View::add_cache_file($view_real_path, $view_path); } elseif(View::$debug_tpl) { $time_cached=filemtime($view_path); $time_real_cached=filemtime($view_real_path); if($time_real_cached>$time_cached) { View::add_cache_file($view_real_path, $view_path); } } }*/ $all_path[]=$view_real_path; if(is_file($view_real_path)) { include($view_real_path); $yes_cache=1; break; } } //If load view, save function name for call write the html again without call include view too if($yes_cache==1) { View::$cache_template[$template]=basename($template).'View'; } else { throw new \Exception('Error: view not found: '.implode(' and ', $all_path)); die; } } ob_start(); $func_view=View::$cache_template[$template]; if(!function_exists($func_view)) { throw new \Exception('Error: Template file loaded but function '.$func_view.' not found: '.implode(' and ', $all_path)); die; } //Load function from loaded view with his parameters //array_unshift($arr_template_values, $this); call_user_func_array($func_view, $arr_template_values); $out_template=ob_get_contents(); ob_end_clean(); return $out_template; } static public function add_cache_file($view_real_path, $view_path) { //Create the cache file $file=file_get_contents($view_real_path); #/', '', $file); $file=preg_replace('/<\?php echo\s+([^;]*?);\|n/', '/', '', $file); $file=preg_replace('/<\?php echo\s+(.*?);/', ' $item_path) for($x=0;$x<$c-1;$x++) { $arr_path[$x]=Utils::slugify($arr_path[$x], $respect_upper=0, $replace_space='-', $replace_dot=1, $replace_barr=1); } $arr_path[$c]=$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(View::$media_env as $folder) { $file_path=View::$root_path.'/'.$folder.'/'.View::$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; case 'html': $type_mime='text/html'; break; } header('Content-Type: '.$type_mime); readfile($file_path); die; } else { header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); echo 'File not found...'; die; } } } /** * Method that use $this->css array for create a series of html tags that load the css stylesheets * * This function is used inside of html tag normally for load css files. */ static public function load_css() { $arr_final_css=array(); View::$css=array_unique(View::$css); foreach(View::$css as $css) { $url=View::get_media_url('css/'.$css); $arr_final_css[]=''."\n"; } foreach(View::$css_module as $module_css => $css) { $css=array_unique($css); foreach($css as $module => $css_item) { $url=View::get_media_url('css/'.$css_item, $module_css); $arr_final_css[]=''."\n"; } } return implode('', $arr_final_css); } /** * Method that use $this->js array for create a series of html tags that load the javascript files * * This function is used inside of html tag normally for load js files. */ static public function load_js() { $arr_final_js=array(); View::$js=array_unique(View::$js); foreach(View::$js as $module_js => $js) { $url=View::get_media_url('js/'.$js); $arr_final_js[]=$arr_final_jscript[]=''."\n"; } foreach(View::$js_module as $module_js => $js) { $js=array_unique($js); foreach($js as $module => $js_item) { $url=View::get_media_url('js/'.$js_item, $module_js); $arr_final_js[]=''."\n"; } } return implode('', $arr_final_js); } /** * Method that use $this->header array for create a series of code (normally javascript) on tag. * * This function is used inside of html tag normally for load inline javascript code. */ static public function load_header() { $arr_final_header=array(); foreach(View::$header as $header) { $arr_final_header[]=$header."\n"; } return implode("\n", $arr_final_header); } /** * A method for make a redirect based on a theme * */ static public function load_theme($title, $cont_index) { echo View::load_view(array($title, $cont_index),'home'); } /** * Function for load multiple views for a only source file. * * Useful for functions where you need separated views for use on something, When you use load_view for execute a view function, the names used for views are in $func_views array. * * @param string $template of the view library. Use the same format for normal views. * @param string The names of templates, used how template_name for call views with load_view. */ static public function load_libraries_views($template, $func_views=array()) { foreach(View::$folder_env as $base_path) { $view_path=View::$root_path.'/'.$base_path.'/'.$template.'.php'; if(is_file($view_path)) { include($view_path); foreach($func_views as $template) { View::$cache_template[$template]=basename($template).'View'; } break; } } } /** * Method for set the flash message */ static public function set_flash($text) { $_SESSION['flash_txt']=$text; } /** * Method for show the flash message */ static public function show_flash() { if(isset($_SESSION['flash_txt'])) { if($_SESSION['flash_txt']!='') { $text=$_SESSION['flash_txt']; $_SESSION['flash_txt']=''; return View::load_view(array($text), 'common/utils/flash'); } } return ''; } /** * Method for escape the variables in an view */ static public function e($text) { $text=htmlspecialchars($text, ENT_QUOTES, 'UTF-8'); return $text; } static public function d($text) { echo $text; } }