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);
 }
function pcpteams_civicrm_custom($op, $groupID, $entityID, &$params)
{
    if ($op != 'create' && $op != 'edit') {
        return;
    }
    $customFields = array();
    if ($groupID == CRM_Pcpteams_Utils::getPcpCustomSetId()) {
        foreach ($params as $key => $value) {
            $customFields[$value['column_name']] = $value['value'];
        }
        $teamContactId = CRM_Pcpteams_Utils::getcontactIdbyPcpId($entityID);
        if ('Team' == CRM_Pcpteams_Utils::checkPcpType($entityID)) {
            $cfpcpab = CRM_Pcpteams_Utils::getPcpABCustomFieldId();
            $customParams = array("custom_{$cfpcpab}" => $entityID);
            CRM_Pcpteams_Utils::reCreateRelationship($teamContactId, $customFields['org_id'], CRM_Pcpteams_Constant::C_CORPORATE_REL_TYPE, $customParams);
        }
    }
}
 static function reCreateRelationship($iContactIdA, $iContactIdB, $relationshipTypeName, $custom = array())
 {
     // Delete any old relationship on changing
     $query = "\n      DELETE cr FROM civicrm_relationship cr\n      INNER JOIN civicrm_relationship_type crt ON crt.id = cr.relationship_type_id";
     $where = " WHERE crt.name_a_b = %1 AND cr.contact_id_a = %2";
     $cfpcpab = CRM_Pcpteams_Utils::getPcpABCustomFieldId();
     if (CRM_Utils_Array::value('custom_' . $cfpcpab, $custom)) {
         $query .= " INNER JOIN civicrm_value_pcp_relationship_set crs ON crs.entity_id = cr.id";
         $where .= " AND crs.pcp_a_b = %3";
     }
     $sql = $query . $where;
     $queryParams = array(1 => array(CRM_Pcpteams_Constant::C_CORPORATE_REL_TYPE, 'String'), 2 => array($iContactIdA, 'Int'), 3 => array($custom['custom_' . $cfpcpab], 'Int'));
     CRM_Core_DAO::executeQuery($sql, $queryParams);
     // Create New Relationship against Team and Coporate
     self::createRelationship($iContactIdA, $iContactIdB, $relationshipTypeName, $custom);
 }