tipman/functions.inc
Josh Holtrop a6ccd8cd1f initial import
git-svn-id: svn://anubis/tipman@1 ce01c143-e732-0410-ac0e-c064f6e6c7ef
2007-06-15 00:53:46 +00:00

166 lines
3.9 KiB
PHP

<?php
function blank_image()
{
return '<img class="blank" width="1" height="1" src="images/blank.png" alt="blank" />';
}
function spacer($w = 1, $h = 1)
{
return '<img class="blank" width="' . $w . '" height="' . $h . '" src="images/blank.png" alt="blank" />';
}
function round_table($body, $center = 0)
{
$bi = blank_image();
return <<< EOE
<table class="round" cellspacing="0" cellpadding="0" border="0">
<tr>
<td class="corner"><img src="images/b_w_tl.png" height="5" width="5" alt="tl" /></td>
<td class="button_top"> $bi </td>
<td class="corner"><img src="images/b_w_tr.png" height="5" width="5" alt="tr" /></td>
</tr><tr>
<td class="button_left"> $bi </td>
<td class="button_middle"
EOE
. (($center == 0) ? '' : 'style="text-align: center;"') . <<< EOE
>
$body
</td>
<td class="button_right"> $bi </td>
</tr><tr>
<td class="corner"><img src="images/b_w_bl.png" height="5" width="5" alt="bl" /></td>
<td class="button_bottom"> $bi </td>
<td class="corner"><img src="images/b_w_br.png" height="5" width="5" alt="br" /></td>
</tr>
</table>
EOE;
}
/* return a footer for the page */
function footer()
{
return round_table('<span class="smaller"> Generated by PHP at ' . date('Y-m-d H:i:s') . '</span>', 1);
}
/* connect to the database, return a handle */
function connect_db()
{
global $MYSQL_SERVER, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DB;
$dbc = mysql_connect($MYSQL_SERVER, $MYSQL_USER, $MYSQL_PASSWORD);
if (!$dbc)
die('Could not connect to MySQL server: ' . mysql_error());
if (!mysql_select_db($MYSQL_DB))
die('Could not select database: ' . mysql_error());
return $dbc;
}
/* perform a query on the MySQL database */
function do_query($query, $dbc)
{
$qr = mysql_query($query, $dbc);
if (!$qr)
die('Invalid query: ' . mysql_error());
return $qr;
}
/* returns the user's ID */
function get_userID($user)
{
$dbc = connect_db();
$qr = do_query("SELECT * FROM `users` WHERE `name` = '$user'", $dbc);
$ra = mysql_fetch_assoc($qr);
if (!$ra)
return -1;
return $ra['id'];
mysql_close($dbc);
}
/* Returns a 31-element array of tip values for the given month */
function get_tips($month, $year, $userID)
{
$month = sprintf("%02d", $month);
$tips = array();
for ($i = 0; $i < 31; $i++)
$tips[] = 0.0;
$dbc = connect_db();
$q = do_query("SELECT * FROM `tips` WHERE `user` = '$userID' AND `date` LIKE '$year-$month%'", $dbc);
while ($ra = mysql_fetch_assoc($q))
{
$dt = substr($ra['date'], 8, 2);
$tips[$dt - 1] = $ra['amount'];
}
mysql_free_result($q);
mysql_close($dbc);
return $tips;
}
/* Returns an assoc-array of check values
* (hours1, amount1, hours2, amount2) for the given month
*/
function get_checks($month, $year, $userID)
{
$month = sprintf("%02d", $month);
if (strlen($month) < 2)
$month = '0' . $month;
$checks = array();
$dbc = connect_db();
$q = do_query("SELECT * FROM `checks` WHERE `user` = '$userID' AND `date` LIKE '$year-$month%'", $dbc);
$dim = date('t', mktime(0, 0, 0, $month, 1, $year));
while ($ra = mysql_fetch_assoc($q))
{
$dt = substr($ra['date'], 8, 2);
if ($dt == 15)
{
$checks['hours1'] = $ra['hours'];
$checks['amount1'] = $ra['amount'];
}
elseif ($dt == $dim)
{
$checks['hours2'] = $ra['hours'];
$checks['amount2'] = $ra['amount'];
}
}
mysql_free_result($q);
mysql_close($dbc);
return $checks;
}
/* Return a string for the previous month */
function get_back_month($month)
{
$mth = substr($month, 0, 2);
$yr = substr($month, 2, 4);
$mth = (($mth + 10) % 12) + 1;
if ($mth == 12)
$yr--;
if (strlen($mth) < 2)
$mth = "0$mth";
return $mth . $yr;
}
/* Return a string for the next month */
function get_next_month($month)
{
$mth = substr($month, 0, 2);
$yr = substr($month, 2, 4);
$mth = ($mth % 12) + 1;
if ($mth == 1)
$yr++;
if (strlen($mth) < 2)
$mth = "0$mth";
return $mth . $yr;
}
?>