blob: 41aa72f81dac72fe3f648a62b21cda8987bed0e4 [file] [log] [blame]
gobrien48275462007-11-29 21:29:17 +00001<?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
droy68ee4942008-02-04 21:24:28 +000012 * Eclipse Contributors (bug 217257)
gobrien48275462007-11-29 21:29:17 +000013*******************************************************************************/
14
15require_once("cb_global.php");
16
gobrien1bc0e192008-01-31 15:43:41 +000017
gobrien48275462007-11-29 21:29:17 +000018$string_id = $App->getHTTPParameter("string_id", "POST");
19$translation = $App->getHTTPParameter("translation", "POST");
gobrien1c01c752008-01-10 18:34:31 +000020
21$language_id = $_SESSION["language"];
22$project_id = $_SESSION['project'];
gobrien755df672008-01-18 01:01:33 +000023$version = $_SESSION["version"];
24
gobrien48275462007-11-29 21:29:17 +000025$user_id = $User->userid;
26
droy2da63a12008-03-27 18:13:05 +000027# TODO: refactor these ifs
28$do_nothing = false;
29
droyc62694d2008-05-14 18:51:30 +000030$affected_rows = 0;
31
droy68ee4942008-02-04 21:24:28 +000032if (empty($translation) || (trim($translation) == '')) {
33
droy2da63a12008-03-27 18:13:05 +000034 $do_nothing = true;
droy68ee4942008-02-04 21:24:28 +000035
36} else if($_POST['translate_action'] != "all"){
gobrien34a08cb2008-02-06 18:44:48 +000037 $query = "update
38 translations
gobriend6da6972008-01-26 00:28:20 +000039 set
gobrien34a08cb2008-02-06 18:44:48 +000040 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);
droyc62694d2008-05-14 18:51:30 +000060 $affected_rows += mysql_affected_rows();
gobrienc5749b72008-02-28 21:43:51 +000061
gobrien198300d2008-03-04 23:22:36 +000062// print $query;
gobriend6da6972008-01-26 00:28:20 +000063}else{
gobrien34a08cb2008-02-06 18:44:48 +000064 //FIND ALL STRINGS THAT ARE THE SAME ACROSS VERSIONS
gobriend6da6972008-01-26 00:28:20 +000065 $query = "select
droya102afc2008-05-21 19:15:42 +000066 s.string_id
gobriend6da6972008-01-26 00:28:20 +000067 from
droya102afc2008-05-21 19:15:42 +000068 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)."')
gobriend6da6972008-01-26 00:28:20 +000071 where
droya102afc2008-05-21 19:15:42 +000072 s.value = (select value from strings where string_id = '".addslashes($string_id)."')
gobriend6da6972008-01-26 00:28:20 +000073 and
droya102afc2008-05-21 19:15:42 +000074 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";
gobriend6da6972008-01-26 00:28:20 +000078
79 $res = mysql_query($query,$dbh);
gobriend6da6972008-01-26 00:28:20 +000080 while($row = mysql_fetch_assoc($res)){
81 $string_ids[] = $row['string_id'];
82 }
83
84 //GET CURRENT TRANSLATION FOR THIS STRING
droya102afc2008-05-21 19:15:42 +000085 $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";
gobriend6da6972008-01-26 00:28:20 +000086 $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
gobrien70d66642008-02-06 18:50:40 +000096 translation_id,string_id,language_id
gobriend6da6972008-01-26 00:28:20 +000097 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
droya102afc2008-05-21 19:15:42 +0000105 and language_id = '" . addslashes($language_id)."'
gobriend6da6972008-01-26 00:28:20 +0000106 ";
gobrien755df672008-01-18 01:01:33 +0000107
gobriend6da6972008-01-26 00:28:20 +0000108 $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'])."',
gobrien70d66642008-02-06 18:50:40 +0000119 language_id = '".addslashes($row['language_id'])."' ,
gobriend6da6972008-01-26 00:28:20 +0000120 value = '".addslashes($translation)."',
121 userid = '".addslashes($user_id)."',
122 created_on = NOW()
123 ";
124 $res2 = mysql_query($query,$dbh);
droyc62694d2008-05-14 18:51:30 +0000125 $affected_rows += mysql_affected_rows();
gobriend6da6972008-01-26 00:28:20 +0000126
127 }
gobrien755df672008-01-18 01:01:33 +0000128
gobriend6da6972008-01-26 00:28:20 +0000129 }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 ";
gobrien755df672008-01-18 01:01:33 +0000144
145 $res = mysql_query($query,$dbh);
146
gobriend6da6972008-01-26 00:28:20 +0000147 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);
droyc62694d2008-05-14 18:51:30 +0000160 $affected_rows += mysql_affected_rows();
droy2da63a12008-03-27 18:13:05 +0000161 }
162 }
gobriend6da6972008-01-26 00:28:20 +0000163}
droy2da63a12008-03-27 18:13:05 +0000164
165if(!$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);
droyc62694d2008-05-14 18:51:30 +0000187 $affected_rows += mysql_affected_rows();
droy2da63a12008-03-27 18:13:05 +0000188 }
189}
gobrien5239b3c2008-06-17 06:51:12 +0000190
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
197print json_encode($response);
198
199?>