settings.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. $page_title = __("Settings");
  3. Core::addBreadcrumb("/account", __("Account"));
  4. Core::addBreadcrumb("/account/settings", $page_title);
  5. $user = Core::$user;
  6. $submit = http::value("submit");
  7. if($submit) {
  8. try {
  9. Core::validateCSRFToken($submit);
  10. $form_exception = new FormException;
  11. // Password
  12. $password = http::value("password");
  13. $password_confirm = http::value("password_confirm");
  14. if($password != $password_confirm) $form_exception->addError(__("Passwords do not match."));
  15. if($password) {
  16. $user->password = password_hash($password, PASSWORD_DEFAULT, ["cost" => 14]);
  17. }
  18. $user->title = http::value("title", "trim");
  19. $user->first_name = http::value("first_name", "trim");
  20. $user->last_name = http::value("last_name", "trim");
  21. $user->email = http::value("email", "email");
  22. $user->phone = http::value("phone", "trim");
  23. $user->has_driving_licence = (int) http::value("has_driving_licence", "int");
  24. $user->has_car = (int) http::value("has_car", "int");
  25. $user->locale = http::value("locale", "trim");
  26. $user->newsletter_consent = http::value("newsletter_content", "int");
  27. $locales = Core::locales();
  28. if($password && !User::validatePassword($password)) $form_exception->addError(__("Password must be at least 6 characters in length and contain at least one lowercase, uppercase, number and special character."));
  29. if(empty($user->email)) $form_exception->addError(__("Email address is invalid."));
  30. if(empty($user->title)) $form_exception->addError(__("Title is required."));
  31. if(empty($user->first_name)) $form_exception->addError(__("First name is required."));
  32. if(empty($user->last_name)) $form_exception->addError(__("Last name is required."));
  33. if(empty($user->date_of_birth)) $form_exception->addError(__("Date of birth is required."));
  34. if(empty($user->phone)) $form_exception->addError(__("Mobile number is required."));
  35. if(!isset($locales[$user->locale])) $form_exception->addError(__("Invalid language selected."));
  36. if($user->isTester()) {
  37. $user->preferred_payment_method = http::value("preferred_payment_method", "int");
  38. $user->bank_name = http::value("bank_name", "trim");
  39. $user->bank_sort_code = http::value("bank_sort_code", "trim");
  40. $user->bank_account_number = http::value("bank_account_number", "trim");
  41. $user->paypal_email = http::value("paypal_email", "email");
  42. if(!isset(User::PAYMENT_METHODS[$user->preferred_payment_method])) $form_exception->addError(__("Invalid payment method selected."));
  43. }
  44. // Activation
  45. $deactivate = http::value("deactivate");
  46. if($deactivate && $user->level == User::LEVEL_TESTER) {
  47. $user->level = User::LEVEL_DEACTIVATED;
  48. }
  49. $reactivate = http::value("reactivate");
  50. if($reactivate && $user->level == User::LEVEL_DEACTIVATED) {
  51. $user->level = User::LEVEL_TESTER;
  52. }
  53. if($form_exception->hasErrors()) throw $form_exception;
  54. $success = $user->commit();
  55. Core::addAlert("success", __("Settings saved successfully."));
  56. HTTP::redirect("/account/settings");
  57. }
  58. catch(FormException $e) {
  59. $e->generateAlerts();
  60. }
  61. catch(Exception $e) {
  62. Core::addAlert("danger", $e->getMessage());
  63. }
  64. }
  65. View::includeOnce("header", compact("page_title"));
  66. View::include("settings", compact("user"));
  67. View::includeOnce("footer");
  68. ?>