<?php
class resourcesClass extends MasterClass {

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

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

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

        $this->dbClose();

        $result['Msg'] .= 'The resource 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/resources/'.$fileName)){                                                          
                      
                    $thumb = $this->createThumbImage(THIS_DOMAIN.'/images/resources/'.$fileName, 500, 500, 'images/resources/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/resources/'.$data['Original_Image'])){
                                     
                       unlink(THIS_DOMAIN.'/images/resources/'.$data['Original_Image']);
 
                     }
                     if(is_file(THIS_DOMAIN.'/images/resources/thumbs/'.$data['Original_Image'])){
                                     
                       unlink(THIS_DOMAIN.'/images/resources/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 Resource_Categories SET ";
           $this->query .= "res_cat_name = '";
           $this->query .= $data['Name'];
           $this->query .= "', res_cat_image = '";
           $this->query .= $img;
           $this->query .= "', res_cat_url = '";
           $this->query .= $url;
           $this->query .= "', res_cat_status = '";
           $this->query .= $data['Status'];
           $this->query .= "' WHERE res_cat_nbr = '";
           $this->query .= $data['Id'];
           $this->query .= "'";

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

           $this->dbClose();

           $result['Msg'] .= 'The resource 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 Resource_Categories WHERE res_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['res_cat_nbr']);
            $info['Name'] = stripslashes($row['res_cat_name']);
            $info['Image'] = stripslashes($row['res_cat_image']);
            $info['Url'] = stripslashes($row['res_cat_url']);
            $info['Status'] = stripslashes($row['res_cat_status']);

        }

        $this->dbClose();

      }

      return $info;

    }

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

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

     $info = array();

      $this->dbConn();

      $this->query = "SELECT * FROM Resource_Categories ORDER BY res_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['res_cat_nbr']);
            $info[$i]['Name'] = stripslashes($row['res_cat_name']);
            $info[$i]['Image'] = stripslashes($row['res_cat_image']);
            $info[$i]['Url'] = stripslashes($row['res_cat_url']);
            $info[$i]['Status'] = stripslashes($row['res_cat_status']);
            $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/resources/'.$info['Image'])){
                                     
          unlink(THIS_DOMAIN.'/images/resources/'.$info['Image']);

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

        }         

      }

      $this->dbConn();

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

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

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

      $this->dbClose();

    }

    return $result;

  }

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

  //function to get page information
  public function getResPageInfo($id){

      $info = array();

      if($this->checkRef()){
          
        if($id != ''){

          $this->dbConn();

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

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

          if($getPage->num_rows > 0){

            $row = $getPage->fetch_assoc();

            $info['Nbr'] = stripslashes($row['res_nbr']);
            $info['Name'] = stripslashes($row['res_name']);
            $info['Url'] = stripslashes($row['res_url']);
            $info['Keywords_Tag'] = stripslashes($row['res_keywords_tag']);
            $info['Description_Tag'] = stripslashes($row['res_description_tag']);
            $info['Content'] = stripslashes($row['res_content']);
            $info['Date_Created'] = stripslashes($row['res_date_created']);
            $info['Status'] = stripslashes($row['res_status']);
            $info['Last_Updated'] = stripslashes($row['res_last_update']);
            $info['Categories'] = array();

            $this->query = "SELECT * FROM Resource_Category_Resources ";
            $this->query .= "LEFT JOIN Resource_Categories ON res_cat_res_category = res_cat_nbr ";
            $this->query .= "WHERE res_cat_res_resource = '";
            $this->query .= $id;
            $this->query .= "'";

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

            if($getCats->num_rows > 0){

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

                $info['Categories'][$i]['Nbr'] = $row['res_cat_nbr'];
                $info['Categories'][$i]['Name'] = $row['res_cat_name'];
                $i++;

              }

            }

          }

          $this->dbClose();
            
        }

      }

      return $info;

    }

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

    //function to update page information
    public function updatePageInfo(){
        
        $result = array();
        
        if($this->checkRef()){
        
            $data = $this->cleanData();

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

                $this->dbConn();
                
                $now = time();

                if(isset($data['PageContent'])){

                    $content = $data['PageContent'];

                }else{

                    $content = $data['Desc'];

                }
           
                $this->query = "UPDATE Pages SET page_title_tag = '";
                $this->query .= substr($data['Title_Tag'],0,65);
                $this->query .= "', page_keywords_tag = '";
                $this->query .= $data['Keywords_Tag'];
                $this->query .= "', page_description_tag = '";
                $this->query .= substr($data['Description_Tag'],0,155);
                $this->query .= "', page_content = '";
                $this->query .= $content;
                $this->query .= "', page_status = '";
                $this->query .= $data['Status'];
                $this->query .= "', page_last_update = '";
                $this->query .= $now;                
                $this->query .= "' WHERE page_nbr = '";
                $this->query .= $this->db->real_escape_string($data['PageId']);
                $this->query .= "'"; 

                $this->db->query($this->query);
               
                $this->dbClose();   
                    
                $result['Status'] = 1;
                $result['Class'] = 'success';
                $result['Msg'] = 'The page information has been successfully updated.';
                
            }else{

                $result['Status'] = 0;
                $result['Class'] = 'error';
                $result['Msg'] = 'No Page Id';            

            }
        
        }
        
        return $result;

    }

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

    //function to get pages
    public function getPages(){
        
        $info = array();
        
        if($this->checkRef()){
            
            $this->dbConn();
            
            $this->query = "SELECT * FROM Pages ORDER BY page_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['page_nbr']);
                    $info[$i]['Name'] = stripslashes($row['page_name']);
                    $info[$i]['Title_Tag'] = stripslashes($row['page_title_tag']);
                    $info[$i]['Keywords_Tag'] = stripslashes($row['page_keywords_tag']);
                    $info[$i]['Description_Tag'] = stripslashes($row['page_description_tag']);
                    $info[$i]['Content'] = stripslashes($row['page_content']);
                    $info[$i]['Date_Created'] = stripslashes($row['page_date_created']);
                    $info[$i]['Status'] = stripslashes($row['page_status']);  
                    $info[$i]['Last_Updated'] = stripslashes($row['page_last_update']);  
                    $i++;
                    
                }
                
            }
            
            $this->dbClose();
            
        }
        
        return $info;
        
    }
 
    ####################################################################################################################################################	
    ####################################################################################################################################################	

    //function to delete content managed page
    function deleteCMP(){
        
      $result = array();
        
      if($this->checkRef()){
           
        $data = $this->cleanData();
           
        if($data['Id'] != ''){
               
          $this->dbConn();
            
          $this->query = "DELETE FROM Pages WHERE page_nbr = '";
          $this->query .= $this->db->real_escape_string($data['Id']);
          $this->query .= "'";
            
          $this->db->query($this->query);
            
          $this->dbClose();
            
          $result['Status'] = 1;
          $result['Msg'] = 'The content managed page has been successfully removed from the system.';
          $result['Class'] = 'success';
               
        }
           
      }
       
      return $result;
        
    }

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

    //function to get page information
    public function getPageInfo($PageId){

      $info = array();

      if($this->checkRef()){
          
          if($PageId != ''){

              $this->dbConn();

              $this->query = "SELECT * FROM Pages WHERE page_nbr = '";
              $this->query .= $this->db->real_escape_string($PageId);
              $this->query .= "'";

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

              if($getPage->num_rows > 0){

                  $row = $getPage->fetch_assoc();

                  $info['Nbr'] = stripslashes($row['page_nbr']);
                  $info['Name'] = stripslashes($row['page_name']);
                  $info['Title_Tag'] = stripslashes($row['page_title_tag']);
                  $info['Keywords_Tag'] = stripslashes($row['page_keywords_tag']);
                  $info['Description_Tag'] = stripslashes($row['page_description_tag']);
                  $info['Content'] = stripslashes($row['page_content']);
                  $info['Date_Created'] = stripslashes($row['page_date_created']);
                  $info['Status'] = stripslashes($row['page_status']);
                  $info['Last_Updated'] = stripslashes($row['page_last_update']);

              }

              $this->dbClose();
              
          }

      }

      return $info;

    }

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

  //function to update page information
  public function updateResPageInfo(){
      
    $result = array();
    
    $now = time();
      
    $data = $this->cleanData();

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

    $this->dbConn();
        
    $this->query = "UPDATE Resource_Pages SET res_name = '";
    $this->query .= $data['Name'];
    $this->query .= "', res_url = '";
    $this->query .= $url;
    $this->query .= "', res_keywords_tag = '";
    $this->query .= $data['Keywords_Tag'];
    $this->query .= "', res_description_tag = '";
    $this->query .= $data['Description_Tag'];
    $this->query .= "', res_content = '";
    $this->query .= $data['Content'];
    $this->query .= "', res_status = '";
    $this->query .= $data['Status'];
    $this->query .= "', res_last_update = '";
    $this->query .= $now;                
    $this->query .= "' WHERE res_nbr = '";
    $this->query .= $this->db->real_escape_string($data['Id']);
    $this->query .= "'"; 

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

    $this->query = "DELETE FROM Resource_Category_Resources WHERE res_cat_res_resource = '";
    $this->query .= $this->db->real_escape_string($data['Id']);
    $this->query .= "'";
    
    $this->db->query($this->query);              

    foreach($data as $key=>$value){

      if(strpos($key,'Category_') !== false){

        if($value != ''){

          $this->query = "INSERT INTO Resource_Category_Resources VALUES ('','";
          $this->query .= $value;
          $this->query .= "','";
          $this->query .= $data['Id'];
          $this->query .= "')";

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

        }

      }

    }

    $this->dbClose();

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

  }

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

  //function to get resource pages
  public function getResPages(){
      
    $info = array();
      
    if($this->checkRef()){

      $data = $this->cleanData();
          
      $this->dbConn();

      if($data['Category'] == ''){
          
        $this->query = "SELECT * FROM Resource_Pages ";

      }else{

        $this->query = "SELECT * FROM Resource_Category_Resources LEFT JOIN Resource_Pages ON res_cat_res_resource = res_nbr ";
        $this->query .= "WHERE res_cat_res_category = '";
        $this->query .= $data['Category'];
        $this->query .= "' ";

      }
      $this->query .= "GROUP BY res_nbr ORDER BY res_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['res_nbr']);
          $info[$i]['Name'] = stripslashes($row['res_name']);
          $info[$i]['Url'] = stripslashes($row['res_url']);
          $info[$i]['Keywords_Tag'] = stripslashes($row['res_keywords_tag']);
          $info[$i]['Description_Tag'] = stripslashes($row['res_description_tag']);
          $info[$i]['Content'] = stripslashes($row['res_content']);
          $info[$i]['Date_Created'] = stripslashes($row['res_date_created']);
          $info[$i]['Status'] = stripslashes($row['res_status']);  
          $info[$i]['Last_Updated'] = stripslashes($row['res_last_update']);  
          $i++;
                  
        }
              
      }
          
      $this->dbClose();
          
    }
      
    return $info;
      
  }

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

  //function to delete content managed page
  function deleteRes(){
      
    $result = array();
      
    if($this->checkRef()){
         
      $data = $this->cleanData();
         
      if($data['Id'] != ''){
             
        $this->dbConn();
             
        $this->query = "DELETE FROM Resource_Pages WHERE res_nbr = '";
        $this->query .= $this->db->real_escape_string($data['Id']);
        $this->query .= "'";
             
        $this->db->query($this->query);

        $this->query = "DELETE FROM Resource_Category_Resources WHERE res_cat_res_resource = '";
        $this->query .= $this->db->real_escape_string($data['Id']);
        $this->query .= "'";
             
        $this->db->query($this->query);             
             
        $this->dbClose();
             
        $result['Status'] = 1;
        $result['Msg'] = 'The resource page has been successfully removed from the system.';
        $result['Class'] = 'success';
             
      }
         
    }
     
    return $result;
      
  }    

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

  //function for adding a new resource page
  public function addResPage(){

    $result = array();

    $data = $this->cleanData();

    $now = time();

    $this->dbConn();

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

    $this->query = "INSERT INTO Resource_Pages VALUES ('','";
    $this->query .= $data['Name'];
    $this->query .= "','";
    $this->query .= $url;    
    $this->query .= "','";
    $this->query .= $data['Keywords_Tag'];
    $this->query .= "','";
    $this->query .= $data['Description_Tag'];
    $this->query .= "','";
    $this->query .= $data['Content'];
    $this->query .= "','";
    $this->query .= $now;
    $this->query .= "','";
    $this->query .= $data['Status'];
    $this->query .= "','";
    $this->query .= $now;
    $this->query .= "')";   

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

    $resId = $this->db->insert_id;

    if($resId != ''){

      foreach($data as $key=>$value){

        if(strpos($key,'Category_') !== false){

          if($value != ''){

            $this->query = "INSERT INTO Resource_Category_Resources VALUES ('','";
            $this->query .= $value;
            $this->query .= "','";
            $this->query .= $resId;
            $this->query .= "')";

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

          }

        }

      }

    }

    $result['Msg'] = 'The resource page was successfully created.';
    $result['Class'] = 'success';
    $result['Status'] = 1;    

    $this->dbClose();

    return $result;

  }

}

?>