2024.8.23
Loading...
Searching...
No Matches
class.action.checkVersion.php
Go to the documentation of this file.
1<?php
2/*
3 * Copyright (c) 2021-2024 Bearsampp
4 * License: GNU General Public License version 3 or later; see LICENSE.txt
5 * Author: Bear
6 * Website: https://bearsampp.com
7 * Github: https://github.com/Bearsampp
8 */
9
10/**
11 * Class ActionCheckVersion
12 *
13 * This class is responsible for checking the current version of the application and displaying a window
14 * with the latest version information if an update is available. It also handles the user interaction with
15 * the window, such as clicking on links or buttons.
16 *
17 * @package Bearsampp
18 */
20{
21 const DISPLAY_OK = 'displayOk';
22
23 private $wbWindow;
24 private $wbImage;
26 private $wbLinkFull;
27 private $wbBtnOk;
28
32
33 /**
34 * Constructor for the ActionCheckVersion class.
35 *
36 * @param array $args Command line arguments passed to the script.
37 */
38 public function __construct($args)
39 {
40 global $bearsamppCore, $bearsamppLang, $bearsamppWinbinder, $appGithubHeader;
41
42 if (!file_exists($bearsamppCore->getExec())) {
44 $this->currentVersion = $bearsamppCore->getAppVersion();
45
46 // Assuming getLatestVersion now returns an array with version and URL
48
49 if ($githubVersionData != null && isset($githubVersionData['version'], $githubVersionData['html_url'])) {
51 $this->githubLatestVersionUrl = $githubVersionData['html_url']; // URL of the latest version
52 if (version_compare($this->currentVersion, $githubLatestVersion, '<')) {
53 $this->showVersionUpdateWindow($bearsamppLang, $bearsamppWinbinder, $bearsamppCore, $githubLatestVersion);
54 } elseif (!empty($args[0]) && $args[0] == self::DISPLAY_OK) {
55 $this->showVersionOkMessageBox($bearsamppLang, $bearsamppWinbinder);
56 }
57 }
58 }
59 }
60
61 /**
62 * Displays a window with the latest version information.
63 *
64 * @param Lang $lang Language processor instance.
65 * @param WinBinder $winbinder WinBinder instance for creating windows and controls.
66 * @param Core $core Core instance for accessing application resources.
67 * @param string $githubLatestVersion The latest version available on GitHub.
68 */
69 private function showVersionUpdateWindow($lang, $winbinder, $core, $githubLatestVersion)
70 {
71 $labelFullLink = $lang->getValue(Lang::DOWNLOAD) . ' ' . APP_TITLE . ' ' . $githubLatestVersion;
72
73 $winbinder->reset();
74 $this->wbWindow = $winbinder->createAppWindow($lang->getValue(Lang::CHECK_VERSION_TITLE), 380, 170, WBC_NOTIFY, WBC_KEYDOWN | WBC_KEYUP);
75
76 $winbinder->createLabel($this->wbWindow, $lang->getValue(Lang::CHECK_VERSION_AVAILABLE_TEXT), 80, 35, 370, 120);
77
78 $this->wbLinkFull = $winbinder->createHyperLink($this->wbWindow, $labelFullLink, 80, 87, 200, 20, WBC_LINES | WBC_RIGHT);
79
80 $this->wbBtnOk = $winbinder->createButton($this->wbWindow, $lang->getValue(Lang::BUTTON_OK), 280, 103);
81 $this->wbImage = $winbinder->drawImage($this->wbWindow, $core->getResourcesPath() . '/homepage/img/about.bmp');
82
84 $winbinder->setHandler($this->wbWindow, $this, 'processWindow');
85 $winbinder->mainLoop();
86 $winbinder->reset();
87 }
88
89 /**
90 * Displays a message box indicating that the current version is the latest.
91 *
92 * @param Lang $lang Language processor instance.
93 * @param WinBinder $winbinder WinBinder instance for creating windows and controls.
94 */
95 private function showVersionOkMessageBox($lang, $winbinder)
96 {
98 $winbinder->messageBoxInfo(
99 $lang->getValue(Lang::CHECK_VERSION_LATEST_TEXT),
100 $lang->getValue(Lang::CHECK_VERSION_TITLE)
101 );
102 }
103
104 /**
105 * Processes window events and handles user interactions.
106 *
107 * @param resource $window The window resource.
108 * @param int $id The control ID that triggered the event.
109 * @param resource $ctrl The control resource.
110 * @param mixed $param1 Additional parameter 1.
111 * @param mixed $param2 Additional parameter 2.
112 */
113 public function processWindow($window, $id, $ctrl, $param1, $param2)
114 {
115 global $bearsamppConfig, $bearsamppWinbinder;
116
117 switch ($id) {
118 case $this->wbLinkFull[WinBinder::CTRL_ID]:
119 $latestVersionInfo = Util::getLatestVersion(APP_GITHUB_LATEST_URL);
120 if ($latestVersionInfo && isset($latestVersionInfo['html_url'])) {
121 $browserPath = $bearsamppConfig->getBrowser();
122 if (!$bearsamppWinbinder->exec($browserPath, $latestVersionInfo['html_url'])) {
123 Util::logError("Failed to open browser at path: $browserPath with URL: " . $latestVersionInfo['html_url']);
124 }
125 } else {
126 Util::logError("Failed to retrieve latest version info or 'html_url' not set.");
127 }
128 break;
129 case IDCLOSE:
130 case $this->wbBtnOk[WinBinder::CTRL_ID]:
131 $bearsamppWinbinder->destroyWindow($window);
132 break;
133 default:
134 Util::logError("Unhandled window control ID: $id");
135 }
136 }
137}
global $bearsamppLang
global $githubVersionData
$githubLatestVersion
global $bearsamppCore
processWindow($window, $id, $ctrl, $param1, $param2)
showVersionOkMessageBox($lang, $winbinder)
showVersionUpdateWindow($lang, $winbinder, $core, $githubLatestVersion)
const CHECK_VERSION_LATEST_TEXT
const DOWNLOAD
const CHECK_VERSION_AVAILABLE_TEXT
const BUTTON_OK
const CHECK_VERSION_TITLE
static stopLoading()
static logError($data, $file=null)
static getLatestVersion($url)
static startLoading()
global $bearsamppConfig
Definition homepage.php:26
const APP_GITHUB_LATEST_URL
Definition root.php:18
const APP_TITLE
Definition root.php:12