"; if ($hastemplates) { // print the user header if ($user=="0") { echo "Global Templates"; } else { if ($user==$techid) { echo "My templates"; } else { echo "Templates for $tblusername"; } } // print the table echo "
"; echo ""; 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."$flagsym  "; } if ($flagstring) { $fmtnote=$fmtnote."
".$flagstring."     "; } else { $fmtnote=$fmtnote."
"; } // 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"; } else { $nameblock="$templatename"; } printf( "\n", $nameblock, $shiftname, $subjname, $fmtnote, $refstring ); }; echo "
Template NameShiftSubjectNoteReferences
%s %s %s %s %s



"; } else { if ($user=="0") { echo "There are no global templates"; } else { if ($user==$techid) { echo "You have no templates"; } else { echo "$tblusername has no templates"; } } echo "

"; } echo "
"; } function templateselect($db,$techid,$operation) { echo "
"; // generate template selection line echo " "; // list user templates echo "
 or  "; // list global templates echo "    "; // show load button echo " Manage templates

"; } // ******** 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,""); $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,""); $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,"Template created."); } 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,"Template modified."); } 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,"Template deleted."); } } if ($operation=="delete") { // just show the link back to the templates page echo "
Manage templates



"; } 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 "
$optext template

"; // show the template selection line templateselect($db,$techid,$operation); // start template entry section echo "
"; if ($isadmin) { echo " "; } else { $authorized=TRUE; echo " "; } echo "
Template Name:  
Tech:  
Shift:   Subject:  
Note: 

    Insert Link
"; // 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("%s", $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("%s", $flagcheckbox["flagid"],$flagcheckbox["flagcolor"],$flagcheckbox["flagname"],$flagcheckbox["flagsym"]); } } if ($flagcount<$flagsperline) { echo "   "; $flagcount++; } else { echo "
"; $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 "

References:                                  "; if ($operation=="add" && $authorized) { echo " "; } if ($operation=="edit" && $authorized) { echo "      

Deleted templates are irretrievable! "; } echo "

"; } else { echo "
Create or copy a template

"; // 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); ?>