<?php
/*
Title:                Quick Gantt
Filename:             index.php
Function:             Build a quick Gantt chart from a CSV file
Date created:         5 Apr 06
Date last modified:   6 Apr 06
Coder:                Leif Gregory
Website:              http://www.devtek.org/software/quick_gantt
E-mail:               leif@devtek.org

Copyright (C) 2006  Leif Gregory

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

$filename         = 'example.csv'; #CSV file to import
$bgcolors         = array(0=>"lightgreen",1=>"mediumslateblue"); #Alternating col colors
$bgcolors2        = array(0=>"thistle",1=>"tan"); #Alternating row colors
$projectName      = 0; #Column in CSV where project name is
$projectResource  = 1; #Column in CSV where project resource is
$projectStarts    = 6; #Column in CSV where project start date is in mm/dd/yyyy or m/d/yyyy
$projectEnds      = 7; #Column in CSV where project end date is in mm/dd/yyyy or m/d/yyyy

//Change the below to whatever month text you want to show as column headers, don't change the numbers or order
$months = array("Jan"=>31,"Feb"=>28,"Mar"=>31,"Apr"=>30,"May"=>31,"Jun"=>30,"Jul"=>31,"Aug"=>31,"Sep"=>30,"Oct"=>31,"Nov"=>30,"Dec"=>31);


////////////////////////////////////////////////////////////////////////////////
// You don't need to edit anything below this line                            //
////////////////////////////////////////////////////////////////////////////////

require_once('includes/functions.php');

$currentYear = date(Y);

$a = 0; #Initialize column background color
$b = 0; #Initialize row background color

//Open the CSV file and populate the array
$row = 0; #Don't want the column headers
$handle = fopen($filename, "r");
while ((
$data = fgetcsv($handle, 1000, ",")) !== FALSE) {
  if (
$row != 0)
  {
    
$projects[$row]['name'] = $data[$projectName];
    
$projects[$row]['resource'] = $data[$projectResource];
    
$projects[$row]['start'] = $data[$projectStarts];
    
$projects[$row]['end'] = $data[$projectEnds];
  }
  
$row++; #Get next row from CSV file
}
fclose($handle); #Cleaning up after ourselves

//$monthsNum gives us the month number
$monthsNum = array(1=>31,2=>28,3=>31,4=>30,5=>31,6=>30,7=>31,8=>31,9=>30,10=>31,11=>30,12=>31);

//Build table headers
$buildTable = '<table cellspacing="0" cellpadding="0" border="0"><tr><th style="border-bottom: 2px solid black;">Resource</th><th style="border-bottom: 2px solid black;">Project</th><th style="border-bottom: 2px solid black;">&nbsp;</th>';

//Put month names in table headers
foreach($months as $name=>$numberedDays)
{
  
$buildTable .= '<th style="background-color:' . $bgcolors[$a] . '; border-bottom: 2px solid black; color: black; text-weight: bold; font-size: 80%; padding: 2px;">' . $name . '</th>';
  
$a = !$a; #Flip column colors
}

$buildTable .= '</tr>' . "\r\n"; #End headers

//Start listing projects and calculating Gantt chart
foreach ($projects as $project)
{
  
//Who the resource is and the project name
  
$buildTable .= '<tr style="background-color: ' . $bgcolors2[$b] . ' "><td style="padding-left: 2px; padding-right: 4px;">' . $project['resource'] . '</td><td style="padding-left: 2px; padding-right: 4px;">' . $project['name'] . '</td>';
  
$b=!$b;
  
  
//Only grab valid start and end dates to build the Gantt (don't grab '?', 'TBD' etc. See the ELSE for these rows)
  
if (preg_match("/\d\d?\/\d\d?\/\d\d\d\d/",$project['start']) && preg_match("/\d\d?\/\d\d?\/\d\d\d\d/",$project['end']))
  {
    if (!
strstr($project['start'], $currentYear)) #Set the start date to beginning of current year
    
{
      
//Stick a red bar on the left to denote that it was started in previous year
      
$numDays = count_days(fixDateU2S('theDate', '1/1/2006'), fixDateU2S('theDate', $project['end']));
      
$buildTable .= '<td><img src="images/red.gif" /></td>';
    }
    else
    {
      
//We're good, this project was started in the current year
      
$numDays = count_days(fixDateU2S('theDate', $project['start']), fixDateU2S('theDate', $project['end']));
      
$buildTable .= '<td>&nbsp</td>';
    }
    
    
//Create the starting date vairables for calculations
    
list($startMonth,$startDay,$startYear) = explode("/", $project['start']);
    
    
$a = 0; #Reset the column coloring
    
    //If it started in current year skip to the right month cell, otherwise start in Jan.
    
if ($startYear == $currentYear)
    {
      for (
$j=2; $j<=$startMonth; $j++)
      {
        
$buildTable .= '<td style="background-color: ' . $bgcolors[$a] .  '">&nbsp;</td>';
        
$a=!$a; #Flip column colors
      
}
    }
    
    
$buildTable .= '<td style="background-color: ' . $bgcolors[$a] . '">'; #Finally, now it's time to build the bars

    //If the project started in current year, use the real start day, otherwise start at 1
    
if ($startYear == $currentYear)
    {
      for(
$k=1; $k<$startDay; $k++)
      {
        
$buildTable .= '<img src="images/blank.gif" />'; #Fill with blanks until we get to the actual start date
      
}
    }
    else
      
$startDay = 1;
    
    
$daysInMonthCtr = $startDay; #Needed to figure out when to cross months
    
    
if ($startYear == $currentYear) #What month are we starting with
      
$currentMonth = $startMonth;
    else
      
$currentMonth = 1;
    
    for(
$i=$startDay; $i<=$startDay+$numDays; $i++)
    {
      if (
$daysInMonthCtr == $monthsNum[$currentMonth]) #Crossing months
      
{
        
$buildTable .= '<img src="images/day.gif" />';
        
$a=!$a;
        
$buildTable .= '</td><td style="background-color: ' . $bgcolors[$a] .  '">';
        
$daysInMonthCtr = 1;
        
$currentMonth++;
      }
      else
#Filling curent month
      
{      
        
$buildTable .= '<img src="images/day.gif" />';
        
$daysInMonthCtr++;
      }
    }
    
$buildTable .= '</td>'; #End current month
    
    //If we ended before Dec, we need to background color the remaining month columns
    
if ($currentMonth < 12)
    {
      for (
$i=$currentMonth+1; $i<= 12; $i++)
      {
        
$a = !$a;
        
$buildTable .= '<td style="background-color: ' . $bgcolors[$a] .  '">&nbsp;</td>';
      }
    }
      
    
$buildTable .= '</tr>' . "\r\n"; #End row for project
  
}
  else
#Not a valid start or end date, just background color the month columns
  
{
    
$a = 0;
    
$buildTable .= '<td>&nbsp;</td>';
    for(
$l=1; $l<=12; $l++)
    {
      
$buildTable .= '<td style="background-color: ' . $bgcolors[$a] . '">&nbsp;</td>';
      
$a=!$a;
    }
    
$buildTable .= '</tr>'; #End row for project
  
}
}

$buildTable .= '</table>' . "\r\n";

//We be done! Let's show the Gantt chart
echo $buildTable;
?>