Created
November 7, 2013 02:40
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
)
)
Question:
Thanks,
Emilio