out[$key] = $value; return $value; } function get_out(){ return $this->out; } function out($key){ return isset($this->out[$key]) ? $this->out[$key] : null; } // --------------------------------------------------------------------------------- // templates // function templates(){ if(($out_pathes = $this->out_pathes()) === false) return false; $templates = array(); $current_out_path = $this->config("out"); foreach($out_pathes as $out_path){ $this->set_config(array("out" => $out_path)); $templates[$out_path] = $this->_get_template(); } $this->set_config(array("out" => $current_out_path)); return $templates; } function get_template(){ if(isset($this->template)) return $this->template; $this->template = $this->_get_template(); return $this->template; } function _get_template(){ $template_class_name = "mw_template"; if($this->out_file_exists("template.php")){ $template_class_file = $this->out_file("template.php"); $template_class_name = "mw_template_".basename(dirname($template_class_file)); if(!class_exists($template_class_name)) require_once $template_class_file; if(!class_exists($template_class_name)) $template_class_name = "mw_template"; } $template = new $template_class_name(true); $template->set_env($this->root_inst); return $template; } function out_pathes(){ $pathes = array(); if(($plugins = $this->plugins()) !== false){ foreach($plugins as $plugin_name => $plugin){ $out_dir = $this->path("mw_dir")."plugins/".$plugin_name."/app/out/"; if( $plugin["installed"] && $plugin["enabled"] && file_exists($out_dir) && is_dir($out_dir) ){ if(($pathes = $this->_out_pathes($out_dir, $pathes)) === false) break; } } if($pathes !== false){ $pathes = $this->_out_pathes($this->path("mw_dir")."app/out/", $pathes); } } else $pathes = false; return $pathes; } function _out_pathes($out_dir, $pathes = array()){ if($dh = opendir($out_dir)){ while(($file = readdir($dh)) !== false){ if(is_dir($out_dir.$file) && substr($file, 0 ,1) != "." && !in_array($file, $pathes)) $pathes[] = $file; } closedir($dh); } else $pathes = false; return $pathes; } // --------------------------------------------------------------------------------- // out files // function out_file_exists($file, $PRIORITE = "DESC"){ $out_file = $this->_out_file($file, $PRIORITE); return $out_file ? true : false; } function out_file($file, $PRIORITE = "DESC"){ $out_file = $this->_out_file($file, $PRIORITE); return $out_file ? $this->path("mw_dir").$out_file : $file; } function out_url($file, $PRIORITE = "DESC"){ $out_file = $this->_out_file($file, $PRIORITE); return $out_file ? $this->path("web").$this->path("mw_path").$out_file : $file; } function _out_file($file, $PRIORITE = "DESC"){ $out_file = false; if($PRIORITE == "ASC"){ $tmp_out_file = "app/out/".$this->config("out")."/".$file; if($file && file_exists($this->path("mw_dir").$tmp_out_file)){ $out_file = $tmp_out_file; } if(!$out_file){ $tmp_out_file = "app/out/".$this->config("default_out")."/".$file; if($file && file_exists($this->path("mw_dir").$tmp_out_file)){ $out_file = $tmp_out_file; } } } if($out_file) return $out_file; if(($plugins = $this->plugins($PRIORITE)) !== false){ foreach($plugins as $plugin_name => $plugin){ $tmp_out_file = "plugins/".$plugin_name."/app/out/".$this->config("out")."/".$file; if($file && $plugin["installed"] && $plugin["enabled"] && file_exists($this->path("mw_dir").$tmp_out_file)){ $out_file = $tmp_out_file; break; } if(!$out_file){ $tmp_out_file = "plugins/".$plugin_name."/app/out/".$this->config("default_out")."/".$file; if($file && $plugin["installed"] && $plugin["enabled"] && file_exists($this->path("mw_dir").$tmp_out_file)){ $out_file = $tmp_out_file; break; } } } if($PRIORITE == "DESC" && !$out_file){ $tmp_out_file = "app/out/".$this->config("out")."/".$file; if($file && file_exists($this->path("mw_dir").$tmp_out_file)){ $out_file = $tmp_out_file; } if(!$out_file){ $tmp_out_file = "app/out/".$this->config("default_out")."/".$file; if($file && file_exists($this->path("mw_dir").$tmp_out_file)){ $out_file = $tmp_out_file; } } } } return $out_file; } // --------------------------------------------------------------------------------- // js / css files // function js_files(){ if(!isset($this->js_files)) $this->js_files = array(); $files = array(); foreach($this->js_files as $url => $enabled){ if($enabled) $files[] = $url; } return $files; } function add_js_file($url){ if(!isset($this->js_files)) $this->js_files = array(); $this->js_files[$url] = true; } function remove_js_file($url){ if(isset($this->js_files) && isset($this->js_files[$url])){ unset($this->js_files[$url]); } } function css_files(){ if(!isset($this->css_files)) $this->css_files = array(); $files = array(); foreach($this->css_files as $url => $enabled){ if($enabled) $files[] = $url; } return $files; } function add_css_file($url){ if(!isset($this->css_files)) $this->css_files = array(); $this->css_files[$url] = true; } function remove_css_file($url){ if(isset($this->css_files) && isset($this->css_files[$url])){ unset($this->css_files[$url]); } } // --------------------------------------------------------------------------------- // out config // function set_out_config($out_config){ $this->out_config = $out_config; return $this->out_config; } function get_out_config(){ return isset($this->out_config) ? $this->out_config : array(); } function out_config($name){ if(isset($this->out_config)){ $CONFIG = $this->get_CONFIG(); return isset($CONFIG["out_".$name]) ? $CONFIG["out_".$name] : (isset($this->out_config[$name]) ? $this->out_config[$name]["default"] : ""); } return null; } // --------------------------------------------------------------------------------- // layouts // function layout(){ return $this->layout; } function init_layout(){ $this->layout = array(); $this->_init_layout("index"); if(($mod = $this->etat("mod")) != "index") $this->_init_layout($mod); return $this->get_layout(); } function _init_layout($mod){ if(($plugins = $this->plugins("ASC")) !== false){ $layout_file = false; $tmp_layout_file = $this->path("mw_dir")."app/out/".$this->config("out")."/layouts/".$mod.".xml"; if(file_exists($tmp_layout_file)) $layout_file = $tmp_layout_file; if(!$layout_file){ $tmp_layout_file = $this->path("mw_dir")."app/out/".$this->config("default_out")."/layouts/".$mod.".xml"; if(file_exists($tmp_layout_file)) $layout_file = $tmp_layout_file; } if($layout_file) $this->load_layout($layout_file); foreach($plugins as $plugin_name => $plugin){ if($plugin["installed"] && $plugin["enabled"]){ $layout_file = false; $tmp_layout_file = $this->path("mw_dir")."plugins/".$plugin_name."/app/out/".$this->config("out")."/layouts/".$mod.".xml"; if(file_exists($tmp_layout_file)) $layout_file = $tmp_layout_file; if(!$layout_file){ $tmp_layout_file = $this->path("mw_dir")."plugins/".$plugin_name."/app/out/".$this->config("default_out")."/layouts/".$mod.".xml"; if(file_exists($tmp_layout_file)) $layout_file = $tmp_layout_file; } if($layout_file) $this->load_layout($layout_file); } } } } function load_layout($layout_file){ if(file_exists($layout_file)){ $xml_parser = new sxml(); $xml_parser->parse(file_get_contents($layout_file)); $layout = $xml_parser->data; if(isset($layout["layout"][0]["subs"])){ foreach($layout["layout"][0]["subs"] as $mod => $mod_node){ if(!isset($this->layout[$mod])){ $this->layout[$mod] = array( "page" => null, "content" => null, "controllers" => array() ); } if(isset($mod_node[0]["attrs"]["page"])) $this->layout[$mod]["page"] = $mod_node[0]["attrs"]["page"]; if(isset($mod_node[0]["attrs"]["content"])) $this->layout[$mod]["content"] = $mod_node[0]["attrs"]["content"]; if(isset($mod_node[0]["subs"])){ foreach($mod_node[0]["subs"] as $controller => $controller_node){ if(!isset($this->layout[$mod]["controllers"][$controller])){ $this->layout[$mod]["controllers"][$controller] = array( "page" => null, "content" => null, "actions" => array() ); } if(isset($controller_node[0]["attrs"]["page"])) $this->layout[$mod]["controllers"][$controller]["page"] = $controller_node[0]["attrs"]["page"]; if(isset($controller_node[0]["attrs"]["content"])) $this->layout[$mod]["controllers"][$controller]["content"] = $controller_node[0]["attrs"]["content"]; if(isset($controller_node[0]["subs"])){ foreach($controller_node[0]["subs"] as $action => $action_node){ if(!isset($this->layout[$mod]["controllers"][$controller]["actions"][$action])){ $this->layout[$mod]["controllers"][$controller]["actions"][$action] = array( "page" => null, "content" => null ); } if(isset($action_node[0]["attrs"]["page"])) $this->layout[$mod]["controllers"][$controller]["actions"][$action]["page"] = $action_node[0]["attrs"]["page"]; if(isset($action_node[0]["attrs"]["content"])) $this->layout[$mod]["controllers"][$controller]["actions"][$action]["content"] = $action_node[0]["attrs"]["content"]; } } } } } } } return false; } function get_layout(){ $mod = $this->etat("mod"); $controller = $this->etat("controller"); $action = $this->etat("action"); $content = ""; if(isset($this->layout[$mod]["controllers"][$controller]["actions"][$action]["content"])){ $content = $this->layout[$mod]["controllers"][$controller]["actions"][$action]["content"]; } else{ if(isset($this->layout[$mod]["controllers"][$controller]["content"])){ $content = $this->layout[$mod]["controllers"][$controller]["content"]; } else{ if(isset($this->layout[$mod]["content"])){ $content = $this->layout[$mod]["content"]; } else{ if(isset($this->layout["index"]["content"])){ $content = $this->layout["index"]["content"]; } } } } $page = ""; if(isset($this->layout[$mod]["controllers"][$controller]["actions"][$action]["page"])){ $page = $this->layout[$mod]["controllers"][$controller]["actions"][$action]["page"]; } else{ if(isset($this->layout[$mod]["controllers"][$controller]["page"])){ $page = $this->layout[$mod]["controllers"][$controller]["page"]; } else{ if(isset($this->layout[$mod]["page"])){ $page = $this->layout[$mod]["page"]; } else{ if(isset($this->layout["index"]["page"])){ $page = $this->layout["index"]["page"]; } } } } return array( "page" => $page, "content" => $content ); } } class mw_template extends empty_class{ var $out; var $ENV_SET; var $template_infos; function set_env(&$env){ $this->modules = array("env" => $env); $this->ENV_SET = true; $this->set_out_config($this->get_out_config()); $this->set_template_infos($this->get_template_infos()); } function render_layout($layout = null){ if(!isset($this->ENV_SET) || !$this->ENV_SET) return false; if(!isset($layout)) $layout = $this->init_layout(); $this->out = $this->get_out(); $this->init(); if( ($init_script = $this->init_script()) && $this->out_file_exists($init_script) ) require $this->out_file($init_script); $data = $this->data(); if($layout["page"]){ if($this->out_file_exists($layout["page"])) require $this->out_file($layout["page"]); } elseif($layout["content"]){ if($this->out_file_exists($layout["content"])) require $this->out_file($layout["content"]); } } function get_template_info($key){ return isset($this->template_infos[$key]) ? $this->template_infos[$key] : ""; } function init(){ return true; } function init_script(){ return false; } function get_out_config(){ $out_config = array(); if($this->ENV_SET){ $env = $this->modules["env"]; $out_config = $env->get_out_config(); } return $out_config; } function set_template_infos($template_infos){ $this->template_infos = $template_infos; } function get_template_infos(){ return array(); } } ?>