*
* Category manager *
* Copyright (C) 2000, 2001 Joseph Engo *
* -------------------------------------------------------------------------*
* This library is part of the phpGroupWare API *
* http://www.phpgroupware.org/api *
* ------------------------------------------------------------------------ *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, *
* or any later version. *
* This library is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU Lesser General Public License for more details. *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
\**************************************************************************/
/* $Id$ */
/*!
@class categories
@abstract class adds ability for applications to make use of categories
@discussion examples can be found in notes app
*/
class categories
{
var $account_id;
var $app_name;
var $cats;
var $db;
var $total_records;
var $grants;
/*!
@function filter
@abstract ?
@param $type string
@result string either subs or mains
*/
function filter($type)
{
switch ($type)
{
case 'subs': $s = " and cat_parent != '0'"; break;
case 'mains': $s = " and cat_parent = '0'"; break;
case 'appandmains': $s = " and cat_appname='" . $this->app_name . "' and cat_parent ='0'"; break;
case 'appandsubs': $s = " and cat_appname='" . $this->app_name . "' and cat_parent !='0'"; break;
default: return False;
}
return $s;
}
/*!
@function total
@abstract returns the total number of categories for app, subs or mains
@param $for one of either 'app' 'subs' or 'mains'
@result integer count of categories
*/
function total($for = 'app')
{
switch($for)
{
case 'app': $w = " where cat_appname='" . $this->app_name . "'"; break;
case 'appandmains': $w = " where cat_appname='" . $this->app_name . "' and cat_parent ='0'";
case 'appandsubs': $w = " where cat_appname='" . $this->app_name . "' and cat_parent !='0'";
case 'subs': $w = " where cat_parent != '0'"; break;
case 'mains': $w = " where cat_parent = '0'"; break;
default: return False;
}
$this->db->query("select count(cat_id) from phpgw_categories $w",__LINE__,__FILE__);
$this->db->next_record();
return $this->db->f(0);
}
/*!
@function return_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
@result $cats array
*/
function return_array($type,$start,$limit = True,$query = '',$sort = '',$order = '',$public = False, $parent_id = '')
{
global $phpgw, $phpgw_info;
if ($public)
{
$public_cats = " OR cat_appname='phpgw' ";
}
$filter = $this->filter($type);
if (!$sort)
{
$sort = "ASC";
}
if ($order)
{
$ordermethod = " order by $order $sort";
}
else
{
$ordermethod = " order by cat_main, cat_level, cat_name asc";
}
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_access='public' AND cat_owner in(" . implode(',',$public_user_list) . ")) ";
}
else
{
$grant_cats = " (cat_owner='" . $this->account_id . "') ";
}
if ($parent_id)
{
$parent_filter = " 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 . "' $parent_filter AND "
. " $grant_cats) $public_cats $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__);
}
//echo 'TEST:' . $sql;
$i = 0;
while ($this->db->next_record())
{
$cats[$i]['id'] = $this->db->f('cat_id');
$cats[$i]['owner'] = $this->db->f('cat_owner');
$cats[$i]['access'] = $this->db->f('cat_access');
$cats[$i]['app_name'] = $this->db->f('cat_appname');
$cats[$i]['main'] = $this->db->f('cat_main');
$cats[$i]['level'] = $this->db->f('cat_level');
$cats[$i]['parent'] = $this->db->f('cat_parent');
$cats[$i]['name'] = $this->db->f('cat_name');
$cats[$i]['description'] = $this->db->f('cat_description');
$cats[$i]['data'] = $this->db->f('cat_data');
$i++;
}
return $cats;
}
/*!
@function return_single
@abstract return single
@param $id integer id of category
@result $cats array populated with
*/
function return_single($id = '')
{
$this->db->query("select * from phpgw_categories where cat_id='$id'",__LINE__,__FILE__);
if ($this->db->next_record())
{
$cats[0]['id'] = $this->db->f('cat_id');
$cats[0]['owner'] = $this->db->f('cat_owner');
$cats[0]['access'] = $this->db->f('cat_access');
$cats[0]['app_name'] = $this->db->f('cat_appname');
$cats[0]['main'] = $this->db->f('cat_main');
$cats[0]['level'] = $this->db->f('cat_level');
$cats[0]['parent'] = $this->db->f('cat_parent');
$cats[0]['name'] = $this->db->f('cat_name');
$cats[0]['description'] = $this->db->f('cat_description');
$cats[0]['data'] = $this->db->f('cat_data');
}
return $cats;
}
/*!
@function categories
@abstract constructor for categories class
@param $accountid account id
@param $app_name app name defaults to current app
*/
function categories($accountid = '',$app_name = '')
{
global $phpgw, $phpgw_info;
$account_id = get_account_id($accountid);
if (! $app_name)
{
$app_name = $phpgw_info['flags']['currentapp'];
}
$this->account_id = $account_id;
$this->app_name = $app_name;
$this->db = $phpgw->db;
$this->db2 = $this->db;
$this->total_records = $this->db2->num_rows();
$this->grants = $phpgw->acl->get_grants($app_name);
$this->cats = $this->return_array($type,$start,$limit,$query,$sort,$order,$public);
}
function in_array($needle,$haystack)
{
if (function_exists('in_array'))
{
return in_array($needle,$haystack);
}
while (list ($k,$v) = each($haystack))
{
if ($v == $needle)
{
return True;
}
}
return False;
}
// Return into a select box, list or other formats
/*!
@function formated_list
@abstract return into a select box, list or other formats
@param $format currently only supports select (select box)
@param $type string - subs or mains
@param $selected - cat_id or array with cat_id values
@result $s array - populated with categories
*/
function formated_list($format,$type,$selected = '',$public = False,$site_link = 'site')
{
global $phpgw;
$filter = $this->filter($type);
if (!is_array($selected))
{
$selected = explode(',',$selected);
}
if ($format == 'select')
{
$cats = $this->return_array($type,$start,False,$query,$sort,$order,$public);
for ($i=0;$iin_array($cats[$i]['id'],$selected))
{
$s .= ' selected';
}
$s .= '>' . $phpgw->strip_html($cats[$i]['name']);
if ($cats[$i]['app_name'] == 'phpgw')
{
$s .= '<' . lang('Global') . '>';
}
$s .= '' . "\n";
}
return $s;
}
if ($format == 'list')
{
$space = ' ';
$cats = $this->return_array($type,$start,False,$query,$sort,$order,$public);
$this->total_records = $this->db2->num_rows();
$s = '