<?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');
  
  $action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');
  if (!isset($HTTP_GET_VARS['suID']))
     tep_redirect(tep_href_link(FILENAME_EVENTS_SURVEYS));


  switch ($action) {
   case 'pos_up':
   case 'pos_down':
        $change_id=(isset($HTTP_GET_VARS['suID'])?(int)$HTTP_GET_VARS['suID']:0);
	$pos=(isset($HTTP_GET_VARS['pos'])?(int)$HTTP_GET_VARS['pos']:0);
	if ($pos>0){
         if ($action=='pos_up')
          $question_query=tep_db_query("SELECT sort_order,question_id from " . TABLE_EVENTS_SURVEYS_QUESTION . " where survey_id='" . (int)$HTTP_GET_VARS['suID'] . "' and sort_order<" . $pos . " order by sort_order desc limit 1");
	 else
	  $question_query=tep_db_query("SELECT sort_order,question_id from " . TABLE_EVENTS_SURVEYS_QUESTION . " where survey_id='" . (int)$HTTP_GET_VARS['suID'] . "' and sort_order>" . $pos . " order by sort_order limit 1");
         if (tep_db_num_rows($question_query)>0){
	  $question_result=tep_db_fetch_array($question_query);
	  $prev_order=$question_result['sort_order'];
	  tep_db_query("UPDATE " . TABLE_EVENTS_SURVEYS_QUESTION . " set sort_order='" . $pos ."' where question_id='" . (int)$question_result['question_id'] . "'");
	  tep_db_query("UPDATE " . TABLE_EVENTS_SURVEYS_QUESTION . " set sort_order='" . $prev_order . "' where question_id='" . (int)$HTTP_GET_VARS['quID'] ."'");
	 }
  }
        tep_redirect(tep_href_link(FILENAME_EVENTS_SURVEYS_EDIT, 'suID=' . $HTTP_GET_VARS['suID']));
   case 'purge':
        $delete_id=(isset($HTTP_GET_VARS['suID'])?(int)$HTTP_GET_VARS['suID']:0);
        $question_query=tep_db_query("SELECT question_id from " . TABLE_EVENTS_SURVEYS_QUESTION . " where survey_id='" . (int)$delete_id . "'");
	$question_ids='';
	while($question_result=tep_db_fetch_array($question_query)){
	    if ($question_ids!='') $question_ids .= ',';
	    $question_ids .= "'" . $question_result['question_id'] . "'";
	}
	if ($question_ids!=''){
	   tep_db_query("DELETE from " . TABLE_EVENTS_SURVEYS_CHOICE . " where question_id in(" . $question_ids . ")");
	   tep_db_query("DELETE from " . TABLE_EVENTS_SURVEYS_QUESTION . " where question_id in(" . $question_ids . ")");
	}
       tep_redirect(tep_href_link(FILENAME_EVENTS_SURVEYS_EDIT, 'suID=' . $delete_id));
        break;
   case 'update_survey':
        $update_id=(isset($HTTP_GET_VARS['suID'])?(int)$HTTP_GET_VARS['suID']:0);
        if (array_key_exists("survey_visibility_flag", $HTTP_POST_VARS))
	   $status = "1";
	else
	   $status = "0";

        $sql_array=array('survey_name'=>tep_db_prepare_input($HTTP_POST_VARS['survey_name']),
	                 'survey_desc'=>tep_db_prepare_input($HTTP_POST_VARS['survey_desc']),
			 'survey_time_period'=>tep_db_prepare_input($HTTP_POST_VARS['survey_time_period']),
			 'survey_expiration_date'=>tep_db_prepare_input($HTTP_POST_VARS['survey_expiration_date']),
			 'survey_visibility_flag'=>tep_db_prepare_input($status));

	tep_db_perform(TABLE_EVENTS_SURVEYS,$sql_array,'update','survey_id="' . $update_id . '"');

        tep_redirect(tep_href_link(FILENAME_EVENTS_SURVEYS_EDIT, 'suID=' . $HTTP_GET_VARS['suID']));
        break;
  case 'add_question' :
        $suID=(isset($HTTP_GET_VARS['suID'])?(int)$HTTP_GET_VARS['suID']:0);
        if (array_key_exists("question_required", $HTTP_POST_VARS))
	   $required = "1";
	else
	   $required = "0";
	$question_type=$HTTP_POST_VARS['question_type'];
	$question_required=($question_type!=6?$required:0);
        $sql_array=array('question_text'=>tep_db_prepare_input($HTTP_POST_VARS['question_text']),
	                 'question_type'=>tep_db_prepare_input($question_type),
			 'question_required'=>tep_db_prepare_input($question_required),
			 'survey_id'=>tep_db_prepare_input($suID));
        
	if (isset($HTTP_POST_VARS['question_id'])){
	    tep_db_perform(TABLE_EVENTS_SURVEYS_QUESTION,$sql_array,'update','question_id="' . $HTTP_POST_VARS['question_id'] . '"');
	    if (!($question_type>=1 && $question_type<=3)){
	       tep_db_query('DELETE from ' . TABLE_EVENTS_SURVEYS_CHOICE . ' where question_id="' . $HTTP_POST_VARS['question_id'] . '"');
	    }
	}    
	else
	{
   	    $order_check=tep_db_query("SELECT max(sort_order) as last_id from " . TABLE_EVENTS_SURVEYS_QUESTION . " where survey_id='" . (int)$suID ."'");
	    $order_result=tep_db_fetch_array($order_check);
	    $sort_order=((int)$order_result['last_id'])+1;
	    $sql_array['sort_order']=tep_db_prepare_input($sort_order);
	    tep_db_perform(TABLE_EVENTS_SURVEYS_QUESTION,$sql_array);
	}

        tep_redirect(tep_href_link(FILENAME_EVENTS_SURVEYS_EDIT, 'suID=' . (int)$HTTP_GET_VARS['suID']));
	break;
  case 'delete_question' :
        $quID=(isset($HTTP_GET_VARS['quID'])?(int)$HTTP_GET_VARS['quID']:0);
	tep_db_query("DELETE FROM " . TABLE_EVENTS_SURVEYS_CHOICE . " where question_id='" . (int)$quID . "'");
	tep_db_query("DELETE FROM " . TABLE_EVENTS_SURVEYS_QUESTION . " where question_id='" . (int)$quID . "'");
	tep_db_query("DELETE FROM " . TABLE_EVENTS_SURVEYS_ANSWER . " where question_id='" . (int)$quID . "'");
	tep_db_query("DELETE FROM " . TABLE_EVENTS_SURVEYS_TEXT . " where question_id='" . (int)$quID . "'");
    tep_redirect(tep_href_link(FILENAME_EVENTS_SURVEYS_EDIT, 'suID=' . $HTTP_GET_VARS['suID']));
	break;
  case 'add_choice' :
        $quID=(isset($HTTP_GET_VARS['quID'])?(int)$HTTP_GET_VARS['quID']:0);
	if ($quID>0){
        $sql_array=array('choice_text'=>tep_db_prepare_input($HTTP_POST_VARS['choice_text']),
	                 'question_id'=>tep_db_prepare_input($quID));
        tep_db_perform(TABLE_EVENTS_SURVEYS_CHOICE,$sql_array);

	}
        tep_redirect(tep_href_link(FILENAME_EVENTS_SURVEYS_EDIT, 'suID=' . (int)$HTTP_GET_VARS['suID']));
	break;
  case 'delete_choice' :
        $chID=(isset($HTTP_GET_VARS['chID'])?(int)$HTTP_GET_VARS['chID']:0);
	tep_db_query("DELETE FROM " . TABLE_EVENTS_SURVEYS_CHOICE . " where choice_id='" . (int)$chID . "'");
	tep_db_query("DELETE FROM " . TABLE_EVENTS_SURVEYS_ANSWER . " where choice_id='" . (int)$chID . "'");
    tep_redirect(tep_href_link(FILENAME_EVENTS_SURVEYS_EDIT, 'suID=' . (int)$HTTP_GET_VARS['suID']));
	break;
  } 

    
    $survey_query = tep_db_query("SELECT * from " . TABLE_EVENTS_SURVEYS . " where survey_id='" . (int)$HTTP_GET_VARS["suID"] . "'");
    $survey = tep_db_fetch_array($survey_query);
 ?>
<!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>
<script language="JavaScript1.3" type="text/javascript">
<!--
function validateFormQuestion( theForm ) {
	if ( theForm.question_text.value == "" ) {
		alert( "<?php echo tep_db_input(ERR_QUESTION_TEXT); ?>" );
		theForm.question_text.focus();
		return ( false );
	}
	return ( true );
}
function validateFormChoice( theForm ) {
	if ( theForm.choice_text.value == "" ) {
		alert( "<?php echo tep_db_input(ERR_CHOICE_TEXT); ?>" );
		theForm.choice_text.focus();
		return ( false );
	}
	return ( true );
}
function validateFormSettings( theForm ) {
    if ( theForm.survey_name.value == "" ){
		alert( "<?php echo tep_db_input(ERR_SURVEY_NAME); ?>" );
		theForm.survey_name.focus(); 
        return ( false );
	}    
    if ( theForm.survey_time_period.value < 1 || theForm.survey_time_period.value > 12 ){
		alert( "<?php echo tep_db_input(ERR_SURVEY_LIMIT); ?>" );
		theForm.survey_time_period.focus(); 
        return ( false );
	}
	return ( true );
}
-->
</script>
<link rel="stylesheet" type="text/css" href="includes/javascript/spiffyCal/spiffyCal_v2_1.css">
<script language="JavaScript" src="includes/javascript/spiffyCal/spiffyCal_v2_1.js"></script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<div id="spiffycalendar" class="text"></div>
<!-- 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% align=left valign="top">
    <table border="0" width="100%" cellspacing="0" cellpadding="0">

      <!--<tr><td class="pageHeading"><?php //echo TEXT_HEADING_EDIT_SURVEY; ?></td>
      </tr>!-->
      <tr><td><?php echo tep_draw_separator('pixel_trans.gif','1','15'); ?></td></tr>
      <tr>
        <td width=50% valign=top><table border=0 cellspacing="0" cellpadding="2" width=100% valign=top>
		<?php tep_content_title_top(TEXT_ADD_QUESTION);?>
           <tr>
              <td><table border="0" cellpadding="2" width="100%" >
	          <?php 
		     $question_edit_array=array('question_text'=>'',
		                                'question_type'=>'1',
						'question_required'=>'1');
		     $question_action='insert';
		     $qInfo=new objectInfo($question_edit_array);
		     if (isset($HTTP_GET_VARS['quID'])){
		       $question_edit_query=tep_db_query("SELECT question_text,question_type,question_required,sort_order from " . TABLE_EVENTS_SURVEYS_QUESTION . " where question_id='" . (int)$HTTP_GET_VARS['quID'] ."'");
		       $question_edit_result=tep_db_fetch_array($question_edit_query);
		       $qInfo=new objectInfo($question_edit_result);
		       $question_action='update';
		     }
	             echo tep_draw_form('add_question',FILENAME_EVENTS_SURVEYS_EDIT,'action=add_question&suID=' . $HTTP_GET_VARS['suID'],'post','onSubmit="return validateFormQuestion(this)"');
		  ?>
	             <tr><td class="main" width=100><?php echo TEXT_QUESTION;?></td>
		         <td class="main"><?php echo tep_draw_input_field('question_text',$qInfo->question_text,'size=40');?></td> 
                     </tr>
                     <tr>
 	                 <td class="main"><?php echo TEXT_QUESTION_TYPE;?></td>
		         <td class="main">
			 <?php $choice_array=array(array('id'=>'1','text'=>TEXT_MULTIPLE_RADIO),
			                           array('id'=>'2','text'=>TEXT_MULTIPLE_PULLDOWN),
						   array('id'=>'3','text'=>TEXT_MULTIPLE_CHECKBOX),
						   array('id'=>'4','text'=>TEXT_INPUT_ONELINE),
						   array('id'=>'5','text'=>TEXT_INPUT_THREELINES),
						   array('id'=>'6','text'=>TEXT_INPUT_HEADING),
						   array('id'=>'7','text'=>TEXT_INPUT_DATE));
			       echo tep_draw_pull_down_menu('question_type',$choice_array,$qInfo->question_type); ?></td>
                     </tr>
		     <?php //echo TEXT_QUESTION_SORT_ORDER;?>
		     <?php //echo tep_draw_input_field('sort_order',$qInfo->sort_order,'size=6');?>
                     <tr>
 	                <td class="main"></td>
		        <td class="main"><?php echo tep_draw_checkbox_field('question_required',1,($qInfo->question_required==1?true:false)) . '&nbsp;' . TEXT_QUESTION_REQUIRE;?></td>
		     </tr>
                     <tr><td><?php echo tep_draw_separator('pixel_trans.gif','1','5'); ?></td></tr>
		     <tr>
	                <td class="main" colspan="2" align=right>
			<?php 
			     if ($question_action=='insert'){
			       echo tep_image_submit('button_add_question.gif',IMAGE_QUESTION_ADD); 
			     } else {
			       echo tep_draw_hidden_field('question_id',$HTTP_GET_VARS['quID']);
			       echo tep_image_submit('button_update_question.gif',IMAGE_QUESTION_UPDATE) . '&nbsp;';
                               echo '<a href="' . tep_href_link(FILENAME_EVENTS_SURVEYS_EDIT,'suID=' .$HTTP_GET_VARS['suID']) . '">' . tep_image_button('button_cancel.gif',IMAGE_CANCEL) . '</a>';
			     }
			?>
		     </tr>
		     <?php echo '</form>';?>
		  </table></td>
	   </tr>
           <tr><td><?php echo tep_draw_separator('pixel_trans.gif','1','15'); ?></td></tr>
		   <?php tep_content_title_bottom();?>
		   <?php tep_content_title_top(TEXT_SURVEY_SETTINGS);?>
           <tr>
              <td><table border="0" cellpadding="2" width="100%" >
	           <?php echo tep_draw_form('update_survey',FILENAME_EVENTS_SURVEYS_EDIT,'action=update_survey&suID=' . $HTTP_GET_VARS['suID'],'post','onSubmit="return validateFormSettings(this);"');?>
                     <tr>   
                        <td class="main" width=100><?php echo TEXT_SURVEY_TITLE;?></td>
                        <td class="main"><?php echo tep_draw_input_field('survey_name',$survey['survey_name'],'size=40'); ?></td>
		     </tr>
                     <tr valign="top">   
                        <td class="main"><?php echo TEXT_SURVEY_DESCRIPTION;?></td>
                        <td class="main"><?php echo tep_draw_textarea_field('survey_desc','7','40','5',$survey['survey_desc']); ?></td>
                     </tr>
<script language="javascript"><!--
  var dateExpires = new ctlSpiffyCalendarBox("dateExpires", "update_survey", "survey_expiration_date","btnDate1","<?php echo $survey['survey_expiration_date'] ?>",scBTNMODE_CUSTOMBLUE);
//--></script>
                     <tr>   
                        <td class="main"><?php echo TEXT_SURVEY_EXPIRES; ?><br><small>(YYYY-MM-DD)</small></td>
                        <td class="main"><script language="javascript">dateExpires.writeControl(); dateExpires.dateFormat="yyyy-MM-dd";</script></td>
                     </tr>
                     <tr>
	                <td class="main"><?php echo TEXT_SURVEY_LIMIT; ?></td>
			<td class="main"><?php echo tep_draw_input_field('survey_time_period',$survey['survey_time_period'],'size=6') . '&nbsp;' . TEXT_MONTHS; ?></td>
                     </tr>
                     <tr> 
		        <td></td>  
                        <td class="main">
			<?php  echo tep_draw_checkbox_field('survey_visibility_flag',1,$survey['survey_visibility_flag']==1?true:false);
			       echo '&nbsp;' . TEXT_SURVEY_VISIBLE; ?></td>
                     </tr>
                     <tr><td><?php echo tep_draw_separator('pixel_trans.gif','1','15'); ?></td></tr>
		     <tr><td class="main" colspan="2" align=right>
		         <?php echo '<a href="' . tep_href_link(FILENAME_EVENTS_SURVEYS_EDIT,'action=purge&suID=' . $HTTP_GET_VARS['suID']) . '">' . tep_image_button('button_survey_purge.gif',IMAGE_SURVEY_PURGE) . '</a>';
			       echo '&nbsp;' . tep_image_submit('button_update.gif',IMAGE_SURVEY_UPDATE);
                               echo '&nbsp;<a href="' . tep_href_link(FILENAME_EVENTS_SURVEYS,'events_id=' . $survey['events_id']) . '">' . tep_image_button('button_back.gif',IMAGE_SURVEY_BACK) . '</a>';
                         ?></td>
	             <?php echo '</form>';?>
		     </tr></table>
	       </td>
	   </tr>
	   		<?php tep_content_title_bottom();?>
	   </table>
      </td>
      <td width=50% valign=top><table border=0 cellspacing="0" cellpadding="2" valign=top width=100%>
	   <tr>
	      <td><table border="0" cellpadding="0" width="100%" >
                    <tr height=20><td class="main" align=center><b><?php echo TEXT_SURVEY_QUESTIONS; ?></b></td></tr>
	           </table>
	      </td>
           </tr>
	   <?php $question_query=tep_db_query("SELECT * from " . TABLE_EVENTS_SURVEYS_QUESTION . " where survey_id='" . (int)$HTTP_GET_VARS['suID'] . "' order by sort_order,question_text");
             $id=1;
             $choice_array=array('1'=>TEXT_MULTIPLE_RADIO,
				 '2'=>TEXT_MULTIPLE_PULLDOWN,
				 '3'=>TEXT_MULTIPLE_CHECKBOX,
				 '4'=>TEXT_INPUT_ONELINE,
				 '5'=>TEXT_INPUT_THREELINES,
				 '6'=>TEXT_INPUT_HEADING,
				 '7'=>TEXT_INPUT_DATE);
	     echo '<ol>';
	     $qu_count=tep_db_num_rows($question_query);
	     if ($qu_count>0){
             while($question_result=tep_db_fetch_array($question_query)){
	   ?>
	     <tr><td><table border="0" cellpadding="2" width="100%" >
	              <tr>
		         <td colspan="2"><table border="0" cellpadding="2" cellspacing="0" width=100%>
	                   <tr valign=top >
			       <td class="main"><?php echo /*'<a href="' . tep_href_link(FILENAME_EVENTS_SURVEYS_EDIT,'action=delete_question&suID=' . (int)$HTTP_GET_VARS['suID'] . '&quID=' . $question_result['question_id']) . '">' . tep_image(DIR_WS_ICONS . '/cross.gif',IMAGE_QUESTION_DELETE) . '</a>' .*/ $id . '. ' ;?></td>
				<td class="main"><?php echo $question_result['question_text'] . '<br>';
		                                 echo '<i><small>' . $choice_array[$question_result['question_type']] . ($question_result['question_required']==1?', ' . TEXT_REQUIRED:'') . '</small></i>';
					    ?>
			       </td>
			       <td align=center valign=middle width=140>
			          <?php echo '<a href="' . tep_href_link(FILENAME_EVENTS_SURVEYS_EDIT,'suID=' . (int)$HTTP_GET_VARS['suID'] . '&quID=' . $question_result['question_id']) . '">' . tep_image_button('button_edit.gif',IMAGE_QUESTION_EDIT,'align=absmiddle') . '</a>&nbsp;';
				        echo '<a href="' . tep_href_link(FILENAME_EVENTS_SURVEYS_EDIT,'action=delete_question&suID=' . (int)$HTTP_GET_VARS['suID'] . '&quID=' . $question_result['question_id']) . '">' . tep_image_button('button_delete.gif',IMAGE_QUESTION_DELETE,'align=absmiddle') . '</a>';
				  ?></td>
			       <td width=16 valign=middle>
			          <?php if ($id>1){
				        echo '<a href="' . tep_href_link(FILENAME_EVENTS_SURVEYS_EDIT,'action=pos_up&pos=' . (int)$question_result['sort_order'] . '&suID=' . (int)$HTTP_GET_VARS['suID'] . '&quID=' . $question_result['question_id']) . '">' . tep_image(DIR_WS_ICONS . '/order_up.gif',IMAGE_ORDER_UP,'16','16') . '</a>';
			               } ?></td>
		           </tr></table>
			  </td></tr>
                      <?php if ($question_result['question_type']>=1 && $question_result['question_type']<=3) { ?>
		      <tr><td><?php echo tep_draw_separator('pixel_trans.gif','10','1'); ?></td>
		         <td><table border="0" cellpadding="2" cellspacing="0" valign=top>
			     <tr><td class="main" colspan=2><u><?php echo TEXT_CHOICES; ?></u></td></tr>
			 <?php $choice_query=tep_db_query("SELECT choice_id,choice_text from " . TABLE_EVENTS_SURVEYS_CHOICE . " where question_id='" . (int)$question_result['question_id'] . "'order by choice_id");
			       $qid=1;
			       while($choice_result=tep_db_fetch_array($choice_query)){
			 ?>
		                 <tr valign=middle>
				   <td class="main"><?php echo $qid . '. ';?>
				   <td class="main">
				   <?php echo $choice_result['choice_text'];
				         echo '&nbsp;<a href="' . tep_href_link(FILENAME_EVENTS_SURVEYS_EDIT,'action=delete_choice&suID=' . (int)$HTTP_GET_VARS['suID'] . '&chID=' . $choice_result['choice_id']) . '">' . tep_image(DIR_WS_ICONS . '/cross.gif',IMAGE_QUESTION_DELETE,13,13,'align=absmiddle') . '</a>';
			           ?></td>
				 </tr>
			 <?php $qid++;} 
			         echo tep_draw_form('add_choice_' . $question_result['question_id'],FILENAME_EVENTS_SURVEYS_EDIT,'action=add_choice&suID=' . $HTTP_GET_VARS['suID'] . '&quID=' . $question_result['question_id'],'post','onSubmit="return validateFormChoice(this)"');
			         echo '<tr valign=middle><td class="main">' . $qid . '. </td><td>' . tep_draw_input_field('choice_text','','size=30');
			         echo '&nbsp;' . tep_image_submit('button_insert.gif',IMAGE_CHOICE_INSERT,'align=absmiddle') . '</td></tr>';
				 echo '</form>';
			 ?>
		      </table></td></tr>
		      <?php } // question_type condition ?>
		      <?php if ($id<$qu_count) { ?>
		      <tr><td align=right colspan="2" valign=middle>
		         <?php echo '<a href="' . tep_href_link(FILENAME_EVENTS_SURVEYS_EDIT,'action=pos_down&pos=' . (int)$question_result['sort_order'] . '&suID=' . (int)$HTTP_GET_VARS['suID'] . '&quID=' . $question_result['question_id']) . '">' . tep_image(DIR_WS_ICONS . '/order_down.gif',IMAGE_ORDER_DOWN,'16','16') . '</a>'; ?></td></tr>
		      <?php } ?>
		      </table></td></tr>
	   <?php $id++;}
	    } else {
	   ?>
           <tr><td><table border="0" cellpadding="2" width="100%" class="attributes-odd">
	              <tr><td class="main"><?php echo TEXT_NO_QUESTIONS; ?></td></tr>
		    </table></td>
	   </tr>
           <?php }?>
	   </table></td>
    </tr></table>
  </td>
</tr></table>
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>