2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2002-2010 */
6 /* Inclusive Design Institute */
8 /* This program is free software. You can redistribute it and/or */
9 /* modify it under the terms of the GNU General Public License */
10 /* as published by the Free Software Foundation. */
11 /************************************************************************/
16 * Class for adding/editing language.
18 * @author Heidi Hazelton
19 * @author Joel Kronenberg
22 class LanguageEditor extends Language {
26 // array of missing terms
29 // array of filters ['new', 'update']
35 * Initializes db and parent properties.
37 function LanguageEditor($myLang) {
38 global $db, $addslashes, $msg;
43 $this->addslashes = $addslashes;
46 $this->Language($myLang);
48 $this->missingTerms = array();
52 * Inserts a new language def'n into the database.
54 * @param array $row The language def'n fields as an assoc array.
55 * @return boolean Returns TRUE if the def'n was inserted correctly,
56 * or FALSE, otherwise.
59 function addLanguage($row, $db) {
63 $row['code'] = trim($row['code']);
64 $row['locale'] = trim($row['locale']);
65 $row['charset'] = trim($row['charset']);
66 $row['reg_exp'] = trim($row['reg_exp']);
67 $row['native_name'] = trim($row['native_name']);
68 $row['english_name'] = trim($row['english_name']);
70 $missing_fields = array();
72 if ($row['code'] == '') {
73 $missing_fields[] = _AT('lang_code');
75 if ($row['charset'] == '') {
76 $missing_fields[] = _AT('charset');
78 if ($row['reg_exp'] == '') {
79 $missing_fields[] = _AT('reg_exp');
81 if ($row['native_name'] == '') {
82 $missing_fields[] = _AT('name_in_language');
84 if ($row['english_name'] == '') {
85 $missing_fields[] = _AT('name_in_english');
88 if ($missing_fields) {
89 $missing_fields = implode(', ', $missing_fields);
90 $msg->addError(array('EMPTY_FIELDS', $missing_fields));
93 if (!$msg->containsErrors()) {
94 $row['code'] = $addslashes($row['code']);
95 $row['locale'] = $addslashes($row['locale']);
96 $row['charset'] = $addslashes($row['charset']);
97 $row['direction'] = $addslashes($row['direction']);
98 $row['reg_exp'] = $addslashes($row['reg_exp']);
99 $row['native_name'] = $addslashes($row['native_name']);
100 $row['english_name'] = $addslashes($row['english_name']);
102 if (!empty($row['locale'])) {
103 $row['code'] .= AT_LANGUAGE_LOCALE_SEP . strtolower($row['locale']);
106 $sql = "INSERT INTO ".TABLE_PREFIX."languages VALUES ('$row[code]', '$row[charset]', '$row[direction]', '$row[reg_exp]', '$row[native_name]', '$row[english_name]', 3)";
108 if (mysql_query($sql, $db)) {
119 // $row = the language info array
120 // $new_exists whether the new code+locale exists already
121 // returns true or false, depending on success if db update
122 // can be called staticly
123 function updateLanguage($row, $new_exists) {
124 $missing_fields = array();
126 if ($row['code'] == '') {
127 $missing_fields[] = _AT('lang_code');
129 if ($row['charset'] == '') {
130 $missing_fields[] = _AT('charset');
132 if ($row['reg_exp'] == '') {
133 $missing_fields[] = _AT('reg_exp');
135 if ($row['native_name'] == '') {
136 $missing_fields[] = _AT('name_in_language');
138 if ($row['english_name'] == '') {
139 $missing_fields[] = _AT('name_in_english');
142 if ($missing_fields) {
143 $missing_fields = implode(', ', $missing_fields);
144 $msg->addError(array('EMPTY_FIELDS', $missing_fields));
148 if (!$this->msg->containsErrors()) {
152 $row['code'] = strtolower($addslashes($row['code']));
153 if (!empty($row['locale'])) {
154 $row['code'] .= AT_LANGUAGE_LOCALE_SEP . strtolower($addslashes($row['locale']));
156 $row['charset'] = strtolower($addslashes($row['charset']));
157 $row['direction'] = strtolower($addslashes($row['direction']));
158 $row['reg_exp'] = strtolower($addslashes($row['reg_exp']));
159 $row['native_name'] = $addslashes($row['native_name']);
160 $row['english_name'] = $addslashes($row['english_name']);
161 if (isset($row['status'])) {
162 $row['status'] = intval($row['status']);
163 $status_sql = ', status='.$row['status'];
168 if ($row['old_code'] == $row['code']) {
169 $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]'";
170 mysql_query($sql, $db);
173 } else if ($new_exists) {
174 $this->msg->addError('LANG_EXISTS');
177 $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]'";
178 mysql_query($sql, $db);
180 $sql = "UPDATE ".TABLE_PREFIX."language_text SET language_code='$row[code]' WHERE language_code='$row[old_code]'";
181 mysql_query($sql, $db);
190 function deleteLanguage() {
191 $sql = "DELETE FROM ".TABLE_PREFIX."languages WHERE language_code='$this->code'";
192 mysql_query($sql, $this->db);
194 $sql = "DELETE FROM ".TABLE_PREFIX."language_text WHERE language_code='$this->code'";
195 mysql_query($sql, $this->db);
197 $sql = "UPDATE ".TABLE_PREFIX."members SET language='".DEFAULT_LANGUAGE."', creation_date=creation_date, last_login=last_login WHERE language='$this->code'";
198 mysql_query($sql, $this->db);
200 $sql = "UPDATE ".TABLE_PREFIX."courses SET primary_language='".DEFAULT_LANGUAGE."' WHERE primary_language='$this->code'";
201 mysql_query($sql, $this->db);
203 cache_purge('system_langs', 'system_langs');
207 function updateTerm($variable, $term, $text) {
208 $addslashes = $this->addslashes;
210 $variable = $addslashes($variable);
211 $term = $addslashes($term);
212 $text = $addslashes($text);
213 $code = $addslashes($this->getCode());
215 $sql = "UPDATE ".TABLE_PREFIX."language_text SET text='$text', revised_date=NOW() WHERE language_code='$code' AND variable='$variable' AND term='$term'";
218 if (mysql_query($sql, $this->db)) {
221 debug(mysql_error($this->db));
228 function insertTerm($variable, $key, $text, $context) {
229 $addslashes = $this->addslashes;
231 $variable = $addslashes($variable);
232 $key = $addslashes($key);
233 $text = $addslashes($text);
234 $code = $addslashes($this->getCode());
235 $context = $addslashes($context);
237 $sql = "INSERT INTO ".TABLE_PREFIX."language_text VALUES('$code', '$variable', '$key', '$text', NOW(), '$context')";
241 function showMissingTermsFrame(){
242 global $_base_path, $addslashes;
243 //$terms = array_slice($this->missingTerms, 0, 20);
244 $terms = $this->missingTerms;
245 $terms = serialize($terms);
246 $terms = urlencode($terms);
248 echo '<div align="center"><iframe src="'.$_base_path.'mods/_core/languages/missing_language.php?terms='.$terms.SEP.'lang='.$_SESSION['lang'].'" width="99%" height="300"></div>';
252 // doesn't actually check if params is one of the possible ones.
253 // possible params should be array ('new', 'update')
254 function setFilter($params){
255 if (!is_array($params)) {
259 foreach($params as $param => $garbage) {
260 $this->filters[$param] = true;
265 function checkFilter($param) {
266 if ($this->filters[$param]) {
273 function printTerms($terms){
274 global $addslashes, $languageManager; // why won't $addslashes = $this->addslashes; work?
278 $terms = unserialize(stripslashes($addslashes($terms)));
282 if ($this->checkFilter('new')) {
283 $new_check = ' checked="checked"';
285 if ($this->checkFilter('update')) {
286 $update_check = ' checked="checked"';
289 $fromLanguage = $languageManager->getLanguage('en');
291 echo '<form method="post" action="'.htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES).'">';
292 echo '<table border="0" cellpadding="0" cellspacing="2">';
295 echo '<input name="filter_new" id="n" value="1" type="checkbox" '.$new_check.' /><label for="n">New Language</label>, ';
296 echo '<input name="filter_update" id="u" value="1" type="checkbox" '.$update_check.' /><label for="u">Updated Language</label> ';
300 foreach($terms as $term => $garbage) {
301 $to_term = $this->getTerm($term);
302 $from_term = $fromLanguage->getTerm($term);
305 if ($to_term === false) {
310 if ($to_term['revised_date_unix'] < $from_term['revised_date_unix']) {
315 if ($this->checkFilter('new') && !$is_new) {
319 if ($this->checkFilter('update') && !$is_old) {
323 if (($counter % 10) == 0) {
325 echo '<td align="center"><input type="submit" name="submit" value="Save Changes" class="button" /></td>';
331 $style = 'style="background-color: white; border: red 2px solid;"';
333 $style = 'style="background-color: white; border: yellow 1px solid;"';
337 echo '<td><strong>[ ' . $term . ' ] '.htmlspecialchars($from_term['text']).'</strong></td></tr>';
338 echo '<tr><td><input type="text" name="'.$term.'" '.$style.' size="100" value="'.htmlspecialchars($to_term['text']).'" />';
339 echo '<input type="hidden" name="old['.$term.']" '.$style.' size="100" value="'.htmlspecialchars($to_term['text']).'" /></td>';
349 function updateTerms($terms) {
352 foreach($terms as $term => $text) {
353 $text = $addslashes($text);
354 $term = $addslashes($term);
356 if (($text != '') && ($text != $_POST['old'][$term])) {
357 $sql = "REPLACE INTO ".TABLE_PREFIX."language_text VALUES ('".$this->getCode()."', '_template', '$term', '$text', NOW(), '')";
358 mysql_query($sql, $this->db);
364 function addMissingTerm($term) {
365 if (!isset($this->missingTerms[$term])) {
366 $this->missingTerms[$term] = '';
371 // this method should be called staticly: LanguageEditor::import()
373 function import($language_sql_file) {
374 // move sql import class from install/ to include/classes/
375 // store the lang def'n in a .ini file and use insertLang
376 // after checking if it already exists
378 // use the sql class to insert the language into the db
380 // check if this language exists before calling this method
382 require_once(AT_INCLUDE_PATH . 'classes/sqlutility.class.php');
383 $sqlUtility = new SqlUtility();
385 $sqlUtility->queryFromFile($language_sql_file, TABLE_PREFIX);
388 // sends the generated language pack to the browser
390 function export($filename = '') {
391 $search = array('"', "'", "\x00", "\x0a", "\x0d", "\x1a"); //\x08\\x09, not required
392 $replace = array('\"', "\'", '\0', '\n', '\r', '\Z');
394 // use a function to generate the ini file
395 // use a diff fn to generate the sql dump
396 // use the zipfile class to package the ini file and the sql dump
397 $sql_dump = "INSERT INTO `languages` VALUES ('$this->code', '$this->characterSet', '$this->direction', '$this->regularExpression', '$this->nativeName', '$this->englishName', $this->status);\r\n\r\n";
399 $sql_dump .= "INSERT INTO `language_text` VALUES ";
401 $sql = "SELECT * FROM ".TABLE_PREFIX."language_text WHERE language_code='$this->code' ORDER BY variable, term";
402 $result = mysql_query($sql, $this->db);
403 if ($row = mysql_fetch_assoc($result)) {
405 $row['text'] = str_replace($search, $replace, $row['text']);
406 $row['context'] = str_replace($search, $replace, $row['context']);
408 $sql_dump .= "('$this->code', '$row[variable]', '$row[term]', '$row[text]', '$row[revised_date]', '$row[context]'),\r\n";
409 } while ($row = mysql_fetch_assoc($result));
411 $this->msg->addError('LANG_EMPTY');
413 $sql_dump = substr($sql_dump, 0, -3) . ";";
415 $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 .';
417 require(AT_INCLUDE_PATH . 'classes/zipfile.class.php');
418 $zipfile = new zipfile();
420 $zipfile->add_file($sql_dump, 'language_text.sql');
421 $zipfile->add_file($readme, 'readme.txt');
422 $zipfile->add_file($this->getXML(), 'language.xml');
425 $fp = fopen($filename, 'wb+');
426 fwrite($fp, $zipfile->get_file(), $zipfile->get_size());
428 $version = str_replace('.','_',VERSION);
430 $zipfile->send_file('atutor_' . $version . '_' . $this->code);