<?php

//header('Content-Type: text/html; charset=UTF-8');
$reporting = false;
$dont_close_session = true;
include_once('../common.inc.php');
//echo ini_get("default_charset");
//dump($_POST);
foreach ($_POST as $k => $v) {
	$v = trim($v);
	if(!empty($v)){
		if (function_exists('mb_detect_encoding') ) {
			$enc =  mb_detect_encoding($v,  mb_detect_order(), true);
			//echo "$enc : $k<br>";
			$v = mb_convert_encoding($v, 'UTF-8', $enc);		
		}
		$_REQUEST[$k] = $v;
		
	}
}

AccessCheck();

if(!empty($_REQUEST['saveDashboard'])) { $saveDashboard = $_REQUEST['saveDashboard']; } else { die( json_encode(array("Status" => "Error", "Message" => "No saveDashboard parameter found")) ); }
if(!empty($_REQUEST['dashboardname'])) { $dashboardname = $_REQUEST['dashboardname']; } else { die( json_encode(array("Status" => "Error", "Message" => "No dashboard name found")) ); }

# these options may never be saved (this used to contain displaymode, but we removed that and have no idea why it was here in the first place)
$never_save = array('IgnReIm','ReIm','regenerateDatafiles');

if(($_REQUEST['dashboardname'] != "Autosaved Workspace" && $_REQUEST['dashboardname'] != "Default") || $saveDashboard == "delete"){
	if(!empty($session->subscription) && !empty($session->subscription['Dashboards']) && $session->subscription['Dashboards'] == "Disabled"){
		$m = str_replace("%x",  _DASHBOARDS , _SUBSCRIPTION_UPGRADE_NOTICE."<br/><br/><button class='btn-upgrade-subscription btn btn-primary'>"._UPGRADE_NOW."</button>");
		die( json_encode(array("Status" => "Error", "Message" => $m)) );

	}
}

if(substr($saveDashboard,1,1) == "\\"){
	$saveDashboard = stripslashes($saveDashboard);
	$dashboardname = stripslashes($dashboardname);
}
if($saveDashboard != "delete") {
	//dump($saveDashboard);
	$tmp_storage = json_decode($saveDashboard,true);
	//dump($saveDashboard);
	if(!empty($tmp_storage['startup']) && $tmp_storage['startup'] != 'off') {
		foreach($profile->dashboards as $k => $dashboarddata) {
			$tmp_dashboarddata = json_decode($dashboarddata['Value'],true);

			$tmp_dashboarddata['startup'] = 'off';
			
			$tmp_dashboarddata = json_encode($tmp_dashboarddata);
			$profile->dashboards[$dashboarddata['Name']] = array(
				"Name" => $dashboarddata['Name']
				,"Value" => $tmp_dashboarddata
				,"Profile" => $conf
			); 
		}
	}
	//dump($tmp_storage);
	if (!is_array($tmp_storage['reports'])) {
		echo json_encode(array("Status" => "Error", "Message" => "There was a problem saving your dashboard"));
		exit(); 
	}
	foreach ($tmp_storage['reports'] as $rk => $row) {
		foreach ($row as $k => $report) {
			$url = $report['url'];
			
			$new_url = "";
			$valid_options = [];
			$url = str_replace("reports.php?","",$url);
			$options = explode("&",$url);

			foreach ($options as $option) {
				
				$kv = explode("=",$option);

				if ($tmp_storage['daterange_lock'] == "off") {					
					if ($kv[0] == "minimumDate" || $kv[0] == "maximumDate") {						
						continue;
					}
				}

				if(!in_array($kv[0],$never_save)){
					$valid_options[] = $option;
				}

			}
			$new_url = "reports.php?" . implode("&",$valid_options);

			$tmp_storage['reports'][$rk][$k]['url'] = $new_url;
		}
	}
	
	$profile->dashboards["{$conf}.dashboards.{$dashboardname}"] = array(
		"Name" => "{$conf}.dashboards.{$dashboardname}"
		,"Value" => json_encode($tmp_storage)
		,"Profile" => $conf
	);

	echo json_encode(array("Status" => "Success", "dashboard" => $tmp_storage), JSON_UNESCAPED_UNICODE); 

} elseif($saveDashboard == "delete") {
	$dashboardname = stripslashes($dashboardname);
	unset($profile->dashboards["{$conf}.dashboards.{$dashboardname}"]);
	
	echo json_encode(array("Status" => "Success", "Message" => "{$dashboardname} "._HAS_BEEN_DELETED)); 
}
$profile->Save();
?>