Skip to content

Instantly share code, notes, and snippets.

@jmertic
Created November 7, 2013 02:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmertic/7348053 to your computer and use it in GitHub Desktop.
Save jmertic/7348053 to your computer and use it in GitHub Desktop.
Example of extending the REST API in Sugar 7. This example overrides the OOTB Reports export functionality to enable CSV exports. To use this, drop the file in the custom/Reports/clients/base/api/ directory.
<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
/*
* Copyright (c) 2013, John Mertic
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* * Neither the name of the SugarCRM nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
require_once('data/BeanFactory.php');
require_once('include/download_file.php');
require_once('modules/Reports/ReportCache.php');
require_once('modules/Reports/Report.php');
require_once('modules/Reports/templates/templates_export.php');
class ReportsExportCsvApi extends SugarApi
{
// how long the cache is ok, in minutes
private $cacheLength = 10;
public function registerApiRest() {
return array(
'exportRecord' => array(
'reqType' => 'GET',
'path' => array('Reports', '?', 'csv'),
'pathVars' => array('module', 'record', 'export_type'),
'method' => 'exportCsv',
'rawReply'=> true,
'shortHelp' => 'This method exports a record as a CSV file',
'longHelp' => '',
),
);
}
/**
* Export a Report As CSV
* @param ServiceBase $api
* @param array $args Arguments array built by the service base
* @return null
*/
public function exportCsv(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($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('CSV export only valid for Rows and Columns reports');
return;
}
$reporter->layout_manager->setAttribute("no_sort",1);
$reporter->fromApi = true;
//Translate csv to correct language
$_REQUEST['module'] = $reporter->report_def['module'];
$mod_strings = return_module_language($current_language);
//Generate actual csv file, return back as raw content
return template_handle_export($reporter, false);
}
}
}
@visaolive
Copy link

I used this, and the response was the following:

stdClass Object
(
[team_count] =>
[team_name] => Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Global
[name_2] =>
[primary] => 1
)

    )

[id] => b802e797-0fc3-c37a-75c1-523cc73cc9a8
[name] => Call List By Last Date Contacted
[module] => Contacts
[report_type] => tabular
[content] => {"display_columns":[{"name":"date_modified","label":"Last Modified","table_key":"self"},{"name":"full_name","label":"Contact Name","table_key":"self"},{"name":"phone_work","label":"Office Phone","table_key":"self"},{"name":"name","label":"Account Name","table_key":"Contacts:accounts"},{"name":"full_name","label":"Assigned to","table_key":"Contacts:assigned_user_link"}],"module":"Contacts","group_defs":[],"summary_columns":[],"report_name":"Call List By Last Date Contacted","chart_type":"none","numerical_chart_column":"","numerical_chart_column_type":"","assigned_user_id":"1","report_type":"tabular","full_table_list":{"self":{"value":"Contacts","module":"Contacts","label":"Contacts","children":{"accounts":"accounts","reports_to_link":"reports_to_link","team_link":"team_link","created_by_link":"created_by_link","modified_user_link":"modified_user_link","assigned_user_link":"assigned_user_link"},"dependents":[]},"Contacts:accounts":{"label":"Account","link_def":{"relationship_name":"accounts_contacts","name":"accounts","link_type":"many","label":"Account","table_key":"Contacts:accounts","bean_is_lhs":0},"parent":"self","optional":0,"module":"Accounts","name":"Contacts  \u003E  Account","dependents":["display_cols_row_4"]},"Contacts:assigned_user_link":{"label":"Assigned to User","link_def":{"relationship_name":"contacts_assigned_user","name":"assigned_user_link","link_type":"one","label":"Assigned to User","table_key":"Contacts:assigned_user_link","bean_is_lhs":0},"parent":"self","optional":true,"module":"Users","name":"Contacts  \u003E  Assigned to User","dependents":["display_cols_row_6"]}},"filters_def":{"Filter_1":{"operator":"AND","0":{"name":"do_not_call","table_key":"self","qualifier_name":"equals","input_name0":["no"]}}}}
[deleted] => 
[date_entered] => 2013-09-20T15:10:00-07:00
[date_modified] => 2013-09-25T17:22:00-07:00
[assigned_user_id] => 1
[modified_user_id] => 1
[assigned_user_name] => Jen Smith
[created_by] => 1
[is_published] => 
[last_run_date] => 
[chart_type] => none
[schedule_type] => pro
[favorite] => 
[my_favorite] => 
[_acl] => stdClass Object
    (
        [fields] => stdClass Object
            (
            )

    )

[following] => 
[_module] => Reports

)

Question:

  • Is this the correct response, or did I implement this correctly?
  • What part of this array has the actual CSV content?

Thanks,

Emilio

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment