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

Revision 2, 13.7 KB checked in by niltonneto, 17 years ago (diff)

Removida todas as tags usadas pelo CVS ($Id, $Source).
Primeira versão no CVS externo.

  • 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 find
62                        @abstract Performs a search in the DB based on the parameters
63                        @author Raphael Derosso Pereira (algorithm and code)
64                        @author Vinicius Cubas Brand (algorithm)
65                       
66                        @param array $what The list of fields to be returned. The format is:
67                                $what = array(
68                                        'contact.company.company_name',
69                                        'contact.names_ordered'
70                                );
71                       
72                        @param array $rules The restrictions.
73                       
74                        The restrictions format is quite complicated, but is very complete.
75                        As defined here, there is the possibility to do almost any type of
76                        search (tell me if you can't do any). For example, imagine the
77                        following search:
78                                       
79                                                and(a,or(d,e,and(f,g)))
80                       
81                        That is represented by the folloowing tree:
82                               
83                                   and
84                                    |
85                  .--------------------.
86                  |                    |
87                a = 5                 or
88                                       |
89                          .---------.------------.
90                          |         |            |
91                       d != 10  e LIKE %a       and
92                                                 |
93                                             .-------.
94                                             |       |
95                                           f = 5   g < 10
96
97
98                        The rules that should be passed to the find function for this tree
99                        is:
100                               
101                                $rules = array(
102                                        0 => array(
103                                                'field' => 'A',
104                                                'type'  => '=',
105                                                'value' => 5
106                                        ),
107                                        1 => array (
108                                                'type'       => 'branch',
109                                                'value'      => 'OR',
110                                                'sub_branch' => array(
111                                                        0 => array(
112                                                                'field' => 'D'
113                                                                'type'  => '!=',
114                                                                'value' => 10
115                                                        ),
116                                                        1 => array(
117                                                                'field' => 'E',
118                                                                'type'  => 'LIKE',
119                                                                'value' => '%a'
120                                                        )
121                                                        2 => array(
122                                                                'type'       => 'branch',
123                                                                'value'      => 'AND',
124                                                                'sub_branch' => array(
125                                                                        0 => array(
126                                                                                'field' => 'F',
127                                                                                'type'  => '=',
128                                                                                'value' => 5
129                                                                        ),
130                                                                        1 => array(
131                                                                                'field' => 'G'
132                                                                                'type'  => '<',
133                                                                                'value' => 10
134                                                                        )
135                                                                )
136                                                        )
137                                                )
138                                        )
139                                );
140
141 
142                        The restriction type can be: =, !=, <=, <, >, >=, NULL, IN, LIKE,
143                        NOT NULL, NOT IN, NOT LIKE
144                        Value of branch can be AND, OR, NOT
145                       
146                        @param array $other Other parameter to the search
147                                $other = array(
148                                        'offset'          => (int),
149                                        'limit'           => (int),
150                                        'sort'            => {ASC|DESC},
151                                        'order'           => (string with field names separated by commas)
152                                        'fields_modifier' => (COUNT|MAX)
153                                );
154
155                        @return array $array[<field_name>][<row_number>]
156                               
157                */
158                function find($what, $rules=false, $other=false)
159                {
160                        return $this->catalog->find($what, $rules, $other);
161                }
162               
163                /*!
164               
165                        @function get_catalog_tree
166                        @abstract Returns an array describing the available
167                                catalog-entity-view tree and their respective
168                                values and types
169                        @author Raphael Derosso Pereira
170
171                        @param (string)  $level The level to be taken
172                        @param (boolean) $recursive Retrive the whole tree from
173                                the level specified until the leaves
174                       
175                        @return The format of the return is:
176                                $return = array(
177                                        0  => array(
178                                                'name'       => '<branch_name>',
179                                                'type'       => '<branch_type>',
180                                                'class'      => '<branch_class>',
181                                                'class_args' => '<branch_class_args>',
182                                                'find_args'  => '<branch_find_args>',
183                                                'sub_branch' => array(
184                                                        0  => array(
185                                                                'name'       => '<branch_name>',
186                                                                'type'       => '<branch_type>',
187                                                                'class'      => '<branch_class>',
188                                                                'class_args' => '<branch_class_args>',
189                                                                'find_args'  => '<branch_find_args>',
190                                                                'sub_branch' => array(...)
191                                                        ),
192                                                        1  => array(...),...
193                                                ),
194                                        ),
195                                        1  => array(...),...
196                                );
197                               
198                                <branch_type> can be 'catalog_group', 'catalog' or 'view';
199                                <branch_class> is the name of the class that is capable of
200                                        handling the information for this catalog/view
201                                <branch_class_args> is an array that holds the arguments to
202                                        be passed to <branch_class> when it is instantiated
203                                <brach_find_args> is the string that should precede the search
204                                        string
205                                               
206                                If the branch is actually a leaf, than 'sub_branch' is false;
207               
208               
209                        TODO: This method is hard-coded, but it should grab the tree
210                        from the DB using the View Manager...
211                */
212                function get_catalog_tree($level = '0', $recursive = false)
213                {
214                        if ($this->tree)
215                        {
216                                if ($level === '0')
217                                {
218                                        return $this->tree['branches'];
219                                }
220                               
221                                $branch =& $this->get_branch_by_level($level);
222                                $info = $this->get_info_by_level($level);
223                               
224                                if ($branch['type'] === 'unknown')
225                                {
226                                        if ($info['type'] === 'ldap')
227                                        {
228                                                $ldap = CreateObject('contactcenter.bo_ldap_manager');
229                                                $new_branch = $ldap->get_ldap_tree($info['src'], $branch['value'], $recursive);
230                                               
231                                                if ($new_branch)
232                                                {
233                                                        $branch = $new_branch;
234                                                }
235                                                else
236                                                {
237                                                        return false;
238                                                }
239                                        }
240                                        $GLOBALS['phpgw']->session->appsession('bo_contactcenter.tree','contactcenter',$this->tree);
241                                       
242                                        return $branch;
243                                }
244                                else if ($branch['type'])
245                                {
246                                        return $branch;
247                                }
248                               
249                                return false;
250                        }
251                       
252                        if ($level !== '0')
253                        {
254                                return false;
255                        }
256                       
257                        $this->tree = array(
258                                0 => array(
259                                        'type' => 'sql'
260                                ),
261                                'branches' => array(
262                                        0  => array(
263                                                'name'       => lang('People'),
264                                                'type'       => 'catalog',
265                                                'class'      => 'bo_people_catalog',
266                                                'icon'       => 'people-mini.png',
267                                                'sub_branch' => false
268                                        ),/*
269                                        1  => array(
270                                                'name'       => lang('Companies'),
271                                                'type'       => 'catalog',
272                                                'class'      => 'bo_company_manager',
273                                                'find_args'  => 'company',
274                                                'icon'       => 'company-mini.png',
275                                                'sub_branch' => false
276                                        ),*/
277                                       
278                                        1  => array(
279                                                'name'       => lang('Groups'),
280                                                'type'       => 'catalog',
281                                                'class'      => 'bo_group_manager',
282                                                'icon'       => 'people-mini.png',
283                                                'sub_branch' => False   
284                                        )
285                                )
286                        );
287                       
288                        $ldap = CreateObject('contactcenter.bo_ldap_manager');
289                        $ldap_srcs = $ldap->get_all_ldap_sources();
290                       
291                        if ($ldap_srcs)
292                        {
293                                $i = 2;
294                                reset($ldap_srcs);
295                                while (list($id,) = each($ldap_srcs))
296                                {
297                                        if (($tree = $ldap->get_ldap_tree($id, $ldap_srcs[$id]['dn'], $recursive)))
298                                        {
299                                                $tree['name'] = $ldap_srcs[$id]['name'];
300                                                array_push($this->tree['branches'], $tree);
301                                                $this->tree[$i]['type'] = 'ldap';
302                                                $this->tree[$i]['src'] = $id;
303                                        }
304                                        $i++;
305                                }
306                        }
307                       
308                        $GLOBALS['phpgw']->session->appsession('bo_contactcenter.tree','contactcenter',$this->tree);
309                        return $this->tree['branches'];
310                }
311
312                /*!
313                       
314                        @function get_branch_by_level
315                        @abstract Returns the branch and it's informations given the level
316                        @author Raphael Derosso Pereira
317                       
318                        @param (string) $level The level to be used
319                       
320                */
321                function & get_branch_by_level($level)
322                {
323                        $path = @explode('.',$level);
324                        $n_ways = count($path);
325                       
326                        if ($n_ways <= 1)
327                        {
328                                return false;
329                        }
330                       
331                        $code = '$branch =& $this->tree[\'branches\']';
332                        for ($i = 1; $i < $n_ways-1; $i++)
333                        {
334                                $code .= '['.$path[$i].'][\'sub_branch\']';
335                        }
336                        $code .= '['.$path[$i].'];';
337
338                        //echo 'Codigo: '.$code.'<br>';
339                        eval($code);
340                       
341                        return $branch;
342                }
343
344                /*!
345                 
346                 @function get_info_by_level
347                 @abstract Returns the information about the catalog, given the level
348                 @author Raphael Derosso Pereira
349
350                 @param (string) $level The catalog level
351
352                */
353                function get_info_by_level($level)
354                {
355                        $path = @explode('.',$level);
356                        $n_ways = count($path);
357                       
358                        if ($n_ways <= 1)
359                        {
360                                return false;
361                        }
362                       
363                        $info = $this->tree[$path[1]];
364                       
365                        return $info;
366                }
367
368                /*!
369
370                        @function get_level_by_branch
371                        @abstract Returns the level of the given branch
372                        @author Raphael Derosso Pereira
373
374                        @param (array) $catalog The catalog
375                        @param (array) $branch  The reference to the branch to be searched
376
377                */
378                function get_level_by_branch($catalog, &$branch, $branch_level = '0')
379                {
380//                      echo '<br>';
381                        reset($branch);
382                        while(list($level, $bcatalog) = each($branch))
383                        {
384//                              echo 'Parent Level:    '.$branch_level.'<br>';
385//                              echo 'This node Level: '.$level.'<br>';
386//                              echo 'Catalog:         '.$bcatalog['name'].'<br>';
387
388                                $found = true;
389                                foreach ($catalog as $property => $value)
390                                {
391                                        if ($property !== 'sub_branch' and $bcatalog[$property] !== $value)
392                                        {
393//                                              echo 'Property <b>'.$property.'</b> differs.<br>';
394//                                              echo 'Expected: '.$value.'<br>';
395//                                              echo 'Found: '.$bcatalog[$property].'<br>';
396                                                $found = false;
397                                        }
398
399                                        if (!$found)
400                                        {
401                                                break;
402                                        }
403                                }
404
405                                if ($found)
406                                {
407//                                      echo '<b>FOUND</b><br>';
408                                        return $branch_level.'.'.((string) $level);
409                                }
410                                else if ($bcatalog['sub_branch'])
411                                {
412//                                      echo 'Not Found<br>';
413
414                                        $search = $this->get_level_by_branch($catalog, $bcatalog['sub_branch'], (string) $level);
415                                       
416                                        if ($search !== false)
417                                        {
418//                                              echo 'Returning level: '.$branch_level.'.'.$search.'<br>';
419//                                              echo 'Sholud it be '.$branch_level.'.'.$nlevel.' ?<br>';
420//                                              echo 'Or '.$branch_level.'.'.((string)$search).' ?<br>';
421                                                return $branch_level.'.'.$search;
422                                        }
423                                }
424                        }
425
426//                      echo 'Not Found in this Branch<br>';
427                        return false;
428                }
429               
430                /*!
431               
432                        @function get_actual_catalog
433                        @abstract Returns the information about the Catalog that is
434                                instantiated
435
436                        @author Raphael Derosso Pereira
437               
438                */
439                function get_actual_catalog()
440                {
441                        $catalog = $this->get_branch_by_level($this->catalog_level[0]);
442                        return $catalog;
443                }
444               
445                /*!
446               
447                        @function set_catalog
448                        @abstract Sets the actual catalog
449                        @author Raphael Derosso Pereira
450                       
451                        @param array $to_catalog The catalog in the format returned by
452                                get_available_tree or the level
453               
454                */
455                function set_catalog(& $to_catalog )
456                {
457                        if(!is_array($to_catalog))
458                        {
459                                if (is_string($to_catalog))
460                                {
461                                        if (!($t =& $this->get_branch_by_level($to_catalog)))
462                                        {
463                                                return false;
464                                        }
465                                        $level = $to_catalog;
466                                        $catalog =& $t;
467                                }
468                                else
469                                {
470                                        return false;
471                                }
472                        }
473                        else
474                        {
475                                $catalog =& $to_catalog;
476                                //echo "Daqui!<br>";
477                                $level = $this->get_level_by_branch($to_catalog, $this->tree['branches']);
478                        }
479                       
480                        switch($catalog['type'])
481                        {
482                                case 'unknown':
483                                        $level = $this->get_level_by_branch($catalog, $this->tree['branches']);
484                                        $catalog =& $this->get_catalog_tree($level);
485
486                                case 'catalog':
487                                case 'catalog_group':
488                                case 'mixed_catalog_group':
489                                        $this->catalog_level = array($level);
490                                        $GLOBALS['phpgw']->session->appsession('bo_contactcenter.catalog_level','contactcenter', $this->catalog_level);
491                                        $call = '$this->catalog = CreateObject('.'\'contactcenter.'.$catalog['class'].'\'';
492                                        if ($catalog['class_args'])
493                                        {
494                                                foreach($catalog['class_args'] as $arg)
495                                                {
496                                                        $args[] = is_string($arg) ? ($arg{0} != '$' ? "'".$arg."'" : $arg) : $arg;
497                                                        //$args[] = is_string($arg) ? "'".$arg."'" : $arg;
498                                                }
499                                                $call .= ','.implode(',',$args);
500                                        }
501                                       
502                                        $call .= ');';
503                                       
504//                                      print_r($catalog);
505//                                      echo '<br><br><b>Setando Catalogo '.$catalog['name'].': </b>'.$call.'<br>';
506
507                                        eval($call);
508                                                               
509                                        return $catalog;
510                                       
511                                default: return false;
512                        }
513                }
514               
515
516
517                /*********************************************************************\
518                 *                Methods to set general fields                      *
519                \*********************************************************************/
520               
521                /*!
522               
523                        @function add_vcard
524                        @abstract Insert a VCard to the squema
525                        @author Raphael Derosso Pereira
526                        @param string $uploaded_file The path to the file that were
527                                uploaded.
528               
529                */
530                function add_vcard ( $uploaded_file )
531                {
532                }
533
534
535               
536                /*********************************************************************\
537                 *                Methods to get general data                        *
538                \*********************************************************************/
539               
540        }
541?>
Note: See TracBrowser for help on using the repository browser.