<?php /*

 ocPortal
 Copyright (c) ocProducts, 2004-2012

 See text/EN/licence.txt for full licencing information.


 NOTE TO PROGRAMMERS:
   Do not edit this file. If you need to make changes, save your changed file to the appropriate *_custom folder
   **** If you ignore this advice, then your website upgrades (e.g. for bug fixes) will likely kill your changes ****

*/

/**
 * @license		http://opensource.org/licenses/cpal_1.0 Common Public Attribution License
 * @copyright	ocProducts Ltd
 * @package		themewizard
 */

/**
 * Module page class.
 */
class Module_admin_themewizard
{

	/**
	 * Standard modular info function.
	 *
	 * @return ?array	Map of module info (NULL: module is disabled).
	 */
	function info()
	{
		$info=array();
		$info['author']='Allen Ellis';
		$info['organisation']='ocProducts';
		$info['hacked_by']=NULL;
		$info['hack_version']=NULL;
		$info['version']=2;
		$info['locked']=false;
		return $info;
	}

	/**
	 * Standard modular entry-point finder function.
	 *
	 * @return ?array	A map of entry points (type-code=>language-code) (NULL: disabled).
	 */
	function get_entry_points()
	{
		return array('misc'=>'THEMEWIZARD','make_logo'=>'LOGOWIZARD');
	}

	/**
	 * Standard modular run function.
	 *
	 * @return tempcode	The result of execution.
	 */
	function run()
	{
		require_lang('themes');
		require_code('themes2');
		require_code('themewizard');

		$GLOBALS['HELPER_PANEL_PIC']='pagepics/themewizard';
		$GLOBALS['HELPER_PANEL_TUTORIAL']='tut_themes';
		$GLOBALS['HELPER_PANEL_TEXT']=comcode_lang_string('DOC_THEMEWIZARD');

		$type=get_param('type','misc');

		if ($type=='misc') return $this->step1();
		if ($type=='step2') return $this->step2();
		if ($type=='step3') return $this->step3();
		if ($type=='step4') return $this->step4();
		if ($type=='make_logo') return $this->make_logo();
		if ($type=='_make_logo') return $this->_make_logo();
		if ($type=='__make_logo') return $this->__make_logo();

		return new ocp_tempcode();
	}

	/**
	 * UI for a theme wizard step (choose colour).
	 *
	 * @return tempcode		The UI
	 */
	function step1()
	{
		$title=get_page_title('_THEMEWIZARD',true,array(integer_format(1),integer_format(4)));

		$post_url=build_url(array('page'=>'_SELF','type'=>'step2'),'_SELF',array('keep_theme_seed','keep_theme_dark','keep_theme_source','keep_theme_algorithm'),false,true);
		$text=do_lang_tempcode('THEMEWIZARD_1_DESCRIBE');
		$submit_name=do_lang_tempcode('PROCEED');

		require_code('form_templates');

		$source_theme=get_param('source_theme','default');

		$hidden=new ocp_tempcode();
		if (count(find_all_themes())==1)
		{
			$hidden->attach(form_input_hidden('source_theme',$source_theme));
		} else
		{
			$themes=nice_get_themes($source_theme,true);
		}

		$fields=new ocp_tempcode();

		$fields->attach(form_input_codename(do_lang_tempcode('NEW_THEME'),do_lang_tempcode('DESCRIPTION_NAME'),'themename',get_param('themename',''),true));

		$fields->attach(do_template('FORM_SCREEN_FIELD_SPACER',array('SECTION_HIDDEN'=>false,'TITLE'=>do_lang_tempcode('PARAMETERS'))));

		$fields->attach(form_input_colour(do_lang_tempcode('SEED_COLOUR'),do_lang_tempcode('DESCRIPTION_SEED_COLOUR'),'seed','#'.get_param('seed',find_theme_seed('default')),true));

		if (count(find_all_themes())!=1)
		{
			$fields->attach(form_input_list(do_lang_tempcode('SOURCE_THEME'),do_lang_tempcode('DESCRIPTION_SOURCE_THEME'),'source_theme',$themes,NULL,true));
		}

		$radios=new ocp_tempcode();
		$radios->attach(form_input_radio_entry('algorithm','equations',$source_theme=='default',do_lang_tempcode('THEMEGEN_ALGORITHM_EQUATIONS')));
		$radios->attach(form_input_radio_entry('algorithm','hsv',$source_theme!='default',do_lang_tempcode('THEMEGEN_ALGORITHM_HSV')));
		$fields->attach(form_input_radio(do_lang_tempcode('THEMEGEN_ALGORITHM'),do_lang_tempcode('DESCRIPTION_THEMEGEN_ALGORITHM'),'algorithm',$radios,true));

		$fields->attach(form_input_tick(do_lang_tempcode('DARK_THEME'),do_lang_tempcode('DESCRIPTION_DARK_THEME'),'dark',get_param_integer('dark',0)==1));

		$fields->attach(do_template('FORM_SCREEN_FIELD_SPACER',array('SECTION_HIDDEN'=>true,'TITLE'=>do_lang_tempcode('ADVANCED'))));
		$fields->attach(form_input_tick(do_lang_tempcode('INHERIT_CSS'),do_lang_tempcode('DESCRIPTION_INHERIT_CSS'),'inherit_css',get_param_integer('inherit_css',0)==1));

		breadcrumb_set_self(do_lang_tempcode('THEMEWIZARD'));

		require_javascript('javascript_ajax');
		$script=find_script('snippet');
		$javascript="
			var form=document.getElementById('main_form');
			form.elements['source_theme'].onchange=function() {
				var default_theme=(form.elements['source_theme'].options[form.elements['source_theme'].selectedIndex].value=='default');
				form.elements['algorithm'][0].checked=default_theme;
				form.elements['algorithm'][1].checked=!default_theme;
			}
			form.old_submit=form.onsubmit;
			form.onsubmit=function()
				{
					document.getElementById('submit_button').disabled=true;
					var url='".addslashes($script)."?snippet=exists_theme&name='+window.encodeURIComponent(form.elements['themename'].value);
					if (!do_ajax_field_test(url))
					{
						document.getElementById('submit_button').disabled=false;
						return false;
					}
					document.getElementById('submit_button').disabled=false;
					if (typeof form.old_submit!='undefined' && form.old_submit) return form.old_submit();
					return true;
				};
		";

		return do_template('FORM_SCREEN',array('_GUID'=>'98963f4d7ff60744382f937e6cc5acbf','GET'=>true,'SKIP_VALIDATION'=>true,'TITLE'=>$title,'JAVASCRIPT'=>$javascript,'FIELDS'=>$fields,'URL'=>$post_url,'TEXT'=>$text,'SUBMIT_NAME'=>$submit_name,'HIDDEN'=>$hidden));
	}

	/**
	 * UI for a theme wizard step (choose preview).
	 *
	 * @return tempcode		The UI
	 */
	function step2()
	{
		$title=get_page_title('_THEMEWIZARD',true,array(integer_format(2),integer_format(4)));

		$source_theme=get_param('source_theme');
		$algorithm=get_param('algorithm');
		$seed=get_param('seed');
		$dark=get_param_integer('dark',0);
		$inherit_css=get_param_integer('inherit_css',0);
		$themename=get_param('themename');
		if ((file_exists(get_custom_file_base().'/themes/'.$themename)) || ($themename=='default'))
		{
			warn_exit(do_lang_tempcode('ALREADY_EXISTS',escape_html($themename)));
		}

		// Check length (6 chars)
		if (strlen($seed)!=6) warn_exit(do_lang_tempcode('INVALID_COLOUR'));

		list($_theme,)=calculate_theme($seed,$source_theme,$algorithm,'colours',$dark==1);
		$theme=array();
		$theme['SOURCE_THEME']=$source_theme;
		$theme['ALGORITHM']=$algorithm;
		$theme['RED']=$_theme['red'];
		$theme['GREEN']=$_theme['green'];
		$theme['BLUE']=$_theme['blue'];
		$theme['DOMINANT']=$_theme['dominant'];
		$theme['LD']=$_theme['LD'];
		$theme['DARK']=$_theme['dark'];
		$theme['SEED']=$_theme['seed'];
		//$theme=$theme['css']; // this will simplify things a bit, since we only use the 'css' sub-array.
		$theme['TITLE']=$title;
		$theme['CHANGE_LINK']=build_url(array('page'=>'_SELF','type'=>'misc','source_theme'=>$source_theme,'algorithm'=>$algorithm,'seed'=>$seed,'dark'=>$dark,'inherit_css'=>$inherit_css,'themename'=>$themename),'_SELF');
		$theme['STAGE3_LINK']=build_url(array('page'=>'_SELF','type'=>'step3','source_theme'=>$source_theme,'algorithm'=>$algorithm,'seed'=>$seed,'dark'=>$dark,'inherit_css'=>$inherit_css,'themename'=>$themename),'_SELF');

		breadcrumb_set_parents(array(array('_SELF:_SELF:misc',do_lang_tempcode('THEMEWIZARD'))));
		return do_template('THEMEWIZARD_2_SCREEN',$theme);
	}

	/**
	 * UI for a theme wizard step (choose save).
	 *
	 * @return tempcode		The UI
	 */
	function step3()
	{
		$title=get_page_title('_THEMEWIZARD',true,array(integer_format(3),integer_format(4)));

		$source_theme=get_param('source_theme');
		$algorithm=get_param('algorithm');
		$seed=get_param('seed');
		$dark=get_param_integer('dark');
		$inherit_css=get_param_integer('inherit_css');
		$themename=get_param('themename');

		$post_url=build_url(array('page'=>'_SELF','type'=>'step4'),'_SELF');
		$submit_name=do_lang_tempcode('ADD_THEME');
		require_code('form_templates');
		$fields=new ocp_tempcode();
		$fields->attach(form_input_tick(do_lang_tempcode('USE_ON_ZONES'),do_lang_tempcode('DESCRIPTION_USE_ON_ZONES'),'use_on_all',true));
		$hidden=new ocp_tempcode();
		$hidden->attach(form_input_hidden('source_theme',$source_theme));
		$hidden->attach(form_input_hidden('algorithm',$algorithm));
		$hidden->attach(form_input_hidden('seed',$seed));
		$hidden->attach(form_input_hidden('themename',$themename));
		$hidden->attach(form_input_hidden('dark',strval($dark)));
		$hidden->attach(form_input_hidden('inherit_css',strval($inherit_css)));

		breadcrumb_set_parents(array(array('_SELF:_SELF:misc',do_lang_tempcode('THEMEWIZARD'))));
		return do_template('FORM_SCREEN',array('_GUID'=>'349383d77ecfce8c65f3303cfec86ea0','SKIP_VALIDATION'=>true,'TITLE'=>$title,'TEXT'=>do_lang_tempcode('REFRESH_TO_FINISH'),'FIELDS'=>$fields,'URL'=>$post_url,'SUBMIT_NAME'=>$submit_name,'HIDDEN'=>$hidden));
	}

	/**
	 * UI for a theme wizard step (actualisation).
	 *
	 * @return tempcode		The UI
	 */
	function step4()
	{
		// Add theme
		$source_theme=post_param('source_theme');
		$algorithm=post_param('algorithm');
		$seed=post_param('seed');
		$themename=post_param('themename');
		$use=(post_param_integer('use_on_all',0)==1);
		$dark=post_param_integer('dark');
		$inherit_css=post_param_integer('inherit_css');

		if (function_exists('set_time_limit')) @set_time_limit(0);

		breadcrumb_set_parents(array(array('_SELF:_SELF:misc',do_lang_tempcode('THEMEWIZARD'))));

		require_code('type_validation');
		if ((!is_alphanumeric($themename)) || (strlen($themename)>40)) warn_exit(do_lang_tempcode('BAD_CODENAME'));
		make_theme($themename,$source_theme,$algorithm,$seed,$use,$dark==1,$inherit_css==1);
		$myfile=@fopen(get_custom_file_base().'/themes/'.filter_naughty($themename).'/theme.ini','wt') OR intelligent_write_error(get_custom_file_base().'/themes/'.filter_naughty($themename).'/theme.ini');
		fwrite($myfile,'title='.$themename.chr(10));
		fwrite($myfile,'description='.do_lang('NA').chr(10));
		fwrite($myfile,'seed='.$seed.chr(10));
		if (fwrite($myfile,'author='.$GLOBALS['FORUM_DRIVER']->get_username(get_member()).chr(10))==0) warn_exit(do_lang_tempcode('COULD_NOT_SAVE_FILE'));
		fclose($myfile);
		sync_file('themes/'.filter_naughty($themename).'/theme.ini');

		// We're done
		$title=get_page_title('_THEMEWIZARD',true,array(integer_format(4),integer_format(4)));
		$message=do_lang_tempcode('THEMEWIZARD_4_DESCRIBE',escape_html('#'.$seed),escape_html($themename));

		require_code('templates_donext');
		return do_next_manager($title,$message,
						NULL,
						NULL,
						/*		TYPED-ORDERED LIST OF 'LINKS'		*/
						/*	 page	 params				  zone	  */
						NULL,																						// Add one
						NULL,																						// Edit this
						NULL,																						// Edit one
						NULL,																						// View this
						NULL,																						// View archive
						NULL,																						// Add to category
						NULL,																						// Add one category
						NULL,																						// Edit one category
						NULL,																						// Edit this category
						NULL,																						// View this category
						/*	  SPECIALLY TYPED 'LINKS'				  */
						array(),
						array(),
						array(
							/*	 type							  page			 params													 zone	  */
							array('edit_this',array('admin_themes',array('type'=>'edit_theme','theme'=>$themename),get_module_zone('admin_themes'))),								 // Edit this
							array('edit_css',array('admin_themes',array('type'=>'choose_css','theme'=>$themename),get_module_zone('admin_themes'))),
							array('edit_templates',array('admin_themes',array('type'=>'edit_templates','theme'=>$themename),get_module_zone('admin_themes'))),
							array('manage_images',array('admin_themes',array('type'=>'manage_images','theme'=>$themename),get_module_zone('admin_themes'))),
							array('manage_themes',array('admin_themes',array('type'=>'misc'),get_module_zone('admin_themes')))
						),
						do_lang('THEME')
		);
	}

	/**
	 * UI for a logo wizard step (ask for input).
	 *
	 * @return tempcode		The UI
	 */
	function make_logo()
	{
		if (!function_exists('imagepng')) warn_exit(do_lang_tempcode('GD_NEEDED'));

		$title=get_page_title('_LOGOWIZARD',true,array(integer_format(1),integer_format(3)));
		$GLOBALS['HELPER_PANEL_PIC']='pagepics/logowizard';

		$post_url=build_url(array('page'=>'_SELF','type'=>'_make_logo'),'_SELF');
		$theme_image_url=build_url(array('page'=>'admin_themes','type'=>'edit_image','id'=>'logo/-logo','lang'=>user_lang(),'theme'=>$GLOBALS['FORUM_DRIVER']->get_theme('')),get_module_zone('admin_themes'));
		$text=do_lang_tempcode('LOGOWIZARD_1_DESCRIBE',escape_html($theme_image_url->evaluate()));
		$submit_name=do_lang_tempcode('PROCEED');

		require_code('form_templates');
		$fields=new ocp_tempcode();
		$fields->attach(form_input_line(do_lang_tempcode('NAME'),do_lang_tempcode('DESCRIPTION_LOGO_NAME'),'name',get_option('site_name'),true));
		$a=$GLOBALS['SITE_DB']->query_value('zones','zone_title',array('zone_name'=>''));
		$fields->attach(form_input_line(do_lang_tempcode('TITLE'),do_lang_tempcode('DESCRIPTION_LOGO_SLOGAN'),'title',get_translated_text($a),true));

		// Find the most appropriate theme to edit for
		$theme=$GLOBALS['SITE_DB']->query_value_null_ok('zones','zone_theme',array('zone_name'=>'site'));
		if (is_null($theme)) // Just in case the 'site' zone no longer exists
			$theme=$GLOBALS['SITE_DB']->query_value('zones','zone_theme',array('zone_name'=>''));
		if ($theme=='-1')
		{
			$theme=preg_replace('#[^A-Za-z\d]#','_',get_site_name());
		}
		if (!file_exists(get_custom_file_base().'/themes/'.$theme)) $theme='default';
		require_code('themes2');

		$fields->attach(form_input_list(do_lang_tempcode('THEME'),do_lang_tempcode('DESCRIPTION_LOGOWIZARD_THEME'),'theme',nice_get_themes($theme,true)));

		breadcrumb_set_self(do_lang_tempcode('LOGOWIZARD'));

		return do_template('FORM_SCREEN',array('_GUID'=>'08449c0ae8edf5c0b3510611c9ac9618','SKIP_VALIDATION'=>true,'TITLE'=>$title,'FIELDS'=>$fields,'URL'=>$post_url,'TEXT'=>$text,'SUBMIT_NAME'=>$submit_name,'HIDDEN'=>''));
	}

	/**
	 * UI for a logo wizard step (show preview).
	 *
	 * @return tempcode		The UI
	 */
	function _make_logo()
	{
		$title=get_page_title('_LOGOWIZARD',true,array(integer_format(2),integer_format(3)));
		$GLOBALS['HELPER_PANEL_PIC']='pagepics/logowizard';

		$preview=do_template('LOGOWIZARD_2_SCREEN',array('NAME'=>post_param('name'),'TITLE'=>post_param('title'),'THEME'=>post_param('theme')));

		breadcrumb_set_parents(array(array('_SELF:_SELF:make_logo',do_lang_tempcode('LOGOWIZARD'))));

		require_code('templates_confirm_screen');
		return form_confirm_screen($title,$preview,'__make_logo','make_logo');
	}

	/**
	 * UI for a logo wizard step (set).
	 *
	 * @return tempcode		The UI
	 */
	function __make_logo()
	{
		$title=get_page_title('_LOGOWIZARD',true,array(integer_format(3),integer_format(3)));
		$GLOBALS['HELPER_PANEL_PIC']='pagepics/logowizard';

		$theme=post_param('theme');

		// Do it
		require_code('themes2');
		$rand=uniqid('');
		foreach (array($theme,'default') as $logo_save_theme)
		{
			$path='themes/'.$logo_save_theme.'/images_custom/'.$rand.'.png';
			$img=generate_logo(post_param('name'),post_param('title'),false,$logo_save_theme,'logo-template');
			@imagepng($img,get_custom_file_base().'/'.$path) OR intelligent_write_error($path);
			imagedestroy($img);
			actual_edit_theme_image('logo/-logo',$logo_save_theme,user_lang(),'logo/-logo',$path);
			if (addon_installed('collaboration_zone'))
				actual_edit_theme_image('logo/collaboration-logo',$logo_save_theme,user_lang(),'logo/collaboration-logo',$path);
			$rand=uniqid('');
			$path='themes/'.$logo_save_theme.'/images_custom/'.$rand.'.png';
			$img=generate_logo(post_param('name'),post_param('title'),false,NULL,'trimmed-logo-template');
			@imagepng($img,get_custom_file_base().'/'.$path) OR intelligent_write_error($path);
			imagedestroy($img);
			actual_edit_theme_image('logo/trimmed-logo',$logo_save_theme,user_lang(),'logo/trimmed-logo',$path);
		}
		persistant_cache_delete('THEME_IMAGES');

		breadcrumb_set_parents(array(array('_SELF:_SELF:make_logo',do_lang_tempcode('START'))));

		$message=do_lang_tempcode('LOGOWIZARD_3_DESCRIBE',escape_html($theme));
		return inform_screen($title,$message);
	}

}


