You are here

function webform_civicrm_get_fields in Webform CiviCRM Integration 7.2

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

Fetches CiviCRM field data. @Param $var: name of variable to return: fields, tokens, lists, or sets @Return fields: The CiviCRM contact fields this module supports @Return tokens: Available tokens keyed to field ids @Return lists: Option lists keyed to option_group name @Return sets: Info on fieldsets (tables)

11 calls to webform_civicrm_get_fields()
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_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_enabled_fields in ./webform_civicrm_utils.inc
Get ids or values of enabled CiviCRM fields for a webform. @Param $node - node object @Param $submission - if supplied, will match field keys with submitted values
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

... See full list

File

./webform_civicrm_utils.inc, line 443
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_fields($var = 'fields') {
  static $fields = array();
  static $tokens = array();
  static $lists = array();
  static $sets = array();
  if (!$fields) {
    $config = CRM_Core_Config::singleton();
    $components = $config->enableComponents;

    // Field keys are in the format table_column
    // Use a # sign as a placeholder for field number in the title (or by default it will be appended to the end)
    // Setting 'expose_list' allows the value to be set on the config form
    // Set label for 'empty_option' for exposed lists that do not require input
    $fields['contact_contact_sub_type'] = array(
      'name' => t('Type of'),
      'type' => 'select',
      'extra' => array(
        'multiple' => 1,
      ),
      'expose_list' => TRUE,
    );
    foreach (array(
      'organization' => t('Organization Name'),
      'legal' => t('Legal Name'),
      'household' => t('Household Name'),
    ) as $key => $label) {
      $fields['contact_' . $key . '_name'] = array(
        'name' => $label,
        'type' => 'textfield',
        'contact_type' => $key == 'household' ? 'household' : 'organization',
      );
    }
    foreach (array(
      'first_name' => t('First Name'),
      'nick_name' => t('Nick Name'),
      'middle_name' => t('Middle Name'),
      'last_name' => t('Last Name'),
      'job_title' => t('Job Title'),
    ) as $key => $label) {
      $fields['contact_' . $key] = array(
        'name' => $label,
        'type' => 'textfield',
        'contact_type' => $key == 'nick_name' ? NULL : 'individual',
      );
    }
    foreach (array(
      'prefix' => t('Name Prefix'),
      'suffix' => t('Name Suffix'),
      'gender' => t('Gender'),
    ) as $key => $label) {
      $fields['contact_' . $key . '_id'] = array(
        'name' => $label,
        'type' => 'select',
        'contact_type' => 'individual',
      );
    }
    $fields['contact_birth_date'] = array(
      'name' => t('Birth Date'),
      'type' => 'date',
      'extra' => array(
        'start_date' => '-100 years',
        'end_date' => 'now',
      ),
      'contact_type' => 'individual',
    );
    $fields['contact_preferred_communication_method'] = array(
      'name' => t('Preferred Communication Method(s)'),
      'type' => 'select',
      'extra' => array(
        'multiple' => 1,
      ),
    );
    $fields['contact_privacy'] = array(
      'name' => t('Privacy Preferences'),
      'type' => 'select',
      'extra' => array(
        'multiple' => 1,
      ),
    );
    $fields['contact_preferred_language'] = array(
      'name' => t('Preferred Language'),
      'type' => 'select',
      'value' => $config->lcMessages,
    );
    $fields['contact_image_URL'] = array(
      'name' => t('Upload Image'),
      'type' => 'file',
      'extra' => array(
        'width' => 40,
      ),
    );
    $fields['contact_contact_id'] = array(
      'name' => t('Contact ID'),
      'type' => 'hidden',
      'extra' => array(
        'description' => t('(hidden field, use for post-processing)'),
      ),
    );
    $fields['contact_external_identifier'] = array(
      'name' => t('External ID'),
      'type' => 'hidden',
      'extra' => array(
        'description' => t('(hidden field, use for post-processing)'),
      ),
    );
    $fields['contact_cs'] = array(
      'name' => t('Generate Checksum'),
      'type' => 'hidden',
      'extra' => array(
        'description' => t('(hidden field, use to create hashed links)'),
      ),
    );
    $fields['email_email'] = array(
      'name' => t('Email'),
      'type' => 'email',
    );
    foreach (array(
      'street_address' => t('Street Address'),
      'supplemental_address_1' => t('Street Address # Line 2'),
      'supplemental_address_2' => t('Street Address # Line 3'),
      'city' => t('City'),
    ) as $key => $value) {
      $fields['address_' . $key] = array(
        'name' => $value,
        'type' => 'textfield',
        'extra' => array(
          'width' => $key == 'city' ? 20 : 60,
        ),
      );
    }
    $fields['address_postal_code'] = array(
      'name' => t('Postal Code'),
      'type' => 'textfield',
      'extra' => array(
        'width' => 7,
      ),
    );
    $fields['address_postal_code_suffix'] = array(
      'name' => t('Postal Code Suffix'),
      'type' => 'textfield',
      'extra' => array(
        'width' => 5,
        'description' => t('+4 digits of Zip Code'),
      ),
    );
    $fields['address_country_id'] = array(
      'name' => t('Country'),
      'type' => 'select',
      'value' => $config->defaultContactCountry,
    );
    $fields['address_state_province_id'] = array(
      'name' => t('State/Province'),
      'type' => 'textfield',
      'extra' => array(
        'maxlength' => 5,
        'width' => 4,
      ),
      'data_type' => 'state_province_abbr',
    );
    $fields['address_master_id'] = array(
      'name' => t('Share address of'),
      'type' => 'select',
      'expose_list' => TRUE,
      'extra' => array(
        'description' => t('Will overwrite address fields with those of the other contact.'),
      ),
      'empty_option' => t('- do not share -'),
    );
    $fields['phone_phone'] = array(
      'name' => t('Phone Number'),
      'type' => 'textfield',
    );
    $fields['phone_phone_type_id'] = array(
      'name' => t('Phone # Type'),
      'type' => 'select',
      'table' => 'phone',
      'expose_list' => TRUE,
    );
    foreach (array(
      'address' => t('Address # Location'),
      'phone' => t('Phone # Location'),
      'email' => t('Email # Location'),
    ) as $key => $label) {
      $fields[$key . '_location_type_id'] = array(
        'name' => $label,
        'type' => 'select',
        'expose_list' => TRUE,
        'value' => '1',
      );
    }
    $fields['website_url'] = array(
      'name' => t('Website'),
      'type' => 'textfield',
      'data_type' => 'Link',
    );
    $fields['website_website_type_id'] = array(
      'name' => t('Website # Type'),
      'type' => 'select',
      'expose_list' => TRUE,
    );
    $fields['other_group'] = array(
      'name' => t('Groups'),
      'type' => 'select',
      'extra' => array(
        'multiple' => 1,
      ),
      'table' => 'group',
      'expose_list' => TRUE,
    );
    $fields['other_tag'] = array(
      'name' => t('Tags'),
      'type' => 'select',
      'extra' => array(
        'multiple' => 1,
      ),
      'table' => 'tag',
      'expose_list' => TRUE,
    );
    $fields['activity_subject'] = array(
      'name' => t('Activity Subject'),
      'type' => 'textfield',
    );
    $fields['activity_details'] = array(
      'name' => t('Activity Details'),
      'type' => 'textarea',
    );
    $fields['activity_status_id'] = array(
      'name' => t('Activity Status'),
      'type' => 'select',
      'value' => '2',
      'expose_list' => TRUE,
    );
    $fields['activity_assignee_contact_id'] = array(
      'name' => t('Assign Activity to'),
      'type' => 'select',
      'expose_list' => TRUE,
      'empty_option' => t('- no one -'),
      'data_type' => 'ContactReference',
    );
    if (in_array('CiviCampaign', $components)) {
      $fields['activity_campaign_id'] = array(
        'name' => t('Campaign'),
        'type' => 'select',
        'expose_list' => TRUE,
        'empty_option' => t('- none -'),
      );
    }
    $fields['relationship_relationship_type_id'] = array(
      'name' => t('Relationship to Contact #'),
      'type' => 'select',
      'expose_list' => TRUE,
      'empty_option' => t('- no relationship -'),
    );
    $fields['relationship_is_active'] = array(
      'name' => t('Relationship to Contact # Is Active'),
      'type' => 'select',
      'expose_list' => TRUE,
      'value' => '1',
    );
    $fields['relationship_relationship_permission'] = array(
      'name' => t('Relationship to Contact # Permission'),
      'type' => 'select',
      'expose_list' => TRUE,
      'empty_option' => t('- no permissions -'),
    );
    $fields['relationship_start_date'] = array(
      'name' => t('Relationship to Contact # Start Date'),
      'type' => 'date',
      'extra' => array(
        'start_date' => '-50 years',
        'end_date' => '+50 years',
      ),
    );
    $fields['relationship_end_date'] = array(
      'name' => t('Relationship to Contact # End Date'),
      'type' => 'date',
      'extra' => array(
        'start_date' => '-50 years',
        'end_date' => '+50 years',
      ),
    );
    if (in_array('CiviEvent', $components)) {
      $fields['participant_event_id'] = array(
        'name' => t('Events'),
        'type' => 'select',
        'extra' => array(
          'multiple' => 1,
        ),
        'expose_list' => TRUE,
      );
      $fields['participant_role_id'] = array(
        'name' => t('Participant Role'),
        'type' => 'select',
        'expose_list' => TRUE,
        'value' => '1',
      );
      $fields['participant_status_id'] = array(
        'name' => t('Registration Status'),
        'type' => 'select',
        'expose_list' => TRUE,
        'value' => '1',
      );
      if (in_array('CiviCampaign', $components)) {
        $fields['participant_campaign_id'] = array(
          'name' => t('Campaign'),
          'type' => 'select',
          'expose_list' => TRUE,
          'empty_option' => t('- none -'),
        );
      }
    }
    $tokens = array(
      'display_name' => t('display name'),
      'first_name' => t('first name'),
      'nick_name' => t('nick name'),
      'middle_name' => t('middle name'),
      'last_name' => t('last name'),
      'individual_prefix' => t('name prefix'),
      'individual_suffix' => t('name suffix'),
      'gender' => t('gender'),
      'birth_date' => t('birth date'),
      'job_title' => t('job title'),
      'current_employer' => t('current employer'),
      'contact_id' => t('contact id'),
      'street_address' => t('street address'),
      'city' => t('city'),
      'state_province' => t('state/province abbr'),
      'state_province_name' => t('state/province full'),
      'postal_code' => t('postal code'),
      'country' => t('country'),
      'world_region' => t('world region'),
      'phone' => t('phone number'),
      'email' => t('email'),
    );

    // key: key accepted by civicrm api
    // value: name of civicrm_option_group
    $lists = array(
      'prefix_id' => 'individual_prefix',
      'suffix_id' => 'individual_suffix',
      'gender_id' => 'gender',
      'preferred_communication_method' => 'preferred_communication_method',
      'preferred_language' => 'languages',
      'privacy' => 'privacy',
      'country_id' => 'country',
      'phone_type_id' => 'phone_type',
      'location_type_id' => 'location_type',
      'website_type_id' => 'website_type',
      'master_id' => 'master_id',
      'relationship_type_id' => 'relationship_type_id',
      'is_active' => 'yes_no',
      'relationship_permission' => 'relationship_permission',
      'contact_sub_type' => 'contact_sub_type',
      'group' => 'group',
      'tag' => 'tag',
      'assignee_contact_id' => 'group_contact',
      'status_id' => 'activity_status',
      'event_id' => 'event',
      'role_id' => 'participant_role',
      'campaign_id' => 'campaign',
    );
    $sets = array(
      'contact' => array(
        'entity_type' => 'contact',
        'label' => t('Contact Fields'),
      ),
      'other' => array(
        'entity_type' => 'contact',
        'label' => t('Tags and Groups'),
        'max_instances' => 1,
      ),
      'address' => array(
        'entity_type' => 'contact',
        'label' => t('Address'),
        'max_instances' => 9,
      ),
      'phone' => array(
        'entity_type' => 'contact',
        'label' => t('Phone'),
        'max_instances' => 9,
      ),
      'email' => array(
        'entity_type' => 'contact',
        'label' => t('Email'),
        'max_instances' => 9,
      ),
      'website' => array(
        'entity_type' => 'contact',
        'label' => t('Website'),
        'max_instances' => 9,
      ),
      'activity' => array(
        'entity_type' => 'activity',
        'label' => t('Expose Activity Fields'),
      ),
      'relationship' => array(
        'entity_type' => 'contact',
        'label' => t('Relationship to Contact'),
      ),
    );
    if (in_array('CiviEvent', $components)) {
      $sets['participant'] = array(
        'entity_type' => 'participant',
        'label' => t('Participant'),
        'max_instances' => 9,
      );
    }

    // Pull custom fields and match to Webform element types
    $custom_types = array(
      'Select' => array(
        'type' => 'select',
      ),
      'Multi-Select' => array(
        'type' => 'select',
        'extra' => array(
          'multiple' => 1,
        ),
      ),
      'AdvMulti-Select' => array(
        'type' => 'select',
        'extra' => array(
          'multiple' => 1,
        ),
      ),
      'Radio' => array(
        'type' => 'select',
        'extra' => array(
          'aslist' => 0,
        ),
      ),
      'CheckBox' => array(
        'type' => 'select',
        'extra' => array(
          'multiple' => 1,
        ),
      ),
      'Text' => array(
        'type' => 'textfield',
      ),
      'TextArea' => array(
        'type' => 'textarea',
      ),
      'RichTextEditor' => array(
        'type' => 'textarea',
      ),
      'Select Date' => array(
        'type' => 'date',
      ),
      'Link' => array(
        'type' => 'textfield',
      ),
      'Select Country' => array(
        'type' => 'select',
      ),
      'Multi-Select Country' => array(
        'type' => 'select',
        'extra' => array(
          'multiple' => 1,
        ),
      ),
      'Select State/Province' => array(
        'type' => 'select',
      ),
      'Multi-Select State/Province' => array(
        'type' => 'select',
        'extra' => array(
          'multiple' => 1,
        ),
      ),
      'Autocomplete-Select' => array(
        'type' => 'select',
      ),
    );
    $sp = CRM_Core_DAO::VALUE_SEPARATOR;
    $custom_extends = "'contact','individual','organization','household','address','activity'";
    if (in_array('CiviEvent', $components, TRUE)) {
      $custom_extends .= ",'participant'";
    }
    if (in_array('CiviCase', $components, TRUE)) {
      $custom_extends .= ",'case'";
    }
    $sql = "\n      SELECT cf.*, cg.title AS custom_group_name, LOWER(cg.extends) AS entity_type, cg.extends_entity_column_id, cg.extends_entity_column_value AS sub_types, cg.is_multiple, cg.max_multiple, cg.id AS custom_group_id\n      FROM civicrm_custom_field cf\n      INNER JOIN civicrm_custom_group cg ON cg.id = cf.custom_group_id\n      WHERE cf.is_active <> 0 AND cg.extends IN ({$custom_extends}) AND cg.is_active <> 0\n      ORDER BY cf.custom_group_id, cf.weight";
    $dao =& CRM_Core_DAO::executeQuery($sql);
    while ($dao
      ->fetch()) {
      if (isset($custom_types[$dao->html_type])) {
        if ($dao->entity_type == 'address') {
          $set = 'address';
        }
        else {
          $set = 'cg' . $dao->custom_group_id;
          $sets[$set]['label'] = $dao->custom_group_name;
          if ($dao->entity_type != 'activity' && $dao->entity_type != 'participant' && $dao->entity_type != 'case') {
            $sets[$set]['entity_type'] = 'contact';
            if ($dao->entity_type != 'contact') {
              $sets[$set]['contact_type'] = $dao->entity_type;
            }
            if ($dao->is_multiple) {
              $sets[$set]['max_instances'] = $dao->max_multiple ? $dao->max_multiple : 9;
            }
            else {
              $sets[$set]['max_instances'] = 1;
            }
          }
          else {
            $sets[$set]['entity_type'] = $dao->entity_type;
          }
          if ($dao->sub_types) {
            $sets[$set]['sub_types'] = explode($sp, trim($dao->sub_types, $sp));
          }
          if ($dao->extends_entity_column_id) {
            $sets[$set]['extension_of'] = $dao->extends_entity_column_id;
          }
        }
        $id = $set . '_custom_' . $dao->id;
        $label = drupal_strtolower($dao->label);
        $fields[$id] = $custom_types[$dao->html_type];
        $fields[$id]['name'] = $dao->label;
        $fields[$id]['mandatory'] = $dao->is_required;
        $fields[$id]['extra']['description'] = $dao->help_pre;
        $fields[$id]['value'] = str_replace($sp, ',', trim($dao->default_value, $sp));
        $fields[$id]['data_type'] = $dao->data_type;
        if ($fields[$id]['type'] == 'date') {
          $fields[$id]['extra']['start_date'] = ($dao->start_date_years ? '-' . $dao->start_date_years : '-50') . ' years';
          $fields[$id]['extra']['end_date'] = ($dao->end_date_years ? '+' . $dao->end_date_years : '+50') . ' years';
        }
        elseif ($og = $dao->option_group_id) {
          $lists['custom_' . $dao->id] = $og;
        }
        elseif ($dao->html_type == 'Select Country' || $dao->html_type == 'Multi-Select Country') {
          $lists['custom_' . $dao->id] = 'country';
        }
        elseif ($dao->html_type == 'Select State/Province' || $dao->html_type == 'Multi-Select State/Province') {
          $lists['custom_' . $dao->id] = 'state_province';
        }
        elseif ($fields[$id]['data_type'] == 'ContactReference') {
          $lists['custom_' . $dao->id] = 'group_contact';
          if (!empty($dao->filter) && ($pos = strpos($dao->filter, 'group=')) !== FALSE) {
            $len = strlen($dao->filter);
            $pos += 6;
            $fields[$id]['extra']['civicrm_group'] = '';
            while ($pos < $len && is_numeric($dao->filter[$pos])) {
              $fields[$id]['extra']['civicrm_group'] .= $dao->filter[$pos++];
            }
          }
        }
        elseif ($fields[$id]['type'] == 'select') {
          $lists['custom_' . $dao->id] = 'yes_no';
        }
        elseif ($fields[$id]['type'] == 'textarea') {
          $fields[$id]['extra']['cols'] = $dao->note_columns;
          $fields[$id]['extra']['rows'] = $dao->note_rows;
        }
      }
    }
  }
  return ${$var};
}