function webform_civicrm_get_options in Webform CiviCRM Integration 6.2
Same name and namespace in other branches
- 6 webform_civicrm_utils.inc \webform_civicrm_get_options()
- 7 webform_civicrm_utils.inc \webform_civicrm_get_options()
- 7.2 webform_civicrm_utils.inc \webform_civicrm_get_options()
Get option values from various civicrm tables @Param $option_group: option group name or id @Param $param: optional extra info for the query (passing a param will bypass caching) @Return array
7 calls to webform_civicrm_get_options()
- webform_civicrm_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.
- webform_civicrm_configure_form_builder in ./
webform_civicrm_admin.inc - Form to configure CiviCRM options for a Webform Called indirectly from hook_menu() for D7-D6 compatibility
- webform_civicrm_field_options in ./
webform_civicrm_utils.inc - Get options for a specific field @Param $field: Webform component array @Param $context: Where is this being called from? @Param $data: CiviCRM entity data
- webform_civicrm_js_options in ./
webform_civicrm_utils.inc - Callback to serve AJAX requests.
- _webform_civicrm_form_validate in ./
webform_civicrm_forms.inc - Recursive validation callback for webform submissions.
File
- ./
webform_civicrm_utils.inc, line 19 - Webform CiviCRM module's common utility functions. The code in this file is cross-compatible with D6/Civi3 and D7/Civi4 Drupal-version-specific functions belong in webform_civicrm_dx_functions.inc
Code
function webform_civicrm_get_options($option_group, $param = NULL) {
static $cache = array();
if ($param || empty($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 == 'tag') {
// $param is entity type i.e. contact
return CRM_Core_BAO_Tag::getTags("civicrm_{$param}", $ret, NULL, '- ');
}
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') {
// For older versions of CiviCRM
if (method_exists('CRM_Core_PseudoConstant', 'languages')) {
return CRM_Core_PseudoConstant::languages();
}
else {
$result = webform_civicrm_api('contact', 'getoptions', array(
'field' => 'preferred_language',
));
return $result['values'];
}
}
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 == '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 DESC';
}
elseif ($option_group == 'group_contact') {
$result = webform_civicrm_api('contact', 'get', array(
'group' => array(
$param => 1,
),
'return.display_name' => 1,
'rowCount' => 100,
'sort' => 'sort_name ASC',
));
if (!empty($result['values'])) {
foreach ($result['values'] as $val) {
$ret[$val['contact_id']] = $val['display_name'];
}
}
return $ret;
}
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 reserved activity types
if ($option_group == 'activity_type') {
$sql .= ' AND (component_id IS NULL' . ($param ? " OR component_id IN ({$param}))" : ') ORDER BY label');
}
else {
$sql .= ' ORDER BY weight, label';
}
}
$dao = CRM_Core_DAO::executeQuery($sql, $vars);
while ($dao
->fetch()) {
$ret[$dao->value] = $dao->label;
}
if ($param) {
return $ret;
}
$cache[$option_group] = $ret;
}
return $cache[$option_group];
}