2024.8.23
Loading...
Searching...
No Matches
class.tpl.app.postgresql.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 TplAppPostgresql
12 *
13 * This class provides methods to generate menu items and actions for managing PostgreSQL services
14 * within the Bearsampp application. It includes functionalities for enabling/disabling PostgreSQL,
15 * switching versions, changing ports, changing root passwords, and managing services.
16 */
18{
19 const MENU = 'postgresql';
20 const MENU_VERSIONS = 'postgresqlVersions';
21 const MENU_SERVICE = 'postgresqlService';
22 const MENU_DEBUG = 'postgresqlDebug';
23
24 const ACTION_ENABLE = 'enablePostgresql';
25 const ACTION_SWITCH_VERSION = 'switchPostgresqlVersion';
26 const ACTION_CHANGE_PORT = 'changePostgresqlPort';
27 const ACTION_CHANGE_ROOT_PWD = 'changePostgresqlRootPwd';
28 const ACTION_INSTALL_SERVICE = 'installPostgresqlService';
29 const ACTION_REMOVE_SERVICE = 'removePostgresqlService';
30
31 /**
32 * Generates the main PostgreSQL menu with options to enable/disable PostgreSQL and access submenus.
33 *
34 * @global object $bearsamppLang Provides language support for retrieving language-specific values.
35 * @global object $bearsamppBins Provides access to system binaries and their configurations.
36 *
37 * @return array The generated menu items and actions for PostgreSQL.
38 */
39 public static function process()
40 {
42
43 return TplApp::getMenuEnable($bearsamppLang->getValue(Lang::POSTGRESQL), self::MENU, get_called_class(), $bearsamppBins->getPostgresql()->isEnable());
44 }
45
46 /**
47 * Generates the PostgreSQL menu with options for versions, service, debug, and console access.
48 *
49 * @global object $bearsamppBins Provides access to system binaries and their configurations.
50 * @global object $bearsamppLang Provides language support for retrieving language-specific values.
51 * @global object $bearsamppTools Provides access to various tools and utilities.
52 *
53 * @return string The generated menu items and actions for PostgreSQL.
54 */
55 public static function getMenuPostgresql()
56 {
57 global $bearsamppBins, $bearsamppLang, $bearsamppTools;
58 $resultItems = $resultActions = '';
59
60 $isEnabled = $bearsamppBins->getPostgresql()->isEnable();
61
62 // Download
63 $resultItems .= TplAestan::getItemLink(
65 Util::getWebsiteUrl('module/postgresql', '#releases'),
66 false,
68 ) . PHP_EOL;
69
70 // Enable
71 $tplEnable = TplApp::getActionMulti(
72 self::ACTION_ENABLE, array($isEnabled ? Config::DISABLED : Config::ENABLED),
73 array($bearsamppLang->getValue(Lang::MENU_ENABLE), $isEnabled ? TplAestan::GLYPH_CHECK : ''),
74 false, get_called_class()
75 );
76 $resultItems .= $tplEnable[TplApp::SECTION_CALL] . PHP_EOL;
77 $resultActions .= $tplEnable[TplApp::SECTION_CONTENT] . PHP_EOL;
78
79 if ($isEnabled) {
80 $resultItems .= TplAestan::getItemSeparator() . PHP_EOL;
81
82 // Versions
83 $tplVersions = TplApp::getMenu($bearsamppLang->getValue(Lang::VERSIONS), self::MENU_VERSIONS, get_called_class());
84 $resultItems .= $tplVersions[TplApp::SECTION_CALL] . PHP_EOL;
85 $resultActions .= $tplVersions[TplApp::SECTION_CONTENT] . PHP_EOL;
86
87 // Service
88 $tplService = TplApp::getMenu($bearsamppLang->getValue(Lang::SERVICE), self::MENU_SERVICE, get_called_class());
89 $resultItems .= $tplService[TplApp::SECTION_CALL] . PHP_EOL;
90 $resultActions .= $tplService[TplApp::SECTION_CONTENT] . PHP_EOL;
91
92 // Debug
93 $tplDebug = TplApp::getMenu($bearsamppLang->getValue(Lang::DEBUG), self::MENU_DEBUG, get_called_class());
94 $resultItems .= $tplDebug[TplApp::SECTION_CALL] . PHP_EOL;
95 $resultActions .= $tplDebug[TplApp::SECTION_CONTENT];
96
97 // Console
98 $resultItems .= TplAestan::getItemConsoleZ(
101 $bearsamppTools->getConsoleZ()->getTabTitlePostgresql()
102 ) . PHP_EOL;
103
104 // Conf
105 $resultItems .= TplAestan::getItemNotepad(basename($bearsamppBins->getPostgresql()->getConf()), $bearsamppBins->getPostgresql()->getConf()) . PHP_EOL;
106
107 // Errors log
108 $resultItems .= TplAestan::getItemNotepad($bearsamppLang->getValue(Lang::MENU_ERROR_LOGS), $bearsamppBins->getPostgresql()->getErrorLog()) . PHP_EOL;
109 }
110
111 return $resultItems . PHP_EOL . $resultActions;
112 }
113
114 /**
115 * Generates the PostgreSQL versions menu with options to switch between different versions.
116 *
117 * @global object $bearsamppBins Provides access to system binaries and their configurations.
118 *
119 * @return string The generated menu items and actions for PostgreSQL versions.
120 */
121 public static function getMenuPostgresqlVersions()
122 {
123 global $bearsamppBins;
124 $items = '';
125 $actions = '';
126
127 foreach ($bearsamppBins->getPostgresql()->getVersionList() as $version) {
128 $tplSwitchPostgresqlVersion = TplApp::getActionMulti(
129 self::ACTION_SWITCH_VERSION, array($version),
130 array($version, $version == $bearsamppBins->getPostgresql()->getVersion() ? TplAestan::GLYPH_CHECK : ''),
131 false, get_called_class()
132 );
133
134 // Item
135 $items .= $tplSwitchPostgresqlVersion[TplApp::SECTION_CALL] . PHP_EOL;
136
137 // Action
138 $actions .= PHP_EOL . $tplSwitchPostgresqlVersion[TplApp::SECTION_CONTENT];
139 }
140
141 return $items . $actions;
142 }
143
144 /**
145 * Generates the action to enable or disable PostgreSQL.
146 *
147 * @global object $bearsamppBins Provides access to system binaries and their configurations.
148 *
149 * @param int $enable The flag to enable (1) or disable (0) PostgreSQL.
150 * @return string The generated action to enable or disable PostgreSQL.
151 */
152 public static function getActionEnablePostgresql($enable)
153 {
154 global $bearsamppBins;
155
156 return TplApp::getActionRun(Action::ENABLE, array($bearsamppBins->getPostgresql()->getName(), $enable)) . PHP_EOL .
158 }
159
160 /**
161 * Generates the action to switch the PostgreSQL version.
162 *
163 * @global object $bearsamppBins Provides access to system binaries and their configurations.
164 *
165 * @param string $version The version to switch to.
166 * @return string The generated action to switch the PostgreSQL version.
167 */
168 public static function getActionSwitchPostgresqlVersion($version)
169 {
170 global $bearsamppBins;
171
172 return TplApp::getActionRun(Action::SWITCH_VERSION, array($bearsamppBins->getPostgresql()->getName(), $version)) . PHP_EOL .
174 }
175
176 /**
177 * Generates the PostgreSQL service menu with options to start, stop, restart, and manage the service.
178 *
179 * @global object $bearsamppLang Provides language support for retrieving language-specific values.
180 * @global object $bearsamppBins Provides access to system binaries and their configurations.
181 *
182 * @return string The generated menu items and actions for PostgreSQL service.
183 */
184 public static function getMenuPostgresqlService()
185 {
187
188 $tplChangePort = TplApp::getActionMulti(
189 self::ACTION_CHANGE_PORT, null,
191 false, get_called_class()
192 );
193
194 $isInstalled = $bearsamppBins->getPostgresql()->getService()->isInstalled();
195
196 $result = TplAestan::getItemActionServiceStart($bearsamppBins->getPostgresql()->getService()->getName()) . PHP_EOL .
197 TplAestan::getItemActionServiceStop($bearsamppBins->getPostgresql()->getService()->getName()) . PHP_EOL .
198 TplAestan::getItemActionServiceRestart($bearsamppBins->getPostgresql()->getService()->getName()) . PHP_EOL .
199 TplAestan::getItemSeparator() . PHP_EOL .
201 Action::CHECK_PORT, array($bearsamppBins->getPostgresql()->getName(), $bearsamppBins->getPostgresql()->getPort()),
202 array(sprintf($bearsamppLang->getValue(Lang::MENU_CHECK_PORT), $bearsamppBins->getPostgresql()->getPort()), TplAestan::GLYPH_LIGHT)
203 ) . PHP_EOL .
204 $tplChangePort[TplApp::SECTION_CALL] . PHP_EOL;
205
206 $tplChangeRootPwd = null;
207 if ($isInstalled) {
208 $tplChangeRootPwd = TplApp::getActionMulti(
209 self::ACTION_CHANGE_ROOT_PWD, null,
211 !$isInstalled, get_called_class()
212 );
213
214 $result .= $tplChangeRootPwd[TplApp::SECTION_CALL] . PHP_EOL;
215 }
216
217 if (!$isInstalled) {
218 $tplInstallService = TplApp::getActionMulti(
219 self::ACTION_INSTALL_SERVICE, null,
221 $isInstalled, get_called_class()
222 );
223
224 $result .= $tplInstallService[TplApp::SECTION_CALL] . PHP_EOL . PHP_EOL .
225 $tplInstallService[TplApp::SECTION_CONTENT] . PHP_EOL;
226 } else {
227 $tplRemoveService = TplApp::getActionMulti(
228 self::ACTION_REMOVE_SERVICE, null,
230 !$isInstalled, get_called_class()
231 );
232
233 $result .= $tplRemoveService[TplApp::SECTION_CALL] . PHP_EOL . PHP_EOL .
234 $tplRemoveService[TplApp::SECTION_CONTENT] . PHP_EOL;
235 }
236
237 $result .= $tplChangePort[TplApp::SECTION_CONTENT] . PHP_EOL .
238 ($tplChangeRootPwd != null ? $tplChangeRootPwd[TplApp::SECTION_CONTENT] . PHP_EOL : '');
239
240 return $result;
241 }
242
243 /**
244 * Generates the PostgreSQL debug menu with options to run various debug commands.
245 *
246 * @global object $bearsamppLang Provides language support for retrieving language-specific values.
247 *
248 * @return string The generated menu items and actions for PostgreSQL debug.
249 */
250 public static function getMenuPostgresqlDebug()
251 {
252 global $bearsamppLang;
253
257 ) . PHP_EOL;
258 }
259
260 /**
261 * Generates the action to change the PostgreSQL port.
262 *
263 * @global object $bearsamppBins Provides access to system binaries and their configurations.
264 *
265 * @return string The generated action to change the PostgreSQL port.
266 */
267 public static function getActionChangePostgresqlPort()
268 {
269 global $bearsamppBins;
270
271 return TplApp::getActionRun(Action::CHANGE_PORT, array($bearsamppBins->getPostgresql()->getName())) . PHP_EOL .
273 }
274
275 /**
276 * Generates the action to change the PostgreSQL root password.
277 *
278 * @global object $bearsamppBins Provides access to system binaries and their configurations.
279 *
280 * @return string The generated action to change the PostgreSQL root password.
281 */
282 public static function getActionChangePostgresqlRootPwd()
283 {
284 global $bearsamppBins;
285
286 return TplApp::getActionRun(Action::CHANGE_DB_ROOT_PWD, array($bearsamppBins->getPostgresql()->getName())) . PHP_EOL .
288 }
289
290 /**
291 * Generates the action to install the PostgreSQL service.
292 *
293 * @return string The generated action to install the PostgreSQL service.
294 */
300
301 /**
302 * Generates the action to remove the PostgreSQL service.
303 *
304 * @return string The generated action to remove the PostgreSQL service.
305 */
311}
$result
global $bearsamppBins
global $bearsamppLang
const CHANGE_PORT
const CHECK_PORT
const ENABLE
const DEBUG_POSTGRESQL
const SWITCH_VERSION
const SERVICE
const CHANGE_DB_ROOT_PWD
const DISABLED
const ENABLED
const MENU_ERROR_LOGS
const VERSIONS
const MENU_INSTALL_SERVICE
const MENU_CHANGE_ROOT_PWD
const DEBUG
const MENU_ENABLE
const DEBUG_POSTGRESQL_VERSION
const DOWNLOAD_MORE
const MENU_REMOVE_SERVICE
const POSTGRESQL
const CONSOLE
const MENU_CHECK_PORT
const SERVICE
const MENU_CHANGE_PORT
const GLYPH_SERVICE_REMOVE
static getItemActionServiceStop($service)
static getItemSeparator()
static getItemActionServiceStart($service)
static getItemConsoleZ($caption, $glyph, $id=null, $title=null, $initDir=null, $command=null)
static getItemActionServiceRestart($service)
static getItemLink($caption, $link, $local=false, $glyph=self::GLYPH_WEB_PAGE)
const GLYPH_SERVICE_INSTALL
static getItemNotepad($caption, $path)
static getActionEnablePostgresql($enable)
static getActionSwitchPostgresqlVersion($version)
static getActionMulti($action, $args=array(), $item=array(), $disabled=false, $class=false)
const SECTION_CALL
static getMenuEnable($caption, $menu, $class, $enabled=true)
const SECTION_CONTENT
static getActionRun($action, $args=array(), $item=array(), $waitUntilTerminated=true)
static getMenu($caption, $menu, $class)
static getWebsiteUrl($path='', $fragment='', $utmSource=true)