You are here

function forena_parameter_form in Forena Reports 7.5

Same name and namespace in other branches
  1. 6.2 forena.common.inc \forena_parameter_form()
  2. 7 forena.common.inc \forena_parameter_form()
  3. 7.2 forena.common.inc \forena_parameter_form()
  4. 7.3 forena.module \forena_parameter_form()
  5. 7.4 forena.module \forena_parameter_form()
1 string reference to 'forena_parameter_form'
Report::parametersForm in src/Report.php

File

./forena.module, line 639

Code

function forena_parameter_form($formid, &$form_state, $parameters, $attributes = array()) {
  $parms = $_GET;
  $menu_parms = array();
  $desc = array();
  if (!isset($form_state['storage'])) {

    // Set Descriptors
    $desc = Frx::Data()
      ->getContext('report');
    $form_state['storage']['desc'] = $desc;
    $desc = $form_state['storage']['desc'];

    // Set menu parms
    $menu_parms = Frx::Data()
      ->getContext('menu-parms');
    if ($menu_parms) {
      $form_state['storage']['menu-parms'] = $menu_parms;
    }
  }
  else {
    extract($form_state['storage']);
  }
  $report_name = @$desc['name'];
  $collapse = isset($attributes['collapsed']) ? $attributes['collapsed'] : FALSE;
  if (isset($form_state['values'])) {
    $collapse = FALSE;
    $parms = array_merge($parms, $form_state['values']['params']);

    // In the case of ahah, we need to restore menu parameters from the form state.
    if (isset($form_state['storage']['menu-parms'])) {
      $menu_parms = $form_state['storage']['menu-parms'];
      $parms = array_merge($menu_parms, $parms);
    }
    drupal_alter('forena_parameters', $report_name, $parms);
    Frx::Data()
      ->push($parms, 'parm');
  }
  $template = @$attributes['template'];
  $collapsible = isset($attributes['collapsible']) ? $attributes['collapsible'] : TRUE;
  $title = isset($attributes['title']) ? $attributes['title'] : t('Parameters');
  $submit_value = isset($attributes['submit']) ? $attributes['submit'] : t('Submit');
  unset($parms['q']);
  $form = array();
  if ($parameters) {

    //drupal_alter('forena_parameters', $report_name,  $parms);
    if ($parameters) {
      $form['params'] = array(
        '#tree' => TRUE,
        '#title' => $title,
        '#type' => 'fieldset',
        '#collapsible' => $collapsible,
        '#collapsed' => $collapse,
        '#prefix' => '<div id="parameters-wrapper">',
        '#suffix' => '</div>',
      );
      foreach ($parameters as $node) {
        $add_null = FALSE;
        $list = array();
        $disabled = FALSE;
        $label = @(string) $node['label'];
        $id = @(string) $node['id'];
        $data_source = @(string) $node['data_source'];
        $data_field = @(string) $node['data_field'];
        $class = @(string) $node['class'];
        $type = @(string) $node['type'];
        $option_str = @(string) $node['options'];
        $options = array();
        if ($option_str) {
          parse_str($option_str, $options);
        }
        if (isset($parms[$id])) {
          $value = $parms[$id];
          $multi_value = (array) $parms[$id];
        }
        else {
          $value = @(string) $node['default'];
          $multi_value = array();
          if (strpos($value, '|') !== FALSE) {
            $multi_value = explode('|', $value);
          }
        }
        $desc = @(string) $node['desc'];
        $label_field = @(string) $node['label_field'];
        @(strcmp((string) $node['require'], "1") == 0) ? $required = TRUE : ($required = FALSE);
        $ctl_attrs = array();

        //returned values filtered against data_field attr.
        if ($data_source) {
          $list = Frx::DataManager()
            ->dataBlockParams($data_source, $data_field, $label_field);
          if (!$required && $add_null) {
            $list = array(
              '' => '',
            ) + $list;
          }
        }

        //Determine the form element type to be displayed

        //If select or radios is chosen then begin a $list array for display values.
        $multiselect = FALSE;
        $ajax = FALSE;
        $add_null = FALSE;
        switch ($type) {
          case 'multiselect':
            $type = 'select';
            $multiselect = TRUE;
            $value = $multi_value;
            break;
          case 'multiselectajax':
            $type = 'select';
            $multiselect = TRUE;
            $value = $multi_value;
            $ajax = TRUE;
            break;
          case 'checkboxes':
            $value = $multi_value;
            break;
          case 'selectajax':
            $ajax = TRUE;
            $type = 'select';
            $add_null = TRUE;
            break;
          case 'select':
            $add_null = TRUE;
            break;
          case 'date_text':
          case 'date_select':
          case 'date_popup':
            $options['date_format'] = @$options['date_format'] ? $options['date_format'] : 'Y-m-d';
            $ctl_attrs['#date_format'] = $options['date_format'];
            if ($value) {
              $datetime = @strtotime($value);
              if ($datetime) {
                $value = date('Y-m-d h:i', $datetime);
              }
            }
            $ctl_attrs['#forena_date_format'] = @$options['date_parm_format'] ? $options['date_parm_format'] : 'Y-m-d';
            if (@$options['date_year_range']) {
              $ctl_attrs['#date_year_range'] = $options['date_year_range'];
            }
            if (@$options['date_label_position']) {
              $ctl_attrs['#date_label_position'] = $options['date_label_position'];
            }
            $list = array();
            break;
          case 'checkbox':
            if (@$option_str['return_value']) {
              $ctl_attrs['#return_value'] = $options['return_value'];
            }
            $list = array();
            break;
          case 'radios':
            break;
          case 'hidden':
            $list = array();
            break;
          default:
            $type = 'textfield';
            $list = array();
        }
        if (isset($menu_parms[$id]) && $type != 'hidden') {
          $disabled = TRUE;
        }

        //If a data_source attr was found then create an array of
        $form['params'][$id] = array(
          '#type' => $type,
          '#title' => $label ? t($label) : t($id),
          '#default_value' => $value,
          '#disabled' => $disabled,
          '#required' => $required,
          '#description' => t($desc),
        );
        if (isset($option['size'])) {
          $forms['params'][$id]['#size'] = @$options['size'];
        }
        $form['params'][$id] = array_merge($form['params'][$id], $ctl_attrs);
        if ($type == 'item') {
          $form['params'][$id]['#markup'] = $value;
        }
        if ($type == 'hidden') {
          $form['params'][$id]['#value'] = $value;
        }

        // Add class to parmeter form.
        if ($class) {
          $form['params'][$id]['#attributes'] = array(
            'class' => @explode(' ', $class),
          );
        }

        //if $list is not empty then push options

        //onto the array. options will cause an error for

        //textfield elements.
        if ($list || $type == 'select' || $type == 'radios') {
          if ($add_null) {
            $prompt = @$options['prompt'];
            if (!$prompt) {
              $prompt = $required ? '-' . t('select') . '-' : '-' . t('none') . '-';
            }
            $form['params'][$id]['#empty_option'] = $prompt;
          }
          $form['params'][$id]['#options'] = $list;
          $form['params'][$id]['#multiple'] = $multiselect;
        }
        if ($ajax) {
          $form['params'][$id]['#ajax'] = array(
            'callback' => 'forena_parameters_callback',
            'wrapper' => 'parameters-wrapper',
          );
        }
      }
      if ($template) {
        $form['params']['#forena-template'] = $template;
        $form['params']['#theme'] = 'forena_fieldset_template';
        _forena_set_inline_theme($form['params']);
      }
      $form['params']['submit'] = array(
        '#type' => 'submit',
        '#value' => $submit_value,
      );
    }
    return $form;
  }
}