Center align week tabs, and misc cleanup
[SonOfLokstallBot.git] / src / bootstrap.php
1 <?php declare(strict_types=1);
2
3 require_once('common.php');
4
5 function tabs($id, $headings, $contents, $activeHeading = null, $justify = false) {
6 $slug = function($string) {
7 return str_replace(' ', '', $string);
8 };
9
10 $heading = function($heading, $tabid, $active = false) {
11 return '<li class="nav-item"><a class="nav-link' . ($active ? ' active' : '') . '" data-toggle="pill" href="#tab' . $tabid . '">' . $heading . '</a></li>';
12 };
13
14 $content = function($content, $id, $active = false) {
15 return '<div class="container-fluid drivers-main tab-pane fade' . ($active ? ' active show' : '') . '" id="tab' . $id . '" role="tabpanel">' . $content . '</div>';
16 };
17
18 $headingAndContent = zipWith(function($heading, $content) {
19 return ['heading' => $heading, 'content' => $content];
20 }, $headings, $contents);
21
22 $smushenated = array_reduce($headingAndContent, function($c, $v) use ($slug, $heading, $content, $id, $activeHeading) {
23 return [
24 'headings' => $c['headings'] . $heading($v['heading'], $id . $slug($v['heading']), $v['heading'] == $activeHeading),
25 'contents' => $c['contents'] . $content($v['content'], $id . $slug($v['heading']), $v['heading'] == $activeHeading)
26 ];
27 }, ['headings' => '', 'contents' => '']);
28
29 return '<ul class="nav nav-pills' . ($justify ? ' justify-content-center' : '') . '">' . $smushenated['headings'] . '</ul><div class="tab-content">' . $smushenated['contents'] . '</div>';
30 }
31
32 function accordion($id, $headings, $contents, $activeHeading = null) {
33 $slug = function($string) {
34 return str_replace(' ', '', strtolower($string));
35 };
36
37 $a = zipWith(function($heading, $content) use ($slug, $id, $activeHeading) {
38 return
39 '<div class="card">' .
40 '<div class="card-header">' .
41 '<a class="collapsed card-link" data-toggle="collapse" href="#' . $slug($heading) . $id . '">' . $heading . '</a>' .
42 '</div>' .
43 '<div id="' . $slug($heading) . $id . '" class="collapse' . ($heading == $activeHeading ? ' show' : '') . '" data-parent="#accordion-' . $id . '">' .
44 '<div class="card-body">' .
45 $content .
46 '</div></div></div>';
47
48
49 }, $headings, $contents);
50
51 return
52 '<div id="accordion-' . $id .'">' .
53 glue('')($a) .
54 '</div>';
55 }
56
57 function table($rows) {
58 return
59 '<table class="table table-striped"><tbody><tr>' .
60 glue('</tr><tr>')(map(function($row) {
61 return '<td>' . glue('</td><td>')($row) . '</td>';
62 })($rows)) .
63 '</tr></tbody></table>';
64 }
65
66
67 function doIt() {
68 $years = array_reverse(range(2018, (int)(new DateTimeImmutable())->format('Y')));
69 $today = turnBackTime(new DateTimeImmutable);
70 return tabs(
71 'year',
72 $years,
73 map(function($year) use ($today) {
74 return accordion(
75 $year,
76 map(('strToUpper', getMonthName))(range(1,12)),
77 map(function($month) use ($year, $today){
78 $mondays = range(1, count(getMondaysForMonth($year, $month)));
79 return tabs(
80 $year . $month,
81 map(function($num) { return "Week $num"; })($mondays),
82 map(function($num) use ($month, $year) {
83 $completedTasksFile = getFilePathForWeek($year, $month, $num, PATH_TO_TASKS_FILES);
84 $completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : [];
85 return table(map(function($task) use($completedTasks) { return [getString($task), in_array($task, $completedTasks) ? '👍😄' : '👎😱']; })(getTasksForTheWeek($year, $month, $num, require 'taskMatrix.php')));
86 })($mondays),
87 ($year == $today->year && $month == $today->month) ? 'Week ' . $today->weekNum : 'Week 1',
88 true
89 );
90 })(range(1,12)),
91 ($year == $today->year) ? strToUpper(getMonthName($today->month)) : 'JANUARY'
92 );
93 })($years),
94 $today->year,
95 true
96 );
97 }