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 preProcess()
 {
     $workflow = $this->get('workflowTeam');
     $teamTitle = $this->get('teamName');
     $this->assign('teamTitle', $teamTitle);
     $this->assign('workflow', $workflow);
     $this->_contactID = CRM_Pcpteams_Utils::getloggedInUserId();
     if (!$this->get('page_id')) {
         CRM_Core_Error::fatal(ts("Can't determine pcp id."));
     }
 }
 function preProcess()
 {
     parent::preProcess();
     $workflowTeam = CRM_Utils_Request::retrieve('op', 'String');
     $page_id = CRM_Utils_Request::retrieve('id', 'Positive');
     $component_page_id = CRM_Utils_Request::retrieve('pageId', 'Positive');
     $snippet = CRM_Utils_Request::retrieve('snippet', 'String');
     $this->set('component_page_id', $component_page_id);
     $this->set('page_id', $page_id);
     if ($workflowTeam) {
         $this->_reactToFile = $this->getTeamReactFile($workflowTeam);
     }
     if ($workflowTeam == 'invite') {
         $this->_contactID = CRM_Pcpteams_Utils::getloggedInUserId();
         //team contactName
         $teamContactId = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $page_id, 'contact_id');
         $teamName = CRM_Contact_BAO_Contact::displayName($teamContactId);
         $this->set('teamName', $teamName);
     }
     $className = 'CRM_Pcpteams_Form_' . $this->_reactToFile;
     $className::preProcess($this);
     $this->assign('reactClass', $this->_reactToFile);
     $this->assign('snippet', $snippet);
 }
 static function hasPermission($pcpId = NULL, $contactId = NULL, $action = CRM_Core_Permission::EDIT, $teamPcpId = NULL)
 {
     if (empty($pcpId)) {
         if ($contactId) {
             if ($action == CRM_Core_Permission::VIEW) {
                 // since get api is open now, we allow viewing member details
                 return TRUE;
             } else {
                 return $contactId == CRM_Pcpteams_Utils::getloggedInUserId() ? true : CRM_Contact_BAO_Contact_Permission::allow($contactId, $action);
             }
         }
         return FALSE;
     }
     $pcpOwnerContactId = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $pcpId, 'contact_id');
     $hasPermission = FALSE;
     if (empty($contactId)) {
         $contactId = CRM_Pcpteams_Utils::getloggedInUserId();
     }
     // Check the pcp page which he is looking is the owner of pcp, then allow 'edit' permission
     if ($pcpOwnerContactId == $contactId) {
         return TRUE;
     } else {
         if ($action == CRM_Core_Permission::VIEW) {
             // Since PCP get api is opened, as long as pcpId is available then allow view permission
             if ($pcpId) {
                 return TRUE;
             }
             //CASE 1: IF logged in user is trying to view team member's pcp page
             //CASE 1A: get all team pcps for logged in user
             $getUserTeamQuery = "\n        SELECT cps.team_pcp_id FROM civicrm_value_pcp_custom_set cps \n        INNER JOIN civicrm_pcp cp ON (cp.id = cps.entity_id)\n        WHERE cp.contact_id = %1 AND cps.team_pcp_id IS NOT NULL\n      ";
             $getUserTeamPcpDAO = CRM_Core_DAO::executeQuery($getUserTeamQuery, array(1 => array($contactId, 'Integer')));
             $userTeamPcps = array();
             while ($getUserTeamPcpDAO->fetch()) {
                 //CASE 2: IF logged in user is admin OR member of pcp being viewed
                 if ($getUserTeamPcpDAO->team_pcp_id == $pcpId) {
                     return TRUE;
                 }
                 $userTeamPcps[] = $getUserTeamPcpDAO->team_pcp_id;
             }
             //CASE 1B: IF pcp being viewed is related to team-pcp via custom teamp-pcp-id OR under approval relationship
             if (!empty($userTeamPcps)) {
                 $userTeamPcpIds = implode(', ', $userTeamPcps);
                 $memberQuery = "\n          SELECT cp.id\n          FROM civicrm_pcp cp\n          LEFT JOIN civicrm_value_pcp_custom_set cpcs ON (cp.id = cpcs.entity_id)\n          LEFT JOIN civicrm_value_pcp_relationship_set crcs ON (cp.id = crcs.pcp_a_b)\n          WHERE (cpcs.entity_id = %1 AND cpcs.team_pcp_id IN ({$userTeamPcpIds})) OR ( crcs.pcp_a_b = %1 AND crcs.pcp_b_a IN ({$userTeamPcpIds}))\n        ";
                 $memberPcp = CRM_Core_DAO::singleValueQuery($memberQuery, array(1 => array($pcpId, 'Integer')));
                 if ($memberPcp) {
                     return TRUE;
                 }
             }
             //CASE 3: IF pcp being viewed has been requested to be joined by logged in user (under approval)
             $relQuery = "\n        SELECT cr.id \n        FROM civicrm_relationship cr\n        INNER JOIN civicrm_value_pcp_relationship_set crcs ON (cr.id = crcs.entity_id) \n        WHERE cr.contact_id_a = %1 AND cr.contact_id_b = %2 AND cr.relationship_type_id = %3 AND crcs.pcp_b_a = %4\n      ";
             $relTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', CRM_Pcpteams_Constant::C_TEAM_RELATIONSHIP_TYPE, 'id', 'name_a_b');
             $relQueryParams = array(1 => array($contactId, 'Integer'), 2 => array($pcpOwnerContactId, 'Integer'), 3 => array($relTypeId, 'Integer'), 4 => array($pcpId, 'Integer'));
             if (CRM_Core_DAO::singleValueQuery($relQuery, $relQueryParams)) {
                 return TRUE;
             }
             //CASE 4: if admin is trying to view the pcp
             if (CRM_Contact_BAO_Contact_Permission::allow($pcpOwnerContactId, CRM_Core_Permission::VIEW)) {
                 return TRUE;
             }
         } else {
             if ($action == CRM_Pcpteams_Constant::C_PERMISSION_MEMBER) {
                 if ($pcpId && $teamPcpId) {
                     //check pcp custom set
                     $queryParams = array(1 => array($pcpId, 'Integer'), 2 => array($teamPcpId, 'Integer'));
                     $query = "\n          SELECT id FROM civicrm_value_pcp_custom_set \n          WHERE entity_id = %1 AND team_pcp_id = %2\n        ";
                     $teamMemberExists = CRM_Core_Dao::singleValueQuery($query, $queryParams);
                     if ($teamMemberExists) {
                         return TRUE;
                     }
                     //check pcp relationship custom set
                     $query = "\n        SELECT id FROM civicrm_value_pcp_relationship_set\n        WHERE pcp_a_b = %1 AND pcp_b_a = %2\n        ";
                     $teamMemberExists = CRM_Core_Dao::singleValueQuery($query, $queryParams);
                     if ($teamMemberExists) {
                         return TRUE;
                     }
                 } else {
                     if ($pcpId && $contactId) {
                         $query = "\n          SELECT cs.id FROM civicrm_value_pcp_custom_set cs\n          INNER JOIN civicrm_pcp cp ON cp.id = cs.entity_id \n          INNER JOIN civicrm_contact cc ON cc.id = cp.contact_id\n          WHERE cs.team_pcp_id = %1 AND cc.id = %2\n        ";
                         $queryParams = array(1 => array($pcpId, 'Integer'), 2 => array($contactId, 'Integer'));
                         $teamMemberExists = CRM_Core_Dao::executeQuery($query, $queryParams);
                         if ($teamMemberExists->fetch()) {
                             return TRUE;
                         }
                     }
                 }
                 return FALSE;
             } else {
                 if ($action == CRM_Pcpteams_Constant::C_PERMISSION_TEAM_ADMIN) {
                     if ($pcpId && $contactId) {
                         $query = "\n          SELECT cs.id FROM civicrm_value_pcp_custom_set cs\n          INNER JOIN civicrm_pcp mp ON mp.id = cs.entity_id\n          INNER JOIN civicrm_pcp tp ON tp.id = cs.team_pcp_id\n          INNER JOIN civicrm_contact tc ON tc.id = tp.contact_id\n          INNER JOIN civicrm_relationship cr ON cr.contact_id_b = tc.id\n          INNER JOIN civicrm_relationship_type crt on crt.id = cr.relationship_type_id\n          WHERE cs.entity_id = %1 AND cr.contact_id_a = %2 AND crt.name_a_b = %3";
                         $queryParams = array(1 => array($pcpId, 'Integer'), 2 => array($contactId, 'Integer'), 3 => array(CRM_Pcpteams_Constant::C_TEAM_ADMIN_REL_TYPE, 'String'));
                         if (CRM_Core_DAO::singleValueQuery($query, $queryParams)) {
                             return TRUE;
                         }
                         if (CRM_Contact_BAO_Contact_Permission::allow($contactId, CRM_Core_Permission::EDIT)) {
                             return TRUE;
                         }
                     }
                 } else {
                     if ($action == CRM_Core_Permission::EDIT) {
                         // A. if logged in user ($contactId) is owner of pcp ($pcpId) it should have returned true in the beginning.
                         // B. at this point we checking if logged in user ($contactId) is admin for team-contact ($pcpOwnerContactId) of pcp ($pcpId)
                         $query = "\n        SELECT cr.id FROM civicrm_relationship cr\n        INNER JOIN civicrm_relationship_type crt ON (crt.id = cr.relationship_type_id)\n        WHERE cr.contact_id_a = %1 AND cr.contact_id_b = %2 AND cr.is_active = %3 AND crt.name_a_b = %4";
                         $queryParams = array(1 => array($contactId, 'Integer'), 2 => array($pcpOwnerContactId, 'Integer'), 3 => array(1, 'Integer'), 4 => array(CRM_Pcpteams_Constant::C_TEAM_ADMIN_REL_TYPE, 'String'));
                         if (CRM_Core_DAO::singleValueQuery($query, $queryParams)) {
                             return TRUE;
                         }
                         if (CRM_Contact_BAO_Contact_Permission::allow($pcpOwnerContactId, CRM_Core_Permission::EDIT)) {
                             return TRUE;
                         }
                     }
                 }
             }
         }
     }
     return FALSE;
 }