Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 120
DocumentService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 8
702.00
0.00% covered (danger)
0.00%
0 / 120
 __construct
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 1
 __clone
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 1
 getInstance
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 4
 searchDocuments
0.00% covered (danger)
0.00%
0 / 1
156.00
0.00% covered (danger)
0.00%
0 / 63
 getDocument
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 17
 givePermissionToUpload
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 15
 checkDocUploadPermission
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 10
 documentUploadPermittedStaff
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 9
<?php
namespace com\linways\core\ams\professional\service;
use com\linways\core\ams\professional\response\SearchDocumentResponse;
use com\linways\core\ams\professional\exception\ProfessionalException;
class DocumentService extends BaseService
{
    private static $_instance = null;
    
    // /Condition 2 - Locked down the constructor
    private function __construct() {
    }
    // Prevent any oustide instantiation of this class
    
    // /Condition 3 - Prevent any object or instance of that class to be cloned
    private function __clone() {
    }
    // Prevent any copy of this object
    
    // /Condition 4 - Have a single globally accessible static method
    public static function getInstance() {
        if (! is_object ( self::$_instance )) // or if( is_null(self::$_instance) ) or if( self::$_instance == null )
            self::$_instance = new self ();
            
            return self::$_instance;
    }
    
    /**
     * Search Documents
     * @param SearchDocumentRequest 
     * @return SearchDocumentResponse
     * Supported Sort options : 
     *  <table>
     *  <tr>
     *  <th>Name</th>
     *  <th>Value</th>
     *  </tr>
     *  <tr> <td> Name </td><td> docName</td> </tr>
     *  <tr> <td> staffName </td><td> staffName</td> </tr>
     *  <tr> <td> MostRead </td><td> sd.docVisitors</td> </tr>
     *  
     *  </table>
     */
    public function searchDocuments($searchDocumentRequest)
    {
        $response = new SearchDocumentResponse();
        $criteria = NULL;
        $sqlCount = "SELECT count(sd.docID) as totalRecords";
        $fromCondition =" FROM staff_document sd INNER JOIN  staffaccounts sa ON sa.staffID = sd.staffID  WHERE sd.docID IS NOT NULL ";
        $sqlSearch = "SELECT sd.docID as id, 
                        sd.docName as name, 
                        sd.docImgPath as imagePath, 
                        sd.staffID as staffId, 
                        sa.staffName as staffName,
                        sa.staffAccount as staffAccount, 
                        sd.docVisitors as noofVisitors, sd.is_private as isPrivate ";
        
        //From condtion added
        $sqlCount .= $fromCondition;
        $sqlSearch .= $fromCondition;
        
        if(!empty($searchDocumentRequest))
        {
            //Category ID
            if(!empty($searchDocumentRequest->categoryId) )
            {
                $criteria .= " AND sd.docCatID = $searchDocumentRequest->categoryId";
            }
            
            //Sub Category 
            if(!empty($searchDocumentRequest->subCategoryId) )
            {
                $criteria .= " AND sd.docSubCatID = $searchDocumentRequest->subCategoryId";
            }
            
            //document Name
            if(!empty($searchDocumentRequest->documentName) )
            {
                $criteria .= " AND sd.docName LIKE '%$searchDocumentRequest->documentName%' ";
            }
            
            //staff
            if(!empty($searchDocumentRequest->staffId) )
            {
                $criteria .= " AND sd.staffID=$searchDocumentRequest->staffId ";
            }
            
            //adding start date and end date
            if (!empty($searchDocumentRequest->startDate) && !empty($searchDocumentRequest->endDate)) {
                
                if (strtotime($searchDocumentRequest->startDate) > strtotime($searchDocumentRequest->endDate)) {
                    
                    throw new ProfessionalException(ProfessionalException::INVALID_DATE_RANGE,"Please choose start date and end date correctly");
                
                }
                else 
                {
                    $criteria .= " AND sd.docUploadTime BETWEEN '$searchDocumentRequest->startDate' AND '$searchDocumentRequest->endDate'";
                }
                
            }
            
            //show private documents
            $isPrivate = $searchDocumentRequest->showPrivateDocuments ? 1 : 0;
            $criteria .= " AND sd.is_private=$isPrivate ";
                             
            
            //Set criteria to count and select query
            $sqlCount .= $criteria;
            
            //Add Sort condtion
            if(!empty($searchDocumentRequest->sortBy))
            {
                
                $criteria .= " ORDER BY $searchDocumentRequest->sortBy $searchDocumentRequest->sortOrder";
            }
            
            //Add Pagination
            $pagination = " LIMIT $searchDocumentRequest->startIndex,$searchDocumentRequest->endIndex";
            
            
            $sqlSearch .= $criteria.$pagination;
            
            
            
            try {
                
                $response->totalRecords = $this->executeQueryForObject($sqlCount)->totalRecords;
                $response->documents = $this->executeQueryForList($sqlSearch);
                
            } catch (\Exception $e) {
                
                throw new ProfessionalException($e->getCode(),$e->getMessage());
                
            }
            
            
        }
        else 
        {
            throw new ProfessionalException(ProfessionalException::INVALID_REQUEST,"SearchDocumentRequest is null!");
        }
        
        
        return $response;
        
    }
    /**
     * Get document by document Id
     * @param int $documentId
     * @return $document
     */
    public function getDocument($documentId)
    {
        $document = NULL;
        $sql = "SELECT sd.docID as id, 
                        sd.docName as name, 
                        sd.docImgPath as imagePath, 
                        sd.staffID as staffId, 
                        sa.staffName as staffName,
                        sa.staffAccount as staffAccount, 
                        sd.docVisitors as noofVisitors, sd.is_private as isPrivate
                    FROM staff_document sd INNER JOIN  staffaccounts sa ON sa.staffID = sd.staffID  WHERE sd.docID=$documentId ";
        
        try {
            $document = $this->executeQueryForObject($sql);
        } catch (\Exception $e) {
             throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
        return $document;
    }
    
    /**
     * @param staffId, action(delete or insert), createdBy user 
     */
    function givePermissionToUpload($staffId,$action,$userId) 
    {
        if ($action == 'INSERT') 
        {
            $sql = "INSERT INTO document_upload_permission (staffID,createdBy) VALUES ('$staffId','$userId')";
        }
        elseif ($action == 'DELETE') 
        {
           $sql = "DELETE FROM document_upload_permission WHERE staffID = '$staffId'";
        }
        
        try {
            $this->executeQuery($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
        
    }
    
    
    /**
     * check if the staff has permission to upload the document
     * @param $staffId
     */
    function checkDocUploadPermission($staffId) 
    {
        $result = null;
        $sql = "SELECT staffID FROM document_upload_permission WHERE staffID = ".$staffId."";
        try {
            $result = $this->executeQueryForObject($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
        return $result;
    }
    
    /**
     * @return Staff
     */
    function documentUploadPermittedStaff($sortBy='staffName',$sortOrder='ASC') {
        $stafflist = null;
        $sql = "SELECT sa.staffName as staffName,dup.staffID as staffId FROM document_upload_permission dup INNER JOIN staffaccounts sa ON sa.staffID = dup.staffID";
        
        try {
            $stafflist = $this->executeQueryForList($sql);    
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
        
        return $stafflist;
    }
}