Issues (584)

testdata/index.php (2 issues)

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       The XOOPS Project http://sourceforge.net/projects/xoops/
11
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
12
 * @package
13
 * @since           2.5.9
14
 * @author          Michael Beck (aka Mamba)
15
 */
16
17
use Xmf\Database\TableLoad;
18
use Xmf\Module\Helper;
19
use Xmf\Request;
20
use Xmf\Yaml;
21
use XoopsModules\Wggithub;
22
use XoopsModules\Wggithub\{
23
    Common,
24
    Utility
25
};
26
27
require_once \dirname(__DIR__, 3) . '/include/cp_header.php';
28
require \dirname(__DIR__) . '/preloads/autoloader.php';
29
30
$op = \Xmf\Request::getCmd('op', '');
31
32
$moduleDirName = \basename(\dirname(__DIR__));
33
$moduleDirNameUpper = \mb_strtoupper($moduleDirName);
34
35
$helper = Wggithub\Helper::getInstance();
36
// Load language files
37
$helper->loadLanguage('common');
38
39
switch ($op) {
40
    case 'load':
41
        if (\Xmf\Request::hasVar('ok', 'REQUEST') && 1 == $_REQUEST['ok']) {
42
            if (!$GLOBALS['xoopsSecurity']->check()) {
43
                \redirect_header('../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 . '_' . 'ADD_SAMPLEDATA_OK')), \constant('CO_' . $moduleDirNameUpper . '_' . 'CONFIRM'), true);
49
            xoops_cp_footer();
50
        }
51
        break;
52
    case 'save':
53
        saveSampleData();
54
        break;
55
}
56
57
// XMF TableLoad for SAMPLE data
58
function loadSampleData()
59
{
60
    global $xoopsConfig;
61
62
    $moduleDirName      = \basename(\dirname(__DIR__));
63
    $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
64
65
    $utility      = new Wggithub\Utility();
66
    $configurator = new Common\Configurator();
67
68
    $tables = Helper::getHelper($moduleDirName)->getModule()->getInfo('tables');
69
70
    $language = 'english/';
71
    if (\is_dir(__DIR__ . '/' . $xoopsConfig['language'])) {
72
        $language = $xoopsConfig['language'] . '/';
73
    }
74
75
    // load module tables
76
    foreach ($tables as $table) {
77
        $tabledata = Yaml::readWrapped($language . $table . '.yml');
78
        TableLoad::truncateTable($table);
79
        TableLoad::loadTableFromArray($table, $tabledata);
80
    }
81
82
    // load permissions
83
    $table     = 'group_permission';
84
    $tabledata = Yaml::readWrapped($language . $table . '.yml');
85
    $mid       = Helper::getHelper($moduleDirName)->getModule()->getVar('mid');
86
    loadTableFromArrayWithReplace($table, $tabledata, 'gperm_modid', $mid);
87
88
    //  ---  COPY test folder files ---------------
89
    if (\is_array($configurator->copyTestFolders) && \count($configurator->copyTestFolders) > 0) {
90
        //        $file = __DIR__ . '/../testdata/images/';
91
        foreach (\array_keys($configurator->copyTestFolders) as $i) {
92
            $src  = $configurator->copyTestFolders[$i][0];
93
            $dest = $configurator->copyTestFolders[$i][1];
94
            $utility::rcopy($src, $dest);
95
        }
96
    }
97
    \redirect_header('../admin/index.php', 1, \constant('CO_' . $moduleDirNameUpper . '_' . 'SAMPLEDATA_SUCCESS'));
98
}
99
100
function saveSampleData()
101
{
102
    global $xoopsConfig;
103
104
    $configurator = new Common\Configurator();
105
106
    $moduleDirName      = \basename(\dirname(__DIR__));
107
    $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
108
109
    $tables = Helper::getHelper($moduleDirName)->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::getHelper($moduleDirName)->getModule()->getVar('mid')));
126
    $skipColumns[] = 'gperm_id';
127
    TableLoad::saveTableToYamlFile('group_permission', $exportFolder . 'group_permission.yml', $criteria, $skipColumns);
128
    unset($criteria);
129
130
    //  ---  COPY test folder files ---------------
131
    if (\is_array($configurator->copyTestFolders) && \count($configurator->copyTestFolders) > 0) {
132
        foreach (\array_keys($configurator->copyTestFolders) as $i) {
133
            $src  = $configurator->copyTestFolders[$i][1];
134
            $dest = $configurator->copyTestFolders[$i][0];
135
            Utility::rcopy($src, $dest);
136
        }
137
    }
138
    \redirect_header('../admin/index.php', 1, \constant('CO_' . $moduleDirNameUpper . '_' . 'SAVE_SAMPLEDATA_SUCCESS'));
139
}
140
141
function exportSchema()
142
{
143
    $moduleDirName      = \basename(\dirname(__DIR__));
144
    $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
0 ignored issues
show
The assignment to $moduleDirNameUpper is dead and can be removed.
Loading history...
145
146
    try {
147
        // TODO set exportSchema
148
        //        $migrate = new Wggithub\Migrate($moduleDirName);
149
        //        $migrate->saveCurrentSchema();
150
        //
151
        //        \redirect_header('../admin/index.php', 1, \constant('CO_' . $moduleDirNameUpper . '_' . 'EXPORT_SCHEMA_SUCCESS'));
152
    } catch (\Exception $e) {
0 ignored issues
show
catch (\Exception $e) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
153
        exit(\constant('CO_' . $moduleDirNameUpper . '_' . 'EXPORT_SCHEMA_ERROR'));
154
    }
155
156
}
157
158
/**
159
 * loadTableFromArrayWithReplace
160
 *
161
 * @param string $table  value with should be used insead of original value of $search
162
 *
163
 * @param array  $data   array of rows to insert
164
 *                       Each element of the outer array represents a single table row.
165
 *                       Each row is an associative array in 'column' => 'value' format.
166
 * @param string $search name of column for which the value should be replaced
167
 * @param        $replace
168
 * @return int number of rows inserted
169
 */
170
function loadTableFromArrayWithReplace($table, $data, $search, $replace)
171
{
172
    /** @var \XoopsDatabase */
173
    $db = \XoopsDatabaseFactory::getDatabaseConnection();
174
    $prefixedTable = $db->prefix($table);
175
    $count = 0;
176
    $sql = 'DELETE FROM ' . $prefixedTable . ' WHERE `' . $search . '`=' . $db->quote($replace);
177
    $db->queryF($sql);
178
    foreach ($data as $row) {
179
        $insertInto = 'INSERT INTO ' . $prefixedTable . ' (';
180
        $valueClause = ' VALUES (';
181
        $first = true;
182
        foreach ($row as $column => $value) {
183
            if ($first) {
184
                $first = false;
185
            } else {
186
                $insertInto .= ', ';
187
                $valueClause .= ', ';
188
            }
189
            $insertInto .= $column;
190
            if ($search === $column) {
191
                $valueClause .= $db->quote($replace);
192
            } else {
193
                $valueClause .= $db->quote($value);
194
            }
195
        }
196
        $sql = $insertInto . ') ' . $valueClause . ')';
197
        $result = $db->queryF($sql);
198
        if (false !== $result) {
199
            ++$count;
200
        }
201
    }
202
203
    return $count;
204
}
205