languageList()   C
last analyzed

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