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

Last change on this file since 7171 was 7171, checked in by pab, 14 years ago
File size: 6.4 KB
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
25include_once(PHPWG_ROOT_PATH.'admin/include/themes.class.php');
26
27class theme_controler {
28static public function _switch()
29{
30  global $user, $template, $conf;
31
32  load_language('plugin.lang', dirname(__FILE__).'/');
33
34  if (!defined('PHPWG_ROOT_PATH')) { die('Hacking attempt!'); }
35  $same = $user['theme'];
36  if ( isset( $_GET['theme']) ) 
37  {
38    if ( !empty($_GET['theme']) and
39      file_exists( PHPWG_ROOT_PATH.'themes/'.$_GET['theme']) )
40    {
41      if (is_a_guest() or is_generic())
42        setcookie( 'pwg_theme_switch', $_GET['theme'],
43          time()+60*60*24*30, cookie_path() );
44      else
45      {
46        $query = 'UPDATE '.USER_INFOS_TABLE.'
47        SET theme = \''.$_GET['theme'].'\'
48        WHERE user_id = '.$user['id'].'
49        ;';
50        pwg_query($query);
51      }
52      $user['theme'] = $_GET['theme'];
53    }
54  }
55  else
56  // Users have $user['template']/$user['theme']
57  // Guest or generic members will use their cookied $user['template']/$user['theme'] !
58  if ((is_a_guest() or is_generic())
59    and isset( $_COOKIE['pwg_theme_switch'] ) )
60  {
61    $user['theme'] = $_COOKIE['pwg_theme_switch'];
62  }
63} // function _switch()
64
65static public function _installed_themes()
66// id, name, screenshot 
67{
68$themes = new themes();
69$themes->sort_fs_themes();
70$db_themes = $themes->get_db_themes();
71$db_theme_ids = array();
72foreach ($db_themes as $db_theme)
73{
74  array_push($db_theme_ids, $db_theme['id']);
75}
76
77$active_themes = array();
78
79
80foreach ($themes->fs_themes as $theme_id => $fs_theme)
81{
82  if (in_array($theme_id, $db_theme_ids))
83      array_push($active_themes, $fs_theme);
84}
85
86return $active_themes;
87} // function  _installed_themes()
88
89
90static public function _flags()
91// Lets user choose from icons
92// Only themes which have gif icon provided in icons plugin directory will be provided to users.
93// Gif file name must be the same as theme's name and must be stored in sub directory named according to template's name. 
94{
95  global $user, $template, $conf, $lang;
96 
97  $available_theme = get_pwg_themes();
98
99  $url_starting = get_query_string_diff(array('theme'));
100 
101  foreach ( theme_controler::_installed_themes() as $theme ) {
102    $qlc_img =  $theme['screenshot']; // $conf['themes_dir'].'/'.$theme_id.'/'.'screenshot.png';
103         
104    if (file_exists($qlc_img)) {
105     
106      $qlc_url = str_replace(array('=&amp;','?&amp;'),array('&amp;','?'),
107                 add_url_params( $url_starting, array( 'theme' => $theme['id'] )));
108      if (isset($lang['theme']))
109        $qlc_alt = $lang['theme'].' '.ucwords( $theme['name'] );
110      else
111        $qlc_alt = ucwords( $theme['name'] );
112      $qlc_title =  $qlc_alt;
113      $qlc = array ( 
114        'url' => $qlc_url,
115        'alt' => $qlc_alt,
116        'img' => $qlc_img,
117        );
118      if ( $theme['id'] !== $user['theme'])
119        $lsw['flags'][$theme['id']] = $qlc ;
120      else $lsw['Active'] = $qlc;
121      }
122  } 
123   
124  $template->set_filename('theme_flags', dirname(__FILE__) . '/flags.tpl');
125  $lsw['side'] = ceil(sqrt(count($available_theme)));
126  $template->assign(array(
127    'theme_switch'=> $lsw,
128    'THEME_SWITCH_PATH' => THEME_SWITCH_PATH,
129  ));
130  $flags = $template->parse('theme_flags',true);
131  $template->clear_assign('theme_switch');
132  $template->concat( 'PLUGIN_INDEX_ACTIONS', $flags);
133} // function _flags()
134
135
136static public function _select()
137// Lets user choose using select control
138// All available themes will be provided to users.
139{
140  global $user, $template, $lang;
141 
142  $available_theme = get_pwg_themes();
143  $url_starting = get_query_string_diff(array('theme'));
144  $options='<li class="selector">'
145   .$lang['theme'].
146    '&nbsp;<select onchange="javasript:window.location=this.value">';
147  foreach ( theme_controler::_installed_themes() as $theme )
148  {
149    $qlc_url = str_replace(array('=&amp;','?&amp;'),array('&amp;','?'),
150      add_url_params( $url_starting, array( 'theme' => $theme['id']  ) ));
151   
152    if ( $theme['id'] == $user['theme'] )
153      $tmp='" selected="selected">';
154    else
155      $tmp='">';
156    $options=$options.'
157    <option label="'.$theme['name'].'" value="'.$qlc_url.$tmp.$theme['name'].'</option>';
158  }
159  $options=$options.'
160  </select></li>';
161  $template->concat( 'PLUGIN_INDEX_ACTIONS',$options);
162} //  function _select()
163
164static public function _theme_admin($menu)
165// adds menu options for future configuration
166{
167//  load_language('plugin.lang', dirname(__FILE__).'/');
168  array_push($menu, array('NAME' => 'Theme Switch',
169    'URL' => get_admin_plugin_menu_link(THEME_SWITCH_PATH.'admin/admin.php')));
170  return $menu;
171} // function _theme_admin()
172
173} // class theme_controler
174
175  /* {html_head} usage function */
176  /* See flags.tpl for example (due no catenation available) */
177if (!function_exists('Componant_exists')) {
178  function Componant_exists($path, $file)
179    { return file_exists( $path . $file); }
180}
181
182?>
Note: See TracBrowser for help on using the repository browser.