Files
HotLunches/ajax/ajax.schoolSearch.php
T
2026-10-19 15:22:52 -07:00

169 lines
6.0 KiB
PHP

<?php
$ValidAccess = 2010;
$sessionName = 'hotlunch';
session_name($sessionName);
session_start();
require_once("include/globals.php");
if (SessionActive() != 1 || ! UserLoggedIn()) {
logExit("Session is not active",$moduleName);
exit;
}
// CSRF token validation
include "include/ajax.csrfToken.php";
$moduleName = "ajax.schoolSearch.php";
// startLog($moduleName);
// Get search text
$text = trim(strtolower($_GET['term']));
$words = explode(' ',$text);
writeLog("text: $text\n");
// School status options
$schoolStatus = "'Active','Special','Deleted','Inactive','Linked'";
$schoolStatus = "'','Active','Special','Linked'";
$schoolStatus = "'','Active','Special'";
// Format where clause
$swhere = "";
for ($i = 0; $i < count($words); $i++) {
$word = db_escape_string($words[$i]);
if (preg_match("/^#/",$word) || is_numeric($word)) {
$num = intval(preg_replace("/^#/","",$word));
// Check for an invoice number
$inv = $num - 500;
if ($inv > 0) {
$query = "SELECT * FROM invoice_header WHERE record_num = $inv";
$results = db_query($query);
if (db_num_rows($results)) {
$row = db_fetch_assoc($results);
$num = $row['school_num'];
}
}
$swhere = "AND record_num = $num ";
$cwhere = "AND s.record_num = $num ";
$schoolStatus = "'Active','Special'";
}
else {
$num = 0;
if (is_numeric($word)) $num = intval($word);
$swhere .= "AND (school_name LIKE '%$word%' " .
"OR city LIKE '$word%' " .
"OR principal LIKE '%$word%' " .
"OR principal_email LIKE '%$word%' " .
"OR phone LIKE '%$word%' " .
"OR default_url_code LIKE '%$word%' " .
"OR url LIKE '%$word%' " .
"OR reseller LIKE '$word%' " .
"OR reply_email LIKE '%$word%' " .
"OR support_email LIKE '%$word%' " .
"OR volunteer_reminder_email LIKE '%$word%' " .
"OR record_num = $num) ";
$cwhere .= "AND (sc.contact_name LIKE '%$word%' OR sc.contact_email LIKE '%$word%' OR sc.contact_phone LIKE '%$word%' OR sc.contact_cell LIKE '%$word%' OR s.record_num = $num) ";
}
}
// Get matching school records
$query = "SELECT DISTINCT school_name,city,province,status,url,reseller,record_num FROM schools WHERE status IN ($schoolStatus) $swhere ORDER BY school_name";
writeLog("query: $query\n");
$results = db_query($query);
$query = "SELECT DISTINCT school_name,city,province,status,url,reseller,record_num FROM schools WHERE status IN ('Linked') $swhere ORDER BY school_name";
writeLog("query: $query\n");
$results2 = db_query($query);
$query = "SELECT DISTINCT school_name,city,province,status,url,reseller,record_num FROM schools WHERE status IN ('Inactive','Deleted') $swhere ORDER BY school_name";
writeLog("query: $query\n");
$results3 = db_query($query);
// Format data for return
$return_data = array();
$num_records = db_num_rows($results) + db_num_rows($results2) + db_num_rows($results3);
if ($num_records > 0) {
writeLog("records: $num_records\n");
if (db_num_rows($results)) {
$return_data[] = array("value" => "-- ACTIVE SCHOOLS ------------",
"school_name" => '',
"school_num" => 0);
while ($row = db_fetch_assoc($results)) {
extract($row);
$r = "";
if ($reseller != 'hotlunch') $r = " ($reseller)";
$school_status = " ($status/$record_num)";
$return_data[] = array("value" => $school_name.' - '.$city.', '.$province.$r."$school_status - $url",
"school_name" => $school_name,
"school_num" => $record_num);
}
}
if (db_num_rows($results2)) {
$return_data[] = array("value" => "-- LINKED SCHOOLS ------------",
"school_name" => '',
"school_num" => 0);
while ($row = db_fetch_assoc($results2)) {
extract($row);
$r = "";
if ($reseller != 'hotlunch') $r = " ($reseller)";
$school_status = " ($status/$record_num)";
$return_data[] = array("value" => $school_name.' - '.$city.', '.$province.$r."$school_status - $url",
"school_name" => $school_name,
"school_num" => $record_num);
}
}
if (db_num_rows($results3)) {
$return_data[] = array("value" => "-- INACTIVE / DELETED SCHOOLS ------------",
"school_name" => '',
"school_num" => 0);
while ($row = db_fetch_assoc($results3)) {
extract($row);
$r = "";
if ($reseller != 'hotlunch') $r = " ($reseller)";
$school_status = " ($status/$record_num)";
$return_data[] = array("value" => $school_name.' - '.$city.', '.$province.$r."$school_status - $url",
"school_name" => $school_name,
"school_num" => $record_num);
}
}
}
// Get matching contact records
$query = "SELECT DISTINCT school_name,city,province,s.record_num,sc.contact_name,sc.contact_phone " .
"FROM schools AS s,school_contact_list AS sc " .
"WHERE status IN ('Active','Special') AND s.record_num = sc.school_num $cwhere " .
"ORDER BY school_name";
writeLog("query: $query\n");
$results = db_query($query);
// Format data for return
$num_records = db_num_rows($results);
if ($num_records > 0) {
writeLog("records: $num_records\n");
$return_data[] = array("value" => "-- CONTACTS ------------",
"school_name" => '',
"school_num" => 0);
while ($row = db_fetch_assoc($results)) {
extract($row);
$return_data[] = array("value" => $school_name.' - '.$contact_name,
"school_name" => $school_name,
"school_num" => $record_num);
}
}
writeLog("completed\n");
echo(json_encode($return_data));
return false;
?>