Index: trunk/phpgwapi/inc/class.categories.inc.php
===================================================================
--- trunk/phpgwapi/inc/class.categories.inc.php	(revision 2)
+++ trunk/phpgwapi/inc/class.categories.inc.php	(revision 577)
@@ -56,5 +56,5 @@
 			$this->app_name		= $GLOBALS['phpgw']->db->db_addslashes($app_name);
 			$this->db			= $GLOBALS['phpgw']->db;
-			$this->db2			= $this->db;
+			$this->db2			= $this->db;			
 			$this->grants		= $GLOBALS['phpgw']->acl->get_grants($app_name);
 		}
@@ -137,22 +137,43 @@
 		@param $order order by
 		@param $globals True or False, includes the global egroupware categories or not
+		@param $parent_id
+		@param $lastmod integer defaults to -1
+		@param column string default to '' (All), includes the column returned.
 		@result $cats array
 		*/
 		function return_array($type,$start,$limit = True,$query = '',$sort = '',$order = '',$globals = False, $parent_id = '', $lastmod = -1, $column = '')
 		{
-			//casting and addslashes for security
-			$start		= (int)$start;
-			$parent_id	= (int)$parent_id;
-			$query		= $this->db->db_addslashes($query);
-			$sort		= $this->db->db_addslashes($sort);
-			$order		= $this->db->db_addslashes($order);
-
-			if ($globals)
-			{
-				$global_cats = " OR cat_appname='phpgw'";
-			}
-
-			$filter = $this->filter($type);
-
+			return $this -> return_sorted_array( $start, $limit, $query, $sort, $order, $globals, $parent_id, NULL, $lastmod, $column);
+		}
+		/*!
+		@function return_sorted_array
+		@abstract return an array populated with categories
+		@param $type string defaults to 'all'
+		@param $start ?
+		@param $limit ?
+		@param $query string defaults to ''
+		@param $sort string sort order, either defaults to 'ASC'
+		@param $order order by
+		@param $globals True or False, includes the global egroupware categories or not
+		@param $parent_id string defaults to '', includes the parent category ID
+		@param $group_id integer defaults to NULL, includes the gidNumber
+		@param $lastmod integer defaults to -1
+		@param column string default to '' (All), includes the column returned.
+		@result $cats array
+		*/			
+		function return_sorted_array($start,$limit = True,$query = '',$sort = '',$order = '',$globals = False, $parent_id = '',$group_id = NULL,$lastmod = -1, $column = '')
+		{
+			//casting and slashes for security
+			$start = (int)$start;
+			$query = $this->db->db_addslashes($query);
+			$sort  = $this->db->db_addslashes($sort);
+			$order = $this->db->db_addslashes($order);
+			$parent_id = (int)$parent_id;
+
+			if ($globals && !$group_id)
+			{
+				$global_cats = " cat_appname='phpgw'";
+			}
+			
 			if (!$sort)
 			{
@@ -160,5 +181,5 @@
 			}
 
-			if (!empty($order) && preg_match('/^[a-zA-Z_(), ]+$/',$order) && (empty($sort) || preg_match('/^(ASC|DESC|asc|desc)$/',$sort)))
+			if (!empty($order) && preg_match('/^[a-zA-Z_, ]+$/',$order) && (empty($sort) || preg_match('/^(ASC|DESC|asc|desc)$/',$sort)))
 			{
 				$ordermethod = " ORDER BY $order $sort";
@@ -166,37 +187,41 @@
 			else
 			{
-				$ordermethod = ' ORDER BY cat_main, cat_level, cat_name ASC';
-			}
-
-			if ($this->account_id == '-1')
-			{
-				$grant_cats = ' cat_owner=-1 ';
-			}
-			else
-			{
+				$ordermethod = ' ORDER BY cat_name ASC';
+			}
+									
+			if($group_id){
+				$grant_cats .= " cat_owner='".$group_id."' ";
+			}
+			else if ($this->account_id != '-1'){
+				$grants = $this->grants;
+				$groups = $GLOBALS['phpgw']->accounts->membership();
+			
 				if (is_array($this->grants))
 				{
-					$grants = $this->grants;
-					while(list($user) = each($grants))
-					{
-						$public_user_list[] = $user;
-					}
-					reset($public_user_list);
-					$grant_cats = ' (cat_owner=' . $this->account_id . " OR cat_owner=-1 OR cat_access='public' AND cat_owner in(" . implode(',',$public_user_list) . ')) ';
+					
+					foreach($grants as $idx => $user){												
+						$public_user_list[$user] = $user;
+					}
+					if(is_array($groups)){
+						foreach($groups as $idx => $group) {
+							$public_user_list[$group['account_id']] = $group['account_id'];
+						}
+					}
+					@reset($public_user_list);
+					$grant_cats = " (cat_owner='" . $this->account_id . "' ".(is_array($public_user_list) ? "OR (cat_owner in(" . implode(',',$public_user_list) . ")  AND cat_access='public')" : "").") ";
+					
 				}
 				else
 				{
-					$grant_cats = ' cat_owner=' . $this->account_id . ' OR cat_owner=-1 ';
-				}
-			}
-
-			if ($parent_id > 0)
-			{
-				$parent_filter = ' AND cat_parent=' . $parent_id;
-			}
+					$grant_cats = " cat_owner='" . $this->account_id . "' or cat_owner='-1' ";
+				}
+			}
+			
+
+			$parent_select = ' AND cat_parent=' . $parent_id;
 
 			if ($query)
 			{
-				$querymethod = " AND (cat_name LIKE '%$query%' OR cat_description LIKE '%$query%') ";
+				$querymethod = " AND (cat_name ILIKE '%$query%' OR cat_description ILIKE '%$query%') ";
 			}
 
@@ -228,109 +253,9 @@
 			}
 
-			$sql = "SELECT $table_column FROM phpgw_categories WHERE (cat_appname='" . $this->app_name . "' AND" . $grant_cats . $global_cats . ')'
-				. $parent_filter . $querymethod . $filter;
-
-			$this->db2->query($sql,__LINE__,__FILE__);
-			$this->total_records = $this->db2->num_rows();
-
-			if ($limit)
-			{
-				$this->db->limit_query($sql . $ordermethod,$start,__LINE__,__FILE__);
-			}
-			else
-			{
-				$this->db->query($sql . $ordermethod,__LINE__,__FILE__);
-			}
-
-			while ($this->db->next_record())
-			{
-				if ($column)
-				{
-					$cats[] = array
-					(
-						$column => $this->db->f(0)
-					);
-				}
-				else
-				{
-					$cats[] = array
-					(
-						'id'			=> $this->db->f('cat_id'),
-						'owner'			=> $this->db->f('cat_owner'),
-						'access'		=> $this->db->f('cat_access'),
-						'app_name'		=> $this->db->f('cat_appname'),
-						'main'			=> $this->db->f('cat_main'),
-						'level'			=> $this->db->f('cat_level'),
-						'parent'		=> $this->db->f('cat_parent'),
-						'name'			=> $this->db->f('cat_name'),
-						'description'	=> $this->db->f('cat_description'),
-						'data'			=> $this->db->f('cat_data'),
-						'last_mod'		=> $this->db->f('last_mod')
-					);
-				}
-			}
-			return $cats;
-		}
-
-		function return_sorted_array($start,$limit = True,$query = '',$sort = '',$order = '',$globals = False, $parent_id = '')
-		{
-			//casting and slashes for security
-			$start = (int)$start;
-			$query = $this->db->db_addslashes($query);
-			$sort  = $this->db->db_addslashes($sort);
-			$order = $this->db->db_addslashes($order);
-			$parent_id = (int)$parent_id;
-
-			if ($globals)
-			{
-				$global_cats = " OR cat_appname='phpgw'";
-			}
-
-			if (!$sort)
-			{
-				$sort = 'ASC';
-			}
-
-			if (!empty($order) && preg_match('/^[a-zA-Z_, ]+$/',$order) && (empty($sort) || preg_match('/^(ASC|DESC|asc|desc)$/')))
-			{
-				$ordermethod = " ORDER BY $order $sort";
-			}
-			else
-			{
-				$ordermethod = ' ORDER BY cat_name ASC';
-			}
-
-			if ($this->account_id == '-1')
-			{
-				$grant_cats = " cat_owner='-1' ";
-			}
-			else
-			{
-				if (is_array($this->grants))
-				{
-					$grants = $this->grants;
-					while(list($user) = each($grants))
-					{
-						$public_user_list[] = $user;
-					}
-					reset($public_user_list);
-					$grant_cats = " (cat_owner='" . $this->account_id . "' OR cat_owner='-1' OR cat_access='public' AND cat_owner in(" . implode(',',$public_user_list) . ")) ";
-				}
-				else
-				{
-					$grant_cats = " cat_owner='" . $this->account_id . "' or cat_owner='-1' ";
-				}
-			}
-
-			$parent_select = ' AND cat_parent=' . $parent_id;
-
-			if ($query)
-			{
-				$querymethod = " AND (cat_name LIKE '%$query%' OR cat_description LIKE '%$query%') ";
-			}
-
-			$sql = "SELECT * FROM phpgw_categories WHERE (cat_appname='" . $this->app_name . "' AND" . $grant_cats . $global_cats . ")"
-					. $querymethod;
-
+
+			$sql = "SELECT".$table_column."FROM phpgw_categories WHERE (cat_appname='" . $this->app_name. "' ".
+					($grant_cats ? " AND".$grant_cats : "") .($global_cats ? " OR".$global_cats: "").
+					")".$querymethod;						
+			
 			$this->db2->query($sql . $parent_select,__LINE__,__FILE__);
 			$total = $this->db2->num_rows();
@@ -350,4 +275,9 @@
 				$cats[$i]['id']          = (int)$this->db->f('cat_id');
 				$cats[$i]['owner']       = (int)$this->db->f('cat_owner');
+				if($cats[$i]['owner'] > 0){
+				// 	Load Name Group.
+					$group = $this->get_group($cats[$i]['owner']);
+					$cats[$i]['owner'] = $group['name'];
+				}
 				$cats[$i]['access']      = $this->db->f('cat_access');
 				$cats[$i]['app_name']    = $this->db->f('cat_appname');
@@ -358,4 +288,5 @@
 				$cats[$i]['description'] = $this->db->f('cat_description');
 				$cats[$i]['data']        = $this->db->f('cat_data');
+					
 				$i++;
 			}
@@ -365,17 +296,6 @@
 			{
 				$sub_select = ' AND cat_parent=' . $cats[$i]['id'] . ' AND cat_level=' . ($cats[$i]['level']+1);
-
-				/*$this->db2->query($sql . $sub_select,__LINE__,__FILE__);
-				$total_subs += $this->db2->num_rows();
-
-				if ($limit)
-				{
-					$this->db->limit_query($sql . $sub_select . $ordermethod,$start,__LINE__,__FILE__);
-				}
-				else
-				{*/
-					$this->db->query($sql . $sub_select . $ordermethod,__LINE__,__FILE__);
-					$total += $this->db->num_rows();
-				//}
+				$this->db->query($sql . $sub_select . $ordermethod,__LINE__,__FILE__);
+				$total += $this->db->num_rows();
 
 				$subcats = array();
@@ -429,5 +349,4 @@
 		{
 			$this->db->query('SELECT * FROM phpgw_categories WHERE cat_id=' . (int)$id,__LINE__,__FILE__);
-
 			if ($this->db->next_record())
 			{
@@ -442,4 +361,10 @@
 				$cats[0]['description'] = $this->db->f('cat_description');
 				$cats[0]['data']        = $this->db->f('cat_data');
+				if($cats[0]['owner'] > 0){
+				// 	Load Group.
+					$group = $this->get_group($cats[0]['owner']);
+					$cats[0]['id_group']    = $group['id'];
+					$cats[0]['name_group']  = $group['name'];					
+				}				
 			}
 			return $cats;
@@ -459,4 +384,11 @@
 			return $this->formated_list($format,$type,$selected,$globals,$site_link);
 		}
+		function get_group($id)
+		{
+			if (!IsSet($id))
+				return "";
+			return array("id" => $id, "name" => $GLOBALS['phpgw']->accounts->id2name($id));
+		}
+
 		function formated_list($format,$type='',$selected = '',$globals = False,$site_link = 'site')
 		{
@@ -591,7 +523,6 @@
 				$id_val = $values['id'] . ',';
 			}
-
 			$this->db->query('INSERT INTO phpgw_categories (' . $id_col . 'cat_parent,cat_owner,cat_access,cat_appname,cat_name,cat_description,cat_data,'
-				. 'cat_main,cat_level, last_mod) VALUES (' . $id_val . (int)$values['parent'] . ',' . $this->account_id . ",'" . $values['access']
+				. 'cat_main,cat_level, last_mod) VALUES (' . $id_val . (int)$values['parent'] . ',' . ($values['group']!= 0 ? $values['group'] : $this->account_id) . ",'" . $values['access']
 				. "','" . $this->app_name . "','" . $values['name'] . "','" . $values['descr'] . "','" . $values['data']
 				. "'," . (int)$values['main'] . ',' . (int)$values['level'] . ',' . time() . ')',__LINE__,__FILE__);
@@ -687,5 +618,8 @@
 			$values['id']     = (int)$values['id'];
 			$values['parent'] = (int)$values['parent'];
-
+			$values['owner'] = (int)$values['group'];
+			if($values['owner']){
+				$owner = "cat_owner = ".$values['owner'].",";
+			}
 			if (isset($values['old_parent']) && (int)$values['old_parent'] != $values['parent'])
 			{
@@ -710,5 +644,5 @@
 			$values['name'] = $this->db->db_addslashes($values['name']);
 
-			$sql = "UPDATE phpgw_categories SET cat_name='" . $values['name'] . "', cat_description='" . $values['descr']
+			$sql = "UPDATE phpgw_categories SET $owner cat_name='" . $values['name'] . "', cat_description='" . $values['descr']
 				. "', cat_data='" . $values['data'] . "', cat_parent=" . $values['parent'] . ", cat_access='"
 				. $values['access'] . "', cat_main=" . $values['main'] . ', cat_level=' . $values['level'] . ',last_mod=' . time()
