/**
  * Tests the project_contacts parameter to the create API, i.e., tests the
  * ability to specify at project creation the contacts related to the project.
  */
 function testCreateProjectWithContacts()
 {
     $contactId1 = $this->individualCreate();
     $contactId2 = $this->individualCreate();
     $contactId3 = $this->individualCreate();
     $projectContacts = array('volunteer_owner' => array($contactId1), 'volunteer_manager' => array($contactId2, $contactId3));
     $params = array('entity_id' => 1, 'entity_table' => 'civicrm_event', 'is_active' => 1, 'project_contacts' => $projectContacts, 'title' => 'Unit Testing for CiviVolunteer (How Meta)');
     $this->callAPIAndDocument('VolunteerProject', 'create', $params, __FUNCTION__, __FILE__);
     $bao = new CRM_Volunteer_BAO_ProjectContact();
     $bao->project_id = 1;
     $bao->relationship_type_id = CRM_Core_OptionGroup::getValue(CRM_Volunteer_BAO_ProjectContact::RELATIONSHIP_OPTION_GROUP, 'volunteer_owner', 'name');
     $this->assertEquals(count($projectContacts['volunteer_owner']), $bao->find());
     $bao->relationship_type_id = CRM_Core_OptionGroup::getValue(CRM_Volunteer_BAO_ProjectContact::RELATIONSHIP_OPTION_GROUP, 'volunteer_manager', 'name');
     $this->assertEquals(count($projectContacts['volunteer_manager']), $bao->find());
 }
/**
 * Helper function to get the default project relationships for a new project.
 *
 * @return array
 */
function _volunteerGetProjectRelationshipDefaults()
{
    $defaults = array();
    $relTypes = CRM_Core_OptionGroup::values("volunteer_project_relationship", true, FALSE, FALSE, NULL, 'name');
    $ownerType = $relTypes['volunteer_owner'];
    $managerType = $relTypes['volunteer_manager'];
    $beneficiaryType = $relTypes['volunteer_beneficiary'];
    $contactId = CRM_Core_Session::getLoggedInContactID();
    $defaults[$ownerType] = array('contact_id' => $contactId);
    $defaults[$managerType] = array('contact_id' => $contactId);
    $employerRelationshipTypeId = civicrm_api3('RelationshipType', 'getvalue', array('return' => "id", 'name_b_a' => "Employer of"));
    try {
        $result = civicrm_api3('Relationship', 'getvalue', array('return' => "contact_id_b", 'contact_id_a' => $contactId, 'relationship_type_id' => $employerRelationshipTypeId, 'is_active' => 1));
        $defaultBeneficiary = array('contact_id' => $result);
    } catch (Exception $e) {
        $domain = civicrm_api3('Domain', 'getsingle', array('current_domain' => 1));
        $defaultBeneficiary = array('contact_id' => $domain['contact_id']);
    }
    $defaults[$beneficiaryType] = $defaultBeneficiary;
    //Re-Format the defaults into the expected structure
    //each type should be an array of arrays, each one
    //containing two keys, one for contact_id, and one for read permissions
    //$defaults['type'] => array( array('contact_id' => ..., 'can_be_read_by_current_user' => ...) )git
    foreach ($defaults as $type => &$contacts) {
        foreach ($contacts as &$contact) {
            if (!is_array($contact)) {
                $contact = array("contact_id" => $contact);
            }
            $contact['can_be_read_by_current_user'] = CRM_Volunteer_BAO_ProjectContact::contactIsReadable($contact['contact_id']);
        }
    }
    return $defaults;
}
/**
 * Returns array of project contacts matching a set of one or more properties
 *
 * @param array $params  Array of one or more valid
 *                       property_name=>value pairs.
 *
 * @return array  Array of matching project contacts
 * {@getfields volunteer_project_contact_get}
 * @access public
 */
function civicrm_api3_volunteer_project_contact_get($params)
{
    $result = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
    if (!empty($result['values'])) {
        foreach ($result['values'] as &$projectContact) {
            //In some contexts we are passing 'return' => 'contact_id' in with $params
            //In this case, there is no relationship_type_id returned as part of the results set above
            //Following that, when you pass a null value into getsingle, it finds 3 results and errors out
            //This solution was created to fall back on relationship_type_id if present in
            //$params, and if not, skip loading the relationship type label.
            $rType = false;
            $rType = array_key_exists("relationship_type_id", $params) ? $params['relationship_type_id'] : $rType;
            $rType = array_key_exists("relationship_type_id", $projectContact) ? $projectContact['relationship_type_id'] : $rType;
            if ($rType) {
                $optionValue = civicrm_api3('OptionValue', 'getsingle', array('option_group_id' => CRM_Volunteer_BAO_ProjectContact::RELATIONSHIP_OPTION_GROUP, 'value' => $rType));
                $projectContact['relationship_type_label'] = $optionValue['label'];
                $projectContact['relationship_type_name'] = $optionValue['name'];
                $projectContact['can_be_read_by_current_user'] = CRM_Volunteer_BAO_ProjectContact::contactIsReadable($projectContact['contact_id']);
            }
        }
    }
    return $result;
}