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

Revision 3310, 118.7 KB checked in by eduardoalex, 14 years ago (diff)

Ticket #1192 - Correcao do problema relatado no ticket.

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