Skip to content

Commit

Permalink
Fixes the sending of a new translation on text annotation edition.
Browse files Browse the repository at this point in the history
Getting the translation is still broken.
  • Loading branch information
HugoFara committed Dec 22, 2023
1 parent 890bcfe commit bec74f7
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 72 deletions.
25 changes: 14 additions & 11 deletions api.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ function imported_terms($get_req)
/**
* Translations for a term to choose an annotation.
*
* @param array $get_req Get request with fields "text_id".
* @param array $get_req Get request with fields "text_id" and "term_lc".
*/
function term_translations($get_req)
{
Expand Down Expand Up @@ -472,7 +472,7 @@ function update_translation($post_req)
(int)$post_req['term_id'], trim($post_req['translation'])
);
$raw_answer = array();
if ($result == "") {
if (str_starts_with($result, "Error")) {
$raw_answer["error"] = $result;
} else {
$raw_answer["update"] = $result;
Expand All @@ -494,7 +494,10 @@ function add_translation($post_req)
$text, (int)$post_req['lg_id'], trim($post_req['translation'])
);
$raw_answer = array();
if ($result == mb_strtolower($text, 'UTF-8')) {
if (is_array($result)) {
$raw_answer["term_id"] = $result[0];
$raw_answer["term_lc"] = $result[1];
} else if ($result == mb_strtolower($text, 'UTF-8')) {
$raw_answer["add"] = $result;
} else {
$raw_answer["error"] = $result;
Expand Down Expand Up @@ -694,20 +697,20 @@ function main_enpoint($method, $requestUri) {
}
break;
case 'translations':
if (!ctype_digit($endpoint_fragments[1])) {
if (ctype_digit($endpoint_fragments[1])) {
$_POST["term_id"] = (int) $endpoint_fragments[1];
$answer = update_translation($_POST);
send_response(200, $answer);
} else if ($endpoint_fragments[1] == 'new') {
$answer = add_translation($_POST);
send_response(200, $answer);
} else {
send_response(
404,
['error' => 'Term ID (Integer) Expected, Got ' .
$endpoint_fragments[1]]
);
}
$_POST["term_id"] = (int) $endpoint_fragments[1];
$answer = update_translation($_POST);
send_response(200, $answer);
break;
case 'translations/new':
$answer = add_translation($_POST);
send_response(200, $answer);
break;
default:
send_response(
Expand Down
4 changes: 3 additions & 1 deletion edit_word.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ function edit_word_do_operation($translation, $fromAnn)
?>
<script type="text/javascript">
window.opener.do_ajax_edit_impr_text(
<?php echo $fromAnn; ?>, <?php echo prepare_textdata_js($textlc); ?>
<?php echo $fromAnn; ?>,
<?php echo prepare_textdata_js($textlc); ?>,
<?php echo $wid; ?>
);
</script>
<?php
Expand Down
18 changes: 12 additions & 6 deletions inc/ajax_add_term_transl.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
* @param int $lang Language ID
* @param string $data Translation
*
* @return string Error message if failure, lowercase $text otherwise
* @return string|array [new word ID, lowercase $text] if success, error message otherwise
*
* @global string $tbpref Database table prefix
*
* @since 2.9.0 Error messages are much more explicit
* @since 2.9.0 Return an array
*/
function add_new_term_transl($text, $lang, $data)
{
Expand Down Expand Up @@ -59,7 +60,7 @@ function add_new_term_transl($text, $lang, $data)
WHERE Ti2LgID = $lang AND LOWER(Ti2Text) = " .
convert_string_to_sqlsyntax_notrim_nonull($textlc)
);
return $textlc;
return array($wid, $textlc);
}

/**
Expand All @@ -68,7 +69,7 @@ function add_new_term_transl($text, $lang, $data)
* @param int $wid Word ID
* @param string $new_trans New translation
*
* @return string WoTextLC, lower version of the word
* @return string WoTextLC, lowercase version of the word
*
* @global string $tbpref Database table prefix
*/
Expand Down Expand Up @@ -111,7 +112,7 @@ function edit_term_transl($wid, $new_trans)
* @param int $wid Word ID
* @param string $new_trans New translation
*
* @return string Term in lower case, or "" if term does not exist
* @return string Term in lower case, or error message if term does not exist
*
* @global string $tbpref
*/
Expand All @@ -126,7 +127,7 @@ function do_ajax_check_update_translation($wid, $new_trans)
if ($cnt_words == 1) {
return edit_term_transl($wid, $new_trans);
}
return "";
return "Error: " . $cnt_words . " word ID found!";
}

/**
Expand All @@ -145,9 +146,14 @@ function do_ajax_add_term_transl($wid, $data)
// Save data
$success = "";
if ($wid == 0) {
$success = add_new_term_transl(
$status = add_new_term_transl(
trim($_POST['text']), (int)$_POST['lang'], $data
);
if (is_array($status)) {
$success = $status[1];
} else {
$success = $status;
}
} else {
$success = do_ajax_check_update_translation($wid, $data);
}
Expand Down
47 changes: 31 additions & 16 deletions inc/ajax_edit_impr_text.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ function make_trans($i, $wid, $trans, $word, $lang): string
'<img class="click" src="icn/plus-button.png"
title="Save another translation to existent term"
alt="Save another translation to existent term"
onclick="addTermTranslation(' . $wid . ', \'#tx' . $i . '\',\'\',' . $lang . ');" />';
onclick="updateTermTranslation(' . $wid . ', \'#tx' . $i . '\');" />';
} else {
$r .=
'<img class="click" src="icn/plus-button.png"
title="Save translation to new term"
alt="Save translation to new term"
onclick="addTermTranslation(0, \'#tx' . $i . '\',' . prepare_textdata_js($word) . ',' . $lang . ');" />';
onclick="addTermTranslation(\'#tx' . $i . '\',' . prepare_textdata_js($word) . ',' . $lang . ');" />';
}
$r .= '&nbsp;&nbsp;
<span id="wait' . $i . '">
Expand All @@ -103,6 +103,34 @@ function make_trans($i, $wid, $trans, $word, $lang): string
return $r;
}


/**
* Find the possible translations for a term.
*
* @param int $word_id Term ID
*
* @return array Return the possible translations.
*/
function get_translations($word_id)
{
global $tbpref;
$translations = array();
$alltrans = get_first_value(
"SELECT WoTranslation AS value FROM {$tbpref}words
WHERE WoID = $word_id"
);
$transarr = preg_split('/[' . get_sepas() . ']/u', $alltrans);
foreach ($transarr as $t) {
$tt = trim($t);
if ($tt == '*' || $tt == '') {
continue;
}
$translations[] = $tt;
}
return $translations;
}


/**
* Gather useful data to edit a term annotation on a specific text.
*
Expand Down Expand Up @@ -202,20 +230,7 @@ function get_term_translations($wordlc, $textid)

// Add other translation choices
if ($wid !== null) {
$translations = array();
$alltrans = get_first_value(
"SELECT WoTranslation AS value FROM {$tbpref}words
WHERE WoID = $wid"
);
$transarr = preg_split('/[' . get_sepas() . ']/u', $alltrans);
foreach ($transarr as $t) {
$tt = trim($t);
if ($tt == '*' || $tt == '') {
continue;
}
$translations[] = $tt;
}
$ann_data["translations"] = $translations;
$ann_data["translations"] = get_translations($wid);
}
return $ann_data;
}
Expand Down
21 changes: 12 additions & 9 deletions js/pgm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 59 additions & 29 deletions src/js/jq_pgm.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,41 +161,28 @@ function changeImprAnnRadio () {
do_ajax_save_impr_text(textid, elem_name, form_data);
}


/**
* Add (new word) or update (existing word) a word translation.
* Update a word translation.
*
* @param {int} wordid Word ID, 0 for new wrod
* @param {int} wordid Word ID
* @param {string} txid Text HTML ID or unique HTML selector
* @param {string} word Word text
* @param {int} lang Language ID
* @returns
*/
function addTermTranslation(wordid, txid, word, lang) {
function updateTermTranslation(wordid, txid) {
const translation = $(txid).val().trim();
const pagepos = $(document).scrollTop();
if (translation == '' || translation == '*') {
alert('Text Field is empty or = \'*\'!');
return;
}
let request = {
translation: translation,
translation: translation,
};
let failure, action_type, endpoint;
if (wordid === 0) {
action_type = "add";
endpoint = "new";
request["term_text"] = word;
request["lg_id"] = lang;
failure = "Adding translation to term failed!";
} else {
action_type = "update";
endpoint = parseInt(wordid, 10);
failure = "Updating translation of term failed!";
}
request["action_type"] = action_type;
failure += "Please reload page and try again."
const failure = "Updating translation of term failed!" +
"Please reload page and try again.";
$.post(
'api.php/v1/translations/' + endpoint,
'api.php/v1/translations/' + wordid,
request,
function (d) {
if (d == '') {
Expand All @@ -206,7 +193,46 @@ function addTermTranslation(wordid, txid, word, lang) {
alert(failure + "\n" + d.error);
return;
}
do_ajax_edit_impr_text(pagepos, d[action_type]);
do_ajax_edit_impr_text(pagepos, d.update, wordid);
},
"json"
);
}

/**
* Add (new word) a word translation.
*
* @param {string} txid Text HTML ID or unique HTML selector
* @param {string} word Word text
* @param {int} lang Language ID
* @returns
*/
function addTermTranslation(txid, word, lang) {
const translation = $(txid).val().trim();
const pagepos = $(document).scrollTop();
if (translation == '' || translation == '*') {
alert('Text Field is empty or = \'*\'!');
return;
}
const failure = "Adding translation to term failed!" +
"Please reload page and try again."
$.post(
'api.php/v1/translations/new',
{
translation: translation,
term_text: word,
lg_id: lang
},
function (d) {
if (d == '') {
alert(failure);
return;
}
if ("error" in d) {
alert(failure + "\n" + d.error);
return;
}
do_ajax_edit_impr_text(pagepos, d.add, d.word_id);
},
"json"
);
Expand Down Expand Up @@ -1643,14 +1669,14 @@ function edit_term_ann_translations(trans_data, text_id)
`<img class="click" src="icn/plus-button.png"
title="Save another translation to existent term"
alt="Save another translation to existent term"
onclick="addTermTranslation(${trans_data.wid}, ` +
`'#tx${trans_data.ann_index}', '',${trans_data.lang_id});" />`;
onclick="updateTermTranslation(${trans_data.wid}, ` +
`'#tx${trans_data.ann_index}');" />`;
} else {
translations_list +=
`<img class="click" src="icn/plus-button.png"
title="Save translation to new term"
alt="Save translation to new term"
onclick="addTermTranslation(0, '#tx${trans_data.ann_index}',` +
onclick="addTermTranslation('#tx${trans_data.ann_index}',` +
`${trans_data.term_lc},${trans_data.lang_id});" />`;
}
translations_list += `&nbsp;&nbsp;
Expand All @@ -1664,11 +1690,15 @@ function edit_term_ann_translations(trans_data, text_id)
/**
* Load the possible translations for a word.
*
* @param {int} pagepos Position to scroll to
* @param {string} word Word to get annotations
* @param {int} pagepos Position to scroll to
* @param {string} word Word in lowercase to get annotations
* @param {int} term_id Term ID
* @returns
*
* @since 2.9.0 The new parameter $wid is now necessary
*/
function do_ajax_edit_impr_text(pagepos, word) {
function do_ajax_edit_impr_text(pagepos, word, term_id)
{
// Special case, on empty word reload the main annotations form
if (word == '') {
$('#editimprtextdata').html('<img src="icn/waiting2.gif" />');
Expand All @@ -1678,7 +1708,7 @@ function do_ajax_edit_impr_text(pagepos, word) {
// Load the possible translations for a word
const textid = $('#editimprtextdata').attr('data_id');
$.getJSON(
'api.php/v1/translations',
'api.php/v1/terms/' + term_id + '/translations/',
{
text_id: textid,
term_lc: word
Expand Down

0 comments on commit bec74f7

Please sign in to comment.