Skip to content
This repository was archived by the owner on Jan 14, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 47 additions & 50 deletions lib/gantti.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

require('calendar.php');
require('calendar.php');

class Gantti {

Expand All @@ -16,49 +16,46 @@ class Gantti {
var $seconds = 0;

function __construct($data, $params=array()) {

$defaults = array(
'title' => false,
'cellwidth' => 40,
'cellheight' => 40,
'today' => true,
);
$this->options = array_merge($defaults, $params);

$this->options = array_merge($defaults, $params);
$this->cal = new Calendar();
$this->data = $data;
$this->seconds = 60*60*24;

$this->cellstyle = 'style="width: ' . $this->options['cellwidth'] . 'px; height: ' . $this->options['cellheight'] . 'px"';
// parse data and find first and last date
$this->parse();

// parse data and find first and last date
$this->parse();

}

function parse() {

foreach($this->data as $d) {

$this->blocks[] = array(
'label' => $d['label'],
'start' => $start = strtotime($d['start']),
'end' => $end = strtotime($d['end']),
'class' => @$d['class']
);

$d['start'] = $start = strtotime($d['start']);
$d['end'] = $end = strtotime($d['end']);

$this->blocks[] = $d;

if(!$this->first || $this->first > $start) $this->first = $start;
if(!$this->last || $this->last < $end) $this->last = $end;

}

$this->first = $this->cal->date($this->first);
$this->last = $this->cal->date($this->last);

$current = $this->first->month();
$lastDay = $this->last->month()->lastDay()->timestamp;

// build the months
// build the months
while($current->lastDay()->timestamp <= $lastDay) {
$month = $current->month();
$this->months[] = $month;
Expand All @@ -67,19 +64,19 @@ function parse() {
}
$current = $current->next();
}

}

function render() {

$html = array();
// common styles

// common styles
$cellstyle = 'style="line-height: ' . $this->options['cellheight'] . 'px; height: ' . $this->options['cellheight'] . 'px"';
$wrapstyle = 'style="width: ' . $this->options['cellwidth'] . 'px"';
$totalstyle = 'style="width: ' . (count($this->days)*$this->options['cellwidth']) . 'px"';
// start the diagram
$html[] = '<figure class="gantt">';
// start the diagram
$html[] = '<figure class="gantt">';

// set a title if available
if($this->options['title']) {
Expand All @@ -90,23 +87,23 @@ function render() {
$html[] = '<aside>';
$html[] = '<ul class="gantt-labels" style="margin-top: ' . (($this->options['cellheight']*2)+1) . 'px">';
foreach($this->blocks as $i => $block) {
$html[] = '<li class="gantt-label"><strong ' . $cellstyle . '>' . $block['label'] . '</strong></li>';
$html[] = '<li class="gantt-label"><strong ' . $cellstyle . '>' . $block['label'] . '</strong></li>';
}
$html[] = '</ul>';
$html[] = '</aside>';

// data section
$html[] = '<section class="gantt-data">';

// data header section
$html[] = '<header>';

// months headers
$html[] = '<ul class="gantt-months" ' . $totalstyle . '>';
foreach($this->months as $month) {
$html[] = '<li class="gantt-month" style="width: ' . ($this->options['cellwidth'] * $month->countDays()) . 'px"><strong ' . $cellstyle . '>' . $month->name() . '</strong></li>';
}
$html[] = '</ul>';
}
$html[] = '</ul>';

// days headers
$html[] = '<ul class="gantt-days" ' . $totalstyle . '>';
Expand All @@ -116,19 +113,19 @@ function render() {
$today = ($day->isToday()) ? ' today' : '';

$html[] = '<li class="gantt-day' . $weekend . $today . '" ' . $wrapstyle . '><span ' . $cellstyle . '>' . $day->padded() . '</span></li>';
}
$html[] = '</ul>';
}
$html[] = '</ul>';

// end header
$html[] = '</header>';

// main items
$html[] = '<ul class="gantt-items" ' . $totalstyle . '>';

foreach($this->blocks as $i => $block) {

$html[] = '<li class="gantt-item">';

// days
$html[] = '<ul class="gantt-days">';
foreach($this->days as $day) {
Expand All @@ -137,8 +134,8 @@ function render() {
$today = ($day->isToday()) ? ' today' : '';

$html[] = '<li class="gantt-day' . $weekend . $today . '" ' . $wrapstyle . '><span ' . $cellstyle . '>' . $day . '</span></li>';
}
$html[] = '</ul>';
}
$html[] = '</ul>';

// the block
$days = (($block['end'] - $block['start']) / $this->seconds);
Expand All @@ -147,37 +144,37 @@ function render() {
$left = round($offset * $this->options['cellwidth']);
$width = round($days * $this->options['cellwidth'] - 9);
$height = round($this->options['cellheight']-8);
$class = ($block['class']) ? ' ' . $block['class'] : '';
$class = (array_key_exists("class", $block)) ? ' ' . $block['class'] : '';
$html[] = '<span class="gantt-block' . $class . '" style="left: ' . $left . 'px; width: ' . $width . 'px; height: ' . $height . 'px"><strong class="gantt-block-label">' . $days . '</strong></span>';
$html[] = '</li>';

}
$html[] = '</ul>';

$html[] = '</ul>';

if($this->options['today']) {

// today
$today = $this->cal->today();
$offset = (($today->timestamp - $this->first->month()->timestamp) / $this->seconds);
$offset = (($today->timestamp - $this->first->month()->timestamp) / $this->seconds);
$left = round($offset * $this->options['cellwidth']) + round(($this->options['cellwidth'] / 2) - 1);

if($today->timestamp > $this->first->month()->firstDay()->timestamp && $today->timestamp < $this->last->month()->lastDay()->timestamp) {
$html[] = '<time style="top: ' . ($this->options['cellheight'] * 2) . 'px; left: ' . $left . 'px" datetime="' . $today->format('Y-m-d') . '">Today</time>';
}

}

// end data section
$html[] = '</section>';
$html[] = '</section>';

// end diagram
$html[] = '</figure>';

return implode('', $html);

}

function __toString() {
return $this->render();
}
Expand Down
2 changes: 1 addition & 1 deletion readme.mdown
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ $data[] = array(
$data[] = array(
'label' => 'Project 3',
'start' => '2012-05-25',
'end' => '2012-06-20'
'end' => '2012-06-20',
'class' => 'urgent',
);

Expand Down