Bearsampp 2026.9.23
Loading...
Searching...
No Matches
class.action.dialogBase.php
Go to the documentation of this file.
1<?php
2/*
3 * Copyright (c) 2021-2026 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
17abstract class ActionDialogBase
18{
20 protected $wbWindow;
22 protected $wbProgressBar;
24 protected $wbBtnSave;
26 protected $wbBtnCancel;
28 protected $wbBtnDelete;
29
31 protected $initValue; // Initial value for edit operations
32
34 const GAUGE_SAVE = 2;
36 const GAUGE_DELETE = 2;
37
43 abstract protected function getWindowTitle();
44
50 protected function getGaugeSave()
51 {
52 return self::GAUGE_SAVE;
53 }
54
60 protected function getGaugeDelete()
61 {
62 return self::GAUGE_DELETE;
63 }
64
73 abstract protected function createFormFields($bearsamppWinbinder);
74
82 abstract protected function getFormValues($bearsamppWinbinder);
83
91 abstract protected function validateInput($values);
92
100 abstract protected function itemExists($values);
101
109 abstract protected function saveItem($values);
110
116 abstract protected function deleteItem();
117
125 abstract protected function getSaveSuccessMessage($values);
126
132 abstract protected function getSaveErrorMessage();
133
139 abstract protected function getDeleteConfirmMessage();
140
146 abstract protected function getDeleteSuccessMessage();
147
153 abstract protected function getDeleteErrorMessage();
154
160 abstract protected function getDialogTitle();
161
167 abstract protected function getDeleteDialogTitle();
168
174 protected function isEditMode()
175 {
176 return isset($this->initValue) && !empty($this->initValue);
177 }
178
184 abstract protected function restartService();
185
193 protected function initializeDialog($args)
194 {
195 // To be implemented by child classes if needed
196 return true;
197 }
198
204 public function __construct($args)
205 {
206 global $bearsamppWinbinder;
207
208 // Initialize dialog (child class can load data, etc.)
209 if (!$this->initializeDialog($args)) {
210 return;
211 }
212
213 $bearsamppWinbinder->reset();
214 $this->wbWindow = $bearsamppWinbinder->createAppWindow(
215 $this->getWindowTitle(),
216 490,
217 200,
218 WBC_NOTIFY,
219 WBC_KEYDOWN | WBC_KEYUP
220 );
221
222 // Create form fields (implemented by child class)
223 $this->createFormFields($bearsamppWinbinder);
224
225 // Create progress bar and buttons
226 $this->createButtons($bearsamppWinbinder);
227
228 // Set up event handler
229 $bearsamppWinbinder->setHandler($this->wbWindow, $this, 'processWindow');
230 $bearsamppWinbinder->mainLoop();
231 $bearsamppWinbinder->reset();
232 }
233
241 protected function createButtons($bearsamppWinbinder)
242 {
243 global $bearsamppLang;
244
245 $this->wbProgressBar = $bearsamppWinbinder->createProgressBar(
246 $this->wbWindow,
247 $this->getGaugeSave() + 1,
248 15,
249 137,
250 $this->isEditMode() ? 190 : 275
251 );
252
253 if ($this->isEditMode()) {
254 // Edit mode: Save, Delete, Cancel
255 $this->wbBtnSave = $bearsamppWinbinder->createButton(
256 $this->wbWindow,
258 215,
259 132
260 );
261 $this->wbBtnDelete = $bearsamppWinbinder->createButton(
262 $this->wbWindow,
264 300,
265 132
266 );
267 $this->wbBtnCancel = $bearsamppWinbinder->createButton(
268 $this->wbWindow,
270 385,
271 132
272 );
273 } else {
274 // Add mode: Save, Cancel
275 $this->wbBtnSave = $bearsamppWinbinder->createButton(
276 $this->wbWindow,
278 300,
279 132
280 );
281 $this->wbBtnCancel = $bearsamppWinbinder->createButton(
282 $this->wbWindow,
284 387,
285 132
286 );
287 }
288 }
289
301 public function processWindow($window, $id, $ctrl, $param1, $param2)
302 {
303 global $bearsamppWinbinder;
304
305 // Handle save button
306 if ($id == $this->wbBtnSave[WinBinder::CTRL_ID]) {
307 $this->handleSave($window);
308
309 return;
310 }
311
312 // Handle delete button (if in edit mode)
313 if ($this->isEditMode() && $id == $this->wbBtnDelete[WinBinder::CTRL_ID]) {
314 $this->handleDelete($window);
315
316 return;
317 }
318
319 // Handle cancel button or window close
320 if ($id == IDCLOSE || $id == $this->wbBtnCancel[WinBinder::CTRL_ID]) {
321 $bearsamppWinbinder->destroyWindow($window);
322
323 return;
324 }
325
326 // Handle custom events (implemented by child class)
327 $this->handleCustomEvent($window, $id, $ctrl, $param1, $param2);
328 }
329
341 protected function handleCustomEvent($window, $id, $ctrl, $param1, $param2)
342 {
343 // Default: do nothing
344 // Child classes can override to handle specific events
345 }
346
354 protected function handleSave($window)
355 {
356 global $bearsamppWinbinder;
357
358 $bearsamppWinbinder->setProgressBarMax($this->wbProgressBar, $this->getGaugeSave() + 1);
359 $bearsamppWinbinder->incrProgressBar($this->wbProgressBar);
360
361 // Get form values
362 $values = $this->getFormValues($bearsamppWinbinder);
363
364 // Validate input
365 $validation = $this->validateInput($values);
366 if (!$validation['valid']) {
367 $bearsamppWinbinder->messageBoxError(
368 $validation['error'],
369 $this->getDialogTitle()
370 );
371 $bearsamppWinbinder->resetProgressBar($this->wbProgressBar);
372
373 return;
374 }
375
376 // Check if item already exists (for add or rename operations)
377 if ($this->itemExists($values)) {
378 $bearsamppWinbinder->resetProgressBar($this->wbProgressBar);
379
380 return;
381 }
382
383 // Save the item
384 if ($this->saveItem($values)) {
385 $bearsamppWinbinder->incrProgressBar($this->wbProgressBar);
386
387 // Restart service
388 $this->restartService();
389 $bearsamppWinbinder->incrProgressBar($this->wbProgressBar);
390
391 // Show success message
392 $bearsamppWinbinder->messageBoxInfo(
393 $this->getSaveSuccessMessage($values),
394 $this->getDialogTitle()
395 );
396 $bearsamppWinbinder->destroyWindow($window);
397 } else {
398 $bearsamppWinbinder->messageBoxError(
399 $this->getSaveErrorMessage(),
400 $this->getDialogTitle()
401 );
402 $bearsamppWinbinder->resetProgressBar($this->wbProgressBar);
403 }
404 }
405
413 protected function handleDelete($window)
414 {
415 global $bearsamppWinbinder;
416
417 $bearsamppWinbinder->setProgressBarMax($this->wbProgressBar, $this->getGaugeDelete() + 1);
418
419 // Confirm deletion
420 $confirm = $bearsamppWinbinder->messageBoxYesNo(
422 $this->getDeleteDialogTitle()
423 );
424
425 $bearsamppWinbinder->incrProgressBar($this->wbProgressBar);
426
427 if ($confirm) {
428 if ($this->deleteItem()) {
429 $bearsamppWinbinder->incrProgressBar($this->wbProgressBar);
430
431 // Restart service
432 $this->restartService();
433 $bearsamppWinbinder->incrProgressBar($this->wbProgressBar);
434
435 // Show success message
436 $bearsamppWinbinder->messageBoxInfo(
438 $this->getDeleteDialogTitle()
439 );
440 $bearsamppWinbinder->destroyWindow($window);
441 } else {
442 $bearsamppWinbinder->messageBoxError(
443 $this->getDeleteErrorMessage(),
444 $this->getDeleteDialogTitle()
445 );
446 $bearsamppWinbinder->resetProgressBar($this->wbProgressBar);
447 }
448 } else {
449 $bearsamppWinbinder->resetProgressBar($this->wbProgressBar);
450 }
451 }
452}
453
global $bearsamppLang
createFormFields($bearsamppWinbinder)
createButtons($bearsamppWinbinder)
handleCustomEvent($window, $id, $ctrl, $param1, $param2)
validateInput($values)
getSaveSuccessMessage($values)
saveItem($values)
getFormValues($bearsamppWinbinder)
processWindow($window, $id, $ctrl, $param1, $param2)
itemExists($values)
const BUTTON_CANCEL
const BUTTON_DELETE
const BUTTON_SAVE