3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * Class CRM_Contact_BAO_Relationship.
31 class CRM_Contact_BAO_Relationship
extends CRM_Contact_DAO_Relationship
{
34 * Various constants to indicate different type of relationships.
38 const ALL
= 0, PAST
= 1, DISABLED
= 2, CURRENT
= 4, INACTIVE
= 8;
41 * Create function - use the API instead.
43 * Note that the previous create function has been renamed 'legacyCreateMultiple'
44 * and this is new in 4.6
45 * All existing calls have been changed to legacyCreateMultiple except the api call - however, it is recommended
46 * that you call that as the end to end testing here is based on the api & refactoring may still be done.
48 * @param array $params
50 * @return \CRM_Contact_BAO_Relationship
51 * @throws \CRM_Core_Exception
53 public static function create(&$params) {
55 $extendedParams = self
::loadExistingRelationshipDetails($params);
56 // When id is specified we always wan't to update, so we don't need to
57 // check for duplicate relations.
58 if (!isset($params['id']) && self
::checkDuplicateRelationship($extendedParams, $extendedParams['contact_id_a'], $extendedParams['contact_id_b'], CRM_Utils_Array
::value('id', $extendedParams, 0))) {
59 throw new CRM_Core_Exception('Duplicate Relationship');
61 $params = $extendedParams;
62 if (self
::checkValidRelationship($params, $params, 0)) {
63 throw new CRM_Core_Exception('Invalid Relationship');
65 $relationship = self
::add($params);
66 if (!empty($params['contact_id_a'])) {
68 'contactTarget' => $relationship->contact_id_b
,
69 'contact' => $params['contact_id_a'],
72 //CRM-16087 removed additional call to function relatedMemberships which is already called by disableEnableRelationship
73 //resulting in membership being created twice
74 if (array_key_exists('is_active', $params) && empty($params['is_active'])) {
75 $action = CRM_Core_Action
::DISABLE
;
79 $action = CRM_Core_Action
::ENABLE
;
82 $id = empty($params['id']) ?
$relationship->id
: $params['id'];
83 self
::disableEnableRelationship($id, $action, $params, $ids, $active);
86 if (empty($params['skipRecentView'])) {
87 self
::addRecent($params, $relationship);
94 * Create multiple relationships for one contact.
96 * The relationship details are the same for each relationship except the secondary contact
99 * @param array $params
100 * Parameters for creating multiple relationships.
101 * The parameters are the same as for relationship create function except that the non-primary
102 * end of the relationship should be an array of one or more contact IDs.
103 * @param string $primaryContactLetter
104 * a or b to denote the primary contact for this action. The secondary may be multiple contacts
105 * and should be an array.
108 * @throws \CRM_Core_Exception
110 public static function createMultiple($params, $primaryContactLetter) {
111 $secondaryContactLetter = ($primaryContactLetter == 'a') ?
'b' : 'a';
112 $secondaryContactIDs = $params['contact_id_' . $secondaryContactLetter];
113 $valid = $invalid = $duplicate = $saved = 0;
114 $relationshipIDs = array();
115 foreach ($secondaryContactIDs as $secondaryContactID) {
117 $params['contact_id_' . $secondaryContactLetter] = $secondaryContactID;
118 $relationship = civicrm_api3('relationship', 'create', $params);
119 $relationshipIDs[] = $relationship['id'];
122 catch (CiviCRM_API3_Exception
$e) {
123 switch ($e->getMessage()) {
124 case 'Duplicate Relationship':
128 case 'Invalid Relationship':
133 throw new CRM_Core_Exception('unknown relationship create error ' . $e->getMessage());
140 'invalid' => $invalid,
141 'duplicate' => $duplicate,
143 'relationship_ids' => $relationshipIDs,
148 * Takes an associative array and creates a relationship object.
149 * @deprecated For single creates use the api instead (it's tested).
150 * For multiple a new variant of this function needs to be written and migrated to as this is a bit
153 * @param array $params
154 * (reference ) an assoc array of name/value pairs.
156 * The array that holds all the db ids.
157 * per http://wiki.civicrm.org/confluence/display/CRM/Database+layer
158 * "we are moving away from the $ids param "
160 * @return CRM_Contact_BAO_Relationship
162 public static function legacyCreateMultiple(&$params, $ids = array()) {
163 $valid = $invalid = $duplicate = $saved = 0;
164 $relationships = $relationshipIds = array();
165 $relationshipId = CRM_Utils_Array
::value('relationship', $ids, CRM_Utils_Array
::value('id', $params));
167 //CRM-9015 - the hooks are called here & in add (since add doesn't call create)
168 // but in future should be tidied per ticket
169 if (empty($relationshipId)) {
176 CRM_Utils_Hook
::pre($hook, 'Relationship', $relationshipId, $params);
178 if (!$relationshipId) {
179 // creating a new relationship
180 $dataExists = self
::dataExists($params);
182 return array(FALSE, TRUE, FALSE, FALSE, NULL);
184 $relationshipIds = array();
185 foreach ($params['contact_check'] as $key => $value) {
186 // check if the relationship is valid between contacts.
187 // step 1: check if the relationship is valid if not valid skip and keep the count
188 // step 2: check the if two contacts already have a relationship if yes skip and keep the count
189 // step 3: if valid relationship then add the relation and keep the count
192 $contactFields = self
::setContactABFromIDs($params, $ids, $key);
193 $errors = self
::checkValidRelationship($contactFields, $ids, $key);
199 //CRM-16978:check duplicate relationship as per case id.
200 if ($caseId = CRM_Utils_Array
::value('case_id', $params)) {
201 $contactFields['case_id'] = $caseId;
204 self
::checkDuplicateRelationship(
206 CRM_Utils_Array
::value('contact', $ids),
215 $singleInstanceParams = array_merge($params, $contactFields);
216 $relationship = self
::add($singleInstanceParams);
217 $relationshipIds[] = $relationship->id
;
218 $relationships[$relationship->id
] = $relationship;
221 // editing the relationship
224 // check for duplicate relationship
225 // @todo this code doesn't cope well with updates - causes e-Notices.
226 // API has a lot of code to work around
227 // this but should review this code & remove the extra handling from the api
228 // it seems doubtful any of this is relevant if the contact fields & relationship
229 // type fields are not set
231 self
::checkDuplicateRelationship(
233 CRM_Utils_Array
::value('contact', $ids),
234 $ids['contactTarget'],
239 return array($valid, $invalid, $duplicate, $saved, NULL);
242 $validContacts = TRUE;
243 //validate contacts in update mode also.
244 $contactFields = self
::setContactABFromIDs($params, $ids, $ids['contactTarget']);
245 if (!empty($ids['contact']) && !empty($ids['contactTarget'])) {
246 if (self
::checkValidRelationship($contactFields, $ids, $ids['contactTarget'])) {
247 $validContacts = FALSE;
251 if ($validContacts) {
252 // editing an existing relationship
253 $singleInstanceParams = array_merge($params, $contactFields);
254 $relationship = self
::add($singleInstanceParams, $ids, $ids['contactTarget']);
255 $relationshipIds[] = $relationship->id
;
256 $relationships[$relationship->id
] = $relationship;
261 // do not add to recent items for import, CRM-4399
262 if (!(!empty($params['skipRecentView']) ||
$invalid ||
$duplicate)) {
263 self
::addRecent($params, $relationship);
266 return array($valid, $invalid, $duplicate, $saved, $relationshipIds, $relationships);
270 * This is the function that check/add if the relationship created is valid.
272 * @param array $params
273 * (reference ) an assoc array of name/value pairs.
275 * The array that holds all the db ids.
276 * @param int $contactId
277 * This is contact id for adding relationship.
279 * @return CRM_Contact_BAO_Relationship
281 public static function add(&$params, $ids = array(), $contactId = NULL) {
282 $relationshipId = CRM_Utils_Array
::value('relationship', $ids, CRM_Utils_Array
::value('id', $params));
285 if ($relationshipId) {
288 //@todo hook are called from create and add - remove one
289 CRM_Utils_Hook
::pre($hook, 'Relationship', $relationshipId, $params);
291 $relationshipTypes = CRM_Utils_Array
::value('relationship_type_id', $params);
293 // explode the string with _ to get the relationship type id
294 // and to know which contact has to be inserted in
295 // contact_id_a and which one in contact_id_b
296 list($type) = explode('_', $relationshipTypes);
298 // check if the relationship type is Head of Household then update the
299 // household's primary contact with this contact.
301 CRM_Contact_BAO_Household
::updatePrimaryContact($params['contact_id_b'], $params['contact_id_a']);
304 $relationship = new CRM_Contact_BAO_Relationship();
305 //@todo this code needs to be updated for the possibility that not all fields are set
306 // by using $relationship->copyValues($params);
308 $relationship->contact_id_b
= $params['contact_id_b'];
309 $relationship->contact_id_a
= $params['contact_id_a'];
310 $relationship->relationship_type_id
= $type;
311 $relationship->id
= $relationshipId;
313 $dateFields = array('end_date', 'start_date');
315 foreach (self
::getdefaults() as $defaultField => $defaultValue) {
316 if (isset($params[$defaultField])) {
317 if (in_array($defaultField, $dateFields)) {
318 $relationship->$defaultField = CRM_Utils_Date
::format(CRM_Utils_Array
::value($defaultField, $params));
319 if (!$relationship->$defaultField) {
320 $relationship->$defaultField = 'NULL';
324 $relationship->$defaultField = $params[$defaultField];
327 elseif (!$relationshipId) {
328 $relationship->$defaultField = $defaultValue;
332 $relationship->save();
334 // add custom field values
335 if (!empty($params['custom'])) {
336 CRM_Core_BAO_CustomValueTable
::store($params['custom'], 'civicrm_relationship', $relationship->id
);
339 $relationship->free();
341 CRM_Utils_Hook
::post($hook, 'Relationship', $relationship->id
, $relationship);
343 return $relationship;
347 * Add relationship to recent links.
349 * @param array $params
350 * @param CRM_Contact_DAO_Relationship $relationship
352 public static function addRecent($params, $relationship) {
353 $url = CRM_Utils_System
::url('civicrm/contact/view/rel',
354 "action=view&reset=1&id={$relationship->id}&cid={$relationship->contact_id_a}&context=home"
356 $session = CRM_Core_Session
::singleton();
357 $recentOther = array();
358 if (($session->get('userID') == $relationship->contact_id_a
) ||
359 CRM_Contact_BAO_Contact_Permission
::allow($relationship->contact_id_a
, CRM_Core_Permission
::EDIT
)
361 $rType = substr(CRM_Utils_Array
::value('relationship_type_id', $params), -3);
362 $recentOther = array(
363 'editUrl' => CRM_Utils_System
::url('civicrm/contact/view/rel',
364 "action=update&reset=1&id={$relationship->id}&cid={$relationship->contact_id_a}&rtype={$rType}&context=home"
366 'deleteUrl' => CRM_Utils_System
::url('civicrm/contact/view/rel',
367 "action=delete&reset=1&id={$relationship->id}&cid={$relationship->contact_id_a}&rtype={$rType}&context=home"
371 $title = CRM_Contact_BAO_Contact
::displayName($relationship->contact_id_a
) . ' (' . CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_RelationshipType',
372 $relationship->relationship_type_id
, 'label_a_b'
373 ) . ' ' . CRM_Contact_BAO_Contact
::displayName($relationship->contact_id_b
) . ')';
375 CRM_Utils_Recent
::add($title,
379 $relationship->contact_id_a
,
386 * Load contact ids and relationship type id when doing a create call if not provided.
388 * There are are various checks done in create which require this information which is optional
391 * @param array $params
392 * Parameters passed to create call.
395 * Parameters with missing fields added if required.
397 public static function loadExistingRelationshipDetails($params) {
398 if (!empty($params['contact_id_a'])
399 && !empty($params['contact_id_b'])
400 && is_numeric($params['relationship_type_id'])) {
403 if (empty($params['id'])) {
407 $fieldsToFill = array('contact_id_a', 'contact_id_b', 'relationship_type_id');
408 $result = CRM_Core_DAO
::executeQuery("SELECT " . implode(',', $fieldsToFill) . " FROM civicrm_relationship WHERE id = %1", array(
414 while ($result->fetch()) {
415 foreach ($fieldsToFill as $field) {
416 $params[$field] = !empty($params[$field]) ?
$params[$field] : $result->$field;
423 * Resolve passed in contact IDs to contact_id_a & contact_id_b.
425 * @param array $params
427 * @param null $contactID
430 * @throws \CRM_Core_Exception
432 public static function setContactABFromIDs($params, $ids = array(), $contactID = NULL) {
433 $returnFields = array();
435 // $ids['contact'] is deprecated but comes from legacyCreateMultiple function.
436 if (empty($ids['contact'])) {
437 if (!empty($params['id'])) {
438 return self
::loadExistingRelationshipDetails($params);
440 throw new CRM_Core_Exception('Cannot create relationship, insufficient contact IDs provided');
442 if (isset($params['relationship_type_id']) && !is_numeric($params['relationship_type_id'])) {
443 $relationshipTypes = CRM_Utils_Array
::value('relationship_type_id', $params);
444 list($relationshipTypeID, $first) = explode('_', $relationshipTypes);
445 $returnFields['relationship_type_id'] = $relationshipTypeID;
447 foreach (array('a', 'b') as $contactLetter) {
448 if (empty($params['contact_' . $contactLetter])) {
449 if ($first == $contactLetter) {
450 $returnFields['contact_id_' . $contactLetter] = CRM_Utils_Array
::value('contact', $ids);
453 $returnFields['contact_id_' . $contactLetter] = $contactID;
459 return $returnFields;
463 * Specify defaults for creating a relationship.
466 * array of defaults for creating relationship
468 public static function getdefaults() {
471 'is_permission_a_b' => 0,
472 'is_permission_b_a' => 0,
474 'start_date' => 'NULL',
476 'end_date' => 'NULL',
482 * Check if there is data to create the object.
484 * @param array $params
485 * (reference ) an assoc array of name/value pairs.
489 public static function dataExists(&$params) {
490 // return if no data present
491 if (!is_array(CRM_Utils_Array
::value('contact_check', $params))) {
498 * Get get list of relationship type based on the contact type.
500 * @param int $contactId
501 * This is the contact id of the current contact.
502 * @param null $contactSuffix
503 * @param string $relationshipId
504 * The id of the existing relationship if any.
505 * @param string $contactType
508 * If true returns relationship types in both the direction.
509 * @param string $column
510 * Name/label that going to retrieve from db.
511 * @param bool $biDirectional
512 * @param string $contactSubType
513 * Includes relationship types between this subtype.
514 * @param bool $onlySubTypeRelationTypes
515 * If set only subtype which is passed by $contactSubType
516 * related relationship types get return
519 * array reference of all relationship types with context to current contact.
521 public static function getContactRelationshipType(
523 $contactSuffix = NULL,
524 $relationshipId = NULL,
528 $biDirectional = TRUE,
529 $contactSubType = NULL,
530 $onlySubTypeRelationTypes = FALSE
533 $relationshipType = array();
534 $allRelationshipType = CRM_Core_PseudoConstant
::relationshipType($column);
536 $otherContactType = NULL;
537 if ($relationshipId) {
538 $relationship = new CRM_Contact_DAO_Relationship();
539 $relationship->id
= $relationshipId;
540 if ($relationship->find(TRUE)) {
541 $contact = new CRM_Contact_DAO_Contact();
542 $contact->id
= ($relationship->contact_id_a
=== $contactId) ?
$relationship->contact_id_b
: $relationship->contact_id_a
;
544 if ($contact->find(TRUE)) {
545 $otherContactType = $contact->contact_type
;
546 //CRM-5125 for contact subtype specific relationshiptypes
547 if ($contact->contact_sub_type
) {
548 $otherContactSubType = $contact->contact_sub_type
;
554 $contactSubType = array();
556 $contactType = CRM_Contact_BAO_Contact
::getContactType($contactId);
557 $contactSubType = CRM_Contact_BAO_Contact
::getContactSubType($contactId);
560 foreach ($allRelationshipType as $key => $value) {
561 // the contact type is required or matches
562 if (((!$value['contact_type_a']) ||
563 $value['contact_type_a'] == $contactType
565 // the other contact type is required or present or matches
566 ((!$value['contact_type_b']) ||
567 (!$otherContactType) ||
568 $value['contact_type_b'] == $otherContactType
570 (in_array($value['contact_sub_type_a'], $contactSubType) ||
571 (!$value['contact_sub_type_a'] && !$onlySubTypeRelationTypes)
574 $relationshipType[$key . '_a_b'] = $value["{$column}_a_b"];
577 if (((!$value['contact_type_b']) ||
578 $value['contact_type_b'] == $contactType
580 ((!$value['contact_type_a']) ||
581 (!$otherContactType) ||
582 $value['contact_type_a'] == $otherContactType
584 (in_array($value['contact_sub_type_b'], $contactSubType) ||
585 (!$value['contact_sub_type_b'] && !$onlySubTypeRelationTypes)
588 $relationshipType[$key . '_b_a'] = $value["{$column}_b_a"];
592 $relationshipType[$key . '_a_b'] = $value["{$column}_a_b"];
593 $relationshipType[$key . '_b_a'] = $value["{$column}_b_a"];
597 if ($biDirectional) {
598 $relationshipType = self
::removeRelationshipTypeDuplicates($relationshipType, $contactSuffix);
601 // sort the relationshipType in ascending order CRM-7736
602 asort($relationshipType);
603 return $relationshipType;
607 * Given a list of relationship types, return the list with duplicate types
608 * removed, being careful to retain only the duplicate which matches the given
609 * 'a_b' or 'b_a' suffix.
611 * @param array $relationshipTypeList A list of relationship types, in the format
612 * returned by self::getContactRelationshipType().
613 * @param string $suffix Either 'a_b' or 'b_a'; defaults to 'a_b'
615 * @return array The modified value of $relationshipType
617 public static function removeRelationshipTypeDuplicates($relationshipTypeList, $suffix = NULL) {
618 if (empty($suffix)) {
622 // Find those labels which are listed more than once.
623 $duplicateValues = array_diff_assoc($relationshipTypeList, array_unique($relationshipTypeList));
625 // For each duplicate label, find its keys, and remove from $relationshipType
626 // the key which does not match $suffix.
627 foreach ($duplicateValues as $value) {
628 $keys = array_keys($relationshipTypeList, $value);
629 foreach ($keys as $key) {
630 if (substr($key, -3) != $suffix) {
631 unset($relationshipTypeList[$key]);
635 return $relationshipTypeList;
639 * Delete current employer relationship.
644 * @return CRM_Contact_DAO_Relationship
646 public static function clearCurrentEmployer($id, $action) {
647 $relationship = new CRM_Contact_DAO_Relationship();
648 $relationship->id
= $id;
649 $relationship->find(TRUE);
651 //to delete relationship between household and individual \
652 //or between individual and organization
653 if (($action & CRM_Core_Action
::DISABLE
) ||
($action & CRM_Core_Action
::DELETE
)) {
654 $relTypes = CRM_Utils_Array
::index(array('name_a_b'), CRM_Core_PseudoConstant
::relationshipType('name'));
656 (isset($relTypes['Employee of']) && $relationship->relationship_type_id
== $relTypes['Employee of']['id']) ||
657 (isset ($relTypes['Household Member of']) && $relationship->relationship_type_id
== $relTypes['Household Member of']['id'])
659 $sharedContact = new CRM_Contact_DAO_Contact();
660 $sharedContact->id
= $relationship->contact_id_a
;
661 $sharedContact->find(TRUE);
664 // changed FROM "...relationship->relationship_type_id == 4..." TO "...relationship->relationship_type_id == 5..."
665 // As the system should be looking for type "employer of" (id 5) and not "sibling of" (id 4)
666 // As suggested by @davecivicrm, the employee relationship type id is fetched using the CRM_Core_DAO::getFieldValue() class and method, since these ids differ from system to system.
667 $employerRelTypeId = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b');
669 if ($relationship->relationship_type_id
== $employerRelTypeId && $relationship->contact_id_b
== $sharedContact->employer_id
) {
670 CRM_Contact_BAO_Contact_Utils
::clearCurrentEmployer($relationship->contact_id_a
);
675 return $relationship;
679 * Delete the relationship.
686 public static function del($id) {
687 // delete from relationship table
688 CRM_Utils_Hook
::pre('delete', 'Relationship', $id, CRM_Core_DAO
::$_nullArray);
690 $relationship = self
::clearCurrentEmployer($id, CRM_Core_Action
::DELETE
);
691 if (CRM_Core_Permission
::access('CiviMember')) {
692 // create $params array which isrequired to delete memberships
693 // of the related contacts.
695 'relationship_type_id' => "{$relationship->relationship_type_id}_a_b",
696 'contact_check' => array($relationship->contact_id_b
=> 1),
700 // calling relatedMemberships to delete the memberships of
702 self
::relatedMemberships($relationship->contact_id_a
,
705 CRM_Core_Action
::DELETE
,
710 $relationship->delete();
711 CRM_Core_Session
::setStatus(ts('Selected relationship has been deleted successfully.'), ts('Record Deleted'), 'success');
713 CRM_Utils_Hook
::post('delete', 'Relationship', $id, $relationship);
715 // delete the recently created Relationship
716 $relationshipRecent = array(
718 'type' => 'Relationship',
720 CRM_Utils_Recent
::del($relationshipRecent);
722 return $relationship;
726 * Disable/enable the relationship.
732 * @param array $params
734 * @param bool $active
736 public static function disableEnableRelationship($id, $action, $params = array(), $ids = array(), $active = FALSE) {
737 $relationship = self
::clearCurrentEmployer($id, $action);
740 // create $params array which is required to delete memberships
741 // of the related contacts.
742 if (empty($params)) {
744 'relationship_type_id' => "{$relationship->relationship_type_id}_a_b",
745 'contact_check' => array($relationship->contact_id_b
=> 1),
748 $contact_id_a = empty($params['contact_id_a']) ?
$relationship->contact_id_a
: $params['contact_id_a'];
749 // calling relatedMemberships to delete/add the memberships of
751 if ($action & CRM_Core_Action
::DISABLE
) {
752 CRM_Contact_BAO_Relationship
::relatedMemberships($contact_id_a,
755 CRM_Core_Action
::DELETE
,
759 elseif ($action & CRM_Core_Action
::ENABLE
) {
760 $ids['contact'] = empty($ids['contact']) ?
$contact_id_a : $ids['contact'];
761 CRM_Contact_BAO_Relationship
::relatedMemberships($contact_id_a,
764 empty($params['id']) ? CRM_Core_Action
::ADD
: CRM_Core_Action
::UPDATE
,
772 * Delete the object records that are associated with this contact.
774 * @param int $contactId
775 * Id of the contact to delete.
777 public static function deleteContact($contactId) {
778 $relationship = new CRM_Contact_DAO_Relationship();
779 $relationship->contact_id_a
= $contactId;
780 $relationship->delete();
782 $relationship = new CRM_Contact_DAO_Relationship();
783 $relationship->contact_id_b
= $contactId;
784 $relationship->delete();
786 CRM_Contact_BAO_Household
::updatePrimaryContact(NULL, $contactId);
790 * Get the other contact in a relationship.
795 * $returns returns the contact ids in the realtionship
797 * @return \CRM_Contact_DAO_Relationship
799 public static function getRelationshipByID($id) {
800 $relationship = new CRM_Contact_DAO_Relationship();
802 $relationship->id
= $id;
803 $relationship->selectAdd();
804 $relationship->selectAdd('contact_id_a, contact_id_b');
805 $relationship->find(TRUE);
807 return $relationship;
811 * Check if the relationship type selected between two contacts is correct.
813 * @param int $contact_a
815 * @param int $contact_b
817 * @param int $relationshipTypeId
818 * Relationship type id.
821 * true if it is valid relationship else false
823 public static function checkRelationshipType($contact_a, $contact_b, $relationshipTypeId) {
824 $relationshipType = new CRM_Contact_DAO_RelationshipType();
825 $relationshipType->id
= $relationshipTypeId;
826 $relationshipType->selectAdd();
827 $relationshipType->selectAdd('contact_type_a, contact_type_b, contact_sub_type_a, contact_sub_type_b');
828 if ($relationshipType->find(TRUE)) {
829 $contact_type_a = CRM_Contact_BAO_Contact
::getContactType($contact_a);
830 $contact_type_b = CRM_Contact_BAO_Contact
::getContactType($contact_b);
832 $contact_sub_type_a = CRM_Contact_BAO_Contact
::getContactSubType($contact_a);
833 $contact_sub_type_b = CRM_Contact_BAO_Contact
::getContactSubType($contact_b);
835 if (((!$relationshipType->contact_type_a
) ||
($relationshipType->contact_type_a
== $contact_type_a)) &&
836 ((!$relationshipType->contact_type_b
) ||
($relationshipType->contact_type_b
== $contact_type_b)) &&
837 ((!$relationshipType->contact_sub_type_a
) ||
(in_array($relationshipType->contact_sub_type_a
,
840 ((!$relationshipType->contact_sub_type_b
) ||
(in_array($relationshipType->contact_sub_type_b
,
854 * This function does the validtion for valid relationship.
856 * @param array $params
857 * This array contains the values there are subitted by the form.
859 * The array that holds all the db ids.
860 * @param int $contactId
861 * This is contact id for adding relationship.
865 public static function checkValidRelationship($params, $ids, $contactId) {
867 // function to check if the relationship selected is correct
868 // i.e. employer relationship can exit between Individual and Organization (not between Individual and Individual)
869 if (!CRM_Contact_BAO_Relationship
::checkRelationshipType($params['contact_id_a'], $params['contact_id_b'],
870 $params['relationship_type_id'])) {
871 $errors = 'Please select valid relationship between these two contacts.';
877 * This function checks for duplicate relationship.
879 * @param array $params
880 * (reference ) an assoc array of name/value pairs.
882 * This the id of the contact whom we are adding relationship.
883 * @param int $contactId
884 * This is contact id for adding relationship.
885 * @param int $relationshipId
886 * This is relationship id for the contact.
889 * true if record exists else false
891 public static function checkDuplicateRelationship(&$params, $id, $contactId = 0, $relationshipId = 0) {
892 $relationshipTypeId = CRM_Utils_Array
::value('relationship_type_id', $params);
893 list($type) = explode('_', $relationshipTypeId);
897 FROM civicrm_relationship
898 WHERE relationship_type_id = " . CRM_Utils_Type
::escape($type, 'Integer');
901 * CRM-11792 - date fields from API are in ISO format, but this function
902 * supports date arrays BAO has increasingly standardised to ISO format
903 * so I believe this function should support ISO rather than make API
904 * format it - however, need to support array format for now to avoid breakage
905 * @ time of writing this function is called from Relationship::legacyCreateMultiple (twice)
906 * CRM_BAO_Contact_Utils::clearCurrentEmployer (seemingly without dates)
907 * CRM_Contact_Form_Task_AddToOrganization::postProcess &
908 * CRM_Contact_Form_Task_AddToHousehold::postProcess
909 * (I don't think the last 2 support dates but not sure
912 $dateFields = array('end_date', 'start_date');
913 foreach ($dateFields as $dateField) {
914 if (array_key_exists($dateField, $params)) {
915 if (empty($params[$dateField]) ||
$params[$dateField] == 'null') {
916 //this is most likely coming from an api call & probably loaded
917 // from the DB to deal with some of the
918 // other myriad of excessive checks still in place both in
919 // the api & the create functions
920 $queryString .= " AND $dateField IS NULL";
923 elseif (is_array($params[$dateField])) {
924 $queryString .= " AND $dateField = " .
925 CRM_Utils_Type
::escape(CRM_Utils_Date
::format($params[$dateField]), 'Date');
928 $queryString .= " AND $dateField = " .
929 CRM_Utils_Type
::escape($params[$dateField], 'Date');
935 " AND ( ( contact_id_a = " . CRM_Utils_Type
::escape($id, 'Integer') .
936 " AND contact_id_b = " . CRM_Utils_Type
::escape($contactId, 'Integer') .
937 " ) OR ( contact_id_a = " . CRM_Utils_Type
::escape($contactId, 'Integer') .
938 " AND contact_id_b = " . CRM_Utils_Type
::escape($id, 'Integer') . " ) ) ";
940 //if caseId is provided, include it duplicate checking.
941 if ($caseId = CRM_Utils_Array
::value('case_id', $params)) {
942 $queryString .= " AND case_id = " . CRM_Utils_Type
::escape($caseId, 'Integer');
945 if ($relationshipId) {
946 $queryString .= " AND id !=" . CRM_Utils_Type
::escape($relationshipId, 'Integer');
949 $relationship = new CRM_Contact_BAO_Relationship();
950 $relationship->query($queryString);
951 while ($relationship->fetch()) {
952 // Check whether the custom field values are identical.
953 $result = self
::checkDuplicateCustomFields($params, $relationship->id
);
955 $relationship->free();
959 $relationship->free();
964 * this function checks whether the values of the custom fields in $params are
965 * the same as the values of the custom fields of the relation with given
968 * @param array $params (reference) an assoc array of name/value pairs
969 * @param int $relationshipId ID of an existing duplicate relation
971 * @return boolean true if custom field values are identical
975 private static function checkDuplicateCustomFields(&$params, $relationshipId) {
976 // Get the custom values of the existing relationship.
977 $existingValues = CRM_Core_BAO_CustomValueTable
::getEntityValues($relationshipId, 'Relationship');
978 // Create a similar array for the new relationship.
979 $newValues = array();
980 if (array_key_exists('custom', $params)) {
981 // $params['custom'] seems to be an array. Each value is again an array.
982 // This array contains one value (key -1), and this value seems to be
983 // an array with the information about the custom value.
984 foreach ($params['custom'] as $value) {
985 foreach ($value as $customValue) {
986 $newValues[$customValue['custom_field_id']] = $customValue['value'];
991 // Calculate difference between arrays. If the only key-value pairs
992 // that are in one array but not in the other are empty, the
993 // custom fields are considered to be equal.
994 // See https://github.com/civicrm/civicrm-core/pull/6515#issuecomment-137985667
995 $diff1 = array_diff_assoc($existingValues, $newValues);
996 $diff2 = array_diff_assoc($newValues, $existingValues);
998 return !array_filter($diff1) && !array_filter($diff2);
1002 * Update the is_active flag in the db.
1005 * Id of the database record.
1006 * @param bool $is_active
1007 * Value we want to set the is_active field.
1009 * @throws CiviCRM_API3_Exception
1011 * DAO object on success, null otherwise
1013 public static function setIsActive($id, $is_active) {
1014 // as both the create & add functions have a bunch of logic in them that
1015 // doesn't seem to cope with a normal update we will call the api which
1016 // has tested handling for this
1017 // however, a longer term solution would be to simplify the add, create & api functions
1018 // to be more standard. It is debatable @ that point whether it's better to call the BAO
1019 // direct as the api is more tested.
1020 $result = civicrm_api('relationship', 'create', array(
1022 'is_active' => $is_active,
1026 if (is_array($result) && !empty($result['is_error']) && $result['error_message'] != 'Duplicate Relationship') {
1027 throw new CiviCRM_API3_Exception($result['error_message'], CRM_Utils_Array
::value('error_code', $result, 'undefined'), $result);
1034 * Fetch a relationship object and store the values in the values array.
1036 * @param array $params
1037 * Input parameters to find object.
1038 * @param array $values
1039 * Output values of the object.
1042 * (reference) the values that could be potentially assigned to smarty
1044 public static function &getValues(&$params, &$values) {
1045 if (empty($params)) {
1050 // get the specific number of relationship or all relationships.
1051 if (!empty($params['numRelationship'])) {
1052 $v['data'] = &CRM_Contact_BAO_Relationship
::getRelationship($params['contact_id'], NULL, $params['numRelationship']);
1055 $v['data'] = CRM_Contact_BAO_Relationship
::getRelationship($params['contact_id']);
1058 // get the total count of relationships
1059 $v['totalCount'] = count($v['data']);
1061 $values['relationship']['data'] = &$v['data'];
1062 $values['relationship']['totalCount'] = &$v['totalCount'];
1068 * Helper function to form the sql for relationship retrieval.
1070 * @param int $contactId
1072 * @param int $status
1073 * (check const at top of file).
1074 * @param int $numRelationship
1075 * No of relationships to display (limit).
1077 * Get the no of relationships.
1078 * $param int $relationshipId relationship id
1079 * @param int $relationshipId
1080 * @param string $direction
1081 * The direction we are interested in a_b or b_a.
1082 * @param array $params
1083 * Array of extra values including relationship_type_id per api spec.
1086 * [select, from, where]
1088 public static function makeURLClause($contactId, $status, $numRelationship, $count, $relationshipId, $direction, $params = array()) {
1089 $select = $from = $where = '';
1093 if ($direction == 'a_b') {
1094 $select .= ' SELECT count(DISTINCT civicrm_relationship.id) as cnt1, 0 as cnt2 ';
1097 $select .= ' SELECT 0 as cnt1, count(DISTINCT civicrm_relationship.id) as cnt2 ';
1101 $select .= ' SELECT civicrm_relationship.id as civicrm_relationship_id,
1102 civicrm_contact.sort_name as sort_name,
1103 civicrm_contact.display_name as display_name,
1104 civicrm_contact.job_title as job_title,
1105 civicrm_contact.employer_id as employer_id,
1106 civicrm_contact.organization_name as organization_name,
1107 civicrm_address.street_address as street_address,
1108 civicrm_address.city as city,
1109 civicrm_address.postal_code as postal_code,
1110 civicrm_state_province.abbreviation as state,
1111 civicrm_country.name as country,
1112 civicrm_email.email as email,
1113 civicrm_contact.contact_type as contact_type,
1114 civicrm_phone.phone as phone,
1115 civicrm_contact.id as civicrm_contact_id,
1116 civicrm_relationship.contact_id_b as contact_id_b,
1117 civicrm_relationship.contact_id_a as contact_id_a,
1118 civicrm_relationship_type.id as civicrm_relationship_type_id,
1119 civicrm_relationship.start_date as start_date,
1120 civicrm_relationship.end_date as end_date,
1121 civicrm_relationship.description as description,
1122 civicrm_relationship.is_active as is_active,
1123 civicrm_relationship.is_permission_a_b as is_permission_a_b,
1124 civicrm_relationship.is_permission_b_a as is_permission_b_a,
1125 civicrm_relationship.case_id as case_id';
1127 if ($direction == 'a_b') {
1128 $select .= ', civicrm_relationship_type.label_a_b as label_a_b,
1129 civicrm_relationship_type.label_b_a as relation ';
1132 $select .= ', civicrm_relationship_type.label_a_b as label_a_b,
1133 civicrm_relationship_type.label_a_b as relation ';
1138 FROM civicrm_relationship
1139 INNER JOIN civicrm_relationship_type ON ( civicrm_relationship.relationship_type_id = civicrm_relationship_type.id )
1140 INNER JOIN civicrm_contact ";
1141 if ($direction == 'a_b') {
1142 $from .= 'ON ( civicrm_contact.id = civicrm_relationship.contact_id_a ) ';
1145 $from .= 'ON ( civicrm_contact.id = civicrm_relationship.contact_id_b ) ';
1148 LEFT JOIN civicrm_address ON (civicrm_address.contact_id = civicrm_contact.id AND civicrm_address.is_primary = 1)
1149 LEFT JOIN civicrm_phone ON (civicrm_phone.contact_id = civicrm_contact.id AND civicrm_phone.is_primary = 1)
1150 LEFT JOIN civicrm_email ON (civicrm_email.contact_id = civicrm_contact.id AND civicrm_email.is_primary = 1)
1151 LEFT JOIN civicrm_state_province ON (civicrm_address.state_province_id = civicrm_state_province.id)
1152 LEFT JOIN civicrm_country ON (civicrm_address.country_id = civicrm_country.id)
1154 $where = 'WHERE ( 1 )';
1156 if ($direction == 'a_b') {
1157 $where .= ' AND civicrm_relationship.contact_id_b = ' . CRM_Utils_Type
::escape($contactId, 'Positive');
1160 $where .= ' AND civicrm_relationship.contact_id_a = ' . CRM_Utils_Type
::escape($contactId, 'Positive') . '
1161 AND civicrm_relationship.contact_id_a != civicrm_relationship.contact_id_b ';
1164 if ($relationshipId) {
1165 $where .= ' AND civicrm_relationship.id = ' . CRM_Utils_Type
::escape($relationshipId, 'Positive');
1168 $date = date('Y-m-d');
1169 if ($status == self
::PAST
) {
1170 //this case for showing past relationship
1171 $where .= ' AND civicrm_relationship.is_active = 1 ';
1172 $where .= " AND civicrm_relationship.end_date < '" . $date . "'";
1174 elseif ($status == self
::DISABLED
) {
1175 // this case for showing disabled relationship
1176 $where .= ' AND civicrm_relationship.is_active = 0 ';
1178 elseif ($status == self
::CURRENT
) {
1179 //this case for showing current relationship
1180 $where .= ' AND civicrm_relationship.is_active = 1 ';
1181 $where .= " AND (civicrm_relationship.end_date >= '" . $date . "' OR civicrm_relationship.end_date IS NULL) ";
1183 elseif ($status == self
::INACTIVE
) {
1184 //this case for showing inactive relationships
1185 $where .= " AND (civicrm_relationship.end_date < '" . $date . "'";
1186 $where .= ' OR civicrm_relationship.is_active = 0 )';
1190 $where .= ' AND civicrm_contact.is_deleted = 0';
1191 if (!empty($params['membership_type_id']) && empty($params['relationship_type_id'])) {
1192 $where .= self
::membershipTypeToRelationshipTypes($params, $direction);
1194 if (!empty($params['relationship_type_id'])) {
1195 if (is_array($params['relationship_type_id'])) {
1196 $where .= " AND " . CRM_Core_DAO
::createSQLFilter('relationship_type_id', $params['relationship_type_id'], 'Integer');
1199 $where .= ' AND relationship_type_id = ' . CRM_Utils_Type
::escape($params['relationship_type_id'], 'Positive');
1202 if ($direction == 'a_b') {
1203 $where .= ' ) UNION ';
1209 return array($select, $from, $where);
1213 * Get a list of relationships.
1215 * @param int $contactId
1217 * @param int $status
1218 * 1: Past 2: Disabled 3: Current.
1219 * @param int $numRelationship
1220 * No of relationships to display (limit).
1222 * Get the no of relationships.
1223 * @param int $relationshipId
1224 * @param array $links
1225 * the list of links to display
1226 * @param int $permissionMask
1227 * the permission mask to be applied for the actions
1228 * @param bool $permissionedContact
1229 * to return only permissioned Contact
1230 * @param array $params
1233 * relationship records
1235 public static function getRelationship(
1237 $status = 0, $numRelationship = 0,
1238 $count = 0, $relationshipId = 0,
1239 $links = NULL, $permissionMask = NULL,
1240 $permissionedContact = FALSE,
1244 if (!$contactId && !$relationshipId) {
1248 list($select1, $from1, $where1) = self
::makeURLClause($contactId, $status, $numRelationship,
1249 $count, $relationshipId, 'a_b', $params
1251 list($select2, $from2, $where2) = self
::makeURLClause($contactId, $status, $numRelationship,
1252 $count, $relationshipId, 'b_a', $params
1255 $order = $limit = '';
1257 if (empty($params['sort'])) {
1258 $order = ' ORDER BY civicrm_relationship_type_id, sort_name ';
1261 $order = " ORDER BY {$params['sort']} ";
1265 if (!empty($params['offset']) && $params['offset'] > 0) {
1266 $offset = $params['offset'];
1269 if ($numRelationship) {
1270 $limit = " LIMIT {$offset}, $numRelationship";
1274 // building the query string
1275 $queryString = $select1 . $from1 . $where1 . $select2 . $from2 . $where2 . $order . $limit;
1277 $relationship = new CRM_Contact_DAO_Relationship();
1279 $relationship->query($queryString);
1282 $relationshipCount = 0;
1283 while ($relationship->fetch()) {
1284 $relationshipCount +
= $relationship->cnt1 +
$relationship->cnt2
;
1286 return $relationshipCount;
1291 if ($status != self
::INACTIVE
) {
1293 $mask = array_sum(array_keys($links));
1294 if ($mask & CRM_Core_Action
::DISABLE
) {
1295 $mask -= CRM_Core_Action
::DISABLE
;
1297 if ($mask & CRM_Core_Action
::ENABLE
) {
1298 $mask -= CRM_Core_Action
::ENABLE
;
1301 if ($status == self
::CURRENT
) {
1302 $mask |
= CRM_Core_Action
::DISABLE
;
1304 elseif ($status == self
::DISABLED
) {
1305 $mask |
= CRM_Core_Action
::ENABLE
;
1308 // temporary hold the value of $mask.
1312 while ($relationship->fetch()) {
1313 $rid = $relationship->civicrm_relationship_id
;
1314 $cid = $relationship->civicrm_contact_id
;
1316 if ($permissionedContact &&
1317 (!CRM_Contact_BAO_Contact_Permission
::allow($cid))
1321 if ($status != self
::INACTIVE
&& $links) {
1322 // assign the original value to $mask
1324 // display action links if $cid has edit permission for the relationship.
1325 if (!($permissionMask & CRM_Core_Permission
::EDIT
) && CRM_Contact_BAO_Contact_Permission
::allow($cid, CRM_Core_Permission
::EDIT
)) {
1326 $permissions[] = CRM_Core_Permission
::EDIT
;
1327 $permissions[] = CRM_Core_Permission
::DELETE
;
1328 $permissionMask = CRM_Core_Action
::mask($permissions);
1330 $mask = $mask & $permissionMask;
1332 $values[$rid]['id'] = $rid;
1333 $values[$rid]['cid'] = $cid;
1334 $values[$rid]['contact_id_a'] = $relationship->contact_id_a
;
1335 $values[$rid]['contact_id_b'] = $relationship->contact_id_b
;
1336 $values[$rid]['contact_type'] = $relationship->contact_type
;
1337 $values[$rid]['relationship_type_id'] = $relationship->civicrm_relationship_type_id
;
1338 $values[$rid]['relation'] = $relationship->relation
;
1339 $values[$rid]['name'] = $relationship->sort_name
;
1340 $values[$rid]['display_name'] = $relationship->display_name
;
1341 $values[$rid]['job_title'] = $relationship->job_title
;
1342 $values[$rid]['email'] = $relationship->email
;
1343 $values[$rid]['phone'] = $relationship->phone
;
1344 $values[$rid]['employer_id'] = $relationship->employer_id
;
1345 $values[$rid]['organization_name'] = $relationship->organization_name
;
1346 $values[$rid]['country'] = $relationship->country
;
1347 $values[$rid]['city'] = $relationship->city
;
1348 $values[$rid]['state'] = $relationship->state
;
1349 $values[$rid]['start_date'] = $relationship->start_date
;
1350 $values[$rid]['end_date'] = $relationship->end_date
;
1351 $values[$rid]['description'] = $relationship->description
;
1352 $values[$rid]['is_active'] = $relationship->is_active
;
1353 $values[$rid]['is_permission_a_b'] = $relationship->is_permission_a_b
;
1354 $values[$rid]['is_permission_b_a'] = $relationship->is_permission_b_a
;
1355 $values[$rid]['case_id'] = $relationship->case_id
;
1358 $values[$rid]['status'] = $status;
1361 $values[$rid]['civicrm_relationship_type_id'] = $relationship->civicrm_relationship_type_id
;
1363 if ($relationship->contact_id_a
== $contactId) {
1364 $values[$rid]['rtype'] = 'a_b';
1367 $values[$rid]['rtype'] = 'b_a';
1373 'rtype' => $values[$rid]['rtype'],
1374 'cid' => $contactId,
1375 'cbid' => $values[$rid]['cid'],
1376 'caseid' => $values[$rid]['case_id'],
1377 'clientid' => $contactId,
1380 if ($status == self
::INACTIVE
) {
1381 // setting links for inactive relationships
1382 $mask = array_sum(array_keys($links));
1383 if (!$values[$rid]['is_active']) {
1384 $mask -= CRM_Core_Action
::DISABLE
;
1387 $mask -= CRM_Core_Action
::ENABLE
;
1388 $mask -= CRM_Core_Action
::DISABLE
;
1390 $mask = $mask & $permissionMask;
1393 // Give access to manage case link by copying to MAX_ACTION index temporarily, depending on case permission of user.
1394 if ($values[$rid]['case_id']) {
1395 // Borrowed logic from CRM_Case_Page_Tab
1396 $hasCaseAccess = FALSE;
1397 if (CRM_Core_Permission
::check('access all cases and activities')) {
1398 $hasCaseAccess = TRUE;
1401 $userCases = CRM_Case_BAO_Case
::getCases(FALSE);
1402 if (array_key_exists($values[$rid]['case_id'], $userCases)) {
1403 $hasCaseAccess = TRUE;
1407 if ($hasCaseAccess) {
1408 // give access by copying to MAX_ACTION temporarily, otherwise leave at NONE which won't display
1409 $links[CRM_Core_Action
::MAX_ACTION
] = $links[CRM_Core_Action
::NONE
];
1410 $links[CRM_Core_Action
::MAX_ACTION
]['name'] = ts('Manage Case #%1', array(1 => $values[$rid]['case_id']));
1411 $links[CRM_Core_Action
::MAX_ACTION
]['class'] = 'no-popup';
1413 // Also make sure we have the right client cid since can get here from multiple relationship tabs.
1414 if ($values[$rid]['rtype'] == 'b_a') {
1415 $replace['clientid'] = $values[$rid]['cid'];
1420 $values[$rid]['action'] = CRM_Core_Action
::formLink(
1426 'relationship.selector.row',
1429 unset($links[CRM_Core_Action
::MAX_ACTION
]);
1433 $relationship->free();
1439 * Get get list of relationship type based on the target contact type.
1441 * @param string $targetContactType
1442 * It's valid contact tpye(may be Individual , Organization , Household).
1445 * array reference of all relationship types with context to current contact type .
1447 static public function getRelationType($targetContactType) {
1448 $relationshipType = array();
1449 $allRelationshipType = CRM_Core_PseudoConstant
::relationshipType();
1451 foreach ($allRelationshipType as $key => $type) {
1452 if ($type['contact_type_b'] == $targetContactType) {
1453 $relationshipType[$key . '_a_b'] = $type['label_a_b'];
1457 return $relationshipType;
1461 * Create / update / delete membership for related contacts.
1463 * This function will create/update/delete membership for related
1464 * contact based on 1) contact have active membership 2) that
1465 * membership is is extedned by the same relationship type to that
1466 * of the existing relationship.
1468 * @param int $contactId
1470 * @param array $params
1471 * array of values submitted by POST.
1474 * @param \const|int $action which action called this function
1476 * @param bool $active
1478 * @throws \CRM_Core_Exception
1480 public static function relatedMemberships($contactId, &$params, $ids, $action = CRM_Core_Action
::ADD
, $active = TRUE) {
1481 // Check the end date and set the status of the relationship
1483 $status = self
::CURRENT
;
1484 $targetContact = $targetContact = CRM_Utils_Array
::value('contact_check', $params, array());
1485 $today = date('Ymd');
1487 // If a relationship hasn't yet started, just return for now
1488 // TODO: handle edge-case of updating start_date of an existing relationship
1489 if (!empty($params['start_date'])) {
1490 $startDate = substr(CRM_Utils_Date
::format($params['start_date']), 0, 8);
1491 if ($today < $startDate) {
1496 if (!empty($params['end_date'])) {
1497 $endDate = substr(CRM_Utils_Date
::format($params['end_date']), 0, 8);
1498 if ($today > $endDate) {
1499 $status = self
::PAST
;
1503 if (($action & CRM_Core_Action
::ADD
) && ($status & self
::PAST
)) {
1504 // If relationship is PAST and action is ADD, do nothing.
1508 $rel = explode('_', $params['relationship_type_id']);
1510 $relTypeId = $rel[0];
1511 if (!empty($rel[1])) {
1512 $relDirection = "_{$rel[1]}_{$rel[2]}";
1515 // this call is coming from somewhere where the direction was resolved early on (e.g an api call)
1516 // so we can assume _a_b
1517 $relDirection = "_a_b";
1518 $targetContact = array($params['contact_id_b'] => 1);
1521 if (($action & CRM_Core_Action
::ADD
) ||
1522 ($action & CRM_Core_Action
::DELETE
)
1524 $contact = $contactId;
1526 elseif ($action & CRM_Core_Action
::UPDATE
) {
1527 $contact = $ids['contact'];
1528 $targetContact = array($ids['contactTarget'] => 1);
1531 // Build the 'values' array for
1534 // This will allow us to check if either of the contacts in
1535 // relationship have active memberships.
1540 $values[$contact] = array(
1541 'relatedContacts' => $targetContact,
1542 'relationshipTypeId' => $relTypeId,
1543 'relationshipTypeDirection' => $relDirection,
1546 if (!empty($targetContact)) {
1547 foreach ($targetContact as $cid => $donCare) {
1548 $values[$cid] = array(
1549 'relatedContacts' => array($contact => 1),
1550 'relationshipTypeId' => $relTypeId,
1553 $relTypeParams = array('id' => $relTypeId);
1554 $relTypeValues = array();
1555 CRM_Contact_BAO_RelationshipType
::retrieve($relTypeParams, $relTypeValues);
1557 if (CRM_Utils_Array
::value('name_a_b', $relTypeValues) == CRM_Utils_Array
::value('name_b_a', $relTypeValues)) {
1558 $values[$cid]['relationshipTypeDirection'] = '_a_b';
1561 $values[$cid]['relationshipTypeDirection'] = ($relDirection == '_a_b') ?
'_b_a' : '_a_b';
1566 // CRM-15829 UPDATES
1567 // If we're looking for active memberships we must consider pending (id: 5) ones too.
1568 // Hence we can't just call CRM_Member_BAO_Membership::getValues below with the active flag, is it would completely miss pending relatioships.
1569 // As suggested by @davecivicrm, the pending status id is fetched using the CRM_Member_PseudoConstant::membershipStatus() class and method, since these ids differ from system to system.
1570 $pendingStatusId = array_search('Pending', CRM_Member_PseudoConstant
::membershipStatus());
1572 $query = 'SELECT * FROM `civicrm_membership_status`';
1574 $query .= ' WHERE `is_current_member` = 1 OR `id` = %1 ';
1577 $dao = CRM_Core_DAO
::executeQuery($query, array(1 => array($pendingStatusId, 'Integer')));
1579 while ($dao->fetch()) {
1580 $membershipStatusRecordIds[$dao->id
] = $dao->id
;
1583 // Now get the active memberships for all the contacts.
1584 // If contact have any valid membership(s), then add it to
1586 foreach ($values as $cid => $subValues) {
1587 $memParams = array('contact_id' => $cid);
1588 $memberships = array();
1590 // CRM-15829 UPDATES
1591 // Since we want PENDING memberships as well, the $active flag needs to be set to false so that this will return all memberships and we can then filter the memberships based on the status IDs recieved above.
1592 CRM_Member_BAO_Membership
::getValues($memParams, $memberships, FALSE, TRUE);
1594 // CRM-15829 UPDATES
1595 // filter out the memberships returned by CRM_Member_BAO_Membership::getValues based on the status IDs fetched on line ~1462
1596 foreach ($memberships as $key => $membership) {
1598 if (!isset($memberships[$key]['status_id'])) {
1602 $membershipStatusId = $memberships[$key]['status_id'];
1603 if (!isset($membershipStatusRecordIds[$membershipStatusId])) {
1604 unset($memberships[$key]);
1608 if (empty($memberships)) {
1612 //get ownerMembershipIds for related Membership
1613 //this is to handle memberships being deleted and recreated
1614 if (!empty($memberships['owner_membership_ids'])) {
1615 $ownerMemIds[$cid] = $memberships['owner_membership_ids'];
1616 unset($memberships['owner_membership_ids']);
1619 $values[$cid]['memberships'] = $memberships;
1621 $deceasedStatusId = array_search('Deceased', CRM_Member_PseudoConstant
::membershipStatus());
1623 // done with 'values' array.
1624 // Finally add / edit / delete memberships for the related contacts
1626 foreach ($values as $cid => $details) {
1627 if (!array_key_exists('memberships', $details)) {
1631 $relatedContacts = array_keys(CRM_Utils_Array
::value('relatedContacts', $details, array()));
1632 $mainRelatedContactId = reset($relatedContacts);
1634 foreach ($details['memberships'] as $membershipId => $membershipValues) {
1635 $relTypeIds = array();
1636 if ($action & CRM_Core_Action
::DELETE
) {
1637 // Delete memberships of the related contacts only if relationship type exists for membership type
1639 SELECT relationship_type_id, relationship_direction
1640 FROM civicrm_membership_type
1641 WHERE id = {$membershipValues['membership_type_id']}";
1642 $dao = CRM_Core_DAO
::executeQuery($query);
1643 $relTypeDirs = array();
1644 while ($dao->fetch()) {
1645 $relTypeId = $dao->relationship_type_id
;
1646 $relDirection = $dao->relationship_direction
;
1648 $relTypeIds = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $relTypeId);
1649 if (in_array($values[$cid]['relationshipTypeId'], $relTypeIds
1650 //CRM-16300 check if owner membership exist for related membership
1651 ) && !empty($membershipValues['owner_membership_id']) && !empty($values[$mainRelatedContactId]['memberships'][$membershipValues['owner_membership_id']])) {
1652 CRM_Member_BAO_Membership
::deleteRelatedMemberships($membershipValues['owner_membership_id'], $membershipValues['membership_contact_id']);
1656 if (($action & CRM_Core_Action
::UPDATE
) &&
1657 ($status & self
::PAST
) &&
1658 ($membershipValues['owner_membership_id'])
1660 // If relationship is PAST and action is UPDATE
1661 // then delete the RELATED membership
1662 CRM_Member_BAO_Membership
::deleteRelatedMemberships($membershipValues['owner_membership_id'],
1663 $membershipValues['membership_contact_id']
1668 // add / edit the memberships for related
1671 // Get the Membership Type Details.
1672 $membershipType = CRM_Member_BAO_MembershipType
::getMembershipTypeDetails($membershipValues['membership_type_id']);
1673 // Check if contact's relationship type exists in membership type
1674 $relTypeDirs = array();
1675 if (!empty($membershipType['relationship_type_id'])) {
1676 $relTypeIds = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $membershipType['relationship_type_id']);
1678 if (!empty($membershipType['relationship_direction'])) {
1679 $relDirections = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $membershipType['relationship_direction']);
1681 foreach ($relTypeIds as $key => $value) {
1682 $relTypeDirs[] = $value . '_' . $relDirections[$key];
1684 $relTypeDir = $details['relationshipTypeId'] . $details['relationshipTypeDirection'];
1685 if (in_array($relTypeDir, $relTypeDirs)) {
1686 // Check if relationship being created/updated is
1687 // similar to that of membership type's
1690 $membershipValues['owner_membership_id'] = $membershipId;
1691 unset($membershipValues['id']);
1692 unset($membershipValues['membership_contact_id']);
1693 unset($membershipValues['contact_id']);
1694 unset($membershipValues['membership_id']);
1695 foreach ($details['relatedContacts'] as $relatedContactId => $donCare) {
1696 $membershipValues['contact_id'] = $relatedContactId;
1697 if ($deceasedStatusId &&
1698 CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $relatedContactId, 'is_deceased')
1700 $membershipValues['status_id'] = $deceasedStatusId;
1701 $membershipValues['skipStatusCal'] = TRUE;
1708 if (!empty($membershipValues[$dateField])) {
1709 $membershipValues[$dateField] = CRM_Utils_Date
::processDate($membershipValues[$dateField]);
1713 if ($action & CRM_Core_Action
::UPDATE
) {
1714 //if updated relationship is already related to contact don't delete existing inherited membership
1715 if (in_array($relTypeId, $relTypeIds
1716 ) && !empty($values[$relatedContactId]['memberships']) && !empty($ownerMemIds
1717 ) && in_array($membershipValues['owner_membership_id'], $ownerMemIds[$relatedContactId])) {
1721 //delete the membership record for related
1722 //contact before creating new membership record.
1723 CRM_Member_BAO_Membership
::deleteRelatedMemberships($membershipId, $relatedContactId);
1726 // check whether we have some related memberships still available
1729 FROM civicrm_membership
1730 LEFT JOIN civicrm_membership_status ON (civicrm_membership_status.id = civicrm_membership.status_id)
1731 WHERE membership_type_id = {$membershipValues['membership_type_id']} AND owner_membership_id = {$membershipValues['owner_membership_id']}
1732 AND is_current_member = 1";
1733 $result = CRM_Core_DAO
::singleValueQuery($query);
1734 if ($result < CRM_Utils_Array
::value('max_related', $membershipValues, PHP_INT_MAX
)) {
1735 CRM_Member_BAO_Membership
::create($membershipValues, CRM_Core_DAO
::$_nullArray);
1739 elseif ($action & CRM_Core_Action
::UPDATE
) {
1740 // if action is update and updated relationship do
1741 // not match with the existing
1742 // membership=>relationship then we need to
1743 // change the status of the membership record to expired for
1744 // previous relationship -- CRM-12078.
1745 // CRM-16087 we need to pass ownerMembershipId to isRelatedMembershipExpired function
1746 if (empty($params['relationship_ids']) && !empty($params['id'])) {
1747 $relIds = array($params['id']);
1750 $relIds = CRM_Utils_Array
::value('relationship_ids', $params);
1752 if (self
::isRelatedMembershipExpired($relTypeIds, $contactId, $mainRelatedContactId, $relTypeId,
1753 $relIds) && !empty($membershipValues['owner_membership_id']
1754 ) && !empty($values[$mainRelatedContactId]['memberships'][$membershipValues['owner_membership_id']])) {
1755 $membershipValues['status_id'] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipStatus', 'Expired', 'id', 'label');
1756 $type = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType', $membershipValues['membership_type_id'], 'name', 'id');
1757 CRM_Member_BAO_Membership
::add($membershipValues);
1758 CRM_Core_Session
::setStatus(ts("Inherited membership {$type} status was changed to Expired due to the change in relationship type."), ts('Record Updated'), 'alert');
1766 * Helper function to check whether the membership is expired or not.
1768 * Function takes a list of related membership types and if it is not also passed a
1769 * relationship ID of that types evaluates whether the membership status should be changed to expired.
1771 * @param array $membershipTypeRelationshipTypeIDs
1772 * Relation type IDs related to the given membership type.
1773 * @param int $contactId
1774 * @param int $mainRelatedContactId
1775 * @param int $relTypeId
1776 * @param array $relIds
1780 public static function isRelatedMembershipExpired($membershipTypeRelationshipTypeIDs, $contactId, $mainRelatedContactId, $relTypeId, $relIds) {
1781 if (empty($membershipTypeRelationshipTypeIDs) ||
in_array($relTypeId, $membershipTypeRelationshipTypeIDs)) {
1785 if (empty($relIds)) {
1789 $relParamas = array(
1790 1 => array($contactId, 'Integer'),
1791 2 => array($mainRelatedContactId, 'Integer'),
1794 if ($contactId == $mainRelatedContactId) {
1795 $recordsFound = (int) CRM_Core_DAO
::singleValueQuery("SELECT COUNT(*) FROM civicrm_relationship WHERE relationship_type_id IN ( " . implode(',', $membershipTypeRelationshipTypeIDs) . " ) AND
1796 contact_id_a IN ( %1 ) OR contact_id_b IN ( %1 ) AND id IN (" . implode(',', $relIds) . ")", $relParamas);
1797 if ($recordsFound) {
1803 $recordsFound = (int) CRM_Core_DAO
::singleValueQuery("SELECT COUNT(*) FROM civicrm_relationship WHERE relationship_type_id IN ( " . implode(',', $membershipTypeRelationshipTypeIDs) . " ) AND contact_id_a IN ( %1, %2 ) AND contact_id_b IN ( %1, %2 ) AND id NOT IN (" . implode(',', $relIds) . ")", $relParamas);
1805 if ($recordsFound) {
1813 * Get Current Employer for Contact.
1815 * @param $contactIds
1819 * array of the current employer
1821 public static function getCurrentEmployer($contactIds) {
1822 $contacts = implode(',', $contactIds);
1825 SELECT organization_name, id, employer_id
1826 FROM civicrm_contact
1827 WHERE id IN ( {$contacts} )
1830 $dao = CRM_Core_DAO
::executeQuery($query);
1831 $currentEmployer = array();
1832 while ($dao->fetch()) {
1833 $currentEmployer[$dao->id
]['org_id'] = $dao->employer_id
;
1834 $currentEmployer[$dao->id
]['org_name'] = $dao->organization_name
;
1837 return $currentEmployer;
1841 * Function to return list of permissioned contacts for a given contact and relationship type.
1843 * @param int $contactID
1844 * contact id whose permissioned contacts are to be found.
1845 * @param int $relTypeId
1846 * one or more relationship type id's.
1847 * @param string $name
1848 * @param string $contactType
1853 public static function getPermissionedContacts($contactID, $relTypeId = NULL, $name = NULL, $contactType = NULL) {
1854 $contacts = array();
1855 $args = array(1 => array($contactID, 'Integer'));
1856 $relationshipTypeClause = $contactTypeClause = '';
1859 // @todo relTypeId is only ever passed in as an int. Change this to reflect that -
1860 // probably being overly conservative by not doing so but working on stable release.
1861 $relationshipTypeClause = 'AND cr.relationship_type_id IN (%2) ';
1862 $args[2] = array($relTypeId, 'String');
1866 $contactTypeClause = ' AND cr.relationship_type_id = crt.id AND crt.contact_type_b = %3 ';
1867 $args[3] = array($contactType, 'String');
1871 SELECT cc.id as id, cc.sort_name as name
1872 FROM civicrm_relationship cr, civicrm_contact cc, civicrm_relationship_type crt
1874 cr.contact_id_a = %1 AND
1875 cr.is_permission_a_b = 1 AND
1876 IF(cr.end_date IS NULL, 1, (DATEDIFF( CURDATE( ), cr.end_date ) <= 0)) AND
1877 cr.is_active = 1 AND
1878 cc.id = cr.contact_id_b AND
1880 $relationshipTypeClause
1884 if (!empty($name)) {
1885 $name = CRM_Utils_Type
::escape($name, 'String');
1887 AND cc.sort_name LIKE '%$name%'";
1890 $dao = CRM_Core_DAO
::executeQuery($query, $args);
1891 while ($dao->fetch()) {
1892 $contacts[$dao->id
] = array(
1893 'name' => $dao->name
,
1894 'value' => $dao->id
,
1902 * Merge relationships from otherContact to mainContact.
1904 * Called during contact merge operation
1906 * @param int $mainId
1907 * Contact id of main contact record.
1908 * @param int $otherId
1909 * Contact id of record which is going to merge.
1910 * @param array $sqls
1911 * (reference) array of sql statements to append to.
1913 * @see CRM_Dedupe_Merger::cpTables()
1915 public static function mergeRelationships($mainId, $otherId, &$sqls) {
1916 // Delete circular relationships
1917 $sqls[] = "DELETE FROM civicrm_relationship
1918 WHERE (contact_id_a = $mainId AND contact_id_b = $otherId)
1919 OR (contact_id_b = $mainId AND contact_id_a = $otherId)";
1921 // Delete relationship from other contact if main contact already has that relationship
1922 $sqls[] = "DELETE r2
1923 FROM civicrm_relationship r1, civicrm_relationship r2
1924 WHERE r1.relationship_type_id = r2.relationship_type_id
1927 r1.contact_id_a = $mainId AND r2.contact_id_a = $otherId AND r1.contact_id_b = r2.contact_id_b
1928 OR r1.contact_id_b = $mainId AND r2.contact_id_b = $otherId AND r1.contact_id_a = r2.contact_id_a
1930 (r1.contact_id_a = $mainId AND r2.contact_id_b = $otherId AND r1.contact_id_b = r2.contact_id_a
1931 OR r1.contact_id_b = $mainId AND r2.contact_id_a = $otherId AND r1.contact_id_a = r2.contact_id_b)
1932 AND r1.relationship_type_id IN (SELECT id FROM civicrm_relationship_type WHERE name_b_a = name_a_b)
1936 // Move relationships
1937 $sqls[] = "UPDATE IGNORE civicrm_relationship SET contact_id_a = $mainId WHERE contact_id_a = $otherId";
1938 $sqls[] = "UPDATE IGNORE civicrm_relationship SET contact_id_b = $mainId WHERE contact_id_b = $otherId";
1940 // Move current employer id (name will get updated later)
1941 $sqls[] = "UPDATE civicrm_contact SET employer_id = $mainId WHERE employer_id = $otherId";
1945 * Set 'is_valid' field to false for all relationships whose end date is in the past, ie. are expired.
1948 * True on success, false if error is encountered.
1950 public static function disableExpiredRelationships() {
1951 $query = "SELECT id FROM civicrm_relationship WHERE is_active = 1 AND end_date < CURDATE()";
1953 $dao = CRM_Core_DAO
::executeQuery($query);
1954 while ($dao->fetch()) {
1955 $result = CRM_Contact_BAO_Relationship
::setIsActive($dao->id
, FALSE);
1956 // Result will be NULL if error occurred. We abort early if error detected.
1957 if ($result == NULL) {
1965 * Function filters the query by possible relationships for the membership type.
1967 * It is intended to be called when constructing queries for the api (reciprocal & non-reciprocal)
1968 * and to add clauses to limit the return to those relationships which COULD inherit a membership type
1969 * (as opposed to those who inherit a particular membership
1971 * @param array $params
1973 * @param null $direction
1975 * @return array|void
1977 public static function membershipTypeToRelationshipTypes(&$params, $direction = NULL) {
1978 $membershipType = civicrm_api3('membership_type', 'getsingle', array(
1979 'id' => $params['membership_type_id'],
1980 'return' => 'relationship_type_id, relationship_direction',
1982 $relationshipTypes = $membershipType['relationship_type_id'];
1983 if (empty($relationshipTypes)) {
1986 // if we don't have any contact data we can only filter on type
1987 if (empty($params['contact_id']) && empty($params['contact_id_a']) && empty($params['contact_id_a'])) {
1988 $params['relationship_type_id'] = array('IN' => $relationshipTypes);
1992 $relationshipDirections = (array) $membershipType['relationship_direction'];
1993 // if we have contact_id_a OR contact_id_b we can make a call here
1994 // if we have contact??
1995 foreach ($relationshipDirections as $index => $mtdirection) {
1996 if (isset($params['contact_id_a']) && $mtdirection == 'a_b' ||
$direction == 'a_b') {
1997 $types[] = $relationshipTypes[$index];
1999 if (isset($params['contact_id_b']) && $mtdirection == 'b_a' ||
$direction == 'b_a') {
2000 $types[] = $relationshipTypes[$index];
2003 if (!empty($types)) {
2004 $params['relationship_type_id'] = array('IN' => $types);
2006 elseif (!empty($clauses)) {
2007 return explode(' OR ', $clauses);
2010 // effectively setting it to return no results
2011 $params['relationship_type_id'] = 0;
2018 * Wrapper for contact relationship selector.
2020 * @param array $params
2021 * Associated array for params record id.
2024 * associated array of contact relationships
2026 public static function getContactRelationshipSelector(&$params) {
2027 // format the params
2028 $params['offset'] = ($params['page'] - 1) * $params['rp'];
2029 $params['sort'] = CRM_Utils_Array
::value('sortBy', $params);
2031 if ($params['context'] == 'past') {
2032 $relationshipStatus = CRM_Contact_BAO_Relationship
::INACTIVE
;
2034 elseif ($params['context'] == 'all') {
2035 $relationshipStatus = CRM_Contact_BAO_Relationship
::ALL
;
2038 $relationshipStatus = CRM_Contact_BAO_Relationship
::CURRENT
;
2041 // check logged in user for permission
2042 $page = new CRM_Core_Page();
2043 CRM_Contact_Page_View
::checkUserPermission($page, $params['contact_id']);
2044 $permissions = array($page->_permission
);
2045 if ($page->_permission
== CRM_Core_Permission
::EDIT
) {
2046 $permissions[] = CRM_Core_Permission
::DELETE
;
2048 $mask = CRM_Core_Action
::mask($permissions);
2050 $permissionedContacts = TRUE;
2051 if ($params['context'] != 'user') {
2052 $links = CRM_Contact_Page_View_Relationship
::links();
2055 $links = CRM_Contact_Page_View_UserDashBoard
::links();
2058 // get contact relationships
2059 $relationships = CRM_Contact_BAO_Relationship
::getRelationship($params['contact_id'],
2060 $relationshipStatus,
2061 $params['rp'], 0, 0,
2063 $permissionedContacts,
2067 $contactRelationships = array();
2068 $params['total'] = 0;
2069 if (!empty($relationships)) {
2070 // FIXME: we cannot directly determine total permissioned relationship, hence re-fire query
2071 $permissionedRelationships = CRM_Contact_BAO_Relationship
::getRelationship($params['contact_id'],
2072 $relationshipStatus,
2075 $permissionedContacts
2077 $params['total'] = count($permissionedRelationships);
2080 foreach ($relationships as $relationshipId => $values) {
2081 $relationship = array();
2083 $relationship['DT_RowId'] = $values['id'];
2084 $relationship['DT_RowClass'] = 'crm-entity';
2085 if ($values['is_active'] == 0) {
2086 $relationship['DT_RowClass'] .= ' disabled';
2089 $relationship['DT_RowAttr'] = array();
2090 $relationship['DT_RowAttr']['data-entity'] = 'relationship';
2091 $relationship['DT_RowAttr']['data-id'] = $values['id'];
2093 //Add image icon for related contacts: CRM-14919
2094 $icon = CRM_Contact_BAO_Contact_Utils
::getImage($values['contact_type'],
2098 $relationship['sort_name'] = $icon . ' ' . CRM_Utils_System
::href(
2100 'civicrm/contact/view',
2101 "reset=1&cid={$values['cid']}");
2103 $relationship['relation'] = CRM_Utils_System
::href(
2104 $values['relation'],
2105 'civicrm/contact/view/rel',
2106 "action=view&reset=1&cid={$values['cid']}&id={$values['id']}&rtype={$values['rtype']}");
2108 if ($params['context'] == 'current') {
2109 if (($params['contact_id'] == $values['contact_id_a'] AND $values['is_permission_a_b'] == 1) OR
2110 ($params['contact_id'] == $values['contact_id_b'] AND $values['is_permission_b_a'] == 1)
2112 $relationship['sort_name'] .= '<span id="permission-a-b" class="crm-marker permission-relationship"> *</span>';
2115 if (($values['cid'] == $values['contact_id_a'] AND $values['is_permission_a_b'] == 1) OR
2116 ($values['cid'] == $values['contact_id_b'] AND $values['is_permission_b_a'] == 1)
2118 $relationship['relation'] .= '<span id="permission-b-a" class="crm-marker permission-relationship"> *</span>';
2122 if (!empty($values['description'])) {
2123 $relationship['relation'] .= "<p class='description'>{$values['description']}</p>";
2126 $relationship['start_date'] = CRM_Utils_Date
::customFormat($values['start_date']);
2127 $relationship['end_date'] = CRM_Utils_Date
::customFormat($values['end_date']);
2128 $relationship['city'] = $values['city'];
2129 $relationship['state'] = $values['state'];
2130 $relationship['email'] = $values['email'];
2131 $relationship['phone'] = $values['phone'];
2132 $relationship['links'] = $values['action'];
2134 array_push($contactRelationships, $relationship);
2138 $relationshipsDT = array();
2139 $relationshipsDT['data'] = $contactRelationships;
2140 $relationshipsDT['recordsTotal'] = $params['total'];
2141 $relationshipsDT['recordsFiltered'] = $params['total'];
2143 return $relationshipsDT;