<?php
require_once("xml.php");

function getReportArray($name)
{
	$xml = new xml();
	$rpt_strXML = LoadSelectedReport( $name);
	return $xml->xml_to_array( $rpt_strXML );
}

function getChartArray($name)
{
	$xml = new xml();
	$chrt_strXML = LoadSelectedChart($name);
	return $xml->xml_to_array($chrt_strXML);
}

function GetUserGroups() {
##if @BUILDER.bDynamicPermissions##
	global $conn;
	$arr = array(
		array(-1, "Admin"),
		array(-2, "Default"),
		array(-3, "Guest")
	);
	$rs=db_query("select * from ##@BUILDER.strUGGroups ts## order by Label",$conn);
	while($data = db_fetch_array( $rs )) 
		$arr[]=array($data["GroupID"],$data["Label"]);
##else##
	$arr = array();
##foreach @BUILDER.UserGroups as @u filter strUserName!="<Default>"##
	if ( "##@u.strUserName s##" == "<Guest>" ) {
		$group = "Guest";
	} else {
		$group = "##@u.strUserName s##";
    }
	$arr[]=array($group,"##@u.strUserName s##");
##endfor##
$arr[]=array("Default","<Default>");
##endif##
	return $arr;
}

function GetUserGroup() {
##if GroupSecurity##
	global $conn;
##if @BUILDER.bDynamicPermissions##
	if(@$_SESSION["UserID"]!="Guest")
	{
		$sql = "select distinct GroupID from ##@BUILDER.strUGMembers ts## where UserName='".db_addslashes(@$_SESSION["UserID"])."'";
		$rs = db_query($sql,$conn);
        $groups = array();
		while($rs && $data=db_fetch_numarray($rs))
			$groups[]=$data[0];
		if(!count($groups))
			$groups[]=-2;
		return $groups;
	}
	else 
		return array(-3);
##else##
	return array(@$_SESSION["GroupID"]);
##endif##
##endif##
	return array();
}

function CheckLastID($type) {
	global $conn;
	
	$strSQL = "SELECT rpt_id FROM webreports WHERE rpt_type = '".$type."'";
	$rs = db_query($strSQL,$conn);
	$maxID = 1;
	
	while( $row = db_fetch_numarray( $rs ) ) {
		if ( $maxID < $row[0] ) {
			$maxID = $row[0];
		}
	}
	
	return ++$maxID;
}

function GetNumberFieldsList($table) {
	$t = GetFieldsList($table);
	$arr=array();
	foreach($t as $f)
		if(IsNumberType(GetFieldType($f,$table)))
			$arr[]=$f;
	return $arr;
}

function GetChartsList() {
	global $conn;

	$xml = new xml();
	$arr=array();
	$strSQL = "SELECT rpt_name, rpt_title, rpt_owner, rpt_status, rpt_content";
	$strSQL .= " FROM webreports WHERE rpt_type = 'chart'";
	$rsChart = db_query($strSQL,$conn);
	
	while( $row = db_fetch_numarray( $rsChart ) ) {
		$chart_arr = $xml->xml_to_array( $row[4] );
        $view = 0;
		$edit = 0;
		$arrUserGroup = GetUserGroup();
        
##if @BUILDER.bDynamicPermissions##
		$guestGroup = -3;
		$defaultGroup = -2;
##else##		
		$guestGroup = "Guest";
		$defaultGroup = "Default";
##endif##		

        for ( $i=0; $i < count($arrUserGroup); $i++ ) {
            if ( @$_SESSION["UserID"] != "Guest" ) {
                $arrAllGroups = GetUserGroups();
                $bFlag = 0;
                for ( $j=0; $j < count($arrAllGroups); $j++ ) {
                    if ( $arrUserGroup[$i] == $arrAllGroups[$j][0] ){
                        $bFlag = 1;
                    }
                }
                if ( !$bFlag ) {
                    $arrUserGroup[$i] = $defaultGroup;
                }
            } else {
                $arrUserGroup[$i] = $guestGroup;
            }
        }
        
		if ( isset($chart_arr['permissions']) ) {
			foreach ( $chart_arr['permissions'] as $arr_prm ) {
				if (in_array($arr_prm['id'], $arrUserGroup)) {
					$view = ( $arr_prm['view'] == "true" ) ? 1 : 0;
					$edit = ( $arr_prm['edit'] == "true" ) ? 1 : 0;
				}
			}
		}

		$arr[] = array(
			"name"		=> $row[0],
			"title"		=> $row[1],
			"owner"		=> $row[2],
			"status"	=> $row[3],
			"view"		=> $view,
			"edit"		=> $edit
		);
	}

	return $arr;
}

function LoadSelectedChart($report) {
	global $conn;
	
	$strSQL = "SELECT rpt_content FROM webreports WHERE rpt_name='".db_addslashes($report)."' and rpt_type='chart'";
	$rsReport = db_query($strSQL,$conn);
	$rptContent = db_fetch_numarray($rsReport);

	return $rptContent[0];	
}

function SaveChart($report, $rtitle, $rstatus, $strXML) {
	global $conn;
	
	$strSQL = "SELECT rpt_id FROM webreports WHERE rpt_name='".db_addslashes($report)."' and rpt_type='chart'";
	$rsReport = db_query($strSQL,$conn);
	$data=db_fetch_array($rsReport);
	if ( $data ) {
		$strSQL = "UPDATE webreports SET rpt_title='".db_addslashes($rtitle)."', rpt_content='".db_addslashes($strXML)."', rpt_status='".db_addslashes($rstatus)."', rpt_mdate='".now()."' WHERE rpt_name='".db_addslashes($report)."' and rpt_type='chart'";
		$rsReport = db_exec($strSQL,$conn);
	} else {
		$strSQL = "INSERT INTO webreports ( rpt_name, rpt_title, rpt_cdate, rpt_mdate, rpt_content, rpt_owner, rpt_status, rpt_type )";
		$strSQL .= " VALUES('".db_addslashes($report)."', '".db_addslashes($rtitle)."', '".now()."', '".now()."', '".db_addslashes($strXML)."', '".db_addslashes(@$_SESSION["UserID"])."', '".db_addslashes($rstatus)."', 'chart')";
		$rsReport = db_exec($strSQL,$conn);
	}
}

function DeleteChart($report) {
	global $conn;
	
	$strSQL = "DELETE FROM webreports WHERE rpt_name='".db_addslashes($report)."' and rpt_type='chart'";
	$rsReport = db_exec($strSQL,$conn);
}

function GetReportsList() {
	global $conn;

	$xml = new xml();
	$arr=array();
	$strSQL = "SELECT rpt_name, rpt_title, rpt_owner, rpt_status, rpt_content";
	$strSQL .= " FROM webreports WHERE rpt_type = 'report'";
	$rsReport = db_query($strSQL,$conn);
	
	while( $row = db_fetch_numarray( $rsReport ) ) {
		$report_arr = $xml->xml_to_array( $row[4] );
        $view = 0;
		$edit = 0;
		$arrUserGroup = GetUserGroup();
##if @BUILDER.bDynamicPermissions##
		$guestGroup = -3;
		$defaultGroup = -2;
##else##		
		$guestGroup = "Guest";
		$defaultGroup = "Default";
##endif##		
        
        for ( $i=0; $i < count($arrUserGroup); $i++ ) {
            if ( @$_SESSION["UserID"] != "Guest" ) {
                $arrAllGroups = GetUserGroups();
                $bFlag = 0;
                for ( $j=0; $j < count($arrAllGroups); $j++ ) {
                    if ( $arrUserGroup[$i] == $arrAllGroups[$j][0] ){
                        $bFlag = 1;
                    }
                }
                if ( !$bFlag ) {
                    $arrUserGroup[$i] = $defaultGroup;
                }
            } else {
                $arrUserGroup[$i] = $guestGroup;
            }
        }

		if ( isset($report_arr['permissions']) ) {
			foreach ( $report_arr['permissions'] as $arr_prm ) {
				if (in_array($arr_prm['id'], $arrUserGroup)) {
					$view = ( $arr_prm['view'] == "true" ) ? 1 : 0;
					$edit = ( $arr_prm['edit'] == "true" ) ? 1 : 0;
				}
			}
		}

		$arr[] = array(
			"name"		=> $row[0],
			"title"		=> $row[1],
			"owner"		=> $row[2],
			"status"	=> $row[3],
			"view"		=> $view,
			"edit"		=> $edit
		);
	}
	
	return $arr;
}

function LoadSelectedReport($report) {
	global $conn;
	
	$strSQL = "SELECT rpt_content FROM webreports WHERE rpt_name='".db_addslashes($report)."' and rpt_type='report'";
	$rsReport = db_query($strSQL,$conn);
	$rptContent = db_fetch_numarray($rsReport);

	return $rptContent[0];	
}

function SaveReport($report, $rtitle, $rstatus, $strXML) {
	global $conn;
	
	$strSQL = "SELECT rpt_id FROM webreports WHERE rpt_name='".db_addslashes($report)."' and rpt_type='report'";
	$rsReport = db_query($strSQL,$conn);
	$data=db_fetch_array($rsReport);
	if ( $data ) {
		$strSQL = "UPDATE webreports SET rpt_title='".db_addslashes($rtitle)."', rpt_content='".db_addslashes($strXML)."', rpt_status='".db_addslashes($rstatus)."', rpt_mdate='".now()."' WHERE rpt_name='".db_addslashes($report)."' and rpt_type='report'";
		$rsReport = db_exec($strSQL,$conn);
	} else {
		$strSQL = "INSERT INTO webreports ( rpt_name, rpt_title, rpt_cdate, rpt_mdate, rpt_content, rpt_owner, rpt_status, rpt_type )";
		$strSQL .= " VALUES('".db_addslashes($report)."', '".db_addslashes($rtitle)."', '".now()."', '".now()."', '".db_addslashes($strXML)."', '".db_addslashes(@$_SESSION["UserID"])."', '".db_addslashes($rstatus)."', 'report')";		
		$rsReport = db_exec($strSQL,$conn);
	}
}

function DeleteReport($report) {
	global $conn;
	
	$strSQL = "DELETE FROM webreports WHERE rpt_name='".db_addslashes($report)."' and rpt_type='report'";
	$rsReport = db_exec($strSQL,$conn);
}

function testAdvSearch($table){
##foreach @BUILDER.Tables as @t##
	if($table=="##@t.strDataSourceTable s##"){
    ##if @t.arrFieldObj[bAdvancedSearch].len##
		return 1;
    ##else##
		return 0;
    ##endif##
	}
##endfor##
return 0;
}
function DefURL()
{
$defaulturl="";
##if CreateMenu##
		$defaulturl="menu.php";
##else##
	##foreach @BUILDER.Tables as @t filter @t.bMenuItem order @t.nMenuOrder##
		##if @first##
		##if @t.bList##
		$defaulturl="##@t.strShortTableName##_list.php";
		##elseif @t.nType==titREPORT##
		$defaulturl="##@t.strShortTableName##_report.php";
		##elseif @t.nType==titCHART##
		$defaulturl="##@t.strShortTableName##_chart.php";
		##else##
		$defaulturl="##@t.strShortTableName##_add.php";
		##endif##
		##endif##
	##endfor##
##endif##			
##if !@BUILDER.Tables[bMenuItem].len##
	##if @BUILDER.Tables.bList##
		$defaulturl="##@BUILDER.Tables.strShortTableName##_list.php";
	##elseif @t.nType==titREPORT##
		$defaulturl="##@BUILDER.Tables.strShortTableName##_report.php";
	##elseif @t.nType==titCHART##
		$defaulturl="##@BUILDER.Tables.strShortTableName##_chart.php";
	##else##
		$defaulturl="##@BUILDER.Tables.strShortTableName##_add.php";
	##endif##
##endif##
return $defaulturl;
}
?>