<?
//error_reporting(0);

//*****************************************************************
function calculateGMT() {
	static $gmt;
	if(!isset($gmt)) {
		global $timedate;
		$dd = gmdate("Y-m-d H:i:s");
		$dd2 = $timedate->handle_offset($dd, "Y-m-d H:i:s", true);
		$gmt = strtotime($dd2) - strtotime($dd);
	}
	return $gmt;
}
//*****************************************************************

function getList($type,$status,$reminder_time,$order_by,$date_field,$module,$role_id)
{
	global $timedate;
	
	if($status=="showall")
	{
		$stat=1;
		$status="Held";
	}
	$gmt = calculateGMT();
	$i=0;
	$z="select * from ".$type." where deleted='0' ".$reminder_time." and status!='".$status."' order by ".$order_by." desc";
	
	$w=$GLOBALS['db']->query($z);
	while($r=$GLOBALS['db']->fetchByAssoc($w))
	{
		$show=0;
		if($r['assigned_user_id']==$_SESSION['authenticated_user_id'])$show=1;

		if($show==1)
		{
			$date_start=explode(" ",$r[$date_field]);
			$ts=explode(":",$date_start[1]);
			$ds=explode("-",$date_start[0]);
			if(strtotime($r[$date_field])-$r['reminder_time'] <= strtotime(gmdate("Y-m-d H:i:s")) || $stat)
			{
				$name[$i]['date_start'] = $date_start[0];
				$name[$i]['time_start']= $date_start[1];
				$name[$i]['name']=$r['name'];
				$name[$i]['id']=$r['id'];
				$name[$i]['type']=$type;
				$name[$i]['status']=$r['status'];
				$name[$i]['reminder_time']=$r['reminder_time'];
				$name[$i]['parent_id']=$r['parent_id'];
				$name[$i]['parent_module']=$r['parent_module'];
				$name[$i]['description']=$r['description'];
				$name[$i]['status']=$r['status'];
				$name[$i]['module']=$module;
				
				if($type=="ecmreminders")$name[$i]['icon']= $r['parent_name'];//"ListEcmDocs";
				else $name[$i]['icon']=$module;
				$name[$i]['edit']=1;
				$name[$i]['record_id']=$r['id'];
				$name[$i]['record_module']=$module;
				if($type=="ecmreminders")
				{
					$name[$i]['edit']=0;
					$name[$i]['record_id']=$r['parent_id'];
					$name[$i]['record_module']=$r['parent_name'];
				}
			
				$i++;
			}
		}
	}
	if (isset($name))
		return $name;
	else return '';
}
function postponeBy($date_field,$type)
{
	global $timedate;
	if(count($_POST['check']))
	{
		foreach($_POST['check'] as $check)
		{
			if($check)
			{
				/*$r=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select reminder_time,".$date_field." from ".$type." where id='".$check."'"));
				if($r[$date_field] && $r['reminder_time'])
				{
					$date_start=explode(" ",$r[$date_field]);
					$ts=explode(":",$date_start[1]);
					$ds=explode("-",$date_start[0]);
					$new_date=gmdate("Y-m-d H:i:s",mktime($ts[0],$ts[1],$ts[2],$ds[1],$ds[2],$ds[0])+$_POST['rsec']+7200);
					$ndate=explode(" ",$new_date);
					$GLOBALS['db']->query("update ".$type." set ".$date_field."='".$ndate[0]." ".$ndate[1]."' where id='".$check."'");
				}*/
				$GLOBALS['db']->query("update ".$type." set reminder_time=reminder_time-".$_REQUEST['rsec']." where id='".$check."'");
			}
		}
	}
}
function postponeTo($date_field,$type)
{
	global $timedate;
	
	$gmt = calculateGMT();
	
	if(count($_POST['check']))
	{
		foreach($_POST['check'] as $check)
		{
			if($check)
			{
				$r=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select reminder_time,".$date_field." from ".$type." where id='".$check."'"));
				$date=$r[$date_field];
				$time=strtotime($date);
				$date_time=strtotime($timedate->to_display($_REQUEST['date'],$timedate->get_date_format(), "Y-m-d")." ".$_REQUEST['hour'].":".$_REQUEST['minute'].":00");
				$reminder_time=$time-$date_time+$gmt;
				$GLOBALS['db']->query("update ".$type." set reminder_time='".$reminder_time."' where id='".$check."'");
				/*
				$time_start=date("H:i:s",strtotime($_REQUEST['date'][6].$_REQUEST['date'][7].$_REQUEST['date'][8].$_REQUEST['date'][9]."-".$_REQUEST['date'][3].$_REQUEST['date'][4]."-".$_REQUEST['date'][0].$_REQUEST['date'][1]." ".$_REQUEST['hour'].":".$_REQUEST['minute'].":00"));
				$GLOBALS['db']->query("update ".$type." set ".$date_field."='".$_REQUEST['date'][6].$_REQUEST['date'][7].$_REQUEST['date'][8].$_REQUEST['date'][9]."-".$_REQUEST['date'][3].$_REQUEST['date'][4]."-".$_REQUEST['date'][0].$_REQUEST['date'][1]." ".$time_start."' where id='".$check."'");*/
				
			}
		}
	}
}
function closeSelected($type,$status,$reminder_time)
{
	global $timedate;
	if(count($_POST['check']))
	{
		foreach($_POST['check'] as $check)
		{
			if($check)
			{
				if($reminder_time=="-1")$rt=",reminder_time='-1'";
				else $rt="";
				$GLOBALS['db']->query("update ".$type." set status='".$status."'".$rt." where id='".$check."'");
			}
		}
	}
}
function reminderTime($type)
{
	global $timedate;
	if(count($_POST['check']))
	{
		foreach($_POST['check'] as $check)
		{
			if($check)
			{
				$r=$GLOBALS['db']->FetchByAssoc($GLOBALS['db']->query("select reminder_time from ".$type." where id='".$check."'"));
				$rtime=$_REQUEST['reminder_minutes']+$_REQUEST['reminder_hours'];
				if($rtime<0)$rtime=60;
				$GLOBALS['db']->query("update ".$type." set reminder_time='".$rtime."' where id='".$check."'");
			}
		}
	}
}
function ListView($name)
{
	global $timedate;
	$html="";
	for($i=0;$i<count($name);$i++)
	{
		$value=$name[$i];
		$ts=explode(":",$value['time_start']);
	
		if($value['reminder_time'] != -1)
		{
			$rem = date("Y-m-d H:i:s",strtotime($value['date_start'].' '.$value['time_start'])-$value['reminder_time']+calculateGMT());
			$rexp=explode(" ",$rem);
			$date_rem=$rexp[0];
			$rrexp=explode(":",$rexp[1]);
		}
		else $rem="";
		
		if($i==0)
		{
			$check="checked";
			global $timedate;
			$_SESSION['reminder']['date']=$timedate->to_display_date($date_rem,false);
			$_SESSION['reminder']['hour']=$rrexp[0];
			$_SESSION['reminder']['minute']=$rrexp[1];
		}
		else $check="";
		
		$onclick='<a class="listViewTdLinkS1" style="cursor:pointer;" onclick="window.open(\'index.php?module='.$value['record_module'].'&action=DetailView&record='.$value['record_id'].'&close=1\',\'CRM\',\'height=780,width=1014,scrollbars=yes\',\'\').focus();">'.$value['name'].'</a>';
		$editlink="";
		if($value['edit'])$editlink='<a style="cursor:pointer;" onclick="window.open(\'index.php?module='.$value['module'].'&action=EditView&record='.$value['id'].'\',\'CRM\',\'height=780,width=1014,scrollbars=yes\',\'\').focus();"><img src="themes/Sugar/images/edit_inline.gif" border="0"></a>';

		$ddd = date("Y-m-d H:i:s",strtotime($value['date_start'].' '.$value['time_start'])-$value['reminder_time']);
		$ddd2 = date("Y-m-d H:i:s",strtotime($value['date_start'].' '.$value['time_start']));
		$html.='<tr>
					<td valign="top" class="oddListRowS1" bgcolor="#ffffff"><input onclick="sListView.check_item(this, document.MassUpdate);show(\''.$value['id'].'\',\''.$timedate->to_display($date_rem, "Y-m-d",$timedate->get_date_format()).'\',\''.$rrexp[0].'\',\''.$rrexp[1].'\');" type="checkbox" name="check[]" id="check'.$value['id'].'" value="'.$value['id'].'" '.$check.'></td>
					<td valign="top" class="oddListRowS1" bgcolor="#ffffff"><img src="themes/default/images/'.$value['icon'].'.gif" border="0"></td>
					<td valign="top" class="oddListRowS1" bgcolor="#ffffff">'.$onclick.'</td>
					<td valign="top" class="oddListRowS1" bgcolor="#ffffff">'.$timedate->to_display_date_time($ddd).'</td>
					<td valign="top" class="oddListRowS1" bgcolor="#ffffff">'.$timedate->to_display_date_time($ddd2).'</td>
					<td valign="top" class="oddListRowS1" bgcolor="#ffffff">'.$value['status'].'</td>
				</tr>';
	}
	return $html;
}
?>
