Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 89 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| OlzICal | |
0.00% |
0 / 89 |
|
0.00% |
0 / 2 |
90 | |
0.00% |
0 / 1 |
| getHtml | |
0.00% |
0 / 88 |
|
0.00% |
0 / 1 |
56 | |||
| escapeText | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | // ============================================================================= |
| 4 | // iCal-Datei generieren mit Terminen des aktuellen Jahres. |
| 5 | // Dieses Script wird immer beim Sichern und beim Löschen eines Termins |
| 6 | // aufgerufen. |
| 7 | // ============================================================================= |
| 8 | |
| 9 | namespace Olz\Termine\Components\OlzICal; |
| 10 | |
| 11 | use Doctrine\Common\Collections\Criteria; |
| 12 | use Olz\Components\Common\OlzComponent; |
| 13 | use Olz\Entity\Termine\Termin; |
| 14 | |
| 15 | /** @extends OlzComponent<array<string, mixed>> */ |
| 16 | class OlzICal extends OlzComponent { |
| 17 | public function getHtml(mixed $args): string { |
| 18 | $jahr = $this->dateUtils()->getCurrentDateInFormat('Y'); |
| 19 | $base_href = $this->envUtils()->getBaseHref(); |
| 20 | $code_href = $this->envUtils()->getCodeHref(); |
| 21 | $now_fmt = date('Ymd\THis\Z'); |
| 22 | $host = $this->envUtils()->getEmailForwardingHost(); |
| 23 | |
| 24 | // Termine abfragen |
| 25 | $termin_repo = $this->entityManager()->getRepository(Termin::class); |
| 26 | $criteria = Criteria::create() |
| 27 | ->where(Criteria::expr()->andX( |
| 28 | Criteria::expr()->gte('start_date', new \DateTime("{$jahr}-01-01")), |
| 29 | Criteria::expr()->eq('on_off', 1), |
| 30 | )) |
| 31 | ->setFirstResult(0) |
| 32 | ->setMaxResults(1000000) |
| 33 | ; |
| 34 | $termine = $termin_repo->matching($criteria); |
| 35 | |
| 36 | // ical-Kalender |
| 37 | $ical = "BEGIN:VCALENDAR". |
| 38 | "\r\nPRODID:OL Zimmerberg Termine". |
| 39 | "\r\nVERSION:2.0". |
| 40 | "\r\nMETHOD:PUBLISH". |
| 41 | "\r\nCALSCALE:GREGORIAN". |
| 42 | "\r\nX-WR-CALNAME:OL Zimmerberg Termine". |
| 43 | "\r\nX-WR-TIMEZONE:Europe/Zurich"; |
| 44 | |
| 45 | // Termine |
| 46 | foreach ($termine as $termin) { |
| 47 | $id = $termin->getId(); |
| 48 | $start_date = $termin->getStartDate(); |
| 49 | $end_date = $termin->getEndDate(); |
| 50 | $duration_days = $end_date ? ($end_date->getTimestamp() - $start_date->getTimestamp()) / 86400 : 0; |
| 51 | $should_split = $duration_days > 8; |
| 52 | $solv_id = $termin->getSolvId(); |
| 53 | |
| 54 | $olz_url = "{$base_href}{$code_href}termine/{$id}"; |
| 55 | $solv_url = "https://www.o-l.ch/cgi-bin/fixtures?&mode=show&unique_id={$solv_id}"; |
| 56 | |
| 57 | $links = "OLZ-Termin: {$olz_url}"; |
| 58 | $attach = "\r\nATTACH;FMTTYPE=text/html:{$olz_url}"; |
| 59 | $links .= $solv_id ? "\nSOLV-Termin: {$solv_url}" : ""; |
| 60 | $attach .= $solv_id ? "\r\nATTACH;FMTTYPE=text/html:{$solv_url}" : ""; |
| 61 | |
| 62 | $start_date_fmt = $this->dateUtils()->olzDate('jjjjmmtt', $start_date); |
| 63 | $end_date_fmt = $end_date ? $this->dateUtils()->olzDate('jjjjmmtt', $end_date) : $start_date_fmt; |
| 64 | $modified_fmt = $termin->getLastModifiedAt()->format('Ymd\THis\Z'); |
| 65 | $created_fmt = $termin->getCreatedAt()->format('Ymd\THis\Z'); |
| 66 | $description_fmt = $this->escapeText("{$termin->getText()}\n{$links}"); |
| 67 | $label_idents = implode(', ', array_map( |
| 68 | fn ($label) => "{$label->getIdent()}", |
| 69 | [...$termin->getLabels()], |
| 70 | )); |
| 71 | if ($should_split) { |
| 72 | $ical .= |
| 73 | "\r\nBEGIN:VEVENT". |
| 74 | "\r\nDTSTART;VALUE=DATE:{$start_date_fmt}". |
| 75 | "\r\nDTEND;VALUE=DATE:{$start_date_fmt}". |
| 76 | "\r\nDTSTAMP:{$now_fmt}". |
| 77 | "\r\nLAST-MODIFIED:{$modified_fmt}". |
| 78 | "\r\nCREATED:{$created_fmt}". |
| 79 | "\r\nSUMMARY:{$termin->getTitle()} (Beginn)". |
| 80 | "\r\nDESCRIPTION:{$description_fmt}". |
| 81 | "\r\nCATEGORIES:{$label_idents}". |
| 82 | $attach. |
| 83 | "\r\nCLASS:PUBLIC". |
| 84 | "\r\nUID:olz_termin_{$id}_start@{$host}". |
| 85 | "\r\nEND:VEVENT". |
| 86 | "\r\nBEGIN:VEVENT". |
| 87 | "\r\nDTSTART;VALUE=DATE:{$end_date_fmt}". |
| 88 | "\r\nDTEND;VALUE=DATE:{$end_date_fmt}". |
| 89 | "\r\nDTSTAMP:{$now_fmt}". |
| 90 | "\r\nLAST-MODIFIED:{$modified_fmt}". |
| 91 | "\r\nCREATED:{$created_fmt}". |
| 92 | "\r\nSUMMARY:{$termin->getTitle()} (Ende)". |
| 93 | "\r\nDESCRIPTION:{$description_fmt}". |
| 94 | "\r\nCATEGORIES:{$label_idents}". |
| 95 | $attach. |
| 96 | "\r\nCLASS:PUBLIC". |
| 97 | "\r\nUID:olz_termin_{$id}_end@{$host}". |
| 98 | "\r\nEND:VEVENT"; |
| 99 | } else { |
| 100 | $ical .= |
| 101 | "\r\nBEGIN:VEVENT". |
| 102 | "\r\nDTSTART;VALUE=DATE:{$start_date_fmt}". |
| 103 | "\r\nDTEND;VALUE=DATE:{$end_date_fmt}". |
| 104 | "\r\nDTSTAMP:{$now_fmt}". |
| 105 | "\r\nLAST-MODIFIED:{$modified_fmt}". |
| 106 | "\r\nCREATED:{$created_fmt}". |
| 107 | "\r\nSUMMARY:{$termin->getTitle()}". |
| 108 | "\r\nDESCRIPTION:{$description_fmt}". |
| 109 | "\r\nCATEGORIES:{$label_idents}". |
| 110 | $attach. |
| 111 | "\r\nCLASS:PUBLIC". |
| 112 | "\r\nUID:olz_termin_{$id}@{$host}". |
| 113 | "\r\nEND:VEVENT"; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | $ical .= "\r\nEND:VCALENDAR"; |
| 118 | |
| 119 | return $ical; |
| 120 | } |
| 121 | |
| 122 | protected function escapeText(string $text): string { |
| 123 | return preg_replace("/(\r\n|\n|\r)/", "\\n", $text) ?: ''; |
| 124 | } |
| 125 | } |