source: extensions/theme_switch/trunk/theme_switch.inc.php @ 7093

Revision 7093, 5.8 KB checked in by pab, 9 years ago (diff)

theme switch compatible with piwigo 2.1.* (with CZ, UK localization)

Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// | Theme switch plugin                                                   |
5// +-----------------------------------------------------------------------+
6// | Copyright(C) 2010      Pavel Budka               http://budkovi.ic.cz |
7// +-----------------------------------------------------------------------+
8// | This program is free software; you can redistribute it and/or modify  |
9// | it under the terms of the GNU General Public License as published by  |
10// | the Free Software Foundation                                          |
11// |                                                                       |
12// | This program is distributed in the hope that it will be useful, but   |
13// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
14// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
15// | General Public License for more details.                              |
16// |                                                                       |
17// | You should have received a copy of the GNU General Public License     |
18// | along with this program; if not, write to the Free Software           |
19// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
20// | USA.                                                                  |
21// +-----------------------------------------------------------------------+
22if (!defined('THEME_SWITCH_PATH'))
23define('THEME_SWITCH_PATH' , PHPWG_PLUGINS_PATH . basename(dirname(__FILE__)) . '/');
24
25class theme_controler {
26static public function _switch()
27{
28  global $user, $template, $conf;
29
30  load_language('plugin.lang', dirname(__FILE__).'/');
31
32  if (!defined('PHPWG_ROOT_PATH')) { die('Hacking attempt!'); }
33  $same = $user['theme'];
34  if ( isset( $_GET['theme']) ) 
35  {
36    if ( !empty($_GET['theme']) and
37      file_exists( PHPWG_ROOT_PATH.'themes/'.$_GET['theme']) )
38    {
39      if (is_a_guest() or is_generic())
40        setcookie( 'pwg_theme_switch', $_GET['theme'],
41          time()+60*60*24*30, cookie_path() );
42      else
43      {
44        $query = 'UPDATE '.USER_INFOS_TABLE.'
45        SET theme = \''.$_GET['theme'].'\'
46        WHERE user_id = '.$user['id'].'
47        ;';
48        pwg_query($query);
49      }
50      $user['theme'] = $_GET['theme'];
51    }
52  }
53  else
54  // Users have $user['template']/$user['theme']
55  // Guest or generic members will use their cookied $user['template']/$user['theme'] !
56  if ((is_a_guest() or is_generic())
57    and isset( $_COOKIE['pwg_theme_switch'] ) )
58  {
59    $user['theme'] = $_COOKIE['pwg_theme_switch'];
60  }
61} // function _switch()
62
63static public function _flags()
64// Lets user choose from icons
65// Only themes which have gif icon provided in icons plugin directory will be provided to users.
66// Gif file name must be the same as theme's name and must be stored in sub directory named according to template's name. 
67{
68  global $user, $template, $conf, $lang;
69 
70  $available_theme = get_pwg_themes();
71
72  $url_starting = get_query_string_diff(array('theme'));
73
74  foreach ( $available_theme as $theme_id => $theme ) {
75    $qlc_img =  $conf['themes_dir'].'/'.$theme_id.'/'.'screenshot.png';
76         
77    if (file_exists($qlc_img)) {
78     
79      $qlc_url = str_replace(array('=&amp;','?&amp;'),array('&amp;','?'),
80                 add_url_params( $url_starting, array( 'theme' => $theme_id )));
81      if (isset($lang['theme']))
82        $qlc_alt = $lang['theme'].': '.ucwords( $theme );
83      else
84        $qlc_alt = ucwords( $theme );
85      $qlc_title =  $qlc_alt;
86      $qlc = array ( 
87        'url' => $qlc_url,
88        'alt' => $qlc_alt,
89        'img' => $qlc_img,
90        );
91      if ( $theme_id !== $user['theme'])
92        $lsw['flags'][$theme_id] = $qlc ;
93      else $lsw['Active'] = $qlc;
94      }
95  } 
96   
97  $template->set_filename('theme_flags', dirname(__FILE__) . '/flags.tpl');
98  $lsw['side'] = ceil(sqrt(count($available_theme)));
99  $template->assign(array(
100    'theme_switch'=> $lsw,
101    'THEME_SWITCH_PATH' => THEME_SWITCH_PATH,
102  ));
103  $flags = $template->parse('theme_flags',true);
104  $template->clear_assign('theme_switch');
105  $template->concat( 'PLUGIN_INDEX_ACTIONS', $flags);
106} // function _flags()
107
108static public function _select()
109// Lets user choose using select control
110// All available themes will be provided to users.
111{
112  global $user, $template, $lang;
113 
114  $available_theme = get_pwg_themes();
115  $url_starting = get_query_string_diff(array('theme'));
116  $options='<li><span class="property">
117   <label for="template">'.$lang['theme'].'</label>
118  </span>
119  <select name="template" onchange="javasript:window.location=this.value">';
120  foreach ( $available_theme as $theme_id => $theme )
121  {
122    $qlc_url = str_replace(array('=&amp;','?&amp;'),array('&amp;','?'),
123      add_url_params( $url_starting, array( 'theme' => $theme_id  ) ));
124   
125    if ( $theme_id == $user['theme'] )
126      $tmp='" selected="selected">';
127    else
128      $tmp='">';
129    $options=$options.'
130    <option label="'.$theme.'" value="'.$qlc_url.$tmp.$theme.'</option>';
131  }
132  $options=$options.'
133  </select></li>';
134  $template->concat( 'PLUGIN_INDEX_ACTIONS',$options);
135} //  function _select()
136
137static public function _theme_admin($menu)
138// adds menu options for future configuration
139{
140//  load_language('plugin.lang', dirname(__FILE__).'/');
141  array_push($menu, array('NAME' => 'Theme Switch',
142    'URL' => get_admin_plugin_menu_link(THEME_SWITCH_PATH.'admin/admin.php')));
143  return $menu;
144} // function _theme_admin()
145
146} // class theme_controler
147
148  /* {html_head} usage function */
149  /* See flags.tpl for example (due no catenation available) */
150if (!function_exists('Componant_exists')) {
151  function Componant_exists($path, $file)
152    { return file_exists( $path . $file); }
153}
154
155?>
Note: See TracBrowser for help on using the repository browser.