diff --git a/Gantt.php b/Gantt.php index eed58b9..ba097a2 100644 --- a/Gantt.php +++ b/Gantt.php @@ -25,11 +25,147 @@ class Gantt { **/ private $events = array(); + /** + * HTML render of our chart + * @access private + * @var string + **/ + private $html = ''; + + /** + * List of colors to use for events + * @access private + * @var array + **/ + private $colors = array(); + + /** + * Track the number of times we've retrieved colors + * @access private + * @var integer + **/ + private $color_count = 0; + /** * Constructor **/ function __construct() { - + //add a default set of colors + $this->add_color('#FF8080'); + $this->add_color('#99CCFF'); + $this->add_color('#00FF00'); + $this->add_color('#FF9900'); + $this->add_color('#800080'); + } + + /** + * Add an event to be processed + * @param string name of the event + * @param integer the week to start this event + * @param integer duration + * @return bool whether or not the event was added successfully + **/ + public function add_event($event, $start, $duration) { + // return false if we do not have valid data + if(!is_int($start)) return false; + if(!is_int($duration)) return false; + + // add the event + $this->events[] = (object)array( + 'event' => $event, + 'start' => $start, + 'duration' => $duration, + ); + + // reset the HTML so it is re-rendered next attempt + $this->html = ''; + + return true; + } + + /** + * Create an HTML Gantt chart from the current events + * @return string html for our gantt chart + **/ + public function render_html() { + // if we have previously rendered this gantt and have not added + // new events, we don't need to do it again and can returned the + // cached html + if($this->html != '') { + return $this->html; + } + $columns = $this->find_last(); + + $this->html = '
Task | ' . "\n"; + $this->html .= 'Weeks | ' . "\n"; + for($i = 1; $i <= $columns; $i++) { + $this->html .= '' . $i . ' | ' . "\n"; + } + $this->html .= '
---|---|---|
' . $event->event . ' | ' . "\n"; + $this->html .= '' . $event->duration . ' | ' . "\n"; + for($i = 1; $i <= $columns; $i++) { + if($i >= $event->start && $i < ($event->start + $event->duration)) { + $style = ' style="background-color:' . $color . '"'; + } else { + $style = ' style="background-color:#fff"'; + } + $this->html .= '' . "\n"; + } + $this->html .= ' |