admin_helper.php ➔ create_language_select()   B
last analyzed

Complexity

Conditions 10
Paths 6

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 6
nop 4
dl 0
loc 28
rs 7.6666
c 0
b 0
f 0

How to fix   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
if (!function_exists('check_admin_redirect')) {
4
5
    /**
6
     * @param array $languages
7
     * @param string $locale
8
     * @param string $url
9
     * @param bool|FALSE $pjax
10
     * @return string
11
     */
12
    function create_language_select($languages, $locale, $url, $pjax = FALSE) {
13
14
        if (count($languages) > 1 && \MY_Controller::isPremiumCMS()) {
15
            $html = "<div class='dropdown d-i_b'>";
16
            foreach ($languages as $language) {
17
                if ($language['identif'] == $locale) {
18
                    $html .= "<a class='btn dropdown-toggle btn-small' data-toggle='dropdown' data-lan='" . $language['identif'] . "' href='#'>";
19
                    $html .= $language['lang_name'];
20
                    $locale = $language['identif'];
21
                    $html .= "<input type='hidden' name='Locale' value='" . $language['identif'] . "'/>";
22
                    $html .= "<span class='caret'></span>";
23
                    $html .= '</a>';
24
                }
25
            }
26
            $html .= "<ul class='dropdown-menu pull-right'>";
27
            foreach ($languages as $language) {
28
                if ($language['identif'] != $locale) {
29
                    $html .= '<li>';
30
                    $html .= "<a href='" . $url . '/' . $language['identif'] . "' class='" . ($pjax ? 'pjax' : '') . "'>" . $language['lang_name'] . '</a>';
31
                    $html .= '</li>';
32
                }
33
            }
34
            if (count($languages) > 1) {
35
                $html .= '</ul></div>';
36
            }
37
        }
38
        return $html ?: '';
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    function create_admin_language_select() {
45
46
        $CI = &get_instance();
47
        $languages = $CI->db->select('lang_name, locale')->order_by('lang_name')->get('languages')->result_array();
48
        $current_locale = $CI->config->item('language');
49
        $current_language = lang('English', 'admin');
50
51
        if (count($languages)) {
52
            $html = '';
53
54
            $english_exists = FALSE;
55
            foreach ($languages as $language) {
56
                $html .= '<li><a href="/admin/settings/switch_admin_lang/' . $language['locale'] . '">' . $language['lang_name'] . '</a></li>';
57
                if ($current_locale == $language['locale']) {
58
                    $current_language = $language['lang_name'];
59
                }
60
61
                if (!$english_exists && strstr($language['locale'], 'en')) {
62
                    $english_exists = TRUE;
63
                }
64
            }
65
66
            if (!$english_exists) {
67
                $html = '<li><a href="/admin/settings/switch_admin_lang/en_US">' . lang('English', 'admin') . '</a></li>' . $html;
68
            }
69
70
            $html = '<div class="dropup d-i_b"><button type="button" class="btn dropdown-toggle" data-toggle="dropdown">' .
71
                $current_language . '<span class="caret"></span></button>
72
            <ul class="dropdown-menu">' .
73
                $html .
74
                '</ul>
75
            </div>';
76
        }
77
        return $html ?: '';
78
    }
79
80
    /**
81
     * @param array $cats
82
     * @param array $selected_cats
83
     */
84
    function build_cats_tree($cats, $selected_cats = []) {
85
86
        if (is_array($cats)) {
87
            foreach ($cats as $cat) {
88
                echo '<option';
89
                if (is_array($selected_cats)) {
90
                    foreach ($selected_cats as $k) {
91
                        if ($k == $cat['id']) {
92
                            echo " selected = 'selected' ";
93
                        }
94
                    }
95
                }
96
                echo " value='" . $cat['id'] . "'>";
97
                for ($i = 0; $i < $cat['level']; $i++) {
98
                    echo '-';
99
                }
100
                echo $cat['name'] . '</option>';
101
                if ($cat['subtree']) {
102
                    build_cats_tree($cat['subtree'], $selected_cats);
103
                }
104
            }
105
        }
106
    }
107
108
    /**
109
     * @param array $cats
110
     * @param null|int $item_id
111
     * @param int $level
112
     */
113
    function build_cats_tree_ul_li($cats, $item_id = NULL, $level = 0) {
114
        if (is_array($cats)) {
115
116
            $subst = '';
117
            if ($level !== 0) {
118
                $indents = 3 * ($level - 1);
119
                $subst = str_repeat('&nbsp;', $indents) . '<span class="simple_tree">↳</span>';
120
            }
121
122
            foreach ($cats as $cat) {
123
                echo '<li>';
124
                if ($cat['id'] == $item_id) {
125
                    echo "<b><a class='category_item' data-title='" . $cat['name'] . "' data-id='" . $cat['id'] . "' href='#'>" . $subst . $cat['name'] . '</a></b>';
126
                } else {
127
128
                    echo "<a class='category_item' data-title='" . $cat['name'] . "' data-id='" . $cat['id'] . "' href='#'>" . $subst . $cat['name'] . '</a>';
129
                }
130
                if ($cat['subtree']) {
131
                    build_cats_tree_ul_li($cat['subtree'], $item_id, ++$level);
132
                }
133
            }
134
        }
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    function getCMSNumber() {
141
142
        return IMAGECMS_NUMBER;
143
    }
144
145
}
146
147
if (!function_exists('get_templates')) {
148
149
    /**
150
     * @return array|bool
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array|false.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
151
     */
152
    function get_templates() {
153
154
        $new_arr_shop = [];
155
        if ($handle = opendir(TEMPLATES_PATH)) {
156
            while (false !== ($file = readdir($handle))) {
157
                if (false === strpos($file, '.') && $file != 'administrator' && $file != 'modules' && !stristr($file, '_mobile')) {
158
                    if (!is_file(TEMPLATES_PATH . $file)) {
159
                        if (is_dir(TEMPLATES_PATH . $file . '/shop/')) {
160
                            $new_arr_shop[$file] = $file;
161
                        } else {
162
                            $new_arr[$file] = $file;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$new_arr was never initialized. Although not strictly required by PHP, it is generally a good practice to add $new_arr = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
163
                        }
164
                    }
165
                }
166
            }
167
            closedir($handle);
168
169
            $templates = SHOP_INSTALLED ? $new_arr_shop : $new_arr;
170
            array_multisort($templates);
171
172
            return $templates;
173
        }
174
175
        return false;
176
    }
177
178
}