ctdo.de/php/util.php

192 lines
6.7 KiB
PHP

<?php
class Util {
function css_link($src) {
return '<link rel="stylesheet" href="'.$src.'">';
}
function generate_nav($active_page, $pages, $page_names) {
$output = '<nav><ul>';
foreach ($pages as $key => $page) {
if ($page == $active_page)
$output .= '<li>'.$this->html_link('/?page='.$page, 'active', $page_names[$key], FALSE).'</li>';
else
$output .= '<li>'.$this->html_link('/?page='.$page, '', $page_names[$key], FALSE).'</li>';
}
$output .= '</ul></nav>';
return $output;
}
function html_link($href, $class, $innerHTML, $blank) {
if($blank)
return '<a href="'.$href.'" class="'.$class.'" target="_blank">'.$innerHTML.'</a>';
else
return '<a href="'.$href.'" class="'.$class.'">'.$innerHTML.'</a>';
}
function raumstatus() {
$url = 'https://status.ctdo.de/api/simple/v2';
$data = json_decode(file_get_contents($url), true);
return $data['state'];
}
function str_mass_replace($searchs, $replacers, $string) {
foreach ($searchs as $key => $search) {
$string = str_replace($search, $replacers[$key], $string);
}
return $string;
}
//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 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 .= '<div class="eventblock"><h3>'.$signatur.'<br>'.$date.'</h3><a href="?page=blog&id='.str_replace('.md', '', $event).'"><h2>'.$title.'</h2><p>'.$desc.'</p></a></div>';
}
} 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 .= '<div class="eventblock"><h3 class="a">'.$signatur.'</h3><h3 class="b">'.$date.'</h3><a href="?page=blog&id='.str_replace('.md', '', $events[$i]).'"><h2>'.$title.'</h2><p>'.$desc.'</p></a></div>';
}
}
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;
}
//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')
$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 .= '<div class="eventblock"><a href="?page=events&id='.str_replace('.md', '', $event).'"><h3 class="a">'.$veranstaltungsort.'</h3><h3 class="b">'.$date.'</h3><h2>'.$title.'</h2><p>'.$desc.'</p></a></div>';
}
} 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 .= '<div class="eventblock"><a href="?page=events&id='.str_replace('.md', '', $event).'"><h3 class="a">'.$veranstaltungsort.'</h3><h3 class="b">'.$date.'</h3><h2>'.$title.'</h2><p>'.$desc.'</p></a></div>';
}
}
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;
$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 + 1;
$output->date = $lastThursday->format('Y-m-d');
return $output;
}
}
?>