roles = $roles; } public function get_roles(){ return $this->roles; } public function clear_roles(){ $this->roles = null; } public 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; } public function set_password($password){ $this->attributs["password"]["value"] = $this->encode_password($password); } public function encode_password($password){ return md5($password); } // ---------------------------------------------------------------------------- // validation public 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; } public function valid_login($login){ if(!isset($login) || !$login){ return "merci de préciser un login"; } return true; } public function valid_password($password){ if(!isset($password) || ($password == $this->encode_password(""))){ return "merci de préciser un mot de passe"; } return true; } // ---------------------------------------------------------------------------- // CRUD public 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; } public 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){ $this->roles = array(); } } return true; } public 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; } public 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); } }