Lexer->addSpecialPattern('\{\{calendar(?:[^\}]*)\}\}', $mode, 'plugin_calendar'); $this->Lexer->addSpecialPattern('\{\{eventlist(?:[^\}]*)\}\}', $mode, 'plugin_calendar'); $this->Lexer->addSpecialPattern('\{\{eventpanel(?:[^\}]*)\}\}', $mode, 'plugin_calendar'); } public function handle($match, $state, $pos, Doku_Handler $handler) { $isEventList = (strpos($match, '{{eventlist') === 0); $isEventPanel = (strpos($match, '{{eventpanel') === 0); if ($isEventList) { $match = substr($match, 12, -2); } elseif ($isEventPanel) { $match = substr($match, 13, -2); } else { $match = substr($match, 10, -2); } $params = array( 'type' => $isEventPanel ? 'eventpanel' : ($isEventList ? 'eventlist' : 'calendar'), 'year' => date('Y'), 'month' => date('n'), 'namespace' => '', 'daterange' => '', 'date' => '' ); if (trim($match)) { $pairs = preg_split('/\s+/', trim($match)); foreach ($pairs as $pair) { if (strpos($pair, '=') !== false) { list($key, $value) = explode('=', $pair, 2); $params[trim($key)] = trim($value); } else { // Handle standalone flags like "today" $params[trim($pair)] = true; } } } return $params; } public function render($mode, Doku_Renderer $renderer, $data) { if ($mode !== 'xhtml') return false; if ($data['type'] === 'eventlist') { $html = $this->renderStandaloneEventList($data); } elseif ($data['type'] === 'eventpanel') { $html = $this->renderEventPanelOnly($data); } else { $html = $this->renderCompactCalendar($data); } $renderer->doc .= $html; return true; } private function renderCompactCalendar($data) { $year = (int)$data['year']; $month = (int)$data['month']; $namespace = $data['namespace']; $events = $this->loadEvents($namespace, $year, $month); $calId = 'cal_' . md5(serialize($data) . microtime()); $monthName = date('F Y', mktime(0, 0, 0, $month, 1, $year)); $prevMonth = $month - 1; $prevYear = $year; if ($prevMonth < 1) { $prevMonth = 12; $prevYear--; } $nextMonth = $month + 1; $nextYear = $year; if ($nextMonth > 12) { $nextMonth = 1; $nextYear++; } $html = '
'; // Embed events data as JSON for JavaScript access $html .= ''; // Left side: Calendar $html .= '
'; // Header with navigation $html .= '
'; $html .= ''; $html .= '

' . $monthName . '

'; $html .= ''; $html .= ''; $html .= '
'; // Calendar grid $html .= ''; $html .= ''; $html .= ''; $html .= ''; $firstDay = mktime(0, 0, 0, $month, 1, $year); $daysInMonth = date('t', $firstDay); $dayOfWeek = date('w', $firstDay); // Load events from previous and next months to catch spanning events $prevMonth = $month - 1; $prevYear = $year; if ($prevMonth < 1) { $prevMonth = 12; $prevYear--; } $nextMonth = $month + 1; $nextYear = $year; if ($nextMonth > 12) { $nextMonth = 1; $nextYear++; } $prevMonthEvents = $this->loadEvents($namespace, $prevYear, $prevMonth); $nextMonthEvents = $this->loadEvents($namespace, $nextYear, $nextMonth); // Combine all events for processing $allEvents = array_merge($events, $prevMonthEvents, $nextMonthEvents); // Build a map of all events with their date ranges $eventRanges = array(); foreach ($allEvents as $dateKey => $dayEvents) { foreach ($dayEvents as $evt) { $eventId = isset($evt['id']) ? $evt['id'] : ''; $startDate = $dateKey; $endDate = isset($evt['endDate']) && $evt['endDate'] ? $evt['endDate'] : $dateKey; // Only process events that touch this month $eventStart = new DateTime($startDate); $eventEnd = new DateTime($endDate); $monthStart = new DateTime(sprintf('%04d-%02d-01', $year, $month)); $monthEnd = new DateTime(sprintf('%04d-%02d-%02d', $year, $month, $daysInMonth)); // Skip if event doesn't overlap with current month if ($eventEnd < $monthStart || $eventStart > $monthEnd) { continue; } // Create entry for each day the event spans $current = clone $eventStart; while ($current <= $eventEnd) { $currentKey = $current->format('Y-m-d'); // Check if this date is in current month $currentDate = DateTime::createFromFormat('Y-m-d', $currentKey); if ($currentDate && $currentDate->format('Y-m') === sprintf('%04d-%02d', $year, $month)) { if (!isset($eventRanges[$currentKey])) { $eventRanges[$currentKey] = array(); } // Add event with span information $evt['_span_start'] = $startDate; $evt['_span_end'] = $endDate; $evt['_is_first_day'] = ($currentKey === $startDate); $evt['_is_last_day'] = ($currentKey === $endDate); $evt['_original_date'] = $dateKey; // Keep track of original date // Check if event continues from previous month or to next month $evt['_continues_from_prev'] = ($eventStart < $monthStart); $evt['_continues_to_next'] = ($eventEnd > $monthEnd); $eventRanges[$currentKey][] = $evt; } $current->modify('+1 day'); } } } $currentDay = 1; $rowCount = ceil(($daysInMonth + $dayOfWeek) / 7); for ($row = 0; $row < $rowCount; $row++) { $html .= ''; for ($col = 0; $col < 7; $col++) { if (($row === 0 && $col < $dayOfWeek) || $currentDay > $daysInMonth) { $html .= ''; } else { $dateKey = sprintf('%04d-%02d-%02d', $year, $month, $currentDay); $isToday = ($dateKey === date('Y-m-d')); $hasEvents = isset($eventRanges[$dateKey]) && !empty($eventRanges[$dateKey]); $classes = 'cal-day'; if ($isToday) $classes .= ' cal-today'; if ($hasEvents) $classes .= ' cal-has-events'; $html .= ''; $currentDay++; } } $html .= ''; } $html .= '
SMTWTFS
'; $html .= '' . $currentDay . ''; if ($hasEvents) { // Sort events by time (no time first, then by time) $sortedEvents = $eventRanges[$dateKey]; usort($sortedEvents, function($a, $b) { $timeA = isset($a['time']) ? $a['time'] : ''; $timeB = isset($b['time']) ? $b['time'] : ''; // Events without time go first if (empty($timeA) && !empty($timeB)) return -1; if (!empty($timeA) && empty($timeB)) return 1; if (empty($timeA) && empty($timeB)) return 0; // Sort by time return strcmp($timeA, $timeB); }); // Show colored stacked bars for each event $html .= '
'; foreach ($sortedEvents as $evt) { $eventId = isset($evt['id']) ? $evt['id'] : ''; $eventColor = isset($evt['color']) ? htmlspecialchars($evt['color']) : '#3498db'; $eventTime = isset($evt['time']) ? $evt['time'] : ''; $eventTitle = isset($evt['title']) ? htmlspecialchars($evt['title']) : 'Event'; $originalDate = isset($evt['_original_date']) ? $evt['_original_date'] : $dateKey; $isFirstDay = isset($evt['_is_first_day']) ? $evt['_is_first_day'] : true; $isLastDay = isset($evt['_is_last_day']) ? $evt['_is_last_day'] : true; $barClass = empty($eventTime) ? 'event-bar-no-time' : 'event-bar-timed'; // Add classes for multi-day spanning if (!$isFirstDay) $barClass .= ' event-bar-continues'; if (!$isLastDay) $barClass .= ' event-bar-continuing'; $html .= ''; $html .= ''; } $html .= '
'; } $html .= '
'; $html .= '
'; // End calendar-left // Right side: Event list $html .= '
'; $html .= '
'; $html .= '
'; $html .= '

Events

'; if ($namespace) { $html .= '' . htmlspecialchars($namespace) . ''; } $html .= '
'; $html .= ''; $html .= '
'; $html .= '
'; $html .= $this->renderEventListContent($events, $calId, $namespace); $html .= '
'; $html .= '
'; // End calendar-right // Event dialog $html .= $this->renderEventDialog($calId, $namespace); // Month/Year picker dialog (at container level for proper overlay) $html .= $this->renderMonthPicker($calId, $year, $month, $namespace); $html .= '
'; // End container return $html; } private function renderEventListContent($events, $calId, $namespace) { if (empty($events)) { return '

No events this month

'; } $html = ''; ksort($events); foreach ($events as $dateKey => $dayEvents) { foreach ($dayEvents as $event) { $eventId = isset($event['id']) ? $event['id'] : ''; $title = isset($event['title']) ? htmlspecialchars($event['title']) : 'Untitled'; $time = isset($event['time']) ? htmlspecialchars($event['time']) : ''; $color = isset($event['color']) ? htmlspecialchars($event['color']) : '#3498db'; $description = isset($event['description']) ? $event['description'] : ''; $isTask = isset($event['isTask']) ? $event['isTask'] : false; $completed = isset($event['completed']) ? $event['completed'] : false; $endDate = isset($event['endDate']) ? $event['endDate'] : ''; // Process description for wiki syntax, HTML, images, and links $renderedDescription = $this->renderDescription($description); // Convert to 12-hour format $displayTime = ''; if ($time) { $timeObj = DateTime::createFromFormat('H:i', $time); if ($timeObj) { $displayTime = $timeObj->format('g:i A'); } else { $displayTime = $time; } } // Format date display with day of week $dateObj = new DateTime($dateKey); $displayDate = $dateObj->format('D, M j'); // e.g., "Mon, Jan 24" // Multi-day indicator $multiDay = ''; if ($endDate && $endDate !== $dateKey) { $endObj = new DateTime($endDate); $multiDay = ' → ' . $endObj->format('D, M j'); } $completedClass = $completed ? ' event-completed' : ''; $html .= '
'; $html .= '
'; $html .= '
'; $html .= '' . $title . ''; $html .= '
'; $html .= '
'; $html .= '' . $displayDate . $multiDay; if ($displayTime) { $html .= ' • ' . $displayTime; } $html .= ''; $html .= '
'; if ($description) { $html .= '
' . $renderedDescription . '
'; } $html .= '
'; // event-info $html .= '
'; $html .= ''; $html .= ''; $html .= '
'; // Checkbox for tasks - ON THE FAR RIGHT if ($isTask) { $checked = $completed ? 'checked' : ''; $html .= ''; } $html .= '
'; } } return $html; } private function renderEventPanelOnly($data) { $year = (int)$data['year']; $month = (int)$data['month']; $namespace = $data['namespace']; $height = isset($data['height']) ? $data['height'] : '400px'; // Validate height format (must be px, em, rem, vh, or %) if (!preg_match('/^\d+(\.\d+)?(px|em|rem|vh|%)$/', $height)) { $height = '400px'; // Default fallback } $events = $this->loadEvents($namespace, $year, $month); $calId = 'panel_' . md5(serialize($data) . microtime()); $monthName = date('F Y', mktime(0, 0, 0, $month, 1, $year)); $prevMonth = $month - 1; $prevYear = $year; if ($prevMonth < 1) { $prevMonth = 12; $prevYear--; } $nextMonth = $month + 1; $nextYear = $year; if ($nextMonth > 12) { $nextMonth = 1; $nextYear++; } $html = '
'; // Header with navigation $html .= '
'; $html .= ''; $html .= '
'; $html .= '

' . $monthName . ' Events

'; if ($namespace) { $namespaceUrl = DOKU_BASE . 'doku.php?id=' . str_replace(':', ':', $namespace); $html .= '' . htmlspecialchars($namespace) . ''; } $html .= '
'; $html .= ''; $html .= ''; $html .= '
'; $html .= '
'; $html .= ''; $html .= '
'; $html .= '
'; $html .= $this->renderEventListContent($events, $calId, $namespace); $html .= '
'; $html .= $this->renderEventDialog($calId, $namespace); // Month/Year picker for event panel $html .= $this->renderMonthPicker($calId, $year, $month, $namespace); $html .= '
'; return $html; } private function renderStandaloneEventList($data) { $namespace = $data['namespace']; $daterange = $data['daterange']; $date = $data['date']; $width = isset($data['width']) ? $data['width'] : '300px'; $height = isset($data['height']) ? $data['height'] : '400px'; $today = isset($data['today']) ? true : false; // Validate width/height format if (!preg_match('/^\d+(\.\d+)?(px|em|rem|vh|vw|%)$/', $width)) { $width = '300px'; } if (!preg_match('/^\d+(\.\d+)?(px|em|rem|vh|%)$/', $height)) { $height = '400px'; } // Handle "today" parameter if ($today) { $startDate = date('Y-m-d'); $endDate = date('Y-m-d'); } elseif ($daterange) { list($startDate, $endDate) = explode(':', $daterange); } elseif ($date) { $startDate = $date; $endDate = $date; } else { $startDate = date('Y-m-01'); $endDate = date('Y-m-t'); } $allEvents = array(); $start = new DateTime($startDate); $end = new DateTime($endDate); $end->modify('+1 day'); $interval = new DateInterval('P1D'); $period = new DatePeriod($start, $interval, $end); static $loadedMonths = array(); foreach ($period as $dt) { $year = (int)$dt->format('Y'); $month = (int)$dt->format('n'); $dateKey = $dt->format('Y-m-d'); $monthKey = $year . '-' . $month; if (!isset($loadedMonths[$monthKey])) { $loadedMonths[$monthKey] = $this->loadEvents($namespace, $year, $month); } $monthEvents = $loadedMonths[$monthKey]; if (isset($monthEvents[$dateKey]) && !empty($monthEvents[$dateKey])) { $allEvents[$dateKey] = $monthEvents[$dateKey]; } } // Compact container with custom size $html = '
'; // Compact header if ($today) { $html .= '
'; $html .= '

📅 Today\'s Events

'; $html .= '
'; } else { $html .= '
'; $html .= '

' . date('M j', strtotime($startDate)); if ($startDate !== $endDate) { $html .= ' - ' . date('M j', strtotime($endDate)); } $html .= '

'; $html .= '
'; } // Scrollable event list $html .= '
'; if (empty($allEvents)) { $html .= '

No events

'; } else { foreach ($allEvents as $dateKey => $dayEvents) { // Compact date header (only if not "today" mode or multi-day range) if (!$today && $startDate !== $endDate) { $dateObj = new DateTime($dateKey); $html .= '
' . $dateObj->format('D, M j') . '
'; } foreach ($dayEvents as $event) { $title = isset($event['title']) ? htmlspecialchars($event['title']) : 'Untitled'; $time = isset($event['time']) ? $event['time'] : ''; $color = isset($event['color']) ? htmlspecialchars($event['color']) : '#3498db'; $description = isset($event['description']) ? $event['description'] : ''; // Convert time to 12-hour format $displayTime = ''; if ($time) { $timeParts = explode(':', $time); if (count($timeParts) === 2) { $hour = (int)$timeParts[0]; $minute = $timeParts[1]; $ampm = $hour >= 12 ? 'PM' : 'AM'; $hour = $hour % 12; if ($hour === 0) $hour = 12; $displayTime = $hour . ':' . $minute . ' ' . $ampm; } else { $displayTime = $time; } } // Compact event item $html .= '
'; $html .= '
' . $title . '
'; if ($displayTime) { $html .= '
' . $displayTime . '
'; } if ($description) { $renderedDesc = $this->renderDescription($description); $html .= '
' . $renderedDesc . '
'; } $html .= '
'; } } } $html .= '
'; // End content $html .= '
'; // End container return $html; } private function renderEventDialog($calId, $namespace) { $html = ''; return $html; } private function renderMonthPicker($calId, $year, $month, $namespace) { $html = ''; return $html; } private function renderDescription($description) { if (empty($description)) { return ''; } // Convert newlines to
for basic formatting $rendered = nl2br($description); // Convert DokuWiki image syntax {{image.jpg}} to HTML $rendered = preg_replace_callback( '/\{\{([^}|]+?)(?:\|([^}]+))?\}\}/', function($matches) { $imagePath = trim($matches[1]); $alt = isset($matches[2]) ? trim($matches[2]) : ''; // Handle external URLs (http:// or https://) if (preg_match('/^https?:\/\//', $imagePath)) { return '' . htmlspecialchars($alt) . ''; } // Handle internal DokuWiki images $imageUrl = DOKU_BASE . 'lib/exe/fetch.php?media=' . rawurlencode($imagePath); return '' . htmlspecialchars($alt) . ''; }, $rendered ); // Convert DokuWiki link syntax [[link|text]] to HTML $rendered = preg_replace_callback( '/\[\[([^|\]]+?)(?:\|([^\]]+))?\]\]/', function($matches) { $link = trim($matches[1]); $text = isset($matches[2]) ? trim($matches[2]) : $link; // Handle external URLs if (preg_match('/^https?:\/\//', $link)) { return '' . htmlspecialchars($text) . ''; } // Handle internal DokuWiki links with section anchors // Split page and section (e.g., "page#section" or "namespace:page#section") $parts = explode('#', $link, 2); $pagePart = $parts[0]; $sectionPart = isset($parts[1]) ? '#' . $parts[1] : ''; // Build URL with properly encoded page and unencoded section anchor $wikiUrl = DOKU_BASE . 'doku.php?id=' . rawurlencode($pagePart) . $sectionPart; return '' . htmlspecialchars($text) . ''; }, $rendered ); // Convert markdown-style links [text](url) to HTML $rendered = preg_replace_callback( '/\[([^\]]+)\]\(([^)]+)\)/', function($matches) { $text = trim($matches[1]); $url = trim($matches[2]); if (preg_match('/^https?:\/\//', $url)) { return '' . htmlspecialchars($text) . ''; } return '' . htmlspecialchars($text) . ''; }, $rendered ); // Convert plain URLs to clickable links $rendered = preg_replace_callback( '/(https?:\/\/[^\s<]+)/', function($matches) { $url = $matches[1]; return '' . htmlspecialchars($url) . ''; }, $rendered ); // Allow basic HTML tags (bold, italic, strong, em, u, code) // Already in the description, just pass through return $rendered; } private function loadEvents($namespace, $year, $month) { $dataDir = DOKU_INC . 'data/meta/'; if ($namespace) { $dataDir .= str_replace(':', '/', $namespace) . '/'; } $dataDir .= 'calendar/'; $eventFile = $dataDir . sprintf('%04d-%02d.json', $year, $month); if (file_exists($eventFile)) { $json = file_get_contents($eventFile); return json_decode($json, true); } return array(); } }