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

Revision 7072, 5.8 KB checked in by pab, 9 years ago (diff)
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// | Theme switch plugin                                                   |
5// +-----------------------------------------------------------------------+
6// | Copyright(C) 2008      Pavel Budka                http://pbudka.co.cc |
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__)) . '/');
24class theme_controler {
25static public function _switch()
26{
27  global $user, $template, $conf, $lang;
28  if (!defined('PHPWG_ROOT_PATH')) { die('Hacking attempt!'); }
29  $same = $user['template'].'/'.$user['theme'];
30  if ( isset( $_GET['theme']) and isset( $_GET['template']) )
31  {
32    if ( !empty($_GET['theme']) and !empty($_GET['template']) and
33      file_exists( PHPWG_ROOT_PATH.'template/'.$_GET['template'].'/theme/'.$_GET['theme']) )
34    {
35      if (is_a_guest() or is_generic())
36      {
37        setcookie( 'pwg_theme_switch', $_GET['theme'],
38          time()+60*60*24*30, cookie_path() );
39        setcookie( 'pwg_template_switch', $_GET['template'],
40          time()+60*60*24*30, cookie_path() );
41      }
42      else
43      {
44        $query = 'UPDATE '.USER_INFOS_TABLE.'
45        SET template = \''.$_GET['template'].'/'.$_GET['theme'].'\'
46        WHERE user_id = '.$user['id'].'
47        ;';
48        pwg_query($query);
49      }
50      $user['theme'] = $_GET['theme'];
51      $user['template'] = $_GET['template'];
52    }
53  }
54  else
55  // Users have $user['template']/$user['theme']
56  // Guest or generic members will use their cookied $user['template']/$user['theme'] !
57  if ((is_a_guest() or is_generic())
58    and isset( $_COOKIE['pwg_theme_switch'] )
59    and isset( $_COOKIE['pwg_template_switch'] ) )
60  {
61    $user['template'] = $_COOKIE['pwg_template_switch'];
62    $user['theme'] = $_COOKIE['pwg_theme_switch'];
63  }
64}
65
66static public function _flags()
67// Lets user choose from icons
68// Only themes which have gif icon provided in icons plugin directory will be provided to users.
69// Gif file name must be the same as theme's name and must be stored in sub directory named according to template's name. 
70{
71  global $user, $template, $lang;
72 
73  $available_theme = get_pwg_themes();
74
75  $url_starting = get_query_string_diff(array('template', 'theme'));
76
77  foreach ( $available_theme as $code ) {
78    $qlc_img =  'plugins/theme_switch/icons/' . $code . '.gif';
79         
80    if (file_exists(PHPWG_ROOT_PATH.$qlc_img)) {
81      list($temp, $theme) = split('/',$code);
82      $qlc_url = str_replace(array('=&amp;','?&amp;'),array('&amp;','?'),
83                 add_url_params( $url_starting, array( 'template' => $temp, 'theme' => $theme  ) ));
84      if (isset($lang['theme']))
85        $qlc_alt = $lang['theme'].': '.ucwords( $code );
86      else
87        $qlc_alt = ucwords( $code );
88      $qlc_title =  $qlc_alt;
89      $qlc = array ( 
90        'url' => $qlc_url,
91        'alt' => $qlc_alt,
92        'img' => $qlc_img,
93        );
94      if ( $code !== $user['template'].'/'.$user['theme'])
95        $lsw['flags'][$code] = $qlc ;
96      else $lsw['Active'] = $qlc;
97      }
98  }
99   
100  $template->set_filename('theme_flags', dirname(__FILE__) . '/flags.tpl');
101  $lsw['side'] = ceil(sqrt(count($available_theme)));
102  $template->assign(array(
103    'theme_switch'=> $lsw,
104    'THEME_SWITCH_PATH' => THEME_SWITCH_PATH,
105  ));
106  $flags = $template->parse('theme_flags',true);
107  $template->clear_assign('theme_switch');
108  $template->concat( 'PLUGIN_INDEX_ACTIONS', $flags);
109}
110
111static public function _select()
112// Lets user choose using select control
113// All available themes will be provided to users.
114{
115  global $user, $template, $lang;
116 
117  $available_theme = get_pwg_themes();
118  $url_starting = get_query_string_diff(array('lang'));
119  $options='<li><span class="property">
120   <label for="template">'.$lang['theme'].'</label>
121  </span>
122  <select name="template" onchange="javasript:window.location=this.value">';
123  foreach ( $available_theme as $code )
124  {
125    list($temp, $theme) = split('/',$code);
126    $qlc_url = str_replace(array('=&amp;','?&amp;'),array('&amp;','?'),
127      add_url_params( $url_starting, array( 'template' => $temp, 'theme' => $theme  ) ));
128   
129    if ( $code == $user['template'].'/'.$user['theme'] )
130      $tmp='" selected="selected">';
131    else
132      $tmp='">';
133    $options=$options.'
134    <option label="'.$code.'" value="'.$qlc_url.$tmp.$code.'</option>';
135  }
136  $options=$options.'
137  </select></li>';
138  $template->concat( 'PLUGIN_INDEX_ACTIONS',$options);
139}
140}
141  /* {html_head} usage function */
142  /* See flags.tpl for example (due no catenation available) */
143if (!function_exists('Componant_exists')) {
144  function Componant_exists($path, $file)
145    { return file_exists( $path . $file); }
146}
147?>
Note: See TracBrowser for help on using the repository browser.