source: branches/2.2/contactcenter/inc/class.ui_data.inc.php @ 3688

Revision 3688, 133.6 KB checked in by alexandrecorreia, 13 years ago (diff)

Ticket #1497 - Correcao para Exibir as contas compartilhadas no ContactCenter?.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2  /***************************************************************************\
3  * eGroupWare - Contacts Center                                              *
4  * http://www.egroupware.org                                                 *
5  * Written by:                                                               *
6  *  - Raphael Derosso Pereira <raphaelpereira@users.sourceforge.net>         *
7  *  - Jonas Goes <jqhcb@users.sourceforge.net>                               *
8  *  sponsored by Thyamad - http://www.thyamad.com                            *
9  * ------------------------------------------------------------------------- *
10  *  This program is free software; you can redistribute it and/or modify it  *
11  *  under the terms of the GNU General Public License as published by the    *
12  *  Free Software Foundation; either version 2 of the License, or (at your   *
13  *  option) any later version.                                               *
14  \***************************************************************************/
15
16
17        class ui_data
18        {
19                var $public_functions = array(
20                        'data_manager' => true,
21                );
22
23                var $bo;
24                var $so_group;
25                var $typeContact;
26
27                var $page_info = array(
28                        'n_cards'          => 20,
29                        'n_pages'          => false,
30                        'actual_letter'    => 'A',
31                        'actual_page'      => 1,
32                        'actual_entries'   => false,
33                        'changed'          => false,
34                        'catalogs'         => false,
35                        'actual_catalog'   => false
36                );
37
38                /*!
39
40                        @function ui_data
41                        @abstract The constructor. Sets the initial parameters and loads
42                                the data saved in the session
43                        @author Raphael Derosso Pereira
44
45                */
46                function ui_data()
47                {
48                        $temp = $GLOBALS['phpgw']->session->appsession('ui_data.page_info','contactcenter');
49                        $temp2 = $GLOBALS['phpgw']->session->appsession('ui_data.all_entries','contactcenter');
50
51                        $this->bo = CreateObject('contactcenter.bo_contactcenter');
52
53                        if ($temp)
54                        {
55                                $this->page_info = $temp;
56                        }
57
58                        if ($temp2)
59                        {
60                                $this->all_entries = $temp2;
61                        }
62
63                        if (!$this->page_info['actual_catalog'])
64                        {
65                                $catalogs = $this->bo->get_catalog_tree();
66                                $this->page_info['actual_catalog'] = $catalogs[0];
67                        }
68
69                        $this->page_info['actual_catalog'] =& $this->bo->set_catalog($this->page_info['actual_catalog']);
70
71
72                        if($this->page_info['actual_catalog']['class'] == 'bo_group_manager')
73                                $this -> typeContact = 'groups';
74/**rev 104**/
75                        else if($this->page_info['actual_catalog']['class'] == 'bo_shared_group_manager')
76                                $this -> typeContact = 'shared_groups';
77                        else if($this->page_info['actual_catalog']['class'] == 'bo_shared_people_manager')
78                                $this -> typeContact = 'shared_contacts';
79/******/
80                        else
81                                $this -> typeContact = 'contacts';
82                }
83
84                /*!
85
86                        @function index
87                        @abstract Builds the Main Page
88                        @author Raphael Derosso Pereira
89                        @author Jonas Goes
90
91                */
92                function index()
93                {
94                        if(!@is_object($GLOBALS['phpgw']->js))
95                        {
96                                $GLOBALS['phpgw']->js = CreateObject('phpgwapi.javascript');
97                        }
98                        $GLOBALS['phpgw']->js->validate_file('venus','table');
99                        $GLOBALS['phpgw']->js->validate_file('venus','shapes');
100                        $GLOBALS['phpgw']->js->validate_file('venus','jsStructUtil');
101                        $GLOBALS['phpgw']->js->validate_file('venus','cssUtil');
102
103//                      $GLOBALS['phpgw']->js->set_onload('setTimeout(\'updateCards()\',1000)');
104                        $GLOBALS['phpgw']->common->phpgw_header();
105
106                        $GLOBALS['phpgw']->template->set_file(array('index' => 'index.tpl'));
107                        $GLOBALS['phpgw']->template->set_var('cc_root_dir', $GLOBALS['phpgw_info']['server']['webserver_url'].'/contactcenter/');
108
109                        /* Quick Add */
110                        $GLOBALS['phpgw']->template->set_var('cc_qa_alias',lang('Alias').':');
111                        $GLOBALS['phpgw']->template->set_var('cc_qa_given_names',lang('Given Names').':');
112                        $GLOBALS['phpgw']->template->set_var('cc_qa_family_names',lang('Family Names').':');
113                        $GLOBALS['phpgw']->template->set_var('cc_qa_phone',lang('Phone').':');
114                        $GLOBALS['phpgw']->template->set_var('cc_qa_email',lang('Email').':');
115                        $GLOBALS['phpgw']->template->set_var('cc_qa_save',lang('Save'));
116                        $GLOBALS['phpgw']->template->set_var('cc_qa_clear',lang('Clear'));
117                        $GLOBALS['phpgw']->template->set_var('cc_qa_close',lang('Close'));
118                        /* End Quick Add */
119
120                        /* Advanced Search */
121                       
122                        $GLOBALS['phpgw']->template->set_var('cc_corporate',lang('Corporate'));
123                        $GLOBALS['phpgw']->template->set_var('cc_cs_title',lang('Advanced Search'));
124                        $GLOBALS['phpgw']->template->set_var('cc_catalogues',lang('Catalogues'));
125                       
126                       
127                       
128                        /* End of Advanced Search*/
129
130                        $cc_css_file = $GLOBALS['phpgw_info']['server']['webserver_url'].'/contactcenter/styles/cc.css';
131                        $cc_card_image_file = $GLOBALS['phpgw_info']['server']['webserver_url'].'/contactcenter/templates/default/images/card.png';
132                        $GLOBALS['phpgw']->template->set_var('cc_css',$cc_css_file);
133                        $GLOBALS['phpgw']->template->set_var('cc_dtree_css', $cc_dtree_file);
134                        $GLOBALS['phpgw']->template->set_var('cc_card_image',$cc_card_image_file);
135
136                        $GLOBALS['phpgw']->template->set_var('cc_personal',lang('Personal'));
137
138/***rev 104***/
139                        //$GLOBALS['phpgw']->template->set_var('cc_full_add',lang('Full Add'));
140                        $GLOBALS['phpgw']->template->set_var('cc_full_add_button',lang('Full Add'));
141/******/
142                        $GLOBALS['phpgw']->template->set_var('cc_full_add_button_sh',lang('Full Add Shared'));
143                        $GLOBALS['phpgw']->template->set_var('cc_reset',lang('Reset'));
144
145                        $GLOBALS['phpgw']->template->set_var('cc_personal_data',lang('Personal Data'));
146                        $GLOBALS['phpgw']->template->set_var('cc_addresses',lang('Addresses'));
147                        $GLOBALS['phpgw']->template->set_var('cc_connections',lang('Connections'));
148                        $GLOBALS['phpgw']->template->set_var('cc_relations',lang('Relations'));
149
150                        $GLOBALS['phpgw']->template->set_var('cc_quick_add',lang('Quick Add'));
151                        $GLOBALS['phpgw']->template->set_var('cc_catalogs',lang('Catalogues'));
152                        $GLOBALS['phpgw']->template->set_var('cc_group_add',lang('Group Add'));
153
154                        /* Panel */
155                        $GLOBALS['phpgw']->template->set_var('cc_panel_new',lang('New').'...');
156                        $GLOBALS['phpgw']->template->set_var('cc_panel_search',lang('Search').'...');
157                        $GLOBALS['phpgw']->template->set_var('cc_panel_table',lang('Table View'));
158                        $GLOBALS['phpgw']->template->set_var('cc_panel_cards',lang('Cards View'));
159                        $GLOBALS['phpgw']->template->set_var('cc_btn_import_export', lang('Import/Export'));
160                        $GLOBALS['phpgw']->template->set_var('cc_btn_new', lang("New..."));
161
162                        $GLOBALS['phpgw']->template->set_var('cc_panel_search_found',lang('Showing found entries'));
163                        $GLOBALS['phpgw']->template->set_var('cc_panel_first_page',lang('First Page'));
164                        $GLOBALS['phpgw']->template->set_var('cc_panel_previous_page',lang('Previous Page'));
165                        $GLOBALS['phpgw']->template->set_var('cc_panel_next_page',lang('Next Page'));
166                        $GLOBALS['phpgw']->template->set_var('cc_panel_last_page',lang('Last Page'));
167                        $GLOBALS['phpgw']->template->set_var('cc_all',lang('all'));
168                        /* End Panel */
169
170                        /* Messages */
171/**rev 104**/
172                        $GLOBALS['phpgw']->template->set_var('cc_msg_not_allowed',lang('Not Allowed'));
173                        $GLOBALS['phpgw']->template->set_var('cc_msg_unavailable',lang('Unavailable function'));
174/*****/
175
176
177                        $GLOBALS['phpgw']->template->set_var('cc_msg_name_mandatory',lang('Name is mandatory'));
178                        $GLOBALS['phpgw']->template->set_var('cc_msg_err_invalid_serch',lang('The query should not have the characters {%,?}'));
179                        $GLOBALS['phpgw']->template->set_var('cc_msg_tel_or_mail_required',lang('Tel or email is required'));
180
181                        $GLOBALS['phpgw']->template->set_var('cc_msg_no_cards',lang('No Cards'));
182                        $GLOBALS['phpgw']->template->set_var('cc_msg_err_no_room',lang('No Room for Cards! Increase your browser area.'));
183                        $GLOBALS['phpgw']->template->set_var('cc_msg_card_new',lang('New from same Company'));
184                        $GLOBALS['phpgw']->template->set_var('cc_msg_card_edit',lang('Edit Contact'));
185                        $GLOBALS['phpgw']->template->set_var('cc_msg_card_remove',lang('Remove Contact'));
186                        $GLOBALS['phpgw']->template->set_var('cc_send_mail',lang('Send Mail'));
187                        $GLOBALS['phpgw']->template->set_var('cc_msg_group_edit',lang('Edit Group'));
188                        $GLOBALS['phpgw']->template->set_var('cc_msg_group_remove',lang('Remove Group'));
189                        $GLOBALS['phpgw']->template->set_var('cc_msg_group_remove_confirm',lang('Confirm Removal of this Group?'));
190                        $GLOBALS['phpgw']->template->set_var('cc_msg_card_remove_confirm',lang('Confirm Removal of this Contact?'));
191                        $GLOBALS['phpgw']->template->set_var('cc_participants',lang('Participants'));
192                        $GLOBALS['phpgw']->template->set_var('cc_empty',lang('Empty'));
193                        /* End Messages */
194
195                        $GLOBALS['phpgw']->template->set_var('cc_results',lang('Results'));
196                        $GLOBALS['phpgw']->template->set_var('cc_is_my',lang('Is My'));
197                        $GLOBALS['phpgw']->template->set_var('cc_ie_personal',lang('Import/Export pesonal contacts'));
198                        $GLOBALS['phpgw']->template->set_var('cc_btn_search',lang('Search'));
199                        $GLOBALS['phpgw']->template->set_var('cc_add_relation',lang('Add Relation'));
200                        $GLOBALS['phpgw']->template->set_var('cc_del_relation',lang('Remove Selected Relations'));
201                        $GLOBALS['phpgw']->template->set_var('cc_msg_group',lang('Group'));
202                        $GLOBALS['phpgw']->template->set_var('cc_msg_contact_full',lang('Contact [Full]'));
203                        $GLOBALS['phpgw']->template->set_var('cc_msg_contact_sh',lang('Contact [Shared]'));
204                        $GLOBALS['phpgw']->template->set_var('cc_msg_contact_qa',lang('Contact [Quick Add]'));
205                        $GLOBALS['phpgw']->template->set_var('cc_contact_title',lang('Contact Center').' - '.lang('Contacts'));
206                        $GLOBALS['phpgw']->template->set_var('cc_window_views_title',lang('Contact Center').' - '.lang('Views'));
207                        $GLOBALS['phpgw']->template->set_var('phpgw_img_dir', $GLOBALS['phpgw_info']['server']['webserver_url'] . '/phpgwapi/images');
208
209                        $GLOBALS['phpgw']->template->set_var('cc_msg_import_contacts', lang('Import Contacts'));
210                        $GLOBALS['phpgw']->template->set_var('cc_msg_export_contacts', lang('Export Contacts'));
211                        $GLOBALS['phpgw']->template->set_var('cc_msg_expresso_info_csv', lang('The Expresso supports the contacts importation in the CSV file format.'));
212                        $GLOBALS['phpgw']->template->set_var('cc_msg_choose_file_type', lang('Select the file type'));
213                        $GLOBALS['phpgw']->template->set_var('cc_msg_outlook_express', lang('Outlook Express'));
214                        $GLOBALS['phpgw']->template->set_var('cc_msg_outlook2k', lang('Outlook 2000'));
215                        $GLOBALS['phpgw']->template->set_var('cc_msg_expresso_default', lang('Expresso (default)'));
216                        $GLOBALS['phpgw']->template->set_var('cc_msg_choose_contacts_file', lang('Select the file that contains the contacts to be imported:'));
217                        $GLOBALS['phpgw']->template->set_var('cc_msg_close_win', lang('Close'));
218                        $GLOBALS['phpgw']->template->set_var('cc_msg_close', lang('Close'));
219                        $GLOBALS['phpgw']->template->set_var('cc_msg_ie_personal', lang('Import / Export personal Contacts'));
220                        $GLOBALS['phpgw']->template->set_var('cc_msg_import_fail', lang('The importation has failed. Verify the file format.'));
221                        $GLOBALS['phpgw']->template->set_var('cc_msg_importing_contacts', lang('Importing Contacts...'));
222                        $GLOBALS['phpgw']->template->set_var('cc_msg_import_finished', lang('The importation has finished.'));
223                        $GLOBALS['phpgw']->template->set_var('cc_msg_new', lang(' new'));
224                        $GLOBALS['phpgw']->template->set_var('cc_msg_failure', lang(' failed'));
225                        $GLOBALS['phpgw']->template->set_var('cc_msg_exists', lang(' were existent'));
226                        $GLOBALS['phpgw']->template->set_var('cc_msg_show_more_info', lang('show more info'));
227                        $GLOBALS['phpgw']->template->set_var('cc_msg_clean', lang('Clean'));
228                        $GLOBALS['phpgw']->template->set_var('cc_msg_invalid_csv', lang('Select a valid CSV file to import your contacts'));
229                        $GLOBALS['phpgw']->template->set_var('cc_msg_export_csv', lang('Select the format type that you want to export your contacts'));
230                        $GLOBALS['phpgw']->template->set_var('cc_msg_automatic', lang('Automatic'));
231                        $GLOBALS['phpgw']->template->set_var('cc_msg_export_error', lang('An error has occurred while the exportation.'));
232                        $GLOBALS['phpgw']->template->set_var('cc_msg_new_email', lang('New Email'));
233                        $GLOBALS['phpgw']->template->set_var('cc_msg_main', lang('Main'));
234                        $GLOBALS['phpgw']->template->set_var('cc_msg_alternative', lang('Alternative'));
235                        $GLOBALS['phpgw']->template->set_var('cc_msg_select_email', lang('Select E-Mail'));
236                        $GLOBALS['phpgw']->template->set_var('cc_msg_new_phone', lang('New Telephone'));
237                        $GLOBALS['phpgw']->template->set_var('cc_msg_home', lang('Home Phone'));
238                        $GLOBALS['phpgw']->template->set_var('cc_msg_cellphone', lang('Cellphone'));
239                        $GLOBALS['phpgw']->template->set_var('cc_msg_corporative_cellphone', lang('Corporative Cellphone'));
240                        $GLOBALS['phpgw']->template->set_var('cc_msg_corporative_fax', lang('Corporative Fax'));
241                        $GLOBALS['phpgw']->template->set_var('cc_msg_corporative_pager', lang('Corporative Pager'));
242                        $GLOBALS['phpgw']->template->set_var('cc_msg_work', lang('Work'));
243                        $GLOBALS['phpgw']->template->set_var('cc_msg_fax', lang('Fax'));
244                        $GLOBALS['phpgw']->template->set_var('cc_msg_pager', lang('Pager'));
245                        $GLOBALS['phpgw']->template->set_var('cc_msg_choose_phone', lang('Select the telephone'));
246                        $GLOBALS['phpgw']->template->set_var('cc_msg_warn_firefox', lang('Warning: Too old version of Firefox'));
247                        $GLOBALS['phpgw']->template->set_var('cc_msg_firefox_half1', lang('For this application work correctly</u>'));
248                        $GLOBALS['phpgw']->template->set_var('cc_msg_firefox_half2', lang('it\'s necessary to update your Firefox Browser for a new version (version > 1.5) Install now clicking in the link bellow, or if you want to update it later'));
249                        $GLOBALS['phpgw']->template->set_var('cc_msg_click_close', lang('click Close'));
250                        $GLOBALS['phpgw']->template->set_var('cc_msg_install_now', lang('Install Now'));
251                        $GLOBALS['phpgw']->template->set_var('cc_msg_install_new_firefox', lang('Install a new Firefox version'));
252                        $GLOBALS['phpgw']->template->set_var('cc_msg_moz_thunderbird', lang('Export as Mozilla Thunderbird CSV.'));
253                        $GLOBALS['phpgw']->template->set_var('cc_msg_outlook_express_pt', lang('Export as Outlook Express (Portuguese) CSV.'));
254                        $GLOBALS['phpgw']->template->set_var('cc_msg_outlook_express_en', lang('Export as Outlook Express (English) CSV.'));
255                        $GLOBALS['phpgw']->template->set_var('cc_msg_outlook_2k_pt', lang('Export as Outlook 2000 (Portuguese) CSV.'));
256                        $GLOBALS['phpgw']->template->set_var('cc_msg_outlook_2k_en', lang('Export as Outlook 2000 (English) CSV.'));
257                        $GLOBALS['phpgw']->template->set_var('cc_msg_expresso_default_csv', lang('Export as Expresso (Default) CSV.'));
258                        $GLOBALS['phpgw']->template->set_var('cc_msg_copy_to_catalog', lang('Copy to personal catalog.'));
259                        $GLOBALS['phpgw']->template->set_var('cc_msg_add_contact_to_group', lang('You did not add any contact for this group.'));
260                        $GLOBALS['phpgw']->template->set_var('cc_msg_fill_field_name', lang('Fill the field Full Name'));
261
262                        $select_groups = '';
263                       
264                        $this->so_group = CreateObject('contactcenter.so_group');
265                       
266                        $array_groups = $this->so_group->selectGroupsOwnerCanImportContacts($GLOBALS['phpgw_info']['user']['account_id']);
267                       
268                        $select_groups = "<select id='id_group'>";//<option value=0>Selecione um grupo...</option></select>";
269                        $select_groups .= "<option value=0 selected>Nenhum...</option>";
270                        foreach ($array_groups as $group){
271                                $select_groups .= "<option value='".$group['id_group']."'>".$group['title']."</option>";                                               
272                        }                                       
273                        $select_groups .= "</select>";
274                       
275                        $GLOBALS['phpgw']->template->set_var('cc_select_groups',$select_groups);
276                       
277                        if($GLOBALS['phpgw_info']['server']['personal_contact_type']=='True'){
278                                $GLOBALS['phpgw']->template->set_var('cc_contact_type', 'advanced');
279                        }else{
280                                $GLOBALS['phpgw']->template->set_var('cc_contact_type', 'default');
281                        }
282                        $GLOBALS['phpgw']->template->parse('out','index');
283
284                        $api = CreateObject('contactcenter.ui_api');
285                        $main = $api->get_full_add();
286                        $main .= $api->get_search_obj();
287                        $main .= $api->get_quick_add_plugin();
288                        $main .= $api->get_add_group();
289                        $main .= $GLOBALS['phpgw']->template->get_var('out');
290
291                        echo $main;
292                }
293
294
295                /*!
296
297                        @function data_manager
298                        @abstract Calls the right method and passes to it the right
299                                parameters
300                        @author Raphael Derosso Pereira
301
302                */
303                function data_manager()
304                {
305                        switch($_GET['method'])
306                        {
307                                /* Cards Methods */
308                                case 'set_n_cards':
309                                        return $this->set_n_cards((int)$_GET['ncards']);
310
311                                case 'get_cards_data':
312
313                                        $ids = false;
314                                        // To support ldap catalogs using accentuation
315                                        if ($_POST['letter'] == 'search' && isset($_POST['data']))
316                                        {
317                                                $ids = utf8_decode($this->search($_POST['data']));
318                                        }
319
320                                        if(isset($_SESSION['ids']))
321                                                $ids = $_SESSION['ids'];
322
323                                        return $this->get_cards_data($_POST['letter'], $_POST['page'], $ids);
324
325                                case 'get_cards_data_get':
326                                        return $this->get_cards_data($_GET['letter'], $_GET['page'], unserialize(str_replace('\\"','"',$_GET['ids'])));
327
328
329                                case 'get_photo':
330                                        return $this->get_photo($_GET['id']);
331
332                                case 'get_visible_all_ldap':
333                                        echo $this->get_visible_all_ldap();
334                                        return;
335
336                                /* Catalog Methods */
337                                case 'set_catalog':
338                                        return $this->set_catalog($_GET['catalog']);
339
340                                case 'get_catalog_tree':
341                                        echo serialize($this->get_catalog_tree($_GET['level']));
342                                        return;
343
344                                case 'get_actual_catalog':
345                                        echo serialize($this->get_actual_catalog());
346                                        return;
347
348                                case 'get_catalog_participants_list':
349                                        echo serialize($this->get_catalog_participants_list($_POST['id']));
350                                        return;
351/**rev 104**/
352                                case 'get_catalog_participants_group':
353                                        echo serialize($this->get_catalog_participants_group($_POST['id']));
354                                        return;
355/***/
356                                case 'get_catalog_add_contact':
357                                        // To support ldap catalogs with accentuation
358                                        echo serialize($this->get_catalog_add_contact(utf8_decode($_POST['id'])));
359                                        return;
360
361                                /* Full Add Methods */
362                                case 'get_full_data':
363                                        //return $this->get_full_data($_GET['id']);
364/**rev 104**/
365                                        return $this->get_full_data($_GET['id'],$_GET['catalog']);
366/****/
367
368                                case 'get_group':
369                                        return $this->get_group_data($_GET['id'],isset($_GET['shared_from'])?$_GET['shared_from']:null);
370
371                                case 'get_contact_full_add_const':
372                                        return $this->get_contact_full_add_const();
373
374                                case 'post_full_add':
375                                        return $this->post_full_add();
376
377                                case 'post_full_add_shared' :
378                                        return $this->post_full_add_shared();
379
380                                case 'post_photo':
381                                        return $this->post_photo((int) $_GET['id'] ? (int) $_GET['id'] : '_new_');
382
383                                case 'get_states':
384                                        return $this->get_states($_GET['country']);
385
386                                case 'get_cities':
387                                        return $this->get_cities($_GET['country'], $_GET['state'] ? $_GET['state'] : null);
388
389
390                                /* Other Methods */
391                                case 'quick_add':
392                                        return $this->quick_add($_POST['add']);
393
394                                case 'add_group':
395                                        return $this->add_group($_POST['add']);
396
397                                case 'remove_entry':
398                                        return $this->remove_entry((int)$_GET['remove']);
399
400                                case 'remove_all_entries':
401                                        return $this->remove_all_entries();
402
403                                case 'remove_group':
404
405                                        return $this->remove_group((int)$_GET['remove']);
406
407                                case 'search':
408                                        $ids = false;
409                                        $ids = $this->search($_GET['data']);
410                                        return $this->get_cards_data('search', '1', $ids);
411
412                                case 'email_win':
413                                        $GLOBALS['phpgw']->common->phpgw_header();
414                                        $api = CreateObject('contactcenter.ui_api');
415                                        $win = $api->get_email_win();
416                                        $win .= $api->get_quick_add_plugin();
417                                        $win .= '<input id="QAbutton" type="button" value="QuickAdd" />'
418                                                .'<br><input type="button" value="EmailWin" onclick="ccEmailWin.open()" />'
419                                                .'<script type="text/javascript">'
420                                                .'      ccQuickAdd.associateAsButton(Element("QAbutton"));'
421                                                .'</script>';
422                                        echo $win;
423                                        return;
424
425                                /* Information Gathering */
426                                case 'get_multiple_entries':
427                                        echo serialize($this->get_multiple_entries(str_replace('\\"','"',$_POST['data'])));
428                                        return;
429
430                                case 'get_all_entries':
431                                        echo serialize($this->get_all_entries(str_replace('\\"','"',$_POST['data'])));
432                                        return;
433
434                                case 'import_contacts':
435                                        return $this->import_contacts($_GET['typeImport'],$_GET['id_group']);
436
437                                case 'export_contacts':
438                                        return $this->export_contacts($_POST['typeExport']);
439                               
440                                case 'get_qtds_compartilhado':
441                                        return $this->get_qtds_compartilhado();
442                                case 'get_list_owners_perms_add':
443                                        echo $this->get_list_owners_perms_add();
444                                        return;
445
446                        }
447                }
448
449                /*!
450
451                        @function set_n_cards
452                        @abstract Informs the class the number of cards the page can show
453                        @author Raphael Derosso Pereira
454
455                        @param integer $n_cards The number of cards
456
457                */
458                function set_n_cards($n_cards)
459                {
460                        if (is_int($n_cards))
461                        {
462                                $this->page_info['n_cards'] = $n_cards;
463                                echo 1;
464                        }
465
466                        $this->save_session();
467                }
468
469                /*!
470
471                        @function set_catalog
472                        @abstract Sets the current catalog selected by the user
473                        @author Raphael Derosso Pereira
474
475                        @param string $id_catalog The sequence of IDs to reach the catalog
476                                separated by commas
477
478                */
479                function set_catalog($id_catalog)
480                {
481                        $id_catalog = str_replace('\\"', '"', $id_catalog);
482                        $temp =& $this->bo->set_catalog($id_catalog);
483
484                        if(!$this->bo->catalog->src_info) {
485                            $ldap = CreateObject('contactcenter.bo_ldap_manager');
486                            $this->bo->catalog->src_info = $ldap->srcs[1];
487                        }
488
489                        $resetCC_actual_letter = 0;
490                        if($this->bo->catalog->src_info['visible'] == "false")
491                        {
492                            $resetCC_actual_letter = 1;
493                        }
494
495                        if ($temp)
496                        {
497                                unset($this->page_info['actual_letter']);
498                                $this->page_info['changed'] = true;
499                                $this->page_info['actual_entries'] = false;
500                                $this->page_info['actual_catalog'] =& $temp;
501                                $this->save_session();
502
503                                $catalog_info = $this->bo->get_branch_by_level($this->bo->catalog_level[0]);
504
505                                if ($catalog_info['class'] === 'bo_global_ldap_catalog' ||
506                                    $catalog_info['class'] === 'bo_catalog_group_catalog')
507                                {
508                                        $perms = 1;
509                                }
510                                else
511                                {
512                                        $perms = 15;
513                                }
514
515                                echo serialize(array(
516                                        'status' => 'ok',
517                                        'catalog' => $catalog_info['class'],
518                                        'external' => $catalog_info['external']?true:false,
519                                        'resetCC_actual_letter' => $resetCC_actual_letter,
520                                        'perms'  => $perms
521                                ));
522
523                                return;
524                        }
525
526                        echo serialize(array(
527                                'status' => 'ok',
528                                'resetCC_actual_letter' => $resetCC_actual_letter,
529                                'perms'  => 0
530                        ));
531                }
532
533
534                /*!
535
536                        @function get_catalog_tree
537                        @abstract Returns the JS serialized array to used as the tree
538                                level
539                        @author Raphael Derosso Pereira
540            @author Mário César Kolling (error messages and timeout)
541
542                        @param (string) $level The level to be taken
543
544                */
545                function get_catalog_tree($level)
546                {
547                        if ($level === '0')
548                        {
549                                $folderImageDir = $GLOBALS['phpgw_info']['server']['webserver_url'] . '/phpgwapi/dftree/images/';
550
551                                $parent = '0';
552
553                                if (!($tree = $this->bo->get_catalog_tree($level)))
554                                {
555                                        return array(
556                                                'msg'    => lang('Couldn\'t get the Catalogue Tree. Please contact the Administrator.'),
557                                                'status' => 'fatal'
558                                        );
559                                }
560                        }
561                        else
562                        {
563                                $last_dot = strrpos($level,'.');
564                                $parent = substr($level, 0, $last_dot);
565                                $child = substr($level, $last_dot+1, strlen($level));
566                                if (!($tree[$child] = $this->bo->get_catalog_tree($level)))
567                                {
568                                        return array(
569                                                'msg'    => lang('Couldn\'t get the Catalogue Tree. Please contact the Administrator.'),
570                                                'status' => 'fatal'
571                                        );
572                                }
573                                // Deals with timeout and returns the generated message to the browser
574                                else if (!empty($tree[$child]['timeout']) && !empty($tree[$child]['msg']))
575                                {
576                                        $tmp = array(
577                                                'msg'    => $tree[$child]['msg'],
578                                                'status' => 'fatal'
579                                        );
580                                        unset($tree[$child]);
581                                        return $tmp;
582                                }
583                        }
584
585                        $folderImageDir = $GLOBALS['phpgw']->common->image('contactcenter','globalcatalog-mini.png');
586                        $folderImageDir = substr($folderImageDir, 0, strpos($folderImageDir, 'globalcatalog-mini.png'));
587
588                        // Deals with error messages from the server and returns them to the browser
589                        if ($tree['msg'])
590                        {
591                                $msg = $tree['msg'];
592                                unset($tree['msg']);
593                        }
594
595                        $tree_js = $this->convert_tree($tree, $folderImageDir, $parent);
596
597                        // Return status = ok, or else return generated message to the browser
598                        if (!$msg)
599                        {
600                                return array(
601                                        'data' => $tree_js,
602                                        'msg'  => lang('Catalog Tree Successfully taken!'),
603                                        'status' => 'ok'
604                                );
605                        }
606                        else
607                        {
608                                return array(
609                                        'data' => $tree_js,
610                                        'msg'  => $msg,
611                                        'status' => 'error'
612                                );
613                        }
614                }
615
616                /*!
617
618                        @function get_actual_catalog
619                        @abstract Returns the actual selected Catalog
620                        @author Raphael Derosso Pereira
621
622                */
623                function get_actual_catalog()
624                {
625                        $level = $this->bo->get_level_by_branch($this->bo->get_actual_catalog(), $this->bo->tree['branches'], '0');
626
627                        if ($level)
628                        {
629                                return array(
630                                        'status' => 'ok',
631                                        'data'   => $level
632                                );
633                        }
634
635                        return array(
636                                'status' => 'fatal',
637                                'msg'    => lang('Couldn\'t get the actual catalog.'),
638                        );
639                }
640
641                function get_qtds_compartilhado() {
642                        $so_contact = CreateObject('contactcenter.so_contact',  $GLOBALS['phpgw_info']['user']['account_id']);
643            $relacionados = $so_contact->get_relations();
644
645                        $perms_relacao = array();
646
647                foreach($relacionados as $uid_relacionado => $tipo_relacionamento) {
648                                $aclTemp = CreateObject("phpgwapi.acl",$uid_relacionado);
649                $aclTemp->read();
650                $perms_relacao[$uid_relacionado] = $aclTemp->get_specific_rights($GLOBALS['phpgw_info']['user']['account_id'],'contactcenter'); //Preciso verificar as permissões que o contato relacionado deu para o atual
651                        }
652
653                        $validos = array();
654                        $count = 0;
655                        foreach($perms_relacao as $uid_relacionado => $val){
656                                if ($perms_relacao[$uid_relacionado]&2)
657                                {
658                                        $validos[$uid_relacionado] = $perms_relacao[$uid_relacionado];
659                                        $count++;
660                                }
661                        }
662                        echo serialize(array(0=>$count));
663                }
664
665                /*!
666
667                        @function get_cards_data
668                        @abstract Returns the information that is placed on the cards
669                        @author Raphael Derosso Pereira
670
671                        @param string $letter The first letter to be searched
672                        @param (int)  $page The page to be taken
673                        @param (str)  $ids The ids to be taken in case of search
674
675                        TODO: This function is not well done. It must be rewritten
676                                using the new array 'msg','status','data' schema.
677                */
678                function get_cards_data($letter, $page, $ids)
679                {
680                        if( $ids )
681                                $_SESSION['ids'] = $ids;
682
683                        // It's an external catalog?
684                        $external = $this->bo->is_external($this->page_info['actual_catalog']);
685                        //echo $page."\n";
686                        if ($letter !== 'search' and ($letter != $this->page_info['actual_letter'] or
687                            ($letter == $this->page_info['actual_letter'] and $page == $this->page_info['actual_page']) or
688                            $this->page_info['changed']))
689                        {
690                                unset($ids);
691                                $this->page_info['changed'] = false;
692
693                                switch ($this->page_info['actual_catalog']['class'])
694                                {
695/**rev 104**/
696                                        case 'bo_shared_people_manager':
697/****/
698                                        case 'bo_people_catalog':
699                                                $field_name = 'id_contact';
700
701                                                if ($letter !== 'number')
702                                                {
703                                                        $find_restric[0] = array(
704                                                                0 => array(
705                                                                        'field' => 'contact.names_ordered',
706                                                                        'type'  => 'iLIKE',
707                                                                        'value' => $letter !== 'all' ? $letter.'%' : '%'
708/**rev 104**/
709        /*                                                      ),
710                                                                1 => array(
711                                                                        'field' => 'contact.id_owner',
712                                                                        'type'  => '=',
713                                                                        'value' => $GLOBALS['phpgw_info']['user']['account_id']
714        */
715                                                                )
716                                                        );
717
718                                                        //Tratamento de permissão de escrita no compartilhamento de catalogo
719                                                        $so_contact = CreateObject('contactcenter.so_contact',  $GLOBALS['phpgw_info']['user']['account_id']);
720                                                        $relacionados = $so_contact->get_relations();
721
722                                                        $perms_relacao = array();
723
724                                                        foreach($relacionados as $uid_relacionado => $tipo_relacionamento) {
725                                                                $aclTemp = CreateObject("phpgwapi.acl",$uid_relacionado);
726                                                                $aclTemp->read();
727                                                                $perms_relacao[$uid_relacionado] = $aclTemp->get_specific_rights($GLOBALS['phpgw_info']['user']['account_id'],'contactcenter'); //Preciso verificar as permissões que o contato relacionado deu para o atual
728                                                        }
729
730                                                        $validos = array();
731                                                        $count = 0;
732                                                        foreach($perms_relacao as $uid_relacionado => $val){
733                                                                if ($perms_relacao[$uid_relacionado]&2)
734                                                                {
735                                                                        $validos[$uid_relacionado] = $perms_relacao[$uid_relacionado];
736                                                                        $count++;
737                                                                }
738                                                        }
739                                                        $prop_names = array();
740                                                        if($validos) {
741                                                                $filtro = "(|";
742                                                                foreach($validos as $i => $prop) {
743                                                                        $filtro .= "(uidNumber=".$i.")";
744                                                                }
745                                                                $filtro .= ")";
746
747                                                                if(!$this->bo->catalog->src_info) {
748                                                                        $ldaps = CreateObject('contactcenter.bo_ldap_manager');
749                                                                        $this->bo->catalog->src_info = $ldaps->srcs[1];
750                                                                }
751                                                                $s = $GLOBALS['phpgw']->common->ldapConnect($this->bo->catalog->src_info['host'], $this->bo->catalog->src_info['acc'], $this->bo->catalog->src_info['pw'], false);
752                                                                $n=$this->bo->catalog->src_info['dn'];
753                                                                $apenasThese = array("cn","uidnumber","uid");
754                                                                $r = ldap_search($s,$n, $filtro,$apenasThese);
755                                                                $infos = ldap_get_entries($s, $r);
756                                                                ldap_close($s);
757                                                                for($z = 0; $z < $infos['count']; $z++) {
758                                                                        $prop_names[$infos[$z]['uidnumber'][0]] = array("cn" => $infos[$z]['cn'][0], "uid" => $infos[$z]['uid'][0]);
759                                                                }
760                                                        }
761                                                        //--------------------------------------------------------------------------------
762                                                        if($this->page_info['actual_catalog']['class'] == 'bo_people_catalog')
763                                                        {
764                                                                $find_restric[0][1] = array(
765                                                                                'field' => 'contact.id_owner',
766                                                                                'type'  => '=',
767                                                                                'value' => $GLOBALS['phpgw_info']['user']['account_id']
768                                                                );
769                                                        }
770/****/
771
772                                                }
773                                                else
774                                                {
775                                                        $find_restric[0] = array(
776                                                                0 => array(
777                                                                        'type'  => 'branch',
778                                                                        'value' => 'OR',
779                                                                        'sub_branch' => array(
780                                                                                0 => array(
781                                                                                        'field' => 'contact.names_ordered',
782                                                                                        'type'  => 'LIKE',
783                                                                                        'value' => '0%'
784                                                                                ),
785                                                                                1 => array(
786                                                                                        'field' => 'contact.names_ordered',
787                                                                                        'type'  => 'LIKE',
788                                                                                        'value' => '1%'
789                                                                                ),
790                                                                                2 => array(
791                                                                                        'field' => 'contact.names_ordered',
792                                                                                        'type'  => 'LIKE',
793                                                                                        'value' => '2%'
794                                                                                ),
795                                                                                3 => array(
796                                                                                        'field' => 'contact.names_ordered',
797                                                                                        'type'  => 'LIKE',
798                                                                                        'value' => '3%'
799                                                                                ),
800                                                                                4 => array(
801                                                                                        'field' => 'contact.names_ordered',
802                                                                                        'type'  => 'LIKE',
803                                                                                        'value' => '4%'
804                                                                                ),
805                                                                                5 => array(
806                                                                                        'field' => 'contact.names_ordered',
807                                                                                        'type'  => 'LIKE',
808                                                                                        'value' => '5%'
809                                                                                ),
810                                                                                6 => array(
811                                                                                        'field' => 'contact.names_ordered',
812                                                                                        'type'  => 'LIKE',
813                                                                                        'value' => '6%'
814                                                                                ),
815                                                                                7 => array(
816                                                                                        'field' => 'contact.names_ordered',
817                                                                                        'type'  => 'LIKE',
818                                                                                        'value' => '7%'
819                                                                                ),
820                                                                                8 => array(
821                                                                                        'field' => 'contact.names_ordered',
822                                                                                        'type'  => 'LIKE',
823                                                                                        'value' => '8%'
824                                                                                ),
825                                                                                9 => array(
826                                                                                        'field' => 'contact.names_ordered',
827                                                                                        'type'  => 'LIKE',
828                                                                                        'value' => '9%'
829                                                                                ),
830                                                                        ),
831/**rev 104**/
832/*                                                              ),
833                                                                1 => array(
834                                                                        'field' => 'contact.id_owner',
835                                                                        'type'  => '=',
836                                                                        'value' => $GLOBALS['phpgw_info']['user']['account_id']
837                                                                ),
838*/
839                                                                )
840                                                        );
841                                                }
842
843                                                if($this->page_info['actual_catalog']['class'] == 'bo_people_catalog'){                                                         
844                                                        $find_restric[0][1]     = array(
845                                                                'field' => 'contact.id_owner',
846                                                                'type'  => '=',
847                                                                'value' => $GLOBALS['phpgw_info']['user']['account_id']
848                                                        );
849                                                }
850/*****/
851
852                                                $find_field[0] = array('contact.id_contact','contact.names_ordered');
853
854                                                $find_other[0] = array(
855/**rev 104**/
856                                                        //'offset' => (($page-1)*$this->page_info['n_cards']),
857                                                        //'limit'  => $this->page_info['n_cards'],
858/*****/
859                                                        'order'  => 'contact.names_ordered'
860                                                );
861
862                                                break;
863
864                                        case 'bo_catalog_group_catalog':
865                                        case 'bo_global_ldap_catalog':
866
867                                                $field_name = 'id_contact';
868
869                                                if ($letter !== 'number')
870                                                {
871                                                        $find_restric[0] = array(
872                                                                0 => array(
873                                                                        'field' => 'contact.names_ordered',
874                                                                        'type'  => 'iLIKE',
875                                                                        'value' => $letter !== 'all' ? $letter.'%' : '%'
876                                                                ),
877                                                                /*
878                                                                 * Restrict the returned contacts in a "first letter" search
879/**rev 104
880                                                                 * to objectClass = phpgwAccount, must have attibute phpgwAccountStatus,
881                                                                 * phpgwAccountVisible != -1
882                                                                 */
883                                                                1 => array(
884                                                                        'field' => 'contact.object_class',
885                                                                        'type'  => '=',
886                                                                        'value' => 'phpgwAccount'
887/**rev 104**/
888                                                                //),/*
889                                                                ),
890/****/
891                                                                2 => array(
892                                                                        'field' => 'contact.account_status',
893                                                                        'type'  => 'iLIKE',
894                                                                        'value' => '%'
895/**rev 104**/
896                                                                //),*/
897                                                                //2 => array(
898                                                                ),
899                                                                3 => array(
900/*****/
901                                                                        'field' => 'contact.account_visible',
902                                                                        'type'  => '!=',
903                                                                        'value' => '-1'
904/**rev 104**/
905        /*                                                      ),
906                                                                3 => array(
907                                                                        'field' => 'contact.object_class',
908                                                                        'type'  => '=',
909                                                                        'value' => 'inetOrgPerson'
910                                                                ),
911        */
912                                                                )
913/*****/
914
915                                                        );
916                                                        // If not external catalog get only phpgwAccountType = u ou l
917                                                        if (!$external)
918                                                        {
919                                                                $find_restric[0][5] =  array(
920                                                                                'type'  => 'branch',
921                                                                                'value' => 'OR',
922                                                                                'sub_branch' => array(
923                                                                                        0 => array(
924                                                                                        'field' => 'contact.account_type',
925                                                                                        'type'  => '=',
926                                                                                        'value' => 'u'
927                                                                                        ),
928                                                                                        1 => array(
929                                                                                        'field' => 'contact.account_type',
930                                                                                        'type'  => '=',
931                                                                                        'value' => 'i'
932/**rev 104**/
933                                                                                        ),
934                                                                                        2 => array(
935                                                                                        'field' => 'contact.account_type',
936                                                                                        'type'  => '=',
937/****/
938                                                                                        'value' => 'l'
939/**rev 104**/
940                                                                                        ),
941                                                                                        3 => array(
942                                                                                        'field' => 'contact.account_type',
943                                                                                        'type'  => '=',
944                                                                                        'value' => 'g'
945/***/
946                                                                                        ),
947                                                                                        4 => array(
948                                                                                        'field' => 'contact.account_type',
949                                                                                        'type'  => '=',
950                                                                                        'value' => 's'
951                                                                                        )
952                                                                                )
953                                                                        );
954                                                        }
955                                                }
956                                                else
957                                                {
958                                                        $find_restric[0] = array(
959                                                                /*
960                                                                 * Restrict the returned contacts in a "first number" search
961/**rev 104
962                                                                 * to objectClass = phpgwAccount, must have attibute phpgwAccountStatus,
963                                                                 * phpgwAccountVisible != -1
964                                                                 */
965                                                                0 => array(
966                                                                        'field' => 'contact.object_class',
967                                                                        'type'  => '=',
968                                                                        'value' => 'phpgwAccount'
969/**rev 104**/
970                                                                //),/*
971                                                                ),
972/****/
973                                                                1 => array(
974                                                                        'field' => 'contact.account_status',
975                                                                        'type'  => 'iLIKE',
976                                                                        'value' => '%'
977/**rev 104**/
978                                                                //),*/
979                                                                //1 => array(
980                                                                ),
981                                                                2 => array(
982/*****/
983                                                                        'field' => 'contact.account_visible',
984                                                                        'type'  => '!=',
985                                                                        'value' => '-1'
986                                                                ),
987/**rev 104**/
988        /*                                                      2 => array(
989                                                                        'field' => 'contact.object_class',
990                                                                        'type'  => '=',
991                                                                        'value' => 'inetOrgPerson'
992                                                                ),
993        */
994/****/
995                                                                3 => array(
996                                                                        'type'  => 'branch',
997                                                                        'value' => 'OR',
998                                                                        'sub_branch' => array(
999                                                                                0 => array(
1000                                                                                        'field' => 'contact.names_ordered',
1001                                                                                        'type'  => 'LIKE',
1002                                                                                        'value' => '0%'
1003                                                                                ),
1004                                                                                1 => array(
1005                                                                                        'field' => 'contact.names_ordered',
1006                                                                                        'type'  => 'LIKE',
1007                                                                                        'value' => '1%'
1008                                                                                ),
1009                                                                                2 => array(
1010                                                                                        'field' => 'contact.names_ordered',
1011                                                                                        'type'  => 'LIKE',
1012                                                                                        'value' => '2%'
1013                                                                                ),
1014                                                                                3 => array(
1015                                                                                        'field' => 'contact.names_ordered',
1016                                                                                        'type'  => 'LIKE',
1017                                                                                        'value' => '3%'
1018                                                                                ),
1019                                                                                4 => array(
1020                                                                                        'field' => 'contact.names_ordered',
1021                                                                                        'type'  => 'LIKE',
1022                                                                                        'value' => '4%'
1023                                                                                ),
1024                                                                                5 => array(
1025                                                                                        'field' => 'contact.names_ordered',
1026                                                                                        'type'  => 'LIKE',
1027                                                                                        'value' => '5%'
1028                                                                                ),
1029                                                                                6 => array(
1030                                                                                        'field' => 'contact.names_ordered',
1031                                                                                        'type'  => 'LIKE',
1032                                                                                        'value' => '6%'
1033                                                                                ),
1034                                                                                7 => array(
1035                                                                                        'field' => 'contact.names_ordered',
1036                                                                                        'type'  => 'LIKE',
1037                                                                                        'value' => '7%'
1038                                                                                ),
1039                                                                                8 => array(
1040                                                                                        'field' => 'contact.names_ordered',
1041                                                                                        'type'  => 'LIKE',
1042                                                                                        'value' => '8%'
1043                                                                                ),
1044                                                                                9 => array(
1045                                                                                        'field' => 'contact.names_ordered',
1046                                                                                        'type'  => 'LIKE',
1047                                                                                        'value' => '9%'
1048                                                                                ),
1049                                                                        ),
1050                                                                ),
1051                                                        );
1052                                                        // If not external catalog get only phpgwAccountType = u ou l
1053                                                        if (!$external)
1054                                                        {
1055                                                                $find_restric[0][5] =  array(
1056                                                                        'type'  => 'branch',
1057                                                                        'value' => 'OR',
1058                                                                        'sub_branch' => array(
1059                                                                                0 => array(
1060                                                                                'field' => 'contact.account_type',
1061                                                                                'type'  => '=',
1062                                                                                'value' => 'u'
1063                                                                                ),
1064                                                                                1 => array(
1065                                                                                'field' => 'contact.account_type',
1066                                                                                'type'  => '=',
1067/**rev 104**/
1068                                                                                //'value' => 'g'
1069                                                                                //);
1070                                                                                //1 => array(
1071                                                                                'value' => 'i'
1072                                                                                ),
1073                                                                                2 => array(
1074/****/
1075                                                                                'field' => 'contact.account_type',
1076                                                                                'type'  => '=',
1077                                                                                'value' => 'l'
1078/**rev 104**/
1079                                                                                ),
1080                                                                                3 => array(
1081                                                                                'field' => 'contact.account_type',
1082                                                                                'type'  => '=',
1083                                                                                'value' => 'g'
1084                                                                                ),
1085/****/
1086                                                                                4 => array(
1087                                                                                'field' => 'contact.account_type',
1088                                                                                'type'  => '=',
1089                                                                                'value' => 's'
1090                                                                                )
1091                                                                        )
1092                                                                );
1093                                                        }
1094                                                }
1095
1096                                                if (!$external)
1097                                                {
1098                                                        // Get only this attributes: dn, cn, phpgwAccountType, objectClass, phpgwAccountStatus, phpghAccountVisible
1099                                                        // for non-external catalogs, used to restrict the attributes used in filters
1100                                                        $find_field[0] = array('contact.id_contact','contact.names_ordered','contact.account_type',
1101                                                                'contact.object_class','contact.account_visible');
1102                                                }
1103                                                else
1104                                                {
1105                                                        // Get only this attributes: dn, cn for external catalogs,
1106                                                        // used to restrict the attributes used in filters
1107                                                        $find_field[0] = array('contact.id_contact','contact.names_ordered');
1108                                                }
1109
1110                                                $find_other[0] = array(
1111                                                        //'offset' => (($page-1)*$this->page_info['n_cards']),
1112                                                        //'limit'  => $this->page_info['n_cards'],
1113                                                        'order'  => 'contact.names_ordered'
1114                                                );
1115
1116                                                break;
1117
1118                                        case 'bo_company_manager':
1119                                                $field_name = 'id_company';
1120
1121                                                $find_field[0] = array('company.id_company','company.company_name');
1122
1123                                                $find_other[0] = array(
1124                                                        //'offset' => (($page-1)*$this->page_info['n_cards']),
1125                                                        //'limit'  => $this->page_info['n_cards'],
1126                                                        'order'  => 'company.company_name'
1127                                                );
1128
1129                                                $find_restric[0] = array(
1130                                                        0 => array(
1131                                                                'field' => 'company.company_name',
1132                                                                'type'  => 'iLIKE',
1133                                                                'value' => $letter !== 'all' ? $letter.'%' : '%'
1134                                                        )
1135                                                );
1136
1137                                                break;
1138
1139                                        case 'bo_group_manager':
1140/**rev 104**/
1141                                        case 'bo_shared_group_manager':
1142/****/
1143
1144                                                $field_name = 'id_group';
1145
1146                                                if ($letter !== 'number')       {
1147
1148                                                        $find_restric[0] = array(
1149                                                                0 => array(
1150                                                                        'field' => 'group.title',
1151                                                                        'type'  => 'iLIKE',
1152                                                                        'value' => $letter !== 'all' ? $letter.'%' : '%'
1153                                                                )
1154                                                        );
1155                                                }
1156                                                 else {
1157
1158                                                        $find_restric[0] = array(
1159                                                                0 => array(
1160                                                                                        'field' => 'group.title',
1161                                                                                        'type'  => 'LIKE',
1162                                                                                        'value' => '0%'
1163                                                                )
1164                                                        );
1165                                                }
1166/**rev 104**/
1167                                                if($this->page_info['actual_catalog']['class'] == 'bo_group_manager'){
1168/****/
1169                                                        array_push($find_restric[0],  array(
1170                                                                                        'field' => 'group.owner',
1171                                                                                        'type'  => '=',
1172                                                                                        'value' => $GLOBALS['phpgw_info']['user']['account_id']
1173                                                                        )
1174                                                        );
1175                                                }
1176
1177                                                $find_field[0] = array('group.id_group','group.title','group.short_name');
1178                                                $find_other[0] = array(
1179                                                        'order'  => 'group.title'
1180                                                );
1181                                                break;
1182
1183                                        /*case 'bo_catalog_group_catalog':
1184                                                $this->page_info['actual_entries'] = false;
1185
1186                                                $this->page_info['actual_letter'] = $letter;
1187                                                $this->page_info['actual_page'] = 0;
1188
1189                                                $this->save_session();
1190                                                $final[0] = 0;
1191                                                $final[1] = $this->typeContact;
1192                                                echo serialize($final);
1193                                                return;*/
1194
1195                                }
1196
1197                                $result = $this->bo->find($find_field[0],$find_restric[0],$find_other[0]);
1198                                $n_entries = count($result);
1199
1200                                if ($n_entries)
1201                                {
1202                                        //echo 'N_entries: '.$n_entries.'<br>';
1203                                        $this->page_info['n_pages'] = ceil($n_entries/($this->page_info['n_cards'] ? $this->page_info['n_cards'] : 1));
1204                                }
1205                                else
1206                                {
1207                                        $this->page_info['n_pages'] = 0;
1208                                }
1209
1210                                if (!$result)
1211                                {
1212                                        $this->page_info['actual_entries'] = false;
1213
1214                                        $this->page_info['actual_letter'] = $letter;
1215                                        $this->page_info['actual_page'] = 0;
1216
1217                                        $this->save_session();
1218                                        $final[0] = 0;
1219                                        $final[1] = $this->typeContact;
1220                                        echo serialize($final);
1221                                        return;
1222                                }
1223                                else
1224                                {
1225                                        unset($this->page_info['actual_entries']);
1226                                        if(is_array($result)) {
1227                                                foreach ($result as $id => $value)
1228                                                {
1229                                                        if($this->page_info['actual_catalog']['class'] != 'bo_shared_people_manager' && $this->page_info['actual_catalog']['class'] != 'bo_shared_group_manager')
1230                                                                $this->page_info['actual_entries'][] = $value[$field_name];
1231                                                        else
1232                                                                $this->page_info['actual_entries'][] = array(0=>$value[$field_name],1=>$value['perms'],2=>$value['owner']);
1233                                                }
1234                                        }
1235                                }
1236                        }
1237                        else if ($letter === 'search')
1238                        {
1239                                //if (!$ids and $this->page_info['actual_letter'] !== 'search')
1240                                if (!$ids)
1241                                {
1242/**rev 104**/
1243                                        //error_log('!$ids e $this->page_info[\'actual_letter\'] != search');
1244/*****/
1245                                        $this->page_info['actual_entries'] = false;
1246
1247                                        $this->page_info['actual_letter'] = $letter;
1248                                        $this->page_info['actual_page'] = 0;
1249
1250                                        $this->save_session();
1251                                        $final[0] = 0;
1252                                        $final[1] = $this -> typeContact;
1253                                        echo serialize($final);
1254                                        return;
1255                                }
1256                                else if ($ids['error'])
1257                                {
1258                                        $this->page_info['actual_entries'] = false;
1259                                        $this->page_info['actual_letter'] = $letter;
1260                                        $this->page_info['actual_page'] = 0;
1261
1262                                        $this->save_session();
1263                                        $final[0] = 0;
1264                                        $final[1] = $this -> typeContact;
1265                                        $final['error'] = $ids['error'];
1266                                        echo serialize($final);
1267                                        return;
1268                                }
1269                                else if ($ids)
1270                                {
1271                                        $this->page_info['actual_letter']  = $letter;
1272                                        $this->page_info['actual_entries'] = $ids;
1273                                        $this->page_info['n_pages'] = ceil(count($ids)/($this->page_info['n_cards'] ? $this->page_info['n_cards'] : 1));
1274                                }
1275                        }
1276                        else
1277                        {
1278                                unset($ids);
1279                        }
1280
1281                        if ($this->page_info['actual_entries'])
1282                        {
1283                                if ($page >= $this->page_info['n_pages'])
1284                                {
1285                                        $page = $this->page_info['n_pages'];
1286                                }
1287
1288                                $final = array(
1289                                        0 => (int)$this->page_info['n_pages'],
1290                                        1 => (int)$page,
1291                                        2 => array(
1292                                                0 => 'cc_company',
1293                                                1 => 'cc_name',
1294                                                2 => 'cc_title',
1295                                                3 => 'cc_phone',
1296                                                4 => 'cc_mail',
1297                                                5 => 'cc_alias',
1298                                                6 => 'cc_id',
1299                                                7 => 'cc_forwarding_address',
1300                                                8 => 'cc_empNumber',
1301                                                9 => 'cc_department',
1302                                                10 => 'cc_mobile'
1303                                        )
1304                                );
1305
1306                                //echo 'Page: '.$page.'<br>';
1307                                $id_i = (($page-1)*$this->page_info['n_cards']);
1308                                $id_f = $id_i + $this->page_info['n_cards'];
1309                                $n_entries = count($this->page_info['actual_entries']);
1310
1311                                //echo 'ID_I: '.$id_i.'<br>';
1312                                //echo 'ID_F: '.$id_f.'<br>';
1313                                ///---------------- Correção Temporária PHP5 -----------------------///
1314                                $ids = array();
1315/**rev 104**/
1316                                $perms = array();
1317                                $owners = array();
1318/****/
1319                                $array_temp = array();
1320
1321                                foreach($this->page_info['actual_entries'] as $key=>$tmp){
1322                                        $array_temp[] = $tmp;
1323                                }
1324
1325                                for($i = $id_i; $i < $id_f and $i < $n_entries; $i++)
1326                                {
1327/**rev 104**/
1328                                        if($this->page_info['actual_catalog']['class'] != 'bo_shared_people_manager' && $this->page_info['actual_catalog']['class'] != 'bo_shared_group_manager')
1329                                        {
1330/****/
1331                                                $ids[] = $array_temp[$i];
1332/**rev 104**/
1333                                        }else {
1334                                                $ids[] = $array_temp[$i][0];
1335                                                $perms[] = $array_temp[$i][1];
1336                                                $owners[] = $array_temp[$i][2];
1337/****/
1338                                        }
1339                                }
1340
1341/**rev 104**/
1342                                // Carrega o nome completo dos donos dos objetos (contatos e grupos);
1343                                $owner_names = array();
1344
1345                                if($owners) {
1346                                        $filter = "(|";
1347                                        foreach($owners as $i => $owner) {
1348                                                $filter .= "(uidNumber=".$owner.")";
1349                                        }
1350                                        $filter .= ")";
1351
1352                                        if(!$this->bo->catalog->src_info) {
1353                                                $ldap = CreateObject('contactcenter.bo_ldap_manager');
1354                                                $this->bo->catalog->src_info = $ldap->srcs[1];
1355                                        }
1356                                        $ds = $GLOBALS['phpgw']->common->ldapConnect($this->bo->catalog->src_info['host'], $this->bo->catalog->src_info['acc'], $this->bo->catalog->src_info['pw'], false);
1357                                        $dn=$this->bo->catalog->src_info['dn'];
1358                                        $justThese = array("cn","uidnumber","uid");
1359                                        $sr = ldap_search($ds,$dn, $filter,$justThese);
1360                                        $info = ldap_get_entries($ds, $sr);
1361                                        ldap_close($ds);
1362                                        for($z = 0; $z < $info['count']; $z++) {
1363                                                $owner_names[$info[$z]['uidnumber'][0]] = array("cn" => $info[$z]['cn'][0], "uid" => $info[$z]['uid'][0]);
1364                                        }
1365                                }
1366
1367/*****/
1368
1369
1370                                /// Original
1371                                //for($i = $id_i; $i < $id_f and $i < $n_entries; $i++)
1372                                //{
1373                                //      $ids[] = $this->page_info['actual_entries'][$i];
1374                                //}
1375                                ///
1376
1377                                $fields = $this->bo->catalog->get_fields(false);
1378/**rev 104**/
1379                                //if( $this->typeContact == 'groups') {
1380                                if( $this->typeContact == 'groups' || $this->typeContact == 'shared_groups') {
1381/****/
1382                                        $final = array(
1383                                                0 => (int)$this->page_info['n_pages'],
1384                                                1 => (int)$page,
1385                                                2 => array(
1386                                                        0 => 'cc_title',
1387                                                        1 => 'cc_short_name',
1388                                                        2 => 'cc_id',
1389                                                        3 => 'cc_contacts'
1390                                                )
1391                                        );
1392
1393                                        $groups =& $this->bo->catalog->get_multiple_entries($ids,$fields);
1394
1395                                        $i = 0;
1396                                        // contatos do grupo
1397                                        $boGroups = CreateObject('contactcenter.bo_group');
1398                                        $contacts = array();
1399                                        foreach($groups as $group)              {
1400
1401                                                $final[3][$i][0] = $group['title'] ? $group['title'] : 'none';
1402                                                $final[3][$i][1] = $group['short_name'] ? $group['short_name'] : 'none';
1403                                                $final[3][$i][2] = $group['id_group'] ? $group['id_group'] : 'none';
1404                                                $contacts = $boGroups -> get_contacts_by_group($group['id_group']);
1405                                                $final[3][$i][3] = $contacts;
1406                                                $final[3][$i][4] = $perms[$i];
1407                                                if($this->typeContact == 'shared_groups'){
1408                                                        $final[3][$i][5] = lang('Shared').": ".$owner_names[$owners[$i]]['cn'];
1409                                                        $final[3][$i][6] = $owner_names[$owners[$i]]['uid'];
1410                                                        $final[3][$i][7] = $owners[$i]; //uidNumber
1411                                                }                                               
1412                                                $i++;                                                   
1413                                        }
1414
1415                                        $this->page_info['actual_letter'] = $letter;
1416                                        $this->page_info['actual_page'] = $page;
1417
1418
1419                                        $lnk_compose = "location.href=('../expressoMail1_2/index.php?to=";
1420
1421                                        $final[5] = '<span class="link"  onclick="'.$lnk_compose;
1422/**rev 104**/
1423                                        //$final[10] = 'groups';
1424                                        $final[10] = $this->typeContact;
1425/******/
1426                                        $this->save_session();
1427                                        echo serialize($final);
1428                                        return;
1429                                }
1430/**rev 104**/
1431                                $final[10] = $this -> typeContact;
1432/*****/
1433
1434                                $fields['photo'] = true;
1435                                $fields['names_ordered'] = true;
1436                                $fields['alias'] = true;
1437                                $fields['account_type'] = true;
1438                                $fields['companies'] = 'default';
1439                                $fields['connections'] = 'default';
1440
1441/**rev 104**/
1442                                // ?aqui alterar a chamada desse método para receber o base dn?
1443                                //$contacts = &$this->bo->catalog->get_multiple_entries($ids,$fields);
1444
1445                                // ?aqui alterar a chamada desse método para receber o base dn?
1446                                if($external)
1447                                {
1448                                        $contacts = &$this->bo->catalog->get_multiple_entries($ids,$fields,false,true);
1449                                } else{
1450                                        $contacts = &$this->bo->catalog->get_multiple_entries($ids,$fields);
1451                                }
1452
1453/*******/
1454
1455
1456                                if (!is_array($contacts) or !count($contacts))
1457                                {
1458                                        $final[0] = 0;
1459                                        $final[1] = $this -> typeContact;
1460                                        echo serialize($final);
1461                                        return;
1462                                }
1463
1464                                $i = 0;
1465                                foreach($contacts as $index => $contact)
1466                                {
1467                                        /*
1468                                         * TODO: Os timeouts de conexão foram retirados, ver se será necessário retornar essa funcionalidade, e,
1469                                         * neste caso, terminar a implementação das mensagens de retorno.
1470                                         */
1471                                        if ($index !== 'error'){
1472                                                $final[3][$i][0] = $contact['companies']['company1']['company_name']?$contact['companies']['company1']['company_name']:'none';
1473
1474/**rev 104**/
1475                                                //$final[3][$i][1] = $contact['names_ordered'] ? $contact['names_ordered'] : 'none';
1476
1477                                                if($this->page_info['actual_catalog']['class']!='bo_global_ldap_catalog'){
1478                                                        $final[3][$i][1] = $contact['names_ordered'] ? urldecode(is_array($contact['names_ordered']) ? $contact['names_ordered'][0] : $contact['names_ordered'])  : 'none';
1479                                                }
1480                                                else {
1481                                                        $contact['names_ordered'][0] = urldecode($contact['names_ordered'][0]);
1482                                                        $final[3][$i][1] = $contact['names_ordered'] ? $contact['names_ordered']  : 'none';
1483                                                }
1484
1485/********/
1486
1487                                                $final[3][$i][2] = $contact['companies']['company1']['title']? urldecode( $contact['companies']['company1']['title'] ) :'none';
1488
1489                                                //Para exibir a matricula do empregado
1490                                                $final[3][$i][8] = $contact['companies']['company1']['empNumber']?$contact['companies']['company1']['empNumber']:'none';
1491                                                //Para exibir o setor/lotacao do empregado
1492                                                $final[3][$i][9] = $contact['companies']['company1']['department']?$contact['companies']['company1']['department']:'none';
1493                                                //Para exibir o celular empresarial do empregado
1494                                                $final[3][$i][10] = $contact['companies']['company1']['celPhone']?$contact['companies']['company1']['celPhone']:'none';
1495
1496                                                //Para exibir o celular empresarial do empregado
1497                                                if ($_SESSION['phpgw_info']['user']['preferences']['contactcenter']['voip_enabled'] && !$external && $final[3][$i][10] != 'none')
1498                                                            $final[3][$i][10] = "<a title=\"".lang("Call Mobile")."\" href=\"#\" onclick=\"connectVoip('".$final[3][$i][10]."', 'mob')\">".$final[3][$i][10]."</a>";
1499
1500                                                /* Select the correct Email and Telephone to be shown */
1501                                                $preferences = ExecMethod('contactcenter.ui_preferences.get_preferences');
1502
1503                                                if (!is_array($preferences))
1504                                                {
1505                                                        $preferences['personCardEmail'] = 1;
1506                                                        $preferences['personCardPhone'] = 2;
1507                                                }
1508                                                if ($contact['connections'])
1509                                                {
1510                                                        $default_email_found = false;
1511                                                        $default_phone_found = false;
1512                                                        foreach($contact['connections'] as $conn_info)
1513                                                        {
1514                                                                if ($conn_info['id_type'] == $preferences['personCardEmail'] and !$default_email_found)
1515                                                                {
1516                                                                        if ($conn_info['connection_is_default'])
1517                                                                        {
1518                                                                                $default_email_found = true;
1519                                                                        }
1520                                                                        $final[3][$i][4] = $conn_info['connection_value'] ? $conn_info['connection_value'] : 'none';
1521                                                                }
1522                                                                else if ($conn_info['id_type'] == $preferences['personCardPhone'] and !$default_phone_found)
1523                                                                {
1524                                                                        if ($conn_info['connection_is_default'])
1525                                                                        {
1526                                                                                $default_phone_found = true;
1527                                                                        }
1528
1529/**rev 104**/
1530                                                                        //if ($_SESSION['phpgw_info']['user']['preferences']['contactcenter']['voip_enabled'] && !$external){
1531                                                                        //      $conn_info['connection_value'] = "<a title=\"".lang("Call Extension")."\" href=\"#\" onclick=\"connectVoip('".$conn_info['connection_value']."', 'ramal')\">".$conn_info['connection_value']."</a>";
1532
1533                                                                        if (!($_SESSION['phpgw_info']['user']['preferences']['contactcenter']['telephone_number'] == $conn_info['connection_value']) && $_SESSION['phpgw_info']['user']['preferences']['contactcenter']['voip_enabled'] && $conn_info['connection_value'] && preg_match('/^\([0-9]{2}\)[0-9]{4}\-[0-9]{4}$/',$conn_info['connection_value'])==1 && !$external){
1534                                                                                $conn_info['connection_value'] = "<a title=\"".lang("Call Extension")."\" href=\"#\" onclick=\"connectVoip('".$conn_info['connection_value']."', 'com')\">".$conn_info['connection_value']."</a>";
1535                                                                       
1536/*****/
1537                                                                        }
1538                                                                        $final[3][$i][3] = $conn_info['connection_value'] ? $conn_info['connection_value'] : 'none';
1539                                                                }
1540                                                        }
1541                                                }
1542
1543                                                if (!$final[3][$i][3])
1544                                                {
1545                                                        $final[3][$i][3] = 'none';
1546                                                }
1547
1548                                                if (!$final[3][$i][4])
1549                                                {
1550                                                        $final[3][$i][4] = 'none';
1551                                                }
1552
1553                                                $final[3][$i][5] = $contact['alias']? urldecode( $contact['alias'] ):'none';
1554                                                $final[3][$i][6] = $ids[$i];
1555
1556/**rev 104**/
1557        /*                              //      If contact is a public list, then load the forwarding addresses.
1558                                                if($contact['account_type'][0] == 'l')
1559                                                        $final[3][$i][7] = array();
1560        */
1561
1562                                                //If contact is a public list or a group, then load the forwarding addresses.
1563                                                if($contact['account_type'][0] == 'l' || $contact['account_type'][0] == 'g')
1564                                                        $final[3][$i][7] = ($contact['account_type'][0] == 'l' ? 'list' : 'group');
1565                                                       
1566                                                if($this->page_info['actual_catalog']['class']=='bo_shared_people_manager') {
1567                                                        $final[3][$i][11] = $perms[$i];
1568                                                        $final[3][$i][12] = lang('Shared').": ".$owner_names[$owners[$i]]['cn'];
1569                                                }
1570/******/
1571
1572                                                $final[4][$i] = $contact['photo'] ? 1  : 0;
1573                                                $i++;
1574                                        }
1575                                        else
1576                                        {
1577                                                // coloca mensagem de erro no vetor que retorna para o browser
1578                                        }
1579                                }
1580                                $lnk_compose = "location.href=('../expressoMail1_2/index.php?to=";
1581                                $final[5] = '<span class="link" onclick="'.$lnk_compose;
1582                                $final[6] = $prop_names;
1583                                $final[7] = $validos;
1584                                $final[8] = $this->page_info['actual_catalog']['class'];
1585                                $final[9] = $count;
1586
1587
1588                                $this->page_info['actual_letter'] = $letter;
1589                                $this->page_info['actual_page'] = $page;
1590
1591                                $this->save_session();
1592                                echo serialize($final);
1593                                return;
1594                        }
1595
1596                        $this->page_info['actual_letter'] = $letter;
1597                        $this->page_info['actual_page'] = $page;
1598
1599                        $this->save_session();
1600
1601                        $final[0] = 0;
1602                        $final[1] = $this -> typeContact;
1603                        echo serialize($final);
1604                }
1605               
1606               
1607                function get_list_owners_perms_add(){
1608                        $acl = CreateObject('phpgwapi.acl');
1609                        $find_result = $acl->get_rights_and_owners($GLOBALS['phpgw_info']['user']['account_id'],'contactcenter');
1610                        if($find_result){
1611                                $owner_names = array();
1612                                $filter = "(|";
1613                                foreach($find_result as $owner) {
1614                                        if(($owner['acl_rights'] & PHPGW_ACL_ADD) == PHPGW_ACL_ADD){
1615                                                $filter .= "(uidNumber=".$owner['acl_account'].")";
1616                                        }
1617                                }
1618                                if(!$this->bo->catalog->src_info) {
1619                                        $ldap = CreateObject('contactcenter.bo_ldap_manager');
1620                                        $this->bo->catalog->src_info = $ldap->srcs[1];
1621                                }
1622                                $ds = $GLOBALS['phpgw']->common->ldapConnect($this->bo->catalog->src_info['host'], $this->bo->catalog->src_info['acc'], $this->bo->catalog->src_info['pw'], false);
1623                                $filter .= ")";
1624                                $dn=$this->bo->catalog->src_info['dn'];
1625                                $justThese = array("cn","uidnumber","uid");
1626                                $sr = ldap_search($ds,$dn, $filter,$justThese);
1627                                $info = ldap_get_entries($ds, $sr);
1628                                for($z = 0; $z < $info['count']; $z++) {
1629                                        $owner_names[$info[$z]['uidnumber'][0]] = array("cn" => $info[$z]['cn'][0], "uid" => $info[$z]['uid'][0]);
1630                                }
1631                                ldap_close($ds);
1632                        }
1633                        echo serialize ($owner_names); 
1634                }
1635
1636                function get_visible_all_ldap()
1637                {
1638                        $bo = CreateObject('contactcenter.bo_ldap_manager');
1639                        $ldap_query = $bo->srcs;
1640                        return $ldap_query[1]['visible'];
1641                }
1642
1643
1644                /*!
1645
1646                        @function get_group_data
1647                        @abstract Returns all the information of a given Group
1648                        @author Nilton Emilio Buhrer Neto
1649
1650                        @param (integer) $id The id to get information
1651
1652                */
1653                function get_group_data($id,$shared_from=null)
1654                {
1655                        $this->bo->catalog = CreateObject('contactcenter.bo_group_manager');
1656                        $fields = $this->bo->catalog->get_fields(true);
1657                        $data = $this->bo->catalog->get_single_entry($id,$fields);
1658                       
1659                        if($id) {                       
1660                                // get All Contacts by group.
1661                                $data['contact_in_list'] = $this->bo->catalog->get_contacts_by_group($id);                                                             
1662                        }
1663                       
1664                        $boGroup = CreateObject('contactcenter.bo_group');
1665
1666                        $all_contacts = $boGroup->get_all_contacts('only_email',$shared_from);
1667                       
1668                        $contact_options = "";
1669                        if(count($all_contacts)) {                                     
1670                                foreach($all_contacts as $idx => $contact) {                           
1671                                        $contact_options .= "<OPTION value='".$contact['id_connection']."'>".$contact['names_ordered']." (".$contact['connection_value'].")</OPTION>";
1672                                }
1673                        }
1674                        $data['contact_list'] = $contact_options;
1675                        $data['result'] = 'ok';                                                         
1676                        echo serialize($data);                 
1677                }               
1678               
1679                /*!
1680
1681                        @function get_full_data
1682                        @abstract Returns all the information of a given Entry
1683                        @author Raphael Derosso Pereira
1684
1685                        @param (integer) $id The id to get information
1686
1687                */
1688/**rev 104**/
1689                //function get_full_data($id)
1690                function get_full_data($id,$catalog='bo_people_catalog')
1691                {
1692                        $dateformat = $GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'];
1693/**rev 104**/
1694                        //$this->bo->catalog = CreateObject('contactcenter.bo_people_catalog');
1695                        $this->bo->catalog = CreateObject('contactcenter.'.$catalog);
1696/****/
1697                        $fields = $this->bo->catalog->get_fields(true);
1698                        $fields['photo'] = false;
1699                        $entry = $this->bo->catalog->get_single_entry($id,$fields);
1700
1701                        if (is_bool($entry['given_names']))
1702                        {
1703                                $data['result'] = 'false';
1704                                echo serialize($data);
1705                                return;
1706                        }
1707
1708                        $date = explode('-', $entry['birthdate']);
1709                        $j = 0;
1710                        for ($i = 0; $i < 5; $i+=2)
1711                        {
1712                                switch($dateformat{$i})
1713                                {
1714                                        case 'Y':
1715                                                $birthdate[$j] = $date[0];
1716                                                break;
1717
1718                                        case 'm':
1719                                        case 'M':
1720                                                $birthdate[$j] = $date[1];
1721                                                break;
1722
1723                                        case 'd':
1724                                                $birthdate[$j] = $date[2];
1725                                }
1726                                $j++;
1727                        }
1728                        $datecount = 0;
1729
1730                        $data['result'] = 'ok';
1731                        $data['cc_full_add_contact_id'] = $id;
1732
1733                        /* Personal Data */
1734                        $data['personal']['cc_pd_photo'] = '../index.php?menuaction=contactcenter.ui_data.data_manager&method=get_photo&id='.$id;
1735                        $data['personal']['cc_pd_alias'] = $entry['alias'];
1736                        $data['personal']['cc_pd_given_names'] = $entry['given_names'];
1737                        $data['personal']['cc_pd_family_names'] = $entry['family_names'];
1738                        $data['personal']['cc_pd_full_name'] = $entry['names_ordered'];
1739                        $data['personal']['cc_pd_suffix'] = $entry['id_suffix'];
1740                        $data['personal']['cc_pd_birthdate_0'] = $birthdate[0];
1741                        $data['personal']['cc_pd_birthdate_1'] = $birthdate[1];
1742                        $data['personal']['cc_pd_birthdate_2'] = $birthdate[2];
1743                        //$data['personal']['cc_pd_sex'] = $entry['sex'] === 'M' ? 1 : ($entry['sex'] === 'F' ? 2 : 0);
1744                        $data['personal']['cc_pd_prefix'] = $entry['id_prefix'];
1745                        $data['personal']['cc_pd_gpg_finger_print'] = $entry['pgp_key'];
1746                        $data['personal']['cc_pd_notes'] = $entry['notes'];
1747
1748                        /* Addresses */
1749                        if (is_array($entry['addresses']))
1750                        {
1751                                $data['addresses'] = $entry['addresses'];
1752                        }
1753
1754                        /* Connections */
1755                        if (is_array($entry['connections']))
1756                        {
1757                                $data['connections'] = array();
1758                                foreach ($entry['connections'] as $connection)
1759                                {
1760                                        $type = $connection['id_type'];
1761                                        $i = count($data['connections'][$type]);
1762                                        $data['connections'][$type][$i]['id'] = $connection['id_connection'];
1763                                        $data['connections'][$type][$i]['name'] = $connection['connection_name'];
1764                                        $data['connections'][$type][$i]['value'] = $connection['connection_value'];
1765                                        $data['connections'][$type][$i]['is_default'] = $connection['connection_is_default'];
1766                                }
1767                        }
1768//                      print_r($data);
1769//OBSERVAR cc_department
1770                        /*Corporative*/
1771                        if($GLOBALS['phpgw_info']['server']['personal_contact_type']=='True'){
1772                                $data['personal']['cc_job_title'] = $entry['job_title'];
1773                                $data['personal']['cc_department'] = $entry['department'];
1774                                $data['personal']['cc_name_corporate'] = $entry['corporate_name'];
1775                                $data['personal']['cc_web_page'] = $entry['web_page'];
1776                        }
1777                       
1778
1779
1780                        /* Relations */
1781
1782                        echo serialize($data);
1783                }
1784
1785                /*!
1786
1787                        @function get_contact_full_add_const
1788                        @abstract Returns all the constant fields in Contact Full Add Window to the JS
1789                        @author Raphael Derosso Pereira
1790                */
1791                function get_contact_full_add_const()
1792                {
1793                        $data = array();
1794                        $boPeopleCatalog = CreateObject('contactcenter.bo_people_catalog');
1795                        $predata[] = $boPeopleCatalog -> get_all_prefixes();
1796                        $predata[] = $boPeopleCatalog -> get_all_suffixes();
1797                        $predata[] = $boPeopleCatalog -> get_all_addresses_types();
1798                        $predata[] = $boPeopleCatalog -> get_all_countries();
1799                        $predata[] = $boPeopleCatalog -> get_all_connections_types();
1800                        //$predata[] = $this->bo->catalog->get_all_relations_types();
1801
1802                        $i = 0;
1803                        foreach($predata as $data_)
1804                        {
1805                                if ($data_)
1806                                {
1807                                        $data[$i] = $data_;
1808                                }
1809
1810                                $i++;
1811                        }
1812
1813                        if (count($data))
1814                        {
1815                                echo serialize($data);
1816                                return;
1817                        }
1818
1819                        echo 0;
1820                }
1821
1822                /*!
1823
1824                        @function quick_add
1825                        @abstract Adds a new Contact using the Quick Add interface
1826                        @author Raphael Derosso Pereira
1827
1828                        @param string $sdata Serialized data
1829                */
1830                function quick_add($sdata)
1831                {
1832
1833                        $sdata = str_replace('\\"', '"', $sdata);
1834                        $new_array = unserialize($sdata);
1835                        $tdata = array();
1836
1837                        foreach($new_array as $tmp)
1838                                $tdata[] = $tmp;
1839
1840                        if (!$tdata)
1841                        {
1842                                echo serialize(array(
1843                                        'msg'    => lang('Problems on adding your Contact. Invalid Data came from client. No Contact added!'),
1844                                        'status' => 'abort'
1845                                ));
1846
1847                                return;
1848                        }
1849
1850                        // verifica se email já existe!
1851                        $boGroup = CreateObject('contactcenter.bo_group');
1852                        $contact = $boGroup->verify_contact($tdata[4]);
1853
1854                        if($contact)
1855                        {
1856                                $str_contact = "\r\n - ".implode("\r\n - ",$contact);
1857
1858                                echo serialize(array(
1859                                        'msg'    => lang('Problems on adding your Contact. The email "%1" already exists in: %2',$tdata[4], $str_contact),
1860                                        'status' => 'alreadyExists'
1861                                ));
1862
1863                                return;
1864                        }
1865
1866                        $data['alias'] = addslashes($tdata[0]);
1867                        $data['given_names'] = addslashes($tdata[1]);
1868                        $data['family_names'] = addslashes($tdata[2]);
1869                        $data['connections']['default_phone']['connection_name'] = lang('Main');
1870                        $data['connections']['default_phone']['connection_value'] = $tdata[3];
1871                        $data['connections']['default_email']['connection_name'] = lang('Main');
1872                        $data['connections']['default_email']['connection_value'] = $tdata[4];
1873                        $data['is_quick_add'] = true;
1874                        $boPeople = CreateObject('contactcenter.bo_people_catalog');
1875
1876                        if ($boPeople ->quick_add($data))
1877                        {
1878                                $this->page_info['changed'] = true;
1879
1880                                echo serialize(array(
1881                                        'msg'    => lang('Entry added with success!'),
1882                                        'status' => 'ok'
1883                                ));
1884                        }
1885                        else
1886                        {
1887                                echo serialize(array(
1888                                        'msg'    => lang('Problems on adding your Contact. No Contact added!'),
1889                                        'status' => 'error'
1890                                ));
1891                        }
1892
1893                        $this->save_session();
1894
1895                }
1896
1897                /*!
1898
1899                        @function add_group
1900                        @abstract Adds a new Group using the Add Group interface
1901                        @author Nilton Emilio Buhrer Neto
1902
1903                        @param string $sdata Serialized data
1904                */
1905                function add_group($sdata)
1906                {
1907                        $sdata = str_replace('\\"', '"', $sdata);
1908                        $tdata = unserialize($sdata);
1909                        $new_tdata = array();
1910
1911                        if (!$tdata)
1912                        {
1913                                echo serialize(array(
1914                                        'msg'    => lang('Problems on adding your Contact. Invalid Data came from client. No Contact added!'),
1915                                        'status' => 'abort'
1916                                ));
1917
1918                                return;
1919                        }
1920
1921                        foreach($tdata as $tmp)
1922                                $new_tdata[] = $tmp;
1923
1924                        $data['title'] = $new_tdata[0];
1925                        $data['contact_in_list'] = $new_tdata[1];
1926                        $data['id_group'] = $new_tdata[2];
1927
1928                        $boGroup = CreateObject('contactcenter.bo_group_manager');
1929                        $id = $boGroup -> add_group($data);
1930
1931                        if ($id)
1932                        {
1933                                $this->page_info['changed'] = true;
1934
1935                                echo serialize(array(
1936                                        'msg'    => lang('Entry added with success!'),
1937                                        'status' => 'ok'
1938                                ));
1939                        }
1940                        else
1941                        {
1942                                echo serialize(array(
1943                                        'msg'    => lang('Problems on adding your Group. Be sure that a group with this name do not exists'),
1944                                        'status' => 'error'
1945                                ));
1946                        }
1947
1948                        $this->save_session();
1949                }
1950
1951                /*!
1952
1953                        @function remove_group
1954                        @abstract Removes a group if the user has the right to do it
1955                        @author Nilton Emilio Buhrer Neto
1956                        @param (integer) $id The id to be removed
1957
1958                */
1959                function remove_group($id)
1960                {
1961                                $soGroup = CreateObject('contactcenter.so_group');
1962                                $data = array ('id_group' => $id);
1963                                if($soGroup -> delete($data)) {
1964                                        echo serialize(array(
1965                                                'msg'    => lang('Removed Entry ID '.$id.'!'),
1966                                                'status' => 'ok'
1967                                        ));
1968                                }
1969                                else {
1970                                        echo serialize(array(
1971                                                'msg'    => lang('Problems on adding your Contact. No Contact added!'),
1972                                                'status' => 'error'
1973                                        ));
1974                                }
1975
1976                        $this->save_session();
1977                }
1978
1979
1980                function remove_all_entries (){
1981
1982                        $error = false;
1983                        $this->all_entries = $this->bo->catalog->get_all_entries_ids();
1984
1985                        foreach($this->all_entries as $index => $id) {
1986                                $result = $this->bo->catalog->remove_single_entry($id);
1987                                if(!$result) {
1988                                        $error = true;
1989                                        break;
1990                                }
1991                        }
1992
1993                        if(!$error) {
1994                                echo serialize(array(
1995                                        'msg'    => lang('Removed Entry ID '.$id.'!'),
1996                                        'status' => 'ok'
1997                                ));
1998                        }
1999                        else {
2000                                echo serialize(array(
2001                                        'msg'    => lang('Couldn\'t remove this entry. Inform the Site Admin!'),
2002                                        'status' => 'fail'
2003                                ));
2004                        }
2005
2006                        $this->save_session();
2007                }
2008
2009                /*!
2010
2011                        @function remove_entry
2012                        @abstract Removes an entry if the user has the right to do it
2013                        @author Raphael Derosso Pereira
2014
2015                        @param (integer) $id The id to be removed
2016
2017                */
2018                function remove_entry ($id)
2019                {
2020                        if (!is_int($id))
2021                        {
2022                                echo lang('Couldn\'t remove entry! Problem passing data to the server. Please inform admin!');
2023                                return;
2024                        }
2025
2026                        $this->page_info['changed'] = true;
2027                        $result = $this->bo->catalog->remove_single_entry($id);
2028
2029                        if ($result)
2030                        {
2031                                if ($pos = array_search($id, $this->page_info['actual_entries']))
2032                                {
2033                                        unset($this->page_info['actual_entries'][$pos]);
2034                                }
2035
2036                                $temp = false;
2037                                reset($this->page_info['actual_entries']);
2038                                foreach($this->page_info['actual_entries'] as $t)
2039                                {
2040                                        $temp[] = $t;
2041                                }
2042
2043                                $this->page_info['actual_entries'] = $temp;
2044
2045                                echo serialize(array(
2046                                        'msg'    => lang('Removed Entry ID '.$id.'!'),
2047                                        'status' => 'ok'
2048                                ));
2049                        }
2050                        else
2051                        {
2052                                echo serialize(array(
2053                                        'msg'    => lang('Couldn\'t remove this entry. Inform the Site Admin!'),
2054                                        'status' => 'fail'
2055                                ));
2056                        }
2057
2058                        $this->save_session();
2059                }
2060
2061
2062                /*!
2063
2064                        @function post_full_add
2065                        @abstract Saves all the information altered/entered in the Full Add
2066                                window
2067                        @author Raphael Derosso Pereira
2068
2069                */
2070                function post_full_add()
2071                {
2072                        $data =  $_POST['data'];
2073                        // Exceptions!!! utf8 special chars.
2074                        $data = preg_replace("/\%u2(\d+)(\d+)(\d+)/","-",$data);
2075                        $data = unserialize(str_replace('\\"', '"', $data));
2076                        $this -> bo -> catalog = CreateObject('contactcenter.bo_people_catalog');
2077
2078                        if (!is_array($data))
2079                        {
2080                                echo serialize(array(
2081                                        'msg' => lang('<p>Some problem receiving data from browser. This is probably a bug in ContactCenter<br>'.
2082                                                  'Please go to eGroupWare Bug Reporting page and report this bug.<br>'.
2083                                                          'Sorry for the inconvenient!<br><br>'.
2084                                                          '<b><i>ContactCenter Developer Team</i></b></p>'),
2085                                        'status' => 'fatal'
2086                                ));
2087                                return;
2088                        }
2089//                      print_r($data);
2090//                      echo '<br><br>';
2091
2092                        $replacer = $data['commercialAnd'];
2093                        unset($data['commercialAnd']);
2094                        if (!is_string($replacer) or strpos($replacer, "'") or strpos($replacer, '"'))
2095                        {
2096                                echo serialize(array(
2097                                        'msg' => lang('Invalid \'&\' replacer! This may be an attempt to bypass Security! Action aborted!'),
2098                                        'status' => 'fatal'
2099                                ));
2100
2101                                return;
2102                        }
2103
2104                        if ($data['id_contact'])
2105                        {
2106                                $id = $data['id_contact'];
2107                                $id_photo = $id;
2108                                unset($data['id_contact']);
2109                        }
2110                        else
2111                        {
2112                                $id_photo = '_new_';
2113                        }
2114
2115                        /*
2116                         * Process Photo, if available
2117                         */
2118                        $sleep_count = 0;
2119                        $photo_ok = $GLOBALS['phpgw']->session->appsession('ui_data.photo','contactcenter');
2120                        while($photo_ok[0]{0} !== 'o' and $photo_ok[1]{0} === 'y')
2121                        {
2122                                sleep(1);
2123                                $photo_ok = $GLOBALS['phpgw']->session->appsession('ui_data.photo','contactcenter');
2124                                $sleep_count++;
2125
2126                                if ($sleep_count > 35)
2127                                {
2128                                        // TODO
2129                                        return;
2130                                }
2131                        }
2132                        $GLOBALS['phpgw']->session->appsession('ui_data.photo','contactcenter', array('wait', 'n'));
2133
2134                        if (isset($this->page_info['photos'][$id_photo]))
2135                        {
2136                                if (array_search($this->page_info['photos'][$id_photo]['status'], array('changed', 'sync')) === false)
2137                                {
2138                                        echo serialize(array(
2139                                                'msg' => $this->page_info['photos'][$id_photo]['msg'],
2140                                                'status' => $this->page_info['photos'][$id_photo]['status']
2141                                        ));
2142
2143                                        return;
2144                                }
2145
2146                                $data['photo'] = $this->page_info['photos'][$id_photo]['content'];
2147                                unset($this->page_info['photos'][$id_photo]);
2148                                $this->save_session();
2149                        }
2150
2151                        /*
2152                         * Arrange Date so it gets inserted correctly
2153                         */
2154
2155                        $dateformat = $GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'];
2156
2157                        $j = 0;
2158                        for ($i = 0; $i < 5; $i+=2)
2159                        {
2160                                switch($dateformat{$i})
2161                                {
2162                                        case 'Y':
2163                                                $date[$j]['size'] = 4;
2164                                                $date[$j]['digit'] = 'Y';
2165                                                break;
2166
2167                                        case 'm':
2168                                        case 'M':
2169                                                $date[$j]['size'] = 2;
2170                                                $date[$j]['digit'] = 'M';
2171                                                break;
2172
2173                                        case 'd':
2174                                                $date[$j]['size'] = 2;
2175                                                $date[$j]['digit'] = 'D';
2176                                }
2177                                $j++;
2178                        }
2179                        $datecount = 0;
2180
2181                        /* Verify Data and performs insertion/update */
2182                        foreach($data as $field => $value)
2183                        {
2184
2185                                switch($field)
2186                                {
2187                                        case 'names_ordered':
2188                                                $data[$field] = addslashes(rawurldecode($value));
2189                                        case 'corporate_name':
2190                                        case 'job_title':
2191                                        case 'department':
2192                                        case 'web_page':
2193                                        case 'alias':
2194                                        case 'given_names':
2195                                        case 'family_names':
2196                                        case 'pgp_key':
2197                                        case 'notes':
2198                                                $data[$field] = addslashes(rawurldecode($data[$field]));
2199                                                break;
2200
2201                                        case 'id_status':
2202                                        case 'id_prefix':
2203                                        case 'id_suffix':
2204                                                if ($data[$field] == 0)
2205                                                {
2206                                                        unset($data[$field]);
2207                                                }
2208                                                break;
2209
2210                                        case 'birthdate_0':
2211                                        case 'birthdate_1':
2212                                        case 'birthdate_2':
2213                                        case 'birthdate':
2214                                       
2215                                                if($field == 'birthdate'){
2216                                                        $array_birth = explode("/",$data[$field]);                                                     
2217                                                        $date['value'][2] = $array_birth[2];
2218                                                        $date['value'][1] = $array_birth[1];
2219                                                        $date['value'][0] = $array_birth[0];                                           
2220                                                }else{                                 
2221                                                        switch($date[$datecount]['digit'])
2222                                                        {
2223                                                                case 'Y':
2224                                                                        $date['value'][2] = (int) $data[$field];
2225                                                                        break;
2226       
2227                                                                case 'M':
2228                                                                        $date['value'][0] = (int) $data[$field];
2229                                                                        break;
2230       
2231                                                                case 'D':
2232                                                                        $date['value'][1] = (int) $data[$field];
2233                                                                        break;
2234                                                        }
2235                                                        unset($data[$field]);
2236                                                }
2237                                               
2238                                                $datecount++;
2239
2240                                                if ($datecount != 3)
2241                                                {
2242                                                        break;
2243                                                }
2244
2245                                                if($date['value'][0] =='' && $date['value'][1] =='' && $date['value'][2] ==''){
2246                                                        $data['birthdate'] = null;
2247                                                        break;
2248                                                }
2249                                                if (!checkdate($date['value'][0], $date['value'][1], $date['value'][2]))
2250                                                {
2251                                                        echo serialize(array(
2252                                                                'msg' => lang('Invalid Date'),
2253                                                                'status' => 'invalid_data'
2254                                                        ));
2255                                                        return;
2256                                                }
2257                                                if( $date['value'][2] != "" && $date['value'][0] != "" && $date['value'][1] != ""){
2258                                                        $data['birthdate'] = $date['value'][2].'-'.$date['value'][0].'-'.$date['value'][1];
2259                                                }
2260                                                break;
2261
2262                                        case 'sex':
2263                                                if ($data[$field] !== 'M' and $data[$field] !== 'F')
2264                                                {
2265                                                        echo serialize(array(
2266                                                                'msg' => lang('Invalid Sex'),
2267                                                                'status' => 'invalid_data'
2268                                                        ));
2269                                                        return;
2270                                                }
2271                                                break;
2272
2273
2274                                        case 'addresses':
2275                                                /* Insert new cities/states */
2276                                                if (isset($value['new_states']))
2277                                                {
2278                                                        foreach($value['new_states'] as $type => $state_info)
2279                                                        {
2280                                                                $index = 'address'.$type;
2281
2282                                                                $id_state = $this->bo->catalog->add_state($state_info);
2283                                                                $data['addresses'][$index]['id_state'] = $id_state;
2284
2285                                                                if ($value['new_cities'][$type])
2286                                                                {
2287                                                                        $data[$field]['new_cities'][$type]['id_state'] = $id_state;
2288                                                                }
2289                                                        }
2290
2291                                                        unset($data['addresses']['new_states']);
2292                                                }
2293
2294                                                if (isset($value['new_cities']))
2295                                                {
2296                                                        foreach($value['new_cities'] as $type => $city_info)
2297                                                        {
2298                                                                $index = 'address'.$type;
2299
2300                                                                $id_city = $this->bo->catalog->add_city($city_info);
2301                                                                $data['addresses'][$index]['id_city'] = $id_city;
2302                                                        }
2303
2304                                                        unset($data['addresses']['new_cities']);
2305                                                }
2306
2307                                        break;
2308
2309                                        case 'birthdate':
2310                                        case 'connections':
2311                                        case 'photo':
2312                                                /* Does nothing... */
2313                                                break;
2314
2315                                        default:
2316                                                echo serialize(array(
2317                                                        'msg' => lang('Invalid field: ').$field,
2318                                                        'status' => 'invalid_data'
2319                                                ));
2320                                                return;
2321                                }
2322                        }
2323
2324                        if (!is_null($id) and $id !== '')
2325                        {
2326                                $id = $this->bo->catalog->update_single_info($id, $data);
2327                                $result = array(
2328                                        'msg' => lang('Updated Successfully!'),
2329                                        'status' => 'ok'
2330                                );
2331                        }
2332                        else
2333                        {
2334                                $id = $this->bo->catalog->add_single_entry($data);
2335                                $result = array(
2336                                        'msg' => lang('Entry Added Successfully!'),
2337                                        'status' => 'ok'
2338                                );
2339                        }
2340
2341                        if (!($id))
2342                        {
2343                                $result = array(
2344                                        'msg' => lang('Some problem occured when trying to insert/update contact information.<br>'.
2345                                                   'Report the problem to the Administrator.'),
2346                                        'status' => 'fail'
2347                                );
2348                        }
2349
2350                        echo serialize($result);
2351                }
2352
2353
2354                function post_full_add_shared()
2355                {
2356                        $data =  $_POST['data'];
2357                        // Exceptions!!! utf8 special chars.
2358                        $data = preg_replace("/\%u2(\d+)(\d+)(\d+)/","-",$data);
2359                        $data = unserialize(str_replace('\\"', '"', $data));
2360                        $this -> bo -> catalog = CreateObject('contactcenter.bo_shared_people_manager');
2361
2362                        if (!is_array($data))
2363                        {
2364                                echo serialize(array(
2365                                        'msg' => lang('<p>Some problem receiving data from browser. This is probably a bug in ContactCenter<br>'.
2366                                                  'Please go to eGroupWare Bug Reporting page and report this bug.<br>'.
2367                                                          'Sorry for the inconvenient!<br><br>'.
2368                                                          '<b><i>ContactCenter Developer Team</i></b></p>'),
2369                                        'status' => 'fatal'
2370                                ));
2371                                return;
2372                        }
2373//                      print_r($data);
2374//                      echo '<br><br>';
2375
2376                        $replacer = $data['commercialAnd'];
2377                        unset($data['commercialAnd']);
2378                        if (!is_string($replacer) or strpos($replacer, "'") or strpos($replacer, '"'))
2379                        {
2380                                echo serialize(array(
2381                                        'msg' => lang('Invalid \'&\' replacer! This may be an attempt to bypass Security! Action aborted!'),
2382                                        'status' => 'fatal'
2383                                ));
2384
2385                                return;
2386                        }
2387
2388                        if ($data['id_contact'])
2389                        {
2390                                $id = $data['id_contact'];
2391                                $id_photo = $id;
2392                                unset($data['id_contact']);
2393                        }
2394                        else
2395                        {
2396                                $id_photo = '_new_';
2397                        }
2398
2399                        if ($data['owner'])
2400                        {
2401                                $owner = $data['owner'];
2402                                unset($data['owner']);
2403                        }
2404                        /*
2405                         * Process Photo, if available
2406                         */
2407                        $sleep_count = 0;
2408                        $photo_ok = $GLOBALS['phpgw']->session->appsession('ui_data.photo','contactcenter');
2409                        while($photo_ok[0]{0} !== 'o' and $photo_ok[1]{0} === 'y')
2410                        {
2411                                sleep(1);
2412                                $photo_ok = $GLOBALS['phpgw']->session->appsession('ui_data.photo','contactcenter');
2413                                $sleep_count++;
2414
2415                                if ($sleep_count > 35)
2416                                {
2417                                        // TODO
2418                                        return;
2419                                }
2420                        }
2421                        $GLOBALS['phpgw']->session->appsession('ui_data.photo','contactcenter', array('wait', 'n'));
2422
2423                        if (isset($this->page_info['photos'][$id_photo]))
2424                        {
2425                                if (array_search($this->page_info['photos'][$id_photo]['status'], array('changed', 'sync')) === false)
2426                                {
2427                                        echo serialize(array(
2428                                                'msg' => $this->page_info['photos'][$id_photo]['msg'],
2429                                                'status' => $this->page_info['photos'][$id_photo]['status']
2430                                        ));
2431
2432                                        return;
2433                                }
2434
2435                                $data['photo'] = $this->page_info['photos'][$id_photo]['content'];
2436                                unset($this->page_info['photos'][$id_photo]);
2437                                $this->save_session();
2438                        }
2439
2440                        /*
2441                         * Arrange Date so it gets inserted correctly
2442                         */
2443
2444                        $dateformat = $GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'];
2445
2446                        $j = 0;
2447                        for ($i = 0; $i < 5; $i+=2)
2448                        {
2449                                switch($dateformat{$i})
2450                                {
2451                                        case 'Y':
2452                                                $date[$j]['size'] = 4;
2453                                                $date[$j]['digit'] = 'Y';
2454                                                break;
2455
2456                                        case 'm':
2457                                        case 'M':
2458                                                $date[$j]['size'] = 2;
2459                                                $date[$j]['digit'] = 'M';
2460                                                break;
2461
2462                                        case 'd':
2463                                                $date[$j]['size'] = 2;
2464                                                $date[$j]['digit'] = 'D';
2465                                }
2466                                $j++;
2467                        }
2468                        $datecount = 0;
2469
2470                        /* Verify Data and performs insertion/update */
2471                        foreach($data as $field => &$value)
2472                        {
2473                                if ($value == '' or is_null($value))
2474                                {
2475                                        unset($data[$field]);
2476                                        continue;
2477                                }
2478
2479                                switch($field)
2480                                {
2481                                        case 'corporate_name':
2482                                        case 'job_title':
2483                                        case 'department':
2484                                        case 'web_page':
2485                                        case 'alias':
2486                                        case 'given_names':
2487                                        case 'family_names':
2488                                        case 'names_ordered':
2489                                        case 'pgp_key':
2490                                        case 'notes':
2491                                        case 'photo':
2492                                                $data[$field] = urldecode( $value );
2493                                                break;
2494
2495                                        case 'id_status':
2496                                        case 'id_prefix':
2497                                        case 'id_suffix':
2498                                                if ($data[$field] == 0)
2499                                                {
2500                                                        unset($data[$field]);
2501                                                }
2502                                                break;
2503
2504                                        case 'birthdate_0':
2505                                        case 'birthdate_1':
2506                                        case 'birthdate_2':
2507
2508                                                switch($date[$datecount]['digit'])
2509                                                {
2510                                                        case 'Y':
2511                                                                $date['value'][2] = (int) $data[$field];
2512                                                                break;
2513
2514                                                        case 'M':
2515                                                                $date['value'][0] = (int) $data[$field];
2516                                                                break;
2517
2518                                                        case 'D':
2519                                                                $date['value'][1] = (int) $data[$field];
2520                                                                break;
2521                                                }
2522                                                unset($data[$field]);
2523                                                $datecount++;
2524
2525                                                if ($datecount != 3)
2526                                                {
2527                                                        break;
2528                                                }
2529
2530                                                if (!checkdate($date['value'][0], $date['value'][1], $date['value'][2]))
2531                                                {
2532                                                        echo serialize(array(
2533                                                                'msg' => lang('Invalid Date'),
2534                                                                'status' => 'invalid_data'
2535                                                        ));
2536                                                        return;
2537                                                }
2538
2539                                                $data['birthdate'] = $date['value'][2].'-'.$date['value'][0].'-'.$date['value'][1];
2540                                                break;
2541
2542                                        case 'sex':
2543                                                if ($data[$field] !== 'M' and $data[$field] !== 'F')
2544                                                {
2545                                                        echo serialize(array(
2546                                                                'msg' => lang('Invalid Sex'),
2547                                                                'status' => 'invalid_data'
2548                                                        ));
2549                                                        return;
2550                                                }
2551                                                break;
2552
2553
2554                                        case 'addresses':
2555                                                /* Insert new cities/states */
2556                                                if (isset($value['new_states']))
2557                                                {
2558                                                        foreach($value['new_states'] as $type => $state_info)
2559                                                        {
2560                                                                $index = 'address'.$type;
2561
2562                                                                $id_state = $this->bo->catalog->add_state($state_info);
2563                                                                $data['addresses'][$index]['id_state'] = $id_state;
2564
2565                                                                if ($value['new_cities'][$type])
2566                                                                {
2567                                                                        $value['new_cities'][$type]['id_state'] = $id_state;
2568                                                                }
2569                                                        }
2570
2571                                                        unset($data['addresses']['new_states']);
2572                                                }
2573
2574                                                if (isset($value['new_cities']))
2575                                                {
2576                                                        foreach($value['new_cities'] as $type => $city_info)
2577                                                        {
2578                                                                $index = 'address'.$type;
2579
2580                                                                $id_city = $this->bo->catalog->add_city($city_info);
2581                                                                $data['addresses'][$index]['id_city'] = $id_city;
2582                                                        }
2583
2584                                                        unset($data['addresses']['new_cities']);
2585                                                }
2586
2587                                        break;
2588
2589                                        case 'connections':
2590                                                /* Does nothing... */
2591                                                break;
2592
2593                                        default:
2594                                                echo serialize(array(
2595                                                        'msg' => lang('Invalid field: ').$field,
2596                                                        'status' => 'invalid_data'
2597                                                ));
2598                                                return;
2599                                }
2600                        }
2601
2602                        $code = '$id = $this->bo->catalog->';
2603
2604                        if (!is_null($id) and $id !== '')
2605                        {
2606                                $code .= $code.'update_single_info($id, $data);';
2607                                $result = array(
2608                                        'msg' => lang('Updated Successfully!'),
2609                                        'status' => 'ok'
2610                                );
2611                        }
2612                        else
2613                        {
2614                                $code .= 'add_single_entry($data,'.$owner.');';
2615                                $result = array(
2616                                        'msg' => lang('Entry Added Successfully!'),
2617                                        'status' => 'ok'
2618                                );
2619                        }
2620
2621                        eval($code);
2622
2623                        if (!($id))
2624                        {
2625                                $result = array(
2626                                        'msg' => lang('Some problem occured when trying to insert/update contact information.<br>'.
2627                                                   'Report the problem to the Administrator.'),
2628                                        'status' => 'fail'
2629                                );
2630                        }
2631
2632                        echo serialize($result);
2633}
2634                /*!
2635
2636                        @function post_photo
2637                        @abstract Wrapper to post a photo without reload a page.
2638                        @author Raphael Derosso Pereira
2639
2640                */
2641                function post_photo($id)
2642                {
2643                        //print_r($_FILES);
2644                        $GLOBALS['phpgw']->session->appsession('ui_data.photo','contactcenter', array('wait', 'y'));
2645
2646                        if (!is_array($_FILES) and is_array(!$_FILES['cc_pd_photo']))
2647                        {
2648                                $this->page_info['photos'][$id]['status'] = 'no_upload';
2649                                $this->page_info['photos'][$id]['msg'] = lang('No Photos uploaded to Server.');
2650
2651                                $this->save_session();
2652                                $GLOBALS['phpgw']->session->appsession('ui_data.photo','contactcenter', array('ok', 'y'));
2653                                return;
2654                        }
2655
2656                        if (!function_exists('imagecreate'))
2657                        {
2658                                $this->page_info['photos'][$id]['status'] = 'no_GD_lib';
2659                                $this->page_info['photos'][$id]['msg'] = lang('Cannot manipulate Image. No Image added. Please, if you want to use images, ask the Administrator to install GD library.');
2660
2661                                $this->save_session();
2662                                $GLOBALS['phpgw']->session->appsession('ui_data.photo','contactcenter', array('ok', 'y'));
2663                                return;
2664                        }
2665
2666                        // TODO: Get Max Size from preferences!
2667                        if ($_FILES['cc_pd_photo']['size'] > 1000000)
2668                        {
2669                                $this->page_info['photos'][$id]['status'] = 'too_large';
2670                                $this->page_info['photos'][$id]['msg'] = lang('Image too large! ContactCenter limits the image size to 1 Mb');
2671
2672                                $this->save_session();
2673                                $GLOBALS['phpgw']->session->appsession('ui_data.photo','contactcenter', array('ok', 'y'));
2674                                return;
2675                        }
2676
2677                        if ($_FILES['cc_pd_photo']['error'])
2678                        {
2679                                $this->page_info['photos'][$id]['status'] = 'error';
2680                                $this->page_info['photos'][$id]['msg'] = lang('Some Error occured while processed the Image. Contact the Administrator. The error code was: ').$_FILES['cc_pd_photo']['error'];
2681
2682                                $this->save_session();
2683                                $GLOBALS['phpgw']->session->appsession('ui_data.photo','contactcenter', array('ok', 'y'));
2684                                return;
2685                        }
2686
2687                        switch($_FILES['cc_pd_photo']['type'])
2688                        {
2689                                case 'image/jpeg':
2690                                case 'image/pjpeg':
2691                                        $src_img = imagecreatefromjpeg($_FILES['cc_pd_photo']['tmp_name']);
2692                                        if ($src_img == '')
2693                                        {
2694                                                $bogus = true;
2695                                        }
2696                                        break;
2697
2698                                case 'image/png':
2699                                case 'image/x-png':
2700                                        $src_img = imagecreatefrompng($_FILES['cc_pd_photo']['tmp_name']);
2701                                        if ($src_img == '')
2702                                        {
2703                                                $bogus = true;
2704                                        }
2705                                        break;
2706
2707                                case 'image/gif':
2708                                        $src_img = imagecreatefromgif($_FILES['cc_pd_photo']['tmp_name']);
2709                                        if ($src_img == '')
2710                                        {
2711                                                $bogus = true;
2712                                        }
2713                                        break;
2714
2715                                default:
2716
2717                                        $this->page_info['photos'][$id]['status'] = 'invalid_image';
2718                                        $this->page_info['photos'][$id]['msg'] = lang('The file must be an JPEG, PNG or GIF Image.');
2719
2720                                        $this->save_session();
2721                                        $GLOBALS['phpgw']->session->appsession('ui_data.photo','contactcenter', array('ok', 'y'));
2722                                        return;
2723                        }
2724
2725                        if ($bogus)
2726                        {
2727                                        $this->page_info['photos'][$id]['status'] = 'invalid_file';
2728                                        $this->page_info['photos'][$id]['msg'] = lang('Couldn\'t open Image. It may be corrupted or internal library doesn\'t support this format.');
2729
2730                                        $this->save_session();
2731                                        $GLOBALS['phpgw']->session->appsession('ui_data.photo','contactcenter', array('ok', 'y'));
2732                                        return;
2733                        }
2734
2735                        $img_size = getimagesize($_FILES['cc_pd_photo']['tmp_name']);
2736                        $dst_img = imagecreatetruecolor(60, 80);
2737
2738                        if (!imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, 60, 80, $img_size[0], $img_size[1]))
2739                        {
2740                                $this->page_info['photos'][$id]['status'] = 'invalid_file';
2741                                $this->page_info['photos'][$id]['msg'] = lang('Couldn\'t open Image. It may be corrupted or internal library doesn\'t support this format.');
2742
2743                                $this->save_session();
2744                                $GLOBALS['phpgw']->session->appsession('ui_data.photo','contactcenter', array('ok', 'y'));
2745                                return;
2746                        }
2747
2748                        ob_start();
2749                        imagepng($dst_img);
2750                        $this->page_info['photos'][$id]['content'] = ob_get_contents();
2751                        ob_end_clean();
2752
2753                        $this->page_info['photos'][$id]['status'] = 'changed';
2754                        $this->page_info['photos'][$id]['msg'] = lang('Photo Successfully Updated!');
2755
2756                        $this->save_session();
2757
2758                        $GLOBALS['phpgw']->session->appsession('ui_data.photo','contactcenter', array('ok', 'y'));
2759
2760                        imagedestroy($src_img);
2761                        imagedestroy($dst_img);
2762                        echo 'ok';
2763                        return;
2764                }
2765
2766
2767                /*!
2768
2769                        @function get_photo
2770                        @abstract Returns the photo to the browser
2771                        @author Raphael Derosso Pereira
2772
2773                */
2774                function get_photo($id)
2775                {
2776                        $fields = $this->bo->catalog->get_fields(false);
2777                        $fields['photo'] = true;
2778
2779                        $contact = $this->bo->catalog->get_single_entry($id, $fields);
2780
2781                        if (!$contact['photo'])
2782                        {
2783                                header('Content-type: image/png');
2784                                echo file_get_contents(PHPGW_INCLUDE_ROOT.'/contactcenter/templates/default/images/photo_celepar.png');
2785                                return;
2786                        }
2787
2788                        header('Content-type: image/jpeg');
2789                        $photo = imagecreatefromstring ($contact['photo']);
2790                        $width = imagesx($photo);
2791                        $height = imagesy($photo);
2792                        $twidth = 70;
2793                        $theight = 90;
2794                        $small_photo = imagecreatetruecolor ($twidth, $theight);
2795                        imagecopyresampled($small_photo, $photo, 0, 0, 0, 0,$twidth, $theight, $width, $height);
2796                        imagejpeg($small_photo,"",100);
2797                        return;
2798                }
2799
2800                /*!
2801
2802                        @function get_states
2803                        @abstract Echos a serialized array containing all the states for the given country
2804                        @author Raphael Derosso Pereira
2805
2806                        @params $id_country The ID of the Country that contains the requested states
2807
2808                */
2809                function get_states($id_country)
2810                {
2811                        $states = $this->bo->catalog->get_all_states($id_country);
2812
2813                        if (!$states)
2814                        {
2815                                $result = array(
2816                                        'msg'    => lang('No States found for this Country.'),
2817                                        'status' => 'empty'
2818                                );
2819
2820                                echo serialize($result);
2821                                return;
2822                        }
2823
2824                        $result = array(
2825                                'msg'    => lang('States Successfully retrieved!'),
2826                                'status' => 'ok'
2827                        );
2828
2829                        foreach ($states as $state_info)
2830                        {
2831                                $result['data'][$state_info['id_state']] = $state_info['name'];
2832
2833                                if ($state_info['symbol'])
2834                                {
2835                                        $result['data'][$state_info['id_state']] .= ', '.$state_info['symbol'];
2836                                }
2837                        }
2838
2839                        echo serialize($result);
2840                }
2841
2842                /*!
2843
2844                        @function get_cities
2845                        @abstract Echos a serialized array containing all the cities of a given state
2846                        @author Raphael Derosso Pereira
2847
2848                        @param $id_country The ID of the Country that has the specified Cities (in case the
2849                                Country doesn't have any States)
2850                        @param $id_state The ID of the State that has the Cities requested
2851
2852                */
2853                function get_cities($id_country, $id_state=false)
2854                {
2855                        $cities = $this->bo->catalog->get_all_cities($id_country, $id_state);
2856
2857                        if (!$cities)
2858                        {
2859                                $result = array(
2860                                        'msg'    => lang('No Cities found for this State.'),
2861                                        'status' => 'empty'
2862                                );
2863
2864                                echo serialize($result);
2865                                return;
2866                        }
2867
2868                        $result = array(
2869                                'msg'    => lang('Cities Successfully retrieved!'),
2870                                'status' => 'ok'
2871                        );
2872
2873                        foreach ($cities as $city_info)
2874                        {
2875                                $result['data'][$city_info['id_city']] = $city_info['name'];
2876                        }
2877
2878                        echo serialize($result);
2879                }
2880
2881                //Traduz o campo na busca completa por entradas no catálogo do usuário.
2882                function aux_full_search ($field,$isldap) {
2883                        $retorno = '';
2884                        if($isldap) {
2885                                switch($field) {
2886                                        case 'mail':
2887                                                $retorno = 'contact.connection.mail';
2888                                                break;
2889                                        case 'phone':
2890                                                $retorno = 'contact.connection.phone';
2891                                                break;
2892                                }
2893                        }
2894                        else {
2895                                switch($field) {
2896                                        case 'corporate':
2897                                                $retorno = 'contact.corporate_name';
2898                                                break;
2899                                        case 'mail':
2900                                        case 'phone':
2901                                                $retorno = 'contact.contact_connection.connection.connection_value';
2902                                                break;
2903                                }
2904                        }
2905                        return $retorno;
2906                }
2907
2908                /*!
2909
2910                        @function search
2911                        @abstract Echos a serialized array containing the IDs
2912                                of the entries that matches the search argument
2913                        @author Raphael Derosso Pereira
2914                        @author Mário César Kolling (external catalogs)
2915
2916                        @param string $str_data A serialized array with two informations:
2917                                $data = array(
2918                                        'search_for' => (string),
2919                                        'recursive'  => (boolean),
2920                                );
2921
2922                */
2923                // SERPRO
2924                function search($str_data)
2925                {
2926                        $data = unserialize($str_data);
2927                        // It's an external catalog?
2928                        $external = $this->bo->is_external($this->page_info['actual_catalog']);
2929                        $full_search = isset($data['full_search'])?$data['full_search']:false;
2930                       
2931                        if (!is_array($data) || (!$data['search_for'] && !$full_search) || !is_array($data['fields']))
2932                        {
2933                                echo serialize(array(
2934                                        'msg'    => lang('Invalid parameters'),
2935                                        'status' => 'abort'
2936                                ));
2937
2938                                return array('error' => lang('Invalid parameters'));
2939                        }
2940
2941
2942                        /*
2943                         * TODO: look into the database to discover the database's encoding and convert the search_for field accordingly
2944                         */
2945                        // Support search parameters with accentuation
2946                        if ($this->page_info['actual_catalog']['class'] != 'bo_people_catalog' &&
2947/**rev 104**/
2948                                //$this->page_info['actual_catalog']['class'] != 'bo_group_manager')
2949                                $this->page_info['actual_catalog']['class'] != 'bo_group_manager' &&
2950                                $this->page_info['actual_catalog']['class'] != 'bo_shared_people_manager' &&
2951                                $this->page_info['actual_catalog']['class'] != 'bo_shared_group_manager')
2952/****/
2953                        {
2954
2955                                $data['search_for'] = utf8_encode($data['search_for']);
2956                        }
2957
2958                        $rules  = array();
2959
2960                        if ($data['search_for'] === '*')
2961                        {
2962                                $rules = array(
2963                                        0 => array(
2964                                                'field' => $data['fields']['search'],
2965                                                'type'  => 'LIKE',
2966                                                'value' => '%'
2967                                        )
2968                                );
2969                        }
2970                        else
2971                        {
2972                                $names = explode(' ', $data['search_for']);
2973
2974                                if (!is_array($names))
2975                                {
2976                                        if(!$full_search) {
2977                                                echo serialize(array(
2978                                                        'msg'    => lang('Invalid Search Parameter'),
2979                                                        'status' => 'abort'
2980                                                ));
2981                                                exit;
2982                                        }
2983                                        else
2984                                                $names = array();
2985
2986                                }
2987
2988                                if (!$external && $this->page_info['actual_catalog']['class'] != 'bo_people_catalog' &&
2989/**rev 104**/
2990                                        //$this->page_info['actual_catalog']['class'] != 'bo_group_manager')
2991                                        $this->page_info['actual_catalog']['class'] != 'bo_group_manager' &&
2992                                        $this->page_info['actual_catalog']['class'] != 'bo_shared_people_manager' &&
2993                                        $this->page_info['actual_catalog']['class'] != 'bo_shared_group_manager' )
2994/*****/
2995                                {
2996                                        /*
2997                                         * Restrict the returned contacts search to objectClass = phpgwAccount,
2998                                         * must have attibute phpgwAccountStatus, phpgwAccountVisible != -1
2999                                         */
3000                                       
3001                                        $rules = array(
3002                                                0 => array(
3003                                                        'field' => 'contact.object_class',
3004                                                        'type'  => '=',
3005                                                        'value' => 'phpgwAccount'
3006                                                ),
3007                                                1 => array(
3008                                                        'field' => 'contact.account_status',
3009                                                        'type'  => 'iLIKE',
3010                                                        'value' => '%'
3011                                                ),
3012/**rev 104**/
3013                                                ///
3014                                                //1 => array(
3015                                                2 => array(
3016/*****/
3017                                                        'field' => 'contact.account_visible',
3018                                                        'type'  => '!=',
3019                                                        'value' => '-1'
3020/**rev 104**/
3021        /*                                      ),
3022                                                2 => array(
3023                                                        'field' => 'contact.object_class',
3024                                                        'type'  => '=',
3025                                                        'value' => 'inetOrgPerson'
3026/****/
3027                                                ),
3028                                        );
3029
3030                                        foreach($full_search as $field => $value) {
3031                                                if(trim($value)!='')
3032                                                        array_push($rules,array(
3033                                                                                        'field' => $this->aux_full_search($field,true),
3034                                                                                        'type' => 'LIKE',
3035                                                                                        'value' => '*'.$value.'*'
3036                                                                                        ));
3037                                        }
3038
3039                                }
3040                                else if(!$external) {
3041                                       
3042                                        foreach($full_search as $field => $value) {
3043                                                if(trim($value)!='')
3044                                                        array_push($rules,array(
3045                                                                                        'field' => $this->aux_full_search($field,false),
3046                                                                                        'type' => 'iLIKE',
3047                                                                                        'value' => '%'.$value.'%'
3048                                                                                        ));
3049                                        }
3050                               
3051                                }
3052
3053                                foreach ($names as $name)
3054                                {
3055                                        if ($name != '')
3056                                        {
3057                                                array_push($rules, array(
3058                                                        'field' => $data['fields']['search'],
3059                                                        'type'  => 'iLIKE',
3060                                                        'value' => '%'.$name.'%'
3061                                                ));
3062                                        }
3063                                }
3064                        }
3065
3066                        if ($external || $this->page_info['actual_catalog']['class'] == 'bo_people_catalog' ||
3067/**rev 104**/
3068                                //$this->page_info['actual_catalog']['class'] == 'bo_group_manager')
3069                                $this->page_info['actual_catalog']['class'] == 'bo_group_manager' ||
3070                                $this->page_info['actual_catalog']['class'] == 'bo_shared_people_manager' ||
3071                                $this->page_info['actual_catalog']['class'] == 'bo_shared_group_manager')
3072
3073
3074/***/
3075                        {
3076                                // Get only this attributes: dn, cn for external catalogs,
3077                                // used to restrict the attributes used in filters
3078                                $ids = $this->bo->find(array($data['fields']['id'], $data['fields']['search']), $rules, array('order' => $data['fields']['search'], 'sort' => 'ASC'), $data['search_for'] != null);
3079                        }
3080                        else
3081                        {
3082                                // Get only this attributes: dn, cn, phpgwAccountType, objectClass, phpgwAccountStatus, phpghAccountVisible
3083                                // for non-external catalogs, used to restrict the attributes used in filters
3084                                $ids = $this->bo->find(array(
3085                                        $data['fields']['id'],
3086                                        $data['fields']['search'],
3087                                        'contact.object_class',
3088                                        //'contact.account_status',
3089                                        'contact.account_visible',
3090                                        'contact.connection.mail',
3091                                        'contact.connection.phone'
3092                                        ), $rules, array('order' => $data['fields']['search'], 'sort' => 'ASC'), $data['search_for_area'], $data['search_for'] != null );
3093                        }
3094
3095                        if (!is_array($ids) || !count($ids))
3096                        {
3097                                $this->last_search_ids = null;
3098                                $this->save_session();
3099                                return null;
3100                        }
3101
3102                        $id_field = substr($data['fields']['id'], strrpos($data['fields']['id'], '.')+1);
3103
3104                        $ids_f = array();
3105
3106                        foreach ($ids as $e_info)
3107                        {
3108/**rev 104**/
3109                                //$ids_f[] = $e_info[$id_field];
3110                                if($this->page_info['actual_catalog']['class'] != 'bo_shared_people_manager' && $this->page_info['actual_catalog']['class'] != 'bo_shared_group_manager')
3111                                {
3112                                        $ids_f[] = $e_info[$id_field];
3113                                } else{
3114                                        $ids_f[] = array(0=>$e_info[$id_field],1=>$e_info['perms'],2=>$e_info['owner']);
3115                                }
3116/****/
3117                        }
3118
3119                        return $ids_f;
3120                }
3121
3122                // CELEPAR
3123                /*
3124        function search($str_data)
3125        {
3126            $data = unserialize($str_data);
3127
3128            if (!is_array($data) || !$data['search_for'] || !is_array($data['fields']))
3129            {
3130                echo serialize(array(
3131                    'msg'    => lang('Invalid parameters'),
3132                    'status' => 'abort'
3133                ));
3134
3135                return;
3136            }
3137
3138            $rules  = array();
3139
3140            if ($data['search_for'] === '*')
3141            {
3142                $rules = array(
3143                    0 => array(
3144                        'field' => $data['fields']['search'],
3145                        'type'  => 'LIKE',
3146                        'value' => '%'
3147                    )
3148                );
3149            }
3150            else
3151            {
3152                $names = explode(' ', $data['search_for']);
3153
3154                if (!is_array($names))
3155                {
3156                    echo serialize(array(
3157                        'msg'    => lang('Invalid Search Parameter'),
3158                        'status' => 'abort'
3159                    ));
3160
3161                    return;
3162                }
3163
3164                foreach ($names as $name)
3165                {
3166                    if ($name != '')
3167                    {
3168                        array_push($rules, array(
3169                            'field' => $data['fields']['search'],
3170                            'type'  => 'iLIKE',
3171                            'value' => '%'.$name.'%'
3172                        ));
3173                    }
3174                }
3175            }
3176
3177
3178
3179            //$catalog = $this->bo->get_branch_by_level($this->bo->catalog_level[0]);
3180
3181            //if ($catalog['class'] === 'bo_people_catalog')
3182            //{
3183            //    array_push($rules, array(
3184            //        'field' => 'contact.id_owner',
3185            //        'type'  => '=',
3186            //        'value' => $GLOBALS['phpgw_info']['user']['account_id']
3187            //    ));
3188            //}
3189
3190
3191            $ids = $this->bo->find(array($data['fields']['id'], $data['fields']['search']), $rules, array('order' => $data['fields']['search'], 'sort' => 'ASC'));
3192
3193            if (!is_array($ids) || !count($ids))
3194            {
3195                echo serialize(array(
3196                    'msg'    => lang('No Entries Found!'),
3197                    'status' => 'empty'
3198                ));
3199
3200                return;
3201            }
3202            $id_field = substr($data['fields']['id'], strrpos($data['fields']['id'], '.')+1);
3203
3204            $ids_f = array();
3205            foreach ($ids as $e_info)
3206            {
3207                $ids_f[] = $e_info[$id_field];
3208            }
3209
3210            echo serialize(array(
3211                'data'   => $ids_f,
3212                'msg'    => lang('Found %1 Entries', count($ids)),
3213                'status' => 'ok'
3214            ));
3215
3216                        return;
3217        }*/
3218                /*!
3219
3220                        @function get_multiple_entries
3221                        @abstract Returns an array containing the specifiend data in the default
3222                                CC UI format
3223                        @author Raphael Derosso Pereira
3224
3225                        @param array str_data A serialized array containing the ID's of the entries
3226                                to be taken, the fields to be taken and the rules to be used on the
3227                                retrieval:
3228                                $data = array(
3229                                        'ids'    => array(...),
3230                                        'fields' => array(...),
3231                                        'rules'  => array(...)
3232                                );
3233
3234                */
3235                function get_multiple_entries($str_data)
3236                {
3237                        $data = unserialize($str_data);
3238
3239                        if (!is_array($data) or !count($data) or !count($data['fields']) or !count($data['ids']))
3240                        {
3241                                return array(
3242                                        'msg'    => lang('Invalid Parameters'),
3243                                        'status' => 'abort'
3244                                );
3245                        }
3246
3247                        $entries = $this->bo->catalog->get_multiple_entries($data['ids'], $data['fields']);
3248
3249                        if (!is_array($entries) or !count($entries))
3250                        {
3251                                return array(
3252                                        'msg'    => lang('No Entries Found!'),
3253                                        'status' => 'empty'
3254                                );
3255                        }
3256
3257                        return array(
3258                                'msg'    => lang('Found %1 Entries!', count($entries)),
3259                                'status' => 'ok',
3260                                'data'   => $entries
3261                        );
3262                }
3263
3264                /*
3265
3266                        @function get_all_entries
3267                        @abstract Returns the specified fields for all catalog's entries
3268                                in the default CC UI format
3269                        @author Raphael Derosso Pereira
3270
3271                        @params array str_data A serialized array containing the fields to
3272                                be grabbed, the maximum number of entries to be returned and a
3273                                boolean specifying if the calls refers to a new grab or to an
3274                                unfinished one.
3275
3276                */
3277                function get_all_entries($str_data)
3278                {
3279                        $data = unserialize($str_data);
3280
3281                        if (!is_array($data) or
3282                            !count($data) or
3283                                !count($data['fields']) or
3284                                !$data['maxlength'] or
3285                                (!$data['new'] and !$data['offset']))
3286                        {
3287                                return array(
3288                                        'msg'    => lang('Invalid Parameters'),
3289                                        'status' => 'abort'
3290                                );
3291                        }
3292
3293                        if ($data['new'])
3294                        {
3295                                $this->all_entries = $this->bo->catalog->get_all_entries_ids();
3296
3297                                $this->save_session();
3298
3299                                if (!is_array($this->all_entries) or !count($this->all_entries))
3300                                {
3301                                        return array(
3302                                                'msg'    => lang('No Entries Found!'),
3303                                                'status' => 'empty'
3304                                        );
3305                                }
3306
3307                                $data['offset'] = 0;
3308                        }
3309
3310                        if ($data['maxlength'] != -1)
3311                        {
3312                                $result = $this->bo->catalog->get_multiple_entries(array_slice($this->all_entries, $data['offset'], $data['maxlength']), $data['fields']);
3313                        }
3314                        else
3315                        {
3316                                $result = $this->bo->catalog->get_multiple_entries($this->all_entries, $data['fields']);
3317                        }
3318
3319                        $prefs = ExecMethod('contactcenter.ui_preferences.get_preferences');
3320
3321                        $jsCode = array();
3322                        $count = 0;
3323                        foreach ($result as $each)
3324                        {
3325                                if (!is_array($each))
3326                                {
3327                                        continue;
3328                                }
3329
3330                                if($this-> typeContact == 'groups') {
3331
3332                                        foreach ($each as $field => $value)     {
3333
3334                                                if ($field === 'title') {
3335                                                        $optionName = '\\"'.$value.'\\"';
3336
3337                                                }
3338                                                else if ($field === 'short_name')       {
3339
3340                                                        $jsCode[] = '_this.entries.options[_this.entries.options.length] = new Option("'.$optionName.' ('.$value.')", "'.$count.'");';
3341                                                        $count++;
3342                                                }
3343                                        }
3344                                }
3345
3346                                else  {
3347                                        foreach ($each as $field => $value)     {
3348                                                if ($field === 'names_ordered') {
3349                                                         if(is_array($value))
3350                                $value = $value[0];
3351                                                        $name = '\\"'.$value.'\\"';
3352                                                }
3353                                                else if ($field === 'connections')      {
3354
3355                                                        foreach ($value as $connection)         {
3356                                                                if ($connection['id_type'] == $prefs['personCardEmail'])        {
3357                                                                        $jsCode[] = '_this.entries.options[_this.entries.options.length] = new Option("'.$name.' <'.$connection['connection_value'].'>", "'.$count.'");';
3358                                                                        $count++;
3359                                                                }
3360                                                        }
3361                                                }
3362                                        }
3363                                }
3364                        }
3365
3366                        $jsCodeFinal = implode("\n", $jsCode);
3367
3368                        $nEntries = count($result);
3369
3370                        if (!$nEntries)
3371                        {
3372                                return array(
3373                                        'msg'    => lang('Error while getting user information...'),
3374                                        'status' => 'abort'
3375                                );
3376                        }
3377
3378                        return array(
3379                                'msg'      => lang('Found %1 Entries!', $nEntries),
3380                                'status'   => 'ok',
3381                                'typeContact'   => $this -> typeContact,
3382                                'final'    => $nEntries + $data['offset'] < count($this->all_entries) ? false : true,
3383                                'offset'   => $data['offset'] + $nEntries,
3384                                'data'     => $jsCodeFinal
3385                        );
3386                }
3387
3388                /*********************************************************************\
3389                 *                      Auxiliar Methods                             *
3390                \*********************************************************************/
3391
3392                /*!
3393
3394                        @function save_session
3395                        @abstract Saves the data on the session
3396                        @author Raphael Derosso Pereira
3397
3398                */
3399                function save_session()
3400                {
3401                        $GLOBALS['phpgw']->session->appsession('ui_data.page_info','contactcenter',$this->page_info);
3402                        $GLOBALS['phpgw']->session->appsession('ui_data.all_entries','contactcenter',$this->all_entries);
3403                }
3404
3405                /*!
3406
3407                        @function convert_tree
3408                        @abstract Converts the tree array in the BO format to a JS tree array compatible
3409                                with the one available in eGW
3410                        @author Raphael Derosso Pereira
3411
3412                        @param (array)  $tree    The tree in the BO format
3413                        @param (string) $name    The tree name
3414                        @param (string) $iconDir The dir where the icons are
3415                        @param (string) $parent  The parent
3416                */
3417
3418                function convert_tree($tree, &$iconDir, $parent='0')
3419                {
3420//                      echo "Entrou<br>\tPai: $parent <br>";
3421                        $rtree = array();
3422
3423                        if ($parent === '0')
3424                        {
3425//                              echo 'Root!<br>';
3426                                $rtree['0'] = array(
3427                                        'type'       => 'catalog_group',
3428                                        'id'         => '0',
3429                                        'pid'        => 'none',
3430                                        'caption'    => lang('Catalogues'),
3431                                        'class'      => 'bo_catalog_group_catalog',
3432                                        'class_args' => array('_ROOT_', '$this', '$this->get_branch_by_level($this->catalog_level[0])')
3433                                );
3434                        }
3435
3436                        foreach($tree as $id => $value)
3437                        {
3438//                              echo 'ID: '.$id.'<br>';
3439                                $rtree[$parent.'.'.$id] = array(
3440                                        'type'    => $value['type'],
3441                                        'id'      => $parent.'.'.$id,
3442                                        'pid'     => $parent,
3443                                        'caption' => $value['name']
3444                                );
3445
3446                                switch($value['type'])
3447                                {
3448                                        case 'catalog_group':
3449                                        case 'mixed_catalog_group':
3450                                                $rtree = $rtree + $this->convert_tree($value['sub_branch'],$iconDir,$parent.'.'.$id);
3451                                                break;
3452                                }
3453                        }
3454
3455                        if (count($rtree))
3456                        {
3457                                return $rtree;
3458                        }
3459                }
3460
3461                function get_catalog_add_contact($id){
3462
3463                        $array_participants = array();
3464                        if(!$this->bo->catalog->src_info) {
3465                                $ldap = CreateObject('contactcenter.bo_ldap_manager');
3466                                $this->bo->catalog->src_info = $ldap->srcs[1];
3467                        }
3468
3469                        $ds = $GLOBALS['phpgw']->common->ldapConnect($this->bo->catalog->src_info['host'], $this->bo->catalog->src_info['acc'], $this->bo->catalog->src_info['pw'], true);
3470                        $dn=$this->bo->catalog->src_info['dn'];
3471                        $justThese = array("givenname","givenname","sn","telephonenumber","mail");
3472                        $sr = ldap_read($ds,$id, "objectClass=*",$justThese);
3473                        $info = ldap_get_entries($ds, $sr);
3474                        for($z = 0; $z < 5; $z++) {
3475                                $participant = $info[0][$justThese[$z]];
3476                                array_push($array_participants, $participant);
3477                        }
3478
3479                        ldap_close($ds);
3480                        echo serialize($array_participants);
3481                }
3482
3483                function get_catalog_participants_group($id)
3484                {
3485                        if(!$this->bo->catalog->src_info) {
3486                                $ldap = CreateObject('contactcenter.bo_ldap_manager');
3487                                $this->bo->catalog->src_info = $ldap->srcs[1];
3488                        }
3489                        $ds = $GLOBALS['phpgw']->common->ldapConnect($this->bo->catalog->src_info['host'], $this->bo->catalog->src_info['acc'], $this->bo->catalog->src_info['pw'], true);
3490                        $justThese = array("description","memberuid");
3491                        $sr = ldap_read($ds,$id, "objectClass=*",$justThese);
3492                        $info = ldap_get_entries($ds, $sr);
3493                        $member_uids = $info[0]['memberuid'];
3494                        $contact['names_ordered'] = $info[0]['description'];
3495                        $filter = "";
3496                        for($z = 0; $z < count($member_uids); $z++) {
3497                                if($member_uids[$z])
3498                                        $filter.="(uid=".$member_uids[$z].")";
3499                        }
3500                        $array_participants = array();
3501                        if($filter) {
3502                                $filter = "(|".$filter.")";
3503                                $valarray = explode(',',$id);
3504                                array_shift($valarray);
3505                                $dn = implode(',',$valarray);
3506                                $justThese = array("cn","mail");
3507                                $sr = ldap_search($ds,$dn, $filter,$justThese);
3508                                $info = ldap_get_entries($ds, $sr);
3509                                for($z = 0; $z < $info['count']; $z++) {
3510                                        $participant =  '<font color=\'DARKBLUE\'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;'.$info[$z]['cn'][0].'&quot; &lt;'.$info[$z]['mail'][0].'&gt;</font><br>';
3511                                    $array_emails[$info[$z]['mail'][0]] = null;
3512                                        array_push($array_participants, $participant);
3513                                }
3514                                ldap_close($ds);
3515                        }
3516                        sort($array_participants);
3517                        $innerHTML = '';
3518                        foreach($array_participants as $index => $participant){
3519                                $innerHTML .= $participant;
3520                        }
3521                        $return = array('size' => count($array_participants), 'names_ordered'=> $contact['names_ordered'], 'inner_html' => $innerHTML);
3522                        echo serialize($return);
3523                }
3524
3525                function get_catalog_participants_list($id)
3526                {
3527
3528                        $fields = $this->bo->catalog->get_fields(false);
3529                        $fields['names_ordered'] = true;
3530                        $fields['mail_forwarding_address'] = true;
3531                        $contact = $this->bo->catalog->get_single_entry($id,$fields);
3532
3533                        $array_participants = array();
3534                        $array_emails = array();
3535
3536                        $filter = null;
3537                        for($z = 0; $z < $contact['mail_forwarding_address']['count']; $z++) {
3538                                        if(strstr($contact['mail_forwarding_address'][$z],'@')) {
3539                                                $filter.="(mail=".$contact['mail_forwarding_address'][$z].")";
3540                                                $array_emails[$contact['mail_forwarding_address'][$z]] = "<font color=black>".$contact['mail_forwarding_address'][$z]."</font>";
3541                                        }
3542                                        else
3543                                                $array_participants[$z] = "<font color=red>".$contact['mail_forwarding_address'][$z]."</font>";
3544                        }
3545
3546                        if($filter) {
3547                                $filter = "(|".$filter.")";
3548                                if(!$this->bo->catalog->src_info) {
3549                                        $ldap = CreateObject('contactcenter.bo_ldap_manager');
3550                                        $this->bo->catalog->src_info = $ldap->srcs[1];
3551                                }
3552                                $ds = $GLOBALS['phpgw']->common->ldapConnect($this->bo->catalog->src_info['host'], $this->bo->catalog->src_info['acc'], $this->bo->catalog->src_info['pw'], true);
3553                                $dn=$this->bo->catalog->src_info['dn'];
3554                                $justThese = array("cn","mail");
3555                                $sr = ldap_search($ds,$dn, $filter,$justThese);
3556                                $info = ldap_get_entries($ds, $sr);
3557                                for($z = 0; $z < $info['count']; $z++) {
3558                                        $participant =  '<font color=\'DARKBLUE\'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;'.$info[$z]['cn'][0].'&quot; &lt;'.$info[$z]['mail'][0].'&gt;</font><br>';
3559                                        $array_emails[$info[$z]['mail'][0]] = null;
3560                                        array_push($array_participants, $participant);
3561                                }
3562
3563                                foreach($array_emails as $index => $email)
3564                                        if($email)
3565                                                array_push($array_participants, "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$email."<br>");
3566
3567                                ldap_close($ds);
3568                        }
3569                        sort($array_participants);
3570                        $innerHTML = '';
3571                        foreach($array_participants as $index => $participant){
3572                                $innerHTML .= $participant;
3573                        }
3574                        $return = array('size' => count($array_participants), 'names_ordered'=> $contact['names_ordered'], 'inner_html' => $innerHTML);
3575                        echo serialize($return);
3576                }
3577
3578                function export_contacts($typeExport){
3579
3580                        $boGroup = CreateObject('contactcenter.bo_group');
3581                        $contacts = $boGroup->get_all_contacts();
3582                        $streamBuffer = '';
3583
3584                        if(!count($contacts))
3585                                echo null;
3586
3587                        switch($typeExport) {
3588
3589                                case 'outlook_en':
3590                                        $streamBuffer = "Name;E-mail Address;Notes;Mobile Phone;Pager;Company;".
3591                                                        "Job Title;Home Phone;Home Fax;Business Phone;Business Fax\r\n";                                                       
3592                                        foreach($contacts as $index => $object){
3593                                                $streamBuffer.= "\"".$object[ 'names_ordered'] . "\";"
3594                                                        . "\"".$object[ 'main-mail' ] . "\";"
3595                                                        . "\"".str_replace("\r\n\x0a","\t",$object[ 'notes' ]) . "\";"
3596                                                        . "\"".$object[ 'mobile' ] . "\";"
3597                                                        . "\"".$object[ 'business-pager' ] . "\";"
3598                                                        . "\"".$object[ 'corporate_name' ] . "\";"
3599                                                        . "\"".$object[ 'job_title' ] . "\";"
3600                                                        . "\"".$object[ 'home-phone' ] . "\";"
3601                                                        . "\"".$object[ 'home-fax' ] . "\";"
3602                                                        . "\"".$object[ 'business-phone' ] . "\";"
3603                                                        . "\"".$object[ 'business-fax' ] . "\"\r\n";
3604                                        }
3605                                        break;
3606
3607                                case 'outlook_pt-BR':
3608                                        $streamBuffer = "Nome;Sobrenome;Segundo nome;Nome;Apelido;End. de email;Endereço residencial;"
3609                                                                        ."Cidade do endereço residencial;CEP do endereço residencial;Estado;País/região do endereço residencial;"
3610                                                                        ."Telefone residencial;Fax residencial;Telefone celular;Página pessoal da Web;Rua do endereço comercial;"
3611                                                                        ."Cidade do endereço comercial;CEP do endereço comercial;Estado do endereço comercial;"
3612                                                                        ."País/região do endereço comercial;Página comercial da Web;Telefone comercial;Fax comercial;Pager;Empresa;"
3613                                                                        ."Cargo;Departamento;End. comercial;Observações\r\n";
3614
3615                                        foreach($contacts as $index => $object){
3616                                                $streamBuffer.= "\"".$object[ 'names_ordered'] . "\";"
3617                                                        .";;;"
3618                                                        . "\"".$object[ 'alias' ] . "\";"
3619                                                        . "\"".$object[ 'main-mail' ] . "\";"
3620                                                        . "\"".$object[ 'home-address' ] . "\";"
3621                                                        . "\"".$object[ 'home-city_name' ] . "\";"
3622                                                        . "\"".$object[ 'home-postal_code' ] . "\";"
3623                                                        . "\"".$object[ 'home-state_name' ] . "\";"
3624                                                        . "\"".$object[ 'home-id_country' ] . "\";"
3625                                                        . "\"".$object[ 'home-phone' ] . "\";"
3626                                                        . "\"".$object[ 'home-fax' ] . "\";"
3627                                                        . "\"".$object[ 'mobile' ] . "\";"
3628                                                        . "\"".$object[ 'web_page' ] . "\";"
3629                                                        . "\"".$object[ 'business-address' ] . "\";"
3630                                                        . "\"".$object[ 'business-city_name' ] . "\";"
3631                                                        . "\"".$object[ 'business-postal_code' ] . "\";"
3632                                                        . "\"".$object[ 'business-state_name' ] . "\";"
3633                                                        . "\"".$object[ 'business-id_country' ] . "\";"
3634                                                        . "\"".$object[ 'web_page' ] . "\";"
3635                                                        . "\"".$object[ 'business-phone' ] . "\";"
3636                                                        . "\"".$object[ 'business-fax' ] . "\";"
3637                                                        . "\"".$object[ 'business-pager' ] . "\";"
3638                                                        . "\"".$object[ 'corporate_name' ] . "\";"
3639                                                        . "\"".$object[ 'job_title' ] . "\";"
3640                                                        . "\"".$object[ 'department' ] . "\";"
3641                                                        .";"
3642                                                        . "\"".str_replace("\r\n\x0a","\t",$object[ 'notes' ]) . "\"\r\n";
3643                                        }
3644
3645                                break;
3646
3647                                case 'outlook2000_pt-BR':
3648                                        $streamBuffer = "\"Tratamento\",\"Nome\",\"Segundo Nome\",\"Sobrenome\",\"Sufixo\",".
3649                                        "\"Empresa\",\"Departamento\",\"Cargo\",\"Rua do endereço comercial\",\"Rua do endereço comercial 2\",".
3650                                        "\"Rua do endereço comercial 3\",\"Cidade do endereço comercial\",\"Estado do endereço comercial\",".
3651                                        "\"CEP do endereço comercial\",\"País do endereço comercial\",\"Endereço residencial\",\"Rua residencial 2\",".
3652                                        "\"Rua residencial 3\",\"Cidade do endereço residencial\",\"Estado\",\"CEP do endereço residencial\",\"País do endereço residencial\",".
3653                                        "\"Outro endereço\",\"Outro endereço 2\",\"Outro endereço 3\",\"Cidade\",\"Estado\",\"CEP\",\"País\",".
3654                                        "\"Telefone do assistente\",\"Fax comercial\",\"Telefone comercial\",\"Telefone comercial 2\",\"Retorno de chamada\",".
3655                                        "\"Telefone do carro\",\"Telefone principal da empresa\",\"Fax residencial\",\"Telefone residencial\",".
3656                                        "\"Telefone residencial 2\",\"ISDN\",\"Telefone celular\",\"Outro fax\",\"Outro telefone\",\"Pager\",\"Telefone principal\",".
3657                                        "\"Radiofone\",\"Telefone TTY/TDD\",\"Telex\",\"Aniversário\",\"Anotações\",\"Caixa postal\",\"Categorias\",".
3658                                        "\"Código da empresa\",\"Código do governo\",\"Cônjuge\",\"Conta\",\"Endereço de correio eletrônico\",".
3659                                        "\"Nome de exibição do correio eletr.\",\"Endereço de correio eletrônico 2\",".
3660                                        "\"Nome de exibição do correio eletr.2\",\"Endereço de correio eletrônico 3\",".
3661                                        "\"Nome de exibição do correio eletr.3\",\"Datas especiais\",\"Disponibilidade da Internet\",".
3662                                        "\"Filhos\",\"Hobby\",\"Idioma\",\"Indicação\",\"Informações para cobrança\",\"Iniciais\",\"Local\",".
3663                                        "\"Nome do assistente\",\"Nome do gerenciador\",\"Página da Web\",\"Palavras-chave\",\"Particular\",\"Personalizado 1\",\"Personalizado 2\",".
3664                                        "\"Personalizado 3\",\"Personalizado 4\",\"Prioridade\",\"Profissão\",\"Quilometragem\",\"Sala\",\"Sensibilidade\",\"Servidor de diretório\",".
3665                                        "\"Sexo\"\r\n";
3666
3667
3668
3669                                        foreach($contacts as $index => $object){
3670                                $streamBuffer .= "\"".$object[ 'alias' ] . "\","                                   
3671                                                                . "\"".$object[ 'names_ordered' ] . "\","
3672                                                                .",,,"
3673                                                                . "\"".$object[ 'corporate_name' ] . "\","
3674                                                                . "\"".$object[ 'department' ] . "\","
3675                                                                . "\"".$object[ 'job_title' ] . "\","
3676                                                                . "\"".$object[ 'business-address' ] . "\","
3677                                                                . "\"".$object[ 'business-address-2' ] . "\","
3678                                                                .","
3679                                                                . "\"".$object[ 'business-city_name' ] . "\","
3680                                                                . "\"".$object[ 'business-state' ] . "\","
3681                                                                . "\"".$object[ 'business-postal_code' ] . "\","
3682                                                                . "\"".$object[ 'business-id_country' ] . "\","
3683                                                                . "\"".$object[ 'home-address' ] . "\","
3684                                                                . "\"".$object[ 'home-address-2' ] . "\","
3685                                                                .","
3686                                                                . "\"".$object[ 'home-city_name' ] . "\","
3687                                                                . "\"".$object[ 'home-state_name' ] . "\","
3688                                                                . "\"".$object[ 'home-postal_code' ] . "\","
3689                                                                . "\"".$object[ 'home-id_country' ] . "\","
3690                                                                .",,,,,,,,"
3691                                                                . "\"".$object[ 'business-fax' ] . "\","
3692                                                                . "\"".$object[ 'business-phone' ] . "\","
3693                                                                . "\"".$object[ 'business-mobile' ] . "\","
3694                                                                .",,,"
3695                                                                . "\"".$object[ 'home-fax' ] . "\","
3696                                                                . "\"".$object[ 'home-phone' ] . "\","
3697                                                                .",,"
3698                                                                . "\"".$object[ 'mobile' ] . "\","
3699                                                                .",,"
3700                                                                . "\"".$object[ 'home-pager' ] . "\","
3701                                                                . "\"".$object[ 'business-phone' ] . "\","
3702                                                                .",,,"
3703                                                                . "\"".$object[ 'birthdate' ] . "\","
3704                                                                . "\"".str_replace("\r\n\x0a","\t",$object[ 'notes' ]) . "\","
3705                                                                .",,,,,,"
3706                                                                . "\"".$object[ 'main-mail' ] . "\","
3707                                                                .","
3708                                                                . "\"".$object[ 'alternative-mail' ] . "\","
3709                                                                .",,,,,,,,,,,,,,"
3710                                                                . "\"".$object[ 'web_page' ] . "\","
3711                                                                .",,,,,,,,,,,,"
3712                                                                . "\"".$object[ 'sex' ] . "\"\r\n";                                                             
3713                            }
3714                                break;
3715
3716                                case 'outlook2000_en':
3717                                        $streamBuffer = "Title,First Name,Middle Name,Last Name,Suffix,Company,Department,Job Title,".
3718                                        "Business Street,Business Street 2,Business Street 3,Business City,Business State,Business Postal Code,".
3719                                        "Business Country,Home Street,Home Street 2,Home Street 3,Home City,Home State,Home Postal Code,Home Country,".
3720                                        "Other Street,Other Street 2,Other Street 3,Other City,Other State,Other Postal Code,Other Country,".
3721                                        "Assistant's Phone,Business Fax,Business Phone,Business Phone 2,Callback,Car Phone,Company Main Phone,Home Fax,".
3722                                        "Home Phone,Home Phone 2,ISDN,Mobile Phone,Other Fax,Other Phone,Pager,Primary Phone,Radio Phone,TTY/TDD Phone,Telex,".
3723                                        "Account,Anniversary,Assistant's Name,Billing Information,Birthday,Categories,Children,Directory Server,E-mail Address,".
3724                                        "E-mail Type,E-mail Display Name,E-mail 2 Address,E-mail 2 Type,E-mail 2 Display Name,E-mail 3 Address,E-mail 3 Type,E-mail 3 Display Name,".
3725                                        "Gender,Government ID Number,Hobby,Initials,Internet Free Busy,Keywords,Language,Location,Manager's Name,Mileage,Notes,".
3726                                        "Office Location,Organizational ID Number,PO Box,Priority,Private,Profession,Referred By,Sensitivity,Spouse,User 1,User 2,User 3,User 4,Web Page\r\n";
3727
3728                                        foreach($contacts as $index => $object){
3729                                                $streamBuffer.= "\"".$object[ 'alias' ] . "\","
3730                                                        . "\"".$object[ 'names_ordered'] . "\","
3731                                                        .",,,"
3732                                                        . "\"".$object[ 'corporate_name' ] . "\","
3733                                                        . "\"".$object[ 'department' ] . "\","
3734                                                        . "\"".$object[ 'job_title' ] . "\","
3735                                                        . "\"".$object[ 'business-address' ] . "\","
3736                                                        . "\"".$object[ 'business-address-2' ] . "\","
3737                                                        .","
3738                                                        . "\"".$object[ 'business-city_name' ] . "\","
3739                                                        . "\"".$object[ 'business-state' ] . "\","
3740                                                        . "\"".$object[ 'business-postal_code' ] . "\","
3741                                                        . "\"".$object[ 'business-id_country' ] . "\","
3742                                                        . "\"".$object[ 'home-address' ] . "\","
3743                                                        . "\"".$object[ 'home-address-2' ] . "\","
3744                                                        .","
3745                                                        . "\"".$object[ 'home-city_name' ] . "\","
3746                                                        . "\"".$object[ 'home-state_name' ] . "\","
3747                                                        . "\"".$object[ 'home-postal_code' ] . "\","
3748                                                        . "\"".$object[ 'home-id_country' ] . "\","
3749                                                        .",,,,,,,,"
3750                                                        . "\"".$object[ 'business-fax' ] . "\","
3751                                                        . "\"".$object[ 'business-phone' ] . "\","
3752                                                        . "\"".$object[ 'business-mobile' ] . "\","
3753                                                        .",,,"
3754                                                        . "\"".$object[ 'home-fax' ] . "\","
3755                                                        . "\"".$object[ 'home-phone' ] . "\","
3756                                                        .",,"
3757                                                        . "\"".$object[ 'mobile' ] . "\","
3758                                                        .",,"
3759                                                        . "\"".$object[ 'business-pager' ] . "\","
3760                                                        . "\"".$object[ 'home-pager' ] . "\","
3761                                                        .",,,,,,,,"
3762                                                        . "\"".$object[ 'birthdate' ] . "\","
3763                                                        .",,,"
3764                                                        . "\"".$object[ 'main-mail' ] . "\","
3765                                                        .",,"
3766                                                        . "\"".$object[ 'alternative-mail' ] . "\","
3767                                                        .",,,,,,,,,,,,,,,"
3768                                                        . "\"".str_replace("\r\n\x0a","\t",$object[ 'notes' ]) . "\","
3769                                                        .",,,,,,,,,,,,,"
3770                                                        . "\"".$object[ 'web_page' ] . "\"\r\n";
3771
3772                                        }
3773                                break;
3774
3775                                case 'thunderbird':
3776                                        $streamBuffer = "First Name,Last Name,Display Name,Nickname,Primary Email,Secondary Email,"
3777                                                ."Screen Name,Work Phone,Home Phone,Fax Number,Pager Number,Mobile Number,Home Address,"
3778                                                ."Home Address 2,Home City,Home State,Home ZipCode,Home Country,Work Address,Work Address 2,"
3779                                                ."Work City,Work State,Work ZipCode,Work Country,Job Title,Department,Organization,Web Page 1,"
3780                                                ."Web Page 2,Birth Year,Birth Month,Birth Day,Custom 1,Custom 2,Custom 3,Custom 4,Notes,\n";
3781
3782                                        foreach($contacts as $index => $object){
3783                                                $array_birth = explode("-",$object[ 'birthdate' ]);
3784                                                $streamBuffer.= "\"".$object[ 'names_ordered'] . "\","
3785                                                        .",,"
3786                                                        . "\"".$object[ 'alias' ] . "\","
3787                                                        . "\"".$object[ 'main-mail' ] . "\","
3788                                                        . "\"".$object[ 'alternative-mail' ] . "\","
3789                                                        .","
3790                                                        . "\"".$object[ 'business-phone' ] . "\","
3791                                                        . "\"".$object[ 'home-phone' ] . "\","
3792                                                        . "\"".$object[ 'business-fax' ] . "\","
3793                                                        . "\"".$object[ 'business-pager' ] . "\","
3794                                                        . "\"".$object[ 'mobile' ] . "\","
3795                                                        . "\"".$object[ 'home-address' ] . "\","
3796                                                        . "\"".$object[ 'home-address-2' ] . "\","
3797                                                        . "\"".$object[ 'home-city_name' ] . "\","
3798                                                        . "\"".$object[ 'home-state_name' ] . "\","
3799                                                        . "\"".$object[ 'home-postal_code' ] . "\","
3800                                                        . "\"".$object[ 'home-id_country' ] . "\","
3801                                                        . "\"".$object[ 'business-address' ] . "\","
3802                                                        . "\"".$object[ 'business-address-2' ] . "\","
3803                                                        . "\"".$object[ 'business-city_name' ] . "\","
3804                                                        . "\"".$object[ 'business-state_name' ] . "\","
3805                                                        . "\"".$object[ 'business-postal_code' ] . "\","
3806                                                        . "\"".$object[ 'business-id_country' ] . "\","
3807                                                        . "\"".$object[ 'job_title' ] . "\","
3808                                                        . "\"".$object[ 'department' ] . "\","
3809                                                        . "\"".$object[ 'corporate_name' ] . "\","
3810                                                        . "\"".$object[ 'web_page' ] . "\","
3811                                                        . "\"".$object[ 'web_page' ] . "\","
3812                                                        . "\"".$array_birth[0] . "\","
3813                                                        . "\"".$array_birth[1] . "\","
3814                                                        . "\"".$array_birth[2] . "\","
3815                                                        .",,,,"
3816                                                        . "\"".str_replace("\r\n\x0a","\t",$object[ 'notes' ]) . "\",\r\n";
3817
3818                                        }
3819                                break;
3820
3821                                case 'expresso':
3822                            $streamBuffer = 'Nome,Apelido,E-mail Principal,E-mail Alternativo,Celular,'
3823                                . 'Telefone Comercial,Endereço Comercial,Complemento End. Comercial,CEP Comercial,Cidade End. Comercial,Estado End. Comercial,País End. Comercial,'
3824                                . 'Telefone Residencial,Endereço Residencial,Complemento End. Residencial,CEP Residencial,Cidade End. Residencial,Estado End. Residencial,País End. Residencial,'
3825                                . 'Aniversário,Sexo,Assinatura GPG,Notas,Página Web,Empresa,Cargo,Departamento,Fax Comercial,Pager Comercial,Celular Comercial,Fax,Pager,Endereço Comercial 2,Endereço Residencial 2'
3826                                                        . "\r\n";
3827
3828                            foreach($contacts as $index => $object){
3829                                $streamBuffer .= "\"".$object[ 'names_ordered'] . "\","
3830                                    . "\"".$object[ 'alias' ] . "\","
3831                                    . "\"".$object[ 'main-mail' ] . "\","
3832                                    . "\"".$object[ 'alternative-mail' ] . "\","
3833                                    . "\"".$object[ 'mobile' ] . "\","
3834                                    . "\"".$object[ 'business-phone' ] . "\","
3835                                    . "\"".$object[ 'business-address' ] . "\","
3836                                    . "\"".$object[ 'business-complement' ] . "\","
3837                                    . "\"".$object[ 'business-postal_code' ] . "\","
3838                                    . "\"".$object[ 'business-city_name' ] . "\","
3839                                    . "\"".$object[ 'business-state_name' ] . "\","
3840                                    . "\"".$object[ 'business-id_country' ] . "\","
3841                                    . "\"".$object[ 'home-phone' ] . "\","
3842                                    . "\"".$object[ 'home-address' ] . "\","
3843                                    . "\"".$object[ 'home-complement' ] . "\","
3844                                    . "\"".$object[ 'home-postal_code' ] . "\","
3845                                    . "\"".$object[ 'home-city_name' ] . "\","
3846                                    . "\"".$object[ 'home-state_name' ] . "\","
3847                                    . "\"".$object[ 'home-id_country' ] . "\","
3848                                                                . "\"".$object[ 'birthdate' ] . "\","
3849                                                                . "\"".$object[ 'sex' ] . "\","
3850                                                                . "\"".$object[ 'pgp_key' ] . "\","
3851                                                                . "\"".str_replace("\r\n\x0a","\t",$object[ 'notes' ]) . "\","
3852                                                                . "\"".$object[ 'web_page' ] . "\","
3853                                                                . "\"".$object[ 'corporate_name' ] . "\","
3854                                                                . "\"".$object[ 'job_title' ] . "\","
3855                                                                . "\"".$object[ 'department' ] . "\","
3856                                                                . "\"".$object[ 'business-fax' ] . "\","
3857                                                                . "\"".$object[ 'business-pager' ] . "\","
3858                                                                . "\"".$object[ 'business-mobile' ] . "\","
3859                                                                . "\"".$object[ 'home-fax' ] . "\","
3860                                                                . "\"".$object[ 'home-pager' ] . "\","
3861                                                                . "\"".$object[ 'business-address-2' ] . "\","
3862                                                                . "\"".$object[ 'home-address-2' ] . "\"\r\n";
3863                            }
3864                        break;
3865
3866                        }
3867
3868                        $file = "contacts_".md5(microtime()).".swp";
3869                        $tempDir = ini_get("session.save_path");
3870                        $f = fopen($tempDir.'/'.$file,"w");
3871                        if(!$f)
3872                                echo null;
3873
3874                        fputs($f,$streamBuffer);
3875                        fclose($f);
3876
3877                        echo $tempDir.'/'.$file;
3878                }
3879
3880                // Get the csv field and put into array, from php.net
3881                function parse_line($input_text, $delimiter = ',', $text_qualifier = '"') {
3882                        $text = trim($input_text);
3883                          if(is_string($delimiter) && is_string($text_qualifier)) {
3884                         $re_d = '\x' . dechex(ord($delimiter));            //format for regexp
3885                        $re_tq = '\x' . dechex(ord($text_qualifier));    //format for regexp
3886
3887                        $fields = array();
3888                        $field_num = 0;
3889                        while(strlen($text) > 0) {
3890                        if($text{0} == $text_qualifier) {
3891                                preg_match('/^' . $re_tq . '((?:[^' . $re_tq . ']|(?<=\x5c)' . $re_tq . ')*)' . $re_tq . $re_d . '?(.*)$/', $text, $matches);
3892
3893                                $value = str_replace('\\' . $text_qualifier, $text_qualifier, $matches[1]);
3894                                $text = trim($matches[2]);
3895
3896                                $fields[$field_num++] = $value;
3897                        } else {
3898                                preg_match('/^([^' . $re_d . ']*)' . $re_d . '?(.*)$/', $text, $matches);
3899
3900                                $value = $matches[1];
3901                                $text = trim($matches[2]);
3902
3903                                $fields[$field_num++] = $value;
3904                }
3905                }
3906                        return $fields;
3907                    } else
3908                        return false;
3909                }
3910
3911                //funcao alterada para importar outros campos alem de nome, telefone e email, de arquivo csv (Outlook 2000)
3912                //em 08/04/2009 - Rommel Cysne (rommel.cysne@serpro.gov.br);
3913                //Foi adicionada uma funcao (escapesheelcmd()) nas variaveis para que caracteres especiais sejam ignorados
3914                //durante a importacao dos contatos; o processo estava travando por causa de caracteres em campos como nome,
3915                //sobrenome, notas e e-mail;
3916                //em 19/06/2009 - Rommel Cysne (rommel.cysne@serpro.gov.br);
3917                function import_contacts($typeImport){
3918                        $this->so_group = CreateObject('contactcenter.so_group');
3919                        if($file = $_SESSION['contactcenter']['importCSV']) {
3920                                unset($_SESSION['contactcenter']['importCSV']);
3921                                $len = filesize($file);
3922                                $count = 0;
3923                                $return = array('error' => false, '_new' => 0, '_existing' => 0, '_failure' => 0);
3924                                $handle = @fopen($file, "r") or die(serialize($return['error'] = true));
3925
3926                                $input_header = fgets($handle);
3927                                if ($typeImport == 'outlook')
3928                                        $delim = ';';
3929                                else if ($typeImport == 'auto')
3930                                $delim = strstr($input_header,',') ? ',' : ';';
3931                                else
3932                                        $delim = ',';
3933                                $csv_header = $this->parse_line($input_header,$delim);
3934                                $firstContact = fgets($handle);
3935                                preg_match("/\"(.+)\"[,;]/sU",$firstContact,$matches); // yahoo csv
3936                                rewind($handle);
3937
3938                                $header = @fgetcsv($handle, $len, $delim) or die(serialize($return['error'] = true));
3939                                if(count($header)  < 2 || count($header) > 100) {
3940                                        $return['error'] = true;
3941                                        $return['sizeheader'] = count($header);
3942                                        echo serialize($return);
3943                                        return;
3944                                }
3945
3946                                if ($matches[0][strlen($matches[0])-1] == ';')
3947                                        $delim = ';';
3948
3949                                $boGroup = CreateObject('contactcenter.bo_group');
3950                                $boPeople = CreateObject('contactcenter.bo_people_catalog');
3951                                        switch($typeImport){
3952                                                case 'outlook2000':
3953                                                        $name_pos=1;
3954                                                        $name2_pos=2;
3955                                                        $name3_pos=3;
3956                                                        $corporate_street_pos=8;
3957                                                        $cep_pos=13;
3958                                                        $corporate_street_2_pos=22;
3959                                                        $fax_pos=30;
3960                                                        $phone_pos=31;
3961                                                        $home_phone_pos=37;
3962                                                        $personal_cell_pos=40;
3963                                                        $pager_pos=43;
3964                                                        $birth_pos=48;
3965                                                        $notes_pos=49;
3966                                                        $email_pos=56;
3967                                                        $aditionalEmail_pos=59;
3968
3969                                                        break;
3970                                                case 'outlook':
3971                                                        $name_pos=3;
3972                                                        $email_pos=4;
3973                                                        $phone_pos=7;
3974                                                        $home_phone_pos=10;
3975                                                        $personal_cell_pos=12;
3976                                                        $corporate_street_pos=13;
3977                                                        $cep_pos=15;
3978                                                        $phone_pos=18;
3979                                                        $fax_pos=19;
3980                                                        $pager_pos=20;
3981                                                        $notes_pos=25;
3982                                                        break;
3983                                                case 'thunderbird':
3984                                                        $name_pos=2;
3985                                                        $email_pos=4;
3986                                                        $phone_pos=7;
3987                                                        break;
3988                                                case 'expresso':
3989                                                        $name_pos=0;
3990                                                        $alias_pos=1;
3991                                                        $email_pos=2;
3992                                                        $aditionalEmail_pos=3;
3993                                                        $personal_cell_pos=4;
3994                                                        $corporate_phone_pos=5;
3995                                                        $corporate_street_pos=6;
3996                                                        $corporate_comp_pos=7;
3997                                                        $corporate_cep_pos=8;
3998                                                        $corporate_city_pos=9;
3999                                                        $corporate_state_pos=10;
4000                                                        $corporate_country_pos=11;
4001                                                        $home_phone_pos=12;
4002                                                        $street_pos=13;
4003                                                        $comp_pos=14;
4004                                                        $cep_pos=15;
4005                                                        $city_pos=16;
4006                                                        $state_pos=17;
4007                                                        $country_pos=18;
4008                                                        $birth_pos=19;
4009                                                        $sex_pos = 20;
4010                                                        $pgp_key_pos = 21;
4011                                                        $notes_pos=22;
4012                                                        $web_page_pos=23;
4013                                                        $corporate_name_pos=24;
4014                                                        $job_title_pos=25;
4015                                                        $department_pos=26;
4016                                                        $corporate_fax_pos=27;
4017                                                        $corporate_pager_pos=28;
4018                                                        $corporate_cell_pos=29;
4019                                                        $fax_pos=30;
4020                                                        $pager_pos=31;
4021                                                        $corporate_street_2_pos = 32;
4022                                                        $street_2_pos = 33;                                             
4023                                                        break;
4024                                        default:
4025                                                        foreach($csv_header as $index => $fieldName)
4026                                                        {
4027                                                                switch($fieldName){
4028                                                                case 'Name':
4029                                                                case 'Nome':
4030                                                                case 'First Name':
4031                                                                        $name_pos = $index;
4032                                                                        break;
4033                                                                case 'Second name':
4034                                                                case 'Segundo nome':
4035                                                                        $name2_pos = $index;
4036                                                                        break;
4037                                                                case 'Sobrenome':
4038                                                                case 'Surname':
4039                                                                        $name3_pos = $index;
4040                                                                        break;
4041                                                                case 'Business Street':
4042                                                                case 'Rua do endereço comercial':
4043                                                                        $corporate_street_pos = $index;
4044                                                                        break;
4045                                                                case 'Rua do endereço comercial 2':
4046                                                                case 'Outro endereço':
4047                                                                        $corporate_street_2_pos = $index;
4048                                                                        break;
4049                                                                case 'Business Postal Code':
4050                                                                case 'CEP do endereço comercial':
4051                                                                        $cep_pos = $index;
4052                                                                        break;
4053                                                                case 'Business Fax':
4054                                                                case 'Fax comercial':
4055                                                                case 'Fax':
4056                                                                        $fax_pos = $index;
4057                                                                        break;
4058                                                                case 'Home Phone':
4059                                                                case 'Telefone residencial':
4060                                                                        $home_phone_pos = $index;
4061                                                                        break;
4062                                                                case 'Mobile phone':
4063                                                                case 'Telefone celular':
4064                                                                        $personal_cell_pos = $index;
4065                                                                        break;
4066                                                                case 'Pager':
4067                                                                        $pager_pos = $index;
4068                                                                        break;
4069                                                                case 'Phone':
4070                                                                case 'Business Phone':
4071                                                                case 'Telefone':
4072                                                                case 'Telefone principal':
4073                                                                case 'Telefone comercial':
4074                                                                        $phone_pos = $index;
4075                                                                        break;
4076                                                                case 'Aniversário':
4077                                                                case 'Birthdate':
4078                                                                        $birth_pos = $index;
4079                                                                case 'Anotações':
4080                                                                case 'Notes':
4081                                                                        $notes_pos = $index;
4082                                                                case 'E-mail':
4083                                                                case 'Email':
4084                                                                case 'E-mail Address':
4085                                                                case 'Endereço de correio eletrônico':
4086                                                                case 'End. de email':
4087                                                                        $email_pos = $index;
4088                                                                        break;
4089                                                                case 'Endereço de correio eletrônico 2':
4090                                                                        $aditionalEmail_pos = $index;
4091                                                                        break;
4092                                                                }
4093                                                        }
4094                                                        break;
4095                                }
4096
4097                                while (($data = fgetcsv($handle, $len, $delim))) {
4098                                        foreach ($header as $key=>$heading)
4099                                $row[$heading]=(isset($data[$key])) ? $data[$key] : '';
4100
4101                                                $sdata = array();
4102                                                $full_name  = trim($row[$header[$name_pos]]);
4103                                                $email          = trim($row[$header[$email_pos]]);
4104                                                $phone          = trim($row[$header[$phone_pos]]);
4105                                                $name2          = trim($row[$header[$name2_pos]]);
4106                                                $name3          = trim($row[$header[$name3_pos]]);
4107
4108                                                $birth          = trim($row[$header[$birth_pos]]);
4109                                                $notes          = trim($row[$header[$notes_pos]]);
4110                                                $altEmail       = trim($row[$header[$altEmail_pos]]);
4111                                                $sdata['alias']                         = trim($row[$header[$alias_pos]]);
4112                                                $sdata['corporate_name']      = trim($row[$header[$corporate_name_pos]]);
4113                                                $sdata['job_title']             = trim($row[$header[$job_title_pos]]);
4114                                                $sdata['department']            = trim($row[$header[$department_pos]]);
4115                                                $sdata['web_page']                      = trim($row[$header[$web_page_pos]]);
4116                                               
4117                                                $sdata['sex']                   = trim($row[$header[$sex_pos]]);
4118                                                $sdata['pgp_key']      = trim($row[$header[$pgp_key_pos]]);
4119
4120
4121                                        $full_name = $full_name;
4122                                        $array_name = explode(' ', str_replace('"','',(str_replace('\'','',$full_name))));
4123                                        $sdata['given_names'] = addslashes($array_name[0]);
4124                                        $array_name[0] = null;
4125                                        $sdata['family_names'] = trim(implode(' ',$array_name));
4126                                        if($sdata['family_names'] == '')
4127                                        {
4128                                                $sdata['family_names'] = addslashes($name2) . " " . addslashes($name3);
4129                                        }
4130
4131                                                $sdata['connections']['default_email']['connection_name'] = lang('Main');
4132                                                $sdata['connections']['default_email']['connection_value'] = addslashes($email);
4133                                                $sdata['connections']['aditional_email']['connection_name'] = "Alternativo";
4134                                                $sdata['connections']['aditional_email']['connection_value'] = trim($row[$header[$aditionalEmail_pos]]);
4135
4136                                                $sdata['connections']['default_phone']['connection_name'] = lang('Main');
4137                                        $sdata['connections']['default_phone']['connection_value'] = $phone;
4138
4139                                                $sdata['connections']['aditional_phone']['cellphone']['connection_name'] = 'Celular';
4140                                                $sdata['connections']['aditional_phone']['cellphone']['connection_value'] = trim($row[$header[$personal_cell_pos]]);
4141                                               
4142                                                $sdata['connections']['aditional_phone']['corporate_phone']['connection_name'] = 'Trabalho';
4143                                                $sdata['connections']['aditional_phone']['corporate_phone']['connection_value'] = trim($row[$header[$corporate_phone_pos]]);
4144                                               
4145                                                $sdata['connections']['aditional_phone']['fax']['connection_name'] = 'Fax';
4146                                                $sdata['connections']['aditional_phone']['fax']['connection_value'] = trim($row[$header[$fax_pos]]);
4147                                               
4148                                                $sdata['connections']['aditional_phone']['corporate_fax']['connection_name'] = 'Fax Corporativo';
4149                                                $sdata['connections']['aditional_phone']['corporate_fax']['connection_value'] = trim($row[$header[$corporate_fax_pos]]);
4150                                                                                               
4151                                                $sdata['connections']['aditional_phone']['corporate_cell']['connection_name'] = 'Celular Corporativo';
4152                                                $sdata['connections']['aditional_phone']['corporate_cell']['connection_value'] = trim($row[$header[$corporate_cell_pos]]);
4153                                               
4154                                                $sdata['connections']['aditional_phone']['corporate_pager']['connection_name'] = 'Pager Corporativo';
4155                                                $sdata['connections']['aditional_phone']['corporate_pager']['connection_value'] = trim($row[$header[$corporate_pager_pos]]);
4156                                                                                                                               
4157                                               
4158                                                $sdata['connections']['aditional_phone']['home_phone']['connection_name'] = 'Casa';
4159                                                $sdata['connections']['aditional_phone']['home_phone']['connection_value'] = trim($row[$header[$home_phone_pos]]);
4160                                               
4161                                                $sdata['connections']['aditional_phone']['pager']['connection_name'] = 'Pager';
4162                                                $sdata['connections']['aditional_phone']['pager']['connection_value'] = trim($row[$header[$pager_pos]]);
4163
4164                                                $sdata['addresses']['address_corporative']['address1'] = trim($row[$header[$corporate_street_pos]]);
4165                                                $sdata['addresses']['address_corporative']['address2'] = trim($row[$header[$corporate_street_2_pos]]);
4166                                                $sdata['addresses']['address_corporative']['complement'] = trim($row[$header[$corporate_comp_pos]]);
4167                                                $sdata['addresses']['address_corporative']['postal_code'] = trim($row[$header[$corporate_cep_pos]]);                                           
4168                                                $sdata['addresses']['address_corporative']['id_country'] = "BR";
4169                                                $sdata['addresses']['address_corporative']['id_state'] = trim($row[$header[$corporate_state_pos]]);     
4170                                                $sdata['addresses']['address_corporative']['id_city'] = trim($row[$header[$corporate_city_pos]]);
4171                                               
4172                                                                                                       
4173                                                $sdata['addresses']['address_personal']['address1'] = trim($row[$header[$street_pos]]);
4174                                                $sdata['addresses']['address_personal']['address2'] = trim($row[$header[$street_2_pos]]);
4175                                                $sdata['addresses']['address_personal']['complement'] = trim($row[$header[$comp_pos]]);
4176                                                $sdata['addresses']['address_personal']['postal_code'] = trim($row[$header[$cep_pos]]);
4177                                                $sdata['addresses']['address_personal']['id_country'] = "BR";                                                   
4178                                                $sdata['addresses']['address_personal']['id_state'] = trim($row[$header[$state_pos]]); 
4179                                                $sdata['addresses']['address_personal']['id_city'] =  trim($row[$header[$city_pos]]);
4180
4181                                        $array_birth = explode("/",$birth);
4182
4183                                        $sdata['birthdate'] = date('Y-m-d', mktime(0,0,0,$array_birth[1],$array_birth[0],$array_birth[2]));
4184
4185                                        $sdata['notes'] = addslashes($notes);
4186                                        //$sdata['is_quick_add'] = true;
4187                                        $sdata['connections']['default_phone']['connection_value'] = $phone;
4188
4189                                        //      verifica se email já existe!
4190                                        $email = addslashes($email);
4191                                        $contact = $boGroup->verify_contact($email);
4192
4193                                        if(!$sdata['given_names'] && $email){
4194                                                        $a_email = explode("@",$email);
4195                                                        $sdata['given_names'] = addslashes($a_email[0]);
4196                                        }
4197
4198                                        $line_iteration = $return['_failure'] + $return['_existing'] + $return['_new'];
4199
4200                                        if($contact){
4201                                                $return['_existing']++;
4202                                        }
4203                                        else if((!eregi("^[/_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) && $email) {
4204                                                $return['_failure']++;
4205                                                $return['_failure_status'] .= "Line: " . ($line_iteration + 2) . ", Invalid E-Mail address: " . $email ."<br>";
4206                                        }
4207                                        else if (!$sdata['given_names'] || !$boPeople ->quick_add($sdata)){
4208                                                $return['_failure']++;
4209                                                $return['_failure_status'] .= "Line: " . ($line_iteration + 2) . ", Invalid Name: " . $sdata['given_names'] ."<br>";
4210                                        }
4211                                        else{   
4212                                                        if($id_group != 0){                                                             
4213                                                                $this->so_group->add_user_by_name($id_group,$full_name);
4214                                                        }                                               
4215                                                        $return['_new']++;
4216                                                }
4217                        }
4218                                fclose($handle);
4219                                unlink($file);
4220                        }
4221                        else
4222                                $return['error'] = true;
4223
4224                        echo serialize($return);
4225                }
4226        }
4227
4228?>
Note: See TracBrowser for help on using the repository browser.