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

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

Importacao inicial do Expresso do Serpro

Line 
1<?php
2  /***************************************************************************\
3  * eGroupWare - Contacts Center                                              *
4  * http://www.egroupware.org                                                 *
5  * Written by:                                                               *
6  *  - Raphael Derosso Pereira <raphaelpereira@users.sourceforge.net>         *
7  *  sponsored by Thyamad - http://www.thyamad.com
8  * ------------------------------------------------------------------------- *
9  *  This program is free software; you can redistribute it and/or modify it  *
10  *  under the terms of the GNU General Public License as published by the    *
11  *  Free Software Foundation; either version 2 of the License, or (at your   *
12  *  option) any later version.                                               *
13  \***************************************************************************/
14
15        class 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 'NOT NULL':
688                                        case 'NOT IN':
689                                        case 'NOT LIKE':
690                                        case 'NOT iLIKE':
691                                                array_push($fields, $restrict_data['field']);
692                                                break;
693                                               
694                                        default:
695                                                exit('Error in '.__FILE__.' on '.__LINE__.'<br>The restriction type passed was: '.$restrict_data['type']);                                     
696                                }
697                        }
698                       
699                        return $fields;
700                }
701               
702                /*!
703               
704                        @function process_restrictions
705                        @abstract Takes the restrictions array and returns an string
706                                that corresponds to the array
707                        @author Raphael Derosso Pereira
708                       
709                        @param array $restriction The restriction array
710                        @param array $associative_tree The
711                        @param string $logic_type The type of the logic that should be
712                                used to join the fields
713               
714                */
715                function process_restrictions(&$restrictions, &$associative_tree, $join_type='AND' )
716                {
717                        foreach($restrictions as $restrict_index => $restrict_data)
718                        {
719                                switch($restrict_data['type'])
720                                {
721                                        case 'branch':
722                                                $return_t[] = ' ('.$this->process_restrictions($restrict_data['sub_branch'],$associative_tree,$restrict_data['value']).') ';
723                                                break;
724                                               
725                                        case 'iLIKE':
726                                                $return_t[] = 'UPPER( '.$associative_tree[$restrict_data['field']]['table'].'.'.
727                                                              $associative_tree[$restrict_data['field']]['field'].') LIKE UPPER(\''.$restrict_data['value'].'\')';
728                                                break;
729                                       
730                                        case 'NOT iLIKE':
731                                                $return_t[] = 'UPPER( '.$associative_tree[$restrict_data['field']]['table'].'.'.
732                                                              $associative_tree[$restrict_data['field']]['field'].') NOT LIKE UPPER(\''.$restrict_data['value'].'\')';
733                                                break;
734                                               
735                                        case '=':
736                                        case '!=':     
737                                        case '<=':
738                                        case '<':
739                                        case '>':
740                                        case '>=':
741                                        case 'NULL':
742                                        case 'LIKE':
743                                        case 'NOT NULL':
744                                        case 'NOT LIKE':
745                                                $return_t[] = $associative_tree[$restrict_data['field']]['table'].'.'.
746                                                              $associative_tree[$restrict_data['field']]['field'].' '.$restrict_data['type'].' \''.$restrict_data['value'].'\'';
747                                                break;
748                                       
749                                        case 'IN':
750                                        case 'NOT IN':
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                               
756                        }
757                       
758                        if (count($return_t) > 1)
759                        {
760                                return(implode(' '.$join_type.' ',$return_t));
761                        }
762                       
763                        return $return_t[0];
764                }
765
766
767                /*********************************************************************\
768                 *                     Data Management                               *
769                \*********************************************************************/
770
771                /*!
772                 @function get_fields
773                 @abstract Returns all the fields that a catalog can have
774                        on an array
775                 @author Raphael Derosso Pereira
776                     
777                 @param bool $all Return filled with True or False?
778                 
779                */
780                function get_fields($all=false)
781                {
782                        if (!is_bool($all))
783                        {
784                                if (is_object($GLOBALS['phpgw']->log))
785                                {
786                                        $GLOBALS['phpgw']->log->message(array(
787                                                'text' => 'F-BadcontactcenterParam, get_contact_fields parameter must be boolean.',
788                                                'line' => __LINE__,
789                                                'file' => __FILE__));
790                                       
791                                        $GLOBALS['phpgw']->log->commit();
792                                }
793                                else {
794                                        exit('Argument Error on: <br>File:'.__FILE__.'<br>Line:'.__LINE__.'<br>');
795                                }
796                        }
797                       
798                        if ($all)
799                        {
800                                return $this->fields;
801                        }
802                        else
803                        {
804                                $fields_temp = $this->fields;
805                               
806                                foreach(array_keys($fields_temp) as $field)
807                                {
808                                        $fields_temp[$field] = false;
809                                }
810                               
811                                return $fields_temp;
812                        }
813                }
814
815
816        /*********************************************************************\
817                 *                Methods to Access Shared Catalog Data              *
818                \*********************************************************************/
819
820
821                /*!
822
823                        @function get_all_countries
824                        @abstract Returns all the countries the API provides
825                        @author Raphael Derosso Pereira
826               
827                */
828                function get_all_countries()
829                {
830                        $obj = CreateObject('phpgwapi.country');
831                        $countries = $obj->country_array;
832                        unset($countries['  ']);
833
834                        foreach($countries as $code => $name)
835                        {
836                                $name = lang($name);
837                                $countries[$code] = $name{0} . strtolower(substr($name, 1));
838                        }
839
840                        return $countries;
841                }
842
843                /*!
844
845                        @function get_all_states
846                        @abstract Returns all the states for the given country
847                        @author Raphael Derosso Pereira
848
849                        @param $id_country The ID of the Country that contains the requested States
850
851                */
852                function get_all_states($id_country)
853                {
854                        $id_states = $this->find(array('state.id_state', 'state.state_name'),
855                                                 array(
856                                                                                0 => array(
857                                                                                        'field' => 'state.id_country',
858                                                                                        'type'  => '=',
859                                                                                        'value' => $id_country
860                                                                                )
861                                                 ),
862                                                                         array(
863                                                                                'order' => 'state.state_name',
864                                                                                'sort'  => 'ASC'
865                                                                         ));
866
867                        if (!is_array($id_states) || count($id_states) == 0)
868                        {
869                                return false;
870                        }
871
872                        $result = false;
873                        foreach($id_states as $id_state)
874                        {
875                                $state =& CreateObject('contactcenter.so_state', $id_state['id_state']);
876                               
877                                $result[$id_state['id_state']]['id_state']   = $state->get_id();
878                                $result[$id_state['id_state']]['id_country'] = $state->get_id_country();
879                                $result[$id_state['id_state']]['name']       = $state->get_state_name();
880                                $result[$id_state['id_state']]['symbol']     = $state->get_state_symbol();
881                        }
882
883                        return $result;
884                }
885
886                /*!
887
888                        @function get_all_cities
889                        @abstract Returns all the cities for the given state
890                        @author Raphael Derosso Pereira
891
892                        #param $id_country The ID of the Country that contains the requested Cities
893                        @param $id_state   The ID of the State that contains the requested Cities
894
895                */
896                function get_all_cities($id_country, $id_state=false)
897                {
898                        if ($id_state)
899                        {
900                                $id_cities = $this->find(array('city.id_city', 'city.city_name'),
901                                                                                 array(
902                                                                                        0 => array(
903                                                                                                'field' => 'city.id_state',
904                                                                                                'type'  => '=',
905                                                                                                'value' => $id_state
906                                                                                        )
907                                                                                 ),
908                                                                                 array(
909                                                                                        'order' => 'city.city_name',
910                                                                                        'sort'  => 'ASC'
911                                                                                 ));
912                        }
913                        else
914                        {
915                                $id_cities = $this->find(array('city.id_city', 'city.city_name'),
916                                                                                 array(
917                                                                                        0 => array(
918                                                                                                'field' => 'city.id_country',
919                                                                                                'type'  => '=',
920                                                                                                'value' => $id_country
921                                                                                        )
922                                                                                 ),
923                                                                                 array(
924                                                                                        'order' => 'city.city_name',
925                                                                                        'sort'  => 'ASC'
926                                                                                 ));
927                        }
928
929                        if (!is_array($id_cities) || count($id_cities) == 0)
930                        {
931                                return false;
932                        }
933
934                        $result = false;
935                        foreach($id_cities as $id_city)
936                        {
937                                $city =& CreateObject('contactcenter.so_city', $id_city['id_city']);
938                               
939                                $result[$id_city['id_city']]['id_city']      = $city->get_id();
940                                $result[$id_city['id_city']]['id_country']   = $city->get_id_country();
941                                $result[$id_city['id_city']]['id_state']     = $city->get_id_state();
942                                $result[$id_city['id_city']]['name']         = $city->get_city_name();
943                                $result[$id_city['id_city']]['timezone']     = $city->get_city_timezone();
944                                $result[$id_city['id_city']]['geo_location'] = $city->get_city_geo_location();
945                        }
946
947                        return $result;
948                }
949
950
951                /*********************************************************************\
952                 *           Methods to Insert/Update Shared Catalog Data            *
953                \*********************************************************************/
954
955                /*!
956                        @function add_city
957                        @abstract Inserts a new City in the DB
958                        @author Raphael Derosso Pereira
959
960                        @params array $city_info The city information:
961                                $city_info = array(
962                                        'id_state'  => (int),
963                                        'id_country' => (int),     MANDATORY
964                                        'city_name' => (str),      MANDATORY
965                                        'city_time_zone' => (int),
966                                        'city_geo_location' => (str),
967                                );
968
969                        @return int City ID
970
971                */
972                function add_city($city_info)
973                {
974                        if(!is_array($city_info) || !count($city_info) || !$city_info['city_name'] || !$city_info['id_country'])
975                        {
976                                exit(print_r(array(
977                                        'file' => __FILE__,
978                                        'line' => __LINE__,
979                                        'msg' => lang('Wrong parameters'),
980                                        'status' => 'fatal',
981                                )));
982                        }
983                       
984                        $permissions = $this->security->get_permissions();
985
986                        if (!is_array($permissions['cities']) || array_search('c', $permissions['cities']) === false)
987                        {
988                                exit(print_r(array(
989                                        'file' => __FILE__,
990                                        'line' => __LINE__,
991                                        'msg' => lang('You does not have sufficient privileges. Aborted!'),
992                                        'status' => 'aborted'
993                                )));
994                        }
995
996                        /* Search for cities with the same data */
997                        $what = array('city.id_city');
998                        $rules = array(
999                                0 => array(
1000                                        'field' => 'city.city_name',
1001                                        'type'  => 'iLIKE',
1002                                        'value' => $city_info['city_name']
1003                                ),
1004                                1 => array(
1005                                        'field' => 'city.id_country',
1006                                        'type'  => 'iLIKE',
1007                                        'value' => $city_info['id_country']
1008                                )
1009                        );
1010
1011                        if (isset($city_info['id_state']))
1012                        {
1013                                array_push($rules, array(
1014                                        'field' => 'city.id_state',
1015                                        'type'  => '=',
1016                                        'value' => $city_info['id_state']
1017                                ));
1018                        }
1019
1020                        $result = $this->find($what, $rules);
1021
1022                        if (is_array($result) and count($result))
1023                        {
1024                                return $result[0]['id_city'];
1025                        }
1026
1027                        $city =& CreateObject('contactcenter.so_city');
1028
1029                        $city->set_id_country($city_info['id_country']);
1030                        $city->set_city_name($city_info['city_name']);
1031                        isset($city_info['id_state']) ? $city->set_id_state($city_info['id_state']) : null;
1032                        isset($city_info['city_timezone']) ? $city->set_city_timezone($city_info['city_timezone']) : null;
1033                        isset($city_info['city_geo_location']) ? $city->set_city_geo_location($city_info['city_geo_location']) : null;
1034
1035                        $city->commit();
1036                        $id = $city->get_id();
1037
1038                        return $id;
1039                }
1040
1041
1042                /*!
1043                        @function add_state
1044                        @abstract Inserts a new State in the DB
1045                        @author Raphael Derosso Pereira
1046
1047                        @params array $state_info The state information:
1048                                $state_info = array(
1049                                        'id_country' => (int),     MANDATORY
1050                                        'state_name' => (str),      MANDATORY
1051                                        'state_symbol' => (str),
1052                                );
1053
1054                        @return int State ID
1055
1056                */
1057                function add_state($state_info)
1058                {
1059                        if(!is_array($state_info) || !count($state_info) || !$state_info['state_name'] || !$state_info['id_country'])
1060                        {
1061                                exit(print_r(array(
1062                                        'msg' => lang('Wrong parameters'),
1063                                        'status' => 'fatal'
1064                                )));
1065                        }
1066                       
1067                        $permissions = $this->security->get_permissions();
1068
1069                        if (!is_array($permissions['states']) || array_search('c', $permissions['states']) === false)
1070                        {
1071                                exit(print_r(array(
1072                                        'msg' => lang('You does not have sufficient privileges. Aborted!'),
1073                                        'status' => 'aborted'
1074                                )));
1075                        }
1076
1077                        /* Search for states with the same data */
1078                        $what = array('state.id_state');
1079                        $rules = array(
1080                                0 => array(
1081                                        'field' => 'state.state_name',
1082                                        'type'  => 'iLIKE',
1083                                        'value' => $state_info['state_name']
1084                                ),
1085                                1 => array(
1086                                        'field' => 'state.id_country',
1087                                        'type'  => 'iLIKE',
1088                                        'value' => $state_info['id_country']
1089                                )
1090                        );
1091                       
1092                        $result = $this->find($what, $rules);
1093
1094                        if (is_array($result) and count($result))
1095                        {
1096                                return $result[0]['id_state'];
1097                        }
1098
1099                        $state =& CreateObject('contactcenter.so_state');
1100
1101                        $state->set_id_country($state_info['id_country']);
1102                        $state->set_state_name($state_info['state_name']);
1103                        isset($state_info['state_symbol']) ? $state->set_state_symbol($state_info['state_symbol']) : null;
1104
1105                        $state->commit();
1106                        $id = $state->get_id();
1107
1108                        return $id;
1109                }
1110
1111        }
1112?>
Note: See TracBrowser for help on using the repository browser.