exportSchema()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 1
nop 0
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
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       The XOOPS Project http://sourceforge.net/projects/xoops/
12
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
13
 * @package      Chess
14
 * @since        2.01
15
 * @author          Michael Beck (aka Mamba)
16
 */
17
18
use XoopsModules\Chess;
19
use XoopsModules\Chess\Common;
20
use XoopsModules\Chess\Utility;
21
22
require_once dirname(__DIR__, 3) . '/include/cp_header.php';
23
require dirname(__DIR__) . '/preloads/autoloader.php';
24
25
$op = \Xmf\Request::getCmd('op', '');
26
27
$moduleDirName      = basename(dirname(__DIR__));
28
$moduleDirNameUpper = mb_strtoupper($moduleDirName);
29
30
$helper = Chess\Helper::getInstance();
31
// Load language files
32
$helper->loadLanguage('common');
33
34
switch ($op) {
35
    case 'load':
36
        if (\Xmf\Request::hasVar('ok', 'REQUEST') && 1 == $_REQUEST['ok']) {
37
            if (!$GLOBALS['xoopsSecurity']->check()) {
38
                redirect_header('../admin/index.php', 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
39
            }
40
41
            loadSampleData();
42
        } else {
43
            xoops_cp_header();
44
45
            xoops_confirm(['ok' => 1, 'op' => 'load'], 'index.php', sprintf(constant('CO_' . $moduleDirNameUpper . '_' . 'ADD_SAMPLEDATA_OK')), constant('CO_' . $moduleDirNameUpper . '_' . 'CONFIRM'), true);
46
47
            xoops_cp_footer();
48
        }
49
        break;
50
    case 'save':
51
        saveSampleData();
52
        break;
53
}
54
55
// XMF TableLoad for SAMPLE data
56
57
function loadSampleData()
58
{
59
    global $xoopsConfig;
60
61
    $moduleDirName = basename(dirname(__DIR__));
62
63
    $moduleDirNameUpper = mb_strtoupper($moduleDirName);
64
65
    $utility = new Chess\Utility();
66
67
    $configurator = new Common\Configurator();
68
69
    $tables = \Xmf\Module\Helper::getHelper($moduleDirName)->getModule()->getInfo('tables');
70
71
    $language = 'english/';
72
73
    if (is_dir(__DIR__ . '/' . $xoopsConfig['language'])) {
74
        $language = $xoopsConfig['language'] . '/';
75
    }
76
77
    foreach ($tables as $table) {
78
        $tabledata = \Xmf\Yaml::readWrapped($language . $table . '.yml');
79
80
        if (is_array($tabledata)) {
81
            \Xmf\Database\TableLoad::truncateTable($table);
82
83
            \Xmf\Database\TableLoad::loadTableFromArray($table, $tabledata);
84
        }
85
    }
86
87
    //  ---  COPY test folder files ---------------
88
89
    if (is_array($configurator->copyTestFolders) && count($configurator->copyTestFolders) > 0) {
90
        //        $file =  dirname(__DIR__) . '/testdata/images/';
91
92
        foreach (array_keys($configurator->copyTestFolders) as $i) {
93
            $src = $configurator->copyTestFolders[$i][0];
94
95
            $dest = $configurator->copyTestFolders[$i][1];
96
97
            $utility::rcopy($src, $dest);
98
        }
99
    }
100
101
    redirect_header('../admin/index.php', 1, constant('CO_' . $moduleDirNameUpper . '_' . 'SAMPLEDATA_SUCCESS'));
102
}
103
104
function saveSampleData()
105
{
106
    global $xoopsConfig;
107
108
    $moduleDirName = basename(dirname(__DIR__));
109
110
    $moduleDirNameUpper = mb_strtoupper($moduleDirName);
111
112
    $tables = \Xmf\Module\Helper::getHelper($moduleDirName)->getModule()->getInfo('tables');
113
114
    $language = 'english/';
115
116
    if (is_dir(__DIR__ . '/' . $xoopsConfig['language'])) {
117
        $language = $xoopsConfig['language'] . '/';
118
    }
119
120
    $languageFolder = __DIR__ . '/' . $language;
121
122
    if (!file_exists($languageFolder . '/')) {
123
        Utility::createFolder($languageFolder . '/');
124
    }
125
126
    $exportFolder = $languageFolder . '/Exports-' . date('Y-m-d-H-i-s') . '/';
127
128
    Utility::createFolder($exportFolder);
129
130
    foreach ($tables as $table) {
131
        \Xmf\Database\TableLoad::saveTableToYamlFile($table, $exportFolder . $table . '.yml');
132
    }
133
134
    redirect_header('../admin/index.php', 1, constant('CO_' . $moduleDirNameUpper . '_' . 'SAMPLEDATA_SUCCESS'));
135
}
136
137
function exportSchema()
138
{
139
    $moduleDirName = basename(dirname(__DIR__));
140
141
    $moduleDirNameUpper = mb_strtoupper($moduleDirName);
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirNameUpper is dead and can be removed.
Loading history...
142
143
    try {
144
        // TODO set exportSchema
145
        //        $migrate = new Chess\Migrate($moduleDirName);
146
        //        $migrate->saveCurrentSchema();
147
        //
148
        //        redirect_header('../admin/index.php', 1, constant('CO_' . $moduleDirNameUpper . '_' . 'EXPORT_SCHEMA_SUCCESS'));
149
    } catch (\Throwable $e) {
0 ignored issues
show
Unused Code introduced by
catch (\Throwable $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...
150
        exit(constant('CO_' . $moduleDirNameUpper . '_' . 'EXPORT_SCHEMA_ERROR'));
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
151
    }
152
}
153