Objects in PHP example from a Well House Consultants training course
More on Objects in PHP
[link]
This example is described in the following article(s): • Planning a hotel refurb - an example of a Gant chart in PHP - [link] • Gant charts - drawing them with a PHP script - [link] |
If you're searching for a page where you can try this code,
select here |
Source code: elligant.php Module: H108
<?php
# Define a class of task objects
# sample data line passed in to constructor:
# x114 Philips TVs 2006-09-25 14? 26" TVs
class task {
var $rawcode;
var $code;
var $supplier;
var $brief;
var $date;
var $rawduration;
var $duration;
var $lensure;
var $full;
var $ends;
var $starts;
var $bulked;
function task($instring) {
global $basket;
$parts = explode(" ",$instring);
$this->rawcode = array_shift($parts);
$this->supplier = array_shift($parts);
$this->brief = array_shift($parts);
$this->date = array_shift($parts);
$this->rawduration = array_shift($parts);
$this->full = implode(" ",$parts);
# Task id
eregi("^([[:alpha:]]*)([[:digit:]]+)",$this->rawcode,$cp);
$this->bulked = $cp[1];
$this->code = $cp[2];
# Task duration
if (eregi('^([[:digit:]]+)(\??)$',
$this->rawduration,$cp)) {
$this->lensure = 1;
if ($cp[2] == "?") $this->lensure=0;
$this->duration = $cp[1];
if ($this->duration <1) {
$this->duration = 1; }
} elseif ($this->rawduration == "OA") {
$this->lensure = -2;
$this->duration = 999;
} else {
$this->lensure = -1;
$this->duration = 0;
}
# Task end date - start and end dates supported
# also supports som etask dependencies e.g. "before_316"
if (eregi('^([[:digit:]]+)-([[:digit:]]+)-([[:digit:]]+)$',
$this->date,$cp)) {
$this->ends = mktime(12,0,0,$cp[2],$cp[3],$cp[1]);
$this->starts = $this->ends - $this->duration * 3600 * 24;
} elseif (eregi('^start_([[:digit:]]+)-([[:digit:]]+)-([[:digit:]]+)$',
$this->date,$cp)) {
$this->starts = mktime(12,0,0,$cp[2],$cp[3],$cp[1]);
$this->ends = $this->starts + $this->duration * 3600 * 24;
} elseif (eregi('^before_([[:digit:]]+)$', $this->date,$cp)) {
$this->ends = $basket[$cp[1]]->getstarts();
$this->starts = $this->ends - $this->duration * 3600 * 24;
} elseif (eregi('^after_([[:digit:]]+)$', $this->date,$cp)) {
$this->starts = $basket[$cp[1]]->getends();
$this->ends = $this->starts + $this->duration * 3600 * 24;
} else {
$this->ends = 0;
}
}
function getcode(){
return $this->code;
}
function getends(){
return $this->ends;
}
function getstarts(){
return $this->starts;
}
function getduration(){
return $this->duration;
}
function getsupplier(){
return $this->supplier;
}
function getbrief(){
return $this->brief;
}
function getlensure(){
return $this->lensure;
}
} # end of class definition
function bystartdate($one,$two) {
return ($one->getstarts() - $two->getstarts());
}
function dayform($unidate) {
return date("D d M Y",$unidate);
}
################ Main code starts here
$source = file("pps");
$basket = array();
# Read and analyse all tasks
foreach ($source as $task) {
# Strip out comments starting # in data
$task = preg_replace("/#.*/","",$task);
if (! ereg("[[:alnum:]]",$task)) continue;
$current = new task($task);
$key = $current->getcode();
# Keep an associative array of task objects
$basket[$key] = $current;
$starts = $current->getstarts();
$ends = $current->getends();
$duration = $current->getduration();
# print ("$key $starts $ends $duration\n");
}
# Put tasks into start date order
uasort($basket,bystartdate);
# Generate output display via a table of stretched images
$offbar = "<img src=off.jpg height=12";
$offmonth = "<img src=off2.jpg height=12";
$onbar = "<img src=on.jpg height=12";
$inprogress = "<img src=on2.jpg height=12";
$tgraph = "<table border=0 cellpadding=0 cellspacing=0>";
$havetitle = 0;
foreach ($basket as $task) {
$from = dayform($f = $task->getstarts());
$to = dayform($t = $task->getends());
$code = $task->getcode();
$supplier = $task->getsupplier();
$brief = $task->getbrief();
$lensure = $task->getlensure();
$texted .= "$code - $from to $to by $supplier, $brief $lensure\n<br>";
$now = time();
$barname = "";
$barline = "";
$barlen = 0;
for ($day=0; $day<180; $day++) {
$now += 3600*24;
$monlen[date("M",$now)]++;
$nvals++;
if ($now < $f or $now > $t) {
$nextbar = $offbar;
# Colour the first day of each month
if (date("d",$now) == 1) $nextbar=$offmonth;
} else {
$nextbar = $onbar;
if ($lensure == -2) $nextbar = $inprogress;
}
if ($barname != $nextbar and $barname != "") {
$barline .= "$barname width=".(2*$barlen).">";
$barname = $nextbar;
$barlen = 2;
} else {
$barname = $nextbar;
$barlen += 2;
}
}
$barline .= "$barname width=".(2*$barlen).">";
# Add month titles above first line
if ($havetitle == 0) {
$tgraph .= "<tr><td> </td><td>";
$tgraph .= "<table border=0 cellpadding=0 cellspacing=0 width=100%><tr>";
foreach (array_keys($monlen) as $month) {
$pc = sprintf("%d",$monlen[$month]*100/$nvals);
$tgraph .= "<th width=$pc%>$month</th>";
}
$tgraph .= "</tr></table></tr>";
$havetitle = 1;
}
$tgraph .= "<tr><td>$code, $supplier, $brief</td>";
$tgraph .= "<td>$barline</td></tr>";
}
$tgraph .= "</table>";
# Generate the HTML page - change the following
# for a nice look and feel
?>
<html>
<head><title>Gant chart - Well House Manor</title></head>
<body><h1>Timeline for suppliers and actions</h1>
<?= $tgraph ?><br>
Textual report:<br>
<?= $texted ?><br>
<hr>
Copyright <?= date ("l dS F Y") ?>
</html>
Learn about this subject
This module and example are covered on the following public courses:
*
Object Oriented Programming in PHP *
PHP Programming *
Learning to program in PHPAlso available on
on site courses for larger groups
Books covering this topic
Yes. We have over 700 books in our library.
Books
covering PHP are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "Objects in PHP" training module. You'll find a description of the topic and some
other closely related examples on the
"Objects in PHP" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from
our download centre under an
Open Training Notes License.
Other resources
• Our
Solutions centre provides a number of longer technical articles.
• Our
Opentalk forum archive provides a question and answer centre.
•
The Horse's mouth provides a daily tip or thought.
• Further resources are available via the
resources centre.
• All of these resources can be searched through through our
search engine
• And there's a global index
here.
Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Web site author
This web site is written and maintained by
Well House Consultants.
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job.
Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are
forbidden from using it for running your own training courses
without our prior written permission. See
our
page on courseware provision for more details.
Any of our images within this code may NOT be reused on a public URL without our
prior permission. For Bona Fide personal use, we will often grant you permission provided
that you provide a link back. Commercial use on a website will incur a license fee for
each image used - details on request.