Index: /trunk/phpgwapi/inc/class.vfs_sql.inc.php
===================================================================
--- /trunk/phpgwapi/inc/class.vfs_sql.inc.php	(revision 1928)
+++ /trunk/phpgwapi/inc/class.vfs_sql.inc.php	(revision 1998)
@@ -1063,5 +1063,4 @@
 				}
 			}
-
 			/* We, however, have to decide this ourselves */
 			if ($this->file_exists (array(
@@ -1114,5 +1113,6 @@
 								'size' => 0,
 								'deleteable' => 'Y',
-								'app' => $currentapp
+								'app' => $currentapp,
+								'comment' => ''
 							)
 					)
@@ -1158,5 +1158,4 @@
 			$quota = $this->get_quota(array('string' => SEP.$path[1].SEP.$path[2]));
 			$size = $this->get_size(array('string' => SEP.$path[1].SEP.$path[2], 'relatives' => $data['relatives'][1]));
-
 			if ($quota > 0 && ($quota * 1024 * 1024) < $size)
 				return false;
@@ -1319,5 +1318,5 @@
 				}
 				else
-				{
+				{	
 					$this->touch (array(
 							'string'	=> $t->fake_full_path,
@@ -2490,4 +2489,43 @@
 			return $size;
 		}
+
+		/*return the total number of files in path*/
+                function count_files($data){
+                        if (!is_array ($data))
+                        {
+                                $data = array ();
+                        }
+
+                        $default_values = array
+                                (
+                                        'relatives'     => array (RELATIVE_CURRENT)
+                                );
+
+                        $data = array_merge ($this->default_values ($data, $default_values), $data);
+
+                        $p = $this->path_parts (array(
+                                        'string'        => $data['string'],
+                                        'relatives'     => RELATIVE_NONE
+                                )
+                        );
+
+                        if (!$this->acl_check (array(
+                                'string'        => $p->fake_full_path,
+                                'relatives'     => $p->mask,
+                                'operation'     => PHPGW_ACL_READ
+                        ))
+                        )
+                        {
+                                return False;
+                        }
+			$sql = "SELECT count(*) FROM phpgw_vfs WHERE directory = '".$GLOBALS['phpgw']->db->db_addslashes($data['string'])."'";
+			$sql .= $this->extra_sql (array ('query_type' => VFS_SQL_SELECT));
+                        $query = $GLOBALS['phpgw']->db->query ($sql, __LINE__,__FILE__);
+
+                        $GLOBALS['phpgw']->db->next_record ();
+                        $record = $GLOBALS['phpgw']->db->Record;
+                        return $record['count'];
+                }
+
 		/*
 		 * get the quota defined for the path in sql table
@@ -2621,5 +2659,6 @@
 					'nofiles'	=> False,
 					'summary'	=> False,
-					'orderby'	=> 'directory'
+					'orderby'	=> 'directory',
+					'otype'		=> 1
 				);
 
@@ -2635,12 +2674,10 @@
 				$this->attributes['summary'] = 'summary';
 
+			$type = $this->file_type (array(
+                                        'string'        => $dir,
+                                        'relatives'     => array ($p->mask)
+                                ));
 			/* If they pass us a file or 'nofiles' is set, return the info for $dir only */
-			if (@$data['file_id']
-				|| ((($type = $this->file_type (array(
-					'string'	=> $dir,
-					'relatives'	=> array ($p->mask)
-				)) != 'Directory'))
-				|| ($data['nofiles'])) && !$p->outside
-			)
+			if (@$data['file_id'] || ($type != 'Directory' || $data['nofiles']) && !$p->outside)
 			{
 				/* SELECT all, the, attributes */
@@ -2773,6 +2810,17 @@
 				$sql .= " AND mime_type='".$data['mime_type']."'";
 			}
-			if (strlen($data['orderby']) > 0)
-				$sql .= ' ORDER BY '.$data['orderby'];
+			if (strlen($data['orderby']) > 0 && $data['orderby'] != 'directory'){
+				$order_direction = $data['otype'] ? ' ASC' : ' DESC';
+				if ($data['orderby'] == 'name' || $data['orderby'] == 'comment')
+					$sql .= ' ORDER BY upper('.$data['orderby'].')'.$order_direction;
+				else
+					$sql .= ' ORDER BY '.$data['orderby'].$order_direction;
+				if ($data['orderby'] != 'name')
+					$sql .= ', upper(name)'.$order_direction;
+			}
+			$data['offset'] = $data['offset'] ? $data['offset'] : 0;
+			$data['limit'] = $data['limit'] ? $data['limit'] : 10000;
+			if ($data['orderby'] != 'directory')
+				$sql .= ' LIMIT '.$data['limit'].' OFFSET '.$data['offset'];
 			$query = $GLOBALS['phpgw']->db->query ($sql, __LINE__, __FILE__);
 
