6571d65046119db14bf5d09cc7efbce655d13549
[mtweb] / web / app / env / modules / mw_env_out.php
1 <?php
2
3   class mw_env_out extends mw_env
4   {
5     var $out;
6     var $out_config;
7     var $layout;
8
9     function set_out($key, $value) { $this->out[$key] = $value; return $value; }
10     function get_out() { return $this->out; }
11     function out($key) { return $this->out[$key]; }
12
13     function out_pathes()
14     { $pathes = array();
15       if($dh = opendir($this->path("out")))
16       { while(($file = readdir($dh)) !== false)
17         { if(is_dir($this->path("out").$file) && substr($file, 0 ,1) != ".") $pathes[] = $file;
18         }
19         closedir($dh);
20       }
21       else $pathes = false;
22       return $pathes;
23     }
24
25     function out_file_exists($file, $PRIORITE = "DESC")
26     { $out_file = $this->_out_file($file, $PRIORITE);
27       return $out_file ? true : false;
28     }
29
30     function out_file($file, $PRIORITE = "DESC")
31     { $out_file = $this->_out_file($file, $PRIORITE);
32       return $out_file ? $out_file : $file;
33     }
34
35     function _out_file($file, $PRIORITE = "DESC")
36     { $out_file = false;
37       if($PRIORITE == "ASC")
38       { $tmp_out_file = $this->path("out").$this->config("out").$file;
39         if($file && file_exists($tmp_out_file))
40         { $out_file = $tmp_out_file;
41         }
42         if(!$out_file)
43         { $tmp_out_file = $this->path("out").$this->path("dist_out").$file;
44           if($file && file_exists($tmp_out_file))
45           { $out_file = $tmp_out_file;
46           }
47         }
48       }
49       if($out_file) return $out_file;
50       if(($plugins = $this->plugins($PRIORITE)) !== false)
51       { foreach($plugins as $plugin_name => $plugin)
52         { $tmp_out_file = $this->path("plugins").$plugin_name."/out/".$this->config("out").$file;
53           if($file && $plugin["installed"] && $plugin["enabled"] && file_exists($tmp_out_file))
54           { $out_file = $tmp_out_file;
55             break;
56           }
57           if(!$out_file)
58           { $tmp_out_file = $this->path("plugins").$plugin_name."/out/".$this->path("dist_out").$file;
59             if($file && $plugin["installed"] && $plugin["enabled"] && file_exists($tmp_out_file))
60             { $out_file = $tmp_out_file;
61               break;
62             }
63           }
64         }
65         if($PRIORITE == "DESC" && !$out_file)
66         { $tmp_out_file = $this->path("out").$this->config("out").$file;
67           if($file && file_exists($tmp_out_file))
68           { $out_file = $tmp_out_file;
69           }
70           if(!$out_file)
71           { $tmp_out_file = $this->path("out").$this->path("dist_out").$file;
72             if($file && file_exists($tmp_out_file))
73             { $out_file = $tmp_out_file;
74             }
75           }
76         }
77       }
78       return $out_file;
79     }
80
81     # ---------------------------------------------------------------------------------
82     #                                                                        out config
83     #
84
85     function set_out_config($out_config)
86     { $this->out_config = $out_config;
87       return $this->out_config;
88     }
89
90     function get_out_config() { return isset($this->out_config) ? $this->out_config : array(); }
91
92     function out_config($name)
93     { if(isset($this->out_config))
94       { $CONFIG = $this->get_CONFIG();
95         return isset($CONFIG["out_".$name]) ? $CONFIG["out_".$name] : $this->out_config[$name]["default"];
96       }
97       return null;
98     }
99
100     # ---------------------------------------------------------------------------------
101     #                                                                           layouts
102     #
103
104     function layout() { return $this->layout; }
105
106     function render_layout($layout = null)
107     { if(!isset($layout)) $layout = $this->init_layout();
108       if(($plugins = $this->plugins("ASC")) !== false)
109       { foreach($plugins as $plugin_name => $plugin)
110         { if($plugin["installed"] && $plugin["enabled"])
111           { $FOUND = false;
112             $functions_file = $this->path("plugins").$plugin_name."/out/".$this->config("out")."functions.php";
113             if(file_exists($functions_file))
114             { $FOUND = true;
115               require $functions_file;
116             }
117             if(!$FOUND)
118             { $functions_file = $this->path("plugins").$plugin_name."/out/".$this->path("dist_out")."functions.php";
119               if($plugin["installed"] && $plugin["enabled"] && file_exists($functions_file))
120               { require $functions_file;
121               }
122             }
123           }
124         }
125         $FOUND = false;
126         $functions_file = $this->path("out").$this->config("out")."functions.php";
127         if(file_exists($functions_file))
128         { $FOUND = true;
129           require $functions_file;
130         }
131         if(!$FOUND)
132         { $functions_file = $this->path("out").$this->path("dist_out")."functions.php";
133           if(file_exists($functions_file))
134           { require $functions_file;
135           }
136         }
137         if($layout["page"])
138         { if($this->out_file_exists($layout["page"])) require $this->out_file($layout["page"]);
139         }
140         elseif($layout["content"])
141         { if($this->out_file_exists($layout["content"])) require $this->out_file($layout["content"]);
142         }
143       }
144     }
145
146     function init_layout()
147     { $this->layout = array();
148       $this->_init_layout("index");
149       if(($mod = $this->etat("mod")) != "index") $this->_init_layout($mod);
150       return $this->get_layout();
151     }
152
153     function _init_layout($mod)
154     { if(($plugins = $this->plugins("ASC")) !== false)
155       { $layout_file = false;
156         $tmp_layout_file = $this->path("out").$this->config("out")."layouts/".$mod.".xml";
157         if(file_exists($tmp_layout_file)) $layout_file = $tmp_layout_file;
158         if(!$layout_file)
159         { $tmp_layout_file = $this->path("out").$this->path("dist_out")."layouts/".$mod.".xml";
160           if(file_exists($tmp_layout_file)) $layout_file = $tmp_layout_file;
161         }
162         if($layout_file) $this->load_layout($layout_file);
163         foreach($plugins as $plugin_name => $plugin)
164         { if($plugin["installed"] && $plugin["enabled"])
165           { $layout_file = false;
166             $tmp_layout_file = $this->path("plugins").$plugin_name."/out/".$this->config("out")."layouts/".$mod.".xml";
167             if(file_exists($tmp_layout_file)) $layout_file = $tmp_layout_file;
168             if(!$layout_file)
169             { $tmp_layout_file = $this->path("plugins").$plugin_name."/out/".$this->path("dist_out")."layouts/".$mod.".xml";
170               if(file_exists($tmp_layout_file)) $layout_file = $tmp_layout_file;
171             }
172             if($layout_file) $this->load_layout($layout_file);
173           }
174         }
175       }
176     }
177
178     function load_layout($layout_file)
179     { if(file_exists($layout_file))
180       { $xml_parser = new sxml();
181         $xml_parser->parse(file_get_contents($layout_file));
182         $layout = $xml_parser->data;
183         if(isset($layout["layout"][0]["subs"]))
184         { foreach($layout["layout"][0]["subs"] as $mod => $mod_node)
185           { if(!isset($this->layout[$mod]))
186             { $this->layout[$mod] = array
187               ( "page" => null,
188                 "content" => null,
189                 "controllers" => array()
190               );
191             }
192             if(isset($mod_node[0]["attrs"]["page"])) $this->layout[$mod]["page"] = $mod_node[0]["attrs"]["page"];
193             if(isset($mod_node[0]["attrs"]["content"])) $this->layout[$mod]["content"] = $mod_node[0]["attrs"]["content"];
194             if(isset($mod_node[0]["subs"]))
195             { foreach($mod_node[0]["subs"] as $controller => $controller_node)
196               { if(!isset($this->layout[$mod]["controllers"][$controller]))
197                 { $this->layout[$mod]["controllers"][$controller] = array
198                   ( "page" => null,
199                     "content" => null,
200                     "actions" => array()
201                   );
202                 }
203                 if(isset($controller_node[0]["attrs"]["page"])) $this->layout[$mod]["controllers"][$controller]["page"] = $controller_node[0]["attrs"]["page"];
204                 if(isset($controller_node[0]["attrs"]["content"])) $this->layout[$mod]["controllers"][$controller]["content"] = $controller_node[0]["attrs"]["content"];
205                 if(isset($controller_node[0]["subs"]))
206                 { foreach($controller_node[0]["subs"] as $action => $action_node)
207                   { if(!isset($this->layout[$mod]["controllers"][$controller]["actions"][$action]))
208                     { $this->layout[$mod]["controllers"][$controller]["actions"][$action] = array
209                       ( "page" => null,
210                         "content" => null
211                       );
212                     }
213                     if(isset($action_node[0]["attrs"]["page"])) $this->layout[$mod]["controllers"][$controller]["actions"][$action]["page"] = $action_node[0]["attrs"]["page"];
214                     if(isset($action_node[0]["attrs"]["content"])) $this->layout[$mod]["controllers"][$controller]["actions"][$action]["content"] = $action_node[0]["attrs"]["content"];
215                   }
216                 }
217               }
218             }
219           }
220         }
221       }
222       return false;
223     }
224
225     function get_layout()
226     { $mod = $this->etat("mod");
227       $controller = $this->etat("controller");
228       $action = $this->etat("action");
229       $content = "";
230       if(isset($this->layout[$mod]["controllers"][$controller]["actions"][$action]["content"]))
231       { $content = $this->layout[$mod]["controllers"][$controller]["actions"][$action]["content"];
232       }
233       else
234       { if(isset($this->layout[$mod]["controllers"][$controller]["content"]))
235         { $content = $this->layout[$mod]["controllers"][$controller]["content"];
236         }
237         else
238         { if(isset($this->layout[$mod]["content"]))
239           { $content = $this->layout[$mod]["content"];
240           }
241           else
242           { if(isset($this->layout["index"]["content"]))
243             { $content = $this->layout["index"]["content"];
244             }
245           }
246         }
247       }
248       $page = "";
249       if(isset($this->layout[$mod]["controllers"][$controller]["actions"][$action]["page"]))
250       { $page = $this->layout[$mod]["controllers"][$controller]["actions"][$action]["page"];
251       }
252       else
253       { if(isset($this->layout[$mod]["controllers"][$controller]["page"]))
254         { $page = $this->layout[$mod]["controllers"][$controller]["page"];
255         }
256         else
257         { if(isset($this->layout[$mod]["page"]))
258           { $page = $this->layout[$mod]["page"];
259           }
260           else
261           { if(isset($this->layout["index"]["page"]))
262             { $page = $this->layout["index"]["page"];
263             }
264           }
265         }
266       }
267       return array
268       ( "page" => $page,
269         "content" => $content
270       );
271     }
272
273   }
274
275 ?>