source: companies/serpro/contactcenter/inc/class.bo_contactcenter.inc.php @ 903

Revision 903, 16.3 KB checked in by niltonneto, 15 years ago (diff)

Importacao inicial do Expresso do Serpro

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                                                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'] = $branch['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                       
327                        $this->tree = array(
328                                0 => array(
329                                        'type' => 'sql'
330                                ),
331                                'branches' => array(
332                                        0  => array(
333                                                'name'       => lang('People'),
334                                                'type'       => 'catalog',
335                                                'class'      => 'bo_people_catalog',
336                                                'icon'       => 'people-mini.png',
337                                                'sub_branch' => false
338                                        ),
339                                        1  => array(
340                                                'name'       => lang('Groups'),
341                                                'type'       => 'catalog',
342                                                'class'      => 'bo_group_manager',
343                                                'icon'       => 'people-mini.png',
344                                                'sub_branch' => False
345                                        )
346                                )
347                        );
348                       
349                        $ldap = CreateObject('contactcenter.bo_ldap_manager');
350                        $ldap_srcs = $ldap->get_all_ldap_sources();
351                       
352                        if ($ldap_srcs)
353                        {
354                                $i = 2;
355                                reset($ldap_srcs);
356                                while (list($id,) = each($ldap_srcs))
357                                {
358                                        if (($tree = $ldap->get_ldap_tree($id, $ldap_srcs[$id]['dn'], $recursive)))
359                                        {
360                                                // It isn't used anymore, but does no harm!
361                                                // It may be usefull later for use with search timeouts, or another ldap error
362                                                if (array_key_exists('error_msg', $tree))
363                                                {
364                                                        if (isset($this->tree['branches']['msg']))
365                                                        {
366                                                                $this->tree['branches']['msg'] .= "\n" . lang('Catalog %1 not showed due to error: ' .
367                                                                                                         $tree['error_msg'], $ldap_srcs[$id]['name']);
368                                                        }
369                                                        else
370                                                        {
371                                                                $this->tree['branches']['msg'] = lang('Catalog %1 not showed due to error: ' .
372                                                                                                         $tree['error_msg'], $ldap_srcs[$id]['name']);
373                                                        }
374                                                }
375                                                else
376                                                {
377                                                        $tree['name'] = $ldap_srcs[$id]['name'];
378                                                        $tree['external'] = false;
379                                                        array_push($this->tree['branches'], $tree);
380                                                        $this->tree[$i]['type'] = 'ldap';
381                                                        $this->tree[$i]['src'] = $id;
382                                                }
383
384                                        }
385                                        $i++;
386                                }
387                        }
388
389                        // external LDAP sources
390                        $ldap_srcs = $ldap->get_external_ldap_sources();
391
392                        if ($ldap_srcs)
393                        {
394                                $i = count($this->tree);
395
396                                reset($ldap_srcs);
397
398                                while (list($id,) = each($ldap_srcs))
399                                {
400                                        // External catalogs are now identified as type unknown during initialization. An optimization change.
401
402                                                $tree['name'] = $ldap_srcs[$id]['name'];
403                                                $tree['type'] = 'unknown';
404                                                $tree['value'] = $ldap_srcs[$id]['dn'];
405                                                $tree['external'] = true;
406                                                array_push($this->tree['branches'], $tree);
407                                                $this->tree[$i]['type'] = 'ldap';
408                                                $this->tree[$i]['src'] = $id;
409                                        $i++;
410                                }
411                        }
412                       
413                        $GLOBALS['phpgw']->session->appsession('bo_contactcenter.tree','contactcenter',$this->tree);
414                        return $this->tree['branches'];
415                }
416
417                /*!
418                       
419                        @function get_branch_by_level
420                        @abstract Returns the branch and it's informations given the level
421                        @author Raphael Derosso Pereira
422                       
423                        @param (string) $level The level to be used
424                       
425                */
426                function & get_branch_by_level($level)
427                {
428                        $path = @explode('.',$level);
429                        $n_ways = count($path);
430                       
431                        if ($n_ways <= 1)
432                        {
433                                return false;
434                        }
435                       
436                        $code = '$branch =& $this->tree[\'branches\']';
437                        for ($i = 1; $i < $n_ways-1; $i++)
438                        {
439                                $code .= '['.$path[$i].'][\'sub_branch\']';
440                        }
441                        $code .= '['.$path[$i].'];';
442
443                        //echo 'Codigo: '.$code.'<br>';
444                        eval($code);
445                       
446                        return $branch;
447                }
448
449                /*!
450                 
451                 @function get_info_by_level
452                 @abstract Returns the information about the catalog, given the level
453                 @author Raphael Derosso Pereira
454
455                 @param (string) $level The catalog level
456
457                */
458                function get_info_by_level($level)
459                {
460                        $path = @explode('.',$level);
461                        $n_ways = count($path);
462                       
463                        if ($n_ways <= 1)
464                        {
465                                return false;
466                        }
467                       
468                        $info = $this->tree[$path[1]];
469                       
470                        return $info;
471                }
472
473                /*!
474
475                        @function get_level_by_branch
476                        @abstract Returns the level of the given branch
477                        @author Raphael Derosso Pereira
478
479                        @param (array) $catalog The catalog
480                        @param (array) $branch  The reference to the branch to be searched
481
482                */
483                function get_level_by_branch($catalog, &$branch, $branch_level = '0')
484                {
485//                      echo '<br>';
486                        reset($branch);
487                        while(list($level, $bcatalog) = each($branch))
488                        {
489//                              echo 'Parent Level:    '.$branch_level.'<br>';
490//                              echo 'This node Level: '.$level.'<br>';
491//                              echo 'Catalog:         '.$bcatalog['name'].'<br>';
492
493                                $found = true;
494                                foreach ($catalog as $property => $value)
495                                {
496                                        if ($property !== 'sub_branch' and $bcatalog[$property] !== $value)
497                                        {
498//                                              echo 'Property <b>'.$property.'</b> differs.<br>';
499//                                              echo 'Expected: '.$value.'<br>';
500//                                              echo 'Found: '.$bcatalog[$property].'<br>';
501                                                $found = false;
502                                        }
503
504                                        if (!$found)
505                                        {
506                                                break;
507                                        }
508                                }
509
510                                if ($found)
511                                {
512//                                      echo '<b>FOUND</b><br>';
513                                        return $branch_level.'.'.((string) $level);
514                                }
515                                else if ($bcatalog['sub_branch'])
516                                {
517//                                      echo 'Not Found<br>';
518
519                                        $search = $this->get_level_by_branch($catalog, $bcatalog['sub_branch'], (string) $level);
520                                       
521                                        if ($search !== false)
522                                        {
523//                                              echo 'Returning level: '.$branch_level.'.'.$search.'<br>';
524//                                              echo 'Sholud it be '.$branch_level.'.'.$nlevel.' ?<br>';
525//                                              echo 'Or '.$branch_level.'.'.((string)$search).' ?<br>';
526                                                return $branch_level.'.'.$search;
527                                        }
528                                }
529                        }
530
531//                      echo 'Not Found in this Branch<br>';
532                        return false;
533                }
534               
535                /*!
536               
537                        @function get_actual_catalog
538                        @abstract Returns the information about the Catalog that is
539                                instantiated
540
541                        @author Raphael Derosso Pereira
542               
543                */
544                function get_actual_catalog()
545                {
546                        $catalog = $this->get_branch_by_level($this->catalog_level[0]);
547                        return $catalog;
548                }
549               
550                /*!
551               
552                        @function set_catalog
553                        @abstract Sets the actual catalog
554                        @author Raphael Derosso Pereira
555                       
556                        @param array $to_catalog The catalog in the format returned by
557                                get_available_tree or the level
558               
559                */
560                function set_catalog(& $to_catalog )
561                {
562                        if(!is_array($to_catalog))
563                        {
564                                if (is_string($to_catalog))
565                                {
566                                        if (!($t =& $this->get_branch_by_level($to_catalog)))
567                                        {
568                                                return false;
569                                        }
570                                        $level = $to_catalog;
571                                        $catalog =& $t;
572                                }
573                                else
574                                {
575                                        return false;
576                                }
577                        }
578                        else
579                        {
580                                $catalog =& $to_catalog;
581                                $level = $this->get_level_by_branch($to_catalog, $this->tree['branches']);
582                        }
583                        $lvl_vector = explode('.', $level);
584                        $id = $lvl_vector[1];
585                       
586                        switch($catalog['type'])
587                        {
588                                case 'unknown':
589                                        $level = $this->get_level_by_branch($catalog, $this->tree['branches']);
590                                        $catalog =& $this->get_catalog_tree($level);
591
592                                case 'catalog':
593                                case 'catalog_group':
594                                case 'mixed_catalog_group':
595                                        $this->catalog_level = array($level);
596                                        $GLOBALS['phpgw']->session->appsession('bo_contactcenter.catalog_level','contactcenter', $this->catalog_level);
597                                        $call = '$this->catalog = CreateObject('.'\'contactcenter.'.$catalog['class'].'\'';
598                                        if ($catalog['class_args'])
599                                        {
600                                                foreach($catalog['class_args'] as $arg)
601                                                {
602                                                        $args[] = is_string($arg) ? ($arg{0} != '$' ? "'".$arg."'" : $arg) : $arg;
603                                                        //$args[] = is_string($arg) ? "'".$arg."'" : $arg;
604                                                }
605                                                $call .= ','.implode(',',$args);
606                                        }
607                                       
608                                        $call .= ');';
609                                       
610//                                      print_r($catalog);
611//                                      echo '<br><br><b>Setando Catalogo '.$catalog['name'].': </b>'.$call.'<br>';
612
613                                        eval($call);
614                                                               
615                                        return $catalog;
616                                       
617                                default: return false;
618                        }
619                }
620               
621
622
623                /*********************************************************************\
624                 *                Methods to set general fields                      *
625                \*********************************************************************/
626               
627                /*!
628               
629                        @function add_vcard
630                        @abstract Insert a VCard to the squema
631                        @author Raphael Derosso Pereira
632                        @param string $uploaded_file The path to the file that were
633                                uploaded.
634               
635                */
636                function add_vcard ( $uploaded_file )
637                {
638                }
639
640
641               
642                /*********************************************************************\
643                 *                Methods to get general data                        *
644                \*********************************************************************/
645               
646        }
647?>
Note: See TracBrowser for help on using the repository browser.