AC_4897, AC_4898, AC_4899: Multifile uploader fixes.
[acontent.git] / include / classes / Language / LanguageEditor.class.php
1 <?php
2 /************************************************************************/
3 /* AContent                                                             */
4 /************************************************************************/
5 /* Copyright (c) 2010                                                   */
6 /* Inclusive Design Institute                                           */
7 /*                                                                      */
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 /************************************************************************/
12
13 /**
14 * LanguageEditor
15 * Class for adding/editing language.
16 * @access       public
17 * @author       Heidi Hazelton
18 * @author       Joel Kronenberg
19 * @package      Language
20 */
21 include_once(TR_INCLUDE_PATH.'classes/DAO/LanguagesDAO.class.php');
22 include_once(TR_INCLUDE_PATH.'classes/DAO/LanguageTextDAO.class.php');
23
24 class LanguageEditor extends Language {
25
26         var $addslashes;
27
28         // array of missing terms
29         var $missingTerms;
30
31         // array of filters ['new', 'update']
32         var $filters;
33         
34         /**
35         * Constructor.
36         * 
37         * Initializes db and parent properties.
38         */
39         function LanguageEditor($myLang) {
40                 global $db, $addslashes, $msg;
41                 
42                 global $savant;
43                 $this->msg =& $msg;
44
45                 $this->addslashes = $addslashes;
46
47                 if (isset($myLang)) {
48                         $this->Language($myLang);
49                 }
50                 $this->missingTerms = array();
51         }
52
53         // public
54         function updateTerm($variable, $term, $text) {
55                 $addslashes = $this->addslashes;
56
57                 $variable = $addslashes($variable);
58                 $term     = $addslashes($term);
59                 $text     = $addslashes($text);
60                 $code     = $addslashes($this->getCode());
61
62                 $sql    = "UPDATE ".TABLE_PREFIX."language_text SET text='$text', revised_date=NOW() WHERE language_code='$code' AND variable='$variable' AND term='$term'";
63
64                 /*
65                 if (mysql_query($sql, $this->db)) {
66                         return TRUE;
67                 } else {
68                         debug(mysql_error($this->db));
69                         return FALSE;
70                 }
71                 */
72         }
73
74         // public
75         function insertTerm($variable, $key, $text, $context) {
76                 $addslashes = $this->addslashes;
77
78                 $variable = $addslashes($variable);
79                 $key      = $addslashes($key);
80                 $text     = $addslashes($text);
81                 $code     = $addslashes($this->getCode());
82                 $context  = $addslashes($context);
83
84                 $sql = "INSERT INTO ".TABLE_PREFIX."language_text VALUES('$code', '$variable', '$key', '$text', NOW(), '$context')";
85         }
86
87         // public
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);
94
95                 echo '<div align="center"><iframe src="'.$_base_path.'admin/missing_language.php?terms='.$terms.SEP.'lang='.$_SESSION['lang'].'" width="99%" height="300"></div>';
96         }
97
98         // public
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)) {
103                         return;
104                 }
105
106                 foreach($params as $param => $garbage) {
107                         $this->filters[$param] = true;
108                 }
109         }
110
111         // private
112         function checkFilter($param) {
113                 if ($this->filters[$param]) {
114                         return true;
115                 }
116                 return false;
117         }
118
119         // public
120         function printTerms($terms){
121                 global $addslashes, $languageManager; // why won't $addslashes = $this->addslashes; work?
122
123                 $counter = 0;
124
125                 $terms = unserialize(stripslashes($addslashes($terms)));
126
127                 natcasesort($terms);
128
129                 if ($this->checkFilter('new')) {
130                         $new_check = ' checked="checked"';
131                 }
132                 if ($this->checkFilter('update')) {
133                         $update_check = ' checked="checked"';
134                 }
135
136                 $fromLanguage =& $languageManager->getLanguage(DEFAULT_LANGUAGE_CODE);
137
138                 echo '<form method="post" action="'.htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES).'">';
139                 echo '<table border="0" cellpadding="0" cellspacing="2">';
140                 echo '<tr>';
141                 echo '<td>Show: ';
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> ';
144                 echo '</td>';
145                 echo '</tr>';
146
147                 foreach($terms as $term => $garbage) {
148                         $to_term   = $this->getTerm($term);
149                         $from_term = $fromLanguage->getTerm($term);
150
151                         $is_new = false;
152                         if ($to_term === false) {
153                                 $is_new = true;
154                         }
155
156                         $is_old = false;
157                         if ($to_term['revised_date_unix'] < $from_term['revised_date_unix']) {
158                                 $is_old = true;
159                         }
160
161
162                         if ($this->checkFilter('new') && !$is_new) {
163                                 continue;
164                         }
165
166                         if ($this->checkFilter('update') && !$is_old) {
167                                 continue;
168                         }
169
170                         if (($counter % 10) == 0) {
171                                 echo '<tr>';
172                                 echo '<td align="center"><input type="submit" name="submit" value="Save Changes" class="button" /></td>';
173                                 echo '</tr>';
174                         }
175
176                         $style = '';
177                         if ($is_new) {
178                                 $style = 'style="background-color: white; border: red 2px solid;"';
179                         } else {
180                                 $style = 'style="background-color: white; border: yellow 1px solid;"';
181                         }
182
183                         echo '<tr>';
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>';
187                         echo '</tr>';
188
189                         $counter++;
190                 }
191                 echo '</table>';
192                 echo '</form>';
193         }
194
195         // public
196         function updateTerms($terms) {
197                 global $addslashes;
198
199                 foreach($terms as $term => $text) {
200                         $text = $addslashes($text);
201                         $term = $addslashes($term);
202                 
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);
206                         }
207                 }
208         }
209
210         // public
211         function addMissingTerm($term) {
212                 if (!isset($this->missingTerms[$term])) {
213                         $this->missingTerms[$term] = '';
214                 }
215         }
216
217
218         // this method should be called staticly: LanguageEditor::import()
219         // public
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
224
225                 // use the sql class to insert the language into the db
226
227                 // check if this language exists before calling this method
228
229                 require_once(TR_INCLUDE_PATH . 'classes/sqlutility.class.php');
230                 $sqlUtility = new SqlUtility();
231
232                 $sqlUtility->queryFromFile($language_sql_file, TABLE_PREFIX);
233         }
234
235         // sends the generated language pack to the browser
236         // public
237         function export($filename = '') {
238 //              $search  = array('"', "'", "\x00", "\x0a", "\x0d", "\x1a"); //\x08\\x09, not required
239 //              $replace = array('\"', "\'", '\0', '\n', '\r', '\Z');
240
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
244                 global $addslashes;
245                 
246                 $sql_dump = "INSERT INTO `languages` VALUES ('$this->code', '$this->characterSet', '$this->regularExpression', '$this->nativeName', '$this->englishName', $this->status);\r\n\r\n";
247
248                 $sql_dump .= "INSERT INTO `language_text` VALUES ";
249
250                 $languageTextDAO = new LanguageTextDAO();
251                 $rows = $languageTextDAO->getAllByLang($this->code);
252
253                 if (is_array($rows)) {
254                         foreach ($rows as $row)
255                         {
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']);
260                                 
261                                 $sql_dump .= "('$this->code', '$row[variable]', '$row[term]', '$row[text]', '$row[revised_date]', '$row[context]'),\r\n";
262                         }
263                 } else {
264                         $this->msg->addError('LANG_EMPTY');
265                         return;
266                 }
267                 $sql_dump = substr($sql_dump, 0, -3) . ";";
268
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 .';
270
271                 require(TR_INCLUDE_PATH . 'classes/zipfile.class.php');
272                 $zipfile = new zipfile();
273
274                 $zipfile->add_file($sql_dump, 'language_text.sql');
275                 $zipfile->add_file($readme, 'readme.txt');
276                 $zipfile->add_file($this->getXML(), 'language.xml');  
277
278                 if ($filename) {
279                         $fp = fopen($filename, 'wb+');
280                         fwrite($fp, $zipfile->get_file(), $zipfile->get_size());
281                 } else {
282                         $version = str_replace('.','_',VERSION);
283
284                         $zipfile->send_file('AContent_' . $version . '_' . $this->code);
285                 }
286         }
287
288 }
289 ?>