/** * Adds a volunteer to the organization. If the volunteer is not a * member yet, then a temporary account will be created. This * will send an e-mail to the user. * * @param string $emailOrUsername * * @return Volunteer|false invited volunteer */ public function inviteVolunteer($emailOrUsername) { $user = false; $isEmail = true; if (Validate::is($emailOrUsername, 'email')) { $user = User::findOne(['where' => ['user_email' => $emailOrUsername]]); $isEmail = true; } else { $user = User::findOne(['where' => ['username' => $emailOrUsername]]); } // create temporary user if (!$user && $isEmail) { $user = User::createTemporary(['user_email' => $emailOrUsername, 'invited_by' => $this->id()]); } if (!$user) { return false; } $isTemporary = $user->isTemporary(); $volunteer = new Volunteer([$user->id(), $this->id()]); if ($volunteer->exists()) { return $volunteer; } $volunteer = new Volunteer(); $volunteer->grantAllPermissions(); $volunteer->create(['uid' => $user->id(), 'organization' => $this->id(), 'application_shared' => true, 'active' => true, 'role' => Volunteer::ROLE_VOLUNTEER]); $base = $this->app['base_url']; $orgName = $this->name; $ctaUrl = $isTemporary ? $base . 'signup?user_email=' . $user->user_email : $base . 'profile'; $user->sendEmail('volunteer-invite', ['subject' => "{$orgName} has invited you as a volunteer", 'orgname' => $orgName, 'cta_url' => $ctaUrl]); return $volunteer; }
public function joinOrganization($req, $res) { $org = $this->getOrg($req, $res); if (!is_object($org)) { return $org; } $currentUser = $this->app['user']; // make sure the user is logged in if (!$currentUser->isLoggedIn()) { setcookie('redirect', $org->url(), time() + 3600, '/'); return $res->redirect('/signup'); } // application shared by default unless explicitly specified $applicationShared = $req->request('application_shared') === null || $req->request('application_shared'); $volunteer = new Volunteer([$currentUser->id(), $org->id()]); $uid = $currentUser->id(); // make sure the application has been shared with the org if ($volunteer->exists()) { $role = max($volunteer->role, Volunteer::ROLE_VOLUNTEER); $volunteer->grantAllPermissions(); $volunteer->set(['application_shared' => $applicationShared, 'role' => $role]); } else { $volunteer = new Volunteer(); $volunteer->create(['uid' => $uid, 'organization' => $org->id(), 'application_shared' => $applicationShared, 'active' => true, 'role' => Volunteer::ROLE_AWAITING_APPROVAL]); } if ($req->query('redir') == 'profile') { return $res->redirect('/profile'); } return new View('joinOrganization', ['title' => 'Joined ' . $org->name, 'org' => $org->toArray(), 'orgObj' => $org]); }