Index: branches/2.2/reports/inc/class.functions.inc.php
===================================================================
--- branches/2.2/reports/inc/class.functions.inc.php	(revision 4033)
+++ branches/2.2/reports/inc/class.functions.inc.php	(revision 4570)
@@ -24,4 +24,7 @@
 			'array_invert'				=> True,
 			'Paginate_user'				=> True,
+			'Paginate_cota'				=> True,
+			'Paginate_shareAccount'			=> True,
+			'Paginate_institutionalAccount'		=> True,
 			'Paginate_user_logon'		=> True,
 			'get_list_all'				=> True,
@@ -854,4 +857,135 @@
 		}
 
+                function get_list_cota_sector($query, $contexts,$sizelimit)
+                {
+                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
+                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
+                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
+                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
+                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
+                        ldap_bind($ldap_conn,$dn,$passwd);
+
+                        $filter="(|(phpgwAccountType=u)(|(phpgwAccountType=s)))";
+                        $justthese = array("uidnumber", "uid", "cn", "mail","accountstatus","dn","createtimestamp","telephoneNumber");
+
+                        $tmp = array();
+
+                        foreach ($contexts as $index=>$context)
+                        {
+
+                                $search = ldap_search($ldap_conn, $query, $filter, $justthese, 0, $sizelimit);
+                                $info = ldap_get_entries($ldap_conn, $search);
+
+                                for ($i=0; $i < $info['count']; $i++)
+                                {
+                                        $tmp[$info[$i]['uid'][0]]['account_id']  = $info[$i]['uidnumber'][0];
+                                        $tmp[$info[$i]['uid'][0]]['account_lid'] = $info[$i]['uid'][0];
+                                        $tmp[$info[$i]['uid'][0]]['account_cn']  = $info[$i]['cn'][0];
+                                        $tmp[$info[$i]['uid'][0]]['account_mail']= $info[$i]['mail'][0];
+                                        $tmp[$info[$i]['uid'][0]]['account_phone']= $info[$i]['telephonenumber'][0];
+                                        $tmp[$info[$i]['uid'][0]]['account_accountstatus']= $info[$i]['accountstatus'][0];
+                                        $tmp[$info[$i]['uid'][0]]['createtimestamp']= $info[$i]['createtimestamp'][0];
+                                        $sort[] = $info[$i]['uid'][0];
+                                }
+                        }
+
+                        ldap_close($ldap_conn);
+
+                        if (count($sort))
+                        {
+                                natcasesort($sort);
+                                foreach ($sort as $user_uid)
+                                        $return[$user_uid] = $tmp[$user_uid];
+                        }
+
+                        return $return;
+
+                }
+
+
+                function get_list_shareAccounts_sector($query, $contexts,$sizelimit)
+                {
+                        $dn		= $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
+                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
+                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
+                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
+                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
+                        ldap_bind($ldap_conn,$dn,$passwd);
+
+                        $filter="(phpgwAccountType=s)";
+                        $justthese = array("uid", "cn", "mail","accountstatus");
+
+                        $tmp = array();
+
+                        foreach ($contexts as $index=>$context)
+                        {
+
+                                $search = ldap_search($ldap_conn, $query, $filter, $justthese, 0, $sizelimit);
+                                $info = ldap_get_entries($ldap_conn, $search);
+
+                                for ($i=0; $i < $info['count']; $i++)
+                                {
+                                        $tmp[$info[$i]['uid'][0]]['account_lid'] = $info[$i]['uid'][0];
+                                        $tmp[$info[$i]['uid'][0]]['account_cn']  = $info[$i]['cn'][0];
+                                        $tmp[$info[$i]['uid'][0]]['account_mail']= $info[$i]['mail'][0];
+                                        $tmp[$info[$i]['uid'][0]]['account_accountstatus']= $info[$i]['accountstatus'][0];
+                                        $sort[] = $info[$i]['uid'][0];
+                                }
+                        }
+
+                        ldap_close($ldap_conn);
+
+                        if (count($sort))
+                        {
+                                natcasesort($sort);
+                                foreach ($sort as $user_uid)
+                                        $return[$user_uid] = $tmp[$user_uid];
+                        }
+
+                        return $return;
+                }
+
+                function get_list_institutionalAccounts_sector($query, $contexts,$sizelimit)
+                {
+                        $dn             = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
+                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
+                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
+                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
+                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
+                        ldap_bind($ldap_conn,$dn,$passwd);
+
+                        $filter="(phpgwAccountType=i)";
+                        $justthese = array("uid", "cn", "mail","accountstatus","mailForwardingAddress");
+
+                        $tmp = array();
+
+                        foreach ($contexts as $index=>$context)
+                        {
+
+                                $search = ldap_search($ldap_conn, $query, $filter, $justthese, 0, $sizelimit);
+                                $info = ldap_get_entries($ldap_conn, $search);
+
+                                for ($i=0; $i < $info['count']; $i++)
+                                {
+                                        $tmp[$info[$i]['uid'][0]]['account_cn']  = $info[$i]['cn'][0];
+                                        $tmp[$info[$i]['uid'][0]]['account_mail']= $info[$i]['mail'][0];
+                                        $tmp[$info[$i]['uid'][0]]['account_accountstatus']= $info[$i]['accountstatus'][0];
+					$tmp[$info[$i]['uid'][0]]['account_mailforwardingaddress']= $info[$i]['mailforwardingaddress']; 
+                                        $sort[] = $info[$i]['uid'][0];
+                                }
+                        }
+
+                        ldap_close($ldap_conn);
+
+                        if (count($sort))
+                        {
+                                natcasesort($sort);
+                                foreach ($sort as $user_uid)
+                                        $return[$user_uid] = $tmp[$user_uid];
+                        }
+
+                        return $return;
+                }
+
 		function get_count_user_sector($query, $contexts,$sizelimit)
 		{
@@ -871,18 +1005,108 @@
 					$entries = ldap_get_entries($ldap_conn, $search);
 					$contexts = array();
-					for ($i=0; $i< $entries['count']; $i++){
-						$contexts[] = $entries[$i]['dn'];
 					}
 				}
+
+                        $filter="(phpgwAccountType=u)";
+                        $justthese = array("dn");
+                        $search = ldap_search($ldap_conn, $context, $filter, $justthese, 0, $sizelimit);
+                        $entries = ldap_get_entries($ldap_conn, $search);
+                        $total_count = $entries["count"];
+
+                        ldap_close($ldap_conn);
+
+                        return $total_count;
 			}	
-			$filter="(&(phpgwAccountType=u)(|(uid=*)))";
-			$justthese = array("uidnumber");
-			$total_count = 0;
-			
+			
+                function get_count_cota_sector($query, $contexts,$sizelimit)
+                {
+                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
+                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
+                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
+                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
+                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
+                        ldap_bind($ldap_conn,$dn,$passwd);
+                        // counting users by sector.
 			foreach ($contexts as $index=>$context)	{				
+                                if($context == $GLOBALS['phpgw_info']['server'] ["ldap_context"]) {
+                                        $contexts[$index] = null;
+                                        $justthese = array("dn");
+                                        $filter="(objectClass=OrganizationalUnit)";
+                                        $search = ldap_list($ldap_conn, $context, $filter, $justthese);
+                                        $entries = ldap_get_entries($ldap_conn, $search);
+                                        $contexts = array();
+                                }
+                        }
+
+                        $filter="(|(phpgwAccountType=u)(|(phpgwAccountType=s)))";
+                        $justthese = array("dn");
 				$search = ldap_search($ldap_conn, $context, $filter, $justthese, 0, $sizelimit);
-				$total_count += ldap_count_entries($ldap_conn, $search);							
-			}
+                        $entries = ldap_get_entries($ldap_conn, $search);
+                        $total_count = $entries["count"];
+
+                        ldap_close($ldap_conn);
+
+                        return $total_count;
+			}
+
+                function get_count_shareAccount_sector($query, $contexts,$sizelimit)
+                {
+                        $dn             = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
+                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
+                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
+                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
+                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
+                        ldap_bind($ldap_conn,$dn,$passwd);
+                        // counting users by sector.
+                        foreach ($contexts as $index=>$context) {
+                                if($context == $GLOBALS['phpgw_info']['server'] ["ldap_context"]) {
+                                        $contexts[$index] = null;
+                                        $justthese = array("dn");
+                                        $filter="(objectClass=OrganizationalUnit)";
+                                        $search = ldap_list($ldap_conn, $context, $filter, $justthese);
+                                        $entries = ldap_get_entries($ldap_conn, $search);
+                                        $contexts = array();
+                                }
+                        }
+
+                        $filter="(phpgwAccountType=s)";
+                        $justthese = array("dn");
+			$search = ldap_search($ldap_conn, $context, $filter, $justthese, 0, $sizelimit);
+			$entries = ldap_get_entries($ldap_conn, $search);
+			$total_count = $entries["count"];
+
+                        ldap_close($ldap_conn);
+                        
+			return $total_count;
+                }
+
+                function get_count_institutionalAccount_sector($query, $contexts,$sizelimit)
+                {
+                        $dn             = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
+                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
+                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
+                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
+                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
+                        ldap_bind($ldap_conn,$dn,$passwd);
+                        // counting users by sector.
+                        foreach ($contexts as $index=>$context) {
+                                if($context == $GLOBALS['phpgw_info']['server'] ["ldap_context"]) {
+                                        $contexts[$index] = null;
+                                        $justthese = array("dn");
+                                        $filter="(objectClass=OrganizationalUnit)";
+                                        $search = ldap_list($ldap_conn, $context, $filter, $justthese);
+                                        $entries = ldap_get_entries($ldap_conn, $search);
+                                        $contexts = array();
+                                }
+                        }
+                        
+			$filter="(phpgwAccountType=i)";
+                        $justthese = array("dn");
+                        $search = ldap_search($ldap_conn, $context, $filter, $justthese, 0, $sizelimit);
+                        $entries = ldap_get_entries($ldap_conn, $search);
+                        $total_count = $entries["count"];
+
 			ldap_close($ldap_conn);
+
 			return $total_count;
 		}
@@ -897,5 +1121,5 @@
 			ldap_bind($ldap_conn,$dn,$passwd);
 
-			$filter="(&(phpgwAccountType=u)(|(uid=*)))";
+			$filter="(phpgwAccountType=u)";
 			$justthese = array("uidnumber", "uid", "cn", "mail","accountstatus","dn","createtimestamp");
 			
@@ -1254,5 +1478,5 @@
 			ldap_bind($ldap_conn,$dn,$passwd);
 			
-			$filter="(&(phpgwAccountType=u)(|(uid=*)))";
+			$filter="(&(phpgwAccountType=u))";
 			$justthese = array("uidnumber", "uid", "cn", "mail","accountstatus","dn","createtimestamp","telephoneNumber");											
 							
@@ -1318,4 +1542,221 @@
 			return $sOrder === "desc" ? array_reverse( $aList ) : $aList;
 		}
+
+		function Paginate_cota($type, $query, $contexts, $Field, $Order = 'asc', $Page = null, $PerPage = null )
+                {
+                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
+                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
+                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
+                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
+                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
+                        ldap_bind($ldap_conn,$dn,$passwd);
+
+                        $filter="(|(phpgwAccountType=u)(|(phpgwAccountType=s)))";
+                        $justthese = array("uidnumber", "uid", "cn", "mail","accountstatus","dn","createtimestamp","telephoneNumber");                                 
+
+                        foreach ($contexts as $index=>$context)
+                        {
+                                $search=ldap_search($ldap_conn, $query, $filter, $justthese);
+
+                                $rConnection = $ldap_conn;
+                                $rSearch = $search;
+                                $sOrder = $Order;
+                                $iPage = $Page;
+                                $iPerPage = $PerPage;
+                                $sField = $Field;
+
+                                $iTotalEntries = ldap_count_entries( $rConnection, $rSearch );
+
+                                if ( $iPage === null || $iPerPage === null )
+                                {
+                                        # fetch all in one page
+                                        $iStart = 0;
+                                        $iEnd = $iTotalEntries - 1;
+                                }
+                                else
+                                {
+                                        # calculate range of page
+                                        $iFimPage = ( ceil( $iTotalEntries / $iPerPage ) - 1 ) * $iPage;
+
+                                        $iStart = ( ceil( ($iPage -1) * $iPerPage ));
+                                        $iEnd = $iPage * $iPerPage;
+
+
+                                        if ( $sOrder === "desc" )
+                                        {
+                                                # revert range
+                                                $iStart = $iTotalEntries - 1 - $iEnd;
+                                                $iEnd = $iStart + $iPerPage - 1;
+                                        }
+                                }
+
+                                /********* Importante Mostra o resultado da paginação **********
+                                var_dump( $iStart . " " . $iEnd );
+                                ****************** Só descomentar ******************************/
+
+                                 # fetch entries
+	                        ldap_sort( $rConnection, $rSearch, $sField );
+
+                                $aList = array();
+                                for (
+                                        $iCurrent = 0, $rEntry = ldap_first_entry( $rConnection, $rSearch );
+                                        $iCurrent <= $iEnd && is_resource( $rEntry );
+                                        $iCurrent++, $rEntry = ldap_next_entry( $rConnection, $rEntry )
+                                        )
+                                {
+                                        if ( $iCurrent >= $iStart )
+                                        {
+                                                array_push( $aList, ldap_get_attributes( $rConnection, $rEntry ));
+                                        }
+                                }
+                        }
+
+                        ldap_close($ldap_conn);
+
+                        # if order is desc revert page's entries
+                        return $sOrder === "desc" ? array_reverse( $aList ) : $aList;
+                }
+
+                function Paginate_shareAccount($type, $query, $contexts, $Field, $Order = 'asc', $Page = null, $PerPage = null )
+
+                {
+                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
+                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
+                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
+                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
+                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
+                        ldap_bind($ldap_conn,$dn,$passwd);
+
+                        $filter="(phpgwAccountType=s)";
+                        $justthese = array("uid", "cn", "mail","accountstatus");                                 
+
+                        foreach ($contexts as $index=>$context)
+                        {
+                                $search=ldap_search($ldap_conn, $query, $filter, $justthese);
+
+                                $rConnection = $ldap_conn;
+                                $rSearch = $search;
+                                $sOrder = $Order;
+                                $iPage = $Page;
+                                $iPerPage = $PerPage;
+                                $sField = $Field;
+
+                                $iTotalEntries = ldap_count_entries( $rConnection, $rSearch );
+
+                                if ( $iPage === null || $iPerPage === null )
+                                {
+                                        # fetch all in one page
+                                        $iStart = 0;
+                                        $iEnd = $iTotalEntries - 1;
+                                }
+                                else
+                                {
+                                        # calculate range of page
+                                        $iFimPage = ( ceil( $iTotalEntries / $iPerPage ) - 1 ) * $iPage;
+
+                                        $iStart = ( ceil( ($iPage -1) * $iPerPage ));
+                                        $iEnd = $iPage * $iPerPage;
+
+                                        if ( $sOrder === "desc" )
+                                        {
+                                                # revert range
+                                                $iStart = $iTotalEntries - 1 - $iEnd;
+                                                $iEnd = $iStart + $iPerPage - 1;
+                                        }
+                                }
+
+                                 # fetch entries
+				ldap_sort( $rConnection, $rSearch, $sField );
+
+                                $aList = array();
+                                for (
+                                        $iCurrent = 0, $rEntry = ldap_first_entry( $rConnection, $rSearch );
+                                        $iCurrent <= $iEnd && is_resource( $rEntry );
+                                        $iCurrent++, $rEntry = ldap_next_entry( $rConnection, $rEntry )
+                                        )
+                                {
+                                        if ( $iCurrent >= $iStart )
+                                        {
+                                                array_push( $aList, ldap_get_attributes( $rConnection, $rEntry ));
+                                        }
+                                }
+                        }
+
+                        ldap_close($ldap_conn);
+
+                        # if order is desc revert page's entries
+                        return $sOrder === "desc" ? array_reverse( $aList ) : $aList;
+                }
+
+                function Paginate_institutionalAccount($type, $query, $contexts, $Field, $Order = 'asc', $Page = null, $PerPage = null )
+                {
+                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
+                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
+                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
+                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
+                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
+                        ldap_bind($ldap_conn,$dn,$passwd);
+
+                        $filter="(phpgwAccountType=i)";
+                        $justthese = array("uid", "cn", "mail","accountstatus","mailforwardingaddress");
+
+                        foreach ($contexts as $index=>$context)
+                        {
+                                $search=ldap_search($ldap_conn, $query, $filter, $justthese);
+
+                                $rConnection = $ldap_conn;
+                                $rSearch = $search;
+                                $sOrder = $Order;
+                                $iPage = $Page;
+                                $iPerPage = $PerPage;
+                                $sField = $Field;
+
+                                $iTotalEntries = ldap_count_entries( $rConnection, $rSearch );
+
+                                if ( $iPage === null || $iPerPage === null )
+                                {
+                                        # fetch all in one page
+                                        $iStart = 0;
+                                        $iEnd = $iTotalEntries - 1;
+                                }
+                                else
+                                {
+                                        # calculate range of page
+                                        $iFimPage = ( ceil( $iTotalEntries / $iPerPage ) - 1 ) * $iPage;
+
+                                        $iStart = ( ceil( ($iPage -1) * $iPerPage ));
+                                        $iEnd = $iPage * $iPerPage;
+
+                                        if ( $sOrder === "desc" )
+                                        {
+                                                # revert range
+                                                $iStart = $iTotalEntries - 1 - $iEnd;
+                                                $iEnd = $iStart + $iPerPage - 1;
+                                        }
+                                }
+
+                                 # fetch entries
+                                ldap_sort( $rConnection, $rSearch, $sField );
+
+                                $aList = array();
+                                for (
+                                        $iCurrent = 0, $rEntry = ldap_first_entry( $rConnection, $rSearch );
+                                        $iCurrent <= $iEnd && is_resource( $rEntry );
+                                        $iCurrent++, $rEntry = ldap_next_entry( $rConnection, $rEntry )
+                                        )
+                                {
+                                        if ( $iCurrent >= $iStart )
+                                        {
+                                                array_push( $aList, ldap_get_attributes( $rConnection, $rEntry ));
+                                        }
+                                }
+                        }
+
+                        ldap_close($ldap_conn);
+
+                        # if order is desc revert page's entries
+                        return $sOrder === "desc" ? array_reverse( $aList ) : $aList;
+                }
+
 
 		function get_list_ou_user_logon($query,$contexts,$sizelimit)
