1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* Copyright (C) 2024 Rafael San José <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This program is free software; you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU General Public License as published by |
7
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
8
|
|
|
* any later version. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Alxarafe\Base\Controller; |
20
|
|
|
|
21
|
|
|
use Alxarafe\Lib\Trans; |
22
|
|
|
use Alxarafe\Tools\Debug; |
23
|
|
|
use Alxarafe\Tools\Dispatcher; |
24
|
|
|
use Alxarafe\Tools\ModuleManager; |
25
|
|
|
use Illuminate\Support\Str; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class GenericController. The generic controller contains what is necessary for any controller |
29
|
|
|
* |
30
|
|
|
* @package Alxarafe\Base |
31
|
|
|
*/ |
32
|
|
|
abstract class GenericController |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Contains the action to execute. |
36
|
|
|
* |
37
|
|
|
* @var string|null |
38
|
|
|
*/ |
39
|
|
|
public ?string $action; |
40
|
|
|
|
41
|
|
|
public array $top_menu = []; |
42
|
|
|
public array $sidebar_menu = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* GenericController constructor. |
46
|
|
|
*/ |
47
|
|
|
public function __construct() |
48
|
|
|
{ |
49
|
|
|
$this->action = filter_input(INPUT_POST, 'action'); |
50
|
|
|
if ($this->action === null) { |
51
|
|
|
$this->action = filter_input(INPUT_GET, 'action'); |
52
|
|
|
} |
53
|
|
|
if ($this->action === null) { |
54
|
|
|
$this->action = 'index'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->top_menu = ModuleManager::getArrayMenu(); |
58
|
|
|
$this->sidebar_menu = ModuleManager::getArraySidebarMenu(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public static function getMenu() |
62
|
|
|
{ |
63
|
|
|
if (!defined('static::MENU')) { |
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
return static::MENU; |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public static function getSidebarMenu() |
70
|
|
|
{ |
71
|
|
|
if (!defined('static::MENU') || !defined('static::SIDEBAR_MENU')) { |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return [ |
76
|
|
|
'base' => static::MENU, |
|
|
|
|
77
|
|
|
'options' => static::SIDEBAR_MENU, |
|
|
|
|
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns an array with the controller actions. |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
public static function getActions(): array |
87
|
|
|
{ |
88
|
|
|
$actions = []; |
89
|
|
|
|
90
|
|
|
$methods = get_class_methods(static::class); |
91
|
|
|
foreach ($methods as $method) { |
92
|
|
|
if (!str_starts_with($method, 'do')) { |
93
|
|
|
continue; |
94
|
|
|
} |
95
|
|
|
$actions[static::class][] = lcfirst(substr($method, 2)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $actions; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Execute the selected action, returning true if successful. |
103
|
|
|
* |
104
|
|
|
* @param bool $executeActions |
105
|
|
|
* |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
|
|
public function index(bool $executeActions = true): bool |
109
|
|
|
{ |
110
|
|
|
if (!$executeActions) { |
111
|
|
|
return false; |
112
|
|
|
} |
113
|
|
|
return $this->executeAction(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Execute the selected action, returning true if successful. |
118
|
|
|
* |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
private function executeAction(): bool |
122
|
|
|
{ |
123
|
|
|
$actionMethod = 'do' . ucfirst(Str::camel($this->action ?? 'index')); |
124
|
|
|
if (!method_exists($this, $actionMethod)) { |
125
|
|
|
Debug::message(Trans::_('unknown_method', ['method' => $actionMethod])); |
126
|
|
|
return false; |
127
|
|
|
} |
128
|
|
|
return $this->beforeAction() && $this->$actionMethod() && $this->afterAction(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* You can include code here that is common to call all controller actions. |
133
|
|
|
* If you need to do something, override this method. |
134
|
|
|
* |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
|
|
public function beforeAction(): bool |
138
|
|
|
{ |
139
|
|
|
return true; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* You can include code here common to calling all controller actions, which will be executed after the action. |
144
|
|
|
* If you need to do something, override this method. |
145
|
|
|
* |
146
|
|
|
* @return bool |
147
|
|
|
*/ |
148
|
|
|
public function afterAction(): bool |
149
|
|
|
{ |
150
|
|
|
return true; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|