nouveau module "models" dans l'environnement
[mtweb] / mw / app / data / models / users.php
diff --git a/mw/app/data/models/users.php b/mw/app/data/models/users.php
new file mode 100644 (file)
index 0000000..7a310db
--- /dev/null
@@ -0,0 +1,115 @@
+<?php
+
+  class mw_model_users extends mw_model{
+
+    var $roles;
+
+    // ----------------------------------------------------------------------------
+    //                                                                   accesseurs
+
+    function set_roles($roles){
+      $this->roles = $roles;
+    }
+
+    function get_roles(){
+      return $this->roles;
+    }
+
+    function clear_roles(){
+      $this->roles = null;
+    }
+
+    function get_values(){
+      $values = array();
+      if(isset($this->roles)) $values["roles"] = $this->roles;
+      foreach($this->attributs as $attribut_name => $attribut){
+        $values[$attribut_name] = isset($attribut["value"]) ? $attribut["value"] : null;
+      }
+      return $values;
+    }
+
+    function set_password($password){
+      $this->attributs["password"]["value"] = $this->encode_password($password);
+    }
+
+    function encode_password($password){
+      return md5($password);
+    }
+
+    // ----------------------------------------------------------------------------
+    //                                                                   validation
+
+    function valid_email($email){
+      if(!isset($email) || !$email){
+        return "merci de préciser un email";
+      }
+      if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
+        return "le mail ne semble pas être un email valide";
+      }
+      return true;
+    }
+
+    function valid_login($login){
+      if(!isset($login) || !$login){
+        return "merci de préciser un login";
+      }
+      return true;
+    }
+
+    function valid_password($password){
+      if(!isset($password) || ($password == $this->encode_password(""))){
+        return "merci de préciser un mot de passe";
+      }
+      return true;
+    }
+
+    // ----------------------------------------------------------------------------
+    //                                                                         CRUD
+
+    function insert(){
+      $data = $this->env->data();
+      $roles = $this->get_roles();
+      $this->clear_roles();
+      $RETURN_INSERT_ID = $this->RETURN_INSERT_ID;
+      $this->RETURN_INSERT_ID = true;
+      if(!($id_user = parent::insert())) return false;
+      $this->set_id($id_user);
+      $this->set_roles($roles);
+      $this->RETURN_INSERT_ID = $RETURN_INSERT_ID;
+      foreach($this->roles as $id_role){
+        if(!$data->add_user_role($this->get_id(), $id_role)) return false;
+      }
+      return $id_user;
+    }
+
+    function load($index_name, $index_value){
+      $data = $this->env->data();
+      if(parent::load($index_name, $index_value) === false) return false;
+      if($this->get_id()){
+        if(($this->roles = $data->list_user_roles($this->get_id())) === false) return false;
+      }
+      return true;
+    }
+
+    function update($index_name, $index_value = null){
+      $data = $this->env->data();
+      $roles = $this->get_roles();
+      $this->clear_roles();
+      if(!($res = parent::update($index_name, $index_value))) return false;
+      $this->set_roles($roles);
+      if(!$data->clear_user_roles($this->get_id())) return false;
+      foreach($this->roles as $id_role){
+        if(!$data->add_user_role($this->get_id(), $id_role)) return false;
+      }
+      return $res;
+    }
+
+    function delete($index_name, $index_value = null){
+      $data = $this->env->data();
+      if(!$data->clear_user_roles($this->get_id())) return false;
+      return parent::delete($index_name, $index_value);
+    }
+
+  }
+
+?>
\ No newline at end of file