2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 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 /************************************************************************/
15 * Class for adding/editing language.
17 * @author Heidi Hazelton
18 * @author Joel Kronenberg
21 include_once(TR_INCLUDE_PATH.'classes/DAO/LanguagesDAO.class.php');
22 include_once(TR_INCLUDE_PATH.'classes/DAO/LanguageTextDAO.class.php');
24 class LanguageEditor extends Language {
28 // array of missing terms
31 // array of filters ['new', 'update']
37 * Initializes db and parent properties.
39 function LanguageEditor($myLang) {
40 global $db, $addslashes, $msg;
45 $this->addslashes = $addslashes;
48 $this->Language($myLang);
50 $this->missingTerms = array();
54 function updateTerm($variable, $term, $text) {
55 $addslashes = $this->addslashes;
57 $variable = $addslashes($variable);
58 $term = $addslashes($term);
59 $text = $addslashes($text);
60 $code = $addslashes($this->getCode());
62 $sql = "UPDATE ".TABLE_PREFIX."language_text SET text='$text', revised_date=NOW() WHERE language_code='$code' AND variable='$variable' AND term='$term'";
65 if (mysql_query($sql, $this->db)) {
68 debug(mysql_error($this->db));
75 function insertTerm($variable, $key, $text, $context) {
76 $addslashes = $this->addslashes;
78 $variable = $addslashes($variable);
79 $key = $addslashes($key);
80 $text = $addslashes($text);
81 $code = $addslashes($this->getCode());
82 $context = $addslashes($context);
84 $sql = "INSERT INTO ".TABLE_PREFIX."language_text VALUES('$code', '$variable', '$key', '$text', NOW(), '$context')";
88 function showMissingTermsFrame(){
89 global $_base_path, $addslashes;
90 //$terms = array_slice($this->missingTerms, 0, 20);
91 $terms = $this->missingTerms;
92 $terms = serialize($terms);
93 $terms = urlencode($terms);
95 echo '<div align="center"><iframe src="'.$_base_path.'admin/missing_language.php?terms='.$terms.SEP.'lang='.$_SESSION['lang'].'" width="99%" height="300"></div>';
99 // doesn't actually check if params is one of the possible ones.
100 // possible params should be array ('new', 'update')
101 function setFilter($params){
102 if (!is_array($params)) {
106 foreach($params as $param => $garbage) {
107 $this->filters[$param] = true;
112 function checkFilter($param) {
113 if ($this->filters[$param]) {
120 function printTerms($terms){
121 global $addslashes, $languageManager; // why won't $addslashes = $this->addslashes; work?
125 $terms = unserialize(stripslashes($addslashes($terms)));
129 if ($this->checkFilter('new')) {
130 $new_check = ' checked="checked"';
132 if ($this->checkFilter('update')) {
133 $update_check = ' checked="checked"';
136 $fromLanguage =& $languageManager->getLanguage(DEFAULT_LANGUAGE_CODE);
138 echo '<form method="post" action="'.htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES).'">';
139 echo '<table border="0" cellpadding="0" cellspacing="2">';
142 echo '<input name="filter_new" id="n" value="1" type="checkbox" '.$new_check.' /><label for="n">New Language</label>, ';
143 echo '<input name="filter_update" id="u" value="1" type="checkbox" '.$update_check.' /><label for="u">Updated Language</label> ';
147 foreach($terms as $term => $garbage) {
148 $to_term = $this->getTerm($term);
149 $from_term = $fromLanguage->getTerm($term);
152 if ($to_term === false) {
157 if ($to_term['revised_date_unix'] < $from_term['revised_date_unix']) {
162 if ($this->checkFilter('new') && !$is_new) {
166 if ($this->checkFilter('update') && !$is_old) {
170 if (($counter % 10) == 0) {
172 echo '<td align="center"><input type="submit" name="submit" value="Save Changes" class="button" /></td>';
178 $style = 'style="background-color: white; border: red 2px solid;"';
180 $style = 'style="background-color: white; border: yellow 1px solid;"';
184 echo '<td><strong>[ ' . $term . ' ] '.htmlspecialchars($from_term['text']).'</strong></td></tr>';
185 echo '<tr><td><input type="text" name="'.$term.'" '.$style.' size="100" value="'.htmlspecialchars($to_term['text']).'" />';
186 echo '<input type="hidden" name="old['.$term.']" '.$style.' size="100" value="'.htmlspecialchars($to_term['text']).'" /></td>';
196 function updateTerms($terms) {
199 foreach($terms as $term => $text) {
200 $text = $addslashes($text);
201 $term = $addslashes($term);
203 if (($text != '') && ($text != $_POST['old'][$term])) {
204 $sql = "REPLACE INTO ".TABLE_PREFIX."language_text VALUES ('".$this->getCode()."', '_template', '$term', '$text', NOW(), '')";
205 mysql_query($sql, $this->db);
211 function addMissingTerm($term) {
212 if (!isset($this->missingTerms[$term])) {
213 $this->missingTerms[$term] = '';
218 // this method should be called staticly: LanguageEditor::import()
220 function import($language_sql_file) {
221 // move sql import class from install/ to include/classes/
222 // store the lang def'n in a .ini file and use insertLang
223 // after checking if it already exists
225 // use the sql class to insert the language into the db
227 // check if this language exists before calling this method
229 require_once(TR_INCLUDE_PATH . 'classes/sqlutility.class.php');
230 $sqlUtility = new SqlUtility();
232 $sqlUtility->queryFromFile($language_sql_file, TABLE_PREFIX);
235 // sends the generated language pack to the browser
237 function export($filename = '') {
238 // $search = array('"', "'", "\x00", "\x0a", "\x0d", "\x1a"); //\x08\\x09, not required
239 // $replace = array('\"', "\'", '\0', '\n', '\r', '\Z');
241 // use a function to generate the ini file
242 // use a diff fn to generate the sql dump
243 // use the zipfile class to package the ini file and the sql dump
246 $sql_dump = "INSERT INTO `languages` VALUES ('$this->code', '$this->characterSet', '$this->regularExpression', '$this->nativeName', '$this->englishName', $this->status);\r\n\r\n";
248 $sql_dump .= "INSERT INTO `language_text` VALUES ";
250 $languageTextDAO = new LanguageTextDAO();
251 $rows = $languageTextDAO->getAllByLang($this->code);
253 if (is_array($rows)) {
254 foreach ($rows as $row)
256 // $row['text'] = str_replace($search, $replace, $row['text']);
257 // $row['context'] = str_replace($search, $replace, $row['context']);
258 $row['text'] = $addslashes($row['text']);
259 $row['context'] = $addslashes($row['context']);
261 $sql_dump .= "('$this->code', '$row[variable]', '$row[term]', '$row[text]', '$row[revised_date]', '$row[context]'),\r\n";
264 $this->msg->addError('LANG_EMPTY');
267 $sql_dump = substr($sql_dump, 0, -3) . ";";
269 $readme = 'This is an AContent 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 AContent table prefix as defined in ./include/config.inc.php .';
271 require(TR_INCLUDE_PATH . 'classes/zipfile.class.php');
272 $zipfile = new zipfile();
274 $zipfile->add_file($sql_dump, 'language_text.sql');
275 $zipfile->add_file($readme, 'readme.txt');
276 $zipfile->add_file($this->getXML(), 'language.xml');
279 $fp = fopen($filename, 'wb+');
280 fwrite($fp, $zipfile->get_file(), $zipfile->get_size());
282 $version = str_replace('.','_',VERSION);
284 $zipfile->send_file('AContent_' . $version . '_' . $this->code);