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

Revision 7673, 23.4 KB checked in by douglasz, 11 years ago (diff)

Ticket #3236 - Correcoes para Performance: Function Within Loop Declaration.

  • 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                @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                $cats_count = count($cats);
425                                for ($i=0;$i<$cats_count;++$i)
426                                {
427                                        if ($cats[$i]['id'] == $self)
428                                        {
429                                                unset($cats[$i]);
430                                        }
431                                }
432                        }
433
434                        if ($format == 'select')
435                        {
436                                while (is_array($cats) && list(,$cat) = each($cats))
437                                {
438                                        $s .= '<option value="' . $cat['id'] . '"';
439                                        if (in_array($cat['id'],$selected))
440                                        {
441                                                $s .= ' selected';
442                                        }
443                                        $s .= '>';
444                                        for ($j=0;$j<$cat['level'];++$j)
445                                        {
446                                                $s .= '&nbsp;';
447                                        }
448                                        $s .= $GLOBALS['phpgw']->strip_html($cat['name']);
449                                        if ($cat['app_name'] == 'phpgw')
450                                        {
451                                                $s .= '&nbsp;&lt;' . lang('Global') . '&gt;';
452                                        }
453                                        if ($cat['owner'] == '-1')
454                                        {
455                                                $s .= '&nbsp;&lt;' . lang('Global') . '&nbsp;' . lang($this->app_name) . '&gt;';
456                                        }
457                                        $s .= '</option>' . "\n";
458                                }
459                                return $s;
460                        }
461
462                        if ($format == 'list')
463                        {
464                                $space = '&nbsp;&nbsp;';
465
466                                $s  = '<table border="0" cellpadding="2" cellspacing="2">' . "\n";
467
468                                if ($this->total_records > 0)
469                                {
470                    $cats_count = count($cats);
471                                        for ($i=0;$i<$cats_count;++$i)
472                                        {
473                                                $image_set = '&nbsp;';
474
475                                                if (in_array($cats[$i]['id'],$selected))
476                                                {
477                                                        $image_set = '<img src="' . PHPGW_IMAGES_DIR . '/roter_pfeil.gif">';
478                                                }
479
480                                                if (($cats[$i]['level'] == 0) && !in_array($cats[$i]['id'],$selected))
481                                                {
482                                                        $image_set = '<img src="' . PHPGW_IMAGES_DIR . '/grauer_pfeil.gif">';
483                                                }
484
485                                                $space_set = str_repeat($space,$cats[$i]['level']);
486
487                                                $s .= '<tr>' . "\n";
488                                                $s .= '<td width="8">' . $image_set . '</td>' . "\n";
489                                                $s .= '<td>' . $space_set . '<a href="' . $GLOBALS['phpgw']->link($site_link,'cat_id=' . $cats[$i]['id']) . '">'
490                                                        . $GLOBALS['phpgw']->strip_html($cats[$i]['name'])
491                                                        . '</a></td>' . "\n"
492                                                        . '</tr>' . "\n";
493                                        }
494                                }
495                                $s .= '</table>' . "\n";
496                                return $s;
497                        }
498                }
499
500                /*!
501                @function add
502                @abstract add categories
503                @param $cat_name category name
504                @param $cat_parent category parent
505                @param $cat_description category description defaults to ''
506                @param $cat_data category data defaults to ''
507                */
508                function add($values)
509                {
510                        $values['id']           = (int)$values['id'];
511                        $values['parent']       = (int)$values['parent'];
512
513                        if ($values['parent'] > 0)
514                        {
515                                $values['level'] = $this->id2name($values['parent'],'level')+1;
516                                $values['main'] = $this->id2name($values['parent'],'main');
517                        }
518
519                        $values['descr'] = $this->db->db_addslashes($values['descr']);
520                        $values['name'] = $this->db->db_addslashes($values['name']);
521
522                        if ($values['id'] > 0)
523                        {
524                                $id_col = 'cat_id,';
525                                $id_val = $values['id'] . ',';
526                        }
527                        $this->db->query('INSERT INTO phpgw_categories (' . $id_col . 'cat_parent,cat_owner,cat_access,cat_appname,cat_name,cat_description,cat_data,'
528                                . 'cat_main,cat_level, last_mod) VALUES (' . $id_val . (int)$values['parent'] . ',' . ($values['group']!= 0 ? $values['group'] : $this->account_id) . ",'" . $values['access']
529                                . "','" . $this->app_name . "','" . $values['name'] . "','" . $values['descr'] . "','" . $values['data']
530                                . "'," . (int)$values['main'] . ',' . (int)$values['level'] . ',' . time() . ')',__LINE__,__FILE__);
531
532                        if ($values['id'] > 0)
533                        {
534                                $max = $values['id'];
535                        }
536                        else
537                        {
538                                $max = $this->db->get_last_insert_id('phpgw_categories','cat_id');
539                        }
540
541                        $max = (int)$max;
542                        if ($values['parent'] == 0)
543                        {
544                                $this->db->query('UPDATE phpgw_categories SET cat_main=' . $max . ' WHERE cat_id=' . $max,__LINE__,__FILE__);
545                        }
546                        return $max;
547                }
548
549                /*!
550                @function delete
551                @abstract delete category
552                @param $cat_id int - category id
553                */
554                /*function delete($cat_id,$subs = False)
555                {
556                        $cat_id = (int)$cat_id;
557                        if ($subs)
558                        {
559                                $subdelete = ' OR cat_parent=' . $cat_id . ' OR cat_main=' . $cat_id;
560                        }
561
562                        $this->db->query('DELETE FROM phpgw_categories WHERE cat_id=' . $cat_id . $subdelete . " AND cat_appname='"
563                                                        . $this->app_name . "'",__LINE__,__FILE__);
564                } */
565
566                function delete($cat_id, $drop_subs = False, $modify_subs = False)
567                {
568                        $cat_id = (int)$cat_id;
569                        if ($drop_subs)
570                        {
571                                $subdelete = ' OR cat_parent=' . $cat_id . ' OR cat_main=' . $cat_id;
572                        }
573
574                        if ($modify_subs)
575                        {
576                                $cats = $this->return_sorted_array('',False,'','','',False, $cat_id);
577
578                                $new_parent = $this->id2name($cat_id,'parent');
579
580                $cats_count = count($cats);
581                                for ($i=0;$i<$cats_count;++$i)
582                                {
583                                        if ($cats[$i]['level'] == 1)
584                                        {
585                                                $this->db->query('UPDATE phpgw_categories set cat_level=0, cat_parent=0, cat_main=' . (int)$cats[$i]['id']
586                                                        . ' WHERE cat_id=' . (int)$cats[$i]['id'] . " AND cat_appname='" . $this->app_name . "'",__LINE__,__FILE__);
587                                                $new_main = $cats[$i]['id'];
588                                        }
589                                        else
590                                        {
591                                                if ($new_main)
592                                                {
593                                                        $update_main = ',cat_main=' . $new_main;
594                                                }
595
596                                                if ($cats[$i]['parent'] == $cat_id)
597                                                {
598                                                        $update_parent = ',cat_parent=' . $new_parent;
599                                                }
600
601                                                $this->db->query('UPDATE phpgw_categories set cat_level=' . ($cats[$i]['level']-1) . $update_main . $update_parent
602                                                        . ' WHERE cat_id=' . (int)$cats[$i]['id'] . " AND cat_appname='" . $this->app_name . "'",__LINE__,__FILE__);
603                                        }
604                                }
605                        }
606
607                        $this->db->query('DELETE FROM phpgw_categories WHERE cat_id=' . $cat_id . $subdelete . " AND cat_appname='"
608                                . $this->app_name . "'",__LINE__,__FILE__);
609                }
610
611                /*!
612                @function edit
613                @abstract edit a category
614                @param $cat_id int - category id
615                @param $cat_parent category parent
616                @param $cat_description category description defaults to ''
617                @param $cat_data category data defaults to ''
618                */
619                function edit($values)
620                {
621                        $values['id']     = (int)$values['id'];
622                        $values['parent'] = (int)$values['parent'];
623                        $values['owner'] = (int)$values['group'];
624                        if($values['owner']){
625                                $owner = "cat_owner = ".$values['owner'].",";
626                        }
627                        if (isset($values['old_parent']) && (int)$values['old_parent'] != $values['parent'])
628                        {
629                                $this->delete($values['id'],False,True);
630                                return $this->add($values);
631                        }
632                        else
633                        {
634                                if ($values['parent'] > 0)
635                                {
636                                        $values['main']  = (int)$this->id2name($values['parent'],'main');
637                                        $values['level'] = (int)$this->id2name($values['parent'],'level') + 1;
638                                }
639                                else
640                                {
641                                        $values['main']  = $values['id'];
642                                        $values['level'] = 0;
643                                }
644                        }
645
646                        $values['descr'] = $this->db->db_addslashes($values['descr']);
647                        $values['name'] = $this->db->db_addslashes($values['name']);
648
649                        $sql = "UPDATE phpgw_categories SET $owner cat_name='" . $values['name'] . "', cat_description='" . $values['descr']
650                                . "', cat_data='" . $values['data'] . "', cat_parent=" . $values['parent'] . ", cat_access='"
651                                . $values['access'] . "', cat_main=" . $values['main'] . ', cat_level=' . $values['level'] . ',last_mod=' . time()
652                                . " WHERE cat_appname='" . $this->app_name . "' AND cat_id=" . $values['id'];
653
654                        $this->db->query($sql,__LINE__,__FILE__);
655                        return $values['id'];
656                }
657
658                function name2id($cat_name)
659                {
660                        $this->db->query("SELECT cat_id FROM phpgw_categories WHERE cat_name='" . $this->db->db_addslashes($cat_name) . "' "
661                                ."AND cat_appname='" . $this->app_name . "' AND (cat_owner=" . $this->account_id . ' OR cat_owner=-1)',__LINE__,__FILE__);
662
663                        if(!$this->db->num_rows())
664                        {
665                                return 0;
666                        }
667
668                        $this->db->next_record();
669
670                        return $this->db->f('cat_id');
671                }
672
673                function id2name($cat_id = '', $item = 'name')
674                {
675                        $cat_id = (int)$cat_id;
676                        if($cat_id == 0)
677                        {
678                                return '--';
679                        }
680                        switch($item)
681                        {
682                                case 'owner':   $value = 'cat_owner'; break;
683                                case 'main':    $value = 'cat_main'; break;
684                                case 'level':   $value = 'cat_level'; break;
685                                case 'parent':  $value = 'cat_parent'; break;
686                                case 'name':    $value = 'cat_name'; break;
687                                default:                $value = 'cat_parent'; break;
688                        }
689
690                        $this->db->query("SELECT $value FROM phpgw_categories WHERE cat_id=" . $cat_id,__LINE__,__FILE__);
691                        $this->db->next_record();
692
693                        if ($this->db->f($value))
694                        {
695                                return $this->db->f($value);
696                        }
697                        else
698                        {
699                                if ($item == 'name')
700                                {
701                                        return '--';
702                                }
703                        }
704                }
705
706                /*!
707                @function return_name
708                @abstract return category name given $cat_id
709                @param $cat_id
710                @result cat_name category name
711                */
712                // NOTE: This is only a temp wrapper, use id2name() to keep things matching across the board. (jengo)
713                function return_name($cat_id)
714                {
715                        return $this->id2name($cat_id);
716                }
717
718                /*!
719                @function exists
720                @abstract used for checking if a category name exists
721                @param $type subs or mains
722                @param $cat_name category name
723                @result boolean true or false
724                */
725                function exists($type,$cat_name = '',$cat_id = '')
726                {
727                        $cat_id = (int)$cat_id;
728                        $filter = $this->filter($type);
729
730                        if ($cat_name)
731                        {
732                                $cat_exists = " cat_name='" . $this->db->db_addslashes($cat_name) . "' ";
733                        }
734
735                        if ($cat_id)
736                        {
737                                $cat_exists = ' cat_parent=' . $cat_id;
738                        }
739
740                        if ($cat_name && $cat_id)
741                        {
742                                $cat_exists = " cat_name='" . $this->db->db_addslashes($cat_name) . "' AND cat_id != $cat_id ";
743                        }
744
745                        $this->db->query("SELECT COUNT(cat_id) FROM phpgw_categories WHERE $cat_exists $filter",__LINE__,__FILE__);
746
747                        $this->db->next_record();
748
749                        if ($this->db->f(0))
750                        {
751                                return True;
752                        }
753                        else
754                        {
755                                return False;
756                        }
757                }
758        }
759?>
Note: See TracBrowser for help on using the repository browser.