0) {
$offset = $offset - ($week_offset * 7);
}
$monday = date("Y-m-d", mktime(0,0,0,date('m', $startdate), date('d', $startdate)-$offset, date('Y', $startdate) ));
//print "Previous monday is $monday";
return $monday;
}
#
# Get mondays
#
function get_mondays($startdate = null) {
$mondays = Array();
for ($i = 0; $i < NUM_WEEKS; $i++) {
array_push($mondays, get_monday($startdate, $i));
}
return $mondays;
}
#
# Is this week the current week?
#
function week_class($monday) {
$class = '';
$this_monday = get_monday();
if ($monday == $this_monday) {
$class = 'currentweek';
} elseif ($monday < $this_monday) {
$class = 'past';
}
return $class;
}
#
# Get Projects
#
function get_projects($type = null) {
if ($type == 'parked') {
$query = "SELECT * FROM projects WHERE parked = 'true' ORDER BY sort_order ASC";
} elseif ($type == 'unparked') {
$query = "SELECT * FROM projects WHERE parked IS NULL OR parked != 'true' ORDER BY sort_order ASC";
} elseif ($type == 'all') {
$query = "SELECT * FROM projects ORDER BY sort_order ASC";
}
$results = Array();
if ($result = mysql_query($query)) {
while($row = mysql_fetch_array($result)) {
array_push($results, $row);
}
} else {
die("
could not get projects because:
" .mysql_error(). "
the query was $query.
");
}
return $results;
}
#
# Get One Project
#
function get_project($project_id) {
$query = "SELECT * FROM projects WHERE project_id = $project_id LIMIT 1";
$record = Array();
if ($result = mysql_query($query)) {
while($row = mysql_fetch_row($result)) {
$record['project_id'] = $row[0];
$record['project_name'] = $row[1];
$record['project_url'] = $row[4];
$record['parked'] = $row[3];
}
} else {
die("could not get project because:
" .mysql_error(). "
the query was $query.
");
}
return $record;
}
#
# Edit Project
#
function edit_project($project_id, $new_name, $new_url, $new_parked) {
$query = "UPDATE projects SET project_name = '$new_name', project_url = '$new_url', parked = '$new_parked' WHERE project_id = $project_id";
if ($result = mysql_query($query)) {
header("Location: " . HOME . "?project_id=".$project_id."&updated=true");
} else {
die("could not update item because:
" .mysql_error(). "
the query was $query.
");
}
}
#
# Add Project
#
function add_project($project_name, $project_url = '') {
$project_name = addslashes($project_name);
$query = "INSERT INTO projects (project_name, project_url) VALUES ('$project_name', '$project_url')";
if ($result = mysql_query($query)) {
header("Location: " . HOME . "");
} else {
die("could not add project because:
" .mysql_error(). "
the query was $query.
");
}
}
#
# Park Project
#
function park_project($project_id) {
$query = "UPDATE projects SET parked = 'true' WHERE project_id = $project_id";
if ($result = mysql_query($query)) {
header("Location: " . HOME . "");
} else {
die("could not park project because:
" .mysql_error(). "
the query was $query.
");
}
}
#
# Unpark project
#
function unpark_project($project_id) {
$query = "UPDATE projects SET parked = 'false' WHERE project_id = $project_id";
if ($result = mysql_query($query)) {
header("Location: " . HOME . "");
} else {
die("could not park project because:
" .mysql_error(). "
the query was $query.
");
}
}
#
# Display project name (and link)
#
function the_project_link($project) {
$link = '';
if ($project['project_url']) {
$link = '#';
}
echo $link;
}
#
# Get Entries
#
function get_entries($project_id, $startdate) {
if ($startdate == '') $startdate = get_monday();
/* get future dates */
$query = "SELECT e.entry_id, e.startdate, p.person_name, p.person_long_name, p.person_role, p.person_id, e.project_id FROM entries e, people p WHERE e.startdate = '$startdate' AND e.project_id = $project_id AND p.person_id = e.person_id ORDER BY person_name ASC";
$results = Array();
if ($result = mysql_query($query)) {
while($row = mysql_fetch_array($result)) {
array_push($results, $row);
}
} else {
die("could not delete item because:
" .mysql_error(). "
the query was $query.
");
}
return $results;
}
#
# Get One Entry
#
function get_entry($entry_id) {
$query = "SELECT * FROM entries WHERE entry_id = $entry_id LIMIT 1";
$record = Array();
if ($result = mysql_query($query)) {
while($row = mysql_fetch_row($result)) {
$record['entry_id'] = $row[0];
$record['startdate'] = $row[1];
$record['project_id'] = $row[2];
$record['person_id'] = $row[3];
}
} else {
die("could not get project because:
" .mysql_error(). "
the query was $query.
");
}
return $record;
}
#
# Get People
#
function get_people() {
$query = "SELECT * FROM people ORDER BY person_name ASC";
$results = Array();
if ($result = mysql_query($query)) {
while($row = mysql_fetch_array($result)) {
array_push($results, $row);
}
} else {
die("could not get people because:
" .mysql_error(). "
the query was $query.
");
}
return $results;
}
#
# Display people
#
function display_people($project_id, $startdate) {
$entries = get_entries($project_id, $startdate);
if (sizeOf($entries) > 0) {
foreach ($entries as $key=>$entry) {
?>
could not get project because:
" .mysql_error(). "
the query was $query.");
}
return $record;
}
#
# Edit a Person
#
function edit_person($person_id, $new_name, $new_long_name, $new_role) {
$query = "UPDATE people SET person_name = '$new_name', person_long_name = '$new_long_name', person_role = '$new_role' WHERE person_id = $person_id";
if ($result = mysql_query($query)) {
header("Location: " . HOME . "?person_id=".$person_id."&updated=true");
} else {
die("could not update item because:
" .mysql_error(). "
the query was $query.
");
}
}
#
# Add Project
#
function add_person($person_name, $person_long_name = '', $person_role = '') {
$person_name = addslashes($person_name);
$person_long_name = addslashes($person_long_name);
$query = "INSERT INTO people (person_name, person_long_name, person_role) VALUES ('$person_name', '$person_long_name', '$person_role')";
if ($result = mysql_query($query)) {
header("Location: " . HOME . "");
} else {
die("could not add person because:
" .mysql_error(). "
the query was $query.
");
}
}
#
# Create an entry
#
function create_entry($person_id, $project_id, $startdate) {
$query = "INSERT INTO entries (person_id, project_id, startdate) VALUES ($person_id, $project_id, '$startdate')";
if ($result = mysql_query($query)) {
display_people($project_id, $startdate);
log_change(CURRENT_USER, 'added', $person_id, 'to', $project_id, $startdate);
} else {
die("could not insert item because:
" .mysql_error(). "
the query was $query.
");
}
}
#
# Update Entry
#
function update_entry($entry_id, $new_project_id, $new_startdate) {
$entry = get_entry($entry_id);
$query = "UPDATE entries SET project_id = $new_project_id, startdate = '$new_startdate' WHERE entry_id = $entry_id";
if ($result = mysql_query($query)) {
display_people($new_project_id, $new_startdate);
log_change(CURRENT_USER, 'removed', $entry['person_id'], 'from', $entry['project_id'], $entry['startdate']);
log_change(CURRENT_USER, 'added', $entry['person_id'], "to", $new_project_id, $new_startdate);
} else {
die("could not update item because:
" .mysql_error(). "
the query was $query.
");
}
}
#
# Delete
#
function delete_entry($entry_id) {
$entry = get_entry($entry_id);
$query = "DELETE FROM entries WHERE entry_id = $entry_id";
if ($result = mysql_query($query)) {
echo $query;
log_change(CURRENT_USER, 'removed', $entry['person_id'], 'from', $entry['project_id'], $entry['startdate']);
} else {
die("could not delete item because:
" .mysql_error(). "
the query was $query.
");
}
}
#
# Reorder
#
function reorder_entries($raw_new_order) {
/* XXX hack */
$tmp = str_replace('&', '', $raw_new_order);
$new_order = explode ('[]=',$tmp);
unset($new_order[0]);
/* this returns something like:
[0] => first item ID
[1] => second item ID
etc.
*/
/* XXX another hack -- looping through repeated queries */
foreach ($new_order as $key => $project_id) {
$query = "UPDATE projects SET sort_order = $key WHERE project_id = $project_id";
if ($result = mysql_query($query)) {
} else {
die("could not delete item because:
" .mysql_error(). "
the query was $query.
");
}
}
}
#
# Get changes
#
function get_changes($num_changes = 20) {
$query = "SELECT * FROM changes ORDER BY change_id DESC LIMIT $num_changes";
$results = Array();
if ($result = mysql_query($query)) {
while($row = mysql_fetch_array($result)) {
array_push($results, $row);
}
} else {
die("could not get changes because:
" .mysql_error(). "
the query was $query.
");
}
$changes = array();
foreach($results as $key => $record) {
$change = array();
$change['timestamp'] = date('n/j/Y', strtotime($record['timestamp']));
$change['instigator'] = $record['instigator'];
$change['verb'] = $record['verb'];
$person = get_person($record['person_id']);
$change['person'] = $person['person_name'];
$change['preposition'] = $record['preposition'];
$project = get_project($record['project_id']);
$change['project'] = $project['project_name'];
$change['week'] = $record['week'];
array_push($changes, $change);
}
return $changes;
}
#
# Add a change to the log
#
function log_change($instigator, $verb, $person_id, $preposition, $project_id, $week) {
$instigator = addslashes($instigator);
$query = "INSERT INTO changes (timestamp, instigator, verb, person_id, preposition, project_id, week) VALUES (NOW(), '$instigator', '$verb', $person_id, '$preposition', $project_id, '$week')";
if ($result = mysql_query($query)) {
#echo "added record " . mysql_insert_id();
} else {
die("could not insert item because:
" .mysql_error(). "
the query was $query.
");
}
}
function list_latest_changes($changes) {
?>