gobrien | 4827546 | 2007-11-29 21:29:17 +0000 | [diff] [blame] | 1 | <?php |
| 2 | /******************************************************************************* |
| 3 | * Copyright (c) 2007 Eclipse Foundation and others. |
| 4 | * All rights reserved. This program and the accompanying materials |
| 5 | * are made available under the terms of the Eclipse Public License v1.0 |
| 6 | * which accompanies this distribution, and is available at |
| 7 | * http://www.eclipse.org/legal/epl-v10.html |
| 8 | * |
| 9 | * Contributors: |
| 10 | * Paul Colton (Aptana)- initial API and implementation |
| 11 | * Eclipse Foundation |
droy | 68ee494 | 2008-02-04 21:24:28 +0000 | [diff] [blame] | 12 | * Eclipse Contributors (bug 217257) |
gobrien | 4827546 | 2007-11-29 21:29:17 +0000 | [diff] [blame] | 13 | *******************************************************************************/ |
| 14 | |
| 15 | require_once("cb_global.php"); |
| 16 | |
gobrien | 1bc0e19 | 2008-01-31 15:43:41 +0000 | [diff] [blame] | 17 | |
gobrien | 4827546 | 2007-11-29 21:29:17 +0000 | [diff] [blame] | 18 | $string_id = $App->getHTTPParameter("string_id", "POST"); |
| 19 | $translation = $App->getHTTPParameter("translation", "POST"); |
gobrien | 1c01c75 | 2008-01-10 18:34:31 +0000 | [diff] [blame] | 20 | |
| 21 | $language_id = $_SESSION["language"]; |
| 22 | $project_id = $_SESSION['project']; |
gobrien | 755df67 | 2008-01-18 01:01:33 +0000 | [diff] [blame] | 23 | $version = $_SESSION["version"]; |
| 24 | |
gobrien | 4827546 | 2007-11-29 21:29:17 +0000 | [diff] [blame] | 25 | $user_id = $User->userid; |
| 26 | |
droy | 2da63a1 | 2008-03-27 18:13:05 +0000 | [diff] [blame] | 27 | # TODO: refactor these ifs |
| 28 | $do_nothing = false; |
| 29 | |
droy | c62694d | 2008-05-14 18:51:30 +0000 | [diff] [blame] | 30 | $affected_rows = 0; |
| 31 | |
droy | 68ee494 | 2008-02-04 21:24:28 +0000 | [diff] [blame] | 32 | if (empty($translation) || (trim($translation) == '')) { |
| 33 | |
droy | 2da63a1 | 2008-03-27 18:13:05 +0000 | [diff] [blame] | 34 | $do_nothing = true; |
droy | 68ee494 | 2008-02-04 21:24:28 +0000 | [diff] [blame] | 35 | |
| 36 | } else if($_POST['translate_action'] != "all"){ |
gobrien | 34a08cb | 2008-02-06 18:44:48 +0000 | [diff] [blame] | 37 | $query = "update |
| 38 | translations |
gobrien | d6da697 | 2008-01-26 00:28:20 +0000 | [diff] [blame] | 39 | set |
gobrien | 34a08cb | 2008-02-06 18:44:48 +0000 | [diff] [blame] | 40 | is_active = 0 |
| 41 | where |
| 42 | string_id = '".addslashes($string_id)."' |
| 43 | and |
| 44 | language_id = '".addslashes($language_id)."' |
| 45 | and |
| 46 | is_active = 1 |
| 47 | "; |
| 48 | $res = mysql_query($query,$dbh); |
| 49 | |
| 50 | $query = "insert into |
| 51 | translations |
| 52 | set |
| 53 | string_id = '".addslashes($string_id)."', |
| 54 | language_id = '".addslashes($language_id)."', |
| 55 | value = '".addslashes($translation)."', |
| 56 | userid = '".addslashes($user_id)."', |
| 57 | created_on = NOW() |
| 58 | "; |
| 59 | $res = mysql_query($query,$dbh); |
droy | c62694d | 2008-05-14 18:51:30 +0000 | [diff] [blame] | 60 | $affected_rows += mysql_affected_rows(); |
gobrien | c5749b7 | 2008-02-28 21:43:51 +0000 | [diff] [blame] | 61 | |
gobrien | 198300d | 2008-03-04 23:22:36 +0000 | [diff] [blame] | 62 | // print $query; |
gobrien | d6da697 | 2008-01-26 00:28:20 +0000 | [diff] [blame] | 63 | }else{ |
gobrien | 34a08cb | 2008-02-06 18:44:48 +0000 | [diff] [blame] | 64 | //FIND ALL STRINGS THAT ARE THE SAME ACROSS VERSIONS |
gobrien | d6da697 | 2008-01-26 00:28:20 +0000 | [diff] [blame] | 65 | $query = "select |
droy | a102afc | 2008-05-21 19:15:42 +0000 | [diff] [blame] | 66 | s.string_id |
gobrien | d6da697 | 2008-01-26 00:28:20 +0000 | [diff] [blame] | 67 | from |
droy | a102afc | 2008-05-21 19:15:42 +0000 | [diff] [blame] | 68 | strings as s |
| 69 | INNER JOIN files AS f on s.file_id = f.file_id |
| 70 | INNER JOIN files AS the_file_selected_for_translation on the_file_selected_for_translation.file_id = (select file_id from strings where string_id = '".addslashes($string_id)."') |
gobrien | d6da697 | 2008-01-26 00:28:20 +0000 | [diff] [blame] | 71 | where |
droy | a102afc | 2008-05-21 19:15:42 +0000 | [diff] [blame] | 72 | s.value = (select value from strings where string_id = '".addslashes($string_id)."') |
gobrien | d6da697 | 2008-01-26 00:28:20 +0000 | [diff] [blame] | 73 | and |
droy | a102afc | 2008-05-21 19:15:42 +0000 | [diff] [blame] | 74 | s.name = (select name from strings where string_id = '".addslashes($string_id)."') |
| 75 | and f.name = the_file_selected_for_translation.name |
| 76 | and f.project_id = the_file_selected_for_translation.project_id |
| 77 | and s.is_active = 1"; |
gobrien | d6da697 | 2008-01-26 00:28:20 +0000 | [diff] [blame] | 78 | |
| 79 | $res = mysql_query($query,$dbh); |
gobrien | d6da697 | 2008-01-26 00:28:20 +0000 | [diff] [blame] | 80 | while($row = mysql_fetch_assoc($res)){ |
| 81 | $string_ids[] = $row['string_id']; |
| 82 | } |
| 83 | |
| 84 | //GET CURRENT TRANSLATION FOR THIS STRING |
droy | a102afc | 2008-05-21 19:15:42 +0000 | [diff] [blame] | 85 | $query= "select value from translations where string_id = '".addslashes($string_id)."' and language_id = '".addslashes($language_id)."' and is_active = 1 order by version limit 1"; |
gobrien | d6da697 | 2008-01-26 00:28:20 +0000 | [diff] [blame] | 86 | $res = mysql_query($query,$dbh); |
| 87 | $string_translation = ""; |
| 88 | while($row = mysql_fetch_assoc($res)){ |
| 89 | $string_translation = $row['value']; |
| 90 | } |
| 91 | |
| 92 | //GET ALL STRINGS WITH SAME TRANSLATIONS |
| 93 | if($string_translation){ |
| 94 | $query = " |
| 95 | select |
gobrien | 70d6664 | 2008-02-06 18:50:40 +0000 | [diff] [blame] | 96 | translation_id,string_id,language_id |
gobrien | d6da697 | 2008-01-26 00:28:20 +0000 | [diff] [blame] | 97 | from |
| 98 | translations |
| 99 | where |
| 100 | string_id in (".addslashes(implode(',',$string_ids)).") |
| 101 | and |
| 102 | value = '".addslashes($string_translation)."' |
| 103 | and |
| 104 | is_active = 1 |
droy | a102afc | 2008-05-21 19:15:42 +0000 | [diff] [blame] | 105 | and language_id = '" . addslashes($language_id)."' |
gobrien | d6da697 | 2008-01-26 00:28:20 +0000 | [diff] [blame] | 106 | "; |
gobrien | 755df67 | 2008-01-18 01:01:33 +0000 | [diff] [blame] | 107 | |
gobrien | d6da697 | 2008-01-26 00:28:20 +0000 | [diff] [blame] | 108 | $res = mysql_query($query,$dbh); |
| 109 | while($row = mysql_fetch_assoc($res)){ |
| 110 | //DE-ACTIVATE ALL OLD TRANSLATIONS |
| 111 | $query = "update translations set is_active = 0 where translation_id = '".addslashes($row['translation_id'])."'"; |
| 112 | $res2 = mysql_query($query,$dbh); |
| 113 | |
| 114 | //INSERT NEW TRANSLATIONS |
| 115 | $query = "insert into |
| 116 | translations |
| 117 | set |
| 118 | string_id = '".addslashes($row['string_id'])."', |
gobrien | 70d6664 | 2008-02-06 18:50:40 +0000 | [diff] [blame] | 119 | language_id = '".addslashes($row['language_id'])."' , |
gobrien | d6da697 | 2008-01-26 00:28:20 +0000 | [diff] [blame] | 120 | value = '".addslashes($translation)."', |
| 121 | userid = '".addslashes($user_id)."', |
| 122 | created_on = NOW() |
| 123 | "; |
| 124 | $res2 = mysql_query($query,$dbh); |
droy | c62694d | 2008-05-14 18:51:30 +0000 | [diff] [blame] | 125 | $affected_rows += mysql_affected_rows(); |
gobrien | d6da697 | 2008-01-26 00:28:20 +0000 | [diff] [blame] | 126 | |
| 127 | } |
gobrien | 755df67 | 2008-01-18 01:01:33 +0000 | [diff] [blame] | 128 | |
gobrien | d6da697 | 2008-01-26 00:28:20 +0000 | [diff] [blame] | 129 | }else{ |
| 130 | $query = " |
| 131 | select |
| 132 | strings.string_id |
| 133 | from |
| 134 | strings |
| 135 | left join |
| 136 | translations |
| 137 | on |
| 138 | strings.string_id = translations.string_id |
| 139 | and |
| 140 | translations.value is NULL |
| 141 | where |
| 142 | strings.string_id in (".addslashes(implode(',',$string_ids)).") |
| 143 | "; |
gobrien | 755df67 | 2008-01-18 01:01:33 +0000 | [diff] [blame] | 144 | |
| 145 | $res = mysql_query($query,$dbh); |
| 146 | |
gobrien | d6da697 | 2008-01-26 00:28:20 +0000 | [diff] [blame] | 147 | while($row = mysql_fetch_assoc($res)){ |
| 148 | $translation_ids[] = $row['string_id']; |
| 149 | //INSERT NEW TRANSLATIONS |
| 150 | $query = "insert into |
| 151 | translations |
| 152 | set |
| 153 | string_id = '".addslashes($row['string_id'])."', |
| 154 | language_id = '".addslashes($language)."' , |
| 155 | value = '".addslashes($translation)."', |
| 156 | userid = '".addslashes($user_id)."', |
| 157 | created_on = NOW() |
| 158 | "; |
| 159 | $res2 = mysql_query($query,$dbh); |
droy | c62694d | 2008-05-14 18:51:30 +0000 | [diff] [blame] | 160 | $affected_rows += mysql_affected_rows(); |
droy | 2da63a1 | 2008-03-27 18:13:05 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
gobrien | d6da697 | 2008-01-26 00:28:20 +0000 | [diff] [blame] | 163 | } |
droy | 2da63a1 | 2008-03-27 18:13:05 +0000 | [diff] [blame] | 164 | |
| 165 | if(!$do_nothing) { |
| 166 | # Find all string_id's that have the same binary value as the one we're translating |
| 167 | # *and* have no translation yet, and update those too. |
| 168 | $sql = "SELECT s.string_id, COUNT(t.string_id) AS tr_count |
| 169 | FROM strings AS s |
| 170 | LEFT JOIN translations AS t ON t.string_id = s.string_id AND t.language_id = '".addslashes($language_id)."' |
| 171 | WHERE BINARY s.value = (select value from strings where string_id = '".addslashes($string_id)."') |
| 172 | AND s.is_active = 1 AND t.value IS NULL GROUP BY s.string_id HAVING tr_count = 0"; |
| 173 | |
| 174 | $res = mysql_query($sql, $dbh); |
| 175 | $str_count = mysql_affected_rows(); |
| 176 | |
| 177 | while($myrow = mysql_fetch_assoc($res)) { |
| 178 | $sql = "insert into |
| 179 | translations |
| 180 | set |
| 181 | string_id = " . $myrow['string_id'] . ", |
| 182 | language_id = '".addslashes($language_id)."', |
| 183 | value = '".addslashes($translation)."', |
| 184 | userid = '".addslashes($user_id)."', |
| 185 | created_on = NOW()"; |
| 186 | mysql_query($sql, $dbh); |
droy | c62694d | 2008-05-14 18:51:30 +0000 | [diff] [blame] | 187 | $affected_rows += mysql_affected_rows(); |
droy | 2da63a1 | 2008-03-27 18:13:05 +0000 | [diff] [blame] | 188 | } |
| 189 | } |
gobrien | 5239b3c | 2008-06-17 06:51:12 +0000 | [diff] [blame^] | 190 | |
| 191 | $response['translationString'] = htmlspecialchars($translation); |
| 192 | |
| 193 | $response['translationArea'] = "<br /><br /><br /> |
| 194 | <center><b>Translated $affected_rows string ". ($affected_rows > 1 || $affected_rows == 0 ? "s" : "" ) ." across all Babel projects. |
| 195 | </b></center>"; |
| 196 | |
| 197 | print json_encode($response); |
| 198 | |
| 199 | ?> |