2024.8.23
Loading...
Searching...
No Matches
class.action.addAlias.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 ActionAddAlias
12 * Handles the creation of a new alias in the Bearsampp application.
13 */
15{
16 private $wbWindow;
17 private $wbLabelName;
18 private $wbInputName;
19 private $wbLabelDest;
20 private $wbInputDest;
21 private $wbBtnDest;
22 private $wbLabelExp;
24 private $wbBtnSave;
25 private $wbBtnCancel;
26
27 const GAUGE_SAVE = 2;
28
29 /**
30 * ActionAddAlias constructor.
31 * Initializes the window and its controls for adding a new alias.
32 *
33 * @param array $args Arguments passed to the constructor.
34 */
35 public function __construct($args)
36 {
37 global $bearsamppLang, $bearsamppBins, $bearsamppWinbinder;
38
39 $initName = 'test';
40 $initDest = 'C:\\';
41 $apachePortUri = $bearsamppBins->getApache()->getPort() != 80 ? ':' . $bearsamppBins->getApache()->getPort() : '';
42
43 $bearsamppWinbinder->reset();
44 $this->wbWindow = $bearsamppWinbinder->createAppWindow($bearsamppLang->getValue(Lang::ADD_ALIAS_TITLE), 490, 200, WBC_NOTIFY, WBC_KEYDOWN | WBC_KEYUP);
45
46 $this->wbLabelName = $bearsamppWinbinder->createLabel($this->wbWindow, $bearsamppLang->getValue(Lang::ALIAS_NAME_LABEL) . ' :', 15, 15, 85, null, WBC_RIGHT);
47 $this->wbInputName = $bearsamppWinbinder->createInputText($this->wbWindow, $initName, 105, 13, 150, null);
48
49 $this->wbLabelDest = $bearsamppWinbinder->createLabel($this->wbWindow, $bearsamppLang->getValue(Lang::ALIAS_DEST_LABEL) . ' :', 15, 45, 85, null, WBC_RIGHT);
50 $this->wbInputDest = $bearsamppWinbinder->createInputText($this->wbWindow, $initDest, 105, 43, 190, null, null, WBC_READONLY);
51 $this->wbBtnDest = $bearsamppWinbinder->createButton($this->wbWindow, $bearsamppLang->getValue(Lang::BUTTON_BROWSE), 300, 43, 110);
52
53 $this->wbLabelExp = $bearsamppWinbinder->createLabel($this->wbWindow, sprintf($bearsamppLang->getValue(Lang::ALIAS_EXP_LABEL), $apachePortUri, $initName, $initDest), 15, 80, 470, 50);
54
55 $this->wbProgressBar = $bearsamppWinbinder->createProgressBar($this->wbWindow, self::GAUGE_SAVE + 1, 15, 137, 275);
56 $this->wbBtnSave = $bearsamppWinbinder->createButton($this->wbWindow, $bearsamppLang->getValue(Lang::BUTTON_SAVE), 300, 132);
57 $this->wbBtnCancel = $bearsamppWinbinder->createButton($this->wbWindow, $bearsamppLang->getValue(Lang::BUTTON_CANCEL), 387, 132);
58
59 $bearsamppWinbinder->setHandler($this->wbWindow, $this, 'processWindow');
60 $bearsamppWinbinder->mainLoop();
61 $bearsamppWinbinder->reset();
62 }
63
64 /**
65 * Processes window events and handles user interactions.
66 *
67 * @param resource $window The window resource.
68 * @param int $id The control ID.
69 * @param resource $ctrl The control resource.
70 * @param mixed $param1 Additional parameter 1.
71 * @param mixed $param2 Additional parameter 2.
72 */
73 public function processWindow($window, $id, $ctrl, $param1, $param2)
74 {
75 global $bearsamppRoot, $bearsamppBins, $bearsamppLang, $bearsamppWinbinder;
76
77 $apachePortUri = $bearsamppBins->getApache()->getPort() != 80 ? ':' . $bearsamppBins->getApache()->getPort() : '';
78 $aliasName = $bearsamppWinbinder->getText($this->wbInputName[WinBinder::CTRL_OBJ]);
79 $aliasDest = $bearsamppWinbinder->getText($this->wbInputDest[WinBinder::CTRL_OBJ]);
80
81 switch ($id) {
82 case $this->wbInputName[WinBinder::CTRL_ID]:
83 $bearsamppWinbinder->setText(
84 $this->wbLabelExp[WinBinder::CTRL_OBJ],
85 sprintf($bearsamppLang->getValue(Lang::ALIAS_EXP_LABEL), $apachePortUri, $aliasName, $aliasDest)
86 );
87 $bearsamppWinbinder->setEnabled($this->wbBtnSave[WinBinder::CTRL_OBJ], empty($aliasName) ? false : true);
88 break;
89 case $this->wbBtnDest[WinBinder::CTRL_ID]:
90 $aliasDest = $bearsamppWinbinder->sysDlgPath($window, $bearsamppLang->getValue(Lang::ALIAS_DEST_PATH), $aliasDest);
91 if ($aliasDest && is_dir($aliasDest)) {
92 $bearsamppWinbinder->setText($this->wbInputDest[WinBinder::CTRL_OBJ], $aliasDest . '\\');
93 $bearsamppWinbinder->setText(
94 $this->wbLabelExp[WinBinder::CTRL_OBJ],
95 sprintf($bearsamppLang->getValue(Lang::ALIAS_EXP_LABEL), $apachePortUri, $aliasName, $aliasDest . '\\')
96 );
97 }
98 break;
99 case $this->wbBtnSave[WinBinder::CTRL_ID]:
100 $bearsamppWinbinder->setProgressBarMax($this->wbProgressBar, self::GAUGE_SAVE + 1);
101 $bearsamppWinbinder->incrProgressBar($this->wbProgressBar);
102
103 if (!ctype_alnum($aliasName)) {
104 $bearsamppWinbinder->messageBoxError(
105 sprintf($bearsamppLang->getValue(Lang::ALIAS_NOT_VALID_ALPHA), $aliasName),
107 $bearsamppWinbinder->resetProgressBar($this->wbProgressBar);
108 break;
109 }
110
111 if (is_file($bearsamppRoot->getAliasPath() . '/' . $aliasName . '.conf')) {
112 $bearsamppWinbinder->messageBoxError(
113 sprintf($bearsamppLang->getValue(Lang::ALIAS_ALREADY_EXISTS), $aliasName),
115 $bearsamppWinbinder->resetProgressBar($this->wbProgressBar);
116 break;
117 }
118 if (file_put_contents($bearsamppRoot->getAliasPath() . '/' . $aliasName . '.conf', $bearsamppBins->getApache()->getAliasContent($aliasName, $aliasDest)) !== false) {
119 $bearsamppWinbinder->incrProgressBar($this->wbProgressBar);
120
121 $bearsamppBins->getApache()->getService()->restart();
122 $bearsamppWinbinder->incrProgressBar($this->wbProgressBar);
123
124 $bearsamppWinbinder->messageBoxInfo(
125 sprintf($bearsamppLang->getValue(Lang::ALIAS_CREATED), $aliasName, $apachePortUri, $aliasName, $aliasDest),
127 $bearsamppWinbinder->destroyWindow($window);
128 } else {
129 $bearsamppWinbinder->messageBoxError($bearsamppLang->getValue(Lang::ALIAS_CREATED_ERROR), $bearsamppLang->getValue(Lang::ADD_ALIAS_TITLE));
130 $bearsamppWinbinder->resetProgressBar($this->wbProgressBar);
131 }
132 break;
133 case IDCLOSE:
134 case $this->wbBtnCancel[WinBinder::CTRL_ID]:
135 $bearsamppWinbinder->destroyWindow($window);
136 break;
137 }
138 }
139}
global $bearsamppBins
global $bearsamppLang
global $bearsamppRoot
processWindow($window, $id, $ctrl, $param1, $param2)
const ALIAS_DEST_PATH
const ALIAS_DEST_LABEL
const ADD_ALIAS_TITLE
const ALIAS_CREATED
const BUTTON_CANCEL
const ALIAS_NOT_VALID_ALPHA
const ALIAS_NAME_LABEL
const BUTTON_SAVE
const ALIAS_EXP_LABEL
const ALIAS_ALREADY_EXISTS
const ALIAS_CREATED_ERROR
const BUTTON_BROWSE