Skip to content

Commit 280626d

Browse files
Merge pull request #6802 from christianbeeznest/GH-6796
Admin: Fix AJAX toggles and sort language selects by english_name - refs #6796
2 parents 8807abb + 04a0775 commit 280626d

File tree

5 files changed

+172
-121
lines changed

5 files changed

+172
-121
lines changed

public/main/admin/languages.php

Lines changed: 108 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
//Ajax request
3030
if (isset($_POST['sent_http_request'])) {
3131
if (isset($_POST['visibility']) &&
32-
$_POST['visibility'] == strval(intval($_POST['visibility'])) && 0 == $_POST['visibility']) {
32+
$_POST['visibility'] == strval(intval($_POST['visibility'])) && 0 == $_POST['visibility']
33+
) {
3334
if (isset($_POST['id']) && $_POST['id'] == strval(intval($_POST['id']))) {
3435
if (false == SubLanguageManager::check_if_language_is_used($_POST['id'])) {
3536
SubLanguageManager::make_unavailable_language($_POST['id']);
@@ -51,75 +52,15 @@
5152
}
5253

5354
$msgLang = isset($_SESSION['disabled_languages']) ? 1 : 0;
54-
$disabledLang = isset($_SESSION['disabled_languages']) ? $_SESSION['disabled_languages'] : null;
55-
56-
$htmlHeadXtra[] = '<script>
57-
$(function () {
58-
var msgLang = '.$msgLang.';
59-
var disabledLang = "'.$disabledLang.'"
60-
61-
if (msgLang == 1) {
62-
$("#id_content_message").html("<div class=\"warning-message alert alert-warning\">'.addslashes(get_lang('There are users currently using the following language. Please disable manually.')).' <br /> " + disabledLang + "</div");
63-
}
64-
65-
$("#disable_all_except_default").click(function () {
66-
if(confirm("'.addslashes(get_lang('Please confirm your choice')).'")) {
67-
$.ajax({
68-
contentType: "application/x-www-form-urlencoded",
69-
beforeSend: function(myObject) {
70-
$("#id_content_message").html("<div class=\"warning-message alert alert-warning\"><em class=\"fa fa-refresh fa-spin\"></em> '.addslashes(get_lang('Loading')).'</div>");
71-
},
72-
type: "GET",
73-
url: "../admin/languages.php",
74-
data: "action=disable_all_except_default",
75-
success: function(datos) {
76-
window.location.href = "'.api_get_self().'";
77-
}
78-
});
79-
}
80-
81-
return false;
82-
});
83-
84-
$(".make_visible_and_invisible").click(function(e) {
85-
e.preventDefault();
86-
87-
var id_link_tool = $(this).attr("id");
88-
var link_id = id_link_tool.split("linktool_")[1];
89-
var currentIcon = $("#imglinktool_" + link_id);
90-
91-
$.ajax({
92-
type: "POST",
93-
url: "../admin/languages.php",
94-
data: { id: link_id, visibility: currentIcon.hasClass("mdi-toggle-switch") ? 0 : 1, sent_http_request: 1 },
95-
beforeSend: function() {
96-
$("#id_content_message").html("<div class=\'warning-message alert alert-warning\'><em class=\'fa fa-refresh fa-spin\'></em>'.addslashes(get_lang('Loading')). '...</div>");
97-
},
98-
success: function(response) {
99-
if (response === "set_visible" || response === "set_hidden") {
100-
var newIconClass = (response === "set_visible") ? "mdi-toggle-switch" : "mdi-toggle-switch-off";
101-
var oldIconClass = (response === "set_visible") ? "mdi-toggle-switch-off" : "mdi-toggle-switch";
102-
103-
currentIcon.removeClass(oldIconClass).addClass(newIconClass);
104-
}
105-
}
106-
});
107-
});
108-
109-
});
110-
</script>';
111-
112-
// unset the msg session variable
55+
$disabledLang = $msgLang ? (string) $_SESSION['disabled_languages'] : '';
11356
unset($_SESSION['disabled_languages']);
11457

115-
// setting the table that is needed for the styles management (there is a check if it exists later in this code)
11658
$tbl_admin_languages = Database::get_main_table(TABLE_MAIN_LANGUAGE);
11759
$tbl_settings_current = Database::get_main_table(TABLE_MAIN_SETTINGS);
11860

11961
$id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
12062
$url = api_get_self();
12163

122-
// we change the availability
12364
switch ($action) {
12465
case 'makeunavailable':
12566
if (!empty($id)) {
@@ -128,7 +69,6 @@
12869
}
12970
header("Location: $url");
13071
exit;
131-
break;
13272
case 'makeavailable':
13373
if (!empty($id)) {
13474
SubLanguageManager::make_available_language($id);
@@ -148,10 +88,10 @@
14888
$failedDisabledLanguages = '';
14989
$checkFailed = false;
15090
foreach ($allLanguages as $language) {
151-
if (false == SubLanguageManager::check_if_language_is_used($language['id'])) {
152-
SubLanguageManager::make_unavailable_language($language['id']);
91+
if (false == SubLanguageManager::check_if_language_is_used((int) $language['id'])) {
92+
SubLanguageManager::make_unavailable_language((int) $language['id']);
15393
} else {
154-
if (intval(SubLanguageManager::get_platform_language_id()) !== intval($language['id'])) {
94+
if ((int) SubLanguageManager::get_platform_language_id() !== (int) $language['id']) {
15595
$failedDisabledLanguages .= ' - '.$language['english_name'].'<br />';
15696
$checkFailed = true;
15797
}
@@ -164,30 +104,32 @@
164104
Display::addFlash(Display::return_message(get_lang('Update successful'), 'success'));
165105
header("Location: $url");
166106
exit;
167-
break;
168107
case 'make_unavailable_confirmed':
169108
$language_info = SubLanguageManager::get_all_information_of_language($id);
170-
if (1 == $language_info['available']) {
109+
if ($language_info && 1 == (int) $language_info['available']) {
171110
SubLanguageManager::make_unavailable_language($id);
172111
$platform_language = api_get_setting('platformLanguage');
173112
UserManager::update_all_user_languages($language_info['english_name'], $platform_language);
174-
Display::addFlash(Display::return_message(get_lang('The language has been hidden. It will not be possible to use it until it becomes visible again.'), 'confirm'));
113+
Display::addFlash(
114+
Display::return_message(
115+
get_lang('The language has been hidden. It will not be possible to use it until it becomes visible again.'),
116+
'confirm'
117+
)
118+
);
175119
header("Location: $url");
176120
exit;
177121
}
178122
break;
179123
}
180124

181125
if (isset($_POST['Submit']) && $_POST['Submit']) {
182-
// changing the name
183126
$name = html_filter($_POST['txt_name']);
184127
$postId = (int) $_POST['edit_id'];
185128
Database::update(
186129
$tbl_admin_languages,
187130
['original_name' => $name],
188131
['id = ?' => $postId]
189132
);
190-
// changing the Platform language
191133
if (isset($_POST['platformlanguage']) && '' != $_POST['platformlanguage']) {
192134
api_set_setting('platformLanguage', $_POST['platformlanguage'], null, null, api_get_current_access_url_id());
193135
header("Location: $url");
@@ -196,59 +138,118 @@
196138
} elseif (isset($_POST['action'])) {
197139
switch ($_POST['action']) {
198140
case 'makeavailable':
199-
if (count($_POST['id']) > 0) {
200-
$ids = [];
201-
foreach ($_POST['id'] as $index => $id) {
202-
$ids[] = intval($id);
141+
if (!empty($_POST['id'])) {
142+
$ids = array_map('intval', (array) $_POST['id']);
143+
if ($ids) {
144+
$sql = "UPDATE $tbl_admin_languages SET available='1' WHERE id IN ('".implode("','", $ids)."')";
145+
Database::query($sql);
203146
}
204-
$sql = "UPDATE $tbl_admin_languages SET available='1' WHERE id IN ('".implode("','", $ids)."')";
205-
Database::query($sql);
206147
header("Location: $url");
207148
exit;
208149
}
209150
break;
210151
case 'makeunavailable':
211-
if (count($_POST['id']) > 0) {
212-
$ids = [];
213-
foreach ($_POST['id'] as $index => $id) {
214-
$ids[] = intval($id);
152+
if (!empty($_POST['id'])) {
153+
$ids = array_map('intval', (array) $_POST['id']);
154+
if ($ids) {
155+
$sql = "UPDATE $tbl_admin_languages SET available='0' WHERE id IN ('".implode("','", $ids)."')";
156+
Database::query($sql);
215157
}
216-
$sql = "UPDATE $tbl_admin_languages SET available='0' WHERE id IN ('".implode("','", $ids)."')";
217-
Database::query($sql);
218158
header("Location: $url");
219159
exit;
220160
}
221161
break;
222162
}
223163
}
224164

225-
// setting the name of the tool
226165
$tool_name = get_lang('Chamilo Portal Languages');
227166

228-
// setting breadcrumbs
229167
$interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('Administration')];
230168

231-
// displaying the explanation for this tool
232-
Display::addFlash(Display::return_message(get_lang('This tool manages the language selection menu on the login page. As a platform administrator you can decide which languages should be available for your users.'), 'normal'));
169+
Display::addFlash(
170+
Display::return_message(
171+
get_lang('This tool manages the language selection menu on the login page. As a platform administrator you can decide which languages should be available for your users.')
172+
)
173+
);
174+
175+
$htmlHeadXtra[] = '<script>
176+
$(function () {
177+
var msgLang = '. $msgLang .';
178+
var disabledLang = "'.addslashes($disabledLang).'";
179+
180+
if (msgLang === 1) {
181+
$("#id_content_message").html("<div class=\"warning-message alert alert-warning\">'.addslashes(get_lang('There are users currently using the following language. Please disable manually.')).'<br />" + disabledLang + "</div>");
182+
}
183+
184+
$("#disable_all_except_default").on("click", function () {
185+
if (confirm("'.addslashes(get_lang('Please confirm your choice')).'")) {
186+
$.ajax({
187+
contentType: "application/x-www-form-urlencoded",
188+
beforeSend: function() {
189+
$("#id_content_message").html("<div class=\"warning-message alert alert-warning\"><em class=\"fa fa-refresh fa-spin\"></em> '.addslashes(get_lang('Loading')).'</div>");
190+
},
191+
type: "GET",
192+
url: "'.api_get_self().'",
193+
data: { action: "disable_all_except_default" },
194+
success: function() {
195+
window.location.href = "'.api_get_self().'";
196+
}
197+
});
198+
}
199+
return false;
200+
});
201+
202+
$(".make_visible_and_invisible").on("click", function (e) {
203+
e.preventDefault();
204+
var $link = $(this);
205+
var id = parseInt($link.data("id"), 10);
206+
var available = parseInt($link.data("available"), 10);
207+
var nextVisibility = available ? 0 : 1;
208+
var $icon = $("#imglinktool_" + id);
209+
210+
$.ajax({
211+
type: "POST",
212+
url: "../admin/languages.php",
213+
data: { id: id, visibility: nextVisibility, sent_http_request: 1 },
214+
beforeSend: function () {
215+
$("#id_content_message").html("<div class=\'warning-message alert alert-warning\'><em class=\'fa fa-refresh fa-spin\'></em> '.addslashes(get_lang('Loading')).'...</div>");
216+
},
217+
success: function (response) {
218+
if (response === "set_visible" || response === "set_hidden") {
219+
var nowAvailable = response === "set_visible" ? 1 : 0;
220+
$link.data("available", nowAvailable);
221+
222+
if (nowAvailable === 1) {
223+
$icon.removeClass("ch-tool-icon-disabled");
224+
} else {
225+
$icon.addClass("ch-tool-icon-disabled");
226+
}
227+
228+
$("#id_content_message").html("<div class=\'alert alert-success\'>'.addslashes(get_lang('Update successful')).'</div>");
229+
} else if (typeof response === "string" && response.indexOf("confirm:") === 0) {
230+
window.location.href = "'.api_get_self().'?action=make_unavailable_confirmed&id=" + id;
231+
}
232+
}
233+
});
234+
});
235+
});
236+
</script>';
233237

234-
// including the header file (which includes the banner itself)
235238
Display::display_header($tool_name);
236239

237240
echo '<a id="disable_all_except_default" href="javascript:void(0)" class="btn btn--primary">
238241
<em class="fa fa-eye"></em> '.get_lang('Disable all languages except the platform default').'</a><br /><br />';
239242

240-
// selecting all the languages
241243
$sql_select = "SELECT * FROM $tbl_admin_languages";
242244
$result_select = Database::query($sql_select);
243245
$currentLanguage = api_get_setting('language.platform_language');
244246

245-
// the table data
246247
$language_data = [];
247248
while ($row = Database::fetch_array($result_select)) {
248249
$row_td = [];
249250
$row_td[] = $row['id'];
250251
$checked = '';
251-
// the first column is the original name of the language OR a form containing the original name
252+
252253
if ('edit' == $action && $row['id'] == $id) {
253254
if ($row['english_name'] == api_get_setting('platformLanguage')) {
254255
$checked = ' checked="checked" ';
@@ -257,22 +258,28 @@
257258
$row_td[] = '
258259
<input type="hidden" name="edit_id" value="'.$id.'" />
259260
<input type="text" name="txt_name" value="'.$row['original_name'].'" />
260-
<input type="checkbox" '.$checked.'name="platformlanguage" id="platformlanguage" value="'.$row['english_name'].'" />
261+
<input type="checkbox" '.$checked.' name="platformlanguage" id="platformlanguage" value="'.$row['english_name'].'" />
261262
<label for="platformlanguage">'.$row['original_name'].' '.get_lang('as platformlanguage').'</label>
262263
<input type="submit" name="Submit" value="'.get_lang('Validate').'" />
263264
<a name="value" />';
264265
} else {
265266
$row_td[] = $row['original_name'];
266267
}
267268

268-
// the second column
269269
$row_td[] = $row['english_name'].' ('.$row['isocode'].')';
270270

271271
if ($row['isocode'] == $currentLanguage) {
272-
$setplatformlanguage = Display::getMdiIcon(ToolIcon::TRANSLATION, 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang('Current portal\'s language'));
272+
$setplatformlanguage = Display::getMdiIcon(
273+
ToolIcon::TRANSLATION,
274+
'ch-tool-icon',
275+
null,
276+
ICON_SIZE_SMALL,
277+
get_lang('Current portal\'s language')
278+
);
273279
} else {
280+
$confirmSet = addslashes(get_lang('Are you sure you want to set this language as the portal\'s default?'));
274281
$setplatformlanguage =
275-
"<a href=\"javascript:if (confirm('".addslashes(get_lang('Are you sure you want to set this language as the portal\'s default?'))."')) { location.href='".api_get_self()."?action=setplatformlanguage&id=".$row['id']."'; }\">".
282+
"<a href=\"javascript:if (confirm('".$confirmSet."')) { location.href='".api_get_self()."?action=setplatformlanguage&id=".$row['id']."'; }\">".
276283
Display::getMdiIcon(ToolIcon::TRANSLATION, 'ch-tool-icon-disabled', null, ICON_SIZE_SMALL, get_lang('Set language as default'))."</a>";
277284
}
278285

@@ -303,13 +310,16 @@
303310
$row_td[] = Display::getMdiIcon(StateIcon::ACTIVE, 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang('Visible')).
304311
"<a href='".api_get_self()."?action=edit&id=".$row['id']."#value'>".
305312
Display::getMdiIcon(ActionIcon::EDIT, 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang('Edit'))."</a>
306-
&nbsp;".$setplatformlanguage.$allow_use_sub_language.$allow_add_term_sub_language.$allow_delete_sub_language;
313+
&nbsp;".$setplatformlanguage.$allow_use_sub_language.$allow_add_term_sub_language.$allow_delete_sub_language;
307314
} else {
308-
$action = ($row['available'] == 1) ? 'makeunavailable' : 'makeavailable';
309315
$icon = ($row['available'] == 1) ? StateIcon::ACTIVE : StateIcon::INACTIVE;
310316
$tooltip = ($row['available'] == 1) ? get_lang('Make unavailable') : get_lang('Make available');
311317

312-
$row_td[] = "<a class=\"make_visible_and_invisible\" id=\"linktool_".$row['id']."\" href='".api_get_self()."?action=$action&id=".$row['id']."'>".
318+
$row_td[] = "<a class=\"make_visible_and_invisible\"
319+
id=\"linktool_".$row['id']."\"
320+
href=\"".api_get_self()."?action=".(($row['available']==1)?'makeunavailable':'makeavailable')."&id=".$row['id']."\"
321+
data-id=\"".$row['id']."\"
322+
data-available=\"".$row['available']."\">".
313323
Display::getMdiIcon($icon, 'ch-tool-icon', null, ICON_SIZE_SMALL, $tooltip, ['id' => 'imglinktool_'.$row['id']])."</a>
314324
<a href='".api_get_self()."?action=edit&id=".$row['id']."#value'>".Display::getMdiIcon(ActionIcon::EDIT, 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang('Edit'))."</a>
315325
&nbsp;".$setplatformlanguage.$allow_use_sub_language.$allow_add_term_sub_language.$allow_delete_sub_language;

public/main/inc/lib/api.lib.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3720,8 +3720,10 @@ function api_get_languages_with_platform_default(): array
37203720
{
37213721
$langTable = Database::get_main_table(TABLE_MAIN_LANGUAGE);
37223722

3723-
// Standard available languages
3724-
$sql = "SELECT isocode, original_name FROM $langTable WHERE available = '1' ORDER BY original_name ASC";
3723+
$sql = "SELECT isocode, original_name
3724+
FROM $langTable
3725+
WHERE available = '1'
3726+
ORDER BY english_name ASC";
37253727
$res = Database::query($sql);
37263728

37273729
$languages = [];
@@ -3740,9 +3742,6 @@ function api_get_languages_with_platform_default(): array
37403742
}
37413743
}
37423744

3743-
// Optional: stable, human-friendly order
3744-
asort($languages, SORT_NATURAL | SORT_FLAG_CASE);
3745-
37463745
return $languages;
37473746
}
37483747

public/main/inc/lib/sub_language.class.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public static function getAllLanguages($onlyActive = false)
3131
if ($onlyActive) {
3232
$sql .= ' WHERE available = 1';
3333
}
34+
$sql .= ' ORDER BY english_name ASC';
35+
3436
$rs = Database::query($sql);
3537
$all_languages = [];
3638
while ($row = Database::fetch_assoc($rs)) {

src/CoreBundle/Form/ProfileType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
7272
$visibleHigh = $expand(\is_array($visibleOptions) ? $visibleOptions : []);
7373
$editableHigh = $expand(\is_array($changeableOptions) ? $changeableOptions : []);
7474

75-
$languages = array_flip($this->languageRepository->getAllAvailableToArray(true));
75+
$languages = array_flip($this->languageRepository->getAllAvailableToArray(true, true));
7676

7777
// Core fields map (keys must align with settings keys)
7878
$fieldsMap = [

0 commit comments

Comments
 (0)