3 class mw_env_out extends mw_env{
14 // ---------------------------------------------------------------------------------
18 function set_out($key, $value){
19 $this->out[$key] = $value;
28 return isset($this->out[$key]) ? $this->out[$key] : null;
31 // ---------------------------------------------------------------------------------
36 if(($out_pathes = $this->out_pathes()) === false) return false;
38 $current_out_path = $this->config("out");
39 foreach($out_pathes as $out_path){
40 $this->set_config(array("out" => $out_path));
41 $templates[$out_path] = $this->_get_template();
43 $this->set_config(array("out" => $current_out_path));
47 function get_template(){
48 if(isset($this->template)) return $this->template;
49 $this->template = $this->_get_template();
50 return $this->template;
53 function _get_template(){
54 $template_class_name = "mw_template";
55 if($this->out_file_exists("template.php")){
56 $template_class_file = $this->out_file("template.php");
57 $template_class_name = "mw_template_".basename(dirname($template_class_file));
58 if(!class_exists($template_class_name)) require_once $template_class_file;
59 if(!class_exists($template_class_name)) $template_class_name = "mw_template";
61 $template = new $template_class_name(true);
62 $template->set_env($this->root_inst);
66 function out_pathes(){
68 if(($plugins = $this->plugins()) !== false){
69 foreach($plugins as $plugin_name => $plugin){
70 $out_dir = $this->path("mw_dir")."plugins/".$plugin_name."/app/out/";
74 && file_exists($out_dir)
77 if(($pathes = $this->_out_pathes($out_dir, $pathes)) === false) break;
80 if($pathes !== false){
81 $pathes = $this->_out_pathes($this->path("mw_dir")."app/out/", $pathes);
88 function _out_pathes($out_dir, $pathes = array()){
89 if($dh = opendir($out_dir)){
90 while(($file = readdir($dh)) !== false){
91 if(is_dir($out_dir.$file) && substr($file, 0 ,1) != "." && !in_array($file, $pathes)) $pathes[] = $file;
99 // ---------------------------------------------------------------------------------
103 function out_file_exists($file, $PRIORITE = "DESC"){
104 $out_file = $this->_out_file($file, $PRIORITE);
105 return $out_file ? true : false;
108 function out_file($file, $PRIORITE = "DESC"){
109 $out_file = $this->_out_file($file, $PRIORITE);
110 return $out_file ? $this->path("mw_dir").$out_file : $file;
113 function out_url($file, $PRIORITE = "DESC"){
114 $out_file = $this->_out_file($file, $PRIORITE);
115 return $out_file ? $this->path("web").$this->path("mw_path").$out_file : $file;
118 function _out_file($file, $PRIORITE = "DESC"){
120 if($PRIORITE == "ASC"){
121 $tmp_out_file = "app/out/".$this->config("out")."/".$file;
122 if($file && file_exists($this->path("mw_dir").$tmp_out_file)){
123 $out_file = $tmp_out_file;
126 $tmp_out_file = "app/out/".$this->config("default_out")."/".$file;
127 if($file && file_exists($this->path("mw_dir").$tmp_out_file)){
128 $out_file = $tmp_out_file;
132 if($out_file) return $out_file;
133 if(($plugins = $this->plugins($PRIORITE)) !== false){
134 foreach($plugins as $plugin_name => $plugin){
135 $tmp_out_file = "plugins/".$plugin_name."/app/out/".$this->config("out")."/".$file;
136 if($file && $plugin["installed"] && $plugin["enabled"] && file_exists($this->path("mw_dir").$tmp_out_file)){
137 $out_file = $tmp_out_file;
141 $tmp_out_file = "plugins/".$plugin_name."/app/out/".$this->config("default_out")."/".$file;
142 if($file && $plugin["installed"] && $plugin["enabled"] && file_exists($this->path("mw_dir").$tmp_out_file)){
143 $out_file = $tmp_out_file;
148 if($PRIORITE == "DESC" && !$out_file){
149 $tmp_out_file = "app/out/".$this->config("out")."/".$file;
150 if($file && file_exists($this->path("mw_dir").$tmp_out_file)){
151 $out_file = $tmp_out_file;
154 $tmp_out_file = "app/out/".$this->config("default_out")."/".$file;
155 if($file && file_exists($this->path("mw_dir").$tmp_out_file)){
156 $out_file = $tmp_out_file;
164 // ---------------------------------------------------------------------------------
169 if(!isset($this->js_files)) $this->js_files = array();
171 foreach($this->js_files as $url => $enabled){
172 if($enabled) $files[] = $url;
177 function add_js_file($url){
178 if(!isset($this->js_files)) $this->js_files = array();
179 $this->js_files[$url] = true;
182 function remove_js_file($url){
183 if(isset($this->js_files) && isset($this->js_files[$url])){
184 unset($this->js_files[$url]);
188 function css_files(){
189 if(!isset($this->css_files)) $this->css_files = array();
191 foreach($this->css_files as $url => $enabled){
192 if($enabled) $files[] = $url;
197 function add_css_file($url){
198 if(!isset($this->css_files)) $this->css_files = array();
199 $this->css_files[$url] = true;
202 function remove_css_file($url){
203 if(isset($this->css_files) && isset($this->css_files[$url])){
204 unset($this->css_files[$url]);
208 // ---------------------------------------------------------------------------------
212 function set_out_config($out_config){
213 $this->out_config = $out_config;
214 return $this->out_config;
217 function get_out_config(){
218 return isset($this->out_config) ? $this->out_config : array();
221 function out_config($name){
222 if(isset($this->out_config)){
223 $CONFIG = $this->get_CONFIG();
224 return isset($CONFIG["out_".$name]) ? $CONFIG["out_".$name] : (isset($this->out_config[$name]) ? $this->out_config[$name]["default"] : "");
229 // ---------------------------------------------------------------------------------
234 return $this->layout;
237 function init_layout(){
238 $this->layout = array();
239 $this->_init_layout("index");
240 if(($mod = $this->etat("mod")) != "index") $this->_init_layout($mod);
241 return $this->get_layout();
244 function _init_layout($mod){
245 if(($plugins = $this->plugins("ASC")) !== false){
246 $layout_file = false;
247 $tmp_layout_file = $this->path("mw_dir")."app/out/".$this->config("out")."/layouts/".$mod.".xml";
248 if(file_exists($tmp_layout_file)) $layout_file = $tmp_layout_file;
250 $tmp_layout_file = $this->path("mw_dir")."app/out/".$this->config("default_out")."/layouts/".$mod.".xml";
251 if(file_exists($tmp_layout_file)) $layout_file = $tmp_layout_file;
253 if($layout_file) $this->load_layout($layout_file);
254 foreach($plugins as $plugin_name => $plugin){
255 if($plugin["installed"] && $plugin["enabled"]){
256 $layout_file = false;
257 $tmp_layout_file = $this->path("mw_dir")."plugins/".$plugin_name."/app/out/".$this->config("out")."/layouts/".$mod.".xml";
258 if(file_exists($tmp_layout_file)) $layout_file = $tmp_layout_file;
260 $tmp_layout_file = $this->path("mw_dir")."plugins/".$plugin_name."/app/out/".$this->config("default_out")."/layouts/".$mod.".xml";
261 if(file_exists($tmp_layout_file)) $layout_file = $tmp_layout_file;
263 if($layout_file) $this->load_layout($layout_file);
269 function load_layout($layout_file){
270 if(file_exists($layout_file)){
271 $xml_parser = new sxml();
272 $xml_parser->parse(file_get_contents($layout_file));
273 $layout = $xml_parser->data;
274 if(isset($layout["layout"][0]["subs"])){
275 foreach($layout["layout"][0]["subs"] as $mod => $mod_node){
276 if(!isset($this->layout[$mod])){
277 $this->layout[$mod] = array(
280 "controllers" => array()
283 if(isset($mod_node[0]["attrs"]["page"])) $this->layout[$mod]["page"] = $mod_node[0]["attrs"]["page"];
284 if(isset($mod_node[0]["attrs"]["content"])) $this->layout[$mod]["content"] = $mod_node[0]["attrs"]["content"];
285 if(isset($mod_node[0]["subs"])){
286 foreach($mod_node[0]["subs"] as $controller => $controller_node){
287 if(!isset($this->layout[$mod]["controllers"][$controller])){
288 $this->layout[$mod]["controllers"][$controller] = array(
294 if(isset($controller_node[0]["attrs"]["page"])) $this->layout[$mod]["controllers"][$controller]["page"] = $controller_node[0]["attrs"]["page"];
295 if(isset($controller_node[0]["attrs"]["content"])) $this->layout[$mod]["controllers"][$controller]["content"] = $controller_node[0]["attrs"]["content"];
296 if(isset($controller_node[0]["subs"])){
297 foreach($controller_node[0]["subs"] as $action => $action_node){
298 if(!isset($this->layout[$mod]["controllers"][$controller]["actions"][$action])){
299 $this->layout[$mod]["controllers"][$controller]["actions"][$action] = array(
304 if(isset($action_node[0]["attrs"]["page"])) $this->layout[$mod]["controllers"][$controller]["actions"][$action]["page"] = $action_node[0]["attrs"]["page"];
305 if(isset($action_node[0]["attrs"]["content"])) $this->layout[$mod]["controllers"][$controller]["actions"][$action]["content"] = $action_node[0]["attrs"]["content"];
316 function get_layout(){
317 $mod = $this->etat("mod");
318 $controller = $this->etat("controller");
319 $action = $this->etat("action");
321 if(isset($this->layout[$mod]["controllers"][$controller]["actions"][$action]["content"])){
322 $content = $this->layout[$mod]["controllers"][$controller]["actions"][$action]["content"];
325 if(isset($this->layout[$mod]["controllers"][$controller]["content"])){
326 $content = $this->layout[$mod]["controllers"][$controller]["content"];
329 if(isset($this->layout[$mod]["content"])){
330 $content = $this->layout[$mod]["content"];
333 if(isset($this->layout["index"]["content"])){
334 $content = $this->layout["index"]["content"];
340 if(isset($this->layout[$mod]["controllers"][$controller]["actions"][$action]["page"])){
341 $page = $this->layout[$mod]["controllers"][$controller]["actions"][$action]["page"];
344 if(isset($this->layout[$mod]["controllers"][$controller]["page"])){
345 $page = $this->layout[$mod]["controllers"][$controller]["page"];
348 if(isset($this->layout[$mod]["page"])){
349 $page = $this->layout[$mod]["page"];
352 if(isset($this->layout["index"]["page"])){
353 $page = $this->layout["index"]["page"];
360 "content" => $content
366 class mw_template extends empty_class{
372 function set_env(&$env){
373 $this->modules = array("env" => $env);
374 $this->ENV_SET = true;
375 $this->set_out_config($this->get_out_config());
376 $this->set_template_infos($this->get_template_infos());
379 function render_layout($layout = null){
380 if(!isset($this->ENV_SET) || !$this->ENV_SET) return false;
381 if(!isset($layout)) $layout = $this->init_layout();
382 $this->out = $this->get_out();
385 ($init_script = $this->init_script())
386 && $this->out_file_exists($init_script)
387 ) require $this->out_file($init_script);
388 $data = $this->data();
390 if($this->out_file_exists($layout["page"])) require $this->out_file($layout["page"]);
392 elseif($layout["content"]){
393 if($this->out_file_exists($layout["content"])) require $this->out_file($layout["content"]);
397 function get_template_info($key){
398 return isset($this->template_infos[$key]) ? $this->template_infos[$key] : "";
405 function init_script(){
409 function get_out_config(){
410 $out_config = array();
412 $env = $this->modules["env"];
413 $out_config = $env->get_out_config();
418 function set_template_infos($template_infos){
419 $this->template_infos = $template_infos;
422 function get_template_infos(){