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