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\Xhelp\{ |
21
|
|
|
Helper, |
22
|
|
|
Common\Configurator, |
23
|
|
|
Common\Confirm, |
24
|
|
|
Utility |
25
|
|
|
}; |
26
|
|
|
|
27
|
|
|
/** @var Helper $helper */ |
28
|
|
|
/** @var Utility $utility */ |
29
|
|
|
/** @var Configurator $configurator */ |
30
|
|
|
|
31
|
|
|
require_once dirname(__DIR__, 3) . '/include/cp_header.php'; |
32
|
|
|
require \dirname(__DIR__) . '/preloads/autoloader.php'; |
33
|
|
|
|
34
|
|
|
$op = Request::getCmd('op', ''); |
35
|
|
|
|
36
|
|
|
$moduleDirName = \basename(\dirname(__DIR__)); |
37
|
|
|
$moduleDirNameUpper = \mb_strtoupper($moduleDirName); |
38
|
|
|
|
39
|
|
|
$helper = Helper::getInstance(); |
40
|
|
|
// Load language files |
41
|
|
|
$helper->loadLanguage('common'); |
42
|
|
|
|
43
|
|
|
switch ($op) { |
44
|
|
|
case 'load': |
45
|
|
|
if (Request::hasVar('ok', 'REQUEST') && 1 === Request::getInt('ok', 0)) { |
46
|
|
|
if (!$GLOBALS['xoopsSecurity']->check()) { |
47
|
|
|
\redirect_header($helper->url('admin/index.php'), 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors())); |
48
|
|
|
} |
49
|
|
|
loadSampleData(); |
50
|
|
|
} else { |
51
|
|
|
xoops_cp_header(); |
52
|
|
|
xoops_confirm(['ok' => 1, 'op' => 'load'], 'index.php', \sprintf(\constant('CO_' . $moduleDirNameUpper . '_' . 'LOAD_SAMPLEDATA_CONFIRM')), \constant('CO_' . $moduleDirNameUpper . '_' . 'CONFIRM'), true); |
53
|
|
|
xoops_cp_footer(); |
54
|
|
|
} |
55
|
|
|
break; |
56
|
|
|
case 'save': |
57
|
|
|
saveSampleData(); |
58
|
|
|
break; |
59
|
|
|
case 'clear': |
60
|
|
|
if (Request::hasVar('ok', 'REQUEST') && 1 === Request::getInt('ok', 0)) { |
61
|
|
|
if (!$GLOBALS['xoopsSecurity']->check()) { |
62
|
|
|
redirect_header($helper->url('admin/index.php'), 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors())); |
63
|
|
|
} |
64
|
|
|
clearSampleData(); |
65
|
|
|
} else { |
66
|
|
|
xoops_cp_header(); |
67
|
|
|
xoops_confirm(['ok' => 1, 'op' => 'clear'], 'index.php', |
68
|
|
|
sprintf(constant('CO_' . $moduleDirNameUpper . '_' . 'CLEAR_SAMPLEDATA')), |
69
|
|
|
constant('CO_' . $moduleDirNameUpper . '_' . 'CONFIRM'), true); |
70
|
|
|
xoops_cp_footer(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// if (isset($_REQUEST['ok']) && 1 === Request::getInt('ok', 0)) { |
74
|
|
|
// if (!$GLOBALS['xoopsSecurity']->check()) { |
75
|
|
|
// $helper->redirect($helper->url('admin/index.php'), 3, \implode(', ', $GLOBALS['xoopsSecurity']->getErrors())); |
76
|
|
|
// } |
77
|
|
|
// clearSampleData(); |
78
|
|
|
// } else { |
79
|
|
|
// $customConfirm = new Confirm(['ok' => 1, 'op' => 'clear'], $_SERVER['REQUEST_URI'], sprintf(constant('CO_' . $moduleDirNameUpper . '_' . 'CLEAR_SAMPLEDATA')), constant('CO_' . $moduleDirNameUpper . '_' . 'CONFIRM')); |
80
|
|
|
// |
81
|
|
|
// $form = $customConfirm->getFormConfirm(); |
82
|
|
|
// $GLOBALS['xoopsTpl']->assign('form', $form->render()); |
83
|
|
|
// } |
84
|
|
|
|
85
|
|
|
break; |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// XMF TableLoad for SAMPLE data |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* |
93
|
|
|
*/ |
94
|
|
|
function loadSampleData() |
95
|
|
|
{ |
96
|
|
|
global $xoopsConfig; |
97
|
|
|
$moduleDirName = \basename(\dirname(__DIR__)); |
98
|
|
|
$moduleDirNameUpper = \mb_strtoupper($moduleDirName); |
99
|
|
|
|
100
|
|
|
$utility = new Utility(); |
101
|
|
|
$configurator = new Configurator(); |
102
|
|
|
|
103
|
|
|
$tables = Helper::getInstance() |
104
|
|
|
->getModule() |
105
|
|
|
->getInfo('tables'); |
106
|
|
|
|
107
|
|
|
$language = 'english/'; |
108
|
|
|
if (\is_dir(__DIR__ . '/' . $xoopsConfig['language'])) { |
109
|
|
|
$language = $xoopsConfig['language'] . '/'; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// load module tables |
113
|
|
|
foreach ($tables as $table) { |
114
|
|
|
$tabledata = Yaml::readWrapped($language . $table . '.yml'); |
115
|
|
|
TableLoad::truncateTable($table); |
116
|
|
|
TableLoad::loadTableFromArray($table, $tabledata); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// load permissions |
120
|
|
|
$table = 'group_permission'; |
121
|
|
|
$tabledata = Yaml::readWrapped($language . $table . '.yml'); |
122
|
|
|
$mid = Helper::getInstance()->getModule()->getVar('mid'); |
123
|
|
|
|
124
|
|
|
loadTableFromArrayWithReplace($table, $tabledata, 'gperm_modid', (string)$mid); |
125
|
|
|
|
126
|
|
|
// --- COPY test folder files --------------- |
127
|
|
|
if (\is_array($configurator->copyTestFolders) && \count($configurator->copyTestFolders) > 0) { |
128
|
|
|
// $file = dirname(__DIR__) . '/testdata/images/'; |
129
|
|
|
foreach (\array_keys($configurator->copyTestFolders) as $i) { |
130
|
|
|
$src = $configurator->copyTestFolders[$i][0]; |
131
|
|
|
$dest = $configurator->copyTestFolders[$i][1]; |
132
|
|
|
$utility::rcopy($src, $dest); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
\redirect_header('../admin/index.php', 1, \constant('CO_' . $moduleDirNameUpper . '_' . 'LOAD_SAMPLEDATA_SUCCESS')); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* |
140
|
|
|
*/ |
141
|
|
|
function saveSampleData() |
142
|
|
|
{ |
143
|
|
|
global $xoopsConfig; |
144
|
|
|
$moduleDirName = \basename(\dirname(__DIR__)); |
145
|
|
|
$moduleDirNameUpper = \mb_strtoupper($moduleDirName); |
146
|
|
|
$helper = Helper::getInstance(); |
147
|
|
|
$tables = $helper->getModule() |
148
|
|
|
->getInfo('tables'); |
149
|
|
|
|
150
|
|
|
$languageFolder = __DIR__ . '/' . $xoopsConfig['language']; |
151
|
|
|
if (!\file_exists($languageFolder . '/')) { |
152
|
|
|
Utility::createFolder($languageFolder . '/'); |
153
|
|
|
} |
154
|
|
|
$exportFolder = $languageFolder . '/Exports-' . date('Y-m-d-H-i-s') . '/'; |
155
|
|
|
Utility::createFolder($exportFolder); |
156
|
|
|
|
157
|
|
|
// save module tables |
158
|
|
|
foreach ($tables as $table) { |
159
|
|
|
TableLoad::saveTableToYamlFile($table, $exportFolder . $table . '.yml'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
// save permissions |
163
|
|
|
$criteria = new \CriteriaCompo(); |
164
|
|
|
$criteria->add( |
165
|
|
|
new \Criteria( |
166
|
|
|
'gperm_modid', |
167
|
|
|
$helper->getModule() |
|
|
|
|
168
|
|
|
->getVar('mid') |
169
|
|
|
) |
170
|
|
|
); |
171
|
|
|
$skipColumns[] = 'gperm_id'; |
|
|
|
|
172
|
|
|
TableLoad::saveTableToYamlFile('group_permission', $exportFolder . 'group_permission.yml', $criteria, $skipColumns); |
173
|
|
|
unset($criteria); |
174
|
|
|
|
175
|
|
|
\redirect_header('../admin/index.php', 1, \constant('CO_' . $moduleDirNameUpper . '_' . 'SAVE_SAMPLEDATA_SUCCESS')); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* |
180
|
|
|
*/ |
181
|
|
|
function exportSchema() |
182
|
|
|
{ |
183
|
|
|
$moduleDirName = \basename(\dirname(__DIR__)); |
184
|
|
|
$moduleDirNameUpper = \mb_strtoupper($moduleDirName); |
|
|
|
|
185
|
|
|
|
186
|
|
|
try { |
187
|
|
|
// TODO set exportSchema |
188
|
|
|
// $migrate = new Migrate($moduleDirName); |
189
|
|
|
// $migrate->saveCurrentSchema(); |
190
|
|
|
// |
191
|
|
|
// redirect_header('../admin/index.php', 1, constant('CO_' . $moduleDirNameUpper . '_' . 'EXPORT_SCHEMA_SUCCESS')); |
192
|
|
|
} catch (\Throwable $e) { |
|
|
|
|
193
|
|
|
exit(constant('CO_' . $moduleDirNameUpper . '_' . 'EXPORT_SCHEMA_ERROR')); |
|
|
|
|
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* loadTableFromArrayWithReplace |
199
|
|
|
* |
200
|
|
|
* @param string $table value with should be used insead of original value of $search |
201
|
|
|
* |
202
|
|
|
* @param array $data array of rows to insert |
203
|
|
|
* Each element of the outer array represents a single table row. |
204
|
|
|
* Each row is an associative array in 'column' => 'value' format. |
205
|
|
|
* @param string $search name of column for which the value should be replaced |
206
|
|
|
* @param string $replace |
207
|
|
|
* @return int number of rows inserted |
208
|
|
|
*/ |
209
|
|
|
function loadTableFromArrayWithReplace(string $table, array $data, string $search, string $replace): int |
210
|
|
|
{ |
211
|
|
|
/** @var \XoopsMySQLDatabase $db */ |
212
|
|
|
$db = \XoopsDatabaseFactory::getDatabaseConnection(); |
213
|
|
|
|
214
|
|
|
$prefixedTable = $db->prefix($table); |
215
|
|
|
$count = 0; |
216
|
|
|
|
217
|
|
|
$sql = 'DELETE FROM ' . $prefixedTable . ' WHERE `' . $search . '`=' . $db->quote($replace); |
218
|
|
|
|
219
|
|
|
$result = $db->queryF($sql); |
220
|
|
|
|
221
|
|
|
if ($result) { |
222
|
|
|
foreach ($data as $row) { |
223
|
|
|
$insertInto = 'INSERT INTO ' . $prefixedTable . ' ('; |
224
|
|
|
$valueClause = ' VALUES ('; |
225
|
|
|
$first = true; |
226
|
|
|
foreach ($row as $column => $value) { |
227
|
|
|
if ($first) { |
228
|
|
|
$first = false; |
229
|
|
|
} else { |
230
|
|
|
$insertInto .= ', '; |
231
|
|
|
$valueClause .= ', '; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
$insertInto .= $column; |
235
|
|
|
if ($search === $column) { |
236
|
|
|
$valueClause .= $db->quote($replace); |
237
|
|
|
} else { |
238
|
|
|
$valueClause .= $db->quote($value); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
$sql = $insertInto . ') ' . $valueClause . ')'; |
243
|
|
|
|
244
|
|
|
$result = $db->queryF($sql); |
245
|
|
|
if (false !== $result) { |
246
|
|
|
++$count; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
return $count; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
function clearSampleData() |
254
|
|
|
{ |
255
|
|
|
$moduleDirName = \basename(\dirname(__DIR__)); |
256
|
|
|
$moduleDirNameUpper = \mb_strtoupper($moduleDirName); |
257
|
|
|
$helper = Helper::getInstance(); |
258
|
|
|
// Load language files |
259
|
|
|
$helper->loadLanguage('common'); |
260
|
|
|
$tables = $helper->getModule() |
261
|
|
|
->getInfo('tables'); |
262
|
|
|
// truncate module tables |
263
|
|
|
foreach ($tables as $table) { |
264
|
|
|
\Xmf\Database\TableLoad::truncateTable($table); |
265
|
|
|
} |
266
|
|
|
redirect_header($helper->url('admin/index.php'), 1, constant('CO_' . $moduleDirNameUpper . '_' . 'CLEAR_SAMPLEDATA_OK')); |
267
|
|
|
} |
268
|
|
|
|