Passed
Push — master ( 22887c...b0a7cb )
by Michael
04:05 queued 02:12
created

languageList()   C

Complexity

Conditions 13
Paths 57

Size

Total Lines 103
Code Lines 90

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 90
nc 57
nop 1
dl 0
loc 103
rs 5.5115
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * xLanguage module (eXtensible Language Management For XOOPS)
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright    XOOPS Project (https://xoops.org)
13
 * @license      {@link http://www.gnu.org/licenses/gpl-2.0.html GNU Public License}
14
 * @package      xlanguage
15
 * @since        2.0
16
 * @author       D.J.(phppp) [email protected]
17
 **/
18
19
use XoopsModules\Xlanguage;
20
21
require_once __DIR__ . '/admin_header.php';
22
23
require_once XOOPS_ROOT_PATH . '/modules/xlanguage/include/vars.php';
24
25
$op = '';
26
if (isset($_POST)) {
27
    foreach ($_POST as $k => $v) {
28
        ${$k} = $v;
29
    }
30
}
31
if (isset($_GET)) {
32
    foreach ($_GET as $k => $v) {
33
        ${$k} = $v;
34
    }
35
}
36
37
define('XLANG_CONFIG_LINK', "<a href='main.php' target='_self'>" . _AM_XLANGUAGE_CONFIG . '</a>');
38
39
/** @var \XoopsModules\Xlanguage\Helper $helper */
40
$helper = \XoopsModules\Xlanguage\Helper::getInstance();
41
/** @var \XoopsModules\Xlanguage\LanguageHandler $xlanguageHandler */
42
$xlanguageHandler = $helper->getHandler('Language');
43
$xlanguageHandler->loadConfig();
44
45
switch ($op) {
46
    case 'del':
47
        if (!isset($_POST['ok']) || 1 != $_POST['ok']) {
48
            xoops_cp_header();
49
            $aboutAdmin = \Xmf\Module\Admin::getInstance();
50
            $adminObject->displayNavigation(basename(__FILE__));
51
            //            echo "<h4>" . XLANG_CONFIG_LINK . "</h4>";
52
            xoops_confirm(['op' => 'del', 'type' => \Xmf\Request::getString('type', 0, 'GET'), 'lang_id' => \Xmf\Request::getInt('lang_id', 0, 'GET'), 'ok' => 1], 'main.php', _AM_XLANGUAGE_DELETE_CFM);
53
        } else {
54
            $isBase = true;
55
            if (isset($type) && 'ext' === $type) {
56
                $isBase = false;
57
            }
58
            $lang = $xlanguageHandler->get($lang_id, $isBase);
59
            $xlanguageHandler->delete($lang);
60
            redirect_header('main.php', 2, _AM_XLANGUAGE_DELETED);
61
        }
62
        break;
63
    case 'save':
64
        $isBase = true;
65
        if (isset($type) && 'ext' === $type) {
66
            $isBase = false;
67
        }
68
        if (isset($lang_id) && $lang_id > 0) {
69
            $lang = $xlanguageHandler->get($lang_id, $isBase);
70
        } else {
71
            $lang = $xlanguageHandler->create(true, $isBase);
72
        }
73
        $lang_name = preg_replace("/[^a-zA-Z0-9\_\-]/", '', $lang_name);
74
75
        $lang->setVar('lang_name', $lang_name);
76
        $lang->setVar('lang_desc', $lang_desc);
77
        $lang->setVar('lang_code', $lang_code);
78
        $lang->setVar('lang_charset', $lang_charset);
79
        $lang->setVar('lang_image', $lang_image);
80
        if (!$isBase) {
81
            $lang->setVar('lang_base', $lang_base);
82
        }
83
        $lang->setVar('weight', $weight);
84
        $xlanguageHandler->insert($lang);
85
        redirect_header('main.php', 2, _AM_XLANGUAGE_SAVED);
86
        break;
87
    case 'edit':
88
        xoops_cp_header();
89
        $aboutAdmin = \Xmf\Module\Admin::getInstance();
90
        $adminObject->displayNavigation(basename(__FILE__));
91
        // echo "<h4>" . XLANG_CONFIG_LINK . "</h4>";
92
        // echo "<br>";
93
        echo '<h4>' . _AM_XLANGUAGE_EDITLANG . '</h4>';
94
        $isBase = true;
95
        if (isset($type) && 'ext' === $type) {
96
            $isBase = false;
97
        }
98
        if (isset($lang_id) && $lang_id > 0) {
99
            $lang = $xlanguageHandler->get($lang_id, $isBase);
100
        } elseif (isset($lang_name)) {
101
            $lang = $xlanguageHandler->getByName($lang_name, $isBase);
102
        } else {
103
            $lang = $xlanguageHandler->create(true, $isBase);
104
        }
105
        $lang_name    = $lang->getVar('lang_name');
106
        $lang_desc    = $lang->getVar('lang_desc');
107
        $lang_code    = $lang->getVar('lang_code');
108
        $lang_charset = $lang->getVar('lang_charset');
109
        $lang_image   = $lang->getVar('lang_image');
110
        $weight       = $lang->getVar('weight');
111
        if (!$isBase) {
112
            $lang_base = $lang->getVar('lang_base');
113
        }
114
        require_once __DIR__ . '/langform.inc.php';
115
        break;
116
    case 'add':
117
        xoops_cp_header();
118
        $aboutAdmin = \Xmf\Module\Admin::getInstance();
119
        //        echo "<h4>" . XLANG_CONFIG_LINK . "</h4>";
120
        //        echo "<br>";
121
        //        echo "<h4>" . _AM_XLANGUAGE_ADDLANG . "</h4>";
122
        if (isset($type) && 'ext' === $type) {
123
            $isBase = false;
124
            $adminObject->displayNavigation('main.php?op=add&type=ext');
125
        } else {
126
            $isBase = true;
127
            $adminObject->displayNavigation('main.php?op=add&type=base');
128
        }
129
        $lang_name    = '';
130
        $lang_desc    = '';
131
        $lang_code    = '';
132
        $lang_charset = '';
133
        $lang_image   = '';
134
        $weight       = 1;
135
        $lang_base    = '';
136
        require_once __DIR__ . '/langform.inc.php';
137
        break;
138
    case 'createconfig':
139
        Xlanguage\Utility::createConfig();
140
        redirect_header('main.php', 1, _AM_XLANGUAGE_CREATED);
141
142
        break;
143
    case 'default':
144
    default:
145
        xoops_cp_header();
146
        $adminObject = \Xmf\Module\Admin::getInstance();
147
        $adminObject->displayNavigation(basename(__FILE__));
148
149
        // if (TDMDownloads_checkModuleAdmin()) {
150
        // $adminObject = \Xmf\Module\Admin::getInstance();
151
        // $adminObject->displayNavigation('downloads.php');
152
        $adminObject->addItemButton(_MI_XLANGUAGE_ADMENU1, 'main.php?op=add&type=base', 'add');
153
        $adminObject->addItemButton(_MI_XLANGUAGE_ADMENU2, 'main.php?op=add&type=ext', 'insert_table_row');
154
155
        $adminObject->displayButton('left');
156
        //        }
157
158
        //        echo "<h4>" . XLANG_CONFIG_LINK . "</h4>";
159
        languageList($xlanguageHandler);
160
        $configfile_status = (@is_readable(XLANGUAGE_CONFIG_FILE)) ? _AM_XLANGUAGE_CONFIGOK : _AM_XLANGUAGE_CONFIGNOTOK;
161
        echo "<table width='100%' border='0' cellspacing='1' class='outer'><tr><td class=\"odd\"><br>";
162
        //        echo " - <b><a href='index.php?op=add&amp;type=base'>" . _AM_XLANGUAGE_ADDBASE . "</a></b><br><br>\n";
163
        //        echo " - <b><a href='index.php?op=add&amp;type=ext'>" . _AM_XLANGUAGE_ADDEXT . "</a></b><br><br>\n";
164
        echo '<b>' . $configfile_status . '</b>: ' . XLANGUAGE_CONFIG_FILE . " (<a href='main.php?op=createconfig' title='" . _AM_XLANGUAGE_CREATECONFIG . "'>" . _AM_XLANGUAGE_CREATECONFIG . "</a>)<br><br>\n";
165
        //        echo " - <b><a href='about.php'>" . _AM_XLANGUAGE_ABOUT . "</a></b>";
166
        echo '</td></tr></table>';
167
        break;
168
}
169
xoops_cp_footer();
170
171
/**
172
 * @param \XoopsModules\Xlanguage\LanguageHandler $xlanguageHandler
173
 */
174
function languageList(Xlanguage\LanguageHandler $xlanguageHandler)
175
{
176
//    global $xlanguageHandler, $xoopsModule;
177
178
    global $pathIcon16;
179
180
    $lang_list = $xlanguageHandler->getAllList();
181
182
    if ($lang_list && is_array($lang_list)) {
183
        echo "<table width='100%' border='0' cellspacing='1' class='outer'><tr><td class=\"odd\">";
184
        echo "<div style='text-align: center;'><b><h4>" . _AM_XLANGUAGE_LANGLIST . '</h4></b><br>';
185
        echo "<table class='outer' width='100%' border='0' cellpadding='0' cellspacing='0' ><tr class='bg2'><th align='center'>"
186
             . _AM_XLANGUAGE_DESC
187
             . "</th><th align='center'>"
188
             . _AM_XLANGUAGE_NAME
189
             . "</th><th align='center'>"
190
             . _AM_XLANGUAGE_CHARSET
191
             . "</th><th align='center'>"
192
             . _AM_XLANGUAGE_CODE
193
             . "</th><th align='center'>"
194
             . _AM_XLANGUAGE_IMAGE
195
             . "</th><th align='center'>"
196
             . _AM_XLANGUAGE_WEIGHT
197
             . "</th><th align='center'>"
198
             . _AM_XLANGUAGE_BASE
199
             . "</th><th align='center'>"
200
             . _AM_XLANGUAGE_ACTION
201
             . "</th></tr>\n";
202
        $class = 'even';
203
        foreach (array_keys($lang_list) as $lang_name) {
204
            $lang     = &$lang_list[$lang_name];
205
            $isOrphan = true;
206
            if (isset($lang['base'])) {
207
                echo "<tr>\n";
208
                echo "<td class='$class' >" . $lang['base']->getVar('lang_desc') . "</td>\n";
209
                echo "<td class='$class' ><b>" . $lang['base']->getVar('lang_name') . "</b></td>\n";
210
                echo "<td class='$class' ><b>" . $lang['base']->getVar('lang_charset') . "</b></td>\n";
211
                echo "<td class='$class' >" . $lang['base']->getVar('lang_code') . "</td>\n";
212
                $lang_image = 'noflag.gif';
213
                if (is_readable(XOOPS_ROOT_PATH . '/modules/xlanguage/assets/images/' . $lang['base']->getVar('lang_image'))) {
214
                    $lang_image = $lang['base']->getVar('lang_image');
215
                }
216
                echo "<td class='$class' ><img src='" . XOOPS_URL . '/modules/xlanguage/assets/images/' . $lang_image . "' alt='" . $lang['base']->getVar('lang_desc') . "'></td>\n";
217
                echo "<td class='$class' >" . $lang['base']->getVar('weight') . "</td>\n";
218
                echo "<td class='$class' >&#216;</td>\n";
219
                echo "<td class='$class' ><a href='main.php?op=edit&amp;type=base&amp;lang_id="
220
                     . $lang['base']->getVar('lang_id')
221
                     . "'><img src="
222
                     . $pathIcon16
223
                     . '/edit.png title='
224
                     . _EDIT
225
                     . "></a>\n"
226
                     . "<a href='main.php?op=del&amp;type=base&amp;lang_id="
227
                     . $lang['base']->getVar('lang_id')
228
                     . "'><img src="
229
                     . $pathIcon16
230
                     . '/delete.png title='
231
                     . _DELETE
232
                     . "></td>\n";
233
                echo "</tr>\n";
234
                $isOrphan = false;
235
                $class    = ('odd' === $class) ? 'even' : 'odd';
236
            }
237
            if (!isset($lang['ext']) || count($lang['ext']) < 1) {
238
                continue;
239
            }
240
            foreach ($lang['ext'] as $ext) {
241
                echo "<tr>\n";
242
                echo "<td class='$class' >" . $ext->getVar('lang_desc') . "</td>\n";
243
                echo "<td class='$class' >" . $ext->getVar('lang_name') . "</td>\n";
244
                echo "<td class='$class' ><b>" . $ext->getVar('lang_charset') . "</b></td>\n";
245
                echo "<td class='$class' >" . $ext->getVar('lang_code') . "</td>\n";
246
                $lang_image = 'noflag.gif';
247
                if (is_readable(XOOPS_ROOT_PATH . '/modules/xlanguage/assets/images/' . $ext->getVar('lang_image'))) {
248
                    $lang_image = $ext->getVar('lang_image');
249
                }
250
                echo "<td class='$class' ><img src='" . XOOPS_URL . '/modules/xlanguage/assets/images/' . $lang_image . "' alt='" . $ext->getVar('lang_desc') . "'></td>\n";
251
                echo "<td class='$class' >" . $ext->getVar('weight') . "</td>\n";
252
                $lang_base = $isOrphan ? "<span style='color:#ff0000'>" . $ext->getVar('lang_base') . '</span>' : $ext->getVar('lang_base');
253
                echo "<td class='$class' ><b>" . $lang_base . "</b></td>\n";
254
                echo "<td class='$class' ><a href='main.php?op=edit&amp;type=ext&amp;lang_id="
255
                     . $ext->getVar('lang_id')
256
                     . "'><img src="
257
                     . $pathIcon16
258
                     . '/edit.png title='
259
                     . _EDIT
260
                     . "></a>\n"
261
                     . "<a href='main.php?op=del&amp;type=ext&amp;lang_id="
262
                     . $ext->getVar('lang_id')
263
                     . "'><img src="
264
                     . $pathIcon16
265
                     . '/delete.png title='
266
                     . _DELETE
267
                     . "></td>\n";
268
                echo "</tr>\n";
269
            }
270
            echo "<tr><td colspan='9' ></td></tr>\n";
271
            $class = ('odd' === $class) ? 'even' : 'odd';
272
        }
273
274
        echo "</table></div>\n";
275
        echo '</td></tr></table>';
276
        echo '<br>';
277
    }
278
}
279