source: extensions/language_switch_menubar/main.inc.php @ 31454

Last change on this file since 31454 was 31454, checked in by ddtddt, 8 years ago

[extensions] - language_switch_menubar - 2.8

File size: 4.8 KB
Line 
1<?php
2/*
3Plugin Name: Language Switch Menubar
4Version: auto
5Description: Switch to another language from flags on your menubar gallery.
6Plugin URI: http://piwigo.org/ext/extension_view.php?eid=601
7Author: ddtddt
8Author URI: http://temmii.com/piwigo/
9*/
10
11// +-----------------------------------------------------------------------+
12// | Ban IP plugin for piwigo                                              |
13// +-----------------------------------------------------------------------+
14// | Copyright(C) 2016 ddtddt                    http://temmii.com/piwigo/ |
15// +-----------------------------------------------------------------------+
16// | This program is free software; you can redistribute it and/or modify  |
17// | it under the terms of the GNU General Public License as published by  |
18// | the Free Software Foundation                                          |
19// |                                                                       |
20// | This program is distributed in the hope that it will be useful, but   |
21// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
22// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
23// | General Public License for more details.                              |
24// |                                                                       |
25// | You should have received a copy of the GNU General Public License     |
26// | along with this program; if not, write to the Free Software           |
27// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
28// | USA.                                                                  |
29// +-----------------------------------------------------------------------+
30
31if (!defined('PHPWG_ROOT_PATH'))
32{
33  die('Hacking attempt!');
34}
35
36define('LSM_DIR' , basename(dirname(__FILE__)));
37define('LSM_PATH' , PHPWG_PLUGINS_PATH . LSM_DIR . '/');
38
39add_event_handler('blockmanager_register_blocks', 'register_lsm_menubar_blocks');
40add_event_handler('blockmanager_apply', 'lsm_apply');
41add_event_handler('loading_lang', 'reloadlang');
42
43function register_lsm_menubar_blocks( $menu_ref_arr )
44{
45  $menu = & $menu_ref_arr[0];
46  if ($menu->get_id() != 'menubar')
47    return;
48  $menu->register_block( new RegisteredBlock( 'mbLSM', 'lsm', 'LSM'));
49}
50
51function lsm_apply($menu_ref_arr)
52{
53  global $template, $user, $template, $conf, $page;
54
55  $menu = & $menu_ref_arr[0];
56 
57  load_language('plugin.lang', LSM_PATH);
58        $template->assign(
59    array       (
60      'LSMTITLE' => l10n('Language'),
61      )
62    );
63 
64  if (($block = $menu->get_block( 'mbLSM' )) != null)
65  {
66    $template->set_template_dir(LSM_PATH.'template/');
67    $block->template = 'flags.tpl';
68  }
69 
70  if (isset($page['section']) and $page['section'] == 'additional_page' and isset($page['additional_page']))
71  {
72    $base_url = make_index_url(array('section'=>'page')).'/'.(isset($page['additional_page']['permalink']) ? $page['additional_page']['permalink'] : $page['additional_page']['id']);
73  }
74  else
75  {
76    $base_url = duplicate_index_url();
77  }
78 
79  $available_lang = get_languages();
80  foreach ($available_lang as $code => $displayname)
81  {
82        $code2=explode("_", $code);
83    $qlc = array (
84      'url' => add_url_params($base_url, array('lang'=> $code)),
85      'alt' => ucwords($displayname),
86          'hreflang' =>$code2[0],
87      'img' => get_root_url().'language/'.$code.'/'.$code.'.jpg',
88      );
89   
90    $lsw['flags'][$code] = $qlc ;
91   
92    if ($code == $user['language'])
93    {
94      $lsw['Active'] = $qlc;
95    }
96  }
97
98  $lsw['side'] = ceil(sqrt(count($available_lang)));
99  $template->clear_assign('lang_switch');   
100  $template->assign(
101    array(
102      'lang_switch'=> $lsw,
103      'LSM_PATH' => LSM_PATH,
104      )
105    );
106}
107
108function reloadlang()
109{
110  global $user;
111  $same = $user['language'];   
112 
113  if (isset($_GET['lang']))
114  {
115    if (!empty($_GET['lang']) and file_exists(PHPWG_ROOT_PATH.'language/'.$_GET['lang'].'/common.lang.php'))
116    {
117      if (is_a_guest() or is_generic())
118      {
119        pwg_set_session_var('lang_switch', $_GET['lang']);
120      }
121      else
122      {
123        $query = '
124UPDATE '.USER_INFOS_TABLE.'
125  SET language = \''.$_GET['lang'].'\'
126  WHERE user_id = '.$user['id'].'
127;';
128        pwg_query($query);
129      }
130      $user['language'] = $_GET['lang'];
131    }
132  }
133  elseif ((is_a_guest() or is_generic()))
134  {
135    $user['language'] = pwg_get_session_var('lang_switch', $user['language']);
136  }
137 
138  if ( $same !== $user['language'])
139  {
140    load_language('common.lang', '', array('language'=>$user['language']));
141   
142    load_language(
143      'lang',
144      PHPWG_ROOT_PATH.PWG_LOCAL_DIR,
145      array(
146        'language' => $user['language'],
147        'no_fallback' => true,
148        'local' => true
149        )
150      );
151  }
152   
153  if (defined('IN_ADMIN') and IN_ADMIN)
154  {
155    load_language('admin.lang', '', array('language'=>$user['language']));
156  }
157}
158
159?>
Note: See TracBrowser for help on using the repository browser.