source: branches/2.2/contactcenter/inc/class.bo_contactcenter.inc.php @ 3282

Revision 3282, 17.5 KB checked in by eduardoalex, 14 years ago (diff)

Ticket #1251 - Commit principal com adição da melhoria no módulo

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2  /***************************************************************************\
3  * eGroupWare - Contacts Center                                              *
4  * http://www.egroupware.org                                                 *
5  * Written by:                                                               *
6  *  - Raphael Derosso Pereira <raphaelpereira@users.sourceforge.net>         *
7  *  sponsored by Thyamad - http://www.thyamad.com
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;
27               
28                /*!
29                        The Security Manager
30                */
31                var $security;
32               
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');
39                       
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                }
58               
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
109                /*!
110               
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)
115                       
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                                );
121                       
122                        @param array $rules The restrictions.
123                       
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:
128                                       
129                                                and(a,or(d,e,and(f,g)))
130                       
131                        That is represented by the folloowing tree:
132                               
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:
150                               
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
191 
192                        The restriction type can be: =, !=, <=, <, >, >=, NULL, IN, LIKE,
193                        NOT NULL, NOT IN, NOT LIKE
194                        Value of branch can be AND, OR, NOT
195                       
196                        @param array $other Other parameter to the search
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>]
206                               
207                */
208                function find($what, $rules=false, $other=false, $area=false, $recursive=false)
209                {
210                        return $this->catalog->find($what, $rules, $other, $area, $recursive);
211                }
212               
213                /*!
214               
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
220                        @author Mário César Kolling (external catalogs and optimizations)
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
225                       
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                                );
248                               
249                                <branch_type> can be 'catalog_group', 'catalog' or 'view';
250                                <branch_class> is the name of the class that is capable of
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
256                                               
257                                If the branch is actually a leaf, than 'sub_branch' is false;
258               
259               
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                                }
271                               
272                                $lvl_vector = explode('.', $level);
273                                $id = $lvl_vector[1];
274
275                                $branch =& $this->get_branch_by_level($level);
276                                $info = $this->get_info_by_level($level);
277                               
278                                if ($branch['type'] === 'unknown')
279                                {
280                                        if ($info['type'] === 'ldap')
281                                        {
282                                                $ldap = CreateObject('contactcenter.bo_ldap_manager');
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                                                }
293                                                if ($new_branch)
294                                                {
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'] = $this->tree['branches'][$id]['external'];
303                                                        $branch = $new_branch;
304                                                }
305                                                else
306                                                {
307                                                        return false;
308                                                }
309                                        }
310                                        $GLOBALS['phpgw']->session->appsession('bo_contactcenter.tree','contactcenter',$this->tree);
311                                       
312                                        return $branch;
313                                }
314                                else if ($branch['type'])
315                                {
316                                        return $branch;
317                                }
318                               
319                                return false;
320                        }
321                       
322                        if ($level !== '0')
323                        {
324                                return false;
325                        }
326                        $this->tree = array(
327                                0 => array(
328                                        'type' => 'sql'
329                                ),
330                                1 => array(
331                                        'type' => 'sql'
332                                ),
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
340                                        ),
341                                        1  => array(
342                                                'name'       => lang('Groups'),
343                                                'type'       => 'catalog',
344                                                'class'      => 'bo_group_manager',
345                                                'icon'       => 'people-mini.png',
346                                                'sub_branch' => False
347                                        )
348                                )
349                        );
350                        if($_SESSION['phpgw_info']['user']['preferences']['contactcenter']['shared_contacts']){
351                                $this->tree[2] = array('type' => 'sql');
352                                $this->tree['branches'][2] = array(
353                                                                                'name' => lang('Shared'),
354                                                                                'type'       => 'mixed_catalog_group',
355                                                                                'class'      => 'bo_shared_people_manager',
356                                                                                'icon'       => 'people-mini.png',
357                                                                                'sub_branch' => array(
358                                                                                        0 =>    array( 
359                                                                                                'name'  => lang('People'),
360                                                                                                'type'  => 'catalog',
361                                                                                                'class' => 'bo_shared_people_manager',
362                                                                                                'icon'  => 'people-mini.png',
363                                                                                                'sub_branch' => False
364                                                                                        ),
365                                                                                        1 =>    array( 
366                                                                                                'name'  => lang('Groups'),
367                                                                                                'type'  => 'catalog',
368                                                                                                'class' => 'bo_shared_group_manager',
369                                                                                                'icon'  => 'people-mini.png',
370                                                                                                'sub_branch' => False
371                                                                                        )
372                                                                                )
373                                                        );
374                                unset($_SESSION['phpgw_info']['user']['preferences']['contactcenter']['shared_contacts']);
375                        }
376                        $ldap = CreateObject('contactcenter.bo_ldap_manager');
377                        $ldap_srcs = $ldap->get_all_ldap_sources();
378                       
379                        if ($ldap_srcs)
380                        {
381                                $i = count($this->tree['branches']); //os Indices dos tipos tem o mesmo numero do de ramos.
382                                reset($ldap_srcs);
383                                while (list($id,) = each($ldap_srcs))
384                                {
385                                        if (($tree = $ldap->get_ldap_tree($id, $ldap_srcs[$id]['dn'], $recursive)))
386                                        {
387                                                // It isn't used anymore, but does no harm!
388                                                // It may be usefull later for use with search timeouts, or another ldap error
389                                                if (array_key_exists('error_msg', $tree))
390                                                {
391                                                        if (isset($this->tree['branches']['msg']))
392                                                        {
393                                                                $this->tree['branches']['msg'] .= "\n" . lang('Catalog %1 not showed due to error: ' .
394                                                                                                         $tree['error_msg'], $ldap_srcs[$id]['name']);
395                                                        }
396                                                        else
397                                                        {
398                                                                $this->tree['branches']['msg'] = lang('Catalog %1 not showed due to error: ' .
399                                                                                                         $tree['error_msg'], $ldap_srcs[$id]['name']);
400                                                        }
401                                                }
402                                                else
403                                                {
404                                                        $tree['name'] = $ldap_srcs[$id]['name'];
405                                                        $tree['external'] = false;
406                                                        array_push($this->tree['branches'], $tree);
407                                                        $this->tree[$i]['type'] = 'ldap';
408                                                        $this->tree[$i]['src'] = $id;
409                                                }
410
411                                        }
412                                        $i++;
413                                }
414                        }
415
416                        // external LDAP sources
417                        $ldap_srcs = $ldap->get_external_ldap_sources();
418
419                        if ($ldap_srcs)
420                        {
421                                $i = count($this->tree['branches']);//os Indices dos tipos tem o mesmo numero do de ramos.
422
423                                reset($ldap_srcs);
424
425                                while (list($id,) = each($ldap_srcs))
426                                {
427                                        // External catalogs are now identified as type unknown during initialization. An optimization change.
428
429                                                $tree['name'] = $ldap_srcs[$id]['name'];
430                                                $tree['type'] = 'unknown';
431                                                $tree['value'] = $ldap_srcs[$id]['dn'];
432                                                $tree['external'] = true;
433                                                array_push($this->tree['branches'], $tree);
434                                                $this->tree[$i]['type'] = 'ldap';
435                                                $this->tree[$i]['src'] = $id;
436                                        $i++;
437                                }
438                        }       
439                        $GLOBALS['phpgw']->session->appsession('bo_contactcenter.tree','contactcenter',$this->tree);
440                        return $this->tree['branches'];
441                }
442
443                /*!
444                       
445                        @function get_branch_by_level
446                        @abstract Returns the branch and it's informations given the level
447                        @author Raphael Derosso Pereira
448                       
449                        @param (string) $level The level to be used
450                       
451                */
452                function & get_branch_by_level($level)
453                {
454                        $path = @explode('.',$level);
455                        $n_ways = count($path);
456                       
457                        if ($n_ways <= 1)
458                        {
459                                return false;
460                        }
461                       
462                        $code = '$branch =& $this->tree[\'branches\']';
463                        for ($i = 1; $i < $n_ways-1; $i++)
464                        {
465                                $code .= '['.$path[$i].'][\'sub_branch\']';
466                        }
467                        $code .= '['.$path[$i].'];';
468
469                        //echo 'Codigo: '.$code.'<br>';
470                        eval($code);
471                       
472                        return $branch;
473                }
474
475                /*!
476                 
477                 @function get_info_by_level
478                 @abstract Returns the information about the catalog, given the level
479                 @author Raphael Derosso Pereira
480
481                 @param (string) $level The catalog level
482
483                */
484                function get_info_by_level($level)
485                {
486                        $path = @explode('.',$level);
487                        $n_ways = count($path);
488                       
489                        if ($n_ways <= 1)
490                        {
491                                return false;
492                        }
493                       
494                        $info = $this->tree[$path[1]];
495                       
496                        return $info;
497                }
498
499                /*!
500
501                        @function get_level_by_branch
502                        @abstract Returns the level of the given branch
503                        @author Raphael Derosso Pereira
504
505                        @param (array) $catalog The catalog
506                        @param (array) $branch  The reference to the branch to be searched
507
508                */
509                function get_level_by_branch($catalog, &$branch, $branch_level = '0')
510                {
511//                      echo '<br>';
512                        reset($branch);
513                        while(list($level, $bcatalog) = each($branch))
514                        {
515//                              echo 'Parent Level:    '.$branch_level.'<br>';
516//                              echo 'This node Level: '.$level.'<br>';
517//                              echo 'Catalog:         '.$bcatalog['name'].'<br>';
518
519                                $found = true;
520                                foreach ($catalog as $property => $value)
521                                {
522                                        if ($property !== 'sub_branch' and $bcatalog[$property] !== $value)
523                                        {
524//                                              echo 'Property <b>'.$property.'</b> differs.<br>';
525//                                              echo 'Expected: '.$value.'<br>';
526//                                              echo 'Found: '.$bcatalog[$property].'<br>';
527                                                $found = false;
528                                        }
529
530                                        if (!$found)
531                                        {
532                                                break;
533                                        }
534                                }
535
536                                if ($found)
537                                {
538//                                      echo '<b>FOUND</b><br>';
539                                        return $branch_level.'.'.((string) $level);
540                                }
541                                else if ($bcatalog['sub_branch'])
542                                {
543//                                      echo 'Not Found<br>';
544
545                                        $search = $this->get_level_by_branch($catalog, $bcatalog['sub_branch'], (string) $level);
546                                       
547                                        if ($search !== false)
548                                        {
549//                                              echo 'Returning level: '.$branch_level.'.'.$search.'<br>';
550//                                              echo 'Sholud it be '.$branch_level.'.'.$nlevel.' ?<br>';
551//                                              echo 'Or '.$branch_level.'.'.((string)$search).' ?<br>';
552                                                return $branch_level.'.'.$search;
553                                        }
554                                }
555                        }
556
557//                      echo 'Not Found in this Branch<br>';
558                        return false;
559                }
560               
561                /*!
562               
563                        @function get_actual_catalog
564                        @abstract Returns the information about the Catalog that is
565                                instantiated
566
567                        @author Raphael Derosso Pereira
568               
569                */
570                function get_actual_catalog()
571                {
572                        $catalog = $this->get_branch_by_level($this->catalog_level[0]);
573                        return $catalog;
574                }
575               
576                /*!
577               
578                        @function set_catalog
579                        @abstract Sets the actual catalog
580                        @author Raphael Derosso Pereira
581                       
582                        @param array $to_catalog The catalog in the format returned by
583                                get_available_tree or the level
584               
585                */
586                function set_catalog(& $to_catalog )
587                {
588                        if(!is_array($to_catalog))
589                        {
590                                if (is_string($to_catalog))
591                                {
592                                        if (!($t =& $this->get_branch_by_level($to_catalog)))
593                                        {
594                                                return false;
595                                        }
596                                        $level = $to_catalog;
597                                        $catalog =& $t;
598                                }
599                                else
600                                {
601                                        return false;
602                                }
603                        }
604                        else
605                        {
606                                $catalog =& $to_catalog;
607                                $level = $this->get_level_by_branch($to_catalog, $this->tree['branches']);
608                        }
609                        $lvl_vector = explode('.', $level);
610                        $id = $lvl_vector[1];
611                       
612                        switch($catalog['type'])
613                        {
614                                case 'unknown':
615                                        $level = $this->get_level_by_branch($catalog, $this->tree['branches']);
616                                        $catalog =& $this->get_catalog_tree($level);
617                               
618                                case 'catalog':
619                                case 'catalog_group':
620                                case 'mixed_catalog_group':
621                                        $this->catalog_level = array($level);
622                                        $GLOBALS['phpgw']->session->appsession('bo_contactcenter.catalog_level','contactcenter', $this->catalog_level);
623                                        $call = '$this->catalog = CreateObject('.'\'contactcenter.'.$catalog['class'].'\'';
624                                        if ($catalog['class_args'])
625                                        {
626                                                foreach($catalog['class_args'] as $arg)
627                                                {
628                                                        $args[] = is_string($arg) ? ($arg{0} != '$' ? "'".$arg."'" : $arg) : $arg;
629                                                        //$args[] = is_string($arg) ? "'".$arg."'" : $arg;
630                                                }
631                                                $call .= ','.implode(',',$args);
632                                        }
633                                       
634                                        $call .= ');';
635                                       
636//                                      print_r($catalog);
637//                                      echo '<br><br><b>Setando Catalogo '.$catalog['name'].': </b>'.$call.'<br>';
638
639                                        eval($call);
640                                                               
641                                        return $catalog;
642                                       
643                                default: return false;
644                        }
645                }
646               
647
648
649                /*********************************************************************\
650                 *                Methods to set general fields                      *
651                \*********************************************************************/
652               
653                /*!
654               
655                        @function add_vcard
656                        @abstract Insert a VCard to the squema
657                        @author Raphael Derosso Pereira
658                        @param string $uploaded_file The path to the file that were
659                                uploaded.
660               
661                */
662                function add_vcard ( $uploaded_file )
663                {
664                }
665
666
667               
668                /*********************************************************************\
669                 *                Methods to get general data                        *
670                \*********************************************************************/
671               
672        }
673?>
Note: See TracBrowser for help on using the repository browser.