You are here

private function AdminForm::buildContributionTab in Webform CiviCRM Integration 8.5

Contribution settings

1 call to AdminForm::buildContributionTab()
AdminForm::buildForm in src/AdminForm.php
Build admin form for civicrm tab of a webform

File

src/AdminForm.php, line 1051
Webform CiviCRM module's admin form.

Class

AdminForm
@file Webform CiviCRM module's admin form.

Namespace

Drupal\webform_civicrm

Code

private function buildContributionTab() {
  $utils = \Drupal::service('webform_civicrm.utils');
  $this->form['contribution'] = [
    '#type' => 'details',
    '#title' => t('Contribution'),
    '#group' => 'webform_civicrm',
    '#description' => t('In order to process live transactions for events, memberships, or contributions, select a contribution page and its billing fields will be included on the webform.'),
    '#attributes' => [
      'class' => [
        'civi-icon-contribution',
      ],
    ],
  ];
  $fid = 'civicrm_1_contribution_1_contribution_enable_contribution';
  $enable_contribution = wf_crm_aval($this->data, 'contribution:1:contribution:1:enable_contribution');
  unset($this->sets['contribution']['fields']['contribution_enable_contribution']);
  $this->form['contribution'][$fid] = [
    '#type' => 'select',
    '#title' => t('Enable Contribution?'),
    '#default_value' => $enable_contribution,
    '#options' => [
      t('No'),
      t('Yes'),
    ],
    '#description' => t('Enable this section to submit a payment for the contact.'),
  ];
  $this
    ->addAjaxItem('contribution', $fid, 'sets');
  $contribution = wf_crm_aval($this->data, 'contribution:1:contribution:1:enable_contribution');
  if (!$contribution) {
    return;
  }

  // Make sure webform is set-up to prevent credit card abuse.
  $this
    ->checkSubmissionLimit();
  $financialType = wf_crm_aval($this->data, 'contribution:1:contribution:1:financial_type_id');

  // Add contribution fields
  foreach ($this->sets as $sid => $set) {
    if ($set['entity_type'] == 'contribution' && (empty($set['sub_types']) || in_array($financialType, $set['sub_types']))) {

      //Build custom fields as per financial type selected.
      if (strpos($sid, 'cg') === 0) {
        $this->form['contribution']['sets']['custom'][$sid] = [
          '#type' => 'fieldset',
          '#title' => $set['label'],
          '#attributes' => [
            'id' => $sid,
            'class' => [
              'web-civi-checkbox-set',
            ],
          ],
          'js_select' => $this
            ->addToggle($sid),
        ];
        $this
          ->addDynamicCustomSetting($this->form['contribution']['sets']['custom'][$sid], $sid, 'contribution', 1);
      }
      else {
        $this->form['contribution']['sets'][$sid] = [
          '#type' => 'fieldset',
          '#title' => $set['label'],
          '#attributes' => [
            'id' => $sid,
            'class' => [
              'web-civi-checkbox-set',
            ],
          ],
          'js_select' => $this
            ->addToggle($sid),
        ];
      }
      if (isset($set['fields'])) {
        foreach ($set['fields'] as $fid => $field) {
          $fid = "civicrm_1_contribution_1_{$fid}";
          if (strpos($sid, 'cg') === 0) {
            $this->form['contribution']['sets']['custom'][$sid][$fid] = $this
              ->addItem($fid, $field);
          }
          else {
            $this->form['contribution']['sets'][$sid][$fid] = $this
              ->addItem($fid, $field);
          }
        }
      }
    }
  }

  //Add financial type config.
  $ft_options = (array) $utils
    ->wf_crm_apivalues('Contribution', 'getoptions', [
    'field' => "financial_type_id",
  ]);
  $this->form['contribution']['sets']['contribution']['civicrm_1_contribution_1_contribution_financial_type_id'] = [
    '#type' => 'select',
    '#title' => t('Financial Type'),
    '#default_value' => $financialType,
    '#options' => $ft_options,
    '#required' => TRUE,
  ];
  $this
    ->addAjaxItem("contribution:sets:contribution", "civicrm_1_contribution_1_contribution_financial_type_id", "..:custom");

  //Add Currency.
  $this->form['contribution']['sets']['contribution']['contribution_1_settings_currency'] = [
    '#type' => 'select',
    '#title' => t('Currency'),
    '#default_value' => wf_crm_aval($this->data, "contribution:1:currency"),
    '#options' => \CRM_Core_OptionGroup::values('currencies_enabled'),
    '#required' => TRUE,
  ];

  // Billing Address
  $n = wf_crm_aval($this->data, "billing:number_number_of_billing");
  $this->form['contribution']['sets']["billing_1_number_of_billing"] = [
    '#type' => 'select',
    '#title' => t('Enable Billing Address?'),
    '#default_value' => $n,
    '#options' => [
      t('No'),
      t('Yes'),
    ],
    '#prefix' => '<div class="number-of">',
    '#suffix' => '</div>',
    '#description' => t('Enable this section if you want billing address to be sent to the payment processor.'),
  ];
  $this
    ->addAjaxItem("contribution:sets", "billing_1_number_of_billing", "billing");

  //Move custom key after billing section.
  $customFields = $this->form['contribution']['sets']['custom'];
  unset($this->form['contribution']['sets']['custom']);
  $this->form['contribution']['sets']['custom'] = $customFields;
  if ($n) {

    // Add contribution fields
    foreach ($this->sets as $sid => $set) {
      if ($sid == 'billing_1_number_of_billing') {
        $fs = "contribution_sets_billing_{$n}_fieldset";
        $this->form['contribution']['sets']['billing'][$fs] = [
          '#type' => 'fieldset',
          '#title' => t('Billing Address'),
          '#attributes' => [
            'id' => $fs,
            'class' => [
              'web-civi-checkbox-set',
            ],
          ],
          'js_select' => $this
            ->addToggle($fs),
        ];
        if (isset($set['fields'])) {
          foreach ($set['fields'] as $fid => $field) {
            $fid = "civicrm_1_contribution_1_{$fid}";
            $this->form['contribution']['sets']['billing'][$fs][$fid] = $this
              ->addItem($fid, $field);
          }
        }
      }
    }
  }

  // LineItem
  $num = wf_crm_aval($this->data, "lineitem:number_number_of_lineitem", 0);
  $this->form['contribution']['sets']["lineitem_1_number_of_lineitem"] = [
    '#type' => 'select',
    '#title' => t('Additional Line items'),
    '#default_value' => $num,
    '#options' => range(0, 9),
    '#prefix' => '<div class="number-of">',
    '#suffix' => '</div>',
  ];
  $this
    ->addAjaxItem("contribution:sets", "lineitem_1_number_of_lineitem", "lineitem");
  for ($n = 1; $n <= $num; ++$n) {
    $fs = "contribution_sets_lineitem_{$n}_fieldset";
    $this->form['contribution']['sets']['lineitem'][$fs] = [
      '#type' => 'fieldset',
      '#title' => t('Line item %num', [
        '%num' => $n,
      ]),
      '#attributes' => [
        'id' => $fs,
        'class' => [
          'web-civi-checkbox-set',
        ],
      ],
      'js_select' => $this
        ->addToggle($fs),
    ];
    foreach ($this->sets['line_items']['fields'] as $fid => $field) {
      $fid = "civicrm_1_lineitem_{$n}_{$fid}";
      $this->form['contribution']['sets']['lineitem'][$fs][$fid] = $this
        ->addItem($fid, $field);
    }
  }

  // Receipt
  $n = wf_crm_aval($this->data, "receipt:number_number_of_receipt", 0);
  $this->form['contribution']['sets']["receipt_1_number_of_receipt"] = [
    '#type' => 'select',
    '#title' => t('Enable Receipt?'),
    '#default_value' => $n,
    '#options' => [
      t('No'),
      t('Yes'),
    ],
    '#prefix' => '<div class="number-of">',
    '#suffix' => '</div>',
    '#description' => t('Enable this section if you want an electronic receipt to be sent automatically to the contributor\'s email address.'),
  ];
  $this
    ->addAjaxItem("contribution:sets", "receipt_1_number_of_receipt", "receipt");
  if ($n) {
    $fs = "contribution_sets_receipt_{$n}_fieldset";
    $this->form['contribution']['sets']['receipt'][$fs] = [
      '#type' => 'fieldset',
      '#title' => t('Receipt'),
      '#attributes' => [
        'id' => $fs,
        'class' => [
          'web-civi-checkbox-set',
        ],
      ],
      'js_select' => $this
        ->addToggle($fs),
    ];
    $emailFields = [
      'receipt_1_number_of_receipt_receipt_from_name' => [
        '#type' => 'textfield',
        '#default_value' => wf_crm_aval($this->data, "receipt:number_number_of_receipt_receipt_from_name", ''),
        '#title' => t('Receipt From Name'),
        '#description' => t('Enter the FROM name to be used in receipt emails.'),
      ],
      'receipt_1_number_of_receipt_receipt_from_email' => [
        '#type' => 'textfield',
        '#default_value' => wf_crm_aval($this->data, "receipt:number_number_of_receipt_receipt_from_email", ''),
        '#title' => t('Receipt From Email'),
        '#required' => TRUE,
        '#description' => t('Enter the FROM email address to be used in receipt emails.'),
      ],
      'receipt_1_number_of_receipt_cc_receipt' => [
        '#type' => 'textfield',
        '#default_value' => wf_crm_aval($this->data, "receipt:number_number_of_receipt_cc_receipt", ''),
        '#title' => t('CC Receipt To'),
        '#description' => t('If you want member(s) of your organization to receive a carbon copy of each emailed receipt, enter one or more email addresses here. Multiple email addresses should be separated by a comma (e.g. jane@example.org, paula@example.org).'),
      ],
      'receipt_1_number_of_receipt_bcc_receipt' => [
        '#type' => 'textfield',
        '#default_value' => wf_crm_aval($this->data, "receipt:number_number_of_receipt_bcc_receipt", ''),
        '#title' => t('BCC Receipt To'),
        '#description' => t('If you want member(s) of your organization to receive a BLIND carbon copy of each emailed receipt, enter one or more email addresses here. Multiple email addresses should be separated by a comma (e.g. jane@example.org, paula@example.org).'),
      ],
      'receipt_1_number_of_receipt_pay_later_receipt' => [
        '#type' => 'textarea',
        '#default_value' => wf_crm_aval($this->data, "receipt:number_number_of_receipt_pay_later_receipt", ''),
        '#title' => t('Pay Later Text'),
        '#prefix' => '<div class="auto-width">',
        '#suffix' => '</div>',
        '#description' => t("Text added to the confirmation email, when the user selects the 'pay later' option (e.g. 'Mail your check to ... within 3 business days.')."),
      ],
      'receipt_1_number_of_receipt_receipt_text' => [
        '#type' => 'textarea',
        '#default_value' => wf_crm_aval($this->data, "receipt:number_number_of_receipt_receipt_text", ''),
        '#title' => t('Receipt Text'),
        '#prefix' => '<div class="auto-width">',
        '#suffix' => '</div>',
        '#description' => t('Enter a message you want included at the beginning of emailed receipts. NOTE: The text entered here will be used for both TEXT and HTML versions of receipt emails so we do not recommend including HTML tags / formatting here.'),
      ],
    ];
    foreach ($emailFields as $k => $fld) {
      $this->form['contribution']['sets']['receipt'][$fs][$k] = $fld;
    }
  }
}