You are here

function webform_charts_edit_chart in Webform Charts 7

Form callback; Display the form for editing an individual chart.

1 string reference to 'webform_charts_edit_chart'
_webform_charts_webform_analysis_alter in includes/webform_charts.pages.inc
Implements hook_webform_analysis_alter().

File

includes/webform_charts.pages.inc, line 218
Menu callbacks and forms used by the Webform Charts module.

Code

function webform_charts_edit_chart($form, $form_state, $node, $component, $data) {

  // Populate settings and defaults.
  form_load_include($form_state, 'inc', 'charts', 'includes/charts.pages');
  $options = isset($component['extra']['chart']) ? $component['extra']['chart'] : array();
  $options += webform_charts_default_settings($node, $component, $data);
  $form['#node'] = $node;
  $form['#component'] = $component;
  $form['#data'] = $data;
  $form['#attached']['library'][] = array(
    'webform_charts',
    'webform_charts.admin',
  );
  if (module_exists('charts_highcharts')) {
    $form['#attached']['library'][] = array(
      'webform_charts',
      'webform_charts.admin.highcharts',
    );
  }
  $form['title'] = array(
    '#markup' => t('Chart configuration'),
  );
  $form['preview'] = _webform_charts_component_chart($node, $component, $data);
  $form['preview']['#weight'] = -100;
  $form['preview']['#title'] = FALSE;
  $parents = webform_component_parent_keys($node, $component);
  $form_key = implode(':', $parents);
  $form['help'] = array(
    '#type' => 'markup',
    '#markup' => '<p>' . t('This chart may be embedded into the <a href="!url">confirmation page</a> by using the token !token.', array(
      '!url' => url('node/' . $node->nid . '/webform/configure'),
      '!token' => "<code>[node:webform-charts:{$form_key}]</code>",
    )) . '</p>',
  );

  // Get a list of available chart libraries.
  $charts_info = charts_info();
  $library_options = array(
    '' => t('- Default -'),
  );
  foreach ($charts_info as $library_name => $library_info) {
    $library_options[$library_name] = $library_info['label'];
  }
  $form['library'] = array(
    '#title' => t('Charting library'),
    '#type' => 'select',
    '#options' => $library_options,
    '#default_value' => $options['library'],
    '#access' => count($library_options) > 2,
    '#attributes' => array(
      'class' => array(
        'chart-library-select',
      ),
    ),
    '#weight' => -20,
  );
  $chart_types = charts_type_info();
  $type_options = array();
  foreach ($chart_types as $chart_type => $chart_type_info) {
    $type_options[$chart_type] = $chart_type_info['label'];
  }
  $form['type'] = array(
    '#title' => t('Chart type'),
    '#title_display' => 'invisible',
    '#type' => 'radios',
    '#default_value' => $options['type'],
    '#options' => $type_options,
    '#required' => TRUE,
    '#weight' => -15,
    '#attributes' => array(
      'class' => array(
        'chart-type-radios',
        'container-inline',
      ),
    ),
  );
  $form['legend'] = array(
    '#title' => t('Show legend'),
    '#type' => 'checkbox',
    '#default_value' => $options['legend'],
    '#weight' => -10,
  );
  $form['tooltips'] = array(
    '#title' => t('Show tooltips'),
    '#type' => 'checkbox',
    '#default_value' => $options['tooltips'],
    '#weight' => -9,
  );
  $form['data_labels'] = array(
    '#title' => t('Show data labels'),
    '#type' => 'checkbox',
    '#default_value' => $options['data_labels'],
    '#weight' => -8,
  );
  $form['stacking'] = array(
    '#title' => t('Stacked'),
    '#type' => 'checkbox',
    '#default_value' => $options['stacking'],
    '#weight' => -6,
    // Only shown if dealing with a multi-axis chart.
    '#access' => !isset($form['preview']['data']),
  );

  // Set data attributes to identify special properties of different types.
  foreach ($chart_types as $chart_type => $chart_type_info) {
    if ($chart_type_info['axis_inverted']) {
      $form['type'][$chart_type]['#attributes']['data-axis-inverted'] = TRUE;
    }
    if ($chart_type_info['stacking']) {
      $form['type'][$chart_type]['#attributes']['data-stacking'] = TRUE;
    }
    if ($chart_type_info['axis'] === CHARTS_SINGLE_AXIS) {
      $form['type'][$chart_type]['#attributes']['data-axis-single'] = TRUE;
      if (!empty($data['table_header'])) {
        $form['type'][$chart_type]['#access'] = FALSE;
      }
    }
  }

  // Try various approaches to determine the labels used for colors.
  $fields = array();

  // If a header is present that will be used for colors.
  if (!empty($data['table_header'])) {
    $fields = $data['table_header'];
    if (isset($fields[0]) && strlen($fields[0]) === 0) {
      array_shift($fields);
    }
    $fields = array_values($fields);
  }
  elseif ($data['table_rows']) {
    $fields = array();
    foreach ($data['table_rows'] as $row_data) {
      if (is_string($row_data[0])) {
        $fields[] = $row_data[0];
      }
    }
  }
  $form['colors'] = array(
    '#theme' => 'webform_charts_colors_element',
    '#theme_wrappers' => array(
      'form_element',
    ),
    '#title' => t('Colors'),
  );
  $color_count = 0;
  foreach ($fields as $field_name => $field_label) {
    $form['colors'][$field_name] = array(
      '#title' => webform_filter_xss($field_label),
      '#type' => 'textfield',
      '#attributes' => array(
        'TYPE' => 'color',
      ),
      '#size' => 10,
      '#maxlength' => 7,
      '#default_value' => !empty($options['colors'][$field_name]) ? $options['colors'][$field_name] : $options['colors'][$color_count],
      '#parents' => array(
        'colors',
        $field_name,
      ),
    );
    $color_count++;
  }
  $form['xaxis'] = array(
    '#title' => t('Horizontal axis'),
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#attributes' => array(
      'class' => array(
        'chart-xaxis',
      ),
    ),
  );
  $form['xaxis']['title'] = array(
    '#title' => t('Custom title'),
    '#type' => 'textfield',
    '#default_value' => $options['xaxis_title'],
    '#parents' => array(
      'xaxis_title',
    ),
  );
  $form['xaxis']['labels_rotation'] = array(
    '#title' => t('Labels rotation'),
    '#type' => 'select',
    '#options' => array(
      0 => '0°',
      30 => '30°',
      45 => '45°',
      60 => '60°',
      90 => '90°',
    ),
    '#default_value' => $options['xaxis_labels_rotation'],
    '#parents' => array(
      'xaxis_labels_rotation',
    ),
  );
  $form['yaxis'] = array(
    '#title' => t('Vertical axis'),
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#attributes' => array(
      'class' => array(
        'chart-yaxis',
      ),
    ),
  );
  $form['yaxis']['title'] = array(
    '#title' => t('Custom title'),
    '#type' => 'textfield',
    '#default_value' => $options['yaxis_title'],
    '#parents' => array(
      'yaxis_title',
    ),
  );
  $form['yaxis']['minmax'] = array(
    '#title' => t('Value range'),
    '#theme_wrappers' => array(
      'form_element',
    ),
  );
  $form['yaxis']['minmax']['min'] = array(
    '#type' => 'textfield',
    '#attributes' => array(
      'TYPE' => 'number',
      'max' => 999999,
      'placeholder' => t('Minimum'),
    ),
    '#default_value' => $options['yaxis_min'],
    '#size' => 12,
    '#suffix' => ' ',
    '#theme_wrappers' => array(),
    '#parents' => array(
      'yaxis_min',
    ),
  );
  $form['yaxis']['minmax']['max'] = array(
    '#type' => 'textfield',
    '#attributes' => array(
      'TYPE' => 'number',
      'max' => 999999,
      'placeholder' => t('Maximum'),
    ),
    '#default_value' => $options['yaxis_max'],
    '#size' => 12,
    '#theme_wrappers' => array(),
    '#parents' => array(
      'yaxis_max',
    ),
  );
  $form['yaxis']['labels_rotation'] = array(
    '#title' => t('Labels rotation'),
    '#type' => 'select',
    '#options' => array(
      0 => '0°',
      30 => '30°',
      45 => '45°',
      60 => '60°',
      90 => '90°',
    ),
    '#default_value' => $options['yaxis_labels_rotation'],
    '#parents' => array(
      'yaxis_labels_rotation',
    ),
  );
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save chart'),
  );
  $form['actions']['reset'] = array(
    '#type' => 'submit',
    '#value' => t('Reset to defaults'),
    '#access' => isset($component['extra']['chart']),
  );
  return $form;
}