root/c3crm/sugarcrm/trunk/export_report.php

Revision 1137 (by jianting, 04/16/06 23:48:07)

lates report module

<?php
//changed by dingjianting on 2006-2-13 for bug#0000118
 $GLOBALS['sugarEntry'] = true;
/*********************************************************************************
 * The contents of this file are subject to the SugarCRM Public License Version
 * 1.1.3 ("License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at http://www.sugarcrm.com/SPL
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * All copies of the Covered Code must include on each user interface screen:
 *    (i) the "Powered by SugarCRM" logo and
 *    (ii) the SugarCRM copyright notice
 * in the same form as they appear in the distribution.  See full license for
 * requirements.
 *
 * The Original Code is: SugarCRM Open Source
 * The Initial Developer of the Original Code is SugarCRM, Inc.
 * Portions created by SugarCRM are Copyright (C) 2004-2005 SugarCRM, Inc.;
 * All Rights Reserved.
 * Contributor(s): ______________________________________.
 ********************************************************************************/

include_once('config.php');
require_once('log4php/LoggerManager.php');
require_once('include/database/PearDatabase.php');
require_once('include/utils.php');
require_once('modules/Users/User.php');
require_once("modules/Reports/ReportEngine.php");
require_once("modules/Reports/Report.php");
require_once("include/modules.php");
require_once("gb2utf8/class.Chinese.php");


// called from another file.
$GLOBALS['log'] = LoggerManager::getLogger('export');

// check for old config format.
if(empty($sugar_config) && isset($dbconfig['db_host_name']))
{
   make_sugar_config($sugar_config);
}

if (!empty($sugar_config['session_dir'])) {
	session_save_path($sugar_config['session_dir']);
}

session_start();

$user_unique_key = (isset($_SESSION['unique_key'])) ? $_SESSION['unique_key'] : "";
$server_unique_key = (isset($sugar_config['unique_key'])) ? $sugar_config['unique_key'] : "";

if ($user_unique_key != $server_unique_key) {
	session_destroy();
	header("Location: index.php?action=Login&module=Users");
	exit();
}

if(!isset($_SESSION['authenticated_user_id']))
{
	// TODO change this to a translated string.
	session_destroy();
	die("An active session is required to export content");
}

// load the logger

$current_user = new User();

$result = $current_user->retrieve($_SESSION['authenticated_user_id']);
if($result == null)
{
	session_destroy();
	die("An active session is required to export content");
}


$content = '';

global $mod_strings;
$focus =& new Report();
$record = $_REQUEST['record'];
if(!empty($_REQUEST['record'])) {
	$result = $focus->retrieve($_REQUEST['record']);
	if($result == null)
	{
		sugar_die("Error retrieving record.  You may not be authorized to view this record.");
	}
}
else {
	header("Location: index.php?module=Reports&action=index");
}

$primary_table = "";
$primary_condition = "";
$related_table = "";
$related_condition = "";
if(!empty($focus->primarymodule)) {
	include_once($beanFiles[$beanList[$focus->primarymodule]]);
	$bean = new $beanList[$focus->primarymodule]();
	$primary_table = $bean->table_name;
	$primary_bean =  $bean->object_name;
	if(!empty($focus->relatedmodule) && $focus->relatedmodule != $focus->primarymodule) {
		$bean->load_relationships();
		$related_module = strtolower($focus->relatedmodule);
		include_once($beanFiles[$beanList[$focus->relatedmodule]]);
		$bean2 = new $beanList[$focus->relatedmodule]();
		$related_table = $bean2->table_name;
		$params = array();
		$params['join_type'] = ' LEFT JOIN ';
				
		$params['join_table_alias']	= $related_table;
		$params['join_table_link_alias'] = "jtl0";
		$join_sql = '';
		$linkname = $bean->get_related_module_link($related_module);
		if(isset($bean->$linkname)) {
			$join_sql = $bean->$linkname->getJoin($params);
		}
	}
}
$query = "select ".$focus->reportfields." from ".$primary_table." ".$join_sql;
$date = $_REQUEST["stdDateFilterField"];
$startdate =  $_REQUEST["startdate"];
$enddate =  $_REQUEST["enddate"];
$where_date = "";
if(!empty($startdate)) {
	$where_date = " ".$primary_table.".".$date.">='".$startdate." 0:0:0' and ";
}

if(!empty($enddate)) {
	$where_date .= " ".$primary_table.".".$date."<='".$enddate." 23:59:59'";
}
if(!empty($focus->reportconditions)) {
	$focus->reportconditions = str_replace("lessthanandequal","<=",$focus->reportconditions);
	$focus->reportconditions = str_replace("lessthan","<",$focus->reportconditions);
	$focus->reportconditions = str_replace("morethanandequal",">=",$focus->reportconditions);
	$focus->reportconditions = str_replace("morethan",">",$focus->reportconditions);
}
if(!empty($where_date)) {
	$query .= " where ".$where_date;
	if(!empty($focus->reportconditions)) {
		$query .= " and ".$focus->reportconditions;
	}
} else {
	if(!empty($focus->reportconditions)) {
		$query .= " where ".$focus->reportconditions;
	}
}

if(!empty($focus->reportorderby)) {
	$query .= " order by ".$focus->reportorderby;
}
$labels = $focus->reportfields_labels;


$db = PearDatabase::getInstance();
$result = $db->query($query,true,"Error exporting $type: "."<BR>$query");
while($val = $db->fetchByAssoc($result,-1,false))
{
	$new_arr = array();

	foreach (array_values($val) as $value)
	{
		array_push($new_arr, preg_replace("/\"/","\"\"",$value));
	}

	$line = implode("\",\"",$new_arr);
	//内容中文处理
	if(function_exists('iconv')) {
		$line = iconv("UTF-8","GB2312",$line);
	} else {
		$chs = new Chinese("UTF8","GB2312",trim($line));
		$line = $chs->ConvertIT();
	}
	$line = "\"" .$line;
	$line .= "\"\r\n";

	$content .= $line;
}
$content = $labels."\r\n".$content;

header("Pragma: cache");
header("Content-Disposition: inline; filename={$record}.csv");
header("Content-Type: text/csv; charset=UTF-8");
header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" );
header( "Cache-Control: post-check=0, pre-check=0", false );
header("Content-Length: ".strlen($content));
print $content;
sugar_cleanup();
exit;

?>
Note: See TracBrowser for help on using the browser.