3 class mw_env_out extends mw_env{
12 // ---------------------------------------------------------------------------------
16 public function set_out($key, $value){
17 $this->out[$key] = $value;
21 public function get_out(){
25 public function out($key){
26 return isset($this->out[$key]) ? $this->out[$key] : null;
29 // ---------------------------------------------------------------------------------
33 public function templates(){
34 if(($out_pathes = $this->out_pathes()) === false) return false;
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();
41 $this->set_config(array("out" => $current_out_path));
45 public function get_template(){
46 if(isset($this->template)) return $this->template;
47 $this->template = $this->_get_template();
48 return $this->template;
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";
59 $template = new $template_class_name(true);
60 $template->set_env($this->root_inst);
64 public function out_pathes(){
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/";
72 && file_exists($out_dir)
75 if(($pathes = $this->_out_pathes($out_dir, $pathes)) === false) break;
78 if($pathes !== false){
79 $pathes = $this->_out_pathes($this->path("mw_dir")."app/out/", $pathes);
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;
97 // ---------------------------------------------------------------------------------
101 public function out_file_exists($file, $PRIORITE = "DESC"){
102 $out_file = $this->_out_file($file, $PRIORITE);
103 return $out_file ? true : false;
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;
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;
116 public function _out_file($file, $PRIORITE = "DESC"){
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;
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;
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;
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;
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;
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;
162 // ---------------------------------------------------------------------------------
166 public function js_files(){
167 if(!isset($this->js_files)) $this->js_files = array();
169 foreach($this->js_files as $url => $enabled){
170 if($enabled) $files[] = $url;
175 public function add_js_file($url){
176 if(!isset($this->js_files)) $this->js_files = array();
177 $this->js_files[$url] = true;
180 public function remove_js_file($url){
181 if(isset($this->js_files) && isset($this->js_files[$url])){
182 unset($this->js_files[$url]);
186 public function css_files(){
187 if(!isset($this->css_files)) $this->css_files = array();
189 foreach($this->css_files as $url => $enabled){
190 if($enabled) $files[] = $url;
195 public function add_css_file($url){
196 if(!isset($this->css_files)) $this->css_files = array();
197 $this->css_files[$url] = true;
200 public function remove_css_file($url){
201 if(isset($this->css_files) && isset($this->css_files[$url])){
202 unset($this->css_files[$url]);
206 // ---------------------------------------------------------------------------------
210 public function set_out_config($out_config){
211 $this->out_config = $out_config;
212 return $this->out_config;
215 public function get_out_config(){
216 return isset($this->out_config) ? $this->out_config : array();
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"] : "");
227 // ---------------------------------------------------------------------------------
231 public function layout(){
232 return $this->layout;
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();
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;
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;
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;
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;
261 if($layout_file) $this->load_layout($layout_file);
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(
278 "controllers" => array()
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(
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(
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"];
314 public function get_layout(){
315 $mod = $this->etat("mod");
316 $controller = $this->etat("controller");
317 $action = $this->etat("action");
319 if(isset($this->layout[$mod]["controllers"][$controller]["actions"][$action]["content"])){
320 $content = $this->layout[$mod]["controllers"][$controller]["actions"][$action]["content"];
323 if(isset($this->layout[$mod]["controllers"][$controller]["content"])){
324 $content = $this->layout[$mod]["controllers"][$controller]["content"];
327 if(isset($this->layout[$mod]["content"])){
328 $content = $this->layout[$mod]["content"];
331 if(isset($this->layout["index"]["content"])){
332 $content = $this->layout["index"]["content"];
338 if(isset($this->layout[$mod]["controllers"][$controller]["actions"][$action]["page"])){
339 $page = $this->layout[$mod]["controllers"][$controller]["actions"][$action]["page"];
342 if(isset($this->layout[$mod]["controllers"][$controller]["page"])){
343 $page = $this->layout[$mod]["controllers"][$controller]["page"];
346 if(isset($this->layout[$mod]["page"])){
347 $page = $this->layout[$mod]["page"];
350 if(isset($this->layout["index"]["page"])){
351 $page = $this->layout["index"]["page"];
358 "content" => $content
364 // -------------------------------------------------------------------------------------------
368 class mw_template extends empty_class{
372 public $template_infos;
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());
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();
387 ($init_script = $this->init_script())
388 && $this->out_file_exists($init_script)
389 ) require $this->out_file($init_script);
390 $data = $this->data();
392 if($this->out_file_exists($layout["page"])) require $this->out_file($layout["page"]);
394 elseif($layout["content"]){
395 if($this->out_file_exists($layout["content"])) require $this->out_file($layout["content"]);
399 public function get_template_info($key){
400 return isset($this->template_infos[$key]) ? $this->template_infos[$key] : "";
403 public function init(){
407 public function init_script(){
411 public function get_out_config(){
412 $out_config = array();
414 $env = $this->modules["env"];
415 $out_config = $env->get_out_config();
420 public function set_template_infos($template_infos){
421 $this->template_infos = $template_infos;
424 public function get_template_infos(){