<?php
class serviceClass extends MasterClass {

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

    //function add a service 
    public function addService(){

      $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/services/'.$fileName)){                                                          
                    
                  $thumb = $this->createThumbImage(THIS_DOMAIN.'/images/services/'.$fileName, 500, 500, 'images/services/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 Services VALUES ('','";
        $this->query .= $data['Category'];
        $this->query .= "','";
        $this->query .= $data['Name'];
        $this->query .= "','";
        $this->query .= $url;
        $this->query .= "','";
        $this->query .= $data['Keywords'];
        $this->query .= "','";
        $this->query .= $data['Description'];
        $this->query .= "','";
        $this->query .= $data['Listing_Description'];
        $this->query .= "','";        
        $this->query .= $data['Content'];
        $this->query .= "','";
        $this->query .= $img;
        $this->query .= "','";
        $this->query .= $data['Status'];
        $this->query .= "','";
        $this->query .= $data['Album'];
        $this->query .= "')";

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

        $this->dbClose();

        $result['Msg'] .= 'The service was successfully added to the system.';

      }

      return $result;

    }

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

    //function edit service info
    public function editService(){

      $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/services/'.$fileName)){                                                          
                      
                    $thumb = $this->createThumbImage(THIS_DOMAIN.'/images/services/'.$fileName, 500, 500, 'images/services/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/services/'.$data['Original_Image'])){
                                     
                       unlink(THIS_DOMAIN.'/images/services/'.$data['Original_Image']);
 
                     }
                     if(is_file(THIS_DOMAIN.'/images/services/thumbs/'.$data['Original_Image'])){
                                     
                       unlink(THIS_DOMAIN.'/images/services/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 Services SET ";
           $this->query .= "service_name = '";
           $this->query .= $data['Name'];
           $this->query .= "', service_category = '";
           $this->query .= $data['Category'];
           $this->query .= "', service_url = '";
           $this->query .= $url;
           $this->query .= "', service_keywords_tag = '";
           $this->query .= $data['Keywords'];
           $this->query .= "', service_description_tag = '";
           $this->query .= $data['Description'];
           $this->query .= "', service_listing_description = '";
           $this->query .= $data['Listing_Description'];           
           $this->query .= "', service_content = '";
           $this->query .= $data['Content'];
           $this->query .= "', service_img = '";
           $this->query .= $img;
           $this->query .= "', service_status = '";
           $this->query .= $data['Status'];
           $this->query .= "', service_gallery_album = '";
           $this->query .= $data['Album'];
           $this->query .= "' WHERE service_nbr = '";
           $this->query .= $data['Id'];
           $this->query .= "'";

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

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

        }

      }

      return $result;

    }

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

    //function get service information
    public function getServiceInfo($id){

     $info = array();

      if($this->checkRef()){

        $this->dbConn();

        $this->query = "SELECT * FROM Services ";
        $this->query .= "LEFT JOIN Service_Categories ON service_category = service_category_nbr ";
        $this->query .= "LEFT JOIN Albums ON service_gallery_album = album_nbr ";
        $this->query .= "WHERE ";
        if($id != ''){
        
          $this->query .= "service_nbr = '";
          $this->query .= $id;

        }else{

          $url = $_SERVER['REQUEST_URI'];
          $urlArray = explode('/', $url);
          $pageName = $urlArray[count($urlArray) - 1];

          $this->query .= "service_url = '";
          $this->query .= $pageName;

        }
        $this->query .= "'";

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

        if($getInfo->num_rows > 0){

            $row = $getInfo->fetch_assoc();

            $info['Nbr'] = stripslashes($row['service_nbr']);
            $info['Name'] = stripslashes($row['service_name']);
            $info['Category_Nbr'] = stripslashes($row['service_category']);
            $info['Category_Name'] = stripslashes($row['service_category_name']);
            $info['Url'] = stripslashes($row['service_url']);
            $info['Keywords'] = stripslashes($row['service_keywords_tag']);
            $info['Description'] = stripslashes($row['service_description_tag']);
            $info['Listing_Description'] = stripslashes($row['service_listing_description']);
            $info['Content'] = stripslashes($row['service_content']);
            $info['Img'] = stripslashes($row['service_img']);
            $info['Status'] = stripslashes($row['service_status']);
            $info['Gallery_Album'] = stripslashes($row['service_gallery_album']);
            $info['Gallery_Category'] = stripslashes($row['album_category']);

        }

        $this->dbClose();

      }

      return $info;

    }

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

    //function get services
    public function getServices(){

      $info = array();

      if($this->checkRef()){

        $this->dbConn();

        $data = $this->cleanData();

        $this->query = "SELECT * FROM Services LEFT JOIN Service_Categories ON service_category = service_category_nbr ";
        if($data['Category'] != ''){

          $this->query .= "WHERE service_category = '";
          $this->query .= $data['Category'];
          $this->query .= "' ";

        }
        $this->query .= "ORDER BY service_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['service_nbr']);
              $info[$i]['Name'] = stripslashes($row['service_name']);
              $info[$i]['Url'] = stripslashes($row['service_url']);
              $info[$i]['Category_Nbr'] = stripslashes($row['service_category']);
              $info[$i]['Category_Name'] = stripslashes($row['service_category_name']);
              $info[$i]['Keywords_Tag'] = stripslashes($row['service_keywords_tag']);
              $info[$i]['Description_Tag'] = stripslashes($row['service_description_tag']);
              $info[$i]['Listing_Description'] = stripslashes($row['service_listing_description']);
              $info[$i]['Content'] = stripslashes($row['service_content']);
              $info[$i]['Img'] = stripslashes($row['service_img']);
              $info[$i]['Status'] = stripslashes($row['service_status']);
              $info[$i]['Gallery_Album'] = stripslashes($row['service_gallery_album']);
              $i++;

            }

        }

        $this->dbClose();

      }

      return $info;

    }

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

   //function delete a service
   public function deleteService(){

    $result = array();

    $data = $this->cleanData();

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

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

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

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

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

        }         

      }

      $this->dbConn();

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

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

      $result['Status'] = '1';
      $result['Class'] = 'success';
      $result['Msg'] = 'The service was successfully removed from the system.';

      $this->dbClose();

    }

    return $result;

  }

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

    //function get service categories
    public function getServiceCategories(){

      $info = array();

      if($this->checkRef()){

        $this->dbConn();

        $data = $this->cleanData();

        $this->query = "SELECT * FROM Service_Categories ";
        $this->query .= "ORDER BY service_category_nbr ASC";

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

        if($getInfo->num_rows > 0){

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

              $info[$i]['Nbr'] = stripslashes($row['service_category_nbr']);
              $info[$i]['Name'] = stripslashes($row['service_category_name']);              
              $info[$i]['Image'] = stripslashes($row['service_category_image']);              
              $i++;

            }

        }

        $this->dbClose();

      }

      return $info;

    }

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

    //function add category
    public function addServiceCategory(){

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

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

        $this->dbClose();

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

      }

      return $result;

    }

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

    //function edit a category
    public function editServiceCategory(){
      
      $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/services/'.$fileName)){                                                          
                      
                    $thumb = $this->createThumbImage(THIS_DOMAIN.'/images/services/'.$fileName, 500, 500, 'images/services/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/services/'.$data['Original_Image'])){
                                     
                       unlink(THIS_DOMAIN.'/images/services/'.$data['Original_Image']);
 
                     }
                     if(is_file(THIS_DOMAIN.'/images/services/thumbs/'.$data['Original_Image'])){
                                     
                       unlink(THIS_DOMAIN.'/images/services/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 Service_Categories SET ";
           $this->query .= "service_category_name = '";
           $this->query .= $data['Name'];
           $this->query .= "', service_category_image = '";
           $this->query .= $img;
           $this->query .= "', service_category_url = '";
           $this->query .= $url;
           $this->query .= "' WHERE service_category_nbr = '";
           $this->query .= $data['Id'];
           $this->query .= "'";

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

           $this->dbClose();

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

        }

      }

      return $result;

    }

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

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

     $info = array();

      if($this->checkRef()){

        $this->dbConn();

        $this->query = "SELECT * FROM Service_Categories WHERE service_category_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['service_category_nbr']);
            $info['Name'] = stripslashes($row['service_category_name']);
            $info['Image'] = stripslashes($row['service_category_image']);
            $info['Url'] = stripslashes($row['service_category_url']);

        }

        $this->dbClose();

      }

      return $info;

    }

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

   //function delete category
   public function deleteServiceCategory(){

    $result = array();

    $data = $this->cleanData();

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

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

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

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

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

        }         

      }

      $this->dbConn();

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

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

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

      $this->dbClose();

    }

    return $result;

  }

}
?>