removing dependency of onbehalf
authormonishdeb <monish.deb@webaccessglobal.com>
Fri, 17 Jul 2015 05:50:44 +0000 (11:20 +0530)
committermonishdeb <monish.deb@webaccessglobal.com>
Thu, 10 Sep 2015 08:24:30 +0000 (13:54 +0530)
CRM/Contact/Form/ProfileContact.php
CRM/Contribute/Form/Contribution/Main.php
templates/CRM/Contribute/Form/Contribution/Main.tpl
templates/CRM/UF/Form/Block.tpl

index 69d9e4c553e3cc644823ea0bf723bc15c14ab8a3..61f3ed48b0b98cb1294a736958834d76c156111e 100644 (file)
@@ -40,8 +40,7 @@ class CRM_Contact_Form_ProfileContact {
    * @param CRM_Core_Form $form
    */
   public static function preProcess(&$form) {
-    $session = CRM_Core_Session::singleton();
-    $contactID = $session->get('userID');
+    $contactID = $form->_contactID;
 
     foreach (array('soft_credit', 'on_behalf') as $module) {
       $ufJoinParams = array(
@@ -79,7 +78,7 @@ class CRM_Contact_Form_ProfileContact {
       }
       else {
         $form->_onbehalf = FALSE;
-        $params = CRM_Contribute_BAO_ContributionPage::formatMultilingualOnBehalfParams($ufJoin->module_data);
+        $params = CRM_Contribute_BAO_ContributionPage::formatMultilingualOnBehalfParams($ufJoin->module_data, TRUE);
         if (CRM_Utils_Array::value('is_for_organization', $params)) {
           if ($params['is_for_organization'] == 2) {
             $form->_onBehalfRequired = TRUE;
@@ -107,6 +106,43 @@ class CRM_Contact_Form_ProfileContact {
               CRM_Core_Error::fatal(ts('This contribution page has been configured for contribution on behalf of an organization and the required fields of the selected onbehalf profile are disabled.'));
             }
 
+            //CRM-5001,CRM-15787
+            $member = CRM_Member_BAO_Membership::getMembershipBlock($form->_id);
+            if (empty($member['is_active'])) {
+              $msg = ts('Mixed profile not allowed for on behalf of registration/sign up.');
+              $onBehalfProfile = CRM_Core_BAO_UFGroup::profileGroups($form->_profileId);
+              foreach (array(
+                  'Individual',
+                  'Organization',
+                  'Household',
+                ) as $contactType) {
+                if (in_array($contactType, $onBehalfProfile) &&
+                  (in_array('Membership', $onBehalfProfile) ||
+                    in_array('Contribution', $onBehalfProfile)
+                  )
+                ) {
+                  CRM_Core_Error::fatal($msg);
+                }
+              }
+
+              if ($postID = CRM_Utils_Array::value('custom_post_id', $form->_values)) {
+                $postProfile = CRM_Core_BAO_UFGroup::profileGroups($postID);
+                foreach (array(
+                    'Individual',
+                    'Organization',
+                    'Household',
+                  ) as $contactType) {
+                  if (in_array($contactType, $postProfile) &&
+                    (in_array('Membership', $postProfile) ||
+                      in_array('Contribution', $postProfile)
+                    )
+                  ) {
+                    CRM_Core_Error::fatal($msg);
+                  }
+                }
+              }
+            }
+
             $form->assign('profileId', $form->_profileId);
             $form->assign('mode', $form->_mode);
 
@@ -164,7 +200,7 @@ class CRM_Contact_Form_ProfileContact {
             }
           }
         }
-        $this->assign('onBehalfRequired', $form->_onBehalfRequired);
+        $form->assign('onBehalfRequired', $form->_onBehalfRequired);
       }
     }
   }
@@ -176,6 +212,27 @@ class CRM_Contact_Form_ProfileContact {
    *
    */
   public static function buildQuickForm(&$form) {
+    if (!empty($form->_honor_block_is_active)) {
+      self::buildHonorBlock($form);
+    }
+
+    if (!empty($form->_onbehalf)) {
+      self::buildOnBehalfBlock($form);
+    }
+  }
+
+  /**
+   * @param $form
+   */
+  public static function postProcess($form) {
+  }
+
+  public static function buildHonorBlock(&$form) {
+    $form->assign('honor_block_is_active', TRUE);
+
+    //build soft-credit section
+    CRM_Contribute_Form_SoftCredit::buildQuickForm($form);
+
     $ufGroup = new CRM_Core_DAO_UFGroup();
     $ufGroup->id = $form->_honoreeProfileId;
     if (!$ufGroup->find(TRUE)) {
@@ -202,10 +259,70 @@ class CRM_Contact_Form_ProfileContact {
     }
   }
 
-  /**
-   * @param $form
-   */
-  public static function postProcess($form) {
-  }
+  public static function buildOnBehalfBlock(&$form) {
+    $form->assign('fieldSetTitle', ts('Organization Details'));
+    $form->assign('buildOnBehalfForm', TRUE);
+
+    $contactID = $form->_contactID;
+
+    if ($form->_membershipContactID) {
+      $entityBlock = array('contact_id' => $form->_membershipContactID);
+      //CRM_Core_BAO_Location::getValues($entityBlock, $form->_defaults);
+    }
+
+    if (!$form->_onBehalfRequired) {
+      $form->addElement('checkbox', 'is_for_organization',
+        $form->_values['for_organization'],
+        NULL, array('onclick' => "showOnBehalf( );")
+      );
+    }
 
+    $form->assign('is_for_organization', TRUE);
+    $form->assign('urlPath', 'civicrm/contribute/transact');
+
+    if ($contactID && count($form->_employers) >= 1) {
+      $form->add('text', 'organization_id', ts('Select an existing related Organization OR enter a new one'));
+
+      $form->add('select', 'onbehalfof_id', '', CRM_Utils_Array::collect('name', $form->_employers));
+
+      $orgOptions = array(
+        0 => ts('Select an existing organization'),
+        1 => ts('Enter a new organization'),
+      );
+
+      $form->addRadio('org_option', ts('options'), $orgOptions);
+      $form->setDefaults(array('org_option' => 0));
+      $form->add('checkbox', 'mode', '');
+    }
+
+    $prefix = 'onbehalf';
+    $profileFields = CRM_Core_BAO_UFGroup::getFields($form->_profileId, FALSE, CRM_Core_Action::VIEW, NULL,
+      NULL, FALSE, NULL, FALSE, NULL,
+      CRM_Core_Permission::CREATE, NULL
+    );
+    $fieldTypes = array('Contact', 'Organization');
+    $contactSubType = CRM_Contact_BAO_ContactType::subTypes('Organization');
+    $fieldTypes = array_merge($fieldTypes, $contactSubType);
+
+    if (is_array($form->_membershipBlock) && !empty($form->_membershipBlock)) {
+      $fieldTypes = array_merge($fieldTypes, array('Membership'));
+    }
+    else {
+      $fieldTypes = array_merge($fieldTypes, array('Contribution'));
+    }
+
+    foreach ($profileFields as $name => $field) {
+      if (in_array($field['field_type'], $fieldTypes)) {
+        list($prefixName, $index) = CRM_Utils_System::explode('-', $name, 2);
+        if (in_array($prefixName, array('organization_name', 'email')) && empty($field['is_required'])) {
+          $field['is_required'] = 1;
+        }
+
+        CRM_Core_BAO_UFGroup::buildProfile($form, $field, NULL, NULL, FALSE, $prefix, NULL, $prefix);
+      }
+    }
+
+    $form->assign('onBehalfOfFields', $profileFields);
+    $form->addElement('hidden', 'hidden_onbehalf_profile', 1);
+  }
 }
index 680938a93860f4af9d69fef9b98c6041d8acae9f..1f32a03635cfa37ab6854898b432ca4d148ca3bf 100644 (file)
@@ -101,54 +101,6 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
     if (!empty($this->_values['footer_text'])) {
       $this->assign('footer_text', $this->_values['footer_text']);
     }
-
-    //CRM-5001
-    //CRM-15787
-    $member = CRM_Member_BAO_Membership::getMembershipBlock($this->_id);
-    if (!empty($this->_values['is_for_organization']) && empty($member['is_active'])) {
-      $msg = ts('Mixed profile not allowed for on behalf of registration/sign up.');
-      $ufJoinParams = array(
-        'module' => 'onBehalf',
-        'entity_table' => 'civicrm_contribution_page',
-        'entity_id' => $this->_id,
-      );
-      $onBehalfProfileIDs = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
-      // getUFGroupIDs returns an array with the first item being the ID we need
-      $onBehalfProfileID = $onBehalfProfileIDs[0];
-      if ($onBehalfProfileID) {
-        $onBehalfProfile = CRM_Core_BAO_UFGroup::profileGroups($onBehalfProfileID);
-        foreach (array(
-                   'Individual',
-                   'Organization',
-                   'Household',
-                 ) as $contactType) {
-          if (in_array($contactType, $onBehalfProfile) &&
-            (in_array('Membership', $onBehalfProfile) ||
-              in_array('Contribution', $onBehalfProfile)
-            )
-          ) {
-            CRM_Core_Error::fatal($msg);
-          }
-        }
-      }
-
-      if ($postID = CRM_Utils_Array::value('custom_post_id', $this->_values)) {
-        $postProfile = CRM_Core_BAO_UFGroup::profileGroups($postID);
-        foreach (array(
-                   'Individual',
-                   'Organization',
-                   'Household',
-                 ) as $contactType) {
-          if (in_array($contactType, $postProfile) &&
-            (in_array('Membership', $postProfile) ||
-              in_array('Contribution', $postProfile)
-            )
-          ) {
-            CRM_Core_Error::fatal($msg);
-          }
-        }
-      }
-    }
   }
 
   /**
@@ -362,12 +314,11 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
       $this->assign('display_name', CRM_Contact_BAO_Contact::displayName($contactID));
     }
 
-    if ($this->_onbehalf) {
-      CRM_Contribute_Form_Contribution_OnBehalfOf::buildQuickForm($this);
-      // Return if we are in an ajax callback
-      if ($this->_snippet) {
-        return;
-      }
+    CRM_Contact_Form_ProfileContact::buildQuickForm($this);
+
+    // Return if we are in an ajax callback
+    if ($this->_onbehalf && $this->_snippet) {
+      return;
     }
 
     $this->applyFilter('__ALL__', 'trim');
@@ -462,25 +413,11 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
       }
     }
 
-    if ($this->_values['is_for_organization']) {
-      $this->buildOnBehalfOrganization();
-    }
-
     //we allow premium for pledge during pledge creation only.
     if (empty($this->_values['pledge_id'])) {
       CRM_Contribute_BAO_Premium::buildPremiumBlock($this, $this->_id, TRUE);
     }
 
-    //add honor block
-    if ($this->_honor_block_is_active) {
-      $this->assign('honor_block_is_active', TRUE);
-
-      //build soft-credit section
-      CRM_Contribute_Form_SoftCredit::buildQuickForm($this);
-      //build honoree profile section
-      CRM_Contact_Form_ProfileContact::buildQuickForm($this);
-    }
-
     //don't build pledge block when mid is passed
     if (!$this->_mid) {
       $config = CRM_Core_Config::singleton();
@@ -584,26 +521,6 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
     $this->addFormRule(array('CRM_Contribute_Form_Contribution_Main', 'formRule'), $this);
   }
 
-  /**
-   * Build elements to enable pay on behalf of an organization.
-   */
-  public function buildOnBehalfOrganization() {
-    if ($this->_membershipContactID) {
-      $entityBlock = array('contact_id' => $this->_membershipContactID);
-      CRM_Core_BAO_Location::getValues($entityBlock, $this->_defaults);
-    }
-
-    if (!$this->_onBehalfRequired) {
-      $this->addElement('checkbox', 'is_for_organization',
-        $this->_values['for_organization'],
-        NULL, array('onclick' => "showOnBehalf( );")
-      );
-    }
-
-    $this->assign('is_for_organization', TRUE);
-    $this->assign('urlPath', 'civicrm/contribute/transact');
-  }
-
   /**
    * Build elements to collect information for recurring contributions.
    *
index 63b18a9698f39af983410e393ec0bf790830c03e..8fb97b9582bd7499d08c606df05abe715bebcfb0 100644 (file)
@@ -25,7 +25,7 @@
 *}
 {* Callback snippet: On-behalf profile *}
 {if $snippet and !empty($isOnBehalfCallback)}
-  {include file="CRM/Contribute/Form/Contribution/OnBehalfOf.tpl" context="front-end"}
+  {* include file="CRM/Contribute/Form/Contribution/OnBehalfOf.tpl" context="front-end" *}
 
 {else}
   {literal}
 
   {if $is_for_organization}
   <div id='onBehalfOfOrg' class="crm-section">
-    {include file="CRM/Contribute/Form/Contribution/OnBehalfOf.tpl"}
+    {include file="CRM/UF/Form/Block.tpl" fields=$onBehalfOfFields mode=8 prefix='onbehalf'}
   </div>
   {/if}
 
index 356cf9554a7443c0f19f11d25b13fc6c238cd8f6..fc2f1ace08281752086c45e239d8bcfbd55d7631 100644 (file)
@@ -47,7 +47,7 @@
         <fieldset class="crm-profile crm-profile-id-{$field.group_id} crm-profile-name-{$field.groupName}"><legend>{$field.groupTitle}</legend>
         {/if}
 
-        {if ($form.formName eq 'Confirm' OR $form.formName eq 'ThankYou') AND $prefix neq 'honor'}
+        {if ($form.formName eq 'Confirm' OR $form.formName eq 'ThankYou') AND $prefix neq 'honor' OR $prefix neq 'onbehalf'}
           <div class="header-dark">{$field.groupTitle} </div>
         {/if}
         {assign var=fieldset  value=`$field.groupTitle`}