<?php
include("../include/session.php");  
include("suggestionList.class.php");
require_once("../include/accessCheck.php");   
require_once("../include/mailer.php"); 
if (isset($_GET['page'])){ //find out what the sub page is
	$page = $_GET['page'];
	}
else{$page = 'display';}

global $pageNum;
if(isset($_GET['pageNum']))
        $pageNum=$_GET['pageNum']; //Get the current page number
    else
        $pageNum=1;
        
/**
* displaySuggestions - Displays the suggestion database table in
* a nicely formatted html table.
*/
function displaySuggestions($appID){
    global $pageNum,$session;
    if(trim($appID)=='')          //Make sure appID isn't blank
    {
        echo "Incorrect application ID.";
        exit;
    }
    //check app permissions
    if($session->userlevel!=1 && !suggestionList::hasAppAccess(trim($appID),$session->userinfo['adminID']))
    {
       echo "Access Denied.";
       exit;
    }
    
//paging code below
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++      
$result=suggestionList::showAppSuggestions($appID);
$nrResults = mysql_numrows($result);

$limit=20; // Limit of result per page 
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
$result = suggestionList::showAppSuggestions($appID,$pageNum,$limit);
$num_rows = mysql_numrows($result);

if(!$result || ($num_rows < 0)){
echo "Error displaying info";
return;
}
if($num_rows == 0){
echo "No record.";
return;
}

/* Display table contents */
echo "<table  cellspacing=\"3\" cellpadding=\"2\">";
echo "<tr><th><b>Application Name</b></th><th><b>Suggestion Subject</b></th><th><b>Submission Date</b></th><th><b>Category</b></th><th><b>Status</b></th><th width='150px'><b>Action</b></th></tr>\n";
for($i=0; $i<$num_rows; $i++){
    
$suggestionID  = mysql_result($result,$i,"suggestionID");
$appName  = mysql_result($result,$i,"appName");
$suggestionSubject  = mysql_result($result,$i,"suggestionSubject");
$submissionDateTime= mysql_result($result,$i,"submissionDateTime");  
$submissionCategory= mysql_result($result,$i,"categoryDescription");
$status= mysql_result($result,$i,"statusDescription");
$disposition  = mysql_result($result,$i,"suggestionDisposition");
$dispositionNotice= mysql_result($result,$i,"submitterNotifiedOfDisposition"); 
$receiptNotice= mysql_result($result,$i,"submitterNotified"); 
$notificationRequest= mysql_result($result,$i,"submitterRequestedNotification"); 

echo '<tr><td class="'.($i%2? "c1":"c2").'">'.$appName.'</td>';
echo '<td class="'.($i%2? "c1":"c2").'">'.$suggestionSubject.'</td>'; 
echo '<td class="'.($i%2? "c1":"c2").'">'.date("m/d/Y g:i A", strtotime($submissionDateTime)).'</td>';
echo '<td class="'.($i%2? "c1":"c2").'">'.$submissionCategory.'</td>';
echo '<td class="'.($i%2? "c1":"c2").'">'.$status.'</td>';  
  
?>  
<script type="text/javascript">

function submitForm(form)
{
    document.getElementById(form).submit();
    return true;  
}
</script>                   
<td align="center" class="<?php echo ($i%2? "c1":"c2") ?>">
    <div style="float: left; width:30px;"> 
            <a href="<?php echo $_SERVER['PHP_SELF'] . '?page=edit&subeditsuggestionID=' . $suggestionID.'&appID='.$appID; ?>"><img SRC="../icons/Edit.png" title="Update Suggestion" ALT="Update Suggestion" border="0"/></a>
    </div>
    <!--    ||||||||||||||||||   --> 
    <div style="float: left; width:30px;">
            <a href="<?php echo $_SERVER['PHP_SELF'] . '?page=display&subdelsuggestionID=' . $suggestionID.'&appID='.$appID;?>" onClick="javascript:return confirm('Are you sure you want to delete this suggestion? This action cannot be undone.');"><img border="0" SRC="../icons/delete.gif" title="Delete Suggestion" ALT="Delete Suggestion"  ></a>
    </div>
      <!--    ||||||||||||||||||   --> 
<?php
if(enableEmail==1)  //if e-mail allowed 
{

            if($receiptNotice==1) //User already notified of suggestion receipt
            {
?>
             <div style="float: left; width:30px;">
                    <img border="0" SRC="../icons/receipt_sent.png" title="Suggestion Receipt Sent" ALT="Suggestion Receipt Sent"  >
                </div>
<?php
            }
            else    //User not notified of receipt
            {
?>
             <div style="float: left; width:30px;">
                    <a href="<?php echo $_SERVER['PHP_SELF'] . '?page=display&subreceiptsuggestionID=' . $suggestionID.'&appID='.$appID;?>" onClick="javascript:return confirm('Click OK to notify user of the receipt of this suggestion.');"><img border="0" SRC="../icons/receipt.png" title="Notify User of Receipt" ALT="Notify User of Receipt"  ></a>
                </div>
<?php 
            
            }
        
} 
if(enableEmail==1 && !is_null($disposition) && trim($disposition)!='')  //if e-mail allowed  and disposition is set
{
        if($notificationRequest==1)
        {
            if($dispositionNotice==1) //User already notified of disposition
            {
?>
             <div style="float: left; width:30px;">
                  <img border="0" SRC="../icons/notification_done.png" title="User notified of Disposition" ALT="User Notified of Disposition"  >
                </div>
<?php
            }
            else    //User not notified of disposition
            {
?>
             <div style="float: left; width:30px;">
                    <a href="<?php echo $_SERVER['PHP_SELF'] . '?page=display&subnoticesuggestionID=' . $suggestionID.'&appID='.$appID;?>" onClick="javascript:return confirm('Click OK to notify user of suggestion disposition.');"><img border="0" SRC="../icons/alert_user.png" title="Notify User of Disposition" ALT="Notify User of Disposition"  ></a>
                </div>
<?php 
            
            }
        }
        else
        {
?>
             <div style="float: left; width:30px;">
                    <img border="0" SRC="../icons/notification_alert.png" title="Notification not requested" ALT="Notification not requested"  >
                </div>
<?php 
        }
}
?>
</td>
<?php
echo "</tr>\n";
}
echo "</table><br>";
//Paging code below
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++    
suggestionList::displayPageNav($nrResults,$limit,$pageNum,$_SERVER['PHP_SELF']."?appID=".$appID);

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
}
/**
* displayComments - Displays the comments database table in
* a nicely formatted html table.
*/
function displayComments($suggestionID){
    global $session;
    if(trim($suggestionID)=='')          //Make sure suggestionID isn't blank
    {
        exit;
    }

$result = suggestionList::showSuggestionComments($suggestionID);
$num_rows = mysql_numrows($result);

if(!$result || ($num_rows < 0) || $num_rows==0){
    echo "No History";  
    return;
}

/* Display table contents */
echo "<table  cellspacing=\"3\" cellpadding=\"2\">";
echo "<tr><th><b>Date</b></th><th><b>Comment</b></th><th><b>Comment Owner</b></th><th><b>Comment Owner's Email</b></th></tr>\n";
for($i=0; $i<$num_rows; $i++){
    
$suggestionID  = mysql_result($result,$i,"suggestionID");
$dateTime  = mysql_result($result,$i,"submissionDateTime");
$comment  = mysql_result($result,$i,"comment");
$ownerName  = mysql_result($result,$i,"firstName")." ".mysql_result($result,$i,"lastName");
$ownerEmail = mysql_result($result,$i,"email"); 

echo '<tr><td class="'.($i%2? "c1":"c2").'">'.date("m-d-Y g:iA", strtotime($dateTime)).'</td>';
echo '<td class="'.($i%2? "c1":"c2").'">'.$comment.'</td>'; 
echo '<td class="'.($i%2? "c1":"c2").'">'.$ownerName.'</td>';
echo '<td class="'.($i%2? "c1":"c2").'">'.$ownerEmail.'</td>';  
  
echo "</tr>\n";
}
echo "</table><br>";

}
//----------------------------------------------------------------------------------
 if(isset($_GET['subnoticesuggestionID']))
 {  
    $suggestionInfo=suggestionList::querySuggestion($_GET['subnoticesuggestionID']);
    
    $mailTo=$suggestionInfo->submitterEmail;
    $subject = "Suggestion Status for ".$suggestionInfo->appName;
    $message = "Dear ".$suggestionInfo->submitterName.",\r\n\r\n".
                "An action has been taken on the following suggestion you submitted:\r\n\r\n".
                "Suggestion Subject: ".$suggestionInfo->suggestionSubject."\r\n\r\n".
                "Suggestion Submitted on: ".date("m/d/Y", strtotime($suggestionInfo->submissionDateTime))."\r\n\r\n".
                "Suggestion Description: ".$suggestionInfo->suggestionDescription."\r\n\r\n".
                "-----------------------------------\r\n\r\n". 
                "Action Taken: ".$suggestionInfo->suggestionDisposition."\r\n\r\n". 
                "-----------------------------------\r\n\r\n".
                "PLEASE NOTE THAT THIS IS AN AUTOMATICALLY GENERATED MESSAGE. PLEASE DO NOT REPLY TO THIS MESSAGE.".
                "YOU ARE RECEIVING THIS EMAIL BECAUSE YOU REQUESTED NOTIFICATION OF YOUR SUBMITTED SUGGESTION.\r\n\r\n"; 
                 
         if($suggestionInfo->submitterNotifiedOfDisposition!=1)    //only mail if not mailed before 
         {
            if(mailer::sendMail($mailTo,$subject,$message))
            { 
                suggestionList::updateDispositionNotification($_GET['subnoticesuggestionID'],1);
                $process_result="The user has been notified.";
                
                    
            }
            else
            {
                $process_result="Failed to send an email to user. There could be a problem with user's email address or user's email server."; 
            }
         }

 }
 //----------------------------------------------------------------------------------
 if(isset($_GET['subreceiptsuggestionID']))
 {  
    $suggestionInfo=suggestionList::querySuggestion($_GET['subreceiptsuggestionID']);
    
    $mailTo=$suggestionInfo->submitterEmail;
    $subject = "Suggestion received for ".$suggestionInfo->appName;
    $message = "Dear ".$suggestionInfo->submitterName.",\r\n\r\n".
                "This message is to confirm that we have received the following suggestion:\r\n\r\n".
                "Suggestion Subject: ".$suggestionInfo->suggestionSubject."\r\n\r\n".
                "Suggestion Submitted on: ".date("m/d/Y", strtotime($suggestionInfo->submissionDateTime))."\r\n\r\n".
                "Suggestion Description: ".$suggestionInfo->suggestionDescription."\r\n\r\n".
                "-----------------------------------\r\n\r\n".
                "PLEASE NOTE THAT THIS IS AN AUTOMATICALLY GENERATED MESSAGE. PLEASE DO NOT REPLY TO THIS MESSAGE.".
                "YOU ARE RECEIVING THIS EMAIL BECAUSE YOU REQUESTED NOTIFICATION OF YOUR SUBMITTED SUGGESTION.\r\n\r\n"; 
        
     if($suggestionInfo->submitterNotified!=1)    //only mail if not mailed before
     {        
        if(mailer::sendMail($mailTo,$subject,$message))
        { 
            suggestionList::updateReceiptNotification($_GET['subreceiptsuggestionID'],1);
            $process_result="The user has been notified of the receipt.";
            
                
        }
        else
        {
            $process_result="Failed to send an email to user. There could be a problem with user's email address or user's email server."; 
        }
     }
 }
 //----------------------------------------------------------------------------------
 if(isset($_GET['subdelsuggestionID']))
 {  
    if(suggestionList::querySuggestion($_GET['subdelsuggestionID'])!=false)
    { 
        $deleteResult=suggestionList::deleteSuggestion($_GET['subdelsuggestionID']);
        
        if($deleteResult)
            $process_result="The selected suggestion has been removed successfully.";
        else
            $process_result="Failed to remove the suggestion from the database.";
    }

 }
  //----------------------------------------------------------------------------------
if(isset($_POST['subeditsuggestion']) && isset($_POST['subeditsuggestionID']))
 {  
    global $form,$session;
   
   $publicView=0 ;
    if(isset($_POST['publicView']))
    {
     $publicView=1;
    }
    $field = "status_code";  
    if(!isset($_POST['status_code']) || strlen(trim($_POST['status_code'])) == 0){
    $form->setError($field, "* Missing Status.");
    }
    
    /* Errors exist, have user correct them */
    if($form->num_errors > 0){
    $_SESSION['value_array'] = $_POST;
    $_SESSION['error_array'] = $form->getErrorArray();
    }
    /* No errors, update application information in the database*/
    else
    { 
       
           if(suggestionList::updateSuggestion($session->userinfo['adminID'],$_POST['subeditsuggestionID'],$_POST['status_code'],$_POST['disposition'],$publicView,$_POST['comment']))
           {
                $process_result= "Suggestion updated successfully. <a href='suggestionList.php?appID=".$_POST['appID']."'>Go Back</a>...";
                $page="edit";
           }    
            else
                $process_result="Failed to update.";
    }

 }

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="../style.css"/>       


</head>
<body >

<div id="layout">
	<?php require('../include/header.php');   ?>
        
<?php    switch ($page){ //what page to load
		case 'display':  //begin display page?>
			<div id="displaySuggestions" class="in"> 
            <P class=veryLargeBlue>Manage Application Suggestions</P>
				<?php if(isset($process_result)) {echo $process_result;}
				if(isset($_GET['appID'])) {
                        displaySuggestions($_GET['appID']);
                }
                else  {
                        echo "Unable to identify the application for which you want to display suggestion table.";
                        exit;
                }
				?>
			</div>
		<?php
		break; //end display page
		
		case 'edit': //begin edit page?>
            <div id="panel-left" class="box panel">
            <P class=veryLargeBlue>Update Suggestion</P> 
                <?php
                if(isset($_REQUEST['subeditsuggestionID']))
                {
                $suggestionInfo=suggestionList::querySuggestion($_REQUEST['subeditsuggestionID']);
                ?>
                <div id="editApp">
                    <?php if(isset($process_result)) {echo $process_result;}?>
                    <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
                        <table align="left" border="0" cellspacing="0" cellpadding="3">

                        <tr>
                            <td class="c1">Application Name:</td>
                            <td class="c1"><?php echo $suggestionInfo->appName; ?></td>
                           
                            <input type="hidden" name="subeditsuggestionID" value="<?php echo $_REQUEST['subeditsuggestionID']; ?>"> 
                            <input type="hidden" name="appID" value="<?php echo $suggestionInfo->appID; ?>" >   
                        </tr>
                         <tr>
                            <td class="c2">App Sub Location:</td>
                            <td class="c2"><?php echo $suggestionInfo->appSubLocationDescription; ?></td>
                        </tr>
                         <tr>
                            <td class="c1">App Sub Location Address:</td>
                            <td class="c1"><?php echo $suggestionInfo->appSubLocationHttp; ?></td>
                        </tr>
                           <tr>
                            <td class="c2">Category:</td>
                            <td class="c2"><?php echo $suggestionInfo->categoryDescription; ?></td>
                        </tr>
                           <tr>
                            <td class="c1">Submission Date:</td>
                            <td class="c1"><?php echo date("m-d-Y @ g:i A", strtotime($suggestionInfo->submissionDateTime)); ?></td>
                        </tr>
                           <tr>
                            <td class="c2">Submitter Name:</td>
                            <td class="c2"><?php echo $suggestionInfo->submitterName; ?></td>
                        </tr>
                         <tr>
                            <td class="c1">Submitter E-Mail:</td>
                            <td class="c1"><?php echo $suggestionInfo->submitterEmail; ?></td>
                        </tr>
                         <tr>
                            <td class="c2">Submitter Location:</td>
                            <td class="c2"><?php echo $suggestionInfo->submitterCity.", ".$suggestionInfo->submitterState.", ".$suggestionInfo->submitterZipCode; ?></td>
                        </tr>
                          <tr>
                            <td class="c1">Suggestion Subject:</td>
                            <td class="c1"><?php echo $suggestionInfo->suggestionSubject; ?></td>
                        </tr>
                          <tr>
                            <td class="c2">Suggestion Description:</td>
                            <td class="c2"><?php echo $suggestionInfo->suggestionDescription; ?></td>
                        </tr>
                        <tr>
                            <td class="c1">Status:</td>
                            <td class="c1">
                            <?php $statusList=suggestionList::showStatusList(); ?>
                            <select class="style01"  name="status_code" size="1" >
                             
                                <?php 
                                     
                                    while ($row = mysql_fetch_object($statusList))
                                            {
                                                if($suggestionInfo->suggestionStatusCode==$row->statusCode)
                                                {
                                                    echo "<option selected value='$row->statusCode' >$row->statusDescription</option>";
                                                }
                                                else
                                                    echo "<option value='$row->statusCode' >$row->statusDescription</option>";
                                            }
                                ?>
                                </select>
                                <?php echo $form->error("status_code"); ?>
                            </td>
                        </tr>
                         <tr>
                            <td class="c2">Suggestion Disposition:</td>
                            <td class="c2"><textarea  name="disposition" cols="70" rows="6" ><?php if(isset($_POST['disposition'])) echo $_POST['disposition']; else echo $suggestionInfo->suggestionDisposition; ?></textarea>
                            <?php  echo $form->error("disposition"); ?></td>
                        </tr>
                        <tr >
                            <th class="c1"><b>Enable Public View:</b></th>
                            <td class="c1">
                            <?php 
                            $checked="";
                            if($suggestionInfo->enablePublicView==1)
                            {
                                $checked= "checked";
                            } 
                                
                                echo "<input name='publicView' type='checkbox' value='1' $checked >";
                             
                            ?>
                            </td>
                        </tr>
                        <tr>
                            <td class="c2">Comment:</td>
                            <td class="c2"><textarea  name="comment" cols="70" rows="6" ><?php if(isset($_POST['comment'])) echo $_POST['comment']; else {
                                
                                $result= suggestionList::showAdminComment($session->userinfo['adminID'],$suggestionInfo->suggestionID);
                                 if($result!=false)
                                    {
                                    echo $result->comment;
                                    }
                                 }  ?></textarea>
                            <?php // echo $form->error("comment"); ?></td>
                        </tr>
                        <tr>
                            <td class="c1">Comment History:</td>
                            <td class="c1"> <?php displayComments($suggestionInfo->suggestionID);?></td>
                        </tr>
                        <input type="hidden" name="subeditsuggestion" value="1"> 

                        <tr>
                            <td></td>
                            <td>
                                <div style="float:left;"><button name="editsuggestion" type="submit" >Update Suggestion</button></div> 
                               
                                <div style="float:left;"><button type="button" onclick="window.location='suggestionList.php?appID=<?php echo $suggestionInfo->appID; ?>'">Cancel</button></div>

                           </td>
                        </tr>
   
                        </table>
                        </form>
         
                </div>
            </div>
		<?php break; //end edit page
		 }
} //end switch page
 require('../include/footer.php'); ?>
</div>
<?php
 unset($_SESSION['error_array']); 
?>
</body>
</html>
