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