You are here

function webform_civicrm_get_options in Webform CiviCRM Integration 6

Same name and namespace in other branches
  1. 6.2 webform_civicrm_utils.inc \webform_civicrm_get_options()
  2. 7 webform_civicrm_utils.inc \webform_civicrm_get_options()
  3. 7.2 webform_civicrm_utils.inc \webform_civicrm_get_options()

Get options from civicrm_option_value OR civicrm_group OR civicrm_country OR civicrm_state_province tables @Param $option_group: option group name or id @Param $var: Can return values as a Webform-style string, or a FAPI style array. Specify 'str' or 'arr', matey! @Param $null_label: If returning FAPI array, provide label if first option should be "none"

6 calls to webform_civicrm_get_options()
webform_civicrm_configure_form in ./webform_civicrm_forms.inc
Form to configure CiviCRM options for a Webform
webform_civicrm_configure_form_submit in ./webform_civicrm_forms.inc
Submission handler, saves CiviCRM options for a Webform node
webform_civicrm_contact_match in ./webform_civicrm_utils.inc
Create or update CiviCRM contact Called by both presave and insert webform hooks in order to handle the optional contact_id field correctly
webform_civicrm_js_options in ./webform_civicrm_utils.inc
Return CiviCRM options via AJAX request
webform_civicrm_process_group_selection in ./webform_civicrm_forms.inc
Custom Processing for CiviCRM groups form

... See full list

File

./webform_civicrm_utils.inc, line 244
Webform CiviCRM module's utility functions.

Code

function webform_civicrm_get_options($option_group, $var = 'str', $null_label = '') {
  $params = array();
  if ($option_group == 'country') {
    $sql = "SELECT name AS label, id AS value FROM civicrm_country";
    $config = CRM_Core_Config::singleton();
    if (!empty($config->countryLimit)) {
      $sql .= ' WHERE id IN (' . implode(',', $config->countryLimit) . ')';
    }
    $sql .= ' ORDER BY name';
  }
  elseif ($option_group == 'state') {
    $config = CRM_Core_Config::singleton();
    $sql = 'SELECT name AS label, id AS value FROM civicrm_state_province WHERE country_id = ' . $config->defaultContactCountry . ' ORDER BY name';
  }
  elseif ($option_group == 'groups' || $option_group == 'mailing_lists') {
    $sql = 'SELECT id AS value, title AS label FROM civicrm_group WHERE is_active = 1';
    if ($option_group == 'mailing_lists') {
      $sql .= " AND group_type LIKE '%2%' AND visibility = 'Public Pages'";
    }
  }
  else {
    $sql = 'SELECT value, label FROM civicrm_option_value WHERE is_active <> 0 AND option_group_id = ';
    if (is_numeric($option_group)) {
      $sql .= '%1';
      $params[1] = array(
        $option_group,
        'Integer',
      );
    }
    else {
      $sql .= "(SELECT id FROM civicrm_option_group WHERE name = %1)";
      $params[1] = array(
        $option_group,
        'String',
      );
    }

    // Exclude reserved activity types
    if ($option_group == 'activity_type') {
      $sql .= ' AND component_id IS NULL';
    }
    $sql .= ' ORDER BY weight, name';
  }
  $dao =& CRM_Core_DAO::executeQuery($sql, $params);
  $arr = array();
  if ($null_label) {
    $arr[] = $null_label;
  }
  $str = '';
  while ($dao
    ->fetch()) {
    $arr[$dao->value] = $dao->label;
    $str .= ($str ? "\n" : '') . $dao->value . '|' . $dao->label;
  }
  return ${$var};
}