diff --git a/app/Services/TaskScheduler.php b/app/Services/TaskScheduler.php new file mode 100644 index 0000000..b10418b --- /dev/null +++ b/app/Services/TaskScheduler.php @@ -0,0 +1,109 @@ +taskName = $taskName; + $this->command = $command; + $this->interval = $interval; + + return $this->isWindows() ? $this->addWindowsTask() : $this->addLinuxTask(); + } + + public function isTaskExists($taskName = null, $command = null) { + return $this->isWindows() + ? $this->isWindowsTaskExists($taskName) + : $this->isLinuxTaskExists($command); + } + + public function removeTask($taskName = null, $command = null) { + return $this->isWindows() + ? $this->removeWindowsTask($taskName) + : $this->removeLinuxTask($command); + } + + private function addLinuxTask() { + $cronJob = "{$this->interval} {$this->command}"; + if ($this->isLinuxTaskExists($this->command)) { + return "✅ Cron-задача уже установлена."; + } + + exec("crontab -l 2>&1", $output, $return_var); + if ($return_var !== 0) { + $output = []; + } + + $output[] = $cronJob; + file_put_contents("/tmp/my_cron", implode(PHP_EOL, $output) . PHP_EOL); + exec("crontab /tmp/my_cron"); + unlink("/tmp/my_cron"); + + return "✅ Cron-задача добавлена!"; + } + + private function isLinuxTaskExists($command = null) { + exec("crontab -l 2>&1", $output); + foreach ($output as $line) { + if (strpos($line, $command ?? $this->command) !== false) { + return true; + } + } + return false; + } + + private function removeLinuxTask($command = null) { + exec("crontab -l 2>&1", $output, $return_var); + if ($return_var !== 0) { + return "❌ Нет задач для удаления."; + } + + $filteredOutput = array_filter($output, function ($line) use ($command) { + return strpos($line, $command ?? $this->command) === false; + }); + + file_put_contents("/tmp/my_cron", implode(PHP_EOL, $filteredOutput) . PHP_EOL); + exec("crontab /tmp/my_cron"); + unlink("/tmp/my_cron"); + + return "✅ Cron-задача удалена!"; + } + + private function addWindowsTask() { + if ($this->isWindowsTaskExists($this->taskName)) { + return "✅ Задача уже существует в Windows."; + } + + $command = "schtasks /Create /SC MINUTE /MO 1 /TN \"{$this->taskName}\" /TR \"{$this->command}\" /F"; + exec($command, $output, $return_code); + + return ($return_code === 0) ? "✅ Задача добавлена в Windows!" : "❌ Ошибка при добавлении задачи."; + } + + private function isWindowsTaskExists($taskName = null) { + exec("schtasks /Query /TN \"". ($taskName ?? $this->taskName) ."\" 2>&1", $output, $return_var); + return $return_var === 0; + } + + private function removeWindowsTask($taskName = null) { + if (!$this->isWindowsTaskExists($taskName)) { + return "❌ Задача не найдена в Windows."; + } + + exec("schtasks /Delete /TN \"". ($taskName ?? $this->taskName) ."\" /F", $output, $return_code); + return ($return_code === 0) ? "✅ Задача удалена из Windows!" : "❌ Ошибка при удалении задачи."; + } +} +?> \ No newline at end of file diff --git a/views/components/AdminSidebar.php b/views/components/AdminSidebar.php index a9de289..f980dd8 100644 --- a/views/components/AdminSidebar.php +++ b/views/components/AdminSidebar.php @@ -68,10 +68,10 @@ body { Новости сайта - + Сущности diff --git a/views/pages/Admin/Contests.php b/views/pages/Admin/Contests.php new file mode 100644 index 0000000..1eb66c9 --- /dev/null +++ b/views/pages/Admin/Contests.php @@ -0,0 +1,264 @@ +isTaskExists("FinishContests", "php /path/to/finish_contests.php")) { + $contestCreate = false; +} +?> + + + + +

Фотоконкурсы

+
+
+ +
+
+
+
+ Провести новый + + addTask( + "FinishContests", + "php ".$_SERVER['DOCUMENT_ROOT']."/app/Controllers/Exec/Tasks/FinishContests.php >> ".$_SERVER['DOCUMENT_ROOT'].NGALLERY['root']['logslocation']." 2>&1", + "* * * * *" // Каждую минуту (можно менять) + );*/ + if ($contestCreate === false) { + echo ""; + } + echo $task->removeTask("FinishContests", "php /path/to/finish_contests.php"); + + ?> + + + + + + + + + + + + $t['themeid']))[0]['title']; + if ($t['status'] === 0) { + $status = 'Ещё не проведён'; + } else if ($t['status'] === 1) { + $status = 'Отбор кандидатов'; + } else if ($t['status'] === 2) { + $status = 'Отбор победителей'; + } else if ($t['status'] === 3) { + $status = 'Завершён'; + } else { + $status = 'Сбой'; + } + echo ' + + + + + + + + '; + } + ?> + +
IDТемаДата начала отбораДата конца отбораДата началаДата концаСтатус
' . $t['id'] . '' . $themetitle . '' . Date::zmdate($t['openpretendsdate']) . '' . Date::zmdate($t['closepretendsdate']) . '' . Date::zmdate($t['opendate']) . '' . Date::zmdate($t['closedate']) . '' . $status . '
+
+
+ + + + + + + + + + + \ No newline at end of file diff --git a/views/pages/Admin/Photo.php b/views/pages/Admin/Photo.php index bc1ebdb..e294cb3 100644 --- a/views/pages/Admin/Photo.php +++ b/views/pages/Admin/Photo.php @@ -39,10 +39,10 @@ use \App\Models\User;