From 1bb33f20c197929bf2cc4bf1bcfb59372a7de3b4 Mon Sep 17 00:00:00 2001 From: xoy Date: Sat, 17 Jun 2023 22:41:31 +0200 Subject: [PATCH] =?UTF-8?q?[Projektstruktur=20und=20Code=20aufr=C3=A4umen?= =?UTF-8?q?=20/=20optimieren]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.php | 90 +++---- pages/{404.html.twig => 404.twig} | 0 pages/{about.html.twig => about.twig} | 0 pages/{blog.html.twig => blog.twig} | 0 ...datenschutz.html.twig => datenschutz.twig} | 0 pages/{events.html.twig => events.twig} | 0 pages/{impressum.html.twig => impressum.twig} | 0 pages/{index.html.twig => index.twig} | 0 pages/{kontakt.html.twig => kontakt.twig} | 0 pages/{support.html.twig => support.twig} | 0 pages/{treff.html.twig => treff.twig} | 0 pages/{verein.html.twig => verein.twig} | 0 php/events.php | 129 ++++++++++ php/posts.php | 48 ++++ php/util.php | 229 ++---------------- .../{html5struct.html.twig => default.twig} | 0 templates/nextevents.html.twig | 1 - templates/topictreff.html.twig | 1 - 18 files changed, 234 insertions(+), 264 deletions(-) rename pages/{404.html.twig => 404.twig} (100%) rename pages/{about.html.twig => about.twig} (100%) rename pages/{blog.html.twig => blog.twig} (100%) rename pages/{datenschutz.html.twig => datenschutz.twig} (100%) rename pages/{events.html.twig => events.twig} (100%) rename pages/{impressum.html.twig => impressum.twig} (100%) rename pages/{index.html.twig => index.twig} (100%) rename pages/{kontakt.html.twig => kontakt.twig} (100%) rename pages/{support.html.twig => support.twig} (100%) rename pages/{treff.html.twig => treff.twig} (100%) rename pages/{verein.html.twig => verein.twig} (100%) create mode 100644 php/events.php create mode 100644 php/posts.php rename templates/{html5struct.html.twig => default.twig} (100%) delete mode 100644 templates/nextevents.html.twig delete mode 100644 templates/topictreff.html.twig diff --git a/index.php b/index.php index 787164c..97ccdfa 100644 --- a/index.php +++ b/index.php @@ -1,19 +1,20 @@ 'strip', 'allow_unsafe_links' => false, ]); -$util = new Util(); - $pageloader = new FilesystemLoader(__DIR__ . '/pages'); $templateloader = new FilesystemLoader(__DIR__ . '/templates'); @@ -23,10 +24,7 @@ $twig_templates = new Environment($templateloader); $title = ''; $content = ''; -$topictreff = $twig_templates->render('topictreff.html.twig'); -$nextevents = $twig_templates->render('nextevents.html.twig'); - -$raumstatus_b = $util->raumstatus(); +$raumstatus_b = get_web_json('https://status.ctdo.de/api/simple/v2')["state"]; $raumstatus = 'unbekannt'; $pages = array('index', 'about', 'treff', 'blog', 'events', 'kontakt', 'support', 'verein'); @@ -36,123 +34,99 @@ $active_page = ''; if(gettype($raumstatus_b) == "boolean") { if($raumstatus_b) - $raumstatus = $util->html_link('https://status.ctdo.de/', 'green-text', 'offen', TRUE); + $raumstatus = html_link('https://status.ctdo.de/', 'green-text', 'offen', TRUE); else - $raumstatus = $util->html_link('https://status.ctdo.de/', 'red-text', 'geschlossen', TRUE); + $raumstatus = html_link('https://status.ctdo.de/', 'red-text', 'geschlossen', TRUE); } -$topic = $util->get_next_topic(); +$topic = get_next_topic(); -$treff = $util->get_next_treff(); +$treff = get_next_treff(); -$repaircafe = $util->get_next_repaircafe(); -$repaircafelink = 'RepairCafe'; -switch ($repaircafe->days) { - case 0: - $repaircafe_output = '

Das nächste '.$repaircafelink.' findet heute statt!

'; - break; - case 1: - $repaircafe_output = '

Das nächste '.$repaircafelink.' findet morgen statt!

'; - break; - default: - $repaircafe_output = '

Das nächste '.$repaircafelink.' findet in '.$repaircafe->days.' Tagen ['.$repaircafe->date.'] statt.

'; - break; -} +$repaircafe = get_next_repaircafe(); -$brunch = $util->get_next_brunch(); -$brunchlink = 'ChaosBrunch'; -switch ($brunch->days) { - case 0: - $brunch_output = '

Das nächste '.$brunchlink.' findet heute statt!

'; - break; - case 1: - $brunch_output = '

Das nächste '.$brunchlink.' findet morgen statt!

'; - break; - default: - $brunch_output = '

Das nächste '.$brunchlink.' findet in '.$brunch->days.' Tagen ['.$brunch->date.'] statt.

'; - break; -} +$brunch = get_next_brunch(); $css = ['main']; if(isset($_GET['page'])) { $page = $_GET['page']; - $active_page = $util->str_mass_replace(array('adresse', 'irc', 'mail'), array('kontakt', 'kontakt', 'kontakt'), $page); + $active_page = str_mass_replace(array('adresse', 'irc', 'mail'), array('kontakt', 'kontakt', 'kontakt'), $page); $title = $page_names[array_search($active_page, $pages)]; switch ($page) { case 'index': - $content = $twig_pages->render('index.html.twig', [ 'topic' => $topic_output, 'posts' => $util->generate_post_list(4) ]); + $content = $twig_pages->render('index.twig', [ 'topic' => $topic_output, 'posts' => generate_post_list(4) ]); $css[] = 'home'; $css[] = 'events'; break; case 'about': - $content = $twig_pages->render('about.html.twig'); + $content = $twig_pages->render('about.twig'); $css[] = 'about'; break; case 'treff': - $content = $twig_pages->render('treff.html.twig'); + $content = $twig_pages->render('treff.twig'); break; case 'blog': if(!isset($_GET['id'])) { - $content = $twig_pages->render('blog.html.twig', [ 'posts' => $util->generate_post_list() ]); + $content = $twig_pages->render('blog.twig', [ 'posts' => generate_post_list() ]); $css[] = 'events'; } else { - $content = $converter->convert($util->get_post_content($_GET['id'])); + $content = $converter->convert(get_post_content($_GET['id'])); $css[] = 'event'; } break; case 'events': if(!isset($_GET['id'])) { - $content = $twig_pages->render('events.html.twig', [ 'topic' => $topic, 'treff' => $treff, 'repaircafe' => $repaircafe, 'brunch' => $brunch, 'events' => $util->generate_event_list() ]); + $content = $twig_pages->render('events.twig', [ 'topic' => $topic, 'treff' => $treff, 'repaircafe' => $repaircafe, 'brunch' => $brunch, 'events' => generate_event_list() ]); $css[] = 'events'; } else { - $content = $util->str_mass_replace(array('{{ topicdatum }}', 'Chaostreff Dortmund e.V.', '{{ treffdatum }}', '{{ repaircafedatum }}', '{{ brunchdatum }}'), array($util->get_next_topic()->date, $util->html_link('/?page=treff', '', 'Chaostreff Dortmund e.V.', FALSE), $util->get_next_treff()->date, $util->get_next_repaircafe()->date, $util->get_next_brunch()->date), $converter->convert($util->get_event_content($_GET['id']))); + $content = str_mass_replace(array('{{ topicdatum }}', 'Chaostreff Dortmund e.V.', '{{ treffdatum }}', '{{ repaircafedatum }}', '{{ brunchdatum }}'), array(get_next_topic()->date, html_link('/?page=treff', '', 'Chaostreff Dortmund e.V.', FALSE), get_next_treff()->date, get_next_repaircafe()->date, get_next_brunch()->date), $converter->convert(get_event_content($_GET['id']))); $css[] = 'event'; } break; case 'kontakt': - $content = $twig_pages->render('kontakt.html.twig'); + $content = $twig_pages->render('kontakt.twig'); $css[] = 'kontakt'; break; case 'adresse': - $content = $twig_pages->render('kontakt/adresse.html.twig'); + $content = $twig_pages->render('kontakt/adresse.twig'); break; case 'irc': - $content = $twig_pages->render('kontakt/irc.html.twig'); + $content = $twig_pages->render('kontakt/irc.twig'); break; case 'mail': - $content = $twig_pages->render('kontakt/mail.html.twig'); + $content = $twig_pages->render('kontakt/mail.twig'); break; case 'tel': - $content = $twig_pages->render('kontakt/tel.html.twig'); + $content = $twig_pages->render('kontakt/tel.twig'); break; case 'support': - $content = $twig_pages->render('support.html.twig'); + $content = $twig_pages->render('support.twig'); break; case 'verein': - $content = $twig_pages->render('verein.html.twig'); + $content = $twig_pages->render('verein.twig'); break; case 'impressum': $title = 'impressum'; - $content = $twig_pages->render('impressum.html.twig'); + $content = $twig_pages->render('impressum.twig'); break; case 'datenschutz': $title = 'datenschutz'; - $content = $twig_pages->render('datenschutz.html.twig'); + $content = $twig_pages->render('datenschutz.twig'); break; default: - $content = $twig_pages->render('404.html.twig', ['page' => $_GET['page']]); + $content = $twig_pages->render('404.twig', ['page' => $_GET['page']]); break; } } else { $active_page = 'index'; $title = $active_page; - $content = $twig_pages->render('index.html.twig', [ 'posts' => $util->generate_post_list(4) ]); + $content = $twig_pages->render('index.twig', [ 'posts' => generate_post_list(4) ]); $css[] = 'home'; $css[] = 'events'; } -$nav = $util->generate_nav($active_page, $pages, $page_names); +$nav = generate_nav($active_page, $pages, $page_names); -echo $twig_templates->render('html5struct.html.twig', ['title' => $title, 'css' => $css, 'nav' => $nav, 'raumstatus' => $raumstatus, 'main' => $content]); +echo $twig_templates->render('default.twig', ['title' => $title, 'css' => $css, 'nav' => $nav, 'raumstatus' => $raumstatus, 'main' => $content]); ?> diff --git a/pages/404.html.twig b/pages/404.twig similarity index 100% rename from pages/404.html.twig rename to pages/404.twig diff --git a/pages/about.html.twig b/pages/about.twig similarity index 100% rename from pages/about.html.twig rename to pages/about.twig diff --git a/pages/blog.html.twig b/pages/blog.twig similarity index 100% rename from pages/blog.html.twig rename to pages/blog.twig diff --git a/pages/datenschutz.html.twig b/pages/datenschutz.twig similarity index 100% rename from pages/datenschutz.html.twig rename to pages/datenschutz.twig diff --git a/pages/events.html.twig b/pages/events.twig similarity index 100% rename from pages/events.html.twig rename to pages/events.twig diff --git a/pages/impressum.html.twig b/pages/impressum.twig similarity index 100% rename from pages/impressum.html.twig rename to pages/impressum.twig diff --git a/pages/index.html.twig b/pages/index.twig similarity index 100% rename from pages/index.html.twig rename to pages/index.twig diff --git a/pages/kontakt.html.twig b/pages/kontakt.twig similarity index 100% rename from pages/kontakt.html.twig rename to pages/kontakt.twig diff --git a/pages/support.html.twig b/pages/support.twig similarity index 100% rename from pages/support.html.twig rename to pages/support.twig diff --git a/pages/treff.html.twig b/pages/treff.twig similarity index 100% rename from pages/treff.html.twig rename to pages/treff.twig diff --git a/pages/verein.html.twig b/pages/verein.twig similarity index 100% rename from pages/verein.html.twig rename to pages/verein.twig diff --git a/php/events.php b/php/events.php new file mode 100644 index 0000000..ed2f920 --- /dev/null +++ b/php/events.php @@ -0,0 +1,129 @@ += 4 && $f != 'treff.md' && $f != 'topictreff.md' && $f != 'repaircafe.md' && $f != 'brunch.md') + $output[] = $f; + } + return $output; +} + +function generate_event_list($limit = -1) { + $events = scan_for_events(); + $output = ""; + if ($limit == -1) { + foreach ($events as $event) { + $lines = file(__DIR__ . '/../events/' . $event); + $title = $lines[0]; + $desc = $lines[1]; + $date = $lines[2]; + $veranstaltungsort = $lines[4]; + $output .= '

'.$veranstaltungsort.'

'.$date.'

'.$title.'

'.$desc.'

'; + } + } else { + if($limit > count($events)) + $limit = count($events); + for ($i = 0; $i < $limit; $i++) { + $lines = file(__DIR__ . '/../events/' . $event); + $title = $lines[0]; + $desc = $lines[1]; + $date = $lines[2]; + $veranstaltungsort = $lines[4]; + $output .= '

'.$veranstaltungsort.'

'.$date.'

'.$title.'

'.$desc.'

'; + } + } + return $output; +} + +function get_event_content($id) { + $lines = file(__DIR__ . '/../events/' . str_replace('.', '', $id) . '.md'); + $output = ""; + $output .= '## Datum/Zeit'."\n\n"; + $output .= $lines[2]."\n".$lines[3]."\n\n## Veranstaltungsort\n\n".$lines[4]."\n\n"; + for ($i = 6; $i < count($lines); $i++) + $output .= $lines[$i] . "\n"; + return $output; +} + +function get_next_topic() { + $output = new stdClass(); + $currentDate = new DateTime(); + + $next_topic = clone $currentDate; + $next_topic->modify('second Tuesday of this month +1 week'); + while ($next_topic->format('N') !== '2') { + $next_topic->add(new DateInterval('P1D')); + } + + $output->days = $currentDate->diff($next_topic)->days+1; + $output->date = $next_topic->format('Y-m-d'); + + return $output; +} + +function get_next_treff() { + $output = new stdClass(); + // Get current date and time + $now = new DateTime(); + + // Find the next Friday + $now->modify('next Friday'); + + // Calculate the number of days until the next Friday + $diff = $now->diff(new DateTime()); + $days_until = $diff->format('%a'); + + $output->days = $days_until+1; + $output->date = $now->format('Y-m-d'); + // Return an array with the count and date of the next Friday + return $output; +} + +function get_next_repaircafe() { + $output = new stdClass(); + $today = new DateTime(); + $lastDayOfMonth = clone $today; + $lastDayOfMonth->modify('last day of this month'); + $lastThursday = clone $lastDayOfMonth; + + while ($lastThursday->format('w') != 4) { // Thursday is represented by 4 (0-6, where 0 is Sunday) + $lastThursday->modify('-1 day'); + } + + $daysUntilLastThursday = $today->diff($lastThursday)->days; + + $output->days = $daysUntilLastThursday; + $output->date = $lastThursday->format('Y-m-d'); + + return $output; +} + +function get_next_brunch() { + $now = new DateTime(); + + if ($now->format('w') == 0) { + $nextSunday = clone $now; + } else { + $nextSunday = new DateTime('next Sunday'); + } + + $weekNumber = (int)$nextSunday->format('W'); + $isEvenWeek = ($weekNumber % 2) == 0; + + if ($isEvenWeek) { + $nextSunday->modify('+1 week'); + } + + $differenz = $nextSunday->diff($now); + $days = $differenz->days; + + $output = new stdClass(); + $output->date = $nextSunday->format('Y-m-d'); + $output->days = $days+1; + + return $output; +} + +?> \ No newline at end of file diff --git a/php/posts.php b/php/posts.php new file mode 100644 index 0000000..543d536 --- /dev/null +++ b/php/posts.php @@ -0,0 +1,48 @@ += 4) + $output[] = $f; + } + return $output; +} + +function generate_post_list($limit = -1) { + $events = scan_for_posts(); + $output = ""; + if ($limit == -1) { + foreach ($events as $event) { + $lines = file(__DIR__ . '/../posts/' . $event); + $title = $lines[0]; + $desc = $lines[1]; + $date = $lines[2]; + $signatur = $lines[3]; + $output .= '

'.$signatur.'
'.$date.'

'.$title.'

'.$desc.'

'; + } + } else { + if($limit > count($events)) + $limit = count($events); + for ($i = 0; $i < $limit; $i++) { + $lines = file(__DIR__ . '/../posts/' . $events[$i]); + $title = $lines[0]; + $desc = $lines[1]; + $date = $lines[2]; + $signatur = $lines[3]; + $output .= '

'.$signatur.'

'.$date.'

'.$title.'

'.$desc.'

'; + } + } + return $output; +} + +function get_post_content($id) { + $lines = file(__DIR__ . '/../posts/' . str_replace('.', '', $id) . '.md'); + $output = ""; + for ($i = 5; $i < count($lines); $i++) + $output .= $lines[$i] . "\n"; + return $output; +} + +?> \ No newline at end of file diff --git a/php/util.php b/php/util.php index 7f11f1b..92ae965 100644 --- a/php/util.php +++ b/php/util.php @@ -1,217 +1,38 @@ '; - } +function css_link($src) { + return ''; +} - function generate_nav($active_page, $pages, $page_names) { - $output = array(); - foreach ($pages as $key => $page) { - if ($page == $active_page) - $output[] = ['page' => $page, 'name' => $page_names[$key], 'active' => TRUE]; - else - $output[] = ['page' => $page, 'name' => $page_names[$key], 'active' => FALSE]; - } - - return $output; - } - - function html_link($href, $class, $innerHTML, $blank) { - if($blank) - return ''.$innerHTML.''; +function generate_nav($active_page, $pages, $page_names) { + $output = array(); + foreach ($pages as $key => $page) { + if ($page == $active_page) + $output[] = ['page' => $page, 'name' => $page_names[$key], 'active' => TRUE]; else - return ''.$innerHTML.''; + $output[] = ['page' => $page, 'name' => $page_names[$key], 'active' => FALSE]; } - function raumstatus() { - $url = 'https://status.ctdo.de/api/simple/v2'; - $data = json_decode(file_get_contents($url), true); - return $data['state']; - } + return $output; +} - function str_mass_replace($searchs, $replacers, $string) { - foreach ($searchs as $key => $search) { - $string = str_replace($search, $replacers[$key], $string); - } - return $string; - } +function html_link($href, $class, $innerHTML, $blank) { + if($blank) + return ''.$innerHTML.''; + else + return ''.$innerHTML.''; +} - //Posts - function scan_for_posts() { - $s = scandir(__DIR__ . '/../posts/', SCANDIR_SORT_DESCENDING); - $output = array(); - foreach ($s as $f) { - if(count(str_split($f)) >= 4) - $output[] = $f; - } - return $output; - } +function get_web_json($url) { + $data = json_decode(file_get_contents($url), true); + return $data; +} - function generate_post_list($limit = -1) { - $events = $this->scan_for_posts(); - $output = ""; - if ($limit == -1) { - foreach ($events as $event) { - $lines = file(__DIR__ . '/../posts/' . $event); - $title = $lines[0]; - $desc = $lines[1]; - $date = $lines[2]; - $signatur = $lines[3]; - $output .= '

'.$signatur.'
'.$date.'

'.$title.'

'.$desc.'

'; - } - } else { - if($limit > count($events)) - $limit = count($events); - for ($i = 0; $i < $limit; $i++) { - $lines = file(__DIR__ . '/../posts/' . $events[$i]); - $title = $lines[0]; - $desc = $lines[1]; - $date = $lines[2]; - $signatur = $lines[3]; - $output .= '

'.$signatur.'

'.$date.'

'.$title.'

'.$desc.'

'; - } - } - return $output; +function str_mass_replace($searchs, $replacers, $string) { + foreach ($searchs as $key => $search) { + $string = str_replace($search, $replacers[$key], $string); } - - function get_post_content($id) { - $lines = file(__DIR__ . '/../posts/' . str_replace('.', '', $id) . '.md'); - $output = ""; - for ($i = 5; $i < count($lines); $i++) - $output .= $lines[$i] . "\n"; - return $output; - } - //Posts end - - //Events - function scan_for_events() { - $s = scandir(__DIR__ . '/../events/', SCANDIR_SORT_DESCENDING); - $output = array(); - foreach ($s as $f) { - if(count(str_split($f)) >= 4 && $f != 'treff.md' && $f != 'topictreff.md' && $f != 'repaircafe.md' && $f != 'brunch.md') - $output[] = $f; - } - return $output; - } - - function generate_event_list($limit = -1) { - $events = $this->scan_for_events(); - $output = ""; - if ($limit == -1) { - foreach ($events as $event) { - $lines = file(__DIR__ . '/../events/' . $event); - $title = $lines[0]; - $desc = $lines[1]; - $date = $lines[2]; - $veranstaltungsort = $lines[4]; - $output .= '

'.$veranstaltungsort.'

'.$date.'

'.$title.'

'.$desc.'

'; - } - } else { - if($limit > count($events)) - $limit = count($events); - for ($i = 0; $i < $limit; $i++) { - $lines = file(__DIR__ . '/../events/' . $event); - $title = $lines[0]; - $desc = $lines[1]; - $date = $lines[2]; - $veranstaltungsort = $lines[4]; - $output .= '

'.$veranstaltungsort.'

'.$date.'

'.$title.'

'.$desc.'

'; - } - } - return $output; - } - - function get_event_content($id) { - $lines = file(__DIR__ . '/../events/' . str_replace('.', '', $id) . '.md'); - $output = ""; - $output .= '## Datum/Zeit'."\n\n"; - $output .= $lines[2]."\n".$lines[3]."\n\n## Veranstaltungsort\n\n".$lines[4]."\n\n"; - for ($i = 6; $i < count($lines); $i++) - $output .= $lines[$i] . "\n"; - return $output; - } - //Events end - - function get_next_topic() { - $output = new stdClass(); - $currentDate = new DateTime(); - - $next_topic = clone $currentDate; - $next_topic->modify('second Tuesday of this month +1 week'); - while ($next_topic->format('N') !== '2') { - $next_topic->add(new DateInterval('P1D')); - } - - $output->days = $currentDate->diff($next_topic)->days+1; - $output->date = $next_topic->format('Y-m-d'); - - return $output; - } - - function get_next_treff() { - $output = new stdClass(); - // Get current date and time - $now = new DateTime(); - - // Find the next Friday - $now->modify('next Friday'); - - // Calculate the number of days until the next Friday - $diff = $now->diff(new DateTime()); - $days_until = $diff->format('%a'); - - $output->days = $days_until+1; - $output->date = $now->format('Y-m-d'); - // Return an array with the count and date of the next Friday - return $output; - } - - function get_next_repaircafe() { - $output = new stdClass(); - $today = new DateTime(); - $lastDayOfMonth = clone $today; - $lastDayOfMonth->modify('last day of this month'); - $lastThursday = clone $lastDayOfMonth; - - while ($lastThursday->format('w') != 4) { // Thursday is represented by 4 (0-6, where 0 is Sunday) - $lastThursday->modify('-1 day'); - } - - $daysUntilLastThursday = $today->diff($lastThursday)->days; - - $output->days = $daysUntilLastThursday; - $output->date = $lastThursday->format('Y-m-d'); - - return $output; - } - - function get_next_brunch() { - $now = new DateTime(); - - if ($now->format('w') == 0) { - $nextSunday = clone $now; - } else { - $nextSunday = new DateTime('next Sunday'); - } - - $weekNumber = (int)$nextSunday->format('W'); - $isEvenWeek = ($weekNumber % 2) == 0; - - if ($isEvenWeek) { - $nextSunday->modify('+1 week'); - } - - $differenz = $nextSunday->diff($now); - $days = $differenz->days; - - $output = new stdClass(); - $output->date = $nextSunday->format('Y-m-d'); - $output->days = $days+1; - - return $output; - } - + return $string; } ?> \ No newline at end of file diff --git a/templates/html5struct.html.twig b/templates/default.twig similarity index 100% rename from templates/html5struct.html.twig rename to templates/default.twig diff --git a/templates/nextevents.html.twig b/templates/nextevents.html.twig deleted file mode 100644 index 2ab6af2..0000000 --- a/templates/nextevents.html.twig +++ /dev/null @@ -1 +0,0 @@ - nextevents \ No newline at end of file diff --git a/templates/topictreff.html.twig b/templates/topictreff.html.twig deleted file mode 100644 index f595525..0000000 --- a/templates/topictreff.html.twig +++ /dev/null @@ -1 +0,0 @@ - topictreff \ No newline at end of file