/** * 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; }
/** * 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; }