123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- class Member extends Controller {
- public function Index(IUserSettingsRepository $userSettingsRepo, $permission_error, $submit_form, $email, $password, $confirm_password) {
- if (Session::IsUserLoggedIn())
- return $this->Manage($userSettingsRepo);
- else
- return $this->Login($permission_error, $submit_form, $email, $password);
- }
- public function Login($permission_error, $submit_form, $email, $password) {
- if (Session::IsUserLoggedIn()) {
- header("location:/member/manage");
- return;
- }
- if (!isset($email))
- $email="";
- $errors=array();
- if (isset($permission_error)&&$permission_error==true)
- $errors[]="You don't have permission to access this page.";
- if (isset($submit_form)&&$email!="") {
- $user=new User($email);
- if ($user->UserId!=null&&$user->ValidatePassword($password)) {
- Session::SetLoggedInUser($user);
- header("location:/member/manage");
- return;
- }
- $errors[]="Unable to log in. Please check your login details and try again.";
- }
- return new View("Member/login.view", array(
- "errors"=>$errors,
- "loginEmail"=>$email
- ));
- }
- public function Logout() {
- Session::Destroy();
- header("location:/member");
- }
- public function Register($email, $password) {
- if (!isset($email, $password))
- return $this->Login(null, null, $email, $password);
- $errors=array();
- if ($email=="")
- $errors[]="Email blank";
- if ($password=="")
- $errors[]="Password blank";
- if (!Utils::IsValidEmail($email))
- $errors[]="Invalid email address";
- $user=new User($email);
- if ($user->UserId!=0)
- $errors[]="Email already in use";
- if (count($errors)>0)
- return new View("Member/login.view", array(
- "errors"=>$errors,
- "registerEmail"=>$email
- ));
- $user=new User();
- $user->UserEmail=$email;
- $user->UserPassword=$password;
- $user->UserCreated=time();
- $user->Save();
- Session::SetLoggedInUser($user);
- header("location:/member/manage");
- }
- public function Manage(IUserSettingsRepository $userSettingsRepo, $errors=array()) {
- if (!Session::IsUserLoggedIn()) {
- header("location:/member/");
- return;
- }
- $user=Session::GetLoggedInUser();
- $settingsToLoad=array(
- "height",
- "weight_graph_duration"
- );
-
- $settings=array();
- foreach ($settingsToLoad as $key){
- $settings[$key]=$userSettingsRepo->GetSetting($user, $key)->Value;
- }
-
- $user=Session::GetLoggedInUser();
- return new View("Member/manage.view", array("user"=>$user, "errors"=>$errors, "settings"=>$settings));
- }
- public function SavePassword($submit_form, $new_password, $confirm_password) {
- if (!Session::IsUserLoggedIn()) {
- header("location:/member/");
- return;
- }
- $user=Session::GetLoggedInUser();
- $errors=array();
- if (isset($submit_form) && $new_password!="") {
- if ($new_password==$confirm_password) {
- $user->UserPassword=$new_password;
- $user->Save();
- header("location:/member/manage");
- return;
- } else
- $errors[]="Passwords did not match";
- }
- if (count($errors)==0){
- header("location:/member/");
- return;
- }
-
- return $this->Manage($errors);
- }
- private function SaveSetting(IUserSettingsRepository $userSettingsRepo, User $user, $key, $value){
- $setting=$userSettingsRepo->GetSetting($user, $key);
- $setting->Value=$value;
- $setting->Save();
- }
-
- public function SaveSettings($submit_form, $height, $weight_graph_duration, IUserSettingsRepository $userSettingsRepo) {
- if (!isset($submit_form) || !Session::IsUserLoggedIn()) {
- header("location:/member/");
- return;
- }
- $user=Session::GetLoggedInUser();
- $errors=array();
-
- $this->SaveSetting($userSettingsRepo, $user, "height", (int)$height);
- $this->SaveSetting($userSettingsRepo, $user, "weight_graph_duration", (int)$weight_graph_duration);
- if (count($errors)==0){
- header("location:/member/");
- return;
- }
- return $this->Manage($errors);
- }
- }
|