|
From: Markus R. K. <man...@di...> - 2026-07-22 11:48:23
|
Hi Paul et al.,
below please find a section that can be added to
plugins/calendar/calendar.php
to have a search field and the needed search algorithm for the web ui.
As mentioned, I moved my new code to a function to be added to above php.
This is for better overview.
The relevant function,
search_engine()
should be called last. So we have this order:
displayPageHeader($color, 'None');
calendar_header();
readcalendardata();
startcalendar();
drawmonthview();
endcalendar();
search_engine();
Since this mailing list's majordomo does not forward emails with
attachments, I add the function here as plain text.
Hope I could help!
Best regards,
Markus
######################################################################
function search_engine() {
// 1. Globale SquirrelMail-Variablen laden
global $data_dir, $username, $default_charset;
// Fallback definieren, falls das Charset nicht gesetzt sein sollte
$charset = !empty($default_charset) ? $default_charset : 'ISO-8859-1';
// ----------------------------------------------------
// 2. Suchformular ausgeben
// ----------------------------------------------------
$search_term_input = isset($_GET['cal_search']) ?
htmlspecialchars($_GET['cal_search'], ENT_COMPAT | ENT_HTML401,
$charset) : '';
$label_search = _("Search");
echo '<br />' . "\n" .
'<form method="GET" action="calendar.php">' . "\n" .
' <table align="center" cellspacing="1" cellpadding="2"
border="0">' . "\n" .
' <tr>' . "\n" .
' <td>' . "\n" .
' <strong>' . $label_search . ':</strong> ' . "\n" .
' <input type="text" name="cal_search" value="' .
$search_term_input . '" size="20" />' . "\n" .
' <input type="submit" value="' . $label_search . '" />' .
"\n" .
' </td>' . "\n" .
' </tr>' . "\n" .
' </table>' . "\n" .
'</form>' . "\n";
// ----------------------------------------------------
// 3. Suche ausführen & Ergebnisse verarbeiten
// ----------------------------------------------------
if (!empty($_GET['cal_search'])) {
$search_term = $_GET['cal_search'];
// Suchbegriff bei Bedarf an das Server-Charset anpassen
if (function_exists('mb_convert_encoding')) {
$search_term_converted = mb_convert_encoding($search_term,
$charset, 'UTF-8');
} else {
$search_term_converted = $search_term;
}
$search_pattern = $data_dir . $username . '.*.cal';
$cal_files = glob($search_pattern);
$results = [];
if (is_array($cal_files)) {
foreach ($cal_files as $file) {
if (!file_exists($file)) continue;
$handle = fopen($file, "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
$line = trim($line);
if (empty($line)) continue;
// Format: MMDDYYYY|HHMM|0|0|Text||
$fields = explode('|', $line);
// Vergleich inkl. Charset-Anpassung
if (isset($fields[4]) && stripos($fields[4],
$search_term_converted) !== false) {
$date_raw = $fields[0]; // MMDDYYYY
$month = substr($date_raw, 0, 2);
$day = substr($date_raw, 2, 2);
$year = substr($date_raw, 4, 4);
$time_raw = str_pad(trim($fields[1]), 4, '0',
STR_PAD_LEFT);
$sort_key = $year . $month . $day . $time_raw;
$formatted_time = substr($time_raw, 0, 2) .
':' . substr($time_raw, 2, 2);
// Text unter Berücksichtigung des Charsets
absichern
$clean_text = htmlspecialchars($fields[4],
ENT_COMPAT | ENT_HTML401, $charset);
$results[] = [
'sort_key' => $sort_key,
'link_params' =>
"year=$year&month=$month&day=$day",
'display_date' => "$day.$month.$year",
'time' => $formatted_time,
'text' => $clean_text
];
}
}
fclose($handle);
}
}
// Nach Datum absteigend sortieren (neueste zuerst)
usort($results, function($a, $b) {
return strcmp($b['sort_key'], $a['sort_key']);
});
}
// ----------------------------------------------------
// 4. Ergebnisse als Tabelle ausgeben
// ----------------------------------------------------
$search_term_display = htmlspecialchars($search_term, ENT_COMPAT |
ENT_HTML401, $charset);
echo '<br />' . "\n";
echo '<table align="center" cellspacing="1" cellpadding="4"
border="0" bgcolor="#ababab" width="80%">' . "\n";
echo ' <tr bgcolor="#dcdcdc">' . "\n";
echo ' <td colspan="3"><strong>' . sprintf(_("Search results"),
$search_term_display) . '</strong></td>' . "\n";
echo ' </tr>' . "\n";
if (empty($results)) {
echo ' <tr bgcolor="#ffffff"><td colspan="3">' . _("No
results") . '</td></tr>' . "\n";
} else {
foreach ($results as $res) {
echo ' <tr bgcolor="#ffffff">' . "\n";
echo ' <td width="15%">' . $res['display_date'] .
'</td>' . "\n";
echo ' <td width="10%">' . $res['time'] . '</td>' . "\n";
echo ' <td><a href="day.php?' . $res['link_params'] .
'">' . $res['text'] . '</a></td>' . "\n";
echo ' </tr>' . "\n";
}
}
echo '</table>' . "\n";
}
}
|