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

330 lines
12 KiB
PHP

<?php
require_once("include/globals.php");
if (SessionActive() != 1 || ! UserLoggedIn()) {
header("Location: /");
exit();
}
// Start the template and output buffering
startTemplate();
$templatePageTitle = getLanguageText('CONTACT-ADMIN-TITLE');
//
// Process all actions to change the contacts for a user account
//
$naction = "";
if (array_key_exists('naction',$_GET)) $naction = $_GET['naction'];
if (array_key_exists('naction',$_POST)) $naction = $_POST['naction'];
$mode = "";
if (array_key_exists('mode',$_GET)) $mode = $_GET['mode'];
if (array_key_exists('mode',$_POST)) $mode = $_POST['mode'];
$_SESSION['mode'] = $mode;
$CurrentTable = "contacts";
$DeleteField = "first_name,last_name";
$dSeparator = " ";
$FormFile = "contacts.form.php";
$OrderBy = "last_name,first_name";
$TitleTest = getLanguageText('TEXT-CONTACT');
$unum = $_SESSION['UserInfo']['record_num'];
$uwhere = "";
if ($_SESSION['UserInfo']['type'] == 'Parent' || $_SESSION['mode'] == 'parent') {
$uwhere = "and user_num = $unum ";
}
// Ensure that only valid user types see the menu
$AllowedUserTypes = array('SuperUser','System Administrator','Application Administrator','Communications Administrator','Administration Team');
if (checkPermission($AllowedUserTypes) == 1 && $_SESSION['mode'] != 'parent') {
include "contacts.options.php";
}
$disclaimer = getLanguageText('TEXT-CONTACT-DISCLAIMER');
$query = "SELECT text_data FROM text_data WHERE school_num = $school_num AND text_id = 'Class List Message'";
$text = db_getVal($query);
if (strlen($text)) $disclaimer = $text;
?>
<table width='500'>
<tr><td align='center' class='success'><?php echo($disclaimer); ?></td></tr>
</table>
<?php
$school_num = $base_school_num;
switch (strtolower($naction)) {
case "add":
$next_naction = "insert";
$active = 'Y';
include $FormFile;
break;
case "insert":
extract($_POST);
$first_name = trim($first_name);
$last_name = trim($last_name);
$home_phone = trim($home_phone);
if (isset($work_phone)) $work_phone = trim($work_phone);
if (isset($cell_phone)) $cell_phone = trim($cell_phone);
if (isset($home_email)) $home_email = trim($home_email);
if (isset($work_email)) $work_email = trim($work_email);
if (!isset($user_num)) $user_num = intval($_SESSION['UserInfo']['record_num']);
$uid = $user_num;
if ($first_name == strtolower($first_name) || $first_name == strtoupper($first_name)) {
$first_name = ucwords(strtolower($first_name));
$first_name = str_replace(" And "," and ",$first_name);
}
if ($last_name == strtolower($last_name) || $last_name == strtoupper($last_name)) {
$last_name = ucwords(strtolower($last_name));
$last_name = str_replace(" And "," and ",$last_name);
}
// If there is a phone number entered then format it
if (isset($home_phone) && strlen($home_phone) > 0) {
$phone = preg_replace("/[^0-9]/","",$home_phone);
if (strlen($phone) == 10) {
$home_phone = "(" . substr($phone,0,3) . ")" . substr($phone,3,3) . "-" . substr($phone,6);
}
}
// Same for work number
if (isset($work_phone) && strlen($work_phone) > 0) {
$phone = preg_replace("/[^0-9]/","",$work_phone);
if (strlen($phone) == 10) {
$work_phone = "(" . substr($phone,0,3) . ")" . substr($phone,3,3) . "-" . substr($phone,6);
}
}
// Same for cell number
if (isset($cell_phone) && strlen($cell_phone) > 0) {
$phone = preg_replace("/[^0-9]/","",$cell_phone);
if (strlen($phone) == 10) {
$cell_phone = "(" . substr($phone,0,3) . ")" . substr($phone,3,3) . "-" . substr($phone,6);
}
}
if (!isset($home_email)) $home_email = '';
if (!isset($work_email)) $work_email = '';
$home_email = strtolower($home_email);
$work_email = strtolower($work_email);
$table = new Table("$CurrentTable");
$query = $table->BuildInsert();
db_query($query);
$form_message = getLanguageText('RECORD-INSERTED-MSG');
$naction = 'list';
header("Location: /?action=$action&naction=$naction&form_message=" . urlencode($form_message) . "&error_message=" . urlencode($error_message));
exit();
case "edit":
$next_naction = "update";
// Get current record
$record_num = intval($_GET['record_num']);
$query = "select * from $CurrentTable " .
"where school_num = $school_num " .
$uwhere .
"and record_num = $record_num";
$results = db_query($query);
if (db_num_rows($results) == 1) {
$row = db_fetch_array($results);
extract($row);
include $FormFile;
}
else {
$error_message = getLanguageText('ERRORMSG-NOT-FOUND');
$naction = 'list';
header("Location: /?action=$action&naction=$naction&form_message=" . urlencode($form_message) . "&error_message=" . urlencode($error_message));
exit();
}
break;
case "update":
extract($_POST);
if (!isset($user_num)) $user_num = intval($_SESSION['UserInfo']['record_num']);
$uid = $user_num;
if ($first_name == strtolower($first_name) || $first_name == strtoupper($first_name)) {
$first_name = ucwords(strtolower($first_name));
}
if ($last_name == strtolower($last_name) || $last_name == strtoupper($last_name)) {
$last_name = ucwords(strtolower($last_name));
}
// If there is a phone number entered then format it
if (isset($home_phone) && strlen($home_phone) > 0) {
$phone = preg_replace("/[^0-9]/","",$home_phone);
if (strlen($phone) == 10) {
$home_phone = "(" . substr($phone,0,3) . ")" . substr($phone,3,3) . "-" . substr($phone,6);
}
}
// Same for work number
if (isset($work_phone) && strlen($work_phone) > 0) {
$phone = preg_replace("/[^0-9]/","",$work_phone);
if (strlen($phone) == 10) {
$work_phone = "(" . substr($phone,0,3) . ")" . substr($phone,3,3) . "-" . substr($phone,6);
}
}
// Same for cell number
if (isset($cell_phone) && strlen($cell_phone) > 0) {
$phone = preg_replace("/[^0-9]/","",$cell_phone);
if (strlen($phone) == 10) {
$cell_phone = "(" . substr($phone,0,3) . ")" . substr($phone,3,3) . "-" . substr($phone,6);
}
}
$home_email = strtolower($home_email);
if (isset($work_email)) $work_email = strtolower($work_email);
$table = new Table("$CurrentTable");
$query = $table->BuildUpdate();
db_query($query);
$form_message = getLanguageText('RECORD-UPDATED-MSG');
// Display the updated entry
$lookup_query = $table->BuildQuery("","","c.");
$lookup_where = "";
if (stripos("where ",$lookup_query)) $lookup_where = preg_replace("/.* where/i","and",$lookup_query);
if (stripos("order by ",$lookup_where)) $lookup_where = preg_replace("/order by .*/i","",$lookup_where);
$lookup_where .= " ";
$naction = 'list';
header("Location: /?action=$action&naction=$naction&form_message=" . urlencode($form_message) . "&error_message=" . urlencode($error_message));
exit();
case "delete":
$record_num = intval($_GET['record_num']);
// Parents can not delete the last contact record
if ($_SESSION['UserInfo']['type'] == 'Parent' || $_SESSION['mode'] == 'parent') {
// Check if this is the only contact record for this account
$query = "select count(*) from $CurrentTable " .
"where school_num = $school_num " .
$uwhere;
$num = db_getVal($query);
if ($num == 1) {
$error_message = getLanguageText('CONTACT-DELETE-ERROR');
$naction = 'list';
header("Location: /?action=$action&naction=$naction&form_message=" . urlencode($form_message) . "&error_message=" . urlencode($error_message));
exit();
}
}
// Verify that the contact record is accessible by the current user
$query = "select * from $CurrentTable " .
"where school_num = $school_num " .
$uwhere .
"and record_num = $record_num";
$results = db_query($query);
if (db_num_rows($results) == 1) {
$row = db_fetch_array($results);
extract($row);
$uid = $user_num;
$dFields = explode(",",$DeleteField);
$dTitle = "";
for ($i = 0; $i < count($dFields); $i++) {
$dTitle .= $row[$dFields[$i]] . $dSeparator;
}
if ($_SESSION['mode'] == 'parent') $HiddenFields = " <input type='hidden' name='mode' value='parent'>\n";
include "delete.form.php";
}
else {
$error_message = getLanguageText('ERRORMSG-NOT-FOUND');
$naction = 'list';
header("Location: /?action=$action&naction=$naction&form_message=" . urlencode($form_message) . "&error_message=" . urlencode($error_message));
exit();
}
break;
case "delete-confirmed":
if (array_key_exists('record_num',$_GET) && $_GET['record_num'] > 0) $record_num = intval($_GET['record_num']);
if (array_key_exists('record_num',$_POST) && $_POST['record_num'] > 0) $record_num = intval($_POST['record_num']);
// Verify that the contact record is accessible by the current user
$query = "select * from $CurrentTable " .
"where school_num = $school_num " .
$uwhere .
"and record_num = $record_num";
$results = db_query($query);
if (db_num_rows($results) == 1) {
$row = db_fetch_array($results);
extract($row);
$uid = $user_num;
// Delete the current record
$query = "delete from $CurrentTable " .
"where school_num = $school_num " .
$uwhere .
"and record_num = $record_num";
db_query($query);
$form_message = getLanguageText('RECORD-DELETED-MSG');
}
else {
$error_message = getLanguageText('ERRORMSG-NO-ACCESS');
}
$naction = 'list';
header("Location: /?action=$action&naction=$naction&form_message=" . urlencode($form_message) . "&error_message=" . urlencode($error_message));
exit();
case "search":
$PageTitle = getLanguageText('TEXT-SEARCH');
$ButtonText = $PageTitle;
$ChoosePagination = 'Y';
$next_naction = "lookup";
include $FormFile;
break;
case "lookup":
extract($_POST);
$table = new Table("$CurrentTable");
$lookup_query = $table->BuildQuery("","","c.");
$lookup_where = "";
if (stripos("where ",$lookup_query)) $lookup_where = preg_replace("/.* where/i","and",$lookup_query);
if (stripos("order by ",$lookup_where)) $lookup_where = preg_replace("/order by .*/i","",$lookup_where);
$lookup_where .= " ";
$naction = "list";
$TitleText = getLanguageText('RECORD-LOOKUP');
include 'contacts.display.php';
break;
case "fetch":
$lookup_where = "where school.record_num = $record_num";
$naction = "list";
$TitleText = getLanguageText('RECORD-LOOKUP');
break;
case "display":
case "list":
default:
include 'contacts.display.php';
break;
}
endTemplate();
?>