You are here

function wf_crm_get_options in Webform CiviCRM Integration 7.3

Get option values from various civicrm tables

Parameters

$option_group: Option group name or id

$param: Optional extra info for the query (passing a param will bypass caching)

Return value

array

8 calls to wf_crm_get_options()
wf_crm_add_remove in ./webform_civicrm_forms.inc
Handle adding/removing multivalued data for a contact/activity/etc. Currently used only for groups and tags, but written with expansion in mind.
wf_crm_ajax in ./contact_component.inc
Drupal page callback to serve AJAX requests.
wf_crm_configure_form in ./webform_civicrm_admin.inc
Drupal form builder callback Form to configure CiviCRM options for a Webform
wf_crm_contact_get in ./webform_civicrm_forms.inc
Fetch all relevant data for a given contact Used to load contacts for pre-filling a webform, and also to fill in a contact via ajax
wf_crm_field_options in ./webform_civicrm_utils.inc
Get options for a specific field

... See full list

File

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

Code

function wf_crm_get_options($option_group, $param = NULL) {
  static $cache = array();
  if ($param || !isset($cache[$option_group])) {
    $vars = $ret = array();
    if ($option_group == 'privacy') {

      // Privacy options aren't in the database as options; they are separate contact fields.
      return array(
        'do_not_email' => ts('Do not email'),
        'do_not_phone' => ts('Do not phone'),
        'do_not_mail' => ts('Do not mail'),
        'do_not_sms' => ts('Do not sms'),
        'do_not_trade' => ts('Do not trade'),
      );
    }
    if ($option_group == 'yes_no') {
      return array(
        1 => t('Yes'),
        0 => t('No'),
      );
    }
    if ($option_group == 'country') {
      $sql = 'SELECT name AS label, id AS value FROM civicrm_country';
      $config = CRM_Core_Config::singleton();
      if (!empty($config->countryLimit) && is_array($config->countryLimit)) {
        $sql .= ' WHERE id IN (' . implode(',', $config->countryLimit) . ')';
      }
      $sql .= ' ORDER BY name';
    }
    elseif ($option_group == 'state_province') {
      $value = $param ? 'UPPER(abbreviation)' : 'id';
      if (!$param || $param == 'default') {
        $config = CRM_Core_Config::singleton();
        if (!$param && !empty($config->provinceLimit)) {
          $param = implode(',', $config->provinceLimit);
        }
        else {
          $param = (int) $config->defaultContactCountry;
        }
      }
      else {
        $param = (int) $param;
      }
      $sql = "SELECT name AS label, {$value} AS value FROM civicrm_state_province WHERE country_id IN ({$param}) ORDER BY name";
    }
    elseif ($option_group == 'county') {
      if (!$param) {
        return array();
      }
      $sql = "SELECT name AS label, id AS value FROM civicrm_county WHERE state_province_id = {$param} ORDER BY name";
    }
    elseif ($option_group == 'tag') {
      require_once 'CRM/Core/BAO/Tag.php';

      // $param must contain entity i.e. contact and optionally a tagset id
      return CRM_Core_BAO_Tag::getTags("civicrm_{$param['entity']}", $ret, wf_crm_aval($param, 'tagset'), '- ');
    }
    elseif ($option_group == 'group' || $option_group == 'mailing_lists') {
      $sql = 'SELECT id AS value, title AS label FROM civicrm_group WHERE is_active = 1 AND is_hidden = 0';
      if ($option_group == 'mailing_lists') {
        $sql .= " AND group_type LIKE '%2%' AND visibility = 'Public Pages'";
      }
    }
    elseif ($option_group == 'languages') {
      require_once 'CRM/Core/PseudoConstant.php';
      return CRM_Core_PseudoConstant::$option_group();
    }
    elseif ($option_group == 'location_type') {
      $sql = 'SELECT display_name AS label, id AS value FROM civicrm_location_type WHERE is_active = 1 ORDER BY is_default DESC';
    }
    elseif ($option_group == 'campaign') {
      $sql = 'SELECT title AS label, id AS value FROM civicrm_campaign WHERE is_active = 1 AND (end_date >= NOW() OR end_date IS NULL) ORDER BY start_date DESC';
    }
    elseif ($option_group == 'survey') {
      $sql = 'SELECT title AS label, id AS value FROM civicrm_survey WHERE is_active = 1';
      if (!empty($param['activity_campaign_id'])) {
        $sql .= ' AND campaign_id = %1';
        $vars[1] = array(
          $param['activity_campaign_id'],
          'Integer',
        );
      }
      if (!empty($param['activity_type_id'])) {
        $sql .= ' AND activity_type_id = %2';
        $vars[2] = array(
          $param['activity_type_id'],
          'Integer',
        );
      }
      $sql .= ' ORDER BY title';
    }
    elseif ($option_group == 'event') {
      $sql = "SELECT title AS label, CONCAT(id, '-', event_type_id) AS value FROM civicrm_event WHERE is_template = 0";
      if (empty($param['show_past_events'])) {
        $sql .= ' AND (end_date >= NOW() OR end_date IS NULL) AND is_active = 1';
      }
      if (is_numeric($param['event_type'])) {
        $sql .= ' AND event_type_id = ' . $param['event_type'];
      }
      $sql .= ' ORDER BY start_date ' . ($param['context'] == 'config_form' ? 'DESC' : '');
    }
    else {
      $sql = 'SELECT value, label FROM civicrm_option_value WHERE is_active <> 0 AND option_group_id = ';
      if (is_numeric($option_group)) {
        $sql .= '%1';
        $vars[1] = array(
          $option_group,
          'Integer',
        );
      }
      else {
        $sql .= '(SELECT id FROM civicrm_option_group WHERE name = %1)';
        $vars[1] = array(
          $option_group,
          'String',
        );
      }

      // Exclude component activity types
      if ($option_group == 'activity_type') {
        if (!$param) {
          $sql .= ' AND component_id IS NULL ORDER BY label';
        }
        else {
          $sql .= " AND component_id = (SELECT id FROM civicrm_component WHERE name = '{$param}')";
        }
      }
      else {
        $sql .= ' ORDER BY weight, label';
      }
    }
    $dao =& CRM_Core_DAO::executeQuery($sql, $vars);

    // Localize the country/province names if in an non-en_US locale
    if ($option_group == 'state_province' || $option_group == 'country') {
      while ($dao
        ->fetch()) {
        $ret[$dao->value] = $dao->label;
      }
      $tsLocale = CRM_Utils_System::getUFLocale();
      if ($tsLocale != '' and $tsLocale != 'en_US') {
        $context = $option_group == 'state_province' ? 'province' : 'country';
        $i18n =& CRM_Core_I18n::singleton();
        $i18n
          ->localizeArray($ret, array(
          'context' => $context,
        ));
        if (method_exists('CRM_Utils_Array', 'asort')) {
          CRM_Utils_Array::asort($ret);
        }
        else {
          asort($ret);
        }
      }
    }
    else {
      while ($dao
        ->fetch()) {
        $ret[$dao->value] = ts($dao->label);
      }
    }
    $dao
      ->free();

    // Do not cache if param is set
    if ($param) {
      return $ret;
    }
    $cache[$option_group] = $ret;
  }
  return $cache[$option_group];
}