manual merge of fixes for CRM-13981
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage / Settings.php
index 28fd4e264df6cfd28dc7d54b2b09af80ad249cee..46219b0bc9fe9d33b799b72de09394462414bba8 100644 (file)
@@ -54,6 +54,7 @@ class CRM_Contribute_Form_ContributionPage_Settings extends CRM_Contribute_Form_
    */
   function setDefaultValues() {
     $defaults = parent::setDefaultValues();
+    $soft_credit_types = CRM_Core_OptionGroup::values('soft_credit_type', TRUE, FALSE, FALSE, NULL, 'name');
 
     if ($this->_id) {
       $title = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage',
@@ -74,9 +75,43 @@ class CRM_Contribute_Form_ContributionPage_Settings extends CRM_Contribute_Form_
         // get the first one only
         $defaults['onbehalf_profile_id'] = $onBehalfIDs[0];
       }
+
+      $ufJoinDAO = new CRM_Core_DAO_UFJoin();
+      $ufJoinDAO->module = 'soft_credit';
+      $ufJoinDAO->entity_id = $this->_id;
+      if ($ufJoinDAO->find(TRUE)) {
+        $defaults['honoree_profile'] = $ufJoinDAO->uf_group_id;
+        $jsonData = json_decode($ufJoinDAO->module_data);
+        if ($jsonData) {
+          foreach ($jsonData->soft_credit as $index => $value){
+            $defaults[$index] = $value;
+          }
+        }
+      }
+      else {
+        $ufGroupDAO = new CRM_Core_DAO_UFGroup();
+        $ufGroupDAO->name = 'honoree_individual';
+        if ($ufGroupDAO->find(TRUE)) {
+          $defaults['honoree_profile'] = $ufGroupDAO->id;
+        }
+        $defaults['soft_credit_types'] = array(
+          CRM_Utils_Array::value('in_honor_of', $soft_credit_types),
+          CRM_Utils_Array::value('in_memory_of', $soft_credit_types)
+        );
+      }
     }
     else {
       CRM_Utils_System::setTitle(ts('Title and Settings'));
+
+      $ufGroupDAO = new CRM_Core_DAO_UFGroup();
+      $ufGroupDAO->name = 'honoree_individual';
+      if ($ufGroupDAO->find(TRUE)) {
+        $defaults['honoree_profile'] = $ufGroupDAO->id;
+      }
+      $defaults['soft_credit_types'] = array(
+        CRM_Utils_Array::value('in_honor_of', $soft_credit_types),
+        CRM_Utils_Array::value('in_memory_of', $soft_credit_types)
+      );
     }
 
     return $defaults;
@@ -171,15 +206,47 @@ class CRM_Contribute_Form_ContributionPage_Settings extends CRM_Contribute_Form_
     // should the honor be enabled
     $this->addElement('checkbox', 'honor_block_is_active', ts('Honoree Section Enabled'), NULL, array('onclick' => "showHonor()"));
 
-    $this->add('text', 'honor_block_title', ts('Honoree Section Title'), $attributes['honor_block_title']);
+    $this->add('text', 'honor_block_title', ts('Honoree Section Title'), array('maxlength' => 255, 'size' => 45));
+
+    $this->add('textarea', 'honor_block_text', ts('Honoree Introductory Message'), array('rows' => 2, 'cols' => 50));
+
+    $softCreditTypes = &$this->add('select', 'soft_credit_types',
+      ts('Honor Types'),
+      CRM_Core_OptionGroup::values("soft_credit_type", FALSE),
+      FALSE,
+      array(
+        'id' => 'soft_credit_types',
+        'multiple' => 'multiple',
+        'title' => '- ' . ts('select') . ' -',
+      )
+    );
 
-    $this->add('textarea', 'honor_block_text', ts('Honoree Introductory Message'), $attributes['honor_block_text']);
+    $entities = array(
+      array('entity_name' => 'contact_1',
+        'entity_type' => 'IndividualModel'
+      ),
+      array('entity_name' => 'organization_1',
+        'entity_type' => 'OrganizationModel'
+      ),
+      array('entity_name' => 'household_1',
+        'entity_type' => 'HouseholdModel'
+      ),
+    );
+    $allowCoreTypes = array_merge(array('Contact', 'Individual', 'Organization', 'Household'), CRM_Contact_BAO_ContactType::subTypes('Individual'));
+    $allowSubTypes = array();
+
+    $this->addProfileSelector('honoree_profile', ts('Honoree Profile'), $allowCoreTypes, $allowSubTypes, $entities);
+
+    if (!empty($this->_submitValues['honor_block_is_active'])) {
+      $this->addRule('soft_credit_types', ts('At least one value must be selected if Honor Section is active'), 'required');
+      $this->addRule('honoree_profile', ts('Please select a profile used for honoree'), 'required');
+    }
 
     // add optional start and end dates
     $this->addDateTime('start_date', ts('Start Date'));
     $this->addDateTime('end_date', ts('End Date'));
 
-    $this->addFormRule(array('CRM_Contribute_Form_ContributionPage_Settings', 'formRule'), $this->_id);
+    $this->addFormRule(array('CRM_Contribute_Form_ContributionPage_Settings', 'formRule'), $this);
 
     parent::buildQuickForm();
   }
@@ -193,17 +260,15 @@ class CRM_Contribute_Form_ContributionPage_Settings extends CRM_Contribute_Form_
    * @static
    * @access public
    */
-  static function formRule($values, $files, $contributionPageId) {
+  static function formRule($values, $files, $self) {
     $errors = array();
-
+    $contributionPageId = $self->_id;
     //CRM-4286
     if (strstr($values['title'], '/')) {
       $errors['title'] = ts("Please do not use '/' in Title");
     }
 
-    if (CRM_Utils_Array::value('is_organization', $values) &&
-      !CRM_Utils_Array::value('onbehalf_profile_id', $values)
-    ) {
+    if (!empty($values['is_organization']) && empty($values['onbehalf_profile_id'])) {
       $errors['onbehalf_profile_id'] = ts('Please select a profile to collect organization information on this contribution page.');
     }
 
@@ -214,9 +279,13 @@ class CRM_Contribute_Form_ContributionPage_Settings extends CRM_Contribute_Form_
       $errors['end_date'] = ts('End date should be after Start date.');
     }
 
+    if (!empty($self->_values['payment_processor']) && $financialType = CRM_Contribute_BAO_Contribution::validateFinancialType($values['financial_type_id'])) {
+      $errors['financial_type_id'] = ts("Financial Account of account relationship of 'Expense Account is' is not configured for Financial Type : ") . $financialType;
+    }
+
     //dont allow on behalf of save when
     //pre or post profile consists of membership fields
-    if ($contributionPageId && CRM_Utils_Array::value('is_organization', $values)) {
+    if ($contributionPageId && !empty($values['is_organization'])) {
       $ufJoinParams = array(
         'module' => 'CiviContribute',
         'entity_table' => 'civicrm_contribution_page',
@@ -275,7 +344,7 @@ class CRM_Contribute_Form_ContributionPage_Settings extends CRM_Contribute_Form_
     $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
     $params['is_credit_card_only'] = CRM_Utils_Array::value('is_credit_card_only', $params, FALSE);
     $params['honor_block_is_active'] = CRM_Utils_Array::value('honor_block_is_active', $params, FALSE);
-    $params['is_for_organization'] = CRM_Utils_Array::value('is_organization', $params) ? CRM_Utils_Array::value('is_for_organization', $params, FALSE) : 0;
+    $params['is_for_organization'] = !empty($params['is_organization']) ? CRM_Utils_Array::value('is_for_organization', $params, FALSE) : 0;
 
     $params['start_date'] = CRM_Utils_Date::processDate($params['start_date'], $params['start_date_time'], TRUE);
     $params['end_date'] = CRM_Utils_Date::processDate($params['end_date'], $params['end_date_time'], TRUE);
@@ -286,24 +355,61 @@ class CRM_Contribute_Form_ContributionPage_Settings extends CRM_Contribute_Form_
       $params['honor_block_title'] = NULL;
       $params['honor_block_text'] = NULL;
     }
+    else {
+      $sctJSON = json_encode(array(
+        'soft_credit' => array(
+          'soft_credit_types' => $params['soft_credit_types'],
+          'honor_block_title' => $params['honor_block_title'],
+          'honor_block_text' => $params['honor_block_text']
+          )
+        )
+      );
+    }
 
     $dao = CRM_Contribute_BAO_ContributionPage::create($params);
 
-    // make entry in UF join table for onbehalf of org profile
     $ufJoinParams = array(
-      'is_active' => 1,
-      'module' => 'OnBehalf',
-      'entity_table' => 'civicrm_contribution_page',
-      'entity_id' => $dao->id,
+      'onbehalf_profile_id' =>
+        array(
+          'is_active' => 1,
+          'module' => 'OnBehalf',
+          'entity_table' => 'civicrm_contribution_page',
+          'entity_id' => $dao->id,
+        ),
+      'honor_block_is_active' =>
+        array(
+          'module' => 'soft_credit',
+          'entity_table' => 'civicrm_contribution_page',
+          'entity_id' => $dao->id,
+        )
     );
 
-    // first delete all past entries
-    CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams);
 
-    if (CRM_Utils_Array::value('onbehalf_profile_id', $params)) {
-      $ufJoinParams['weight'] = 1;
-      $ufJoinParams['uf_group_id'] = $params['onbehalf_profile_id'];
-      CRM_Core_BAO_UFJoin::create($ufJoinParams);
+    foreach ($ufJoinParams as $index => $ufJoinParam) {
+      if (!empty($params[$index])) {
+          $ufJoinParam['weight'] = 1;
+          if ($index == 'honor_block_is_active') {
+            $ufJoinParam['is_active'] = 1;
+            $ufJoinParam['uf_group_id'] = $params['honoree_profile'];
+            $ufJoinParam['module_data'] = $sctJSON;
+          }
+          else {
+            // first delete all past entries
+            CRM_Core_BAO_UFJoin::deleteAll($ufJoinParam);
+            $ufJoinParam['uf_group_id'] = $params[$index];
+          }
+            CRM_Core_BAO_UFJoin::create($ufJoinParam);
+        }
+      elseif ($index == 'honor_block_is_active') {
+        //On subsequent honor_block_is_active uncheck, disable(don't delete)
+        //that particular honoree profile entry in UFjoin table, CRM-13981
+        $ufId = CRM_Core_BAO_UFJoin::findJoinEntryId($ufJoinParam);
+        if ($ufId) {
+          $ufJoinParam['uf_group_id'] = CRM_Core_BAO_UFJoin::findUFGroupId($ufJoinParam);
+          $ufJoinParam['is_active'] = 0;
+          CRM_Core_BAO_UFJoin::create($ufJoinParam);
+        }
+      }
     }
 
     $this->set('id', $dao->id);