You are here

function forena_parameter_form in Forena Reports 7.2

Same name and namespace in other branches
  1. 6.2 forena.common.inc \forena_parameter_form()
  2. 7.5 forena.module \forena_parameter_form()
  3. 7 forena.common.inc \forena_parameter_form()
  4. 7.3 forena.module \forena_parameter_form()
  5. 7.4 forena.module \forena_parameter_form()

Form to edit parameters Extra features: In the parameters section of the report are new attributes frx:parm: @data_source = data block for the parameter to values from @data_field = specific field of the data block to recieve values from. if no field is specified then the first column is arbitrarily chosen. @type = The form element that will display the values: select, radios are supported default is textfield.

This function will evaluate each parameter, determine its type, data_source, and data_field and display those values appropriately.

1 string reference to 'forena_parameter_form'
FrxDrupalApplication::theme in ./FrxDrupalApplication.inc
Theme the output of a css Enter description here ...

File

./forena.common.inc, line 173
Common functions used throughout the project but loaded in this file to keep the module file lean.

Code

function forena_parameter_form($formid, &$form_state, $rpt, $collapse = FALSE) {
  $parms = $_GET;
  $r = new FrxReport($rpt);
  if (isset($form_state['values'])) {
    $collapse = FALSE;
    $parms = array_merge($parms, $form_state['values']['params']);
  }
  unset($parms['q']);
  $form = array();
  if ($r) {

    //      drupal_set_title($r->title);
    $head = $r->rpt_xml->head;
    FrxReportGenerator::instance()
      ->alter_parameters('', $parms);
    $nodes = $head
      ->xpath('frx:parameters/frx:parm');
    if ($nodes) {
      $form['params'] = array(
        '#tree' => TRUE,
        '#title' => t('Parameters'),
        '#type' => 'fieldset',
        '#collapsible' => TRUE,
        '#collapsed' => $collapse,
        '#prefix' => '<div id="parameters-wrapper">',
        '#suffix' => '</div>',
      );
      foreach ($nodes as $node) {
        $label = (string) $node['label'];
        $id = (string) $node['id'];
        $data_source = (string) $node['data_source'];
        $data_field = (string) $node['data_field'];
        $type = (string) $node['type'];
        if (isset($parms[$id])) {
          $value = $parms[$id];
          $multi_value = (array) $parms[$id];
        }
        else {
          $value = (string) $node;
          $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);

        //Determine the form element type to be displayed

        //If select or radios is chosen then begin a $list array for display values.
        $multiselect = FALSE;
        $list = array();
        $ajax = FALSE;
        $add_null = FALSE;
        switch ($type) {
          case 'multiselect':
            $type = 'select';
            $multiselect = TRUE;
            $value = $multi_value;
            break;
          case 'checkboxes':
            $value = $multi_value;
            break;
          case 'selectajax':
            $ajax = TRUE;
            $type = 'select';
            break;
          case 'select':
            $add_null = TRUE;
            break;
          case 'radios':
            break;
          default:
            $type = 'textfield';
            $list = '';
        }

        //If a data_source attr was found then create an array of

        //returned values filtered against data_field attr.
        if ($data_source) {
          $list = FrxReportGenerator::instance()
            ->data_block_params($data_source, $data_field, $label_field, $parms);
          if (!$required && $add_null) {
            $list = array(
              '' => '',
            ) + $list;
          }
        }
        $form['params'][$id] = array(
          '#type' => $type,
          '#title' => $label ? t($label) : t($id),
          '#default_value' => $value,
          '#required' => $required,
          '#description' => t($desc),
        );

        //if $list is not empty then push options

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

        //textfield elements.
        if ($type != 'textfield') {
          $form['params'][$id]['#options'] = $list;
          $form['params'][$id]['#multiple'] = $multiselect;
          if ($ajax) {
            $form['params'][$id]['#ajax'] = array(
              'callback' => 'forena_parameters_callback',
              'wrapper' => 'parameters-wrapper',
            );
          }
        }
      }
      $form['params']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit'),
      );
      return $form;
    }
  }
}