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