You are here

function forena_save_report in Forena Reports 8

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

Save the report file to disk

Parameters

string $report_name : File name to save report to

string $report : Report xml to save.

$save_file: Wheter to save file to disk.

$altered: Where or not he report has been altered?

Return value

int Indictates whether report was successfully saved.

1 call to forena_save_report()
ReportEditor::save in src/Editor/ReportEditor.php
Save report

File

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

Code

function forena_save_report($report_name, $report, $save_file = FALSE, $altered = 0) {
  static $save_count = 0;
  if ($report && !is_object($report)) {
    try {
      $report = new SimpleXMLElement($report);
    } catch (Exception $e) {
      $this
        ->error(t('Could not save %s because XML was malformed', htmlspecialchars($report_name)), "<p>Invalid XML</p><pre>XML:" . htmlspecialchars($report) . "\n" . $e
        ->getMessage() . "</pre>");
      return 0;
    }
  }
  if (!_forena_verify_directory($report_name) && $save_file) {
    drupal_set_message(t('Error creating directory.  Check Permissions'), 'error');
    return 0;
  }
  $report_path = forena_report_path();
  $r = new FrxReport($report);
  $data['title'] = $r->title;
  $data['category'] = $r->category;
  $data['options'] = $r->options;
  $data['descriptor'] = $r->descriptor;
  $data['name'] = $report_name;
  $data['altered'] = $altered;
  $r_xml = $report
    ->asXML();
  $name = $data['name'];
  $filepath = $report_name . '.frx';

  // If we need to save this to the file system
  if ($save_file) {
    FrxAPI::File()
      ->save($filepath, $r_xml);
  }

  // Get the security caches from the reports
  if ($report) {
    $cache = forena_load_cache($report);
  }
  else {
    $cache = '';
  }
  $rpt_cache = '';
  if ($cache) {
    $rpt_cache = serialize($cache);
  }

  // Set default interpretations of data
  $data['enabled'] = isset($data['enabled']) && $data['enabled'] ? 1 : 0;
  if (isset($data['options']['hidden']) && (string) $data['options']['hidden']) {
    $data['hidden'] = $data['options']['hidden'] && $data['options']['hidden'] != 'N' && $data['options']['hidden'] != '0' ? 1 : 0;
    if (!$data['category']) {
      $data['category'] = 'All';
    }
  }
  else {

    // Set hidden based on category
    $data['hidden'] = $data['category'] ? 0 : 1;
  }
  $save_count++;
  $r = NULL;
  $result = NULL;
  $r_xml = NULL;
  $report = NULL;
  $data = NULL;
  $rpt = NULL;
  $cache = NULL;
  $rpt_cache = NULL;

  // Destroy object to clear memory leaks.
  if ($r) {
    $r
      ->__destruct();
    unset($r);
  }
  return $save_count;
}