<?php
 // Include the class:

require_once("../include/constants.php");
require_once("../include/db.class.php"); 
     
 class organization
{
     
     //This functions returns a data set with all organizations
     function showAllOrganizations($pageNum=NULL, $limit=NULL)
     {
        
        // Open the database (construct the object):
        $db = new DB();
        
        if(!is_null($pageNum) && !is_null($limit))
        {
           $queryResult= $db->query("SELECT * FROM Organization ORDER BY organizationName ASC LIMIT ".(($pageNum-1)*$limit).", $limit");  
        }
        else
        {
            $queryResult= $db->query("SELECT * FROM Organization ORDER BY organizationName ASC");
        }
        return $queryResult;
     
     }

     //This function returns a data row for the given organization
     function queryOrganization($organizationID)
     {
     
        $db = new DB();
        $queryResult= $db->query("SELECT * FROM Organization  WHERE organizationID='$organizationID'");
        $result=$db->fetchNextObject($queryResult); 
        return $result;
     
     
     }
     //This function returns applications that belong to a given organization
     function queryOrganizationApps($organizationID)
     {
     
        $db = new DB();
        $queryResult= $db->query("SELECT A.*, B.appName FROM AppOrganizationIntersection A, Application B WHERE A.appID=B.appID and A.organizationID='$organizationID' ORDER BY B.appName");
        return $queryResult;
     
     
     }
     //This function returns admins that belong to a given organization
     function queryOrganizationAdmin($organizationID)
     {
     
        $db = new DB();
        $queryResult= $db->query("SELECT A.*, B.firstName, B.lastName FROM AdminOrganizationIntersection A, Administrator B WHERE A.adminID=B.adminID and A.organizationID='$organizationID' ORDER BY B.firstName, B.lastName");
        return $queryResult;
     
     
     }
      //This function adds an organization to the database
     function addOrganization($organizationName,$organizationLocation,$organizationJurisdiction,$organizationComment)
     {
         $organizationName = mysql_real_escape_string($organizationName);
         $organizationLocation = mysql_real_escape_string($organizationLocation); 
         $organizationJurisdiction = mysql_real_escape_string($organizationJurisdiction); 
         $organizationComment = mysql_real_escape_string($organizationComment); 
   
         $db = new DB();  
         $sql="INSERT INTO Organization (organizationName,organizationLocation,organizationJurisdiction,organizationComment)
                VALUES ('$organizationName','$organizationLocation','$organizationJurisdiction','$organizationComment')";
          
         $queryResult= $db->execute($sql);
         
         return $queryResult;   

     }
     //This function updates organization info in the database
     function updateOrganization($organizationID,$organizationName,$organizationLocation,$organizationJurisdiction,$organizationComment)
     {
            $sql=sprintf("UPDATE Organization SET organizationName='%s',organizationLocation='%s',organizationJurisdiction='%s',organizationComment='%s' WHERE organizationID='$organizationID'",
                get_magic_quotes_gpc() ? $organizationName : addslashes($organizationName),
                get_magic_quotes_gpc() ? $organizationLocation : addslashes($organizationLocation),
                get_magic_quotes_gpc() ? $organizationJurisdiction : addslashes($organizationJurisdiction),
                get_magic_quotes_gpc() ? $organizationComment : addslashes($organizationComment));
                
            $db = new DB();
                
            $queryResult= $db->execute($sql);
             
            return $queryResult;
         
     }
     //------------------------------------------------------------------------------------------
       //This function deletes an organization from the database
       //return true if successfull, return message if failed 
     function deleteOrganization($organizationID)
     {
     
        $db = new DB();
       
         $db->execute("DELETE FROM AdminOrganizationIntersection WHERE organizationID='$organizationID'");    
         $db->execute("DELETE FROM AppOrganizationIntersection WHERE organizationID='$organizationID'"); 
        $queryResult= $db->execute("DELETE FROM Organization WHERE organizationID='$organizationID'"); 
        return $queryResult;
     
     }
     //This function checks to see if an organization with this name exists in the system.
     //if an organization with this name exists, return true; else false;
     function organizationDuplicateCheck($organizationName)
     {
         $db = new DB(); 
        if(!get_magic_quotes_gpc()){
            $organizationName = addslashes($organizationName);
        }
        $q = "SELECT organizationID FROM Organization WHERE organizationName='$organizationName'";
        $result = $db->query($q);
        return (mysql_numrows($result) > 0);
     }
         


   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>";
    }
}
?>
