You are here

function webform_civicrm_field_options in Webform CiviCRM Integration 7.2

Same name and namespace in other branches
  1. 6.2 webform_civicrm_utils.inc \webform_civicrm_field_options()

Get options for a specific field @Param $field: Webform component array @Param $context: Where is this being called from? @Param $data: CiviCRM entity data

4 calls to webform_civicrm_field_options()
webform_civicrm_configure_form_item in ./webform_civicrm_admin.inc
Build a field item for the configure form
webform_civicrm_configure_form_submit in ./webform_civicrm_admin.inc
Submission handler, saves CiviCRM options for a Webform node
webform_civicrm_process_group_selection in ./webform_civicrm_admin.inc
Custom Processing for CiviCRM ContactReference fields
_webform_civicrm_webform_component_form_alter in ./webform_civicrm_admin.inc
Alter back-end webform component edit forms. Called by hook_form_alter() whenever editing a webform component.

File

./webform_civicrm_utils.inc, line 137
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_field_options($field, $context, $data) {
  $ret = array();
  $lists = webform_civicrm_get_fields('lists');
  $fields = webform_civicrm_get_fields();
  if ($pieces = webform_civicrm_explode_key($field['form_key'])) {
    list($lobo, $c, $ent, $n, $table, $name) = $pieces;

    // Ensure we have complete info for this field
    $field += $fields[$table . '_' . $name];
    if ($name === 'contact_sub_type') {
      list($contact_types, $sub_types) = webform_civicrm_get_contact_types();
      $ret = $sub_types[$data['contact'][$c]['contact'][1]['contact_type']];
    }
    elseif ($name === 'relationship_type_id') {
      $ret = webform_civicrm_get_contact_relationship_types($data['contact'][$c]['contact'][1]['contact_type'], $data['contact'][$n]['contact'][1]['contact_type'], $data['contact'][$c]['contact'][1]['contact_sub_type'], $data['contact'][$n]['contact'][1]['contact_sub_type']);
    }
    elseif ($name === 'relationship_permission') {
      $ret = array(
        1 => t('Contact !a may view and edit contact !b', array(
          '!a' => $c,
          '!b' => $n,
        )),
        2 => t('Contact !a may view and edit contact !b', array(
          '!a' => $n,
          '!b' => $c,
        )),
        3 => t('Both contacts may view and edit each other'),
      );
    }
    elseif ($name === 'master_id') {
      foreach ($data['contact'] as $num => $contact) {
        if ($num != $c) {
          $ret[$num] = t('Contact !num', array(
            '!num' => $num,
          ));
        }
      }
    }
    elseif ($name === 'tag') {
      $ret = webform_civicrm_get_options('tag', $ent);
    }
    elseif ($name === 'status_id' && $table === 'participant') {
      require_once 'CRM/Event/PseudoConstant.php';
      $ret = CRM_Event_PseudoConstant::participantStatus(NULL, NULL, 'label');
    }
    elseif (isset($lists[$name])) {
      $param = NULL;
      if ($name === 'event_id') {
        $param = $data['reg_options'];
      }

      // If this is a contract reference or assignee field, set group id as param
      if (webform_civicrm_aval($field, 'data_type') === 'ContactReference') {
        if ($name === 'assignee_contact_id') {
          $param = webform_civicrm_aval($data, 'activity:1:assignee_group');
        }
        else {
          $param = webform_civicrm_aval($field, 'extra:civicrm_group');
        }

        // List webform contacts if no group specified
        if (!$param) {
          foreach ($data['contact'] as $num => $contact) {
            $ret['#' . $num] = t('Contact !num', array(
              '!num' => $num,
            ));
          }
        }
      }
      if (!$ret) {
        $ret = webform_civicrm_get_options($lists[$name], $param);

        // Hack to format money data correctly
        if (!empty($field['data_type']) && $field['data_type'] === 'Money') {
          $old = $ret;
          $ret = array();
          foreach ($old as $key => $val) {
            $ret[number_format(str_replace(',', '', $key), 2, '.', '')] = $val;
          }
        }
      }
    }

    // Remove options that were set behind the scenes
    if ($context != 'config_form' && !empty($field['extra']['multiple']) && !empty($field['expose_list'])) {
      foreach (webform_civicrm_aval($data, "{$ent}:{$c}:{$table}:{$n}:{$name}", array()) as $key => $val) {
        unset($ret[$key]);
      }
    }
  }
  return $context == 'component_insert' ? webform_civicrm_array2str($ret) : $ret;
}