You are here

private function wf_crm_webform_preprocess::addResources in Webform CiviCRM Integration 7.4

Same name and namespace in other branches
  1. 7.5 includes/wf_crm_webform_preprocess.inc \wf_crm_webform_preprocess::addResources()

Add necessary js & css to the form

1 call to wf_crm_webform_preprocess::addResources()
wf_crm_webform_preprocess::alterForm in includes/wf_crm_webform_preprocess.inc
Alter front-end of webforms: Called by hook_form_alter() when rendering a civicrm-enabled webform Add custom prefix. Display messages. Block users who should not have access. Set webform default values.

File

includes/wf_crm_webform_preprocess.inc, line 172

Class

wf_crm_webform_preprocess

Code

private function addResources() {
  $this->form['#attached']['js'][] = array(
    'data' => drupal_get_path('module', 'webform_civicrm') . '/js/webform_civicrm_forms.js',
    'scope' => 'footer',
  );
  $this->form['#attached']['css'][] = drupal_get_path('module', 'webform_civicrm') . '/css/webform_civicrm_forms.css';
  $config = CRM_Core_Config::singleton();

  // Variables to push to the client-side
  $js_vars = array();

  // JS Cache eliminates the need for most ajax state/province callbacks
  foreach ($this->data['contact'] as $c) {
    if (!empty($c['number_of_address'])) {
      $js_vars += array(
        'defaultCountry' => $config->defaultContactCountry,
        'defaultStates' => wf_crm_get_states($config->defaultContactCountry),
        'noCountry' => t('- First Choose a Country -'),
        'callbackPath' => url('webform-civicrm/js', array(
          'alias' => TRUE,
        )),
      );
      break;
    }
  }

  // Preprocess contribution page
  if (!empty($this->data['contribution'])) {
    $this
      ->addPaymentJs();
    $this->form['#attached']['js'][] = array(
      'data' => drupal_get_path('module', 'webform_civicrm') . '/js/webform_civicrm_payment.js',
      'scope' => 'footer',
    );
    $page_id = $this->data['contribution'][1]['contribution'][1]['contribution_page_id'];
    if (version_compare($this->civicrm_version, '4.7', '>=')) {
      $currency = $this->contribution_page['currency'];
      $contributionCallbackQuery = array(
        'currency' => $currency,
        'snippet' => 4,
      );
      $contributionCallbackUrl = 'civicrm/payment/form';
      $js_vars['processor_id_key'] = 'processor_id';
    }
    else {
      $contributionCallbackQuery = array(
        'reset' => 1,
        'id' => $page_id,
        'qfKey' => $this
          ->getQfKey(),
        'snippet' => 4,
      );
      $contributionCallbackUrl = 'civicrm/contribute/transact';
      $js_vars['processor_id_key'] = 'type';
    }
    if (!empty($this->data['contribution'][1]['contribution'][1]['is_test'])) {

      // RM: This is needed in order for CiviCRM to know that this is a 'test' (i.e. 'preview' action in CiviCRM) transaction - otherwise, CiviCRM defaults to 'live' and returns the payment form with public key for the live payment processor!
      $contributionCallbackQuery['action'] = CRM_Core_Action::description(CRM_Core_Action::PREVIEW);
    }
    $js_vars['contributionCallback'] = url($contributionCallbackUrl, array(
      'query' => $contributionCallbackQuery,
      'alias' => TRUE,
    ));

    // Add payment processor - note we have to search in 2 places because $this->loadMultipageData hasn't been run. Maybe it should be?
    $fid = 'civicrm_1_contribution_1_contribution_payment_processor_id';
    if (!empty($this->enabled[$fid])) {
      $js_vars['paymentProcessor'] = wf_crm_aval($this->form_state, 'storage:submitted:' . $this->enabled[$fid]);
    }
    else {
      $js_vars['paymentProcessor'] = $this
        ->getData($fid);
    }
  }
  if ($js_vars) {
    $this->form['#attached']['js'][] = array(
      'data' => array(
        'webform_civicrm' => $js_vars,
      ),
      'type' => 'setting',
    );
  }
}