Example #1
0
 /**
  * Set the default values.
  */
 public function setDefaultValues()
 {
     // check if the user is registered and we have a contact ID
     $contactID = $this->getContactID();
     if (!empty($contactID)) {
         $fields = array();
         $removeCustomFieldTypes = array('Contribution', 'Membership');
         $contribFields = CRM_Contribute_BAO_Contribution::getContributionFields();
         // remove component related fields
         foreach ($this->_fields as $name => $dontCare) {
             //don't set custom data Used for Contribution (CRM-1344)
             if (substr($name, 0, 7) == 'custom_') {
                 $id = substr($name, 7);
                 if (!CRM_Core_BAO_CustomGroup::checkCustomField($id, $removeCustomFieldTypes)) {
                     continue;
                 }
                 // ignore component fields
             } elseif (array_key_exists($name, $contribFields) || substr($name, 0, 11) == 'membership_' || substr($name, 0, 13) == 'contribution_') {
                 continue;
             }
             $fields[$name] = 1;
         }
         if (!empty($fields)) {
             CRM_Core_BAO_UFGroup::setProfileDefaults($contactID, $fields, $this->_defaults);
         }
         $billingDefaults = $this->getProfileDefaults('Billing', $contactID);
         $this->_defaults = array_merge($this->_defaults, $billingDefaults);
     }
     if (!empty($this->_ccid) && !empty($this->_pendingAmount)) {
         $this->_defaults['total_amount'] = CRM_Utils_Money::format($this->_pendingAmount, NULL, '%a');
     }
     /*
      * hack to simplify credit card entry for testing
      *
      * $this->_defaults['credit_card_type']     = 'Visa';
      *         $this->_defaults['amount']               = 168;
      *         $this->_defaults['credit_card_number']   = '4111111111111111';
      *         $this->_defaults['cvv2']                 = '000';
      *         $this->_defaults['credit_card_exp_date'] = array('Y' => date('Y')+1, 'M' => '05');
      *         // hack to simplify direct debit entry for testing
      *         $this->_defaults['account_holder'] = 'Max Müller';
      *         $this->_defaults['bank_account_number'] = '12345678';
      *         $this->_defaults['bank_identification_number'] = '12030000';
      *         $this->_defaults['bank_name'] = 'Bankname';
      */
     //build set default for pledge overdue payment.
     if (!empty($this->_values['pledge_id'])) {
         //used to record completed pledge payment ids used later for honor default
         $completedContributionIds = array();
         $pledgePayments = CRM_Pledge_BAO_PledgePayment::getPledgePayments($this->_values['pledge_id']);
         $paymentAmount = 0;
         $duePayment = FALSE;
         foreach ($pledgePayments as $payId => $value) {
             if ($value['status'] == 'Overdue') {
                 $this->_defaults['pledge_amount'][$payId] = 1;
                 $paymentAmount += $value['scheduled_amount'];
             } elseif (!$duePayment && $value['status'] == 'Pending') {
                 $this->_defaults['pledge_amount'][$payId] = 1;
                 $paymentAmount += $value['scheduled_amount'];
                 $duePayment = TRUE;
             } elseif ($value['status'] == 'Completed' && $value['contribution_id']) {
                 $completedContributionIds[] = $value['contribution_id'];
             }
         }
         $this->_defaults['price_' . $this->_priceSetId] = $paymentAmount;
         if (count($completedContributionIds)) {
             $softCredit = array();
             foreach ($completedContributionIds as $id) {
                 $softCredit = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($id);
             }
             if (isset($softCredit['soft_credit'])) {
                 $this->_defaults['soft_credit_type_id'] = $softCredit['soft_credit'][1]['soft_credit_type'];
                 //since honoree profile fieldname of fields are prefixed with 'honor'
                 //we need to reformat the fieldname to append prefix during setting default values
                 CRM_Core_BAO_UFGroup::setProfileDefaults($softCredit['soft_credit'][1]['contact_id'], CRM_Core_BAO_UFGroup::getFields($this->_honoreeProfileId), $defaults);
                 foreach ($defaults as $fieldName => $value) {
                     $this->_defaults['honor[' . $fieldName . ']'] = $value;
                 }
             }
         }
     } elseif (!empty($this->_values['pledge_block_id'])) {
         //set default to one time contribution.
         $this->_defaults['is_pledge'] = 0;
     }
     // to process Custom data that are appended to URL
     $getDefaults = CRM_Core_BAO_CustomGroup::extractGetParams($this, "'Contact', 'Individual', 'Contribution'");
     $this->_defaults = array_merge($this->_defaults, $getDefaults);
     $config = CRM_Core_Config::singleton();
     // set default country from config if no country set
     if (empty($this->_defaults["billing_country_id-{$this->_bltID}"])) {
         $this->_defaults["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry;
     }
     // set default state/province from config if no state/province set
     if (empty($this->_defaults["billing_state_province_id-{$this->_bltID}"])) {
         $this->_defaults["billing_state_province_id-{$this->_bltID}"] = $config->defaultContactStateProvince;
     }
     $entityId = $memtypeID = NULL;
     if ($this->_priceSetId) {
         if ($this->_useForMember && !empty($this->_currentMemberships) || $this->_defaultMemTypeId) {
             $selectedCurrentMemTypes = array();
             foreach ($this->_priceSet['fields'] as $key => $val) {
                 foreach ($val['options'] as $keys => $values) {
                     $opMemTypeId = CRM_Utils_Array::value('membership_type_id', $values);
                     $priceFieldName = 'price_' . $values['price_field_id'];
                     $priceFieldValue = CRM_Price_BAO_PriceSet::getPriceFieldValueFromURL($this, $priceFieldName);
                     if (!empty($priceFieldValue)) {
                         CRM_Price_BAO_PriceSet::setDefaultPriceSetField($priceFieldName, $priceFieldValue, $val['html_type'], $this->_defaults);
                         // break here to prevent overwriting of default due to 'is_default'
                         // option configuration or setting of current membership or
                         // membership for related organization.
                         // The value sent via URL get's higher priority.
                         break;
                     } elseif ($opMemTypeId && in_array($opMemTypeId, $this->_currentMemberships) && !in_array($opMemTypeId, $selectedCurrentMemTypes)) {
                         CRM_Price_BAO_PriceSet::setDefaultPriceSetField($priceFieldName, $keys, $val['html_type'], $this->_defaults);
                         $memtypeID = $selectedCurrentMemTypes[] = $values['membership_type_id'];
                     } elseif (!empty($values['is_default']) && !$opMemTypeId && (!isset($this->_defaults[$priceFieldName]) || $val['html_type'] == 'CheckBox' && !isset($this->_defaults[$priceFieldName][$keys]))) {
                         CRM_Price_BAO_PriceSet::setDefaultPriceSetField($priceFieldName, $keys, $val['html_type'], $this->_defaults);
                         $memtypeID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $this->_defaults[$priceFieldName], 'membership_type_id');
                     }
                 }
             }
             $entityId = CRM_Utils_Array::value('id', CRM_Member_BAO_Membership::getContactMembership($contactID, $memtypeID, NULL));
         } else {
             CRM_Price_BAO_PriceSet::setDefaultPriceSet($this, $this->_defaults);
         }
     }
     //set custom field defaults set by admin if value is not set
     if (!empty($this->_fields)) {
         //load default campaign from page.
         if (array_key_exists('contribution_campaign_id', $this->_fields)) {
             $this->_defaults['contribution_campaign_id'] = CRM_Utils_Array::value('campaign_id', $this->_values);
         }
         //set custom field defaults
         foreach ($this->_fields as $name => $field) {
             if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) {
                 if (!isset($this->_defaults[$name])) {
                     CRM_Core_BAO_CustomField::setProfileDefaults($customFieldID, $name, $this->_defaults, $entityId, CRM_Profile_Form::MODE_REGISTER);
                 }
             }
         }
     }
     if (!empty($this->_paymentProcessors)) {
         foreach ($this->_paymentProcessors as $pid => $value) {
             if (!empty($value['is_default'])) {
                 $this->_defaults['payment_processor_id'] = $pid;
             }
         }
     }
     return $this->_defaults;
 }