egroupware_official/phpgwapi/inc/class.categories.inc.php

493 lines
14 KiB
PHP
Raw Normal View History

<?php
/**************************************************************************\
* phpGroupWare API - Categories *
* This file written by Joseph Engo <jengo@phpgroupware.org> *
* 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$ */
2001-03-22 07:10:22 +01:00
/*!
@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;
2001-03-27 09:19:28 +02:00
var $grants;
2001-03-22 07:10:22 +01:00
/*!
@function filter
@abstract ?
@param $type string
@result string either subs or mains
*/
function filter($type)
{
switch ($type)
{
2001-05-02 01:17:13 +02:00
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;
}
2001-03-22 07:10:22 +01:00
/*!
@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;
2001-05-02 01:17:13 +02:00
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();
2001-05-02 01:17:13 +02:00
return $this->db->f(0);
}
2001-03-22 07:10:22 +01:00
/*!
@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
*/
2001-04-30 02:27:54 +02:00
function return_array($type,$start,$limit = True,$query = '',$sort = '',$order = '',$public = False, $parent_id = '')
{
global $phpgw, $phpgw_info;
2001-04-27 18:06:17 +02:00
if ($public)
2001-03-25 07:58:28 +02:00
{
$public_cats = " OR cat_appname='phpgw' ";
2001-03-25 07:58:28 +02:00
}
$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";
}
2001-03-27 09:19:28 +02:00
if (is_array($this->grants))
{
2001-05-15 02:00:49 +02:00
$grants = $this->grants;
while(list($user) = each($grants))
{
$public_user_list[] = $user;
}
reset($public_user_list);
2001-06-07 08:25:48 +02:00
$grant_cats = " (cat_owner='" . $this->account_id . "' OR cat_access='public' AND cat_owner in(" . implode(',',$public_user_list) . ")) ";
2001-03-27 09:19:28 +02:00
}
2001-05-02 01:17:13 +02:00
else
2001-03-27 09:19:28 +02:00
{
2001-06-07 08:25:48 +02:00
$grant_cats = " (cat_owner='" . $this->account_id . "') ";
2001-03-27 09:19:28 +02:00
}
if ($parent_id)
{
$parent_filter = " and cat_parent='$parent_id'";
}
if ($query)
{
2001-04-27 18:06:17 +02:00
$querymethod = " AND (cat_name like '%$query%' OR cat_description like '%$query%') ";
}
2001-05-02 01:17:13 +02:00
2001-06-07 08:25:48 +02:00
$sql = "SELECT * from phpgw_categories WHERE (cat_appname='" . $this->app_name . "' $parent_filter AND "
. " $grant_cats) $public_cats $querymethod $filter";
2001-06-06 05:17:45 +02:00
2001-05-15 02:00:49 +02:00
$this->db2->query($sql,__LINE__,__FILE__);
2001-06-06 05:17:45 +02:00
2001-05-15 02:00:49 +02:00
$this->total_records = $this->db2->num_rows();
2001-06-06 05:17:45 +02:00
if ($limit)
{
$this->db->limit_query($sql . $ordermethod,$start,__LINE__,__FILE__);
}
else
{
$this->db->query($sql . $ordermethod,__LINE__,__FILE__);
}
2001-06-06 05:17:45 +02:00
2001-05-15 02:00:49 +02:00
//echo '<b>TEST:</b>' . $sql;
$i = 0;
while ($this->db->next_record())
{
2001-05-15 02:00:49 +02:00
$cats[$i]['id'] = $this->db->f('cat_id');
$cats[$i]['owner'] = $this->db->f('cat_owner');
$cats[$i]['access'] = $this->db->f('cat_access');
2001-04-27 18:06:17 +02:00
$cats[$i]['app_name'] = $this->db->f('cat_appname');
2001-05-15 02:00:49 +02:00
$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');
2001-04-27 18:06:17 +02:00
$cats[$i]['description'] = $this->db->f('cat_description');
2001-05-15 02:00:49 +02:00
$cats[$i]['data'] = $this->db->f('cat_data');
$i++;
2001-05-15 02:00:49 +02:00
}
return $cats;
}
2001-03-22 07:10:22 +01:00
/*!
@function return_single
@abstract return single
2001-05-02 01:17:13 +02:00
@param $id integer id of category
@result $cats array populated with
2001-03-22 07:10:22 +01:00
*/
function return_single($id = '')
{
$this->db->query("select * from phpgw_categories where cat_id='$id'",__LINE__,__FILE__);
2001-05-02 01:17:13 +02:00
if ($this->db->next_record())
2001-04-28 14:52:20 +02:00
{
2001-05-15 02:00:49 +02:00
$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');
2001-05-15 02:00:49 +02:00
$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');
2001-05-15 02:00:49 +02:00
$cats[0]['data'] = $this->db->f('cat_data');
2001-04-28 14:52:20 +02:00
}
2001-05-15 02:00:49 +02:00
return $cats;
}
2001-03-22 07:10:22 +01:00
/*!
@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'];
}
2001-05-15 02:00:49 +02:00
$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();
2001-05-15 02:00:49 +02:00
$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);
2001-05-24 02:51:35 +02:00
}
while (list ($k,$v) = each($haystack))
{
if ($v == $needle)
{
return True;
}
2001-05-24 02:51:35 +02:00
}
return False;
}
// Return into a select box, list or other formats
2001-03-22 07:10:22 +01:00
/*!
@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
2001-03-22 07:10:22 +01:00
@result $s array - populated with categories
*/
2001-05-01 04:15:13 +02:00
function formated_list($format,$type,$selected = '',$public = False,$site_link = 'site')
{
global $phpgw;
$filter = $this->filter($type);
2001-06-06 05:17:45 +02:00
if (!is_array($selected))
{
$selected = explode(',',$selected);
2001-06-06 05:17:45 +02:00
}
if ($format == 'select')
{
2001-04-30 02:27:54 +02:00
$cats = $this->return_array($type,$start,False,$query,$sort,$order,$public);
2001-03-27 05:40:18 +02:00
for ($i=0;$i<count($cats);$i++)
{
2001-03-27 05:40:18 +02:00
$s .= '<option value="' . $cats[$i]['id'] . '"';
if ($this->in_array($cats[$i]['id'],$selected))
{
$s .= ' selected';
}
2001-03-27 05:40:18 +02:00
$s .= '>' . $phpgw->strip_html($cats[$i]['name']);
2001-05-01 04:15:13 +02:00
if ($cats[$i]['app_name'] == 'phpgw')
2001-03-25 09:21:37 +02:00
{
2001-06-06 05:17:45 +02:00
$s .= '&lt;' . lang('Global') . '&gt;';
2001-03-25 09:21:37 +02:00
}
2001-05-24 02:51:35 +02:00
$s .= '</option>' . "\n";
}
return $s;
}
2001-05-01 04:15:13 +02:00
2001-05-15 02:00:49 +02:00
if ($format == 'list')
{
$space = '&nbsp;&nbsp;';
2001-05-01 04:15:13 +02:00
2001-05-15 02:00:49 +02:00
$cats = $this->return_array($type,$start,False,$query,$sort,$order,$public);
2001-05-02 01:17:13 +02:00
$this->total_records = $this->db2->num_rows();
2001-05-15 02:00:49 +02:00
$s = '<table border="0" cellpadding="2" cellspacing="2">' . "\n";
2001-05-02 01:17:13 +02:00
if ($this->total_records > 0)
2001-05-15 02:00:49 +02:00
{
for ($i=0;$i<count($cats);$i++)
2001-05-15 02:00:49 +02:00
{
$image_set = '&nbsp;';
2001-05-02 01:17:13 +02:00
if ($this->in_array($cats[$i]['id'],$selected))
{
$image_set = '<img src="' . PHPGW_IMAGES_DIR . '/roter_pfeil.gif">';
}
2001-05-01 04:15:13 +02:00
if (($cats[$i]['level'] == 0) && !$this->in_array($cats[$i]['id'],$selected))
{
$image_set = '<img src="' . PHPGW_IMAGES_DIR . '/grauer_pfeil.gif">';
}
2001-05-01 04:15:13 +02:00
$space_set = str_repeat($space,$cats[$i]['level']);
$s .= '<tr>' . "\n";
$s .= '<td width="8">' . $image_set . '</td>' . "\n";
$s .= '<td>' . $space_set . '<a href="' . $phpgw->link($site_link,'cat_id=' . $cats[$i]['id']) . '">' . $phpgw->strip_html($cats[$i]['name'])
. '</a></td>' . "\n";
$s .= '</tr>' . "\n";
}
2001-05-15 02:00:49 +02:00
}
$s .= '</table>' . "\n";
return $s;
2001-05-15 02:00:49 +02:00
}
}
2001-07-19 01:50:38 +02:00
2001-03-22 07:10:22 +01:00
/*!
@function add
@abstract add categories
@param $cat_name category name
@param $cat_parent category parent
@param $cat_description category description defaults to ''
@param $cat_data category data defaults to ''
*/
2001-07-19 01:50:38 +02:00
function add($cat_values)
{
2001-07-19 01:50:38 +02:00
if ($cat_values['parent'] && $cat_values['parent'] != 0)
2001-04-28 00:50:02 +02:00
{
2001-07-19 01:50:38 +02:00
$cat_values['main'] = $this->id2name($cat_values['parent'],'main');
$cat_values['level'] = $this->id2name($cat_values['parent'],'level')+1;
2001-04-28 00:50:02 +02:00
}
2001-07-19 01:50:38 +02:00
$cat_values[descr] = addslashes($cat_values['descr']);
$cat_values['name'] = addslashes($cat_values['name']);
2001-05-15 02:00:49 +02:00
2001-07-19 01:50:38 +02:00
$this->db->query("insert into phpgw_categories (cat_parent,cat_owner,cat_access,cat_appname,cat_name,"
. "cat_description,cat_data,cat_main,cat_level) values ('" . $cat_values['parent'] . "','" . $this->account_id . "','" . $cat_values['access'] . "','"
. $this->app_name . "','" . $cat_values['name'] . "','" . $cat_values['descr']
. "','" . $cat_values['data'] . "','" . $cat_values['main'] . "','" . $cat_values['level'] . "')",__LINE__,__FILE__);
2001-04-27 18:06:17 +02:00
2001-07-19 01:50:38 +02:00
if (!$cat_values['parent'] || $cat_values['parent'] == 0)
2001-05-15 02:00:49 +02:00
{
$this->db2->query("select max(cat_id) as max from phpgw_categories",__LINE__,__FILE__);
$this->db2->next_record();
$this->db->query("update phpgw_categories set cat_main='" . $this->db2->f('max') . "' where cat_id='"
. $this->db2->f('max') . "'",__LINE__,__FILE__);
2001-05-15 02:00:49 +02:00
}
}
2001-07-19 01:50:38 +02:00
2001-03-22 07:10:22 +01:00
/*!
@function delete
@abstract delete category
@param $cat_id int - category id
*/
2001-07-20 03:05:47 +02:00
function delete($cat_id,$subs = False)
{
2001-07-20 03:05:47 +02:00
if ($subs)
2001-05-15 02:00:49 +02:00
{
2001-07-20 03:05:47 +02:00
$subdelete = " OR cat_parent='$cat_id' OR cat_main='$cat_id' ";
2001-05-15 02:00:49 +02:00
}
2001-05-15 02:00:49 +02:00
$this->db->query("delete from phpgw_categories where cat_id='$cat_id' $subdelete and cat_appname='"
. $this->app_name . "'",__LINE__,__FILE__);
}
2001-03-22 07:10:22 +01:00
/*!
@function edit
@abstract edit a category
@param $cat_id int - category id
@param $cat_parent category parent
@param $cat_description category description defaults to ''
@param $cat_data category data defaults to ''
*/
2001-07-19 01:50:38 +02:00
function edit($cat_values)
{
2001-07-19 01:50:38 +02:00
if ($cat_values['parent'] && ($cat_values['parent'] != 0))
2001-05-15 02:00:49 +02:00
{
2001-07-19 01:50:38 +02:00
$cat_values['main'] = $this->id2name($cat_values['parent'],'main');
$cat_values['level'] = $this->id2name($cat_values['parent'],'level')+1;
2001-05-15 02:00:49 +02:00
}
else
{
2001-07-19 01:50:38 +02:00
$cat_values['main'] = $cat_values['id'];
$cat_values['level'] = 0;
2001-05-15 02:00:49 +02:00
}
2001-07-19 01:50:38 +02:00
$cat_values['descr'] = addslashes($cat_values['descr']);
$cat_values['name'] = addslashes($cat_values['name']);
$this->db->query("update phpgw_categories set cat_name='" . $cat_values['name'] . "', cat_description='"
. $cat_values['descr'] . "', cat_data='" . $cat_values['data'] . "', cat_parent='"
. $cat_values['parent'] . "', cat_access='" . $cat_values['access'] . "', cat_main='"
. $cat_values['main'] . "', cat_level='" . $cat_values['level'] . "' "
. "where cat_appname='" . $this->app_name . "' and cat_id='" . $cat_values['id'] . "'",__LINE__,__FILE__);
}
function name2id($cat_name)
{
$this->db->query("select cat_id from phpgw_categories where cat_name='"
. "$cat_name'",__LINE__,__FILE__);
$this->db->next_record();
return $this->db->f('cat_id');
}
function id2name($cat_id, $item = 'name')
{
switch($item)
{
case 'name': $value = 'cat_name'; break;
case 'owner': $value = 'cat_owner'; break;
case 'main': $value = 'cat_main'; break;
2001-07-19 01:50:38 +02:00
case 'level': $value = 'cat_level'; break;
}
$this->db->query("select $value from phpgw_categories where cat_id='"
. "$cat_id'",__LINE__,__FILE__);
$this->db->next_record();
if ($this->db->f($value))
{
return $this->db->f($value);
}
else
{
if ($item == 'name')
{
return '--';
}
}
}
/*!
@function return_name
@abstract return category name given $cat_id
@param $cat_id
@result cat_name category name
*/
// NOTE: This is only a temp wrapper, use id2name() to keep things matching across the board. (jengo)
function return_name($cat_id)
{
return $this->id2name($cat_id);
}
2001-03-22 07:10:22 +01:00
/*!
@function exists
@abstract used for checking if a category name exists
@param $type subs or mains
@param $cat_name category name
@result boolean true or false
*/
2001-04-22 01:22:27 +02:00
function exists($type,$cat_name = '',$cat_id = '')
{
2001-05-15 02:00:49 +02:00
$filter = $this->filter($type);
if ($cat_name)
{
$cat_exists = " cat_name='" . addslashes($cat_name) . "' ";
}
if ($cat_id)
{
$cat_exists = " cat_parent='$cat_id' ";
}
if ($cat_name && $cat_id)
{
$cat_exists = " cat_name='" . addslashes($cat_name) . "' AND cat_id != '$cat_id' ";
}
$this->db->query("select count(cat_id) from phpgw_categories where $cat_exists $filter",__LINE__,__FILE__);
2001-05-15 02:00:49 +02:00
$this->db->next_record();
if ($this->db->f(0))
{
return True;
}
else
{
return False;
}
}
}
?>