You are here

function forena_report in Forena Reports 7

Same name and namespace in other branches
  1. 8 forena.module \forena_report()
  2. 6.2 forena.module \forena_report()
  3. 6 forena.module \forena_report()
  4. 7.5 forena.module \forena_report()
  5. 7.2 forena.module \forena_report()
  6. 7.3 forena.module \forena_report()
  7. 7.4 forena.module \forena_report()

Load and render a report based on a drupal path. In this function the arglist is used to get the full path to the report.

Return value

unknown

1 string reference to 'forena_report'
forena_menu in ./forena.module
Implementation of hook_menu.

File

./forena.module, line 355

Code

function forena_report($name_in, $parms = array()) {
  require_once 'forena.common.inc';
  $output = '';
  $desc = forena_report_desc($name_in);
  $name = $desc['name'];
  $format = isset($desc['format']) ? $desc['format'] : '';
  $filename = $desc['filename'];
  $css_files = array();

  // Determine the data to get.
  if (!$parms) {
    $parms = $_GET;
  }
  else {
    $parms = (array) $parms;
  }
  unset($parms['q']);
  if ($name) {
    $r = forena_get_report($name, $parms);
    if ($r) {

      //check for default parameters
      $r_params = $r->parameters;
      $reload_params = FALSE;
      $missing_parms = FALSE;
      if ($r_params) {

        //put default parameters in parms array
        foreach ($r_params as $key => $parm) {
          if (@(!$parms[$key]) && @$parm['value']) {
            $parms[$key] = $parm['value'];
            $reload_params = TRUE;
          }

          //do not show report if a required parameter does not have a value

          //force the user to input a parameter
          if (@(!$parms[$key]) && @strcmp($parm['require'], "1") == 0 && !$parm['value']) {
            $missing_parms = TRUE;
          }
        }
      }

      //Reload report if parameters were missing
      if ($reload_params) {
        $r = forena_get_report($name, $parms);
      }

      // Get data from the report;
      $r_parms = $r->parameters;
      $form = isset($r->options['form']) ? $r->options['form'] : '';
      $rpt_xml = $r->rpt_xml;

      // Default the form
      if (!$form) {
        $form = variable_get('forena_default_form', 'letter');
      }
      $q;

      //put title on top of report
      $title = $r->title;
      if (!$missing_parms) {
        $output .= $r
          ->render($format);
        $css_files = forena_report_css($desc, $form, $format);
      }
      if ($format) {

        //a format was requested
        $header = '<h1>' . $title . '</h1>';
        $output = $header . $output;
        $css_text = '';
        $r_text = '';
        if ($css_files) {
          foreach ($css_files as $css_file) {
            $css_text .= file_get_contents($css_file);
          }
        }
        $options = array(
          'css' => $css_text,
          'docname' => str_replace(' ', '_', $title),
          'xml' => $r_text,
          'title' => $title[0],
        );
        $output = forena_generate_doc($format, $output, $options);
        return $output;
      }
      else {

        // We've got parameters so display the parameters form
        if ($r_parms) {
          $f = drupal_get_form('forena_parameter_form');
          $output = drupal_render($f) . $output;
        }

        //set the title
        $title = $r->title;
        drupal_set_title(filter_xss($title));

        //Creating links for downloadable documents.

        //build querystring for document href
        $q = '';
        foreach ($parms as $key => $value) {
          $q .= "&" . $key . '=' . $value;
        }
        $q = trim($q, '&');

        //Building the document links
        $rpt_xml = $r->rpt_xml;
        $nodes = $rpt_xml
          ->xpath('//frx:docgen/frx:doc');
        $div = '<div class="doclinks">';
        $default_doctypes = variable_get('forena_doc_defaults', array());
        $base = base_path();
        if (!$missing_parms) {
          if (!$nodes) {

            //show the default. All supported links
            foreach ($default_doctypes as $value) {
              if (is_object(forena_get_doctypes($value))) {
                $div .= '<a class="doclinks" href="' . $base . '/report_doc/' . $name_in . '.' . $value . '?' . $q . '">' . strtoupper($value) . '</a>';
              }
            }
          }
          else {

            //There were nodes. show the prefered doc types
            $doctypes = forena_supported_doctypes();
            foreach ($nodes as $value) {
              $arr = $value
                ->attributes();
              $type = (string) $arr['type'];
              if (@$doctypes[$type]) {
                if (is_object(forena_get_doctypes($type))) {
                  $div .= '<a class="doclinks" href="' . $base . '/report_doc/' . $name_in . '.' . $type . '?' . $q . '">' . strtoupper($type) . '</a>';
                }
              }
            }
          }
          $div .= '</div>';
          $output = $div . '<div class="forena-report">' . $output . '</div>';
        }
        $path = drupal_get_path('module', 'forena');
        drupal_add_css($path . '/forena.css', 'module');
        if ($css_files) {
          foreach ($css_files as $css_file) {
            drupal_add_css($css_file, 'module');
          }
        }
        return $output;
      }
    }
    else {

      // Didn't find it so assume we've got a stale cache entry and delete it.
      require_once 'forena.admin.inc';
      forena_delete_report($name, FALSE);
      drupal_not_found();
    }
  }
  else {
    drupal_not_found();
  }
}