[ Index ] |
|
Code source de b2evolution 2.1.0-beta |
1 <?php 2 /** 3 * This file implements the UI controller for settings management. 4 * 5 * This file is part of the evoCore framework - {@link http://evocore.net/} 6 * See also {@link http://sourceforge.net/projects/evocms/}. 7 * 8 * @copyright (c)2003-2007 by Francois PLANQUE - {@link http://fplanque.net/} 9 * Parts of this file are copyright (c)2004-2006 by Daniel HAHLER - {@link http://thequod.de/contact}. 10 * 11 * {@internal License choice 12 * - If you have received this file as part of a package, please find the license.txt file in 13 * the same folder or the closest folder above for complete license terms. 14 * - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/) 15 * then you must choose one of the following licenses before using the file: 16 * - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php 17 * - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php 18 * }} 19 * 20 * {@internal Open Source relicensing agreement: 21 * Daniel HAHLER grants Francois PLANQUE the right to license 22 * Daniel HAHLER's contributions to this file and the b2evolution project 23 * under any OSI approved OSS license (http://www.opensource.org/licenses/). 24 * }} 25 * 26 * @package admin 27 * 28 * {@internal Below is a list of authors who have contributed to design/coding of this file: }} 29 * @author blueyed: Daniel HAHLER 30 * @author fplanque: Francois PLANQUE 31 * 32 * @version $Id: locales.ctrl.php,v 1.1 2007/06/25 11:00:40 fplanque Exp $ 33 */ 34 if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' ); 35 36 37 // Check minimum permission: 38 $current_User->check_perm( 'options', 'view', true ); 39 40 41 $AdminUI->set_path( 'options', 'regional' ); 42 43 param( 'action', 'string' ); 44 param( 'edit_locale', 'string' ); 45 param( 'loc_transinfo', 'integer', 0 ); 46 47 if( in_array( $action, array( 'update', 'reset', 'updatelocale', 'createlocale', 'deletelocale', 'extract', 'prioup', 'priodown' )) ) 48 { // We have an action to do.. 49 // Check permission: 50 $current_User->check_perm( 'options', 'edit', true ); 51 // clear settings cache 52 $cache_settings = ''; 53 54 switch( $action ) 55 { // switch between regional actions 56 // UPDATE regional settings 57 case 'update': 58 param( 'newdefault_locale', 'string', true); 59 $Settings->set( 'default_locale', $newdefault_locale ); 60 61 param( 'newtime_difference', 'string', '' ); 62 $newtime_difference = trim($newtime_difference); 63 if( $newtime_difference == '' ) 64 { 65 $newtime_difference = 0; 66 } 67 if( strpos($newtime_difference, ':') !== false ) 68 { // hh:mm:ss format: 69 $ntd = explode(':', $newtime_difference); 70 if( count($ntd) > 3 ) 71 { 72 param_error( 'newtime_difference', T_('Invalid time format.') ); 73 } 74 else 75 { 76 $newtime_difference = $ntd[0]*3600 + ($ntd[1]*60); 77 78 if( count($ntd) == 3 ) 79 { // add seconds: 80 $newtime_difference += $ntd[2]; 81 } 82 } 83 } 84 else 85 { // just hours: 86 $newtime_difference = $newtime_difference*3600; 87 } 88 89 $Settings->set( 'time_difference', $newtime_difference ); 90 91 if( ! $Messages->count('error') ) 92 { 93 locale_updateDB(); 94 $Settings->dbupdate(); 95 $Messages->add( T_('Regional settings updated.'), 'success' ); 96 } 97 break; 98 99 100 // CREATE/EDIT locale 101 case 'updatelocale': 102 case 'createlocale': 103 param( 'newloc_locale', 'string', true ); 104 param( 'newloc_enabled', 'integer', 0); 105 param( 'newloc_name', 'string', true); 106 param( 'newloc_charset', 'string', true); 107 param( 'newloc_datefmt', 'string', true); 108 param( 'newloc_timefmt', 'string', true); 109 param( 'newloc_startofweek', 'integer', true); 110 param( 'newloc_messages', 'string', true); 111 112 if( $action == 'updatelocale' ) 113 { 114 param( 'oldloc_locale', 'string', true); 115 116 $query = "SELECT loc_locale FROM T_locales WHERE loc_locale = '$oldloc_locale'"; 117 if( $DB->get_var($query) ) 118 { // old locale exists in DB 119 if( $oldloc_locale != $newloc_locale ) 120 { // locale key was renamed, we delete the old locale in DB and remember to create the new one 121 $q = $DB->query( 'DELETE FROM T_locales 122 WHERE loc_locale = "'.$oldloc_locale.'"' ); 123 if( $DB->rows_affected ) 124 { 125 $Messages->add( sprintf(T_('Deleted settings for locale «%s» in database.'), $oldloc_locale), 'success' ); 126 } 127 } 128 } 129 else 130 { // old locale is not in DB yet. Insert it. 131 $query = "INSERT INTO T_locales 132 ( loc_locale, loc_charset, loc_datefmt, loc_timefmt, loc_startofweek, loc_name, loc_messages, loc_priority, loc_enabled ) 133 VALUES ( '$oldloc_locale', 134 '{$locales[$oldloc_locale]['charset']}', '{$locales[$oldloc_locale]['datefmt']}', 135 '{$locales[$oldloc_locale]['timefmt']}', '{$locales[$oldloc_locale]['startofweek']}', 136 '{$locales[$oldloc_locale]['name']}', '{$locales[$oldloc_locale]['messages']}', 137 '{$locales[$oldloc_locale]['priority']}',"; 138 if( $oldloc_locale != $newloc_locale ) 139 { // disable old locale 140 $query .= ' 0)'; 141 $Messages->add( sprintf(T_('Inserted (and disabled) locale «%s» into database.'), $oldloc_locale), 'success' ); 142 } 143 else 144 { // keep old state 145 $query .= ' '.$locales[$oldloc_locale]['enabled'].')'; 146 $Messages->add( sprintf(T_('Inserted locale «%s» into database.'), $oldloc_locale), 'success' ); 147 } 148 $q = $DB->query($query); 149 } 150 } 151 152 $query = 'REPLACE INTO T_locales 153 ( loc_locale, loc_charset, loc_datefmt, loc_timefmt, loc_startofweek, loc_name, loc_messages, loc_priority, loc_enabled ) 154 VALUES ( '.$DB->quote($newloc_locale).', '.$DB->quote($newloc_charset).', '.$DB->quote($newloc_datefmt).', ' 155 .$DB->quote($newloc_timefmt).', '.$DB->quote($newloc_startofweek).', '.$DB->quote($newloc_name).', ' 156 .$DB->quote($newloc_messages).', "1", '.$DB->quote($newloc_enabled).' )'; 157 $q = $DB->query($query); 158 $Messages->add( sprintf(T_('Saved locale «%s».'), $newloc_locale), 'success' ); 159 160 // reload locales: an existing one could have been renamed (but we keep $evo_charset, which may have changed) 161 $old_evo_charset = $evo_charset; 162 unset( $locales ); 163 include $conf_path.'_locales.php'; 164 if( file_exists($conf_path.'_overrides_TEST.php') ) 165 { // also overwrite settings again: 166 include $conf_path.'_overrides_TEST.php'; 167 } 168 $evo_charset = $old_evo_charset; 169 170 break; 171 172 173 // RESET locales in DB 174 case 'reset': 175 // reload locales from files 176 unset( $locales ); 177 include $conf_path.'_locales.php'; 178 if( file_exists($conf_path.'_overrides_TEST.php') ) 179 { // also overwrite settings again: 180 include $conf_path.'_overrides_TEST.php'; 181 } 182 183 // delete everything from locales table 184 $q = $DB->query( 'DELETE FROM T_locales WHERE 1=1' ); 185 186 if( !isset( $locales[$current_locale] ) ) 187 { // activate default locale 188 locale_activate( $default_locale ); 189 } 190 191 // reset default_locale 192 $Settings->set( 'default_locale', $default_locale ); 193 $Settings->dbupdate(); 194 195 $Messages->add( T_('Locales table deleted, defaults from <code>/conf/_locales.php</code> loaded.'), 'success' ); 196 break; 197 198 199 // EXTRACT locale 200 case 'extract': 201 // Get PO file for that edit_locale: 202 $AdminUI->append_to_titlearea( 'Extracting language file for '.$edit_locale.'...' ); 203 204 $po_file = $locales_path.$locales[$edit_locale]['messages'].'/LC_MESSAGES/messages.po'; 205 if( ! is_file( $po_file ) ) 206 { 207 $Messages->add( sprintf(T_('File <code>%s</code> not found.'), '/'.$locales_subdir.$locales[$edit_locale]['messages'].'/LC_MESSAGES/messages.po'), 'error' ); 208 break; 209 } 210 211 $outfile = $locales_path.$locales[$edit_locale]['messages'].'/_global.php'; 212 if( !is_writable($outfile) ) 213 { 214 $Messages->add( sprintf( 'The file «%s» is not writable.', $outfile ) ); 215 break; 216 } 217 218 219 // File exists: 220 // Get PO file for that edit_locale: 221 $lines = file($po_file); 222 $lines[] = ''; // Adds a blank line at the end in order to ensure complete handling of the file 223 $all = 0; 224 $fuzzy=0; 225 $untranslated=0; 226 $translated=0; 227 $status='-'; 228 $matches = array(); 229 $sources = array(); 230 $loc_vars = array(); 231 $ttrans = array(); 232 foreach ($lines as $line) 233 { 234 // echo 'LINE:', $line, '<br />'; 235 if(trim($line) == '' ) 236 { // Blank line, go back to base status: 237 if( $status == 't' ) 238 { // ** End of a translation **: 239 if( $msgstr == '' ) 240 { 241 $untranslated++; 242 // echo 'untranslated: ', $msgid, '<br />'; 243 } 244 else 245 { 246 $translated++; 247 248 // Inspect where the string is used 249 $sources = array_unique( $sources ); 250 // echo '<p>sources: ', implode( ', ', $sources ), '</p>'; 251 foreach( $sources as $source ) 252 { 253 if( !isset( $loc_vars[$source] ) ) $loc_vars[$source] = 1; 254 else $loc_vars[$source] ++; 255 } 256 257 // Save the string 258 // $ttrans[] = "\n\t'".str_replace( "'", "\'", str_replace( '\"', '"', $msgid ))."' => '".str_replace( "'", "\'", str_replace( '\"', '"', $msgstr ))."',"; 259 // $ttrans[] = "\n\t\"$msgid\" => \"$msgstr\","; 260 $ttrans[] = "\n\t'".str_replace( "'", "\'", str_replace( '\"', '"', $msgid ))."' => \"".str_replace( '$', '\$', $msgstr)."\","; 261 262 } 263 } 264 $status = '-'; 265 $msgid = ''; 266 $msgstr = ''; 267 $sources = array(); 268 } 269 elseif( ($status=='-') && preg_match( '#^msgid "(.*)"#', $line, $matches)) 270 { // Encountered an original text 271 $status = 'o'; 272 $msgid = $matches[1]; 273 // echo 'original: "', $msgid, '"<br />'; 274 $all++; 275 } 276 elseif( ($status=='o') && preg_match( '#^msgstr "(.*)"#', $line, $matches)) 277 { // Encountered a translated text 278 $status = 't'; 279 $msgstr = $matches[1]; 280 // echo 'translated: "', $msgstr, '"<br />'; 281 } 282 elseif( preg_match( '#^"(.*)"#', $line, $matches)) 283 { // Encountered a followup line 284 if ($status=='o') 285 $msgid .= $matches[1]; 286 elseif ($status=='t') 287 $msgstr .= $matches[1]; 288 } 289 elseif( ($status=='-') && preg_match( '@^#:(.*)@', $line, $matches)) 290 { // Encountered a source code location comment 291 // echo $matches[0],'<br />'; 292 $sourcefiles = preg_replace( '@\\\\@', '/', $matches[1] ); 293 // $c = preg_match_all( '@ ../../../([^:]*):@', $sourcefiles, $matches); 294 $c = preg_match_all( '@ ../../../([^/:]*/?)@', $sourcefiles, $matches); 295 for( $i = 0; $i < $c; $i++ ) 296 { 297 $sources[] = $matches[1][$i]; 298 } 299 // echo '<br />'; 300 } 301 elseif(strpos($line,'#, fuzzy') === 0) 302 $fuzzy++; 303 } 304 305 if( $loc_vars ) 306 { 307 ksort( $loc_vars ); 308 309 $list_counts = ''; 310 foreach( $loc_vars as $source => $c ) 311 { 312 $list_counts .= "\n<li>$source = $c"; 313 } 314 $Messages->add( 'Sources and number of strings: <ul>'.$list_counts.'</ul>', 'note' ); 315 } 316 317 $fp = fopen( $outfile, 'w+' ); 318 fwrite( $fp, "<?php\n" ); 319 fwrite( $fp, "/*\n" ); 320 fwrite( $fp, " * Global lang file\n" ); 321 fwrite( $fp, " * This file was generated automatically from messages.po\n" ); 322 fwrite( $fp, " */\n" ); 323 fwrite( $fp, "if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );" ); 324 fwrite( $fp, "\n\n" ); 325 326 327 fwrite( $fp, "\n\$trans['".$locales[$edit_locale]['messages']."'] = array(" ); 328 // echo '<pre>'; 329 foreach( $ttrans as $line ) 330 { 331 // echo htmlspecialchars( $line ); 332 fwrite( $fp, $line ); 333 } 334 // echo '</pre>'; 335 fwrite( $fp, "\n);\n?>" ); 336 fclose( $fp ); 337 338 break; 339 340 case 'deletelocale': 341 // --- DELETE locale from DB 342 if( $DB->query( 'DELETE FROM T_locales WHERE loc_locale = "'.$DB->escape( $edit_locale ).'"' ) ) 343 { 344 $Messages->add( sprintf(T_('Deleted locale «%s» from database.'), $edit_locale), 'success' ); 345 } 346 347 // reload locales 348 unset( $locales ); 349 require $conf_path.'_locales.php'; 350 if( file_exists($conf_path.'_overrides_TEST.php') ) 351 { // also overwrite settings again: 352 include $conf_path.'_overrides_TEST.php'; 353 } 354 355 break; 356 357 // --- SWITCH PRIORITIES ----------------- 358 case 'prioup': 359 case 'priodown': 360 $switchcond = ''; 361 if( $action == 'prioup' ) 362 { 363 $switchcond = 'return ($lval[\'priority\'] > $i && $lval[\'priority\'] < $locales[ $edit_locale ][\'priority\']);'; 364 $i = -1; 365 } 366 elseif( $action == 'priodown' ) 367 { 368 $switchcond = 'return ($lval[\'priority\'] < $i && $lval[\'priority\'] > $locales[ $edit_locale ][\'priority\']);'; 369 $i = 256; 370 } 371 372 if( !empty($switchcond) ) 373 { // we want to switch priorities 374 375 foreach( $locales as $lkey => $lval ) 376 { // find nearest priority 377 if( eval($switchcond) ) 378 { 379 // remember it 380 $i = $lval['priority']; 381 $lswitchwith = $lkey; 382 } 383 } 384 if( $i > -1 && $i < 256 ) 385 { // switch 386 #echo 'Switching prio '.$locales[ $lswitchwith ]['priority'].' with '.$locales[ $lswitch ]['priority'].'<br />'; 387 $locales[ $lswitchwith ]['priority'] = $locales[ $edit_locale ]['priority']; 388 $locales[ $edit_locale ]['priority'] = $i; 389 390 $query = "REPLACE INTO T_locales ( loc_locale, loc_charset, loc_datefmt, loc_timefmt, loc_name, loc_messages, loc_priority, loc_enabled ) VALUES 391 ( '$edit_locale', '{$locales[ $edit_locale ]['charset']}', '{$locales[ $edit_locale ]['datefmt']}', '{$locales[ $edit_locale ]['timefmt']}', '{$locales[ $edit_locale ]['name']}', '{$locales[ $edit_locale ]['messages']}', '{$locales[ $edit_locale ]['priority']}', '{$locales[ $edit_locale ]['enabled']}'), 392 ( '$lswitchwith', '{$locales[ $lswitchwith ]['charset']}', '{$locales[ $lswitchwith ]['datefmt']}', '{$locales[ $lswitchwith ]['timefmt']}', '{$locales[ $lswitchwith ]['name']}', '{$locales[ $lswitchwith ]['messages']}', '{$locales[ $lswitchwith ]['priority']}', '{$locales[ $lswitchwith ]['enabled']}')"; 393 $q = $DB->query( $query ); 394 395 $Messages->add( T_('Switched priorities.'), 'success' ); 396 } 397 398 } 399 break; 400 } 401 locale_overwritefromDB(); 402 } 403 404 405 406 // Display <html><head>...</head> section! (Note: should be done early if actions do not redirect) 407 $AdminUI->disp_html_head(); 408 409 // Display title, menu, messages, etc. (Note: messages MUST be displayed AFTER the actions) 410 $AdminUI->disp_body_top(); 411 412 // Begin payload block: 413 $AdminUI->disp_payload_begin(); 414 415 // Display VIEW: 416 $AdminUI->disp_view( 'locales/_locale_settings.form.php' ); 417 418 // End payload block: 419 $AdminUI->disp_payload_end(); 420 421 // Display body bottom, debug info and close </html>: 422 $AdminUI->disp_global_footer(); 423 424 425 /* 426 * $Log: locales.ctrl.php,v $ 427 * Revision 1.1 2007/06/25 11:00:40 fplanque 428 * MODULES (refactored MVC) 429 * 430 * Revision 1.13 2007/04/26 00:11:14 fplanque 431 * (c) 2007 432 * 433 * Revision 1.12 2007/03/12 14:09:25 waltercruz 434 * Changing the WHERE 1 queries to boolean (WHERE 1=1) queries to satisfy the standarts 435 * 436 * Revision 1.11 2006/11/24 18:27:23 blueyed 437 * Fixed link to b2evo CVS browsing interface in file docblocks 438 * 439 * Revision 1.10 2006/11/15 00:35:14 blueyed 440 * Fix 441 */ 442 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Thu Nov 29 23:58:50 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |