[2] | 1 | <?php |
---|
| 2 | /**************************************************************************\ |
---|
| 3 | * eGroupWare API - Categories * |
---|
| 4 | * This file written by Joseph Engo <jengo@phpgroupware.org> * |
---|
| 5 | * and Bettina Gille [ceb@phpgroupware.org] * |
---|
| 6 | * Category manager * |
---|
| 7 | * Copyright (C) 2000, 2001 Joseph Engo, Bettina Gille * |
---|
| 8 | * Copyright (C) 2002, 2003 Bettina Gille * |
---|
| 9 | * ------------------------------------------------------------------------ * |
---|
| 10 | * This library is part of the eGroupWare API * |
---|
| 11 | * http://www.egroupware.org * |
---|
| 12 | * ------------------------------------------------------------------------ * |
---|
| 13 | * This library is free software; you can redistribute it and/or modify it * |
---|
| 14 | * under the terms of the GNU Lesser General Public License as published by * |
---|
| 15 | * the Free Software Foundation; either version 2.1 of the License, * |
---|
| 16 | * or any later version. * |
---|
| 17 | * This library is distributed in the hope that it will be useful, but * |
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * |
---|
| 20 | * See the GNU Lesser General Public License for more details. * |
---|
| 21 | * You should have received a copy of the GNU Lesser General Public License * |
---|
| 22 | * along with this library; if not, write to the Free Software Foundation, * |
---|
| 23 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
---|
| 24 | \**************************************************************************/ |
---|
| 25 | |
---|
| 26 | /*! |
---|
| 27 | @class categories |
---|
| 28 | @abstract class adds ability for applications to make use of categories |
---|
| 29 | @discussion examples can be found in notes app |
---|
| 30 | */ |
---|
| 31 | class categories |
---|
| 32 | { |
---|
| 33 | var $account_id; |
---|
| 34 | var $app_name; |
---|
| 35 | var $cats; |
---|
| 36 | var $db; |
---|
| 37 | var $total_records; |
---|
| 38 | var $grants; |
---|
| 39 | |
---|
| 40 | /*! |
---|
| 41 | @function categories |
---|
| 42 | @abstract constructor for categories class |
---|
| 43 | @param $accountid account id |
---|
| 44 | @param $app_name app name defaults to current app |
---|
| 45 | */ |
---|
| 46 | function categories($accountid = '',$app_name = '') |
---|
| 47 | { |
---|
| 48 | $account_id = get_account_id($accountid); |
---|
| 49 | |
---|
| 50 | if (! $app_name) |
---|
| 51 | { |
---|
| 52 | $app_name = $GLOBALS['phpgw_info']['flags']['currentapp']; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | $this->account_id = $account_id; |
---|
| 56 | $this->app_name = $GLOBALS['phpgw']->db->db_addslashes($app_name); |
---|
| 57 | $this->db = $GLOBALS['phpgw']->db; |
---|
[577] | 58 | $this->db2 = $this->db; |
---|
[2] | 59 | $this->grants = $GLOBALS['phpgw']->acl->get_grants($app_name); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | /*! |
---|
| 63 | @function filter |
---|
| 64 | @abstract ? |
---|
| 65 | @param $type string |
---|
| 66 | @result string either subs or mains |
---|
| 67 | */ |
---|
| 68 | function filter($type) |
---|
| 69 | { |
---|
| 70 | switch ($type) |
---|
| 71 | { |
---|
| 72 | case 'subs': $s = ' AND cat_parent != 0'; break; |
---|
| 73 | case 'mains': $s = ' AND cat_parent = 0'; break; |
---|
| 74 | case 'appandmains': $s = " AND cat_appname='" . $this->app_name . "' AND cat_parent =0"; break; |
---|
| 75 | case 'appandsubs': $s = " AND cat_appname='" . $this->app_name . "' AND cat_parent !=0"; break; |
---|
| 76 | case 'noglobal': $s = " AND cat_appname != '" . $this->app_name . "'"; break; |
---|
| 77 | case 'noglobalapp': $s = " AND cat_appname = '" . $this->app_name . "' AND cat_owner != " . $this->account_id; break; |
---|
| 78 | default: return False; |
---|
| 79 | } |
---|
| 80 | return $s; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | /*! |
---|
| 84 | @function total |
---|
| 85 | @abstract returns the total number of categories for app, subs or mains |
---|
| 86 | @param $for one of either 'app' 'subs' or 'mains' |
---|
| 87 | @result integer count of categories |
---|
| 88 | */ |
---|
| 89 | function total($for = 'app') |
---|
| 90 | { |
---|
| 91 | switch($for) |
---|
| 92 | { |
---|
| 93 | case 'app': $w = " WHERE cat_appname='" . $this->app_name . "'"; break; |
---|
| 94 | case 'appandmains': $w = " WHERE cat_appname='" . $this->app_name . "' AND cat_parent =0"; break; |
---|
| 95 | case 'appandsubs': $w = " WHERE cat_appname='" . $this->app_name . "' AND cat_parent !=0"; break; |
---|
| 96 | case 'subs': $w = ' WHERE cat_parent != 0'; break; |
---|
| 97 | case 'mains': $w = ' WHERE cat_parent = 0'; break; |
---|
| 98 | default: return False; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | $this->db->query("SELECT COUNT(cat_id) FROM phpgw_categories $w",__LINE__,__FILE__); |
---|
| 102 | $this->db->next_record(); |
---|
| 103 | |
---|
| 104 | return $this->db->f(0); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | /*! |
---|
| 108 | @funtion return_all_children |
---|
| 109 | @abstract returns array with id's of all children from $cat_id and $cat_id itself! |
---|
| 110 | @param $cat_id integer cat-id to search for |
---|
| 111 | @returns array of cat-id's |
---|
| 112 | */ |
---|
| 113 | function return_all_children($cat_id) |
---|
| 114 | { |
---|
| 115 | $all_children = array($cat_id); |
---|
| 116 | |
---|
| 117 | $children = $this->return_array('subs',0,False,'','','',True,$cat_id,-1,'id'); |
---|
| 118 | if (is_array($children) && count($children)) |
---|
| 119 | { |
---|
| 120 | foreach($children as $child) |
---|
| 121 | { |
---|
| 122 | $all_children = array_merge($all_children,$this->return_all_children($child['id'])); |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | //echo "<p>categories::return_all_children($cat_id)=(".implode(',',$all_children).")</p>\n"; |
---|
| 126 | return $all_children; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | /*! |
---|
| 130 | @function return_array |
---|
| 131 | @abstract return an array populated with categories |
---|
| 132 | @param $type string defaults to 'all' |
---|
| 133 | @param $start ? |
---|
| 134 | @param $limit ? |
---|
| 135 | @param $query string defaults to '' |
---|
| 136 | @param $sort string sort order, either defaults to 'ASC' |
---|
| 137 | @param $order order by |
---|
| 138 | @param $globals True or False, includes the global egroupware categories or not |
---|
[577] | 139 | @param $parent_id |
---|
| 140 | @param $lastmod integer defaults to -1 |
---|
| 141 | @param column string default to '' (All), includes the column returned. |
---|
[2] | 142 | @result $cats array |
---|
| 143 | */ |
---|
| 144 | function return_array($type,$start,$limit = True,$query = '',$sort = '',$order = '',$globals = False, $parent_id = '', $lastmod = -1, $column = '') |
---|
| 145 | { |
---|
[577] | 146 | return $this -> return_sorted_array( $start, $limit, $query, $sort, $order, $globals, $parent_id, NULL, $lastmod, $column); |
---|
| 147 | } |
---|
| 148 | /*! |
---|
| 149 | @function return_sorted_array |
---|
| 150 | @abstract return an array populated with categories |
---|
| 151 | @param $type string defaults to 'all' |
---|
| 152 | @param $start ? |
---|
| 153 | @param $limit ? |
---|
| 154 | @param $query string defaults to '' |
---|
| 155 | @param $sort string sort order, either defaults to 'ASC' |
---|
| 156 | @param $order order by |
---|
| 157 | @param $globals True or False, includes the global egroupware categories or not |
---|
| 158 | @param $parent_id string defaults to '', includes the parent category ID |
---|
| 159 | @param $group_id integer defaults to NULL, includes the gidNumber |
---|
| 160 | @param $lastmod integer defaults to -1 |
---|
| 161 | @param column string default to '' (All), includes the column returned. |
---|
| 162 | @result $cats array |
---|
| 163 | */ |
---|
| 164 | function return_sorted_array($start,$limit = True,$query = '',$sort = '',$order = '',$globals = False, $parent_id = '',$group_id = NULL,$lastmod = -1, $column = '') |
---|
| 165 | { |
---|
| 166 | //casting and slashes for security |
---|
| 167 | $start = (int)$start; |
---|
| 168 | $query = $this->db->db_addslashes($query); |
---|
| 169 | $sort = $this->db->db_addslashes($sort); |
---|
| 170 | $order = $this->db->db_addslashes($order); |
---|
| 171 | $parent_id = (int)$parent_id; |
---|
[2] | 172 | |
---|
[577] | 173 | if ($globals && !$group_id) |
---|
[2] | 174 | { |
---|
[577] | 175 | $global_cats = " cat_appname='phpgw'"; |
---|
[2] | 176 | } |
---|
[577] | 177 | |
---|
[2] | 178 | if (!$sort) |
---|
| 179 | { |
---|
| 180 | $sort = 'ASC'; |
---|
| 181 | } |
---|
| 182 | |
---|
[577] | 183 | if (!empty($order) && preg_match('/^[a-zA-Z_, ]+$/',$order) && (empty($sort) || preg_match('/^(ASC|DESC|asc|desc)$/',$sort))) |
---|
[2] | 184 | { |
---|
| 185 | $ordermethod = " ORDER BY $order $sort"; |
---|
| 186 | } |
---|
| 187 | else |
---|
| 188 | { |
---|
[577] | 189 | $ordermethod = ' ORDER BY cat_name ASC'; |
---|
[2] | 190 | } |
---|
[577] | 191 | |
---|
| 192 | if($group_id){ |
---|
| 193 | $grant_cats .= " cat_owner='".$group_id."' "; |
---|
[2] | 194 | } |
---|
[577] | 195 | else if ($this->account_id != '-1'){ |
---|
| 196 | $grants = $this->grants; |
---|
| 197 | $groups = $GLOBALS['phpgw']->accounts->membership(); |
---|
| 198 | |
---|
[2] | 199 | if (is_array($this->grants)) |
---|
| 200 | { |
---|
[577] | 201 | |
---|
| 202 | foreach($grants as $idx => $user){ |
---|
| 203 | $public_user_list[$user] = $user; |
---|
[2] | 204 | } |
---|
[577] | 205 | if(is_array($groups)){ |
---|
| 206 | foreach($groups as $idx => $group) { |
---|
| 207 | $public_user_list[$group['account_id']] = $group['account_id']; |
---|
| 208 | } |
---|
| 209 | } |
---|
| 210 | @reset($public_user_list); |
---|
| 211 | $grant_cats = " (cat_owner='" . $this->account_id . "' ".(is_array($public_user_list) ? "OR (cat_owner in(" . implode(',',$public_user_list) . ") AND cat_access='public')" : "").") "; |
---|
| 212 | |
---|
[2] | 213 | } |
---|
| 214 | else |
---|
| 215 | { |
---|
[577] | 216 | $grant_cats = " cat_owner='" . $this->account_id . "' or cat_owner='-1' "; |
---|
[2] | 217 | } |
---|
| 218 | } |
---|
[577] | 219 | |
---|
[2] | 220 | |
---|
[577] | 221 | $parent_select = ' AND cat_parent=' . $parent_id; |
---|
[2] | 222 | |
---|
| 223 | if ($query) |
---|
| 224 | { |
---|
[577] | 225 | $querymethod = " AND (cat_name ILIKE '%$query%' OR cat_description ILIKE '%$query%') "; |
---|
[2] | 226 | } |
---|
| 227 | |
---|
| 228 | if($lastmod && $lastmod >= 0) |
---|
| 229 | { |
---|
| 230 | $querymethod .= ' AND last_mod > ' . (int)$lastmod; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | if($column) |
---|
| 234 | { |
---|
| 235 | switch($column) |
---|
| 236 | { |
---|
| 237 | case 'id': $table_column = ' cat_id '; break; |
---|
| 238 | case 'owner': $table_column = ' cat_owner '; break; |
---|
| 239 | case 'access': $table_column = ' cat_access '; break; |
---|
| 240 | case 'app_name': $table_column = ' cat_appname '; break; |
---|
| 241 | case 'main': $table_column = ' cat_main '; break; |
---|
| 242 | case 'parent': $table_column = ' cat_parent '; break; |
---|
| 243 | case 'name': $table_column = ' cat_name '; break; |
---|
| 244 | case 'description': $table_column = ' cat_description '; break; |
---|
| 245 | case 'data': $table_column = ' cat_data '; break; |
---|
| 246 | case 'last_mod': $table_column = ' last_mod '; break; |
---|
| 247 | default: $table_column = ' cat_id '; break; |
---|
| 248 | } |
---|
| 249 | } |
---|
| 250 | else |
---|
| 251 | { |
---|
| 252 | $table_column = ' * '; |
---|
| 253 | } |
---|
| 254 | |
---|
[4786] | 255 | $this->app_name = pg_escape_string($this->app_name); |
---|
[577] | 256 | $sql = "SELECT".$table_column."FROM phpgw_categories WHERE (cat_appname='" . $this->app_name. "' ". |
---|
| 257 | ($grant_cats ? " AND".$grant_cats : "") .($global_cats ? " OR".$global_cats: ""). |
---|
| 258 | ")".$querymethod; |
---|
| 259 | |
---|
[2] | 260 | $this->db2->query($sql . $parent_select,__LINE__,__FILE__); |
---|
| 261 | $total = $this->db2->num_rows(); |
---|
| 262 | |
---|
| 263 | if ($limit) |
---|
| 264 | { |
---|
| 265 | $this->db->limit_query($sql . $parent_select . $ordermethod,$start,__LINE__,__FILE__); |
---|
| 266 | } |
---|
| 267 | else |
---|
| 268 | { |
---|
| 269 | $this->db->query($sql . $parent_select . $ordermethod,__LINE__,__FILE__); |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | $i = 0; |
---|
| 273 | while ($this->db->next_record()) |
---|
| 274 | { |
---|
| 275 | $cats[$i]['id'] = (int)$this->db->f('cat_id'); |
---|
| 276 | $cats[$i]['owner'] = (int)$this->db->f('cat_owner'); |
---|
[577] | 277 | if($cats[$i]['owner'] > 0){ |
---|
| 278 | // Load Name Group. |
---|
| 279 | $group = $this->get_group($cats[$i]['owner']); |
---|
| 280 | $cats[$i]['owner'] = $group['name']; |
---|
| 281 | } |
---|
[2] | 282 | $cats[$i]['access'] = $this->db->f('cat_access'); |
---|
| 283 | $cats[$i]['app_name'] = $this->db->f('cat_appname'); |
---|
| 284 | $cats[$i]['main'] = (int)$this->db->f('cat_main'); |
---|
| 285 | $cats[$i]['level'] = (int)$this->db->f('cat_level'); |
---|
| 286 | $cats[$i]['parent'] = (int)$this->db->f('cat_parent'); |
---|
| 287 | $cats[$i]['name'] = $this->db->f('cat_name'); |
---|
| 288 | $cats[$i]['description'] = $this->db->f('cat_description'); |
---|
| 289 | $cats[$i]['data'] = $this->db->f('cat_data'); |
---|
[577] | 290 | |
---|
[7655] | 291 | ++$i; |
---|
[2] | 292 | } |
---|
| 293 | |
---|
| 294 | $num_cats = count($cats); |
---|
[7655] | 295 | for ($i=0;$i < $num_cats;++$i) |
---|
[2] | 296 | { |
---|
| 297 | $sub_select = ' AND cat_parent=' . $cats[$i]['id'] . ' AND cat_level=' . ($cats[$i]['level']+1); |
---|
[577] | 298 | $this->db->query($sql . $sub_select . $ordermethod,__LINE__,__FILE__); |
---|
| 299 | $total += $this->db->num_rows(); |
---|
[2] | 300 | |
---|
| 301 | $subcats = array(); |
---|
| 302 | $j = 0; |
---|
| 303 | while ($this->db->next_record()) |
---|
| 304 | { |
---|
| 305 | $subcats[$j]['id'] = (int)$this->db->f('cat_id'); |
---|
| 306 | $subcats[$j]['owner'] = (int)$this->db->f('cat_owner'); |
---|
| 307 | $subcats[$j]['access'] = $this->db->f('cat_access'); |
---|
| 308 | $subcats[$j]['app_name'] = $this->db->f('cat_appname'); |
---|
| 309 | $subcats[$j]['main'] = (int)$this->db->f('cat_main'); |
---|
| 310 | $subcats[$j]['level'] = (int)$this->db->f('cat_level'); |
---|
| 311 | $subcats[$j]['parent'] = (int)$this->db->f('cat_parent'); |
---|
| 312 | $subcats[$j]['name'] = $this->db->f('cat_name'); |
---|
| 313 | $subcats[$j]['description'] = $this->db->f('cat_description'); |
---|
| 314 | $subcats[$j]['data'] = $this->db->f('cat_data'); |
---|
[7655] | 315 | ++$j; |
---|
[2] | 316 | } |
---|
| 317 | |
---|
| 318 | $num_subcats = count($subcats); |
---|
| 319 | if ($num_subcats != 0) |
---|
| 320 | { |
---|
| 321 | $newcats = array(); |
---|
[7655] | 322 | for ($k = 0; $k <= $i; ++$k) |
---|
[2] | 323 | { |
---|
| 324 | $newcats[$k] = $cats[$k]; |
---|
| 325 | } |
---|
[7655] | 326 | for ($k = 0; $k < $num_subcats; ++$k) |
---|
[2] | 327 | { |
---|
| 328 | $newcats[$k+$i+1] = $subcats[$k]; |
---|
| 329 | } |
---|
[7655] | 330 | for ($k = $i+1; $k < $num_cats; ++$k) |
---|
[2] | 331 | { |
---|
| 332 | $newcats[$k+$num_subcats] = $cats[$k]; |
---|
| 333 | } |
---|
| 334 | $cats = $newcats; |
---|
| 335 | $num_cats = count($cats); |
---|
| 336 | } |
---|
| 337 | } |
---|
| 338 | $this->total_records = $total; |
---|
| 339 | return $cats; |
---|
| 340 | } |
---|
| 341 | |
---|
| 342 | /*! |
---|
| 343 | @function return_single |
---|
| 344 | @abstract return single |
---|
| 345 | @param $id integer id of category |
---|
| 346 | @result $cats array populated with |
---|
| 347 | */ |
---|
| 348 | function return_single($id = '') |
---|
| 349 | { |
---|
| 350 | $this->db->query('SELECT * FROM phpgw_categories WHERE cat_id=' . (int)$id,__LINE__,__FILE__); |
---|
| 351 | if ($this->db->next_record()) |
---|
| 352 | { |
---|
| 353 | $cats[0]['id'] = $this->db->f('cat_id'); |
---|
| 354 | $cats[0]['owner'] = $this->db->f('cat_owner'); |
---|
| 355 | $cats[0]['access'] = $this->db->f('cat_access'); |
---|
| 356 | $cats[0]['app_name'] = $this->db->f('cat_appname'); |
---|
| 357 | $cats[0]['main'] = $this->db->f('cat_main'); |
---|
| 358 | $cats[0]['level'] = $this->db->f('cat_level'); |
---|
| 359 | $cats[0]['parent'] = $this->db->f('cat_parent'); |
---|
| 360 | $cats[0]['name'] = $this->db->f('cat_name'); |
---|
| 361 | $cats[0]['description'] = $this->db->f('cat_description'); |
---|
| 362 | $cats[0]['data'] = $this->db->f('cat_data'); |
---|
[577] | 363 | if($cats[0]['owner'] > 0){ |
---|
| 364 | // Load Group. |
---|
| 365 | $group = $this->get_group($cats[0]['owner']); |
---|
| 366 | $cats[0]['id_group'] = $group['id']; |
---|
| 367 | $cats[0]['name_group'] = $group['name']; |
---|
| 368 | } |
---|
[2] | 369 | } |
---|
| 370 | return $cats; |
---|
| 371 | } |
---|
| 372 | |
---|
| 373 | /*! |
---|
| 374 | @function formated_list |
---|
| 375 | @abstract return into a select box, list or other formats |
---|
| 376 | @param $format currently supports select (select box) or list |
---|
| 377 | @param $type string - subs or mains |
---|
| 378 | @param $selected - cat_id or array with cat_id values |
---|
| 379 | @param $globals True or False, includes the global egroupware categories or not |
---|
| 380 | @result $s array - populated with categories |
---|
| 381 | */ |
---|
| 382 | function formatted_list($format,$type='',$selected = '',$globals = False,$site_link = 'site') |
---|
| 383 | { |
---|
| 384 | return $this->formated_list($format,$type,$selected,$globals,$site_link); |
---|
| 385 | } |
---|
[577] | 386 | function get_group($id) |
---|
| 387 | { |
---|
| 388 | if (!IsSet($id)) |
---|
| 389 | return ""; |
---|
| 390 | return array("id" => $id, "name" => $GLOBALS['phpgw']->accounts->id2name($id)); |
---|
| 391 | } |
---|
| 392 | |
---|
[2] | 393 | function formated_list($format,$type='',$selected = '',$globals = False,$site_link = 'site') |
---|
| 394 | { |
---|
| 395 | if(is_array($format)) |
---|
| 396 | { |
---|
| 397 | $temp_format = $format['format']; |
---|
| 398 | $type = ($format['type']?$format['type']:'all'); |
---|
| 399 | $selected = (isset($format['selected'])?$format['selected']:''); |
---|
| 400 | $self = (isset($format['self'])?$format['self']:''); |
---|
| 401 | $globals = (isset($format['globals'])?$format['globals']:True); |
---|
| 402 | $site_link = (isset($format['site_link'])?$format['site_link']:'site'); |
---|
| 403 | settype($format,'string'); |
---|
| 404 | $format = ($temp_format?$temp_format:'select'); |
---|
| 405 | unset($temp_format); |
---|
| 406 | } |
---|
| 407 | |
---|
| 408 | if (!is_array($selected)) |
---|
| 409 | { |
---|
| 410 | $selected = explode(',',$selected); |
---|
| 411 | } |
---|
| 412 | |
---|
| 413 | if ($type != 'all') |
---|
| 414 | { |
---|
| 415 | $cats = $this->return_array($type,$start,False,$query,$sort,$order,$globals); |
---|
| 416 | } |
---|
| 417 | else |
---|
| 418 | { |
---|
| 419 | $cats = $this->return_sorted_array($start,False,$query,$sort,$order,$globals); |
---|
| 420 | } |
---|
| 421 | |
---|
| 422 | if($self) |
---|
| 423 | { |
---|
[7712] | 424 | for ($i=0;$i<count($cats);++$i) |
---|
[2] | 425 | { |
---|
| 426 | if ($cats[$i]['id'] == $self) |
---|
| 427 | { |
---|
| 428 | unset($cats[$i]); |
---|
| 429 | } |
---|
| 430 | } |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | if ($format == 'select') |
---|
| 434 | { |
---|
| 435 | while (is_array($cats) && list(,$cat) = each($cats)) |
---|
| 436 | { |
---|
| 437 | $s .= '<option value="' . $cat['id'] . '"'; |
---|
| 438 | if (in_array($cat['id'],$selected)) |
---|
| 439 | { |
---|
| 440 | $s .= ' selected'; |
---|
| 441 | } |
---|
| 442 | $s .= '>'; |
---|
[7655] | 443 | for ($j=0;$j<$cat['level'];++$j) |
---|
[2] | 444 | { |
---|
| 445 | $s .= ' '; |
---|
| 446 | } |
---|
| 447 | $s .= $GLOBALS['phpgw']->strip_html($cat['name']); |
---|
| 448 | if ($cat['app_name'] == 'phpgw') |
---|
| 449 | { |
---|
| 450 | $s .= ' <' . lang('Global') . '>'; |
---|
| 451 | } |
---|
| 452 | if ($cat['owner'] == '-1') |
---|
| 453 | { |
---|
| 454 | $s .= ' <' . lang('Global') . ' ' . lang($this->app_name) . '>'; |
---|
| 455 | } |
---|
| 456 | $s .= '</option>' . "\n"; |
---|
| 457 | } |
---|
| 458 | return $s; |
---|
| 459 | } |
---|
| 460 | |
---|
| 461 | if ($format == 'list') |
---|
| 462 | { |
---|
| 463 | $space = ' '; |
---|
| 464 | |
---|
| 465 | $s = '<table border="0" cellpadding="2" cellspacing="2">' . "\n"; |
---|
| 466 | |
---|
| 467 | if ($this->total_records > 0) |
---|
| 468 | { |
---|
[7673] | 469 | $cats_count = count($cats); |
---|
| 470 | for ($i=0;$i<$cats_count;++$i) |
---|
[2] | 471 | { |
---|
| 472 | $image_set = ' '; |
---|
| 473 | |
---|
| 474 | if (in_array($cats[$i]['id'],$selected)) |
---|
| 475 | { |
---|
| 476 | $image_set = '<img src="' . PHPGW_IMAGES_DIR . '/roter_pfeil.gif">'; |
---|
| 477 | } |
---|
| 478 | |
---|
| 479 | if (($cats[$i]['level'] == 0) && !in_array($cats[$i]['id'],$selected)) |
---|
| 480 | { |
---|
| 481 | $image_set = '<img src="' . PHPGW_IMAGES_DIR . '/grauer_pfeil.gif">'; |
---|
| 482 | } |
---|
| 483 | |
---|
| 484 | $space_set = str_repeat($space,$cats[$i]['level']); |
---|
| 485 | |
---|
| 486 | $s .= '<tr>' . "\n"; |
---|
| 487 | $s .= '<td width="8">' . $image_set . '</td>' . "\n"; |
---|
| 488 | $s .= '<td>' . $space_set . '<a href="' . $GLOBALS['phpgw']->link($site_link,'cat_id=' . $cats[$i]['id']) . '">' |
---|
| 489 | . $GLOBALS['phpgw']->strip_html($cats[$i]['name']) |
---|
| 490 | . '</a></td>' . "\n" |
---|
| 491 | . '</tr>' . "\n"; |
---|
| 492 | } |
---|
| 493 | } |
---|
| 494 | $s .= '</table>' . "\n"; |
---|
| 495 | return $s; |
---|
| 496 | } |
---|
| 497 | } |
---|
| 498 | |
---|
| 499 | /*! |
---|
| 500 | @function add |
---|
| 501 | @abstract add categories |
---|
| 502 | @param $cat_name category name |
---|
| 503 | @param $cat_parent category parent |
---|
| 504 | @param $cat_description category description defaults to '' |
---|
| 505 | @param $cat_data category data defaults to '' |
---|
| 506 | */ |
---|
| 507 | function add($values) |
---|
| 508 | { |
---|
| 509 | $values['id'] = (int)$values['id']; |
---|
| 510 | $values['parent'] = (int)$values['parent']; |
---|
| 511 | |
---|
| 512 | if ($values['parent'] > 0) |
---|
| 513 | { |
---|
| 514 | $values['level'] = $this->id2name($values['parent'],'level')+1; |
---|
| 515 | $values['main'] = $this->id2name($values['parent'],'main'); |
---|
| 516 | } |
---|
| 517 | |
---|
| 518 | $values['descr'] = $this->db->db_addslashes($values['descr']); |
---|
| 519 | $values['name'] = $this->db->db_addslashes($values['name']); |
---|
| 520 | |
---|
| 521 | if ($values['id'] > 0) |
---|
| 522 | { |
---|
| 523 | $id_col = 'cat_id,'; |
---|
| 524 | $id_val = $values['id'] . ','; |
---|
| 525 | } |
---|
| 526 | $this->db->query('INSERT INTO phpgw_categories (' . $id_col . 'cat_parent,cat_owner,cat_access,cat_appname,cat_name,cat_description,cat_data,' |
---|
[577] | 527 | . 'cat_main,cat_level, last_mod) VALUES (' . $id_val . (int)$values['parent'] . ',' . ($values['group']!= 0 ? $values['group'] : $this->account_id) . ",'" . $values['access'] |
---|
[2] | 528 | . "','" . $this->app_name . "','" . $values['name'] . "','" . $values['descr'] . "','" . $values['data'] |
---|
| 529 | . "'," . (int)$values['main'] . ',' . (int)$values['level'] . ',' . time() . ')',__LINE__,__FILE__); |
---|
| 530 | |
---|
| 531 | if ($values['id'] > 0) |
---|
| 532 | { |
---|
| 533 | $max = $values['id']; |
---|
| 534 | } |
---|
| 535 | else |
---|
| 536 | { |
---|
| 537 | $max = $this->db->get_last_insert_id('phpgw_categories','cat_id'); |
---|
| 538 | } |
---|
| 539 | |
---|
| 540 | $max = (int)$max; |
---|
| 541 | if ($values['parent'] == 0) |
---|
| 542 | { |
---|
| 543 | $this->db->query('UPDATE phpgw_categories SET cat_main=' . $max . ' WHERE cat_id=' . $max,__LINE__,__FILE__); |
---|
| 544 | } |
---|
| 545 | return $max; |
---|
| 546 | } |
---|
| 547 | |
---|
| 548 | /*! |
---|
| 549 | @function delete |
---|
| 550 | @abstract delete category |
---|
| 551 | @param $cat_id int - category id |
---|
| 552 | */ |
---|
| 553 | /*function delete($cat_id,$subs = False) |
---|
| 554 | { |
---|
| 555 | $cat_id = (int)$cat_id; |
---|
| 556 | if ($subs) |
---|
| 557 | { |
---|
| 558 | $subdelete = ' OR cat_parent=' . $cat_id . ' OR cat_main=' . $cat_id; |
---|
| 559 | } |
---|
| 560 | |
---|
| 561 | $this->db->query('DELETE FROM phpgw_categories WHERE cat_id=' . $cat_id . $subdelete . " AND cat_appname='" |
---|
| 562 | . $this->app_name . "'",__LINE__,__FILE__); |
---|
| 563 | } */ |
---|
| 564 | |
---|
| 565 | function delete($cat_id, $drop_subs = False, $modify_subs = False) |
---|
| 566 | { |
---|
| 567 | $cat_id = (int)$cat_id; |
---|
| 568 | if ($drop_subs) |
---|
| 569 | { |
---|
| 570 | $subdelete = ' OR cat_parent=' . $cat_id . ' OR cat_main=' . $cat_id; |
---|
| 571 | } |
---|
| 572 | |
---|
| 573 | if ($modify_subs) |
---|
| 574 | { |
---|
| 575 | $cats = $this->return_sorted_array('',False,'','','',False, $cat_id); |
---|
| 576 | |
---|
| 577 | $new_parent = $this->id2name($cat_id,'parent'); |
---|
| 578 | |
---|
[7673] | 579 | $cats_count = count($cats); |
---|
| 580 | for ($i=0;$i<$cats_count;++$i) |
---|
[2] | 581 | { |
---|
| 582 | if ($cats[$i]['level'] == 1) |
---|
| 583 | { |
---|
| 584 | $this->db->query('UPDATE phpgw_categories set cat_level=0, cat_parent=0, cat_main=' . (int)$cats[$i]['id'] |
---|
| 585 | . ' WHERE cat_id=' . (int)$cats[$i]['id'] . " AND cat_appname='" . $this->app_name . "'",__LINE__,__FILE__); |
---|
| 586 | $new_main = $cats[$i]['id']; |
---|
| 587 | } |
---|
| 588 | else |
---|
| 589 | { |
---|
| 590 | if ($new_main) |
---|
| 591 | { |
---|
| 592 | $update_main = ',cat_main=' . $new_main; |
---|
| 593 | } |
---|
| 594 | |
---|
| 595 | if ($cats[$i]['parent'] == $cat_id) |
---|
| 596 | { |
---|
| 597 | $update_parent = ',cat_parent=' . $new_parent; |
---|
| 598 | } |
---|
| 599 | |
---|
| 600 | $this->db->query('UPDATE phpgw_categories set cat_level=' . ($cats[$i]['level']-1) . $update_main . $update_parent |
---|
| 601 | . ' WHERE cat_id=' . (int)$cats[$i]['id'] . " AND cat_appname='" . $this->app_name . "'",__LINE__,__FILE__); |
---|
| 602 | } |
---|
| 603 | } |
---|
| 604 | } |
---|
| 605 | |
---|
| 606 | $this->db->query('DELETE FROM phpgw_categories WHERE cat_id=' . $cat_id . $subdelete . " AND cat_appname='" |
---|
| 607 | . $this->app_name . "'",__LINE__,__FILE__); |
---|
| 608 | } |
---|
| 609 | |
---|
| 610 | /*! |
---|
| 611 | @function edit |
---|
| 612 | @abstract edit a category |
---|
| 613 | @param $cat_id int - category id |
---|
| 614 | @param $cat_parent category parent |
---|
| 615 | @param $cat_description category description defaults to '' |
---|
| 616 | @param $cat_data category data defaults to '' |
---|
| 617 | */ |
---|
| 618 | function edit($values) |
---|
| 619 | { |
---|
| 620 | $values['id'] = (int)$values['id']; |
---|
| 621 | $values['parent'] = (int)$values['parent']; |
---|
[577] | 622 | $values['owner'] = (int)$values['group']; |
---|
| 623 | if($values['owner']){ |
---|
| 624 | $owner = "cat_owner = ".$values['owner'].","; |
---|
| 625 | } |
---|
[2] | 626 | if (isset($values['old_parent']) && (int)$values['old_parent'] != $values['parent']) |
---|
| 627 | { |
---|
| 628 | $this->delete($values['id'],False,True); |
---|
| 629 | return $this->add($values); |
---|
| 630 | } |
---|
| 631 | else |
---|
| 632 | { |
---|
| 633 | if ($values['parent'] > 0) |
---|
| 634 | { |
---|
| 635 | $values['main'] = (int)$this->id2name($values['parent'],'main'); |
---|
| 636 | $values['level'] = (int)$this->id2name($values['parent'],'level') + 1; |
---|
| 637 | } |
---|
| 638 | else |
---|
| 639 | { |
---|
| 640 | $values['main'] = $values['id']; |
---|
| 641 | $values['level'] = 0; |
---|
| 642 | } |
---|
| 643 | } |
---|
| 644 | |
---|
| 645 | $values['descr'] = $this->db->db_addslashes($values['descr']); |
---|
| 646 | $values['name'] = $this->db->db_addslashes($values['name']); |
---|
| 647 | |
---|
[577] | 648 | $sql = "UPDATE phpgw_categories SET $owner cat_name='" . $values['name'] . "', cat_description='" . $values['descr'] |
---|
[2] | 649 | . "', cat_data='" . $values['data'] . "', cat_parent=" . $values['parent'] . ", cat_access='" |
---|
| 650 | . $values['access'] . "', cat_main=" . $values['main'] . ', cat_level=' . $values['level'] . ',last_mod=' . time() |
---|
| 651 | . " WHERE cat_appname='" . $this->app_name . "' AND cat_id=" . $values['id']; |
---|
| 652 | |
---|
| 653 | $this->db->query($sql,__LINE__,__FILE__); |
---|
| 654 | return $values['id']; |
---|
| 655 | } |
---|
| 656 | |
---|
| 657 | function name2id($cat_name) |
---|
| 658 | { |
---|
| 659 | $this->db->query("SELECT cat_id FROM phpgw_categories WHERE cat_name='" . $this->db->db_addslashes($cat_name) . "' " |
---|
| 660 | ."AND cat_appname='" . $this->app_name . "' AND (cat_owner=" . $this->account_id . ' OR cat_owner=-1)',__LINE__,__FILE__); |
---|
| 661 | |
---|
| 662 | if(!$this->db->num_rows()) |
---|
| 663 | { |
---|
| 664 | return 0; |
---|
| 665 | } |
---|
| 666 | |
---|
| 667 | $this->db->next_record(); |
---|
| 668 | |
---|
| 669 | return $this->db->f('cat_id'); |
---|
| 670 | } |
---|
| 671 | |
---|
| 672 | function id2name($cat_id = '', $item = 'name') |
---|
| 673 | { |
---|
| 674 | $cat_id = (int)$cat_id; |
---|
| 675 | if($cat_id == 0) |
---|
| 676 | { |
---|
| 677 | return '--'; |
---|
| 678 | } |
---|
| 679 | switch($item) |
---|
| 680 | { |
---|
| 681 | case 'owner': $value = 'cat_owner'; break; |
---|
| 682 | case 'main': $value = 'cat_main'; break; |
---|
| 683 | case 'level': $value = 'cat_level'; break; |
---|
| 684 | case 'parent': $value = 'cat_parent'; break; |
---|
| 685 | case 'name': $value = 'cat_name'; break; |
---|
| 686 | default: $value = 'cat_parent'; break; |
---|
| 687 | } |
---|
| 688 | |
---|
| 689 | $this->db->query("SELECT $value FROM phpgw_categories WHERE cat_id=" . $cat_id,__LINE__,__FILE__); |
---|
| 690 | $this->db->next_record(); |
---|
| 691 | |
---|
| 692 | if ($this->db->f($value)) |
---|
| 693 | { |
---|
| 694 | return $this->db->f($value); |
---|
| 695 | } |
---|
| 696 | else |
---|
| 697 | { |
---|
| 698 | if ($item == 'name') |
---|
| 699 | { |
---|
| 700 | return '--'; |
---|
| 701 | } |
---|
| 702 | } |
---|
| 703 | } |
---|
| 704 | |
---|
| 705 | /*! |
---|
| 706 | @function return_name |
---|
| 707 | @abstract return category name given $cat_id |
---|
| 708 | @param $cat_id |
---|
| 709 | @result cat_name category name |
---|
| 710 | */ |
---|
| 711 | // NOTE: This is only a temp wrapper, use id2name() to keep things matching across the board. (jengo) |
---|
| 712 | function return_name($cat_id) |
---|
| 713 | { |
---|
| 714 | return $this->id2name($cat_id); |
---|
| 715 | } |
---|
| 716 | |
---|
| 717 | /*! |
---|
| 718 | @function exists |
---|
| 719 | @abstract used for checking if a category name exists |
---|
| 720 | @param $type subs or mains |
---|
| 721 | @param $cat_name category name |
---|
| 722 | @result boolean true or false |
---|
| 723 | */ |
---|
| 724 | function exists($type,$cat_name = '',$cat_id = '') |
---|
| 725 | { |
---|
| 726 | $cat_id = (int)$cat_id; |
---|
| 727 | $filter = $this->filter($type); |
---|
| 728 | |
---|
| 729 | if ($cat_name) |
---|
| 730 | { |
---|
| 731 | $cat_exists = " cat_name='" . $this->db->db_addslashes($cat_name) . "' "; |
---|
| 732 | } |
---|
| 733 | |
---|
| 734 | if ($cat_id) |
---|
| 735 | { |
---|
| 736 | $cat_exists = ' cat_parent=' . $cat_id; |
---|
| 737 | } |
---|
| 738 | |
---|
| 739 | if ($cat_name && $cat_id) |
---|
| 740 | { |
---|
| 741 | $cat_exists = " cat_name='" . $this->db->db_addslashes($cat_name) . "' AND cat_id != $cat_id "; |
---|
| 742 | } |
---|
| 743 | |
---|
| 744 | $this->db->query("SELECT COUNT(cat_id) FROM phpgw_categories WHERE $cat_exists $filter",__LINE__,__FILE__); |
---|
| 745 | |
---|
| 746 | $this->db->next_record(); |
---|
| 747 | |
---|
| 748 | if ($this->db->f(0)) |
---|
| 749 | { |
---|
| 750 | return True; |
---|
| 751 | } |
---|
| 752 | else |
---|
| 753 | { |
---|
| 754 | return False; |
---|
| 755 | } |
---|
| 756 | } |
---|
| 757 | } |
---|
| 758 | ?> |
---|