saveSampleData()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
nc 4
nop 0
dl 0
loc 21
rs 9.8666
c 1
b 0
f 0
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 XoopsModules\Mastopgo2;
18
use XoopsModules\Mastopgo2\Common;
19
use XoopsModules\Mastopgo2\Utility;
20
21
require_once dirname(dirname(dirname(__DIR__))) . '/include/cp_header.php';
22
require dirname(__DIR__) . '/preloads/autoloader.php';
23
24
$op = \Xmf\Request::getCmd('op', '');
25
26
$moduleDirName = basename(dirname(__DIR__));
27
$moduleDirNameUpper = mb_strtoupper($moduleDirName);
28
29
$helper = Mastopgo2\Helper::getInstance();
30
// Load language files
31
$helper->loadLanguage('common');
32
33
switch ($op) {
34
    case 'load':
35
        if (\Xmf\Request::hasVar('ok', 'REQUEST') && 1 == $_REQUEST['ok']) {
36
            if (!$GLOBALS['xoopsSecurity']->check()) {
37
                redirect_header('../admin/index.php', 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
38
            }
39
            loadSampleData();
40
        } else {
41
            xoops_cp_header();
42
            xoops_confirm(['ok' => 1, 'op' => 'load'], 'index.php', sprintf(constant('CO_' . $moduleDirNameUpper . '_' . 'ADD_SAMPLEDATA_OK')), constant('CO_' . $moduleDirNameUpper . '_' . 'CONFIRM'), true);
43
            xoops_cp_footer();
44
        }
45
        break;
46
    case 'save':
47
        saveSampleData();
48
        break;
49
}
50
51
// XMF TableLoad for SAMPLE data
52
53
function loadSampleData()
54
{
55
    global $xoopsConfig;
56
57
    $moduleDirName = basename(dirname(__DIR__));
58
    $moduleDirNameUpper = mb_strtoupper($moduleDirName);
59
60
    $utility      = new Mastopgo2\Utility();
61
    $configurator = new Common\Configurator();
62
63
    $tables = \Xmf\Module\Helper::getHelper($moduleDirName)->getModule()->getInfo('tables');
64
65
    $language = 'english';
66
    if (is_dir(__DIR__ . '/' . $xoopsConfig['language'])) {
67
        $language = $xoopsConfig['language'];
68
    }
69
70
    $languageFolder = __DIR__ . '/' . $language . '/';
71
72
    foreach ($tables as $table) {
73
        $tabledata = \Xmf\Yaml::readWrapped($languageFolder . $table . '.yml');
74
        \Xmf\Database\TableLoad::truncateTable($table);
75
        \Xmf\Database\TableLoad::loadTableFromArray($table, $tabledata);
0 ignored issues
show
Bug introduced by
It seems like $tabledata can also be of type boolean; however, parameter $data of Xmf\Database\TableLoad::loadTableFromArray() does only seem to accept array, 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

75
        \Xmf\Database\TableLoad::loadTableFromArray($table, /** @scrutinizer ignore-type */ $tabledata);
Loading history...
76
    }
77
78
    //  ---  COPY test folder files ---------------
79
    if (is_array($configurator->copyTestFolders) && count($configurator->copyTestFolders) > 0) {
80
        //        $file = __DIR__ . '/../testdata/images/';
81
        foreach (array_keys($configurator->copyTestFolders) as $i) {
82
            $src  = $configurator->copyTestFolders[$i][0];
83
            $dest = $configurator->copyTestFolders[$i][1];
84
            $utility::rcopy($src, $dest);
85
        }
86
    }
87
    redirect_header('../admin/index.php', 1, constant('CO_' . $moduleDirNameUpper . '_' . 'SAMPLEDATA_SUCCESS'));
88
}
89
90
function saveSampleData()
91
{
92
    global $xoopsConfig;
93
94
    $moduleDirName = basename(dirname(__DIR__));
95
    $moduleDirNameUpper = mb_strtoupper($moduleDirName);
96
97
    $tables = \Xmf\Module\Helper::getHelper($moduleDirName)->getModule()->getInfo('tables');
98
99
    $languageFolder = __DIR__ . '/' . $xoopsConfig['language'];
100
    if (!file_exists($languageFolder . '/')) {
101
       Utility::createFolder($languageFolder . '/');
102
    }
103
    $exportFolder = $languageFolder . '/Exports-' . date('Y-m-d-H-i-s') . '/';
104
    Utility::createFolder($exportFolder);
105
106
    foreach ($tables as $table) {
107
        \Xmf\Database\TableLoad::saveTableToYamlFile($table, $exportFolder . $table . '.yml');
108
    }
109
110
    redirect_header('../admin/index.php', 1, constant('CO_' . $moduleDirNameUpper . '_' . 'SAMPLEDATA_SUCCESS'));
111
}
112
113
function exportSchema()
114
{
115
    $moduleDirName      = basename(dirname(__DIR__));
116
    $moduleDirNameUpper = mb_strtoupper($moduleDirName);
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirNameUpper is dead and can be removed.
Loading history...
117
118
    try {
119
        // TODO set exportSchema
120
        //        $migrate = new Mastopgo2\Migrate($moduleDirName);
121
        //        $migrate->saveCurrentSchema();
122
        //
123
        //        redirect_header('../admin/index.php', 1, constant('CO_' . $moduleDirNameUpper . '_' . 'EXPORT_SCHEMA_SUCCESS'));
124
    }
125
    catch (\Exception $e) {
0 ignored issues
show
Unused Code introduced by
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...
126
        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...
127
    }
128
129
}
130