You are here

function forena_query_preview in Forena Reports 7.3

Same name and namespace in other branches
  1. 8 forena_query/forena_query.inc \forena_query_preview()
  2. 7.5 forena_query.inc \forena_query_preview()
  3. 7.4 forena_query.inc \forena_query_preview()
1 string reference to 'forena_query_preview'
forena_query_builder_form in ./forena_query.inc

File

./forena_query.inc, line 299

Code

function forena_query_preview($form, &$form_state) {
  $form_state['rebuild'] = TRUE;
  $provider = $form_state['storage']['provider'];
  $block_def = $form_state['storage']['block'];
  $parms = $form_state['storage']['parameters'];
  if (isset($form_state['values']['parameters'])) {
    $parms = $form_state['storage']['parameters'] = $form_state['values']['parameters'];
  }
  FrxReportGenerator::instance()
    ->alter_parameters('', $parms);
  if ($block_def['type'] == 'sql') {
    $sql = $block_def['file'];
    $data = Frx::RepoMan()
      ->sqlData($provider, $sql, $parms);
  }
  else {
    Frx::Data()
      ->push($parms, 'parm');
    $block_name = $form_state['storage']['block_name'];
    $data = Frx::RepoMan()
      ->data($block_name);
  }
  $t = Frx::Template('FrxTable');
  $config = array(
    'class' => 'dataTable',
    'id' => 'preview-data',
  );
  if ($data && $t) {
    $t
      ->generate($data, $config);
    $fields = $t->columns;
    if ($fields) {
      $fields = array_combine($fields, $fields);
    }
    $form_state['storage']['fields'] = $fields;
    if ($fields) {
      $report = $t
        ->asXML();
      $rpt = new FrxReport($report, $data);
      $preview = $rpt
        ->render('web', FALSE);
    }
    else {
      if (is_object($data) && method_exists($data, 'asXML')) {
        $preview = '<pre>' . htmlspecialchars($data
          ->asXML()) . '</pre>';
      }
      else {
        $preview = '<pre>' . htmlspecialchars(print_r($data, 1)) . '</pre>';
      }
    }
    $form_state['storage']['preview'] = $preview;
  }
  else {
    $form_state['storage']['preview'] = '';
  }
}