<?php
class eventClass extends MasterClass { 
	
    ####################################################################################################################################################	
    ####################################################################################################################################################	
    
    //function for adding an event
    public function addEvent(){

        $data = $this->cleanData();

        $now = time();
        $result = array();
        $result['Msg'] .= '';
        $result['Status'] = '1';
        $result['Class'] = 'success';

        $img = '';

        if(isset($data['Start_Month']) && $data['Start_Month'] != '' && 
            isset($data['Start_Day']) && $data['Start_Day'] != '' && 
            isset($data['Start_Year']) && $data['Start_Year'] != ''){

            $sDate = mktime(0,0,0,$data['Start_Month'],$data['Start_Day'],$data['Start_Year']);

        }else{

            $sDate = 0;

        }

        if(isset($data['End_Month']) && $data['End_Month'] != '' && 
            isset($data['End_Day']) && $data['End_Day'] != '' && 
            isset($data['End_Year']) && $data['End_Year'] != ''){

            $eDate = mktime(0,0,0,$data['End_Month'],$data['End_Day'],$data['End_Year']);

        }else{

            $eDate = 0;

        }        
        
        if(isset($_FILES)) {
          
          if(is_file($_FILES['Files']['tmp_name'][0])){
              
              if($this->checkType($_FILES['Files']['type'][0],ALLOWED_FILES_IMAGES)){  

                $canvas = $_FILES['Files']['tmp_name'][0];
                $fileName = $now.'_'.$this->urlSafeString($_FILES['Files']['name'][0], true); 

                if(move_uploaded_file($canvas,THIS_DOMAIN.'/images/events/'.$fileName)){                                                          
                    
                  $thumb = $this->createThumbImage(THIS_DOMAIN.'/images/events/'.$fileName, 395, 254, 'images/events/thumbs', $fileName);
                  if($thumb['Status'] === true){

                    $img = $fileName;
                    $result['Msg'] .= 'The image was successfully added to the system.<br/>';

                  }else{

                    $result['Msg'] .= $thumb['Msg'].'<br/>';
                    $result['Class'] = 'error';

                  }
                    
                }else{

                  $result['Msg'] .= 'The file you uploaded could not be moved.<br/>';                                  
                  $result['Class'] = 'error';

                }

              }else{
                  
                $result['Msg'] .= 'The File you are attempting to upload is not an approved Files type. You can only upload image Files.<br/>';
                $result['Class'] = 'error';               
                  
              }
                  
          }

        }
        
        $this->dbConn();

        $this->query = "INSERT INTO Events VALUES ('','";
        $this->query .= $data['Name'];
        $this->query .= "','";
        $this->query .= $sDate;
        $this->query .= "','";
        $this->query .= $eDate;
        $this->query .= "','";
        $this->query .= $data['Details'];
        $this->query .= "','";
        $this->query .= $img;
        $this->query .= "')";

        $this->db->query($this->query);

        $result['Msg'] = 'New event successfully added into the system.';

        $this->dbClose();

        return $result;

    }

    ####################################################################################################################################################	
    ####################################################################################################################################################	
    
    //function for editing an event
    public function editEvent(){

        $result = array();
        $result['Msg'] = '';
        $result['Status'] = '1';
        $result['Class'] = 'success';
        $now = time(); 

        $data = $this->cleanData();

        if(isset($data['Start_Month']) && $data['Start_Month'] != '' && 
          isset($data['Start_Day']) && $data['Start_Day'] != '' && 
          isset($data['Start_Year']) && $data['Start_Year'] != ''){

          $sDate = mktime(0,0,0,$data['Start_Month'],$data['Start_Day'],$data['Start_Year']);

        }else{

            $sDate = 0;

        }

        if(isset($data['End_Month']) && $data['End_Month'] != '' && 
            isset($data['End_Day']) && $data['End_Day'] != '' && 
            isset($data['End_Year']) && $data['End_Year'] != ''){

            $eDate = mktime(0,0,0,$data['End_Month'],$data['End_Day'],$data['End_Year']);

        }else{

            $eDate = 0;

        } 

        if($data['Id'] != ''){

          $img = $data['Original_Image'];
 
          if(isset($_FILES)) {
            
            if(is_file($_FILES['Files']['tmp_name'][0])){
                
                if($this->checkType($_FILES['Files']['type'][0],ALLOWED_FILES_IMAGES)){  
  
                  $canvas = $_FILES['Files']['tmp_name'][0];
                  $fileName = $now.'_'.$this->urlSafeString($_FILES['Files']['name'][0], true); 
  
                  if(move_uploaded_file($canvas,THIS_DOMAIN.'/images/events/'.$fileName)){                                                          
                      
                    $thumb = $this->createThumbImage(THIS_DOMAIN.'/images/events/'.$fileName, 395, 254, 'images/events/thumbs', $fileName);
                    if($thumb['Status'] === true){
  
                      $img = $fileName;
                      $result['Msg'] .= 'The image was successfully updated in the system.<br/>';
                      if(is_file(THIS_DOMAIN.'/images/events/'.$data['Original_Image'])){
                                     
                       unlink(THIS_DOMAIN.'/images/events/'.$data['Original_Image']);
 
                     }
                     if(is_file(THIS_DOMAIN.'/images/events/thumbs/'.$data['Original_Image'])){
                                     
                       unlink(THIS_DOMAIN.'/images/events/thumbs/'.$data['Original_Image']);
 
                     }                    
  
                    }else{
  
                      $result['Msg'] .= 'The original image is still being utilized due to the following error:<br/>';
                      $result['Msg'] .= $thumb['Msg'].'<br/>';
                      $result['Class'] = 'error';
  
                    }
                      
                  }else{
  
                    $result['Msg'] .= 'The original image is still being utilized due to the following error:<br/>';
                    $result['Msg'] .= 'The file you uploaded could not be moved.<br/>';
                    $result['Class'] = 'error';
  
                  }
  
                }else{
                    
                  $result['Msg'] .= 'The original image is still being utilized due to the following error:<br/>';
                  $result['Msg'] .= 'The File you are attempting to upload is not an approved Files type. You can only upload image Files.<br/>';
                  $result['Class'] = 'error';               
                    
                }
                    
            }
 
          }          

           $this->dbConn();

           $this->query = "UPDATE Events SET ";
           $this->query .= "event_name = '";
           $this->query .= $data['Name'];
           $this->query .= "', event_start_date = '";
           $this->query .= $sDate;
           $this->query .= "', event_end_date = '";
           $this->query .= $eDate;
           $this->query .= "', event_details = '";
           $this->query .= $data['Details'];
           $this->query .= "', event_image = '";
           $this->query .= $img;
           $this->query .= "' WHERE event_nbr = '";
           $this->query .= $data['Id'];
           $this->query .= "'";

           $this->db->query($this->query);

           $this->dbClose();
           
           $result['Msg'] .= 'The event information was successfully updated in the system.';
           $result['Image'] = $img;

        }
        
        return $result;

    }

    ####################################################################################################################################################	
    ####################################################################################################################################################	
    
    //function for getting event info
    public function getEventInfo($id){

        $info = array();

        $this->dbConn();

        $this->query = "SELECT * FROM Events WHERE event_nbr = '";
        $this->query .= $this->db->real_escape_string($id);
        $this->query .= "'";

        $getInfo = $this->db->query($this->query);

        if($getInfo->num_rows > 0){

          $row = $getInfo->fetch_assoc();

          $info['Nbr'] = stripslashes($row['event_nbr']);
          $info['Name'] = stripslashes($row['event_name']);
          $info['Start_Date'] = stripslashes($row['event_start_date']);
          $info['End_Date'] = stripslashes($row['event_end_date']);
          $info['Details'] = stripslashes($row['event_details']);
          $info['Img'] = stripslashes($row['event_image']);

        }

        $this->dbClose();

        return $info;

    }

    ####################################################################################################################################################	
    ####################################################################################################################################################	
    
    //function for getting events
    public function getEvents(){

      $info = array();

      $this->dbConn();

      $data = $this->cleanData();

      $this->dbConn();

      $this->query = "SELECT * FROM Events WHERE event_nbr != '' ";
      if(isset($data['Start_Month']) && $data['Start_Month'] != '' && 
          isset($data['Start_Day']) && $data['Start_Day'] != '' && 
          isset($data['Start_Year']) && $data['Start_Year'] != ''){

          $sDate = mktime(0,0,0,$data['Start_Month'],$data['Start_Day'],$data['Start_Year']);
          $this->query .= "AND event_start_date >= '";
          $this->query .= $sDate;
          $this->query .= "' ";

      }

      if(isset($data['End_Month']) && $data['End_Month'] != '' && 
          isset($data['End_Day']) && $data['End_Day'] != '' && 
          isset($data['End_Year']) && $data['End_Year'] != ''){

          $eDate = mktime(0,0,0,$data['End_Month'],$data['End_Day'],$data['End_Year']);
          $this->query .= "AND event_end_date <= '";
          $this->query .= $eDate;
          $this->query .= "' ";

      }
      $this->query .= "ORDER BY event_start_date DESC";

      $getInfo = $this->db->query($this->query);

      if($getInfo->num_rows > 0){

        $i=0;
        while($row = $getInfo->fetch_assoc()){

          $info[$i]['Nbr'] = stripslashes($row['event_nbr']);
          $info[$i]['Name'] = stripslashes($row['event_name']);
          $info[$i]['Start_Date'] = stripslashes($row['event_start_date']);
          $info[$i]['End_Date'] = stripslashes($row['event_end_date']);
          $info[$i]['Details'] = stripslashes($row['event_details']);
          $info[$i]['Img'] = stripslashes($row['event_image']);
          $i++;

        }

      }

      $this->dbClose();

      return $info;

  }    

    ####################################################################################################################################################	
    ####################################################################################################################################################	
    
    //function for deleting an event
    public function deleteEvent(){

      $result = array();

      $data = $this->cleanData();
  
      if($data['Id'] != ''){
  
        $info = $this->getEventInfo($data['Id']);
  
        if($info['Img'] != ''){
  
          if(is_file(THIS_DOMAIN.'/images/events/'.$info['Img'])){
                                       
            unlink(THIS_DOMAIN.'/images/events/'.$info['Img']);
  
          }
          if(is_file(THIS_DOMAIN.'/images/events/thumbs/'.$info['Img'])){
                          
            unlink(THIS_DOMAIN.'/images/events/thumbs/'.$info['Img']);
  
          }         
  
        }
  
        $this->dbConn();
  
        $this->query = "DELETE FROM Events WHERE event_nbr = '";
        $this->query .= $data['Id'];
        $this->query .= "'";
  
        $this->db->query($this->query);
  
        $result['Status'] = '1';
        $result['Class'] = 'success';
        $result['Msg'] = 'The event was successfully removed from the system.';
  
        $this->dbClose();
  
      }
  
      return $result;
    }


}
?>