CRM-16490: allow all permissioned organization for on-behalf registration
authormonishdeb <monish.deb@webaccessglobal.com>
Thu, 10 Sep 2015 19:54:25 +0000 (01:24 +0530)
committermonishdeb <monish.deb@webaccessglobal.com>
Thu, 10 Sep 2015 19:54:25 +0000 (01:24 +0530)
----------------------------------------
* CRM-16490: load org for permissioned relationships other than current employer
  https://issues.civicrm.org/jira/browse/CRM-16490

CRM/Contact/BAO/Relationship.php
CRM/Contribute/Form/Contribution/Confirm.php
CRM/Contribute/Form/ContributionBase.php
CRM/Core/Page/AJAX/Location.php

index 58be6cb1852778d403e8dfb04cbb62adbb5118c1..18b21e2824e7788400925c87c3f1341d10054ca0 100644 (file)
@@ -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)) {
index 287988b8355b0a624c6886525f9fa3e4289f4f9e..2ce049836915c839ccb73386a36f267ae2b62362 100644 (file)
@@ -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);
index 17059240a29a0fdc32e14e45eddef29786b66737..5f1984b3d0c08166a82a48f71a6cb746baea50fb 100644 (file)
@@ -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'),
index 50473b6dbba3bc5f1906020ea240bba9b37500e7..b0d13d58bc2fc355c8affba6f309c89652455002 100644 (file)
@@ -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();
     }