/** * Clear cached current employer name. * * @param int $contactId * Contact id ( mostly individual contact id). * @param int $employerId * Contact id ( mostly organization contact id). */ public static function clearCurrentEmployer($contactId, $employerId = NULL) { $query = "UPDATE civicrm_contact\nSET organization_name=NULL, employer_id = NULL\nWHERE id={$contactId}; "; $dao = CRM_Core_DAO::executeQuery($query); // need to handle related meberships. CRM-3792 if ($employerId) { //1. disable corresponding relationship. //2. delete related membership. //get the relationship type id of "Employee of" $relTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b'); if (!$relTypeId) { CRM_Core_Error::fatal(ts("You seem to have deleted the relationship type 'Employee of'")); } $relMembershipParams['relationship_type_id'] = $relTypeId . '_a_b'; $relMembershipParams['contact_check'][$employerId] = 1; //get relationship id. if (CRM_Contact_BAO_Relationship::checkDuplicateRelationship($relMembershipParams, $contactId, $employerId)) { $relationship = new CRM_Contact_DAO_Relationship(); $relationship->contact_id_a = $contactId; $relationship->contact_id_b = $employerId; $relationship->relationship_type_id = $relTypeId; if ($relationship->find(TRUE)) { CRM_Contact_BAO_Relationship::setIsActive($relationship->id, FALSE); CRM_Contact_BAO_Relationship::relatedMemberships($contactId, $relMembershipParams, $ids = array(), CRM_Core_Action::DELETE); } $relationship->free(); } } }
/** * process the form after the input has been submitted and validated * * @access public * @return None */ public function postProcess() { require_once 'CRM/Contact/Form/Relationship.php'; // store the submitted values in an array $params = $this->controller->exportValues($this->_name); $this->set('searchDone', 0); if (CRM_Utils_Array::value('_qf_AddToOrganization_refresh', $_POST)) { $searchParams['contact_type'] = array('Organization' => 'Organization'); $searchParams['rel_contact'] = $params['name']; CRM_Contact_Form_Relationship::search($searchParams); $this->set('searchDone', 1); return; } $data = array(); //$params['relationship_type_id']='4_a_b'; $data['relationship_type_id'] = $params['relationship_type_id']; $data['is_active'] = 1; $invalid = 0; $valid = 0; $duplicate = 0; if (is_array($this->_contactIds)) { foreach ($this->_contactIds as $value) { $ids = array(); $ids['contact'] = $value; //contact b --> organization // contact a -> individual $errors = CRM_Contact_BAO_Relationship::checkValidRelationship($params, $ids, $params['contact_check']); if ($errors) { $invalid = $invalid + 1; continue; } if (CRM_Contact_BAO_Relationship::checkDuplicateRelationship($params, CRM_Utils_Array::value('contact', $ids), $params['contact_check'])) { // step 2 $duplicate++; continue; } CRM_Contact_BAO_Relationship::add($data, $ids, $params['contact_check']); $valid++; } $status = array(ts('Added Contact(s) to Organization'), ts('Total Selected Contact(s): %1', array(1 => $valid + $invalid + $duplicate))); if ($valid) { $status[] = ts('New relationship record(s) created: %1.', array(1 => $valid)) . '<br/>'; } if ($invalid) { $status[] = ts('Relationship record(s) not created due to invalid target contact type: %1.', array(1 => $invalid)) . '<br/>'; } if ($duplicate) { $status[] = ts('Relationship record(s) not created - duplicate of existing relationship: %1.', array(1 => $duplicate)) . '<br/>'; } CRM_Core_Session::setStatus($status); } }
/** * Create relationship between contacts who share an address. * * Note that currently we create relationship only for Individual contacts * Individual + Household and Individual + Orgnization * * @param int $masterAddressId * Master address id. * @param array $params * Associated array of submitted values. */ public static function processSharedAddressRelationship($masterAddressId, $params) { // get the contact type of contact being edited / created $currentContactType = CRM_Contact_BAO_Contact::getContactType($params['contact_id']); $currentContactId = $params['contact_id']; // if current contact is not of type individual return if ($currentContactType != 'Individual') { return; } // get the contact id and contact type of shared contact // check the contact type of shared contact, return if it is of type Individual $query = 'SELECT cc.id, cc.contact_type FROM civicrm_contact cc INNER JOIN civicrm_address ca ON cc.id = ca.contact_id WHERE ca.id = %1'; $dao = CRM_Core_DAO::executeQuery($query, array(1 => array($masterAddressId, 'Integer'))); $dao->fetch(); // if current contact is not of type individual return, since we don't create relationship between // 2 individuals if ($dao->contact_type == 'Individual') { return; } $sharedContactType = $dao->contact_type; $sharedContactId = $dao->id; // create relationship between ontacts who share an address if ($sharedContactType == 'Organization') { return CRM_Contact_BAO_Contact_Utils::createCurrentEmployerRelationship($currentContactId, $sharedContactId); } // get the relationship type id of "Household Member of" $relTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Household Member of', 'id', 'name_a_b'); if (!$relTypeId) { CRM_Core_Error::fatal(ts("You seem to have deleted the relationship type 'Household Member of'")); } $relParam = array('is_active' => TRUE, 'relationship_type_id' => $relTypeId, 'contact_id_a' => $currentContactId, 'contact_id_b' => $sharedContactId); // If already there is a relationship record of $relParam criteria, avoid creating relationship again or else // it will casue CRM-16588 as the Duplicate Relationship Exception will revert other contact field values on update if (CRM_Contact_BAO_Relationship::checkDuplicateRelationship($relParam, $currentContactId, $sharedContactId)) { return; } try { // create relationship civicrm_api3('relationship', 'create', $relParam); } catch (CiviCRM_API3_Exception $e) { // We catch and ignore here because this has historically been a best-effort relationship create call. // presumably it could refuse due to duplication or similar and we would ignore that. } }
/** * Function to create new retaionship * * @param object $contact A valid Contact object. * * @param object $target_contact A valid Contact object * @param String $relationship_type_name A valid Relationship_type eg. Parent of etc. * @param array $ params Associative array of property name/value pairs to be inserted. See Data Model for available properties. * * @return newly created 'relationship object' object * * @access public * */ function crm_create_relationship($contact = null, $target_contact = null, $relationship_type_name, $params) { $relationTypeID = null; if (!isset($contact->id) and !isset($target_contact->id)) { return _crm_error('source or target contact object does not have contact ID'); } $sourceContact = $contact->id; $targetContact = $target_contact->id; require_once 'CRM/Contact/DAO/RelationshipType.php'; $reletionType =& new CRM_Contact_DAO_RelationshipType(); $reletionType->name_a_b = $relationship_type_name; $reletionType->find(); if ($reletionType->fetch()) { $relationTypeID = $reletionType->id; $relationTypeID .= '_a_b'; } if (!$relationTypeID) { $reletionType =& new CRM_Contact_DAO_RelationshipType(); $reletionType->name_b_a = $relationship_type_name; $reletionType->find(); if ($reletionType->fetch()) { $relationTypeID = $reletionType->id; $relationTypeID .= '_b_a'; } } if (!$relationTypeID) { return _crm_error('$relationship_type_ is not valid relationship type '); } $params['relationship_type_id'] = $relationTypeID; $ids['contact'] = $sourceContact; $params['contact_check'] = array($targetContact => $targetContact); require_once 'CRM/Contact/BAO/Relationship.php'; $errors = CRM_Contact_BAO_Relationship::checkValidRelationship($params, $ids, $targetContact); if ($errors) { return _crm_error($errors); } if (CRM_Contact_BAO_Relationship::checkDuplicateRelationship($params, $sourceContact, $targetContact)) { return _crm_error('Duplicate relationship'); } return CRM_Contact_BAO_Relationship::add($params, $ids, $targetContact); }
/** * process the form after the input has been submitted and validated * * @access public * * @return None */ public function postProcess() { // store the submitted values in an array $params = $this->controller->exportValues($this->_name); $this->set('searchDone', 0); if (CRM_Utils_Array::value('_qf_AddToOrganization_refresh', $_POST)) { $searchParams['contact_type'] = array('Organization' => 'Organization'); $searchParams['rel_contact'] = $params['name']; CRM_Contact_Form_Task_AddToHousehold::search($this, $searchParams); $this->set('searchDone', 1); return; } $data = array(); $data['relationship_type_id'] = $params['relationship_type_id']; $data['is_active'] = 1; $invalid = 0; $valid = 0; $duplicate = 0; if (is_array($this->_contactIds)) { foreach ($this->_contactIds as $value) { $ids = array(); $ids['contact'] = $value; $errors = CRM_Contact_BAO_Relationship::checkValidRelationship($params, $ids, $params['contact_check']); if ($errors) { $invalid++; continue; } if (CRM_Contact_BAO_Relationship::checkDuplicateRelationship($params, CRM_Utils_Array::value('contact', $ids), $params['contact_check'])) { $duplicate++; continue; } CRM_Contact_BAO_Relationship::add($data, $ids, $params['contact_check']); $valid++; } $org = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params['contact_check'], 'display_name'); list($rtype, $a_b) = explode('_', $data['relationship_type_id'], 2); $relationship = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $rtype, "label_{$a_b}"); $status = array(ts('%count %2 %3 relationship created', array('count' => $valid, 'plural' => '%count %2 %3 relationships created', 2 => $relationship, 3 => $org))); if ($duplicate) { $status[] = ts('%count was skipped because the contact is already %2 %3', array('count' => $duplicate, 'plural' => '%count were skipped because the contacts are already %2 %3', 2 => $relationship, 3 => $org)); } if ($invalid) { $status[] = ts('%count relationship was not created because the contact is not of the right type for this relationship', array('count' => $invalid, 'plural' => '%count relationships were not created because the contact is not of the right type for this relationship')); } $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>'; CRM_Core_Session::setStatus($status, ts('Relationship Created', array('count' => $valid, 'plural' => 'Relationships Created')), 'success', array('expires' => 0)); } }
/** * takes an associative array and creates a relationship object * * * @param array $params (reference ) an assoc array of name/value pairs * @param array $ids the array that holds all the db ids * * @return object CRM_Contact_BAO_Relationship object * @access public * @static */ function create(&$params, &$ids) { $valid = $invalid = $duplicate = $saved = 0; $relationshipId = CRM_Utils_Array::value('relationship', $ids); if (!$relationshipId) { // creating a new relationship $dataExists = CRM_Contact_BAO_Relationship::dataExists($params); if (!$dataExists) { return null; } foreach ($params['contact_check'] as $key => $value) { $errors = ''; // check if the realtionship is valid between contacts. // step 1: check if the relationship is valid if not valid skip and keep the count // step 2: check the if two contacts already have a relationship if yes skip and keep the count // step 3: if valid relationship then add the relation and keep the count $errors = CRM_Contact_BAO_Relationship::checkValidRelationship($params, $ids, $key); // step 1 if ($errors) { $invalid++; continue; } if (CRM_Contact_BAO_Relationship::checkDuplicateRelationship($params, CRM_Utils_Array::value('contact', $ids), $key)) { // step 2 $duplicate++; continue; } $relationship = CRM_Contact_BAO_Relationship::add($params, $ids, $key); $valid++; } //return array( $valid, $invalid, $duplicate, $saved ); } else { //editing the relationship // check for duplicate relationship if (CRM_Contact_BAO_Relationship::checkDuplicateRelationship($params, CRM_Utils_Array::value('contact', $ids), $ids['contactTarget'], $relationshipId)) { $duplicate++; return array($valid, $invalid, $duplicate); } // editing an existing relationship CRM_Contact_BAO_Relationship::add($params, $ids, $ids['contactTarget']); $saved++; //return array( $valid, $invalid, $duplicate, $saved ); } return array($valid, $invalid, $duplicate, $saved); }
static function createRelationship($iContactIdA, $iContactIdB, $relationshipTypeName, $custom = array(), $is_active = 1) { if (empty($iContactIdA) || empty($iContactIdB)) { $status = empty($iContactIdB) ? 'ContactIdB is Missing' : 'ContactIdA is Missing'; CRM_Core_Error::debug_var('Input Details', $status); return FALSE; } $relTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $relationshipTypeName, 'id', 'name_a_b'); if ($relTypeId) { $aParams = array(); //check the duplicates $aParams = array('version' => '3', 'is_active' => '1', 'relationship_type_id' => $relTypeId . '_a_b'); $bDuplicateFound = CRM_Contact_BAO_Relationship::checkDuplicateRelationship($aParams, $iContactIdA, $iContactIdB); if ($bDuplicateFound) { CRM_Core_Error::debug_log_message(ts('Relationship already exists.')); return TRUE; } else { $aParams['contact_id_a'] = $iContactIdA; $aParams['contact_id_b'] = $iContactIdB; $aParams['relationship_type_id'] = $relTypeId; $aParams['is_active'] = $is_active; if (!empty($custom)) { $aParams = array_merge($aParams, $custom); } $createRelationship = civicrm_api3('Relationship', 'create', $aParams); if (!civicrm_error($createRelationship)) { return TRUE; } } } return FALSE; }