source: sandbox/2.2.0.2/contactcenter/inc/class.bo_people_catalog.inc.php @ 4477

Revision 4477, 49.8 KB checked in by airton, 13 years ago (diff)

Ticket #1912 - Permitir a criação de contatos sem email

  • 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        include_once('class.abo_catalog.inc.php');
16        class bo_people_catalog extends abo_catalog
17        {
18                       
19                var $fields = array(
20                        'id_contact'    => true,
21                        'status'        => true,
22                        'photo'         => true,
23                        'alias'         => true,
24                        'prefix'        => true,
25                        'given_names'   => true,
26                        'family_names'  => true,
27                        'names_ordered' => true,
28                        'suffix'        => true,
29                        'birthdate'     => true,
30                        'sex'           => true,
31                        'pgp_key'       => true,
32                        'notes'         => true,
33                        'corporate_name'=> true,
34                        'job_title'     => true,
35                        'department'    => true,
36                        'web_page'              => true,
37                        /* Array fields */
38                        'companies'     => true,
39                        'relations'     => true,
40                        'addresses'     => true,
41                        'connections'   => true
42                );
43       
44                /*!
45               
46                 @function bo_people_catalog
47                 @abstract Constructor
48                 @author Raphael Derosso Pereira
49                 
50                */
51                function bo_people_catalog()
52                {
53                        $this->init();
54                }
55
56                /*!
57                        @function find
58                        @abstract Find function for this catalog
59                        @author Raphael Derosso Pereira
60
61                */
62                function find($what, $rules=false, $other=false)
63                {
64                        if (is_array($what) and count($what))
65                        {
66                                $found = false;
67                               
68                                foreach ($what as $value)
69                                {
70                                        if (strpos($value, 'contact') === 0)
71                                        {
72                                                $found = true;
73                                        }
74                                }
75                               
76                                if (!$found)
77                                {
78                                        return $this->sql_find($what, $rules, $other);
79                                }
80                               
81                                if ($rules and is_array($rules))
82                                {
83                                        array_push($rules, array(
84                                                'field' => 'contact.id_owner',
85                                                'type'  => '=',
86                                                'value' => $GLOBALS['phpgw_info']['user']['account_id']
87                                        ));
88                                }
89                                else
90                                {
91                                        $rules = array(
92                                                0 => array(
93                                                        'field' => 'contact.id_owner',
94                                                        'type'  => '=',
95                                                        'value' => $GLOBALS['phpgw_info']['user']['account_id']
96                                                )
97                                        );
98                                }
99                        }
100
101                        return $this->sql_find($what, $rules, $other);
102                }
103                       
104                /*!
105               
106                 @function get_single_entry
107                 @abstract Returns all information requested about one contact
108                 @author Raphael Derosso Pereira
109                     
110                 @param integer $id_contact The contact ID
111                 @param array $fields The array returned by get_fields whith true
112                        on the fields to be taken.
113                       
114                */
115                function get_single_entry ( $id_contact, $fields )
116                {       
117                        if (!is_array($fields))
118                        {
119                                if (is_object($GLOBALS['phpgw']->log))
120                                {
121                                        $GLOBALS['phpgw']->log->message(array(
122                                                'text' => 'F-BadcontactcenterParam, wrong get_single_entry parameters type.',
123                                                'line' => __LINE__,
124                                                'file' => __FILE__));
125                                       
126                                        $GLOBALS['phpgw']->log->commit();
127                                }
128                                else
129                                {
130                                        exit('Argument Error on: <br>File:'.__FILE__.'<br>Line:'.__LINE__.'<br>');
131                                }
132                        }
133                       
134                        // Verify permissions
135                        $permissions = $this->security->get_permissions('entry', $id_contact);
136
137                        if (!$permissions['read'])
138                        {
139                                return false;
140                        }
141                       
142                        //$contact_data = $this->fields;
143       
144                        $contact = CreateObject('contactcenter.so_contact', $id_contact);
145
146                        foreach ($fields as $field => $trueness)
147                        {
148                                if (!$trueness)
149                                {
150                                        //unset($contact_data[$field]);
151                                        continue;
152                                }
153                                       
154                                switch ($field)
155                                {
156                                        case 'companies':
157                                                $companies = $this->get_companies($id_contact,$trueness);
158
159                                                if (is_array($companies) and count($companies))
160                                                {
161                                                        $contact_data['companies'] = $companies;
162                                                }
163                                                break;
164                                       
165                                        case 'relations':
166                                                $relations = $this->get_relations($id_contact,$trueness);
167
168                                                if (is_array($relations) and count($relations))
169                                                {
170                                                        $contact_data['relations'] = $relations;
171                                                }
172                                                break;
173                                       
174                                        case 'addresses':
175                                                $addresses = $this->get_addresses($id_contact,$trueness);
176
177                                                if (is_array($addresses) and count($addresses))
178                                                {
179                                                        $contact_data['addresses'] = $addresses;
180                                                }
181                                                break;
182                                       
183                                        case 'connections':
184                                                $connections = $this->get_connections($id_contact,$trueness);
185
186                                                if (is_array($connections) and count($connections))
187                                                {
188                                                        $contact_data['connections'] = $connections;
189                                                }
190                                                break;
191                                       
192                                        case 'prefix':
193                                                $id = $contact->get_prefix();
194                                                if ($id)
195                                                {
196                                                        $prefix = CreateObject('contactcenter.so_prefix', $id);
197                                                        $contact_data['id_prefix'] = $id;
198                                                        $contact_data['prefix'] = $prefix->get_prefix(); 
199                                                }
200                                                break;
201                                               
202                                        case 'suffix':
203                                                $id = $contact->get_suffix();
204                                                if ($id)
205                                                {
206                                                        $suffix = CreateObject('contactcenter.so_suffix', $id);
207                                                        $contact_data['id_suffix'] = $id;
208                                                        $contact_data['suffix'] = $suffix->get_suffix(); 
209                                                }
210                                                break;
211                                               
212                                        case 'status':
213                                                $id = $contact->get_status();
214                                                if ($id)
215                                                {
216                                                        $status = CreateObject('contactcenter.so_status', $id);
217                                                        $contact_data['id_status'] = $id;
218                                                        $contact_data['status'] = $status->get_status(); 
219                                                }
220                                                break;
221                                               
222                                        default:
223                                                //$func_name = 'contact->get_'.$field;
224                                                //$contact_data[$field] = $this->$func_name();
225                                                $contact_data[$field] = $contact->get_field($field);
226                                                break;
227                                }
228                        }
229                       
230                        if (!is_array($contact_data))
231                        {
232                                return false;
233                        }
234                       
235                        return $contact_data;
236                }
237       
238                /*!
239                 
240                 @function get_multiple_entries
241                 @abstract Returns multiple Contacts data into one array
242                 @author Raphael Derosso Pereira
243
244                 @param array $id_contacts The Contacts IDs
245                 @param array $fields The Contacts fields to be retrieved
246                 @param array $other_data Other informations. The format is:
247                        $other_data = array(
248                                'offset'    => <offset>,
249                                'limit'     => <max_num_returns>,
250                                'sort'      => <sort>,
251                                'order_by'  => <order by>
252                        );
253               
254                */
255                function get_multiple_entries ( $id_contacts, $fields, $other_data = false )
256                {
257                        if (!is_array($id_contacts) or !is_array($fields) or ($other_data != false and !is_array($other_data)))
258                        {
259                                if (is_object($GLOBALS['phpgw']->log))
260                                {
261                                        $GLOBALS['phpgw']->log->message(array(
262                                                'text' => 'F-BadcontactcenterParam, wrong get_multiple_entry parameter type.',
263                                                'line' => __LINE__,
264                                                'file' => __FILE__));
265                                       
266                                        $GLOBALS['phpgw']->log->commit();
267                                }
268                                else {
269                                        exit('Argument Error on: <br>File:'.__FILE__.'<br>Line:'.__LINE__.'<br>');
270                                }
271                        }
272                       
273                        $contacts = array();
274       
275                        if ($other_data)
276                        {
277                                //TODO
278                        }
279       
280                        foreach ($id_contacts as $id)
281                        {
282                                $contact = $this->get_single_entry($id,$fields);
283                                if ($contact)
284                                {
285                                        $contacts[$id] = $contact;
286                                }
287                        }
288                       
289                        return $contacts;
290                }
291
292                /*!
293               
294                        @function get_all_entries_ids
295                        @abstract Returns the IDs of all the entries in this catalog
296                        @author Raphael Derosso Pereira
297
298                */
299                function get_all_entries_ids ()
300                {
301                        $search_fields = array('contact.id_contact', 'contact.names_ordered');
302                        $search_other  = array('order' => 'contact.names_ordered');
303
304                        $result_i = $this->find($search_fields, null, $search_other);
305
306                        if (is_array($result_i) and count($result_i))
307                        {
308                                $result = array();
309                                foreach($result_i as $result_part)
310                                {
311                                        $result[] = $result_part['id_contact'];
312                                }
313
314                                return $result;
315                        }
316
317                        return null;
318                }
319       
320                /*!
321               
322                        @function get_relations
323                        @abstract Returns the IDs of all Contacts relations
324                        @author Raphael Derosso Pereira
325                 
326                        @param integer $id_contact The Contact ID
327                       
328                        @return The following array:
329                                 $return = array(
330                                        'relation1' => array(
331                                                'id_relation'     => <id_relation>,
332                                                'id_type'         => <id_type>,
333                                                'type'            => '<type_name>',
334                                                'is_subordinated' => <trueness>
335                                        ),
336                                        'relation2' => array(...),
337                                        ...
338                                 );
339                 
340                */
341                function get_relations ($id_contact,$extra=false)
342                {
343                        $contact = CreateObject('contactcenter.so_contact', $id_contact);
344                        $relations = $contact->get_relations();
345               
346                        $count = 1;     
347                        foreach($relations as $id => $type)
348                        {
349                                $relation = CreateObject('contactcenter.so_contact_relation_type', $type);
350                               
351                                if ($extra === 'subordinated' and $relation->get_is_subordinated())
352                                {
353                                        $return['relation'.$count]['id_relation'] = $id;
354                                        $return['relation'.$count]['id_type'] = $type;
355                                        $return['relation'.$count]['type'] = $relation->get_type_name();
356                                        $return['relation'.$count]['is_subordinated'] = $relation->get_is_subordinated();
357                                        $count++;
358                                }
359                                else if ($extra !== 'subordinated')
360                                {
361                                        $return['relation'.$count]['id_relation'] = $id;
362                                        $return['relation'.$count]['id_type'] = $type;
363                                        $return['relation'.$count]['type'] = $relation->get_type_name();
364                                        $return['relation'.$count]['is_subordinated'] = $relation->get_is_subordinated();
365                                        $count++;
366                                }
367                        }
368                       
369                        return $return;
370                }
371       
372                /*!
373               
374                        @function get_addresses
375                        @abstract Returns all Contacts Address Information
376                        @author Raphael Derosso Pereira
377                 
378                        @param integer $id_contact The Contact ID
379                       
380                        @return The following array:
381                                 $return = array(
382                                        'address1' => array(
383                                                'id_address'         => <id_address>,
384                                                'id_type'            => <id_type>,
385                                                'type'               => '<type_name>',
386                                                'address1'           => '<address1>',
387                                                'address2'           => '<address2>',
388                                                'complement'         => '<complement>',
389                                                'address_other'      => '<address_other>',
390                                                'postal_code'        => '<postal_code>',
391                                                'po_box'                     => '<po_box>',
392                                                'id_city'            => '<city>',
393                                                'city_name'          => (string),
394                                                'city_timezone'      => (int),
395                                                'city_geo_location'  => (string),
396                                                'id_state'           => (int),
397                                                'state_name'         => (string),
398                                                'state_symbol'       => (string),
399                                                'id_country'         => (int),
400                                                'country_name'       => (string),
401                                                'address_is_default' => <trueness>
402                                        ),
403                                        'address2' => array(...),
404                                        ...
405                                 );
406                 
407                */
408                function get_addresses ( $id_contact,$extra=false )
409                {
410                        $contact = CreateObject('contactcenter.so_contact', $id_contact);
411                        $addresses = $contact->get_addresses(extra);
412               
413                        foreach($addresses as $id => $type)
414                        {
415                                $address = CreateObject('contactcenter.so_address',$id);
416                                $address_type = CreateObject('contactcenter.so_contact_address_type',$type);
417                               
418                                if ($extra === 'default' and $address->is_default())
419                                {
420                                        $return['address'.$type]['id_address'] = $id;
421                                        $return['address'.$type]['id_typeof_address'] = $type;
422                                        $return['address'.$type]['type'] = $address_type->get_type_name();
423                                        $return['address'.$type]['address1'] = $address->get_address1();
424                                        $return['address'.$type]['address2'] = $address->get_address2();
425                                        $return['address'.$type]['complement'] = $address->get_complement();
426                                        $return['address'.$type]['address_other'] = $address->get_address_other();
427                                        $return['address'.$type]['postal_code'] = $address->get_postal_code();
428                                        $return['address'.$type]['po_box'] = $address->get_po_box();
429                                        $return['address'.$type]['address_is_default'] = true;
430                                        $return['address'.$type]['id_city'] = $id_city = $address->get_id_city();
431                                        $return['address'.$type]['id_state'] = $id_state = $address->get_id_state();
432                                        $return['address'.$type]['id_country'] = $id_country = $address->get_id_country();
433                                       
434                                        if ($id_city)
435                                        {
436                                                $city = CreateObject('contactcenter.so_city',$id_city);
437                                                $return['address'.$type]['city_name'] = $city->get_city_name();
438                                                $return['address'.$type]['city_timezone'] = $city->get_city_timezone();
439                                                $return['address'.$type]['city_geo_location'] = $city->get_city_geo_location();
440                                        }
441                                       
442                                        if ($id_state)
443                                        {
444                                                $state = CreateObject('contactcenter.so_state',$id_state);
445                                                $return['address'.$type]['state_name'] = $state->get_state_name();
446                                                $return['address'.$type]['state_symbol'] = $state->get_state_symbol();
447                                                $return['address'.$type]['id_country'] = $id_country = $state->get_id_country();
448                                        }
449
450                                        $country = CreateObject('contactcenter.so_country',$id_country);
451                                        $return['address'.$type]['country_name'] = $country->get_country_name();
452                                }
453                                else if ($extra !== 'default')
454                                {       
455                                        $return['address'.$type]['id_address'] = $id;
456                                        $return['address'.$type]['id_typeof_address'] = $type;
457                                        $return['address'.$type]['type'] = $address_type->get_type_name();
458                                        $return['address'.$type]['address1'] = $address->get_address1();
459                                        $return['address'.$type]['address2'] = $address->get_address2();
460                                        $return['address'.$type]['complement'] = $address->get_complement();
461                                        $return['address'.$type]['address_other'] = $address->get_address_other();
462                                        $return['address'.$type]['postal_code'] = $address->get_postal_code();
463                                        $return['address'.$type]['po_box'] = $address->get_po_box();
464                                        $return['address'.$type]['address_is_default'] = $address->is_default();
465                                        $return['address'.$type]['id_city'] = $id_city = $address->get_id_city();
466                                        $return['address'.$type]['id_state'] = $id_state = $address->get_id_state();
467                                        $return['address'.$type]['id_country'] = $id_country = $address->get_id_country();
468                                       
469                                        if ($id_city)
470                                        {
471                                                $city = CreateObject('contactcenter.so_city',$id_city);
472                                                $return['address'.$type]['city_name'] = $city->get_city_name();
473                                                $return['address'.$type]['city_timezone'] = $city->get_city_timezone();
474                                                $return['address'.$type]['city_geo_location'] = $city->get_city_geo_location();
475                                        }
476                                       
477                                        if ($id_state)
478                                        {
479                                                $state = CreateObject('contactcenter.so_state',$id_state);
480                                                $return['address'.$type]['state_name'] = $state->get_state_name();
481                                                $return['address'.$type]['state_symbol'] = $state->get_state_symbol();
482                                                $return['address'.$type]['id_country'] = $id_country = $state->get_id_country();
483                                        }
484
485                                        $country = CreateObject('contactcenter.so_country',$id_country);
486                                        $return['address'.$type]['country_name'] = $country->get_country_name();
487                                }
488                        }
489                       
490                        return $return;
491                }
492       
493       
494                /*!
495               
496                        @function get_connections
497                        @abstract Returns all Contacts connections information
498                        @author Raphael Derosso Pereira
499                 
500                        @param integer $id_contact The Contact ID
501                       
502                        @return The following array:
503                                 $return = array(
504                                        'connection1' => array(
505                                                'id_connection'         => <id_connection>,
506                                                'id_type'               => <id_type>,
507                                                'type'                  => '<type_name>',
508                                                'connection_name'       => '<connection_name>',
509                                                'connection_value'      => '<connection_value>',
510                                                'connection_is_default' => '<connection_is_default>'
511                                        ),
512                                        'connection2' => array(...),
513                                        ...
514                                 );
515                 
516                */
517                function get_connections ( $id_contact,$extra=false )
518                {
519                        $contact = CreateObject('contactcenter.so_contact', $id_contact);
520                        $connections = $contact->get_connections($extra);
521               
522                        $count = 1;     
523                        foreach($connections as $id => $type)
524                        {
525                                $connection = CreateObject('contactcenter.so_connection', $id);
526                                if ($getDummy || $connection->get_name() !== 'no_conns')
527                                {
528                                        if ($extra === 'default' and $connection->is_default())
529                                        {
530                                                $connection_type = CreateObject('contactcenter.so_contact_connection_type', $type);
531                                                $return['connection'.$count]['id_connection'] = $id;
532                                                $return['connection'.$count]['id_type'] = $type;
533                                                $return['connection'.$count]['type'] = $connection_type->get_type_name();
534                                                $return['connection'.$count]['connection_name'] = $connection->get_name();
535                                                $return['connection'.$count]['connection_value'] = $connection->get_value();
536                                                $return['connection'.$count]['connection_is_default'] = $connection->is_default();
537                                                $count++;
538                                        }
539                                        else if ($extra !== 'default')
540                                        {
541                                                $connection_type = CreateObject('contactcenter.so_contact_connection_type', $type);
542                                                $return['connection'.$count]['id_connection'] = $id;
543                                                $return['connection'.$count]['id_type'] = $type;
544                                                $return['connection'.$count]['type'] = $connection_type->get_type_name();
545                                                $return['connection'.$count]['connection_name'] = $connection->get_name();
546                                                $return['connection'.$count]['connection_value'] = $connection->get_value();
547                                                $return['connection'.$count]['connection_is_default'] = $connection->is_default();
548                                                $count++;
549                                        }
550                                }
551                        }
552                       
553                        return $return;
554                }
555       
556                /*!
557               
558                        @function get_companies
559                        @abstract Returns all Contacts companies information
560                        @author Raphael Derosso Pereira
561                 
562                        @param integer $id_contact The Contact ID
563                       
564                        @return The following array:
565                                 $return = array(
566                                        'company1' => array(
567                                                'id_company'   => <id_company>,
568                                                'company_name' => '<company_name>'
569                                                'title'        => '<company_name>',
570                                                'department'   => '<company_value>',
571                                        ),
572                                        'company2' => array(...),
573                                        ...
574                                 );
575                 
576                */
577                function get_companies ( $id_contact,$extra=false )
578                {
579                        $contact = CreateObject('contactcenter.so_contact', $id_contact);
580                        $companies = $contact->get_companies($extra);
581               
582                        $count = 1;     
583                        foreach($companies as $id => $value)
584                        {
585                                $company = CreateObject('contactcenter.so_company', $id);
586
587                                if ($extra === 'default' and $value['default_company'])
588                                {
589                                        $return['company'.$count]['id_company'] = $id;
590                                        $return['company'.$count]['company_name'] = $company->get_company_name();
591                                        $return['company'.$count]['title'] = $value['title'];
592                                        $return['company'.$count]['department'] = $value['department'];
593                                        $return['company'.$count]['default_company'] = $value['default_company'];
594                                        $return['company'.$count]['default_contact'] = $value['default_contact'];
595                                        $count++;
596                                }
597                                else if ($extra !== 'default')
598                                {
599                                        $return['company'.$count]['id_company'] = $id;
600                                        $return['company'.$count]['company_name'] = $company->get_company_name();
601                                        $return['company'.$count]['title'] = $value['title'];
602                                        $return['company'.$count]['department'] = $value['department'];
603                                        $return['company'.$count]['default_company'] = $value['default_company'];
604                                        $return['company'.$count]['default_contact'] = $value['default_contact'];
605                                        $count++;
606                                }
607                        }
608                       
609                        return $return;
610                }
611               
612               
613                /*********************************************************************\
614                 *                Methods to get general fields                      *
615                \*********************************************************************/
616               
617                /*!
618               
619                 @function get_all_prefixes
620                 @abstract Returns all the registered prefixes
621                 @author Raphael Derosso Pereira
622                 
623                */
624                function get_all_prefixes (  )
625                {
626                        $fields = array('prefix.id_prefix','prefix.prefix');
627                       
628                        $prefixes = $this->find($fields);
629                       
630                        if (!is_array($prefixes))
631                        {
632                                return false;
633                        }
634                       
635                        while (list(,$prefix) = each($prefixes))
636                        {
637                                $result[$prefix['id_prefix']] = $prefix['prefix'];
638                        }
639                       
640                        return $result;
641                }
642               
643       
644                /*!
645               
646                 @function get_all_suffixes
647                 @abstract Returns all the registered suffixes
648                 @author Raphael Derosso Pereira
649                 @return An array as follows:
650                        $return = array(
651                                <id_suffix1> => '<suffix_name1>',
652                                <id_suffix2> => '<suffix_name2>',
653                                ...                             
654                        );
655                 
656                */
657                function get_all_suffixes (  )
658                {
659                        $fields = array('suffix.id_suffix','suffix.suffix');
660                       
661                        $suffixes = $this->find($fields);
662                       
663                        if (!is_array($suffixes))
664                        {
665                                return false;
666                        }
667                       
668                        while (list(,$suffix) = each($suffixes))
669                        {
670                                $result[$suffix['id_suffix']] = $suffix['suffix'];
671                        }
672                       
673                        return $result;
674                }
675       
676                /*!
677               
678                 @function get_all_status
679                 @abstract Returns all the registered status
680                 @author Raphael Derosso Pereira
681                 @return An array as follows:
682                        $return = array(
683                                <id_status1> => '<status_name1>',
684                                <id_status2> => '<status_name2>',
685                                ...                             
686                        );
687                 
688                */
689                function get_all_status (  )
690                {
691                        $fields = array('status.id_status','status.status_name');
692                       
693                        $status = $this->find($fields);
694                       
695                        if (!is_array($status))
696                        {
697                                return false;
698                        }
699                       
700                        while (list(,$status_) = each($status))
701                        {
702                                $result[$status_['id_status']] = $status_['status_name'];
703                        }
704                       
705                        return $result;
706                }
707
708                /*!
709               
710                 @function get_all_relations_types
711                 @abstract Returns all contacts relations types
712                 @author Raphael Derosso Pereira
713                 
714                 @return array The format of the return is:
715                        $return = array(
716                                <id_type1> => '<type1_name>',
717                                <id_type2> => '<type2_name>',
718                                ...);
719                */
720                function get_all_relations_types (  )
721                {
722                        $fields = array('typeof_contact_relation.id_typeof_contact_relation', 'typeof_contact_relation.contact_relation_type_name');
723                       
724                        $relation_types = $this->find($fields);
725                       
726                        if (!is_array($relation_types))
727                        {
728                                return false;
729                        }
730                       
731                        while (list(,$relation_type) = each($relation_types))
732                        {
733                                $result[$relation_type['id_typeof_contact_relation']] = $relation_type['contact_relation_type_name'];                           
734                        }
735                       
736                        return $result;
737                }
738       
739                /*!
740               
741                 @function get_all_addresses_types
742                 @abstract Returns all contacts addresses types
743                 @author Raphael Derosso Pereira
744                 
745                 @return array The format of the return is:
746                        $return = array(
747                                <id_type1> => '<type1_name>',
748                                <id_type2> => '<type2_name>',
749                                ...);
750                */
751                function get_all_addresses_types (  )
752                {
753                        $fields = array('typeof_contact_address.id_typeof_contact_address', 'typeof_contact_address.contact_address_type_name');
754                       
755                        $address_types = $this->find($fields);
756                       
757                        if (!is_array($address_types))
758                        {
759                                return false;
760                        }
761                       
762                        while (list(,$address_type) = each($address_types))
763                        {
764                                $result[$address_type['id_typeof_contact_address']] = $address_type['contact_address_type_name'];                               
765                        }
766                       
767                        return $result;
768                }
769
770                /*!
771               
772                 @function get_all_connections_types
773                 @abstract Returns all contacts connections types
774                 @author Raphael Derosso Pereira
775                 
776                 @return array The format of the return is:
777                        $return = array(
778                                <id_type1> => '<type1_name>',
779                                <id_type2> => '<type2_name>',
780                                ...);
781                */
782                function get_all_connections_types (  )
783                {
784                        $fields = array('typeof_contact_connection.id_typeof_contact_connection', 'typeof_contact_connection.contact_connection_type_name');
785                       
786                        $connection_types = $this->find($fields);
787                       
788                        if (!is_array($connection_types))
789                        {
790                                return false;
791                        }
792                       
793                        while (list(,$connection_type) = each($connection_types))
794                        {
795                                $result[$connection_type['id_typeof_contact_connection']] = $connection_type['contact_connection_type_name'];                           
796                        }
797                       
798                        return $result;
799                }
800
801                /*!
802               
803                        @function get_vcard
804                        @abstract Returns an URL that points to the file
805                                that contains a vCard of the specified Contact
806                        @author Raphael Derosso Pereira
807               
808                        @param integer $id_status The Contact ID
809                       
810                */
811                function get_vcard ( $id_contact )
812                {
813                }
814
815
816                /*********************************************************************\
817                 *                   Methods to Include Data                         *
818                \*********************************************************************/
819
820                /*!
821               
822                        @function add_single_entry
823                        @abstract Insert a new Contact record in the DB
824                        @author Raphael Derosso Pereira
825               
826                        @param array $data The Contact Information.
827                       
828                        Format:
829               
830                                $data = array(
831                                        'id_status'          => <id_status>,
832                                        'photo'              => '<photo_bin_stream>',
833                                        'alias'              => '<alias>',
834                                        'id_prefix'          => <id_prefix>,
835                                        'given_names'        => '<given_names>',
836                                        'family_names'       => '<family_names>',
837                                        'names_ordered'      => '<names_ordered>',
838                                        'id_suffix'          => <id_suffix>,
839                                        'birthdate'          => '<birthdate>',
840                                        'sex'                => '<sex>',
841                                        'pgp_key'            => '<pgp_key>',
842                                        'notes'              => '<notes>',
843                                       
844                                        'companies'          => array(
845                                                company1 => array(
846                                                        'id_company'      => <id_company>,
847                                                        'company_name'    => <company_name>,
848                                                        'title'           => <title>,
849                                                        'department'      => <department>,
850                                                        'default_company' => (bool),
851                                                        'default_contact' => (bool)
852                                                ),
853                                                company2 => array(...),
854                                                ...
855                                        ),
856                                       
857                                        'relations'          => array(
858                                                'relation1' => array(
859                                                        'id_relation'        => <id_relation>,
860                                                        'id_typeof_relation' => <id_typeof_relation>,
861                                                ),
862                                                'relation2' => array(...),
863                                                ...
864                                        ),
865                                       
866                                        'addresses'          => array(
867                                                'address1' => array(
868                                                        'id_typeof_address'  => <id_typeof_address>,
869                                                        'address1'           => '<address1>',
870                                                        'address2'           => '<address2>',
871                                                        'complement'         => '<complement>',
872                                                        'address_other'      => '<address_other>',
873                                                        'postal_code'        => '<postal_code>',
874                                                        'po_box'                     => '<po_box>',
875                                                        'id_city'            => '<city>',
876                                                        'address_is_default' => <trueness>
877                                                ),
878                                                'address2' => array(...),
879                                                ...
880                                        ),
881                                       
882                                        'connections'        => array(
883                                                'connection1' => array(
884                                                        'id_typeof_connection'  => <id_typeof_connection>,
885                                                        'connection_name'       => <connection_name>,
886                                                        'connection_value'      => <connection_value>,
887                                                        'connection_is_default' => <trueness>
888                                                ),
889                                                'connection2' => array(...),
890                                                ...
891                                        ),
892                                       
893                                );
894                       
895                        If any of the above fields doesn't have a value, it should hold false.
896                        In the case of the multiple-values fields, instead of the array, there
897                        should be a false.
898
899                        @return integer $id The Contact ID
900                */
901                //alterada para receber a informacao em 'notes' - Rommel Cysne - rommel.cysne@serpro.gov.br
902                function add_single_entry ( $data )
903                {
904                        $permissions = $this->security->get_permissions();
905                       
906                        $exist_connections = false;
907                       
908                        if (!$permissions['create'])
909                        {
910                                //return false;
911                        }
912                       
913                        $contact = CreateObject('contactcenter.so_contact');
914                        $contact->reset_values();
915                       
916                        $altered = false;
917                        foreach($data as $field => $value)
918                        {
919                                if ($value === false && $field != 'connections')
920                                {
921                                        continue;
922                                }
923                               
924                                $altered = true;
925                                switch($field)
926                                {
927                                        case 'photo':
928                                        case 'alias':
929                                        case 'id_prefix':
930                                        case 'id_status':
931                                        case 'id_suffix':
932                                        case 'corporate_name':
933                                        case 'job_title':
934                                        case 'department':
935                                        case 'web_page':
936                                        case 'given_names':
937                                        case 'family_names':
938                                        case 'names_ordered':
939                                        case 'birthdate':
940                                        case 'sex':
941                                        case 'pgp_key':
942                                        case 'notes':
943                                                $contact->set_field($field,$value);
944                                                break;
945
946                                        case 'companies':                                       
947                                                foreach($value as $company_fields)
948                                                {
949                                                        if ($company_fields['company_name'])
950                                                        {
951                                                                $fields = array('company.id_company');
952                                                                $restric = array(
953                                                                        0 => array(
954                                                                                'field' => 'company.company_name',
955                                                                                'type'  => 'iLIKE',
956                                                                                'value' => $company_fields['company_name']
957                                                                        )
958                                                                );
959                                                               
960                                                                if($result = $this->find($fields,$restric))
961                                                                {
962                                                                        $id = $result[0]['id_company'];
963                                                                        $company_fields['id_company'] = $id;
964                                                                }
965                                                                else
966                                                                {
967                                                                        $company = CreateObject('contactcenter.so_company');
968                                                                        $company->reset_values();
969                                                                        $company->set_company_name($company_fields['company_name']);
970                                                                        $company->set_field('id_company_owner',$GLOBALS['phpgw_info']['user']['account_id']);
971                                                                        $company_fields['id_company'] = $company->commit();
972                                                                }
973                                                        }
974                                                       
975                                                        $contact->set_company($company_fields);
976                                                }
977                                                break;
978                                       
979                                        case 'relations':
980                                                foreach($value as $relation_fields)
981                                                {
982                                                        $contact->set_relation($relation_fields['id_relation'], $relation_fields['id_typeof_relation']);
983                                                }
984                                                break;
985                                       
986                                        case 'addresses':
987                                                foreach($value as $address_fields)
988                                                {
989                                                        $address = CreateObject('contactcenter.so_address');
990                                                        $address->reset_values();
991                                                        foreach($address_fields as $a_field => $a_value)
992                                                        {
993                                                                if ($a_field !== 'id_typeof_address')
994                                                                {
995                                                                        $address->set_field($a_field,$a_value);
996                                                                }
997                                                        }
998                                                        $address->commit();
999                                                        $id_address = $address->get_id();
1000                                                        $contact->set_address($id_address, $address_fields['id_typeof_address']);
1001                                                }
1002                                                break;
1003                                       
1004                                        case 'connections':
1005                                                foreach($value as $connection_name => $connection_fields)
1006                                                {
1007                                                        $exist_connections = true;
1008                                                        $connection = CreateObject('contactcenter.so_connection');
1009                                                        $connection->reset_values();
1010                                                       
1011                                                        foreach($connection_fields as $a_field => $a_value)
1012                                                        {
1013                                                                if ($a_field !== 'id_typeof_connection')
1014                                                                {
1015                                                                        $connection->set_field($a_field,$a_value);
1016                                                                }
1017                                                        }
1018                                                       
1019                                                        $connection->commit();
1020                                                        $id_connection = $connection->get_id();
1021                                                        $contact->set_connection($id_connection, $connection_fields['id_typeof_connection']);
1022                                                }
1023                                                break;
1024                                               
1025                                        default:
1026                                                return false;
1027                                }
1028                        }
1029
1030                        if ($altered)
1031                        {
1032                                $contact->set_field('id_owner',$GLOBALS['phpgw_info']['user']['account_id']);
1033                                //insert a dummy connection if the contact dont have an email and telephone
1034                                if (!$exist_connections)
1035                                {
1036                                        $this->add_dummy($contact);
1037                                }
1038                                return $contact->commit();
1039                        }
1040                       
1041                        return false;
1042                }
1043               
1044                /*!
1045               
1046                        @function quick_add
1047                        @abstract Insert a new Contact record in the DB with just the
1048                                main fields
1049                        @author Raphael Derosso Pereira
1050               
1051                        @param array $data The Contact Information.
1052                       
1053                        Format:
1054               
1055                                $data = array(
1056                                        'alias'              => '<alias>',
1057                                        'given_names'        => '<given_names>',
1058                                        'family_names'       => '<family_names>',
1059
1060                                        'connections'        => array(
1061                                                'default_email' => array(
1062                                                        'connection_name'       => <connection_name>,
1063                                                        'connection_value'      => <connection_value>
1064                                                ),
1065                                                'default_phone' => array(
1066                                                        'connection_name'       => <connection_name>,
1067                                                        'connection_value'      => <connection_value>
1068                                                )
1069                                        )
1070                                );
1071                */
1072                //alterada para receber os outros parametros existentes na adicao completa de contatos pessoais
1073                //Rommel Cysne - rommel.cysne@serpro.gov.br
1074                function quick_add ( $data )
1075                {
1076                        $permissions = $this->security->get_permissions();
1077                       
1078                        if (!$permissions['create'])
1079                        {
1080                                return false;
1081                        }
1082
1083                        // TODO: GET THE ORDER TO PUT names_ordered FROM PREFERENCES!
1084
1085                        $preferences = ExecMethod('contactcenter.ui_preferences.get_preferences');
1086                       
1087                        if ( !isset($preferences['personCardEmail']) || ( !is_numeric($preferences['personCardEmail'])) )
1088                        {
1089                                $preferences['personCardEmail'] = 1;
1090                        }
1091                        if ( !isset($preferences['personCardPhone']) || ( !is_numeric($preferences['personCardPhone'])) )
1092                        {
1093                                $preferences['personCardPhone'] = 2;
1094                        }
1095                       
1096                        if(!$data['is_quick_add']){                     
1097                                $sql = "select * FROM phpgw_cc_state where (upper(state_name) = upper('".$data['addresses']['address_corporative']['id_state']."')) or (upper(state_symbol) = upper('".$data['addresses']['address_corporative']['id_state']."'))";     
1098                                                       
1099                                $this->db->query($sql, __LINE__, __FILE__);
1100                               
1101                                $corporative_state = array('id_state'=>2);
1102                                while($this->db->next_record())
1103                                {
1104                                        $corporative_state = $this->db->row();
1105                                }
1106                               
1107                                $sql = "select * FROM phpgw_cc_city where id_state = ".$corporative_state['id_state']." and upper(city_name) = upper('".$data['addresses']['address_corporative']['id_city']."') ";
1108                                                                               
1109                                $this->db->query($sql, __LINE__, __FILE__);
1110                               
1111                                $corporative_city = array('id_city'=>null);
1112                                while($this->db->next_record())
1113                                {
1114                                        $corporative_city = $this->db->row();
1115                                }
1116                               
1117                                //Personal Address
1118                               
1119                                $sql = "select * FROM phpgw_cc_state where upper(state_name) = upper('".$data['addresses']['address_personal']['id_state']."')";       
1120                                                       
1121                                $this->db->query($sql, __LINE__, __FILE__);
1122                               
1123                                $personal_state = array('id_state'=>2);
1124                                while($this->db->next_record())
1125                                {
1126                                        $personal_state = $this->db->row();
1127                                }
1128                               
1129                                $sql = "select * FROM phpgw_cc_city where id_state = ".$personal_state['id_state']." and upper(city_name) = upper('".$data['addresses']['address_personal']['id_city']."') ";
1130                                                                               
1131                                $this->db->query($sql, __LINE__, __FILE__);
1132                               
1133                                $personal_city = array('id_city'=>null);
1134                                while($this->db->next_record())
1135                                {
1136                                        $personal_city = $this->db->row();
1137                                }
1138                        }
1139                       
1140                        $new_data = array(
1141                                'notes'              => $data['notes'],
1142                                'alias'              => $data['alias'],
1143                                'id_status'          => 1,
1144                                'given_names'        => $data['given_names'],
1145                                'family_names'       => $data['family_names'],
1146                                'names_ordered'      => $data['given_names'].' '.$data['family_names'],
1147                                'family_names'       => $data['family_names'],
1148                                'alias'                  => $data['alias'],
1149                                'corporate_name'     => $data['corporate_name'],
1150                                'job_title'              => $data['job_title'],
1151                                'department'             => $data['department'],
1152                                'web_page'               => $data['web_page'],
1153                                'birthdate'                      => $data['birthdate'],
1154                                'pgp_key'                => $data['pgp_key'],
1155                                'sex'                    => $data['sex'],
1156                        );
1157
1158                        $i = 1;
1159                        if ($data['connections']['default_email']['connection_value'])
1160                        {
1161                                $new_data['connections']['connection'.$i] = array(
1162                                        'id_typeof_connection'  => $preferences['personCardEmail'],
1163                                        'connection_name'       => $data['connections']['default_email']['connection_name'],
1164                                        'connection_value'      => $data['connections']['default_email']['connection_value'],
1165                                        'connection_is_default' => 1,
1166                                );
1167                                $i++;
1168                        }
1169                       
1170                        if ($data['connections']['aditional_email']['connection_value'])
1171                        {
1172                                $new_data['connections']['connection'.$i] = array(
1173                                        'id_typeof_connection'  => $preferences['personCardEmail'],
1174                                        'connection_name'       => $data['connections']['aditional_email']['connection_name'],
1175                                        'connection_value'      => $data['connections']['aditional_email']['connection_value'],
1176                                        'connection_is_default' => 0,
1177                                );
1178                                $i++;
1179                        }
1180                                                       
1181                        if ($data['connections']['default_phone']['connection_value'])
1182                        {
1183                                $new_data['connections']['connection'.$i] = array(
1184                                        'id_typeof_connection'  => $preferences['personCardPhone'],
1185                                        'connection_name'       => $data['connections']['default_phone']['connection_name'],
1186                                        'connection_value'      => $data['connections']['default_phone']['connection_value'],
1187                                        'connection_is_default' => 1,
1188                                );
1189                               
1190                               
1191                                foreach($data['connections']['aditional_phone'] as $connection_name => $connection_fields)
1192                                {
1193                                        if($connection_fields['connection_value'] != ''){
1194                                                $i++;
1195                                                $new_data['connections']['connection'.$i] = array(
1196                                                'id_typeof_connection'  => $preferences['personCardPhone'],
1197                                                'connection_name'       => $connection_fields['connection_name'],
1198                                                'connection_value'      => $connection_fields['connection_value'],
1199                                                'connection_is_default' => 0,
1200                                                );
1201                                        }                                       
1202                                }
1203                               
1204                        }else{
1205                                $default =1;
1206                                foreach($data['connections']['aditional_phone'] as $connection_name => $connection_fields)
1207                                {
1208                                        $i++;
1209                                        $new_data['connections']['connection'.$i] = array(
1210                                        'id_typeof_connection'  => $preferences['personCardPhone'],
1211                                        'connection_name'       => $connection_fields['connection_name'],
1212                                        'connection_value'      => $connection_fields['connection_value'],
1213                                        'connection_is_default' => $default,
1214                                        );
1215                                        $default = 0;                                   
1216                                }
1217                        }
1218                        if(!$data['is_quick_add']){                     
1219                                $new_data['addresses']['address'.$i] = array(
1220                                        'id_typeof_address'  => 2,
1221                                        'address1'       => $data['addresses']['address_personal']['address1'],
1222                                        'address2'       => $data['addresses']['address_personal']['address2'],
1223                                        'complement'     => $data['addresses']['address_personal']['complement'],
1224                                        'postal_code'    => $data['addresses']['address_personal']['postal_code'],
1225                                        'id_country'     => $data['addresses']['address_personal']['id_country'],
1226                                        'id_state'               => $personal_state['id_state'],
1227                                        'id_city'                => $personal_city['id_city'],
1228                                        'address_is_default' => 0,
1229                                );
1230                                $i++;
1231                       
1232                                $new_data['addresses']['address'.$i] = array(
1233                                        'id_typeof_address'  => 1,
1234                                        'address1'       => $data['addresses']['address_corporative']['address1'],
1235                                        'address2'       => $data['addresses']['address_corporative']['address2'],
1236                                        'complement'     => $data['addresses']['address_corporative']['complement'],
1237                                        'postal_code'    => $data['addresses']['address_corporative']['postal_code'],
1238                                        'id_country'     => $data['addresses']['address_corporative']['id_country'],
1239                                        'id_state'               => $corporative_state['id_state'],
1240                                        'id_city'                => $corporative_city['id_city'],
1241                                        'address_is_default' => 1,
1242                                );                                                     
1243                                $i++;
1244                        }
1245
1246                        $j = 1;
1247
1248                        /*if($data['addresses']['addr1']['address_value'])
1249                        {
1250                                $new_data['addresses']['address'.$j] = array(
1251                                        'id_typeof_address'     => 1, //<id_typeof_address> - 1 - Comercial; 2 - Residencial
1252                                        'address1'              => $data['addresses']['addr1']['address_value'],
1253                                        'address2'              => $data['addresses']['addr2']['address_value'],
1254                                        'id_country'            => 'BR',
1255                                        //'complement'          => '<complement>',
1256                                        //'address_other'       => '<address_other>',
1257                                        'postal_code'           => $data['addresses']['cep']['address_value'],
1258                                        //'po_box'              => '<po_box>',
1259                                        //'id_city'             => '<city>',
1260                                        'address_is_default'    => 'false',
1261                                );
1262                                $j++;
1263                        }*/
1264
1265
1266                        return $this->add_single_entry($new_data);
1267                }
1268       
1269                /*!
1270               
1271                        @function add_prefix
1272                        @abstract Insert a new Prefix in the DB
1273                        @author Raphael Derosso Pereira
1274               
1275                        @param string $prefix The Prefix
1276                        @return integer The new ID
1277                       
1278                */
1279                function add_prefix ( $prefix )
1280                {
1281                        $permissions = $this->security->get_type_permissions();
1282                       
1283                        if (!$permissions['create'])
1284                        {
1285                                return false;
1286                        }
1287                }
1288       
1289                /*!
1290               
1291                        @function add_suffix
1292                        @abstract Insert a new suffix in the DB
1293                        @author Raphael Derosso Pereira
1294               
1295                        @param string $suffix The suffix
1296                        @return integer The new ID
1297                       
1298                */
1299                function add_suffix ( $suffix )
1300                {
1301                        $permissions = $this->security->get_type_permissions();
1302                       
1303                        if (!$permissions['create'])
1304                        {
1305                                return false;
1306                        }
1307                }
1308       
1309                /*!
1310               
1311                        @function add_status
1312                        @abstract Insert a new Status in the DB
1313                        @author Raphael Derosso Pereira
1314               
1315                        @param string $status The Status
1316                        @return integer The new ID
1317                       
1318                */
1319                function add_status ( $status )
1320                {
1321                        $permissions = $this->security->get_type_permissions();
1322                       
1323                        if (!$permissions['create'])
1324                        {
1325                                return false;
1326                        }
1327                }
1328       
1329                /*!
1330               
1331                        @function add_relation_type
1332                        @abstract Insert a new Relation Type in the DB
1333                        @author Raphael Derosso Pereira
1334               
1335                        @param string $type_name The Relation Type
1336                        @return integer The new ID
1337                       
1338                */
1339                function add_relation_type ( $type_name )
1340                {
1341                        $permissions = $this->security->get_type_permissions();
1342                       
1343                        if (!$permissions['create'])
1344                        {
1345                                return false;
1346                        }
1347                }
1348       
1349                /*!
1350               
1351                        @function add_address_type
1352                        @abstract Insert a new Address Type in the DB
1353                        @author Raphael Derosso Pereira
1354               
1355                        @param string $type_name The Address Type
1356                        @return integer The new ID
1357                       
1358                */
1359                function add_address_type ( $type_name )
1360                {
1361                        $permissions = $this->security->get_type_permissions();
1362                       
1363                        if (!$permissions['create'])
1364                        {
1365                                return false;
1366                        }
1367                }
1368       
1369                /*!
1370               
1371                        @function add_connection_type
1372                        @abstract Insert a new Connection Type in the DB
1373                        @author Raphael Derosso Pereira
1374               
1375                        @param string $type_name The Connection Type
1376                        @return integer The new ID
1377                       
1378                */
1379                function add_connection_type ( $type_name )
1380                {
1381                        $permissions = $this->security->get_type_permissions();
1382                       
1383                        if (!$permissions['create'])
1384                        {
1385                                return false;
1386                        }
1387                }
1388       
1389
1390
1391                /*********************************************************************\
1392                 *                   Methods to Alter Data                           *
1393                \*********************************************************************/
1394
1395                /*!
1396               
1397                        @function update_single_info
1398                        @abstract Update information of an existing Contact
1399                        @author Raphael Derosso Pereira
1400               
1401                        @param integer $id_status The Contact ID
1402                        @param string $status The new Status value
1403                       
1404                */
1405                function update_single_info ( $id_contact, $data )
1406                {
1407                        $permissions = $this->security->get_permissions($id_contact);
1408                       
1409                        if (!$permissions['write'])
1410                        {
1411                                return false;
1412                        }
1413                       
1414                        $contact = CreateObject('contactcenter.so_contact', $id_contact);
1415                       
1416                        $altered = false;
1417//                      print_r($data);
1418                        foreach($data as $field => $value)
1419                        {
1420                                if ($value === false)
1421                                {
1422                                        continue;
1423                                }
1424                               
1425                                $altered = true;
1426                                switch($field)
1427                                {
1428                                        case 'photo':
1429                                        case 'alias':
1430                                        case 'id_prefix':
1431                                        case 'id_status':
1432                                        case 'id_suffix':
1433                                        case 'corporate_name':
1434                                        case 'job_title':
1435                                        case 'department':
1436                                        case 'web_page':                                       
1437                                        case 'given_names':
1438                                        case 'family_names':
1439                                        case 'names_ordered':
1440                                        case 'birthdate':
1441                                        case 'sex':
1442                                        case 'pgp_key':
1443                                        case 'notes':
1444                                                $contact->set_field($field,$value);
1445                                                break;
1446
1447                                        case 'companies':                                       
1448                                                foreach($value as $company_fields)
1449                                                {
1450                                                        if ($company_fields['company_name'])
1451                                                        {
1452                                                                $fields = array('company.id_company');
1453                                                                $restric = array(
1454                                                                        0 => array(
1455                                                                                'field' => 'company.company_name',
1456                                                                                'type'  => 'iLIKE',
1457                                                                                'value' => $company_fields['company_name']
1458                                                                        )
1459                                                                );
1460                                                               
1461                                                                if($result = $this->find($fields,$restric))
1462                                                                {
1463                                                                        $id = $result[0]['id_company'];
1464                                                                        $company_fields['id_company'] = $id;
1465                                                                }
1466                                                                else
1467                                                                {
1468                                                                        $company = CreateObject('contactcenter.so_company');
1469                                                                        $company->reset_values();
1470                                                                        $company->set_company_name($company_fields['company_name']);
1471                                                                        $company->set_field('id_company_owner',$GLOBALS['phpgw_info']['user']['account_id']);
1472                                                                        $company_fields['id_company'] = $company->commit();
1473                                                                }
1474                                                        }
1475                                                       
1476                                                        $contact->set_company($company_fields);
1477                                                }
1478                                                break;
1479                                       
1480                                        case 'relations':
1481                                                foreach($value as $relation_fields)
1482                                                {
1483                                                        $contact->set_relation($relation_fields['id_relation'], $relation_fields['id_typeof_relation']);
1484                                                }
1485                                                break;
1486                                       
1487                                        case 'addresses':
1488                                                foreach($value as $address_name => $address_fields)
1489                                                {
1490                                                        if ($address_fields['id_address'] && $address_fields['id_address'] !== '')
1491                                                        {
1492                                                                $address = CreateObject('contactcenter.so_address', $address_fields['id_address']);
1493                                                        }
1494                                                        else
1495                                                        {
1496                                                                $address = CreateObject('contactcenter.so_address');
1497                                                                $address->reset_values();
1498                                                        }
1499
1500                                                        if (!isset($address_fields['id_country']) or $address_fields['id_country'] === false)
1501                                                        {
1502                                                                #var_dump($address_fields);
1503                                                                echo(serialize(array(
1504                                                                        'file' => __FILE__,
1505                                                                        'line' => __LINE__,
1506                                                                        'msg'  => lang('An Address must have at least a Country'),
1507                                                                        'status' => 'aborted'
1508                                                                )));
1509                                                                return;
1510                                                        }
1511                                                       
1512                                                        foreach ($address_fields as $f_name => $f_value)
1513                                                        {
1514                                                                if ($f_value === false)
1515                                                                {
1516                                                                        $address->set_field($f_name, null);
1517                                                                }
1518                                                                elseif (isset($f_value))
1519                                                                {
1520                                                                        $address->set_field($f_name, $f_value);
1521                                                                }
1522                                                        }
1523                                                               
1524                                                        $address->commit();
1525                                                        $id_address = $address->get_id();
1526                                                        $contact->set_address($id_address, $address_fields['id_typeof_address']);
1527                                                }
1528                                                break;
1529                                       
1530                                        case 'connections':
1531                                                //alteracao para permitir a exibicao de contatos sem email ou telefone
1532                                                $all_removed = true;
1533                                                $added_connection = false;
1534                                                foreach($value as $connection_name => $connection_fields)
1535                                                {
1536                                                        if ($connection_name === 'removed_conns')
1537                                                        {
1538                                                                foreach($connection_fields as $id)
1539                                                                {
1540                                                                        $connection = CreateObject('contactcenter.so_connection', $id);
1541                                                                        if (!($connection->remove()))
1542                                                                        {
1543                                                                                return false;
1544                                                                        }
1545
1546                                                                        $contact->remove_connection($id);
1547                                                                }
1548
1549                                                                continue;
1550                                                        }
1551                                                       
1552                                                        $all_removed = false;
1553                                                        // removes the dummy if there is at least one connection
1554                                                        if (!$added_connection)
1555                                                        {
1556                                                                $this->remove_dummy($contact); //remove the dummy
1557                                                        }
1558                                                        $added_connection = true;
1559                                                        $id_connection = $connection_fields['id_connection'];
1560                                                        if ($id_connection === '_NEW_' or
1561                                                            $id_connection === '')
1562                                                        {
1563                                                                $connection = CreateObject('contactcenter.so_connection');
1564                                                                $connection->reset_values();
1565                                                        }
1566                                                        else
1567                                                        {
1568                                                                $connection = CreateObject('contactcenter.so_connection', $id_connection);
1569                                                        }
1570                                                               
1571                                                        foreach($connection_fields as $a_field => $a_value)
1572                                                        {
1573                                                                if ($a_field !== 'id_typeof_connection')
1574                                                                {
1575                                                                        $connection->set_field($a_field,$a_value);
1576                                                                }
1577                                                        }
1578                                                       
1579                                                        if (!$connection->commit())
1580                                                        {
1581                                                                return false;
1582                                                        }
1583                                                        $id_connection = $connection->get_id();
1584                                                        $contact->set_connection($id_connection, $connection_fields['id_typeof_connection']);
1585                                                }
1586                                                // add the dummy if there is no other connection
1587                                                if ($all_removed)
1588                                                        $this->add_dummy($contact);
1589                                                break;
1590                                               
1591                                        default:
1592                                                echo 'Invalid Field: '.$field.'<br>Value: '.$value;
1593                                                return false;
1594                                }
1595                        }
1596                       
1597                        if ($altered)
1598                        {
1599                                $contact->set_field("last_status", 'U');
1600                                $contact->set_field("last_update",time()."000");
1601                                return $contact->commit();
1602                        }
1603                       
1604                        return false;
1605                }
1606       
1607                /*!
1608                        @function remove_dummy
1609                        @abstract Remove the dummy connection entry.
1610                        @author Luiz Carlos Viana Melo - Prognus
1611                 */
1612                function remove_dummy($so_contact)
1613                {
1614                        //$so_contact = CreateObject('contactcenter.so_contact', $id_contact);
1615                        $connections = $so_contact->get_connections();
1616                        foreach ($connections as $id_connection => $type_connection)
1617                        {
1618                                $so_connection = CreateObject('contactcenter.so_connection', $id_connection);
1619                                if ($so_connection->get_field('connection_name') === 'no_conns') //if connection is the dummy one
1620                                {
1621                                        if (!$so_connection->remove())
1622                                        {
1623                                                return false;
1624                                        }
1625                                        $so_contact->remove_connection($id_connection);
1626                                        //$so_connection->commit();
1627                                        break;
1628                                }
1629                        }
1630               
1631                }
1632               
1633                /*!
1634                        @function add_dummy
1635                        @abstract Add a dummy connection entry on a contact. This is necessary for
1636                                possibility on adding a contact that dont have an email or telephone.
1637                        @return The dummy's connection ID.
1638                        @author Luiz Carlos Viana Melo - Prognus
1639                 */
1640                function add_dummy($so_contact)
1641                {
1642                        $so_connection = CreateObject('contactcenter.so_connection');
1643                        $so_connection->reset_values();
1644                       
1645                        $so_connection->set_field('connection_name', 'no_conns'); //defined code for the dummy connection
1646                        $so_connection->set_field('connection_value', '');
1647                        $so_connection->set_field('connection_is_default', true);
1648                       
1649                        if (!$so_connection->commit())
1650                        {
1651                                return false;
1652                        }
1653                        $id_connection = $so_connection->get_id();
1654                        $so_contact->set_connection($id_connection, 2);
1655                        return $id_connection;
1656                }
1657                                       
1658                /*!
1659               
1660                        @function update_prefix
1661                        @abstract Update an existing Prefix
1662                        @author Raphael Derosso Pereira
1663               
1664                        @param integer $id_prefix The Prefix ID
1665                        @param string $prefix The new Prefix value
1666                       
1667                */
1668                function update_prefix ( $id_prefix, $prefix )
1669                {
1670                        $permissions = $this->security->get_type_permissions();
1671                       
1672                        if (!$permissions['alter'])
1673                        {
1674                                return false;
1675                        }
1676                }
1677       
1678                /*!
1679               
1680                        @function update_suffix
1681                        @abstract Update an existing suffix
1682                        @author Raphael Derosso Pereira
1683               
1684                        @param integer $id_suffix The suffix ID
1685                        @param string $suffix The new suffix value
1686                       
1687                */
1688                function update_suffix ( $id_suffix, $suffix )
1689                {
1690                        $permissions = $this->security->get_type_permissions();
1691                       
1692                        if (!$permissions['alter'])
1693                        {
1694                                return false;
1695                        }
1696                }
1697       
1698                /*!
1699               
1700                        @function update_status
1701                        @abstract Update an existing Status
1702                        @author Raphael Derosso Pereira
1703               
1704                        @param integer $id_status The Status ID
1705                        @param string $status The new Status value
1706                       
1707                */
1708                function update_status ( $id_status, $status )
1709                {
1710                        $permissions = $this->security->get_type_permissions();
1711                       
1712                        if (!$permissions['alter'])
1713                        {
1714                                return false;
1715                        }
1716                }
1717       
1718                /*!
1719               
1720                        @function update_relation_type
1721                        @abstract Update an existing Relation Type
1722                        @author Raphael Derosso Pereira
1723               
1724                        @param integer $id_type The Type ID
1725                        @param string $type The new type value
1726                       
1727                */
1728                function update_relation_type ( $old_name, $relation_name )
1729                {
1730                }
1731       
1732                /*!
1733               
1734                        @function update_address_type
1735                        @abstract Update an existing Address Type
1736                        @author Raphael Derosso Pereira
1737               
1738                        @param integer $id_uype The Type ID
1739                        @param string $type The new type value
1740                       
1741                */
1742                function update_address_type ( $id_type, $type )
1743                {
1744                }
1745       
1746                /*!
1747               
1748                        @function update_connection_type
1749                        @abstract Update an existing Connection Type
1750                        @author Raphael Derosso Pereira
1751               
1752                        @param integer $id_type The Type ID
1753                        @param string $type The new type value
1754                       
1755                */
1756                function update_connection_type ( $id_type, $type )
1757                {
1758                }
1759
1760
1761                /*********************************************************************\
1762                 *                   Methods to Remove Data                          *
1763                \*********************************************************************/
1764
1765                /*!
1766               
1767                        @function remove_single_entry
1768                        @abstract Remove one contact from the DB
1769                        @author Raphael Derosso Pereira
1770               
1771                        @param integer $id_contact The Contact ID
1772                       
1773                */
1774                function remove_single_entry ( $id_contact )
1775                {
1776                        $permissions = $this->security->get_permissions();
1777                       
1778                        if (!$permissions['remove'])
1779                        {
1780                                return false;
1781                        }
1782                       
1783                        $contact = CreateObject('contactcenter.so_contact', $id_contact);
1784                       
1785                        if ($contact->get_state() === 'new')
1786                        {
1787                                return false;
1788                        }
1789                       
1790                        $addresses = $contact->get_addresses();
1791                        $connections = $contact->get_connections();
1792                       
1793                        if (!$contact->remove())
1794                        {
1795                                return false;
1796                        }
1797                       
1798                        foreach ($addresses as $id => $type)
1799                        {
1800                                if (!($address = CreateObject('contactcenter.so_address', $id)))
1801                                {
1802                                        return false;
1803                                }
1804                               
1805                                if (!($address->remove()))
1806                                {
1807                                        return false;
1808                                }
1809                               
1810                        }
1811
1812                        foreach ($connections as $id => $type)
1813                        {
1814                                if (!($connection = CreateObject('contactcenter.so_connection',$id)))
1815                                {                                       
1816                                        return false;
1817                                }
1818                               
1819                                if (!($connection->remove()))
1820                                {
1821                                        return false;
1822                                }
1823                               
1824                        }
1825                       
1826                        return true;
1827                }
1828        }
1829?>
Note: See TracBrowser for help on using the repository browser.