<?php
require_once("../include/constants.php");
require_once("../include/db.class.php"); 

class applicationPermission{
	function getOrganizationIDAppID($adminID){
        // Open the database (construct the object):
        $db = new DB();
        {
           $queryResult= $db->query("SELECT * FROM AdminOrganizationIntersection admin LEFT JOIN AppOrganizationIntersection app ON admin.organizationID=app.organizationID WHERE admin.adminID='" .$adminID. "'");  
        }
        return $queryResult;
	}
	
	function getOrganizationAndApp($organizationID){
        $db = new DB();
		
		   $queryString="SELECT A.appName, O.organizationName, A.appID FROM AppOrganizationIntersection AOI LEFT JOIN Organization O ON AOI.organizationID=O.organizationID 
		   							LEFT JOIN Application A ON AOI.appID=A.appID WHERE";
			$num=count($organizationID);
		   $whereString="";
		   for($i=0;$i<$num;$i++){
		   		$whereString=$whereString. " AOI.organizationID='".$organizationID[$i]."'";
				if($i<$num-1){
					$whereString=$whereString. " OR ";
				}
		   }
		   $queryString=$queryString.$whereString. " ORDER BY O.organizationName";
           $queryResult= $db->query($queryString);  
        return $queryResult;
	}
	function getAppPermission($adminID){
        $db = new DB();
        {
           $queryResult= $db->query("SELECT * FROM AdminAppIntersection WHERE adminID='" .$adminID. "'");  
        }
        return $queryResult;
	}
	
	function updateAppPermission($adminID, $appID){
        $db = new DB();
        $db->query("DELETE FROM AdminAppIntersection WHERE adminID='" .$adminID."'");
		$num=count($appID);
		if($num>0){
			for($i=0;$i<$num;$i++){
				$queryResult= $db->query("INSERT INTO AdminAppIntersection (adminID, appID) VALUES ('" .$adminID."','" .$appID[$i]."')");
			}
		}else{
			$queryResult=true;
		}
        return $queryResult;
	}
	
   function displayPageNav($totalRows,$limit,$pageNum,$pageAddr)
    {

        if (($totalRows%$limit)<>0) {
            $pmax=floor($totalRows/$limit)+1;  // Divide to total result by the number of query you want
        // to display per page($limit) and create a Max page
        } else {
            $pmax=floor($totalRows/$limit);
        }
        if($pmax==1)  //if there is only 1 page, no need to display page number
            return;
            
        echo "<div class='navpage'>"; 

        // We need to create a Previous page, so we need $pageNum to be bigger than 1, otherwise at the first page we would get a 0.
        //For the previous to show up we need at least to be at the second page.
        if($pageNum > 1) {
            $prevp="<a href='".$pageAddr."?pageNum=".($pageNum-1)."' title='Previous Page'>Previous Page</a>&nbsp;&nbsp;"; 
            echo $prevp;
        } else {echo "";}   

        // Here We want create a page from the results we got by dividing the total by the the limit. So let say you got
        //45 results and you want 5 results per page; these simple lines will create you 8 pages.
        $pid=1;
        while ($pid<=$pmax) {
            $paging= "<a href='".$pageAddr."?pageNum=$pid' title='Page $pid of $pmax'>$pid</a>&nbsp;&nbsp;";
        //So here, let say we are at the 3rd page and we want the 3 to be blank so the user can know where is he now.
        //This will act as Current Page! We need to replace the url by a text
            if($pid==$pageNum)
                echo "<span>$pageNum</span>&nbsp;&nbsp;";
            else 
                echo $paging; 
            $pid++;  // create pages until reach the result
        } 
        //We want to create a next page and a last page, $pageNum have to be less than $pmax.
        if($pageNum < $pmax) {
            $nextp="<a href='".$pageAddr."?pageNum=".($pageNum+1)."' title='Next Page'>Next Page</a>&nbsp;&nbsp;"; 
            echo $nextp;
        } else {echo "";}
        if($pageNum != $pmax) {      
            echo  "<a href='".$pageAddr."?pageNum=$pmax' title='Last Page'>Last Page</a>"; }
        
        echo "</div>";
    }
}

?>