Files
OpenLog/distfiles/templates.php

566 lines
21 KiB
PHP

<?php
/*
templates.php
OpenLog Online Logbook
Copyright (C) 2026 Rod Wright
SPDX-License-Identifier: GPL-2.0
*/
include("functions.php");
if (!$_SESSION['login'] && !$_REQUEST['partview']) {
header("Location: login.php");
}
$techid = $_SESSION['login'] ?? NULL;
$isadmin = dblookup($db, "techs", "techid", "admin", $techid);
$print = $_REQUEST['print'] ?? NULL;
$efftechid = $_REQUEST['efftechid'] ?? NULL;
$shift = $_REQUEST['shift'] ?? NULL;
$subject = $_REQUEST['subject'] ?? NULL;
$note = $_REQUEST['note'] ?? NULL;
$flags = $_POST['flags'] ?? [];
$refstring = $_REQUEST['refstring'] ?? NULL;
$submit = $_REQUEST['submit'] ?? NULL;
$operation = $_REQUEST['operation'] ?? NULL;
$templateid = $_REQUEST['templateid'] ?? NULL;
$templatename = $_REQUEST['templatename'] ?? NULL;
$usertemplateid = $_REQUEST['usertemplateid'] ?? NULL;
$globaltemplateid = $_REQUEST['globaltemplateid'] ?? NULL;
$loadtemplate = $_REQUEST['loadtemplate'] ?? NULL;
$logname = LOG_NAME;
$techalpha = TECH_ALPHA;
$subjalpha = SUBJ_ALPHA;
$defaultshift = dblookup($db, "techs", "techid", "shift", $techid);
if ($print == "Printer Friendly") {
$sbcolor = P_SIDEBAR_COLOR;
$mbgcolor = P_MENUBG_COLOR;
} else {
$sbcolor = SIDEBAR_COLOR;
$mbgcolor = MENUBG_COLOR;
}
framework("begin", "$logname", "Note Templates", $print);
// make basic determination about authorization
if ($isadmin) {
$authorized = TRUE;
} else if ($efftechid == $techid) {
$authorized = TRUE;
} else {
$authorized = FALSE;
}
// var_dump($_REQUEST);
function templatetable($db, $techid, $user)
{
// Show template table
$isadmin = dblookup($db, "techs", "techid", "admin", $techid);
$templatequery = mysqli_query($db, "select * from notetemplates where tech=\"$user\" order by templatename asc");
$tblusername = dblookup($db, "techs", "techid", "techname", $user);
if (mysqli_num_rows($templatequery)) {
$hastemplates = TRUE;
} else {
$hastemplates = FALSE;
}
echo "<b><font size=\"+1\">";
if ($hastemplates) {
// print the user header
if ($user == "0") {
echo "Global Templates</font></b>";
} else {
if ($user == $techid) {
echo "My templates</font></b>";
} else {
echo "Templates for $tblusername</font></b>";
}
}
// print the table
echo "<br><table border=\"1\" width=\"100%\">";
echo "<tr><th width=\"70\">Template Name</th><th width=\"100\">Shift</th><th width=\"100\">Subject</th><th>Note</th><th width=\"70\">References</th></tr>";
while ($tablerow = mysqli_fetch_assoc($templatequery)) {
$templateid = $tablerow["templateid"];
$templatename = $tablerow["templatename"];
// convert shift number to name
$shiftname = dblookup($db, "shifts", "shiftid", "shiftname", $tablerow["shift"]);
// convert subject number to name
$subjname = dblookup($db, "subjects", "subjectid", "subjectname", $tablerow["subject"]);
// create links of urls in note
$fmtnote = convertweblinks($tablerow["lognote"]);
// generate the flags string
$flagstring = "";
$flagqry = mysqli_query($db, "select flagid from flagmap where notetemplateid=$templateid");
while ($flagid = mysqli_fetch_row($flagqry)) {
$flagparams = mysqli_fetch_assoc(mysqli_query($db, "select * from flags where flagid=\"$flagid[0]\""));
$flagcolor = $flagparams["flagcolor"];
$flagsym = $flagparams["flagsym"];
$flagname = $flagparams["flagname"];
$flagstring = $flagstring . "<font color=\"$flagcolor\" title=\"$flagname\">$flagsym</font>&nbsp;&nbsp;";
}
if ($flagstring) {
$fmtnote = $fmtnote . "<br>" . $flagstring . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
} else {
$fmtnote = $fmtnote . "<br>";
}
// generate a reference string
$refstring = NULL;
$refstringqry = mysqli_query($db, "select target from reflinks where templateid=\"$templateid\"");
while ($tgt = mysqli_fetch_row($refstringqry)) {
$refstring = $refstring . " " . $tgt[0];
}
// now print the line in the table
if (!$isadmin && $user == "0") {
$nameblock = "$templatename</a>";
} else {
$nameblock = "<a href=\"templates.php?operation=edit&templateid=$templateid\">$templatename</a>";
}
printf(
"<tr>
<td valign=\"top\">%s</td>
<td valign=\"top\"> %s</td>
<td valign=\"top\">%s</td>
<td valign=\"top\">%s</td>
<td valign=\"top\">%s</td>
</tr>\n",
$nameblock,
$shiftname,
$subjname,
$fmtnote,
$refstring
);
};
echo "</table><br><hr><br>";
} else {
if ($user == "0") {
echo "There are no global templates</font></b>";
} else {
if ($user == $techid) {
echo "You have no templates</font></b>";
} else {
echo "$tblusername has no templates</font></b>";
}
}
echo "<br><hr>";
}
echo "<br>";
}
// ******** begin database manipulation ********
if ($usertemplateid) {
$templateid = $usertemplateid;
} else if ($globaltemplateid) {
$templateid = $globaltemplateid;
} else {
$loadtemplate = NULL;
}
if ($operation == "add") {
if ($submit) {
if ($note) {
// remove cruft from note text
$note = strip_tags($note, "<a>");
$note = mysqli_real_escape_string($db, $note);
if ($authorized) {
// check if name supplied
if ($templatename) {
$namesupplied = TRUE;
// check if supplied name already exists
if (! mysqli_num_rows(mysqli_query($db, "select templateid from notetemplates where tech=\"$efftechid\" and templatename=\"$templatename\""))) {
// add the record to notetemplates
mysqli_query($db, "insert into notetemplates(templatename,shift,tech,subject,lognote) values (\"$templatename\", \"$shift\",\"$efftechid\",\"$subject\",\"$note\")");
$newtemplateid = mysqli_insert_id($db);
// update the flagmap table
foreach ($flags as $thisflag) {
// set flag for this note
mysqli_query($db, "insert into flagmap(flagid,notetemplateid) values(\"$thisflag\",\"$newtemplateid\")");
}
// add references to the reflinks table
if (strlen($refstring)) {
$refstring = trim($refstring);
$refarray = explode(" ", $refstring);
foreach ($refarray as $target) {
mysqli_query($db, "insert into reflinks(templateid,target) values($newtemplateid,$target)");
}
}
$templateid = $newtemplateid;
} else {
$nameexists = TRUE;
}
} else {
$namesupplied = FALSE;
}
}
}
}
}
if ($operation == "edit" || $operation == "delete") {
// test to see if this templateid is owned by techid to prevent hijacking
$owner = dblookup($db, "notetemplates", "templateid", "tech", $templateid);
if (!$isadmin && ($owner != $techid)) $authorized = FALSE;
if ($submit) {
if ($note) {
// remove cruft from note text
$note = strip_tags($note, "<a>");
$note = mysqli_real_escape_string($db, $note);
if ($templatename) {
$namesupplied = TRUE;
} else {
$namesupplied = FALSE;
}
if ($authorized) {
// check if name was supplied
if ($templatename) {
// update notetemplates
mysqli_query($db, "update notetemplates set templatename=\"$templatename\", shift=\"$shift\", tech=\"$efftechid\", subject=\"$subject\", lognote=\"$note\" where templateid=\"$templateid\"");
// update the flagmap table
mysqli_query($db, "delete from flagmap where notetemplateid=\"$templateid\"");
foreach ($flags as $thisflag) {
// set flag for this note
mysqli_query($db, "insert into flagmap(flagid,notetemplateid) values(\"$thisflag\",\"$templateid\")");
}
// update the reflinks table
mysqli_query($db, "delete from reflinks where templateid=\"$templateid\"");
if (strlen($refstring)) {
$refstring = trim($refstring);
$refarray = explode(" ", $refstring);
foreach ($refarray as $target) {
mysqli_query($db, "insert into reflinks(templateid,target) values($templateid,$target)");
}
}
}
}
}
}
if ($operation == "delete") {
if ($authorized) {
// update notetemplates
mysqli_query($db, "delete from notetemplates where templateid=\"$templateid\"");
// update the flagmap table
mysqli_query($db, "delete from flagmap where notetemplateid=\"$templateid\"");
// update the reflinks table
mysqli_query($db, "delete from reflinks where templateid=\"$templateid\"");
$templateid = NULL;
$note = NULL;
}
}
}
// ******** end database manipulation ********
pagetable("begin");
if (!$print) {
pageblock("left", "begin");
sidemenu();
pageblock("left", "end");
}
pageblock("right", "begin");
banner($print);
if ($submit || $operation == "delete") {
// show success/failure messages
$nameexists = $nameexists ?? NULL;
$namesupplied = $namesupplied ?? NULL;
if ($operation == "add") {
$addfail = FALSE;
if ($nameexists) {
format_message(1, "Template creation failed because the name you supplied already exists. Please try again.");
$addfail = TRUE;
}
if (!$namesupplied) {
format_message(1, "Template creation failed because you didn't supply a name. Please try again.");
$addfail = TRUE;
}
if (!$note) {
format_message(1, "Template creation failed because you didn't enter any note text. Please try again.");
$addfail = TRUE;
}
if (!$authorized) {
format_message(1, "Template creation failed because you are not authorized to do so.");
$addfail = TRUE;
}
if (!$addfail) format_message(0, "<strong>Template created.</strong>");
}
if ($operation == "edit") {
$editfail = FALSE;
if (!$namesupplied) {
format_message(1, "Template save failed because you didn't supply a name. Please try again.");
$editfail = TRUE;
}
if (!$note) {
format_message(1, "Template save failed because you didn't enter any note text. Please try again.");
$editfail = TRUE;
}
if (!$authorized) {
format_message(1, "Template save failed because you are not authorized to do so.");
$editfail = TRUE;
}
if (!$editfail) format_message(0, "<strong>Template modified.</strong>");
}
if ($operation == "delete") {
$deletefail = FALSE;
if (!$authorized) {
format_message(1, "Template delete failed because you are not authorized to do so.");
$deletefail = TRUE;
}
if (!$deletefail) format_message(0, "<strong>Template deleted.</strong>");
}
}
if ($operation == "delete") {
// just show the link back to the templates page
echo "
<br><a href=\"templates.php\">Manage templates</a><br><br><br><br>
";
} else if ($operation == "add" || $operation == "edit") {
// Display the input form
if ($loadtemplate) {
// make templateid the selected template
if ($usertemplateid) {
$templateid = $usertemplateid;
} else if ($globaltemplateid) {
$templateid = $globaltemplateid;
} else {
$templateid = NULL;
}
}
if ($operation == "edit" || $loadtemplate) {
// use values from the templateid supplied with a defined tech id
$loadeddata = mysqli_fetch_assoc(mysqli_query($db, "select * from notetemplates where templateid=\"$templateid\""));
$filltemplatename = $loadeddata["templatename"];
if ($efftechid) {
$filltech = $efftechid;
} else if ($isadmin && $loadeddata["tech"] == "0") {
$filltech = "0";
} else {
$filltech = $techid;
}
$fillshift = $loadeddata["shift"];
$fillsubject = $loadeddata["subject"];
$fillnote = $loadeddata["lognote"];
} else {
// use empty values with a defined tech id
$filltemplatename = NULL;
if ($efftechid) {
$filltech = $efftechid;
} else {
$filltech = $techid;
}
$fillshift = $defaultshift;
$fillsubject = NULL;
$fillnote = NULL;
}
if ($operation == "add") $optext = "Creating new";
if ($operation == "edit") $optext = "Editing";
echo "
<br><b>$optext template</b><br><hr>
";
// show the template selection line
templateselect($techid, $operation);
// start template entry section
echo "
<form method=\"post\" action=\"templates.php\">
<table border=\"0\" cellpadding=\"5\">
<tr><td colspan=\"2\"><b>Template Name: </b>&nbsp;
<input type=\"text\" name=\"templatename\" size=\"30\" value=\"$filltemplatename\" title=\"What you'd like to name the template.\"></td></tr>
";
if ($isadmin) {
echo "
<tr><td colspan=\"2\"><b>Tech: </b>&nbsp;
<select name=\"efftechid\">
";
if ($filltech == "0") {
echo "<option value=\"0\" selected>--Global Template--</option>";
} else {
echo "<option value=\"0\">--Global Template--</option>";
}
if ($techalpha) $torder = "techname";
else $torder = "techid";
$techrow = mysqli_query($db, "select * from techs order by $torder asc");
while ($techitem = mysqli_fetch_assoc($techrow)) {
if ($techitem["techid"] == $filltech) {
printf("<option value=\"%s\" selected>%s</option>", $techitem["techid"], $techitem["techname"]);
} else {
printf("<option value=\"%s\">%s</option>", $techitem["techid"], $techitem["techname"]);
}
}
echo "
</select></td></tr>
";
} else {
$authorized = TRUE;
echo "
<input type=\"hidden\" name=\"efftechid\" value=\"$techid\">
";
}
echo "
<tr>
<td><b>Shift: </b>&nbsp;
<select name=\"shift\">
";
$shiftrow = mysqli_query($db, "select * from shifts");
while ($shiftitem = mysqli_fetch_assoc($shiftrow)) {
if ($shiftitem["status"] == 1) {
if ($shiftitem["shiftid"] == $fillshift) {
printf("<option value=\"%s\" selected>%s</option>", $shiftitem["shiftid"], $shiftitem["shiftname"]);
} else {
printf("<option value=\"%s\">%s</option>", $shiftitem["shiftid"], $shiftitem["shiftname"]);
}
}
}
echo "
</select></td>
<td><b>Subject: </b>&nbsp;
<select name=\"subject\">
";
if ($subjalpha) $sorder = "subjectname";
else $sorder = "subjectid";
$subjrow = mysqli_query($db, "select * from subjects order by $sorder asc");
while ($subjitem = mysqli_fetch_assoc($subjrow)) {
if ($subjitem["status"] == 1) {
if ($subjitem["subjectid"] == $fillsubject) {
printf("<option value=\"%s\" selected>%s</option>", $subjitem["subjectid"], $subjitem["subjectname"]);
} else {
printf("<option value=\"%s\">%s</option>", $subjitem["subjectid"], $subjitem["subjectname"]);
}
}
}
echo "
</select></td>
</tr></table>
<b>Note:</b>&nbsp;<img src=\"icons/question.png\" title=\"Enter note text below. To insert a link to a web page, enter it's info in the link fields below the Note box and click Insert Link. You must provide the URL, but the link text is optional. \"><br>
<textarea rows=\"10\" cols=\"60\" name=\"note\" id=\"notetextarea\" wrap=\"soft\">$fillnote";
echo "</textarea><br>
<input type=\"text\" size=\"23\" id=\"linktextfield\" placeholder=\"Enter link text here\">&nbsp;
<input type=\"text\" size=\"23\" id=\"linkurlfield\" placeholder=\"Enter URL here\">&nbsp;
<a href=\"#\" onclick=\"insertUrlAtCaret('notetextarea','linktextfield','linkurlfield');return false;\">Insert Link</a><br>
";
// create an array called theseflags and optionally load it with flagids set for this template
unset($theseflags);
$theseflags = [];
if ($templateid) {
$allflags = mysqli_query($db, "select flagid from flags");
while ($thisflag = mysqli_fetch_row($allflags)) {
if (mysqli_num_rows(mysqli_query($db, "select id from flagmap where flagid=\"$thisflag[0]\" and notetemplateid=\"$templateid\""))) {
$theseflags[] = $thisflag[0];
}
}
}
$flagslist = mysqli_query($db, "select * from flags where status=1");
$flagsperline = 8;
$flagcount = 1;
// display checkboxes for all the flags, selecting the ones that are set for this template
while ($flagcheckbox = mysqli_fetch_assoc($flagslist)) {
if ($operation == "edit" && in_array($flagcheckbox["flagid"], $theseflags)) {
printf(
"<input type=\"checkbox\" name=\"flags[]\" value=\"%s\" checked><font color=\"%s\" title=\"%s\">%s</font>",
$flagcheckbox["flagid"],
$flagcheckbox["flagcolor"],
$flagcheckbox["flagname"],
$flagcheckbox["flagsym"]
);
} else {
$flagid = $flagcheckbox["flagid"];
if (mysqli_num_rows(mysqli_query($db, "select flagid from flags where flagid=\"$flagid\" and status=1"))) {
printf(
"<input type=\"checkbox\" name=\"flags[]\" value=\"%s\"><font color=\"%s\" title=\"%s\">%s</font>",
$flagcheckbox["flagid"],
$flagcheckbox["flagcolor"],
$flagcheckbox["flagname"],
$flagcheckbox["flagsym"]
);
}
}
if ($flagcount < $flagsperline) {
echo "&nbsp;&nbsp;&nbsp;";
$flagcount++;
} else {
echo "<br>";
$flagcount = 1;
}
}
if ($operation == "edit" || $loadtemplate) {
// generate a reference string
$refstring = NULL;
$refstringqry = mysqli_query($db, "select target from reflinks where templateid=\"$templateid\"");
while ($tgt = mysqli_fetch_row($refstringqry)) {
$refstring = $refstring . " " . $tgt[0];
}
}
echo "
<br><br><b>References: </b>&nbsp;
<input type=\"text\" name=\"refstring\" size=\"20\" value=\"$refstring\" title=\"One or more Note ID numbers separated by spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
";
if ($operation == "add" && $authorized) {
echo "
<input type=\"hidden\" name=\"operation\" value=\"add\">
<input type=\"submit\" name=\"submit\" value=\"Save Template\">
";
}
if ($operation == "edit" && $authorized) {
echo "
<input type=\"hidden\" name=\"operation\" value=\"edit\">
<input type=\"hidden\" name=\"templateid\" value=\"$templateid\">
<input type=\"submit\" name=\"submit\" value=\"Save Template\">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><br>
<button type=\"submit\" name=\"operation\" value=\"delete\">Delete this template</button><font color=\"#FF0000\"><b>Deleted templates are irretrievable!</b></font>
";
}
echo "
</form>
<br>
";
} else {
echo "<br><font size= \"+1\"><b><a href=\"templates.php?operation=add\">Create or copy a template</a></b></font><br><hr>";
// Show my user template table
templatetable($db, $techid, $techid);
// Show global template table
templatetable($db, $techid, "0");
if ($isadmin) {
// Show template tables for other users
$techqry = mysqli_query($db, "select techid from techs where techid != $techid and techid != 0");
while ($techtblrow = (mysqli_fetch_row($techqry))) {
templatetable($db, $techid, $techtblrow[0]);
}
}
}
banner($print);
pageblock("right", "end");
pagetable("end");
framework("end", "", "", $print);