/**
  * This function checks for duplicate relationship.
  *
  * @param array $params
  *   (reference ) an assoc array of name/value pairs.
  * @param int $id
  *   This the id of the contact whom we are adding relationship.
  * @param int $contactId
  *   This is contact id for adding relationship.
  * @param int $relationshipId
  *   This is relationship id for the contact.
  *
  * @return bool
  *   true if record exists else false
  */
 public static function checkDuplicateRelationship(&$params, $id, $contactId = 0, $relationshipId = 0)
 {
     $relationshipTypeId = CRM_Utils_Array::value('relationship_type_id', $params);
     list($type) = explode('_', $relationshipTypeId);
     $queryString = "\nSELECT id\nFROM   civicrm_relationship\nWHERE  relationship_type_id = " . CRM_Utils_Type::escape($type, 'Integer');
     /*
      * CRM-11792 - date fields from API are in ISO format, but this function
      * supports date arrays BAO has increasingly standardised to ISO format
      * so I believe this function should support ISO rather than make API
      * format it - however, need to support array format for now to avoid breakage
      * @ time of writing this function is called from Relationship::legacyCreateMultiple (twice)
      * CRM_BAO_Contact_Utils::clearCurrentEmployer (seemingly without dates)
      * CRM_Contact_Form_Task_AddToOrganization::postProcess &
      * CRM_Contact_Form_Task_AddToHousehold::postProcess
      * (I don't think the last 2 support dates but not sure
      */
     $dateFields = array('end_date', 'start_date');
     foreach ($dateFields as $dateField) {
         if (array_key_exists($dateField, $params)) {
             if (empty($params[$dateField]) || $params[$dateField] == 'null') {
                 //this is most likely coming from an api call & probably loaded
                 // from the DB to deal with some of the
                 // other myriad of excessive checks still in place both in
                 // the api & the create functions
                 $queryString .= " AND {$dateField} IS NULL";
                 continue;
             } elseif (is_array($params[$dateField])) {
                 $queryString .= " AND {$dateField} = " . CRM_Utils_Type::escape(CRM_Utils_Date::format($params[$dateField]), 'Date');
             } else {
                 $queryString .= " AND {$dateField} = " . CRM_Utils_Type::escape($params[$dateField], 'Date');
             }
         }
     }
     $queryString .= " AND ( ( contact_id_a = " . CRM_Utils_Type::escape($id, 'Integer') . " AND contact_id_b = " . CRM_Utils_Type::escape($contactId, 'Integer') . " ) OR ( contact_id_a = " . CRM_Utils_Type::escape($contactId, 'Integer') . " AND contact_id_b = " . CRM_Utils_Type::escape($id, 'Integer') . " ) ) ";
     //if caseId is provided, include it duplicate checking.
     if ($caseId = CRM_Utils_Array::value('case_id', $params)) {
         $queryString .= " AND case_id = " . CRM_Utils_Type::escape($caseId, 'Integer');
     }
     if ($relationshipId) {
         $queryString .= " AND id !=" . CRM_Utils_Type::escape($relationshipId, 'Integer');
     }
     $relationship = new CRM_Contact_BAO_Relationship();
     $relationship->query($queryString);
     while ($relationship->fetch()) {
         // Check whether the custom field values are identical.
         $result = self::checkDuplicateCustomFields($params, $relationship->id);
         if ($result) {
             $relationship->free();
             return TRUE;
         }
     }
     $relationship->free();
     return FALSE;
 }
Beispiel #2
0
/**
 * 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;
}
 /**
  * this function checks for duplicate relationship
  *
  * @param array $params (reference ) an assoc array of name/value pairs
  * @param integer $id this the id of the contact whom we are adding relationship
  * @param integer $contactId  this is contact id for adding relationship
  * @param integer $relationshipId this is relationship id for the contact
  *
  * @return boolean true if record exists else false
  * @access public
  * @static
  */
 static function checkDuplicateRelationship(&$params, $id, $contactId = 0, $relationshipId = 0)
 {
     $start_date = CRM_Utils_Date::format(CRM_Utils_Array::value('start_date', $params));
     $end_date = CRM_Utils_Date::format(CRM_Utils_Array::value('end_date', $params));
     $relationshipTypeId = CRM_Utils_Array::value('relationship_type_id', $params);
     list($type, $first, $second) = explode('_', $relationshipTypeId);
     $queryString = " SELECT id\n                         FROM   civicrm_relationship\n                         WHERE  relationship_type_id = " . CRM_Utils_Type::escape($type, 'Integer');
     if ($start_date) {
         $queryString .= " AND start_date = " . CRM_Utils_Type::escape($start_date, 'Date');
     }
     if ($end_date) {
         $queryString .= " AND end_date = " . CRM_Utils_Type::escape($end_date, 'Date');
     }
     $queryString .= " AND ( ( contact_id_a = " . CRM_Utils_Type::escape($id, 'Integer') . " AND contact_id_b = " . CRM_Utils_Type::escape($contactId, 'Integer') . " ) OR ( contact_id_a = " . CRM_Utils_Type::escape($contactId, 'Integer') . " AND contact_id_b = " . CRM_Utils_Type::escape($id, 'Integer') . " ) ) ";
     //if caseId is provided, include it duplicate checking.
     if ($caseId = CRM_Utils_Array::value('case_id', $params)) {
         $queryString .= " AND case_id = " . CRM_Utils_Type::escape($caseId, 'Integer');
     }
     if ($relationshipId) {
         $queryString .= " AND id !=" . CRM_Utils_Type::escape($relationshipId, 'Integer');
     }
     $relationship = new CRM_Contact_BAO_Relationship();
     $relationship->query($queryString);
     $relationship->fetch();
     $relationship->free();
     return $relationship->id ? TRUE : FALSE;
 }
/**
 * 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;
}
Beispiel #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;
}
/**
 * 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');
}