<?
global $timedate;
global $current_user;
global $mod_strings;
class Calendar
{
	var $day_name=array();
	var $day_name_short=array();
	
	var $year;
	var $month;
	var $day;
	
	var $act_status;
	var $act_user;
	var $act_type;
	
	var $box_width=150;
	var $hour_height=60;
	var $hour_label_height=20;
	
	function Calendar()
	{
		require_once("modules/EcmCalendars/language/en_us.lang.php");
		$this->day_name[0]=$GLOBALS['mod_strings']['LBL_MONDAY'];
		$this->day_name[1]=$GLOBALS['mod_strings']['LBL_TUESDAY'];
		$this->day_name[2]=$GLOBALS['mod_strings']['LBL_WEDNESDAY'];
		$this->day_name[3]=$GLOBALS['mod_strings']['LBL_THURSDAY'];
		$this->day_name[4]=$GLOBALS['mod_strings']['LBL_FRIDAY'];
		$this->day_name[5]=$GLOBALS['mod_strings']['LBL_SATURDAY'];
		$this->day_name[6]=$GLOBALS['mod_strings']['LBL_SUNDAY'];
		//print_r($this->day_name);
		$this->day_name_short[0]=$GLOBALS['mod_strings']['LBL_SHORT_MONDAY'];
		$this->day_name_short[1]=$GLOBALS['mod_strings']['LBL_SHORT_TUESDAY'];
		$this->day_name_short[2]=$GLOBALS['mod_strings']['LBL_SHORT_WEDNESDAY'];
		$this->day_name_short[3]=$GLOBALS['mod_strings']['LBL_SHORT_THURSDAY'];
		$this->day_name_short[4]=$GLOBALS['mod_strings']['LBL_SHORT_FRIDAY'];
		$this->day_name_short[5]=$GLOBALS['mod_strings']['LBL_SHORT_SATURDAY'];
		$this->day_name_short[6]=$GLOBALS['mod_strings']['LBL_SHORT_SUNDAY'];
	}
	function navArray()
	{
		if($this->month==12)
		{
			$year=$this->year+1;
			$arr['next']=$year."-01";
		}
		else
		{
			$month=$this->month+1;
			$arr['next']=$this->year."-".$month;
		}
		if($this->month==1)
		{
			$year=$this->year-1;
			$arr['prev']=$year."-12";
		}
		else
		{
			$month=$this->month-1;
			$arr['prev']=$this->year."-".$month;
		}
		return $arr;
	}
	function showNav()
	{
		$arr=$this->navArray();
		$html='<table cellspacing="0" cellpadding="0" border="0" width="100%"><tr><td align="left" valign="bottom"><button onClick="mintajaxget(\'index.php?to_pdf=1&module=EcmCalendars&action=ShowCalendar&date='.$arr['prev'].'&act_user='.$this->act_user.'&act_status='.$this->act_status.'\',\'calendar_dashlet\');" type="button" class="button"><img src="themes/Sugar/images/previous.gif" width="8" height="11" border="0" align="absmiddle"></button></td>';
		$html.='<td align="center" class="monthDashlet">';
		$html.=$this->monthByNo((int)$this->month).' '.$this->year;
		$html.='</td>';
		$html.='<td align="right" valign="bottom"><button onClick="mintajaxget(\'index.php?to_pdf=1&module=EcmCalendars&action=ShowCalendar&date='.$arr['next'].'&act_user='.$this->act_user.'&act_status='.$this->act_status.'\',\'calendar_dashlet\');" type="button" class="button"><img src="themes/Sugar/images/next.gif" width="8" height="11" border="0" align="absmiddle"></button></td></tr></table><br>';
		return $html;
	}
	function showNavDay()
	{
		$tomorrow=date("Y-m-d",@mktime(0,0,0,$this->month,$this->day,$this->year)+24*3600);
		$yesterday=date("Y-m-d",@mktime(0,0,0,$this->month,$this->day,$this->year)-24*3600);
		$html='<table cellspacing="0" cellpadding="0" border="0" width="100%"><tr><td align="left" valign="bottom"><button onClick="document.getElementById(\'calendar\').src=\'index.php?module=EcmCalendars&action=ShowBigCalendar&to_pdf=1&date='.$yesterday.'\';mintajaxget(\'index.php?module=EcmCalendars&action=ShovNav&date='.$yesterday.'&to_pdf=1\',\'navDay\');" type="button" class="button" ><img src="themes/Sugar/images/previous.gif" width="8" height="11" border="0" align="absmiddle"></button></td>';
		$html.='<td align="center" class="monthDashlet">';
		$html.=$this->day.' '.$this->monthByNo((int)$this->month).' '.$this->year;
		$html.='</td>';
		$html.='<td align="right" valign="bottom"><button onClick="document.getElementById(\'calendar\').src=\'index.php?module=EcmCalendars&action=ShowBigCalendar&to_pdf=1&date='.$tomorrow.'\';mintajaxget(\'index.php?module=EcmCalendars&action=ShovNav&date='.$tomorrow.'&to_pdf=1\',\'navDay\');" type="button" class="button"><img src="themes/Sugar/images/next.gif" width="8" height="11"  border="0" align="absmiddle"></button></td></tr></table><br>';
		return $html;
	}
	function showNavMiniCalendar()
	{
		$arr=$this->navArray();
		$html='<table cellspacing="0" cellpadding="0" border="0" width="100%"><tr><td align="left" valign="bottom"><button onClick="mintajaxget(\'index.php?to_pdf=1&module=EcmCalendars&action=ShowMiniCalendar&date='.$arr['prev'].'&act_user='.$this->act_user.'&act_status='.$this->act_status.'\',\'mini_calendar\');" type="button" class="button" title="Previous" ><img src="themes/Sugar/images/previous.gif" width="8" height="11" alt="Previous"  border="0" align="absmiddle"></button></td>';
		$html.='<td align="center" class="monthDashlet">';
		$html.=$this->monthByNo((int)$this->month).' '.$this->year;
		$html.='</td>';
		$html.='<td align="right" valign="bottom"><button onClick="mintajaxget(\'index.php?to_pdf=1&module=EcmCalendars&action=ShowMiniCalendar&date='.$arr['next'].'&act_user='.$this->act_user.'&act_status='.$this->act_status.'\',\'mini_calendar\');" type="button" class="button" title="Next" ><img src="themes/Sugar/images/next.gif" width="8" height="11" alt="Next"  border="0" align="absmiddle"></button></td></tr></table><br>';
		return $html;
	}
	function showFooter()
	{
		$html='<br><table cellspacing="0" cellpadding="0" border="0" width="100%"><tr>';
		$html.='<td align="left">';
		$html.=$GLOBALS['mod_strings']['LBL_USER'].':&nbsp;<select id="act_user" name="act_user" onchange="mintajaxget(\'index.php?to_pdf=1&module=EcmCalendars&action=ShowCalendar&date='.$_REQUEST['date'].'&act_user=\'+this.value+\'&act_status='.$this->act_status.'&act_type='.$this->act_type.'\',\'calendar_dashlet\');">';
		$html.='<option value="All"';
		if($this->act_user=='All')$html.=' selected';
		$html.='>'.$GLOBALS['mod_strings']['LBL_ALL'].'</option>';
		$w=$GLOBALS['db']->query("select user_name,id, first_name, last_name from users where deleted='0' order by user_name asc");
		while($r=$GLOBALS['db']->fetchByAssoc($w))
		{
			$html.='<option value="'.$r['id'].'"';
			if($this->act_user==$r['id'])$html.=' selected';
			$html.='>'.$r['first_name'].' '.$r['last_name'].'</option>';
		}
		$html.='</select></td>';
		
		$html.='<td align="left">'.$GLOBALS['mod_strings']['LBL_ACTIVITIES'].':&nbsp;<select id="act_type" name="act_type" onchange="mintajaxget(\'index.php?to_pdf=1&module=EcmCalendars&action=ShowCalendar&date='.$_REQUEST['date'].'&act_user='.$this->act_user.'&act_status='.$this->act_status.'&act_type=\'+this.value,\'calendar_dashlet\');">';
		$html.='<option value="All"';
		if($this->act_type=="All")$html.=" selected";
		$html.='>'.$GLOBALS['mod_strings']['LBL_ALL'].'</option><option value="Calls"';
		if($this->act_type=="Calls")$html.=" selected";
		$html.='>'.$GLOBALS['mod_strings']['LBL_CALLS'].'</option><option value="Meetings"';
		if($this->act_type=="Meetings")$html.=" selected";
		$html.='>'.$GLOBALS['mod_strings']['LBL_MEETINGS'].'</option><option value="Tasks"';
		if($this->act_type=="Tasks")$html.=" selected";
		$html.='>'.$GLOBALS['mod_strings']['LBL_TASKS'].'</option></select>';
		$html.='</td>';
		
		
		global $app_list_strings;	
		$html.='<td align="right">'.$GLOBALS['mod_strings']['LBL_STATUS'].':&nbsp;<select id="act_status" name="act_status" onchange="mintajaxget(\'index.php?to_pdf=1&module=EcmCalendars&action=ShowCalendar&date='.$_REQUEST['date'].'&act_user='.$this->act_user.'&act_status=\'+this.value+\'&act_type='.$this->act_type.'\',\'calendar_dashlet\');">';
		$html.='<option value="All"';
		$html.=" selected";
		$html.='>Wszystkie</option>';
		foreach ($app_list_strings['activity_status_dom'] as $k=>$v) {
			$html.='<option value="'.$k.'"';
			if($this->act_status==$k)$html.=" selected";
		$html.='>'.$v.'</option>';
		}
		

		$html.='</td>';
		$html.='</tr></table><br>';
		return $html;
	}
	function showFooterMiniCalendar()
	{
		$html='<br><table cellspacing="0" cellpadding="0" border="0" width="100%"><tr>';
		$html.='<td align="left">';
		$html.=$GLOBALS['mod_strings']['LBL_USER'].':&nbsp;<select id="act_user" name="act_user" onchange="mintajaxget(\'index.php?to_pdf=1&module=EcmCalendars&action=ShowMiniCalendar&date='.$_REQUEST['date'].'&act_user=\'+this.value+\'&act_status='.$this->act_status.'&act_type='.$this->act_type.'\',\'mini_calendar\');">';
		$html.='<option value="All"';
		if($this->act_user=='All')$html.=' selected';
		$html.='>'.$GLOBALS['mod_strings']['LBL_ALL'].'</option>';				
		$active_users = get_user_array(FALSE, "Active");
		foreach ($active_users as $id=>$name)
		{
			$html.='<option value="'.$id.'"';
			if($this->act_user==$id)$html.=' selected';
			$html.='>'.$name.'</option>';
		}
		$html.='</select></td>';
		
		$html.='<td align="left">'.$GLOBALS['mod_strings']['LBL_ACTIVITIES'].':&nbsp;<select id="act_type" name="act_type" onchange="mintajaxget(\'index.php?to_pdf=1&module=EcmCalendars&action=ShowMiniCalendar&date='.$_REQUEST['date'].'&act_user='.$this->act_user.'&act_status='.$this->act_status.'&act_type=\'+this.value,\'mini_calendar\');">';
		$html.='<option value="All"';
		if($this->act_type=="All")$html.=" selected";
		$html.='>'.$GLOBALS['mod_strings']['LBL_ALL'].'</option><option value="Calls"';
		if($this->act_type=="Calls")$html.=" selected";
		$html.='>'.$GLOBALS['mod_strings']['LBL_CALLS'].'</option><option value="Meetings"';
		if($this->act_type=="Meetings")$html.=" selected";
		$html.='>'.$GLOBALS['mod_strings']['LBL_MEETINGS'].'</option><option value="Tasks"';
		if($this->act_type=="Tasks")$html.=" selected";
		$html.='>'.$GLOBALS['mod_strings']['LBL_TASKS'].'</option></select>';
		$html.='</td>';
		
		$html.='<td align="right">'.$GLOBALS['mod_strings']['LBL_STATUS'].':&nbsp;<select id="act_status" name="act_status" onchange="mintajaxget(\'index.php?to_pdf=1&module=EcmCalendars&action=ShowMiniCalendar&date='.$_REQUEST['date'].'&act_user='.$this->act_user.'&act_status=\'+this.value+\'&act_type='.$this->act_type.'\',\'mini_calendar\');">';
		$html.='<option value="All"';
		if($this->act_status=="All")$html.=" selected";
		$html.='>'.$GLOBALS['mod_strings']['LBL_ALL'].'</option>';
		global $app_list_strings;
		foreach ($app_list_strings['activity_status_dom'] as $k=>$v) {
			$html.='<option value="'.$k.'"';
			if($this->act_status==$k)$html.=" selected";
			$html.='>'.$v.'</option>';
		}
		$html.='</td>';
		$html.='</tr></table><br>';
		return $html;
	}
	function daysInMonth($month,$year)
	{
 		$days = 31;
 		while (!checkdate($month, $days, $year)) $days--;
 		return $days;
	}
	function getNoOfWeek($month,$year,$day)
	{
		$no=strftime("%W",mktime(0,0,0,$month,$day,$year))+1;
		return $no; 
	}
	function noDayInWeek($month,$year,$day=1)
	{
 		$d = date("w", mktime(0,0,0,$month,$day,$year));
		$nod=array(
			0=>6,
			1=>0,
			2=>1,
			3=>2,
			4=>3,
			5=>4,
			6=>5,
		);
		$dayd=$nod[$d];
 		return $dayd;
	}
	function dayByNo($no)
	{
 		$days = $this->day_name;
		return $days[$no];
	}
	function monthByNo($no) 
	{

		$months = array(1=>$GLOBALS['mod_strings']['LBL_JANUARY'],2=>$GLOBALS['mod_strings']['LBL_FEBRUARY'],3=>$GLOBALS['mod_strings']['LBL_MARCH'],4=>$GLOBALS['mod_strings']['LBL_APRIL'],5=>$GLOBALS['mod_strings']['LBL_MAY'],6=>$GLOBALS['mod_strings']['LBL_JUNE'],7=>$GLOBALS['mod_strings']['LBL_JULY'],8=>$GLOBALS['mod_strings']['LBL_AUGUST'],9=>$GLOBALS['mod_strings']['LBL_SEPTEMBER'],10=>$GLOBALS['mod_strings']['LBL_OCTOBER'],11=>$GLOBALS['mod_strings']['LBL_NOVEMBER'],12=>$GLOBALS['mod_strings']['LBL_DECEMBER']);
		return $months[$no];
	}
	function headerTableDashlet($days="")
	{
		if(!$days)$days=$this->day_name_short;
		$html= '<script type="text/javascript" src="modules/EcmProducts/mintajax.js"></script>';
		$html.=$this->showNav();
		$html.='<table cellspacing="0" cellpadding="1" border="0" width="100%" align="center"><tr valign="top">';
		$html.='<th class="headerDashlet" width="1%">'.$GLOBALS['mod_strings']['LBL_WEEK'].'</th>';
		
		foreach($days as $dn)
		{
			$html.='<th class="headerDashlet" width="14%">'.$dn.'</th>';
		}
		$html.='</tr><tr>';
		
		return $html;
	}
	function headerTableMiniCalendar($days="")
	{
		if(!$days)$days=$this->day_name_short;
		$html=$this->showNavMiniCalendar();
		$html.='<table cellspacing="0" cellpadding="1" border="0" width="100%" align="center"><tr valign="top">';
		$html.='<th class="headerDashlet" width="1%">'.$GLOBALS['mod_strings']['LBL_WEEK'].'</th>';
		
		foreach($days as $dn)
		{
			$html.='<th class="headerDashlet" width="14%">'.$dn.'</th>';
		}
		$html.='</tr><tr>';
		
		return $html;
	}
	function headerTableMonth($days="")
	{
		if(!$days)$days=$this->day_name_short;
		$html='<table style="'.$this->table_style.'" cellspacing="0" cellpadding="1" border="0" width="100%" class="monthBox"><tr valign="top">';
		$html.='<th width="1%" class="monthCalBodyTHWeek">'.$GLOBALS['mod_strings']['LBL_WEEK'].'</th>';
		
		foreach($days as $dn)
		{
			$html.='<th width="14%" class="monthCalBodyTHDay">'.$dn.'</th>';
		}
		$html.='</tr><tr class="monthViewDayHeight">';
		
		return $html;
	}
	function headerTableWeek($days="")
	{
		if(!$days)$days=$this->day_name;
		$html='<table style="'.$this->table_style.'" cellspacing="3" cellpadding="3" border="0" width="100%"><tr valign="top">';
		$html.='<td width="1%">'.$GLOBALS['mod_strings']['LBL_WEEK'].'</td>';
		
		foreach($days as $dn)
		{
			$html.='<td class="monthCalBodyTHDay">'.$dn.'</td>';
		}
		$html.='</tr><tr valign="top">';
		
		return $html;
	}
	function headerTableDay()
	{
		if(!$days)$days=$this->day_name;
		$html='<table style="'.$this->table_style.'" cellspacing="3" cellpadding="3" border="0" width="100%"><tr valign="top">';
		$html.='<td width="1%">'.$GLOBALS['mod_strings']['LBL_WEEK'].'</td>';
		
		foreach($days as $dn)
		{
			$html.='<td class="monthCalBodyTHDay">'.$dn.'</td>';
		}
		$html.='</tr><tr valign="top">';
		
		return $html;
	}
	
	function showTableMiniCalendar($month,$year)
	{
		global $timedate;
		$html=$this->headerTableMiniCalendar();
		
		if($this->noDayInWeek($month,$year)>0)$html.='<td class="weekDashlet">'.$this->getNoOfWeek($month,$year,1).'</td>';
		for($i=0;$i<$this->noDayInWeek($month,$year);$i++)$html.='<td class="dayDashlet">&nbsp;</td>';
		
		for($i=1;$i<$this->daysInMonth($month,$year)+1;$i++)
		{
			if(($this->noDayInWeek($month,$year)+$i-1)%7==0)$html.='</tr><tr><td class="weekDashlet">'.$this->getNoOfWeek($month,$year,$i).'</td>';
 			
			if($i<10)$ni='0'.$i;
			else $ni=$i;
			
			if(($this->noDayInWeek($month,$year)+$i)%7==0)$class="sundayDashlet";
			elseif(date("d")==$i && date("m")==$month && date("Y")==$year)$class="headerDashlet";
			else $class="dayDashlet";
			
			if($this->act_type=="All")
			{
				$num_act=count($this->getActivities($month,$year,$ni,"calls","Calls","date_start"))+count($this->getActivities($month,$year,$ni,"meetings","Meetings","date_start"))+count($this->getActivities($month,$year,$ni,"tasks","Tasks","date_start"));
				$actt=array();
				if($this->showActivities($month,$year,$ni,"calls","Calls","date_start"))$actt[]=$this->showActivities($month,$year,$ni,"calls","Calls","date_start");
				if($this->showActivities($month,$year,$ni,"meetings","Meetings","date_start"))$actt[]=$this->showActivities($month,$year,$ni,"meetings","Meetings","date_start");
				if($this->showActivities($month,$year,$ni,"tasks","Tasks","date_start"))$actt[]=$this->showActivities($month,$year,$ni,"tasks","Tasks","date_start");
				$act=htmlspecialchars('<div class="divDashlet">').implode("<br>",$actt).htmlspecialchars('</div>');
			}
			else
			{
				$num_act=count($this->getActivities($month,$year,$ni,strtolower($this->act_type),$this->act_type,"date_start"));
				$actt=array();
				if($this->showActivities($month,$year,$ni,strtolower($this->act_type),$this->act_type,"date_start"))$ac=$this->showActivities($month,$year,$ni,strtolower($this->act_type),$this->act_type,"date_start");
				$act=htmlspecialchars('<div class="divDashlet">').$ac.htmlspecialchars('</div>');
			}

			if($num_act>0)
			{
				if(date("d")==$i && date("m")==$month && date("Y")==$year)$class="headerDashlet";
				elseif(($this->noDayInWeek($month,$year)+$i)%7==0)$class="actsundayDashlet";
				else $class="actDashlet";
				$mouse=' onclick="
				if(!document.getElementById(\'desc\'))
				{
					showdescriptionCalendar(\''.$GLOBALS['mod_strings']['LBL_ACTIVITIES'].' '.$timedate->to_display($year.'-'.$month.'-'.$ni, "Y-m-d", $timedate->get_date_format()).'\',\''.$act.'\',this,1);
				} 
				else 
				{
					hidedescriptionCalendar();
				};"';
				$img='<br><img'.$mouse.' style="float:right;margin:3px;" src="modules/EcmSales/images/search.gif" border="0">';
			}
			else 
			{
				$mouse=' onmouseout="this.style.fontWeight=\'\';" onmouseover="this.style.fontWeight=\'bold\';hidedescriptionCalendar();"';
				$img='';
			}
			
			$html.='<td style="position:relative;cursor:pointer;" class="'.$class.'"><span onmouseout="this.style.fontWeight=\'\';" onmouseover="this.style.fontWeight=\'bold\';" onclick="document.getElementById(\'calendar\').src=\'index.php?module=EcmCalendars&action=ShowBigCalendar&to_pdf=1&date='.$year.'-'.$month.'-'.$ni.'\';mintajaxget(\'index.php?module=EcmCalendars&action=ShovNav&date='.$year.'-'.$month.'-'.$ni.'&to_pdf=1\',\'navDay\');">'.$ni.'</span>&nbsp;'.$img.'</td>';
		}
		
		$html.='</tr></table>';
		$html.=$this->showFooterMiniCalendar();
		
		return $html;
	}
	function showTableDashlet($month,$year)
	{
		global $timedate;
		$html=$this->headerTableDashlet();
		
		if($this->noDayInWeek($month,$year)>0)$html.='<td class="weekDashlet">'.$this->getNoOfWeek($month,$year,1).'</td>';
		for($i=0;$i<$this->noDayInWeek($month,$year);$i++)$html.='<td class="dayDashlet">&nbsp;</td>';
		
		for($i=1;$i<$this->daysInMonth($month,$year)+1;$i++)
		{
			if(($this->noDayInWeek($month,$year)+$i-1)%7==0)$html.='</tr><tr><td class="weekDashlet">'.$this->getNoOfWeek($month,$year,$i).'</td>';
 			
			if($i<10)$ni='0'.$i;
			else $ni=$i;
			
			if(($this->noDayInWeek($month,$year)+$i)%7==0)$class="sundayDashlet";
			elseif(date("d")==$i && date("m")==$month && date("Y")==$year)$class="headerDashlet";
			else $class="dayDashlet";
			
			if($this->act_type=="All")
			{
				$num_act=count($this->getActivities($month,$year,$ni,"calls","Calls","date_start"))+count($this->getActivities($month,$year,$ni,"meetings","Meetings","date_start"))+count($this->getActivities($month,$year,$ni,"tasks","Tasks","date_start"));
				$actt=array();
				if($this->showActivities($month,$year,$ni,"calls","Calls","date_start"))$actt[]=$this->showActivities($month,$year,$ni,"calls","Calls","date_start");
				if($this->showActivities($month,$year,$ni,"meetings","Meetings","date_start"))$actt[]=$this->showActivities($month,$year,$ni,"meetings","Meetings","date_start");
				if($this->showActivities($month,$year,$ni,"tasks","Tasks","date_start"))$actt[]=$this->showActivities($month,$year,$ni,"tasks","Tasks","date_start");
				$act=htmlspecialchars('<div class="divDashlet"><div class="outTable">').implode("<br>",$actt).htmlspecialchars('</div></div>');
			}
			else
			{
				$num_act=count($this->getActivities($month,$year,$ni,strtolower($this->act_type),$this->act_type,"date_start"));
				$actt=array();
				if($this->showActivities($month,$year,$ni,strtolower($this->act_type),$this->act_type,"date_start"))$ac=$this->showActivities($month,$year,$ni,strtolower($this->act_type),$this->act_type,"date_start");
				$act=htmlspecialchars('<div class="divDashlet">').$ac.htmlspecialchars('</div>');
			}

			if($num_act>0)
			{
				if(date("d")==$i && date("m")==$month && date("Y")==$year)$class="headerDashlet";
				elseif(($this->noDayInWeek($month,$year)+$i)%7==0)$class="actsundayDashlet";
				else $class="actDashlet";
				$mouse=' onclick="
				if(!document.getElementById(\'desc\'))
				{
					showdescriptionCalendar(\''.$GLOBALS['mod_strings']['LBL_ACTIVITIES'].' '.$timedate->to_display($year.'-'.$month.'-'.$ni, "Y-m-d", $timedate->get_date_format()).'\',\''.$act.'\',this,1);
				} 
				else 
				{
					hidedescriptionCalendar();
				};"';
				$img='<br><img'.$mouse.' style="float:right;margin:3px;" src="modules/EcmSales/images/search.gif" border="0">';
			}
			else 
			{
				$mouse=' onmouseout="this.style.fontWeight=\'\';" onmouseover="this.style.fontWeight=\'bold\';hidedescriptionCalendar();"';
				$img='';
			}
			
			$html.='<td style="position:relative;cursor:pointer;" class="'.$class.'"><span onmouseout="this.style.fontWeight=\'\';" onmouseover="this.style.fontWeight=\'bold\';" onclick="location.href=\'index.php?module=EcmCalendars&action=index&date='.$year.'-'.$month.'-'.$ni.'\';">'.$ni.'</span>&nbsp;'.$img.'</td>';
		}
		
		$html.='</tr></table>';
		$html.=$this->showFooter();
		
		return $html;
	}
	function showTableMonth($month,$year)
	{
		$html=$this->headerTableMonth();
		
		$html.='<td class="monthCalBodyWeek" style="'.$this->cell_style.'">'.$this->getNoOfWeek($month,$year,1).'</td>';
		for($i=0;$i<$this->noDayInWeek($month,$year);$i++)$html.='<td style="'.$this->cell_style.'">&nbsp;</td>';
		
		for($i=1;$i<$this->daysInMonth($month,$year)+1;$i++)
		{
			if(($this->noDayInWeek($month,$year)+$i-1)%7==0)$html.='</tr><tr class="monthViewDayHeight"><td class="monthCalBodyWeek" align="center" valign="top" style="'.$this->cell_style.'">'.$this->getNoOfWeek($month,$year,$i).'</td>';
 			
			if($i<10)$ni='0'.$i;
			else $ni=$i;
			$html.='<td class="monthCalBodyWeekDay" style="'.$this->cell_style.';height:10px;" align="right" valign="top"><a href="#"  class="monthCalBodyWeekDayDateLink">'.$ni.'</a><br>';
			$html.=$this->showActivities($month,$year,$ni,"calls","Calls","date_start").'&lt;br&gt;';
			$html.=$this->showActivities($month,$year,$ni,"meetings","Meetings","date_start");
			$html.='</td>';
		}
		
		$html.='</tr></table>';
		
		return $html;
	}
	function showTableWeek($month,$year,$day)
	{
		$html=$this->headerTableWeek($this->day_name);
		
		$days=$this->getDaysWeek($month,$year,$day);
		$html.='<td height="40" valign="top">'.$this->getNoOfWeek($month,$year,$day).'</td>';
		foreach($days as $d)
		{
			$exp=explode("-",$d);
			$html.='<td height="40" valign="top">'.$d.'<br>';
			$html.=$this->showActivities($month,$year,$exp[2],"calls","Calls","date_start").'<br>';
			$html.=$this->showActivities($month,$year,$exp[2],"meetings","Meetings","date_start");
			$html.="</td>";
		}
		
		$html.='</tr></table>';
		
		return $html;
	}
	function showTableDay($month,$year,$day)
	{
		$html='<table id="table_day" cellspacing="0" cellpadding="0" border="0" width="100%">';
		
		for($i=6;$i<=24;$i++){
			if($i<10)$h='0'.$i;
			else $h=$i;
			$html.='<tr class="hourDayView"><td class="hourLabelDayView" id="hour'.$i.'">'.$h.':00</td><td width="100%" class="hourDayView">&nbsp;</td></tr>';
		}
		
		$html.='</table>';
		$html.=$this->showActivitiesDay($month,$year,$day,array("meetings","calls","tasks"),array("Meetings","Calls","Tasks"),array("date_start","date_start","date_start"));
		return $html;
	}
	function matchTime($time1,$duration1,$time2,$duration2)
	{
		$exp1=explode(":",$time1);
		$date1=date("H:i:s",@mktime($exp1[0],$exp1[1],$exp1[2],0,0,0)+$duration1);

		$exp2=explode(":",$time2);
		$date2=date("H:i:s",@mktime($exp2[0],$exp2[1],$exp2[2],0,0,0));

		if($date1>$date2)return true;
	}
	function getActivityHours($time,$duration)
	{
		$exp=explode(":",$time);
		$hours=ceil($duration/3600);
		for($i=0;$i<=$hours;$i++)
		{
			$n=$i*3600;
			$arr[date("H",mktime($exp[0],$exp[1],$exp[2],0,0,0)+$n)]=date("H",mktime($exp[0],$exp[1],$exp[2],0,0,0)+$n);
		}
		//print_r($arr);
		return $arr;
	}
	function getAllActivityHours($arr)
	{
		foreach($arr as $r)
		{
			$time=explode(" ",$r['date']);
			$exp=explode(":",$time[1]);
			for($i=0;$i<(int)$r['duration'];$i++)
			{
				$arrr[date("H",@mktime($exp[0],$exp[1],$exp[2],0,0,0)+$i)]=date("H",@mktime($exp[0],$exp[1],$exp[2],0,0,0)+$i);
			}
		}
		return $arrr;
	}
	function resizeHours($arr)
	{
		$html='';
		foreach($arr as $a)$html.='resizeHours("'.(int)$a.'","'.$this->hour_height.'");';
		return $html;
	}
	function getHourPosition($hour,$resized_hours)
	{
		$v=0;
		for($i=0;$i<=(int)$hour;$i++)$arr[$i]=$this->hour_label_height;
		foreach($resized_hours as $a)$arr[(int)$a]=$this->hour_height;
		for($i=0;$i<=(int)$hour;$i++)$v+=$arr[$i];
		return $v;
	}
	
	function multisort($data,$keys)
	{
		if(count($data)>0)
		{
		foreach($data as $key => $row)
		{
    		foreach($keys as $k)
			{
      			$cols[$k['key']][$key] = $row[$k['key']];
    		}
  		}

  		$idkeys=@array_keys($data);
  		$i=0;
  		foreach($keys as $k)
		{
    		if($i>0)$sort.=',';
   			$sort.='$cols['.$k['key'].']';
    		if($k['sort'])$sort.=',SORT_'.strtoupper($k['sort']);
    		if($k['type'])$sort.=',SORT_'.strtoupper($k['type']);
    		$i++;
  		}
  		$sort.=',$idkeys';
  		$sort='@array_multisort('.$sort.');';
  		eval($sort);
  		foreach($idkeys as $idkey)
		{
    		$result[$idkey]=$data[$idkey];
  		}
  		return $result;
		}
	}
	function ca($position,$boxes_defs,$time,$duration){
		foreach($boxes_defs as $bd){
			if($this->matchTime($bd['box_time'],$bd['box_duration'],$time,$duration) && $position==$bd['box_position'])return false;
		}
		return true;
	}
	function setBoxesPositions($b,$positions){
		$b=$this->multisort($b,array(array('key'=>'box_time','sort'=>'asc')));
		for($i=count($b)-1;$i>=0;$i--){
			for($j=0;$j<count($b);$j++){
				foreach($positions as $position){
					if($i!=$j && $position==$b[$j]['box_position'] && !$this->matchTime($b[$j]['box_time'],$b[$j]['box_duration'],$b[$i]['box_time'],$b[$i]['box_duration'])){
						if($this->ca($position,$b,$b[$i]['box_time'],$b[$i]['box_duration']))$b[$i]['box_position']=$b[$j]['box_position'];
					}
				}
			}
		}
		//print_r($b);
		return $b;
	}
	function showActivitiesDay($month,$year,$day,$table,$module,$date_field)
	{
		$calls=$this->getActivities($month,$year,$day,"calls","Calls","date_start");
		$meetings=$this->getActivities($month,$year,$day,"meetings","Meetings","date_start");
		$tasks=$this->getActivities($month,$year,$day,"tasks","Tasks","date_start");
		
		if(count($calls)>0)foreach($calls as $call)$arr[]=$call;
		if(count($meetings)>0)foreach($meetings as $meeting)$arr[]=$meeting;
		if(count($tasks)>0)foreach($tasks as $task)$arr[]=$task;
		
		$arr=$this->multisort($arr,array(array('key'=>'date','sort'=>'asc')));
		//print_r($arr);
		if(count($arr)>0)
		{
			$cc=0;
			$mn=$this->hour_height/60;
			
			foreach($arr as $r)
			{
				$o++;
				$exp=explode(" ",$r['date']);
				$hour=explode(":",$exp[1]);
				$y=$hour[1]*$mn;//by id

				$height=$this->hour_height;
				
				if($time_temp && $duration_temp && $this->matchTime($time_temp,$duration_temp,$exp[1],$r['duration']))$cc+=$this->box_width;
				else $cc=0;
				$maxcc+=$this->box_width;		
							
				$boxes_defs[]=array(
					'box_position'=>$cc,
					'box_height'=>$height,
					'box_time'=>$exp[1],
					'box_id'=>$r['id'],
					'box_duration'=>$r['duration'],
				);
				
				if($r['status']=="Held" || $r['status']=="Not Held")$activityClass="activityBoxHeld";
				elseif(date("Y-m-d H:i:s")>$r['date'])$activityClass="activityBoxExpired";
				else $activityClass="activityBox";
				
				if($r['status']=="Held" || $r['status']=="Not Held")
				{
					$linkClass="boxLink";
					$linkClassName="boxLinkNameHeld";
					$hourClass="";
				}
				elseif(date("Y-m-d H:i:s")>$r['date'])
				{
					$linkClass="boxLink";
					$linkClassName="boxLinkExpired";
					$hourClass="";
				}
				else 
				{
					$linkClass="boxLink";
					$linkClassName="boxLink";
					$hourClass="";
				}
				
				$u = new User();
				$u -> retrieve($r['user_id']);
				$user = $u->first_name.PHP_EOL.$u->last_name;
				unset($u);
				
				$html.='<div id="activity'.$r['id'].'" class="'.$activityClass.' olFgClass" style="height:'.$height.';width:'.$this->box_width.'px;overflow:none;">';
				$html.='<div class="rowTable">';
				$html.='<div class="cellTable'.$hourClass.'" style="width:70px;"><img src="themes/Sugar/images/'.$r['module'].'.gif" border="0">&nbsp;'.substr($exp[1],0,5).'</div>';
				$html.='<div class="cellTable" style="width:80px;text-align:right;"><a target="_top" class="'.$linkClass.'" href="index.php?module=Employees&action=DetailView&record='.$r['user_id'].'">'.$user.'</a></div>';
				$html.='</div>';	
							
				if(strlen($r['name'])>24)$name=mb_substr($r['name'],0,23,"UTF-8")."&nbsp;...";
				else $name=$r['name'];
				//$html.='<div class="rowTable">';
				$html.='<div style="width:150px;"><a target="_top" class="'.$linkClassName.'" href="index.php?module='.$r['module'].'&action=DetailView&record='.$r['id'].'">'.$r['name'].'</a></div>';
				//$html.='</div>';
				
				if($r['account_name'] && $r['account_id'])
				{
					if(strlen($r['account_name'])>24)$account_name=mb_substr($r['account_name'],0,23,"UTF-8")."&nbsp;...";
					else $account_name=$r['account_name'];
					//$html.='<div class="rowTable">';
					$html.='<div style="width:150px;"><a target="_top" class="'.$linkClass.'" href="index.php?module=Accounts&action=DetailView&record='.$r['account_id'].'">'.$account_name.'</a></div>';
					//$html.='</div>';
				}
				for($i=0;$i<count($r['contact_id']);$i++)
				{
					if($r['contact_id'][$i] && $r['contact_name'][$i])
					{
					if(strlen($r['contact_name'])>24)$contact_name=mb_substr($r['contact_name'][$i],0,23,"UTF-8")."&nbsp;...";
					else $contact_name=$r['contact_name'][$i];
					//$html.='<div class="rowTable">';
					$html.='<div style="width:150px;"><a target="_top" class="'.$linkClass.'" href="index.php?module=Contacts&action=DetailView&record='.$r['contact_id'][$i].'">'.$contact_name.'</a></div><br>';
					//$html.='</div>';
					}
				}
				$html.='</div>';
				$html.='<script language="javascript">'.$this->resizeHours($this->getActivityHours($exp[1],$r['duration'])).'</script>';
				//$html.='<script language="javascript">setBoxStyle('.$hour[0].','.$y.','.$cc.',"'.$r['id'].'");</script>';
				$time_temp=$exp[1];
				$duration_temp=$r['duration'];
			}
			$pos=0;
			$i=0;
			$row=0;
			$maxhour=$this->hour_height;
			foreach($arr as $r){
				$exp=explode(" ",$r['date']);
				$hour=explode(":",$exp[1]);
				
				if($temp_hour==(int)$hour[0] && $i<3){
					$pos+=$this->box_width;
					$i++;
				}
				elseif($temp_hour==(int)$hour[0] && $i>=3){
					$pos=0;
					$i=0;
					$row++;
					$bd_hours[(int)$hour[0]]=($row+1)*$this->hour_height;
				}
				else{
					$pos=0;
					$i=0;
					$row=0;
					$maxhour=$this->hour_height;
				}
				
				$b[(int)$hour[0]][]=array("id"=>$r['id'],"time"=>$exp[1],"pos"=>$pos,"i"=>$i,"row"=>$row);
				$temp_hour=(int)$hour[0];
				$temp_pos=$pos;
				$temp_id=$r['id'];
				$temp_row=$row;
				
			}
			//print_r($b);
			if(count($bd_hours)>0){
				foreach($bd_hours as $key=>$value){
					$html.='<script language="javascript">document.getElementById("hour'.$key.'").style.height="'.$value.'px";</script>';
				}
			}
			if(count($b)>0){
				foreach($b as $bdd){
					foreach($bdd as $bd){
						$hour=explode(":",$bd['time']);
						$y=$this->hour_height*$bd['row'];
						$html.='<script language="javascript">setBoxStyle('.(int)$hour[0].','.$y.','.$bd['pos'].',"'.$bd['id'].'");</script>';
					}
				}
			}
		}
		
		return $html;
	}
	function showActivities($month,$year,$day,$table,$module,$date_field)
	{
		
		$arr=$this->getActivities($month,$year,$day,$table,$module,$date_field);
		if(count($arr)>0)
		{
			$html='';
			foreach($arr as $r)
			{
				if($r['status']=="Held" || $r['status']=="Not Held")
				{
					$linkClass="dashletLink";
					$linkClassName="dashletLinkNameHeld";
					$hourClass="";
				}
				elseif(date("Y-m-d H:i:s")>$r['date'])
				{
					$linkClass="dashletLink";
					$linkClassName="dashletLinkExpired";
					$hourClass="";
				}
				else 
				{
					$linkClass="dashletLink";
					$linkClassName="dashletLink";
					$hourClass="";
				}
				$exp=explode(" ",$r['date']);
				
				$u = new User();
				$u -> retrieve($r['user_id']);
				$user = $u->first_name.PHP_EOL.$u->last_name;
				unset($u);
				
				$html.='<div class="rowTable">';
				$html.='<div class="cellTable'.$hourClass.'" style="width:70px;"><img src="themes/Sugar/images/'.$r['module'].'.gif" border="0">&nbsp;'.substr($exp[1],0,5).'</div>';
				$html.='<div class="cellTable" style="width:140px;"><a class="'.$linkClassName.'" href="index.php?module='.$r['module'].'&action=DetailView&record='.$r['id'].'">'.$r['name'].'</a></div>';
				$html.='<div class="cellTable" style="width:70px;"><a class="'.$linkClass.'" href="index.php?module=Employees&action=DetailView&record='.$r['user_id'].'">'.$user.'</a></div>';
				$html.='</div>';
				if($r['account_name'] && $r['account_id'])
				{
					$html.='<div class="rowTable">';
					$html.='<div class="cellTable" style="width:70px;"></div>';
					$html.='<div class="cellTable" style="width:210px;"><a class="'.$linkClass.'" href="index.php?module=Accounts&action=DetailView&record='.$r['account_id'].'">'.$r['account_name'].'</a></div>';
					$html.='</div>';
				}
				for($i=0;$i<=0;$i++)
				{
					if($r['contact_id'][$i] && $r['contact_name'][$i])
					{
					$html.='<div class="rowTable">';
					$html.='<div class="cellTable" style="width:70px;"></div>';
					$html.='<div class="cellTable" style="width:210px;"><a class="'.$linkClass.'" href="index.php?module=Contacts&action=DetailView&record='.$r['contact_id'][$i].'">'.$r['contact_name'][$i].'</a></div><br>';
					$html.='</div>';
					}
				}
			}
		}
		$html=str_replace("<","&lt;",$html);
		$html=str_replace(">","&gt;",$html);
		$html=str_replace("'","&acute;",$html);
		$html=str_replace('"',"&quot;",$html);
		$html=str_replace("\n","",$html);
		return $html;
	}
	function getDaysWeek($month,$year,$day)
	{
		$no=$this->noDayInWeek($month,$year,$day);
		for($i=0;$i<$no;$i++)
		{
			$days[$i]=date("Y-m-d",mktime(0,0,0,$month,$day,$year)-($no-$i)*24*3600);
		}
		$days[$no]=$year."-".$month."-".$day;
		for($i=0;$i<7-$no;$i++)
		{
			$days[$i+$no]=date("Y-m-d",mktime(0,0,0,$month,$day,$year)+$i*24*3600);
		}
		
		return $days;
	}
	function getActivities($month,$year,$day,$table,$module,$date_field,$status="")
	{
		if($month<10 && strlen($month)<2)$month="0".$month;
		global $timedate,$current_user;
		if($this->act_status=="All" || !$this->act_status)$status="";
		else $status= " and ".$table.".status='".$this->act_status."'";
		if($this->act_user=="All" || !$this->act_user)$assigned_user="";
		else $assigned_user= " and ".$table.".assigned_user_id='".$this->act_user."'";

		$q="select ".$table.".*,users.user_name as user,users.id as user_id from ".$table." inner join users on users.id=".$table.".assigned_user_id where ".$table.".deleted='0' and ".$table.".".$date_field." like '".$year."-".$month."-".$day."%'".$status.$assigned_user." order by ".$table.".".$date_field." asc";
//print $q;
		$query=$GLOBALS['db']->query($q);
		while($row=$GLOBALS['db']->fetchByAssoc($query))
		{
			$exp=explode(" ",$row[$date_field]);
			$expp=explode(":",$exp[1]);
			$expd=explode("-",$exp[0]);
			if((int)$expp[0]>3){
			
			$r=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select name from accounts where id='".$row['parent_id']."'"));
			$account_name=$r['name'];
			if($table!="tasks")
			{
			$w=$GLOBALS['db']->query("select contacts.first_name as first_name,contacts.last_name as last_name,contacts.id as id from ".$table."_contacts inner join contacts on contacts.id=".$table."_contacts.contact_id where ".$table."_contacts.deleted='0' and ".substr($table,0,strlen($table)-1)."_id='".$row['id']."' and contacts.deleted='0'");
			$contact_name=array();
			$contact_id=array();
			while($r=$GLOBALS['db']->fetchByAssoc($w))
			{
				$contact_name[]=$r['first_name']." ".$r['last_name'];
				$contact_id[]=$r['id'];
			}
			}
			$duration=($row['duration_hours']*60+$row['duration_minutes'])*60;
			$ddate=date("Y-m-d H:i:s",mktime($expp[0],$expp[1],$expp[2],$expd[1],$expd[2],$expd[0]));
			
			$arr[]=array(
				"id"=>$row['id'],
				"name"=>$row['name'],
				"date"=>$timedate->handle_offset($ddate, "Y-m-d H:i:s", true, $current_user),
				"module"=>$module,
				"user"=>$row['user'],
				"user_id"=>$row['user_id'],
				"duration"=>$duration,
				"account_id"=>$row['parent_id'],
				"account_name"=>$account_name,
				"contact_id"=>$contact_id,
				"contact_name"=>$contact_name,
				"status"=>$row['status'],
			);
			}
		}
		return $arr;
	}
}
?>