Passed
Push — 1.11.x ( bce6cd...c146d9 )
by Angel Fernando Quiroz
12:25
created

main/admin/sub_language.php (1 issue)

Check for undefined variables.

Best Practice Comprehensibility Minor
1
<?php
2
/* For licensing terms, see /license.txt */
3
/**
4
 * Script for sub-language administration.
5
 *
6
 * @package chamilo.admin.sub_language
7
 */
8
$cidReset = true;
9
$this_script = 'sub_language';
10
require_once __DIR__.'/../inc/global.inc.php';
11
$this_section = SECTION_PLATFORM_ADMIN;
12
13
api_protect_admin_script();
14
$htmlHeadXtra[] = '<script>
15
 $(function () {
16
    $(".save").click(function() {
17
        var button_name=$(this).attr("name");
18
        var button_array=button_name.split("|");
19
        var button_name=button_array[1];
20
        var file_id=button_array[2];
21
        var is_variable_language="$"+button_name;
22
23
        var is_new_language = $("#txtid_"+file_id+"_"+button_name).val();
24
        if (is_new_language == undefined) {
25
            is_new_language="_";
26
        }
27
        if (is_new_language.length>0 && is_new_language!="_" && file_id!="" && button_name!="") {
28
            $.ajax({
29
                contentType: "application/x-www-form-urlencoded",
30
                beforeSend: function(myObject) {
31
                    $("#div_message_information_id").html("<div class=\"alert alert-info\"><img src=\'../inc/lib/javascript/indicator.gif\' /></div>");
32
                },
33
                type: "POST",
34
                url: "../admin/sub_language_ajax.inc.php",
35
                data: {
36
                    \'new_language\': is_new_language,
37
                    \'variable_language\': is_variable_language,
38
                    \'file_id\': file_id,
39
                    \'id\': '.intval($_REQUEST['id']).',
40
                    \'sub\': '.intval($_REQUEST['sub_language_id']).',
41
                    \'sub_language_id\': '.intval($_REQUEST['sub_language_id']).'
42
                },
43
                success: function(datos) {
44
                    if (datos == "1") {
45
                        $("#div_message_information_id").html(\''.Display::return_message(get_lang('TheNewWordHasBeenAdded'), 'success').'\');
46
                    } else {
47
                        $("#div_message_information_id").html("<div class=\"alert alert-warning\">" + datos +"</div>");
48
                    }
49
                }
50
            });
51
        } else {
52
            $("#div_message_information_id").html(\''.Display::return_message(get_lang('FormHasErrorsPleaseComplete'), 'error').'\');
53
        }
54
    });
55
});
56
</script>';
57
58
/**
59
 * Main code.
60
 */
61
// setting the name of the tool
62
$tool_name = get_lang('CreateSubLanguage');
63
// setting breadcrumbs
64
$interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')];
65
$interbreadcrumb[] = ['url' => 'languages.php', 'name' => get_lang('PlatformLanguages')];
66
67
$sublanguage_folder_error = false;
68
69
if (isset($_GET['id']) && $_GET['id'] == strval(intval($_GET['id']))) {
70
    $language_name = SubLanguageManager::get_name_of_language_by_id($_GET['id']);
71
    $sub_language_name = SubLanguageManager::get_name_of_language_by_id($_GET['sub_language_id']);
72
    $all_data_of_language = SubLanguageManager::get_all_information_of_language($_GET['id']);
73
    $all_data_of_sublanguage = SubLanguageManager::get_all_information_of_language($_GET['sub_language_id']);
74
    $sub_language_file = api_get_path(SYS_LANG_PATH).$all_data_of_sublanguage['dokeos_folder'];
75
76
    if (!file_exists($sub_language_file) || !is_writable($sub_language_file)) {
77
        $sublanguage_folder_error = $sub_language_file.' '.get_lang('IsNotWritable');
78
    }
79
    if (SubLanguageManager::check_if_exist_language_by_id($_GET['id']) === true) {
80
        $language_id_exist = true;
81
    } else {
82
        $language_id_exist = false;
83
    }
84
} else {
85
    $language_name = '';
86
    $language_id_exist = false;
87
}
88
89
$intro = sprintf(get_lang('RegisterTermsOfSubLanguageForX'), strtolower($sub_language_name));
90
$path_folder = api_get_path(SYS_LANG_PATH).$all_data_of_language['dokeos_folder'];
91
92
if (!is_dir($path_folder) || strlen($all_data_of_language['dokeos_folder']) == 0) {
93
    api_not_allowed(true);
94
}
95
96
Display :: display_header($language_name);
97
98
echo '<div class="actions-message" >';
99
echo $intro;
100
echo '<br />';
101
printf(get_lang('ParentLanguageX'), $language_name);
102
echo '</div>';
103
echo '<br />';
104
$txt_search_word = (!empty($_REQUEST['txt_search_word']) ? Security::remove_XSS($_REQUEST['txt_search_word']) : '');
105
$html = '<div style="float:left" class="actions">';
106
$html .= '<form style="float:left"  id="Searchlanguage" name="Searchlanguage" method="GET" action="sub_language.php">';
107
$html .= '&nbsp;'.get_lang('OriginalName').'&nbsp; :&nbsp;';
108
109
$html .= '<input name="id" type="hidden"  id="id" value="'.Security::remove_XSS($_REQUEST['id']).'" />';
110
$html .= '<input name="sub_language_id" type="hidden"  id="id" value="'.Security::remove_XSS($_REQUEST['sub_language_id']).'" />';
111
$html .= '<input name="txt_search_word" type="text" size="50"  id="txt_search_word" value="'.$txt_search_word.'" />';
112
$html .= "&nbsp;".'<button name="SubmitSearchLanguage" class="search" type="submit">'.get_lang('Search').'</button>';
113
$html .= '</form>';
114
$html .= '</div>';
115
echo $html;
116
echo '<br /><br /><br />';
117
if (!empty($sublanguage_folder_error)) {
118
    echo Display::return_message($sublanguage_folder_error, 'warning');
119
}
120
echo '<div id="div_message_information_id">&nbsp;</div>';
121
122
/**
123
 * @param $term The term to search
124
 * @param bool $search_in_variable     The search will include the variable definition of the term
125
 * @param bool $search_in_english      The search will include the english language variables
126
 * @param bool $search_in_parent       The search will include the parent language variables of the sub language
127
 * @param bool $search_in_sub_language The search will include the sub language variables
128
 *
129
 * @author Julio Montoya
130
 *
131
 * @return array
132
 */
133
function search_language_term(
134
    $term,
135
    $search_in_variable = true,
136
    $search_in_english = true,
137
    $search_in_parent = true,
138
    $search_in_sub_language = true
139
) {
140
    //These the $_REQUEST['id'] and the $_REQUEST['sub_language_id'] variables are process in global.inc.php (LOAD LANGUAGE FILES SECTION)
141
    /*
142
        These 4 arrays are set in global.inc.php with the condition that will be load from sub_language.php or sub_language_ajax.inc.php
143
        $english_language_array
144
        $parent_language_array
145
        $sub_language_array
146
        $language_files_to_load
147
    */
148
    global $language_files_to_load, $sub_language_array, $english_language_array, $parent_language_array;
149
    $language_files_to_load_keys = array_flip($language_files_to_load);
150
    $array_to_search = $parent_language_array;
151
    $list_info = [];
152
    $term = '/'.Security::remove_XSS(trim($_REQUEST['txt_search_word'])).'/i';
153
    //@todo optimize this foreach
154
    foreach ($language_files_to_load as $lang_file) {
155
        //searching in parent language of the sub language
156
        if ($search_in_parent) {
157
            $variables = $parent_language_array[$lang_file];
158
            foreach ($variables as $parent_name_variable => $parent_variable_value) {
159
                //arrays are avoided
160
                if (is_array($parent_variable_value)) {
161
                    continue;
162
                }
163
                $founded = false;
164
                // searching the item in the parent tool
165
                if (preg_match($term, $parent_variable_value) !== 0) {
166
                    $founded = true;
167
                }
168
                if ($founded) {
169
                    //loading variable from the english array
170
                    $sub_language_name_variable = isset($sub_language_array[$lang_file][$parent_name_variable])
171
                        ? $sub_language_array[$lang_file][$parent_name_variable]
172
                        : '';
173
                    //loading variable from the english array
174
                    $english_name_variable = $english_language_array[$lang_file][$parent_name_variable];
175
176
                    //config buttons
177
                    /*if (strlen($english_name_variable)>1500) {
178
                        $size =20;
179
                    } else {
180
                        $size =4;
181
                    }*/
182
183
                    $obj_text = Display::tag(
184
                        'textarea',
185
                        $sub_language_name_variable,
186
                        [
187
                            'rows' => 10,
188
                            'cols' => 40,
189
                            'name' => 'txt|'.$parent_name_variable.'|'.$language_files_to_load_keys[$lang_file],
190
                            'id' => 'txtid_'.$language_files_to_load_keys[$lang_file].'_'.$parent_name_variable,
191
                        ]
192
                    );
193
                    $obj_button = Display::button(
194
                        'btn|'.$parent_name_variable.'|'.$language_files_to_load_keys[$lang_file],
195
                        get_lang('Save'),
196
                        [
197
                            'class' => 'save  btn btn-default btn-sm',
198
                            'type' => 'button',
199
                            'id' => 'btnid_'.$parent_name_variable,
200
                        ]
201
                    );
202
203
                    $list_info[$parent_name_variable] = [
204
                        $lang_file.'.inc.php',
205
                        $parent_name_variable,
206
                        htmlentities($english_name_variable),
207
                        htmlentities($parent_variable_value),
208
                        $obj_text,
209
                        $obj_button,
210
                    ];
211
                }
212
            }
213
        }
214
215
        //search in english
216
        if ($search_in_english || $search_in_variable) {
217
            $variables = $english_language_array[$lang_file];
218
            foreach ($variables as $name_variable => $variable_value) {
219
                if (isset($list_info[$name_variable])) {
220
                    continue;
221
                }
222
223
                if (is_array($variable_value)) {
224
                    continue;
225
                }
226
227
                if (is_array($variable_value)) {
228
                    echo $lang_file;
229
                }
230
                $founded = false;
231
                if ($search_in_english && $search_in_variable) {
232
                    // searching the item in the parent tool
233
                    if (preg_match($term, $variable_value) !== 0 || preg_match($term, $name_variable) !== 0) {
234
                        $founded = true;
235
                    }
236
                } else {
237
                    if ($search_in_english) {
238
                        if (preg_match($term, $variable_value) !== 0) {
239
                            $founded = true;
240
                        }
241
                    } else {
242
                        if (preg_match($term, $name_variable) !== 0) {
243
                            $founded = true;
244
                        }
245
                    }
246
                }
247
248
                if ($founded) {
249
                    //loading variable from the english array
250
                    $sub_language_name_variable = null;
251
                    if (isset($sub_language_array[$lang_file][$name_variable])) {
252
                        $sub_language_name_variable = $sub_language_array[$lang_file][$name_variable];
253
                    }
254
                    $parent_variable_value = null;
255
                    if (isset($parent_language_array[$lang_file][$name_variable])) {
256
                        $parent_variable_value = $parent_language_array[$lang_file][$name_variable];
257
                    }
258
                    //config buttons
259
                    $obj_text = Display::tag(
260
                        'textarea',
261
                        $sub_language_name_variable,
262
                        [
263
                            'rows' => 10,
264
                            'cols' => 40,
265
                            'name' => 'txt|'.$name_variable.'|'.$language_files_to_load_keys[$lang_file],
266
                            'id' => 'txtid_'.$language_files_to_load_keys[$lang_file].'_'.$name_variable,
267
                        ]
268
                    );
269
                    $obj_button = Display::button(
270
                        'btn|'.$name_variable.'|'.$language_files_to_load_keys[$lang_file],
271
                        get_lang('Save'),
272
                        [
273
                            'class' => 'save btn btn-default btn-sm',
274
                            'type' => 'button',
275
                            'id' => 'btnid_'.$name_variable,
276
                        ]
277
                    );
278
279
                    //loading variable from the english array
280
                    $english_name_variable = $english_language_array[$lang_file][$name_variable];
281
282
                    $list_info[] = [
283
                        $lang_file.'.inc.php',
284
                        $name_variable,
285
                        htmlentities($english_name_variable),
286
                        htmlentities($parent_variable_value),
287
                        $obj_text,
288
                        $obj_button,
289
                    ];
290
                }
291
            }
292
        }
293
294
        // Search in sub language
295
        if ($search_in_sub_language) {
296
            $variables = $sub_language_array[$lang_file];
297
            foreach ($variables as $name_variable => $variable_value) {
298
                if (is_array($parent_variable_value)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $parent_variable_value does not seem to be defined for all execution paths leading up to this point.
Loading history...
299
                    continue;
300
                }
301
302
                if (is_array($variable_value)) {
303
                    continue;
304
                }
305
306
                $founded = false;
307
                // searching the item in the parent tool
308
                if (preg_match($term, $variable_value) !== 0) {
309
                    $founded = true;
310
                }
311
                if ($founded) {
312
                    //loading variable from the english array
313
                    $sub_language_name_variable = isset($sub_language_array[$lang_file][$name_variable])
314
                        ? $sub_language_array[$lang_file][$name_variable]
315
                        : '';
316
                    $parent_variable_value = isset($parent_language_array[$lang_file][$name_variable])
317
                        ? $parent_language_array[$lang_file][$name_variable]
318
                        : '';
319
                    //config buttons
320
                    $obj_text = Display::tag(
321
                        'textarea',
322
                        $sub_language_name_variable,
323
                        [
324
                            'rows' => 10,
325
                            'cols' => 40,
326
                            'name' => 'txt|'.$name_variable.'|'.$language_files_to_load_keys[$lang_file],
327
                            'id' => 'txtid_'.$language_files_to_load_keys[$lang_file].'_'.$name_variable,
328
                        ]
329
                    );
330
                    $obj_button = Display::button(
331
                        'btn|'.$name_variable.'|'.$language_files_to_load_keys[$lang_file],
332
                        get_lang('Save'),
333
                        [
334
                            'class' => 'save btn btn-default btn-sm',
335
                            'type' => 'button',
336
                            'id' => 'btnid_'.$name_variable,
337
                        ]
338
                    );
339
340
                    //loading variable from the english array
341
                    $english_name_variable = $english_language_array[$lang_file][$name_variable];
342
                    $list_info[] = [$lang_file.'.inc.php',
343
                        $name_variable,
344
                        $english_name_variable,
345
                        $parent_variable_value, $obj_text, $obj_button, ];
346
                }
347
            }
348
        }
349
    }
350
351
    $list_info = array_unique_dimensional($list_info);
352
353
    return $list_info;
354
}
355
356
// Allow see data in sort table
357
$list_info = [];
358
if (isset($_REQUEST['txt_search_word'])) {
359
    //@todo fix to accept a char with 1 char
360
    if (strlen(trim($_REQUEST['txt_search_word'])) > 2) {
361
        $list_info = search_language_term(
362
            $_REQUEST['txt_search_word'],
363
            true,
364
            true,
365
            true,
366
            true
367
        );
368
    }
369
}
370
371
$parameters = [
372
    'id' => intval($_GET['id']),
373
    'sub_language_id' => intval($_GET['sub_language_id']),
374
    'txt_search_word' => $txt_search_word,
375
];
376
$table = new SortableTableFromArrayConfig($list_info, 1, 20, 'data_info');
377
$table->set_additional_parameters($parameters);
378
$table->set_header(0, get_lang('LanguageFile'));
379
$table->set_header(1, get_lang('LanguageVariable'));
380
$table->set_header(2, get_lang('EnglishName'));
381
$table->set_header(3, get_lang('OriginalName'));
382
$table->set_header(4, get_lang('Translation'), false);
383
$table->set_header(5, get_lang('Action'), false);
384
$table->setHideColumn(0);
385
$table->display();
386
387
Display :: display_footer();
388