X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FContact%2FBAO%2FRelationship.php;h=fc8f192a0a55f449885e338474accf3ea276df1a;hb=d0592c3d38c59d5a263a5fb70be8af6dc97f1692;hp=86ad794c319ec7ad770aa177e2db4efc35cfd564;hpb=49f8272dffabdf0801411bc2a870201bb57c5fb1;p=civicrm-core.git diff --git a/CRM/Contact/BAO/Relationship.php b/CRM/Contact/BAO/Relationship.php index 86ad794c31..fc8f192a0a 100644 --- a/CRM/Contact/BAO/Relationship.php +++ b/CRM/Contact/BAO/Relationship.php @@ -113,9 +113,11 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship { } else { // check for duplicate relationship - //@todo this code doesn't cope well with updates - causes e-Notices. API has a lot of code to work around + // @todo this code doesn't cope well with updates - causes e-Notices. + // API has a lot of code to work around // this but should review this code & remove the extra handling from the api - // it seems doubtful any of this is relevant if the contact fields & relationship type fields are not set + // it seems doubtful any of this is relevant if the contact fields & relationship + // type fields are not set if ( self::checkDuplicateRelationship( $params, @@ -130,9 +132,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship { $validContacts = TRUE; //validate contacts in update mode also. - if (CRM_Utils_Array::value('contact', $ids) && - CRM_Utils_Array::value('contactTarget', $ids) - ) { + if (!empty($ids['contact']) && !empty($ids['contactTarget'])) { if (self::checkValidRelationship($params, $ids, $ids['contactTarget'])) { $validContacts = FALSE; $invalid++; @@ -148,7 +148,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship { } // do not add to recent items for import, CRM-4399 - if (!(CRM_Utils_Array::value('skipRecentView', $params) || $invalid || $duplicate)) { + if (!(!empty($params['skipRecentView']) || $invalid || $duplicate)) { $url = CRM_Utils_System::url('civicrm/contact/view/rel', "action=view&reset=1&id={$relationship->id}&cid={$relationship->contact_id_a}&context=home" ); @@ -199,7 +199,9 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship { * @static */ static function add(&$params, $ids = array(), $contactId = NULL) { - $relationshipId = CRM_Utils_Array::value('relationship', $ids, CRM_Utils_Array::value('id', $params)); + $relationshipId = + CRM_Utils_Array::value('relationship', $ids, CRM_Utils_Array::value('id', $params)); + $hook = 'create'; if($relationshipId) { $hook = 'edit'; @@ -209,14 +211,16 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship { $relationshipTypes = CRM_Utils_Array::value('relationship_type_id', $params); - // expolode the string with _ to get the relationship type id and to know which contact has to be inserted in + // explode the string with _ to get the relationship type id + // and to know which contact has to be inserted in // contact_id_a and which one in contact_id_b list($type, $first, $second) = explode('_', $relationshipTypes); ${'contact_' . $first} = CRM_Utils_Array::value('contact', $ids); ${'contact_' . $second} = $contactId; - //check if the relationship type is Head of Household then update the household's primary contact with this contact. + // check if the relationship type is Head of Household then update the + // household's primary contact with this contact. if ($type == 6) { CRM_Contact_BAO_Household::updatePrimaryContact($contact_b, $contact_a); } @@ -251,7 +255,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship { $relationship->save(); // add custom field values - if (CRM_Utils_Array::value('custom', $params)) { + if (!empty($params['custom'])) { CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_relationship', $relationship->id); } @@ -664,14 +668,16 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship { $relationshipTypeId = CRM_Utils_Array::value('relationship_type_id', $params); list($type, $first, $second) = explode('_', $relationshipTypeId); - $queryString = " SELECT id - FROM civicrm_relationship - WHERE relationship_type_id = " . CRM_Utils_Type::escape($type, 'Integer'); + $queryString = " +SELECT id +FROM civicrm_relationship +WHERE 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 + * 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::create (twice) * CRM_BAO_Contact_Utils::clearCurrentEmployer (seemingly without dates) * CRM_Contact_Form_Task_AddToOrganization::postProcess & @@ -683,16 +689,20 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship { 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 + //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'); + $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'); + else { + $queryString .= " AND $dateField = " . + CRM_Utils_Type::escape($params[$dateField], 'Date'); } } } @@ -762,7 +772,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship { $v = array(); // get the specific number of relationship or all relationships. - if (CRM_Utils_Array::value('numRelationship', $params)) { + if (!empty($params['numRelationship'])) { $v['data'] = &CRM_Contact_BAO_Relationship::getRelationship($params['contact_id'], NULL, $params['numRelationship']); } else { @@ -953,15 +963,24 @@ LEFT JOIN civicrm_country ON (civicrm_address.country_id = civicrm_country.id) $order = $limit = ''; if (!$count) { - $order = ' ORDER BY civicrm_relationship_type_id, sort_name '; + if (empty($params['sort'])) { + $order = ' ORDER BY civicrm_relationship_type_id, sort_name '; + } + else { + $order = " ORDER BY {$params['sort']} "; + } + + $offset = 0; + if (!empty($params['offset'])) { + $offset = $params['offset']; + } if ($numRelationship) { - $limit = " LIMIT 0, $numRelationship"; + $limit = " LIMIT {$offset}, $numRelationship"; } } // building the query string - $queryString = ''; $queryString = $select1 . $from1 . $where1 . $select2 . $from2 . $where2 . $order . $limit; $relationship = new CRM_Contact_DAO_Relationship(); @@ -1090,7 +1109,15 @@ LEFT JOIN civicrm_country ON (civicrm_address.country_id = civicrm_country.id) } } - $values[$rid]['action'] = CRM_Core_Action::formLink($links, $mask, $replace); + $values[$rid]['action'] = CRM_Core_Action::formLink( + $links, + $mask, + $replace, + ts('more'), + FALSE, + 'relationship.selector.row', + 'Relationship', + $rid); unset($links[CRM_Core_Action::MAX_ACTION]); } } @@ -1276,10 +1303,10 @@ SELECT relationship_type_id, relationship_direction $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($membershipValues['membership_type_id']); // Check if contact's relationship type exists in membership type $relTypeDirs = array(); - if (CRM_Utils_Array::value('relationship_type_id', $membershipType)) { + if (!empty($membershipType['relationship_type_id'])) { $relTypeIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $membershipType['relationship_type_id']); } - if (CRM_Utils_Array::value('relationship_direction', $membershipType)) { + if (!empty($membershipType['relationship_direction'])) { $relDirections = explode(CRM_Core_DAO::VALUE_SEPARATOR, $membershipType['relationship_direction']); } foreach ($relTypeIds as $key => $value) { @@ -1306,7 +1333,7 @@ SELECT relationship_type_id, relationship_direction } foreach (array( 'join_date', 'start_date', 'end_date') as $dateField) { - if (CRM_Utils_Array::value($dateField, $membershipValues)) { + if (!empty($membershipValues[$dateField])) { $membershipValues[$dateField] = CRM_Utils_Date::processDate($membershipValues[$dateField]); } } @@ -1437,7 +1464,7 @@ WHERE id IN ( {$contacts} ) * Function to return list of permissioned contacts for a given contact and relationship type * * @param $contactID int contact id whose permissioned contacts are to be found. - * @param $relTypeId relationship type id + * @param $relTypeId string one or more relationship type id's * @param $name string * * @static @@ -1453,7 +1480,7 @@ SELECT cc.id as id, cc.sort_name as name FROM civicrm_relationship cr, civicrm_contact cc WHERE cr.contact_id_a = %1 AND -cr.relationship_type_id = %2 AND +cr.relationship_type_id IN (%2) AND cr.is_permission_a_b = 1 AND IF(cr.end_date IS NULL, 1, (DATEDIFF( CURDATE( ), cr.end_date ) <= 0)) AND cr.is_active = 1 AND @@ -1465,7 +1492,7 @@ cc.id = cr.contact_id_b"; AND cc.sort_name LIKE '%$name%'"; } - $args = array(1 => array($contactID, 'Integer'), 2 => array($relTypeId, 'Integer')); + $args = array(1 => array($contactID, 'Integer'), 2 => array($relTypeId, 'String')); $dao = CRM_Core_DAO::executeQuery($query, $args); while ($dao->fetch()) { @@ -1478,35 +1505,6 @@ AND cc.sort_name LIKE '%$name%'"; return $contacts; } - static function getValidContactTypeList($relType) { - // string looks like 4_a_b - $rel_parts = explode('_', $relType); - $allRelationshipType = CRM_Core_PseudoConstant::relationshipType('label'); - $contactProfiles = CRM_Core_BAO_UFGroup::getReservedProfiles('Contact', NULL); - - if ($rel_parts[1] == 'a') { - $leftType = $allRelationshipType[$rel_parts[0]]['contact_type_b']; - } - else { - $leftType = $allRelationshipType[$rel_parts[0]]['contact_type_a']; - } - - // Handle 'All Contacts' contact type for left side of relationship ($leftType is empty in this case) - // In this case all reserved profiles are available - if ($leftType == '') { - $contactTypes = $contactProfiles; - } else { - $contactTypes = array(); - foreach ($contactProfiles as $key => $value) { - if (strpos($value, $leftType) !== FALSE) { - $contactTypes = array($key => $value); - } - } - } - - return $contactTypes; - } - /** * Merge relationships from otherContact to mainContact * Called during contact merge operation @@ -1610,5 +1608,115 @@ AND cc.sort_name LIKE '%$name%'"; } } } + + + /** + * This function is a wrapper for contact relationship selector + * + * @param array $params associated array for params record id. + * + * @return array $contactRelationships associated array of contact relationships + * @access public + */ + public static function getContactRelationshipSelector(&$params) { + // format the params + $params['offset'] = ($params['page'] - 1) * $params['rp']; + $params['sort'] = CRM_Utils_Array::value('sortBy', $params); + + if ($params['context'] == 'past') { + $relationshipStatus = CRM_Contact_BAO_Relationship::INACTIVE; + } + else { + $relationshipStatus = CRM_Contact_BAO_Relationship::CURRENT; + } + + // check logged in user for permission + $page = new CRM_Core_Page(); + CRM_Contact_Page_View::checkUserPermission($page, $params['contact_id']); + $permissions = array($page->_permission); + if ($page->_permission == CRM_Core_Permission::EDIT) { + $permissions[] = CRM_Core_Permission::DELETE; + } + $mask = CRM_Core_Action::mask($permissions); + + if ($params['context'] != 'user') { + $links = CRM_Contact_Page_View_Relationship::links(); + $permissionedContacts = FALSE; + } + else { + $links = CRM_Contact_Page_View_UserDashBoard::links(); + $permissionedContacts = TRUE; + $mask = NULL; + } + // get contact relationships + $relationships = CRM_Contact_BAO_Relationship::getRelationship($params['contact_id'], + $relationshipStatus, + $params['rp'], 0, 0, + $links, $mask, + $permissionedContacts, + $params + ); + + $contactRelationships = array(); + $params['total'] = 0; + if (!empty($relationships)) { + // get the total relationships + if ($params['context'] != 'user') { + $params['total'] = CRM_Contact_BAO_Relationship::getRelationship($params['contact_id'], + $relationshipStatus, 0, 1, 0, NULL, NULL, $permissionedContacts); + } + else { + // FIX ME: we cannot directly determine total permissioned relationship, hence re-fire query + $permissionedRelationships = CRM_Contact_BAO_Relationship::getRelationship($params['contact_id'], + $relationshipStatus, + 0, 0, 0, + NULL, NULL, TRUE + ); + $params['total'] = count($permissionedRelationships); + } + + // format params + foreach ($relationships as $relationshipId => $values) { + $contactRelationships[$relationshipId]['name'] = CRM_Utils_System::href( + $values['name'], + 'civicrm/contact/view', + "reset=1&cid={$values['contact_id_b']}"); + + $contactRelationships[$relationshipId]['relation'] = CRM_Utils_System::href( + $values['relation'], + 'civicrm/contact/view/rel', + "action=view&reset=1&cid={$values['contact_id_a']}&id={$values['id']}&rtype={$values['rtype']}"); + + if ($params['context'] == 'current') { + if (($params['contact_id'] == $values['contact_id_a'] AND $values['is_permission_a_b'] == 1) OR + ($params['contact_id'] == $values['contact_id_b'] AND $values['is_permission_b_a'] == 1) + ) { + $contactRelationships[$relationshipId]['name'] .= ' *'; + } + + if (($values['cid'] == $values['contact_id_a'] AND $values['is_permission_a_b'] == 1) OR + ($values['cid'] == $values['contact_id_b'] AND $values['is_permission_b_a'] == 1) + ) { + $contactRelationships[$relationshipId]['relation'] .= ' *'; + } + } + + if (!empty($values['description'])) { + $contactRelationships[$relationshipId]['relation'] .= "

{$values['description']}

"; + } + + $contactRelationships[$relationshipId]['start_date'] = CRM_Utils_Date::customFormat($values['start_date']); + $contactRelationships[$relationshipId]['end_date'] = CRM_Utils_Date::customFormat($values['end_date']); + $contactRelationships[$relationshipId]['city'] = $values['city']; + $contactRelationships[$relationshipId]['state'] = $values['state']; + $contactRelationships[$relationshipId]['email'] = $values['email']; + $contactRelationships[$relationshipId]['phone'] = $values['phone']; + $contactRelationships[$relationshipId]['links'] = $values['action']; + $contactRelationships[$relationshipId]['id'] = $values['id']; + } + } + return $contactRelationships; + } + }