<?php

class docClass extends MasterClass {
	
    ####################################################################################################################################################	
    ####################################################################################################################################################	

    //function to add a document category
    public function addDocCategory(){

      $result = array();

      if($this->checkRef()){

        $data = $this->cleanData();

        $this->dbConn();

        $this->query = "INSERT INTO Document_Categories VALUES ('','";
        $this->query .= $data['Name'];
        $this->query .= "','";
        $this->query .= $data['Hidden'];
        $this->query .= "')";

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

        $this->dbClose();

        $result['Msg'] = 'The new doc category was successfully added.';
        $result['Class'] = 'success';
        $result['Status'] = '1';

      }

      return $result;

    }

    ####################################################################################################################################################	
    ####################################################################################################################################################	
    //function to edit category
    public function editDocCategory(){

      $result = array();

      if($this->checkRef()){

        $data = $this->cleanData();

        $this->dbConn();

        $this->query = "UPDATE Document_Categories SET ";
        $this->query .= "doc_category_name = '";
        $this->query .= $data['Name'];
        $this->query .= "', doc_category_hidden = '";
        $this->query .= $data['Hidden'];
        $this->query .= "' WHERE doc_category_nbr = '";
        $this->query .= $data['Id'];
        $this->query .= "'";

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

        $result['Status'] = '1';
        $result['Class'] = 'success';
        $result['Msg'] = 'The doc category was successfully updated.';

        $this->dbClose();

      }

      return $result;      

    }

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

    //function to get document category info
    public function getGetDocCategoryInfo($id){

        $info = array();

        if($this->checkRef()){

        $this->dbConn();

        $this->query = "SELECT * FROM Document_Categories WHERE doc_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['doc_category_nbr']); 
            $info['Name'] = stripslashes($row['doc_category_name']); 
            $info['Hidden'] = stripslashes($row['doc_category_hidden']); 

        }

        $this->dbClose();

      }

      return $info;

    }

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

    //function get list of document categories
    public function getDocCategories(){

      $info = array();

      if($this->checkRef()){

        $this->dbConn();

        $this->query = "SELECT * FROM Document_Categories ORDER BY doc_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['doc_category_nbr']);
              $info[$i]['Name'] = stripslashes($row['doc_category_name']);
              $info[$i]['Hidden'] = stripslashes($row['doc_category_hidden']);
              $i++;

            }

        }

        $this->dbClose();

      }

      return $info;

    }

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

   //function get delete  document type
   public function deleteDocumentCategory(){

    $result = array();

    $data = $this->cleanData();

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

      $this->dbConn();

      $this->query = "SELECT * FROM Documents WHERE doc_category = '";
      $this->query .= $data['Id'];
      $this->query .= "'";

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

      if($check->num_rows < 1){

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

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

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

      }else{

        $result['Status'] = '0';
        $result['Class'] = 'error';
        $result['Msg'] = 'Document of this category exists in the system so the category cannot be deleted.';        

      }

      $this->dbClose();

    }

    return $result;

  }

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

  //function to add a new documents
  public function addDocument(){

    $result = array();
    $now = time();
    $found = false;
    $result['Docs'] = false;

    if($this->checkRef()){

      $data = $this->cleanData();

      if(isset($_FILES)) {

        $k=0;
        foreach($_FILES['Files']['tmp_name'] as $key=> $value){
        
          if(is_file($_FILES['Files']['tmp_name'][$key])){

            $found = true;
            $result['Docs'] = true;

            $docName = $now.'_'.$this->urlSafeString($_FILES['Files']['name'][$key],true);

            $fileType = $this->dType($docName);
            $result[$k]['Type'] = $fileType;

            if($fileType == 'pdf'){

              //pdf file upload
              if(move_uploaded_file($_FILES['Files']['tmp_name'][$key],THIS_DOMAIN.'/documents/'.$docName)){

                $this->dbConn();

                $this->query = "INSERT INTO Documents VALUES ('','";
                $this->query .= $data['Category'];
                $this->query .= "','";
                $this->query .= $data['Name'];
                $this->query .= "','";
                $this->query .= $data['Hidden'];
                $this->query .= "','";                
                $this->query .= $data['Desc'];
                $this->query .= "','";
                $this->query .= $docName;
                $this->query .= "')";

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

                $result['Msg'] .= 'The pdf document was successfully uploaded into the system.';              
                $result['Status'] = true;
                $result['Class'] = 'success';
                $result['FileName'] = $docName;                
                $result['FileId'] = $this->db->insert_id;

                $this->dbClose();

              }else{

                $result['Msg'] = 'The file was could not be moved.';
                $result['Status'] = false;
                $result['Class'] = 'error';
                $result['ErrorFile'] = $_FILES['Files']['name'][$key];

              }
              $k++;

            }else{

              $result['Msg'] = 'This server will only accept pdf documents.';
              $result['Status'] = true;
              $result['Class'] = 'success';      

            }

          }

        }

        if($found === false){

          $result['Msg'] = 'No files were uploaded.';
          $result['Status'] = true;
          $result['Class'] = 'success';          

        }        

      }else{

        $result['Msg'] = 'Could not see uploaded file.';
        $result['Status'] = '0';
        $result['Class'] = 'error';

      }

    }

    return $result;

  }

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

  //function to get documents
  public function getDocuments(){

    $info = array();

    $data = $this->cleanData();

    $this->dbConn();

    $this->query = "SELECT * FROM Documents LEFT JOIN Document_Categories ON doc_category = doc_category_nbr ";
    $this->query .= "WHERE doc_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]['Doc_Category_Nbr'] = stripslashes($row['doc_category_nbr']);
        $info[$i]['Doc_Category_Name'] = stripslashes($row['doc_category_name']);
        $info[$i]['Doc_Category_Hidden'] = stripslashes($row['doc_category_hidden']);
        $info[$i]['Nbr'] = stripslashes($row['doc_nbr']);
        $info[$i]['Doc_Name'] = stripslashes($row['doc_name']);
        $info[$i]['Doc_Desc'] = stripslashes($row['doc_desc']);
        $info[$i]['Doc_File'] = stripslashes($row['doc_file']);
        $info[$i]['Doc_Hidden'] = stripslashes($row['doc_hidden']);
        $i++;

      }

    }

    $this->dbClose();

    return $info;

  }

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

  //function to get document info
  public function getDocInfo($id){

    $info = array();

    $this->dbConn();

    $this->query = "SELECT * FROM Documents LEFT JOIN Document_Categories ON doc_category = doc_category_nbr ";
    $this->query .= "WHERE doc_nbr = '";
    $this->query .= $id;
    $this->query .= "'";
    
    $getInfo = $this->db->query($this->query);

    if($getInfo->num_rows > 0){

        $row = $getInfo->fetch_assoc();

        $info['Doc_Category_Nbr'] = stripslashes($row['doc_category_nbr']);
        $info['Doc_Category_Name'] = stripslashes($row['doc_category_name']);
        $info['Doc_Category_Hidden'] = stripslashes($row['doc_category_hidden']);
        $info['Nbr'] = stripslashes($row['doc_nbr']);
        $info['Doc_Name'] = stripslashes($row['doc_name']);
        $info['Doc_Desc'] = stripslashes($row['doc_desc']);
        $info['Doc_File'] = stripslashes($row['doc_file']);
        $info['Doc_Hidden'] = stripslashes($row['doc_hidden']);

    }  
    
    $this->dbClose();

    return $info;

  }

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

  //function to delete a document
  public function deleteDoc(){

    $result = array();

    $data = $this->cleanData();

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

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

      $this->dbConn();

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

        if(is_file(THIS_DOMAIN.'/documents/'.$info['Doc_File'])){

          unlink(THIS_DOMAIN.'/documents/'.$info['Doc_File']);

        }

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

          $this->query = "DELETE FROM Documents WHERE doc_nbr = '";
          $this->query .= $info['Nbr'];
          $this->query .= "'";

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

        }        

      }

      $result['Status'] = '1';
      $result['Class'] = 'success';
      //$result['Msg'] = 'The document was successfully deleted from the system.';
      $result['Msg'] = 'file: '.$info['Doc_File'];

      $this->dbClose();

    }

    return $result;
  }

    
}

?>