<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
  // This version uses salon_name and is waiting for 2 solutions from forum
 
    if (!defined('START_SESSION')) { define('START_SESSION', true); }
	 
  require_once "cmsAdmin/lib/viewer_functions.php";
  $CMS_USER = getCurrentUserFromCMS(); 
 ?>
<?php  
 if (@$_POST['formSubmit'])   {
 	
      // error checking
    $errorsAndAlerts = "";
    if (!@$_REQUEST['attendee_number'] && !@$_REQUEST['first_name'])                { $errorsAndAlerts .= "You must enter your First Name<br/>\n"; }
	if (!@$_REQUEST['attendee_number'] && !@$_REQUEST['last_name'])                { $errorsAndAlerts .= "You must enter your Last Name<br/>\n"; }
    if (!@$_REQUEST['attendee_number'] && !@$_REQUEST['email_address'])                   { $errorsAndAlerts .= "You must enter your E-mail Address<br/>\n"; }
    else if(!@$_REQUEST['attendee_number'] && !isValidEmail(@$_REQUEST['email_address'])) { $errorsAndAlerts .= "Please enter a valid E-mail address (example: user@example.com)<br/>\n"; }
   $count = mysql_select_count_from('salon_listings', "'".mysql_escape($_REQUEST['email_address'])."' IN (email_address)");
 $count2 = mysql_select_count_from('salon_listings', "'".mysql_escape($_REQUEST['attendee_number'])."' IN (num)");    
 if (@$_REQUEST['attendee_number'] && @$count2 != 1)                   { $errorsAndAlerts .= "Sorry, the Attendee ID Number you entered, doesn't match our records, please try again or enter your name and email address. <br/>\n"; } ?>
<?php
	
// turn off strict mysql error checking for: STRICT_ALL_TABLES
  mysqlStrictMode(false); // disable Mysql strict errors for when a field isn't defined below (can be caused when fields are added later)

    // The values for AWC = 1, and for MMM = 2
   
    // Check for email address. Insert new record if email address is not found.
	   if ($count == 0 && !$errorsAndAlerts) {  
            @$_REQUEST['first_name']=ucwords(@$_REQUEST['first_name']);		
			@$_REQUEST['last_name']=ucwords(@$_REQUEST['last_name']);	
	  $lecturedate =  date("m-y").'AWC, ' ;			
     mysql_query("INSERT INTO `{$TABLE_PREFIX}salon_listings` SET
                      first_name         = '".mysql_escape( $_REQUEST['first_name'] )."',
					  last_name         = '".mysql_escape( $_REQUEST['last_name'] )."',
					  email_address            = '".mysql_escape( $_REQUEST['email_address'] )."',
					  salon_name = '1',
					  attended = '$lecturedate',
					  updatedByUserNum = '".mysql_escape( $CMS_USER['num'] )."',	 
					  createdDate      = NOW(),
                      updatedDate      = NOW()")
                    or die("MySQL Error Creating Record:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
      $userNum = mysql_insert_id();
    // on success
	 $first_name = $_REQUEST['first_name'] ;
	 $last_name = $_REQUEST['last_name'] ;
	 $email_address = $_REQUEST['email_address'] ;
	 $welcome_email = '1';
      unset($_REQUEST['first_name'], $_REQUEST['last_name'], $_REQUEST['email_address']
	  ); // clear form
      $errorsAndAlerts = "Thanks $first_name, we've signed you in, and we're ready for the next guest.";
    }
     
   
	// Check for first name, last name, and duplicate email address. Update record if email address is found.
	      if (!@$_REQUEST['attendee_number'] && $count > 0 && !$errorsAndAlerts) {  
		    $email_address = $_REQUEST['email_address'] ;
			@$_REQUEST['first_name']=ucwords(@$_REQUEST['first_name']);		
			@$_REQUEST['last_name']=ucwords(@$_REQUEST['last_name']);	
			 
	 $lecturedate =  date("m-y").'AWC, ' ;
		 list($salon_listingsRecords, $salon_listingsMetaData) = getRecords(array(
    'tableName'   => 'salon_listings',
	'where'      => "email_address = '$email_address'", 
  ));
 ?>
<?php foreach ($salon_listingsRecords as $record ): ?>
<?php  $recnumber2 =   $record['num']; ?>
<?php endforeach; ?>
<?php     $query = "UPDATE `{$TABLE_PREFIX}salon_listings` SET
                     first_name         = '".mysql_escape( $_REQUEST['first_name'] )."',
					 last_name         = '".mysql_escape( $_REQUEST['last_name'] )."',
				    attended = CONCAT(attended, '$lecturedate'),
					 updatedByUserNum = '".mysql_escape( $CMS_USER['num'] )."',
                     updatedDate      = NOW()
                    WHERE email_address = '".mysql_escape( $_REQUEST['email_address'] )."'";
					
					 // load records
 
           
				      
	// Add "AWC" (1) to the list if it's not already there.
$my_var = mysql_get('salon_listings',$recnumber2);	 
	 function addValueToListValues($values, $newValue){
    if(!strstr($values, "\t$newValue\t")){
       return $updatedValue = $values.$newValue."\t";
    }else{
      return $values;
    }
  }
	
$updatedValue = addValueToListValues($my_var['salon_name'],'1');
  mysql_update('salon_listings',$recnumber2, null, array('salon_name' => $updatedValue));
  

	// remove "NONE" since we're adding a salon_name
	  $my_var1 = mysql_get('salon_listings',$recnumber2);
	  function removeValueFromListValues($values, $removeValue){
    if(strstr($values, "\t$removeValue\t")){
      return $updatedValue = str_replace($removeValue."\t",'', $values);
    }else{
      return $values;
    }
  }	
	 $updatedValue = removeValueFromListValues($my_var1['salon_name'],'3');
   mysql_update('salon_listings',$recnumber2, null, array('salon_name' => $updatedValue));
   
      mysql_query($query) or die("MySQL Error:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
      $userNum = mysql_insert_id();

      // on success
	   $first_name = $_REQUEST['first_name'] ;
      unset($_REQUEST['first_name'], $_REQUEST['last_name'], $_REQUEST['email_address']); // clear form
      $errorsAndAlerts = "Thanks $first_name, we've signed you in, and we're ready for the next guest.";
    }
	

		// Check for attendee ID number. Update record if ID number is found.
	      if (@$_REQUEST['attendee_number']  && !$errorsAndAlerts) {  
		   	
	 $lecturedate =  date("m-y").'AWC, ' ;
		 		
      $query = "UPDATE `{$TABLE_PREFIX}salon_listings` SET
                    
				     attended = CONCAT(attended, '$lecturedate'),
					 updatedByUserNum = '".mysql_escape( $CMS_USER['num'] )."',
                     updatedDate      = NOW()
					  WHERE num = '".mysql_escape( $_REQUEST['attendee_number'] )."'";
					  
		$recnumber = mysql_escape(@$_REQUEST['attendee_number']);	
					      
	 	
// Add "AWC" (1) to the list if it's not already there.
$my_var = mysql_get('salon_listings',$recnumber);
	 function addValueToListValues($values, $newValue){
    if(!strstr($values, "\t$newValue\t")){
       return $updatedValue = $values.$newValue."\t";
    }else{
      return $values;
    }
  }
	

$updatedValue = addValueToListValues($my_var['salon_name'],'1');
  mysql_update('salon_listings',$recnumber, null, array('salon_name' => $updatedValue));
  
  $my_var1 = mysql_get('salon_listings',$recnumber);
	// remove "NONE" since we're adding a salon_name
	function removeValueFromListValues($values, $removeValue){
    if(strstr($values, "\t$removeValue\t")){
      return $updatedValue = str_replace($removeValue."\t",'', $values);
    }else{
      return $values;
    }
  }	
	 $updatedValue = removeValueFromListValues($my_var1['salon_name'],'3');
   mysql_update('salon_listings',$recnumber, null, array('salon_name' => $updatedValue));
				  
	// Get the attendee's first name from the updated record. 
					   $loggedInUser = mysql_get('salon_listings', $_REQUEST['attendee_number']);
					    $first_name = @$loggedInUser['first_name']; ;
                     mysql_query($query) or die("MySQL Error:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
      $userNum = mysql_insert_id();

      // on success
       unset($_REQUEST['attendee_number'], $_REQUEST['first_name'], $_REQUEST['last_name'], $_REQUEST['email_address']); // clear form
     $errorsAndAlerts = "Thanks $first_name. We've signed you in, and we're ready for the next guest.";
    }
    }
	
  // load viewer library
  $libraryPath = 'cmsAdmin/lib/viewer_functions.php';
  $dirsToCheck = array('/hsphere/local/home/a887307/elleschorrphotography.com/','','../','../../','../../../');
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

  // load records
  list($salon_listingsRecords, $salon_listingsMetaData) = getRecords(array(
    'tableName'   => 'salon_listings',
  ));
  
    // load record from 'salon_messages'
  list($salon_messagesRecords, $salon_messagesMetaData) = getRecords(array(
    'tableName'   => 'salon_messages',
    'where'       => '', // load first record
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $salon_messagesRecord = @$salon_messagesRecords[0]; // get first record
  
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Art, Women, and Culture Salon Sign In</title>
<link href="css/elle.css.php" rel="stylesheet" type="text/css" />
</head>

<body bgcolor="#000000">
<table width="75%" border="0" align="center" cellpadding="20">
  <tr>
    <td class="Heading-Text-Yellow" colspan="2"><?php if (@$errorsAndAlerts): ?>
      <?php echo $errorsAndAlerts; ?>
      <?php else : ?>
      &nbsp;
      <?php endif ?></td>
  </tr>
  <tr>
    <td width="40%" style="text-align:center"><h1> WELCOME TO<br />
        THE ART, WOMEN, AND CULTURE SALON </h1>
      <h2>PLEASE SIGN IN:</h2>
      <span class="Large-Text">Suggested Donation $10<br />
      email_address is <?php echo $email_address ?> <br /></td>
    <td width="60%"><form action="" method="post" name="registrationForm">
        <input type="hidden" value="1" name="formSubmit" />
        <table align="center">
          <tr>
            <td colspan="2" class="Medium-Text">EXPRESS SIGN IN: If you have an Attendee ID Number, just enter it and click/tap the submit button and you'll automatically be signed in.<br />
              <br /></td>
          </tr>
          <tr>
            <td class="Medium-Text">Attendee ID Number</td>
            <td><input class="Medium-Text" type="text" id="attendee" name="attendee_number" value="<?php echo htmlspecialchars(@$_REQUEST['attendee_number']); ?>"  /></td>
          </tr>
          <tr>
            <td class="Medium-Text">First Name</td>
            <td><input class="Medium-Text" type="text" id="first" name="first_name" value="<?php echo htmlspecialchars(@$_REQUEST['first_name']); ?>"  /></td>
          </tr>
          <tr>
            <td class="Medium-Text">Last Name</td>
            <td><input  class="Medium-Text" type="text" id="last" name="last_name" value="<?php echo htmlspecialchars(@$_REQUEST['last_name']); ?>"  /></td>
          </tr>
          <tr>
            <td class="Medium-Text">E-mail Address</td>
            <td><input type="text" class="Medium-Text" id="email" name="email_address" value="<?php echo htmlspecialchars(@$_REQUEST['email_address']); ?>"  /></td>
          </tr>
          <tr>
            <td colspan="2" align="center"><br/>
              <br/>
              <input name="submitregistration" id="signup" class="Heading-Text" type="submit" value="Click/Tap To Submit" /></td>
          </tr>
        </table>
      </form></td>
  </tr>
</table>
<?php 
if (@$welcome_email == 1):?>
<?php // delay execution by .5 sec
usleep(500000);
?>
<?php //$email = mysql_escape($_REQUEST['email_address']) ?>
<?php  // load records
  list($salon_listingsRecords, $salon_listingsMetaData) = getRecords(array(
    'tableName'   => 'salon_listings',
	'where'      => "email_address = '$email_address'", 
  ));
 ?>
<?php foreach ($salon_listingsRecords as $record ): ?>
<?php ob_start(); // start capturing output ?>
<?php $welcome_email_message_awc = @$salon_messagesRecord['welcome_email_message_awc'] ?>
<?php $welcome_email_message_awc = wordwrap($welcome_email_message_awc, 70); ?>
<?php echo $welcome_email_message_awc ?><br />
<br />
<?php if($record['num']): ?>
Just so you know, your Express Sign In Attendee ID number is: <?php echo $record['num'] ?> <br />
<br />
<?php endif ?>
Next time you come to a salon, you can enter that number and you'll be signed in without having to enter your name and e-mail address.
<?php $output = ob_get_clean(); // stop capturing output  ?>
<?php $first_name = strtoupper($first_name) ?>
<?php $last_name = strtoupper($last_name) ?>
<?php  $the_to = $email_address ?>
<?php 
$message = <<<EOF
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type content='text/html; charset=utf-8" />
<style type="text/css">
.special {font-family:Arial; color:  rgb(227,224,219); font-size: 1.0em; font-weight: bold; text-decoration: underline;}
.body-text {font-family:Arial; font-size: 1.0em;}
.heading-text {font-family:Arial; font-size: 1.3em;}
.body-text-yellow {font-family:Arial; color: rgb(255,255,0); font-size: 1.0em;}
.heading-text-yellow {font-family:Arial; color: rgb(255,255,0); font-size: 1.3em; font-weight: bold;}
</style>
</head>
<body bgcolor="#B2A1C7">
<table style='background-color: #B2A1C7;' width='100%' align='center' border='0' >
			  <tr>
    <td align='left' >
	<table style='background-color: #B2A1C7;' width='70%' align='center' border='0' >
			  <tr>
    <td align='left' >
	<div align='center' class='heading-text'><br /><br />WELCOME, $first_name $last_name </div>
 <br /><br />
 <div align='left' class='body-text'>
   $output \r\n

    <br /><br />
Elle Schorr<br />
Salon Facilitator
  </td>
    </tr>
	</table>
	</td>
    </tr>
	</table>
  </body>
</html>
EOF;
 $the_from  = "elleschorr@elleschorrphotography.com";
 $the_subject = "Thanks for attending the Art, Women and Culture Salon";

$mailArray = array( 
'to' => $the_to,
'from' => $the_from,
'subject' => $the_subject,
'html'         => $message 
); 
$errors =  sendMessage($mailArray);

?>
<?php endforeach; ?>
<div align='center' class='heading-text'>A welcome e-mail with your Express Sign In ID number has been sent to you at: <?php echo @$the_to ?></div>
<?php endif; ?>
</body>
</html>
