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