| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* File translation.str_mgmt.php
|
|
|
4 |
*
|
|
|
5 |
* Additional functions for Translation class
|
|
|
6 |
*
|
|
|
7 |
* Functions allowing user to create the new language, translation for specific
|
|
|
8 |
* strings and full management of the languages database.
|
|
|
9 |
*
|
|
|
10 |
* @author Wojciech Zieliñski <voyteck@caffe.com.pl>
|
|
|
11 |
* @author Lorenzo Alberton <l dot alberton at quipo dot it>
|
|
|
12 |
* @version 1.3
|
|
|
13 |
* @access public
|
|
|
14 |
* @package Translation
|
|
|
15 |
*/
|
|
|
16 |
/**
|
|
|
17 |
* require PEAR::DB
|
|
|
18 |
*/
|
|
|
19 |
require_once 'DB.php';
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* helper method
|
|
|
23 |
*/
|
|
|
24 |
function setDefaultTableDefinitions($LangID, $CustomTables)
|
|
|
25 |
{
|
|
|
26 |
//set defaults
|
|
|
27 |
$TableDefinitions = array(
|
|
|
28 |
'langsavail' => array(
|
|
|
29 |
'name' => 'tr_langsavail',
|
|
|
30 |
'lang_id' => 'lang_id',
|
|
|
31 |
'lang_name' => 'name',
|
|
|
32 |
'metatags' => 'metatags',
|
|
|
33 |
'errortext' => 'errortext'
|
|
|
34 |
),
|
|
|
35 |
'strings_'.$LangID => array(
|
|
|
36 |
'name' => 'strings_'.$LangID,
|
|
|
37 |
'page_id' => 'page_id',
|
|
|
38 |
'string_id' => 'string_id',
|
|
|
39 |
'string' => 'string'
|
|
|
40 |
)
|
|
|
41 |
);
|
|
|
42 |
if (is_array($CustomTables['langsavail'])) {
|
|
|
43 |
$TableDefinitions['langsavail']['name'] = isset($CustomTables['langsavail']['name']) ? $CustomTables['langsavail']['name'] : 'tr_langsavail';
|
|
|
44 |
$TableDefinitions['langsavail']['lang_id'] = isset($CustomTables['langsavail']['lang_id']) ? $CustomTables['langsavail']['lang_id'] : 'lang_id';
|
|
|
45 |
$TableDefinitions['langsavail']['lang_name'] = isset($CustomTables['langsavail']['lang_name']) ? $CustomTables['langsavail']['lang_name'] : 'lang_name';
|
|
|
46 |
$TableDefinitions['langsavail']['metatags'] = isset($CustomTables['langsavail']['metatags']) ? $CustomTables['langsavail']['metatags'] : 'metatags';
|
|
|
47 |
$TableDefinitions['langsavail']['errortext'] = isset($CustomTables['langsavail']['errortext']) ? $CustomTables['langsavail']['errortext'] : 'errortext';
|
|
|
48 |
} elseif (!empty($CustomTables['langsavail'])) {
|
|
|
49 |
$TableDefinitions['langsavail']['name'] = $CustomTables['langsavail'];
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
if (is_array($CustomTables['strings_'.$LangID])) {
|
|
|
53 |
$TableDefinitions['strings_'.$LangID]['name'] = isset($CustomTables['strings_'.$LangID]['name']) ? $CustomTables['strings_'.$LangID]['name'] : 'strings_'.$LangID;
|
|
|
54 |
$TableDefinitions['strings_'.$LangID]['page_id'] = isset($CustomTables['strings_'.$LangID]['page_id']) ? $CustomTables['strings_'.$LangID]['page_id'] : 'page_id';
|
|
|
55 |
$TableDefinitions['strings_'.$LangID]['string_id'] = isset($CustomTables['strings_'.$LangID]['string_id']) ? $CustomTables['strings_'.$LangID]['string_id'] : 'string_id';
|
|
|
56 |
$TableDefinitions['strings_'.$LangID]['string'] = isset($CustomTables['strings_'.$LangID]['string']) ? $CustomTables['strings_'.$LangID]['string'] : 'string';
|
|
|
57 |
} elseif (!empty($CustomTables['strings_'.$LangID])) {
|
|
|
58 |
$TableDefinitions['strings_'.$LangID]['name'] = $CustomTables['strings_'.$LangID];
|
|
|
59 |
}
|
|
|
60 |
return $TableDefinitions;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* New language creation
|
|
|
66 |
*
|
|
|
67 |
* Creates new language in the system.
|
|
|
68 |
* Creates lang entry in the languages table and the table for language strings.
|
|
|
69 |
* If other langs have been created before and their tables were filled with
|
|
|
70 |
* strings, function addTranslation should be executed for each of the added
|
|
|
71 |
* strings just after calling this function and before using the Translation class
|
|
|
72 |
* for any purpose.
|
|
|
73 |
*
|
|
|
74 |
* @param string $LangID Language identifier
|
|
|
75 |
* @param string $LangName Language name - store the language name of the lang,
|
|
|
76 |
* possibly in the language described. This name can be later
|
|
|
77 |
* retrieved by calling getLangName and getOtherLangs methods
|
|
|
78 |
* and used for hyperlinks changing the site language.
|
|
|
79 |
* @param string $METATags Tags that may describe the language codepage etc.
|
|
|
80 |
* These tags can be retrieved by calling getMetaTags method.
|
|
|
81 |
* @param string $pear_DSN PEAR DSN string for database connection
|
|
|
82 |
* @param array $CustomTables Custom table definitions
|
|
|
83 |
* @return mixed Return 1 if everything went OK, a PEAR::DB_Error object if not.
|
|
|
84 |
*/
|
|
|
85 |
function createNewLang($LangID, $LangName, $METATags, $pear_DSN, $CustomTables=0)
|
|
|
86 |
{
|
|
|
87 |
$TableDefinitions = setDefaultTableDefinitions($LangID, $CustomTables);
|
|
|
88 |
|
|
|
89 |
$db = DB::connect($pear_DSN);
|
|
|
90 |
if (DB::isError($db)) {
|
|
|
91 |
return $db;
|
|
|
92 |
}
|
|
|
93 |
$query = sprintf('INSERT INTO %s (%s, %s, %s) VALUES ("%s", "%s", "%s")',
|
|
|
94 |
$TableDefinitions['langsavail']['name'],
|
|
|
95 |
$TableDefinitions['langsavail']['lang_id'],
|
|
|
96 |
$TableDefinitions['langsavail']['lang_name'],
|
|
|
97 |
$TableDefinitions['langsavail']['metatags'],
|
|
|
98 |
addslashes($LangID),
|
|
|
99 |
addslashes($LangName),
|
|
|
100 |
addslashes($METATags)
|
|
|
101 |
);
|
|
|
102 |
$result = $db->query($query);
|
|
|
103 |
if (DB::isError($result)) {
|
|
|
104 |
return $result;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
//check if the table already exists
|
|
|
108 |
$exists = false;
|
|
|
109 |
$res = $db->query('SHOW TABLES');
|
|
|
110 |
if (DB::isError($res)) {
|
|
|
111 |
return $res;
|
|
|
112 |
}
|
|
|
113 |
while ($row = $res->fetchRow()) {
|
|
|
114 |
if ($row[0] == 'tr_strings_'.$LangID) {
|
|
|
115 |
$exists = true;
|
|
|
116 |
break;
|
|
|
117 |
}
|
|
|
118 |
}
|
|
|
119 |
if (!$exists) {
|
|
|
120 |
$query = 'CREATE TABLE '. $TableDefinitions['strings_'.$LangID]['name'] .'('
|
|
|
121 |
. $TableDefinitions['strings_'.$LangID]['page_id'] .' VARCHAR(16) default NULL, '
|
|
|
122 |
. $TableDefinitions['strings_'.$LangID]['string_id'].' VARCHAR(32) NOT NULL, '
|
|
|
123 |
. $TableDefinitions['strings_'.$LangID]['string'] .' TEXT, '
|
|
|
124 |
.'UNIQUE KEY page_id ('.$TableDefinitions['strings_'.$LangID]['page_id'].', '
|
|
|
125 |
.$TableDefinitions['strings_'.$LangID]['string_id'].'))';
|
|
|
126 |
$result = $db->query($query);
|
|
|
127 |
if (DB::isError($result)) {
|
|
|
128 |
$query = sprintf('DELETE FROM %s WHERE %s="%s"',
|
|
|
129 |
$TableDefinitions['langsavail']['name'],
|
|
|
130 |
$TableDefinitions['langsavail']['lang_id'],
|
|
|
131 |
addslashes($LangID)
|
|
|
132 |
);
|
|
|
133 |
$delresult = $db->query($query);
|
|
|
134 |
return $result;
|
|
|
135 |
}
|
|
|
136 |
}
|
|
|
137 |
return 1;
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* Language removal
|
|
|
142 |
*
|
|
|
143 |
* Removes language from system.
|
|
|
144 |
* This function should be used carefully - it will permanently remove all the
|
|
|
145 |
* strings that has been added to the language table by dropping this table.
|
|
|
146 |
* If other langs are stored in the table, then only this lang column will be dropped.
|
|
|
147 |
*
|
|
|
148 |
* @param string $LangID Language identifier
|
|
|
149 |
* @param string $pear_DSN PEAR DSN string for database connection
|
|
|
150 |
* @param array $CustomTables Custom table definitions
|
|
|
151 |
* @param boolean $force If true, the table is dropped without checks
|
|
|
152 |
* @return mixed Return 1 if everything went OK, a PEAR::DB_Error object if not.
|
|
|
153 |
*/
|
|
|
154 |
function removeLang($LangID, $pear_DSN, $CustomTables=0, $force=false)
|
|
|
155 |
{
|
|
|
156 |
$db = DB::connect($pear_DSN);
|
|
|
157 |
if (DB::isError($db)) {
|
|
|
158 |
return $db;
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
$TableDefinitions = setDefaultTableDefinitions($LangID, $CustomTables);
|
|
|
162 |
|
|
|
163 |
if (!$force) {
|
|
|
164 |
//check if other langs are stored in this table
|
|
|
165 |
//'DESCRIBE' == 'SHOW COLUMNS FROM'
|
|
|
166 |
$res = $db->query('DESCRIBE '.$TableDefinitions['strings_'.$LangID]['name']);
|
|
|
167 |
if (DB::isError($res)) {
|
|
|
168 |
return $res;
|
|
|
169 |
}
|
|
|
170 |
if ($res->numRows() > 3) {
|
|
|
171 |
$query = 'ALTER TABLE '. $TableDefinitions['strings_'.$LangID]['name']
|
|
|
172 |
.' DROP COLUMN '. $TableDefinitions['strings_'.$LangID]['string'];
|
|
|
173 |
$res = $db->query('DESCRIBE '.$TableDefinitions['strings_'.$LangID]['name']);
|
|
|
174 |
if (DB::isError($res)) {
|
|
|
175 |
return $res;
|
|
|
176 |
}
|
|
|
177 |
return 1;
|
|
|
178 |
}
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
$result = $db->query('DROP TABLE '.$TableDefinitions['strings_'.$LangID]['name']);
|
|
|
182 |
if (DB::isError($result)) {
|
|
|
183 |
return $result;
|
|
|
184 |
}
|
|
|
185 |
$query = sprintf('DELETE FROM %s WHERE %s="%s"',
|
|
|
186 |
$TableDefinitions['langsavail']['name'],
|
|
|
187 |
$TableDefinitions['langsavail']['lang_id'],
|
|
|
188 |
$LangID);
|
|
|
189 |
$result = $db->query($query);
|
|
|
190 |
if (DB::isError($result)) {
|
|
|
191 |
return $result;
|
|
|
192 |
}
|
|
|
193 |
return 1;
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
/**
|
|
|
197 |
* Translation adding
|
|
|
198 |
* Adds string to one or more language tables.
|
|
|
199 |
*
|
|
|
200 |
* @param string $PageID page identifier. Might be "" if the string is to be
|
|
|
201 |
* available from any page, independendly from translation
|
|
|
202 |
* object creation parameters.
|
|
|
203 |
* @param string $StringID string identifier. Must be unique for the same PageID
|
|
|
204 |
* and strings that were created without PageID's.
|
|
|
205 |
* This rule must be respected to prevent ambiguities.
|
|
|
206 |
* @param array $String array of strings - the array keys should be languages id's,
|
|
|
207 |
* the values - the sttrings in these languages - e.g.:
|
|
|
208 |
* ("en"->"English text", "pl"->"Tekst polski", ...)
|
|
|
209 |
* @param string $pear_DSN PEAR DSN string for database connection
|
|
|
210 |
* @param array $CustomTables Custom table definitions
|
|
|
211 |
* @return mixed Return 1 if everything went OK, a PEAR::DB_Error object if not.
|
|
|
212 |
*/
|
|
|
213 |
function addTranslation($PageID, $StringID, $String, $pear_DSN, $CustomTables=0)
|
|
|
214 |
{
|
|
|
215 |
$db = DB::connect($pear_DSN);
|
|
|
216 |
if (DB::isError($db)) {
|
|
|
217 |
return $db;
|
|
|
218 |
}
|
|
|
219 |
$TableDefinitions = array();
|
|
|
220 |
$langs = array_keys($String);
|
|
|
221 |
foreach ($langs as $aLang) {
|
|
|
222 |
$TableDefinitions = array_merge_recursive(
|
|
|
223 |
$TableDefinitions,
|
|
|
224 |
setDefaultTableDefinitions($aLang, $CustomTables)
|
|
|
225 |
);
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
foreach ($String as $LangID => $Text) {
|
|
|
229 |
$data[] = array($TableDefinitions['strings_'.$LangID]['name'], $Text);
|
|
|
230 |
}
|
|
|
231 |
$query = sprintf('INSERT INTO ! (%s, %s, %s) VALUES ("%s", "%s", ?)',
|
|
|
232 |
$TableDefinitions['strings_'.$LangID]['page_id'],
|
|
|
233 |
$TableDefinitions['strings_'.$LangID]['string_id'],
|
|
|
234 |
$TableDefinitions['strings_'.$LangID]['string'],
|
|
|
235 |
addslashes($PageID),
|
|
|
236 |
addslashes($StringID)
|
|
|
237 |
);
|
|
|
238 |
$result = $db->executeMultiple(($db->prepare($query)), $data);
|
|
|
239 |
if (DB::isError($result)) {
|
|
|
240 |
return $result;
|
|
|
241 |
}
|
|
|
242 |
return 1;
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
/**
|
|
|
246 |
* Translation removal
|
|
|
247 |
*
|
|
|
248 |
* Removes string from all of string tables
|
|
|
249 |
* @param string $PageID page identifier.
|
|
|
250 |
* @param string $StringID string identifier.
|
|
|
251 |
* @param string $pear_DSN PEAR DSN string for database connection
|
|
|
252 |
* @param array $CustomTables Custom table definitions
|
|
|
253 |
* @return mixed Return 1 if everything went OK, a PEAR::DB_Error object if not.
|
|
|
254 |
*/
|
|
|
255 |
function removeTranslation($PageID, $StringID, $pear_DSN, $CustomTables=0)
|
|
|
256 |
{
|
|
|
257 |
$db = DB::connect($pear_DSN);
|
|
|
258 |
if (DB::isError($db)) {
|
|
|
259 |
return $db;
|
|
|
260 |
}
|
|
|
261 |
|
|
|
262 |
$result = $db->query('SELECT '. $TableDefinitions['langsavail']['lang_id']
|
|
|
263 |
.' FROM '. $TableDefinitions['langsavail']['name']);
|
|
|
264 |
if (DB::isError($result)) {
|
|
|
265 |
return $result;
|
|
|
266 |
}
|
|
|
267 |
while ($row = $result->fetchRow()) {
|
|
|
268 |
$languages[] = $row[0];
|
|
|
269 |
}
|
|
|
270 |
$TableDefinitions = array();
|
|
|
271 |
foreach ($langs as $LangID) {
|
|
|
272 |
$TableDefinitions = array_merge_recursive(
|
|
|
273 |
$TableDefinitions,
|
|
|
274 |
setDefaultTableDefinitions($LangID, $CustomTables)
|
|
|
275 |
);
|
|
|
276 |
$query = sprintf('DELETE FROM %s WHERE %s="%s" AND %s="%s"',
|
|
|
277 |
$TableDefinitions['strings_'.$LangID]['name'],
|
|
|
278 |
$TableDefinitions['strings_'.$LangID]['page_id'],
|
|
|
279 |
addslashes($PageID),
|
|
|
280 |
$TableDefinitions['strings_'.$LangID]['string_id'],
|
|
|
281 |
addslashes($StringID)
|
|
|
282 |
);
|
|
|
283 |
$result = $db->query($query);
|
|
|
284 |
if (DB::isError($result)) {
|
|
|
285 |
return $result;
|
|
|
286 |
}
|
|
|
287 |
}
|
|
|
288 |
return 1;
|
|
|
289 |
}
|
|
|
290 |
?>
|