AnsweredAssumed Answered

REST v10 Custom Report Export with Runtime Filters

Question asked by Gavan Walker on May 10, 2016

Hey Guys,

 

@Mike Russel has helped me on this forum get a custom export type for JSON to export a specific report, my next question is exporting data from a report with Run time filters.

I have a date field which I have made a RunTime filter within the Sugar REPORT and want to use Is Between but unsure of the structure of my $_POST array and how to apply this to the report.

 

I have the report exporting via JSON, but in the examples of filters they are all OR and AND operators and unsure how to pass my two date ranges in an array and have it set to BETWEEN.

 

Within the RegisterApiRest function I have the below type put in ready to take POST arguments

 

    public function registerApiRest() {
        return array(
            'exportRecord' => array(
                'reqType' => 'GET',
                'path' => array('Reports', '?', '?'),
                'pathVars' => array('module', 'record', 'export_type'),
                'method' => 'exportJSON',
                'rawReply'=> true,
                'shortHelp' => 'This method exports a record in the JSON format',
                'longHelp' => '',
                ),
            'exportRecordPost' => array(
                'reqType' => 'POST',
                'path' => array('Reports', '?', '?'),
                'pathVars' => array('module', 'record', 'export_type'),
                'method' => 'exportJSON',
                'rawReply'=> true,
                'shortHelp' => 'This method exports a record in the specified type. Allows for passing in of Report Run Time filters to alter the exported report data.',
                'longHelp' => '',
                ),
            );
    }

 

This sets up the foundation to POST my array with the RunTime Filters

 

Within my exportJSON method I have a check to detect whether or not there is any $_POST

 public function exportJson(ServiceBase $api, array $args)
    {
        $this->requireArgs($args,array('record'));
        $args['module'] = 'Reports';
        $GLOBALS['disable_date_format'] = FALSE;
        $saved_report = $this->loadBean($api, $args, 'view');

        if(!$saved_report->ACLAccess('view')) {
            throw new SugarApiExceptionNotAuthorized('No access to view records for module: Reports');
        }


        global $beanList, $beanFiles;
        global $sugar_config,$current_language;
        $report_filename = false;

  if (isset($_POST)){
  $saved_report = $this->updateRunTimeFilters($saved_report);
  }

        if($saved_report->id != null)
        {
            //Translate pdf to correct language
            $reporter = new Report(html_entity_decode($saved_report->content), '', '');
            if ( $reporter->report_def['report_type'] != 'tabular' ) {
                throw new SugarApiException('JSON export only valid for Rows and Columns reports');
                return;
            }
            $reporter->layout_manager->setAttribute("no_sort",1);
            $reporter->fromApi = true;


            $_REQUEST['module'] = $reporter->report_def['module'];
            $mod_strings = return_module_language($current_language);
            //Generate actual json file, return back as raw content

 

The updateRunTimeFilters method is from @Mike Russel

I am unsure exactly how to pass through an array with my date range and set to Is Between and have the below function parase that array from $_POST and alter the report..

protected function updateRunTimeFilters(SugarBean $report) 
  {
            global $current_user;         // do this to go through the transformation


  $report_def = isset($_POST['report_def']) ? html_entity_decode($_POST['report_def']) : array();
  $reportID = $report->id;


  if (!empty($_POST['reset_filters'])) {
  $rCache = new ReportCache();
  $rCache->delete($report->id);


  $newArray = array();
  $newArray['filters_def'] = $report->report_def['filters_def'];
  $reportCache = saveReportFilters($reportID, json_encode($newArray));
  $report = new Report($report->content);
  }
  else if (!empty($_POST['report_def'])) {
  $requestFilterDef = json_decode($report_def, TRUE);
  $reportContent = json_decode($report->content, TRUE);


  $reportContent['filters_def'] = $requestFilterDef['filters_def'];
  if (isset($requestFilterDef['order_by'])) {
  $reportContent['order_by'] = $requestFilterDef['order_by'];
  }
  if (isset($requestFilterDef['summary_order_by'])) {
  $reportContent['summary_order_by'] = $requestFilterDef['summary_order_by'];
  }
  $report_def = json_encode($reportContent);


  $report = new Report($report_def);
  $newArray = array();
  $newArray['filters_def'] = $requestFilterDef['filters_def'];
  $reportCache = saveReportFilters($reportID, json_encode($newArray));
  }
  return $report;
    }

 

Is anyone able to assist please.

Thank you.

Outcomes