1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Alxarafe. Development of PHP applications in a flash! |
4
|
|
|
* Copyright (C) 2018 Alxarafe <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Alxarafe\Core\Base; |
8
|
|
|
|
9
|
|
|
use Alxarafe\Core\Helpers\Globals; |
10
|
|
|
use Alxarafe\Core\Singletons\Render; |
11
|
|
|
use DebugBar\DebugBarException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class BasicController |
15
|
|
|
* |
16
|
|
|
* Define el controlador básico. |
17
|
|
|
* No requiere uso de base de datos ni de autenticación. |
18
|
|
|
* |
19
|
|
|
* @author Rafael San José Tovar <[email protected]> |
20
|
|
|
* @version 2023.0108 |
21
|
|
|
* |
22
|
|
|
* @package Alxarafe\Core\Base |
23
|
|
|
*/ |
24
|
|
|
abstract class BasicController |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Indica si hay que proteger la edición de la salida o cierre accidental del navegador. |
28
|
|
|
* Si true, se necesita pulsar el botón aceptar o cancelar, o confirmar el cierre. |
29
|
|
|
* |
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
public bool $protectedClose; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Contiene la acción a ejecutar por el controlador o null, si no hay acción. |
36
|
|
|
* |
37
|
|
|
* @var string|null |
38
|
|
|
*/ |
39
|
|
|
public ?string $action; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Indica si hay o no menú que mostrar. |
43
|
|
|
* Los controladores básicos no usan menú. |
44
|
|
|
* |
45
|
|
|
* @var bool |
46
|
|
|
*/ |
47
|
|
|
public bool $hasMenu = false; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Contiene una instancia de la clase View o nulo si no ha sido asignada. |
51
|
|
|
* |
52
|
|
|
* @var View |
53
|
|
|
*/ |
54
|
|
|
public View $view; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* BasicController constructor |
58
|
|
|
*/ |
59
|
|
|
public function __construct() |
60
|
|
|
{ |
61
|
|
|
// parent::__construct(); |
62
|
|
|
|
63
|
|
|
$this->protectedClose = false; |
64
|
|
|
if (!$this->preLoad()) { |
65
|
|
|
trigger_error('preLoad fails!'); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Realiza la carga inicial de variables del controlador. |
71
|
|
|
* |
72
|
|
|
* @author Rafael San José Tovar <[email protected]> |
73
|
|
|
* @version 2023.0108 |
74
|
|
|
* |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
public function preLoad(): bool |
78
|
|
|
{ |
79
|
|
|
$this->action = filter_input(INPUT_POST, 'action'); |
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Forma la ruta para recargar usando el mismo módulo y controlador, ignorando |
85
|
|
|
* el resto de variables GET. |
86
|
|
|
* |
87
|
|
|
* @param string $module |
88
|
|
|
* @param string $controller |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public static function url(string $module = Globals::DEFAULT_MODULE_NAME, string $controller = Globals::DEFAULT_CONTROLLER_NAME): string |
93
|
|
|
{ |
94
|
|
|
return BASE_URI . '?' . Globals::MODULE_GET_VAR . '=' . $module . '&' . Globals::CONTROLLER_GET_VAR . '=' . $controller; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Punto de entrada del controlador. |
99
|
|
|
* El dispatcher: |
100
|
|
|
* - En primer lugar instancia (carga el controlador y se ejecuta el __construct) |
101
|
|
|
* - Y a continuación, lanza el método indicado, por defecto es main() |
102
|
|
|
* |
103
|
|
|
* @author Rafael San José Tovar <[email protected]> |
104
|
|
|
* @version 2023.0108 |
105
|
|
|
* |
106
|
|
|
* @return void |
107
|
|
|
* @throws DebugBarException |
108
|
|
|
*/ |
109
|
|
|
public function main(): void |
110
|
|
|
{ |
111
|
|
|
$result = true; |
|
|
|
|
112
|
|
|
if (isset($this->action)) { |
113
|
|
|
$result = $this->doAction(); |
114
|
|
|
} |
115
|
|
|
Render::setTemplate($this->setTemplate()); |
116
|
|
|
$view = new View($this); |
|
|
|
|
117
|
|
|
// $view->render(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Ejecuta una acción |
122
|
|
|
* |
123
|
|
|
* @author Rafael San José Tovar <[email protected]> |
124
|
|
|
* @version 2023.0108 |
125
|
|
|
* |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
|
|
public function doAction(): bool |
129
|
|
|
{ |
130
|
|
|
switch ($this->action) { |
131
|
|
|
case 'save': |
132
|
|
|
return $this->doSave(); |
133
|
|
|
case 'exit': |
134
|
|
|
$this->doExit(); |
135
|
|
|
break; |
136
|
|
|
default: |
137
|
|
|
trigger_error("The '$this->action' action has not been defined!"); |
138
|
|
|
} |
139
|
|
|
return true; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Guarda los cambios. Por defecto no hace nada, hay que sobreescribirlo. |
144
|
|
|
* TODO: Analizar si puede convenir que sea un método abstracto. |
145
|
|
|
* Podría ser un método abstracto, pero es posible que no sea necesario. |
146
|
|
|
* |
147
|
|
|
* @author Rafael San José Tovar <[email protected]> |
148
|
|
|
* @version 2023.0108 |
149
|
|
|
* |
150
|
|
|
* @return bool |
151
|
|
|
*/ |
152
|
|
|
public function doSave(): bool |
153
|
|
|
{ |
154
|
|
|
return true; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Regresa a la pantalla principal. |
159
|
|
|
* |
160
|
|
|
* @author Rafael San José Tovar <[email protected]> |
161
|
|
|
* @version 2023.0108 |
162
|
|
|
* |
163
|
|
|
*/ |
164
|
|
|
public function doExit(): void |
165
|
|
|
{ |
166
|
|
|
header('Location: ' . BASE_URI); |
167
|
|
|
die(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Method to assign the template to the view. |
172
|
|
|
*/ |
173
|
|
|
abstract public function setTemplate(): string; |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* addCSS includes the common CSS files to all views templates. Also defines CSS folders templates. |
177
|
|
|
* |
178
|
|
|
* @return void |
179
|
|
|
* @throws DebugBarException |
180
|
|
|
*/ |
181
|
|
|
public function addCSS() |
182
|
|
|
{ |
183
|
|
|
// $this->addToVar('cssCode', $this->addResource('/bower_modules/bootstrap/dist/css/bootstrap.min', 'css')); |
184
|
|
|
// $this->addToVar('cssCode', $this->addResource('/css/alxarafe', 'css')); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* addJS includes the common JS files to all views templates. Also defines JS folders templates. |
189
|
|
|
* |
190
|
|
|
* @return void |
191
|
|
|
*/ |
192
|
|
|
public function addJS() |
193
|
|
|
{ |
194
|
|
|
// $this->addToVar('jsCode', $this->addResource('/bower_modules/jquery/dist/jquery.min', 'js')); |
195
|
|
|
// $this->addToVar('jsCode', $this->addResource('/bower_modules/bootstrap/dist/js/bootstrap.min', 'js')); |
196
|
|
|
// $this->addToVar('jsCode', $this->addResource('/js/alxarafe', 'js')); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
} |
200
|
|
|
|