source: branches/2.2/contactcenter/inc/class.bo_shared_people_manager.inc.php @ 1496

Revision 1496, 42.3 KB checked in by wmerlotto, 15 years ago (diff)

Ticket #628 - Implementada a funcionalidade de adição no catálogo compartilhado.

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