adaptations pour pludieurs instances d'applications
[mtweb] / mw / env / modules / mw_env_run.php
index 51b6330..c0a97a2 100644 (file)
@@ -3,28 +3,34 @@
   class mw_env_run extends mw_env{
 
     var $etat;
+    var $controllers;
 
     function user(){
       $data = $this->data();
       return $data->get_session_user();
     }
 
-    function set_etat($etat, $valid_status = true){
+    function set_etat($etat, $valid_role = true){
       if(($this->etat = $this->valid_etat($etat)) !== false){
-        if(!$valid_status || $this->status_ok($this->etat, false)){
+        if(!$valid_role || $this->action_allowed($this->etat, false)){
           return $this->etat;
         }
-        else $this->erreur("Vous n'avez pas le statut requis pour effectuer cette action");
+        else{
+          $etat = $this->etat;
+          $this->erreur("Vous n'avez pas le role requis pour effectuer cette action");
+          $this->call_observers("action_permission_denied", array("etat" => $etat));
+        }
       }
       else $this->erreur("etat invalide");
       return false;
     }
 
     function valid_etat($etat){
-      $_etat = array();
-      $_etat["mod"] = "";
-      $_etat["controller"] = "";
-      $_etat["action"] = "";
+      $_etat = array(
+        "mod" => "",
+        "controller" => "",
+        "action" => ""
+      );
       if(is_array($etat)){
         $_etat["mod"] = isset($etat["mod"]) ? $etat["mod"] : "";
         $_etat["controller"] = isset($etat["controller"]) ? $etat["controller"] : "";
       return $this->valid_etat($this->etat);
     }
 
-    function status_ok($etat, $CHECK_FORMAT = true){
-      $OK = $this->config("default_allow");
+    function action_allowed($etat, $CHECK_FORMAT = true){
       $data = $this->data();
       if($CHECK_FORMAT) $etat = $this->valid_etat($etat);
-      if($etat !== false){
-        if(($user_status = $data->get_user_status()) !== false){
-          if(
-            (
-              $action_status = $data->get_action_status(
-                $etat["mod"],
-                $etat["controller"],
-                $etat["action"]
-              )
-            ) !== false
-          ){
-            $action = $etat["mod"]."/".$etat["controller"]."/".$etat["action"];
-            if(isset($action_status[$action])){
-              $OK =
-                   (isset($action_status[$action][0]) && $action_status[$action][0])
-                || (isset($action_status[$action][$user_status]) && $action_status[$action][$user_status]);
-            }
-            else{
-              $action = $etat["mod"]."/".$etat["controller"];
-              if(isset($action_status[$action])){
-                $OK =
-                     (isset($action_status[$action][0]) && $action_status[$action][0])
-                  || (isset($action_status[$action][$user_status]) && $action_status[$action][$user_status]);
-              }
-              else{
-                $action = $etat["mod"];
-                if(isset($action_status[$action])){
-                  $OK =
-                       (isset($action_status[$action][0]) && $action_status[$action][0])
-                    || (isset($action_status[$action][$user_status]) && $action_status[$action][$user_status]);
-                }
-              }
-            }
+      if($etat === false){
+        $this->erreur("etat invalide");
+        return false;
+      }
+      if(($user_roles = $data->get_user_roles()) === false){
+        $this->erreur("Impossible de lire les roles de l'utilisateur courant");
+        return false;
+      }
+      if(!$user_roles){
+        $this->erreur("L'utilisateur courant n'a aucun role");
+        return false;
+      }
+      if(
+        (
+          $action_roles = $data->get_action_roles(
+            $etat["mod"],
+            $etat["controller"],
+            $etat["action"]
+          )
+        ) === false
+      ){
+        $this->erreur("Impossible de lire les roles des actions en base");
+        return false;
+      }
+      foreach($user_roles as $id_role){
+        $OK = $this->config("default_allow");
+        $action = $etat["mod"];
+        if(isset($action_roles[$action])){
+          $OK =
+               (isset($action_roles[$action][0]) && $action_roles[$action][0])
+            || (isset($action_roles[$action][$id_role]) && $action_roles[$action][$id_role]);
+        }
+        if(!$OK){
+          $action = $etat["mod"]."/".$etat["controller"];
+          if(isset($action_roles[$action])){
+            $OK =
+                 (isset($action_roles[$action][0]) && $action_roles[$action][0])
+              || (isset($action_roles[$action][$id_role]) && $action_roles[$action][$id_role]);
           }
-          else $this->erreur("Impossible de lire les status des actions en base");
         }
-        else $this->erreur("Impossible de lire le statut de l'utilisateur courant");
+        if(!$OK){
+          $action = $etat["mod"]."/".$etat["controller"]."/".$etat["action"];
+          if(isset($action_roles[$action])){
+            $OK =
+                 (isset($action_roles[$action][0]) && $action_roles[$action][0])
+              || (isset($action_roles[$action][$id_role]) && $action_roles[$action][$id_role]);
+          }
+        }
+        if($OK) break;
       }
-      else $this->erreur("etat invalide");
       return $OK;
     }
 
-    function run($etat, $valid_status = true, $params = array(), $method = "GET"){
-      if($this->set_etat($etat, $valid_status)){
-        $controller_file = "mods/".$this->etat("mod")."/".$this->etat("controller").".php";
-        if($this->app_file_exists($controller_file = "mods/".$this->etat("mod")."/".$this->etat("controller").".php", "DESC")){
-          if(!class_exists("mw_mod")) require $this->app_file("mods/mw_mod.php");
-          if(!class_exists($controller_class = "mw_".$this->etat("mod")."_".$this->etat("controller"))){
-            require $this->app_file($controller_file, "DESC");
-          }
-          if(class_exists($controller_class)){
-            $controller = new $controller_class();
-            $action_method = $this->etat("action");
-            if(method_exists($controller, $action_method)){
-              foreach($params as $key => $value){
-                switch(strtolower($method)){
-                  case "get": $_GET[$this->param($key)] = $value; break;
-                  case "post": $_POST[$key] = $value; break;
-                  default: break;
+    function run($etat, $params = array(), $valid_role = true){
+      if($this->set_etat($etat, $valid_role)){
+        if($controller = $this->get_controller($this->etat("mod")."/".$this->etat("controller"))){
+          $action_method = $this->etat("action");
+          if(method_exists($controller, $action_method)){
+            foreach($params as $params_method => $values){
+              foreach($values as $key => $value){
+                switch(strtolower($params_method)){
+                  case "get":
+                    $_GET[$this->param($key)] = $value;
+                    break;
+                  case "post":
+                    $_POST[$key] = $value;
+                    break;
                 }
               }
-              if(($controller_validate = $controller->validate($this)) === true){
-                if(($controller_prepare_inputs = $controller->prepare_inputs($this)) === true){
-                  $controller->$action_method($this);
-                }
-                else $this->erreur($controller_prepare_inputs);
+            }
+            if(($controller_validate = $controller->validate()) === true){
+              if(($controller_prepare_inputs = $controller->prepare_inputs()) === true){
+                $etat_before = $this->etat;
+                $this->call_observers("before_action");
+                $controller->$action_method($this);
+                $etat_after = $this->etat;
+                $this->etat = $etat_before;
+                $this->call_observers("after_action");
+                $this->etat = $etat_after;
               }
-              else $this->erreur($controller_validate);
+              else $this->erreur($controller_prepare_inputs);
             }
-            else $this->erreur("Impossible de trouver l'action ".$this->etat("action"));
+            else $this->erreur($controller_validate);
           }
-          else $this->erreur("Impossible d'instancier le controleur ".$this->etat("controller"));
+          else $this->erreur("Impossible de trouver l'action ".$this->etat("action"));
         }
-        else $this->erreur("Impossible de trouver le controleur ".$this->etat("controller")." pour le module ".$this->etat("mod"));
+        else $this->erreur("Impossible d'instancier le controleur ".$this->etat("controller"));
       }
-      else $this->erreur("Impossible d'effectuer cette action");
     }
 
-    function etat($name){
-      return $this->etat[$name];
+    function is_running($etat){
+      $_etat = array();
+      if(is_array($etat)){
+        if(isset($etat["mod"])){
+          $_etat["mod"] = $etat["mod"];
+          if(isset($etat["controller"])){
+            $_etat["controller"] = $etat["controller"];
+            if(isset($etat["action"])) $_etat["action"] = $etat["action"];
+          }
+        }
+      }
+      else{
+        $etat = explode("/", $etat);
+        foreach($etat as $etat_item){
+          if($etat_item){
+            if(!isset($_etat["mod"])) $_etat["mod"] = $etat_item;
+            else{
+              if(!isset($_etat["controller"])) $_etat["controller"] = $etat_item;
+              else{
+                if(!isset($_etat["action"])) $_etat["action"] = $etat_item;
+                break;
+              }
+            }
+          }
+        }
+      }
+      $IS_RUNNING = true;
+      if($IS_RUNNING && isset($_etat["mod"])) $IS_RUNNING = ($_etat["mod"] == $this->etat("mod"));
+      if($IS_RUNNING && isset($_etat["controller"])) $IS_RUNNING = ($_etat["controller"] == $this->etat("controller"));
+      if($IS_RUNNING && isset($_etat["action"])) $IS_RUNNING = ($_etat["action"] == $this->etat("action"));
+      return $IS_RUNNING;
     }
 
-    function check_stop(){
-      return $this->etat("mod") == "reponses";
+    function etat($name = null){
+      if(!isset($name)) return $this->etat;
+      return $this->etat[$name];
     }
 
-    function get_mod($mod_name){
-      if($etat = $this->valid_etat($mod_name)){
-        if($this->app_file_exists($controller_file = "mods/".$etat["mod"]."/".$etat["controller"].".php")){
-          if(!class_exists("mw_mod")) require $this->app_file("mods/mw_mod.php");
-          if(!class_exists($controller_class = "mw_".$etat["mod"]."_".$etat["controller"])){
-            require $this->app_file($controller_file);
-          }
-          if(class_exists($controller_class)){
-            return new $controller_class();
+    function get_controller($controller_path){
+      if($etat = $this->valid_etat($controller_path)){
+        if(!isset($this->controllers)) $this->controllers = array();
+        if(!isset($this->controllers[$etat["mod"]])) $this->controllers[$etat["mod"]] = array();
+        if(!isset($this->controllers[$etat["mod"]][$etat["controller"]])){
+          $controller_class = "mw_".$etat["mod"]."_".$etat["controller"];
+          if(!class_exists($controller_class)){
+            $controller_file = "controllers/".$etat["mod"]."/".$etat["controller"].".php";
+            if($this->app_file_exists($controller_file, "DESC")){
+              require_once $this->app_file($controller_file, "DESC");
+            }
+            if(!class_exists($controller_class)) return false;
           }
+          $this->controllers[$etat["mod"]][$etat["controller"]] = new $controller_class();
+          $this->controllers[$etat["mod"]][$etat["controller"]]->set_env($this);
         }
+        return $this->controllers[$etat["mod"]][$etat["controller"]];
       }
       return false;
     }
 
   }
 
+  class mw_controller{
+
+    var $env;
+
+    function set_env(&$env){
+      $this->env = &$env;
+    }
+
+    function env(){
+      return $this->env;
+    }
+
+    function validate(){
+      return true;
+    }
+
+    function prepare_inputs(){
+      return true;
+    }
+
+  }
+
 ?>
\ No newline at end of file