source: trunk/plugins/language_switch/language_switch.inc.php @ 3376

Last change on this file since 3376 was 3376, checked in by vdigital, 15 years ago

Merge: 3375
+ Language Switch code review (No cookies => session).
+ Optional $confno_flag_languages = Array() to remove flags

  • Property svn:eol-style set to LF
File size: 4.7 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2009 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23if (!defined('LANGUAGE_SWITCH_PATH'))
24define('LANGUAGE_SWITCH_PATH' , PHPWG_PLUGINS_PATH . basename(dirname(__FILE__)) . '/');
25class language_controler {
26  static public function _switch()
27  {
28    global $user;
29    if (!defined('PHPWG_ROOT_PATH')) { die('Hacking attempt!'); }
30    $same = $user['language'];
31    if ( isset( $_GET['lang']) ) {
32      if ( !empty($_GET['lang'] ) and file_exists( PHPWG_ROOT_PATH.'language/'
33        . $_GET['lang'].'/common.lang.php') ) {
34        if (is_a_guest() or is_generic()) {
35          pwg_set_session_var( 'lang_switch', $_GET['lang'] );
36        } else {
37          $query = 'UPDATE '.USER_INFOS_TABLE.'
38          SET language = \''.$_GET['lang'].'\'
39          WHERE user_id = '.$user['id'].'
40          ;';
41          pwg_query($query);
42        }
43        $user['language'] = $_GET['lang'];
44      }
45    }
46    elseif ((is_a_guest() or is_generic())) {
47      $user['language'] = pwg_get_session_var( 'lang_switch', $user['language'] );
48    }
49  // Reload language only if it isn't the same one
50    if ( $same !== $user['language']) {
51      load_language('common.lang', '', array('language'=>$user['language']) );
52      load_language('local.lang', '', array('language'=>$user['language'], 'no_fallback'=>true) );
53      if (defined('IN_ADMIN') and IN_ADMIN) { // Never currently
54        load_language('admin.lang', '', array('language'=>$user['language']) );
55      }
56    }
57  }
58  static public function _flags()
59  {
60    global $user, $template, $conf;
61    $available_lang = get_languages();
62    if ( isset($conf['no_flag_languages']) ) 
63      $available_lang = array_diff_key($available_lang, array_flip($conf['no_flag_languages']));
64    $url_starting = $_SERVER['REQUEST_URI'];
65    if ( isset( $_GET['lang']) ) {
66      $pos = stripos  ( $url_starting  , '&lang=' );
67      if (is_numeric($pos) and $pos > 0) $url_starting = substr($url_starting, 0, $pos);
68    }
69    $pos = stripos($url_starting, script_basename());
70    if (is_numeric($pos)) $url_starting = substr($url_starting, $pos);
71    foreach ( $available_lang as $code => $displayname ) {
72      $qlc = array ( 
73        'url' => add_url_params( $url_starting, array('lang'=> $code) ),
74        'alt' => ucwords( $displayname ),
75        'img' => 'plugins/language_switch/icons/' . $code . '.jpg',
76        );
77      if ( $code !== $user['language'] and file_exists(PHPWG_ROOT_PATH.$qlc['img']) ) 
78        $lsw['flags'][$code] = $qlc ;
79      else $lsw['Active'] = $qlc;
80    }
81    $template->set_filename('language_flags', dirname(__FILE__) . '/flags.tpl');
82    $lsw['side'] = ceil(sqrt(count($available_lang)));
83    $template->assign(array(
84      'lang_switch'=> $lsw,
85      'LANGUAGE_SWITCH_PATH' => LANGUAGE_SWITCH_PATH,
86    ));
87    $flags = $template->parse('language_flags',true);
88    $template->clear_assign('lang_switch');
89    $template->concat( 'PLUGIN_INDEX_ACTIONS', $flags);
90    // TODO : Try to cache $flags and $user['language'] in $_SESSION for performance
91  }
92}
93  /* {html_head} usage function */
94  /* See flags.tpl for example (due no catenation available) */
95if (!function_exists('Componant_exists')) {
96  function Componant_exists($path, $file)
97    { return file_exists( $path . $file); }
98}
99
100?>
Note: See TracBrowser for help on using the repository browser.