|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* You may not change or alter any portion of this comment or credits |
|
5
|
|
|
* of supporting developers from this source code or any supporting source code |
|
6
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
|
7
|
|
|
* This program is distributed in the hope that it will be useful, |
|
8
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
9
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
10
|
|
|
* |
|
11
|
|
|
* @copyright XOOPS Project (https://xoops.org) |
|
12
|
|
|
* @license GNU GPL 2 (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html) |
|
13
|
|
|
* @since 2.5.9 |
|
14
|
|
|
* @author Michael Beck (aka Mamba) |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
use Xmf\Database\TableLoad; |
|
18
|
|
|
use Xmf\Request; |
|
19
|
|
|
use Xmf\Yaml; |
|
20
|
|
|
use XoopsModules\Xoopsheadline\{ |
|
21
|
|
|
Common\Configurator, |
|
22
|
|
|
Helper, |
|
23
|
|
|
Utility |
|
24
|
|
|
}; |
|
25
|
|
|
|
|
26
|
|
|
/** @var Helper $helper */ |
|
27
|
|
|
/** @var Utility $utility */ |
|
28
|
|
|
/** @var Configurator $configurator */ |
|
29
|
|
|
require \dirname(__DIR__, 3) . '/include/cp_header.php'; |
|
30
|
|
|
require \dirname(__DIR__) . '/preloads/autoloader.php'; |
|
31
|
|
|
|
|
32
|
|
|
$op = Request::getCmd('op', ''); |
|
33
|
|
|
|
|
34
|
|
|
$moduleDirName = \basename(\dirname(__DIR__)); |
|
35
|
|
|
$moduleDirNameUpper = \mb_strtoupper($moduleDirName); |
|
36
|
|
|
|
|
37
|
|
|
$helper = Helper::getInstance(); |
|
38
|
|
|
// Load language files |
|
39
|
|
|
$helper->loadLanguage('common'); |
|
40
|
|
|
|
|
41
|
|
|
switch ($op) { |
|
42
|
|
|
case 'load': |
|
43
|
|
|
if (Request::hasVar('ok', 'REQUEST') && 1 === Request::getInt('ok', 0)) { |
|
44
|
|
|
if (!$GLOBALS['xoopsSecurity']->check()) { |
|
45
|
|
|
redirect_header($helper->url('admin/index.php'), 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors())); |
|
46
|
|
|
} |
|
47
|
|
|
loadSampleData(); |
|
48
|
|
|
} else { |
|
49
|
|
|
xoops_cp_header(); |
|
50
|
|
|
xoops_confirm(['ok' => 1, 'op' => 'load'], 'index.php', constant('CO_' . $moduleDirNameUpper . '_' . 'LOAD_SAMPLEDATA_CONFIRM'), constant('CO_' . $moduleDirNameUpper . '_' . 'CONFIRM'), true); |
|
51
|
|
|
xoops_cp_footer(); |
|
52
|
|
|
} |
|
53
|
|
|
break; |
|
54
|
|
|
case 'save': |
|
55
|
|
|
saveSampleData(); |
|
56
|
|
|
break; |
|
57
|
|
|
case 'clear': |
|
58
|
|
|
if (Request::hasVar('ok', 'REQUEST') && 1 === Request::getInt('ok', 0)) { |
|
59
|
|
|
if (!$GLOBALS['xoopsSecurity']->check()) { |
|
60
|
|
|
redirect_header($helper->url('admin/index.php'), 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors())); |
|
61
|
|
|
} |
|
62
|
|
|
clearSampleData(); |
|
63
|
|
|
} else { |
|
64
|
|
|
xoops_cp_header(); |
|
65
|
|
|
xoops_confirm(['ok' => 1, 'op' => 'clear'], 'index.php', sprintf(constant('CO_' . $moduleDirNameUpper . '_' . 'CLEAR_SAMPLEDATA')), constant('CO_' . $moduleDirNameUpper . '_' . 'CONFIRM'), true); |
|
66
|
|
|
xoops_cp_footer(); |
|
67
|
|
|
} |
|
68
|
|
|
break; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// XMF TableLoad for SAMPLE data |
|
72
|
|
|
|
|
73
|
|
|
function loadSampleData(): void |
|
74
|
|
|
{ |
|
75
|
|
|
global $xoopsConfig; |
|
76
|
|
|
$moduleDirName = \basename(\dirname(__DIR__)); |
|
77
|
|
|
$moduleDirNameUpper = \mb_strtoupper($moduleDirName); |
|
78
|
|
|
|
|
79
|
|
|
$utility = new Utility(); |
|
80
|
|
|
$configurator = new Configurator(); |
|
81
|
|
|
|
|
82
|
|
|
$tables = Helper::getInstance()->getModule()->getInfo('tables'); |
|
83
|
|
|
|
|
84
|
|
|
$language = 'english/'; |
|
85
|
|
|
if (is_dir(__DIR__ . '/' . $xoopsConfig['language'])) { |
|
86
|
|
|
$language = $xoopsConfig['language'] . '/'; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
// load module tables |
|
90
|
|
|
foreach ($tables as $table) { |
|
91
|
|
|
$tabledata = Yaml::readWrapped($language . $table . '.yml'); |
|
92
|
|
|
TableLoad::truncateTable($table); |
|
93
|
|
|
TableLoad::loadTableFromArray($table, $tabledata); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// load permissions |
|
97
|
|
|
$table = 'group_permission'; |
|
98
|
|
|
$tabledata = Yaml::readWrapped($language . $table . '.yml'); |
|
99
|
|
|
$mid = \Xmf\Module\Helper::getHelper($moduleDirName)->getModule()->getVar('mid'); |
|
100
|
|
|
loadTableFromArrayWithReplace($table, $tabledata, 'gperm_modid', $mid); |
|
101
|
|
|
|
|
102
|
|
|
// --- COPY test folder files --------------- |
|
103
|
|
|
if (is_array($configurator->copyTestFolders) && count($configurator->copyTestFolders) > 0) { |
|
104
|
|
|
// $file = \dirname(__DIR__) . '/testdata/images/'; |
|
105
|
|
|
foreach (array_keys($configurator->copyTestFolders) as $i) { |
|
106
|
|
|
$src = $configurator->copyTestFolders[$i][0]; |
|
107
|
|
|
$dest = $configurator->copyTestFolders[$i][1]; |
|
108
|
|
|
$utility::rcopy($src, $dest); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
\redirect_header('../admin/index.php', 1, \constant('CO_' . $moduleDirNameUpper . '_' . 'LOAD_SAMPLEDATA_SUCCESS')); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
function saveSampleData(): void |
|
115
|
|
|
{ |
|
116
|
|
|
global $xoopsConfig; |
|
117
|
|
|
$moduleDirName = \basename(\dirname(__DIR__)); |
|
118
|
|
|
$moduleDirNameUpper = \mb_strtoupper($moduleDirName); |
|
119
|
|
|
$helper = Helper::getInstance(); |
|
120
|
|
|
$tables = $helper->getModule()->getInfo('tables'); |
|
121
|
|
|
|
|
122
|
|
|
$languageFolder = __DIR__ . '/' . $xoopsConfig['language']; |
|
123
|
|
|
if (!file_exists($languageFolder . '/')) { |
|
124
|
|
|
Utility::createFolder($languageFolder . '/'); |
|
125
|
|
|
} |
|
126
|
|
|
$exportFolder = $languageFolder . '/Exports-' . date('Y-m-d-H-i-s') . '/'; |
|
127
|
|
|
Utility::createFolder($exportFolder); |
|
128
|
|
|
|
|
129
|
|
|
// save module tables |
|
130
|
|
|
foreach ($tables as $table) { |
|
131
|
|
|
TableLoad::saveTableToYamlFile($table, $exportFolder . $table . '.yml'); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
// save permissions |
|
135
|
|
|
$criteria = new \CriteriaCompo(); |
|
136
|
|
|
$criteria->add(new \Criteria('gperm_modid', $helper->getModule()->getVar('mid'))); |
|
|
|
|
|
|
137
|
|
|
$skipColumns[] = 'gperm_id'; |
|
|
|
|
|
|
138
|
|
|
TableLoad::saveTableToYamlFile('group_permission', $exportFolder . 'group_permission.yml', $criteria, $skipColumns); |
|
139
|
|
|
unset($criteria); |
|
140
|
|
|
|
|
141
|
|
|
\redirect_header('../admin/index.php', 1, \constant('CO_' . $moduleDirNameUpper . '_' . 'SAVE_SAMPLEDATA_SUCCESS')); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
function exportSchema(): void |
|
145
|
|
|
{ |
|
146
|
|
|
$moduleDirName = \basename(\dirname(__DIR__)); |
|
147
|
|
|
$moduleDirNameUpper = \mb_strtoupper($moduleDirName); |
|
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
try { |
|
150
|
|
|
// TODO set exportSchema |
|
151
|
|
|
// $migrate = new Migrate($moduleDirName); |
|
152
|
|
|
// $migrate->saveCurrentSchema(); |
|
153
|
|
|
// |
|
154
|
|
|
// redirect_header('../admin/index.php', 1, constant('CO_' . $moduleDirNameUpper . '_' . 'EXPORT_SCHEMA_SUCCESS')); |
|
155
|
|
|
} catch (\Throwable $e) { |
|
|
|
|
|
|
156
|
|
|
exit(constant('CO_' . $moduleDirNameUpper . '_' . 'EXPORT_SCHEMA_ERROR')); |
|
|
|
|
|
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* loadTableFromArrayWithReplace |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $table value with should be used insead of original value of $search |
|
164
|
|
|
* |
|
165
|
|
|
* @param array $data array of rows to insert |
|
166
|
|
|
* Each element of the outer array represents a single table row. |
|
167
|
|
|
* Each row is an associative array in 'column' => 'value' format. |
|
168
|
|
|
* @param string $search name of column for which the value should be replaced |
|
169
|
|
|
* @return int number of rows inserted |
|
170
|
|
|
*/ |
|
171
|
|
|
function loadTableFromArrayWithReplace(string $table, array $data, string $search, string $replace): int |
|
172
|
|
|
{ |
|
173
|
|
|
/** @var \XoopsMySQLDatabase $db */ |
|
174
|
|
|
$db = \XoopsDatabaseFactory::getDatabaseConnection(); |
|
175
|
|
|
|
|
176
|
|
|
$prefixedTable = $db->prefix($table); |
|
177
|
|
|
$count = 0; |
|
178
|
|
|
|
|
179
|
|
|
$sql = 'DELETE FROM ' . $prefixedTable . ' WHERE `' . $search . '`=' . $db->quote($replace); |
|
180
|
|
|
|
|
181
|
|
|
$result = $db->queryF($sql); |
|
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
foreach ($data as $row) { |
|
184
|
|
|
$insertInto = 'INSERT INTO ' . $prefixedTable . ' ('; |
|
185
|
|
|
$valueClause = ' VALUES ('; |
|
186
|
|
|
$first = true; |
|
187
|
|
|
foreach ($row as $column => $value) { |
|
188
|
|
|
if ($first) { |
|
189
|
|
|
$first = false; |
|
190
|
|
|
} else { |
|
191
|
|
|
$insertInto .= ', '; |
|
192
|
|
|
$valueClause .= ', '; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
$insertInto .= $column; |
|
196
|
|
|
if ($search === $column) { |
|
197
|
|
|
$valueClause .= $db->quote($replace); |
|
198
|
|
|
} else { |
|
199
|
|
|
$valueClause .= $db->quote($value); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
$sql = $insertInto . ') ' . $valueClause . ')'; |
|
204
|
|
|
|
|
205
|
|
|
$result = $db->queryF($sql); |
|
206
|
|
|
if (false !== $result) { |
|
207
|
|
|
++$count; |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
return $count; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
function clearSampleData(): void |
|
215
|
|
|
{ |
|
216
|
|
|
$moduleDirName = \basename(\dirname(__DIR__)); |
|
217
|
|
|
$moduleDirNameUpper = \mb_strtoupper($moduleDirName); |
|
218
|
|
|
$helper = Helper::getInstance(); |
|
219
|
|
|
// Load language files |
|
220
|
|
|
$helper->loadLanguage('common'); |
|
221
|
|
|
$tables = $helper->getModule()->getInfo('tables'); |
|
222
|
|
|
// truncate module tables |
|
223
|
|
|
foreach ($tables as $table) { |
|
224
|
|
|
\Xmf\Database\TableLoad::truncateTable($table); |
|
225
|
|
|
} |
|
226
|
|
|
redirect_header($helper->url('admin/index.php'), 1, constant('CO_' . $moduleDirNameUpper . '_' . 'CLEAR_SAMPLEDATA_OK')); |
|
227
|
|
|
} |
|
228
|
|
|
|