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; |
20
|
|
|
|
21
|
|
|
use Alxarafe\Tools\Debug; |
22
|
|
|
use Illuminate\Support\Str; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class GenericController. The generic controller contains what is necessary for any controller |
26
|
|
|
* |
27
|
|
|
* @package Alxarafe\Base |
28
|
|
|
*/ |
29
|
|
|
abstract class GenericController |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Contains the action to execute. |
33
|
|
|
* |
34
|
|
|
* @var string|null |
35
|
|
|
*/ |
36
|
|
|
public ?string $action; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* GenericController constructor. |
40
|
|
|
*/ |
41
|
|
|
public function __construct() |
42
|
|
|
{ |
43
|
|
|
$this->action = filter_input(INPUT_POST, 'action'); |
44
|
|
|
if ($this->action === null) { |
45
|
|
|
$this->action = filter_input(INPUT_GET, 'action'); |
46
|
|
|
} |
47
|
|
|
if ($this->action === null) { |
48
|
|
|
$this->action = 'index'; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns the generic url of the controller; |
54
|
|
|
* |
55
|
|
|
* @param $full |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public static function url($full = true) |
60
|
|
|
{ |
61
|
|
|
$url = ''; |
62
|
|
|
if ($full) { |
63
|
|
|
$url .= BASE_URL . '/index.php'; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$url .= |
67
|
|
|
'?module=' . filter_input(INPUT_GET, 'module') . |
68
|
|
|
'&controller=' . filter_input(INPUT_GET, 'controller'); |
69
|
|
|
|
70
|
|
|
$action = filter_input(INPUT_GET, 'action'); |
71
|
|
|
if ($action) { |
72
|
|
|
$url .= '&action=' . $action; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $url; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Execute the selected action, returning true if successful. |
80
|
|
|
* |
81
|
|
|
* @param bool $executeActions |
82
|
|
|
* |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
|
|
public function index(bool $executeActions = true): bool |
86
|
|
|
{ |
87
|
|
|
if (!$executeActions) { |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
return $this->executeAction(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Execute the selected action, returning true if successful. |
95
|
|
|
* |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
private function executeAction(): bool |
99
|
|
|
{ |
100
|
|
|
$actionMethod = 'do' . ucfirst(Str::camel($this->action ?? 'index')); |
101
|
|
|
if (!method_exists($this, $actionMethod)) { |
102
|
|
|
Debug::message('Does not exist the method ' . $actionMethod); |
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
return $this->beforeAction() && $this->$actionMethod() && $this->afterAction(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* You can include code here that is common to call all controller actions. |
111
|
|
|
* If you need to do something, override this method. |
112
|
|
|
* |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
|
|
public function beforeAction(): bool |
116
|
|
|
{ |
117
|
|
|
return true; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* You can include code here common to calling all controller actions, which will be executed after the action. |
123
|
|
|
* If you need to do something, override this method. |
124
|
|
|
* |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
|
|
public function afterAction(): bool |
128
|
|
|
{ |
129
|
|
|
return true; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|