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
8require_once('lib/wizard/wizard.php');
9
10/**
11 * The Wizard's editor type selector handler
12 */
13class AdminWizardLookAndFeel extends Wizard
14{
15	function pageTitle()
16	{
17		return tra('Set up Look & Feel');
18	}
19	function isEditable()
20	{
21		return true;
22	}
23
24	function onSetupPage($homepageUrl)
25	{
26		global $prefs;
27		$smarty = TikiLib::lib('smarty');
28		$tikilib = TikiLib::lib('tiki');
29		$themelib = TikiLib::lib('theme');
30		$csslib = TikiLib::lib('css');
31		$headerlib = TikiLib::lib('header');
32		// Run the parent first
33		parent::onSetupPage($homepageUrl);
34
35//handle case when changing the themes in the Look and Feel settings panel
36		$a_theme = $prefs['theme'];
37		if (isset($_REQUEST['looksetup'])) {
38			ask_ticket('admin-inc-look');
39			if (isset($_REQUEST['theme'])) {
40				check_ticket('admin-inc-general');
41
42				if (! isset($_REQUEST['theme_option']) || $_REQUEST['theme_option'] = '') {
43					// theme has no options
44					$_REQUEST['theme_option'] = '';
45				}
46				check_ticket('admin-inc-general');
47			}
48		} else {
49			// just changed theme menu, so refill options
50			if (isset($_REQUEST['theme']) && $_REQUEST['theme'] != '') {
51				$a_theme = $_REQUEST['theme'];
52			}
53		}
54
55		$themes = $themelib->list_themes();
56		$smarty->assign_by_ref('themes', $themes);
57		$theme_options = $themelib->list_theme_options($a_theme);
58		$smarty->assign('theme_options', $theme_options);
59
60		$theme_layouts = TikiLib::lib('css')->list_layouts();
61		$smarty->assign('theme_layouts', $theme_layouts);
62
63// get thumbnail if there is one
64		$thumbfile = $themelib->get_thumbnail_file($prefs['site_theme'], $prefs['site_theme_option']);
65		if (empty($thumbfile)) {
66			$thumbfile = $themelib->get_thumbnail_file($prefs['site_theme']);
67		}
68		if (empty($thumbfile)) {
69			$thumbfile = 'img/trans.png';
70		}
71		$smarty->assign('thumbfile', $thumbfile);
72
73// hash of themes and their options and their thumbnail images
74		if ($prefs['feature_jquery'] == 'y') {
75			$js = 'var theme_options = {';
76			foreach ($themes as $theme => $value) {
77				$js .= "\n'$theme':['" . $themelib->get_thumbnail_file($theme, '') . '\',{';
78				$options = $themelib->list_theme_options($theme);
79				if ($options) {
80					foreach ($options as $option) {
81						$js .= "'$option':'" . $themelib->get_thumbnail_file($theme, $option) . '\',';
82					}
83					$js = substr($js, 0, strlen($js) - 1) . '}';
84				} else {
85					$js .= '}';
86				}
87				$js .= '],';
88			}
89			$js = substr($js, 0, strlen($js) - 1);
90			$js .= '};';
91
92			$js .= 'var theme_layouts = ';
93			foreach ($themes as $theme => $value) {
94				$theme_layouts[$theme] = $csslib->list_user_selectable_layouts($theme);
95				$options = $themelib->list_theme_options($theme);
96				if ($options) {
97					foreach ($options as $option) {
98						$theme_layouts[$theme . ':' . $option] = $csslib->list_user_selectable_layouts($theme, $option);
99					}
100				}
101			}
102			//encode $theme_layouts into json to allow js below to fetch layouts based on theme selected by user
103			$theme_layouts_js = json_encode($theme_layouts);
104			$js .= $theme_layouts_js . ";";
105
106			// JS to handle theme/option changes client-side
107			// the var (theme_options) has to be declared in the same block for AJAX call scope
108			$none = json_encode(tr('None'));
109			$headerlib->add_cssfile('themes/base_files/feature_css/admin.css');
110			$headerlib->add_js(
111				<<<JS
112				$js
113
114\$(document).ready( function() {
115
116	var setupThemeSelects = function (themeDropDown, optionDropDown, showPreview) {
117		// pick up theme drop-down change
118		themeDropDown.change( function() {
119			var ops = theme_options[themeDropDown.val()];
120			var none = true;
121			var current = optionDropDown.val();
122			optionDropDown.empty().attr('disabled',false)
123					.append(\$('<option/>').attr('value','').text($none));
124			if (themeDropDown.val()) {
125				\$.each(ops[1], function(i, val) {
126					optionDropDown.append(\$('<option/>').attr('value',i).text(i));
127					none = false;
128				});
129			}
130			optionDropDown.val(current);
131			if (none) {
132				optionDropDown.attr('disabled',true);
133			}
134			optionDropDown.change();
135			if (jqueryTiki.chosen) {
136				optionDropDown.trigger("chosen:updated");
137			}
138		}).change();
139		optionDropDown.change( function() {
140			if (showPreview !== undefined) {
141				var t = themeDropDown.val();
142				var o = optionDropDown.val();
143				var f = theme_options[t][1][o];
144
145				if ( ! f ) {
146					f = theme_options[t][0];
147				}
148
149				if (f) {
150					\$('#theme_thumb').fadeOut('fast').attr('src', f).fadeIn('fast').animate({'opacity': 1}, 'fast');
151				} else {
152					\$('#theme_thumb').animate({'opacity': 0.3}, 'fast');
153				}
154			}
155		});
156	};
157
158	setupThemeSelects(\$('#wizardBody select[name=theme]'), \$('#wizardBody select[name=theme_option]'), true);
159	setupThemeSelects(\$('#wizardBody select[name=theme_admin]'), \$('#wizardBody select[name=theme_option_admin]'));
160
161	var setupThemeLayouts = function (themeDropDown, optionDropDown, layoutDropDown) {
162		themeDropDown,optionDropDown.change( function() {
163			var theme_name = themeDropDown.val();
164			if (optionDropDown.val()){
165				theme_name += ":" + optionDropDown.val();
166			}
167			var layouts = theme_layouts[theme_name];
168			var current = layoutDropDown.val();
169			layoutDropDown.empty();
170			//if no theme, it means it's the admin dropdown and is set to site theme. default to site layout
171			if (!theme_name){
172				layoutDropDown.append(\$('<option/>').attr('value','site_layout').text('Site layout'));
173				layoutDropDown.attr('disabled',true);
174				layoutDropDown.val('site_layout');
175			} else {
176				layoutDropDown.attr('disabled',false);
177				\$.each(layouts, function(i, val) {
178					layoutDropDown.append(\$('<option/>').attr('value',i).text(val));
179				});
180
181				//try setting the option to the previously selected option and if no layout matched, set to 'basic'
182				layoutDropDown.val(current);
183				if (!layoutDropDown.val()){
184					layoutDropDown.val('basic');
185				}
186			}
187
188			layoutDropDown.change();
189
190		}).change();
191	};
192
193	setupThemeLayouts(\$('#wizardBody select[name=theme]'), \$('#wizardBody select[name=theme_option]'), \$('#wizardBody select[name=site_layout]') );
194	setupThemeLayouts(\$('#wizardBody select[name=theme_admin]'), \$('#wizardBody select[name=theme_option_admin]'), \$('#wizardBody select[name=site_layout_admin]') );
195});
196JS
197			);
198		}
199
200//        // find thumbnail if there is one
201//		$a_style = $prefs['site_style'];
202//			// just changed theme menu, so refill options
203//		if (isset($_REQUEST['style']) && $_REQUEST['style'] != '') {
204//			$a_style = $_REQUEST['style'];
205//		}
206//		$thumbfile = $this->get_thumbnail_file($a_style, $prefs['site_style_option']);
207//		if (empty($thumbfile)) {
208//			$thumbfile = $this->get_thumbnail_file($a_style);
209//		}
210//		if (empty($thumbfile)) {
211//			$thumbfile = 'img/trans.png';
212//		}
213//		if (!empty($thumbfile)) {
214//			$smarty->assign('thumbfile', $thumbfile);
215//		}
216//
217//		$styles = $tikilib->list_styles();
218//		$smarty->assign_by_ref('styles', $styles);
219//		$smarty->assign('a_style', $a_style);
220//		$smarty->assign('style_options', $tikilib->list_style_options($a_style));
221//
222//		$this->setupThumbnailScript($styles);
223//
224		return true;
225	}
226
227	function getTemplate()
228	{
229		$wizardTemplate = 'wizard/admin_look_and_feel.tpl';
230		return $wizardTemplate;
231	}
232
233	function onContinue($homepageUrl)
234	{
235		// Run the parent first
236		$changes = parent::onContinue($homepageUrl);
237		if (array_key_exists('style', $changes) || array_key_exists('style_option', $changes)) {
238			$query = ['url' => $_REQUEST['url'], 'wizard_step' => $_REQUEST['wizard_step'], 'showOnLogin' => $_REQUEST['showOnLogin']];
239			TikiLib::lib('access')->redirect($_SERVER['PHP_SELF'] . '?' . http_build_query($query, '', '&'));
240		}
241	}
242
243	/**
244	 * @param $stl - style file name (e.g. thenews.css)
245	 * @param $opt - optional option file name
246	 * @return string path to thumbnail file
247	 */
248	function get_thumbnail_file($stl, $opt = '') // find thumbnail if there is one
249	{
250		$tikilib = TikiLib::lib('tiki');
251		if (! empty($opt) && $opt != tr('None')) {
252			$filename = preg_replace('/\.css$/i', '.png', $opt); // change .css to .png
253		} else {
254			$filename = preg_replace('/\.css$/i', '.png', $stl); // change .css to .png
255			$opt = '';
256		}
257		return $tikilib->get_style_path($stl, $opt, $filename);
258	}
259
260	function setupThumbnailScript($styles)
261	{
262		global	$prefs;
263		$headerlib = TikiLib::lib('header');
264		$tikilib = TikiLib::lib('tiki');
265
266		if ($prefs['feature_jquery'] == 'y') {
267			// hash of themes and their options and their thumbnail images
268			$js = 'var style_options = {';
269			foreach ($styles as $s) {
270				$js .= "\n'$s':['" . $this->get_thumbnail_file($s, '') . '\',{';
271				$options = $tikilib->list_style_options($s);
272				if ($options) {
273					foreach ($options as $o) {
274						$js .= "'$o':'" . $this->get_thumbnail_file($s, $o) . '\',';
275					}
276					$js = substr($js, 0, strlen($js) - 1) . '}';
277				} else {
278					$js .= '}';
279				}
280				$js .= '],';
281			}
282
283			$js = substr($js, 0, strlen($js) - 1);
284			$js .= '};';
285			// JS to handle theme/option changes client-side
286			// the var (style_options) has to be declared in the same block for AJAX call scope
287			$none = json_encode(tr('None'));
288
289			$headerlib->add_js(
290				<<<JS
291$js
292
293\$(document).ready( function() {
294	var setupStyleSelects = function (styleDropDown, optionDropDown, showPreview) {
295		// pick up theme drop-down change
296		styleDropDown.change( function() {
297			var ops = style_options[styleDropDown.val()];
298			var none = true;
299			var current = optionDropDown.val();
300			optionDropDown.empty().attr('disabled',false)
301					.append(\$('<option/>').attr('value',$none).text($none));
302			if (styleDropDown.val()) {
303				\$.each(ops[1], function(i, val) {
304					optionDropDown.append(\$('<option/>').attr('value',i).text(i.replace(/\.css\$/, '')));
305					none = false;
306				});
307			}
308			optionDropDown.val(current);
309			if (none) {
310				optionDropDown.attr('disabled',true);
311			}
312			optionDropDown.change();
313			if (jqueryTiki.chosen) {
314				optionDropDown.trigger("chosen:updated");
315			}
316		}).change();
317		optionDropDown.change( function() {
318			if (showPreview !== undefined) {
319				var t = styleDropDown.val();
320				var o = optionDropDown.val();
321				var f = style_options[t][1][o];
322
323				if ( ! f ) {
324					f = style_options[t][0];
325				}
326
327				if (f) {
328					\$('#style_thumb').fadeOut('fast').attr('src', f).fadeIn('fast').animate({'opacity': 1}, 'fast');
329				} else {
330					\$('#style_thumb').animate({'opacity': 0.3}, 'fast');
331				}
332			}
333		});
334	};
335	setupStyleSelects(\$('select[name=style]'), \$('select[name=style_option]'), true);
336	setupStyleSelects(\$('select[name=style_admin]'), \$('select[name=style_admin_option]'));
337});
338JS
339			);
340		}
341	}
342}
343