Files
OpenLog/distfiles/functions.php

928 lines
38 KiB
PHP

<?php
/*
functions.php
OpenLog Online Logbook
Copyright (C) 2025 Rod Wright
SPDX-License-Identifier: GPL-2.0
*/
// test for correct installation
if (!file_exists("db.php")) {
echo "
<html>
<head>
<meta http-equiv=\"x-ua-compatible\" content=\"IE=edge\">
<title>Installation error</title>
</head>
<body>
Could not find the db.php file required for this installation.
</body>
</html>
";
exit();
}
include("db.php");
include("settings.php");
// build the basic color array
$basic_colors=array(
"White" => "#FFFFFF",
"Silver" => "#C0C0C0",
"Gray" => "#808080",
"Black" => "#000000",
"Red" => "#FF0000",
"Maroon" => "#800000",
"Orange" => "#FFA500",
"Yellow" => "#FFFF00",
"Olive" => "#808000",
"Lime" => "#00FF00",
"Green" => "#008000",
"Aqua" => "#00FFFF",
"Teal" => "#008080",
"Blue" => "#0000FF",
"Navy" => "#000080",
"Fuchsia" => "#FF00FF",
"Purple" => "#800080");
// Generic functions
function framework($option,$htmltitle,$pagetitle,$print) {
// generates html code for the beginning and end of all pages
if ($option=="begin") {
// generates everything down to the first horiz rule inclusive
// htmltitle is the page title that appears in the window titlebar
// pagetitle is what will appear as the title of the page itself
// print is whether to format for printing or not
if ($print) {
$bgcolor=P_BACKGROUND_COLOR;
$textcolor=P_TEXT_COLOR;
$linkcolor=P_LINK_COLOR;
$vlinkcolor=P_VLINK_COLOR;
$hdgcolor=P_HEADING_COLOR;
} else {
$bgcolor=BACKGROUND_COLOR;
$textcolor=TEXT_COLOR;
$linkcolor=LINK_COLOR;
$vlinkcolor=VLINK_COLOR;
$hdgcolor=HEADING_COLOR;
}
echo "
<html>
<head>
<title>$htmltitle</title>
<style type=\"text/css\">
* {font-family: Arial, Helvetica, sans-serif;}
table.mfentry {width: 550px; table-layout:fixed;}
td.forcewrap {word-wrap: break-word;}
</style>
<link href=\"jquery-ui/jquery-ui.css\" rel=\"stylesheet\">
<style>div.ui-datepicker{font-size:12px;}</style>
</head>
";
echo "
<body bgcolor=\"$bgcolor\" text=\"$textcolor\" link=\"$linkcolor\" vlink=\"$vlinkcolor\">
";
if ($htmltitle || $pagetitle) {
echo "
<font size=5 color=\"$hdgcolor\"><b> $htmltitle - $pagetitle</b></font><br>
";
}
} else if ($option=="end") {
// generates everything from the last horiz rule down to end of page
echo "
<hr><br><br>
";
if (defined("FOOTER_MESSAGE")) {
$footer_msg=FOOTER_MESSAGE;
echo "
<center>$footer_msg</center><br>
";
}
echo "
<script src=\"jquery-ui/external/jquery/jquery.js\"></script>
<script src=\"jquery-ui/jquery-ui.js\"></script>
<script>
$( \"#singledatepicker\" ).datepicker({
showOn: \"button\",
buttonImage: \"jquery-ui/images/calendar.gif\",
buttonImageOnly: true,
buttonText: \"Select date\",
inline: true,
showButtonPanel: true,
changeMonth: true,
changeYear: true,
dateFormat: \"yy-mm-dd\"
});
$( \"#startdatepicker\" ).datepicker({
showOn: \"button\",
buttonImage: \"jquery-ui/images/calendar.gif\",
buttonImageOnly: true,
buttonText: \"Select start date\",
inline: true,
showButtonPanel: true,
changeMonth: true,
changeYear: true,
dateFormat: \"yy-mm-dd\"
});
$( \"#enddatepicker\" ).datepicker({
showOn: \"button\",
buttonImage: \"jquery-ui/images/calendar.gif\",
buttonImageOnly: true,
buttonText: \"Select end date\",
inline: true,
showButtonPanel: true,
changeMonth: true,
changeYear: true,
dateFormat: \"yy-mm-dd\"
});
$( \"#completedatepicker\" ).datepicker({
showOn: \"button\",
buttonImage: \"jquery-ui/images/calendar.gif\",
buttonImageOnly: true,
buttonText: \"Select completed date\",
inline: true,
showButtonPanel: true,
changeMonth: true,
changeYear: true,
dateFormat: \"yy-mm-dd\"
});
$( \"#deferdatepicker\" ).datepicker({
showOn: \"button\",
buttonImage: \"jquery-ui/images/calendar.gif\",
buttonImageOnly: true,
buttonText: \"Select defer date\",
inline: true,
showButtonPanel: true,
changeMonth: true,
changeYear: true,
dateFormat: \"yy-mm-dd\"
});
var caldate = document.getElementById(\"caldate\");
var defdate = caldate.textContent;
$( \"#lognotescalendar\" ).datepicker({
inline: true,
altField: \"#targetdate\",
showButtonPanel: true,
changeMonth: true,
changeYear: true,
dateFormat: \"yy-mm-dd\",
onSelect: function(){
$(\"#calendar\").submit();
}
}).datepicker(\"setDate\", defdate);
function insertUrlAtCaret(outputAreaId, inputAreaId) {
var rawlinktext=document.getElementById(inputAreaId).value;
var url='<a href=\"' + rawlinktext.replace(/ /g,'\%20') + '\">' + rawlinktext + '</a>';
var txtarea = document.getElementById(outputAreaId);
var scrollPos = txtarea.scrollTop;
var caretPos = txtarea.selectionStart;
var front = (txtarea.value).substring(0, caretPos);
var back = (txtarea.value).substring(txtarea.selectionEnd, txtarea.value.length);
txtarea.value = front + url + back;
caretPos = caretPos + url.length;
txtarea.selectionStart = caretPos;
txtarea.selectionEnd = caretPos;
txtarea.focus();
txtarea.scrollTop = scrollPos;
}
</script>
</body>
</html>
";
} else {
echo "
<br>Invalid option \"$option\" passed to framework(). Valid options are \"begin\" and \"end\".<br>
";
exit(1);
}
}
function pagetable($option) {
if ($option=="begin") {
echo "
<br>
<table border=\"0\" width=\"100%\" cellpadding=\"10\">
<tr>
";
} else if ($option=="end") {
echo "
</tr></table>
";
} else {
echo "
<br>Invalid option \"$option\" passed to pagetable(). Valid options are \"begin\" and \"end\".<br>
";
exit(1);
}
}
function pageblock($block,$option) {
global $sbcolor;
if ($block=="left") {
if ($option=="begin") {
echo "
<td width=\"210\" bgcolor=\"$sbcolor\" valign=\"top\">
";
} else if ($option=="end") {
echo "
</td>
";
} else {
echo "
<br>Invalid option \"$option\" passed to pageblock(). Valid options are \"begin\" and \"end\".<br>
";
exit(1);
}
} else if ($block=="right") {
if ($option=="begin") {
echo "
<td valign=\"top\">
";
} else if ($option=="end") {
echo "
</td>
";
} else {
echo "
<br>Invalid option \"$option\" passed to pageblock(). Valid options are \"begin\" and \"end\".<br>
";
exit(1);
}
} else {
echo "
<br>Invalid block \"$block\" passed to pageblock(). Valid blocks are \"left\" and \"right\".<br>
";
exit(1);
}
}
function sidemenu($noprint=false) {
// Displays sidebar menu
global $db,$mbgcolor,$noprint;
$doparts=PARTS_FUNCTIONS;
$doschedmaint=SCHEDMAINT_FUNCTIONS;
echo "
<hr><br>
<table bgcolor=\"$mbgcolor\" border=\"1\" width=\"100%\">
<tr><td align=\"center\"><a href=\"lognotes.php\" title=\"Show today's notes\"><b>Logbook</b></a></td></tr>
<tr><td align=\"center\"><a href=\"logentry.php\" title=\"Add new log entry or manage templates\">Add Logbook Entry</a></td></tr>
<tr><td align=\"center\"><a href=\"search.php?target[]=log\">Search Logbook</a></td></tr>
</table>
<hr>
";
if ($doparts) {
echo "
<table bgcolor=\"$mbgcolor\" border=\"1\" width=\"100%\">
<tr><td align=\"center\"><a href=\"maintform.php\"><b>Maintenance Forms</b></a></td></tr>
<tr><td align=\"center\"><a href=\"maintformentry.php\">New Maintenance Form</a></td></tr>
<tr><td align=\"center\"><a href=\"search.php?target[]=maintform\">Search Maintenance Forms</a></td></tr>
</table>
<hr>
<table bgcolor=\"$mbgcolor\" border=\"1\" width=\"100%\">
<tr><td align=\"center\"><a href=\"parts.php\"><b>Parts</b></a></td></tr>
<tr><td align=\"center\"><a href=\"partentry.php\">Add New Part</a></td></tr>
<tr><td align=\"center\"><a href=\"search.php?target[]=part\">Search Parts</a></td></tr>
</table>
<hr>
";
}
if ($doschedmaint) {
echo "
<table bgcolor=\"$mbgcolor\" border=\"1\" width=\"100%\">
<tr><td align=\"center\"><a href=\"schedmaint.php\"><b>Scheduled Maintenance</b></a></td></tr>
</table>
<hr>
";
}
echo "
<table bgcolor=\"$mbgcolor\" border=\"1\" width=\"100%\">
<tr><td align=\"center\"><a href=\"admin.php\" title=\"Control global settings. Must be an administrator.\"><b>Administration</b></a></td></tr>
<tr><td align=\"center\"><a href=\"profile.php\">My Profile</a></td></tr>
</table>
";
if (!isset($_SESSION['login'])) {
echo "
<hr>
<table bgcolor=\"$mbgcolor\" border=\"1\" width=\"100%\">
<tr><td align=\"center\"><a href=\"login.php\"><b>Log In</b></a></td></tr>
</table>
";
} else {
$loginid=$_SESSION['login'];
$loggedinas=mysqli_fetch_row(mysqli_query($db,"select techname from techs where techid=\"$loginid\""))[0];
echo "
<hr>
<table bgcolor=\"$mbgcolor\" border=\"1\" width=\"100%\">
<tr><td align=\"center\">Logged in as $loggedinas</td></tr><tr><td align=\"center\"><a href=\"login.php?cancel=1\"><b>Log Out</b></a></td></tr>
</table>
";
}
if (!$noprint) {
echo "
<hr>
<center>
<form method=\"post\" target=\"_blank\"><input type=\"submit\" name=\"print\" value=\"Printer Friendly\"></form>
</center>
<hr>
";
}
echo "
<br>
<table bgcolor=\"$mbgcolor\" border=\"1\" width=\"100%\">
<tr><td align=\"center\"><a href=\"about.php\">About</a></td></tr>
</table>
";
}
function banner($print) {
// displays a banner
global $db;
$bannerid=BANNER;
// get banner parameters from banner table
$banrow=mysqli_fetch_assoc(mysqli_query($db,"select * from banners where bannerid=\"$bannerid\""));
if (!$banrow) {
$bannerid=1;
} else {
$bannertext=$banrow["bannername"];
if ($print) {
$bannercolor="#FFFFFF";
$bannertxtcolor="#000000";
} else {
$bannercolor=$banrow["bannercolor"];
$bannertxtcolor=$banrow["textcolor"];
}
}
if ($bannerid != 1) {
echo "
<table width=\"100%\">
<tr bgcolor=\"$bannercolor\" align=\"center\"><td colspan=2><font size=\"+1\" color=\"$bannertxtcolor\"><b>$bannertext</b></font></td></tr></table>
";
}
}
function dblookup($dbhandle,$table,$in_field,$out_field,$in_data) {
// look up a single field in a database given a value for a single field
$out_data=mysqli_fetch_row(mysqli_query($dbhandle,"select $out_field from $table where $in_field=\"$in_data\""));
return($out_data[0]);
}
function calendar($year,$month,$day,$link_url) {
// takes a numeric year, month, day, and the basic url for the day links as arguments
// Shows a calendar with clickable dates.
// Based on an embeddable calendar by Yves Glodt.
// See http://www.mind.lu/~yg/stuff/
$YearToShow = $year;
$MonthToShow = $month;
$DayToShow = $day;
$NumberOfDays = date(t,mktime(0,0,0,$MonthToShow+1,0,$YearToShow,-1));
if ($month==1) {
$mbackyear=$year-1;
$mbackmonth=12;
} else {
$mbackyear=$year;
$mbackmonth=$month-1;
}
if ($month==12) {
$mfwdyear=$year+1;
$mfwdmonth=1;
} else {
$mfwdyear=$year;
$mfwdmonth=$month+1;
}
$ybackyear=$year-1;
$ybackmonth=$month;
$yfwdyear=$year+1;
$yfwdmonth=$month;
$MonthNames = array(1=>'January','February','March','April','May','June','July','August','September','October','November','December');
echo <<<EOT
<table border="1" cellpadding="1" cellspacing="0">
<tr>
<td colspan="7" align="center">
<a href="$link_url?y=$mbackyear&m=$mbackmonth&d=$day"><img src="icons/control-180.png" border="0" title="Prev month" alt="Prev month"></a>
&nbsp;&nbsp;<font size="+1">$MonthNames[$MonthToShow]</font>&nbsp;&nbsp;
<a href="$link_url?y=$mfwdyear&m=$mfwdmonth&d=$day"><img src="icons/control.png" border="0" title="Next month" alt="Next month"></a>
<br>
<a href="$link_url?y=$ybackyear&m=$ybackmonth&d=$day"><img src="icons/control-double-180.png" border="0" title="Prev year" alt="Prev year"></a>
&nbsp;&nbsp;<font size="+1">$YearToShow</font>&nbsp;&nbsp;
<a href="$link_url?y=$yfwdyear&m=$yfwdmonth&d=$day"><img src="icons/control-double.png"border="0" title="Next year" alt="Next year"></a>
</td>
</tr>
<tr align="center">
<th>Sun</th>
<th>Mon</th>
<th>Tue</th>
<th>Wed</th>
<th>Thu</th>
<th>Fri</th>
<th>Sat</th>
</tr>
EOT;
$FirstDayOfWeek = date(l,mktime(0,0,0,$MonthToShow,1,$YearToShow));
switch ($FirstDayOfWeek) {
case 'Monday':
$offset = 1;
break;
case 'Tuesday':
$offset = 2;
break;
case 'Wednesday':
$offset = 3;
break;
case 'Thursday':
$offset = 4;
break;
case 'Friday':
$offset = 5;
break;
case 'Saturday':
$offset = 6;
break;
default:
$offset = 0;
}
if ($offset > 0) {
print "<tr align=\"center\">\n";
echo str_repeat("<td class=\"n\">&nbsp;</td>\n",$offset);
}
for ($i=1; $i <= $NumberOfDays; $i++) {
$DayOfWeek = date(l,mktime(0,0,0,$MonthToShow,$i,$YearToShow));
if($DayOfWeek == 'Sunday') {
print "<tr align=\"center\">\n";
}
if ($i != $DayToShow) {
print "<td><a href=\"$link_url?y=$YearToShow&m=$MonthToShow&d=$i\">$i</a></td>\n";
} else {
print "<td><a href=\"$link_url?y=$YearToShow&m=$MonthToShow&d=$i\"><b><font size=\"+1\" color=\"#FF0000\">$i</font></b></a></td>\n";
}
if($DayOfWeek == 'Saturday') {
print "</tr>\n";
}
}
if ( ( ($offset == 5) && ($NumberOfDays > 30) ) || ( ($offset == 6) && ($NumberOfDays > 29) ) ) {
if (42-$NumberOfDays-$offset > 0) {
echo str_repeat("<td class=\"n\">&nbsp;</td>\n",42-$NumberOfDays-$offset);
}
print "</tr>\n";
} elseif ( ($NumberOfDays != 28) || ($offset > 0) ) {
if (35-$NumberOfDays-$offset > 0) {
echo str_repeat("<td class=\"n\">&nbsp;</td>\n",35-$NumberOfDays-$offset);
print "</tr>\n";
}
}
print "</table>\n";
}
function colorpicker($baseurl) {
// takes a standard url and tacks the color number on to the end of it
// giving a palette of links
echo "
<map name=\"palette\">
<area shape=\"rect\" alt=\"#330033\" href=\"$baseurl 330033\" coords=\"171,109,186,121\">
<area shape=\"rect\" alt=\"#330099\" href=\"$baseurl 330099\" coords=\"153,109,168,121\">
<area shape=\"rect\" alt=\"#000066\" href=\"$baseurl 000066\" coords=\"135,109,150,121\">
<area shape=\"rect\" alt=\"#003333\" href=\"$baseurl 003333\" coords=\"117,109,132,121\">
<area shape=\"rect\" alt=\"#003300\" href=\"$baseurl 003300\" coords=\"99,109,114,121\">
<area shape=\"rect\" alt=\"#333300\" href=\"$baseurl 333300\" coords=\"81,109,96,121\">
<area shape=\"rect\" alt=\"#663333\" href=\"$baseurl 663333\" coords=\"63,109,78,121\">
<area shape=\"rect\" alt=\"#663300\" href=\"$baseurl 663300\" coords=\"45,109,60,121\">
<area shape=\"rect\" alt=\"#330000\" href=\"$baseurl 330000\" coords=\"27,109,43,121\">
<area shape=\"rect\" alt=\"#000000\" href=\"$baseurl 000000\" coords=\"9,109,24,121\">
<area shape=\"rect\" alt=\"#663366\" href=\"$baseurl 663366\" coords=\"171,93,186,105\">
<area shape=\"rect\" alt=\"#333399\" href=\"$baseurl 333399\" coords=\"153,93,168,105\">
<area shape=\"rect\" alt=\"#000099\" href=\"$baseurl 000099\" coords=\"135,93,150,105\">
<area shape=\"rect\" alt=\"#336666\" href=\"$baseurl 336666\" coords=\"117,93,132,105\">
<area shape=\"rect\" alt=\"#006600\" href=\"$baseurl 006600\" coords=\"99,93,114,105\">
<area shape=\"rect\" alt=\"#666600\" href=\"$baseurl 666600\" coords=\"81,93,96,105\">
<area shape=\"rect\" alt=\"#996633\" href=\"$baseurl 996633\" coords=\"63,93,78,105\">
<area shape=\"rect\" alt=\"#993300\" href=\"$baseurl 993300\" coords=\"45,93,60,105\">
<area shape=\"rect\" alt=\"#660000\" href=\"$baseurl 660000\" coords=\"27,93,42,105\">
<area shape=\"rect\" alt=\"#333333\" href=\"$baseurl 333333\" coords=\"9,93,24,105\">
<area shape=\"rect\" alt=\"#993399\" href=\"$baseurl 993399\" coords=\"171,77,186,89\">
<area shape=\"rect\" alt=\"#6600CC\" href=\"$baseurl 6600CC\" coords=\"153,77,168,89\">
<area shape=\"rect\" alt=\"#3333FF\" href=\"$baseurl 3333FF\" coords=\"135,77,150,89\">
<area shape=\"rect\" alt=\"#339999\" href=\"$baseurl 339999\" coords=\"117,77,132,89\">
<area shape=\"rect\" alt=\"#009900\" href=\"$baseurl 009900\" coords=\"99,77,114,89\">
<area shape=\"rect\" alt=\"#999900\" href=\"$baseurl 999900\" coords=\"81,77,96,89\">
<area shape=\"rect\" alt=\"#CC9933\" href=\"$baseurl CC9933\" coords=\"63,77,78,89\">
<area shape=\"rect\" alt=\"#CC6600\" href=\"$baseurl CC6600\" coords=\"45,77,60,89\">
<area shape=\"rect\" alt=\"#990000\" href=\"$baseurl 990000\" coords=\"27,77,42,89\">
<area shape=\"rect\" alt=\"#666666\" href=\"$baseurl 666666\" coords=\"9,77,24,89\">
<area shape=\"rect\" alt=\"#CC33CC\" href=\"$baseurl CC33CC\" coords=\"171,61,186,73\">
<area shape=\"rect\" alt=\"#6633FF\" href=\"$baseurl 6633FF\" coords=\"153,61,168,73\">
<area shape=\"rect\" alt=\"#3366FF\" href=\"$baseurl 3366FF\" coords=\"135,61,150,73\">
<area shape=\"rect\" alt=\"#00CCCC\" href=\"$baseurl 00CCCC\" coords=\"117,61,132,73\">
<area shape=\"rect\" alt=\"#33CC00\" href=\"$baseurl 33CC00\" coords=\"99,61,114,73\">
<area shape=\"rect\" alt=\"#FFCC00\" href=\"$baseurl FFCC00\" coords=\"81,61,96,73\">
<area shape=\"rect\" alt=\"#FFCC33\" href=\"$baseurl FFCC33\" coords=\"63,61,78,73\">
<area shape=\"rect\" alt=\"#FF6600\" href=\"$baseurl FF6600\" coords=\"45,61,60,73\">
<area shape=\"rect\" alt=\"#CC0000\" href=\"$baseurl CC0000\" coords=\"27,61,42,73\">
<area shape=\"rect\" alt=\"#999999\" href=\"$baseurl 999999\" coords=\"9,61,24,73\">
<area shape=\"rect\" alt=\"#CC66CC\" href=\"$baseurl CC66CC\" coords=\"171,45,186,57\">
<area shape=\"rect\" alt=\"#6666CC\" href=\"$baseurl 6666CC\" coords=\"153,45,168,57\">
<area shape=\"rect\" alt=\"#33CCFF\" href=\"$baseurl 33CCFF\" coords=\"135,45,150,57\">
<area shape=\"rect\" alt=\"#66CCCC\" href=\"$baseurl 66CCCC\" coords=\"117,45,132,57\">
<area shape=\"rect\" alt=\"#33FF33\" href=\"$baseurl 33FF33\" coords=\"99,45,114,57\">
<area shape=\"rect\" alt=\"#FFFF00\" href=\"$baseurl FFFF00\" coords=\"81,45,96,57\">
<area shape=\"rect\" alt=\"#FFCC66\" href=\"$baseurl FFCC66\" coords=\"63,45,78,57\">
<area shape=\"rect\" alt=\"#FF9900\" href=\"$baseurl FF9900\" coords=\"45,45,60,57\">
<area shape=\"rect\" alt=\"#FF0000\" href=\"$baseurl FF0000\" coords=\"27,45,42,57\">
<area shape=\"rect\" alt=\"#C0C0C0\" href=\"$baseurl C0C0C0\" coords=\"9,45,24,57\">
<area shape=\"rect\" alt=\"#FF99FF\" href=\"$baseurl FF99FF\" coords=\"171,29,186,41\">
<area shape=\"rect\" alt=\"#9999FF\" href=\"$baseurl 9999FF\" coords=\"153,29,168,41\">
<area shape=\"rect\" alt=\"#66FFFF\" href=\"$baseurl 66FFFF\" coords=\"135,29,150,41\">
<area shape=\"rect\" alt=\"#33FFFF\" href=\"$baseurl 33FFFF\" coords=\"117,29,132,41\">
<area shape=\"rect\" alt=\"#66FF99\" href=\"$baseurl 66FF99\" coords=\"99,29,114,41\">
<area shape=\"rect\" alt=\"#FFFF33\" href=\"$baseurl FFFF33\" coords=\"81,29,96,41\">
<area shape=\"rect\" alt=\"#FFFF66\" href=\"$baseurl FFFF66\" coords=\"63,29,78,41\">
<area shape=\"rect\" alt=\"#FF9966\" href=\"$baseurl FF9966\" coords=\"45,29,60,41\">
<area shape=\"rect\" alt=\"#FF6666\" href=\"$baseurl FF6666\" coords=\"27,29,42,41\">
<area shape=\"rect\" alt=\"#CCCCCC\" href=\"$baseurl CCCCCC\" coords=\"9,29,24,41\">
<area shape=\"rect\" alt=\"#FFCCFF\" href=\"$baseurl FFCCFF\" coords=\"171,13,186,25\">
<area shape=\"rect\" alt=\"#CCCCFF\" href=\"$baseurl CCCCFF\" coords=\"153,13,168,25\">
<area shape=\"rect\" alt=\"#CCFFFF\" href=\"$baseurl CCFFFF\" coords=\"135,13,150,25\">
<area shape=\"rect\" alt=\"#99FFFF\" href=\"$baseurl 99FFFF\" coords=\"117,13,132,25\">
<area shape=\"rect\" alt=\"#99FF99\" href=\"$baseurl 99FF99\" coords=\"99,13,114,25\">
<area shape=\"rect\" alt=\"#FFFFCC\" href=\"$baseurl FFFFCC\" coords=\"81,13,96,25\">
<area shape=\"rect\" alt=\"#FFFF99\" href=\"$baseurl FFFF99\" coords=\"63,13,78,25\">
<area shape=\"rect\" alt=\"#FFCC99\" href=\"$baseurl FFCC99\" coords=\"45,13,60,25\">
<area shape=\"rect\" alt=\"#FFCCCC\" href=\"$baseurl FFCCCC\" coords=\"27,13,42,25\">
<area shape=\"rect\" alt=\"#FFFFFF\" href=\"$baseurl FFFFFF\" coords=\"9,13,24,25\">
</map>
<img src=\"images/color-palette_1.png\" border=\"0\" usemap=\"#palette\">
";
}
// Project functions
function getsetting($setting_name,$tech_id) {
// get the value of a setting for a tech
global $db;
$confinfo=mysqli_fetch_assoc(mysqli_query($db,"select confid,confvalue from configs where confname=\"$setting_name\""));
$confid=$confinfo["confid"];
$confvalue=$confinfo["confvalue"];
$custqry=mysqli_query($db,"select customconfvalue from customs where customconfnum=\"$confid\" and customtech=\"$tech_id\"");
if (mysqli_num_rows($custqry)) {
$custvalue=mysqli_fetch_row($custqry)[0];
}
// if value in custom table, use it
if (mysqli_num_rows($custqry)) {
$retvalue=$custvalue;
$table="customs";
} else {
$retvalue=$confvalue;
$table="configs";
}
$result=array('value' => $retvalue,'table' => $table,'id' => $confid);
return $result;
}
function putsetting($setting_name,$setting_value,$tech_id) {
// set the value of a setting for a tech
global $db;
$confinfo=mysqli_fetch_assoc(mysqli_query($db,"select confid,confvalue from configs where confname=\"$setting_name\""));
$confid=$confinfo["confid"];
$confvalue=$confinfo["confvalue"];
$custqry=mysqli_query($db,"select customconfvalue from customs where customconfnum=\"$confid\" and customtech=\"$tech_id\"");
if (mysqli_num_rows($custqry)) {
if ($setting_value == $confvalue) {
mysqli_query($db,"delete from customs where customtech=\"$tech_id\" and customconfnum=\"$confid\"");
}
$custvalue=mysqli_fetch_row($custqry)[0];
if ($custvalue != $setting_value) {
mysqli_query($db,"update customs set customconfvalue=\"$setting_value\" where customconfnum=\"$confid\" and customtech=\"$tech_id\"");
}
} else {
mysqli_query($db,"insert into customs(customtech,customconfnum,customconfvalue) values(\"$tech_id\",\"$confid\",\"$setting_value\")");
}
}
function islink($il_note) {
// takes a noteid number as an argument
// determines if the argument is a link in a reference chain
// returns 1 if note is a link, 0 if not
global $db;
$il_pq=mysqli_fetch_row(mysqli_query($db,"select refs from lognotes where lognoteid=\"$il_note\""));
if ($il_pq[0]=="") {
$il_hasprev=0;
} else {
$il_hasprev=1;
}
$il_sq=mysqli_num_rows(mysqli_query($db,"select lognoteid from lognotes where refs like \"%$il_note%\""));
if ($il_sq) {
$il_hassubs=1;
} else {
$il_hassubs=0;
}
if($il_hassubs || $il_hasprev) {
return 1;
} else {
return 0;
}
}
function generate_chain($chainlink) {
// generate a reference chain
// takes a single parameter, a noteid
// finds all notes that are members of a chain containing the supplied noteid
// returns a sorted array of all notes in chain
global $db;
$refsqry=mysqli_query($db,"select lognoteid,refs from lognotes where refs!=\"\"");
$nanote=array();
$narefs=array();
while ($allrefs=mysqli_fetch_row($refsqry)) {
$spltrefs=explode(",",$allrefs[1]);
$iptr=0;
while ($iptr<count($spltrefs)) {
array_push($nanote,$allrefs[0]);
array_push($narefs,$spltrefs[$iptr]);
$iptr=$iptr+1;
}
}
$chainarray=array($chainlink);
$results=array();
$mptr=0;
while($mptr<count($chainarray)) {
$nptr=0;
while($nptr<count($nanote)) {
if(($nanote[$nptr]==$chainarray[$mptr]) && ( ! in_array($narefs[$nptr],$chainarray))) {
array_push($chainarray,$narefs[$nptr]);
}
$nptr=$nptr+1;
}
$rptr=0;
while($rptr<count($narefs)) {
if(($narefs[$rptr]==$chainarray[$mptr]) && ( ! in_array($nanote[$rptr],$chainarray))) {
array_push($chainarray,$nanote[$rptr]);
}
$rptr=$rptr+1;
}
$mptr=$mptr+1;
}
sort($chainarray);
return($chainarray);
}
function convertweblinks($rawstring) {
// takes a string, finds urls, and returns the string with urls converted to clickable links
$urlchunksize=URL_CHUNK_SIZE;
// define regex pattern for urls
$urlpatt="/<weblink\b[^>]*>(.*?)<\/weblink>/";
preg_match_all($urlpatt,$rawstring,$weblinks, PREG_SET_ORDER);
foreach ($weblinks as $urlblock) {
// split up really long url
$spliturl=str_split($urlblock[1],$urlchunksize);
unset($urlstack);
foreach ($spliturl as $urlchunk) {
$urlstack[]="<a href=\"$urlblock[1]\" target=\"_blank\">$urlchunk</a>";
}
$hyperlink=implode("<br>",$urlstack);
$rawstring=str_replace($urlblock[0],$hyperlink,$rawstring);
}
return $rawstring;
}
function showlinklist($print) {
global $db;
$linkresult=mysqli_query($db,"select linkid from links where status=\"1\"");
if (mysqli_num_rows($linkresult) && !$print) {
echo "
<b>Links</b><br>
";
$link=mysqli_query($db,"select displaytxt,urltxt from links where status=\"1\" order by linkpriority asc");
while ($linkrow = mysqli_fetch_row($link)) {
printf("<a href=\"%s\">%s</a><br>",$linkrow[1],$linkrow[0]);
}
}
}
function nag($tech_id) {
// nags the user if something is pending
global $db, $today;
// maintenance forms
$mfpending=mysqli_num_rows(mysqli_query($db,"select maintformid from maintforms where mftech=\"$tech_id\" and pending=1"));
if ($mfpending) {
echo "<font color=\"#FF0000\">NOTICE</font>: You have one or more maintenance forms to complete. <a href=\"maintform.php\">Click here to complete them</a>.<br><hr>";
}
// scheduled maintenance events
$ucevents=get_upcoming_events();
$due=false;
$overdue=false;
foreach ($ucevents as $event) {
if ($event['duedate'] == $today) $due=true;
if ($event['duedate'] < $today) $overdue=true;
}
if ($due) {
echo "<font color=\"#FF0000\">NOTICE</font>: One or more scheduled maintenance events are due today. <a href=\"schedmaint.php\">Click here to view them</a>.<br><hr>";
}
if ($overdue) {
echo "<font color=\"#FF0000\" size=\"+1\">NOTICE</font>: One or more scheduled maintenance events are <font color=\"#FF0000\">OVERDUE</font>. <a href=\"schedmaint.php\">Click here to view them</a>.<br><hr>";
}
}
function get_part_data($partinputdata) {
// given an associative array of partid, uid, partnum, sernum,
// return an associative array of
// partid, uid, partnum, sernum, partname, result
// where result=0 for success, 1 for no data found, 2 for mismatch
global $db;
// pull input data from input array
if ($partinputdata['partid'] && $partinputdata['partid'] != "") $partid= $partinputdata['partid'];
if ($partinputdata['uid'] && $partinputdata['uid'] != "") $uid= $partinputdata['uid'];
if ($partinputdata['partnum'] && $partinputdata['partnum'] != "") $partnum= $partinputdata['partnum'];
if ($partinputdata['sernum'] && $partinputdata['sernum'] != "") $sernum= $partinputdata['sernum'];
// for part data supplied, see what info is available
// if a partid is supplied, pull other info from parts
if ($partid) { // if partid supplied
$partqry=mysqli_query($db,"select * from parts where partid=\"$partid\"");
} else if ($uid && (!$partnum && !$sernum)) { // if uid only supplied
$partqry=mysqli_query($db,"select * from parts where uid like \"$uid\"");
} else if (($partnum && $sernum) && !$uid) { // if pn/sn only supplied
$partqry=mysqli_query($db,"select * from parts where partnum=\"$partnum\" and sernum=\"$sernum\"");
} else if ($uid && $partnum && $sernum ) { //if uid and pn/sn supplied
$partqry=mysqli_query($db,"select * from parts where uid like \"$uid\" and partnum=\"$partnum\" and sernum=\"$sernum\"");
$checkmm=1;
$mismatchqry=mysqli_query($db,"select * from parts where
(uid like \"$uid\" and ((partnum !=\"$partnum\" and partnum !=\"\")or (sernum !=\"$sernum\" and sernum !=\"\"))) or
((uid not like \"$uid\" and uid not like \"\") and (partnum =\"$partnum\" and sernum =\"$sernum\"))
");
} else if ($uid && $partnum && ! $sernum ) { //if uid and pn only supplied
$partqry=mysqli_query($db,"select * from parts where uid like \"$uid\" and partnum=\"$partnum\"");
$checkmm=1;
$mismatchqry=mysqli_query($db,"select partid from parts where
(uid like \"$uid\" and partnum !=\"$partnum\") or
(uid not like \"$uid\" and partnum =\"$partnum\")
");
} else if ($uid && ! $partnum && $sernum ) { //if uid and sn only supplied
$partqry=mysqli_query($db,"select * from parts where uid like \"$uid\" and sernum=\"$sernum\"");
$checkmm=1;
$mismatchqry=mysqli_query($db,"select partid from parts where
(uid like \"$uid\" and sernum !=\"$sernum\") or
(uid not like \"$uid\" and sernum =\"$sernum\")
");
} else {
// not enough input data supplied, so return result of 1
$result=1;
}
if (! $result) {
if (mysqli_num_rows($partqry)) {
$partdata=mysqli_fetch_assoc($partqry);
} else {
$result=1;
}
}
// detect mismatched input data
if ($checkmm) {
if (mysqli_num_rows($mismatchqry)) {
$result=2;
//while ($mmdata=mysqli_fetch_assoc($mismatchqry)) {
//var_dump($mmdata);
//}
}
}
if ($result) {
$partdata=array();
$partdata['result']=$result;
} else {
$partdata['result']=0;
}
return $partdata;
}
function get_upcoming_events() {
// build array of upcoming events
// takes no arguments
// returns an array containing arrays of upcoming event data
global $db, $today;
$uceventsqry=mysqli_query($db,"select * from events where active_flag=\"1\" and (deferred_flag=\"0\" or (deferred_flag=\"1\" and deferred_date <= \"$today\"))");
$ucevents=array();
while ($uceventrow=mysqli_fetch_assoc($uceventsqry)) {
// provide eventid,task,device,start_date,last_date,deferred_flag,deferred_date,active_flag
extract($uceventrow,EXTR_PREFIX_ALL,"eventdata");
// provide deviceid,devicename,devicedesc,group_flag,active_flag
$devicedata=mysqli_fetch_assoc(mysqli_query($db,"select * from devices where deviceid=\"$eventdata_device\""));
extract($devicedata,EXTR_PREFIX_ALL,"devicedata");
// provide taskid,taskname,taskdesc,frequency,period,weekdays_only,nextdue_relative,refdoc_name,refdoc_url,active_flag
$taskdata=mysqli_fetch_assoc(mysqli_query($db,"select * from tasks where taskid=\"$eventdata_task\""));
extract($taskdata,EXTR_PREFIX_ALL,"taskdata");
if ( ! ($taskdata_period == "once" && $eventdata_last_date >= $eventdata_start_date)) { // ignore completed one-time events
$start=date_create($eventdata_start_date); // create start date object
if ($eventdata_last_date=="0000-00-00") { // event has never been done. last date object will be start date object
$lastdatestrg="never";
$last=$start;
} else { // create last date object
$lastdatestrg="$eventdata_last_date";
$last=date_create($eventdata_last_date);
}
if ($taskdata_period=="once") { // nextdue object will be start date object
$nextdue=$start;
} else { // determine nextdue object from last date object (relative) or interval from start date object (absolute)
if ($lastdatestrg=="never") { // event has been scheduled to start today but has not been completed
$nextdue=$start;
} else if ($last==$start) { // event was completed for the first time on the start date
$nextdue=$start;
date_add($nextdue, date_interval_create_from_date_string("$taskdata_frequency $taskdata_period"));
} else {
if ($taskdata_nextdue_relative) { // calculate from day of last done date
$nextdue=$last;
date_add($nextdue, date_interval_create_from_date_string("$taskdata_frequency $taskdata_period"));
} else { // calculate from day of start date
$nextdue=$start;
$testdate=date_create("1970-01-01");
while ($testdate <= $last) {
date_add($nextdue, date_interval_create_from_date_string("$taskdata_frequency $taskdata_period"));
$testdate=$nextdue;
}
}
}
}
if ($taskdata_weekdays_only) { // move forward off the weekend if required
$nextduedow=date("w",date_timestamp_get($nextdue));
if ($nextduedow==0) { // due on a Sunday
date_add($nextdue, date_interval_create_from_date_string("1 days"));
}
if ($nextduedow==6) { // due on a Saturday
date_add($nextdue, date_interval_create_from_date_string("2 days"));
}
}
$duedate=date_format($nextdue, 'Y-m-d');
$daysaway=date_diff(date_create($today),$nextdue)->format('%a');
$direction=date_diff(date_create($today),$nextdue)->format('%R');
if ($direction=="+") {
if ($daysaway == 0) {
$duedatestrg="<font color=\"#FF0000\"> $duedate </font>";
} else if ($daysaway < 4) {
$duedatestrg="<font color=\"#FFFF00\"> $duedate </font>";
} else {
$duedatestrg="<font color=\"#00FF00\"> $duedate </font>";
}
} else {
$duedatestrg="<font size=\"+1\" color=\"#FF0000\"><b> $duedate </b></font><img src=\"icons/exclamation-red.png\" border=\"0\">";
}
$uceventline=array(
'eventdata_eventid'=>$eventdata_eventid,
'duedate'=>$duedate,
'duedatestrg'=>$duedatestrg,
'devicedata_devicedesc'=>$devicedata_devicedesc,
'devicedata_devicename'=>$devicedata_devicename,
'taskdata_taskdesc'=>$taskdata_taskdesc,
'taskdata_taskname'=>$taskdata_taskname,
'lastdatestrg'=>$lastdatestrg
);
$ucevents[]=$uceventline;
}
}
return $ucevents;
}
function format_message($msgtype,$msgtxt) {
// displays a formatted message
// $msgtype is 0 for success or anything else for failure
// $msgtxt is the string to display
if ($msgtype == 0) {
$state="ui-state-highlight";
$icon="ui-icon-info";
} else {
$state="ui-state-error";
$icon="ui-icon-alert";
}
if ($msgtxt == "") $msgtxt="PROGRAM ERROR: No message text was supplied.";
echo "
<div class=\"$state ui-corner-all\" style=\"margin-top: 20px; padding: 0 .7em;\">
<p><span class=\"ui-icon $icon\" style=\"float: left; margin-right: .3em;\"></span>
$msgtxt</p>
</div><br><br>
";
}
?>