diff --git a/module/AbaLookup/config/module.config.php b/module/AbaLookup/config/module.config.php index 2ebe094..d194152 100644 --- a/module/AbaLookup/config/module.config.php +++ b/module/AbaLookup/config/module.config.php @@ -3,6 +3,7 @@ return [ 'controllers' => [ 'invokables' => [ + 'Auth' => 'AbaLookup\AuthController', 'Home' => 'AbaLookup\HomeController', 'Users' => 'AbaLookup\UsersController', ], @@ -89,7 +90,7 @@ 'options' => [ 'route' => '/users', 'defaults' => [ - 'controller' => 'Users', + 'controller' => 'Auth', ], ], 'may_terminate' => FALSE, @@ -138,11 +139,10 @@ realpath(sprintf('%s/../view', __DIR__)), ], 'template_map' => [ - 'layout/layout' => realpath(sprintf('%s/../view/layout/layout.phtml', __DIR__)), - 'layout/home' => realpath(sprintf('%s/../view/layout/home.phtml', __DIR__)), - 'error/index' => realpath(sprintf('%s/../view/error/index.phtml', __DIR__)), 'error/404' => realpath(sprintf('%s/../view/error/404.phtml', __DIR__)), - 'profile/edit' => realpath(sprintf('%s/../view/aba-lookup/users/profile-edit.phtml', __DIR__)), + 'error/index' => realpath(sprintf('%s/../view/error/index.phtml', __DIR__)), + 'layout/home' => realpath(sprintf('%s/../view/layout/home.phtml', __DIR__)), + 'layout/layout' => realpath(sprintf('%s/../view/layout/layout.phtml', __DIR__)), 'widget/footer' => realpath(sprintf('%s/../view/aba-lookup/widget/footer.phtml', __DIR__)), ], ], diff --git a/module/AbaLookup/src/AbaLookup/AbaLookupController.php b/module/AbaLookup/src/AbaLookup/AbaLookupController.php index 35a0e8f..63cd600 100644 --- a/module/AbaLookup/src/AbaLookup/AbaLookupController.php +++ b/module/AbaLookup/src/AbaLookup/AbaLookupController.php @@ -5,9 +5,6 @@ use Zend\Mvc\Controller\AbstractActionController; use Zend\View\Model\ViewModel; -/** - * Base controller class - */ abstract class AbaLookupController extends AbstractActionController { /** diff --git a/module/AbaLookup/src/AbaLookup/AuthController.php b/module/AbaLookup/src/AbaLookup/AuthController.php new file mode 100644 index 0000000..e33313a --- /dev/null +++ b/module/AbaLookup/src/AbaLookup/AuthController.php @@ -0,0 +1,133 @@ +redirectToUsersRoute($uid); + return; + } + // Prepare the view layout + $this->prepareLayout(); + } + + /** + * Registers the user or shows a registration form + * + * Shows the registration form or sends the POST data along to the + * API for validation as needed. + * + * @return array|Zend\Http\Response + */ + public function registerAction() + { + // Get the user type from the URL + $type = $this->params('type'); + // Create a registration form for the particular + // type of user that is registering + $form = new RegisterForm($type); + // If the user has NOT submitted a POST request + if (!$this->request->isPost()) { + // Show the registration form + return [ + 'form' => $form, + 'type' => $type, + ]; + } + // The user has submitted via POST + // TODO - Validate Terms of Service + // TODO - Show previous data to user + $data = $this->params(); // TODO - Is this correct? + try { + $id = $this->getApi('UserAccount')->put( + $data->fromPost($form::ELEMENT_NAME_EMAIL_ADDRESS), + $data->fromPost($form::ELEMENT_NAME_PASSWORD), + $data->fromPost($form::ELEMENT_NAME_DISPLAY_NAME), + $data->fromPost($form::ELEMENT_NAME_USER_TYPE), + $data->fromPost($form::ELEMENT_NAME_POSTAL_CODE), + array_intersect_key( + $data->fromPost(), + // Flip this array to get the keys that are valid + // Only the valid keys remain from the POST data + array_flip([ + $form::ELEMENT_NAME_ABA_COURSE, + $form::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT, + $form::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT_DATE, + $form::ELEMENT_NAME_GENDER, + $form::ELEMENT_NAME_PHONE_NUMBER, + ]) + ) + ); + } catch (Lookup\Api\Exception\InvalidDataException $e) { + // Show the user the error message + return [ + 'error' => $e->getMessage(), + 'form' => $form, + 'type' => $type, + ]; + } + Session::setUserId($id); + // Redirect the user to their profile page + return $this->redirectToUsersRoute($id); + } + + /** + * Logs the user in + * + * Sends the POST data along to the API as needed. + * + * @return array|Zend\Http\Response + */ + public function loginAction() + { + // Create a login form + $form = new LoginForm(); + // If the user has NOT submitted a POST request + if (!$this->request->isPost()) { + // Show the login form + return [ + 'form' => $form, + ]; + } + // The user has submitted data via POST + $data = $this->params(); + try { + $id = $this->getApi('UserAccount')->get([ + 'email' => $data->fromPost($form::ELEMENT_NAME_EMAIL_ADDRESS), + 'password' => $data->fromPost($form::ELEMENT_NAME_PASSWORD), + ]); + } catch (Lookup\Api\Exception\InvalidDataException $e) { + return [ + 'error' => $e->getMessage(), + 'form' => $form, + ]; + } + // Create a session for the user + Session::setUserId($id, (bool) $data->fromPost($form::ELEMENT_NAME_REMEMBER_ME)); + return $this->redirectToUsersRoute($id); + } + + /** + * Logs the user out + * + * If a user is logged in, log them out. Invalidates the session. + * Reroutes the user to the home page. + * + * @return Zend\Http\Response + */ + public function logoutAction() + { + Session::unsetUserId(); + return $this->redirectHome(); + } +} diff --git a/module/AbaLookup/src/AbaLookup/Form/AbstractBaseForm.php b/module/AbaLookup/src/AbaLookup/Form/AbstractBaseForm.php index 6359743..110c124 100644 --- a/module/AbaLookup/src/AbaLookup/Form/AbstractBaseForm.php +++ b/module/AbaLookup/src/AbaLookup/Form/AbstractBaseForm.php @@ -2,23 +2,8 @@ namespace AbaLookup\Form; -use - AbaLookup\Entity\User, - Zend\Filter\Digits, - Zend\Filter\StringTrim, - Zend\Form\Exception\DomainException, - Zend\Form\Form, - Zend\I18n\Filter\Alnum as AlnumFilter, - Zend\Validator\Date as DateValidator, - Zend\Validator\EmailAddress as EmailAddressValidator, - Zend\Validator\NotEmpty, - Zend\Validator\Regex, - Zend\Validator\StringLength as StrlenValidator -; +use Zend\Form\Form; -/** - * Abstract base class for custom forms - */ abstract class AbstractBaseForm extends Form { /** @@ -35,226 +20,12 @@ abstract class AbstractBaseForm extends Form const ELEMENT_NAME_PHONE_NUMBER = 'phone-number'; const ELEMENT_NAME_POSTAL_CODE = 'postal-code'; const ELEMENT_NAME_REMEMBER_ME = 'remember-me'; + const ELEMENT_NAME_WEEKDAY = 'weekday'; + const ELEMENT_NAME_ADD_REMOVE_AVAILABILITY = 'add-remove-availability'; /** - * Error message if form is invalid + * Constants for user types */ - protected $message; - - /** - * Returns whether the display name is valid - * - * Also sets the error message appropriately. - * - * @return bool - */ - protected function isDisplayNameValid() - { - // Filter out all but alphanumeric - $displayName = (new AlnumFilter(/* Allow whitespace */ TRUE)) - ->filter($this->data[self::ELEMENT_NAME_DISPLAY_NAME]); - $this->data[self::ELEMENT_NAME_DISPLAY_NAME] = $displayName; - // Is valid? - $isValid = isset($displayName) - && (new StrlenValidator(['min' => User::MINIMUM_LENGTH_DISPLAY_NAME])) - ->isValid($displayName) - && (new NotEmpty())->isValid($displayName); - // Set the message - if (!$isValid) { - $this->message = 'The entered display name is invalid.'; - } - return $isValid; - } - - /** - * Returns whether the email address is valid - * - * Also sets the error message appropriately. - * - * @return bool - */ - protected function isEmailAddressValid() - { - // Is valid? - $isValid = (new EmailAddressValidator()) - ->isValid($this->data[self::ELEMENT_NAME_EMAIL_ADDRESS]); - // Set the message - if (!$isValid) { - $this->message = 'The entered email address is not valid.'; - } - return $isValid; - } - - /** - * Returns whether the password is valid - * - * Also sets the error message appropriately if needed. - * - * @return bool - */ - protected function isPasswordValid() - { - // Aliases - $confirmPassword = $this->data[self::ELEMENT_NAME_CONFIRM_PASSWORD]; - $minlen = User::MINIMUM_LENGTH_PASSWORD; - $password = $this->data[self::ELEMENT_NAME_PASSWORD]; - // Validators - $strlen = new StrlenValidator(['min' => $minlen]); - // Is valid? - $isValid = isset($password, $confirmPassword) - && $strlen->isValid($password); - if (!$isValid) { - $this->message = sprintf( - 'Password must be at least %d characters long.', - $minlen - ); - } elseif ($password !== $confirmPassword) { - $isValid = FALSE; - $this->message = 'You must confirm your password.'; - } - return $isValid; - } - - /** - * Returns whether the phone number is valid - * - * Also sets the error message appropriately. - * - * @return bool - */ - protected function isPhoneNumberValid() - { - // Filter out all but digits - $phone = (new Digits())->filter($this->data[self::ELEMENT_NAME_PHONE_NUMBER]); - $this->data[self::ELEMENT_NAME_PHONE_NUMBER] = $phone; - // Is valid? - if ((new NotEmpty())->isValid($phone)) { - $isValid = (new StrlenValidator(['min' => User::MINIMUM_LENGTH_PHONE_NUMBER])) - ->isValid($phone); - // Set the message - if (!$isValid) { - $this->message = 'The entered phone number is not valid.'; - return FALSE; - } - } - return TRUE; - } - - /** - * Returns whether the postal code is valid - * - * Sets the error message appropriately as well. Note that this does not - * ensure that the postal code exists, but that it is a postal code. - * - * @return boolg - */ - protected function isPostalCodeValid() - { - $postalCode = (new AlnumFilter(/* Allow whitespace */ FALSE)) - ->filter($this->data[self::ELEMENT_NAME_POSTAL_CODE]); - $this->data[self::ELEMENT_NAME_POSTAL_CODE] = $postalCode; - if (!$postalCode) { - return TRUE; - } - $isValid = (new Regex(['pattern' => '/^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$/i'])) - ->isValid($postalCode); - if (!$isValid) { - $this->message = 'The entered postal code is not valid.'; - } - return $isValid; - } - - /** - * Returns whether the Certificate of Conduct is properly set - * - * Checks three possible cases: - * - * 1. The checkbox to indicate that the user has recieved their Certificate - * of Conduct is checked, and the date entered is valid. - * 2. The checkbox is checked, but the entered date is not valid. This will - * set the error message appropriately. - * 3. The checkbox was not selected, and in this case, the value should be NULL. - * - * Postcondition: {@code $this->data[self::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT]} will contain - * the UNIX timestamp for the date the user entered IF the checkbox was checked, ELSE it will - * contain NULL. You should never need to access the date field directly other than in this function. - * "Date field" refers to: {@code $this->data[self::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT_DATE]} - * - * @return bool Whether the Certificate of Conduct is properly set. - */ - protected function isCertificateOfConductValid() - { - // Treat the checkbox as a boolean value - $cert = (bool) $this->data[self::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT]; - $date = $this->data[self::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT_DATE]; - // Is valid? - $isValid = FALSE; - // If checkbox was checked and date was valid - if ($cert) { - if ((new DateValidator(['format' => 'Y-m-d']))->isValid($date)) { - $this->data[self::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT] = strtotime($date); - $isValid = TRUE; - } else { - // The checkbox was checked but invalid date - $this->message = 'The entered date is not valid.'; - $isValid = FALSE; - } - } else { - // Checkbox was not checked - $this->data[self::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT] = NULL; - $isValid = TRUE; - } - return $isValid; - } - - /** - * Returns the error message generated by the form - * - * @return string - */ - public function getMessage() - { - return isset($this->message) ? $this->message : ''; - } - - /** - * Validates the form - * - * Overrides Zend\Form\Form::isValid. - * - * @return bool Is the form valid? - * @throws DomainException - */ - public function isValid() - { - if ($this->hasValidated) { - // Validation has already occurred - return $this->isValid; - } - // Default to invalid - $this->isValid = FALSE; - if (!is_array($this->data)) { - $data = $this->extract(); - if (!is_array($data) || !isset($this->data)) { - // No data has been set - throw new DomainException(sprintf( - '%s is unable to validate as there is no data currently set', __METHOD__ - )); - } - $this->data = $data; - } - // Trim all the data - $strtrim = new StringTrim(); - foreach ($this->data as $k => $v) { - $this->data[$k] = $strtrim->filter($v); - } - $this->setIsValid(); - $this->hasValidated = TRUE; - return $this->isValid; - } - - /** - * Sets the {@code $isValid} property. - */ - abstract public function setIsValid(); + const USER_TYPE_ABA_THERAPIST = 'therapist'; + const USER_TYPE_PARENT = 'parent'; } diff --git a/module/AbaLookup/src/AbaLookup/Form/LoginForm.php b/module/AbaLookup/src/AbaLookup/Form/LoginForm.php index c5b39c3..b466e0e 100644 --- a/module/AbaLookup/src/AbaLookup/Form/LoginForm.php +++ b/module/AbaLookup/src/AbaLookup/Form/LoginForm.php @@ -2,21 +2,8 @@ namespace AbaLookup\Form; -use - AbaLookup\Entity\User, - Zend\Filter\StringTrim, - Zend\Validator\EmailAddress as EmailAddressValidator, - Zend\Validator\StringLength as StringLengthValidator -; - -/** - * The login form for users - */ class LoginForm extends AbstractBaseForm { - /** - * Constructor - */ public function __construct() { parent::__construct(); @@ -63,66 +50,4 @@ public function __construct() ], ]); } - - /** - * Sets the {@code $isValid} property - */ - public function setIsValid() - { - // Data aliases - $email = $this->data[self::ELEMENT_NAME_EMAIL_ADDRESS]; - $password = $this->data[self::ELEMENT_NAME_PASSWORD]; - // Validators - $emailAddress = new EmailAddressValidator(); - $minPasswordChars = new StringLengthValidator(['min' => User::MINIMUM_LENGTH_PASSWORD]); - // Set is valid? - if ( - !$emailAddress->isValid($email) - || !$minPasswordChars->isValid($password) - ) { - $this->message = 'The entered credentials are not valid.'; - $this->isValid = FALSE; - } else { - $this->isValid = TRUE; - } - } - - /** - * Returns the email address entered - * - * @return string|NULL - */ - public function getEmailAddress() - { - if (!$this->hasValidated || !$this->isValid) { - return NULL; - } - return $this->data[self::ELEMENT_NAME_EMAIL_ADDRESS]; - } - - /** - * Returns the password entered - * - * @return string|NULL - */ - public function getPassword() - { - if (!$this->hasValidated || !$this->isValid) { - return NULL; - } - return $this->data[self::ELEMENT_NAME_PASSWORD]; - } - - /** - * Returns whether to remember the user session - * - * @return bool|NULL - */ - public function rememberMe() - { - if (!$this->hasValidated || !$this->isValid) { - return NULL; - } - return (bool) $this->data[self::ELEMENT_NAME_REMEMBER_ME]; - } } diff --git a/module/AbaLookup/src/AbaLookup/Form/ProfileEditForm.php b/module/AbaLookup/src/AbaLookup/Form/ProfileEditForm.php index f52e55f..a14fe4e 100644 --- a/module/AbaLookup/src/AbaLookup/Form/ProfileEditForm.php +++ b/module/AbaLookup/src/AbaLookup/Form/ProfileEditForm.php @@ -2,22 +2,12 @@ namespace AbaLookup\Form; -use - AbaLookup\Entity\User, - AbaLookup\Entity\UserType -; - -/** - * The form for editing a user profile - */ class ProfileEditForm extends AbstractBaseForm { /** - * Constructor - * - * @param User $user The user whose profile is being edited + * @param Lookup\Entity\User $user The user whose profile is being edited. */ - public function __construct(User $user) + public function __construct(Lookup\Entity\User $user) { parent::__construct(); // Display name @@ -69,8 +59,16 @@ public function __construct(User $user) 'label' => 'Postal code (optional)', ], ]); + // Hidden user type field + $this->add([ + 'name' => self::ELEMENT_NAME_USER_TYPE, + 'attributes' => [ + 'type' => 'hidden', + 'value' => $user->getUserType(), + ], + ]); // Show therapist-only fields? - if ($user->getUserType() === UserType::TYPE_ABA_THERAPIST) { + if ($user->getUserType() === self::USER_TYPE_ABA_THERAPIST) { // ABA training course $this->add([ 'name' => self::ELEMENT_NAME_ABA_COURSE, @@ -124,46 +122,4 @@ public function __construct(User $user) ], ]); } - - /** - * Sets the {@code $isValid} property - */ - public function setIsValid() - { - $this->isValid = $this->isDisplayNameValid() - && $this->isEmailAddressValid() - && $this->isPhoneNumberValid() - && $this->isPostalCodeValid() - && $this->isCertificateOfConductValid(); - } - - /** - * Updates the user with their new information - * - * Populates the fields with the updated data. - * - * @param User $user The user to update. - * @return bool Whether the update was successful. - */ - public function updateUser(User $user) - { - if (!$this->hasValidated || !$this->isValid) { - return FALSE; - } - // Aliases - $abaCourse = $this->data[self::ELEMENT_NAME_ABA_COURSE]; - $certificateOfConduct = $this->data[self::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT]; - $displayName = $this->data[self::ELEMENT_NAME_DISPLAY_NAME]; - $email = $this->data[self::ELEMENT_NAME_EMAIL_ADDRESS]; - $phone = $this->data[self::ELEMENT_NAME_PHONE_NUMBER]; - $postalCode = $this->data[self::ELEMENT_NAME_POSTAL_CODE]; - // Update the information - $user->setAbaCourse($abaCourse !== NULL ? (bool) $abaCourse : $abaCourse) - ->setCertificateOfConduct($certificateOfConduct) - ->setDisplayName($displayName) - ->setEmail($email) - ->setPhone($phone ? (int) $phone : NULL) - ->setPostalCode($postalCode ? $postalCode : NULL); - return TRUE; - } } diff --git a/module/AbaLookup/src/AbaLookup/Form/RegisterForm.php b/module/AbaLookup/src/AbaLookup/Form/RegisterForm.php index 9fbcb38..d71e891 100644 --- a/module/AbaLookup/src/AbaLookup/Form/RegisterForm.php +++ b/module/AbaLookup/src/AbaLookup/Form/RegisterForm.php @@ -2,32 +2,14 @@ namespace AbaLookup\Form; -use - AbaLookup\Entity\User, - AbaLookup\Entity\UserType -; - -/** - * The form for registering users - */ class RegisterForm extends AbstractBaseForm { /** - * The user type for this form. - * - * @see UserType - */ - protected $userType; - - /** - * Constructor - * - * @param string $userType The type of the user registering. + * @param string $utype The type of the user registering. */ - public function __construct($userType) + public function __construct($utype) { parent::__construct(); - $this->userType = $userType; // Display name $this->add([ 'name' => self::ELEMENT_NAME_DISPLAY_NAME, @@ -84,8 +66,16 @@ public function __construct($userType) 'label' => 'Your phone number (optional)', ], ]); + // Hidden user type field + $this->add([ + 'name' => self::ELEMENT_NAME_USER_TYPE, + 'attributes' => [ + 'type' => 'hidden', + 'value' => $utype, + ], + ]); // Show therapist-only fields? - if ($userType === UserType::TYPE_ABA_THERAPIST) { + if ($utype === self::USER_TYPE_ABA_THERAPIST) { // Gender $this->add([ 'name' => self::ELEMENT_NAME_GENDER, @@ -127,7 +117,7 @@ public function __construct($userType) 'checked_value' => TRUE, ], ]); - // Certificate of Conduct Date + // Certificate of Conduct date $this->add([ 'name' => self::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT_DATE, 'type' => 'text', @@ -161,57 +151,4 @@ public function __construct($userType) ], ]); } - - /** - * Sets the {@code $isValid} property - */ - public function setIsValid() - { - $this->isValid = $this->isDisplayNameValid() - && $this->isEmailAddressValid() - && $this->isPasswordValid() - && $this->isPhoneNumberValid() - && $this->isPostalCodeValid() - && $this->isCertificateOfConductValid(); - } - - /** - * Returns the new {@code User} from the form fields - * - * @return User|NULL - */ - public function getUser() - { - if (!$this->hasValidated || !$this->isValid) { - return NULL; - } - // Data field aliases - $displayName = $this->data[self::ELEMENT_NAME_DISPLAY_NAME]; - $email = $this->data[self::ELEMENT_NAME_EMAIL_ADDRESS]; - $password = $this->data[self::ELEMENT_NAME_PASSWORD]; - $phone = $this->data[self::ELEMENT_NAME_PHONE_NUMBER]; - $gender = $this->data[self::ELEMENT_NAME_GENDER]; - $abaCourse = $this->data[self::ELEMENT_NAME_ABA_COURSE]; - $certificateOfConduct = $this->data[self::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT]; - $postalCode = $this->data[self::ELEMENT_NAME_POSTAL_CODE]; - // Create and return a new user - $user = new User( - $displayName, - $email, - $password, - $this->userType, - $gender, - $abaCourse !== NULL ? (bool) $abaCourse : $abaCourse, - $certificateOfConduct - ); - if ($phone) { - // The user entered a phone number - $user->setPhone((int) $phone); - } - if ($postalCode) { - // The user entered their postal code - $user->setPostalCode($postalCode); - } - return $user; - } } diff --git a/module/AbaLookup/src/AbaLookup/Form/ScheduleForm.php b/module/AbaLookup/src/AbaLookup/Form/ScheduleForm.php new file mode 100644 index 0000000..3e4c52e --- /dev/null +++ b/module/AbaLookup/src/AbaLookup/Form/ScheduleForm.php @@ -0,0 +1,55 @@ +add([ + 'name' => self::ELEMENT_NAME_WEEKDAY, + 'type' => 'select', + 'options' => [ + 'label' => 'Weekday', + 'value_options' => [ + '1' => 'Sunday', + '2' => 'Monday', + '3' => 'Tuesday', + '4' => 'Wednesday', + '5' => 'Thursday', + '6' => 'Friday', + '7' => 'Saturday', + ], + ], + 'attributes' => [ + 'value' => '1', + ]; + ]); + // TODO - Add inputs for times (issue #84) + // Add/remove availability + $this->add([ + 'name' => self::ELEMENT_NAME_ADD_REMOVE_AVAILABILITY, + 'type' => 'radio', + 'options' => [ + 'label' => 'Add or remove availability', + 'value_options' => [ + '0' => 'Add', + '1' => 'Remove', + ], + ], + 'attributes' => [ + 'value' => '0', + ]; + ]); + // Submit + $this->add([ + 'name' => 'login', + 'type' => 'submit', + 'attributes' => [ + 'value' => 'Update schedule', + ], + ]); + } +} diff --git a/module/AbaLookup/src/AbaLookup/Form/View/Helper/Form.php b/module/AbaLookup/src/AbaLookup/Form/View/Helper/Form.php index 46863a3..e6169a6 100644 --- a/module/AbaLookup/src/AbaLookup/Form/View/Helper/Form.php +++ b/module/AbaLookup/src/AbaLookup/Form/View/Helper/Form.php @@ -2,10 +2,8 @@ namespace AbaLookup\Form\View\Helper; -use - Zend\Form\FormInterface, - Zend\Form\View\Helper\Form as ZendFormHelper -; +use Zend\Form\FormInterface; +use Zend\Form\View\Helper\Form as ZendFormHelper; /** * View helper that generates a form @@ -15,7 +13,7 @@ class Form extends ZendFormHelper /** * Returns the HTML markup for the given form * - * @param FormInterface $f The form. + * @param Zend\Form\FormInterface $f The form. * @return string */ public function markup(FormInterface $f) diff --git a/module/AbaLookup/src/AbaLookup/HomeController.php b/module/AbaLookup/src/AbaLookup/HomeController.php index 997c67c..de89b96 100644 --- a/module/AbaLookup/src/AbaLookup/HomeController.php +++ b/module/AbaLookup/src/AbaLookup/HomeController.php @@ -3,33 +3,69 @@ namespace AbaLookup; use AbaLookup\Session\Session; -use Zend\EventManager\EventManagerInterface; -use Zend\Mvc\MvcEvent; class HomeController extends AbaLookupController { - public function setEventManager(EventManagerInterface $events) - { - parent::setEventManager($events); - $events->attach(MvcEvent::EVENT_DISPATCH, [$this, 'beforeAction'], 100); - } + /** + * @var Lookup\Entity\User + */ + protected $user; - public function beforeAction() + public function __construct() { // Set the home layout $this->layout('layout/home'); $uid = Session::getUserId(); try { - $user = $this->getApi('UserAccount') - ->get($uid); + $$this->user = $this->getApi('UserAccount') + ->get($uid); } catch (Lookup\Api\Exception\InvalidDataException $e) { // TODO - Handle this - $user = NULL; + $this->user = NULL; } // Prepare the layout - $this->prepareLayout($user); + $this->prepareLayout($this->user); + } + + public function indexAction() + { + return [ + 'user' => $this->user, + ]; + } + + public function privacyAction() + { + return [ + 'user' => $this->user, + ]; + } + + public function aboutAction() + { + return [ + 'user' => $this->user, + ]; + } + + public function termsAction() + { + return [ + 'user' => $this->user, + ]; + } + + public function sponsorsAction() + { + return [ + 'user' => $this->user, + ]; + } + + public function colophonAction() + { return [ - 'user' => $user, + 'user' => $this->user, ]; } } diff --git a/module/AbaLookup/src/AbaLookup/Match/Match.php b/module/AbaLookup/src/AbaLookup/Match/Match.php deleted file mode 100644 index 4c9ca8d..0000000 --- a/module/AbaLookup/src/AbaLookup/Match/Match.php +++ /dev/null @@ -1,8 +0,0 @@ -getManager() ->getConfig() ->setCookieHttpOnly(TRUE) // As per issue #87 ->rememberMe((is_bool($remember) && $remember) ? Session::SECONDS_3_MONTHS : 0); - $session->offsetSet(Session::SESSION_USER_KEY_ID, $id); + $session->offsetSet(Session::USER_KEY_ID, $id); } /** @@ -42,7 +39,7 @@ public static function setUserId($id, $remember = FALSE) */ public static function getUserId() { - return (new Container(Session::SESSION_USER_NAMESPACE))->offsetGet(Session::SESSION_USER_ID_KEY); + return (new Container(Session::USER_NAMESPACE))->offsetGet(Session::USER_KEY_ID); } /** @@ -52,6 +49,6 @@ public static function getUserId() */ public static function unsetUserId() { - (new Container(Session::SESSION_USER_NAMESPACE))->offsetUnset(Session::SESSION_USER_ID_KEY); + (new Container(Session::USER_NAMESPACE))->offsetUnset(Session::USER_KEY_ID); } } diff --git a/module/AbaLookup/src/AbaLookup/UsersController.php b/module/AbaLookup/src/AbaLookup/UsersController.php index ca0a5c6..d1e367f 100644 --- a/module/AbaLookup/src/AbaLookup/UsersController.php +++ b/module/AbaLookup/src/AbaLookup/UsersController.php @@ -2,142 +2,37 @@ namespace AbaLookup; -use AbaLookup\Form\LoginForm; use AbaLookup\Form\ProfileEditForm; -use AbaLookup\Form\RegisterForm; use AbaLookup\Form\ScheduleForm; use AbaLookup\Session\Session; -use Zend\View\Model\ViewModel; -/** - * Controller for user actions - */ class UsersController extends AbaLookupController { /** - * Registers the user or shows a registration form - * - * Shows the registration form or sends the POST data along to the - * API for validation as needed. - * - * @return array|Zend\Http\Response + * The ID of the user in session */ - public function registerAction() - { - // Is a user logged in? - $uid = Session::getUserId(); - if (!is_null($uid)) { - // Redirect the user to their profile page - return $this->redirectToUsersRoute($id); - } - // Prepare the view layout - $this->prepareLayout(); - // Get the user type from the URL - $type = $this->params('type'); - // Create a registration form for the particular - // type of user that is registering - $form = new RegisterForm($type); - // If the user has NOT submitted a POST request - if (!$this->request->isPost()) { - // Show the registration form - return [ - 'form' => $form, - 'type' => $type, - ]; - } - // The user has submitted via POST - // TODO - Validate Terms of Service - // TODO - Show previous data to user - $data = $this->params(); // TODO - Is this correct? - try { - $id = $this->getApi('UserAccount')->put( - $data->fromPost($form::ELEMENT_NAME_EMAIL_ADDRESS), - $data->fromPost($form::ELEMENT_NAME_PASSWORD), - $data->fromPost($form::ELEMENT_NAME_DISPLAY_NAME), - $data->fromPost($form::ELEMENT_NAME_USER_TYPE), - $data->fromPost($form::ELEMENT_NAME_POSTAL_CODE), - array_intersect_key( - $data->fromPost(), - // Flip this array to get the keys that are valid - // Only the valid keys remain from the POST data - array_flip([ - $form::ELEMENT_NAME_ABA_COURSE, - $form::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT, - $form::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT_DATE, - $form::ELEMENT_NAME_GENDER, - $form::ELEMENT_NAME_PHONE_NUMBER, - ]) - ) - ); - } catch (Lookup\Api\Exception\InvalidDataException $e) { - // Show the user the error message - return [ - 'error' => $e->getMessage(), - 'form' => $form, - 'type' => $type, - ]; - } - Session::setUserId($id); - // Redirect the user to their profile page - return $this->redirectToUsersRoute($id); - } + protected $uid; /** - * Logs the user in - * - * Sends the POST data along to the API as needed. + * The user object for the user in session * - * @return array|Zend\Http\Response + * @var Lookup\Entity\User */ - public function loginAction() + protected $user; + + public function __construct() { - // Is a user logged in? - $uid = Session::getUserId(); - if (!is_null($uid)) { - // Redirect the user to their profile page - return $this->redirectToUsersRoute($id); - } - // Prepare the view layout - $this->prepareLayout(); - // Create a login form - $form = new LoginForm(); - // If the user has NOT submitted a POST request - if (!$this->request->isPost()) { - // Show the login form - return [ - 'form' => $form, - ]; - } - // The user has submitted data via POST - $data = $this->params(); try { - $id = $this->getApi('UserAccount')->get([ - 'email' => $data->fromPost($form::ELEMENT_NAME_EMAIL_ADDRESS), - 'password' => $data->fromPost($form::ELEMENT_NAME_PASSWORD), - ]); + $this->uid = Session::getUserId(); + $this->user = $this->getApi('UserAccount') + ->get($uid); } catch (Lookup\Api\Exception\InvalidDataException $e) { - return [ - 'error' => $e->getMessage(), - 'form' => $form, - ]; + // The user ID is NOT valid + $this->redirectToLoginPage(); + return; } - // Create a session for the user - Session::setUserId($id, $form->rememberMe()); - return $this->redirectToUsersRoute($id); - } - - /** - * Logs the user out - * - * If a user is logged in, log them out. Invalidates the session. - * Reroutes the user to the home page. - * - * @return Zend\Http\Response - */ - public function logoutAction() - { - Session::unsetUserId(); - return $this->redirectHome(); + // Prepare the layout + $this->prepareLayout($this->user); } /** @@ -149,29 +44,19 @@ public function logoutAction() */ public function profileAction() { - try { - $uid = Session::getUserId(); - $user = $this->getApi('UserAccount') - ->get($uid); - } catch (Lookup\Api\Exception\InvalidDataException $e) { - // The ID is NOT valid - return $this->redirectToLoginPage(); - } - // Prepare the layout - $this->prepareLayout($user); - $form = new ProfileEditForm($user); + $form = new ProfileEditForm($this->user); // If the user has NOT submitted a POST request if (!$this->request->isPost()) { // Show the edit form return [ - 'user' => $user, 'form' => $form, + 'user' => $this->user, ]; } // The user has submitted data via POST $data = $this->params(); try { - $this->getApi('UserAccount')->post($uid, [ + $this->getApi('UserAccount')->post($this->uid, [ 'aba_course' => $data->fromPost($form::ELEMENT_NAME_ABA_COURSE), 'certificate_of_conduct' => $data->fromPost($form::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT_DATE), 'display_name' => $data->fromPost($form::ELEMENT_NAME_DISPLAY_NAME), @@ -184,12 +69,12 @@ public function profileAction() // Show the error message return [ 'error' => $e->getMessage(), - 'user' => $user, - 'form' => $form, + 'form' => $form, + 'user' => $this->user, ]; } // Redirect to the profile page - return $this->redirectToUsersRoute($uid); + return $this->redirectToUsersRoute($this->uid); } /** @@ -201,29 +86,22 @@ public function profileAction() */ public function scheduleAction() { - try { - $uid = Session::getUserId(); - $user = $this->getApi('UserAccount') - ->get($uid); - } catch (Lookup\Api\Exception\InvalidDataException $e) { - // The user ID is NOT valid - return $this->redirectToLoginPage(); - } - // Prepare the layout - $this->prepareLayout($user); + // Create the schedule edit form + $form = new ScheduleForm(); // Get the user's schedules $schedules = $this->getApi('Schedule') - ->get(['user_id' => $uid]); + ->get(['user_id' => $this->uid]); if ($this->request->isPost()) { // Add the availability to the schedule - $data = $this->params(); + // $data = $this->params(); // TODO - Make a PUT request to ScheduleInterval API - return $this->redirectToUsersRoute($uid, 'schedule'); + // return $this->redirectToUsersRoute($this->uid, 'schedule'); } // Show the user their schedule return [ + 'form' => $form, + 'user' => $this->user, 'schedules' => $schedules, - 'user' => $user, ]; } @@ -234,19 +112,9 @@ public function scheduleAction() */ public function matchesAction() { - try { - $uid = Session::getUserId(); - $user = $this->getApi('UserAccount') - ->get($uid); - } catch (Lookup\Api\Exception\InvalidDataException $e) { - // The user ID is NOT valid - return $this->redirectToLoginPage(); - } - // Prepare the layout - $this->prepareLayout($user); // Show the user their matches return [ - 'user' => $user, + 'user' => $this->user, ]; } } diff --git a/module/AbaLookup/view/aba-lookup/users/profile-edit.phtml b/module/AbaLookup/view/aba-lookup/users/profile-edit.phtml deleted file mode 100644 index 8db1bdf..0000000 --- a/module/AbaLookup/view/aba-lookup/users/profile-edit.phtml +++ /dev/null @@ -1,19 +0,0 @@ -headTitle('Profile - ' . $this->user->getDisplayName()); -?> -
{$this->error}
"; - } - $this->form->setAttribute('class', 'left'); - $this->form->setAttribute('action', '/users' . '/' . $this->user->getId() . '/profile/edit'); - $this->form->setAttribute('method', 'post'); - echo $this->form()->markup($this->form); - ?> -{$this->confirm}
"; + if (isset($this->error)) { + echo "{$this->error}
"; } - ?> -- user->getDisplayName(); ?> -
-- user->getEmail(); ?> -
-- user->getPhone(); - echo isset($phone) ? $phone : 'n/a'; - ?> -
- user->getUserType() === UserType::TYPE_ABA_THERAPIST): - ?> -- user->getGender(); - echo isset($gender) ? ($gender == 'F' ? 'Female' : 'Male') : 'Undisclosed'; - ?> -
-- user->getCertificateOfConduct(); - echo isset($epoch) ? 'Completed on ' . date('F jS, Y', $epoch) : - 'You will need to get a Certificate of Conduct from the RNC'; - ?> -
-- user->getAbaCourse()) ? 'Completed' : 'You will need to complete your ABA training course'; ?> -
- anchor( - 'Edit your information', - sprintf('/users/%d/profile/edit', $this->user->getId()), - ['button'] - ); + $this->form->setAttribute('class', 'left'); + $this->form->setAttribute('action', '/users' . '/' . $this->user->getId() . '/profile/edit'); + $this->form->setAttribute('method', 'post'); + echo $this->form()->markup($this->form); ?>