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

Revision 4502, 52.0 KB checked in by airton, 13 years ago (diff)

Ticket #1928 - Falhas na associação de um contato com um grupo de contato

  • 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();
521                        $getDummy = false;
522                        if ($extra && $extra === 'defaultwdummy')
523                        {
524                                $getDummy = true;
525                                $extra = 'default';
526                        }
527                       
528                        $count = 1;     
529                        foreach($connections as $id => $type)
530                        {
531                                $connection = CreateObject('contactcenter.so_connection', $id);
532                                if ($getDummy || $connection->get_name() !== 'no_conns')
533                                {
534                                        if ($extra === 'default' and $connection->is_default())
535                                        {
536                                                $connection_type = CreateObject('contactcenter.so_contact_connection_type', $type);
537                                                $return['connection'.$count]['id_connection'] = $id;
538                                                $return['connection'.$count]['id_type'] = $type;
539                                                $return['connection'.$count]['type'] = $connection_type->get_type_name();
540                                                $return['connection'.$count]['connection_name'] = $connection->get_name();
541                                                $return['connection'.$count]['connection_value'] = $connection->get_value();
542                                                $return['connection'.$count]['connection_is_default'] = $connection->is_default();
543                                                $count++;
544                                        }
545                                        else if ($extra !== 'default')
546                                        {
547                                                $connection_type = CreateObject('contactcenter.so_contact_connection_type', $type);
548                                                $return['connection'.$count]['id_connection'] = $id;
549                                                $return['connection'.$count]['id_type'] = $type;
550                                                $return['connection'.$count]['type'] = $connection_type->get_type_name();
551                                                $return['connection'.$count]['connection_name'] = $connection->get_name();
552                                                $return['connection'.$count]['connection_value'] = $connection->get_value();
553                                                $return['connection'.$count]['connection_is_default'] = $connection->is_default();
554                                                $count++;
555                                        }
556                                }
557                        }
558                       
559                        return $return;
560                }
561
562                /*
563                 * @function update_contact_groups
564                 * @abstract Atualiza os grupos onde o contato está inserido.
565                 * @author Luiz Carlos Viana Melo - Prognus
566                 * @param (integer) $id_contact O ID do contato.
567                 * @param (array) $groups Os grupos onde o contato será inserido/removido. O
568                 * formato dos grupos será:
569                 * array(
570                 *      'added'         => array(
571                 *                                              id_group => array (
572                 *                                                                              'id_group' => O ID do grupo.
573                 *                                                                              'title' => O título do grupo.
574                 *                                                                      )
575                 *                                      )
576                 *      'removed'       => mesmo formato do added.
577                 * )
578                 */
579                function update_contact_groups($id_contact, $groups, $old_connections = false)
580                {
581                        $connections = $this->get_connections($id_contact, 'defaultwdummy');
582                        $default_connection = false;
583                        foreach ($connections as $connection)
584                        {
585                                if (!$default_connection)
586                                {
587                                        $default_connection = $connection;
588                                }
589                                if ($connection['id_type'] === 1)
590                                {
591                                        $default_connection = $connection;
592                                }
593                                if($default_connection['id_type'] === 1)
594                                {
595                                        break;
596                                }
597                                       
598                        }
599                       
600                        $altered = false;
601
602                        if ($default_connection)
603                        {
604                                $bo_group = CreateObject('contactcenter.bo_group');
605                                $old_groups = $groups['previous_selected'];
606                                if ($old_connections)
607                                        $bo_group->update_contact_groups($default_connection['id_connection'], $old_connections, $old_groups);
608                                $added_groups = $groups['added'];
609                                $removed_groups = $groups['removed'];
610                                       
611                                foreach ($added_groups as $group)
612                                {
613                                        $altered = true;
614                                        $c = array();
615                                        $c[] = $default_connection['id_connection'];
616                                        $bo_group->add_contacts_in_group($group['id_group'], $c);
617                                }
618                                       
619                                foreach ($removed_groups as $group)
620                                {
621                                        $altered = true;
622                                        $c = array();
623                                        $c[] = $default_connection['id_connection'];
624                                        $bo_group->remove_contacts_from_group($group['id_group'], $c);
625                                }
626                        }
627                       
628                        if ($altered)
629                        {
630                                $contact = CreateObject('contactcenter.so_contact', $id_contact);
631                                $contact->set_field("last_status", 'U');
632                                $contact->set_field("last_update",time()."000");
633                                return $contact->commit();
634                        }
635                }
636       
637                /*!
638               
639                        @function get_companies
640                        @abstract Returns all Contacts companies information
641                        @author Raphael Derosso Pereira
642                 
643                        @param integer $id_contact The Contact ID
644                       
645                        @return The following array:
646                                 $return = array(
647                                        'company1' => array(
648                                                'id_company'   => <id_company>,
649                                                'company_name' => '<company_name>'
650                                                'title'        => '<company_name>',
651                                                'department'   => '<company_value>',
652                                        ),
653                                        'company2' => array(...),
654                                        ...
655                                 );
656                 
657                */
658                function get_companies ( $id_contact,$extra=false )
659                {
660                        $contact = CreateObject('contactcenter.so_contact', $id_contact);
661                        $companies = $contact->get_companies($extra);
662               
663                        $count = 1;     
664                        foreach($companies as $id => $value)
665                        {
666                                $company = CreateObject('contactcenter.so_company', $id);
667
668                                if ($extra === 'default' and $value['default_company'])
669                                {
670                                        $return['company'.$count]['id_company'] = $id;
671                                        $return['company'.$count]['company_name'] = $company->get_company_name();
672                                        $return['company'.$count]['title'] = $value['title'];
673                                        $return['company'.$count]['department'] = $value['department'];
674                                        $return['company'.$count]['default_company'] = $value['default_company'];
675                                        $return['company'.$count]['default_contact'] = $value['default_contact'];
676                                        $count++;
677                                }
678                                else if ($extra !== 'default')
679                                {
680                                        $return['company'.$count]['id_company'] = $id;
681                                        $return['company'.$count]['company_name'] = $company->get_company_name();
682                                        $return['company'.$count]['title'] = $value['title'];
683                                        $return['company'.$count]['department'] = $value['department'];
684                                        $return['company'.$count]['default_company'] = $value['default_company'];
685                                        $return['company'.$count]['default_contact'] = $value['default_contact'];
686                                        $count++;
687                                }
688                        }
689                       
690                        return $return;
691                }
692               
693               
694                /*********************************************************************\
695                 *                Methods to get general fields                      *
696                \*********************************************************************/
697               
698                /*!
699               
700                 @function get_all_prefixes
701                 @abstract Returns all the registered prefixes
702                 @author Raphael Derosso Pereira
703                 
704                */
705                function get_all_prefixes (  )
706                {
707                        $fields = array('prefix.id_prefix','prefix.prefix');
708                       
709                        $prefixes = $this->find($fields);
710                       
711                        if (!is_array($prefixes))
712                        {
713                                return false;
714                        }
715                       
716                        while (list(,$prefix) = each($prefixes))
717                        {
718                                $result[$prefix['id_prefix']] = $prefix['prefix'];
719                        }
720                       
721                        return $result;
722                }
723               
724       
725                /*!
726               
727                 @function get_all_suffixes
728                 @abstract Returns all the registered suffixes
729                 @author Raphael Derosso Pereira
730                 @return An array as follows:
731                        $return = array(
732                                <id_suffix1> => '<suffix_name1>',
733                                <id_suffix2> => '<suffix_name2>',
734                                ...                             
735                        );
736                 
737                */
738                function get_all_suffixes (  )
739                {
740                        $fields = array('suffix.id_suffix','suffix.suffix');
741                       
742                        $suffixes = $this->find($fields);
743                       
744                        if (!is_array($suffixes))
745                        {
746                                return false;
747                        }
748                       
749                        while (list(,$suffix) = each($suffixes))
750                        {
751                                $result[$suffix['id_suffix']] = $suffix['suffix'];
752                        }
753                       
754                        return $result;
755                }
756       
757                /*!
758               
759                 @function get_all_status
760                 @abstract Returns all the registered status
761                 @author Raphael Derosso Pereira
762                 @return An array as follows:
763                        $return = array(
764                                <id_status1> => '<status_name1>',
765                                <id_status2> => '<status_name2>',
766                                ...                             
767                        );
768                 
769                */
770                function get_all_status (  )
771                {
772                        $fields = array('status.id_status','status.status_name');
773                       
774                        $status = $this->find($fields);
775                       
776                        if (!is_array($status))
777                        {
778                                return false;
779                        }
780                       
781                        while (list(,$status_) = each($status))
782                        {
783                                $result[$status_['id_status']] = $status_['status_name'];
784                        }
785                       
786                        return $result;
787                }
788
789                /*!
790               
791                 @function get_all_relations_types
792                 @abstract Returns all contacts relations types
793                 @author Raphael Derosso Pereira
794                 
795                 @return array The format of the return is:
796                        $return = array(
797                                <id_type1> => '<type1_name>',
798                                <id_type2> => '<type2_name>',
799                                ...);
800                */
801                function get_all_relations_types (  )
802                {
803                        $fields = array('typeof_contact_relation.id_typeof_contact_relation', 'typeof_contact_relation.contact_relation_type_name');
804                       
805                        $relation_types = $this->find($fields);
806                       
807                        if (!is_array($relation_types))
808                        {
809                                return false;
810                        }
811                       
812                        while (list(,$relation_type) = each($relation_types))
813                        {
814                                $result[$relation_type['id_typeof_contact_relation']] = $relation_type['contact_relation_type_name'];                           
815                        }
816                       
817                        return $result;
818                }
819       
820                /*!
821               
822                 @function get_all_addresses_types
823                 @abstract Returns all contacts addresses types
824                 @author Raphael Derosso Pereira
825                 
826                 @return array The format of the return is:
827                        $return = array(
828                                <id_type1> => '<type1_name>',
829                                <id_type2> => '<type2_name>',
830                                ...);
831                */
832                function get_all_addresses_types (  )
833                {
834                        $fields = array('typeof_contact_address.id_typeof_contact_address', 'typeof_contact_address.contact_address_type_name');
835                       
836                        $address_types = $this->find($fields);
837                       
838                        if (!is_array($address_types))
839                        {
840                                return false;
841                        }
842                       
843                        while (list(,$address_type) = each($address_types))
844                        {
845                                $result[$address_type['id_typeof_contact_address']] = $address_type['contact_address_type_name'];                               
846                        }
847                       
848                        return $result;
849                }
850
851                /*!
852               
853                 @function get_all_connections_types
854                 @abstract Returns all contacts connections types
855                 @author Raphael Derosso Pereira
856                 
857                 @return array The format of the return is:
858                        $return = array(
859                                <id_type1> => '<type1_name>',
860                                <id_type2> => '<type2_name>',
861                                ...);
862                */
863                function get_all_connections_types (  )
864                {
865                        $fields = array('typeof_contact_connection.id_typeof_contact_connection', 'typeof_contact_connection.contact_connection_type_name');
866                       
867                        $connection_types = $this->find($fields);
868                       
869                        if (!is_array($connection_types))
870                        {
871                                return false;
872                        }
873                       
874                        while (list(,$connection_type) = each($connection_types))
875                        {
876                                $result[$connection_type['id_typeof_contact_connection']] = $connection_type['contact_connection_type_name'];                           
877                        }
878                       
879                        return $result;
880                }
881
882                /*!
883               
884                        @function get_vcard
885                        @abstract Returns an URL that points to the file
886                                that contains a vCard of the specified Contact
887                        @author Raphael Derosso Pereira
888               
889                        @param integer $id_status The Contact ID
890                       
891                */
892                function get_vcard ( $id_contact )
893                {
894                }
895
896
897                /*********************************************************************\
898                 *                   Methods to Include Data                         *
899                \*********************************************************************/
900
901                /*!
902               
903                        @function add_single_entry
904                        @abstract Insert a new Contact record in the DB
905                        @author Raphael Derosso Pereira
906               
907                        @param array $data The Contact Information.
908                       
909                        Format:
910               
911                                $data = array(
912                                        'id_status'          => <id_status>,
913                                        'photo'              => '<photo_bin_stream>',
914                                        'alias'              => '<alias>',
915                                        'id_prefix'          => <id_prefix>,
916                                        'given_names'        => '<given_names>',
917                                        'family_names'       => '<family_names>',
918                                        'names_ordered'      => '<names_ordered>',
919                                        'id_suffix'          => <id_suffix>,
920                                        'birthdate'          => '<birthdate>',
921                                        'sex'                => '<sex>',
922                                        'pgp_key'            => '<pgp_key>',
923                                        'notes'              => '<notes>',
924                                       
925                                        'companies'          => array(
926                                                company1 => array(
927                                                        'id_company'      => <id_company>,
928                                                        'company_name'    => <company_name>,
929                                                        'title'           => <title>,
930                                                        'department'      => <department>,
931                                                        'default_company' => (bool),
932                                                        'default_contact' => (bool)
933                                                ),
934                                                company2 => array(...),
935                                                ...
936                                        ),
937                                       
938                                        'relations'          => array(
939                                                'relation1' => array(
940                                                        'id_relation'        => <id_relation>,
941                                                        'id_typeof_relation' => <id_typeof_relation>,
942                                                ),
943                                                'relation2' => array(...),
944                                                ...
945                                        ),
946                                       
947                                        'addresses'          => array(
948                                                'address1' => array(
949                                                        'id_typeof_address'  => <id_typeof_address>,
950                                                        'address1'           => '<address1>',
951                                                        'address2'           => '<address2>',
952                                                        'complement'         => '<complement>',
953                                                        'address_other'      => '<address_other>',
954                                                        'postal_code'        => '<postal_code>',
955                                                        'po_box'                     => '<po_box>',
956                                                        'id_city'            => '<city>',
957                                                        'address_is_default' => <trueness>
958                                                ),
959                                                'address2' => array(...),
960                                                ...
961                                        ),
962                                       
963                                        'connections'        => array(
964                                                'connection1' => array(
965                                                        'id_typeof_connection'  => <id_typeof_connection>,
966                                                        'connection_name'       => <connection_name>,
967                                                        'connection_value'      => <connection_value>,
968                                                        'connection_is_default' => <trueness>
969                                                ),
970                                                'connection2' => array(...),
971                                                ...
972                                        ),
973                                       
974                                );
975                       
976                        If any of the above fields doesn't have a value, it should hold false.
977                        In the case of the multiple-values fields, instead of the array, there
978                        should be a false.
979
980                        @return integer $id The Contact ID
981                */
982                //alterada para receber a informacao em 'notes' - Rommel Cysne - rommel.cysne@serpro.gov.br
983                function add_single_entry ( $data )
984                {
985                        $permissions = $this->security->get_permissions();
986                       
987                        $exist_connections = false;
988                       
989                        if (!$permissions['create'])
990                        {
991                                //return false;
992                        }
993                       
994                        $contact = CreateObject('contactcenter.so_contact');
995                        $contact->reset_values();
996                       
997                        $altered = false;
998                        foreach($data as $field => $value)
999                        {
1000                                if ($value === false && $field != 'connections')
1001                                {
1002                                        continue;
1003                                }
1004                               
1005                                $altered = true;
1006                                switch($field)
1007                                {
1008                                        case 'photo':
1009                                        case 'alias':
1010                                        case 'id_prefix':
1011                                        case 'id_status':
1012                                        case 'id_suffix':
1013                                        case 'corporate_name':
1014                                        case 'job_title':
1015                                        case 'department':
1016                                        case 'web_page':
1017                                        case 'given_names':
1018                                        case 'family_names':
1019                                        case 'names_ordered':
1020                                        case 'birthdate':
1021                                        case 'sex':
1022                                        case 'pgp_key':
1023                                        case 'notes':
1024                                                $contact->set_field($field,$value);
1025                                                break;
1026
1027                                        case 'companies':                                       
1028                                                foreach($value as $company_fields)
1029                                                {
1030                                                        if ($company_fields['company_name'])
1031                                                        {
1032                                                                $fields = array('company.id_company');
1033                                                                $restric = array(
1034                                                                        0 => array(
1035                                                                                'field' => 'company.company_name',
1036                                                                                'type'  => 'iLIKE',
1037                                                                                'value' => $company_fields['company_name']
1038                                                                        )
1039                                                                );
1040                                                               
1041                                                                if($result = $this->find($fields,$restric))
1042                                                                {
1043                                                                        $id = $result[0]['id_company'];
1044                                                                        $company_fields['id_company'] = $id;
1045                                                                }
1046                                                                else
1047                                                                {
1048                                                                        $company = CreateObject('contactcenter.so_company');
1049                                                                        $company->reset_values();
1050                                                                        $company->set_company_name($company_fields['company_name']);
1051                                                                        $company->set_field('id_company_owner',$GLOBALS['phpgw_info']['user']['account_id']);
1052                                                                        $company_fields['id_company'] = $company->commit();
1053                                                                }
1054                                                        }
1055                                                       
1056                                                        $contact->set_company($company_fields);
1057                                                }
1058                                                break;
1059                                       
1060                                        case 'relations':
1061                                                foreach($value as $relation_fields)
1062                                                {
1063                                                        $contact->set_relation($relation_fields['id_relation'], $relation_fields['id_typeof_relation']);
1064                                                }
1065                                                break;
1066                                       
1067                                        case 'addresses':
1068                                                foreach($value as $address_fields)
1069                                                {
1070                                                        $address = CreateObject('contactcenter.so_address');
1071                                                        $address->reset_values();
1072                                                        foreach($address_fields as $a_field => $a_value)
1073                                                        {
1074                                                                if ($a_field !== 'id_typeof_address')
1075                                                                {
1076                                                                        $address->set_field($a_field,$a_value);
1077                                                                }
1078                                                        }
1079                                                        $address->commit();
1080                                                        $id_address = $address->get_id();
1081                                                        $contact->set_address($id_address, $address_fields['id_typeof_address']);
1082                                                }
1083                                                break;
1084                                       
1085                                        case 'connections':
1086                                                foreach($value as $connection_name => $connection_fields)
1087                                                {
1088                                                        $exist_connections = true;
1089                                                        $connection = CreateObject('contactcenter.so_connection');
1090                                                        $connection->reset_values();
1091                                                       
1092                                                        foreach($connection_fields as $a_field => $a_value)
1093                                                        {
1094                                                                if ($a_field !== 'id_typeof_connection')
1095                                                                {
1096                                                                        $connection->set_field($a_field,$a_value);
1097                                                                }
1098                                                        }
1099                                                       
1100                                                        $connection->commit();
1101                                                        $id_connection = $connection->get_id();
1102                                                        $contact->set_connection($id_connection, $connection_fields['id_typeof_connection']);
1103                                                }
1104                                                break;
1105                                               
1106                                        default:
1107                                                return false;
1108                                }
1109                        }
1110
1111                        if ($altered)
1112                        {
1113                                $contact->set_field('id_owner',$GLOBALS['phpgw_info']['user']['account_id']);
1114                                //insert a dummy connection if the contact dont have an email and telephone
1115                                if (!$exist_connections)
1116                                {
1117                                        $this->add_dummy($contact);
1118                                }
1119                                return $contact->commit();
1120                        }
1121                       
1122                        return false;
1123                }
1124               
1125                /*!
1126               
1127                        @function quick_add
1128                        @abstract Insert a new Contact record in the DB with just the
1129                                main fields
1130                        @author Raphael Derosso Pereira
1131               
1132                        @param array $data The Contact Information.
1133                       
1134                        Format:
1135               
1136                                $data = array(
1137                                        'alias'              => '<alias>',
1138                                        'given_names'        => '<given_names>',
1139                                        'family_names'       => '<family_names>',
1140
1141                                        'connections'        => array(
1142                                                'default_email' => array(
1143                                                        'connection_name'       => <connection_name>,
1144                                                        'connection_value'      => <connection_value>
1145                                                ),
1146                                                'default_phone' => array(
1147                                                        'connection_name'       => <connection_name>,
1148                                                        'connection_value'      => <connection_value>
1149                                                )
1150                                        )
1151                                );
1152                */
1153                //alterada para receber os outros parametros existentes na adicao completa de contatos pessoais
1154                //Rommel Cysne - rommel.cysne@serpro.gov.br
1155                function quick_add ( $data )
1156                {
1157                        $permissions = $this->security->get_permissions();
1158                       
1159                        if (!$permissions['create'])
1160                        {
1161                                return false;
1162                        }
1163
1164                        // TODO: GET THE ORDER TO PUT names_ordered FROM PREFERENCES!
1165
1166                        $preferences = ExecMethod('contactcenter.ui_preferences.get_preferences');
1167                       
1168                        if ( !isset($preferences['personCardEmail']) || ( !is_numeric($preferences['personCardEmail'])) )
1169                        {
1170                                $preferences['personCardEmail'] = 1;
1171                        }
1172                        if ( !isset($preferences['personCardPhone']) || ( !is_numeric($preferences['personCardPhone'])) )
1173                        {
1174                                $preferences['personCardPhone'] = 2;
1175                        }
1176                       
1177                        if(!$data['is_quick_add']){                     
1178                                $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']."'))";     
1179                                                       
1180                                $this->db->query($sql, __LINE__, __FILE__);
1181                               
1182                                $corporative_state = array('id_state'=>2);
1183                                while($this->db->next_record())
1184                                {
1185                                        $corporative_state = $this->db->row();
1186                                }
1187                               
1188                                $sql = "select * FROM phpgw_cc_city where id_state = ".$corporative_state['id_state']." and upper(city_name) = upper('".$data['addresses']['address_corporative']['id_city']."') ";
1189                                                                               
1190                                $this->db->query($sql, __LINE__, __FILE__);
1191                               
1192                                $corporative_city = array('id_city'=>null);
1193                                while($this->db->next_record())
1194                                {
1195                                        $corporative_city = $this->db->row();
1196                                }
1197                               
1198                                //Personal Address
1199                               
1200                                $sql = "select * FROM phpgw_cc_state where upper(state_name) = upper('".$data['addresses']['address_personal']['id_state']."')";       
1201                                                       
1202                                $this->db->query($sql, __LINE__, __FILE__);
1203                               
1204                                $personal_state = array('id_state'=>2);
1205                                while($this->db->next_record())
1206                                {
1207                                        $personal_state = $this->db->row();
1208                                }
1209                               
1210                                $sql = "select * FROM phpgw_cc_city where id_state = ".$personal_state['id_state']." and upper(city_name) = upper('".$data['addresses']['address_personal']['id_city']."') ";
1211                                                                               
1212                                $this->db->query($sql, __LINE__, __FILE__);
1213                               
1214                                $personal_city = array('id_city'=>null);
1215                                while($this->db->next_record())
1216                                {
1217                                        $personal_city = $this->db->row();
1218                                }
1219                        }
1220                       
1221                        $new_data = array(
1222                                'notes'              => $data['notes'],
1223                                'alias'              => $data['alias'],
1224                                'id_status'          => 1,
1225                                'given_names'        => $data['given_names'],
1226                                'family_names'       => $data['family_names'],
1227                                'names_ordered'      => $data['given_names'].' '.$data['family_names'],
1228                                'family_names'       => $data['family_names'],
1229                                'alias'                  => $data['alias'],
1230                                'corporate_name'     => $data['corporate_name'],
1231                                'job_title'              => $data['job_title'],
1232                                'department'             => $data['department'],
1233                                'web_page'               => $data['web_page'],
1234                                'birthdate'                      => $data['birthdate'],
1235                                'pgp_key'                => $data['pgp_key'],
1236                                'sex'                    => $data['sex'],
1237                        );
1238
1239                        $i = 1;
1240                        if ($data['connections']['default_email']['connection_value'])
1241                        {
1242                                $new_data['connections']['connection'.$i] = array(
1243                                        'id_typeof_connection'  => $preferences['personCardEmail'],
1244                                        'connection_name'       => $data['connections']['default_email']['connection_name'],
1245                                        'connection_value'      => $data['connections']['default_email']['connection_value'],
1246                                        'connection_is_default' => 1,
1247                                );
1248                                $i++;
1249                        }
1250                       
1251                        if ($data['connections']['aditional_email']['connection_value'])
1252                        {
1253                                $new_data['connections']['connection'.$i] = array(
1254                                        'id_typeof_connection'  => $preferences['personCardEmail'],
1255                                        'connection_name'       => $data['connections']['aditional_email']['connection_name'],
1256                                        'connection_value'      => $data['connections']['aditional_email']['connection_value'],
1257                                        'connection_is_default' => 0,
1258                                );
1259                                $i++;
1260                        }
1261                                                       
1262                        if ($data['connections']['default_phone']['connection_value'])
1263                        {
1264                                $new_data['connections']['connection'.$i] = array(
1265                                        'id_typeof_connection'  => $preferences['personCardPhone'],
1266                                        'connection_name'       => $data['connections']['default_phone']['connection_name'],
1267                                        'connection_value'      => $data['connections']['default_phone']['connection_value'],
1268                                        'connection_is_default' => 1,
1269                                );
1270                               
1271                               
1272                                foreach($data['connections']['aditional_phone'] as $connection_name => $connection_fields)
1273                                {
1274                                        if($connection_fields['connection_value'] != ''){
1275                                                $i++;
1276                                                $new_data['connections']['connection'.$i] = array(
1277                                                'id_typeof_connection'  => $preferences['personCardPhone'],
1278                                                'connection_name'       => $connection_fields['connection_name'],
1279                                                'connection_value'      => $connection_fields['connection_value'],
1280                                                'connection_is_default' => 0,
1281                                                );
1282                                        }                                       
1283                                }
1284                               
1285                        }else{
1286                                $default =1;
1287                                foreach($data['connections']['aditional_phone'] as $connection_name => $connection_fields)
1288                                {
1289                                        $i++;
1290                                        $new_data['connections']['connection'.$i] = array(
1291                                        'id_typeof_connection'  => $preferences['personCardPhone'],
1292                                        'connection_name'       => $connection_fields['connection_name'],
1293                                        'connection_value'      => $connection_fields['connection_value'],
1294                                        'connection_is_default' => $default,
1295                                        );
1296                                        $default = 0;                                   
1297                                }
1298                        }
1299                        if(!$data['is_quick_add']){                     
1300                                $new_data['addresses']['address'.$i] = array(
1301                                        'id_typeof_address'  => 2,
1302                                        'address1'       => $data['addresses']['address_personal']['address1'],
1303                                        'address2'       => $data['addresses']['address_personal']['address2'],
1304                                        'complement'     => $data['addresses']['address_personal']['complement'],
1305                                        'postal_code'    => $data['addresses']['address_personal']['postal_code'],
1306                                        'id_country'     => $data['addresses']['address_personal']['id_country'],
1307                                        'id_state'               => $personal_state['id_state'],
1308                                        'id_city'                => $personal_city['id_city'],
1309                                        'address_is_default' => 0,
1310                                );
1311                                $i++;
1312                       
1313                                $new_data['addresses']['address'.$i] = array(
1314                                        'id_typeof_address'  => 1,
1315                                        'address1'       => $data['addresses']['address_corporative']['address1'],
1316                                        'address2'       => $data['addresses']['address_corporative']['address2'],
1317                                        'complement'     => $data['addresses']['address_corporative']['complement'],
1318                                        'postal_code'    => $data['addresses']['address_corporative']['postal_code'],
1319                                        'id_country'     => $data['addresses']['address_corporative']['id_country'],
1320                                        'id_state'               => $corporative_state['id_state'],
1321                                        'id_city'                => $corporative_city['id_city'],
1322                                        'address_is_default' => 1,
1323                                );                                                     
1324                                $i++;
1325                        }
1326
1327                        $j = 1;
1328
1329                        /*if($data['addresses']['addr1']['address_value'])
1330                        {
1331                                $new_data['addresses']['address'.$j] = array(
1332                                        'id_typeof_address'     => 1, //<id_typeof_address> - 1 - Comercial; 2 - Residencial
1333                                        'address1'              => $data['addresses']['addr1']['address_value'],
1334                                        'address2'              => $data['addresses']['addr2']['address_value'],
1335                                        'id_country'            => 'BR',
1336                                        //'complement'          => '<complement>',
1337                                        //'address_other'       => '<address_other>',
1338                                        'postal_code'           => $data['addresses']['cep']['address_value'],
1339                                        //'po_box'              => '<po_box>',
1340                                        //'id_city'             => '<city>',
1341                                        'address_is_default'    => 'false',
1342                                );
1343                                $j++;
1344                        }*/
1345
1346
1347                        return $this->add_single_entry($new_data);
1348                }
1349       
1350                /*!
1351               
1352                        @function add_prefix
1353                        @abstract Insert a new Prefix in the DB
1354                        @author Raphael Derosso Pereira
1355               
1356                        @param string $prefix The Prefix
1357                        @return integer The new ID
1358                       
1359                */
1360                function add_prefix ( $prefix )
1361                {
1362                        $permissions = $this->security->get_type_permissions();
1363                       
1364                        if (!$permissions['create'])
1365                        {
1366                                return false;
1367                        }
1368                }
1369       
1370                /*!
1371               
1372                        @function add_suffix
1373                        @abstract Insert a new suffix in the DB
1374                        @author Raphael Derosso Pereira
1375               
1376                        @param string $suffix The suffix
1377                        @return integer The new ID
1378                       
1379                */
1380                function add_suffix ( $suffix )
1381                {
1382                        $permissions = $this->security->get_type_permissions();
1383                       
1384                        if (!$permissions['create'])
1385                        {
1386                                return false;
1387                        }
1388                }
1389       
1390                /*!
1391               
1392                        @function add_status
1393                        @abstract Insert a new Status in the DB
1394                        @author Raphael Derosso Pereira
1395               
1396                        @param string $status The Status
1397                        @return integer The new ID
1398                       
1399                */
1400                function add_status ( $status )
1401                {
1402                        $permissions = $this->security->get_type_permissions();
1403                       
1404                        if (!$permissions['create'])
1405                        {
1406                                return false;
1407                        }
1408                }
1409       
1410                /*!
1411               
1412                        @function add_relation_type
1413                        @abstract Insert a new Relation Type in the DB
1414                        @author Raphael Derosso Pereira
1415               
1416                        @param string $type_name The Relation Type
1417                        @return integer The new ID
1418                       
1419                */
1420                function add_relation_type ( $type_name )
1421                {
1422                        $permissions = $this->security->get_type_permissions();
1423                       
1424                        if (!$permissions['create'])
1425                        {
1426                                return false;
1427                        }
1428                }
1429       
1430                /*!
1431               
1432                        @function add_address_type
1433                        @abstract Insert a new Address Type in the DB
1434                        @author Raphael Derosso Pereira
1435               
1436                        @param string $type_name The Address Type
1437                        @return integer The new ID
1438                       
1439                */
1440                function add_address_type ( $type_name )
1441                {
1442                        $permissions = $this->security->get_type_permissions();
1443                       
1444                        if (!$permissions['create'])
1445                        {
1446                                return false;
1447                        }
1448                }
1449       
1450                /*!
1451               
1452                        @function add_connection_type
1453                        @abstract Insert a new Connection Type in the DB
1454                        @author Raphael Derosso Pereira
1455               
1456                        @param string $type_name The Connection Type
1457                        @return integer The new ID
1458                       
1459                */
1460                function add_connection_type ( $type_name )
1461                {
1462                        $permissions = $this->security->get_type_permissions();
1463                       
1464                        if (!$permissions['create'])
1465                        {
1466                                return false;
1467                        }
1468                }
1469       
1470
1471
1472                /*********************************************************************\
1473                 *                   Methods to Alter Data                           *
1474                \*********************************************************************/
1475
1476                /*!
1477               
1478                        @function update_single_info
1479                        @abstract Update information of an existing Contact
1480                        @author Raphael Derosso Pereira
1481               
1482                        @param integer $id_status The Contact ID
1483                        @param string $status The new Status value
1484                       
1485                */
1486                function update_single_info ( $id_contact, $data )
1487                {
1488                        $permissions = $this->security->get_permissions($id_contact);
1489                       
1490                        if (!$permissions['write'])
1491                        {
1492                                return false;
1493                        }
1494                       
1495                        $contact = CreateObject('contactcenter.so_contact', $id_contact);
1496                       
1497                        $altered = false;
1498//                      print_r($data);
1499                        foreach($data as $field => $value)
1500                        {
1501                                if ($value === false)
1502                                {
1503                                        continue;
1504                                }
1505                               
1506                                $altered = true;
1507                                switch($field)
1508                                {
1509                                        case 'photo':
1510                                        case 'alias':
1511                                        case 'id_prefix':
1512                                        case 'id_status':
1513                                        case 'id_suffix':
1514                                        case 'corporate_name':
1515                                        case 'job_title':
1516                                        case 'department':
1517                                        case 'web_page':                                       
1518                                        case 'given_names':
1519                                        case 'family_names':
1520                                        case 'names_ordered':
1521                                        case 'birthdate':
1522                                        case 'sex':
1523                                        case 'pgp_key':
1524                                        case 'notes':
1525                                                $contact->set_field($field,$value);
1526                                                break;
1527
1528                                        case 'companies':                                       
1529                                                foreach($value as $company_fields)
1530                                                {
1531                                                        if ($company_fields['company_name'])
1532                                                        {
1533                                                                $fields = array('company.id_company');
1534                                                                $restric = array(
1535                                                                        0 => array(
1536                                                                                'field' => 'company.company_name',
1537                                                                                'type'  => 'iLIKE',
1538                                                                                'value' => $company_fields['company_name']
1539                                                                        )
1540                                                                );
1541                                                               
1542                                                                if($result = $this->find($fields,$restric))
1543                                                                {
1544                                                                        $id = $result[0]['id_company'];
1545                                                                        $company_fields['id_company'] = $id;
1546                                                                }
1547                                                                else
1548                                                                {
1549                                                                        $company = CreateObject('contactcenter.so_company');
1550                                                                        $company->reset_values();
1551                                                                        $company->set_company_name($company_fields['company_name']);
1552                                                                        $company->set_field('id_company_owner',$GLOBALS['phpgw_info']['user']['account_id']);
1553                                                                        $company_fields['id_company'] = $company->commit();
1554                                                                }
1555                                                        }
1556                                                       
1557                                                        $contact->set_company($company_fields);
1558                                                }
1559                                                break;
1560                                       
1561                                        case 'relations':
1562                                                foreach($value as $relation_fields)
1563                                                {
1564                                                        $contact->set_relation($relation_fields['id_relation'], $relation_fields['id_typeof_relation']);
1565                                                }
1566                                                break;
1567                                       
1568                                        case 'addresses':
1569                                                foreach($value as $address_name => $address_fields)
1570                                                {
1571                                                        if ($address_fields['id_address'] && $address_fields['id_address'] !== '')
1572                                                        {
1573                                                                $address = CreateObject('contactcenter.so_address', $address_fields['id_address']);
1574                                                        }
1575                                                        else
1576                                                        {
1577                                                                $address = CreateObject('contactcenter.so_address');
1578                                                                $address->reset_values();
1579                                                        }
1580
1581                                                        if (!isset($address_fields['id_country']) or $address_fields['id_country'] === false)
1582                                                        {
1583                                                                #var_dump($address_fields);
1584                                                                echo(serialize(array(
1585                                                                        'file' => __FILE__,
1586                                                                        'line' => __LINE__,
1587                                                                        'msg'  => lang('An Address must have at least a Country'),
1588                                                                        'status' => 'aborted'
1589                                                                )));
1590                                                                return;
1591                                                        }
1592                                                       
1593                                                        foreach ($address_fields as $f_name => $f_value)
1594                                                        {
1595                                                                if ($f_value === false)
1596                                                                {
1597                                                                        $address->set_field($f_name, null);
1598                                                                }
1599                                                                elseif (isset($f_value))
1600                                                                {
1601                                                                        $address->set_field($f_name, $f_value);
1602                                                                }
1603                                                        }
1604                                                               
1605                                                        $address->commit();
1606                                                        $id_address = $address->get_id();
1607                                                        $contact->set_address($id_address, $address_fields['id_typeof_address']);
1608                                                }
1609                                                break;
1610                                       
1611                                        case 'connections':
1612                                                //alteracao para permitir a exibicao de contatos sem email ou telefone
1613                                                $all_removed = true;
1614                                                $added_connection = false;
1615                                                foreach($value as $connection_name => $connection_fields)
1616                                                {
1617                                                        if ($connection_name === 'removed_conns')
1618                                                        {
1619                                                                foreach($connection_fields as $id)
1620                                                                {
1621                                                                        $connection = CreateObject('contactcenter.so_connection', $id);
1622                                                                        if (!($connection->remove()))
1623                                                                        {
1624                                                                                return false;
1625                                                                        }
1626
1627                                                                        $contact->remove_connection($id);
1628                                                                }
1629
1630                                                                continue;
1631                                                        }
1632                                                       
1633                                                        $all_removed = false;
1634                                                        // removes the dummy if there is at least one connection
1635                                                        if (!$added_connection)
1636                                                        {
1637                                                                $this->remove_dummy($contact); //remove the dummy
1638                                                        }
1639                                                        $added_connection = true;
1640                                                        $id_connection = $connection_fields['id_connection'];
1641                                                        if ($id_connection === '_NEW_' or
1642                                                            $id_connection === '')
1643                                                        {
1644                                                                $connection = CreateObject('contactcenter.so_connection');
1645                                                                $connection->reset_values();
1646                                                        }
1647                                                        else
1648                                                        {
1649                                                                $connection = CreateObject('contactcenter.so_connection', $id_connection);
1650                                                        }
1651                                                               
1652                                                        foreach($connection_fields as $a_field => $a_value)
1653                                                        {
1654                                                                if ($a_field !== 'id_typeof_connection')
1655                                                                {
1656                                                                        $connection->set_field($a_field,$a_value);
1657                                                                }
1658                                                        }
1659                                                       
1660                                                        if (!$connection->commit())
1661                                                        {
1662                                                                return false;
1663                                                        }
1664                                                        $id_connection = $connection->get_id();
1665                                                        $contact->set_connection($id_connection, $connection_fields['id_typeof_connection']);
1666                                                }
1667                                                // add the dummy if there is no other connection
1668                                                if ($all_removed)
1669                                                        $this->add_dummy($contact);
1670                                                break;
1671                                               
1672                                        default:
1673                                                echo 'Invalid Field: '.$field.'<br>Value: '.$value;
1674                                                return false;
1675                                }
1676                        }
1677                       
1678                        if ($altered)
1679                        {
1680                                $contact->set_field("last_status", 'U');
1681                                $contact->set_field("last_update",time()."000");
1682                                return $contact->commit();
1683                        }
1684                       
1685                        return false;
1686                }
1687       
1688                /*!
1689                        @function remove_dummy
1690                        @abstract Remove the dummy connection entry.
1691                        @author Luiz Carlos Viana Melo - Prognus
1692                 */
1693                function remove_dummy($so_contact)
1694                {
1695                        //$so_contact = CreateObject('contactcenter.so_contact', $id_contact);
1696                        $connections = $so_contact->get_connections();
1697                        foreach ($connections as $id_connection => $type_connection)
1698                        {
1699                                $so_connection = CreateObject('contactcenter.so_connection', $id_connection);
1700                                if ($so_connection->get_field('connection_name') === 'no_conns') //if connection is the dummy one
1701                                {
1702                                        if (!$so_connection->remove())
1703                                        {
1704                                                return false;
1705                                        }
1706                                        $so_contact->remove_connection($id_connection);
1707                                        //$so_connection->commit();
1708                                        break;
1709                                }
1710                        }
1711               
1712                }
1713               
1714                /*!
1715                        @function add_dummy
1716                        @abstract Add a dummy connection entry on a contact. This is necessary for
1717                                possibility on adding a contact that dont have an email or telephone.
1718                        @return The dummy's connection ID.
1719                        @author Luiz Carlos Viana Melo - Prognus
1720                 */
1721                function add_dummy($so_contact)
1722                {
1723                        $so_connection = CreateObject('contactcenter.so_connection');
1724                        $so_connection->reset_values();
1725                       
1726                        $so_connection->set_field('connection_name', 'no_conns'); //defined code for the dummy connection
1727                        $so_connection->set_field('connection_value', '');
1728                        $so_connection->set_field('connection_is_default', true);
1729                       
1730                        if (!$so_connection->commit())
1731                        {
1732                                return false;
1733                        }
1734                        $id_connection = $so_connection->get_id();
1735                        $so_contact->set_connection($id_connection, 2);
1736                        return $id_connection;
1737                }
1738                                       
1739                /*!
1740               
1741                        @function update_prefix
1742                        @abstract Update an existing Prefix
1743                        @author Raphael Derosso Pereira
1744               
1745                        @param integer $id_prefix The Prefix ID
1746                        @param string $prefix The new Prefix value
1747                       
1748                */
1749                function update_prefix ( $id_prefix, $prefix )
1750                {
1751                        $permissions = $this->security->get_type_permissions();
1752                       
1753                        if (!$permissions['alter'])
1754                        {
1755                                return false;
1756                        }
1757                }
1758       
1759                /*!
1760               
1761                        @function update_suffix
1762                        @abstract Update an existing suffix
1763                        @author Raphael Derosso Pereira
1764               
1765                        @param integer $id_suffix The suffix ID
1766                        @param string $suffix The new suffix value
1767                       
1768                */
1769                function update_suffix ( $id_suffix, $suffix )
1770                {
1771                        $permissions = $this->security->get_type_permissions();
1772                       
1773                        if (!$permissions['alter'])
1774                        {
1775                                return false;
1776                        }
1777                }
1778       
1779                /*!
1780               
1781                        @function update_status
1782                        @abstract Update an existing Status
1783                        @author Raphael Derosso Pereira
1784               
1785                        @param integer $id_status The Status ID
1786                        @param string $status The new Status value
1787                       
1788                */
1789                function update_status ( $id_status, $status )
1790                {
1791                        $permissions = $this->security->get_type_permissions();
1792                       
1793                        if (!$permissions['alter'])
1794                        {
1795                                return false;
1796                        }
1797                }
1798       
1799                /*!
1800               
1801                        @function update_relation_type
1802                        @abstract Update an existing Relation Type
1803                        @author Raphael Derosso Pereira
1804               
1805                        @param integer $id_type The Type ID
1806                        @param string $type The new type value
1807                       
1808                */
1809                function update_relation_type ( $old_name, $relation_name )
1810                {
1811                }
1812       
1813                /*!
1814               
1815                        @function update_address_type
1816                        @abstract Update an existing Address Type
1817                        @author Raphael Derosso Pereira
1818               
1819                        @param integer $id_uype The Type ID
1820                        @param string $type The new type value
1821                       
1822                */
1823                function update_address_type ( $id_type, $type )
1824                {
1825                }
1826       
1827                /*!
1828               
1829                        @function update_connection_type
1830                        @abstract Update an existing Connection Type
1831                        @author Raphael Derosso Pereira
1832               
1833                        @param integer $id_type The Type ID
1834                        @param string $type The new type value
1835                       
1836                */
1837                function update_connection_type ( $id_type, $type )
1838                {
1839                }
1840
1841
1842                /*********************************************************************\
1843                 *                   Methods to Remove Data                          *
1844                \*********************************************************************/
1845
1846                /*!
1847               
1848                        @function remove_single_entry
1849                        @abstract Remove one contact from the DB
1850                        @author Raphael Derosso Pereira
1851               
1852                        @param integer $id_contact The Contact ID
1853                       
1854                */
1855                function remove_single_entry ( $id_contact )
1856                {
1857                        $permissions = $this->security->get_permissions();
1858                       
1859                        if (!$permissions['remove'])
1860                        {
1861                                return false;
1862                        }
1863                       
1864                        $contact = CreateObject('contactcenter.so_contact', $id_contact);
1865                       
1866                        if ($contact->get_state() === 'new')
1867                        {
1868                                return false;
1869                        }
1870                       
1871                        $addresses = $contact->get_addresses();
1872                        $connections = $contact->get_connections();
1873                       
1874                        if (!$contact->remove())
1875                        {
1876                                return false;
1877                        }
1878                       
1879                        foreach ($addresses as $id => $type)
1880                        {
1881                                if (!($address = CreateObject('contactcenter.so_address', $id)))
1882                                {
1883                                        return false;
1884                                }
1885                               
1886                                if (!($address->remove()))
1887                                {
1888                                        return false;
1889                                }
1890                               
1891                        }
1892                        $groups = CreateObject('contactcenter.so_group');
1893                       
1894                        foreach ($connections as $id => $type)
1895                        {
1896                                if (!($connection = CreateObject('contactcenter.so_connection',$id)))
1897                                {                                       
1898                                        return false;
1899                                }
1900                                $groups->deleteContactFromGroups($id);
1901                                if (!($connection->remove()))
1902                                {
1903                                        return false;
1904                                }
1905                               
1906                        }
1907                       
1908                        return true;
1909                }
1910        }
1911?>
Note: See TracBrowser for help on using the repository browser.