<?php
$keyMaster= $_SESSION['project_id'];

if ($_SESSION['language'] == "Spanish") {
$language=<<<EOT
// Language
g.addLang('es',{
	'january': 'Enero',
	'february': 'Febrero',
	'march': 'Marzo',
	'april': 'Abril',
	'maylong': 'Mayo',
	'june': 'Junio',
	'july': 'Julio',
	'august': 'Agosto',
	'september': 'Septiembre',
	'october':'Octubre',
	'november':'Noviembre',
	'december':'Diciembre',
	'jan':'Ene',
	'feb':'Feb',
	'mar':'Mar',
	'apr':'Abr',
	'may':'May',
	'jun':'Jun',
	'jul':'Jul',
	'aug':'Ago',
	'sep':'Sep',
	'oct':'Oct',
	'nov':'Nov',
	'dec':'Dic',

	'sunday':'Domingo',
	'monday':'Lunes',
	'tuesday':'Martes',
	'wednesday':'Miércoles',
	'thursday':'Jueves',
	'friday':'Viernes',
	'saturday':'Sábado',
	'sun':'	Dom',
	'mon':'	Lun',
	'tue':'	Mar',
	'wed':'	Mie',
	'thu':'	Jue',
	'fri':'	Vie',
	'sat':'	Sab',
	'resource':'Recurso',
	'duration':'Duración',
	'comp':'% Compl.',
	'completion':'Terminado',
	'startdate':'Inicio',
	'enddate':'Fin',
	'moreinfo':'+información',
	'notes':'Notas',

	'format':'Formato',
	'hour':'Hora',
	'day':'Dia',
	'week':'Semana',
	'month':'Mes',
	'quarter':'Trimestre',
	'hours':'Horas',
	'days':'Días',
	'weeks':'Semanas',
	'months':'Meses',
	'quarters':'Trimestres',
	'hr':'Hr',
	'dy':'D',
	'wk':'Sem',
	'mth':'Mes',
	'qtr':'Trim',
	'hrs':'Hrs',
	'dys':'Dias',
	'wks':'Sems',
	'mths':'Meses',
	'qtrs':'Trims'});

g.setLang('es');
EOT;
} else {
$language='';	
}

if ($_SESSION['language'] == "Spanish") {
	$options=<<<EOT
	g.setDateTaskTableDisplayFormat('dd/mm/yyyy'); // Set format to display dates ('mm/dd/yyyy', 'dd/mm/yyyy', 'yyyy-mm-dd')
	g.setDateTaskDisplayFormat('day dd month yyyy'); // Shown in tool tip box
	g.setDayMajorDateDisplayFormat('mon yyyy - Week ww') // Set format to display dates in the "Major" header of the "Day" view
	g.setWeekMinorDateDisplayFormat('dd mon') 	// Set format to display dates in the "Minor" header of the "Week" view
EOT;
} else {
	$options=<<<EOT
	g.setDateTaskTableDisplayFormat('mm.dd.yyyy'); // Set format to display dates ('mm/dd/yyyy', 'dd/mm/yyyy', 'yyyy-mm-dd')
	// g.setDateTaskDisplayFormat('day dd month yyyy'); // Shown in tool tip box
	// g.setDayMajorDateDisplayFormat('yyyy mon- Week ww') // Set format to display dates in the "Major" header of the "Day" view
	// g.setWeekMinorDateDisplayFormat('mon dd') // Set format to display dates in the "Minor" header of the "Week" view
EOT;
}

$records ='';
// Read Task the Project
$sql="SELECT
			t.`task_id`,
			t.`name`,
			DATE_FORMAT(t.`start_date`,'%Y-%m-%d') `start_date`,
			DATE_FORMAT(t.`end_date`,'%Y-%m-%d') `end_date`,
			t.`dynamic`,
			t.`related_url`,
			t.`milestone`,
			t.`percent_complete`,
			ifnull(t.`parent`,0) parent,
			ifnull(t.`dependent`,0) dependent,
			t.`description`,
			ifnull(u.login,'') creator
		FROM tasks t
			inner join users u on (t.creator=u.user_id)
		WHERE t.`projects_project_id` = $keyMaster
		-- ORDER BY t.`order`";
$resql = DB::Query($sql);

 while ($row = $resql->fetchAssoc()) {
		$task_id = $row['task_id'];
		$task_name = addslashes($row['name']);
		$task_start_date = $row['start_date'];
		$task_end_date = $row['end_date'];
		$task_color = 'gtaskblue';
		if ($row['dynamic'] == 1){ $task_color = 'ggroupblack'; }
		$task_link = $row['related_url'];
		$task_mile = $row['milestone'];
		$task_resource = $row['creator'];
		$task_completion= $row['percent_complete'];
		$task_group = $row['dynamic'];
		$task_parent = $row['parent'];
		$task_open = '0';
		$task_depend = $row['dependent'];
		$task_caption = '';
		$task_description = $row['description'];
		$task_note = addslashes( 
						str_replace(hex2bin('0a'),'',
							str_replace(hex2bin('0d'),'',
								str_replace(PHP_EOL,'<br />', $task_description)
								)
							)
						);

		$records.="g.AddTaskItem(new JSGantt.TaskItem(";
		$records.="$task_id,'$task_name','$task_start_date','$task_end_date','$task_color','$task_link',$task_mile,'$task_resource',$task_completion,$task_group,$task_parent,$task_open,'$task_depend','$task_caption','$task_note',g));";
    }

// Read Project the Project
$sql="SELECT (999000000+p.project_id) task_id,
       concat(p.short_name,' - ',p.name) name,
      DATE_FORMAT(p.`start_date`,'%Y-%m-%d') `start_date`,
			DATE_FORMAT(ifnull(p.`actual_end_date`,p.`end_date`),'%Y-%m-%d') `end_date`,
      0  `dynamic`,
			'' `related_url`,
			0  `milestone`,
			p.`percent_complete`,
			0   parent,
			0   dependent,
			p .`description`,
			ifnull(u.login,'') creator
FROM projects p
  inner join users u on (p.owner=u.user_id)
WHERE p.project_parent = $keyMaster";
$resql = DB::Query($sql);

 while ($row = $resql->fetchAssoc()) {
		$task_id = $row['task_id'];
		$task_name = addslashes($row['name']);
		$task_start_date = $row['start_date'];
		$task_end_date = $row['end_date'];
		$task_color = 'gtaskpurple';
		if ($row['dynamic'] == 1){ $task_color = 'ggroupblack'; }
		$task_link = $row['related_url'];
		$task_mile = $row['milestone'];
		$task_resource = $row['creator'];
		$task_completion= $row['percent_complete'];
		$task_group = $row['dynamic'];
		$task_parent = $row['parent'];
		$task_open = '0';
		$task_depend = $row['dependent'];
		$task_caption = '';
		$task_description = $row['description'];
		$task_note = addslashes( 
						str_replace(hex2bin('0a'),'',
							str_replace(hex2bin('0d'),'',
								str_replace(PHP_EOL,'<br />', $task_description)
								)
							)
						);

		$records.="g.AddTaskItem(new JSGantt.TaskItem(";
		$records.="$task_id,'$task_name','$task_start_date','$task_end_date','$task_color','$task_link',$task_mile,'$task_resource',$task_completion,$task_group,$task_parent,$task_open,'$task_depend','$task_caption','$task_note',g));";
    }

$html = <<<EOT
<link rel="stylesheet" type="text/css" href="jsgantt/jsgantt.css" />
<script language="javascript" src="jsgantt/jsgantt.js"></script>

<div style="position:relative" class="gantt" id="GanttChartDIV"></div>

<script>
var g = new JSGantt.GanttChart(document.getElementById('GanttChartDIV'), 'week');
if (g.getDivId() != null) {
	g.setCaptionType('Complete');  				// Set to Show Caption (None,Caption,Resource,Duration,Complete)
	g.setQuarterColWidth(36);
	g.setShowDur(0); 							// Show/Hide Duration (0/1)
	g.setShowComp(0); 							// Show/Hide % Compl. (0/1)
	g.setDateInputFormat('yyyy-mm-dd');  		// Set format of input dates ('mm/dd/yyyy', 'dd/mm/yyyy', 'yyyy-mm-dd')
$options
	g.setShowTaskInfoLink(1); 					// Show link in tool tip (0/1)
	g.setShowEndWeekDate(0); 					// Show/Hide the date for the last day of the week in header for daily view (1/0)
	g.setUseSingleCell(10000); 					// Set the threshold at which we will only use one cell per table row (0 disables).  Helps with rendering performance for large charts.
	g.setFormatArr('Day', 'Week', 'Month', 'Quarter'); // Even with setUseSingleCell using Hour format on such a large chart can cause issues in some browsers

$language
// Parameters(pID, pName,pStart,pEnd,pStyle,pLink (unused), pMile, pRes, pComp, pGroup, pParent, pOpen, pDepend, pCaption, pNotes, pGantt)

$records
	g.Draw();	
	g.DrawDependencies();
} else {
	alert("not defined");
}

</script>
EOT;

echo $html;

?>