From: monishdeb Date: Thu, 10 Sep 2015 19:54:25 +0000 (+0530) Subject: CRM-16490: allow all permissioned organization for on-behalf registration X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=33260076495697444ed36a9f0cc0335f5b80a821;p=civicrm-core.git CRM-16490: allow all permissioned organization for on-behalf registration ---------------------------------------- * CRM-16490: load org for permissioned relationships other than current employer https://issues.civicrm.org/jira/browse/CRM-16490 --- diff --git a/CRM/Contact/BAO/Relationship.php b/CRM/Contact/BAO/Relationship.php index 58be6cb185..18b21e2824 100644 --- a/CRM/Contact/BAO/Relationship.php +++ b/CRM/Contact/BAO/Relationship.php @@ -1749,14 +1749,15 @@ WHERE id IN ( {$contacts} ) * @param int $relTypeId * one or more relationship type id's. * @param string $name + * @param string $contactType * * @return array * Array of contacts */ - public static function getPermissionedContacts($contactID, $relTypeId = NULL, $name = NULL) { + public static function getPermissionedContacts($contactID, $relTypeId = NULL, $name = NULL, $contactType = NULL) { $contacts = array(); $args = array(1 => array($contactID, 'Integer')); - $relationshipTypeClause = ''; + $relationshipTypeClause = $contactTypeClause = ''; if ($relTypeId) { // @todo relTypeId is only ever passed in as an int. Change this to reflect that - @@ -1764,9 +1765,15 @@ WHERE id IN ( {$contacts} ) $relationshipTypeClause = 'AND cr.relationship_type_id IN (%2) '; $args[2] = array($relTypeId, 'String'); } + + if ($contactType) { + $contactTypeClause = ' AND cr.relationship_type_id = crt.id AND crt.contact_type_b = %3 '; + $args[3] = array($contactType, 'String'); + } + $query = " SELECT cc.id as id, cc.sort_name as name -FROM civicrm_relationship cr, civicrm_contact cc +FROM civicrm_relationship cr, civicrm_contact cc, civicrm_relationship_type crt WHERE cr.contact_id_a = %1 AND cr.is_permission_a_b = 1 AND @@ -1775,6 +1782,7 @@ cr.is_active = 1 AND cc.id = cr.contact_id_b AND cc.is_deleted = 0 $relationshipTypeClause +$contactTypeClause "; if (!empty($name)) { diff --git a/CRM/Contribute/Form/Contribution/Confirm.php b/CRM/Contribute/Form/Contribution/Confirm.php index 287988b835..2ce0498369 100644 --- a/CRM/Contribute/Form/Contribution/Confirm.php +++ b/CRM/Contribute/Form/Contribution/Confirm.php @@ -1142,29 +1142,29 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr * Array of fields from the onbehalf profile relevant to the organization. */ public static function processOnBehalfOrganization(&$behalfOrganization, &$contactID, &$values, &$params, $fields = NULL) { - $isCurrentEmployer = FALSE; + $isNotCurrentEmployer = FALSE; $dupeIDs = array(); $orgID = NULL; if (!empty($behalfOrganization['organization_id'])) { $orgID = $behalfOrganization['organization_id']; unset($behalfOrganization['organization_id']); - $isCurrentEmployer = TRUE; + } + // create employer relationship with $contactID only when new organization is there + // else retain the existing relationship + else { + // get the Employee relationship type id + $relTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b'); +; + // keep relationship params ready + $relParams['relationship_type_id'] = $relTypeId . '_a_b'; + $relParams['is_permission_a_b'] = 1; + $relParams['is_active'] = 1; + $isNotCurrentEmployer = TRUE; } // formalities for creating / editing organization. $behalfOrganization['contact_type'] = 'Organization'; - // get the relationship type id - $relType = new CRM_Contact_DAO_RelationshipType(); - $relType->name_a_b = 'Employee of'; - $relType->find(TRUE); - $relTypeId = $relType->id; - - // keep relationship params ready - $relParams['relationship_type_id'] = $relTypeId . '_a_b'; - $relParams['is_permission_a_b'] = 1; - $relParams['is_active'] = 1; - if (!$orgID) { // check if matching organization contact exists $dedupeParams = CRM_Dedupe_Finder::formatParams($behalfOrganization, 'Organization'); @@ -1195,9 +1195,11 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr NULL, NULL, 'Organization' ); // create relationship - $relParams['contact_check'][$orgID] = 1; - $cid = array('contact' => $contactID); - CRM_Contact_BAO_Relationship::legacyCreateMultiple($relParams, $cid); + if ($isNotCurrentEmployer) { + $relParams['contact_check'][$orgID] = 1; + $cid = array('contact' => $contactID); + CRM_Contact_BAO_Relationship::legacyCreateMultiple($relParams, $cid); + } // if multiple match - send a duplicate alert if ($dupeIDs && (count($dupeIDs) > 1)) { @@ -1217,13 +1219,13 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr //make this employee of relationship as current //employer / employee relationship, CRM-3532 - if ($isCurrentEmployer && + if ($isNotCurrentEmployer && ($orgID != CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'employer_id')) ) { - $isCurrentEmployer = FALSE; + $isNotCurrentEmployer = FALSE; } - if (!$isCurrentEmployer && $orgID) { + if (!$isNotCurrentEmployer && $orgID) { //build current employer params $currentEmpParams[$contactID] = $orgID; CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($currentEmpParams); diff --git a/CRM/Contribute/Form/ContributionBase.php b/CRM/Contribute/Form/ContributionBase.php index 17059240a2..5f1984b3d0 100644 --- a/CRM/Contribute/Form/ContributionBase.php +++ b/CRM/Contribute/Form/ContributionBase.php @@ -215,14 +215,11 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { $this->_defaultMemTypeId = $membership->membership_type_id; if ($membership->contact_id != $this->_contactID) { $validMembership = FALSE; - $employers = CRM_Contact_BAO_Relationship::getPermissionedContacts( - $this->_userID, - CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b') - ); - if (!empty($employers) && array_key_exists($membership->contact_id, $employers)) { + $organizations = CRM_Contact_BAO_Relationship::getPermissionedContacts($this->_userID, NULL, NULL, 'Organization'); + if (!empty($organizations) && array_key_exists($membership->contact_id, $organizations)) { $this->_membershipContactID = $membership->contact_id; $this->assign('membershipContactID', $this->_membershipContactID); - $this->assign('membershipContactName', $employers[$this->_membershipContactID]['name']); + $this->assign('membershipContactName', $organizations[$this->_membershipContactID]['name']); $validMembership = TRUE; } else { @@ -804,12 +801,10 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { } if ($contactID) { - $employers = CRM_Contact_BAO_Relationship::getPermissionedContacts( - $contactID, - CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b') - ); + // retrieve all permissioned organizations of contact $contactID + $organizations = CRM_Contact_BAO_Relationship::getPermissionedContacts($contactID, NULL, NULL, 'Organization'); - if (count($employers)) { + if (count($organizations)) { // Related org url - pass checksum if needed $args = array( 'ufId' => $form->_values['onbehalf_profile_id'], @@ -826,8 +821,8 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { $locDataURL = CRM_Utils_System::url('civicrm/ajax/permlocation', $args, FALSE, NULL, FALSE); $form->assign('locDataURL', $locDataURL); } - if (count($employers) > 0) { - $form->add('select', 'onbehalfof_id', '', CRM_Utils_Array::collect('name', $employers)); + if (count($organizations) > 0) { + $form->add('select', 'onbehalfof_id', '', CRM_Utils_Array::collect('name', $organizations)); $orgOptions = array( 0 => ts('Select an existing organization'), diff --git a/CRM/Core/Page/AJAX/Location.php b/CRM/Core/Page/AJAX/Location.php index 50473b6dbb..b0d13d58bc 100644 --- a/CRM/Core/Page/AJAX/Location.php +++ b/CRM/Core/Page/AJAX/Location.php @@ -58,11 +58,8 @@ class CRM_Core_Page_AJAX_Location { } // Verify user permission on related contact - $employers = CRM_Contact_BAO_Relationship::getPermissionedContacts( - $user, - CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b') - ); - if (!isset($employers[$cid])) { + $organizations = CRM_Contact_BAO_Relationship::getPermissionedContacts($user, NULL, NULL, 'Organization'); + if (!isset($organizations[$cid])) { CRM_Utils_System::civiExit(); }