You are here

function theme_webform_civicrm_components_form in Webform CiviCRM Integration 7.3

Theme override for webform components form.

Parameters

$variables: Includes the form array.

Return value

string Formatted HTML form, ready for display.

File

./webform_civicrm_admin.inc, line 1499

Code

function theme_webform_civicrm_components_form($variables) {
  $form = $variables['form'];
  $form['components']['#attached']['library'][] = array(
    'webform',
    'admin',
  );

  // TODO: Attach these. See http://drupal.org/node/732022.
  drupal_add_tabledrag('webform-components', 'order', 'sibling', 'webform-weight');
  drupal_add_tabledrag('webform-components', 'match', 'parent', 'webform-pid', 'webform-pid', 'webform-cid');
  $node = $form['#node'];

  // Prepare for CiviCRM processing
  $enabled = array();
  unset($form['add']['type']['#options']['civicrm_contact']);
  if (!empty($node->webform_civicrm)) {
    civicrm_initialize();
    $enabled = wf_crm_enabled_fields($node);
    $form['components']['#attached']['css'][] = drupal_get_path('module', 'webform_civicrm') . '/webform_civicrm_admin.css';
  }
  $header = array(
    t('Label'),
    t('Type'),
    t('Value'),
    t('Mandatory'),
    t('Weight'),
    array(
      'data' => t('Operations'),
      'colspan' => 3,
    ),
  );
  $rows = array();

  // Add a row containing form elements for a new item.
  unset($form['add']['name']['#title'], $form['add_type']['#description']);
  $form['add']['name']['#attributes']['rel'] = t('New component name');
  $form['add']['name']['#attributes']['class'] = array(
    'webform-default-value',
  );
  $form['add']['cid']['#attributes']['class'] = array(
    'webform-cid',
  );
  $form['add']['pid']['#attributes']['class'] = array(
    'webform-pid',
  );
  $form['add']['weight']['#attributes']['class'] = array(
    'webform-weight',
  );
  $row_data = array(
    drupal_render($form['add']['name']),
    drupal_render($form['add']['type']),
    '',
    drupal_render($form['add']['mandatory']),
    drupal_render($form['add']['cid']) . drupal_render($form['add']['pid']) . drupal_render($form['add']['weight']),
    array(
      'colspan' => 3,
      'data' => drupal_render($form['add']['add']),
    ),
  );
  $add_form = array(
    'data' => $row_data,
    'class' => array(
      'draggable',
      'webform-add-form',
    ),
  );
  if (!empty($node->webform['components'])) {
    $component_tree = array();
    $page_count = 1;
    _webform_components_tree_build($node->webform['components'], $component_tree, 0, $page_count);
    $component_tree = _webform_components_tree_sort($component_tree);

    // Build the table rows.
    function _webform_components_form_rows($node, $cid, $component, $level, &$form, &$rows, &$add_form, $enabled) {

      // CiviCRM additions
      $clone = t('Clone');
      $options = array();
      if ($component['type'] == 'civicrm_contact') {
        $types = array(
          'autocomplete' => t('Contact - Autocomplete'),
          'select' => t('Contact - Select List'),
          'hidden' => $component['extra']['show_hidden_contact'] ? t('Contact - Static') : t('Contact - Hidden'),
        );
        $type = $types[$component['extra']['widget']];
      }
      elseif ($component['type'] == 'select') {
        if ($component['extra']['aslist']) {
          $type = $component['extra']['multiple'] ? t('Multi-select') : t('Select');
        }
        else {
          $type = $component['extra']['multiple'] ? t('Checkboxes') : t('Radio buttons');
        }
      }
      else {
        $type = $form['add']['type']['#options'][$component['type']];
      }
      if (in_array($cid, $enabled)) {
        $fields = wf_crm_get_fields();
        $sets = wf_crm_get_fields('sets');
        $class = 'civi-icon';
        $clone = '';
        list(, $c, $ent, $n, $table, $name) = explode('_', $component['form_key'], 6);
        $field = wf_crm_aval($fields, $table . '_' . $name, array(
          'type' => 'fieldset',
        ));
        if ($component['type'] == 'fieldset') {
          $title = t('Container for Contact !num', array(
            '!num' => $c,
          ));
          $type = t('Contact !num Fieldset', array(
            '!num' => $c,
          ));
          $clone = t('Clone Contact');
          $options['attributes']['title'] = t('Add a new contact to the form with the same fields and settings');
          $class .= ' fieldset';
        }
        elseif ($ent == 'contact') {
          $field_type = $table == 'contact' || $table == 'other' ? $field['name'] : $sets[$table]['label'];
          $title = t('Contact !num !type Field', array(
            '!num' => $c,
            '!type' => $field_type,
          ));
        }
        else {
          $title = t('Field for !type', array(
            '!type' => $sets[$table]['label'],
          ));
        }
        if ($table === 'address' && $component['type'] === 'textfield') {
          if ($name === 'state_province_id' || $name === 'county_id') {
            $type = t('AJAX Select');
          }
        }
        if ($component['type'] == 'civicrm_contact' || $component['type'] == 'fieldset') {
          $class .= ' ' . $node->webform_civicrm['data']['contact'][$c]['contact'][1]['contact_type'];
        }
        if ($component['type'] == 'select') {
          $type .= ' (' . (empty($component['extra']['civicrm_live_options']) ? t('static') : t('live')) . ')';
        }

        // Show defaults with labels instead of keys
        if ($component['type'] == 'civicrm_contact') {
          if ($component['extra']['default'] == 'contact_id') {
            $component['value'] = check_plain(wf_crm_display_name($component['extra']['default_contact_id']));
          }
          elseif ($component['extra']['default'] == 'user') {
            $component['value'] = t('Current User');
          }
          elseif ($component['extra']['default'] == 'auto') {
            $component['value'] = t('Auto - From Filters');
          }
          elseif ($component['extra']['default'] == 'relationship' && $component['extra']['default_relationship']) {
            $component['value'] = t('Relationship to Contact 1');
          }
        }
        elseif (isset($component['value']) && strlen($component['value']) && $field['type'] == 'select') {
          if ($component['type'] == 'select') {
            $items = wf_crm_str2array($component['extra']['items']);
          }
          else {
            $items = wf_crm_field_options($component, 'components_form', $node->webform_civicrm['data']);
          }
          $val = '';
          foreach (explode(',', $component['value']) as $v) {
            if (isset($items[trim($v)])) {
              $val .= ($val ? ', ' : '') . $items[trim($v)];
            }
          }
          $component['value'] = $val;
        }
        $type = array(
          'data' => '<span class="' . $class . '"> </span>' . $type,
          'title' => $title,
        );
      }

      // Create presentable values.
      if (drupal_strlen($component['value']) > 30) {
        $component['value'] = drupal_substr($component['value'], 0, 30);
        $component['value'] .= '...';
      }
      $component['value'] = check_plain($component['value']);

      // Remove individual titles from the mandatory and weight fields.
      unset($form['components'][$cid]['mandatory']['#title']);
      unset($form['components'][$cid]['pid']['#title']);
      unset($form['components'][$cid]['weight']['#title']);

      // Add special classes for weight and parent fields.
      $form['components'][$cid]['cid']['#attributes']['class'] = array(
        'webform-cid',
      );
      $form['components'][$cid]['pid']['#attributes']['class'] = array(
        'webform-pid',
      );
      $form['components'][$cid]['weight']['#attributes']['class'] = array(
        'webform-weight',
      );

      // Build indentation for this row.
      $indents = '';
      for ($n = 1; $n <= $level; $n++) {
        $indents .= '<div class="indentation">&nbsp;</div>';
      }

      // Add each component to a table row.
      $row_data = array(
        $indents . filter_xss($component['name']),
        $type,
        $component['value'] == '' ? '-' : $component['value'],
        drupal_render($form['components'][$cid]['mandatory']),
        drupal_render($form['components'][$cid]['cid']) . drupal_render($form['components'][$cid]['pid']) . drupal_render($form['components'][$cid]['weight']),
        l(t('Edit'), 'node/' . $node->nid . '/webform/components/' . $cid),
        $clone ? l($clone, 'node/' . $node->nid . '/webform/components/' . $cid . '/clone', $options) : ' ',
        l(t('Delete'), 'node/' . $node->nid . '/webform/components/' . $cid . '/delete'),
      );
      $row_class = array(
        'draggable',
      );
      if (!webform_component_feature($component['type'], 'group')) {
        $row_class[] = 'tabledrag-leaf';
      }
      if ($component['type'] == 'pagebreak') {
        $row_class[] = 'tabledrag-root';
        $row_class[] = 'webform-pagebreak';
        $row_data[0] = array(
          'class' => array(
            'webform-pagebreak',
          ),
          'data' => $row_data[0],
        );
      }
      $rows[] = array(
        'data' => $row_data,
        'class' => $row_class,
      );
      if (isset($component['children']) && is_array($component['children'])) {
        foreach ($component['children'] as $cid => $component) {
          _webform_components_form_rows($node, $cid, $component, $level + 1, $form, $rows, $add_form, $enabled);
        }
      }

      // Add the add form if this was the last edited component.
      if (isset($_GET['cid']) && $component['cid'] == $_GET['cid'] && $add_form) {
        $add_form['data'][0] = $indents . $add_form['data'][0];
        $rows[] = $add_form;
        $add_form = FALSE;
      }
    }
    foreach ($component_tree['children'] as $cid => $component) {
      _webform_components_form_rows($node, $cid, $component, 0, $form, $rows, $add_form, $enabled);
    }
  }
  else {
    $message = wf_crm_admin_access($node) ? t('Add a webform component below, or click the CiviCRM tab to add CRM fields.') : t('No Components, add a component below.');
    $rows[] = array(
      array(
        'data' => $message,
        'colspan' => 9,
      ),
    );
  }

  // Append the add form if not already printed.
  if ($add_form) {
    $rows[] = $add_form;
  }
  $output = '';
  $output .= theme('table', array(
    'header' => $header,
    'rows' => $rows,
    'attributes' => array(
      'id' => 'webform-components',
    ),
  ));
  $output .= drupal_render_children($form);
  return $output;
}