/**
  * Trigger a rule for this trigger
  *
  * @param $op
  * @param $objectName
  * @param $objectId
  * @param $objectRef
  */
 public function triggerTrigger($op, $objectName, $objectId, $objectRef)
 {
     $t = $this->getTriggerDataFromPost($op, $objectName, $objectId, $objectRef);
     //trigger for each client
     $clients = CRM_Case_BAO_Case::getCaseClients($objectId);
     foreach ($clients as $client) {
         $triggerData = clone $t;
         $triggerData->setEntityData('Relationship', null);
         $triggerData->setContactId($client);
         CRM_Civirules_Engine::triggerRule($this, $triggerData);
     }
     //trigger for each case role
     $relatedContacts = CRM_Case_BAO_Case::getRelatedContacts($objectId);
     foreach ($relatedContacts as $contact) {
         $triggerData = clone $t;
         $relationshipData = null;
         $relationship = new CRM_Contact_BAO_Relationship();
         $relationship->contact_id_b = $contact['contact_id'];
         $relationship->case_id = $objectId;
         if ($relationship->find(true)) {
             CRM_Core_DAO::storeValues($relationship, $relationshipData);
         }
         $triggerData->setEntityData('Relationship', $relationshipData);
         $triggerData->setContactId($contact['contact_id']);
         CRM_Civirules_Engine::triggerRule($this, $triggerData);
     }
 }
/**
 * Delete a relationship.
 *
 * @param array $params
 *
 * @return array
 *   API Result Array
 */
function civicrm_api3_relationship_delete($params)
{
    if (!CRM_Utils_Rule::integer($params['id'])) {
        return civicrm_api3_create_error('Invalid value for relationship ID');
    }
    $relationBAO = new CRM_Contact_BAO_Relationship();
    $relationBAO->id = $params['id'];
    if (!$relationBAO->find(TRUE)) {
        return civicrm_api3_create_error('Relationship id is not valid');
    } else {
        $relationBAO->del($params['id']);
        return civicrm_api3_create_success('Deleted relationship successfully');
    }
}
/**
 * Get contact membership record.
 * 
 * This api is used for finding an existing membership record.
 * This api will also return the mebership records for the contacts
 * having mebership based on the relationship with the direct members.
 * 
 * @param  Array $params key/value pairs for contact_id and some
 *          options affecting the desired results; has legacy support
 *          for just passing the contact_id itself as the argument
 *
 * @return  Array of all found membership property values.
 * @access public
 */
function civicrm_membership_contact_get(&$params)
{
    _civicrm_initialize();
    $activeOnly = false;
    if (is_array($params)) {
        $contactID = CRM_Utils_Array::value('contact_id', $params);
        $activeOnly = CRM_Utils_Array::value('active_only', $params, false);
        if ($activeOnly == 1) {
            $activeOnly = true;
        } else {
            $activeOnly = false;
        }
    } elseif (CRM_Utils_Rule::integer($params)) {
        $contactID = $params;
    } else {
        return civicrm_create_error('Paramers can be only of type array or integer');
    }
    if (empty($contactID)) {
        return civicrm_create_error('Invalid value for ContactID.');
    }
    // get the membership for the given contact ID
    require_once 'CRM/Member/BAO/Membership.php';
    $membership = array('contact_id' => $contactID);
    $membershipValues = array();
    CRM_Member_BAO_Membership::getValues($membership, $membershipValues, $activeOnly);
    $recordCount = 0;
    if (empty($membershipValues)) {
        # No results is NOT an error!
        # return civicrm_create_error('No memberships for this contact.');
        $membershipValues['record_count'] = $recordCount;
        return $membershipValues;
    }
    $members[$contactID] = array();
    $relationships = array();
    foreach ($membershipValues as $membershipId => $values) {
        // populate the membership type name for the membership type id
        require_once 'CRM/Member/BAO/MembershipType.php';
        $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($values['membership_type_id']);
        $membershipValues[$membershipId]['membership_name'] = $membershipType['name'];
        if (CRM_Utils_Array::value('relationship_type_id', $membershipType)) {
            $relationships[$membershipType['relationship_type_id']] = $membershipId;
        }
        // populating relationship type name.
        require_once 'CRM/Contact/BAO/RelationshipType.php';
        $relationshipType = new CRM_Contact_BAO_RelationshipType();
        $relationshipType->id = CRM_Utils_Array::value('relationship_type_id', $membershipType);
        if ($relationshipType->find(true)) {
            $membershipValues[$membershipId]['relationship_name'] = $relationshipType->name_a_b;
        }
        require_once 'CRM/Core/BAO/CustomGroup.php';
        $groupTree =& CRM_Core_BAO_CustomGroup::getTree('Membership', CRM_Core_DAO::$_nullObject, $membershipId, false, $values['membership_type_id']);
        $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1, CRM_Core_DAO::$_nullObject);
        $defaults = array();
        CRM_Core_BAO_CustomGroup::setDefaults($groupTree, $defaults);
        if (!empty($defaults)) {
            foreach ($defaults as $key => $val) {
                $membershipValues[$membershipId][$key] = $val;
            }
        }
        $recordCount++;
    }
    $members[$contactID] = $membershipValues;
    // populating contacts in members array based on their relationship with direct members.
    require_once 'CRM/Contact/BAO/Relationship.php';
    if (!empty($relationships)) {
        foreach ($relationships as $relTypeId => $membershipId) {
            // As members are not direct members, there should not be
            // membership id in the result array.
            unset($membershipValues[$membershipId]['id']);
            $relationship = new CRM_Contact_BAO_Relationship();
            $relationship->contact_id_b = $contactID;
            $relationship->relationship_type_id = $relTypeId;
            if ($relationship->find()) {
                while ($relationship->fetch()) {
                    clone $relationship;
                    $membershipValues[$membershipId]['contact_id'] = $relationship->contact_id_a;
                    $members[$contactID][$relationship->contact_id_a] = $membershipValues[$membershipId];
                }
            }
            $recordCount++;
        }
    }
    $members['record_count'] = $recordCount;
    return $members;
}
/**
 * non-standard behaviour inherited from v2
* @param array $params parameters passed into get function
* @return array result for calling function
*/
function _civicrm_api3_membership_relationsship_get_customv2behaviour(&$params, $membershipValues, $contactID)
{
    $relationships = array();
    foreach ($membershipValues as $membershipId => $values) {
        // populate the membership type name for the membership type id
        $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($values['membership_type_id']);
        $membershipValues[$membershipId]['membership_name'] = $membershipType['name'];
        if (!empty($membershipType['relationship_type_id'])) {
            $relationships[$membershipType['relationship_type_id']] = $membershipId;
        }
        // populating relationship type name.
        $relationshipType = new CRM_Contact_BAO_RelationshipType();
        $relationshipType->id = CRM_Utils_Array::value('relationship_type_id', $membershipType);
        if ($relationshipType->find(TRUE)) {
            $membershipValues[$membershipId]['relationship_name'] = $relationshipType->name_a_b;
        }
        _civicrm_api3_custom_data_get($membershipValues[$membershipId], 'Membership', $membershipId, NULL, $values['membership_type_id']);
    }
    $members = $membershipValues;
    // populating contacts in members array based on their relationship with direct members.
    if (!empty($relationships)) {
        foreach ($relationships as $relTypeId => $membershipId) {
            // As members are not direct members, there should not be
            // membership id in the result array.
            unset($membershipValues[$membershipId]['id']);
            $relationship = new CRM_Contact_BAO_Relationship();
            $relationship->contact_id_b = $contactID;
            $relationship->relationship_type_id = $relTypeId;
            if ($relationship->find()) {
                while ($relationship->fetch()) {
                    clone $relationship;
                    $membershipValues[$membershipId]['contact_id'] = $relationship->contact_id_a;
                    $members[$membershipId]['related_contact_id'] = $relationship->contact_id_a;
                }
            }
        }
    }
    return $members;
}
Exemple #5
0
/**
 * Get conatct membership record.
 * 
 * This api is used for finding an existing membership record.
 * This api will also return the mebership records for the contacts
 * having mebership based on the relationship with the direct members.
 * 
 * @param  Int  $contactID  ID of a contact
 *
 * @return  Array of all found membership property values.
 * @access public
 */
function crm_get_contact_memberships($contactID)
{
    _crm_initialize();
    if (empty($contactID)) {
        return _crm_error('Invalid value for ContactID.');
    }
    // get the membership for the given contact ID
    require_once 'CRM/Member/BAO/Membership.php';
    $membership = array('contact_id' => $contactID);
    $membershipValues = array();
    CRM_Member_BAO_Membership::getValues($membership, $membershipValues);
    if (empty($membershipValues)) {
        return _crm_error('No memberships for this contact.');
    }
    foreach ($membershipValues as $membershipId => $values) {
        // populate the membership type name for the membership type id
        require_once 'CRM/Member/BAO/MembershipType.php';
        $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($values['membership_type_id']);
        $membershipValues[$membershipId]['membership_name'] = $membershipType['name'];
        $relationships[$membershipType['relationship_type_id']] = $membershipId;
        // populating relationship type name.
        require_once 'CRM/Contact/BAO/RelationshipType.php';
        $relationshipType = new CRM_Contact_BAO_RelationshipType();
        $relationshipType->id = $membershipType['relationship_type_id'];
        if ($relationshipType->find(true)) {
            $membershipValues[$membershipId]['relationship_name'] = $relationshipType->name_a_b;
        }
    }
    $members[$contactID] = $membershipValues;
    // populating contacts in members array based on their relationship with direct members.
    require_once 'CRM/Contact/BAO/Relationship.php';
    foreach ($relationships as $relTypeId => $membershipId) {
        // As members are not direct members, there should not be
        // membership id in the result array.
        unset($membershipValues[$membershipId]['id']);
        $relationship = new CRM_Contact_BAO_Relationship();
        $relationship->contact_id_b = $contactID;
        $relationship->relationship_type_id = $relTypeId;
        if ($relationship->find()) {
            while ($relationship->fetch()) {
                clone $relationship;
                $membershipValues[$membershipId]['contact_id'] = $relationship->contact_id_a;
                $members[$contactID][$relationship->contact_id_a] = $membershipValues[$membershipId];
            }
        }
    }
    return $members;
}
/**
 * take the input parameter list as specified in the data model and
 * convert it into the same format that we use in QF and BAO object
 *
 * @param array $params Associative array of property name/value
 *                             pairs to insert in new contact.
 * @param array $values The reformatted properties that we can use internally
 *                            '
 *
 * @throws Exception
 * @return array|CRM_Error
 * @access public
 */
function _civicrm_api3_relationship_format_params($params, &$values)
{
    // copy all the relationship fields as is
    $fields = CRM_Contact_DAO_Relationship::fields();
    _civicrm_api3_store_values($fields, $params, $values);
    $relationTypes = CRM_Core_PseudoConstant::relationshipType('name');
    if (!empty($params['id'])) {
        $relation = new CRM_Contact_BAO_Relationship();
        $relation->id = $params['id'];
        if (!$relation->find(TRUE)) {
            throw new Exception('Relationship id is not valid');
        } else {
            if (isset($params['contact_id_a']) && $params['contact_id_a'] != $relation->contact_id_a || isset($params['contact_id_b']) && $params['contact_id_b'] != $relation->contact_id_b) {
                throw new Exception('Cannot change the contacts once relationship has been created');
            } else {
                // since the BAO function is not std & won't accept just 'id' (aargh) let's
                // at least return our BAO here
                $values = array();
                _civicrm_api3_object_to_array($relation, $values);
                $values = array_merge($values, $params);
                // and we need to reformat our date fields....
                $dateFields = array('start_date', 'end_date');
                foreach ($dateFields as $dateField) {
                    $values[$dateField] = CRM_Utils_Date::processDate($values[$dateField]);
                }
            }
        }
    }
    foreach ($params as $key => $value) {
        // ignore empty values or empty arrays etc
        if (CRM_Utils_System::isNull($value)) {
            continue;
        }
        switch ($key) {
            case 'contact_id_a':
            case 'contact_id_b':
                if (!CRM_Utils_Rule::integer($value)) {
                    throw new Exception("contact_id not valid: {$value}");
                }
                $dao = new CRM_Core_DAO();
                $qParams = array();
                $svq = $dao->singleValueQuery("SELECT id FROM civicrm_contact WHERE id = {$value}", $qParams);
                if (!$svq) {
                    throw new Exception("Invalid Contact ID: There is no contact record with contact_id = {$value}.");
                }
                break;
            case 'relationship_type':
                foreach ($relationTypes as $relTypId => $relValue) {
                    if (CRM_Utils_Array::key(ucfirst($value), $relValue)) {
                        $relationshipTypeId = $relTypId;
                        break;
                    }
                }
                if ($relationshipTypeId) {
                    if (!empty($values['relationship_type_id']) && $relationshipTypeId != $values['relationship_type_id']) {
                        throw new Exception('Mismatched Relationship Type and Relationship Type Id');
                    }
                    $values['relationship_type_id'] = $params['relationship_type_id'] = $relationshipTypeId;
                } else {
                    throw new Exception('Invalid Relationship Type');
                }
            case 'relationship_type_id':
                if ($key == 'relationship_type_id' && !array_key_exists($value, $relationTypes)) {
                    throw new Exception("{$key} not a valid: {$value}");
                }
                // execute for both relationship_type and relationship_type_id
                $relation = $relationTypes[$params['relationship_type_id']];
                if (!empty($params['contact_id_a']) && $relation['contact_type_a'] && $relation['contact_type_a'] != CRM_Contact_BAO_Contact::getContactType($params['contact_id_a'])) {
                    throw new Exception("Contact ID :{$params['contact_id_a']} is not of contact type {$relation['contact_type_a']}");
                }
                if (!empty($params['contact_id_b']) && $relation['contact_type_b'] && $relation['contact_type_b'] != CRM_Contact_BAO_Contact::getContactType($params['contact_id_b'])) {
                    throw new Exception("Contact ID :{$params['contact_id_b']} is not of contact type {$relation['contact_type_b']}");
                }
                break;
            default:
                break;
        }
    }
    if (array_key_exists('note', $params)) {
        $values['note'] = $params['note'];
    }
    _civicrm_api3_custom_format_params($params, $values, 'Relationship');
    return array();
}
/**
 * This function ensures that we have the right input parameters
 *
 * We also need to make sure we run all the form rules on the params list
 * to ensure that the params are valid
 *
 * @param array  $params       Associative array of property name/value
 *                             pairs to insert in new relationship.
 *
 * @return bool|CRM_Utils_Error
 * @access private
 */
function _civicrm_relationship_check_params(&$params)
{
    static $required = array('contact_id_a' => NULL, 'contact_id_b' => NULL, 'relationship_type_id' => 'relationship_type');
    // params should be an array
    if (!is_array($params)) {
        return civicrm_create_error('Input parameter is not an array');
    }
    // cannot create with empty params
    if (empty($params)) {
        return civicrm_create_error('Input Parameters empty');
    }
    // check params for validity of Relationship id
    if (CRM_Utils_Array::value('id', $params)) {
        require_once 'CRM/Contact/BAO/Relationship.php';
        $relation = new CRM_Contact_BAO_Relationship();
        $relation->id = $params['id'];
        if (!$relation->find(TRUE)) {
            return civicrm_create_error('Relationship id is not valid');
        } else {
            if ($params['contact_id_a'] != $relation->contact_id_a || $params['contact_id_b'] != $relation->contact_id_b) {
                return civicrm_create_error('Cannot change the contacts once relationship has been created');
            }
        }
    }
    $valid = TRUE;
    $error = '';
    foreach ($required as $field => $eitherField) {
        if (!CRM_Utils_Array::value($field, $params)) {
            if ($eitherField && CRM_Utils_Array::value($eitherField, $params)) {
                continue;
            }
            $valid = FALSE;
            $error .= " {$field}";
        }
    }
    if (!$valid) {
        return civicrm_create_error('Required fields not found' . $error);
    }
    return array();
}
function _civicrm_api3_relationship_check_params(&$params)
{
    // check params for validity of Relationship id
    if (CRM_Utils_Array::value('id', $params)) {
        $relation = new CRM_Contact_BAO_Relationship();
        $relation->id = $params['id'];
        if (!$relation->find(TRUE)) {
            throw new Exception('Relationship id is not valid');
        } else {
            if (isset($params['contact_id_a']) && $params['contact_id_a'] != $relation->contact_id_a || isset($params['contact_id_b']) && $params['contact_id_b'] != $relation->contact_id_b) {
                throw new Exception('Cannot change the contacts once relationship has been created');
            } else {
                // since the BAO function is not std & won't accept just 'id' (aargh) let's
                // at least return our BAO here
                $values = array();
                _civicrm_api3_object_to_array($relation, $values);
                return $values;
            }
        }
    }
    return array();
}
/**
 * Get contact membership record.
 *
 * This api will return the membership records for the contacts
 * having membership based on the relationship with the direct members.
 *
 * @param  Array $params key/value pairs for contact_id and some
 *          options affecting the desired results; has legacy support
 *          for just passing the contact_id itself as the argument
 *
 * @return  Array of all found membership property values.
 * @access public
 * @todo needs some love - basically only a get for a given contact right now
 * {@getfields membership_get}
 */
function civicrm_api3_membership_get($params)
{
    $contactID = $activeOnly = $membershipTypeId = $membershipType = NULL;
    $contactID = CRM_Utils_Array::value('contact_id', $params);
    if (is_array(CRM_Utils_Array::value('filters', $params)) && !empty($params['filters'])) {
        $activeOnly = CRM_Utils_Array::value('is_current', $params['filters'], FALSE);
    }
    $activeOnly = CRM_Utils_Array::value('active_only', $params, $activeOnly);
    //@todo replace this by handling in API layer - we should have enough info to do this
    // between pseudoconstant & fk - see comments in format_params
    $membershipTypeId = CRM_Utils_Array::value('membership_type_id', $params);
    if (!$membershipTypeId) {
        $membershipType = CRM_Utils_Array::value('membership_type', $params);
        if ($membershipType) {
            $membershipTypeId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $membershipType, 'id', 'name');
        }
    }
    if (CRM_Utils_Array::value('contact_id', $params)) {
        $membershipValues = _civicrm_api3_membership_get_customv2behaviour($params, $contactID, $membershipTypeId, $activeOnly);
    } else {
        //legacy behaviour only ever worked when contact_id passed in - use standard api function otherwise
        $membershipValues = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE);
    }
    if (empty($membershipValues)) {
        # No results is NOT an error!
        return civicrm_api3_create_success($membershipValues, $params);
    }
    $relationships = array();
    foreach ($membershipValues as $membershipId => $values) {
        // populate the membership type name for the membership type id
        $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($values['membership_type_id']);
        $membershipValues[$membershipId]['membership_name'] = $membershipType['name'];
        if (CRM_Utils_Array::value('relationship_type_id', $membershipType)) {
            $relationships[$membershipType['relationship_type_id']] = $membershipId;
        }
        // populating relationship type name.
        $relationshipType = new CRM_Contact_BAO_RelationshipType();
        $relationshipType->id = CRM_Utils_Array::value('relationship_type_id', $membershipType);
        if ($relationshipType->find(TRUE)) {
            $membershipValues[$membershipId]['relationship_name'] = $relationshipType->name_a_b;
        }
        _civicrm_api3_custom_data_get($membershipValues[$membershipId], 'Membership', $membershipId, NULL, $values['membership_type_id']);
    }
    $members = $membershipValues;
    // populating contacts in members array based on their relationship with direct members.
    if (!empty($relationships)) {
        foreach ($relationships as $relTypeId => $membershipId) {
            // As members are not direct members, there should not be
            // membership id in the result array.
            unset($membershipValues[$membershipId]['id']);
            $relationship = new CRM_Contact_BAO_Relationship();
            $relationship->contact_id_b = $contactID;
            $relationship->relationship_type_id = $relTypeId;
            if ($relationship->find()) {
                while ($relationship->fetch()) {
                    clone $relationship;
                    $membershipValues[$membershipId]['contact_id'] = $relationship->contact_id_a;
                    $members[$membershipId]['related_contact_id'] = $relationship->contact_id_a;
                }
            }
        }
    }
    return civicrm_api3_create_success($members, $params, 'membership', 'get');
}