<?php

function cloudfw_form_register($case=NULL) {
	
//if ($case==NULL) return false;

$nonce = $_REQUEST["nonce"];
check_admin_referer();
require_once(TEMPLATEPATH . '/cloudfw/inc/core/core.register.php');
				
/*----------------------------------------------------------------------------------*/
		
switch ($case) {

case "save_options":

	global $message;
	cloudfw_save_options();
	
	if ($_REQUEST['message'])
		$message = $_REQUEST['message'];
	
break;	

				
/*----------------------------------------------------------------------------------*/
				
case PFIX."_add_custom_sidebar":

	$inputs = array(
		'_custom_sidebars_name','_custom_sidebars_desc'
	);
	
	foreach ($inputs as $vars){${PFIX.$vars} = stripslashes($_POST[PFIX.$vars]);}
	global $_opt, $message;
	
		if (!empty($_opt[PFIX."_custom_sidebars"]) && is_array($_opt[PFIX."_custom_sidebars"])) $next_number = count($_opt[PFIX."_custom_sidebars"])+1;else $next_number =1;
		if (empty(${PFIX.'_custom_sidebars_name'})) ${PFIX.'_custom_sidebars_name'} = __('Unnamed Sidebar','cloudfw'). '#'.$next_number ;
	
	$id = sanitize_title_with_dashes(${PFIX.'_custom_sidebars_name'}.'_'.cloudfw_randomizer(10));
	
	${PFIX.'_custom_sidebars'} = $_opt[PFIX."_custom_sidebars"];
	${PFIX.'_custom_sidebars'}[$id] = array('name' =>  ${PFIX.'_custom_sidebars_name'}, 'desc' =>  ${PFIX.'_custom_sidebars_desc'});

	$message = 1005;
	update_option(PFIX.'_custom_sidebars', ${PFIX.'_custom_sidebars'});

break;

/*----------------------------------------------------------------------------------*/

case PFIX."_ga":

	$inputs = array(
	// GA
		'_ga_id', '_ga_pass', '_ga_profile'
	);
	
	foreach ($inputs as $vars){global ${PFIX.$vars}; ${PFIX.$vars} = stripslashes($_POST[PFIX.$vars]);}
	
	//echo ${PFIX.'_ga_id'};exit;
	
	if (!(empty(${PFIX.'_ga_id'})) || (empty(${PFIX.'_ga_pass'}))) {
	
		update_option(PFIX.'_ga_id', ${PFIX.'_ga_id'});
		update_option(PFIX.'_ga_pass', ${PFIX.'_ga_pass'});
	
	};

	
	if (!empty($ga_profile) && empty(${PFIX.'_ga_profile'})) update_option(PFIX.'_ga_profile', ${PFIX.'_ga_profile'});
	
break;

/*----------------------------------------------------------------------------------*/

case "import-data":
	global $message;
	
	if ( ! wp_verify_nonce( $nonce, 'cloudfw') )
		die(__("Failed nonce check and the action aborted.","cloudfw"));

	
	include_once(TMP_ADMIN_PATH.'/classes/class.import.php');	

	$args = array();

	$args['allowedTypes']	= array();
	$args['sizeLimit']		= apply_filters('cloudfw_upload_limit', cloudfw_upload_size());
	$args['type']			= $_POST["type"];

	$importer = new CloudFw_Import( $args );
	$message = $importer->result();

	/*echo '<pre>';
		print_r ($message);
	echo '</pre>';*/

break;

/*----------------------------------------------------------------------------------*/

case "import-zip":
	if (!wp_verify_nonce( $nonce, 'cloudfw')) die(__("Failed nonce check and the action aborted.","cloudfw"));

	delete_transient('cloudfw_icons');
	$accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/s-compressed');
	global $message;
				
	$the_file = CLOUD_UPLOAD_DIR.basename($_FILES['uploadedfile']['name']);
	$temp_file = $_FILES['uploadedfile']['tmp_name'];

	foreach($accepted_types as $mime_type) {
		if($mime_type == $_FILES['uploadedfile']['type']) {
			$okay = true;
			break;
		}
	}
	
	if (!$okay) {$message = 9015; @unlink($temp_file); return false;}

	$target = explode('.', $_FILES['uploadedfile']['name']);
	$target = TEMPLATEPATH.'/lib/icons/'.$target[0].'/';
	mkdir($target);
	
	if(move_uploaded_file($temp_file, $the_file)) {
		
		if (extractZip($the_file, $target)) $message = 9012; else $message = 9014;
		
	} else{
		
		$message = 9005;
	}
	
	@unlink($the_file);

break;

/*----------------------------------------------------------------------------------*/
// DEFAULT SLIDER ITEM ADDING AND UPDATING
case "manage_slider_items":
				
	$id = $_POST["slider_id"];
	$main_slider_id = $_POST["main_slider_id"];
	
	global $_opt, $message, $extra_query;
	$slider_default = cloudfw_get_slider($main_slider_id);
	$main_slider_type = cloudfw_get_slider_type($main_slider_id);
	
		if (!$main_slider_type) exit('ERROR __');
	
	$slider_variables = cloudfw_get_content_maps($main_slider_type);

		if (!is_array($slider_variables)) exit('ERROR __');
	
	foreach ((array)$slider_variables as $vars){$$vars = stripslashes($_POST[$vars]);}

	if (!$id && !is_numeric($id)):
		// Add New Slider Item					
		
		foreach ((array) $slider_variables as $vars){
			$additive_item_vars[$vars] = $$vars;
			}
			
		$additive_item = $additive_item_vars;
		$slider_default ? array_unshift ($slider_default,$additive_item): $slider_default[] = $additive_item;
		
		update_option($main_slider_id, $slider_default);
		
		delete_transient('cloudfw_slider_'.$main_slider_id);
		
		$message = 6001;	
		
		$extra_query = array("slider_id" => 0);
		
		
	else: 
		// Update A Slider Item
		foreach ((array) $slider_variables as $vars){
			$gonnabeupdated_item_vars[$vars] = $$vars;
			}
			
		$gonnabeupdated_item = $gonnabeupdated_item_vars;
		$slider_default[$id] = $gonnabeupdated_item;
	
		update_option($main_slider_id, $slider_default);
		
		delete_transient('cloudfw_slider_'.$main_slider_id);
		
		$message = 6002;
		
	endif;
	
break;
				

/*----------------------------------------------------------------------------------*/
				
// DEFAULT SLIDER ITEM SORTING
case PFIX."_slider_sorting":
	
	
	global $slider_default, $_opt, $message;
	
	$main_slider_id = $_POST["main_slider_id"];
	
	$slider_default = cloudfw_get_slider($main_slider_id);

	
	$ii = 0;
	foreach ((array) $slider_default as $vars => $var){
		
		$sorting_number = $_POST["s_".$ii];
		$delete = ($_POST["d_".$ii]) ? true:false;

		if (!$delete):
		
		$new_slider_default[$sorting_number] = $slider_default[$vars];
		
		
		endif;
		$ii++;
		}
		ksort($new_slider_default);
		
	$i = 0; 	
	foreach ((array) $new_slider_default as $key => $vars){
		$renew_slider_default[$i] = $vars;
		$i++;
		}
		
		update_option($main_slider_id, $renew_slider_default);
		
		$message = 6002;
		delete_transient('cloudfw_slider_'.$main_slider_id);
		
break;

/*----------------------------------------------------------------------------------*/


// ADD SLIDER
case "manage_main_slider":
	global $message, $extra_query;
	
	
	$id = $_POST["slider_id"];
	$slider_type = $id ? cloudfw_get_slider_type($id) : $_POST["slider_type"];
	
	$slider_data = array();
	$variables = cloudfw_get_content_maps('main_'.$slider_type);
	
	if (!is_array($variables)) exit('ERROR');
	
	foreach ((array) $variables as $vars => $var){
		if (!is_array($var))
			$slider_data[$var] = stripslashes($_POST[$var]);
		else
		
			if(isset($_POST[$vars])):
				
				$slider_data[$vars] = stripslashes($_POST[$vars]);
				
			else:
			
				if(isset($var["default"]) && !$id):
					
					$slider_data[$vars] = stripslashes($var["default"]);
					
				elseif($var["type"] == "boolean"):
				
					$slider_data[$vars] = "FALSE";
					
				endif;	
				
				
			endif;
	
						
	}// end foreach
	
	
	if (!$id) {
		// New Slider
		$slider_data['type'] = $slider_type;
		$slider_id = cloudfw_create_slider( $slider_data );
		
		$_POST["comeback"] .= "&msid=".$slider_id."&slider_type=".$slider_type;
		$message = 6004;	
	
	}else {
		// Update Slider
		
		$slider_data['id'] = $id;
		$slider_data['type'] = $slider_type;
		cloudfw_create_slider( $slider_data );
		$message = 6005;	
		
	}
	
	
break;
/*----------------------------------------------------------------------------------*/
		
// ADD SKIN
case PFIX."_add_skin":
	global $message;
	
	
	$id = $_POST["skin_id"];
	$skin_apply = $_POST["skin_apply"];
	
	if (empty($id)) {
		$id = cloudfw_manage_skin_ID();
		$force = TRUE;
		$message = 7001;
	}			
				
		$variables = array( 'skin_name' );				
		foreach ($variables as $var){$$var = stripslashes($_POST[$var]);}

		
		$the_skin = array(
			'mode' 		=> 'custom',
			'name'		=> $skin_name,
			'id' 		=> $id,
			'data' 		=> cloudfw_PV( cloudfw_get_content_maps("skin_map") )
		);

if ($skin_apply == 1) {
	$force = TRUE;
	$message = 7005;
	 }			
		update_option($id, $the_skin);
		cloudfw_sync_skins($id, $force);
		
		if (!isset($message)) $message = 7002;
			
break;		
/*----------------------------------------------------------------------------------*/

// CHANGE SKIN
case PFIX."_colors":
	global $message;
	
	$variables = array(
		PFIX.'_skin'
	 );
	
	foreach ($variables as $var){$$var = stripslashes($_POST[$var]);}
	
	if (!empty(${PFIX.'_skin'})) {
		cloudfw_change_skin(${PFIX.'_skin'});
	}
	$message = 7004;
			
break;

/*----------------------------------------------------------------------------------*/

// CREATE SKIN
case PFIX."_create_skin":
		
	global $message, $_id, $jump_tab;
	
	$variables = array(
		'skin_name'
	 );
	
	foreach ($variables as $var){$$var = stripslashes($_POST[$var]);}

	if (empty($skin_name)) $skin_name = __('Unnamed Skin','cloudfw');
	
	$id = cloudfw_manage_skin_ID();
	
	$the_skin = array(
		'mode'	=>  'custom',
		'id'	=>	$id,
		'name'  =>	$skin_name
	);
	update_option($id , $the_skin);
	delete_transient(PFIX.'_cloudfw_skin');
	
	$message = 7001;
	$_id = $id;
	$jump_tab = 1;
			
break;
/*----------------------------------------------------------------------------------*/

// LOAD FONTS
case PFIX."_font_settings":
	
	global $message;
	
	$variables = array(
		 PFIX.'_loaded_fonts'
	 );
	
	foreach ($variables as $var){$$var = $_POST[$var];}
		
	update_option(PFIX.'_fonts', ${PFIX.'_loaded_fonts'});
	
	$message = 8010;
			
break;

/*----------------------------------------------------------------------------------*/

// LOAD FONTS
case PFIX."_font_settings_customize":
		
	global $message;
	
	$map = cloudfw_get_content_maps("font_map");
	$results = cloudfw_PV( $map );
	$results = cloudfw_prepare_skin_data( $map, $results );
	
/*echo '<pre>';
 print_r($results);
echo '</pre>';
exit;*/
	update_option(PFIX.'_font_engine', $results);
	delete_transient(PFIX.'_cloudfw_skin');
	
	$message = 8011;
	return false;
			
break;
	}

};

/*----------------------------------------------------------------------------------*/ ?>