Passed
Push — master ( 6499a1...73e8f6 )
by Michael
36s queued 12s
created

testdata/index.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 * This program is distributed in the hope that it will be useful,
7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9
 *
10
 * @copyright       XOOPS Project (https://xoops.org)
11
 * @license         GNU GPL 2 (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
12
 * @since           2.5.9
13
 * @author          Michael Beck (aka Mamba): https://github.com/mambax7
14
 */
15
16
use Xmf\Database\TableLoad;
17
use Xmf\Request;
18
use Xmf\Yaml;
19
use XoopsModules\Xoopstube\{Helper,
20
    Common\Configurator,
21
    Utility
22
};
23
/** @var Helper $helper */
24
/** @var Utility $utility */
25
/** @var Configurator $configurator */
26
27
require dirname(__DIR__, 3) . '/include/cp_header.php';
28
require dirname(__DIR__) . '/preloads/autoloader.php';
29
30
$op = Request::getCmd('op', '');
31
32
$moduleDirName      = \basename(\dirname(__DIR__));
33
$moduleDirNameUpper = mb_strtoupper($moduleDirName);
34
35
$helper = Helper::getInstance();
36
// Load language files
37
$helper->loadLanguage('common');
38
39
switch ($op) {
40
    case 'load':
41
        if (Request::hasVar('ok', 'REQUEST') && 1 === Request::getInt('ok', 0)) {
42
            if (!$GLOBALS['xoopsSecurity']->check()) {
43
                redirect_header($helper->url('admin/index.php'), 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
44
            }
45
            loadSampleData();
46
        } else {
47
            xoops_cp_header();
48
            xoops_confirm(['ok' => 1, 'op' => 'load'], 'index.php', sprintf(constant('CO_' . $moduleDirNameUpper . '_' . 'LOAD_SAMPLEDATA_CONFIRM')), constant('CO_' . $moduleDirNameUpper . '_' . 'CONFIRM'), true);
49
            xoops_cp_footer();
50
        }
51
        break;
52
    case 'save':
53
        saveSampleData();
54
        break;
55
    case 'clear':
56
        clearSampleData();
57
        break;
58
}
59
60
// XMF TableLoad for SAMPLE data
61
62
function loadSampleData()
63
{
64
    global $xoopsConfig;
65
    $moduleDirName      = \basename(\dirname(__DIR__));
66
    $moduleDirNameUpper = mb_strtoupper($moduleDirName);
67
68
    $utility      = new Utility();
69
    $configurator = new Configurator();
70
71
    $tables = \Xmf\Module\Helper::getHelper($moduleDirName)->getModule()->getInfo('tables');
72
73
    $language = 'english/';
74
    if (is_dir(__DIR__ . '/' . $xoopsConfig['language'])) {
75
        $language = $xoopsConfig['language'] . '/';
76
    }
77
78
    // load module tables
79
    foreach ($tables as $table) {
80
        $tabledata = Yaml::readWrapped($language . $table . '.yml');
81
        TableLoad::truncateTable($table);
82
        TableLoad::loadTableFromArray($table, $tabledata);
83
    }
84
85
    // load permissions
86
    $table     = 'group_permission';
87
    $tabledata = Yaml::readWrapped($language . $table . '.yml');
88
    $mid       = \Xmf\Module\Helper::getHelper($moduleDirName)->getModule()->getVar('mid');
89
    loadTableFromArrayWithReplace($table, $tabledata, 'gperm_modid', $mid);
90
91
    //  ---  COPY test folder files ---------------
92
    if (is_array($configurator->copyTestFolders) && count($configurator->copyTestFolders) > 0) {
93
        //        $file =  dirname(__DIR__) . '/testdata/images/';
94
        foreach (array_keys($configurator->copyTestFolders) as $i) {
95
            $src  = $configurator->copyTestFolders[$i][0];
96
            $dest = $configurator->copyTestFolders[$i][1];
97
            $utility::rcopy($src, $dest);
98
        }
99
    }
100
    \redirect_header('../admin/index.php', 1, \constant('CO_' . $moduleDirNameUpper . '_' . 'LOAD_SAMPLEDATA_SUCCESS'));
101
}
102
103
function saveSampleData()
104
{
105
    global $xoopsConfig;
106
    $moduleDirName      = \basename(\dirname(__DIR__));
107
    $moduleDirNameUpper = mb_strtoupper($moduleDirName);
108
    $helper             = Helper::getInstance();
109
    $tables             = $helper->getModule()->getInfo('tables');
110
111
    $languageFolder = __DIR__ . '/' . $xoopsConfig['language'];
112
    if (!file_exists($languageFolder . '/')) {
113
        Utility::createFolder($languageFolder . '/');
114
    }
115
    $exportFolder = $languageFolder . '/Exports-' . date('Y-m-d-H-i-s') . '/';
116
    Utility::createFolder($exportFolder);
117
118
    // save module tables
119
    foreach ($tables as $table) {
120
        TableLoad::saveTableToYamlFile($table, $exportFolder . $table . '.yml');
121
    }
122
123
    // save permissions
124
    $criteria = new \CriteriaCompo();
125
    $criteria->add(new \Criteria('gperm_modid', $helper->getModule()->getVar('mid')));
0 ignored issues
show
It seems like $helper->getModule()->getVar('mid') can also be of type array and array; however, parameter $value of Criteria::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

125
    $criteria->add(new \Criteria('gperm_modid', /** @scrutinizer ignore-type */ $helper->getModule()->getVar('mid')));
Loading history...
126
    $skipColumns[] = 'gperm_id';
127
    TableLoad::saveTableToYamlFile('group_permission', $exportFolder . 'group_permission.yml', $criteria, $skipColumns);
128
    unset($criteria);
129
130
    \redirect_header('../admin/index.php', 1, \constant('CO_' . $moduleDirNameUpper . '_' . 'SAVE_SAMPLEDATA_SUCCESS'));
131
}
132
133
function exportSchema()
134
{
135
    $moduleDirName      = \basename(\dirname(__DIR__));
136
    $moduleDirNameUpper = mb_strtoupper($moduleDirName);
137
138
    try {
139
        // TODO set exportSchema
140
        //        $migrate = new Migrate($moduleDirName);
141
        //        $migrate->saveCurrentSchema();
142
        //
143
        //        redirect_header('../admin/index.php', 1, constant('CO_' . $moduleDirNameUpper . '_' . 'EXPORT_SCHEMA_SUCCESS'));
144
    } catch (\Throwable $e) {
145
        exit(constant('CO_' . $moduleDirNameUpper . '_' . 'EXPORT_SCHEMA_ERROR'));
146
    }
147
}
148
149
/**
150
 * loadTableFromArrayWithReplace
151
 *
152
 * @param string $table  value with should be used insead of original value of $search
153
 *
154
 * @param array  $data   array of rows to insert
155
 *                       Each element of the outer array represents a single table row.
156
 *                       Each row is an associative array in 'column' => 'value' format.
157
 * @param string $search name of column for which the value should be replaced
158
 * @param        $replace
159
 * @return int number of rows inserted
160
 */
161
function loadTableFromArrayWithReplace($table, $data, $search, $replace)
162
{
163
    /** @var \XoopsMySQLDatabase $db */
164
    $db = \XoopsDatabaseFactory::getDatabaseConnection();
165
166
    $prefixedTable = $db->prefix($table);
167
    $count         = 0;
168
169
    $sql = 'DELETE FROM ' . $prefixedTable . ' WHERE `' . $search . '`=' . $db->quote($replace);
170
171
    $result = $db->queryF($sql);
172
173
    foreach ($data as $row) {
174
        $insertInto  = 'INSERT INTO ' . $prefixedTable . ' (';
175
        $valueClause = ' VALUES (';
176
        $first       = true;
177
        foreach ($row as $column => $value) {
178
            if ($first) {
179
                $first = false;
180
            } else {
181
                $insertInto  .= ', ';
182
                $valueClause .= ', ';
183
            }
184
185
            $insertInto .= $column;
186
            if ($search === $column) {
187
                $valueClause .= $db->quote($replace);
188
            } else {
189
                $valueClause .= $db->quote($value);
190
            }
191
        }
192
193
        $sql = $insertInto . ') ' . $valueClause . ')';
194
195
        $result = $db->queryF($sql);
196
        if (false !== $result) {
197
            ++$count;
198
        }
199
    }
200
201
    return $count;
202
}
203
204
function clearSampleData(){
205
    $moduleDirName      = \basename(\dirname(__DIR__));
206
    $moduleDirNameUpper = mb_strtoupper($moduleDirName);
207
    $helper             = Helper::getInstance();
208
    // Load language files
209
    $helper->loadLanguage('common');
210
    $tables = $helper->getModule()->getInfo('tables');
211
    // truncate module tables
212
    foreach ($tables as $table) {
213
        \Xmf\Database\TableLoad::truncateTable($table);
214
    }
215
    redirect_header($helper->url('admin/index.php'), 1, constant('CO_' . $moduleDirNameUpper . '_' . 'CLEAR_SAMPLEDATA_OK'));
216
}
217