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; |
---|
58 | $this->db2 = $this->db; |
---|
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 |
---|
139 | @param $parent_id |
---|
140 | @param $lastmod integer defaults to -1 |
---|
141 | @param column string default to '' (All), includes the column returned. |
---|
142 | @result $cats array |
---|
143 | */ |
---|
144 | function return_array($type,$start,$limit = True,$query = '',$sort = '',$order = '',$globals = False, $parent_id = '', $lastmod = -1, $column = '') |
---|
145 | { |
---|
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; |
---|
172 | |
---|
173 | if ($globals && !$group_id) |
---|
174 | { |
---|
175 | $global_cats = " cat_appname='phpgw'"; |
---|
176 | } |
---|
177 | |
---|
178 | if (!$sort) |
---|
179 | { |
---|
180 | $sort = 'ASC'; |
---|
181 | } |
---|
182 | |
---|
183 | if (!empty($order) && preg_match('/^[a-zA-Z_, ]+$/',$order) && (empty($sort) || preg_match('/^(ASC|DESC|asc|desc)$/',$sort))) |
---|
184 | { |
---|
185 | $ordermethod = " ORDER BY $order $sort"; |
---|
186 | } |
---|
187 | else |
---|
188 | { |
---|
189 | $ordermethod = ' ORDER BY cat_name ASC'; |
---|
190 | } |
---|
191 | |
---|
192 | if($group_id){ |
---|
193 | $grant_cats .= " cat_owner='".$group_id."' "; |
---|
194 | } |
---|
195 | else if ($this->account_id != '-1'){ |
---|
196 | $grants = $this->grants; |
---|
197 | $groups = $GLOBALS['phpgw']->accounts->membership(); |
---|
198 | |
---|
199 | if (is_array($this->grants)) |
---|
200 | { |
---|
201 | |
---|
202 | foreach($grants as $idx => $user){ |
---|
203 | $public_user_list[$user] = $user; |
---|
204 | } |
---|
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 | |
---|
213 | } |
---|
214 | else |
---|
215 | { |
---|
216 | $grant_cats = " cat_owner='" . $this->account_id . "' or cat_owner='-1' "; |
---|
217 | } |
---|
218 | } |
---|
219 | |
---|
220 | |
---|
221 | $parent_select = ' AND cat_parent=' . $parent_id; |
---|
222 | |
---|
223 | if ($query) |
---|
224 | { |
---|
225 | $querymethod = " AND (cat_name ILIKE '%$query%' OR cat_description ILIKE '%$query%') "; |
---|
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 | |
---|
255 | $this->app_name = pg_escape_string($this->app_name); |
---|
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 | |
---|
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'); |
---|
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 | } |
---|
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'); |
---|
290 | |
---|
291 | $i++; |
---|
292 | } |
---|
293 | |
---|
294 | $num_cats = count($cats); |
---|
295 | for ($i=0;$i < $num_cats;$i++) |
---|
296 | { |
---|
297 | $sub_select = ' AND cat_parent=' . $cats[$i]['id'] . ' AND cat_level=' . ($cats[$i]['level']+1); |
---|
298 | $this->db->query($sql . $sub_select . $ordermethod,__LINE__,__FILE__); |
---|
299 | $total += $this->db->num_rows(); |
---|
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'); |
---|
315 | $j++; |
---|
316 | } |
---|
317 | |
---|
318 | $num_subcats = count($subcats); |
---|
319 | if ($num_subcats != 0) |
---|
320 | { |
---|
321 | $newcats = array(); |
---|
322 | for ($k = 0; $k <= $i; $k++) |
---|
323 | { |
---|
324 | $newcats[$k] = $cats[$k]; |
---|
325 | } |
---|
326 | for ($k = 0; $k < $num_subcats; $k++) |
---|
327 | { |
---|
328 | $newcats[$k+$i+1] = $subcats[$k]; |
---|
329 | } |
---|
330 | for ($k = $i+1; $k < $num_cats; $k++) |
---|
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'); |
---|
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 | } |
---|
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 | } |
---|
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 | |
---|
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 | { |
---|
424 | for ($i=0;$i<count($cats);$i++) |
---|
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 .= '>'; |
---|
443 | for ($j=0;$j<$cat['level'];$j++) |
---|
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 | { |
---|
469 | for ($i=0;$i<count($cats);$i++) |
---|
470 | { |
---|
471 | $image_set = ' '; |
---|
472 | |
---|
473 | if (in_array($cats[$i]['id'],$selected)) |
---|
474 | { |
---|
475 | $image_set = '<img src="' . PHPGW_IMAGES_DIR . '/roter_pfeil.gif">'; |
---|
476 | } |
---|
477 | |
---|
478 | if (($cats[$i]['level'] == 0) && !in_array($cats[$i]['id'],$selected)) |
---|
479 | { |
---|
480 | $image_set = '<img src="' . PHPGW_IMAGES_DIR . '/grauer_pfeil.gif">'; |
---|
481 | } |
---|
482 | |
---|
483 | $space_set = str_repeat($space,$cats[$i]['level']); |
---|
484 | |
---|
485 | $s .= '<tr>' . "\n"; |
---|
486 | $s .= '<td width="8">' . $image_set . '</td>' . "\n"; |
---|
487 | $s .= '<td>' . $space_set . '<a href="' . $GLOBALS['phpgw']->link($site_link,'cat_id=' . $cats[$i]['id']) . '">' |
---|
488 | . $GLOBALS['phpgw']->strip_html($cats[$i]['name']) |
---|
489 | . '</a></td>' . "\n" |
---|
490 | . '</tr>' . "\n"; |
---|
491 | } |
---|
492 | } |
---|
493 | $s .= '</table>' . "\n"; |
---|
494 | return $s; |
---|
495 | } |
---|
496 | } |
---|
497 | |
---|
498 | /*! |
---|
499 | @function add |
---|
500 | @abstract add categories |
---|
501 | @param $cat_name category name |
---|
502 | @param $cat_parent category parent |
---|
503 | @param $cat_description category description defaults to '' |
---|
504 | @param $cat_data category data defaults to '' |
---|
505 | */ |
---|
506 | function add($values) |
---|
507 | { |
---|
508 | $values['id'] = (int)$values['id']; |
---|
509 | $values['parent'] = (int)$values['parent']; |
---|
510 | |
---|
511 | if ($values['parent'] > 0) |
---|
512 | { |
---|
513 | $values['level'] = $this->id2name($values['parent'],'level')+1; |
---|
514 | $values['main'] = $this->id2name($values['parent'],'main'); |
---|
515 | } |
---|
516 | |
---|
517 | $values['descr'] = $this->db->db_addslashes($values['descr']); |
---|
518 | $values['name'] = $this->db->db_addslashes($values['name']); |
---|
519 | |
---|
520 | if ($values['id'] > 0) |
---|
521 | { |
---|
522 | $id_col = 'cat_id,'; |
---|
523 | $id_val = $values['id'] . ','; |
---|
524 | } |
---|
525 | $this->db->query('INSERT INTO phpgw_categories (' . $id_col . 'cat_parent,cat_owner,cat_access,cat_appname,cat_name,cat_description,cat_data,' |
---|
526 | . 'cat_main,cat_level, last_mod) VALUES (' . $id_val . (int)$values['parent'] . ',' . ($values['group']!= 0 ? $values['group'] : $this->account_id) . ",'" . $values['access'] |
---|
527 | . "','" . $this->app_name . "','" . $values['name'] . "','" . $values['descr'] . "','" . $values['data'] |
---|
528 | . "'," . (int)$values['main'] . ',' . (int)$values['level'] . ',' . time() . ')',__LINE__,__FILE__); |
---|
529 | |
---|
530 | if ($values['id'] > 0) |
---|
531 | { |
---|
532 | $max = $values['id']; |
---|
533 | } |
---|
534 | else |
---|
535 | { |
---|
536 | $max = $this->db->get_last_insert_id('phpgw_categories','cat_id'); |
---|
537 | } |
---|
538 | |
---|
539 | $max = (int)$max; |
---|
540 | if ($values['parent'] == 0) |
---|
541 | { |
---|
542 | $this->db->query('UPDATE phpgw_categories SET cat_main=' . $max . ' WHERE cat_id=' . $max,__LINE__,__FILE__); |
---|
543 | } |
---|
544 | return $max; |
---|
545 | } |
---|
546 | |
---|
547 | /*! |
---|
548 | @function delete |
---|
549 | @abstract delete category |
---|
550 | @param $cat_id int - category id |
---|
551 | */ |
---|
552 | /*function delete($cat_id,$subs = False) |
---|
553 | { |
---|
554 | $cat_id = (int)$cat_id; |
---|
555 | if ($subs) |
---|
556 | { |
---|
557 | $subdelete = ' OR cat_parent=' . $cat_id . ' OR cat_main=' . $cat_id; |
---|
558 | } |
---|
559 | |
---|
560 | $this->db->query('DELETE FROM phpgw_categories WHERE cat_id=' . $cat_id . $subdelete . " AND cat_appname='" |
---|
561 | . $this->app_name . "'",__LINE__,__FILE__); |
---|
562 | } */ |
---|
563 | |
---|
564 | function delete($cat_id, $drop_subs = False, $modify_subs = False) |
---|
565 | { |
---|
566 | $cat_id = (int)$cat_id; |
---|
567 | if ($drop_subs) |
---|
568 | { |
---|
569 | $subdelete = ' OR cat_parent=' . $cat_id . ' OR cat_main=' . $cat_id; |
---|
570 | } |
---|
571 | |
---|
572 | if ($modify_subs) |
---|
573 | { |
---|
574 | $cats = $this->return_sorted_array('',False,'','','',False, $cat_id); |
---|
575 | |
---|
576 | $new_parent = $this->id2name($cat_id,'parent'); |
---|
577 | |
---|
578 | for ($i=0;$i<count($cats);$i++) |
---|
579 | { |
---|
580 | if ($cats[$i]['level'] == 1) |
---|
581 | { |
---|
582 | $this->db->query('UPDATE phpgw_categories set cat_level=0, cat_parent=0, cat_main=' . (int)$cats[$i]['id'] |
---|
583 | . ' WHERE cat_id=' . (int)$cats[$i]['id'] . " AND cat_appname='" . $this->app_name . "'",__LINE__,__FILE__); |
---|
584 | $new_main = $cats[$i]['id']; |
---|
585 | } |
---|
586 | else |
---|
587 | { |
---|
588 | if ($new_main) |
---|
589 | { |
---|
590 | $update_main = ',cat_main=' . $new_main; |
---|
591 | } |
---|
592 | |
---|
593 | if ($cats[$i]['parent'] == $cat_id) |
---|
594 | { |
---|
595 | $update_parent = ',cat_parent=' . $new_parent; |
---|
596 | } |
---|
597 | |
---|
598 | $this->db->query('UPDATE phpgw_categories set cat_level=' . ($cats[$i]['level']-1) . $update_main . $update_parent |
---|
599 | . ' WHERE cat_id=' . (int)$cats[$i]['id'] . " AND cat_appname='" . $this->app_name . "'",__LINE__,__FILE__); |
---|
600 | } |
---|
601 | } |
---|
602 | } |
---|
603 | |
---|
604 | $this->db->query('DELETE FROM phpgw_categories WHERE cat_id=' . $cat_id . $subdelete . " AND cat_appname='" |
---|
605 | . $this->app_name . "'",__LINE__,__FILE__); |
---|
606 | } |
---|
607 | |
---|
608 | /*! |
---|
609 | @function edit |
---|
610 | @abstract edit a category |
---|
611 | @param $cat_id int - category id |
---|
612 | @param $cat_parent category parent |
---|
613 | @param $cat_description category description defaults to '' |
---|
614 | @param $cat_data category data defaults to '' |
---|
615 | */ |
---|
616 | function edit($values) |
---|
617 | { |
---|
618 | $values['id'] = (int)$values['id']; |
---|
619 | $values['parent'] = (int)$values['parent']; |
---|
620 | $values['owner'] = (int)$values['group']; |
---|
621 | if($values['owner']){ |
---|
622 | $owner = "cat_owner = ".$values['owner'].","; |
---|
623 | } |
---|
624 | if (isset($values['old_parent']) && (int)$values['old_parent'] != $values['parent']) |
---|
625 | { |
---|
626 | $this->delete($values['id'],False,True); |
---|
627 | return $this->add($values); |
---|
628 | } |
---|
629 | else |
---|
630 | { |
---|
631 | if ($values['parent'] > 0) |
---|
632 | { |
---|
633 | $values['main'] = (int)$this->id2name($values['parent'],'main'); |
---|
634 | $values['level'] = (int)$this->id2name($values['parent'],'level') + 1; |
---|
635 | } |
---|
636 | else |
---|
637 | { |
---|
638 | $values['main'] = $values['id']; |
---|
639 | $values['level'] = 0; |
---|
640 | } |
---|
641 | } |
---|
642 | |
---|
643 | $values['descr'] = $this->db->db_addslashes($values['descr']); |
---|
644 | $values['name'] = $this->db->db_addslashes($values['name']); |
---|
645 | |
---|
646 | $sql = "UPDATE phpgw_categories SET $owner cat_name='" . $values['name'] . "', cat_description='" . $values['descr'] |
---|
647 | . "', cat_data='" . $values['data'] . "', cat_parent=" . $values['parent'] . ", cat_access='" |
---|
648 | . $values['access'] . "', cat_main=" . $values['main'] . ', cat_level=' . $values['level'] . ',last_mod=' . time() |
---|
649 | . " WHERE cat_appname='" . $this->app_name . "' AND cat_id=" . $values['id']; |
---|
650 | |
---|
651 | $this->db->query($sql,__LINE__,__FILE__); |
---|
652 | return $values['id']; |
---|
653 | } |
---|
654 | |
---|
655 | function name2id($cat_name) |
---|
656 | { |
---|
657 | $this->db->query("SELECT cat_id FROM phpgw_categories WHERE cat_name='" . $this->db->db_addslashes($cat_name) . "' " |
---|
658 | ."AND cat_appname='" . $this->app_name . "' AND (cat_owner=" . $this->account_id . ' OR cat_owner=-1)',__LINE__,__FILE__); |
---|
659 | |
---|
660 | if(!$this->db->num_rows()) |
---|
661 | { |
---|
662 | return 0; |
---|
663 | } |
---|
664 | |
---|
665 | $this->db->next_record(); |
---|
666 | |
---|
667 | return $this->db->f('cat_id'); |
---|
668 | } |
---|
669 | |
---|
670 | function id2name($cat_id = '', $item = 'name') |
---|
671 | { |
---|
672 | $cat_id = (int)$cat_id; |
---|
673 | if($cat_id == 0) |
---|
674 | { |
---|
675 | return '--'; |
---|
676 | } |
---|
677 | switch($item) |
---|
678 | { |
---|
679 | case 'owner': $value = 'cat_owner'; break; |
---|
680 | case 'main': $value = 'cat_main'; break; |
---|
681 | case 'level': $value = 'cat_level'; break; |
---|
682 | case 'parent': $value = 'cat_parent'; break; |
---|
683 | case 'name': $value = 'cat_name'; break; |
---|
684 | default: $value = 'cat_parent'; break; |
---|
685 | } |
---|
686 | |
---|
687 | $this->db->query("SELECT $value FROM phpgw_categories WHERE cat_id=" . $cat_id,__LINE__,__FILE__); |
---|
688 | $this->db->next_record(); |
---|
689 | |
---|
690 | if ($this->db->f($value)) |
---|
691 | { |
---|
692 | return $this->db->f($value); |
---|
693 | } |
---|
694 | else |
---|
695 | { |
---|
696 | if ($item == 'name') |
---|
697 | { |
---|
698 | return '--'; |
---|
699 | } |
---|
700 | } |
---|
701 | } |
---|
702 | |
---|
703 | /*! |
---|
704 | @function return_name |
---|
705 | @abstract return category name given $cat_id |
---|
706 | @param $cat_id |
---|
707 | @result cat_name category name |
---|
708 | */ |
---|
709 | // NOTE: This is only a temp wrapper, use id2name() to keep things matching across the board. (jengo) |
---|
710 | function return_name($cat_id) |
---|
711 | { |
---|
712 | return $this->id2name($cat_id); |
---|
713 | } |
---|
714 | |
---|
715 | /*! |
---|
716 | @function exists |
---|
717 | @abstract used for checking if a category name exists |
---|
718 | @param $type subs or mains |
---|
719 | @param $cat_name category name |
---|
720 | @result boolean true or false |
---|
721 | */ |
---|
722 | function exists($type,$cat_name = '',$cat_id = '') |
---|
723 | { |
---|
724 | $cat_id = (int)$cat_id; |
---|
725 | $filter = $this->filter($type); |
---|
726 | |
---|
727 | if ($cat_name) |
---|
728 | { |
---|
729 | $cat_exists = " cat_name='" . $this->db->db_addslashes($cat_name) . "' "; |
---|
730 | } |
---|
731 | |
---|
732 | if ($cat_id) |
---|
733 | { |
---|
734 | $cat_exists = ' cat_parent=' . $cat_id; |
---|
735 | } |
---|
736 | |
---|
737 | if ($cat_name && $cat_id) |
---|
738 | { |
---|
739 | $cat_exists = " cat_name='" . $this->db->db_addslashes($cat_name) . "' AND cat_id != $cat_id "; |
---|
740 | } |
---|
741 | |
---|
742 | $this->db->query("SELECT COUNT(cat_id) FROM phpgw_categories WHERE $cat_exists $filter",__LINE__,__FILE__); |
---|
743 | |
---|
744 | $this->db->next_record(); |
---|
745 | |
---|
746 | if ($this->db->f(0)) |
---|
747 | { |
---|
748 | return True; |
---|
749 | } |
---|
750 | else |
---|
751 | { |
---|
752 | return False; |
---|
753 | } |
---|
754 | } |
---|
755 | } |
---|
756 | ?> |
---|