Files
2026-10-19 15:22:52 -07:00

1179 lines
39 KiB
PHP

<?php
writeLog("functions.php");
//
// Useful Functions
//
//
//
//
function paginateData($action,$number_of_records,$current_page) {
global $FormTarget, $recordsPerPage, $numberOfPageLinks;
// Determine the number of pages required
$num_of_pages = intval(($number_of_records + $recordsPerPage - 1) / $recordsPerPage);
// Set start and end page to include in links
if ($num_of_pages <= $numberOfPageLinks) {
$start_page = 1;
$max_page = $num_of_pages;
}
else {
$leading_pages = intval($numberOfPageLinks / 2);
if ($current_page <= $leading_pages) $start_page = 1;
else $start_page = $current_page - $leading_pages;
$max_page = $start_page + $numberOfPageLinks;
if ($max_page > $num_of_pages) {
$start_page = $num_of_pages - $numberOfPageLinks;
$max_page = $num_of_pages;
}
}
$page_links = "<b>Total Records Found: $number_of_records</b><br>\n";
// Previous page link
if ($current_page > 1) {
$prev_page = $current_page - 1;
$page_links .= "<a href='$FormTarget?action=$action&current_page=$prev_page'>&lt;&lt;&lt;</a>&nbsp;";
$page_links .= "&nbsp;&nbsp;&nbsp;";
}
// Indicate the current page number
$page_links .= "Viewing Page: $current_page of $num_of_pages";
// Next page link
if ($current_page < $num_of_pages) {
$next_page = $current_page + 1;
$page_links .= "&nbsp;&nbsp;&nbsp;";
$page_links .= "<a href='$FormTarget?action=$action&current_page=$next_page'>&gt;&gt;&gt;</a>";
}
$page_links .= "<br>\n";
$page_links .= "<br>\n";
// Start set of links
if ($start_page > 1) {
$page_links .= "<a href='$FormTarget?action=$action&current_page=1'>1</a>&nbsp;...&nbsp;";
}
// Loop to create set of page links
for ($i = $start_page; $i <= $max_page; $i++) {
if ($i == $current_page) $page_links .= "<b>$i</b>&nbsp;";
else $page_links .= "<a href='$FormTarget?action=$action&current_page=$i'>$i</a>&nbsp;";
}
// End set of links
if ($max_page < $num_of_pages) {
$page_links .= "...<a href='$FormTarget?action=$action&current_page=$num_of_pages'>$num_of_pages</a>&nbsp;";
}
return($page_links);
}
//
// List a MySQL data set with edit and delete commands
//
function listData($results,$column_headers,$action,$commands=array(),$insert_new='Y',$column_alignment=array(),$totals_row=array(),$sequence_records='N',$additional_args="") {
$msg = getLanguageText('TEXT-CONFIRM-DELETE');
if (strlen($additional_args) == 0) {
$default = array(
"<a href='?action=$action&naction=edit&record_num=%d' title='Edit Current Entry' class='edit-record'>" .
"<img src='images/icons/edit16.png' alt='Edit Current Entry' border='0'></a>",
"<a href='?action=$action&naction=delete-confirmed&record_num=%d' onClick='return confirm(\"$msg\");' title='Delete Current Entry' rnum='%d' dflag='%s' class='delete-record'>" .
"<img src='images/icons/delete16.png' alt='Delete Current Entry' border='0'></a>");
}
else {
$default = array(
"<a href='?action=$action&naction=edit&record_num=%d%s' title='Edit Current Entry' class='edit-record'>" .
"<img src='images/icons/edit16.png' alt='Edit Current Entry' border='0'></a>",
"<a href='?action=$action&naction=delete-confirmed&record_num=%d%s' onClick='return confirm(\"$msg\");' title='Delete Current Entry' rnum='%d' dflag='%s' class='delete-record'>" .
"<img src='images/icons/delete16.png' alt='Delete Current Entry' border='0'></a>");
}
// Merge the commands array passed with the default commands
$commands_array = array_merge($default,$commands);
listDataCustom($results,$column_headers,$action,$commands_array,$insert_new,$column_alignment,$totals_row,$sequence_records,$additional_args);
}
//
// List a data set with edit and delete commands and drag and drop sorting
//
function sortData($results,$column_headers,$action,$commands=array(),$insert_new='Y',$column_alignment=array(),$totals_row=array()) {
$resultsType = 'mysql';
if (is_array($results)) $resultsType = 'array';
$msg = getLanguageText('TEXT-CONFIRM-DELETE');
$default = array(
"<a href='?action=$action&naction=edit&record_num=%d' title='Edit Current Entry' class='edit-record'>" .
"<img src='images/icons/edit16.png' alt='Edit Current Entry' border='0'></a>",
"<a href='?action=$action&naction=delete-confirmed&record_num=%d' onClick='return confirm(\"$msg\");' title='Delete Current Entry' class='delete-record'>" .
"<img src='images/icons/delete16.png' alt='Delete Current Entry' border='0'></a>");
if (array_key_exists('javaDelete', $GLOBALS) && $GLOBALS['javaDelete'] == 'Y') {
$msg = getLanguageText('TEXT-CONFIRM-DELETE');
$default = array(
"<a href='?action=$action&naction=edit&record_num=%d' title='Edit Current Entry' class='edit-record'>" .
"<img src='images/icons/edit16.png' alt='Edit Current Entry' border='0'></a>",
"<a href='?action=$action&naction=delete-confirmed&record_num=%d' onClick='return confirm(\"$msg\");' title='Delete Current Entry' class='delete-record'>" .
"<img src='images/icons/delete16.png' alt='Delete Current Entry' border='0'></a>");
}
// Merge the commands array passed with the default commands
$commands_array = array_merge($default,$commands);
// Check that there are some records in data set
if ($resultsType == 'mysql') $number_of_records = db_num_rows($results);
else $number_of_records = count($results);
if ($number_of_records <= 1) {
// Display the data
if (!isset($sequence_records)) $sequence_records = 'N';
if (!isset($additional_args)) $additional_args = '';
listDataCustom($results,$column_headers,$action,$commands_array,'N',$column_alignment,array(),'N',$totals_row,$sequence_records,$additional_args);
if (isset($insert_new) && $insert_new == 'Y') {
echo("<br>\n");
echo("<p><a href='?action=$action&naction=add' class='btn btn--primary'>" . getLanguageText('TEXT-INSERT-NEW') . "</a></p>\n");
}
}
else {
// Setup the form for the sorting
echo("<form method='post'>\n");
// Display the data
if (!isset($sequence_records)) $sequence_records = 'Y';
if (!isset($additional_args)) $additional_args = '';
listDataCustom($results,$column_headers,$action,$commands_array,'N',$column_alignment,array(),'N',$totals_row,$sequence_records,$additional_args);
// Complete the form
echo(" <br/>\n");
echo(" <input type='hidden' name='row_sequence' id='row_sequence_id' value=''>\n");
// The hidden fields for the form from the $action variable
$action_array = array();
parse_str("action=$action",$action_array);
foreach ($action_array as $k => $v) {
echo(" <input type='hidden' name='$k' value='$v'>\n");
}
?>
<table cellpadding='5' cellspacing='2' border='0'>
<tr>
<td align='center'>
<input type='hidden' name='naction' value='save-sort'>
<input type='submit' name='submit_button' class='formButton' value='<?php displayLanguageText('TEXT-SAVE-ROW-SORT') ?>'>
</td>
<?php if (isset($insert_new) && $insert_new == 'Y') { ?>
<td align='center'>
<a href='?action=<?php echo($action) ?>&naction=add' class='btn btn--primary'><?php displayLanguageText('TEXT-INSERT-NEW') ?></a>
</td>
<?php } ?>
</tr>
</table>
</form>
<?php
$numberOfColumns = count($column_headers) + 1;
include "drag-drop-sort.php";
}
}
//
// List a MySQL data set with edit and delete commands and multiple item selection boxes
//
function listDataMulti($results,$column_headers,$action,$commands=array(),$insert_new='Y',$column_alignment=array(),$totals_row=array(),$sequence_records='N',$additional_args="") {
global $default_data_width;
if (count($commands) == 0) {
$commands = array(
"<a href='?action=$action&naction=edit&record_num=%d' title='Edit Current Entry' class='edit-record'>" .
"<img src='images/icons/edit16.png' alt='Edit Current Entry' border='0'></a>",
"<a href='?action=$action&naction=delete&record_num=%d' title='Delete Current Entry' class='delete-record'>" .
"<img src='images/icons/delete16.png' alt='Delete Current Entry' border='0'></a>",
"<input type='checkbox' name='record_%d' value='Y'>");
}
else {
// Merge the commands array passed with the delete checkbox
$default = array(
"<a href='?action=$action&naction=edit&record_num=%d' title='Edit Current Entry' class='edit-record'>" .
"<img src='images/icons/edit16.png' alt='Edit Current Entry' border='0'></a>",
"<a href='?action=$action&naction=delete&record_num=%d' title='Delete Current Entry' class='delete-record'>" .
"<img src='images/icons/delete16.png' alt='Delete Current Entry' border='0'></a>");
$delete = array("<input type='checkbox' name='record_%d' value='Y'>");
$commands = array_merge($default,$commands,$delete);
}
// Determine if there are any records
$number_of_records = db_num_rows($results);
if ($number_of_records > 0) {
echo("<form method='post' action='$FormTarget'>\n");
}
listDataCustom($results,$column_headers,$action,$commands,'N',$column_alignment,$totals_row,$sequence_records,$additional_args);
if ($number_of_records > 0) {
echo("<table width='$default_data_width' cellpadding='2' cellspacing='1' border='0'>\n");
echo(" <tr>\n");
echo(" <td align='right'>\n");
echo(" <input type='hidden' name='action' value='$action'>\n");
echo(" <input type='submit' name='naction' value='" . displayLanguageText('TEXT-DELETE-SELECTED') . "'>\n");
echo(" </td>\n");
echo(" </tr>\n");
echo("</table>\n");
echo("</form>\n");
}
// Command to add a record
if (isset($insert_new) && $insert_new == 'Y') {
echo("<center>\n");
echo("<br>\n");
echo("<p><a href='?action=$action&naction=add' class='btn btn--primary'>" . getLanguageText('TEXT-INSERT-NEW') . "</a></p>\n");
echo("</center>\n");
}
}
//
// List a MySQL data set with custom commands
//
function listDataCustom($results,$column_headers,$action,$commands=array(),$insert_new='Y',$column_alignment=array(),$totals_row=array(),$sequence_records='Y',$additional_args="") {
$content = getDataCustom($results,$column_headers,$action,$commands,$insert_new,$column_alignment,$totals_row,$sequence_records,$additional_args);
echo($content);
}
function getDataCustom($results,$column_headers,$action,$commands=array(),$insert_new='Y',$column_alignment=array(),$totals_row=array(),$sequence_records='Y',$additional_args="") {
global $default_data_width, $customTableID, $displayTableHeaders, $columnFields, $columnWidths;
$localContent = "";
$resultsType = 'mysql';
if (is_array($results)) $resultsType = 'array';
$table_width = $default_data_width;
if (isset($GLOBALS['TableWidth']) && $GLOBALS['TableWidth'] > 0) $table_width = $GLOBALS['TableWidth'];
// Check for a result dataset
if (!isset($results)) {
$localContent .= "<p><b>Error: Missing Data Set</b></p>\n";
return;
}
// Check for an array of column headers
if (!isset($column_headers) || !is_array($column_headers)) {
$localContent .= "<p><b>Error: Missing Column Headers</b></p>\n";
return;
}
$localContent .= "<center>\n";
// Check that there are some records in data set
if ($resultsType == 'mysql') $number_of_records = db_num_rows($results);
else $number_of_records = count($results);
if ($number_of_records > 0) {
// Start the dataset table
if (!isset($GLOBALS['display_dataset_count']) || $GLOBALS['display_dataset_count'] != 'N')
$localContent .= "<p><b>--- " . getLanguageText('TEXT-DISPLAYING') . " $number_of_records " . getLanguageText('TEXT-ENTRIES') . " ---</b></p>\n";
// Command to add a record
if ($number_of_records > 10 && ($insert_new == 'Y' || $insert_new == 'T')) {
$localContent .= "<p><a href='?action=$action&naction=add' class='btn btn--primary'>" . getLanguageText('TEXT-INSERT-NEW') . "</a></p>\n";
}
$dataTableID = "data-table";
if (isset($customTableID) && strlen($customTableID)) $dataTableID = $customTableID;
$localContent .= "<table width='$table_width' id='$dataTableID' class='content-table'>\n";
if (!isset($displayTableHeaders) || $displayTableHeaders !== false) {
$localContent .= "<thead>\n";
// Output the column headers
$localContent .= " <tr noDrop='true' noDrag='true'>\n";
for ($i = 0; $i < count($column_headers); $i++) {
$align = 'left';
$fname = '';
if (array_key_exists($i, $column_alignment)) $align = $column_alignment[$i];
if (isset($columnFields) && is_array($columnFields) && array_key_exists($i, $columnFields)) {
$fname = $columnFields[$i];
}
$width = '';
if (isset($columnWidths) && is_array($columnWidths) && array_key_exists($i, $columnWidths)) $width = " style='width: ".$columnWidths[$i]."px;'";
$localContent .= " <td align='$align' valign='top' class='data-header' fname='$fname'$width><b>" . $column_headers[$i] . "</b></td>\n";
}
if (count($commands)) $localContent .= " <td class='data-header' fname='none'>&nbsp;</td>\n";
$localContent .= " </tr>\n";
$localContent .= "</thead>\n";
}
$localContent .= "<tbody>\n";
// Display the records in the dataset
$RowClasses = array("data-odd","data-even");
$count = 0;
// Process each entry in data set
for ($rn = 0; $rn < $number_of_records; $rn++) {
// Get the next row to process
if ($resultsType == 'mysql') $row = db_fetch_array($results);
else $row = $results[$rn];
$bgclass = $RowClasses[$count % 2];
$rnum = $count + 1;
if (isset($row['record_num'])) $rnum = $row['record_num'];
$rowid = $rnum;
if (isset($GLOBALS['useRowIDS']) && $GLOBALS['useRowIDS'] == true) $rowid = "row_$rnum";
$localContent .= " <tr id='$rowid' class='$bgclass'>\n";
$count++;
for ($i = 0; $i < count($column_headers); $i++) {
$align = 'left';
if (array_key_exists($i, $column_alignment)) $align = $column_alignment[$i];
$col_id = $rnum . "_$i";
$colIncr = 0;
$extra = '';
if (array_key_exists("colspan_$i",$row) && intval($row["colspan_$i"]) > 1) {
$extra .= "colspan='" . $row["colspan_$i"] . "' ";
$colIncr = intval($row["colspan_$i"]) - 1;
}
if (array_key_exists("class_$i",$row) && strlen($row["class_$i"])) {
$extra .= "class='" . $row["class_$i"] . "' ";
}
$data = '';
if (array_key_exists($i, $row)) {
$data = $row[$i];
if (isset($columnFields) && count($columnFields) > $i) {
if (isset($row['record_num'])) {
$record_num = $row['record_num'];
$fname = str_replace('_','-',$columnFields[$i]);
$data = "<span id='$fname-$record_num' rnum='$record_num' fname='$fname' class='$fname-span'>$data</span>";
}
}
}
$localContent .= " <td id='$col_id' align='$align' valign='top' $extra>$data</td>\n";
$i += $colIncr;
}
if (count($commands) || ($sequence_records == 'Y' && isset($row['sequence']) && $row['sequence'] > 0)) {
$width = count($commands) * 20;
$col_id = $rnum . ".$i";
if (!array_key_exists('display_commands',$row) || $row['display_commands'] != 'N') {
$localContent .= " <td id='$col_id' width='$width' valign='top'>\n";
$localContent .= " <table cellpadding='0' cellspacing='2' border='0'>\n";
$localContent .= " <tr class='$bgclass'>\n";
for ($i = 0; $i < count($commands); $i++) {
$command = $commands[$i];
if (strlen($command)) {
if (strpos($command,"command-button") == FALSE && strpos($command,"<img") == FALSE) {
$command = preg_replace("#(<a [^>]*)>(.*)</a>#","\\1 class='smallButton'>\\2</a>",$command);
}
$numRNUM = substr_count($command,"%d");
$localContent .= " <td valign='top'><nobr>";
if ($numRNUM == 1) {
if (array_key_exists('delete_flag',$row) && strlen($row['delete_flag']) > 0 && strpos($command, "dflag")) {
$command_string = sprintf($command,$row['record_num'],$row['delete_flag']);
}
else {
$command_string = sprintf($command,$row['record_num'],$additional_args);
}
}
if ($numRNUM == 2) {
if (array_key_exists('delete_flag',$row) && strlen($row['delete_flag']) > 0 && strpos($command, "dflag")) {
$command_string = sprintf($command,$row['record_num'],$row['record_num'],$row['delete_flag']);
}
else {
// $command_string = sprintf($command,$row['record_num'],$row['record_num'],$row['delete_flag'],$additional_args);
$command_string = sprintf($command,$row['record_num'],$row['record_num'],$additional_args);
}
}
if (array_key_exists('delete_flag',$row) && $row['delete_flag'] == 'N' && strpos($command_string, "{DISABLED-FLAG}") !== false) {
$command_string = str_replace("{DISABLED-FLAG}", "disabled", $command_string);
}
if (strpos($command_string, 'asnum') === false && array_key_exists('school_num', $row)) {
$asnum = $row['school_num'];
$command_string = str_replace("title=", "asnum='$asnum' title=", $command_string);
}
$command_string = str_replace("24.png", "16.png", $command_string);
$localContent .= "$command_string</nobr></td>\n";
}
}
// if ($sequence_records == 'Y' && isset($row['sequence']) && $row['sequence'] > 0) {
// $localContent .= " <td>&nbsp;&nbsp;<nobr>";
// if ($count < $number_of_records) {
// $localContent .= "&nbsp;&nbsp;<a href='?action=$action&naction=down&record_num=" .
// $row['record_num'] . "'><img src='admin/tridown.gif' border='0'></a>";
// }
//
// if ($number_of_records > 1 && $count == $number_of_records) {
// $localContent .= "&nbsp;&nbsp;<img src='admin/spacer.gif' height='1' width='10'>";
// }
//
// if ($count > 1) {
// $localContent .= "&nbsp;&nbsp;<a href='?action=$action&naction=up&record_num=" .
// $row['record_num'] . "'><img src='admin/triup.gif' border='0'></a>";
// }
// else {
// $localContent .= "&nbsp;&nbsp;<img src='admin/spacer.gif' height='1' width='10'>";
// }
// $localContent .= "</nobr></td>\n";
// }
$localContent .= " </tr>\n";
$localContent .= " </table>\n";
$localContent .= " </td>\n";
}
}
$localContent .= " </tr>\n";
}
// Display totals row if there is one provided
if (isset($totals_row) && is_array($totals_row) && count($totals_row) > 0) {
// Output the column headers
$localContent .= " <tr noDrop='true' noDrag='true'>\n";
for ($i = 0; $i < count($column_headers); $i++) {
$align = 'left';
if (array_key_exists($i, $column_alignment)) $align = $column_alignment[$i];
$localContent .= " <td align='$align' valign='top' class='data-header'><b>" . $totals_row[$i] . "</b></td>\n";
}
if (count($commands)) $localContent .= " <td class='data-header'>&nbsp;</td>\n";
$localContent .= " </tr>\n";
}
// Complete the table
$localContent .= "</tbody>\n";
$localContent .= "</table>\n";
}
else {
$localContent .= "<p><b>" . getLanguageText('ERRORMSG-NO-RECORDS') . "</b></p>\n";
}
// Command to add a record
if (isset($insert_new) && $insert_new == 'Y') {
$localContent .= "<br>\n";
$localContent .= "<p><a href='?action=$action&naction=add' class='btn btn--primary'>" . getLanguageText('TEXT-INSERT-NEW') . "</a></p>\n";
}
$localContent .= "</center>\n";
return($localContent);
}
//
// List a MySQL data set with thumbnails and custom commands
//
function listImageData($results,$column_headers,$photo_path="",$commands=array()) {
global $default_data_width;
// Check for a mysql result dataset
if (!isset($results)) {
echo("<p><b>Error: Missing Data Set</b></p>\n");
return;
}
// Check for an array of column headers
if (!isset($column_headers) || !is_array($column_headers)) {
echo("<p><b>Error: Missing Column Headers</b></p>\n");
return;
}
echo("<center>\n");
// Check that there are some records in data set
$number_of_records = db_num_rows($results);
if ($number_of_records > 0) {
// Start the dataset table
echo("<p><b>--- " . getLanguageText('TEXT-DISPLAYING') . " $number_of_records " . getLanguageText('TEXT-ENTRIES') . " ---</b></p>\n");
echo("<table width='$default_data_width' cellpadding='2' cellspacing='1' border='0'>\n");
// Output the column headers
echo(" <tr>\n");
echo(" <td align='center' valign='top' class='data-header'><b>" . $column_headers[0] . "</b></td>\n");
for ($i = 1; $i < count($column_headers); $i++) {
echo(" <td valign='top' class='data-header'><b>" . $column_headers[$i] . "</b></td>\n");
}
if (count($commands)) echo(" <td class='data-header'>&nbsp;</td>\n");
echo(" </tr>\n");
// Display the records in the dataset
$RowClasses = array("data-odd","data-even");
$count = 0;
while ($row = db_fetch_array($results)) {
echo(" <tr>\n");
$bgclass = $RowClasses[$count % 2];
$count++;
// Treat the first parameter as the image name
$thumb = $row[0];
if (is_file("$photo_path/$thumb")) {
$image = "<img src='$photo_path/$thumb' border=1 vspace=1>";
$large = "";
if (isset($row['photo'])) {
$large = $row['photo'];
}
if (isset($row['large'])) {
$large = $row['large'];
}
if (strlen($large) > 0) {
$image = "<a href='$photo_path/$large' target='_blank'>$image</a>";
}
echo(" <td valign='top' align='center' class='$bgclass'>$image</td>\n");
}
else {
echo(" <td class='$bgclass'>Image Not Found</td>\n");
}
for ($i = 1; $i < count($column_headers); $i++) {
echo(" <td valign='top' class='$bgclass'>" . $row[$i] . "</td>\n");
}
if (count($commands) || (isset($row['sequence']) && $row['sequence'] > 0)) {
$width = count($commands) * 30;
echo(" <td width='$width' valign='top' class='$bgclass'>");
echo(" <table cellpadding='0' cellspacing='2' border='0'>\n");
echo(" <tr>\n");
for ($i = 0; $i < count($commands); $i++) {
$command = $commands[$i];
if (strlen($command)) {
echo(" <td class='smallButton'>&nbsp;");
printf($command,$row['record_num']);
echo("&nbsp;</td>\n");
}
}
// if (isset($row['sequence']) && $row['sequence'] > 0) {
// echo(" <td>&nbsp;&nbsp;<nobr>");
// if ($count < $number_of_records) {
// echo("&nbsp;&nbsp;<a href='?action=$action&naction=down&record_num=" .
// $row['record_num'] . "'><img src='admin/tridown.gif' border='0'></a>");
// }
//
// if ($number_of_records > 2 && $count == $number_of_records) {
// echo("&nbsp;&nbsp;<img src='admin/spacer.gif' height='1' width='10'>");
// }
//
// if ($count > 1) {
// echo("&nbsp;&nbsp;<a href='?action=$action&naction=up&record_num=" .
// $row['record_num'] . "'><img src='admin/triup.gif' border='0'></a>");
// }
// echo("</nobr></td>\n");
// }
echo(" </tr>\n");
echo(" </table>\n");
echo(" </td>\n");
}
echo(" </tr>\n");
}
// Complete the table
echo("</table>\n");
}
else {
echo("<p><b>" . getLanguageText('ERRORMSG-NO-RECORDS') . "</b></p>\n");
}
echo("</center>\n");
}
//
// Produce an XLS file from a data set
//
function xlsData($data,$column_headers=array(),$filename='data.xls',$titles=array()) {
// Initialize the spreadsheet document type
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private',false);
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=$filename");
header('Content-Transfer-Encoding: text');
echo "\xEF\xBB\xBF";
// If there are titles
if (count($titles)) {
for ($i = 0; $i < count($titles); $i++) {
$title = $titles[$i];
for ($j = 0; $j < count($title); $j++) {
$text = $title[$j];
echo("\"$text\",");
}
echo("\n");
}
}
// If there are column headers
if (count($column_headers)) {
for ($i = 0; $i < count($column_headers); $i++) {
if ($i > 0) echo(",");
$header = $column_headers[$i];
echo("\"$header\"");
}
echo("\n");
}
// Determine the data type of the data set
$resultsType = 'mysql';
if (is_array($data)) $resultsType = 'array';
// Check that there are some records in data set
if ($resultsType == 'mysql') $number_of_records = db_num_rows($data);
else $number_of_records = count($data);
// Process each record of the data set
if ($number_of_records > 0) {
for ($rn = 0; $rn < $number_of_records; $rn++) {
// Get the next row to process
if ($resultsType == 'mysql') $row = db_fetch_array($data);
else $row = $data[$rn];
if (is_array($row)) {
for ($i = 0; $i < count($row); $i++) {
if ($i > 0) echo(",");
if (array_key_exists($i,$row)) {
$field = $row[$i];
if (is_numeric($field)) echo($field);
else {
$field = str_replace('"',"\"\"",$field);
echo("\"$field\"");
}
}
}
}
else echo($row);
echo("\n");
}
}
}
//
// List an array of data records with edit and delete commands
//
function listArray($data,$column_headers,$action) {
global $default_data_width;
// Check for an array of data
if (!isset($data) || !is_array($data)) {
echo("<p><b>Error: Missing or Invalid Data Array</b></p>\n");
return;
}
// Check for an array of column headers
if (!isset($column_headers) || !is_array($column_headers)) {
echo("<p><b>Error: Missing Column Headers</b></p>\n");
return;
}
echo("<center>\n");
if (count($data) > 0) {
// Start the dataset table
echo("<table width='$default_data_width' cellpadding='2' cellspacing='1' border='0'>\n");
// Output the column headers
echo(" <tr>\n");
for ($i = 0; $i < count($column_headers); $i++) {
echo(" <td valign='top' class='data-header'><b>" . $column_headers[$i] . "</b></td>\n");
}
echo(" <td valign='top' class='data-header'>&nbsp;</td>\n");
echo(" </tr>\n");
// Display the records in the dataset
$RowClasses = array("data-odd","data-even");
$count = 0;
for ($n = 0; $n < count($data); $n++) {
$row = $data[$n];
echo(" <tr>\n");
$bgclass = $RowClasses[$count % 2];
$count++;
for ($i = 0; $i < count($column_headers); $i++) {
echo(" <td valign='top' class='$bgclass'>" . $row[$i] . "</td>\n");
}
echo(" <td align='center' valign='top' class='$bgclass'>");
echo("[<a href='?action=$action&naction=edit&record_num=" . $row['record_num'] . "'>Edit</a>]&nbsp;");
echo("[<a href='?action=$action&naction=delete&record_num=" . $row['record_num'] . "'>Delete</a>]");
echo("</td>\n");
echo(" </tr>\n");
}
// Complete the table
echo("</table>\n");
}
else {
echo("<p><b>" . getLanguageText('ERRORMSG-NO-RECORDS') . "</b></p>\n");
}
// Command to add a record
echo("<br>\n");
echo("<p>[<a href='?action=$action&naction=add'>" . getLanguageText('TEXT-INSERT-NEW') . "</a>]</p>\n");
echo("</center>\n");
}
//
// List an array of report titles
//
function reportTitles($titles,$alignment=array(),$color="") {
if (isset($color) && strlen($color) > 0) $bgcolor = " bgcolor='$color'";
echo(" <tr>\n");
for ($i = 0; $i < count($titles); $i++) {
if (isset($alignment[$i])) $align = " align='" . $alignment[$i] . "'";
echo(" <td$bgcolor$align><b>" . $titles[$i] . "</b></td>\n");
}
echo(" </tr>\n");
}
//
// List a report line of data from an array
//
function reportLine($data,$alignment=array(),$color="") {
if (isset($color) && strlen($color) > 0) $bgcolor = " bgcolor='$color'";
echo(" <tr>\n");
$keys = array_keys($data);
for ($i = 0; $i < count($keys); $i++) {
if (isset($alignment[$i])) $align = " align='" . $alignment[$i] . "'";
echo(" <td$bgcolor$align>" . $data[$keys[$i]] . "</td>\n");
}
echo(" </tr>\n");
}
//
// Extract an array into the GLOBAL space
//
function array_extract($data,$mode="") {
$keys = array_keys($data);
for ($i = 0; $i < count($keys); $i++) {
switch ($mode) {
case 'SPECIAL':
$GLOBALS[$keys[$i]] = htmlspecialchars($data[$keys[$i]],ENT_QUOTES);
break;
case 'STRIP':
default:
$GLOBALS[$keys[$i]] = stripslashes($data[$keys[$i]]);
}
}
}
//
// Function to parse the array parameter and return a string
//
function parseArray($var,$var_name='') {
// Return if parameter is not an array
if (!is_array($var)) return('');
// Return if the array has no elements
if (count($var) == 0) return('');
// Create the result string
$result = "";
if (strlen($var_name) > 0) $result .= "ARRAY NAME: $" . $var_name . "\n\n";
$keys = array_keys($var);
foreach($keys as $name) {
$value = $var[$name];
if (!is_array($value)) $result .= "$name => $value\n";
else parseArray($value, $name);
$result .= "+++++++++\n";
}
return($result);
}
//
// Function to strip excess characters from currency value
//
function trimNumber($num) {
$patterns = array('/\$/','/'.chr(163).'/','/,/');
return(preg_replace($patterns,"",$num));
}
//
// Function to URL encode an array
//
function encodeArray($args) {
if (is_array($args)) {
$argString = "";
foreach ($args as $key => $value) {
if (strlen($argString) > 0) $argString .= "&";
$argString .= "$key=";
if (is_string($value) && strlen($value)) $argString .= urlencode($value);
}
}
return($argString);
}
//
// Format numbers to currency
//
function formatCurrency($amount) {
global $currencySymbol;
$sign = '';
if ($amount < 0) $sign = '-';
$text = $sign . $currencySymbol . number_format(abs($amount),2);
return($text);
}
//
// Format URL's in text
//
function formatUrlsInText($text){
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
preg_match_all($reg_exUrl, $text, $matches);
$usedPatterns = array();
foreach($matches[0] as $pattern){
if(!array_key_exists($pattern, $usedPatterns)){
$usedPatterns[$pattern]=true;
$text = str_replace($pattern, "<a href=\"{$pattern}\" rel=\"nofollow\">{$pattern}</a> ", $text);
}
}
return $text;
}
//
// str_match()
//
// Map PHP strstr to a more consistent format
//
function str_match($pattern, $string) {
return(strstr($string,$pattern));
}
//
// stri_match()
//
// Map PHP strstr to a more consistent format
//
function stri_match($pattern, $string) {
return(stristr($string,$pattern));
}
//
// encrypt_password()
//
function encrypt_password($password = "") {
global $userSALT;
if (strlen($password) == 0) return -1;
$password_crypt = crypt($password,$userSALT);
return($password_crypt);
}
// Check if parent account has an email address
function emailCheck() {
global $school_num, $action;
if (array_key_exists('action', $_GET)) $action = $_GET['action'];
if ($action == "account") return;
if ($_SESSION['UserInfo']['type'] != 'Parent') return;
if (strlen($_SESSION['UserInfo']['email'])) return;
$unum = $_SESSION['UserInfo']['record_num'];
$email = db_getVal("SELECT email FROM users WHERE school_num = $school_num AND record_num = $unum");
if (strlen($email)) return;
return(-1);
}
//
// Resize image if required
//
function resizeImage($image, $image_path, $maxImageDimension = 900, $maxThumbDimension = 200) {
global $school_record;
$image_path = rtrim($image_path, '/');
// startLog("resizeImage");
// writeLog("image: $image");
// writeLog("image_path: $image_path");
// writeLog("$image_path/$image");
$mimetype = mime_content_type("$image_path/$image");
if (strpos($mimetype, "image") !== 0) {
// writeLog("Not an Image");
return;
}
// Setup image conversion if required
$cmd="/usr/bin/convert";
$tempImage = "$image_path/temp";
// Set thumbnail name
$segments = explode('.',$image);
$lastSegment = count($segments) - 1;
$fname = "";
$ext = $segments[$lastSegment];
for ($i = 0; $i < $lastSegment; $i++) {
if (strlen($fname)) $fname .= '.';
$fname .= $segments[$i];
}
$thumbnail = $fname."-tn.".$ext;
// writeLog("thumbnail: $thumbnail");
if (is_file($tempImage)) {
// writeLog("temp image exists");
// writeLog("unlink $tempImage");
unlink($tempImage);
}
// Get image info
$image_size = GetImageSize("$image_path/$image");
$image_width = $image_size[0];
$image_height = $image_size[1];
// writeLog("image_width: $image_width");
// writeLog("image_height: $image_height");
if (is_file("$image_path/$thumbnail")) {
// writeLog("unlink $image_path/$thumbnail");
unlink("$image_path/$thumbnail");
}
// Check which is largest dimension
if ($image_width >= $image_height) {
if ($image_width > $maxImageDimension) {
$height = intval($image_height * $maxImageDimension / $image_width);
$command = "$cmd -size {$image_width}x$image_height $image_path/$image -resize {$maxImageDimension}x$height +profile \"*\" $tempImage";
exec($command);
if (is_file($tempImage)) {
unlink("$image_path/$image");
rename($tempImage,"$image_path/$image");
}
}
}
else {
if ($image_height > $maxImageDimension) {
$width = intval($image_width * $maxImageDimension / $image_height);
$command = "$cmd -size {$image_width}x{$image_height} $image_path/$image -resize {$width}x{$maxImageDimension} +profile \"*\" $tempImage";
exec($command);
if (is_file($tempImage)) {
unlink("$image_path/$image");
rename($tempImage,"$image_path/$image");
}
}
}
// Create the thumbnail
$width = intval($image_width * $maxThumbDimension / $image_height);
$width = $maxThumbDimension;
$height = intval($image_height * $maxThumbDimension / $image_width);
$command = "$cmd -size {$image_width}x{$image_height} $image_path/$image -resize {$width}x{$maxThumbDimension} +profile \"*\" $tempImage";
exec($command);
if (is_file($tempImage)) {
$res = rename($tempImage,"$image_path/$thumbnail");
if (!$res) copy("$image_path/$image","$image_path/$thumbnail");
return($thumbnail);
}
return('');
}
//
// Create a thumbnail image "-tn"
//
function createThumbnail($image, $imagedir) {
global $school_record, $maxImageDimension, $maxThumbDimension;
// startLog("createThumbnail");
// Setup image conversion if required
$cmd="/usr/bin/convert";
$target = "$imagedir/temp";
if (is_file($target)) {
// writeLog("target exists");
unlink("$imagedir/$image");
}
// Get image info
$image_size = GetImageSize("$imagedir/$image");
$image_width = $image_size[0];
$image_height = $image_size[1];
// writeLog("image_width: $image_width");
// writeLog("image_height: $image_height");
// Determine name of thumbnail image
$segments = explode('.',$image);
$lastSegment = count($segments) - 1;
$fname = "";
$ext = $segments[$lastSegment];
for ($i = 0; $i < $lastSegment; $i++) {
if (strlen($fname)) $fname .= '.';
$fname .= $segments[$i];
}
$thumbnail = $fname."-tn.".$ext;
if (is_file("$imagedir/$thumbnail")) {
unlink("$imagedir/$thumbnail");
}
// writeLog("maxImageDimension: $maxImageDimension");
// Check which is largest dimension
if ($image_width > 0 && $image_width >= $image_height) {
// Create the thumbnail
$height = intval($image_height * $maxThumbDimension / $image_width);
$command = "$cmd -size {$image_width}x$image_height $imagedir/$image -resize {$maxThumbDimension}x$height +profile \"*\" $target";
// writeLog("command: $command");
exec($command);
if (is_file($target)) {
$res = rename($target,"$imagedir/$thumbnail");
if (!$res) copy("$imagedir/$image","$imagedir/$thumbnail");
}
}
else {
if ($image_height > 0) {
// Create the thumbnail
$width = intval($image_width * $maxThumbDimension / $image_height);
$command = "$cmd -size {$image_width}x{$image_height} $imagedir/$image -resize {$width}x{$maxThumbDimension} +profile \"*\" $target";
// writeLog("command: $command");
exec($command);
if (is_file($target)) {
$res = rename($target,"$imagedir/$thumbnail");
if (!$res) copy("$imagedir/$image","$imagedir/$thumbnail");
}
}
}
return($thumbnail);
}
//
// Reset OP cache if in development mode
//
function resetCache() {
$mode = 'Production';
$mode = 'Development';
if (function_exists('opcache_reset') && $mode == 'Development') opcache_reset();
}
//
// Help message for form fields
//
function displayHelp($msg, $align='right') {
if (strlen($msg) == 0) return;
if (substr($msg,0,5) === 'TEXT-') $msg = getLanguageText($msg);
$text = "<span class='hl-tooltip hl-tooltip-image help-text'><img src='images/icons/help16.png' width='14' height='14'>\n" .
"<span class='hl-tooltiptext hl-tooltip-center100' style='textalign: left; width: 200px; padding: 5px;'>$msg</span>\n" .
"</span>";
echo($text);
}
?>