2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2002-2008 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/
6 /* Adaptive Technology Resource Centre / University of Toronto */
9 /* This program is free software. You can redistribute it and/or */
10 /* modify it under the terms of the GNU General Public License */
11 /* as published by the Free Software Foundation. */
12 /************************************************************************/
17 * Class for adding/editing language.
19 * @author Heidi Hazelton
20 * @author Joel Kronenberg
23 class LanguageEditor extends Language {
27 // array of missing terms
30 // array of filters ['new', 'update']
36 * Initializes db and parent properties.
38 function LanguageEditor($myLang) {
39 global $db, $addslashes, $msg;
44 $this->addslashes = $addslashes;
47 $this->Language($myLang);
49 $this->missingTerms = array();
53 * Inserts a new language def'n into the database.
55 * @param array $row The language def'n fields as an assoc array.
56 * @return boolean Returns TRUE if the def'n was inserted correctly,
57 * or FALSE, otherwise.
60 function addLanguage($row, $db) {
64 $row['code'] = trim($row['code']);
65 $row['locale'] = trim($row['locale']);
66 $row['charset'] = trim($row['charset']);
67 $row['reg_exp'] = trim($row['reg_exp']);
68 $row['native_name'] = trim($row['native_name']);
69 $row['english_name'] = trim($row['english_name']);
71 $missing_fields = array();
73 if ($row['code'] == '') {
74 $missing_fields[] = _AT('lang_code');
76 if ($row['charset'] == '') {
77 $missing_fields[] = _AT('charset');
79 if ($row['reg_exp'] == '') {
80 $missing_fields[] = _AT('reg_exp');
82 if ($row['native_name'] == '') {
83 $missing_fields[] = _AT('name_in_language');
85 if ($row['english_name'] == '') {
86 $missing_fields[] = _AT('name_in_english');
89 if ($missing_fields) {
90 $missing_fields = implode(', ', $missing_fields);
91 $msg->addError(array('EMPTY_FIELDS', $missing_fields));
94 if (!$msg->containsErrors()) {
95 $row['code'] = $addslashes($row['code']);
96 $row['locale'] = $addslashes($row['locale']);
97 $row['charset'] = $addslashes($row['charset']);
98 $row['direction'] = $addslashes($row['direction']);
99 $row['reg_exp'] = $addslashes($row['reg_exp']);
100 $row['native_name'] = $addslashes($row['native_name']);
101 $row['english_name'] = $addslashes($row['english_name']);
103 if (!empty($row['locale'])) {
104 $row['code'] .= AT_LANGUAGE_LOCALE_SEP . strtolower($row['locale']);
107 $sql = "INSERT INTO ".TABLE_PREFIX."languages VALUES ('$row[code]', '$row[charset]', '$row[direction]', '$row[reg_exp]', '$row[native_name]', '$row[english_name]', 3)";
109 if (mysql_query($sql, $db)) {
120 // $row = the language info array
121 // $new_exists whether the new code+locale exists already
122 // returns true or false, depending on success if db update
123 // can be called staticly
124 function updateLanguage($row, $new_exists) {
125 $missing_fields = array();
127 if ($row['code'] == '') {
128 $missing_fields[] = _AT('lang_code');
130 if ($row['charset'] == '') {
131 $missing_fields[] = _AT('charset');
133 if ($row['reg_exp'] == '') {
134 $missing_fields[] = _AT('reg_exp');
136 if ($row['native_name'] == '') {
137 $missing_fields[] = _AT('name_in_language');
139 if ($row['english_name'] == '') {
140 $missing_fields[] = _AT('name_in_english');
143 if ($missing_fields) {
144 $missing_fields = implode(', ', $missing_fields);
145 $msg->addError(array('EMPTY_FIELDS', $missing_fields));
149 if (!$this->msg->containsErrors()) {
153 $row['code'] = strtolower($addslashes($row['code']));
154 if (!empty($row['locale'])) {
155 $row['code'] .= AT_LANGUAGE_LOCALE_SEP . strtolower($addslashes($row['locale']));
157 $row['charset'] = strtolower($addslashes($row['charset']));
158 $row['direction'] = strtolower($addslashes($row['direction']));
159 $row['reg_exp'] = strtolower($addslashes($row['reg_exp']));
160 $row['native_name'] = $addslashes($row['native_name']);
161 $row['english_name'] = $addslashes($row['english_name']);
162 if (isset($row['status'])) {
163 $row['status'] = intval($row['status']);
164 $status_sql = ', status='.$row['status'];
169 if ($row['old_code'] == $row['code']) {
170 $sql = "UPDATE ".TABLE_PREFIX."languages SET char_set='$row[charset]', direction='$row[direction]', reg_exp='$row[reg_exp]', native_name='$row[native_name]', english_name='$row[english_name]' $status_sql WHERE language_code='$row[code]'";
171 mysql_query($sql, $db);
174 } else if ($new_exists) {
175 $this->msg->addError('LANG_EXISTS');
178 $sql = "UPDATE ".TABLE_PREFIX."languages SET language_code='$row[code]', char_set='$row[charset]', direction='$row[direction]', reg_exp='$row[reg_exp]', native_name='$row[native_name]', english_name='$row[english_name]' $status_sql WHERE language_code='$row[old_code]'";
179 mysql_query($sql, $db);
181 $sql = "UPDATE ".TABLE_PREFIX."language_text SET language_code='$row[code]' WHERE language_code='$row[old_code]'";
182 mysql_query($sql, $db);
191 function deleteLanguage() {
192 $sql = "DELETE FROM ".TABLE_PREFIX."languages WHERE language_code='$this->code'";
193 mysql_query($sql, $this->db);
195 $sql = "DELETE FROM ".TABLE_PREFIX."language_text WHERE language_code='$this->code'";
196 mysql_query($sql, $this->db);
198 $sql = "UPDATE ".TABLE_PREFIX."members SET language='".DEFAULT_LANGUAGE."', creation_date=creation_date, last_login=last_login WHERE language='$this->code'";
199 mysql_query($sql, $this->db);
201 $sql = "UPDATE ".TABLE_PREFIX."courses SET primary_language='".DEFAULT_LANGUAGE."' WHERE primary_language='$this->code'";
202 mysql_query($sql, $this->db);
204 cache_purge('system_langs', 'system_langs');
208 function updateTerm($variable, $term, $text) {
209 $addslashes = $this->addslashes;
211 $variable = $addslashes($variable);
212 $term = $addslashes($term);
213 $text = $addslashes($text);
214 $code = $addslashes($this->getCode());
216 $sql = "UPDATE ".TABLE_PREFIX."language_text SET text='$text', revised_date=NOW() WHERE language_code='$code' AND variable='$variable' AND term='$term'";
219 if (mysql_query($sql, $this->db)) {
222 debug(mysql_error($this->db));
229 function insertTerm($variable, $key, $text, $context) {
230 $addslashes = $this->addslashes;
232 $variable = $addslashes($variable);
233 $key = $addslashes($key);
234 $text = $addslashes($text);
235 $code = $addslashes($this->getCode());
236 $context = $addslashes($context);
238 $sql = "INSERT INTO ".TABLE_PREFIX."language_text VALUES('$code', '$variable', '$key', '$text', NOW(), '$context')";
242 function showMissingTermsFrame(){
243 global $_base_path, $addslashes;
244 //$terms = array_slice($this->missingTerms, 0, 20);
245 $terms = $this->missingTerms;
246 $terms = serialize($terms);
247 $terms = urlencode($terms);
249 echo '<div align="center"><iframe src="'.$_base_path.'admin/missing_language.php?terms='.$terms.SEP.'lang='.$_SESSION['lang'].'" width="99%" height="300"></div>';
253 // doesn't actually check if params is one of the possible ones.
254 // possible params should be array ('new', 'update')
255 function setFilter($params){
256 if (!is_array($params)) {
260 foreach($params as $param => $garbage) {
261 $this->filters[$param] = true;
266 function checkFilter($param) {
267 if ($this->filters[$param]) {
274 function printTerms($terms){
275 global $addslashes, $languageManager; // why won't $addslashes = $this->addslashes; work?
279 $terms = unserialize(stripslashes($addslashes($terms)));
283 if ($this->checkFilter('new')) {
284 $new_check = ' checked="checked"';
286 if ($this->checkFilter('update')) {
287 $update_check = ' checked="checked"';
290 $fromLanguage =& $languageManager->getLanguage('en');
292 echo '<form method="post" action="'.htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES).'">';
293 echo '<table border="0" cellpadding="0" cellspacing="2">';
296 echo '<input name="filter_new" id="n" value="1" type="checkbox" '.$new_check.' /><label for="n">New Language</label>, ';
297 echo '<input name="filter_update" id="u" value="1" type="checkbox" '.$update_check.' /><label for="u">Updated Language</label> ';
301 foreach($terms as $term => $garbage) {
302 $to_term = $this->getTerm($term);
303 $from_term = $fromLanguage->getTerm($term);
306 if ($to_term === false) {
311 if ($to_term['revised_date_unix'] < $from_term['revised_date_unix']) {
316 if ($this->checkFilter('new') && !$is_new) {
320 if ($this->checkFilter('update') && !$is_old) {
324 if (($counter % 10) == 0) {
326 echo '<td align="center"><input type="submit" name="submit" value="Save Changes" class="button" /></td>';
332 $style = 'style="background-color: white; border: red 2px solid;"';
334 $style = 'style="background-color: white; border: yellow 1px solid;"';
338 echo '<td><strong>[ ' . $term . ' ] '.htmlspecialchars($from_term['text']).'</strong></td></tr>';
339 echo '<tr><td><input type="text" name="'.$term.'" '.$style.' size="100" value="'.htmlspecialchars($to_term['text']).'" />';
340 echo '<input type="hidden" name="old['.$term.']" '.$style.' size="100" value="'.htmlspecialchars($to_term['text']).'" /></td>';
350 function updateTerms($terms) {
353 foreach($terms as $term => $text) {
354 $text = $addslashes($text);
355 $term = $addslashes($term);
357 if (($text != '') && ($text != $_POST['old'][$term])) {
358 $sql = "REPLACE INTO ".TABLE_PREFIX."language_text VALUES ('".$this->getCode()."', '_template', '$term', '$text', NOW(), '')";
359 mysql_query($sql, $this->db);
365 function addMissingTerm($term) {
366 if (!isset($this->missingTerms[$term])) {
367 $this->missingTerms[$term] = '';
372 // this method should be called staticly: LanguageEditor::import()
374 function import($language_sql_file) {
375 // move sql import class from install/ to include/classes/
376 // store the lang def'n in a .ini file and use insertLang
377 // after checking if it already exists
379 // use the sql class to insert the language into the db
381 // check if this language exists before calling this method
383 require_once(AT_INCLUDE_PATH . 'classes/sqlutility.class.php');
384 $sqlUtility =& new SqlUtility();
386 $sqlUtility->queryFromFile($language_sql_file, TABLE_PREFIX);
389 // sends the generated language pack to the browser
391 function export($filename = '') {
392 $search = array('"', "'", "\x00", "\x0a", "\x0d", "\x1a"); //\x08\\x09, not required
393 $replace = array('\"', "\'", '\0', '\n', '\r', '\Z');
395 // use a function to generate the ini file
396 // use a diff fn to generate the sql dump
397 // use the zipfile class to package the ini file and the sql dump
398 $sql_dump = "INSERT INTO `languages` VALUES ('$this->code', '$this->characterSet', '$this->direction', '$this->regularExpression', '$this->nativeName', '$this->englishName', $this->status);\r\n\r\n";
400 $sql_dump .= "INSERT INTO `language_text` VALUES ";
402 $sql = "SELECT * FROM ".TABLE_PREFIX."language_text WHERE language_code='$this->code' ORDER BY variable, term";
403 $result = mysql_query($sql, $this->db);
404 if ($row = mysql_fetch_assoc($result)) {
406 $row['text'] = str_replace($search, $replace, $row['text']);
407 $row['context'] = str_replace($search, $replace, $row['context']);
409 $sql_dump .= "('$this->code', '$row[variable]', '$row[term]', '$row[text]', '$row[revised_date]', '$row[context]'),\r\n";
410 } while ($row = mysql_fetch_assoc($result));
412 $this->msg->addError('LANG_EMPTY');
414 $sql_dump = substr($sql_dump, 0, -3) . ";";
416 $readme = 'This is an ATutor language pack. Use the administrator Language section to import this language pack or manually import the contents of the SQL file into your [table_prefix]language_text table, where `table_prefix` should be replaced with your correct ATutor table prefix as defined in ./include/config.inc.php . Additional Language Packs can be found on http://atutor.ca .';
418 require(AT_INCLUDE_PATH . 'classes/zipfile.class.php');
419 $zipfile =& new zipfile();
421 $zipfile->add_file($sql_dump, 'language_text.sql');
422 $zipfile->add_file($readme, 'readme.txt');
423 $zipfile->add_file($this->getXML(), 'language.xml');
426 $fp = fopen($filename, 'wb+');
427 fwrite($fp, $zipfile->get_file(), $zipfile->get_size());
429 $version = str_replace('.','_',VERSION);
431 $zipfile->send_file('atutor_' . $version . '_' . $this->code);