source: trunk/phpgwapi/inc/class.categories.inc.php @ 2

Revision 2, 24.0 KB checked in by niltonneto, 17 years ago (diff)

Removida todas as tags usadas pelo CVS ($Id, $Source).
Primeira versão no CVS externo.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
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                @result $cats array
140                */
141                function return_array($type,$start,$limit = True,$query = '',$sort = '',$order = '',$globals = False, $parent_id = '', $lastmod = -1, $column = '')
142                {
143                        //casting and addslashes for security
144                        $start          = (int)$start;
145                        $parent_id      = (int)$parent_id;
146                        $query          = $this->db->db_addslashes($query);
147                        $sort           = $this->db->db_addslashes($sort);
148                        $order          = $this->db->db_addslashes($order);
149
150                        if ($globals)
151                        {
152                                $global_cats = " OR cat_appname='phpgw'";
153                        }
154
155                        $filter = $this->filter($type);
156
157                        if (!$sort)
158                        {
159                                $sort = 'ASC';
160                        }
161
162                        if (!empty($order) && preg_match('/^[a-zA-Z_(), ]+$/',$order) && (empty($sort) || preg_match('/^(ASC|DESC|asc|desc)$/',$sort)))
163                        {
164                                $ordermethod = " ORDER BY $order $sort";
165                        }
166                        else
167                        {
168                                $ordermethod = ' ORDER BY cat_main, cat_level, cat_name ASC';
169                        }
170
171                        if ($this->account_id == '-1')
172                        {
173                                $grant_cats = ' cat_owner=-1 ';
174                        }
175                        else
176                        {
177                                if (is_array($this->grants))
178                                {
179                                        $grants = $this->grants;
180                                        while(list($user) = each($grants))
181                                        {
182                                                $public_user_list[] = $user;
183                                        }
184                                        reset($public_user_list);
185                                        $grant_cats = ' (cat_owner=' . $this->account_id . " OR cat_owner=-1 OR cat_access='public' AND cat_owner in(" . implode(',',$public_user_list) . ')) ';
186                                }
187                                else
188                                {
189                                        $grant_cats = ' cat_owner=' . $this->account_id . ' OR cat_owner=-1 ';
190                                }
191                        }
192
193                        if ($parent_id > 0)
194                        {
195                                $parent_filter = ' AND cat_parent=' . $parent_id;
196                        }
197
198                        if ($query)
199                        {
200                                $querymethod = " AND (cat_name LIKE '%$query%' OR cat_description LIKE '%$query%') ";
201                        }
202
203                        if($lastmod && $lastmod >= 0)
204                        {
205                                $querymethod .= ' AND last_mod > ' . (int)$lastmod;
206                        }
207
208                        if($column)
209                        {
210                                switch($column)
211                                {
212                                        case 'id':                      $table_column = ' cat_id '; break;
213                                        case 'owner':           $table_column = ' cat_owner '; break;
214                                        case 'access':          $table_column = ' cat_access '; break;
215                                        case 'app_name':        $table_column = ' cat_appname '; break;
216                                        case 'main':            $table_column = ' cat_main '; break;
217                                        case 'parent':          $table_column = ' cat_parent '; break;
218                                        case 'name':            $table_column = ' cat_name '; break;
219                                        case 'description': $table_column = ' cat_description '; break;
220                                        case 'data':            $table_column = ' cat_data '; break;
221                                        case 'last_mod':        $table_column = ' last_mod '; break;
222                                        default:                        $table_column = ' cat_id '; break;
223                                }
224                        }
225                        else
226                        {
227                                $table_column = ' * ';
228                        }
229
230                        $sql = "SELECT $table_column FROM phpgw_categories WHERE (cat_appname='" . $this->app_name . "' AND" . $grant_cats . $global_cats . ')'
231                                . $parent_filter . $querymethod . $filter;
232
233                        $this->db2->query($sql,__LINE__,__FILE__);
234                        $this->total_records = $this->db2->num_rows();
235
236                        if ($limit)
237                        {
238                                $this->db->limit_query($sql . $ordermethod,$start,__LINE__,__FILE__);
239                        }
240                        else
241                        {
242                                $this->db->query($sql . $ordermethod,__LINE__,__FILE__);
243                        }
244
245                        while ($this->db->next_record())
246                        {
247                                if ($column)
248                                {
249                                        $cats[] = array
250                                        (
251                                                $column => $this->db->f(0)
252                                        );
253                                }
254                                else
255                                {
256                                        $cats[] = array
257                                        (
258                                                'id'                    => $this->db->f('cat_id'),
259                                                'owner'                 => $this->db->f('cat_owner'),
260                                                'access'                => $this->db->f('cat_access'),
261                                                'app_name'              => $this->db->f('cat_appname'),
262                                                'main'                  => $this->db->f('cat_main'),
263                                                'level'                 => $this->db->f('cat_level'),
264                                                'parent'                => $this->db->f('cat_parent'),
265                                                'name'                  => $this->db->f('cat_name'),
266                                                'description'   => $this->db->f('cat_description'),
267                                                'data'                  => $this->db->f('cat_data'),
268                                                'last_mod'              => $this->db->f('last_mod')
269                                        );
270                                }
271                        }
272                        return $cats;
273                }
274
275                function return_sorted_array($start,$limit = True,$query = '',$sort = '',$order = '',$globals = False, $parent_id = '')
276                {
277                        //casting and slashes for security
278                        $start = (int)$start;
279                        $query = $this->db->db_addslashes($query);
280                        $sort  = $this->db->db_addslashes($sort);
281                        $order = $this->db->db_addslashes($order);
282                        $parent_id = (int)$parent_id;
283
284                        if ($globals)
285                        {
286                                $global_cats = " OR cat_appname='phpgw'";
287                        }
288
289                        if (!$sort)
290                        {
291                                $sort = 'ASC';
292                        }
293
294                        if (!empty($order) && preg_match('/^[a-zA-Z_, ]+$/',$order) && (empty($sort) || preg_match('/^(ASC|DESC|asc|desc)$/')))
295                        {
296                                $ordermethod = " ORDER BY $order $sort";
297                        }
298                        else
299                        {
300                                $ordermethod = ' ORDER BY cat_name ASC';
301                        }
302
303                        if ($this->account_id == '-1')
304                        {
305                                $grant_cats = " cat_owner='-1' ";
306                        }
307                        else
308                        {
309                                if (is_array($this->grants))
310                                {
311                                        $grants = $this->grants;
312                                        while(list($user) = each($grants))
313                                        {
314                                                $public_user_list[] = $user;
315                                        }
316                                        reset($public_user_list);
317                                        $grant_cats = " (cat_owner='" . $this->account_id . "' OR cat_owner='-1' OR cat_access='public' AND cat_owner in(" . implode(',',$public_user_list) . ")) ";
318                                }
319                                else
320                                {
321                                        $grant_cats = " cat_owner='" . $this->account_id . "' or cat_owner='-1' ";
322                                }
323                        }
324
325                        $parent_select = ' AND cat_parent=' . $parent_id;
326
327                        if ($query)
328                        {
329                                $querymethod = " AND (cat_name LIKE '%$query%' OR cat_description LIKE '%$query%') ";
330                        }
331
332                        $sql = "SELECT * FROM phpgw_categories WHERE (cat_appname='" . $this->app_name . "' AND" . $grant_cats . $global_cats . ")"
333                                        . $querymethod;
334
335                        $this->db2->query($sql . $parent_select,__LINE__,__FILE__);
336                        $total = $this->db2->num_rows();
337
338                        if ($limit)
339                        {
340                                $this->db->limit_query($sql . $parent_select . $ordermethod,$start,__LINE__,__FILE__);
341                        }
342                        else
343                        {
344                                $this->db->query($sql . $parent_select . $ordermethod,__LINE__,__FILE__);
345                        }
346
347                        $i = 0;
348                        while ($this->db->next_record())
349                        {
350                                $cats[$i]['id']          = (int)$this->db->f('cat_id');
351                                $cats[$i]['owner']       = (int)$this->db->f('cat_owner');
352                                $cats[$i]['access']      = $this->db->f('cat_access');
353                                $cats[$i]['app_name']    = $this->db->f('cat_appname');
354                                $cats[$i]['main']        = (int)$this->db->f('cat_main');
355                                $cats[$i]['level']       = (int)$this->db->f('cat_level');
356                                $cats[$i]['parent']      = (int)$this->db->f('cat_parent');
357                                $cats[$i]['name']        = $this->db->f('cat_name');
358                                $cats[$i]['description'] = $this->db->f('cat_description');
359                                $cats[$i]['data']        = $this->db->f('cat_data');
360                                $i++;
361                        }
362
363                        $num_cats = count($cats);
364                        for ($i=0;$i < $num_cats;$i++)
365                        {
366                                $sub_select = ' AND cat_parent=' . $cats[$i]['id'] . ' AND cat_level=' . ($cats[$i]['level']+1);
367
368                                /*$this->db2->query($sql . $sub_select,__LINE__,__FILE__);
369                                $total_subs += $this->db2->num_rows();
370
371                                if ($limit)
372                                {
373                                        $this->db->limit_query($sql . $sub_select . $ordermethod,$start,__LINE__,__FILE__);
374                                }
375                                else
376                                {*/
377                                        $this->db->query($sql . $sub_select . $ordermethod,__LINE__,__FILE__);
378                                        $total += $this->db->num_rows();
379                                //}
380
381                                $subcats = array();
382                                $j = 0;
383                                while ($this->db->next_record())
384                                {
385                                        $subcats[$j]['id']          = (int)$this->db->f('cat_id');
386                                        $subcats[$j]['owner']       = (int)$this->db->f('cat_owner');
387                                        $subcats[$j]['access']      = $this->db->f('cat_access');
388                                        $subcats[$j]['app_name']    = $this->db->f('cat_appname');
389                                        $subcats[$j]['main']        = (int)$this->db->f('cat_main');
390                                        $subcats[$j]['level']       = (int)$this->db->f('cat_level');
391                                        $subcats[$j]['parent']      = (int)$this->db->f('cat_parent');
392                                        $subcats[$j]['name']        = $this->db->f('cat_name');
393                                        $subcats[$j]['description'] = $this->db->f('cat_description');
394                                        $subcats[$j]['data']        = $this->db->f('cat_data');
395                                        $j++;
396                                }
397
398                                $num_subcats = count($subcats);
399                                if ($num_subcats != 0)
400                                {
401                                        $newcats = array();
402                                        for ($k = 0; $k <= $i; $k++)
403                                        {
404                                                $newcats[$k] = $cats[$k];
405                                        }
406                                        for ($k = 0; $k < $num_subcats; $k++)
407                                        {
408                                                $newcats[$k+$i+1] = $subcats[$k];
409                                        }
410                                        for ($k = $i+1; $k < $num_cats; $k++)
411                                        {
412                                                $newcats[$k+$num_subcats] = $cats[$k];
413                                        }
414                                        $cats = $newcats;
415                                        $num_cats = count($cats);
416                                }
417                        }
418                        $this->total_records = $total;
419                        return $cats;
420                }
421
422                /*!
423                @function return_single
424                @abstract return single
425                @param $id integer id of category
426                @result $cats  array populated with
427                */
428                function return_single($id = '')
429                {
430                        $this->db->query('SELECT * FROM phpgw_categories WHERE cat_id=' . (int)$id,__LINE__,__FILE__);
431
432                        if ($this->db->next_record())
433                        {
434                                $cats[0]['id']          = $this->db->f('cat_id');
435                                $cats[0]['owner']       = $this->db->f('cat_owner');
436                                $cats[0]['access']      = $this->db->f('cat_access');
437                                $cats[0]['app_name']    = $this->db->f('cat_appname');
438                                $cats[0]['main']        = $this->db->f('cat_main');
439                                $cats[0]['level']       = $this->db->f('cat_level');
440                                $cats[0]['parent']      = $this->db->f('cat_parent');
441                                $cats[0]['name']        = $this->db->f('cat_name');
442                                $cats[0]['description'] = $this->db->f('cat_description');
443                                $cats[0]['data']        = $this->db->f('cat_data');
444                        }
445                        return $cats;
446                }
447
448                /*!
449                @function formated_list
450                @abstract return into a select box, list or other formats
451                @param $format currently supports select (select box) or list
452                @param $type string - subs or mains
453                @param $selected - cat_id or array with cat_id values
454                @param $globals True or False, includes the global egroupware categories or not
455                @result $s array - populated with categories
456                */
457                function formatted_list($format,$type='',$selected = '',$globals = False,$site_link = 'site')
458                {
459                        return $this->formated_list($format,$type,$selected,$globals,$site_link);
460                }
461                function formated_list($format,$type='',$selected = '',$globals = False,$site_link = 'site')
462                {
463                        if(is_array($format))
464                        {
465                                $temp_format = $format['format'];
466                                $type = ($format['type']?$format['type']:'all');
467                                $selected = (isset($format['selected'])?$format['selected']:'');
468                                $self = (isset($format['self'])?$format['self']:'');
469                                $globals = (isset($format['globals'])?$format['globals']:True);
470                                $site_link = (isset($format['site_link'])?$format['site_link']:'site');
471                                settype($format,'string');
472                                $format = ($temp_format?$temp_format:'select');
473                                unset($temp_format);
474                        }
475
476                        if (!is_array($selected))
477                        {
478                                $selected = explode(',',$selected);
479                        }
480
481                        if ($type != 'all')
482                        {
483                                $cats = $this->return_array($type,$start,False,$query,$sort,$order,$globals);
484                        }
485                        else
486                        {
487                                $cats = $this->return_sorted_array($start,False,$query,$sort,$order,$globals);
488                        }
489
490                        if($self)
491                        {
492                                for ($i=0;$i<count($cats);$i++)
493                                {
494                                        if ($cats[$i]['id'] == $self)
495                                        {
496                                                unset($cats[$i]);
497                                        }
498                                }
499                        }
500
501                        if ($format == 'select')
502                        {
503                                while (is_array($cats) && list(,$cat) = each($cats))
504                                {
505                                        $s .= '<option value="' . $cat['id'] . '"';
506                                        if (in_array($cat['id'],$selected))
507                                        {
508                                                $s .= ' selected';
509                                        }
510                                        $s .= '>';
511                                        for ($j=0;$j<$cat['level'];$j++)
512                                        {
513                                                $s .= '&nbsp;';
514                                        }
515                                        $s .= $GLOBALS['phpgw']->strip_html($cat['name']);
516                                        if ($cat['app_name'] == 'phpgw')
517                                        {
518                                                $s .= '&nbsp;&lt;' . lang('Global') . '&gt;';
519                                        }
520                                        if ($cat['owner'] == '-1')
521                                        {
522                                                $s .= '&nbsp;&lt;' . lang('Global') . '&nbsp;' . lang($this->app_name) . '&gt;';
523                                        }
524                                        $s .= '</option>' . "\n";
525                                }
526                                return $s;
527                        }
528
529                        if ($format == 'list')
530                        {
531                                $space = '&nbsp;&nbsp;';
532
533                                $s  = '<table border="0" cellpadding="2" cellspacing="2">' . "\n";
534
535                                if ($this->total_records > 0)
536                                {
537                                        for ($i=0;$i<count($cats);$i++)
538                                        {
539                                                $image_set = '&nbsp;';
540
541                                                if (in_array($cats[$i]['id'],$selected))
542                                                {
543                                                        $image_set = '<img src="' . PHPGW_IMAGES_DIR . '/roter_pfeil.gif">';
544                                                }
545
546                                                if (($cats[$i]['level'] == 0) && !in_array($cats[$i]['id'],$selected))
547                                                {
548                                                        $image_set = '<img src="' . PHPGW_IMAGES_DIR . '/grauer_pfeil.gif">';
549                                                }
550
551                                                $space_set = str_repeat($space,$cats[$i]['level']);
552
553                                                $s .= '<tr>' . "\n";
554                                                $s .= '<td width="8">' . $image_set . '</td>' . "\n";
555                                                $s .= '<td>' . $space_set . '<a href="' . $GLOBALS['phpgw']->link($site_link,'cat_id=' . $cats[$i]['id']) . '">'
556                                                        . $GLOBALS['phpgw']->strip_html($cats[$i]['name'])
557                                                        . '</a></td>' . "\n"
558                                                        . '</tr>' . "\n";
559                                        }
560                                }
561                                $s .= '</table>' . "\n";
562                                return $s;
563                        }
564                }
565
566                /*!
567                @function add
568                @abstract add categories
569                @param $cat_name category name
570                @param $cat_parent category parent
571                @param $cat_description category description defaults to ''
572                @param $cat_data category data defaults to ''
573                */
574                function add($values)
575                {
576                        $values['id']           = (int)$values['id'];
577                        $values['parent']       = (int)$values['parent'];
578
579                        if ($values['parent'] > 0)
580                        {
581                                $values['level'] = $this->id2name($values['parent'],'level')+1;
582                                $values['main'] = $this->id2name($values['parent'],'main');
583                        }
584
585                        $values['descr'] = $this->db->db_addslashes($values['descr']);
586                        $values['name'] = $this->db->db_addslashes($values['name']);
587
588                        if ($values['id'] > 0)
589                        {
590                                $id_col = 'cat_id,';
591                                $id_val = $values['id'] . ',';
592                        }
593
594                        $this->db->query('INSERT INTO phpgw_categories (' . $id_col . 'cat_parent,cat_owner,cat_access,cat_appname,cat_name,cat_description,cat_data,'
595                                . 'cat_main,cat_level, last_mod) VALUES (' . $id_val . (int)$values['parent'] . ',' . $this->account_id . ",'" . $values['access']
596                                . "','" . $this->app_name . "','" . $values['name'] . "','" . $values['descr'] . "','" . $values['data']
597                                . "'," . (int)$values['main'] . ',' . (int)$values['level'] . ',' . time() . ')',__LINE__,__FILE__);
598
599                        if ($values['id'] > 0)
600                        {
601                                $max = $values['id'];
602                        }
603                        else
604                        {
605                                $max = $this->db->get_last_insert_id('phpgw_categories','cat_id');
606                        }
607
608                        $max = (int)$max;
609                        if ($values['parent'] == 0)
610                        {
611                                $this->db->query('UPDATE phpgw_categories SET cat_main=' . $max . ' WHERE cat_id=' . $max,__LINE__,__FILE__);
612                        }
613                        return $max;
614                }
615
616                /*!
617                @function delete
618                @abstract delete category
619                @param $cat_id int - category id
620                */
621                /*function delete($cat_id,$subs = False)
622                {
623                        $cat_id = (int)$cat_id;
624                        if ($subs)
625                        {
626                                $subdelete = ' OR cat_parent=' . $cat_id . ' OR cat_main=' . $cat_id;
627                        }
628
629                        $this->db->query('DELETE FROM phpgw_categories WHERE cat_id=' . $cat_id . $subdelete . " AND cat_appname='"
630                                                        . $this->app_name . "'",__LINE__,__FILE__);
631                } */
632
633                function delete($cat_id, $drop_subs = False, $modify_subs = False)
634                {
635                        $cat_id = (int)$cat_id;
636                        if ($drop_subs)
637                        {
638                                $subdelete = ' OR cat_parent=' . $cat_id . ' OR cat_main=' . $cat_id;
639                        }
640
641                        if ($modify_subs)
642                        {
643                                $cats = $this->return_sorted_array('',False,'','','',False, $cat_id);
644
645                                $new_parent = $this->id2name($cat_id,'parent');
646
647                                for ($i=0;$i<count($cats);$i++)
648                                {
649                                        if ($cats[$i]['level'] == 1)
650                                        {
651                                                $this->db->query('UPDATE phpgw_categories set cat_level=0, cat_parent=0, cat_main=' . (int)$cats[$i]['id']
652                                                        . ' WHERE cat_id=' . (int)$cats[$i]['id'] . " AND cat_appname='" . $this->app_name . "'",__LINE__,__FILE__);
653                                                $new_main = $cats[$i]['id'];
654                                        }
655                                        else
656                                        {
657                                                if ($new_main)
658                                                {
659                                                        $update_main = ',cat_main=' . $new_main;
660                                                }
661
662                                                if ($cats[$i]['parent'] == $cat_id)
663                                                {
664                                                        $update_parent = ',cat_parent=' . $new_parent;
665                                                }
666
667                                                $this->db->query('UPDATE phpgw_categories set cat_level=' . ($cats[$i]['level']-1) . $update_main . $update_parent
668                                                        . ' WHERE cat_id=' . (int)$cats[$i]['id'] . " AND cat_appname='" . $this->app_name . "'",__LINE__,__FILE__);
669                                        }
670                                }
671                        }
672
673                        $this->db->query('DELETE FROM phpgw_categories WHERE cat_id=' . $cat_id . $subdelete . " AND cat_appname='"
674                                . $this->app_name . "'",__LINE__,__FILE__);
675                }
676
677                /*!
678                @function edit
679                @abstract edit a category
680                @param $cat_id int - category id
681                @param $cat_parent category parent
682                @param $cat_description category description defaults to ''
683                @param $cat_data category data defaults to ''
684                */
685                function edit($values)
686                {
687                        $values['id']     = (int)$values['id'];
688                        $values['parent'] = (int)$values['parent'];
689
690                        if (isset($values['old_parent']) && (int)$values['old_parent'] != $values['parent'])
691                        {
692                                $this->delete($values['id'],False,True);
693                                return $this->add($values);
694                        }
695                        else
696                        {
697                                if ($values['parent'] > 0)
698                                {
699                                        $values['main']  = (int)$this->id2name($values['parent'],'main');
700                                        $values['level'] = (int)$this->id2name($values['parent'],'level') + 1;
701                                }
702                                else
703                                {
704                                        $values['main']  = $values['id'];
705                                        $values['level'] = 0;
706                                }
707                        }
708
709                        $values['descr'] = $this->db->db_addslashes($values['descr']);
710                        $values['name'] = $this->db->db_addslashes($values['name']);
711
712                        $sql = "UPDATE phpgw_categories SET cat_name='" . $values['name'] . "', cat_description='" . $values['descr']
713                                . "', cat_data='" . $values['data'] . "', cat_parent=" . $values['parent'] . ", cat_access='"
714                                . $values['access'] . "', cat_main=" . $values['main'] . ', cat_level=' . $values['level'] . ',last_mod=' . time()
715                                . " WHERE cat_appname='" . $this->app_name . "' AND cat_id=" . $values['id'];
716
717                        $this->db->query($sql,__LINE__,__FILE__);
718                        return $values['id'];
719                }
720
721                function name2id($cat_name)
722                {
723                        $this->db->query("SELECT cat_id FROM phpgw_categories WHERE cat_name='" . $this->db->db_addslashes($cat_name) . "' "
724                                ."AND cat_appname='" . $this->app_name . "' AND (cat_owner=" . $this->account_id . ' OR cat_owner=-1)',__LINE__,__FILE__);
725
726                        if(!$this->db->num_rows())
727                        {
728                                return 0;
729                        }
730
731                        $this->db->next_record();
732
733                        return $this->db->f('cat_id');
734                }
735
736                function id2name($cat_id = '', $item = 'name')
737                {
738                        $cat_id = (int)$cat_id;
739                        if($cat_id == 0)
740                        {
741                                return '--';
742                        }
743                        switch($item)
744                        {
745                                case 'owner':   $value = 'cat_owner'; break;
746                                case 'main':    $value = 'cat_main'; break;
747                                case 'level':   $value = 'cat_level'; break;
748                                case 'parent':  $value = 'cat_parent'; break;
749                                case 'name':    $value = 'cat_name'; break;
750                                default:                $value = 'cat_parent'; break;
751                        }
752
753                        $this->db->query("SELECT $value FROM phpgw_categories WHERE cat_id=" . $cat_id,__LINE__,__FILE__);
754                        $this->db->next_record();
755
756                        if ($this->db->f($value))
757                        {
758                                return $this->db->f($value);
759                        }
760                        else
761                        {
762                                if ($item == 'name')
763                                {
764                                        return '--';
765                                }
766                        }
767                }
768
769                /*!
770                @function return_name
771                @abstract return category name given $cat_id
772                @param $cat_id
773                @result cat_name category name
774                */
775                // NOTE: This is only a temp wrapper, use id2name() to keep things matching across the board. (jengo)
776                function return_name($cat_id)
777                {
778                        return $this->id2name($cat_id);
779                }
780
781                /*!
782                @function exists
783                @abstract used for checking if a category name exists
784                @param $type subs or mains
785                @param $cat_name category name
786                @result boolean true or false
787                */
788                function exists($type,$cat_name = '',$cat_id = '')
789                {
790                        $cat_id = (int)$cat_id;
791                        $filter = $this->filter($type);
792
793                        if ($cat_name)
794                        {
795                                $cat_exists = " cat_name='" . $this->db->db_addslashes($cat_name) . "' ";
796                        }
797
798                        if ($cat_id)
799                        {
800                                $cat_exists = ' cat_parent=' . $cat_id;
801                        }
802
803                        if ($cat_name && $cat_id)
804                        {
805                                $cat_exists = " cat_name='" . $this->db->db_addslashes($cat_name) . "' AND cat_id != $cat_id ";
806                        }
807
808                        $this->db->query("SELECT COUNT(cat_id) FROM phpgw_categories WHERE $cat_exists $filter",__LINE__,__FILE__);
809
810                        $this->db->next_record();
811
812                        if ($this->db->f(0))
813                        {
814                                return True;
815                        }
816                        else
817                        {
818                                return False;
819                        }
820                }
821        }
822?>
Note: See TracBrowser for help on using the repository browser.