Member.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. class Member extends Controller {
  3. public function Index(IUserSettingsRepository $userSettingsRepo, $permission_error, $submit_form, $email, $password, $confirm_password) {
  4. if (Session::IsUserLoggedIn())
  5. return $this->Manage($userSettingsRepo);
  6. else
  7. return $this->Login($permission_error, $submit_form, $email, $password);
  8. }
  9. public function Login($permission_error, $submit_form, $email, $password) {
  10. if (Session::IsUserLoggedIn()) {
  11. header("location:/member/manage");
  12. return;
  13. }
  14. if (!isset($email))
  15. $email="";
  16. $errors=array();
  17. if (isset($permission_error)&&$permission_error==true)
  18. $errors[]="You don't have permission to access this page.";
  19. if (isset($submit_form)&&$email!="") {
  20. $user=new User($email);
  21. if ($user->UserId!=null&&$user->ValidatePassword($password)) {
  22. Session::SetLoggedInUser($user);
  23. header("location:/member/manage");
  24. return;
  25. }
  26. $errors[]="Unable to log in. Please check your login details and try again.";
  27. }
  28. return new View("Member/login.view", array(
  29. "errors"=>$errors,
  30. "loginEmail"=>$email
  31. ));
  32. }
  33. public function Logout() {
  34. Session::Destroy();
  35. header("location:/member");
  36. }
  37. public function Register($email, $password) {
  38. if (!isset($email, $password))
  39. return $this->Login(null, null, $email, $password);
  40. $errors=array();
  41. if ($email=="")
  42. $errors[]="Email blank";
  43. if ($password=="")
  44. $errors[]="Password blank";
  45. if (!Utils::IsValidEmail($email))
  46. $errors[]="Invalid email address";
  47. $user=new User($email);
  48. if ($user->UserId!=0)
  49. $errors[]="Email already in use";
  50. if (count($errors)>0)
  51. return new View("Member/login.view", array(
  52. "errors"=>$errors,
  53. "registerEmail"=>$email
  54. ));
  55. $user=new User();
  56. $user->UserEmail=$email;
  57. $user->UserPassword=$password;
  58. $user->UserCreated=time();
  59. $user->Save();
  60. Session::SetLoggedInUser($user);
  61. header("location:/member/manage");
  62. }
  63. public function Manage(IUserSettingsRepository $userSettingsRepo, $errors=array()) {
  64. if (!Session::IsUserLoggedIn()) {
  65. header("location:/member/");
  66. return;
  67. }
  68. $user=Session::GetLoggedInUser();
  69. $settingsToLoad=array(
  70. "height",
  71. "weight_graph_duration"
  72. );
  73. $settings=array();
  74. foreach ($settingsToLoad as $key){
  75. $settings[$key]=$userSettingsRepo->GetSetting($user, $key)->Value;
  76. }
  77. $user=Session::GetLoggedInUser();
  78. return new View("Member/manage.view", array("user"=>$user, "errors"=>$errors, "settings"=>$settings));
  79. }
  80. public function SavePassword($submit_form, $new_password, $confirm_password) {
  81. if (!Session::IsUserLoggedIn()) {
  82. header("location:/member/");
  83. return;
  84. }
  85. $user=Session::GetLoggedInUser();
  86. $errors=array();
  87. if (isset($submit_form) && $new_password!="") {
  88. if ($new_password==$confirm_password) {
  89. $user->UserPassword=$new_password;
  90. $user->Save();
  91. header("location:/member/manage");
  92. return;
  93. } else
  94. $errors[]="Passwords did not match";
  95. }
  96. if (count($errors)==0){
  97. header("location:/member/");
  98. return;
  99. }
  100. return $this->Manage($errors);
  101. }
  102. private function SaveSetting(IUserSettingsRepository $userSettingsRepo, User $user, $key, $value){
  103. $setting=$userSettingsRepo->GetSetting($user, $key);
  104. $setting->Value=$value;
  105. $setting->Save();
  106. }
  107. public function SaveSettings($submit_form, $height, $weight_graph_duration, IUserSettingsRepository $userSettingsRepo) {
  108. if (!isset($submit_form) || !Session::IsUserLoggedIn()) {
  109. header("location:/member/");
  110. return;
  111. }
  112. $user=Session::GetLoggedInUser();
  113. $errors=array();
  114. $this->SaveSetting($userSettingsRepo, $user, "height", (int)$height);
  115. $this->SaveSetting($userSettingsRepo, $user, "weight_graph_duration", (int)$weight_graph_duration);
  116. if (count($errors)==0){
  117. header("location:/member/");
  118. return;
  119. }
  120. return $this->Manage($errors);
  121. }
  122. }