You are here

function wf_crm_field_options in Webform CiviCRM Integration 7.5

Same name and namespace in other branches
  1. 7.3 webform_civicrm_utils.inc \wf_crm_field_options()
  2. 7.4 includes/utils.inc \wf_crm_field_options()

Get options for a specific field

Parameters

array $field: Webform component array

string $context: Where is this being called from?

array $data: Array of crm entity data

Return value

array

12 calls to wf_crm_field_options()
webform_civicrm_node_load in ./webform_civicrm.module
Implements hook_node_load().
webform_civicrm_update_7300 in ./webform_civicrm.install
Note: There are differences in how contact references and relationships work in the 3.x branch. Read the upgrade instructions at http://drupal.org/node/1615380
webform_civicrm_webform_submission_render_alter in ./webform_civicrm.module
Implements hook_webform_submission_render_alter().
wf_crm_admin_component::buildOptionsTable in includes/wf_crm_admin_component.inc
Interface similar to options_element module but with the important difference that options already exist in the civi db so this does not allow create/delete of options
wf_crm_admin_component::postProcess in includes/wf_crm_admin_component.inc
Custom Processing for CiviCRM webform component option lists

... See full list

File

includes/utils.inc, line 20
Webform CiviCRM module's common utility functions.

Code

function wf_crm_field_options($field, $context, $data) {
  $ret = [];
  $fields = wf_crm_get_fields();
  if ($pieces = wf_crm_explode_key($field['form_key'])) {
    list(, $c, $ent, $n, $table, $name) = $pieces;

    // Ensure we have complete info for this field
    if (isset($fields[$table . '_' . $name])) {
      $field += $fields[$table . '_' . $name];
    }
    if ($name === 'contact_sub_type') {
      list($contact_types, $sub_types) = wf_crm_get_contact_types();
      $ret = wf_crm_aval($sub_types, $data['contact'][$c]['contact'][1]['contact_type'], []);
    }
    elseif ($name === 'relationship_type_id') {
      $ret = wf_crm_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 = [
        1 => t('!a may view and edit !b', [
          '!a' => wf_crm_contact_label($c, $data, 'plain'),
          '!b' => wf_crm_contact_label($n, $data, 'plain'),
        ]),
        2 => t('!a may view and edit !b', [
          '!a' => wf_crm_contact_label($n, $data, 'plain'),
          '!b' => wf_crm_contact_label($c, $data, 'plain'),
        ]),
        3 => t('Both contacts may view and edit each other'),
      ];
    }
    elseif ($name === 'master_id' || wf_crm_aval($field, 'data_type') === 'ContactReference') {
      $contact_type = wf_crm_aval($field, 'reference_contact_type', 'contact');
      foreach ($data['contact'] as $num => $contact) {
        if ($num != $c || $name != 'master_id') {
          if ($contact_type == 'contact' || $contact_type == $contact['contact'][1]['contact_type']) {
            $ret[$num] = wf_crm_contact_label($num, $data, 'plain');
          }
        }
      }
    }
    elseif ($name == 'privacy') {
      $ret = wf_crm_get_privacy_options();
    }
    elseif (isset($field['table']) && $field['table'] === 'tag') {
      $split = explode('_', $name);
      $ret = wf_crm_get_tags($ent, wf_crm_aval($split, 1));
    }
    elseif (isset($field['table']) && $field['table'] === 'group') {
      $ret = wf_crm_apivalues('group', 'get', [
        'is_hidden' => 0,
      ], 'title');
    }
    elseif ($name === 'survey_id') {
      $ret = wf_crm_get_surveys(wf_crm_aval($data, "activity:{$c}:activity:1", []));
    }
    elseif ($name == 'event_id') {
      $ret = wf_crm_get_events($data['reg_options'], $context);
    }
    elseif ($table == 'contribution' && $name == 'is_test') {

      // Getoptions would return 'yes' and 'no' - this is a bit more descriptive
      $ret = [
        0 => t('Live Transactions'),
        1 => t('Test Mode'),
      ];
    }
    elseif ($table == 'membership' && $name == 'num_terms') {
      $ret = drupal_map_assoc(range(1, 9));
    }
    else {
      $params = [
        'field' => $name,
        'context' => 'create',
      ];

      // Special case for contribution_recur fields
      if ($table == 'contribution' && strpos($name, 'frequency_') === 0) {
        $table = 'contribution_recur';
      }

      // Use the Contribution table to pull up financial type id-s
      if ($table == 'membership' && $name == 'financial_type_id') {
        $table = 'contribution';
      }

      // Custom fields - use main entity
      if (substr($table, 0, 2) == 'cg') {
        $table = $ent;
      }
      else {

        // Pass data into api.getoptions for contextual filtering
        $params += wf_crm_aval($data, "{$ent}:{$c}:{$table}:{$n}", []);
      }
      $ret = wf_crm_apivalues($table, 'getoptions', $params);

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

    // Remove options that were set behind the scenes on the admin form
    if ($context != 'config_form' && !empty($field['extra']['multiple']) && !empty($field['expose_list'])) {
      foreach (wf_crm_aval($data, "{$ent}:{$c}:{$table}:{$n}:{$name}", []) as $key => $val) {
        unset($ret[$key]);
      }
    }
  }
  if (!empty($field['exposed_empty_option'])) {
    $ret = [
      0 => $field['exposed_empty_option'],
    ] + $ret;
  }
  return $ret;
}