<?
if(!empty($auth_head))
   eval($auth_head);
else{
   echo 'You are not Authorized to use this page';
   exit;
}

//$dp = '%(19|20)\d\d[-/.](0[1-9]|1[012])[-/.](0[1-9]|[12][0-9]|3[01])%';
$success = false;
//$valid   = true;
//$valid   = ( $valid && (preg_match($dp,$_POST['deadline']) || empty($_POST['deadline'])) ) ? true : false;

if( $_POST['but'] == "Submit" || $_POST['but'] == "Deny" || $_POST['but'] == "Approve" ){
   if(isset($_GET['act']))
      $act = $_GET['act'];
   else{
      unset($act);
      exit;
   }
   
   $data    = $_POST;
   $tbname  = TABLE_WORKSHOPS;
   //$primary = "id";

   if(!empty($act) && $act != "add"){
      $uid = $data['uid'];
   }


   foreach($data as $key => $value){
      if($key != "but"){
         $$key = $value;
      }
   }
         
   $tbname = TABLE_WORKSHOPS;
   $suf  = "last='$last', first='$first', institution='$institution', email='$email', attending='$attending'";
   if(!empty($confirmed))
      $suf .= ", confirmed='$confirmed'";
   else
      $suf .= ", confirmed=NULL";
   if(!empty($address))
      $suf .= ", address='$address'";
   else
      $suf .= ", address=NULL";
   if(!empty($phone))
      $suf .= ", phone='$phone'";
   else
      $suf .= ", phone=NULL";
   if(!empty($support))
      $suf .= ", support='$support'";
   else
      $suf .= ", support='N'";
   if(!empty($sup_contrib))
      $suf .= ", sup_contrib='$sup_contrib'";
   else
      $suf .= ", sup_contrib='N'";
   if(!empty($region))
      $suf .= ", region='$region'";
   else
      $suf .= ", region=NULL";
   if(!empty($interest))
      $suf .= ", interest='$interest'";
   else
      $suf .= ", interest=NULL";
   if(!empty($interest))
      $suf .= ", interest_contrib='$interest_contrib'";
   else
      $suf .= ", interest_contrib=NULL";
   if(!empty($ao_interest))
      $suf .= ", ao_interest='$ao_interest'";
   else
      $suf .= ", ao_interest='N'";
   if(!empty($contrib))
      $suf .= ", contrib='$contrib'";
   else
      $suf .= ", contrib=NULL";
   if(!empty($h_checkin))
      $suf .= ", h_checkin='$h_checkin'";
   else
      $suf .= ", h_checkin=NULL";
   if(!empty($h_checkout))
      $suf .= ", h_checkout='$h_checkout'";
   else
      $suf .= ", h_checkout=NULL";

   if(isset($act) && $act != "add" && $act != "app"){
      $query   = "SELECT uid FROM $tbname WHERE workshop='synth07' AND uid=$uid";
      //echo $query;
      $result  = $db->sql_query($query);
      $num_res = $db->sql_numrows($result);
   }
   if(isset($act) && $act == "app"){
      if($data['but'] == "Deny"){
         $query  = "UPDATE $tbname SET confirmed='N' WHERE workshop='synth07' AND uid=$uid";
         $result = $db->sql_query($query);
         if(!$result){
            $indication = "unknown error.";
         }
         else{
            $indication = "Denied. " . stripslashes($data['first']) . " " . stripslashes($data['last']) . " was denied attendance to the workshop!";
         }
      } // if($_POST['but'] == "Deny" && $num_res == 1)
      else{
         $query  = "UPDATE $tbname SET confirmed='Y' WHERE workshop='synth07' AND uid=$uid";
         //echo $query;
         $result = $db->sql_query($query);
         if(!$result){
            $indication = "unknown error please try again";
         }
         else{
            $indication = "Confirmed. " . stripslashes($data['first']) . " " . stripslashes($data['last']) . "'s attendance to the workshop is confirmed!";
         } // else
      } // else
   } // if(isset($act) && $act == "app")
         
      

   if(isset($act) && $act == "add"){
      $query  = "INSERT INTO $tbname SET workshop='synth07', $suf";
      //echo $query;
      $result = $db->sql_query($query);
      if(!$result){
         $indication = "unknown error please try again";
      }
      else{
         $indication = stripslashes($data['first']) . " " . stripslashes($data['last']) . " Added Successfully!";
      }
   } // if(isset($act) && $act == "add")

   if(isset($act) && $act == "upd"){
      if($num_res == 1){
         $query  = "UPDATE $tbname SET $suf WHERE workshop='synth07' AND uid=$uid";
         //echo $query;
         $result = $db->sql_query($query);
         if(!$result){
            $indication = "unknown error please try again";
         }
         else{
            $indication = stripslashes($data['first']) . " " . stripslashes($data['last']) . " Updated Successfully!";
         }
      }
      else{
         $indication =  stripslashes($data['first']) . " " . stripslashes($data['last']) . " Does Not Exist in the Database, Please Add them First!";
      }
   } // if(isset($act) && $act == "upd")
   if(isset($act) && $act == "del"){
      if($num_res == 1){
         $query  = "DELETE FROM $tbname WHERE workshop='synth07' AND uid=$uid";
         $result = $db->sql_query($query);
         if(!$result)
            $indication = "unknown error please try again";
         else
            $indication = stripslashes($data['first']) . " " . stripslashes($data['last']) . " Deleted Successfully!";
      }
      else{
         $indication = stripslashes($data['first']) . " " . stripslashes($data['last']) . " Does Not Exist in the Database, Delete Failed!";
      }
   } // if(isset($act) && $act == "del")
}
?>
