<?php
 // Include the class:

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

     
 class setting
{
     
     //This functions returns a data set with all users
     function showAllCategories($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
		   							SuggestionCategory
                                    ORDER BY categoryID ASC LIMIT ".(($pageNum-1)*$limit).", $limit");  
        }
        else
        {
            $queryResult= $db->query("SELECT * FROM
		   							 SuggestionCategory 
                                     ORDER BY categoryID ASC");
        }
        return $queryResult;
     }

     //This function returns a data row for the given category
     function queryCategory($categoryID)
     {
        $db = new DB();
        $queryResult= $db->query("SELECT * FROM SuggestionCategory
                                    WHERE categoryID='$categoryID'");
        $result=$db->fetchNextObject($queryResult); 
        return $result;
     }
	 
	//This function returns a data row for the given configTable
     function queryDisplayConfig()
     {
        $db = new DB();
        $queryResult= $db->query("SELECT * FROM ConfigTable
                                    WHERE settingID=1");
        $result=$db->fetchNextObject($queryResult); 
        return $result;
     }
	 
     //This function returns mail settings in an array format
     function queryMailConfig()
     {
        $db = new DB();
        $queryResult= $db->query("SELECT * FROM ConfigTable WHERE 
                                    settingName='mailer' or settingName='sendMailPath' or settingName='SMTPAuthentication' or
                                    settingName='SMTPUsername' or settingName='SMTPPassword' or settingName='SMTPHost' or
                                    settingName='mailFromName' or settingName='mailFromEmail'");
         
        return $queryResult;
     }
     
     function updateDisplayConfig($settingValue)
     {
         
         $db = new DB();
                    
        $queryResult= $db->execute(sprintf("UPDATE ConfigTable SET settingValue='%s' WHERE settingName='defaultPublicOrPrivate'",
                                get_magic_quotes_gpc() ? $settingValue : addslashes($settingValue)));    
        return $queryResult;
     }
	 function updateMailConfig($mailer,$sendMailPath,$authentication,$SMTPUser,$SMTPPass,$SMTPHost,$mailFromName, $mailFromEmail)
	 {
		 
         $db = new DB();
         
         $db->execute("UPDATE ConfigTable SET settingValue='$mailer' WHERE settingName='mailer'");
         
         $db->execute(sprintf("UPDATE ConfigTable SET settingValue='%s' WHERE settingName='sendMailPath'",
                                get_magic_quotes_gpc() ? $sendMailPath : addslashes($sendMailPath)));
         
         $db->execute("UPDATE ConfigTable SET settingValue='$authentication' WHERE settingName='SMTPAuthentication'");
		
         $db->execute(sprintf("UPDATE ConfigTable SET settingValue='%s' WHERE settingName='SMTPUsername'",
                                get_magic_quotes_gpc() ? $SMTPUser : addslashes($SMTPUser)));
                                
         $db->execute(sprintf("UPDATE ConfigTable SET settingValue='%s' WHERE settingName='SMTPPassword'",
                                get_magic_quotes_gpc() ? $SMTPPass : addslashes($SMTPPass)));
         
         $db->execute(sprintf("UPDATE ConfigTable SET settingValue='%s' WHERE settingName='SMTPHost'",
                                get_magic_quotes_gpc() ? $SMTPHost : addslashes($SMTPHost)));
         
         $db->execute(sprintf("UPDATE ConfigTable SET settingValue='%s' WHERE settingName='mailFromName'",
                                get_magic_quotes_gpc() ? $mailFromName : addslashes($mailFromName)));
                                
        $queryResult= $db->execute(sprintf("UPDATE ConfigTable SET settingValue='%s' WHERE settingName='mailFromEmail'",
                                get_magic_quotes_gpc() ? $mailFromEmail : addslashes($mailFromEmail)));
             
        return $queryResult;
	 }

      //This function adds a category to the database
     function addCategory($categoryDescription,$categoryComment)
     {
         $categoryDescription = mysql_real_escape_string($categoryDescription);
         $categoryComment = mysql_real_escape_string($categoryComment); 
         
         $db = new DB();
           
         $sql1="INSERT INTO SuggestionCategory (categoryDescription,categoryComment)
                VALUES ('$categoryDescription','$categoryComment')";
          
         $queryResult1= $db->execute($sql1);
         return $queryResult1;
     }
     //This function updates category info in the database
     function updateCategory($categoryID,$categoryDescription,$categoryComment)
     {
	    $sql=sprintf("UPDATE SuggestionCategory SET categoryDescription='%s',categoryComment='%s' WHERE categoryID='$categoryID'",
            get_magic_quotes_gpc() ? $categoryDescription : addslashes($categoryDescription),
            get_magic_quotes_gpc() ? $categoryComment : addslashes($categoryComment));
				
        $db = new DB();
                
        $queryResult= $db->execute($sql);
             
        return $queryResult;
     }
     //------------------------------------------------------------------------------------------
       //This function deletes a user from the database
       //return true if successfull, return message if failed 
     function deleteCategory($categoryID)
     {
     
        $db = new DB();

         $queryResult=$db->execute("DELETE FROM SuggestionCategory WHERE categoryID='$categoryID'");   
        return $queryResult;
     
     }
     //This function checks to see if a category with this description exists in the system.
     //if a category with this description exists, return true; else false;
     function categoryDescriptionDuplicateCheck($description)
     {
         $db = new DB(); 
        if(!get_magic_quotes_gpc()){
            $description = addslashes($description);
        }
        $q = "SELECT categoryID FROM SuggestionCategory WHERE categoryDescription='$description'";
        $result = $db->query($q);
        return (mysql_numrows($result) > 0);
     }
	 
	 //This function checks to see if a category with this description exists in the system.
     //if a category with this description exists, return true; else false;
     function categoryUpdateDuplicateCheck($categoryID, $description)
     {
         $db = new DB(); 
        if(!get_magic_quotes_gpc()){
            $description = addslashes($description);
        }
        $q = "SELECT categoryID FROM SuggestionCategory WHERE categoryDescription='$description' AND categoryID <> $categoryID";
        $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>";
    }
}
?>
