source: sandbox/2.2.0.2/contactcenter/inc/class.abo_catalog.inc.php @ 4538

Revision 4538, 30.0 KB checked in by airton, 13 years ago (diff)

Ticket #1934 - Parametrização das buscas LDAP no Contactcenter

  • 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 abo_catalog
16        {
17                var $db;
18               
19                var $security;
20               
21                /*!
22                        @attr array Tables
23                        @abstract The main descriptor between high-level field indication
24                                and low-level table relations
25                        @author Raphael Derosso Pereira (conception and code)
26                        @author Vinicius Cubas Brand (conception)
27                */
28
29                var $tables = array(
30                        'status' => array(
31                                'table' => 'phpgw_cc_status'
32                        ),
33                       
34                        'prefix' => array(
35                                'table' => 'phpgw_cc_prefixes'
36                        ),
37                       
38                        'suffix' => array(
39                                'table' => 'phpgw_cc_suffixes'
40                        ),
41                       
42                        'contact' => array(
43                                'table'  => 'phpgw_cc_contact',
44                               
45                                'status' => 'id_status,id_status',
46                                'prefix' => 'id_prefix,id_prefix',
47                                'suffix' => 'id_suffix,id_suffix',
48                               
49                                'contact_related' => 'id_contact,id_contact',
50                                'related'         => array('shortcut' => 'contact_related'),
51                               
52                                'contact_connection' => 'id_contact,id_contact',
53                                'connection'         => array('path' => 'contact_connection'),
54
55                                'contact_address' => 'id_contact,id_contact',
56                                'address'         => array('path' => 'contact_address'),
57                               
58                                'business_info'   => 'id_contact,id_contact',
59                                'company'         => array('path' => 'business_info')
60                        ),
61                       
62                        'business_info' => array(
63                                'table'   => 'phpgw_cc_contact_company',
64                                'company' => 'id_company,id_company'
65                        ),
66                       
67                        'group' => array('phpgw_cc_groups',
68                                'table'              => 'phpgw_cc_groups',
69                        ),
70                       
71                        'company' => array(
72                                'table'              => 'phpgw_cc_company',
73                               
74                                'company_related'    => 'id_company.id_company',
75                                'related'            => array('shortcut' => 'company_related'),
76                               
77                                'company_address'    => 'id_company,id_company',
78                                'address'            => array('path' => 'company_address'),
79                               
80                                'company_connection' => 'id_company,id_company',
81                                'connection'         => array('path' => 'company_connection'),
82                               
83                                'business_info'      => 'id_company,id_company',
84                                'contact'            => array('path' => 'business_info'),
85                               
86                                'legal'              => 'id_company,id_company',
87                        ),
88                       
89                        'company_related' => array(
90                                'table'                   => 'phpgw_cc_company_rels',
91                                'company'                 => 'id_related,id_company',
92
93                                'company_related'         => 'id_related,id_company',
94                                'related'                 => array('shortcut' => 'company_related'),
95
96                                'typeof_company_relation' => 'id_typeof_company_relation,id_typeof_company_relation',
97                                'typeof_relation'         => array('shortcut' => 'typeof_company_relation'),
98                                'type'                    => array('shortcut' => 'typeof_company_relation')
99                        ),
100                        'contact_related' => array(
101                                'table'                   => 'phpgw_cc_contact_rels',
102                                'contact'                 => 'id_related,id_contact',
103                               
104                                'contact_related'         => 'id_contact,id_related',
105                                'related'                 => array('shortcut' => 'contact_related'),
106                               
107                                'typeof_contact_relation' => 'id_typeof_contact_relation,id_typeof_contact_relation',
108                                'typeof_relation'         => array('shortcut' => 'typeof_contact_relation'),
109                                'type'                    => array('shortcut' => 'typeof_contact_relation')
110                        ),
111                       
112                        'company_address' => array(
113                                'table'          => 'phpgw_cc_company_addrs',
114                                'address'        => 'id_address,id_address',
115                                'typeof_address' => array('shortcut' => 'typeof_company_address'),
116                                'type'           => array('shortcut' => 'typeof_company_address')
117                        ),
118                        'contact_address' => array(
119                                'table'          => 'phpgw_cc_contact_addrs',
120                                'address'        => 'id_address,id_address',
121                                'typeof_address' => array('shortcut' => 'typeof_contact_address'),
122                                'type'           => array('shortcut' => 'typeof_contact_address')
123                        ),
124                        'address' => array(
125                                'table' => 'phpgw_cc_addresses',
126                                'city'  => 'id_city,id_city'
127                        ),
128                        'city' => array(
129                                'table'   => 'phpgw_cc_city',
130                                'state'   => 'id_state,id_state',
131                                'country' => 'id_country,id_country'
132                        ),
133                        'state' => array(
134                                'table'   => 'phpgw_cc_state',
135                                'country' => 'id_country,id_country',
136                        ),
137                        'country' => array(
138                                'table' => 'phpgw_cc_country'
139                        ),
140                       
141                        'company_connection' => array(
142                                'table'             => 'phpgw_cc_company_conns',
143                                'connection'        => 'id_connection,id_connection',
144                                'typeof_connection' => array('shortcut' => 'typeof_company_connection'),
145                                'type'              => array('shortcut' => 'typeof_company_connection')
146                        ),
147                        'contact_connection' => array(
148                                'table'             => 'phpgw_cc_contact_conns',
149                                'connection'        => 'id_connection,id_connection',
150                                'typeof_connection' => array('shortcut' => 'typeof_contact_connection'),
151                                'type'              => array('shortcut' => 'typeof_contact_connection')
152                        ),
153                        'connection' => array(
154                                'table'             => 'phpgw_cc_connections'
155                        ),
156
157                        'legal' => array(
158                                'table'                => 'phpgw_cc_company_legals',
159                                'typeof_company_legal' => 'id_typeof_company_legal,id_typeof_company_legal',
160                                'typeof_legal'         => array('shortcut' => 'typeof_company_legal'),
161                                'type'                 => array('shortcut' => 'typeof_company_legal')
162                        ),
163                       
164                                               
165                        'typeof_contact_relation' => array(
166                                'table' => 'phpgw_cc_typeof_ct_rels'
167                        ),
168                        'typeof_company_relation' => array(
169                                'table' => 'phpgw_cc_typeof_co_rels'
170                        ),
171                        'typeof_contact_address' => array(
172                                'table' => 'phpgw_cc_typeof_ct_addrs'
173                        ),
174                        'typeof_company_address' => array(
175                                'table' => 'phpgw_cc_typeof_co_addrs'
176                        ),
177                        'typeof_contact_connection' => array(
178                                'table' => 'phpgw_cc_typeof_ct_conns'
179                        ),
180                        'typeof_company_connection' => array(
181                                'table' => 'phpgw_cc_typeof_co_conns'
182                        ),
183                        'typeof_company_legal' => array(
184                                'table' => 'phpgw_cc_typeof_co_legals'
185                        )
186                );
187               
188                function init()
189                {
190                        $this->db =& $GLOBALS['phpgw']->db;
191                        $this->security =& CreateObject('contactcenter.security_manager');
192                }
193
194                /*!
195               
196                        @function sql_find
197                        @abstract Performs a search in the DB based on the parameters
198                        @author Raphael Derosso Pereira (algorithm and code)
199                        @author Vinicius Cubas Brand (algorithm)
200                       
201                        @param array $what The list of fields to be returned. The format is:
202                                $what = array(
203                                        'contact.company.company_name',
204                                        'contact.names_ordered'
205                                );
206                       
207                        @param array $rules The restrictions.
208                       
209                        The restrictions format is quite complicated, but is very complete.
210                        As defined here, there is the possibility to do almost any type of
211                        search (tell me if you can't do any). For example, imagine the
212                        following search:
213                                       
214                                                and(a,or(d,e,and(f,g)))
215                       
216                        That is represented by the folloowing tree:
217                               
218                                   and
219                                    |
220                  .--------------------.
221                  |                    |
222                a = 5                 or
223                                       |
224                          .---------.------------.
225                          |         |            |
226                       d != 10  e LIKE %a       and
227                                                 |
228                                             .-------.
229                                             |       |
230                                           f = 5   g < 10
231
232
233                        The rules that should be passed to the find function for this tree
234                        is:
235                               
236                                $rules = array(
237                                        0 => array(
238                                                'field' => 'A',
239                                                'type'  => '=',
240                                                'value' => 5
241                                        ),
242                                        1 => array (
243                                                'type'       => 'branch',
244                                                'value'      => 'OR',
245                                                'sub_branch' => array(
246                                                        0 => array(
247                                                                'field' => 'D'
248                                                                'type'  => '!=',
249                                                                'value' => 10
250                                                        ),
251                                                        1 => array(
252                                                                'field' => 'E',
253                                                                'type'  => 'LIKE',
254                                                                'value' => '%a'
255                                                        )
256                                                        2 => array(
257                                                                'type'       => 'branch',
258                                                                'value'      => 'AND',
259                                                                'sub_branch' => array(
260                                                                        0 => array(
261                                                                                'field' => 'F',
262                                                                                'type'  => '=',
263                                                                                'value' => 5
264                                                                        ),
265                                                                        1 => array(
266                                                                                'field' => 'G'
267                                                                                'type'  => '<',
268                                                                                'value' => 10
269                                                                        )
270                                                                )
271                                                        )
272                                                )
273                                        )
274                                );
275
276 
277                        The restriction type can be: =, !=, <=, <, >, >=, NULL, IN, LIKE,
278                        NOT NULL, NOT IN, NOT LIKE
279                        Value of branch can be AND, OR, NOT
280                       
281                        @param array $other Other parameter to the search
282                                $other = array(
283                                        'offset'          => (int),
284                                        'limit'           => (int),
285                                        'order'           => (string with field names separated by commas)
286                                        'sort'            => {ASC|DESC},
287                                        'fields_modifier' => (COUNT|MAX)
288                                );
289
290                        @return array $array[<field_name>][<row_number>]
291                               
292                */
293                function sql_find($what, $rules=false, $other=false)
294                {
295                        if (!is_array($what))
296                        {
297                                exit('Error');
298                        }
299                       
300                        $tables_def = $GLOBALS['phpgw']->db->get_table_definitions('contactcenter');
301                       
302                        $query_select = 'SELECT ';
303                        $query_from = array();
304                        $query_fields_joins = array();
305                        $query_restric_joins = array();
306                        $query_wheres = array();
307                        $tables_as = array();
308                       
309                        $n_fields = count($what);
310                        for($i = 0; $i < $n_fields; $i++)
311                        {
312                                $path = $this->get_tables_by_field($what[$i], $tables_def);
313                                $n_tables = count($path);
314                               
315                                $fields[] = $path[$n_tables-1];
316                                $fields_translate[$what[$i]] = $path[$n_tables-1];
317                               
318                                list($from_t,) = explode('.',$path[0]);
319                                $query_from[] = $from_t;
320                               
321                                $last_non_unique = false;
322                                for($j = 0; $j < $n_tables-1; $j += 2)
323                                {
324                                        list($left_t,$left_k) = explode('.',$path[$j+1]);
325                                        list($right_t,$right_k) = explode('.',$path[$j]);
326                                       
327                                        $query_join_t = 'LEFT JOIN '.$left_t.' AS t0'.$i.$j.' ON t0'.
328                                                         $i.$j.'.'.$left_k.'=';
329
330                                        $query_join_reg = 'LEFT JOIN '.$left_t.' AS t[0-9]{2,} ON t[0-9]{2,}'.
331                                                         '\.'.$left_k.'=';
332                                       
333                                        $t = $j-2;
334                                        if ($j == 0)
335                                        {
336                                                $query_join_t .= $path[$j];
337                                                $query_join_reg .= $path[$j];
338                                        }
339                                        else
340                                        {
341                                                $query_join_t .= 't0'.$i.$t.'.'.$right_k;
342                                                $query_join_reg .= 't[0-9]{2,}\.'.$right_k;
343                                        }
344                                       
345                                        $query_fields_joins[] = $query_join_t;
346                                        $tables_as[] = 't0'.$i.$j;
347                                        continue;
348                                       
349                                        /* TODO: The code below detects the double JOIN lines, but
350                                         * the problem is that it doesn't do that correctly, because
351                                         * it should consider the hole path, not just one entry.
352                                         */
353                                        $unique = true;
354                                        if ($n_joins = count($query_fields_joins))
355                                        {
356                                                for($k = 0; $k < $n_joins; $k++)
357                                                {
358                                                        if (ereg($query_join_reg,$query_fields_joins[$k]))
359                                                        {       
360                                                                $unique = false;
361                                                                $last_non_unique = $k; 
362                                                        }
363                                                }
364                                        }
365
366                                        if ($unique and $j != 0)
367                                        {
368                                                if ($j != 0)
369                                                {
370                                                        if ($last_non_unique === false)
371                                                        {
372                                                                $query_join_t .= 't0'.$i.$t.'.'.$right_k;
373                                                        }
374                                                        else
375                                                        {
376                                                                $query_join_t .= $tables_as[$last_non_unique].'.'.$right_k;
377                                                        }
378                                                }
379                                                $query_fields_joins[] = $query_join_t;
380                                                $tables_as[] = 't0'.$i.$j;
381       
382                                                $last_non_unique = false;
383                                        }
384                                }
385                        }
386                       
387                        $restric_fields = $this->get_fields_from_restrictions($rules);
388                        $n_restrictions = count($restric_fields);
389                       
390                        for ($i = 0; $i < $n_restrictions; $i++)
391                        {
392                                $path = $this->get_tables_by_field($restric_fields[$i], $tables_def);
393                                $n_tables = count($path);
394                               
395                                $last_non_unique = false;
396                                for($j = 0; $j < $n_tables-1; $j += 2)
397                                {
398                                        list($left_t,$left_k) = explode('.',$path[$j+1]);
399                                        list($right_t,$right_k) = explode('.',$path[$j]);
400                                       
401                                        $query_join_t = 'LEFT JOIN '.$left_t.' AS t1'.$i.$j.' ON t1'.
402                                                         $i.$j.'.'.$left_k.'=';
403
404                                        $query_join_reg = 'LEFT JOIN '.$left_t.' AS t[0-9]{2,} ON t[0-9]{2,}'.
405                                                         '\.'.$left_k.'=';
406
407                                        $t = $j-2;
408                                        if ($j == 0)
409                                        {
410                                                $query_join_t .= $path[$j];
411                                                $query_join_reg .= $path[$j];
412                                        }
413                                        else
414                                        {
415                                                $query_join_t .= 't1'.$i.$t.'.'.$right_k;
416                                                $query_join_reg .= 't[0-9]{2,}\.'.$right_k;
417                                        }
418                                       
419                                        $query_restric_joins[] = $query_join_t;
420                                        $tables_as[] = 't1'.$i.$j;
421                                        continue;
422                                       
423                                        /* TODO: The code below detects the double JOIN lines, but
424                                         * the problem is that it doesn't do that correctly, because
425                                         * it should consider the hole path, not just one entry.
426                                         */
427                                        $unique = true;
428                                        if ($n_joins = count($query_restric_joins))
429                                        {
430                                                for($k = 0; $k < $n_joins; $k++)
431                                                {
432                                                        if (ereg($query_join_reg,$query_restric_joins[$k]))
433                                                        {       
434                                                                $unique = false;
435                                                                $last_non_unique = $k; 
436                                                        }
437                                                }
438                                        }
439
440                                        if ($unique)
441                                        {
442                                                if ($j != 0)
443                                                {
444                                                        if ($last_non_unique === false)
445                                                        {
446                                                                $query_join_t .= 't1'.$i.$t.'.'.$right_k;
447                                                        }
448                                                        else
449                                                        {
450                                                                $query_join_t .= $tables_as[$last_non_unique].'.'.$right_k;
451                                                        }
452                                                }
453                                                $query_restric_joins[] = $query_join_t;
454                                                $tables_as[] = 't1'.$i.$j;
455       
456                                                $last_non_unique = false;
457                                        }
458                                }
459                               
460                                list($table,$field_r) = explode('.',$path[$n_tables-1]);
461                               
462                                if ($n_tables > 1)
463                                {
464                                        $t = $j-2;
465                                        $tables_restric[$restric_fields[$i]] = array(
466                                                'table' => 't1'.$i.$t,
467                                                'field' => $field_r
468                                        );
469                                }
470                                else
471                                {
472                                        $tables_restric[$restric_fields[$i]] = array(
473                                                'table' => $table,
474                                                'field' => $field_r
475                                        );
476                                }
477                               
478                        }
479
480                        if ($other)
481                        {                       
482                                foreach($other as $name => $value)
483                                {
484                                        switch($name)
485                                        {
486                                                case 'offset':
487                                                        if(is_int($value))
488                                                        {
489                                                                $query_other[2] = 'OFFSET '.$value;
490                                                        }
491                                                        break;
492                                                         
493                                                case 'limit':
494                                                        if(is_int($value))
495                                                        {
496                                                                $query_other[3] = 'LIMIT '.$value;
497                                                        }
498                                                        break;
499                                                       
500                                                case 'order':
501                                                        $order_fs = explode(',',$value);
502                                                        foreach($order_fs as $order_f)
503                                                        {
504                                                                $query_other[0] = 'ORDER BY '.$fields_translate[$order_f];
505                                                        }
506                                                        break;
507                                                       
508                                                case 'sort':
509                                                        switch($value)
510                                                        {
511                                                                case 'ASC':
512                                                                case 'DESC':
513                                                                        $query_other[1] = $value;
514                                                        }
515                                                        break;
516                                                       
517                                                case 'fields_modifier':
518                                                        switch($value)
519                                                        {
520                                                                case 'COUNT':
521                                                                case 'MAX':
522                                                                        $query_fields_mod = $value;
523                                                        }
524                                                        break;
525                                                                                       
526                                                default:
527                                                        exit('Invalid \'other\' field passed to find in file '.__FILE__.' on line '.__LINE__);
528                                        }
529                                }
530                        }
531
532                        $query_from = array_unique($query_from);
533                        if ($query_fields_mod)
534                        {
535                                $query_select .= ' '.$query_fields_mod.'('.implode(',',$fields).') AS mod';
536                        }
537                        else
538                        {
539                                $query_select .= implode(',',$fields);
540                        }
541                       
542                        @ksort($query_other);
543                       
544                        $query_from_f = ' FROM '.implode(',',$query_from);
545                       
546                        $query = $query_select . $query_from_f;
547                       
548                        if (count($query_fields_joins))
549                        {
550                                $query = ' '.implode("\n",$query_fields_joins);
551                        }
552
553                        if (count($query_restric_joins))
554                        {
555                                $query .= ' '.implode("\n",$query_restric_joins);
556                        }
557
558                        if ($rules)
559                        {
560                                $query .= ' WHERE '.$this->process_restrictions($rules,$tables_restric);
561                        }
562
563                        if (count($query_other))
564                        {
565                                if (!($query_other[1] and !$query_other[0]))
566                                {
567                                        $query .= ' '.@implode(' ',$query_other);
568                                }
569                        }
570
571                        //echo 'Query in Find: "'.$query.'"<br>';
572                        if (!$this->db->query($query))
573                        {
574                                exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
575                        }
576                         
577                        $return = false;
578                        while($this->db->next_record())
579                        {
580                                $return[] = $this->db->row();
581                        }
582                         
583                        return $return;
584                }
585               
586                /*!
587                       
588                        @function get_tables_by_field
589                        @abstract Returns the table wich holds the specified field
590                        @author Raphael Derosso Pereira
591                       
592                        @param string $field  The field to be found
593                        @param array  $tables The array returned by get_db_tables
594                */
595                function get_tables_by_field ($field, &$apptables)
596                {
597                       
598                        $field_parts = explode('.',$field);
599                        $n_fields = count($field_parts);
600                       
601                        $return = array();
602                        $previous = false;
603                        for($i = 0; $i < $n_fields-1; $i++)
604                        {
605                                $actual = $field_parts[$i];
606                                $next = $field_parts[$i+1];
607                               
608                                if (!isset($apptables[$this->tables[$actual]['table']]))
609                                {
610                                        exit('Some unrecognized parameter in '.__FILE__.' on line '.__LINE__.'<br>'.
611                                             'Couldn\'t find '.$this->tables[$actual]['table']);
612                                }
613                               
614                                if (array_key_exists($next,$apptables[$this->tables[$actual]['table']]['fd']))
615                                {
616                                        array_push($return, $this->tables[$actual]['table'].'.'.$next);
617                                        continue;
618                                }
619
620                                if (array_key_exists($next,$this->tables[$actual]))
621                                {
622                                        if (is_array($this->tables[$actual][$next]))
623                                        {
624                                            if (isset($this->tables[$actual][$next]['shortcut']))
625                                                {
626                                                        $next = $this->tables[$actual][$next]['shortcut'];
627                                                        $field_parts[$i+1] = $next;
628                                                }
629                                                else if (isset($this->tables[$actual][$next]['path']))
630                                                {
631                                                        $path = $this->tables[$actual][$next]['path'];
632                                                        $field_parts[$i+1] = $path;
633                                                        $field_parts = array_merge(array_slice($field_parts,0,$i+2),$next,array_slice($field_parts,$i+2,count($field_parts)));
634                                                        $next = $path;
635                                                        $n_fields = count($field_parts);
636                                                }
637                                        }
638
639                                        list($key1, $key2) = explode(',',$this->tables[$actual][$next]);
640                                        array_push($return, $this->tables[$actual]['table'].'.'.$key1, $this->tables[$next]['table'].'.'.$key2);
641                                        continue;
642                                }
643
644                                exit('Invalid field in '.__FILE__.' on line '.__LINE__.'<br>Actual: '.$actual.'<br>Next: '.$next);
645                        }
646                       
647                        return $return;
648                }
649
650                /*!
651               
652                        @function get_fields_from_restrictions
653                        @abstract Returns an array containing the fields inside the restrictions
654                                ignoring the branches
655                        @author Raphael Derosso Pereira
656                       
657                        @param array $restrictions The restrictions
658               
659                */
660                function get_fields_from_restrictions(&$restrictions)
661                {
662                        if (!is_array($restrictions))
663                        {
664                                return null;
665                        }
666                       
667                        $fields = array();
668                       
669                        foreach ($restrictions as $restrict_data)
670                        {
671                                switch($restrict_data['type'])
672                                {
673                                        case 'branch':
674                                                $fields = array_merge($fields, $this->get_fields_from_restrictions($restrict_data['sub_branch']));
675                                                break;
676                                               
677                                        case '=':
678                                        case '!=':     
679                                        case '<=':
680                                        case '<':
681                                        case '>':
682                                        case '>=':
683                                        case 'NULL':
684                                        case 'IN':
685                                        case 'LIKE':
686                                        case 'iLIKE':
687                                        case 'LIKE and ~=':
688                                        case 'NOT NULL':
689                                        case 'NOT IN':
690                                        case 'NOT LIKE':
691                                        case 'NOT iLIKE':
692                                                array_push($fields, $restrict_data['field']);
693                                                break;
694                                               
695                                        default:
696                                                exit('Error in '.__FILE__.' on '.__LINE__.'<br>The restriction type passed was: '.$restrict_data['type']);                                     
697                                }
698                        }
699                       
700                        return $fields;
701                }
702               
703                /*!
704               
705                        @function process_restrictions
706                        @abstract Takes the restrictions array and returns an string
707                                that corresponds to the array
708                        @author Raphael Derosso Pereira
709                       
710                        @param array $restriction The restriction array
711                        @param array $associative_tree The
712                        @param string $logic_type The type of the logic that should be
713                                used to join the fields
714               
715                */
716                function process_restrictions(&$restrictions, &$associative_tree, $join_type='AND' )
717                {
718                        foreach($restrictions as $restrict_index => $restrict_data)
719                        {
720                                switch($restrict_data['type'])
721                                {
722                                        case 'branch':
723                                                $return_t[] = ' ('.$this->process_restrictions($restrict_data['sub_branch'],$associative_tree,$restrict_data['value']).') ';
724                                                break;
725                                               
726                                        case 'iLIKE':
727                                                $return_t[] = 'UPPER( '.$associative_tree[$restrict_data['field']]['table'].'.'.
728                                                              $associative_tree[$restrict_data['field']]['field'].') LIKE UPPER(\''.$restrict_data['value'].'\')';
729                                                break;
730                                       
731                                        case 'LIKE and ~=':
732                                                $return_t[] = 'UPPER( '.$associative_tree[$restrict_data['field']]['table'].'.'.
733                                                              $associative_tree[$restrict_data['field']]['field'].') LIKE UPPER(\''."%".$restrict_data['value']."%".'\')';     
734                                                break;
735                                       
736                                        case 'NOT iLIKE':
737                                                $return_t[] = 'UPPER( '.$associative_tree[$restrict_data['field']]['table'].'.'.
738                                                              $associative_tree[$restrict_data['field']]['field'].') NOT LIKE UPPER(\''.$restrict_data['value'].'\')';
739                                                break;
740                                               
741                                        case '=':
742                                        case '!=':     
743                                        case '<=':
744                                        case '<':
745                                        case '>':
746                                        case '>=':
747                                        case 'NULL':
748                                        case 'LIKE':
749                                        case 'NOT NULL':
750                                        case 'NOT LIKE':
751                                                $return_t[] = $associative_tree[$restrict_data['field']]['table'].'.'.
752                                                              $associative_tree[$restrict_data['field']]['field'].' '.$restrict_data['type'].' \''.$restrict_data['value'].'\'';
753                                                break;
754                                       
755                                        case 'IN':
756                                        case 'NOT IN':
757                                                $return_t[] = $associative_tree[$restrict_data['field']]['table'].'.'.
758                                                              $associative_tree[$restrict_data['field']]['field'].' '.$restrict_data['type'].' '.$restrict_data['value'];
759                                                break;
760                                }
761                               
762                        }
763                       
764                        if (count($return_t) > 1)
765                        {
766                                return(implode(' '.$join_type.' ',$return_t));
767                        }
768                       
769                        return $return_t[0];
770                }
771
772
773                /*********************************************************************\
774                 *                     Data Management                               *
775                \*********************************************************************/
776
777                /*!
778                 @function get_fields
779                 @abstract Returns all the fields that a catalog can have
780                        on an array
781                 @author Raphael Derosso Pereira
782                     
783                 @param bool $all Return filled with True or False?
784                 
785                */
786                function get_fields($all=false)
787                {
788                        if (!is_bool($all))
789                        {
790                                if (is_object($GLOBALS['phpgw']->log))
791                                {
792                                        $GLOBALS['phpgw']->log->message(array(
793                                                'text' => 'F-BadcontactcenterParam, get_contact_fields parameter must be boolean.',
794                                                'line' => __LINE__,
795                                                'file' => __FILE__));
796                                       
797                                        $GLOBALS['phpgw']->log->commit();
798                                }
799                                else {
800                                        exit('Argument Error on: <br>File:'.__FILE__.'<br>Line:'.__LINE__.'<br>');
801                                }
802                        }
803                       
804                        if ($all)
805                        {
806                                return $this->fields;
807                        }
808                        else
809                        {
810                                $fields_temp = $this->fields;
811                               
812                                foreach(array_keys($fields_temp) as $field)
813                                {
814                                        $fields_temp[$field] = false;
815                                }
816                               
817                                return $fields_temp;
818                        }
819                }
820
821
822        /*********************************************************************\
823                 *                Methods to Access Shared Catalog Data              *
824                \*********************************************************************/
825
826
827                /*!
828
829                        @function get_all_countries
830                        @abstract Returns all the countries the API provides
831                        @author Raphael Derosso Pereira
832               
833                */
834                function get_all_countries()
835                {
836                        $obj = CreateObject('phpgwapi.country');
837                        $countries = $obj->country_array;
838                        unset($countries['  ']);
839
840                        foreach($countries as $code => $name)
841                        {
842                                $name = lang($name);
843                                $countries[$code] = $name{0} . strtolower(substr($name, 1));
844                        }
845
846                        return $countries;
847                }
848
849                /*!
850
851                        @function get_all_states
852                        @abstract Returns all the states for the given country
853                        @author Raphael Derosso Pereira
854
855                        @param $id_country The ID of the Country that contains the requested States
856
857                */
858                function get_all_states($id_country)
859                {
860                        $id_states = $this->find(array('state.id_state', 'state.state_name'),
861                                                 array(
862                                                                                0 => array(
863                                                                                        'field' => 'state.id_country',
864                                                                                        'type'  => '=',
865                                                                                        'value' => $id_country
866                                                                                )
867                                                 ),
868                                                                         array(
869                                                                                'order' => 'state.state_name',
870                                                                                'sort'  => 'ASC'
871                                                                         ));
872
873                        if (!is_array($id_states) || count($id_states) == 0)
874                        {
875                                return false;
876                        }
877
878                        $result = false;
879                        foreach($id_states as $id_state)
880                        {
881                                $state =& CreateObject('contactcenter.so_state', $id_state['id_state']);
882                               
883                                $result[$id_state['id_state']]['id_state']   = $state->get_id();
884                                $result[$id_state['id_state']]['id_country'] = $state->get_id_country();
885                                $result[$id_state['id_state']]['name']       = $state->get_state_name();
886                                $result[$id_state['id_state']]['symbol']     = $state->get_state_symbol();
887                        }
888
889                        return $result;
890                }
891
892                /*!
893
894                        @function get_all_cities
895                        @abstract Returns all the cities for the given state
896                        @author Raphael Derosso Pereira
897
898                        #param $id_country The ID of the Country that contains the requested Cities
899                        @param $id_state   The ID of the State that contains the requested Cities
900
901                */
902                function get_all_cities($id_country, $id_state=false)
903                {
904                        if ($id_state)
905                        {
906                                $id_cities = $this->find(array('city.id_city', 'city.city_name'),
907                                                                                 array(
908                                                                                        0 => array(
909                                                                                                'field' => 'city.id_state',
910                                                                                                'type'  => '=',
911                                                                                                'value' => $id_state
912                                                                                        )
913                                                                                 ),
914                                                                                 array(
915                                                                                        'order' => 'city.city_name',
916                                                                                        'sort'  => 'ASC'
917                                                                                 ));
918                        }
919                        else
920                        {
921                                $id_cities = $this->find(array('city.id_city', 'city.city_name'),
922                                                                                 array(
923                                                                                        0 => array(
924                                                                                                'field' => 'city.id_country',
925                                                                                                'type'  => '=',
926                                                                                                'value' => $id_country
927                                                                                        )
928                                                                                 ),
929                                                                                 array(
930                                                                                        'order' => 'city.city_name',
931                                                                                        'sort'  => 'ASC'
932                                                                                 ));
933                        }
934
935                        if (!is_array($id_cities) || count($id_cities) == 0)
936                        {
937                                return false;
938                        }
939
940                        $result = false;
941                        foreach($id_cities as $id_city)
942                        {
943                                $city =& CreateObject('contactcenter.so_city', $id_city['id_city']);
944                               
945                                $result[$id_city['id_city']]['id_city']      = $city->get_id();
946                                $result[$id_city['id_city']]['id_country']   = $city->get_id_country();
947                                $result[$id_city['id_city']]['id_state']     = $city->get_id_state();
948                                $result[$id_city['id_city']]['name']         = $city->get_city_name();
949                                $result[$id_city['id_city']]['timezone']     = $city->get_city_timezone();
950                                $result[$id_city['id_city']]['geo_location'] = $city->get_city_geo_location();
951                        }
952
953                        return $result;
954                }
955
956
957                /*********************************************************************\
958                 *           Methods to Insert/Update Shared Catalog Data            *
959                \*********************************************************************/
960
961                /*!
962                        @function add_city
963                        @abstract Inserts a new City in the DB
964                        @author Raphael Derosso Pereira
965
966                        @params array $city_info The city information:
967                                $city_info = array(
968                                        'id_state'  => (int),
969                                        'id_country' => (int),     MANDATORY
970                                        'city_name' => (str),      MANDATORY
971                                        'city_time_zone' => (int),
972                                        'city_geo_location' => (str),
973                                );
974
975                        @return int City ID
976
977                */
978                function add_city($city_info)
979                {
980                        if(!is_array($city_info) || !count($city_info) || !$city_info['city_name'] || !$city_info['id_country'])
981                        {
982                                exit(print_r(array(
983                                        'file' => __FILE__,
984                                        'line' => __LINE__,
985                                        'msg' => lang('Wrong parameters'),
986                                        'status' => 'fatal',
987                                )));
988                        }
989                       
990                        $permissions = $this->security->get_permissions();
991
992                        if (!is_array($permissions['cities']) || array_search('c', $permissions['cities']) === false)
993                        {
994                                exit(print_r(array(
995                                        'file' => __FILE__,
996                                        'line' => __LINE__,
997                                        'msg' => lang('You does not have sufficient privileges. Aborted!'),
998                                        'status' => 'aborted'
999                                )));
1000                        }
1001
1002                        /* Search for cities with the same data */
1003                        $what = array('city.id_city');
1004                        $rules = array(
1005                                0 => array(
1006                                        'field' => 'city.city_name',
1007                                        'type'  => 'iLIKE',
1008                                        'value' => $city_info['city_name']
1009                                ),
1010                                1 => array(
1011                                        'field' => 'city.id_country',
1012                                        'type'  => 'iLIKE',
1013                                        'value' => $city_info['id_country']
1014                                )
1015                        );
1016
1017                        if (isset($city_info['id_state']))
1018                        {
1019                                array_push($rules, array(
1020                                        'field' => 'city.id_state',
1021                                        'type'  => '=',
1022                                        'value' => $city_info['id_state']
1023                                ));
1024                        }
1025
1026                        $result = $this->find($what, $rules);
1027
1028                        if (is_array($result) and count($result))
1029                        {
1030                                return $result[0]['id_city'];
1031                        }
1032
1033                        $city =& CreateObject('contactcenter.so_city');
1034
1035                        $city->set_id_country($city_info['id_country']);
1036                        $city->set_city_name($city_info['city_name']);
1037                        isset($city_info['id_state']) ? $city->set_id_state($city_info['id_state']) : null;
1038                        isset($city_info['city_timezone']) ? $city->set_city_timezone($city_info['city_timezone']) : null;
1039                        isset($city_info['city_geo_location']) ? $city->set_city_geo_location($city_info['city_geo_location']) : null;
1040
1041                        $city->commit();
1042                        $id = $city->get_id();
1043
1044                        return $id;
1045                }
1046
1047
1048                /*!
1049                        @function add_state
1050                        @abstract Inserts a new State in the DB
1051                        @author Raphael Derosso Pereira
1052
1053                        @params array $state_info The state information:
1054                                $state_info = array(
1055                                        'id_country' => (int),     MANDATORY
1056                                        'state_name' => (str),      MANDATORY
1057                                        'state_symbol' => (str),
1058                                );
1059
1060                        @return int State ID
1061
1062                */
1063                function add_state($state_info)
1064                {
1065                        if(!is_array($state_info) || !count($state_info) || !$state_info['state_name'] || !$state_info['id_country'])
1066                        {
1067                                exit(print_r(array(
1068                                        'msg' => lang('Wrong parameters'),
1069                                        'status' => 'fatal'
1070                                )));
1071                        }
1072                       
1073                        $permissions = $this->security->get_permissions();
1074
1075                        if (!is_array($permissions['states']) || array_search('c', $permissions['states']) === false)
1076                        {
1077                                exit(print_r(array(
1078                                        'msg' => lang('You does not have sufficient privileges. Aborted!'),
1079                                        'status' => 'aborted'
1080                                )));
1081                        }
1082
1083                        /* Search for states with the same data */
1084                        $what = array('state.id_state');
1085                        $rules = array(
1086                                0 => array(
1087                                        'field' => 'state.state_name',
1088                                        'type'  => 'iLIKE',
1089                                        'value' => $state_info['state_name']
1090                                ),
1091                                1 => array(
1092                                        'field' => 'state.id_country',
1093                                        'type'  => 'iLIKE',
1094                                        'value' => $state_info['id_country']
1095                                )
1096                        );
1097                       
1098                        $result = $this->find($what, $rules);
1099
1100                        if (is_array($result) and count($result))
1101                        {
1102                                return $result[0]['id_state'];
1103                        }
1104
1105                        $state =& CreateObject('contactcenter.so_state');
1106
1107                        $state->set_id_country($state_info['id_country']);
1108                        $state->set_state_name($state_info['state_name']);
1109                        isset($state_info['state_symbol']) ? $state->set_state_symbol($state_info['state_symbol']) : null;
1110
1111                        $state->commit();
1112                        $id = $state->get_id();
1113
1114                        return $id;
1115                }
1116
1117        }
1118?>
Note: See TracBrowser for help on using the repository browser.