source: trunk/contactcenter/inc/class.bo_contactcenter.inc.php @ 498

Revision 498, 16.9 KB checked in by niltonneto, 16 years ago (diff)

Implementação que verifica se algum usuário compartilhou contatos com usuário logado. O ramo "compartilhado"
só irá aparecer se existir tal compartilhamento.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
RevLine 
[2]1<?php
2  /***************************************************************************\
3  * eGroupWare - Contacts Center                                              *
4  * http://www.egroupware.org                                                 *
5  * Written by:                                                               *
6  *  - Raphael Derosso Pereira <raphaelpereira@users.sourceforge.net>         *
[285]7  *  sponsored by Thyamad - http://www.thyamad.com
[2]8  * ------------------------------------------------------------------------- *
9  *  This program is free software; you can redistribute it and/or modify it  *
10  *  under the terms of the GNU General Public License as published by the    *
11  *  Free Software Foundation; either version 2 of the License, or (at your   *
12  *  option) any later version.                                               *
13  \***************************************************************************/
14
15        class bo_contactcenter
16        {
17
18                /*!
19                        This var holds the actual catalog level.
20                */
21                var $catalog_level;
22
23                /*!
24                        This holds the instantiated catalog class;
25                */
26                var $catalog;
[285]27               
[2]28                /*!
29                        The Security Manager
30                */
31                var $security;
[285]32               
[2]33
34                function bo_contactcenter($catalog=false)
35                {
36                        $this->tree = $GLOBALS['phpgw']->session->appsession('bo_contactcenter.tree','contactcenter');
37                        $this->catalog_level = $GLOBALS['phpgw']->session->appsession('bo_contactcenter.catalog_level','contactcenter');
38                        $this->security = CreateObject('contactcenter.bo_security_manager');
[285]39                       
[2]40                        if ($catalog)
41                        {
42                                $this->set_catalog($catalog);
43                        }
44                        else
45                        {
46                                if ($this->catalog_level[0])
47                                {
48                                        $this->set_catalog($this->get_branch_by_level($this->catalog_level[0]));
49                                }
50                                else
51                                {
52                                        $this->catalog_level = array('0.0');
53                                        $this->get_catalog_tree();
54                                        $this->set_catalog($this->catalog_level[0]);
55                                }
56                        }
57                }
[285]58               
[284]59                /*
60                 *
61                 * @function is_external
62                 * @author Mário César Kolling <mario.kolling@serpro.gov.br>
63                 * @abstract Verify if a catalog is external
64                 * @param (mixed) an catalog array or a catalog tree level, a string in the form 0.sublevel.subsublevel
65                 * @return (boolean) true if it is an external catalog false otherwise
66                 *
67                 */
68
69                function is_external($catalog)
70                {
71                        if (is_array($catalog))
72                        {
73                                $level = $this->get_level_by_branch($catalog, $this->tree['branches']);
74                        }
75                        else
76                        {
77                                $level = $catalog;
78                        }
79
80                        $lvl_vector = explode('.', $level);
81                        $id = $lvl_vector[1];
82
83                        if ($this->tree['branches'][$id]['external'])
84                        {
85                                return true;
86                        }
87
88                        return false;
89
90                }
91
92                /*
93                 function get_letters_filter($catalog)
94                {
95
96                }
97
98                function get_numbers_filter($catalog)
99                {
100
101                }
102
103                function get_search_filter($catalog)
104                {
105
106                }
107                */
108
[2]109                /*!
[285]110               
[2]111                        @function find
112                        @abstract Performs a search in the DB based on the parameters
113                        @author Raphael Derosso Pereira (algorithm and code)
114                        @author Vinicius Cubas Brand (algorithm)
[285]115                       
[2]116                        @param array $what The list of fields to be returned. The format is:
117                                $what = array(
118                                        'contact.company.company_name',
119                                        'contact.names_ordered'
120                                );
[285]121                       
[2]122                        @param array $rules The restrictions.
[285]123                       
[2]124                        The restrictions format is quite complicated, but is very complete.
125                        As defined here, there is the possibility to do almost any type of
126                        search (tell me if you can't do any). For example, imagine the
127                        following search:
[285]128                                       
[2]129                                                and(a,or(d,e,and(f,g)))
[285]130                       
[2]131                        That is represented by the folloowing tree:
[285]132                               
[2]133                                   and
134                                    |
135                  .--------------------.
136                  |                    |
137                a = 5                 or
138                                       |
139                          .---------.------------.
140                          |         |            |
141                       d != 10  e LIKE %a       and
142                                                 |
143                                             .-------.
144                                             |       |
145                                           f = 5   g < 10
146
147
148                        The rules that should be passed to the find function for this tree
149                        is:
[285]150                               
[2]151                                $rules = array(
152                                        0 => array(
153                                                'field' => 'A',
154                                                'type'  => '=',
155                                                'value' => 5
156                                        ),
157                                        1 => array (
158                                                'type'       => 'branch',
159                                                'value'      => 'OR',
160                                                'sub_branch' => array(
161                                                        0 => array(
162                                                                'field' => 'D'
163                                                                'type'  => '!=',
164                                                                'value' => 10
165                                                        ),
166                                                        1 => array(
167                                                                'field' => 'E',
168                                                                'type'  => 'LIKE',
169                                                                'value' => '%a'
170                                                        )
171                                                        2 => array(
172                                                                'type'       => 'branch',
173                                                                'value'      => 'AND',
174                                                                'sub_branch' => array(
175                                                                        0 => array(
176                                                                                'field' => 'F',
177                                                                                'type'  => '=',
178                                                                                'value' => 5
179                                                                        ),
180                                                                        1 => array(
181                                                                                'field' => 'G'
182                                                                                'type'  => '<',
183                                                                                'value' => 10
184                                                                        )
185                                                                )
186                                                        )
187                                                )
188                                        )
189                                );
190
[285]191 
192                        The restriction type can be: =, !=, <=, <, >, >=, NULL, IN, LIKE,
[2]193                        NOT NULL, NOT IN, NOT LIKE
194                        Value of branch can be AND, OR, NOT
[285]195                       
196                        @param array $other Other parameter to the search
[2]197                                $other = array(
198                                        'offset'          => (int),
199                                        'limit'           => (int),
200                                        'sort'            => {ASC|DESC},
201                                        'order'           => (string with field names separated by commas)
202                                        'fields_modifier' => (COUNT|MAX)
203                                );
204
205                        @return array $array[<field_name>][<row_number>]
[285]206                               
[2]207                */
208                function find($what, $rules=false, $other=false)
209                {
210                        return $this->catalog->find($what, $rules, $other);
211                }
[285]212               
[2]213                /*!
[285]214               
[2]215                        @function get_catalog_tree
216                        @abstract Returns an array describing the available
217                                catalog-entity-view tree and their respective
218                                values and types
219                        @author Raphael Derosso Pereira
[284]220                        @author Mário César Kolling (external catalogs and optimizations)
[2]221
222                        @param (string)  $level The level to be taken
223                        @param (boolean) $recursive Retrive the whole tree from
224                                the level specified until the leaves
[285]225                       
[2]226                        @return The format of the return is:
227                                $return = array(
228                                        0  => array(
229                                                'name'       => '<branch_name>',
230                                                'type'       => '<branch_type>',
231                                                'class'      => '<branch_class>',
232                                                'class_args' => '<branch_class_args>',
233                                                'find_args'  => '<branch_find_args>',
234                                                'sub_branch' => array(
235                                                        0  => array(
236                                                                'name'       => '<branch_name>',
237                                                                'type'       => '<branch_type>',
238                                                                'class'      => '<branch_class>',
239                                                                'class_args' => '<branch_class_args>',
240                                                                'find_args'  => '<branch_find_args>',
241                                                                'sub_branch' => array(...)
242                                                        ),
243                                                        1  => array(...),...
244                                                ),
245                                        ),
246                                        1  => array(...),...
247                                );
[285]248                               
[2]249                                <branch_type> can be 'catalog_group', 'catalog' or 'view';
[285]250                                <branch_class> is the name of the class that is capable of
[2]251                                        handling the information for this catalog/view
252                                <branch_class_args> is an array that holds the arguments to
253                                        be passed to <branch_class> when it is instantiated
254                                <brach_find_args> is the string that should precede the search
255                                        string
[285]256                                               
257                                If the branch is actually a leaf, than 'sub_branch' is false;
258               
259               
[2]260                        TODO: This method is hard-coded, but it should grab the tree
261                        from the DB using the View Manager...
262                */
263                function get_catalog_tree($level = '0', $recursive = false)
264                {
265                        if ($this->tree)
266                        {
267                                if ($level === '0')
268                                {
269                                        return $this->tree['branches'];
270                                }
[285]271                               
[284]272                                $lvl_vector = explode('.', $level);
273                                $id = $lvl_vector[1];
274
[2]275                                $branch =& $this->get_branch_by_level($level);
276                                $info = $this->get_info_by_level($level);
[285]277                               
[2]278                                if ($branch['type'] === 'unknown')
279                                {
280                                        if ($info['type'] === 'ldap')
281                                        {
282                                                $ldap = CreateObject('contactcenter.bo_ldap_manager');
[284]283                                                if ($this->tree['branches'][$id]['external'])
284                                                {
285                                                        // if it's an external catalog
286                                                        $new_branch = $ldap->get_external_ldap_tree($info['src'], $branch['value'], $recursive);
287                                                }
288                                                else
289                                                {
290                                                        // if it's not an external catalog
291                                                        $new_branch = $ldap->get_ldap_tree($info['src'], $branch['value'], $recursive);
292                                                }
[2]293                                                if ($new_branch)
294                                                {
[284]295                                                        if (!empty($new_branch['timeout']) && !empty($new_branch['msg']))
296                                                        {
297                                                                return $new_branch;
298                                                        }
299
300                                                        // Necessary for the new way the catalog tree is built at initialization
301                                                        $new_branch['name'] = $branch['name'];
302                                                        $new_branch['external'] = $branch['external'];
[2]303                                                        $branch = $new_branch;
304                                                }
305                                                else
306                                                {
307                                                        return false;
308                                                }
309                                        }
310                                        $GLOBALS['phpgw']->session->appsession('bo_contactcenter.tree','contactcenter',$this->tree);
[285]311                                       
[2]312                                        return $branch;
313                                }
314                                else if ($branch['type'])
315                                {
316                                        return $branch;
317                                }
[285]318                               
[2]319                                return false;
320                        }
[285]321                       
[2]322                        if ($level !== '0')
323                        {
324                                return false;
325                        }
326                        $this->tree = array(
327                                0 => array(
328                                        'type' => 'sql'
329                                ),
[498]330                                1 => array(
331                                        'type' => 'sql'
332                                ),
[2]333                                'branches' => array(
334                                        0  => array(
335                                                'name'       => lang('People'),
336                                                'type'       => 'catalog',
337                                                'class'      => 'bo_people_catalog',
338                                                'icon'       => 'people-mini.png',
339                                                'sub_branch' => false
[285]340                                        ),
[2]341                                        1  => array(
342                                                'name'       => lang('Groups'),
343                                                'type'       => 'catalog',
344                                                'class'      => 'bo_group_manager',
345                                                'icon'       => 'people-mini.png',
[284]346                                                'sub_branch' => False
[2]347                                        )
348                                )
349                        );
[498]350                        if($_SESSION['phpgw_info']['user']['preferences']['contactcenter']['shared_contacts']){
351                                $this->tree[2] = array('type' => 'sql');
352                                $this->tree['branches'][2] = array('name' => lang('Shared'),
353                                                                                'type'       => 'catalog',
354                                                                                'class'      => 'bo_shared_catalog_manager',
355                                                                                'icon'       => 'people-mini.png',
356                                                                                'sub_branch' => false);
357                                unset($_SESSION['phpgw_info']['user']['preferences']['contactcenter']['shared_contacts']);
358                        }
[2]359                        $ldap = CreateObject('contactcenter.bo_ldap_manager');
360                        $ldap_srcs = $ldap->get_all_ldap_sources();
[285]361                       
[2]362                        if ($ldap_srcs)
363                        {
[498]364                                $i = count($this->tree['branches']); //os Indices dos tipos tem o mesmo numero do de ramos.
[2]365                                reset($ldap_srcs);
366                                while (list($id,) = each($ldap_srcs))
367                                {
368                                        if (($tree = $ldap->get_ldap_tree($id, $ldap_srcs[$id]['dn'], $recursive)))
369                                        {
[284]370                                                // It isn't used anymore, but does no harm!
371                                                // It may be usefull later for use with search timeouts, or another ldap error
372                                                if (array_key_exists('error_msg', $tree))
373                                                {
374                                                        if (isset($this->tree['branches']['msg']))
375                                                        {
376                                                                $this->tree['branches']['msg'] .= "\n" . lang('Catalog %1 not showed due to error: ' .
377                                                                                                         $tree['error_msg'], $ldap_srcs[$id]['name']);
378                                                        }
379                                                        else
380                                                        {
381                                                                $this->tree['branches']['msg'] = lang('Catalog %1 not showed due to error: ' .
382                                                                                                         $tree['error_msg'], $ldap_srcs[$id]['name']);
383                                                        }
384                                                }
385                                                else
386                                                {
387                                                        $tree['name'] = $ldap_srcs[$id]['name'];
388                                                        $tree['external'] = false;
389                                                        array_push($this->tree['branches'], $tree);
390                                                        $this->tree[$i]['type'] = 'ldap';
391                                                        $this->tree[$i]['src'] = $id;
392                                                }
393
394                                        }
395                                        $i++;
396                                }
397                        }
398
399                        // external LDAP sources
400                        $ldap_srcs = $ldap->get_external_ldap_sources();
401
402                        if ($ldap_srcs)
403                        {
[498]404                                $i = count($this->tree['branches']);//os Indices dos tipos tem o mesmo numero do de ramos.
[284]405
406                                reset($ldap_srcs);
407
408                                while (list($id,) = each($ldap_srcs))
409                                {
410                                        // External catalogs are now identified as type unknown during initialization. An optimization change.
411
[2]412                                                $tree['name'] = $ldap_srcs[$id]['name'];
[284]413                                                $tree['type'] = 'unknown';
414                                                $tree['value'] = $ldap_srcs[$id]['dn'];
415                                                $tree['external'] = true;
[2]416                                                array_push($this->tree['branches'], $tree);
417                                                $this->tree[$i]['type'] = 'ldap';
418                                                $this->tree[$i]['src'] = $id;
419                                        $i++;
420                                }
421                        }
[285]422                       
[2]423                        $GLOBALS['phpgw']->session->appsession('bo_contactcenter.tree','contactcenter',$this->tree);
424                        return $this->tree['branches'];
425                }
426
427                /*!
[285]428                       
[2]429                        @function get_branch_by_level
430                        @abstract Returns the branch and it's informations given the level
431                        @author Raphael Derosso Pereira
[285]432                       
[2]433                        @param (string) $level The level to be used
[285]434                       
[2]435                */
436                function & get_branch_by_level($level)
437                {
438                        $path = @explode('.',$level);
439                        $n_ways = count($path);
[285]440                       
[2]441                        if ($n_ways <= 1)
442                        {
443                                return false;
444                        }
[285]445                       
[2]446                        $code = '$branch =& $this->tree[\'branches\']';
447                        for ($i = 1; $i < $n_ways-1; $i++)
448                        {
449                                $code .= '['.$path[$i].'][\'sub_branch\']';
450                        }
451                        $code .= '['.$path[$i].'];';
452
453                        //echo 'Codigo: '.$code.'<br>';
454                        eval($code);
[285]455                       
[2]456                        return $branch;
457                }
458
459                /*!
[285]460                 
[2]461                 @function get_info_by_level
462                 @abstract Returns the information about the catalog, given the level
463                 @author Raphael Derosso Pereira
464
465                 @param (string) $level The catalog level
466
467                */
468                function get_info_by_level($level)
469                {
470                        $path = @explode('.',$level);
471                        $n_ways = count($path);
[285]472                       
[2]473                        if ($n_ways <= 1)
474                        {
475                                return false;
476                        }
[285]477                       
[2]478                        $info = $this->tree[$path[1]];
[285]479                       
[2]480                        return $info;
481                }
482
483                /*!
484
485                        @function get_level_by_branch
486                        @abstract Returns the level of the given branch
487                        @author Raphael Derosso Pereira
488
489                        @param (array) $catalog The catalog
490                        @param (array) $branch  The reference to the branch to be searched
491
492                */
493                function get_level_by_branch($catalog, &$branch, $branch_level = '0')
494                {
495//                      echo '<br>';
496                        reset($branch);
497                        while(list($level, $bcatalog) = each($branch))
498                        {
499//                              echo 'Parent Level:    '.$branch_level.'<br>';
500//                              echo 'This node Level: '.$level.'<br>';
501//                              echo 'Catalog:         '.$bcatalog['name'].'<br>';
502
503                                $found = true;
504                                foreach ($catalog as $property => $value)
505                                {
506                                        if ($property !== 'sub_branch' and $bcatalog[$property] !== $value)
507                                        {
508//                                              echo 'Property <b>'.$property.'</b> differs.<br>';
509//                                              echo 'Expected: '.$value.'<br>';
510//                                              echo 'Found: '.$bcatalog[$property].'<br>';
511                                                $found = false;
512                                        }
513
514                                        if (!$found)
515                                        {
516                                                break;
517                                        }
518                                }
519
520                                if ($found)
521                                {
522//                                      echo '<b>FOUND</b><br>';
523                                        return $branch_level.'.'.((string) $level);
524                                }
525                                else if ($bcatalog['sub_branch'])
526                                {
527//                                      echo 'Not Found<br>';
528
529                                        $search = $this->get_level_by_branch($catalog, $bcatalog['sub_branch'], (string) $level);
[285]530                                       
[2]531                                        if ($search !== false)
532                                        {
533//                                              echo 'Returning level: '.$branch_level.'.'.$search.'<br>';
534//                                              echo 'Sholud it be '.$branch_level.'.'.$nlevel.' ?<br>';
535//                                              echo 'Or '.$branch_level.'.'.((string)$search).' ?<br>';
536                                                return $branch_level.'.'.$search;
537                                        }
538                                }
539                        }
540
541//                      echo 'Not Found in this Branch<br>';
542                        return false;
543                }
[285]544               
[2]545                /*!
[285]546               
[2]547                        @function get_actual_catalog
[285]548                        @abstract Returns the information about the Catalog that is
[2]549                                instantiated
550
551                        @author Raphael Derosso Pereira
[285]552               
[2]553                */
554                function get_actual_catalog()
555                {
556                        $catalog = $this->get_branch_by_level($this->catalog_level[0]);
557                        return $catalog;
558                }
[285]559               
[2]560                /*!
[285]561               
[2]562                        @function set_catalog
563                        @abstract Sets the actual catalog
564                        @author Raphael Derosso Pereira
[285]565                       
[2]566                        @param array $to_catalog The catalog in the format returned by
567                                get_available_tree or the level
[285]568               
[2]569                */
570                function set_catalog(& $to_catalog )
571                {
572                        if(!is_array($to_catalog))
573                        {
574                                if (is_string($to_catalog))
575                                {
576                                        if (!($t =& $this->get_branch_by_level($to_catalog)))
577                                        {
578                                                return false;
579                                        }
580                                        $level = $to_catalog;
581                                        $catalog =& $t;
582                                }
583                                else
584                                {
585                                        return false;
586                                }
587                        }
588                        else
589                        {
590                                $catalog =& $to_catalog;
591                                $level = $this->get_level_by_branch($to_catalog, $this->tree['branches']);
592                        }
[284]593                        $lvl_vector = explode('.', $level);
594                        $id = $lvl_vector[1];
[285]595                       
[2]596                        switch($catalog['type'])
597                        {
598                                case 'unknown':
599                                        $level = $this->get_level_by_branch($catalog, $this->tree['branches']);
600                                        $catalog =& $this->get_catalog_tree($level);
601
602                                case 'catalog':
603                                case 'catalog_group':
604                                case 'mixed_catalog_group':
605                                        $this->catalog_level = array($level);
606                                        $GLOBALS['phpgw']->session->appsession('bo_contactcenter.catalog_level','contactcenter', $this->catalog_level);
607                                        $call = '$this->catalog = CreateObject('.'\'contactcenter.'.$catalog['class'].'\'';
608                                        if ($catalog['class_args'])
609                                        {
610                                                foreach($catalog['class_args'] as $arg)
611                                                {
612                                                        $args[] = is_string($arg) ? ($arg{0} != '$' ? "'".$arg."'" : $arg) : $arg;
613                                                        //$args[] = is_string($arg) ? "'".$arg."'" : $arg;
614                                                }
615                                                $call .= ','.implode(',',$args);
616                                        }
[285]617                                       
[2]618                                        $call .= ');';
[285]619                                       
[2]620//                                      print_r($catalog);
621//                                      echo '<br><br><b>Setando Catalogo '.$catalog['name'].': </b>'.$call.'<br>';
622
623                                        eval($call);
[285]624                                                               
[2]625                                        return $catalog;
[285]626                                       
[2]627                                default: return false;
628                        }
629                }
[285]630               
[2]631
632
633                /*********************************************************************\
634                 *                Methods to set general fields                      *
635                \*********************************************************************/
[285]636               
[2]637                /*!
[285]638               
[2]639                        @function add_vcard
640                        @abstract Insert a VCard to the squema
641                        @author Raphael Derosso Pereira
642                        @param string $uploaded_file The path to the file that were
643                                uploaded.
[285]644               
[2]645                */
646                function add_vcard ( $uploaded_file )
647                {
648                }
649
650
[285]651               
[2]652                /*********************************************************************\
653                 *                Methods to get general data                        *
654                \*********************************************************************/
[285]655               
[2]656        }
657?>
Note: See TracBrowser for help on using the repository browser.