You are here

public static function wf_crm_admin_form::handleDynamicCustomField in Webform CiviCRM Integration 7.4

Same name and namespace in other branches
  1. 7.5 includes/wf_crm_admin_form.inc \wf_crm_admin_form::handleDynamicCustomField()

When a custom field is saved/deleted in CiviCRM, sync webforms with dynamic fieldsets.

Parameters

string $op:

int $fid:

int $gid:

3 calls to wf_crm_admin_form::handleDynamicCustomField()
webform_civicrm_civicrm_post in ./webform_civicrm.module
Implements hook_civicrm_post().
webform_civicrm_civicrm_postSave_civicrm_custom_field in ./webform_civicrm.module
Implements hook_civicrm_postSave_tableName().
webform_civicrm_civicrm_pre in ./webform_civicrm.module
Implementation of hook_civicrm_pre()

File

includes/wf_crm_admin_form.inc, line 2064
Webform CiviCRM module's admin form.

Class

wf_crm_admin_form
@file Webform CiviCRM module's admin form.

Code

public static function handleDynamicCustomField($op, $fid, $gid) {
  module_load_include('inc', 'webform_civicrm', 'includes/utils');
  module_load_include('inc', 'webform', 'includes/webform.components');
  $sets = wf_crm_get_fields('sets');
  $webforms = db_query("SELECT nid FROM {webform_civicrm_forms} WHERE data LIKE '%\"dynamic_custom_cg{$gid}\";i:1;%'");
  foreach ($webforms as $webform) {
    $field_name = "cg{$gid}_custom_{$fid}";
    $field_info = wf_crm_get_field($field_name);

    // $field_info contains old data, so re-fetch
    $fieldConfigs = wf_civicrm_api('CustomField', 'getsingle', array(
      'id' => $fid,
    ));

    // Reset node cache to avoid stale data, so as not to insert the same component twice
    $node = node_load($webform->nid, NULL, TRUE);
    $enabled = wf_crm_enabled_fields($node, NULL, TRUE);
    $updated = array();

    // Handle update & delete of existing components
    foreach ($node->webform['components'] as $component) {
      if (substr($component['form_key'], 0 - strlen($field_name)) === $field_name) {
        if ($pieces = wf_crm_explode_key($component['form_key'])) {
          list(, $c, $ent, $n, $table, $name) = $pieces;
          if (!empty($node->webform_civicrm['data'][$ent][$c]["dynamic_custom_cg{$gid}"])) {
            if ($op == 'delete' || $op == 'disable') {
              webform_component_delete($node, $component);
            }
            elseif (isset($field_info)) {
              $component['name'] = $fieldConfigs['label'];
              $component['required'] = $fieldConfigs['is_required'];
              $component['value'] = $fieldConfigs['default_value'] ? $fieldConfigs['default_value'] : "";
              $component['extra']['description'] = $fieldConfigs['help_pre'] ? $fieldConfigs['help_pre'] : "";
              $component['extra']['description_above'] = $field_info['extra']['description_above'];
              webform_component_update($component);
            }
            $updated[$ent][$c] = 1;
          }
        }
      }
    }

    // Handle create new components
    if ($op == 'create' || $op == 'enable') {
      $ent = $sets["cg{$gid}"]['entity_type'];
      foreach ($node->webform_civicrm['data'][$ent] as $c => $item) {
        if (!empty($item["dynamic_custom_cg{$gid}"]) && empty($updated[$ent][$c])) {
          $new = $field_info;
          $new['nid'] = $node->nid;
          $new['form_key'] = "civicrm_{$c}_{$ent}_1_{$field_name}";
          $new['weight'] = 0;
          foreach ($node->webform['components'] as $component) {
            if (strpos($component['form_key'], "civicrm_{$c}_{$ent}_1_cg{$gid}_custom_") === 0 && $component['weight'] >= $new['weight']) {
              $new['weight'] = $component['weight'] + 1;
              $new['pid'] = $component['pid'];
            }
          }
          if ($op == 'enable') {
            $new['name'] = $fieldConfigs['label'];
            $new['required'] = $fieldConfigs['is_required'];
            $new['value'] = implode(',', wf_crm_explode_multivalue_str($fieldConfigs['default_value']));
            $new['data_type'] = $fieldConfigs['data_type'];
            $custom_types = wf_crm_custom_types_map_array();
            $new['type'] = $custom_types[$fieldConfigs['html_type']]['type'];
          }
          wf_crm_admin_form::insertComponent($new, $enabled, $node->webform_civicrm);
        }
      }
    }
  }
}