<?php
class faqClass extends MasterClass {

    ####################################################################################################################################################
    ####################################################################################################################################################

    //function add faq 
    public function addFAQ(){

     $result = array();

     if($this->checkRef()){

        $data = $this->cleanData();

        $this->dbConn();

        $this->query = "INSERT INTO FAQs VALUES ('','";
        $this->query .= $data['Category'];
        $this->query .= "','";
        $this->query .= $data['Question'];
        $this->query .= "','";
        $this->query .= $data['Answer'];
        $this->query .= "')";

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

        $this->dbClose();

        $result['Msg'] = 'The FAQ has been successfully added to the system.';
        $result['Status'] = '1';
        $result['Class'] = 'success';

      }

      return $result;

    }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //function edit a faq
    public function editFAQ(){

     $result = array();

     if($this->checkRef()){

        $data = $this->cleanData();

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

           $this->dbConn();

           $this->query = "UPDATE FAQs SET faq_question = '";
           $this->query .= $data['Question'];
           $this->query .= "', faq_category = '";
           $this->query .= $data['Category'];
           $this->query .= "', faq_answer = '";
           $this->query .= $data['Answer'];
           $this->query .= "' WHERE faq_nbr = '";
           $this->query .= $data['Id'];
           $this->query .= "'";

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

           $this->dbClose();

           $result['Msg'] = 'The FAQ was successfully updated in the system.';
           $result['Status'] = '1';
           $result['Class'] = 'success';

        }

      }

      return $result;

    }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //function get faq info
    public function getFAQInfo($id){

     $info = array();

      if($this->checkRef()){

        $this->dbConn();

        $this->query = "SELECT * FROM FAQs LEFT JOIN FAQ_Categories ON faq_category = faq_cat_nbr  WHERE faq_nbr = '";
        $this->query .= $id;
        $this->query .= "'";

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

        if($getInfo->num_rows > 0){

            $row = $getInfo->fetch_assoc();

            $info['Nbr'] = stripslashes($row['faq_nbr']);
            $info['Category'] = stripslashes($row['faq_category']);
            $info['Category_Name'] = stripslashes($row['faq_cat_name']);
            $info['Question'] = stripslashes($row['faq_question']);
            $info['Answer'] = stripslashes($row['faq_answer']);

        }

        $this->dbClose();

      }

      return $info;

    }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //function get faqs list
    public function getFAQs(){

     $info = array();

      if($this->checkRef()){

        $data = $this->cleanData();

        $this->dbConn();

        $this->query = "SELECT * FROM FAQs LEFT JOIN FAQ_Categories ON faq_category = faq_cat_nbr WHERE faq_nbr != '' ";
        if($data['Category'] != ''){

          $this->query .= "AND faq_category = '";
          $this->query .= $data['Category'];
          $this->query .= "' ";

        }        

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

        if($getInfo->num_rows > 0){

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

              $info[$i]['Nbr'] = stripslashes($row['faq_nbr']);
              $info[$i]['Category'] = stripslashes($row['faq_category']);
              $info[$i]['Category_Name'] = stripslashes($row['faq_cat_name']);
              $info[$i]['Question'] = stripslashes($row['faq_question']);
              $info[$i]['Answer'] = stripslashes($row['faq_answer']);
              $i++;

            }

        }

        $this->dbClose();

      }

      return $info;

    }

   ####################################################################################################################################################
   ####################################################################################################################################################

   //function get delete faq
   public function deleteFAQ(){

    $result = array();

    $data = $this->cleanData();

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

      $this->dbConn();

      $this->query = "DELETE FROM FAQs WHERE faq_nbr = '";
      $this->query .= $data['Id'];
      $this->query .= "'";

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

      $result['Status'] = '1';
      $result['Class'] = 'success';
      $result['Msg'] = 'The FAQ was successfully deleted.';

      $this->dbClose();

    }

    return $result;

  }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //function add category
    public function addCategory(){

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

     if($this->checkRef()){

        $data = $this->cleanData();

        $url = '';
        $url = $this->urlSafeString($data['Name']);

        $img = '';

        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/faqs/'.$fileName)){                                                          
                    
                  $thumb = $this->createThumbImage(THIS_DOMAIN.'/images/faqs/'.$fileName, 500, 500, 'images/faqs/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 FAQ_Categories VALUES ('','";
        $this->query .= $data['Name'];
        $this->query .= "','";
        $this->query .= $img;
        $this->query .= "')";

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

        $this->dbClose();

        $result['Msg'] .= 'The FAQ category information was successfully added.';

      }

      return $result;

    }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //function edit a category
    public function editCategory(){
      
      $result = array();
      $result['Msg'] = '';
      $result['Status'] = '1';
      $result['Class'] = 'success';
      $now = time();

     if($this->checkRef()){

        $data = $this->cleanData();
        $url = '';
        $url = $this->urlSafeString($data['Name']);

        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/faqs/'.$fileName)){                                                          
                      
                    $thumb = $this->createThumbImage(THIS_DOMAIN.'/images/faqs/'.$fileName, 500, 500, 'images/faqs/thumbs', $fileName);
                    if($thumb['Status'] === true){
  
                      $img = $fileName;
                      $result['Msg'] .= 'The profile image was successfully updated in the system.<br/>';
                      if(is_file(THIS_DOMAIN.'/images/faqs/'.$data['Original_Image'])){
                                     
                       unlink(THIS_DOMAIN.'/images/faqs/'.$data['Original_Image']);
 
                     }
                     if(is_file(THIS_DOMAIN.'/images/faqs/thumbs/'.$data['Original_Image'])){
                                     
                       unlink(THIS_DOMAIN.'/images/faqs/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['Msg'] .= $_FILES['Files']['type'][0];
                  $result['Class'] = 'error';               
                    
                }
                    
            }
 
          }             

           $this->dbConn();

           $this->query = "UPDATE FAQ_Categories SET ";
           $this->query .= "faq_cat_name = '";
           $this->query .= $data['Name'];
           $this->query .= "', faq_cat_img = '";
           $this->query .= $img;
           $this->query .= "' WHERE faq_cat_nbr = '";
           $this->query .= $data['Id'];
           $this->query .= "'";

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

           $this->dbClose();

           $result['Msg'] .= 'The FAQ category information was successfully updated in the system.';           
           $result['Image'] = $img;

        }

      }

      return $result;

    }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //function get category info
    public function getCategoryInfo($id){

     $info = array();

      if($this->checkRef()){

        $this->dbConn();

        $this->query = "SELECT * FROM FAQ_Categories WHERE faq_cat_nbr = '";
        $this->query .= $id;
        $this->query .= "'";

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

        if($getInfo->num_rows > 0){

            $row = $getInfo->fetch_assoc();

            $info['Nbr'] = stripslashes($row['faq_cat_nbr']);
            $info['Name'] = stripslashes($row['faq_cat_name']);
            $info['Image'] = stripslashes($row['faq_cat_img']);

        }

        $this->dbClose();

      }

      return $info;

    }

    ####################################################################################################################################################
    ####################################################################################################################################################

    //function get list of categories
    public function getCategories(){

     $info = array();

      $this->dbConn();

      $this->query = "SELECT * FROM FAQ_Categories ORDER BY faq_cat_name ASC";

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

      if($getInfo->num_rows > 0){

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

            $info[$i]['Nbr'] = stripslashes($row['faq_cat_nbr']);
            $info[$i]['Name'] = stripslashes($row['faq_cat_name']);
            $info[$i]['Image'] = stripslashes($row['faq_cat_img']);
            $i++;

          }

      }

      $this->dbClose();

      return $info;

    }

   ####################################################################################################################################################
   ####################################################################################################################################################

   //function delete category
   public function deleteCategory(){

    $result = array();

    $data = $this->cleanData();

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

      $info = $this->getCategoryInfo($data['Id']);

      if($info['Image'] != ''){

        if(is_file(THIS_DOMAIN.'/images/faqs/'.$info['Image'])){
                                     
          unlink(THIS_DOMAIN.'/images/faqs/'.$info['Image']);

        }
        if(is_file(THIS_DOMAIN.'/images/faqs/thumbs/'.$info['Image'])){
                        
          unlink(THIS_DOMAIN.'/images/faqs/thumbs/'.$info['Image']);

        }         

      }

      $this->dbConn();

      $this->query = "DELETE FROM FAQ_Categories WHERE faq_cat_nbr = '";
      $this->query .= $data['Id'];
      $this->query .= "'";

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

      $result['Status'] = '1';
      $result['Class'] = 'success';
      $result['Msg'] = 'The FAQ category was successfully deleted in the system.';

      $this->dbClose();

    }

    return $result;

  }


}
?>