| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php /** @noinspection PhpUnhandledExceptionInspection */
- /**
- * @var int $id
- * @var FormException $form_exception
- */
- Core::addScript("public/js/app-user-edit.js");
- $user = User::get($id);
- if(!$user) throw new Exception("User not found.");
- if($user->level > Core::$user->level) $form_exception->addError("You do not have permission to edit this user as their level is higher.");
- $page_title = "Edit User";
- $user_customer_link = UserCustomer::queryOne(["user_id" => $user->id]);
- if(!$user_customer_link) $user_customer_link = new UserCustomer;
- $customers = Customer::query([], ["sort" => "name"]);
- Core::addBreadcrumb("/account", "Account");
- Core::addBreadcrumb("/account/users", "Users");
- Core::addBreadcrumb("/account/users/$id", $user->getDisplayName());
- Core::addBreadcrumb("/account/users/$id/edit", $page_title);
- $submit = HTTP::value("submit");
- if($submit) {
- try {
- Core::validateCSRFToken($submit);
- $form_exception = new FormException;
- // Password
- $password = HTTP::value("password");
- $password_confirm = HTTP::value("password_confirm");
- if($password !== $password_confirm) $form_exception->addError("Passwords do not match.");
- if($password) {
- $user->password = password_hash($password, PASSWORD_DEFAULT, ["cost" => 14]);
- }
- $user->level = HTTP::value("level", "int");
- $user->email = HTTP::value("email", "email");
- $user->title = HTTP::value("title", "trim");
- $user->first_name = HTTP::value("first_name", "trim");
- $user->last_name = HTTP::value("last_name", "trim");
- $user->date_of_birth = HTTP::value("date_of_birth");
- $user->reported = HTTP::value("reported", "int");
- $user->reported_comment = HTTP::value("reported_comment", "trim");
- $user->phone = HTTP::value("phone", "trim");
- $user->gender = HTTP::value("gender", "trim");
- $user->activated = HTTP::value("activated", "int");
- $user->can_claim = HTTP::value("can_claim", "int");
- $user->has_driving_licence = (int) HTTP::value("has_driving_licence", "int");
- $user->has_car = (int) HTTP::value("has_car", "int");
- $user_customer_link->delete();
- $customer_id = HTTP::value("customer_id", "int");
- if($customer_id) {
- $customer = Customer::get($customer_id);
- if(!$customer) $form_exception->addError("Customer is invalid.");
- $user_customer_link = new UserCustomer;
- $user_customer_link->user_id = $user->id;
- $user_customer_link->customer_id = $customer->id;
- $user_customer_link->commit();
- }
- 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.");
- if(!isset(User::LEVELS[$user->level])) $form_exception->addError("User level is invalid.");
- if($user->level > Core::$user->level) $form_exception->addError("You cannot elevate this user's level higher than your own.");
- if(empty($user->email)) $form_exception->addError("Email address is invalid.");
- if(empty($user->title)) $form_exception->addError("Title is required.");
- if(empty($user->first_name)) $form_exception->addError("First name is required.");
- if(empty($user->last_name)) $form_exception->addError("Last name is required.");
- if(empty($user->date_of_birth)) $form_exception->addError("Date of birth is required.");
- if(empty($user->phone)) $form_exception->addError("Mobile number is required.");
- if($form_exception->hasErrors()) throw $form_exception;
- $success = $user->commit();
- if(!$success) throw new Exception("User was not updated successfully. If the email was changed, another user may already be using that address.");
- Core::addAlert("success", "User updated successfully.");
- HTTP::redirect("/account/users/$id/edit");
- }
- catch(FormException $e) {
- $e->generateAlerts();
- }
- catch(Exception $e) {
- Core::addAlert("danger", $e->getMessage());
- }
- }
- View::includeOnce("header", compact("page_title"));
- View::include("user_edit_form", compact("user", "customers", "user_customer_link"));
- View::includeOnce("footer");
|