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

Revision 284, 17.1 KB checked in by rafaelraymundo, 16 years ago (diff)

Vide Trac - #197, #166, #198, #199

  1. Correção de problema na leitura do arquivo configuração.
  2. Permissão do click2dial também no contactcenter.
  3. Visualização de matricula, e nro celular no resultado da pesquisa(se estiverem populados)
  4. Adicionada a leitura a Catálogos Externos
  • 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)
209                {
210                        return $this->catalog->find($what, $rules, $other);
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
284                                                if ($this->tree['branches'][$id]['external'])
285                                                {
286                                                        // if it's an external catalog
287                                                        $new_branch = $ldap->get_external_ldap_tree($info['src'], $branch['value'], $recursive);
288                                                }
289                                                else
290                                                {
291                                                        // if it's not an external catalog
292                                                        $new_branch = $ldap->get_ldap_tree($info['src'], $branch['value'], $recursive);
293                                                }
294
295                                                if ($new_branch)
296                                                {
297                                                        if (!empty($new_branch['timeout']) && !empty($new_branch['msg']))
298                                                        {
299                                                                return $new_branch;
300                                                        }
301
302                                                        // Necessary for the new way the catalog tree is built at initialization
303                                                        $new_branch['name'] = $branch['name'];
304                                                        $new_branch['external'] = $branch['external'];
305                                                        $branch = $new_branch;
306
307                                                }
308                                                else
309                                                {
310                                                        return false;
311                                                }
312                                        }
313                                        $GLOBALS['phpgw']->session->appsession('bo_contactcenter.tree','contactcenter',$this->tree);
314                                        return $branch;
315                                }
316                                else if ($branch['type'])
317                                {
318                                        return $branch;
319                                }
320
321                                return false;
322                        }
323
324                        if ($level !== '0')
325                        {
326                                return false;
327                        }
328
329                        $this->tree = array(
330                                0 => 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('Companies'),
343                                                'type'       => 'catalog',
344                                                'class'      => 'bo_company_manager',
345                                                'find_args'  => 'company',
346                                                'icon'       => 'company-mini.png',
347                                                'sub_branch' => false
348                                        ),*/
349
350                                        1  => array(
351                                                'name'       => lang('Groups'),
352                                                'type'       => 'catalog',
353                                                'class'      => 'bo_group_manager',
354                                                'icon'       => 'people-mini.png',
355                                                'sub_branch' => False
356                                        )
357                                )
358                        );
359
360                        $ldap = CreateObject('contactcenter.bo_ldap_manager');
361                        $ldap_srcs = $ldap->get_all_ldap_sources();
362
363                        if ($ldap_srcs)
364                        {
365                                $i = 2;
366                                reset($ldap_srcs);
367                                while (list($id,) = each($ldap_srcs))
368                                {
369                                        if (($tree = $ldap->get_ldap_tree($id, $ldap_srcs[$id]['dn'], $recursive)))
370                                        {
371                                                // It isn't used anymore, but does no harm!
372                                                // It may be usefull later for use with search timeouts, or another ldap error
373                                                if (array_key_exists('error_msg', $tree))
374                                                {
375                                                        if (isset($this->tree['branches']['msg']))
376                                                        {
377                                                                $this->tree['branches']['msg'] .= "\n" . lang('Catalog %1 not showed due to error: ' .
378                                                                                                         $tree['error_msg'], $ldap_srcs[$id]['name']);
379                                                        }
380                                                        else
381                                                        {
382                                                                $this->tree['branches']['msg'] = lang('Catalog %1 not showed due to error: ' .
383                                                                                                         $tree['error_msg'], $ldap_srcs[$id]['name']);
384                                                        }
385                                                }
386                                                else
387                                                {
388                                                        $tree['name'] = $ldap_srcs[$id]['name'];
389                                                        $tree['external'] = false;
390                                                        array_push($this->tree['branches'], $tree);
391                                                        $this->tree[$i]['type'] = 'ldap';
392                                                        $this->tree[$i]['src'] = $id;
393                                                }
394
395                                        }
396                                        $i++;
397                                }
398                        }
399
400                        // external LDAP sources
401                        $ldap_srcs = $ldap->get_external_ldap_sources();
402
403                        if ($ldap_srcs)
404                        {
405                                $i = count($this->tree);
406
407                                reset($ldap_srcs);
408
409                                while (list($id,) = each($ldap_srcs))
410                                {
411                                        // External catalogs are now identified as type unknown during initialization. An optimization change.
412
413                                        /*
414                                        if (($tree = $ldap->get_external_ldap_tree($id, $ldap_srcs[$id]['dn'], $recursive)))
415                                        {
416                                                if (array_key_exists('error_msg', $tree))
417                                                {
418                                                        if (isset($this->tree['branches']['msg']))
419                                                        {
420                                                                $this->tree['branches']['msg'] .= "\n" . lang('Catalog %1 not showed due to error: ' .
421                                                                                                         $tree['error_msg'], $ldap_srcs[$id]['name']);
422                                                        }
423                                                        else
424                                                        {
425                                                                $this->tree['branches']['msg'] = lang('Catalog %1 not showed due to error: ' .
426                                                                                                         $tree['error_msg'], $ldap_srcs[$id]['name']);
427                                                        }
428                                                }
429
430                                                else
431                                                {
432                                                */
433                                                $tree['name'] = $ldap_srcs[$id]['name'];
434                                                $tree['type'] = 'unknown';
435                                                $tree['value'] = $ldap_srcs[$id]['dn'];
436                                                $tree['external'] = true;
437                                                //$tree['sub_branch'] = false;
438                                                array_push($this->tree['branches'], $tree);
439                                                $this->tree[$i]['type'] = 'ldap';
440                                                $this->tree[$i]['src'] = $id;
441                                        //      }
442                                        //}
443                                        $i++;
444                                }
445                        }
446
447                        $GLOBALS['phpgw']->session->appsession('bo_contactcenter.tree','contactcenter',$this->tree);
448                        return $this->tree['branches'];
449                        //return null;
450                }
451
452                /*!
453
454                        @function get_branch_by_level
455                        @abstract Returns the branch and it's informations given the level
456                        @author Raphael Derosso Pereira
457
458                        @param (string) $level The level to be used
459
460                */
461                function & get_branch_by_level($level)
462                {
463                        $path = @explode('.',$level);
464                        $n_ways = count($path);
465
466                        if ($n_ways <= 1)
467                        {
468                                return false;
469                        }
470
471                        $code = '$branch =& $this->tree[\'branches\']';
472                        for ($i = 1; $i < $n_ways-1; $i++)
473                        {
474                                $code .= '['.$path[$i].'][\'sub_branch\']';
475                        }
476                        $code .= '['.$path[$i].'];';
477
478                        //echo 'Codigo: '.$code.'<br>';
479                        eval($code);
480
481                        return $branch;
482                }
483
484                /*!
485
486                 @function get_info_by_level
487                 @abstract Returns the information about the catalog, given the level
488                 @author Raphael Derosso Pereira
489
490                 @param (string) $level The catalog level
491
492                */
493                function get_info_by_level($level)
494                {
495                        $path = @explode('.',$level);
496                        $n_ways = count($path);
497
498                        if ($n_ways <= 1)
499                        {
500                                return false;
501                        }
502
503                        $info = $this->tree[$path[1]];
504
505                        return $info;
506                }
507
508                /*!
509
510                        @function get_level_by_branch
511                        @abstract Returns the level of the given branch
512                        @author Raphael Derosso Pereira
513
514                        @param (array) $catalog The catalog
515                        @param (array) $branch  The reference to the branch to be searched
516
517                */
518                function get_level_by_branch($catalog, &$branch, $branch_level = '0')
519                {
520//                      echo '<br>';
521                        reset($branch);
522                        while(list($level, $bcatalog) = each($branch))
523                        {
524//                              echo 'Parent Level:    '.$branch_level.'<br>';
525//                              echo 'This node Level: '.$level.'<br>';
526//                              echo 'Catalog:         '.$bcatalog['name'].'<br>';
527
528                                $found = true;
529                                foreach ($catalog as $property => $value)
530                                {
531                                        if ($property !== 'sub_branch' and $bcatalog[$property] !== $value)
532                                        {
533//                                              echo 'Property <b>'.$property.'</b> differs.<br>';
534//                                              echo 'Expected: '.$value.'<br>';
535//                                              echo 'Found: '.$bcatalog[$property].'<br>';
536                                                $found = false;
537                                        }
538
539                                        if (!$found)
540                                        {
541                                                break;
542                                        }
543                                }
544
545                                if ($found)
546                                {
547//                                      echo '<b>FOUND</b><br>';
548                                        return $branch_level.'.'.((string) $level);
549                                }
550                                else if ($bcatalog['sub_branch'])
551                                {
552//                                      echo 'Not Found<br>';
553
554                                        $search = $this->get_level_by_branch($catalog, $bcatalog['sub_branch'], (string) $level);
555
556                                        if ($search !== false)
557                                        {
558//                                              echo 'Returning level: '.$branch_level.'.'.$search.'<br>';
559//                                              echo 'Sholud it be '.$branch_level.'.'.$nlevel.' ?<br>';
560//                                              echo 'Or '.$branch_level.'.'.((string)$search).' ?<br>';
561                                                return $branch_level.'.'.$search;
562                                        }
563                                }
564                        }
565
566//                      echo 'Not Found in this Branch<br>';
567                        return false;
568                }
569
570                /*!
571
572                        @function get_actual_catalog
573                        @abstract Returns the information about the Catalog that is
574                                instantiated
575
576                        @author Raphael Derosso Pereira
577
578                */
579                function get_actual_catalog()
580                {
581                        $catalog = $this->get_branch_by_level($this->catalog_level[0]);
582                        return $catalog;
583                }
584
585                /*!
586
587                        @function set_catalog
588                        @abstract Sets the actual catalog
589                        @author Raphael Derosso Pereira
590
591                        @param array $to_catalog The catalog in the format returned by
592                                get_available_tree or the level
593
594                */
595                function set_catalog(& $to_catalog )
596                {
597                        if(!is_array($to_catalog))
598                        {
599                                if (is_string($to_catalog))
600                                {
601                                        if (!($t =& $this->get_branch_by_level($to_catalog)))
602                                        {
603                                                return false;
604                                        }
605                                        $level = $to_catalog;
606                                        $catalog =& $t;
607                                }
608                                else
609                                {
610                                        return false;
611                                }
612                        }
613                        else
614                        {
615                                $catalog =& $to_catalog;
616                                //echo "Daqui!<br>";
617                                $level = $this->get_level_by_branch($to_catalog, $this->tree['branches']);
618                        }
619
620                        $lvl_vector = explode('.', $level);
621                        $id = $lvl_vector[1];
622
623                        switch($catalog['type'])
624                        {
625                                case 'unknown':
626                                        $level = $this->get_level_by_branch($catalog, $this->tree['branches']);
627                                        $catalog =& $this->get_catalog_tree($level);
628
629                                case 'catalog':
630                                case 'catalog_group':
631                                case 'mixed_catalog_group':
632                                        $this->catalog_level = array($level);
633                                        $GLOBALS['phpgw']->session->appsession('bo_contactcenter.catalog_level','contactcenter', $this->catalog_level);
634                                        $call = '$this->catalog = CreateObject('.'\'contactcenter.'.$catalog['class'].'\'';
635                                        if ($catalog['class_args'])
636                                        {
637                                                foreach($catalog['class_args'] as $arg)
638                                                {
639                                                        $args[] = is_string($arg) ? ($arg{0} != '$' ? "'".$arg."'" : $arg) : $arg;
640                                                        //$args[] = is_string($arg) ? "'".$arg."'" : $arg;
641                                                }
642                                                $call .= ','.implode(',',$args);
643                                        }
644
645                                        $call .= ');';
646
647//                                      print_r($catalog);
648//                                      echo '<br><br><b>Setando Catalogo '.$catalog['name'].': </b>'.$call.'<br>';
649
650                                        eval($call);
651
652                                        return $catalog;
653
654                                default: return false;
655                        }
656                }
657
658
659
660                /*********************************************************************\
661                 *                Methods to set general fields                      *
662                \*********************************************************************/
663
664                /*!
665
666                        @function add_vcard
667                        @abstract Insert a VCard to the squema
668                        @author Raphael Derosso Pereira
669                        @param string $uploaded_file The path to the file that were
670                                uploaded.
671
672                */
673                function add_vcard ( $uploaded_file )
674                {
675                }
676
677
678
679                /*********************************************************************\
680                 *                Methods to get general data                        *
681                \*********************************************************************/
682
683        }
684?>
Note: See TracBrowser for help on using the repository browser.