<?php
/*
  osCommRes, Services Online
  http://www.oscommres.com

  Copyright (c) 2005 osCommRes

  Released under the GNU General Public License
  Freeway eCommerce from ZacWare
http://www.zac-ware.com/freeway

Copyright 2007 ZacWare Pty. Ltd 

*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );
  require('includes/application_top.php');
  require(DIR_WS_CLASSES . '/currencies.php');
  $currencies=new currencies;

  // action - course of action to move or display page contents,services_id - source service where the survey is moved
  // template -type indicates email templates or sms templates

	$action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');
	$service_id = (isset($HTTP_GET_VARS['suID']) ?(int)$HTTP_GET_VARS['suID'] : 0);

	$templates=getTemplates($service_id,$template_type);
	// get the action to update
	if ($action=="update"){
		$survey_keys=&$HTTP_POST_VARS["select_survey"];
		$service_keys=&$HTTP_POST_VARS["select_service"];
		
	//	if (sizeof($survey_keys)<=0 || sizeof($service_keys)<=0 || $service_id<0)
	//		tep_redirect(tep_href_link(FILENAME_SERVICES_SURVEYS,'service_id=' . $service_id));

		// fetch services into total array to find and service has already survey
		$status=array();
		reset($service_keys);
		for ($icnt=0;$icnt<sizeof($service_keys);$icnt++){
			$status[$service_keys[$icnt]]=array();
		}
		//get the input survey_ids,service_ids
		$survey_string=join("','",$survey_keys);
		$service_string=join(",",$service_keys);

		$survey_query=tep_db_query("SELECT  * from " . TABLE_SERVICES_SURVEYS . " where service_id='" . $service_id . "' and survey_type in('" . $survey_string . "')");
		$survey_details=array();
		while($survey_result=tep_db_fetch_array($survey_query)){
			$survey_id=$survey_result["survey_id"];
			unset($survey_result["survey_id"]);
			$survey_details[$survey_id]=array('details'=>$survey_result,'questions'=>array());
			$question_query=tep_db_query("SELECT * from " . TABLE_SERVICES_SURVEYS_QUESTION . " where survey_id='" . $survey_id . "'");
			while($question_result=tep_db_fetch_array($question_query)){
				$question_id=$question_result["question_id"];
				unset($question_result["question_id"]);
				$survey_details[$survey_id]['questions'][$question_id]=array('details'=>$question_result,'choices'=>array());
				if ($question_result["question_type"]<=3){
					$choice_query=tep_db_query("SELECT * from " . TABLE_SERVICES_SURVEYS_CHOICE . " where question_id='" . $question_id . "'");
					while($choice_result=tep_db_fetch_array($choice_query)){
						$choice_id=$choice_result["choice_id"];
						unset($choice_result["choice_id"]);
						$survey_details[$survey_id]['questions'][$question_id]["choices"][$choice_id]=$choice_result;
					}
				}
			}
		}
		// get the survey status for services we have to copy
		$service_status_query=tep_db_query("SELECT count(*) as total,survey_type,service_id,survey_id from " . TABLE_SERVICES_SURVEYS . " where service_id in(" . $service_string . ") and survey_type in('" . $survey_string . "') group by service_id,survey_type");
		
		while($service_status=tep_db_fetch_array($service_status_query)){
			if (isset($status[$service_status["service_id"]])){
				$status[$service_status["service_id"]][$service_status["survey_type"]]['count']=$service_status["total"];
				$status[$service_status["service_id"]][$service_status["survey_type"]]['survey_id']=$service_status["survey_id"];
			}
		}
		$copy_details=array();
		reset($status);
		//iterate with the services we have to find insert or update
		if (sizeof($status)>0 && sizeof($survey_details)>0){
			while(list($ekey,)=each($status)){
				reset($survey_details);
				while(list($survey_id,)=each($survey_details)){
					$survey=$survey_details[$survey_id]['details'];
					$count=isset($status[$ekey][$survey["survey_type"]]["count"])?$status[$ekey][$survey["survey_type"]]["count"]:0;
					if ($count>0){ // update operation
						tep_delete_survey($status[$ekey][$survey["survey_type"]]["survey_id"]);
						$status[$ekey][$survey["survey_type"]]["count"]=0;
					}
					$survey["service_id"]=$ekey;
					tep_db_perform(TABLE_SERVICES_SURVEYS,$survey);
					$new_survey_id=tep_db_insert_id();
					if (sizeof($survey_details[$survey_id]['questions'])>0){
						reset($survey_details[$survey_id]['questions']);
						while(list($question_id,)=each($survey_details[$survey_id]['questions'])){
							$question=$survey_details[$survey_id]['questions'][$question_id]['details'];
							$question["survey_id"]=$new_survey_id;
							tep_db_perform(TABLE_SERVICES_SURVEYS_QUESTION,$question);
							$new_question_id=tep_db_insert_id();
							if ($question["question_type"]<=3 && sizeof($survey_details[$survey_id]['questions'][$question_id]['choices'])>0){
								reset($survey_details[$survey_id]['questions'][$question_id]["choices"]);
								while(list($choice_id,)=each($survey_details[$survey_id]['questions'][$question_id]['choices'])){
									$choice=$survey_details[$survey_id]['questions'][$question_id]['choices'][$choice_id];
									$choice["question_id"]=$new_question_id;
									tep_db_perform(TABLE_SERVICES_SURVEYS_CHOICE,$choice);
								}
							}
						}
					}
				}
			}
		}
	}
	// get the message list
	$type_params="";
	reset($templates);
	while(list($key,)=each($templates)){
		if ($type_params=="") $type_params.=" and survey_type in(";
		$type_params.="'" . $key . "',";
	}
	$type_params=substr($type_params,0,-1) . ")";
	$survey_query=tep_db_query("SELECT count(*) as total,survey_type from " . TABLE_SERVICES_SURVEYS . " where service_id='" . $service_id . "' " . $type_params . " group by survey_type");
	while($survey=tep_db_fetch_array($survey_query)){
		if ((int)$survey["total"]>0 && isset($templates[$survey['survey_type']])){
			$templates[$survey['survey_type']]['status']=1;
			$templates[$survey['survey_type']]['color']='#0000FF';
		}
	}
	$service_array=array();
	$service_query=tep_db_query("SELECT se.service_id,sed.service_name from " . TABLE_SERVICES . " se, " . TABLE_SERVICES_DESCRIPTION . " sed where se.service_id=sed.service_id " . SERVICES_CONDITION_STATUS . " order by sed.service_name");
	while($service_result=tep_db_fetch_array($service_query)){
		if ($service_id!=$service_result["service_id"])
			$service_array[$service_result['service_id']]=array('text'=>$service_result['service_name'],'color'=>"#000000");
	}
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script language="javascript" src="includes/menu.js"></script>
<script language="javascript" src="includes/general.js"></script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF" onLoad="SetFocus();">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
	<tr> 
		<!-- body_text //-->
		<td width="100%" valign="top">
		<table border="0" width="100%" cellspacing="0" cellpadding="2">
		<?php if ($action=="") { ?>
			<script language=javascript>
				function validateForm(){
					var icnt;
					var listbox;
					var error_result=Array();
					var listbox_name=Array("select_survey[]","select_service[]");
					error_result[0]="<?php echo stripslashes(addslashes(ERR_SELECT_SURVEY));?>";
					error_result[1]="<?php echo stripslashes(addslashes(ERR_SELECT_SERVICE));?>";
					for(ecnt=0;ecnt<listbox_name.length;ecnt++){
						listbox=document.copy_survey.elements[listbox_name[ecnt]];
						if (listbox==null) break;
						if (listbox.length){
							for(icnt=0;icnt<listbox.length;icnt++){
								if (listbox[icnt].checked){
									 error_result[ecnt]="";
									 break;
								}
							}
						} else {
							if (listbox.checked) error_result[ecnt]="";
						}
					}
					if (error_result[0]!=""){
						alert(error_result[0]);	
						return false;
					}
					if (error_result[1]!=""){
						alert(error_result[1]);
						return false;
					}
					return true;
				}
			</script>		
			<?php echo tep_draw_form("copy_survey",FILENAME_SERVICES_COPY_SURVEYS,'action=update&suID=' . $service_id,"post",'onSubmit="javascript:return validateForm();"');
			?>
			<!--<tr>
				<td class="pageHeading"><?php //echo sprintf(HEADING_TITLE,tep_get_service_name($service_id));?></td>
			</tr>!-->
			<tr>
				<td><?php echo tep_draw_separator('pixel_trans.gif','1','5');?></td>
			</tr>
			<tr>
				<td>
				<table border="0" cellpadding="0" cellspacing="0">
					<tr>
						<td>
							<table border="0" cellpadding="2" cellspacing="5">
								<tr class="attributes-even">
									<td class="tableHeading"><?php echo SELECT_MESSAGES;?></td>
									<td class="tableHeading"><?php echo SELECT_SERVICES;?></td>
								</tr>
								<tr>
									<td class="tableHeading"><?php echo customListBox("select_survey",$templates,0,300,200);?></td>
									<td class="tableHeading"><?php echo customListBox("select_service",$service_array,0,500,200);?></td>
								</tr>
							</table>
						</td>
					</tr>
					<tr>
						<td><?php echo tep_draw_separator('pixel_trans.gif','1','5');?></td>
					</tr>
					<tr>
						<td>
							<table border="0" cellpadding="2" cellspacing="5" width="100%">
								<tr>
									<td class="main" align="left"><?php echo '<a href="' . tep_href_link(FILENAME_SERVICES_SURVEYS,'service_id=' . $service_id) . '">' . tep_image_button("button_back.gif",IMAGE_BACK) .'</a>';?></td>
									<td class="main" align="right"><?php echo tep_image_submit("button_copy_surveys.gif",IMAGE_COPY_SURVEYS);?></td>
								</tr>
							</table>
						</td>
					</tr>
				</table>
				</td>
			</tr>
			</form>
		<?php }?>
		</table>
		</td>
	</tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>


<?php  // function used in this page

	function &getTemplates($events_id,$type){
		$templates=array(	'RV'=>array('text'=>TEXT_RV,'status'=>0,'color'=>'#FF0000'),
							'WV'=>array('text'=>TEXT_WV,'status'=>0,'color'=>'#FF0000'),
							'FV'=>array('text'=>TEXT_FV,'status'=>0,'color'=>'#FF0000'),
							);
		return $templates;
	}
	function customListBox($name,&$list,$selected="",$width=300,$height=100){
	$result="";
		$result.='<div id="' . $name . '_box" style="background-color: #FFFFFF;border-color: #7b9ebd; border-style: solid; border-width: 1px;overflow:auto;width:' . $width . ';height:' . $height . '">' . "\n";
		$result.='<table border="0" cellspacing="0" cellpadding="2" width="100%" valign="middle" align="center" class="BoxTable">' . "\n";
		reset($list);

		$selected=array();
		if (isset($HTTP_POST_VARS[$name]) && is_array($HTTP_POST_VARS[$name])){
			$selected=&$HTTP_POST_VARS[$name];
		}
		while(list($key,)=each($list)){
			$result.='<tr class="attributes-even"><td class="main" width="20">' . "\n";
			$result.='<input type="checkbox" name="' . $name . '[]" value="' . $key . '" ' . (in_array($key,$selected)?"checked":"") . ' >';
			$result.='<td class="main"><font color="' . $list[$key]['color'] . '">' . $list[$key]['text'] .'</font></td></tr>' ."\n";
		}
		$result.='</table></div>' . "\n";
		echo $result;
	}
?>