<?php
class staffClass extends MasterClass {

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

    //function add a staff member
    public function addStaffMember(){

     $result = array();         
     $result['Status'] = true;
     $result['Class'] = 'success';

     $now = time();

     if($this->checkRef()){

        $data = $this->cleanData();

        $this->dbConn();

        $img = '';

        if(isset($_FILES)) {

          foreach($_FILES['Files']['tmp_name'] as $key=> $value){

            $imgName = $now.'_'.$this->urlSafeString($_FILES['Files']['name'][$key],true);
            $imgProcess = $this->createThumbImage($_FILES['Files']['tmp_name'][$key], STAFF_MAX_WIDTH, STAFF_MAX_HEIGHT, 'images/staff', $imgName);               
            if($imgProcess['Status'] === true){

              $result['FileName'] = $imgName;              
              $img = $imgName;

            }else{

              $result['Msg'] .= 'The image file you uploaded did not process. Please make sure you upload the proper format and have an image at least 400px wide and 500px tall.<br/>';
              $result['Status'] = false;
              $result['Class'] = 'error';

            }

          }

        }

        $this->query = "INSERT INTO Staff VALUES ('','";
        $this->query .= $data['Name'];
        $this->query .= "','";
        $this->query .= $data['Title'];
        $this->query .= "','";
        $this->query .= $data['Phone'];
        $this->query .= "','";
        $this->query .= $data['Email'];
        $this->query .= "','";
        $this->query .= $img;
        $this->query .= "','";
        $this->query .= $data['Bio'];
        $this->query .= "')";

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

        $this->dbClose();

        $result['Msg'] = 'The staff member information has been added to the system.';

      }

      return $result;

    }

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

    //function edit staff member info
    public function editStaffMember(){

     $result = array();
     $result['Status'] = true;
     $result['Class'] = 'success';

     $now  = time();

     if($this->checkRef()){

        $data = $this->cleanData();

        $img = $data['Img'];

        if(isset($_FILES)) {

          foreach($_FILES['Files']['tmp_name'] as $key=> $value){

            $imgName = $now.'_'.$this->urlSafeString($_FILES['Files']['name'][$key],true);
            $imgProcess = $this->createThumbImage($_FILES['Files']['tmp_name'][$key], STAFF_MAX_WIDTH, STAFF_MAX_HEIGHT, 'images/staff', $imgName);               
            if($imgProcess['Status'] === true){

              $result['ImageName'] = $imgName;
              $img = $imgName;

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

                if(is_file(THIS_DOMAIN.'/images/staff/'.$data['Img'])){

                  unlink(THIS_DOMAIN.'/images/staff/'.$data['Img']);
      
                }

              }

            }else{

              $result['Msg'] .= 'The image file you uploaded did not process. Please make sure you upload the proper format and have an image at least 400px wide and 500px tall.<br/>';
              $result['Status'] = false;
              $result['Class'] = 'error';

            }

          }

        }        

        $this->dbConn();

        $this->query = "UPDATE Staff SET staff_name = '";
        $this->query .= $data['Name'];
        $this->query .= "', staff_title = '";
        $this->query .= $data['Title'];
        $this->query .= "', staff_phone = '";
        $this->query .= $data['Phone'];
        $this->query .= "', staff_email = '";
        $this->query .= $data['Email'];
        $this->query .= "', staff_bio = '";
        $this->query .= $data['Bio'];        
        $this->query .= "', staff_img = '";
        $this->query .= $img;
        $this->query .= "' WHERE staff_nbr = '";
        $this->query .= $data['Id'];
        $this->query .= "'";

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

        $this->dbClose();

        $result['Msg'] = 'The staff member information has been successfully updated.';

      }

      return $result;

    }

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

    //function staff member info
    public function getStaffMemberInfo($id){

      $info = array();

      if($this->checkRef()){

        $this->dbConn();

        $this->query = "SELECT * FROM Staff WHERE staff_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['staff_nbr']);
            $info['Name'] = stripslashes($row['staff_name']);
            $info['Title'] = stripslashes($row['staff_title']);
            $info['Phone'] = stripslashes($row['staff_phone']);
            $info['Email'] = stripslashes($row['staff_email']);
            $info['Img'] = stripslashes($row['staff_img']);
            $info['Bio'] = stripslashes($row['staff_bio']);

        }

        $this->dbClose();

      }

      return $info;

    }

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

    //function get list of staff members
    public function getStaffMembers(){

     $info = array();

      if($this->checkRef()){

        $this->dbConn();

        $this->query = "SELECT * FROM Staff ORDER BY staff_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['staff_nbr']);
              $info[$i]['Name'] = stripslashes($row['staff_name']);
              $info[$i]['Title'] = stripslashes($row['staff_title']);
              $info[$i]['Phone'] = stripslashes($row['staff_phone']);
              $info[$i]['Email'] = stripslashes($row['staff_email']);
              $info[$i]['Img'] = stripslashes($row['staff_img']);
              $info[$i]['Bio'] = stripslashes($row['staff_bio']);
              $i++;

            }

        }

        $this->dbClose();

      }

      return $info;

    }

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

   //function get delete a staff member
   public function deleteStaffMember(){

    $result = array();

    $data = $this->cleanData();

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

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

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

        if(is_file(THIS_DOMAIN.'/images/staff/'.$info['Img'])){

          unlink(THIS_DOMAIN.'/images/staff/'.$info['Img']);

        }

      }

      $this->dbConn();

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

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

      $result['Status'] = '1';
      $result['Class'] = 'success';
      $result['Msg'] = 'The staff member has been deleted from the system.';

      $this->dbClose();

    }

    return $result;

  }

}

?>