Implement reminders for house maintenance
[SonOfLokstallBot.git] / purjolok.php
index eeb2e6d..395f41d 100644 (file)
@@ -118,4 +118,105 @@ getTelegram()->addCommand(
 
 );
 
+getTelegram()->addCommand(
+    new class extends Command {
+        protected $name = 'tasks';
+        protected $description = 'List tasks for this week';
+
+        public function handle($arguments) {
+            $dt = new DateTimeImmutable();
+            $directory = sprintf(
+                "tasks/%s/%s/%s",
+                $dt->format('Y'),
+                $dt->format('F'),
+                $dt->format('W')
+            );
+
+            $mondays = [
+                (int)(new DateTimeImmutable('first monday of this month'))->format('d'),
+                (int)(new DateTimeImmutable('second monday of this month'))->format('d'),
+                (int)(new DateTimeImmutable('third monday of this month'))->format('d'),
+                (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'),
+            ];
+            $closestMonday = closest($dt->format('d'), $mondays);
+
+            $tasksForTheWeek = getTasksForTheWeek(
+                array_search($closestMonday, $mondays),
+                $dt->format('m'),
+                require 'taskMatrix.php'
+            );
+
+            $completedTasksFile = "$directory" . "/completed.txt";
+            $completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : [];
+
+            $stringAndCode = function($string) {
+                return getString($string) . " (" . $string . ")";
+            };
+
+            $this->replyWithMessage([
+                'text' => ununlines([
+                    getString('tasksForTheWeek'),
+                    unlines(map($stringAndCode)(array_diff($tasksForTheWeek, $completedTasks)))
+                ])
+            ]);
+        }
+    }
+);
+
+getTelegram()->addCommand(
+    new class extends Command {
+        protected $name = 'completetask';
+        protected $description = 'Mark a task as completed';
+
+        public function handle($arguments) {
+            $dt = new DateTimeImmutable();
+            $mondays = [
+                (int)(new DateTimeImmutable('first monday of this month'))->format('d'),
+                (int)(new DateTimeImmutable('second monday of this month'))->format('d'),
+                (int)(new DateTimeImmutable('third monday of this month'))->format('d'),
+                (int)(new DateTimeImmutable('fourth monday of this month'))->format('d'),
+            ];
+            $closestMonday = closest($dt->format('d'), $mondays);
+
+            $directory = sprintf(
+                "tasks/%s/%s/%s",
+                $dt->format('Y'),
+                $dt->format('F'),
+                $dt->format('W')
+            );
+
+            $tasksForTheWeek = getTasksForTheWeek(
+                array_search($closestMonday, $mondays),
+                $dt->format('m'),
+                require 'taskMatrix.php'
+            );
+
+            $completedTasksFile = "$directory" . "/completed.txt";
+            $completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : [];
+
+            if (!is_dir($directory)) {
+                mkdir($directory, 0777, true);
+            }
+
+            if (in_array($arguments, $completedTasks)) {
+                $this->replyWithMessage(['text' => getString('taskAlreadyCompleted')]);
+                return;
+            }
+
+            if (!in_array($arguments, $tasksForTheWeek)) {
+                $this->replyWithMessage(['text' => getString('unknownTask')]);
+                return;
+            }
+
+            file_put_contents(
+                $completedTasksFile,
+                "$arguments\n",
+                FILE_APPEND
+            );
+
+            $this->replyWithMessage(['text' => getString('taskCompleted')]);
+        }
+    }
+);
+
 getTelegram()->commandsHandler(true); //must come afterwards because lolzer