You are here

function webform_report_criteria_form in Webform Report 7

Same name and namespace in other branches
  1. 6.2 webform_report_criteria.inc \webform_report_criteria_form()
  2. 6 webform_report.module \webform_report_criteria_form()

Generate a form for specifying report criteria.

Parameters

form_state: drupal form state *param node current node object

Return value

an array of form elements

1 string reference to 'webform_report_criteria_form'
webform_report_menu in ./webform_report.module
Implementation of hook_menu()

File

./webform_report_criteria.inc, line 19
This file contains the functions required to implement the Report Criteria form for the webform report module

Code

function webform_report_criteria_form($form, &$form_state, $node) {
  global $user;
  drupal_add_css(drupal_get_path('module', 'webform_report') . '/webform_report.css');
  module_load_include('inc', 'webform_report', 'webform_report');
  $mod_path = drupal_get_path('module', 'webform_report');
  $img_path = $mod_path . "/images/";
  if (isset($node->wnid)) {
    $webform_components = _webform_report_get_components($node->wnid);
    $form['nid'] = array(
      '#type' => 'value',
      '#value' => $node->nid,
    );
    $comp_options = array();
    $comp_options[0] = t('Select a field');
    $filter_comp_options = array();
    $filter_comp_options[0] = t('Select a field');
    foreach ($webform_components as $cid => $comp) {
      $opt = $comp['name'];
      if (strlen($opt) > 50) {
        $opt = substr($opt, 0, 50) . '...';
      }
      $comp_options[$cid] = $opt;

      // leave link options off filter component list
      if ($cid > -5) {
        $filter_comp_options[$cid] = $opt;
      }
    }
    $component_date_format_options = array(
      '' => '',
      'm/d/Y' => 'mm/dd/yyyy',
      'd/m/Y' => 'dd/mm/yyyy',
      'Y/m/d' => 'yyyy/mm/dd',
    );
    $component_time_format_options = array(
      '' => '',
      'H:i' => 'hh:mm',
      'h:i a' => 'hh:mm am/pm',
    );
    $component_select_format_options = array(
      '' => '',
      'KEY' => 'Display Key',
      'VALUE' => 'Display Value',
    );
    $component_select_options = array(
      '' => '',
      'KEY' => 'Sort By Key',
      'VALUE' => 'Sort By Value',
    );
    $columns = _webform_report_get_columns($node, $webform_components);

    // column fieldset
    $form['columns'] = array(
      '#type' => 'fieldset',
      '#title' => t('Report Columns'),
      '#description' => t('Columns are listed in same order as this list.'),
      '#tree' => TRUE,
    );

    // form list of report columns
    $cc = count($columns);
    $i = 0;
    for ($i = 0; $i < $cc; $i++) {
      $cid = $columns[$i]['cid'];
      $form['columns'][$i]['name'] = array(
        '#markup' => $columns[$i]['name'],
      );
      $form['columns'][$i]['type'] = array(
        '#markup' => $columns[$i]['type'],
      );
      if ($columns[$i]['type'] == 'date') {
        $form['columns'][$i]['format'] = array(
          '#type' => 'select',
          '#default_value' => $columns[$i]['format'],
          '#options' => $component_date_format_options,
        );
      }
      if ($columns[$i]['type'] == 'time') {
        $form['columns'][$i]['format'] = array(
          '#type' => 'select',
          '#default_value' => $columns[$i]['format'],
          '#options' => $component_time_format_options,
        );
      }
      if ($columns[$i]['type'] == 'select') {
        $form['columns'][$i]['format'] = array(
          '#type' => 'select',
          '#default_value' => $columns[$i]['format'],
          '#options' => $component_select_format_options,
        );
        $form['columns'][$i]['option'] = array(
          '#type' => 'select',
          '#default_value' => $columns[$i]['option'],
          '#options' => $component_select_options,
        );
      }
      $form['columns'][$i]['hidden'] = array(
        '#type' => 'checkbox',
        '#default_value' => $columns[$i]['hidden'],
      );
      if ($columns[$i]['type'] != 'date' && $columns[$i]['type'] != 'time') {
        $form['columns'][$i]['num'] = array(
          '#type' => 'checkbox',
          '#default_value' => array_key_exists('num', $columns[$i]) ? $columns[$i]['num'] : FALSE,
        );
      }
      $form['columns'][$i]['weight'] = array(
        '#type' => 'weight',
        '#default_value' => $columns[$i]['weight'],
        '#delta' => 10,
        '#attributes' => array(
          'class' => array(
            'webform-report-columns-item-weight',
          ),
        ),
      );
      $form['columns'][$i]['delete'] = array(
        '#type' => 'image_button',
        '#src' => $img_path . 'trash.png',
        '#return_value' => 'columns_delete_' . $i . '_' . $cid,
        '#attributes' => array(
          'alt' => t('Delete this item'),
        ),
        '#submit' => array(
          'webform_report_criteria_form_list_submit',
        ),
      );
    }
    $form['columns']['addcomp'] = array(
      '#type' => 'select',
      '#options' => $comp_options,
      '#required' => FALSE,
    );
    $form['columns']['add'] = array(
      '#type' => 'submit',
      '#value' => t('Add Column'),
      '#return_value' => 'columns_add',
      '#submit' => array(
        'webform_report_criteria_form_list_submit',
      ),
    );

    // make sure we get report values
    unset($_SESSION['search_form'][$node->nid]);
    $filters = _webform_report_get_filters($node, $webform_components);
    $filter_options = _webform_report_get_filter_options();
    $filter_options_datetime = _webform_report_get_filter_options(TRUE);

    // filters fieldset
    $form['filters'] = array(
      '#type' => 'fieldset',
      '#title' => t('Filters'),
      '#description' => t('All filters are ANDed together.'),
      '#tree' => TRUE,
    );

    // form list of report filters
    $cc = count($filters);
    $i = 0;
    for ($i = 0; $i < $cc; $i++) {
      $cid = $filters[$i]['cid'];
      $type = $filters[$i]['type'];
      $ftype = $filters[$i]['ftype'];
      $value = $filters[$i]['value'];
      $form['filters'][$i]['name'] = array(
        '#markup' => $filters[$i]['name'],
      );
      $form['filters'][$i]['type'] = array(
        '#markup' => $type,
      );
      $form['filters'][$i]['ftype'] = array(
        '#type' => 'select',
        '#default_value' => $ftype,
      );

      // use reduced options for date/time
      if ($type == 'date' || $type == 'time') {
        $form['filters'][$i]['ftype']['#options'] = $filter_options_datetime;
      }
      else {
        $form['filters'][$i]['ftype']['#options'] = $filter_options;
      }

      // insert value field(s)
      webform_report_criteria_filter_value_field($form, $i, $type, $ftype, $value);
      $form['filters'][$i]['weight'] = array(
        '#type' => 'weight',
        '#default_value' => $columns[$i]['weight'],
        '#delta' => 10,
        '#attributes' => array(
          'class' => array(
            'webform-report-filters-item-weight',
          ),
        ),
      );
      $form['filters'][$i]['delete'] = array(
        '#type' => 'image_button',
        '#src' => $img_path . 'trash.png',
        '#return_value' => 'filters_delete_' . $i . '_' . $cid,
        '#attributes' => array(
          'alt' => t('Delete this item'),
        ),
        '#submit' => array(
          'webform_report_criteria_form_list_submit',
        ),
      );
    }
    $form['filters']['addcomp'] = array(
      '#type' => 'select',
      '#options' => $filter_comp_options,
      '#required' => FALSE,
    );
    $form['filters']['add'] = array(
      '#type' => 'submit',
      '#value' => t('Add Filter'),
      '#return_value' => 'filters_add',
      '#submit' => array(
        'webform_report_criteria_form_list_submit',
      ),
    );
    $sorton = _webform_report_get_sorton($node, $webform_components);
    $sort_options = array(
      SORT_ASC => t('Ascending'),
      SORT_DESC => t('Descending'),
    );

    // sort fieldset
    $form['sorton'] = array(
      '#type' => 'fieldset',
      '#title' => t('Sort By'),
      '#description' => t('Sorting is applied in same order as this list.'),
      '#tree' => TRUE,
    );

    // form list of report sort options
    $cc = count($sorton);
    $i = 0;
    for ($i = 0; $i < $cc; $i++) {
      $cid = $sorton[$i]['cid'];
      $form['sorton'][$i]['name'] = array(
        '#markup' => $sorton[$i]['name'],
      );
      $form['sorton'][$i]['type'] = array(
        '#markup' => $sorton[$i]['type'],
      );
      $form['sorton'][$i]['order'] = array(
        '#type' => 'select',
        '#options' => $sort_options,
        '#default_value' => $sorton[$i]['order'],
      );
      $form['sorton'][$i]['weight'] = array(
        '#type' => 'weight',
        '#default_value' => $columns[$i]['weight'],
        '#delta' => 10,
        '#attributes' => array(
          'class' => array(
            'webform-report-sorton-item-weight',
          ),
        ),
      );
      $form['sorton'][$i]['delete'] = array(
        '#type' => 'image_button',
        '#src' => $img_path . 'trash.png',
        '#return_value' => 'sorton_delete_' . $i . '_' . $cid,
        '#attributes' => array(
          'alt' => t('Delete this item'),
        ),
        '#submit' => array(
          'webform_report_criteria_form_list_submit',
        ),
      );
    }
    $form['sorton']['addcomp'] = array(
      '#type' => 'select',
      '#options' => $filter_comp_options,
      '#required' => FALSE,
    );
    $form['sorton']['add'] = array(
      '#type' => 'submit',
      '#value' => t('Add Sort'),
      '#return_value' => 'sorton_add',
      '#submit' => array(
        'webform_report_criteria_form_list_submit',
      ),
    );

    // options fieldset
    $form['options'] = array(
      '#type' => 'fieldset',
      '#title' => t('Options'),
      '#description' => t('Report options.'),
    );
    $results_per_page_options = array(
      0 => 'All',
      20 => '20',
      40 => '40',
      60 => '60',
      80 => '80',
      100 => '100',
      200 => '200',
      300 => '300',
      400 => '400',
      500 => '500',
    );
    $default = 20;
    if (is_array($node->options) && array_key_exists('results_per_page', $node->options)) {
      $default = $node->options['results_per_page'];
    }
    $form['options']['results_per_page'] = array(
      '#type' => 'select',
      '#title' => t('Results per page'),
      '#options' => $results_per_page_options,
      '#default_value' => $default,
    );
    $default = FALSE;
    if (is_array($node->options) && array_key_exists('hide_csv', $node->options)) {
      $default = $node->options['hide_csv'];
    }
    $form['options']['hide_csv'] = array(
      '#type' => 'checkbox',
      '#title' => t('Hide CSV Download option'),
      '#default_value' => $default,
    );
    $default = FALSE;
    if (is_array($node->options) && array_key_exists('search_form', $node->options)) {
      $default = $node->options['search_form'];
    }
    $form['options']['search_form'] = array(
      '#type' => 'checkbox',
      '#title' => t('Display Search Form'),
      '#default_value' => $default,
      '#description' => t('Checking this option will display a search form on the report page.  Note: If site caching is enabled, the search form will not be displayed to anonymous users.'),
    );
    $default = 'REPORT';
    if (is_array($node->options) && array_key_exists('layout', $node->options)) {
      $default = $node->options['layout'];
    }
    $form['options']['layout'] = array(
      '#type' => 'select',
      '#title' => t('Report Layout'),
      '#options' => array(
        'REPORT' => t('Report'),
        'SUBMISSION' => t('Submission'),
      ),
      '#default_value' => $default,
      '#description' => t('Choose the report layout.'),
    );

    // PHP code fieldset - only shown if user has permission
    if (user_access('use php code')) {
      $code = isset($node->options['php_code']) ? $node->options['php_code'] : '';
      $form['php'] = array(
        '#type' => 'fieldset',
        '#title' => t('PHP Code'),
        '#collapsible' => TRUE,
        '#collapsed' => empty($code),
        '#description' => t('PHP processing code can be used to modify report values before the report is displayed.  <strong>This option should be used with <u>caution</u> as improper code can crash or damage a site.</strong>'),
      );
      $form['php']['variables'] = array(
        '#markup' => '<p>' . t('The following variables are available to thie code:') . '</p>' . theme('item_list', array(
          '$node: ' . t('The webform report node object.'),
          '$headers: ' . t('The report header array as input to <a href="http://api.drupal.org/api/drupal/includes--theme.inc/function/theme_table/6">theme_table</a>.'),
          '$rows: ' . t('The report row array as input to <a href="http://api.drupal.org/api/drupal/includes--theme.inc/function/theme_table/6">theme_table</a> with additional keys <i>field</i> (field id), <i>sort</i> (value used to sort the column), and <i>hidden</i> (indicates if value is to be displayed on report).'),
        )) . '<p>The <i>$headers</i> and <i>$rows</i> may be modifed in order to alter the report output.',
      );
      $form['php']['php_code'] = array(
        '#type' => 'textarea',
        '#title' => t('PHP Processing Code'),
        '#default_value' => $code,
        '#cols' => 40,
        '#rows' => 10,
        '#description' => t('The PHP code must start with &lt;?php in order to be executed.'),
      );
    }
    $form['update'] = array(
      '#type' => 'submit',
      '#value' => t('Update'),
    );
    return $form;
  }
  else {
    drupal_set_message(t('Error - Webform is not set for this report - cannot proceed.  Please delete and re-create this report.'), 'error');
    return NULL;
  }
}