static function postProcess(&$form)
 {
     $values = $form->exportValues();
     $teamId = $values['pcp_team_contact'];
     $teampcpId = CRM_Pcpteams_Utils::getPcpIdByContactAndEvent($form->get('component_page_id'), $teamId);
     $userId = CRM_Pcpteams_Utils::getloggedInUserId();
     // Create Team Member of relation to this Team
     $cfpcpab = CRM_Pcpteams_Utils::getPcpABCustomFieldId();
     $cfpcpba = CRM_Pcpteams_Utils::getPcpBACustomFieldId();
     $customParams = array("custom_{$cfpcpab}" => $form->get('page_id'), "custom_{$cfpcpba}" => $teampcpId);
     CRM_Pcpteams_Utils::createTeamRelationship($userId, $teamId, $customParams);
     $form->_teamName = CRM_Contact_BAO_Contact::displayName($teamId);
     $form->set('teamName', $form->_teamName);
     $form->set('teamContactID', $teamId);
     $form->set('teamPcpId', $teampcpId);
     $teamAdminId = CRM_Pcpteams_Utils::getTeamAdmin($teampcpId);
     // Team Join: create activity
     $actParams = array('target_contact_id' => $teamId, 'assignee_contact_id' => $teamAdminId);
     CRM_Pcpteams_Utils::createPcpActivity($actParams, CRM_Pcpteams_Constant::C_AT_REQ_MADE);
     CRM_Core_Session::setStatus(ts('A notification has been sent to the team. Once approved, team should be visible on your page.'), ts('Team Request Sent'));
     //send email once the team request has done.
     list($teamAdminName, $teamAdminEmail) = CRM_Contact_BAO_Contact::getContactDetails($teamAdminId);
     $contactDetails = civicrm_api('Contact', 'get', array('version' => 3, 'sequential' => 1, 'id' => $userId));
     $emailParams = array('tplParams' => array('teamAdminName' => $teamAdminName, 'userFirstName' => $contactDetails['values'][0]['first_name'], 'userlastName' => $contactDetails['values'][0]['last_name'], 'teamName' => $form->_teamName, 'pageURL' => CRM_Utils_System::url('civicrm/pcp/manage', "reset=1&id={$teampcpId}", TRUE, NULL, FALSE, TRUE)), 'email' => array($teamAdminName => array('first_name' => $teamAdminName, 'last_name' => $teamAdminName, 'email-Primary' => $teamAdminEmail, 'display_name' => $teamAdminName)), 'valueName' => CRM_Pcpteams_Constant::C_MSG_TPL_JOIN_REQUEST);
     $sendEmail = CRM_Pcpteams_Utils::sendMail($userId, $emailParams);
 }
 static function postProcess(&$form)
 {
     $values = $form->exportValues();
     $orgName = $values['organization_name'];
     $cSubType = CRM_Pcpteams_Constant::C_CONTACT_SUB_TYPE_TEAM;
     $params = array('version' => '1', 'contact_type' => 'Organization', 'contact_sub_type' => $cSubType, 'organization_name' => $orgName);
     $createTeam = civicrm_api3('Contact', 'create', $params);
     // Create Dummy Team PCP Page
     $teamPcpId = CRM_Pcpteams_Utils::createDefaultPcp($createTeam['id'], $form->get('component_page_id'));
     // Create/Update custom record with team pcp id and create relationship with user as Team Admin
     if ($teamPcpId) {
         $userId = CRM_Pcpteams_Utils::getloggedInUserId();
         CRM_Pcpteams_Utils::createTeamRelationship($userId, $createTeam['id'], $custom = array(), 'create');
         $params = array('version' => 3, 'entity_id' => $form->get('page_id'), "team_pcp_id" => $teamPcpId);
         $result = civicrm_api3('pcpteams', 'customcreate', $params);
         $form->set('teamName', $orgName);
         $form->set('teamContactID', $createTeam['id']);
         $form->set('teamPcpId', $teamPcpId);
         $actParams = array('target_contact_id' => $createTeam['id']);
         CRM_Pcpteams_Utils::createPcpActivity($actParams, CRM_Pcpteams_Constant::C_AT_TEAM_CREATE);
         CRM_Core_Session::setStatus(ts("Your Team %1 has been created, you can invite members from your team page.", array(1 => $orgName)), ts('New Team Created'));
     } else {
         CRM_Core_Session::setStatus(ts("Failed to Create Team \"{$orgName}\" ..."));
     }
 }
 function postProcess()
 {
     $values = $this->exportValues();
     $branchId = $values['pcp_branch_contact'];
     if ($branchId && $this->get('page_id')) {
         // $branchCfId = CRM_Pcpteams_Utils::getBranchorPartnerCustomFieldId();
         $params = array('version' => 3, 'entity_id' => $this->get('page_id'), "org_id" => $branchId);
         $result = civicrm_api3('pcpteams', 'customcreate', $params);
         if (!civicrm_error($result)) {
             $branchName = CRM_Contact_BAO_Contact::displayName($branchId);
             // Group Join: create activity
             $actParams = array('target_contact_id' => $branchId);
             CRM_Pcpteams_Utils::createPcpActivity($actParams, CRM_Pcpteams_Constant::C_AT_GROUP_JOIN);
             CRM_Core_Session::setStatus(ts("Successfully added to the branch {$branchName}"), '', 'success');
         }
     }
 }
 function postProcess()
 {
     $values = $this->exportValues();
     $tributeId = $values['pcp_tribute_contact'];
     if ($tributeId && $this->_tributeReason) {
         // $tributeCfId        = CRM_Pcpteams_Utils::getPcpTypeCustomFieldId();
         // $tributeContactCfId = CRM_Pcpteams_Utils::getPcpTypeContactCustomFieldId();
         $selectedReason = CRM_Core_OptionGroup::getValue(CRM_Pcpteams_Constant::C_PCP_TYPE, $this->_tributeReason, 'name');
         $tributeContatparams = array('version' => 3, 'entity_id' => $this->get('page_id'), "tribute" => $selectedReason, "tribute_contact_id" => $tributeId);
         $result = civicrm_api3('pcpteams', 'customcreate', $tributeContatparams);
         if (!civicrm_error($result)) {
             $tributeName = CRM_Contact_BAO_Contact::displayName($tributeId);
             // Tribute Join: create activity
             $actParams = array('target_contact_id' => $tributeId, 'reason' => $this->_tributeReason);
             CRM_Pcpteams_Utils::createPcpActivity($actParams, CRM_Pcpteams_Constant::C_AT_TRIBUTE_JOIN);
             CRM_Core_Session::setStatus(ts("Successfully added to {$this->_tributeReason} of {$tributeName}"), '', 'success');
         }
     }
     //FIXME: need to discuss with DS, to redirect the after completed the form entries
     CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/pcp/page', 'reset=1&id=' . $this->get('page_id')));
 }
 static function withdrawJoinRequest()
 {
     $entity_id = CRM_Utils_Type::escape($_POST['entity_id'], 'Integer');
     $op = CRM_Utils_Type::escape($_POST['op'], 'String');
     $pcp_id = CRM_Utils_Type::escape($_POST['pcp_id'], 'Integer');
     $team_pcp_id = CRM_Utils_Type::escape($_POST['team_pcp_id'], 'Integer');
     $targetId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Relationship', $entity_id, 'contact_id_b');
     $userID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Relationship', $entity_id, 'contact_id_a');
     //check user permission
     if (!CRM_Pcpteams_Utils::hasPermission($pcp_id, $userID)) {
         CRM_Core_Session::setStatus(ts("Sorry! You dont have right permission to withdraw this request"));
         CRM_Utils_System::civiExit();
     }
     $teamAdminID = CRM_Pcpteams_Utils::getTeamAdmin($team_pcp_id);
     $getUserPcpQuery = "SELECT pcp_a_b FROM civicrm_value_pcp_relationship_set WHERE entity_id = {$entity_id}";
     $userPcpId = CRM_Core_DAO::singleValueQuery($getUserPcpQuery);
     $updatedResult = civicrm_api3('Relationship', 'delete', array('sequential' => 1, 'id' => $entity_id));
     if (!civicrm_error($updatedResult)) {
         //create Activity - Join Team Request withdraw
         $actParams = array('assignee_contact_id' => $teamAdminID, 'target_contact_id' => $targetId);
         //FIXME: Make sure the activity type., doesn't have seperate activity type for withdraw at the moment.
         CRM_Pcpteams_Utils::createPcpActivity($actParams, CRM_Pcpteams_Constant::C_AT_REQ_DECLINED);
         list($userName, $userEmail) = CRM_Contact_BAO_Contact::getContactDetails($userID);
         $contactDetails = civicrm_api('Contact', 'get', array('version' => 3, 'sequential' => 1, 'id' => $userID));
         $emailParams = array('tplParams' => array('userFirstName' => $contactDetails['values'][0]['first_name'], 'userLastName' => $contactDetails['values'][0]['last_name'], 'teamName' => CRM_Contact_BAO_Contact::displayName($targetId), 'pageURL' => CRM_Utils_System::url('civicrm/pcp/manage', "reset=1&id={$userPcpId}", TRUE, NULL, FALSE, TRUE)), 'email' => array($userName => array('first_name' => $contactDetails['values'][0]['first_name'], 'last_name' => $contactDetails['values'][0]['last_name'], 'email-Primary' => $userEmail, 'display_name' => $userName)), 'valueName' => CRM_Pcpteams_Constant::C_MSG_TPL_JOIN_REQ_DECLINE_TEAM);
         $sendEmail = CRM_Pcpteams_Utils::sendMail($teamAdminID, $emailParams);
         //end
         echo 'declined';
     } else {
         echo $updatedResult['error_message'];
     }
     CRM_Utils_System::civiExit();
 }
 function postProcess()
 {
     //return TRUE;
     $values = $this->controller->exportValues($this->_name);
     // Find the msg_tpl ID of sample invite template
     $teampcpId = CRM_Pcpteams_Utils::getPcpIdByContactAndEvent($this->get('component_page_id'), $this->get('teamContactID'));
     //FIXME : Find the solution to check is empty array of invitees
     $isInviteeEmailFound = FALSE;
     foreach ($values['friend'] as $key => $value) {
         if ($value['email']) {
             $isInviteeEmailFound = TRUE;
         }
     }
     if ($isInviteeEmailFound) {
         // Send Invitation emails
         $pcpDetails = civicrm_api('pcpteams', 'get', array('version' => 3, 'sequential' => 1, 'pcp_id' => $this->get('page_id')));
         list($userName, $userEmail) = CRM_Contact_BAO_Contact::getContactDetails($this->_contactID);
         $values['tplParams'] = array('eventName' => $pcpDetails['values'][0]['page_title'], 'userName' => $userName, 'teamName' => $this->get('teamName'), 'pageURL' => CRM_Utils_System::url('civicrm/pcp/support', "reset=1&pageId={$this->get('component_page_id')}&component=event&tpId={$this->_invitedTeamPcpID}", TRUE, NULL, FALSE, TRUE));
         // As team contact id is set in the team join post process, team contact id is not available in this form if you are coming from manage page
         $teamContactId = $this->get('teamContactID');
         if (empty($teamContactId)) {
             $teamContactId = $pcpDetails['values'][0]['contact_id'];
         }
         // Create Team Invite activity
         $actParams = array('assignee_contact_id' => $teamContactId);
         $checkAdminParams = array('version' => 3, 'user_id' => $this->_contactID, 'team_contact_id' => $teamContactId);
         $chkTeamAdmin = civicrm_api('Pcpteams', 'checkTeamAdmin', $checkAdminParams);
         $isTeamAdmin = $chkTeamAdmin['is_team_admin'];
         $teamInviteActivityType = $isTeamAdmin ? CRM_Pcpteams_Constant::C_AT_INVITATION_FROM_ADMIN : CRM_Pcpteams_Constant::C_AT_INVITATION_FROM_MEMBER;
         $activity = CRM_Pcpteams_Utils::createPcpActivity($actParams, $teamInviteActivityType);
         $result = CRM_Pcpteams_Utils::sendInviteEmail(CRM_Pcpteams_Constant::C_MSG_TPL_INVITE_TEAM, $this->_contactID, $values, $teampcpId, $activity['id']);
         if ($result) {
             if ($_GET['snippet']) {
                 // from pcp edit screen
                 CRM_Core_Session::setStatus(ts('Invitation request(s) has been sent'), ts('Invite Team'));
             } else {
                 // from workflow (create team)
                 CRM_Core_Session::setStatus(ts('Your team has been created. We have emailed your team mates asking them to join you. You will receive and email when each of them accepts your invite Invitation request(s) has been sent'), ts('Team Join Request(s) Sent'));
             }
         } else {
             if ($_GET['snippet']) {
                 CRM_Core_Session::setStatus(ts('No invitation request was sent.'), ts('No Invites'));
             } else {
                 // from workflow (create team)
                 CRM_Core_Session::setStatus(ts('Your team has been created. You can invite members from your team page.'), ts('Invite Members From Your Team Page'));
             }
         }
     }
 }