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 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();
 }