/**
  * Add method
  *
  * @return \App\Controller\Response
  */
 public function add()
 {
     $user = $this->Users->newEntity();
     if ($this->request->is('post') || $this->request->is('put')) {
         $this->request->data['password'] = $this->request->data['new_password'];
         if (empty($this->request->data['client_communities'][0]['id'])) {
             $this->request->data['client_communities'] = [];
         }
         // Ignore ClientCommunity if user is not a client
         if ($this->request->data['role'] != 'client') {
             unset($this->request->data['client_communities']);
         }
         $user = $this->Users->patchEntity($user, $this->request->data);
         $errors = $user->errors();
         if (empty($errors) && $this->Users->save($user)) {
             $Mailer = new Mailer();
             $result = $Mailer->sendNewAccountEmail($user, $this->request->data['new_password']);
             if ($result) {
                 $this->Flash->success('User account created and login credentials emailed');
                 return $this->redirect(['prefix' => 'admin', 'action' => 'index']);
             } else {
                 $this->Users->delete($user);
                 $msg = 'There was an error emailing this user with their login info. No new account was created.';
                 $msg .= ' Please try again or contact an administrator for assistance.';
                 $this->Flash->error($msg);
             }
         } else {
             $msg = 'There was an error creating this user\'s account.';
             $msg .= ' Please try again or contact an administrator for assistance.';
             $this->Flash->error($msg);
         }
     } else {
         $this->request->data['all_communities'] = false;
     }
     $this->prepareForm($user);
     $this->set(['titleForLayout' => 'Add User']);
     $this->render('/Admin/Users/form');
 }
 /**
  * Add client method
  *
  * @param int $communityId Community ID
  * @return \Cake\Network\Response|null
  */
 public function addClient($communityId)
 {
     $community = $this->Communities->get($communityId);
     $usersTable = TableRegistry::get('Users');
     if ($this->request->is('post')) {
         $client = $usersTable->newEntity($this->request->data());
         $client->role = 'client';
         $client->client_communities = [$this->Communities->get($communityId)];
         $client->password = $this->request->data('unhashed_password');
         $errors = $client->errors();
         if (empty($errors) && $usersTable->save($client)) {
             $Mailer = new Mailer();
             $result = $Mailer->sendNewAccountEmail($client, $this->request->data('unhashed_password'));
             if ($result) {
                 $msg = 'Client account created for ' . $client->name . ' and login instructions emailed';
                 $this->Flash->success($msg);
                 return $this->redirect(['action' => 'clients', $communityId]);
             } else {
                 $msg = 'There was an error emailing account login info to ' . $client->name . '.';
                 $msg .= ' No new account was created. Please contact an administrator for assistance.';
                 $retval[] = $msg;
                 $usersTable->delete($client);
             }
         } else {
             $msg = 'There was an error saving that client.';
             $msg .= ' Please try again or contact an administrator for assistance.';
             $this->Flash->error($msg);
         }
     } else {
         $client = $usersTable->newEntity();
         $client->unhashed_password = $usersTable->generatePassword();
     }
     $this->set(['client' => $client, 'communityId' => $communityId, 'communityName' => $community->name, 'salutations' => $usersTable->getSalutations(), 'role' => 'client', 'titleForLayout' => 'Add a New Client for ' . $community->name]);
 }