998f48993a6e69475b272df47fe96da48747ce94
[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     var $js_files;
10     var $css_files;
11
12     var $template;
13
14     // ---------------------------------------------------------------------------------
15     //                                                                          out vars
16     //
17
18     function set_out($key, $value){
19       $this->out[$key] = $value;
20       return $value;
21     }
22
23     function get_out(){
24       return $this->out;
25     }
26
27     function out($key){
28       return isset($this->out[$key]) ? $this->out[$key] : null;
29     }
30
31     // ---------------------------------------------------------------------------------
32     //                                                                         templates
33     //
34
35     function templates(){
36       if(($out_pathes = $this->out_pathes()) === false) return false;
37       $templates = array();
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();
42       }
43       $this->set_config(array("out" => $current_out_path));
44       return $templates;
45     }
46
47     function get_template(){
48       if(isset($this->template)) return $this->template;
49       $this->template = $this->_get_template();
50       return $this->template;
51     }
52
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";
60       }
61       $template = new $template_class_name(true);
62       $template->set_env($this->root_inst);
63       return $template;
64     }
65
66     function out_pathes(){
67       $pathes = array();
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/";
71           if(
72                $plugin["installed"]
73             && $plugin["enabled"]
74             && file_exists($out_dir)
75             && is_dir($out_dir)
76           ){
77             if(($pathes = $this->_out_pathes($out_dir, $pathes)) === false) break;
78           }
79         }
80         if($pathes !== false){
81           $pathes = $this->_out_pathes($this->path("mw_dir")."app/out/", $pathes);
82         }
83       }
84       else $pathes = false;
85       return $pathes;
86     }
87
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;
92         }
93         closedir($dh);
94       }
95       else $pathes = false;
96       return $pathes;
97     }
98
99     // ---------------------------------------------------------------------------------
100     //                                                                         out files
101     //
102
103     function out_file_exists($file, $PRIORITE = "DESC"){
104       $out_file = $this->_out_file($file, $PRIORITE);
105       return $out_file ? true : false;
106     }
107
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;
111     }
112
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;
116     }
117
118     function _out_file($file, $PRIORITE = "DESC"){
119       $out_file = false;
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;
124         }
125         if(!$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;
129           }
130         }
131       }
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;
138             break;
139           }
140           if(!$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;
144               break;
145             }
146           }
147         }
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;
152           }
153           if(!$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;
157             }
158           }
159         }
160       }
161       return $out_file;
162     }
163
164     // ---------------------------------------------------------------------------------
165     //                                                                    js / css files
166     //
167
168     function js_files(){
169       if(!isset($this->js_files)) $this->js_files = array();
170       $files = array();
171       foreach($this->js_files as $url => $enabled){
172         if($enabled) $files[] = $url;
173       }
174       return $files;
175     }
176
177     function add_js_file($url){
178       if(!isset($this->js_files)) $this->js_files = array();
179       $this->js_files[$url] = true;
180     }
181
182     function remove_js_file($url){
183       if(isset($this->js_files) && isset($this->js_files[$url])){
184         unset($this->js_files[$url]);
185       }
186     }
187
188     function css_files(){
189       if(!isset($this->css_files)) $this->css_files = array();
190       $files = array();
191       foreach($this->css_files as $url => $enabled){
192         if($enabled) $files[] = $url;
193       }
194       return $files;
195     }
196
197     function add_css_file($url){
198       if(!isset($this->css_files)) $this->css_files = array();
199       $this->css_files[$url] = true;
200     }
201
202     function remove_css_file($url){
203       if(isset($this->css_files) && isset($this->css_files[$url])){
204         unset($this->css_files[$url]);
205       }
206     }
207
208     // ---------------------------------------------------------------------------------
209     //                                                                        out config
210     //
211
212     function set_out_config($out_config){
213       $this->out_config = $out_config;
214       return $this->out_config;
215     }
216
217     function get_out_config(){
218       return isset($this->out_config) ? $this->out_config : array();
219     }
220
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"] : "");
225       }
226       return null;
227     }
228
229     // ---------------------------------------------------------------------------------
230     //                                                                           layouts
231     //
232
233     function layout(){
234       return $this->layout;
235     }
236
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();
242     }
243
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;
249         if(!$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;
252         }
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;
259             if(!$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;
262             }
263             if($layout_file) $this->load_layout($layout_file);
264           }
265         }
266       }
267     }
268
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(
278                 "page" => null,
279                 "content" => null,
280                 "controllers" => array()
281               );
282             }
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(
289                     "page" => null,
290                     "content" => null,
291                     "actions" => array()
292                   );
293                 }
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(
300                         "page" => null,
301                         "content" => null
302                       );
303                     }
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"];
306                   }
307                 }
308               }
309             }
310           }
311         }
312       }
313       return false;
314     }
315
316     function get_layout(){
317       $mod = $this->etat("mod");
318       $controller = $this->etat("controller");
319       $action = $this->etat("action");
320       $content = "";
321       if(isset($this->layout[$mod]["controllers"][$controller]["actions"][$action]["content"])){
322         $content = $this->layout[$mod]["controllers"][$controller]["actions"][$action]["content"];
323       }
324       else{
325         if(isset($this->layout[$mod]["controllers"][$controller]["content"])){
326           $content = $this->layout[$mod]["controllers"][$controller]["content"];
327         }
328         else{
329           if(isset($this->layout[$mod]["content"])){
330             $content = $this->layout[$mod]["content"];
331           }
332           else{
333             if(isset($this->layout["index"]["content"])){
334               $content = $this->layout["index"]["content"];
335             }
336           }
337         }
338       }
339       $page = "";
340       if(isset($this->layout[$mod]["controllers"][$controller]["actions"][$action]["page"])){
341         $page = $this->layout[$mod]["controllers"][$controller]["actions"][$action]["page"];
342       }
343       else{
344         if(isset($this->layout[$mod]["controllers"][$controller]["page"])){
345           $page = $this->layout[$mod]["controllers"][$controller]["page"];
346         }
347         else{
348           if(isset($this->layout[$mod]["page"])){
349             $page = $this->layout[$mod]["page"];
350           }
351           else{
352             if(isset($this->layout["index"]["page"])){
353               $page = $this->layout["index"]["page"];
354             }
355           }
356         }
357       }
358       return array(
359         "page" => $page,
360         "content" => $content
361       );
362     }
363
364   }
365
366   class mw_template extends empty_class{
367
368     var $out;
369     var $ENV_SET;
370     var $template_infos;
371
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());
377     }
378
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();
383       $this->init();
384       if(
385             ($init_script = $this->init_script())
386         &&  $this->out_file_exists($init_script)
387       ) require $this->out_file($init_script);
388       $data = $this->data();
389       if($layout["page"]){
390         if($this->out_file_exists($layout["page"])) require $this->out_file($layout["page"]);
391       }
392       elseif($layout["content"]){
393         if($this->out_file_exists($layout["content"])) require $this->out_file($layout["content"]);
394       }
395     }
396
397     function get_template_info($key){
398       return isset($this->template_infos[$key]) ? $this->template_infos[$key] : "";
399     }
400
401     function init(){
402       return true;
403     }
404
405     function init_script(){
406       return false;
407     }
408
409     function get_out_config(){
410       $out_config = array();
411       if($this->ENV_SET){
412         $env = $this->modules["env"];
413         $out_config = $env->get_out_config();
414       }
415       return $out_config;
416     }
417
418     function set_template_infos($template_infos){
419       $this->template_infos = $template_infos;
420     }
421
422     function get_template_infos(){
423       return array();
424     }
425
426   }
427
428 ?>