3 class mw_env_run extends mw_env{
10 return $data->get_session_user();
13 function set_etat($etat, $valid_role = true){
14 if(($this->etat = $this->valid_etat($etat)) !== false){
15 if(!$valid_role || $this->action_allowed($this->etat, false)){
20 $this->erreur("Vous n'avez pas le role requis pour effectuer cette action");
21 $this->call_observers("action_permission_denied", array("etat" => $etat));
24 else $this->erreur("etat invalide");
28 function valid_etat($etat){
35 $_etat["mod"] = isset($etat["mod"]) ? $etat["mod"] : "";
36 $_etat["controller"] = isset($etat["controller"]) ? $etat["controller"] : "";
37 $_etat["action"] = isset($etat["action"]) ? $etat["action"] : "";
40 $etat = explode("/", $etat);
41 foreach($etat as $etat_item){
43 if(!$_etat["mod"]) $_etat["mod"] = $etat_item;
45 if(!$_etat["controller"]) $_etat["controller"] = $etat_item;
47 if(!$_etat["action"]) $_etat["action"] = $etat_item;
55 $_etat["mod"] = "index";
56 $_etat["controller"] = "index";
57 $_etat["action"] = "index";
60 if(!$_etat["controller"]){
61 $_etat["controller"] = "index";
62 $_etat["action"] = "index";
65 if(!$_etat["action"]) $_etat["action"] = "index";
71 && isset($_etat["mod"]) && preg_match("/^[a-zA-Z0-9_]+$/", $_etat["mod"])
72 && isset($_etat["controller"]) && preg_match("/^[a-zA-Z0-9_]+$/", $_etat["controller"])
73 && isset($_etat["action"]) && preg_match("/^[a-zA-Z0-9_]+$/", $_etat["action"])
80 function etat_is_valid(){
81 return $this->valid_etat($this->etat);
84 function action_allowed($etat, $CHECK_FORMAT = true){
85 $data = $this->data();
86 if($CHECK_FORMAT) $etat = $this->valid_etat($etat);
88 $this->erreur("etat invalide");
91 if(($user_roles = $data->get_user_roles()) === false){
92 $this->erreur("Impossible de lire les roles de l'utilisateur courant");
96 $this->erreur("L'utilisateur courant n'a aucun role");
101 $action_roles = $data->get_action_roles(
108 $this->erreur("Impossible de lire les roles des actions en base");
111 foreach($user_roles as $id_role){
112 $OK = $this->config("default_allow");
113 $action = $etat["mod"];
114 if(isset($action_roles[$action])){
116 (isset($action_roles[$action][0]) && $action_roles[$action][0])
117 || (isset($action_roles[$action][$id_role]) && $action_roles[$action][$id_role]);
120 $action = $etat["mod"]."/".$etat["controller"];
121 if(isset($action_roles[$action])){
123 (isset($action_roles[$action][0]) && $action_roles[$action][0])
124 || (isset($action_roles[$action][$id_role]) && $action_roles[$action][$id_role]);
128 $action = $etat["mod"]."/".$etat["controller"]."/".$etat["action"];
129 if(isset($action_roles[$action])){
131 (isset($action_roles[$action][0]) && $action_roles[$action][0])
132 || (isset($action_roles[$action][$id_role]) && $action_roles[$action][$id_role]);
140 function run($etat, $valid_role = true, $params = array(), $method = "GET"){
141 if($this->set_etat($etat, $valid_role)){
142 if($controller = $this->get_controller($this->etat("mod")."/".$this->etat("controller"))){
143 $action_method = $this->etat("action");
144 if(method_exists($controller, $action_method)){
145 foreach($params as $key => $value){
146 switch(strtolower($method)){
147 case "get": $_GET[$this->param($key)] = $value; break;
148 case "post": $_POST[$key] = $value; break;
152 if(($controller_validate = $controller->validate()) === true){
153 if(($controller_prepare_inputs = $controller->prepare_inputs()) === true){
154 $etat_before = $this->etat;
155 $this->call_observers("before_action");
156 $controller->$action_method($this);
157 $etat_after = $this->etat;
158 $this->etat = $etat_before;
159 $this->call_observers("after_action");
160 $this->etat = $etat_after;
162 else $this->erreur($controller_prepare_inputs);
164 else $this->erreur($controller_validate);
166 else $this->erreur("Impossible de trouver l'action ".$this->etat("action"));
168 else $this->erreur("Impossible d'instancier le controleur ".$this->etat("controller"));
172 function is_running($etat){
175 if(isset($etat["mod"])){
176 $_etat["mod"] = $etat["mod"];
177 if(isset($etat["controller"])){
178 $_etat["controller"] = $etat["controller"];
179 if(isset($etat["action"])) $_etat["action"] = $etat["action"];
184 $etat = explode("/", $etat);
185 foreach($etat as $etat_item){
187 if(!isset($_etat["mod"])) $_etat["mod"] = $etat_item;
189 if(!isset($_etat["controller"])) $_etat["controller"] = $etat_item;
191 if(!isset($_etat["action"])) $_etat["action"] = $etat_item;
199 if($IS_RUNNING && isset($_etat["mod"])) $IS_RUNNING = ($_etat["mod"] == $this->etat("mod"));
200 if($IS_RUNNING && isset($_etat["controller"])) $IS_RUNNING = ($_etat["controller"] == $this->etat("controller"));
201 if($IS_RUNNING && isset($_etat["action"])) $IS_RUNNING = ($_etat["action"] == $this->etat("action"));
205 function etat($name = null){
206 if(!isset($name)) return $this->etat;
207 return $this->etat[$name];
210 function get_controller($controller_path){
211 if($etat = $this->valid_etat($controller_path)){
212 if(!isset($this->controllers)) $this->controllers = array();
213 if(!isset($this->controllers[$etat["mod"]])) $this->controllers[$etat["mod"]] = array();
214 if(!isset($this->controllers[$etat["mod"]][$etat["controller"]])){
215 $controller_class = "mw_".$etat["mod"]."_".$etat["controller"];
216 if(!class_exists($controller_class)){
217 $controller_file = "controllers/".$etat["mod"]."/".$etat["controller"].".php";
218 if($this->app_file_exists($controller_file, "DESC")){
219 require_once $this->app_file($controller_file, "DESC");
221 if(!class_exists($controller_class)) return false;
223 $this->controllers[$etat["mod"]][$etat["controller"]] = new $controller_class();
224 $this->controllers[$etat["mod"]][$etat["controller"]]->set_env($this);
226 return $this->controllers[$etat["mod"]][$etat["controller"]];
237 function set_env(&$env){
249 function prepare_inputs(){