1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8// This script may only be included - so its better to die if called directly.
9if (strpos($_SERVER['SCRIPT_NAME'], basename(__FILE__)) !== false) {
10	header('location: index.php');
11	exit;
12}
13global $prefs;
14$themelib = TikiLib::lib('theme');
15$csslib = TikiLib::lib('css');
16
17//handle case when changing the themes in the Look and Feel settings panel
18$a_theme = $prefs['theme'];
19if (isset($_REQUEST['looksetup'])) {
20	if (isset($_REQUEST['theme'])) {
21		if (! isset($_REQUEST['theme_option']) || $_REQUEST['theme_option'] = '') {
22			// theme has no options
23			$_REQUEST['theme_option'] = '';
24		}
25	}
26} else {
27	// just changed theme menu, so refill options
28	if (isset($_REQUEST['theme']) && $_REQUEST['theme'] != '') {
29		$a_theme = $_REQUEST['theme'];
30	}
31}
32
33$themes = $themelib->list_themes();
34$smarty->assign_by_ref('themes', $themes);
35$theme_options = $themelib->list_theme_options($a_theme);
36$smarty->assign('theme_options', $theme_options);
37
38$themePrefs = TikiLib::lib('prefs')->getThemePrefs();
39$smarty->assign('themePrefs', $themePrefs);
40
41// get thumbnail if there is one
42$thumbfile = $themelib->get_thumbnail_file($prefs['site_theme'], $prefs['site_theme_option']);
43if (empty($thumbfile)) {
44	$thumbfile = $themelib->get_thumbnail_file($prefs['site_theme']);
45}
46if (empty($thumbfile)) {
47	$thumbfile = 'img/trans.png';
48}
49$smarty->assign('thumbfile', $thumbfile);
50
51// hash of themes and their options and their thumbnail images
52if ($prefs['feature_jquery'] == 'y') {
53	$js = 'var theme_options = {';
54	foreach ($themes as $theme => $value) {
55		$js .= "\n'$theme':['" . $themelib->get_thumbnail_file($theme, '') . '\',{';
56		$options = $themelib->list_theme_options($theme);
57		if ($options) {
58			foreach ($options as $option) {
59				$js .= "'$option':'" . $themelib->get_thumbnail_file($theme, $option) . '\',';
60			}
61			$js = substr($js, 0, strlen($js) - 1) . '}';
62		} else {
63			$js .= '}';
64		}
65		$js .= '],';
66	}
67	$js = substr($js, 0, strlen($js) - 1);
68	$js .= '};';
69
70	//Setup theme layouts array matching themes and theme:options with their respective layouts
71	$js .= 'var theme_layouts = ';
72	foreach ($themes as $theme => $value) {
73		$theme_layouts[$theme] = $csslib->list_user_selectable_layouts($theme);
74		$options = $themelib->list_theme_options($theme);
75		if ($options) {
76			foreach ($options as $option) {
77				$theme_layouts[$theme . ':' . $option] = $csslib->list_user_selectable_layouts($theme, $option);
78			}
79		}
80	}
81	//encode $theme_layouts into json to allow js below to fetch layouts based on theme selected by user
82	$theme_layouts_js = json_encode($theme_layouts);
83	$js .= $theme_layouts_js . ";";
84
85	// JS to handle theme/option changes client-side
86	// the var (theme_options) has to be declared in the same block for AJAX call scope
87	$none = json_encode(tr('None'));
88	$headerlib->add_js(
89		<<<JS
90$js
91
92\$(document).ready( function() {
93
94	var setupThemeSelects = function (themeDropDown, optionDropDown, showPreview) {
95		// pick up theme drop-down change
96		themeDropDown.change( function() {
97			var ops = theme_options[themeDropDown.val()];
98			var none = true;
99			var current = optionDropDown.val();
100			optionDropDown.empty().attr('disabled',false)
101					.append(\$('<option/>').attr('value','').text($none));
102			if (themeDropDown.val()) {
103				\$.each(ops[1], function(i, val) {
104					optionDropDown.append(\$('<option/>').attr('value',i).text(i));
105					none = false;
106				});
107			}
108			optionDropDown.val(current);
109			if (!optionDropDown.val()){
110				optionDropDown.val('');
111			}
112
113			if (none) {
114				optionDropDown.attr('disabled',true);
115			}
116			optionDropDown.change();
117			if (jqueryTiki.chosen) {
118				optionDropDown.trigger("chosen:updated");
119			}
120		}).change();
121		optionDropDown.change( function() {
122			if (showPreview !== undefined) {
123				var t = themeDropDown.val();
124				var o = optionDropDown.val();
125				var f = theme_options[t][1][o];
126
127				if ( ! f ) {
128					f = theme_options[t][0];
129				}
130
131				if (f) {
132					\$('#theme_thumb').fadeOut('fast').attr('src', f).fadeIn('fast').animate({'opacity': 1}, 'fast');
133				} else {
134					\$('#theme_thumb').animate({'opacity': 0.3}, 'fast');
135				}
136			}
137		});
138	};
139
140	setupThemeSelects(\$('.tab-content select[name=theme]'), \$('.tab-content select[name=theme_option]'), true);
141	setupThemeSelects(\$('.tab-content select[name=theme_admin]'), \$('.tab-content select[name=theme_option_admin]'));
142
143	var setupThemeLayouts = function (themeDropDown, optionDropDown, layoutDropDown) {
144		themeDropDown,optionDropDown.change( function() {
145			var theme_name = themeDropDown.val();
146			if (optionDropDown.val()){
147				theme_name += ":" + optionDropDown.val();
148			}
149			var layouts = theme_layouts[theme_name];
150			var current = layoutDropDown.val();
151			layoutDropDown.empty();
152			if (!theme_name){
153				layoutDropDown.append(\$('<option/>').attr('value','').text('Site layout'));
154				layoutDropDown.attr('disabled',true);
155			} else {
156				layoutDropDown.attr('disabled',false);
157				\$.each(layouts, function(i, val) {
158					layoutDropDown.append(\$('<option/>').attr('value',i).text(val));
159				});
160
161				//try setting the option to the previously selected option and if no layout matched, set to 'basic'
162				layoutDropDown.val(current);
163				if (!layoutDropDown.val()){
164					layoutDropDown.val('basic');
165				}
166			}
167			layoutDropDown.change();
168
169		}).change();
170	};
171
172	setupThemeLayouts(\$('.tab-content select[name=theme]'), \$('.tab-content select[name=theme_option]'), \$('.tab-content select[name=site_layout]') );
173	setupThemeLayouts(\$('.tab-content select[name=theme_admin]'), \$('.tab-content select[name=theme_option_admin]'), \$('.tab-content select[name=site_layout_admin]') );
174});
175JS
176	);
177}
178