phpDocumentor pond
[ class tree: pond ] [ index: pond ] [ all elements ]

Source for file locales.ctrl.php

Documentation is available at locales.ctrl.php

  1. <?php
  2. /**
  3.  * This file implements the UI controller for settings management.
  4.  *
  5.  * This file is part of Quam Plures - {@link http://quamplures.net/}
  6.  * See also {@link https://launchpad.net/quam-plures}.
  7.  *
  8.  * @copyright (c) 2009 - 2011 by the Quam Plures developers - {@link http://quamplures.net/}
  9.  * @copyright (c)2003-2009 by Francois PLANQUE - {@link http://fplanque.net/}
  10.  *  Parts of this file are copyright (c)2004-2006 by Daniel HAHLER - {@link http://thequod.de/contact}.
  11.  *
  12.  *  {@internal License choice
  13.  *  - If you have received this file as part of a package, please find the license.txt file in
  14.  *    the same folder or the closest folder above for complete license terms.
  15.  *  - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/)
  16.  *    then you must choose one of the following licenses before using the file:
  17.  *    - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
  18.  *    - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php
  19.  *  }}}
  20.  *
  21.  *  {@internal Open Source relicensing agreement:
  22.  *  Daniel HAHLER grants Francois PLANQUE the right to license
  23.  *  Daniel HAHLER's contributions to this file and the b2evolution project
  24.  *  under any OSI approved OSS license (http://www.opensource.org/licenses/).
  25.  *  }}}
  26.  *
  27.  *  {@internal Below is a list of authors who have contributed to design/coding of this file: }}
  28.  * @author blueyed: Daniel HAHLER
  29.  * @author fplanque: Francois PLANQUE
  30.  *
  31.  * @package pond
  32.  */
  33. if!defined('QP_MAIN_INIT') ) die'Please, do not access this page directly.' );
  34.  
  35.  
  36. // Check minimum permission:
  37. $current_User->check_perm'options''view'true );
  38.  
  39.  
  40. $AdminUI->set_path'options''regional' );
  41.  
  42. param'action''string' );
  43. param'edit_locale''string' );
  44. param'loc_transinfo''integer');
  45.  
  46.  
  47. // Load all available locale defintions:
  48.  
  49.  
  50. ifin_array$actionarray'update''reset''updatelocale''createlocale''deletelocale''extract''prioup''priodown' )) )
  51. // We have an action to do..
  52.     // Check permission:
  53.     $current_User->check_perm'options''edit'true );
  54.     // clear settings cache
  55.     $cache_settings '';
  56.  
  57.     switch$action )
  58.     // switch between regional actions
  59.         // UPDATE regional settings
  60.         case 'update':
  61.             param'newdefault_locale''string'true);
  62.             $Settings->set'default_locale'$newdefault_locale );
  63.  
  64.             param'newtime_difference''string''' );
  65.             $newtime_difference trim($newtime_difference);
  66.             if$newtime_difference == '' )
  67.             {
  68.                 $newtime_difference 0;
  69.             }
  70.             ifstrpos($newtime_difference':'!== false )
  71.             // hh:mm:ss format:
  72.                 $ntd explode(':'$newtime_difference);
  73.                 ifcount($ntd)
  74.                 {
  75.                     param_error'newtime_difference'T_('Invalid time format.') );
  76.                 }
  77.                 else
  78.                 {
  79.                     $newtime_difference $ntd[0]*3600 ($ntd[1]*60);
  80.  
  81.                     ifcount($ntd== )
  82.                     // add seconds:
  83.                         $newtime_difference += $ntd[2];
  84.                     }
  85.                 }
  86.             }
  87.             else
  88.             // just hours:
  89.                 $newtime_difference $newtime_difference*3600;
  90.             }
  91.  
  92.             $Settings->set'time_difference'$newtime_difference );
  93.  
  94.             if$Messages->count('error') )
  95.             {
  96.                 locale_updateDB();
  97.                 $Settings->dbupdate();
  98.                 $Messages->addT_('Regional settings updated.')'success' );
  99.             }
  100.             break;
  101.  
  102.  
  103.         // CREATE/EDIT locale
  104.         case 'updatelocale':
  105.         case 'createlocale':
  106.             param'newloc_locale''string'true );
  107.             param'newloc_enabled''integer'0);
  108.             param'newloc_name''string'true);
  109.             param'newloc_charset''string'true);
  110.             param'newloc_datefmt''string'true);
  111.             param'newloc_timefmt''string'true);
  112.             param'newloc_startofweek''integer'true);
  113.             param'newloc_priority''integer'1);
  114.             param'newloc_messages''string'true);
  115.  
  116.             if$action == 'updatelocale' )
  117.             {
  118.                 param'oldloc_locale''string'true);
  119.  
  120.                 $query "SELECT loc_locale FROM T_locales WHERE loc_locale = '$oldloc_locale'";
  121.                 if$DB->get_var($query) )
  122.                 // old locale exists in DB
  123.                     if$oldloc_locale != $newloc_locale )
  124.                     // locale key was renamed, we delete the old locale in DB and remember to create the new one
  125.                         $q $DB->query'DELETE FROM T_locales
  126.                                                                 WHERE loc_locale = "'.$oldloc_locale.'"' );
  127.                         if$DB->rows_affected )
  128.                         {
  129.                             $Messages->addsprintf(T_('Deleted settings for locale &laquo;%s&raquo; in database.')$oldloc_locale)'success' );
  130.                         }
  131.                     }
  132.                 }
  133.                 else
  134.                 // old locale is not in DB yet. Insert it.
  135.                     $query "INSERT INTO T_locales
  136.                                         ( loc_locale, loc_charset, loc_datefmt, loc_timefmt, loc_startofweek, loc_name, loc_messages, loc_priority, loc_enabled )
  137.                                         VALUES ( '$oldloc_locale',
  138.                                         '{$locales[$oldloc_locale]['charset']}', '{$locales[$oldloc_locale]['datefmt']}',
  139.                                         '{$locales[$oldloc_locale]['timefmt']}', '{$locales[$oldloc_locale]['startofweek']}',
  140.                                         '{$locales[$oldloc_locale]['name']}', '{$locales[$oldloc_locale]['messages']}',
  141.                                         '{$locales[$oldloc_locale]['priority']}',";
  142.                     if$oldloc_locale != $newloc_locale )
  143.                     // disable old locale
  144.                         $query .= ' 0)';
  145.                         $Messages->addsprintf(T_('Inserted (and disabled) locale &laquo;%s&raquo; into database.')$oldloc_locale)'success' );
  146.                     }
  147.                     else
  148.                     // keep old state
  149.                         $query .= ' '.$locales[$oldloc_locale]['enabled'].')';
  150.                         $Messages->addsprintf(T_('Inserted locale &laquo;%s&raquo; into database.')$oldloc_locale)'success' );
  151.                     }
  152.                     $q $DB->query($query);
  153.                 }
  154.             }
  155.  
  156.             $query 'REPLACE INTO T_locales
  157.                                 ( loc_locale, loc_charset, loc_datefmt, loc_timefmt, loc_startofweek, loc_name, loc_messages, loc_priority, loc_enabled )
  158.                                 VALUES ( '.$DB->quote($newloc_locale).', '.$DB->quote($newloc_charset).', '.$DB->quote($newloc_datefmt).', '
  159.                                     .$DB->quote($newloc_timefmt).', '.$DB->quote($newloc_startofweek).', '.$DB->quote($newloc_name).', '
  160.                                     .$DB->quote($newloc_messages).', '.$DB->quote($newloc_priority).', '.$DB->quote($newloc_enabled).' )';
  161.             $q $DB->query($query);
  162.             $Messages->addsprintf(T_('Saved locale &laquo;%s&raquo;.')$newloc_locale)'success' );
  163.  
  164.             // reload locales: an existing one could have been renamed (but we keep $evo_charset, which may have changed)
  165.             $old_evo_charset $evo_charset;
  166.             unset$locales );
  167. /**
  168.  * dummy docblock makes error-free autodocs
  169.  */
  170.             include $conf_path.'_locales.php';
  171.             iffile_exists($conf_path.'_overrides_TEST.php') )
  172.             // also overwrite settings again:
  173.                 include $conf_path.'_overrides_TEST.php';
  174.             }
  175.             $evo_charset $old_evo_charset;
  176.  
  177.             // Load all available locale defintions:
  178.             locales_load_available_defs();
  179.  
  180.             break;
  181.  
  182.  
  183.         // RESET locales in DB
  184.         case 'reset':
  185.             // reload locales from files
  186.             unset$locales );
  187.             include $conf_path.'_locales.php';
  188.             iffile_exists($conf_path.'_overrides_TEST.php') )
  189.             // also overwrite settings again:
  190.                 include $conf_path.'_overrides_TEST.php';
  191.             }
  192.  
  193.             // delete everything from locales table
  194.             $q $DB->query'DELETE FROM T_locales WHERE 1=1' );
  195.  
  196.             if!isset$locales[$current_locale) )
  197.             // activate default locale
  198.                 locale_activate$default_locale );
  199.             }
  200.  
  201.             // reset default_locale
  202.             $Settings->set'default_locale'$default_locale );
  203.             $Settings->dbupdate();
  204.  
  205.             // Load all available locale defintions:
  206.             locales_load_available_defs();
  207.  
  208.             $Messages->addT_('Locale definitions reset to defaults. (<code>/qp_config/_locales.php</code>)')'success' );
  209.             break;
  210.  
  211.  
  212.         // EXTRACT locale
  213.         case 'extract':
  214.             // Get PO file for that edit_locale:
  215.             $AdminUI->append_to_titlearea'Extracting language file for '.$edit_locale.'...' );
  216.  
  217.             $po_file $locales_path.$locales[$edit_locale]['messages'].'.po';
  218.             ifis_file$po_file ) )
  219.             {
  220.                 $Messages->addsprintfT_('File <code>%s</code> not found.')rel_path_to_base$po_file ) )'error' );
  221.                 break;
  222.             }
  223.  
  224.             $outfile $locales_path.'php/'.$locales[$edit_locale]['messages'].'/_global.php';
  225.             if!is_writable($outfile) )
  226.             {
  227.                 $Messages->addsprintf'The file &laquo;%s&raquo; is not writable.'rel_path_to_base$outfile ) ) );
  228.                 break;
  229.             }
  230.  
  231.  
  232.             load_class'locales/_pofile.class.php' );
  233.             $POFile new POFile($po_file);
  234.             $POFile->read(true)// adds info about sources to $Messages
  235.             $POFile->write_evo_trans($outfile$locales[$edit_locale]['messages']);
  236.             break;
  237.  
  238.         case 'deletelocale':
  239.             // --- DELETE locale from DB
  240.             if$DB->query'DELETE FROM T_locales WHERE loc_locale = "'.$DB->escape$edit_locale ).'"' ) )
  241.             {
  242.                 $Messages->addsprintf(T_('Deleted locale &laquo;%s&raquo; from database.')$edit_locale)'success' );
  243.             }
  244.  
  245.             // reload locales
  246.             unset$locales );
  247.             require $conf_path.'_locales.php';
  248.             iffile_exists($conf_path.'_overrides_TEST.php') )
  249.             // also overwrite settings again:
  250.                 include $conf_path.'_overrides_TEST.php';
  251.             }
  252.  
  253.             // Load all available locale defintions:
  254.             locales_load_available_defs();
  255.  
  256.             break;
  257.  
  258.         // --- SWITCH PRIORITIES -----------------
  259.         case 'prioup':
  260.         case 'priodown':
  261.             $switchcond '';
  262.             if$action == 'prioup' )
  263.             {
  264.                 $switchcond 'return ($lval[\'priority\'] > $i && $lval[\'priority\'] < $locales[ $edit_locale ][\'priority\']);';
  265.                 $i = -1;
  266.             }
  267.             elseif$action == 'priodown' )
  268.             {
  269.                 $switchcond 'return ($lval[\'priority\'] < $i && $lval[\'priority\'] > $locales[ $edit_locale ][\'priority\']);';
  270.                 $i 256;
  271.             }
  272.  
  273.             if!empty($switchcond) )
  274.             // we want to switch priorities
  275.  
  276.                 foreach$locales as $lkey => $lval )
  277.                 // find nearest priority
  278.                     ifeval($switchcond) )
  279.                     {
  280.                         // remember it
  281.                         $i $lval['priority'];
  282.                         $lswitchwith $lkey;
  283.                     }
  284.                 }
  285.                 if$i > -&& $i 256 )
  286.                 // switch
  287.                     #echo 'Switching prio '.$locales[ $lswitchwith ]['priority'].' with '.$locales[ $lswitch ]['priority'].'<br />';
  288.                     $locales$lswitchwith ]['priority'$locales$edit_locale ]['priority'];
  289.                     $locales$edit_locale ]['priority'$i;
  290.  
  291.                     $query "REPLACE INTO T_locales ( loc_locale, loc_charset, loc_datefmt, loc_timefmt, loc_name, loc_messages, loc_priority, loc_enabled )    VALUES
  292.                         ( '$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']}'),
  293.                         ( '$lswitchwith', '{$locales[ $lswitchwith ]['charset']}', '{$locales[ $lswitchwith ]['datefmt']}', '{$locales[ $lswitchwith ]['timefmt']}', '{$locales[ $lswitchwith ]['name']}', '{$locales[ $lswitchwith ]['messages']}', '{$locales[ $lswitchwith ]['priority']}', '{$locales[ $lswitchwith ]['enabled']}')";
  294.                     $q $DB->query$query );
  295.  
  296.                     $Messages->addT_('Switched priorities.')'success' );
  297.                 }
  298.  
  299.             }
  300.             break;
  301.     }
  302. }
  303.  
  304.  
  305.  
  306. // Display <html><head>...</head> section! (Note: should be done early if actions do not redirect)
  307. $AdminUI->disp_html_head();
  308.  
  309. // Display title, menu, messages, etc. (Note: messages MUST be displayed AFTER the actions)
  310. $AdminUI->disp_body_top();
  311.  
  312. // Begin payload block:
  313. $AdminUI->disp_payload_begin();
  314.  
  315. // Display VIEW:
  316. $AdminUI->disp_view'locales/_locale_settings.form.php' );
  317.  
  318. // End payload block:
  319. $AdminUI->disp_payload_end();
  320.  
  321. // Display body bottom, debug info and close </html>:
  322. $AdminUI->disp_global_footer();
  323.  
  324.  
  325. ?>