<?php

/*

 osCommRes, Services Oline 
http://www.oscommres.com 
Copyright (c) 2005 osCommRes 

Freeway eCommerce 
http://www.zac-ware.com/freeway 
Copyright (c) 2007 ZacWare

Released under the GNU General Public License 
*/
// 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,events_id - source event where the survey is moved
  // template -type indicates email templates or sms templates

	$action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');
	$events_id = (isset($HTTP_GET_VARS['eID']) ?(int)$HTTP_GET_VARS['eID'] : 0);

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

		// fetch events into total array to find and event has already survey
		$status=array();
		reset($events_keys);
		for ($icnt=0;$icnt<sizeof($events_keys);$icnt++){
			$status[$events_keys[$icnt]]=array();
		}
		//get the input survey_ids,events_ids
		$survey_string=join("','",$survey_keys);
		$events_string=join(",",$events_keys);

		$survey_query=tep_db_query("SELECT  * from " . TABLE_EVENTS_SURVEYS . " where events_id='" . $events_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_EVENTS_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_EVENTS_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 events we have to copy
		$event_status_query=tep_db_query("SELECT count(*) as total,survey_type,events_id,survey_id from " . TABLE_EVENTS_SURVEYS . " where events_id in(" . $events_string . ") and survey_type in('" . $survey_string . "') group by events_id,survey_type");
		
		while($event_status=tep_db_fetch_array($event_status_query)){
			if (isset($status[$event_status["events_id"]])){
				$status[$event_status["events_id"]][$event_status["survey_type"]]['count']=$event_status["total"];
				$status[$event_status["events_id"]][$event_status["survey_type"]]['survey_id']=$event_status["survey_id"];
			}
		}
		$copy_details=array();
		reset($status);
		//iterate with the events 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["events_id"]=$ekey;
					tep_db_perform(TABLE_EVENTS_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_EVENTS_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_EVENTS_SURVEYS_CHOICE,$choice);
								}
							}
						}
					}
				}
			}
		}

		tep_redirect(tep_href_link(FILENAME_EVENTS_SURVEYS,'events_id=' . $events_id));
	}
	// 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_EVENTS_SURVEYS . " where events_id='" . $events_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';
		}
	}
	$events_array=array();
	$event_query=tep_db_query("SELECT e.events_id,ed.events_name from " . TABLE_EVENTS . " e, " . TABLE_EVENTS_DESCRIPTION . " ed where e.events_id=ed.events_id " . EVENTS_CONDITION_STATUS . " order by ed.events_name");
	while($event_result=tep_db_fetch_array($event_query)){
		if ($events_id!=$event_result["events_id"])
			$events_array[$event_result['events_id']]=array('text'=>$event_result['events_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_event[]");
					error_result[0]="<?php echo stripslashes(addslashes(ERR_SELECT_SURVEY));?>";
					error_result[1]="<?php echo stripslashes(addslashes(ERR_SELECT_EVENT));?>";
					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_EVENTS_COPY_SURVEYS,'action=update&eID=' . $events_id,"post",'onSubmit="javascript:return validateForm();"');?>
			<tr>
				<td class="pageHeading"><?php echo sprintf(HEADING_TITLE,tep_get_events_name($events_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_EVENTS;?></td>
								</tr>
								<tr>
									<td class="tableHeading"><?php echo customListBox("select_survey",$templates,0,300,200);?></td>
									<td class="tableHeading"><?php echo customListBox("select_event",$events_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_EVENTS_SURVEYS,'events_id=' . $events_id) . '"</a>' . tep_image_button("button_back.gif",IMAGE_BACK);?></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;
	}
?>