source: trunk/contactcenter/inc/class.ui_data.inc.php @ 118

Revision 118, 80.1 KB checked in by wmerlotto, 16 years ago (diff)

ContactCenter? internacionalizado. Corrigido falhas de tradução do ExpressoMail?.

  • 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                        if($this->page_info['actual_catalog']['class'] == 'bo_group_manager')
71                                $this -> typeContact = 'groups';
72                        else
73                                $this -> typeContact = 'contacts';
74                }
75
76                /*!
77               
78                        @function index
79                        @abstract Builds the Main Page
80                        @author Raphael Derosso Pereira
81                        @author Jonas Goes
82                       
83                */             
84                function index()
85                {       
86                        if(!@is_object($GLOBALS['phpgw']->js))
87                        {
88                                $GLOBALS['phpgw']->js = CreateObject('phpgwapi.javascript');
89                        }
90                        $GLOBALS['phpgw']->js->validate_file('venus','table');
91                        $GLOBALS['phpgw']->js->validate_file('venus','shapes');
92                        $GLOBALS['phpgw']->js->validate_file('venus','jsStructUtil');
93                        $GLOBALS['phpgw']->js->validate_file('venus','cssUtil');
94                       
95//                      $GLOBALS['phpgw']->js->set_onload('setTimeout(\'updateCards()\',1000)');
96                        $GLOBALS['phpgw']->common->phpgw_header();
97                       
98                        $GLOBALS['phpgw']->template->set_file(array('index' => 'index.tpl'));
99                        $GLOBALS['phpgw']->template->set_var('cc_root_dir', $GLOBALS['phpgw_info']['server']['webserver_url'].'/contactcenter/');
100                       
101                        /* Quick Add */
102                        $GLOBALS['phpgw']->template->set_var('cc_qa_alias',lang('Alias').':');
103                        $GLOBALS['phpgw']->template->set_var('cc_qa_given_names',lang('Given Names').':');
104                        $GLOBALS['phpgw']->template->set_var('cc_qa_family_names',lang('Family Names').':');
105                        $GLOBALS['phpgw']->template->set_var('cc_qa_phone',lang('Phone').':');
106                        $GLOBALS['phpgw']->template->set_var('cc_qa_email',lang('Email').':');
107                        $GLOBALS['phpgw']->template->set_var('cc_qa_save',lang('Save'));
108                        $GLOBALS['phpgw']->template->set_var('cc_qa_clear',lang('Clear'));
109                        $GLOBALS['phpgw']->template->set_var('cc_qa_close',lang('Close'));
110                        /* End Quick Add */
111                       
112                        $cc_css_file = $GLOBALS['phpgw_info']['server']['webserver_url'].'/contactcenter/styles/cc.css';
113                        $cc_card_image_file = $GLOBALS['phpgw_info']['server']['webserver_url'].'/contactcenter/templates/default/images/card.png';
114                        $GLOBALS['phpgw']->template->set_var('cc_css',$cc_css_file);
115                        $GLOBALS['phpgw']->template->set_var('cc_dtree_css', $cc_dtree_file);
116                        $GLOBALS['phpgw']->template->set_var('cc_card_image',$cc_card_image_file);
117                       
118                        $GLOBALS['phpgw']->template->set_var('cc_personal',lang('Personal'));
119                       
120                        $GLOBALS['phpgw']->template->set_var('cc_full_add',lang('Full Add'));
121                        $GLOBALS['phpgw']->template->set_var('cc_reset',lang('Reset'));
122                       
123                        $GLOBALS['phpgw']->template->set_var('cc_personal_data',lang('Personal Data'));
124                        $GLOBALS['phpgw']->template->set_var('cc_addresses',lang('Addresses'));
125                        $GLOBALS['phpgw']->template->set_var('cc_connections',lang('Connections'));
126                        $GLOBALS['phpgw']->template->set_var('cc_relations',lang('Relations'));
127
128                        $GLOBALS['phpgw']->template->set_var('cc_quick_add',lang('Quick Add'));
129                        $GLOBALS['phpgw']->template->set_var('cc_catalogs',lang('Catalogues'));
130                        $GLOBALS['phpgw']->template->set_var('cc_group_add',lang('Group Add'));
131                       
132                        /* Panel */
133                        $GLOBALS['phpgw']->template->set_var('cc_panel_new',lang('New').'...');
134                        $GLOBALS['phpgw']->template->set_var('cc_panel_search',lang('Search').'...');
135                        $GLOBALS['phpgw']->template->set_var('cc_panel_table',lang('Table View'));
136                        $GLOBALS['phpgw']->template->set_var('cc_panel_cards',lang('Cards View'));
137                        $GLOBALS['phpgw']->template->set_var('cc_btn_import_export', lang('Import/Export'));
138                        $GLOBALS['phpgw']->template->set_var('cc_btn_new', lang("New..."));
139                       
140                        $GLOBALS['phpgw']->template->set_var('cc_panel_search_found',lang('Showing found entries'));
141                        $GLOBALS['phpgw']->template->set_var('cc_panel_first_page',lang('First Page'));
142                        $GLOBALS['phpgw']->template->set_var('cc_panel_previous_page',lang('Previous Page'));
143                        $GLOBALS['phpgw']->template->set_var('cc_panel_next_page',lang('Next Page'));
144                        $GLOBALS['phpgw']->template->set_var('cc_panel_last_page',lang('Last Page'));
145                        $GLOBALS['phpgw']->template->set_var('cc_all',lang('all'));
146                        /* End Panel */
147                       
148                        /* Messages */
149                        $GLOBALS['phpgw']->template->set_var('cc_msg_no_cards',lang('No Cards'));
150                        $GLOBALS['phpgw']->template->set_var('cc_msg_err_no_room',lang('No Room for Cards! Increase your browser area.'));
151                        $GLOBALS['phpgw']->template->set_var('cc_msg_card_new',lang('New from same Company'));
152                        $GLOBALS['phpgw']->template->set_var('cc_msg_card_edit',lang('Edit Contact'));
153                        $GLOBALS['phpgw']->template->set_var('cc_msg_card_remove',lang('Remove Contact'));
154                        $GLOBALS['phpgw']->template->set_var('cc_send_mail',lang('Send Mail'));
155                        $GLOBALS['phpgw']->template->set_var('cc_msg_group_edit',lang('Edit Group'));
156                        $GLOBALS['phpgw']->template->set_var('cc_msg_group_remove',lang('Remove Group'));
157                        $GLOBALS['phpgw']->template->set_var('cc_msg_group_remove_confirm',lang('Confirm Removal of this Group?'));
158                        $GLOBALS['phpgw']->template->set_var('cc_msg_card_remove_confirm',lang('Confirm Removal of this Contact?'));
159                        $GLOBALS['phpgw']->template->set_var('cc_participants',lang('Participants'));
160                        $GLOBALS['phpgw']->template->set_var('cc_empty',lang('Empty'));
161                        /* End Messages */
162                       
163                        $GLOBALS['phpgw']->template->set_var('cc_results',lang('Results'));
164                        $GLOBALS['phpgw']->template->set_var('cc_is_my',lang('Is My'));
165                        $GLOBALS['phpgw']->template->set_var('cc_ie_personal',lang('Import/Export pesonal contacts'));
166                        $GLOBALS['phpgw']->template->set_var('cc_btn_search',lang('Search'));
167                        $GLOBALS['phpgw']->template->set_var('cc_add_relation',lang('Add Relation'));
168                        $GLOBALS['phpgw']->template->set_var('cc_del_relation',lang('Remove Selected Relations'));                     
169                        $GLOBALS['phpgw']->template->set_var('cc_msg_group',lang('Group'));
170                        $GLOBALS['phpgw']->template->set_var('cc_msg_contact_full',lang('Contact [Full]'));
171                        $GLOBALS['phpgw']->template->set_var('cc_msg_contact_qa',lang('Contact [Quick Add]'));
172                        $GLOBALS['phpgw']->template->set_var('cc_contact_title',lang('Contact Center').' - '.lang('Contacts'));
173                        $GLOBALS['phpgw']->template->set_var('cc_window_views_title',lang('Contact Center').' - '.lang('Views'));
174                        $GLOBALS['phpgw']->template->set_var('phpgw_img_dir', $GLOBALS['phpgw_info']['server']['webserver_url'] . '/phpgwapi/images');
175
176                        $GLOBALS['phpgw']->template->set_var('cc_msg_import_contacts', lang('Import Contacts'));
177                        $GLOBALS['phpgw']->template->set_var('cc_msg_export_contacts', lang('Export Contacts'));
178                        $GLOBALS['phpgw']->template->set_var('cc_msg_expresso_info_csv', lang('The Expresso supports the contacts importation in the CSV file format.'));
179                        $GLOBALS['phpgw']->template->set_var('cc_msg_choose_file_type', lang('Select the file type.'));
180                        $GLOBALS['phpgw']->template->set_var('cc_msg_outlook_express', lang('Outlook Express'));
181                        $GLOBALS['phpgw']->template->set_var('cc_msg_outlook2k', lang('Outlook 2000'));
182                        $GLOBALS['phpgw']->template->set_var('cc_msg_expresso_default', lang('Expresso (default)'));
183                        $GLOBALS['phpgw']->template->set_var('cc_msg_choose_contacts_file', lang('Select the file that contains the contacts to be imported:'));
184                        $GLOBALS['phpgw']->template->set_var('cc_msg_close', lang('Close'));
185                        $GLOBALS['phpgw']->template->set_var('cc_msg_ie_personal', lang('Import / Export personal Contacts'));
186                        $GLOBALS['phpgw']->template->set_var('cc_msg_import_fail', lang('The importation has failed. Verify the file format.'));
187                        $GLOBALS['phpgw']->template->set_var('cc_msg_importing_contacts', lang('Importing Contacts...'));
188                        $GLOBALS['phpgw']->template->set_var('cc_msg_import_finished', lang('The importation has finished.'));
189                        $GLOBALS['phpgw']->template->set_var('cc_msg_choose_valid_csv', lang('Select a valid CSV file to import your contacts'));
190                        $GLOBALS['phpgw']->template->set_var('cc_msg_export_error', lang('An error has occurred while the exportation.'));
191                        $GLOBALS['phpgw']->template->set_var('cc_msg_new_email', lang('New E-Mail...'));
192                        $GLOBALS['phpgw']->template->set_var('cc_msg_main', lang('Main'));
193                        $GLOBALS['phpgw']->template->set_var('cc_msg_alternative', lang('Alternative'));
194                        $GLOBALS['phpgw']->template->set_var('cc_msg_select_email', lang('Select E-Mail'));
195                        $GLOBALS['phpgw']->template->set_var('cc_msg_new_phone', lang('New Telephone'));
196                        $GLOBALS['phpgw']->template->set_var('cc_msg_home', lang('Home'));
197                        $GLOBALS['phpgw']->template->set_var('cc_msg_cellphone', lang('Cellphone'));
198                        $GLOBALS['phpgw']->template->set_var('cc_msg_work', lang('Work'));
199                        $GLOBALS['phpgw']->template->set_var('cc_msg_fax', lang('Fax'));
200                        $GLOBALS['phpgw']->template->set_var('cc_msg_pager', lang('Pager'));
201                        $GLOBALS['phpgw']->template->set_var('cc_msg_choose_phone', lang('Select the telephone'));
202                        $GLOBALS['phpgw']->template->set_var('cc_msg_warn_firefox', lang('Warning: Too old version of Firefox'));
203                        $GLOBALS['phpgw']->template->set_var('cc_msg_firefox_half1', lang('For this application work correctly</u>'));
204                        $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'));
205                        $GLOBALS['phpgw']->template->set_var('cc_msg_click_close', lang('click Close'));
206                        $GLOBALS['phpgw']->template->set_var('cc_msg_install_now', lang('Install Now'));
207                        $GLOBALS['phpgw']->template->set_var('cc_msg_install_new_firefox', lang('Install a new Firefox version'));
208                        $GLOBALS['phpgw']->template->set_var('cc_msg_moz_thunderbird', lang('Export as Mozilla Thunderbird CSV.'));
209                        $GLOBALS['phpgw']->template->set_var('cc_msg_outlook_express_pt', lang('Export as Outlook Express (Portuguese) CSV.'));
210                        $GLOBALS['phpgw']->template->set_var('cc_msg_outlook_express_en', lang('Export as Outlook Express (English) CSV.'));
211                        $GLOBALS['phpgw']->template->set_var('cc_msg_outlook_2k_pt', lang('Export as Outlook 2000 (Portuguese) CSV.'));
212                        $GLOBALS['phpgw']->template->set_var('cc_msg_outlook_2k_en', lang('Export as Outlook 2000 (English) CSV.'));
213                        $GLOBALS['phpgw']->template->set_var('cc_msg_expresso_default_csv', lang('Export as Expresso (Default) CSV.'));
214                        $GLOBALS['phpgw']->template->set_var('cc_msg_copy_to_catalog', lang('Copy to personal catalog.'));
215                        $GLOBALS['phpgw']->template->set_var('cc_msg_add_contact_to_group', lang('You did not add any contact for this group.'));
216                        $GLOBALS['phpgw']->template->set_var('cc_msg_fill_field_name', lang('Fill the field Full Name.'));
217                       
218                        $GLOBALS['phpgw']->template->parse('out','index');
219                       
220                        $api = CreateObject('contactcenter.ui_api');
221                        $main = $api->get_full_add();
222                        $main .= $api->get_search_obj();
223                        $main .= $api->get_quick_add_plugin();
224                        $main .= $api->get_add_group();
225                        $main .= $GLOBALS['phpgw']->template->get_var('out');
226
227                        echo $main;
228                }
229
230               
231                /*!
232               
233                        @function data_manager
234                        @abstract Calls the right method and passes to it the right
235                                parameters
236                        @author Raphael Derosso Pereira
237               
238                */
239                function data_manager()
240                {
241                        switch($_GET['method'])
242                        {
243                                /* Cards Methods */
244                                case 'set_n_cards':
245                                        return $this->set_n_cards((int)$_GET['ncards']);
246                                       
247                                case 'get_cards_data':
248                                        return $this->get_cards_data($_POST['letter'], $_POST['page'], unserialize(str_replace('\\"','"',$_POST['ids'])));
249                                case 'get_cards_data_get':
250                                        return $this->get_cards_data($_GET['letter'], $_GET['page'], unserialize(str_replace('\\"','"',$_GET['ids'])));
251
252
253                                case 'get_photo':
254                                        return $this->get_photo($_GET['id']);
255
256                                       
257                                /* Catalog Methods */
258                                case 'set_catalog':
259                                        return $this->set_catalog($_GET['catalog']);
260                                       
261                                case 'get_catalog_tree':
262                                        echo serialize($this->get_catalog_tree($_GET['level']));
263                                        return;
264
265                                case 'get_actual_catalog':
266                                        echo serialize($this->get_actual_catalog());
267                                        return;
268
269                                case 'get_catalog_participants_list':
270                                        echo serialize($this->get_catalog_participants_list($_POST['id']));
271                                        return;
272
273                                case 'get_catalog_add_contact':
274                                        echo serialize($this->get_catalog_add_contact($_POST['id']));
275                                        return;
276                                       
277                                /* Full Add Methods */
278                                case 'get_full_data':
279                                        return $this->get_full_data($_GET['id']);
280                               
281                                case 'get_group':
282                                        return $this->get_group_data($_GET['id']);
283                                       
284                                case 'get_contact_full_add_const':
285                                        return $this->get_contact_full_add_const();
286
287                                case 'post_full_add':
288                                        return $this->post_full_add();
289
290                                case 'post_photo':
291                                        return $this->post_photo((int) $_GET['id'] ? (int) $_GET['id'] : '_new_');
292
293                                case 'get_states':
294                                        return $this->get_states($_GET['country']);
295                                       
296                                case 'get_cities':
297                                        return $this->get_cities($_GET['country'], $_GET['state'] ? $_GET['state'] : null);
298                                       
299                                       
300                                /* Other Methods */
301                                case 'quick_add':                                                                                                                       
302                                        return $this->quick_add($_POST['add']);
303                               
304                                case 'add_group':                                                                                                                       
305                                        return $this->add_group($_POST['add']);
306                                                                       
307                                case 'remove_entry':
308                                        return $this->remove_entry((int)$_GET['remove']);
309                               
310                                case 'remove_all_entries':
311                                        return $this->remove_all_entries();
312
313                                case 'remove_group':
314                                                                               
315                                        return $this->remove_group((int)$_GET['remove']);                                       
316
317                                case 'search':
318                                        return $this->search(str_replace('\\"', '"', $_GET['data']));
319
320                                case 'email_win':
321                                        $GLOBALS['phpgw']->common->phpgw_header();
322                                        $api = CreateObject('contactcenter.ui_api');
323                                        $win = $api->get_email_win();
324                                        $win .= $api->get_quick_add_plugin();
325                                        $win .= '<input id="QAbutton" type="button" value="QuickAdd" />'
326                                                .'<br><input type="button" value="EmailWin" onclick="ccEmailWin.open()" />'
327                                                .'<script type="text/javascript">'
328                                                .'      ccQuickAdd.associateAsButton(Element("QAbutton"));'
329                                                .'</script>';
330                                        echo $win;
331                                        return;
332
333                                /* Information Gathering */
334                                case 'get_multiple_entries':
335                                        echo serialize($this->get_multiple_entries(str_replace('\\"','"',$_POST['data'])));
336                                        return;
337
338                                case 'get_all_entries':
339                                        echo serialize($this->get_all_entries(str_replace('\\"','"',$_POST['data'])));
340                                        return;
341
342                                case 'import_contacts':
343                                        return $this->import_contacts($_GET['typeImport']);
344
345                                case 'export_contacts':
346                                        return $this->export_contacts($_POST['typeExport']);
347
348                        }
349                }
350
351                /*!
352               
353                        @function set_n_cards
354                        @abstract Informs the class the number of cards the page can show
355                        @author Raphael Derosso Pereira
356                       
357                        @param integer $n_cards The number of cards
358                       
359                */
360                function set_n_cards($n_cards)
361                {
362                        if (is_int($n_cards))
363                        {
364                                $this->page_info['n_cards'] = $n_cards;
365                                echo 1;
366                        }
367                       
368                        $this->save_session();
369                }
370                               
371                /*!
372               
373                        @function set_catalog
374                        @abstract Sets the current catalog selected by the user
375                        @author Raphael Derosso Pereira
376                       
377                        @param string $id_catalog The sequence of IDs to reach the catalog
378                                separated by commas
379               
380                */
381                function set_catalog($id_catalog)
382                {
383                        $id_catalog = str_replace('\\"', '"', $id_catalog);
384                        $temp =& $this->bo->set_catalog($id_catalog);
385                       
386                        if ($temp)
387                        {
388                                $this->page_info['changed'] = true;
389                                $this->page_info['actual_entries'] = false;
390                                $this->page_info['actual_catalog'] =& $temp;
391                                $this->save_session();
392                               
393                                $catalog_info = $this->bo->get_branch_by_level($this->bo->catalog_level[0]);
394                               
395                                if ($catalog_info['class'] === 'bo_global_ldap_catalog' ||
396                                    $catalog_info['class'] === 'bo_catalog_group_catalog')
397                                {
398                                        $perms = 1;
399                                }
400                                else
401                                {
402                                        $perms = 15;
403                                }
404                               
405                                echo serialize(array(
406                                        'status' => 'ok',
407                                        'perms'  => $perms
408                                ));
409
410                                return;
411                        }
412                       
413                        echo serialize(array(
414                                'status' => 'ok',
415                                'perms'  => 0
416                        ));
417                }
418               
419               
420                /*!
421               
422                        @function get_catalog_tree
423                        @abstract Returns the JS serialized array to used as the tree
424                                level
425                        @author Raphael Derosso Pereira
426                       
427                        @param (string) $level The level to be taken
428               
429                */
430                function get_catalog_tree($level)
431                {
432                        if ($level === '0')
433                        {
434                                $folderImageDir = $GLOBALS['phpgw_info']['server']['webserver_url'] . '/phpgwapi/dftree/images/';
435
436                                $parent = '0';
437                               
438                                if (!($tree = $this->bo->get_catalog_tree($level)))
439                                {
440                                        return array(
441                                                'msg'    => lang('Couldn\'t get the Catalogue Tree. Please contact the Administrator.'),
442                                                'status' => 'fatal'
443                                        );
444                                }
445                        }
446                        else
447                        {
448                                $last_dot = strrpos($level,'.');
449                                $parent = substr($level, 0, $last_dot);
450                                $child = substr($level, $last_dot+1, strlen($level));
451                       
452                                if (!($tree[$child] = $this->bo->get_catalog_tree($level)))
453                                {
454                                        return array(
455                                                'msg'    => lang('Couldn\'t get the Catalogue Tree. Please contact the Administrator.'),
456                                                'status' => 'fatal'
457                                        );
458                                }
459                        }
460                       
461                        $folderImageDir = $GLOBALS['phpgw']->common->image('contactcenter','globalcatalog-mini.png');
462                        $folderImageDir = substr($folderImageDir, 0, strpos($folderImageDir, 'globalcatalog-mini.png'));
463                       
464                        $tree_js = $this->convert_tree($tree, $folderImageDir, $parent);
465                       
466                        return array(
467                                'data' => $tree_js,
468                                'msg'  => lang('Catalog Tree Successfully taken!'),
469                                'status' => 'ok'
470                        );
471                }
472               
473/*              function get_catalog_tree($level, $name = 'tree')
474                {
475                        if ($level === '0')
476                        {
477                                $folderImageDir = $GLOBALS['phpgw_info']['server']['webserver_url'] . '/phpgwapi/dftree/images/';
478                                $tree_js =  $name." = new dFTree({'name': '".$name."'});\n";
479
480                                $parent = '0';
481                                $child  = '0';
482                               
483                                if (!($tree = $this->bo->get_catalog_tree($level)))
484                                {
485                                        return '0';
486                                }
487                        }
488                        else
489                        {
490                                $last_dot = strrpos($level,'.');
491                                $parent = substr($level, 0, $last_dot);
492                                $child = substr($level, $last_dot+1, strlen($level));
493                       
494                                $tree_js = '';
495                               
496                                if (!($tree[$child] = $this->bo->get_catalog_tree($level)))
497                                {
498                                        return '0';
499                                }
500                        }
501                       
502                        $folderImageDir = $GLOBALS['phpgw']->common->image('contactcenter','globalcatalog-mini.png');
503                        $folderImageDir = substr($folderImageDir, 0, strpos($folderImageDir, 'globalcatalog-mini.png'));
504                       
505                        $tree_js .= $this->convert_tree($tree, $name, $folderImageDir, $parent);
506                       
507                        return $tree_js;
508                }
509*/             
510               
511                /*!
512               
513                        @function get_actual_catalog
514                        @abstract Returns the actual selected Catalog
515                        @author Raphael Derosso Pereira
516
517                */
518                function get_actual_catalog()
519                {                       
520                        $level = $this->bo->get_level_by_branch($this->bo->get_actual_catalog(), $this->bo->tree['branches'], '0');
521                       
522                        if ($level)
523                        {
524                                return array(
525                                        'status' => 'ok',
526                                        'data'   => $level
527                                );
528                        }
529
530                        return array(
531                                'status' => 'fatal',
532                                'msg'    => lang('Couldn\'t get the actual catalog.'),
533                        );
534                }
535               
536                /*!
537               
538                        @function get_cards_data
539                        @abstract Returns the information that is placed on the cards
540                        @author Raphael Derosso Pereira
541                       
542                        @param string $letter The first letter to be searched
543                        @param (int)  $page The page to be taken
544                        @param (str)  $ids The ids to be taken in case of search
545
546                        TODO: This function is not well done. It must be rewritten
547                                using the new array 'msg','status','data' schema.
548                */
549                function get_cards_data($letter, $page, $ids)
550                {
551                        //echo $page."\n";
552                        if ($letter !== 'search' and ($letter != $this->page_info['actual_letter'] or
553                            ($letter == $this->page_info['actual_letter'] and $page == $this->page_info['actual_page']) or
554                            $this->page_info['changed']))
555                        {
556                                unset($ids);
557                                $this->page_info['changed'] = false;
558                                 
559                                switch ($this->page_info['actual_catalog']['class'])
560                                {
561                                        case 'bo_people_catalog':
562                                                $field_name = 'id_contact';
563
564                                                if ($letter !== 'number')
565                                                {
566                                                        $find_restric[0] = array(
567                                                                0 => array(
568                                                                        'field' => 'contact.names_ordered',
569                                                                        'type'  => 'iLIKE',
570                                                                        'value' => $letter !== 'all' ? $letter.'%' : '%'
571                                                                ),
572                                                                1 => array(
573                                                                        'field' => 'contact.id_owner',
574                                                                        'type'  => '=',
575                                                                        'value' => $GLOBALS['phpgw_info']['user']['account_id']
576                                                                )
577                                                        );
578                                                }
579                                                else
580                                                {
581                                                        $find_restric[0] = array(
582                                                                0 => array(
583                                                                        'type'  => 'branch',
584                                                                        'value' => 'OR',
585                                                                        'sub_branch' => array(
586                                                                                0 => array(
587                                                                                        'field' => 'contact.names_ordered',
588                                                                                        'type'  => 'LIKE',
589                                                                                        'value' => '0%'
590                                                                                ),
591                                                                                1 => array(
592                                                                                        'field' => 'contact.names_ordered',
593                                                                                        'type'  => 'LIKE',
594                                                                                        'value' => '1%'
595                                                                                ),
596                                                                                2 => array(
597                                                                                        'field' => 'contact.names_ordered',
598                                                                                        'type'  => 'LIKE',
599                                                                                        'value' => '2%'
600                                                                                ),
601                                                                                3 => array(
602                                                                                        'field' => 'contact.names_ordered',
603                                                                                        'type'  => 'LIKE',
604                                                                                        'value' => '3%'
605                                                                                ),
606                                                                                4 => array(
607                                                                                        'field' => 'contact.names_ordered',
608                                                                                        'type'  => 'LIKE',
609                                                                                        'value' => '4%'
610                                                                                ),
611                                                                                5 => array(
612                                                                                        'field' => 'contact.names_ordered',
613                                                                                        'type'  => 'LIKE',
614                                                                                        'value' => '5%'
615                                                                                ),
616                                                                                6 => array(
617                                                                                        'field' => 'contact.names_ordered',
618                                                                                        'type'  => 'LIKE',
619                                                                                        'value' => '6%'
620                                                                                ),
621                                                                                7 => array(
622                                                                                        'field' => 'contact.names_ordered',
623                                                                                        'type'  => 'LIKE',
624                                                                                        'value' => '7%'
625                                                                                ),
626                                                                                8 => array(
627                                                                                        'field' => 'contact.names_ordered',
628                                                                                        'type'  => 'LIKE',
629                                                                                        'value' => '8%'
630                                                                                ),
631                                                                                9 => array(
632                                                                                        'field' => 'contact.names_ordered',
633                                                                                        'type'  => 'LIKE',
634                                                                                        'value' => '9%'
635                                                                                ),
636                                                                        ),
637                                                                ),
638                                                                1 => array(
639                                                                        'field' => 'contact.id_owner',
640                                                                        'type'  => '=',
641                                                                        'value' => $GLOBALS['phpgw_info']['user']['account_id']
642                                                                ),
643                                                        );
644                                                }
645                                               
646                                                $find_field[0] = array('contact.id_contact','contact.names_ordered');
647                                               
648                                                $find_other[0] = array(
649                                                        //'offset' => (($page-1)*$this->page_info['n_cards']),
650                                                        //'limit'  => $this->page_info['n_cards'],
651                                                        'order'  => 'contact.names_ordered'
652                                                );
653                                               
654                                                break;
655                                       
656                                        case 'bo_global_ldap_catalog':
657                                                $field_name = 'id_contact';
658
659                                                if ($letter !== 'number')
660                                                {
661                                                        $find_restric[0] = array(
662                                                                0 => array(
663                                                                        'field' => 'contact.names_ordered',
664                                                                        'type'  => 'iLIKE',
665                                                                        'value' => $letter !== 'all' ? $letter.'%' : '%'
666                                                                )
667                                                        );
668                                                        $find_restric[0][1] =  array(
669                                                                        'type'  => 'branch',
670                                                                        'value' => 'OR',
671                                                                        'sub_branch' => array(
672                                                                                0 => array(
673                                                                                'field' => 'contact.account_type',
674                                                                                'type'  => '=',
675                                                                                'value' => 'u'
676                                                                                ),
677                                                                                /*1 => array(
678                                                                                'field' => 'contact.account_type',
679                                                                                'type'  => '=',
680                                                                                'value' => 'g'
681                                                                                ),*/
682                                                                                2 => array(
683                                                                                'field' => 'contact.account_type',
684                                                                                'type'  => '=',
685                                                                                'value' => 'l'
686                                                                                )
687                                                                        )
688                                                        );
689                                                }
690                                                else
691                                                {
692                                                        $find_restric[0] = array(
693                                                                0 => array(
694                                                                        'type'  => 'branch',
695                                                                        'value' => 'OR',
696                                                                        'sub_branch' => array(
697                                                                                0 => array(
698                                                                                        'field' => 'contact.names_ordered',
699                                                                                        'type'  => 'LIKE',
700                                                                                        'value' => '0%'
701                                                                                ),
702                                                                                1 => array(
703                                                                                        'field' => 'contact.names_ordered',
704                                                                                        'type'  => 'LIKE',
705                                                                                        'value' => '1%'
706                                                                                ),
707                                                                                2 => array(
708                                                                                        'field' => 'contact.names_ordered',
709                                                                                        'type'  => 'LIKE',
710                                                                                        'value' => '2%'
711                                                                                ),
712                                                                                3 => array(
713                                                                                        'field' => 'contact.names_ordered',
714                                                                                        'type'  => 'LIKE',
715                                                                                        'value' => '3%'
716                                                                                ),
717                                                                                4 => array(
718                                                                                        'field' => 'contact.names_ordered',
719                                                                                        'type'  => 'LIKE',
720                                                                                        'value' => '4%'
721                                                                                ),
722                                                                                5 => array(
723                                                                                        'field' => 'contact.names_ordered',
724                                                                                        'type'  => 'LIKE',
725                                                                                        'value' => '5%'
726                                                                                ),
727                                                                                6 => array(
728                                                                                        'field' => 'contact.names_ordered',
729                                                                                        'type'  => 'LIKE',
730                                                                                        'value' => '6%'
731                                                                                ),
732                                                                                7 => array(
733                                                                                        'field' => 'contact.names_ordered',
734                                                                                        'type'  => 'LIKE',
735                                                                                        'value' => '7%'
736                                                                                ),
737                                                                                8 => array(
738                                                                                        'field' => 'contact.names_ordered',
739                                                                                        'type'  => 'LIKE',
740                                                                                        'value' => '8%'
741                                                                                ),
742                                                                                9 => array(
743                                                                                        'field' => 'contact.names_ordered',
744                                                                                        'type'  => 'LIKE',
745                                                                                        'value' => '9%'
746                                                                                ),
747                                                                        ),
748                                                                ),
749                                                        );
750                                                        $find_restric[0][1] =  array(
751                                                                'type'  => 'branch',
752                                                                'value' => 'OR',
753                                                                'sub_branch' => array(
754                                                                        0 => array(
755                                                                        'field' => 'contact.account_type',
756                                                                        'type'  => '=',
757                                                                        'value' => 'u'
758                                                                        ),
759                                                                        /*1 => array(
760                                                                        'field' => 'contact.account_type',
761                                                                        'type'  => '=',
762                                                                        'value' => 'g'
763                                                                        ),*/
764                                                                        2 => array(
765                                                                        'field' => 'contact.account_type',
766                                                                        'type'  => '=',
767                                                                        'value' => 'l'
768                                                                        )
769                                                                )
770                                                        );
771                                                }
772                                               
773                                                $find_field[0] = array('contact.id_contact','contact.names_ordered','contact.account_type');
774                                               
775                                                $find_other[0] = array(
776                                                        //'offset' => (($page-1)*$this->page_info['n_cards']),
777                                                        //'limit'  => $this->page_info['n_cards'],
778                                                        'order'  => 'contact.names_ordered'
779                                                );
780                                               
781                                                break;
782                                       
783                                        case 'bo_company_manager':
784                                                $field_name = 'id_company';
785       
786                                                $find_field[0] = array('company.id_company','company.company_name');
787                                               
788                                                $find_other[0] = array(
789                                                        //'offset' => (($page-1)*$this->page_info['n_cards']),
790                                                        //'limit'  => $this->page_info['n_cards'],
791                                                        'order'  => 'company.company_name'
792                                                );
793       
794                                                $find_restric[0] = array(
795                                                        0 => array(
796                                                                'field' => 'company.company_name',
797                                                                'type'  => 'iLIKE',
798                                                                'value' => $letter !== 'all' ? $letter.'%' : '%'
799                                                        )
800                                                );
801                                                       
802                                                break;
803
804                                        case 'bo_group_manager':
805                                       
806                                                $field_name = 'id_group';
807                                               
808                                                if ($letter !== 'number')       {
809                                                       
810                                                        $find_restric[0] = array(
811                                                                0 => array(
812                                                                        'field' => 'group.title',
813                                                                        'type'  => 'iLIKE',
814                                                                        'value' => $letter !== 'all' ? $letter.'%' : '%'
815                                                                )
816                                                        );
817                                                }
818                                                 else {
819                                                       
820                                                        $find_restric[0] = array(
821                                                                0 => array(
822                                                                                        'field' => 'group.title',
823                                                                                        'type'  => 'LIKE',
824                                                                                        'value' => '0%'                                                                         
825                                                                )
826                                                        );                                             
827                                                }
828                                               
829                                                array_push($find_restric[0],  array(
830                                                                                        'field' => 'group.owner',
831                                                                                        'type'  => '=',
832                                                                                        'value' => $GLOBALS['phpgw_info']['user']['account_id']                                                                         
833                                                                )
834                                                );
835                                               
836                                                $find_field[0] = array('group.id_group','group.title','group.short_name');                                             
837                                                $find_other[0] = array(
838                                                        'order'  => 'group.title'
839                                                );
840                                               
841                                                break;
842                                               
843                                        case 'bo_catalog_group_catalog':
844                                                $this->page_info['actual_entries'] = false;
845                                               
846                                                $this->page_info['actual_letter'] = $letter;
847                                                $this->page_info['actual_page'] = 0;
848                                               
849                                                $this->save_session();
850                                                $final[0] = 0;
851                                                $final[1] = $this -> typeContact;
852                                                echo serialize($final);                                         
853                                                return;
854
855                                }
856                               
857                                $result = $this->bo->find($find_field[0],$find_restric[0],$find_other[0]);
858                                $n_entries = count($result);
859                               
860                                if ($n_entries)
861                                {
862                                        //echo 'N_entries: '.$n_entries.'<br>';
863                                        $this->page_info['n_pages'] = ceil($n_entries/($this->page_info['n_cards'] ? $this->page_info['n_cards'] : 1));
864                                }
865                                else
866                                {
867                                        $this->page_info['n_pages'] = 0;
868                                }
869
870                                if (!$result)
871                                {
872                                        $this->page_info['actual_entries'] = false;
873                                       
874                                        $this->page_info['actual_letter'] = $letter;
875                                        $this->page_info['actual_page'] = 0;
876                                       
877                                        $this->save_session();                         
878                                        $final[0] = 0;
879                                        $final[1] = $this -> typeContact;
880                                        echo serialize($final);                                                                                 
881                                        return;
882                                }
883                                else
884                                {
885                                        unset($this->page_info['actual_entries']);
886                                        foreach ($result as $id => $value)
887                                        {
888                                                $this->page_info['actual_entries'][] = $value[$field_name];
889                                        }
890                                        //print_r($this->page_info['actual_entries']);
891                                }
892                        }
893                        else if ($letter === 'search')
894                        {
895                                if (!$ids and $this->page_info['actual_letter'] !== 'search')
896                                {
897                                        $this->page_info['actual_entries'] = false;
898                                       
899                                        $this->page_info['actual_letter'] = $letter;
900                                        $this->page_info['actual_page'] = 0;
901                                       
902                                        $this->save_session();
903                                        $final[0] = 0;
904                                        $final[1] = $this -> typeContact;
905                                        echo serialize($final);                                 
906                                        return;
907                                }
908                                else if ($ids)
909                                {                               
910                                        $this->page_info['actual_letter']  = $letter;
911                                        $this->page_info['actual_entries'] = $ids;
912                                        $this->page_info['n_pages'] = ceil(count($ids)/($this->page_info['n_cards'] ? $this->page_info['n_cards'] : 1));
913                                }
914                        }
915                        else
916                        {
917                                unset($ids);
918                        }
919
920                        if ($this->page_info['actual_entries'])
921                        {
922                                if ($page >= $this->page_info['n_pages'])
923                                {
924                                        $page = $this->page_info['n_pages'];
925                                }
926                               
927                                $final = array(
928                                        0 => (int)$this->page_info['n_pages'],
929                                        1 => (int)$page,
930                                        2 => array(
931                                                0 => 'cc_company',
932                                                1 => 'cc_name',
933                                                2 => 'cc_title',
934                                                3 => 'cc_phone',
935                                                4 => 'cc_mail',
936                                                5 => 'cc_alias',
937                                                6 => 'cc_id',
938                                                7 => 'cc_forwarding_address'
939                                        )
940                                );
941                               
942                                //echo 'Page: '.$page.'<br>';
943                                $id_i = (($page-1)*$this->page_info['n_cards']);
944                                $id_f = $id_i + $this->page_info['n_cards'];
945                                $n_entries = count($this->page_info['actual_entries']);
946                               
947                                //echo 'ID_I: '.$id_i.'<br>';
948                                //echo 'ID_F: '.$id_f.'<br>';
949                                ///---------------- Correção Temporária PHP5 -----------------------///
950                                $ids = array();
951                                $array_temp = array();                 
952                                                               
953                                foreach($this->page_info['actual_entries'] as $key=>$tmp){
954                                        $array_temp[] = $tmp;                           
955                                }       
956                               
957                                for($i = $id_i; $i < $id_f and $i < $n_entries; $i++)
958                                {
959                                        $ids[] = $array_temp[$i];
960                                }
961                               
962                                /// Original
963                                //for($i = $id_i; $i < $id_f and $i < $n_entries; $i++)
964                                //{
965                                //      $ids[] = $this->page_info['actual_entries'][$i];
966                                //}
967                                ///
968                               
969                                $fields = $this->bo->catalog->get_fields(false);
970
971                                if( $this -> typeContact == 'groups') {
972                                        $final = array(
973                                                0 => (int)$this->page_info['n_pages'],
974                                                1 => (int)$page,
975                                                2 => array(
976                                                        0 => 'cc_title',
977                                                        1 => 'cc_short_name',
978                                                        2 => 'cc_id',
979                                                        3 => 'cc_contacts'
980                                                )                                               
981                                        );                                     
982                                       
983                                        $groups =& $this->bo->catalog->get_multiple_entries($ids,$fields);
984                                       
985                                        $i = 0;
986                                        // contatos do grupo
987                                        $boGroups = CreateObject('contactcenter.bo_group');                                     
988                                        $contacts = array();
989                                        foreach($groups as $group)              {
990                                                                               
991                                                $final[3][$i][0] = $group['title'] ? $group['title'] : 'none';
992                                                $final[3][$i][1] = $group['short_name'] ? $group['short_name'] : 'none';                                               
993                                                $final[3][$i][2] = $group['id_group'] ? $group['id_group'] : 'none';
994                                                $contacts = $boGroups -> get_contacts_by_group($group['id_group']);
995                                                $final[3][$i][3] = $contacts;
996                                                $i++;                                                   
997                                        }
998                                       
999                                        $this->page_info['actual_letter'] = $letter;
1000                                        $this->page_info['actual_page'] = $page;                                       
1001                                       
1002                                       
1003                                        $lnk_compose = "location.href=('../expressoMail1_2/index.php?to=";
1004                                       
1005                                        $final[5] = '<span class="link"  onclick="'.$lnk_compose;
1006                                        $final[10] = 'groups';                                                                                                                                                                                                                         
1007                                        $this->save_session();                                 
1008                                        echo serialize($final);
1009                                        return;                                 
1010                                }
1011                               
1012                                $fields['photo'] = true;
1013                                $fields['names_ordered'] = true;
1014                                $fields['alias'] = true;
1015                                $fields['account_type'] = true;
1016                                $fields['companies'] = 'default';
1017                                $fields['connections'] = 'default';
1018                                $contacts =& $this->bo->catalog->get_multiple_entries($ids,$fields);
1019                               
1020                               
1021                                if (!is_array($contacts) or !count($contacts))
1022                                {
1023                                        $final[0] = 0;
1024                                        $final[1] = $this -> typeContact;
1025                                        echo serialize($final);
1026                                        return;
1027                                }
1028                               
1029                                $i = 0;
1030                                foreach($contacts as $contact)
1031                                {
1032                                        $final[3][$i][0] = $contact['companies']['company1']['company_name']?$contact['companies']['company1']['company_name']:'none';
1033                                        $final[3][$i][1] = $contact['names_ordered'] ? $contact['names_ordered'] : 'none';
1034                                        $final[3][$i][2] = $contact['companies']['company1']['title']?$contact['companies']['company1']['title']:'none';
1035
1036                                        /* Select the correct Email and Telephone to be shown */
1037                                        $preferences = ExecMethod('contactcenter.ui_preferences.get_preferences');
1038                                        if (!is_array($preferences))
1039                                        {
1040                                                $preferences['personCardEmail'] = 1;
1041                                                $preferences['personCardPhone'] = 2;
1042                                        }
1043                                        if ($contact['connections'])
1044                                        {
1045                                                $default_email_found = false;
1046                                                $default_phone_found = false;
1047                                                foreach($contact['connections'] as $conn_info)
1048                                                {
1049                                                        if ($conn_info['id_type'] == $preferences['personCardEmail'] and !$default_email_found)
1050                                                        {
1051                                                                if ($conn_info['connection_is_default'])
1052                                                                {
1053                                                                        $default_email_found = true;
1054                                                                }
1055                                                                $final[3][$i][4] = $conn_info['connection_value'] ? $conn_info['connection_value'] : 'none';
1056                                                        }
1057                                                        else if ($conn_info['id_type'] == $preferences['personCardPhone'] and !$default_phone_found)
1058                                                        {
1059                                                                if ($conn_info['connection_is_default'])
1060                                                                {
1061                                                                        $default_phone_found = true;
1062                                                                }
1063                                                                $final[3][$i][3] = $conn_info['connection_value'] ? $conn_info['connection_value'] : 'none';
1064                                                        }
1065                                                }
1066                                        }
1067                                       
1068                                        if (!$final[3][$i][3])
1069                                        {
1070                                                $final[3][$i][3] = 'none';
1071                                        }
1072                                       
1073                                        if (!$final[3][$i][4])
1074                                        {
1075                                                $final[3][$i][4] = 'none';
1076                                        }
1077
1078                                        $final[3][$i][5] = $contact['alias']?$contact['alias']:'none';
1079                                        $final[3][$i][6] = $ids[$i];
1080
1081                                //      If contact is a public list, then load the forwarding addresses.
1082                                        if($contact['account_type'][0] == 'l')
1083                                                $final[3][$i][7] = array();
1084
1085                                        $final[4][$i] = $contact['photo'] ? 1  : 0;
1086                                        $i++;
1087                                }
1088                                $lnk_compose = "location.href=('../expressoMail1_2/index.php?to=";                             
1089                                $final[5] = '<span class="link" onclick="'.$lnk_compose;
1090                               
1091                               
1092                                $this->page_info['actual_letter'] = $letter;
1093                                $this->page_info['actual_page'] = $page;
1094                               
1095                                $this->save_session();                         
1096                                echo serialize($final);
1097                                return;
1098                        }
1099                       
1100                        $this->page_info['actual_letter'] = $letter;
1101                        $this->page_info['actual_page'] = $page;
1102                       
1103                        $this->save_session();
1104                       
1105                        $final[0] = 0;
1106                        $final[1] = $this -> typeContact;
1107                        echo serialize($final);
1108                }
1109               
1110                /*!
1111               
1112                        @function get_group_data
1113                        @abstract Returns all the information of a given Group
1114                        @author Nilton Emilio Buhrer Neto
1115                       
1116                        @param (integer) $id The id to get information
1117               
1118                */             
1119                function get_group_data($id)
1120                {
1121                        $this->bo->catalog = CreateObject('contactcenter.bo_group_manager');
1122                        $fields = $this->bo->catalog->get_fields(true);
1123                        $data = $this->bo->catalog->get_single_entry($id,$fields);
1124                       
1125                        if($id) {                       
1126                                // get All Contacts by group.
1127                                $data['contact_in_list'] = $this->bo->catalog->get_contacts_by_group($id);                                                             
1128                        }
1129                       
1130                        $boGroup = CreateObject('contactcenter.bo_group');
1131                                               
1132                        $data['contact_list'] = $boGroup->get_all_contacts();
1133                       
1134                        $data['result'] = 'ok';                                                         
1135                        echo serialize($data);                 
1136                }               
1137               
1138                /*!
1139               
1140                        @function get_full_data
1141                        @abstract Returns all the information of a given Entry
1142                        @author Raphael Derosso Pereira
1143                       
1144                        @param (integer) $id The id to get information
1145               
1146                */
1147                function get_full_data($id)
1148                {
1149                        $dateformat = $GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'];
1150                        $this->bo->catalog = CreateObject('contactcenter.bo_people_catalog');           
1151                        $fields = $this->bo->catalog->get_fields(true);
1152                        $fields['photo'] = false;
1153                        $entry = $this->bo->catalog->get_single_entry($id,$fields);
1154
1155                        if (is_bool($entry['given_names']))
1156                        {
1157                                $data['result'] = 'false';
1158                                echo serialize($data);
1159                                return;
1160                        }
1161
1162                        $date = explode('-', $entry['birthdate']);
1163                        $j = 0;
1164                        for ($i = 0; $i < 5; $i+=2)
1165                        {
1166                                switch($dateformat{$i})
1167                                {
1168                                        case 'Y':
1169                                                $birthdate[$j] = $date[0];
1170                                                break;
1171
1172                                        case 'm':
1173                                        case 'M':
1174                                                $birthdate[$j] = $date[1];
1175                                                break;
1176
1177                                        case 'd':
1178                                                $birthdate[$j] = $date[2];
1179                                }
1180                                $j++;
1181                        }
1182                        $datecount = 0;
1183                       
1184                        $data['result'] = 'ok';
1185                        $data['cc_full_add_contact_id'] = $id;
1186
1187                        /* Personal Data */
1188                        $data['personal']['cc_pd_photo'] = '../index.php?menuaction=contactcenter.ui_data.data_manager&method=get_photo&id='.$id;
1189                        $data['personal']['cc_pd_alias'] = $entry['alias'];
1190                        $data['personal']['cc_pd_given_names'] = $entry['given_names'];
1191                        $data['personal']['cc_pd_family_names'] = $entry['family_names'];
1192                        $data['personal']['cc_pd_full_name'] = $entry['names_ordered'];
1193                        $data['personal']['cc_pd_suffix'] = $entry['id_suffix'];
1194                        $data['personal']['cc_pd_birthdate_0'] = $birthdate[0];
1195                        $data['personal']['cc_pd_birthdate_1'] = $birthdate[1];
1196                        $data['personal']['cc_pd_birthdate_2'] = $birthdate[2];
1197                        //$data['personal']['cc_pd_sex'] = $entry['sex'] === 'M' ? 1 : ($entry['sex'] === 'F' ? 2 : 0);
1198                        $data['personal']['cc_pd_prefix'] = $entry['id_prefix'];
1199                        $data['personal']['cc_pd_gpg_finger_print'] = $entry['pgp_key'];
1200                        $data['personal']['cc_pd_notes'] = $entry['notes'];
1201
1202                        /* Addresses */
1203                        if (is_array($entry['addresses']))
1204                        {
1205                                $data['addresses'] = $entry['addresses'];
1206                        }
1207
1208                        /* Connections */
1209                        if (is_array($entry['connections']))
1210                        {
1211                                $data['connections'] = array();
1212                                foreach ($entry['connections'] as $connection)
1213                                {
1214                                        $type = $connection['id_type'];
1215                                        $i = count($data['connections'][$type]);
1216                                        $data['connections'][$type][$i]['id'] = $connection['id_connection'];
1217                                        $data['connections'][$type][$i]['name'] = $connection['connection_name'];
1218                                        $data['connections'][$type][$i]['value'] = $connection['connection_value'];
1219                                        $data['connections'][$type][$i]['is_default'] = $connection['connection_is_default'];
1220                                }
1221                        }
1222//                      print_r($data);
1223
1224                        /* Relations */
1225                       
1226                        echo serialize($data);
1227                }
1228
1229                /*!
1230
1231                        @function get_contact_full_add_const
1232                        @abstract Returns all the constant fields in Contact Full Add Window to the JS
1233                        @author Raphael Derosso Pereira
1234                */
1235                function get_contact_full_add_const()
1236                {
1237                        $data = array();
1238                        $boPeopleCatalog = CreateObject('contactcenter.bo_people_catalog');
1239                        $predata[] = $boPeopleCatalog -> get_all_prefixes();
1240                        $predata[] = $boPeopleCatalog -> get_all_suffixes();
1241                        $predata[] = $boPeopleCatalog -> get_all_addresses_types();
1242                        $predata[] = $boPeopleCatalog -> get_all_countries();
1243                        $predata[] = $boPeopleCatalog -> get_all_connections_types();
1244                        //$predata[] = $this->bo->catalog->get_all_relations_types();
1245
1246                        $i = 0;
1247                        foreach($predata as $data_)
1248                        {
1249                                if ($data_)
1250                                {
1251                                        $data[$i] = $data_;
1252                                }
1253
1254                                $i++;
1255                        }
1256
1257                        if (count($data))
1258                        {
1259                                echo serialize($data);
1260                                return;
1261                        }
1262                       
1263                        echo 0;
1264                }
1265               
1266                /*!
1267               
1268                        @function quick_add
1269                        @abstract Adds a new Contact using the Quick Add interface
1270                        @author Raphael Derosso Pereira
1271                       
1272                        @param string $sdata Serialized data
1273                */
1274                function quick_add($sdata)
1275                {
1276                       
1277                        $sdata = str_replace('\\"', '"', $sdata);
1278                        $new_array = unserialize($sdata);
1279                        $tdata = array();
1280                       
1281                        foreach($new_array as $tmp)
1282                                $tdata[] = $tmp;
1283                       
1284                        if (!$tdata)
1285                        {
1286                                echo serialize(array(
1287                                        'msg'    => lang('Problems on adding your Contact. Invalid Data came from client. No Contact added!'),
1288                                        'status' => 'abort'
1289                                ));
1290                               
1291                                return;
1292                        }
1293                       
1294                        // verifica se email já existe!
1295                        $boGroup = CreateObject('contactcenter.bo_group');
1296                        $contact = $boGroup->verify_contact($tdata[4]);
1297                                                                                               
1298                        if($contact)
1299                        {                                                       
1300                                $str_contact = "\r\n - ".implode("\r\n - ",$contact);
1301                               
1302                                echo serialize(array(
1303                                        'msg'    => lang('Problems on adding your Contact. The email "%1" already exists in: %2',$tdata[4], $str_contact),
1304                                        'status' => 'alreadyExists'
1305                                ));
1306                               
1307                                return;                 
1308                        }
1309
1310                        $data['alias'] = $tdata[0];
1311                        $data['given_names'] = $tdata[1];
1312                        $data['family_names'] = $tdata[2];
1313//                      $data['connections']['default_phone']['id_typeof_connection'] = 1;
1314                        $data['connections']['default_phone']['connection_name'] = lang('Main');
1315                        $data['connections']['default_phone']['connection_value'] = $tdata[3];
1316//                      $data['connections']['default_email']['id_typeof_connection'] = 2;
1317                        $data['connections']['default_email']['connection_name'] = lang('Main');
1318                        $data['connections']['default_email']['connection_value'] = $tdata[4];
1319
1320                        $boPeople = CreateObject('contactcenter.bo_people_catalog');
1321
1322                        if ($boPeople ->quick_add($data))
1323                        {                               
1324                                $this->page_info['changed'] = true;
1325                               
1326                                echo serialize(array(
1327                                        'msg'    => lang('Entry added with success!'),
1328                                        'status' => 'ok'
1329                                ));
1330                        }
1331                        else
1332                        {
1333                                echo serialize(array(
1334                                        'msg'    => lang('Problems on adding your Contact. No Contact added!'),
1335                                        'status' => 'error'
1336                                ));
1337                        }
1338
1339                        $this->save_session(); 
1340                       
1341                }
1342
1343                /*!
1344               
1345                        @function add_group
1346                        @abstract Adds a new Group using the Add Group interface
1347                        @author Nilton Emilio Buhrer Neto
1348                       
1349                        @param string $sdata Serialized data
1350                */
1351                function add_group($sdata)
1352                {
1353                        $sdata = str_replace('\\"', '"', $sdata);
1354                        $tdata = unserialize($sdata);
1355                        $new_tdata = array();
1356                       
1357                        if (!$tdata)
1358                        {
1359                                echo serialize(array(
1360                                        'msg'    => lang('Problems on adding your Contact. Invalid Data came from client. No Contact added!'),
1361                                        'status' => 'abort'
1362                                ));
1363                               
1364                                return;
1365                        }
1366                       
1367                        foreach($tdata as $tmp)
1368                                $new_tdata[] = $tmp;
1369                       
1370                        $data['title'] = $new_tdata[0];                 
1371                        $data['contact_in_list'] = $new_tdata[1];
1372                        $data['id_group'] = $new_tdata[2];
1373                                               
1374                        $boGroup = CreateObject('contactcenter.bo_group_manager');
1375                        $id = $boGroup -> add_group($data);
1376                       
1377                        if ($id)
1378                        {
1379                                $this->page_info['changed'] = true;
1380                               
1381                                echo serialize(array(
1382                                        'msg'    => lang('Entry added with success!'),
1383                                        'status' => 'ok'
1384                                ));
1385                        }
1386                        else
1387                        {
1388                                echo serialize(array(
1389                                        'msg'    => lang('Problems on adding your Contact. No Contact added!'),
1390                                        'status' => 'error'
1391                                ));
1392                        }
1393
1394                        $this->save_session();
1395                }
1396
1397                /*!
1398               
1399                        @function remove_group
1400                        @abstract Removes a group if the user has the right to do it
1401                        @author Nilton Emilio Buhrer Neto                       
1402                        @param (integer) $id The id to be removed
1403                               
1404                */
1405                function remove_group($id)
1406                {                               
1407                                $soGroup = CreateObject('contactcenter.so_group');                             
1408                                $data = array ('id_group' => $id);
1409                                if($soGroup -> delete($data)) {
1410                                        echo serialize(array(
1411                                                'msg'    => lang('Removed Entry ID '.$id.'!'),
1412                                                'status' => 'ok'
1413                                        ));                                                     
1414                                }
1415                                else {
1416                                        echo serialize(array(
1417                                                'msg'    => lang('Problems on adding your Contact. No Contact added!'),
1418                                                'status' => 'error'
1419                                        ));
1420                                }
1421
1422                        $this->save_session();                         
1423                }
1424
1425
1426                function remove_all_entries (){
1427               
1428                        $error = false;
1429                        $this->all_entries = $this->bo->catalog->get_all_entries_ids();
1430
1431                        foreach($this->all_entries as $index => $id) {
1432                                $result = $this->bo->catalog->remove_single_entry($id);
1433                                if(!$result) {
1434                                        $error = true;
1435                                        break;         
1436                                }
1437                        }
1438
1439                        if(!$error) {
1440                                echo serialize(array(
1441                                        'msg'    => lang('Removed Entry ID '.$id.'!'),
1442                                        'status' => 'ok'
1443                                ));
1444                        }
1445                        else {
1446                                echo serialize(array(
1447                                        'msg'    => lang('Couldn\'t remove this entry. Inform the Site Admin!'),
1448                                        'status' => 'fail'
1449                                ));
1450                        }
1451
1452                        $this->save_session();
1453                }
1454
1455                /*!
1456               
1457                        @function remove_entry
1458                        @abstract Removes an entry if the user has the right to do it
1459                        @author Raphael Derosso Pereira
1460                       
1461                        @param (integer) $id The id to be removed
1462                               
1463                */
1464                function remove_entry ($id)
1465                {
1466                        if (!is_int($id))
1467                        {
1468                                echo lang('Couldn\'t remove entry! Problem passing data to the server. Please inform admin!');
1469                                return;
1470                        }
1471                       
1472                        $this->page_info['changed'] = true;
1473                        $result = $this->bo->catalog->remove_single_entry($id);
1474                       
1475                        if ($result)
1476                        {
1477                                if ($pos = array_search($id, $this->page_info['actual_entries']))
1478                                {
1479                                        unset($this->page_info['actual_entries'][$pos]);
1480                                }
1481                               
1482                                $temp = false;
1483                                reset($this->page_info['actual_entries']);
1484                                foreach($this->page_info['actual_entries'] as $t)
1485                                {
1486                                        $temp[] = $t;
1487                                }
1488                               
1489                                $this->page_info['actual_entries'] = $temp;
1490
1491                                echo serialize(array(
1492                                        'msg'    => lang('Removed Entry ID '.$id.'!'),
1493                                        'status' => 'ok'
1494                                ));
1495                        }
1496                        else
1497                        {
1498                                echo serialize(array(
1499                                        'msg'    => lang('Couldn\'t remove this entry. Inform the Site Admin!'),
1500                                        'status' => 'fail'
1501                                ));
1502                        }
1503                       
1504                        $this->save_session();
1505                }
1506
1507               
1508                /*!
1509               
1510                        @function post_full_add
1511                        @abstract Saves all the information altered/entered in the Full Add
1512                                window
1513                        @author Raphael Derosso Pereira
1514
1515                */
1516                function post_full_add()
1517                {
1518                        $data =  $_POST['data'];
1519                        // Exceptions!!! utf8 special chars.
1520                        $data = preg_replace("/\%u2(\d+)(\d+)(\d+)/","-",$data);
1521                        $data = unserialize(str_replace('\\"', '"', $data));
1522                        $this -> bo -> catalog = CreateObject('contactcenter.bo_people_catalog');
1523
1524                        if (!is_array($data))
1525                        {
1526                                echo serialize(array(
1527                                        'msg' => lang('<p>Some problem receiving data from browser. This is probably a bug in ContactCenter<br>'.
1528                                                  'Please go to eGroupWare Bug Reporting page and report this bug.<br>'.
1529                                                          'Sorry for the inconvenient!<br><br>'.
1530                                                          '<b><i>ContactCenter Developer Team</i></b></p>'),
1531                                        'status' => 'fatal'
1532                                ));
1533                                return;
1534                        }
1535//                      print_r($data);
1536//                      echo '<br><br>';
1537
1538                        $replacer = $data['commercialAnd'];
1539                        unset($data['commercialAnd']);
1540                        if (!is_string($replacer) or strpos($replacer, "'") or strpos($replacer, '"'))
1541                        {
1542                                echo serialize(array(
1543                                        'msg' => lang('Invalid \'&\' replacer! This may be an attempt to bypass Security! Action aborted!'),
1544                                        'status' => 'fatal'
1545                                ));
1546                               
1547                                return;
1548                        }
1549
1550                        if ($data['id_contact'])
1551                        {
1552                                $id = $data['id_contact'];
1553                                $id_photo = $id;
1554                                unset($data['id_contact']);
1555                        }
1556                        else
1557                        {
1558                                $id_photo = '_new_';
1559                        }
1560                       
1561                        /*
1562                         * Process Photo, if available
1563                         */
1564                        $sleep_count = 0;
1565                        $photo_ok = $GLOBALS['phpgw']->session->appsession('ui_data.photo','contactcenter');
1566                        while($photo_ok[0]{0} !== 'o' and $photo_ok[1]{0} === 'y')
1567                        {
1568                                sleep(1);
1569                                $photo_ok = $GLOBALS['phpgw']->session->appsession('ui_data.photo','contactcenter');
1570                                $sleep_count++;
1571
1572                                if ($sleep_count > 35)
1573                                {
1574                                        // TODO
1575                                        return;
1576                                }
1577                        }
1578                        $GLOBALS['phpgw']->session->appsession('ui_data.photo','contactcenter', array('wait', 'n'));
1579                       
1580                        if (isset($this->page_info['photos'][$id_photo]))
1581                        {
1582                                if (array_search($this->page_info['photos'][$id_photo]['status'], array('changed', 'sync')) === false)
1583                                {
1584                                        echo serialize(array(
1585                                                'msg' => $this->page_info['photos'][$id_photo]['msg'],
1586                                                'status' => $this->page_info['photos'][$id_photo]['status']
1587                                        ));
1588
1589                                        return;
1590                                }
1591
1592                                $data['photo'] = $this->page_info['photos'][$id_photo]['content'];
1593                                unset($this->page_info['photos'][$id_photo]);
1594                                $this->save_session();
1595                        }
1596                       
1597                        /*
1598                         * Arrange Date so it gets inserted correctly
1599                         */
1600                         
1601                        $dateformat = $GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'];
1602               
1603                        $j = 0;
1604                        for ($i = 0; $i < 5; $i+=2)
1605                        {
1606                                switch($dateformat{$i})
1607                                {
1608                                        case 'Y':
1609                                                $date[$j]['size'] = 4;
1610                                                $date[$j]['digit'] = 'Y';
1611                                                break;
1612
1613                                        case 'm':
1614                                        case 'M':
1615                                                $date[$j]['size'] = 2;
1616                                                $date[$j]['digit'] = 'M';
1617                                                break;
1618
1619                                        case 'd':
1620                                                $date[$j]['size'] = 2;
1621                                                $date[$j]['digit'] = 'D';
1622                                }
1623                                $j++;
1624                        }
1625                        $datecount = 0;
1626
1627                        /* Verify Data and performs insertion/update */
1628                        foreach($data as $field => $value)
1629                        {
1630                                if ($value == '' or is_null($value))
1631                                {
1632                                        unset($data[$field]);
1633                                        continue;
1634                                }
1635                               
1636                                switch($field)
1637                                {
1638                                        case 'alias':
1639                                        case 'given_names':
1640                                        case 'family_names':
1641                                        case 'names_ordered':
1642                                        case 'pgp_key':
1643                                        case 'notes':
1644                                        case 'photo':
1645                                                /* Do Nothing. This is just to make sure no invalid field is passed */
1646                                                break;
1647                                       
1648                                        case 'id_status':
1649                                        case 'id_prefix':
1650                                        case 'id_suffix':
1651                                                if ($data[$field] == 0)
1652                                                {
1653                                                        unset($data[$field]);
1654                                                }
1655                                                break;
1656                                       
1657                                        case 'birthdate_0':
1658                                        case 'birthdate_1':
1659                                        case 'birthdate_2':
1660                                       
1661                                                switch($date[$datecount]['digit'])
1662                                                {
1663                                                        case 'Y':
1664                                                                $date['value'][2] = (int) $data[$field];
1665                                                                break;
1666
1667                                                        case 'M':
1668                                                                $date['value'][0] = (int) $data[$field];
1669                                                                break;
1670
1671                                                        case 'D':
1672                                                                $date['value'][1] = (int) $data[$field];
1673                                                                break;
1674                                                }
1675                                                unset($data[$field]);
1676                                                $datecount++;
1677
1678                                                if ($datecount != 3)
1679                                                {
1680                                                        break;
1681                                                }
1682                                               
1683                                                if (!checkdate($date['value'][0], $date['value'][1], $date['value'][2]))
1684                                                {
1685                                                        echo serialize(array(
1686                                                                'msg' => lang('Invalid Date'),
1687                                                                'status' => 'invalid_data'
1688                                                        ));
1689                                                        return;
1690                                                }
1691                                               
1692                                                $data['birthdate'] = $date['value'][2].'-'.$date['value'][0].'-'.$date['value'][1];
1693                                                break;
1694
1695                                        case 'sex':
1696                                                if ($data[$field] !== 'M' and $data[$field] !== 'F')
1697                                                {
1698                                                        echo serialize(array(
1699                                                                'msg' => lang('Invalid Sex'),
1700                                                                'status' => 'invalid_data'
1701                                                        ));
1702                                                        return;
1703                                                }
1704                                                break;
1705
1706
1707                                        case 'addresses':
1708                                                /* Insert new cities/states */
1709                                                if (isset($value['new_states']))
1710                                                {
1711                                                        foreach($value['new_states'] as $type => $state_info)
1712                                                        {
1713                                                                $index = 'address'.$type;
1714                                                               
1715                                                                $id_state = $this->bo->catalog->add_state($state_info);
1716                                                                $data['addresses'][$index]['id_state'] = $id_state;
1717
1718                                                                if ($value['new_cities'][$type])
1719                                                                {
1720                                                                        $value['new_cities'][$type]['id_state'] = $id_state;
1721                                                                }
1722                                                        }
1723
1724                                                        unset($data['addresses']['new_states']);
1725                                                }
1726
1727                                                if (isset($value['new_cities']))
1728                                                {
1729                                                        foreach($value['new_cities'] as $type => $city_info)
1730                                                        {
1731                                                                $index = 'address'.$type;
1732                                                               
1733                                                                $id_city = $this->bo->catalog->add_city($city_info);
1734                                                                $data['addresses'][$index]['id_city'] = $id_city;
1735                                                        }
1736
1737                                                        unset($data['addresses']['new_cities']);
1738                                                }
1739
1740                                        break;
1741
1742                                        case 'connections':
1743                                                /* Does nothing... */
1744                                                break;
1745
1746                                        default:
1747                                                echo serialize(array(
1748                                                        'msg' => lang('Invalid field: ').$field,
1749                                                        'status' => 'invalid_data'
1750                                                ));
1751                                                return;
1752                                }
1753                        }
1754
1755                        $code = '$id = $this->bo->catalog->';
1756
1757                        if (!is_null($id) and $id !== '')
1758                        {
1759                                $code .= $code.'update_single_info($id, $data);';
1760                                $result = array(
1761                                        'msg' => lang('Updated Successfully!'),
1762                                        'status' => 'ok'
1763                                );
1764                        }
1765                        else
1766                        {
1767                                $code .= 'add_single_entry($data);';
1768                                $result = array(
1769                                        'msg' => lang('Entry Added Successfully!'),
1770                                        'status' => 'ok'
1771                                );
1772                        }
1773                       
1774                        eval($code);
1775
1776                        if (!($id))
1777                        {
1778                                $result = array(
1779                                        'msg' => lang('Some problem occured when trying to insert/update contact information.<br>'.
1780                                                   'Report the problem to the Administrator.'),
1781                                        'status' => 'fail'
1782                                );
1783                        }
1784
1785                        echo serialize($result);
1786                }
1787
1788                /*!
1789               
1790                        @function post_photo
1791                        @abstract Wrapper to post a photo without reload a page.
1792                        @author Raphael Derosso Pereira
1793
1794                */
1795                function post_photo($id)
1796                {
1797                        //print_r($_FILES);
1798                        $GLOBALS['phpgw']->session->appsession('ui_data.photo','contactcenter', array('wait', 'y'));
1799                       
1800                        if (!is_array($_FILES) and is_array(!$_FILES['cc_pd_photo']))
1801                        {
1802                                $this->page_info['photos'][$id]['status'] = 'no_upload';
1803                                $this->page_info['photos'][$id]['msg'] = lang('No Photos uploaded to Server.');
1804                               
1805                                $this->save_session();
1806                                $GLOBALS['phpgw']->session->appsession('ui_data.photo','contactcenter', array('ok', 'y'));
1807                                return;
1808                        }
1809
1810                        if (!function_exists('imagecreate'))
1811                        {
1812                                $this->page_info['photos'][$id]['status'] = 'no_GD_lib';
1813                                $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.');
1814                               
1815                                $this->save_session();
1816                                $GLOBALS['phpgw']->session->appsession('ui_data.photo','contactcenter', array('ok', 'y'));
1817                                return;
1818                        }
1819
1820                        // TODO: Get Max Size from preferences!
1821                        if ($_FILES['cc_pd_photo']['size'] > 1000000)
1822                        {
1823                                $this->page_info['photos'][$id]['status'] = 'too_large';
1824                                $this->page_info['photos'][$id]['msg'] = lang('Image too large! ContactCenter limits the image size to 1 Mb');
1825
1826                                $this->save_session();
1827                                $GLOBALS['phpgw']->session->appsession('ui_data.photo','contactcenter', array('ok', 'y'));
1828                                return;
1829                        }
1830
1831                        if ($_FILES['cc_pd_photo']['error'])
1832                        {
1833                                $this->page_info['photos'][$id]['status'] = 'error';
1834                                $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'];
1835
1836                                $this->save_session();
1837                                $GLOBALS['phpgw']->session->appsession('ui_data.photo','contactcenter', array('ok', 'y'));
1838                                return;
1839                        }
1840                       
1841                        switch($_FILES['cc_pd_photo']['type'])
1842                        {
1843                                case 'image/jpeg':
1844                                case 'image/pjpeg':
1845                                        $src_img = imagecreatefromjpeg($_FILES['cc_pd_photo']['tmp_name']);
1846                                        if ($src_img == '')
1847                                        {
1848                                                $bogus = true;
1849                                        }
1850                                        break;
1851
1852                                case 'image/png':
1853                                case 'image/x-png':
1854                                        $src_img = imagecreatefrompng($_FILES['cc_pd_photo']['tmp_name']);
1855                                        if ($src_img == '')
1856                                        {
1857                                                $bogus = true;
1858                                        }
1859                                        break;
1860
1861                                case 'image/gif':
1862                                        $src_img = imagecreatefromgif($_FILES['cc_pd_photo']['tmp_name']);
1863                                        if ($src_img == '')
1864                                        {
1865                                                $bogus = true;
1866                                        }
1867                                        break;
1868
1869                                default:
1870                                       
1871                                        $this->page_info['photos'][$id]['status'] = 'invalid_image';
1872                                        $this->page_info['photos'][$id]['msg'] = lang('The file must be an JPEG, PNG or GIF Image.');
1873
1874                                        $this->save_session();
1875                                        $GLOBALS['phpgw']->session->appsession('ui_data.photo','contactcenter', array('ok', 'y'));
1876                                        return;
1877                        }
1878
1879                        if ($bogus)
1880                        {
1881                                        $this->page_info['photos'][$id]['status'] = 'invalid_file';
1882                                        $this->page_info['photos'][$id]['msg'] = lang('Couldn\'t open Image. It may be corrupted or internal library doesn\'t support this format.');
1883                                       
1884                                        $this->save_session();
1885                                        $GLOBALS['phpgw']->session->appsession('ui_data.photo','contactcenter', array('ok', 'y'));
1886                                        return;
1887                        }
1888                       
1889                        $img_size = getimagesize($_FILES['cc_pd_photo']['tmp_name']);
1890                        $dst_img = imagecreatetruecolor(60, 80);
1891                       
1892                        if (!imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, 60, 80, $img_size[0], $img_size[1]))
1893                        {
1894                                $this->page_info['photos'][$id]['status'] = 'invalid_file';
1895                                $this->page_info['photos'][$id]['msg'] = lang('Couldn\'t open Image. It may be corrupted or internal library doesn\'t support this format.');
1896                               
1897                                $this->save_session();
1898                                $GLOBALS['phpgw']->session->appsession('ui_data.photo','contactcenter', array('ok', 'y'));
1899                                return;
1900                        }
1901                       
1902                        ob_start();
1903                        imagepng($dst_img);
1904                        $this->page_info['photos'][$id]['content'] = ob_get_contents();
1905                        ob_end_clean();
1906
1907                        $this->page_info['photos'][$id]['status'] = 'changed';
1908                        $this->page_info['photos'][$id]['msg'] = lang('Photo Successfully Updated!');
1909
1910                        $this->save_session();
1911               
1912                        $GLOBALS['phpgw']->session->appsession('ui_data.photo','contactcenter', array('ok', 'y'));
1913
1914                        imagedestroy($src_img);
1915                        imagedestroy($dst_img);
1916                        echo 'ok';
1917                        return;
1918                }
1919
1920
1921                /*!
1922
1923                        @function get_photo
1924                        @abstract Returns the photo to the browser
1925                        @author Raphael Derosso Pereira
1926
1927                */
1928                function get_photo($id)
1929                {
1930                        $fields = $this->bo->catalog->get_fields(false);
1931                        $fields['photo'] = true;
1932                       
1933                        $contact = $this->bo->catalog->get_single_entry($id, $fields);
1934
1935                        if (!$contact['photo'])
1936                        {
1937                                header('Content-type: image/png');
1938                                echo file_get_contents(PHPGW_INCLUDE_ROOT.'/contactcenter/templates/default/images/photo_celepar.png');
1939                                return;
1940                        }
1941                       
1942                        header('Content-type: image/jpeg');
1943                        $photo = imagecreatefromstring ($contact['photo']);
1944                        $width = imagesx($photo);
1945                        $height = imagesy($photo);
1946                        $twidth = 70;
1947                        $theight = 90;
1948                        $small_photo = imagecreatetruecolor ($twidth, $theight);
1949                        imagecopyresampled($small_photo, $photo, 0, 0, 0, 0,$twidth, $theight, $width, $height);
1950                        imagejpeg($small_photo,"",100);
1951                        return;
1952                }
1953               
1954                /*!
1955               
1956                        @function get_states
1957                        @abstract Echos a serialized array containing all the states for the given country
1958                        @author Raphael Derosso Pereira
1959
1960                        @params $id_country The ID of the Country that contains the requested states
1961
1962                */
1963                function get_states($id_country)
1964                {
1965                        $states = $this->bo->catalog->get_all_states($id_country);
1966
1967                        if (!$states)
1968                        {
1969                                $result = array(
1970                                        'msg'    => lang('No States found for this Country.'),
1971                                        'status' => 'empty'
1972                                );
1973
1974                                echo serialize($result);
1975                                return;
1976                        }
1977                       
1978                        $result = array(
1979                                'msg'    => lang('States Successfully retrieved!'),
1980                                'status' => 'ok'
1981                        );
1982                       
1983                        foreach ($states as $state_info)
1984                        {
1985                                $result['data'][$state_info['id_state']] = $state_info['name'];
1986
1987                                if ($state_info['symbol'])
1988                                {
1989                                        $result['data'][$state_info['id_state']] .= ', '.$state_info['symbol'];
1990                                }
1991                        }
1992
1993                        echo serialize($result);
1994                }
1995
1996                /*!
1997
1998                        @function get_cities
1999                        @abstract Echos a serialized array containing all the cities of a given state
2000                        @author Raphael Derosso Pereira
2001
2002                        @param $id_country The ID of the Country that has the specified Cities (in case the
2003                                Country doesn't have any States)
2004                        @param $id_state The ID of the State that has the Cities requested
2005
2006                */
2007                function get_cities($id_country, $id_state=false)
2008                {
2009                        $cities = $this->bo->catalog->get_all_cities($id_country, $id_state);
2010
2011                        if (!$cities)
2012                        {
2013                                $result = array(
2014                                        'msg'    => lang('No Cities found for this State.'),
2015                                        'status' => 'empty'
2016                                );
2017
2018                                echo serialize($result);
2019                                return;
2020                        }
2021                       
2022                        $result = array(
2023                                'msg'    => lang('Cities Successfully retrieved!'),
2024                                'status' => 'ok'
2025                        );
2026                       
2027                        foreach ($cities as $city_info)
2028                        {
2029                                $result['data'][$city_info['id_city']] = $city_info['name'];
2030                        }
2031                       
2032                        echo serialize($result);
2033                }
2034
2035
2036                /*!
2037               
2038                        @function search
2039                        @abstract Echos a serialized array containing the IDs
2040                                of the entries that matches the search argument
2041                        @author Raphael Derosso Pereira
2042
2043                        @param string $str_data A serialized array with two informations:
2044                                $data = array(
2045                                        'search_for' => (string),
2046                                        'recursive'  => (boolean),
2047                                );
2048
2049                */
2050                function search($str_data)
2051                {
2052                        $data = unserialize($str_data);
2053
2054                        if (!is_array($data) || !$data['search_for'] || !is_array($data['fields']))
2055                        {
2056                                echo serialize(array(
2057                                        'msg'    => lang('Invalid parameters'),
2058                                        'status' => 'abort'
2059                                ));
2060
2061                                return;
2062                        }
2063
2064                        $rules  = array();
2065
2066                        if ($data['search_for'] === '*')
2067                        {
2068                                $rules = array(
2069                                        0 => array(
2070                                                'field' => $data['fields']['search'],
2071                                                'type'  => 'LIKE',
2072                                                'value' => '%'
2073                                        )
2074                                );
2075                        }
2076                        else
2077                        {
2078                                $names = explode(' ', $data['search_for']);
2079
2080                                if (!is_array($names))
2081                                {
2082                                        echo serialize(array(
2083                                                'msg'    => lang('Invalid Search Parameter'),
2084                                                'status' => 'abort'
2085                                        ));
2086                                       
2087                                        return;
2088                                }
2089                               
2090                                foreach ($names as $name)
2091                                {
2092                                        if ($name != '')
2093                                        {
2094                                                array_push($rules, array(
2095                                                        'field' => $data['fields']['search'],
2096                                                        'type'  => 'iLIKE',
2097                                                        'value' => '%'.$name.'%'
2098                                                ));
2099                                        }
2100                                }
2101                        }
2102                       
2103                        /*
2104                        $catalog = $this->bo->get_branch_by_level($this->bo->catalog_level[0]);
2105                       
2106                        if ($catalog['class'] === 'bo_people_catalog')
2107                        {
2108                                array_push($rules, array(
2109                                        'field' => 'contact.id_owner',
2110                                        'type'  => '=',
2111                                        'value' => $GLOBALS['phpgw_info']['user']['account_id']
2112                                ));
2113                        }
2114                        */
2115                       
2116                        $ids = $this->bo->find(array($data['fields']['id'], $data['fields']['search']), $rules, array('order' => $data['fields']['search'], 'sort' => 'ASC'));
2117
2118                        if (!is_array($ids) || !count($ids))
2119                        {
2120                                echo serialize(array(
2121                                        'msg'    => lang('No Entries Found!'),
2122                                        'status' => 'empty'
2123                                ));
2124
2125                                return;
2126                        }
2127
2128                        $id_field = substr($data['fields']['id'], strrpos($data['fields']['id'], '.')+1);
2129
2130                        $ids_f = array();
2131                        foreach ($ids as $e_info)
2132                        {
2133                                $ids_f[] = $e_info[$id_field];
2134                        }
2135                       
2136                        echo serialize(array(
2137                                'data'   => $ids_f,
2138                                'msg'    => lang('Found %1 Entries', count($ids)),
2139                                'status' => 'ok'
2140                        ));
2141                }
2142
2143                /*!
2144
2145                        @function get_multiple_entries
2146                        @abstract Returns an array containing the specifiend data in the default
2147                                CC UI format
2148                        @author Raphael Derosso Pereira
2149
2150                        @param array str_data A serialized array containing the ID's of the entries
2151                                to be taken, the fields to be taken and the rules to be used on the
2152                                retrieval:
2153                                $data = array(
2154                                        'ids'    => array(...),
2155                                        'fields' => array(...),
2156                                        'rules'  => array(...)
2157                                );
2158
2159                */
2160                function get_multiple_entries($str_data)
2161                {
2162                        $data = unserialize($str_data);
2163                       
2164                        if (!is_array($data) or !count($data) or !count($data['fields']) or !count($data['ids']))
2165                        {
2166                                return array(
2167                                        'msg'    => lang('Invalid Parameters'),
2168                                        'status' => 'abort'
2169                                );
2170                        }
2171
2172                        $entries = $this->bo->catalog->get_multiple_entries($data['ids'], $data['fields']);
2173                       
2174                        if (!is_array($entries) or !count($entries))
2175                        {
2176                                return array(
2177                                        'msg'    => lang('No Entries Found!'),
2178                                        'status' => 'empty'
2179                                );
2180                        }
2181
2182                        return array(
2183                                'msg'    => lang('Found %1 Entries!', count($entries)),
2184                                'status' => 'ok',
2185                                'data'   => $entries
2186                        );
2187                }
2188
2189                /*
2190
2191                        @function get_all_entries
2192                        @abstract Returns the specified fields for all catalog's entries
2193                                in the default CC UI format
2194                        @author Raphael Derosso Pereira
2195
2196                        @params array str_data A serialized array containing the fields to
2197                                be grabbed, the maximum number of entries to be returned and a
2198                                boolean specifying if the calls refers to a new grab or to an
2199                                unfinished one.
2200
2201                */
2202                function get_all_entries($str_data)
2203                {
2204                        $data = unserialize($str_data);
2205                       
2206                        if (!is_array($data) or
2207                            !count($data) or
2208                                !count($data['fields']) or
2209                                !$data['maxlength'] or
2210                                (!$data['new'] and !$data['offset']))
2211                        {
2212                                return array(
2213                                        'msg'    => lang('Invalid Parameters'),
2214                                        'status' => 'abort'
2215                                );
2216                        }
2217
2218                        if ($data['new'])
2219                        {
2220                                $this->all_entries = $this->bo->catalog->get_all_entries_ids();
2221
2222                                $this->save_session();
2223
2224                                if (!is_array($this->all_entries) or !count($this->all_entries))
2225                                {
2226                                        return array(
2227                                                'msg'    => lang('No Entries Found!'),
2228                                                'status' => 'empty'
2229                                        );
2230                                }
2231
2232                                $data['offset'] = 0;
2233                        }
2234                       
2235                        if ($data['maxlength'] != -1)
2236                        {
2237                                $result = $this->bo->catalog->get_multiple_entries(array_slice($this->all_entries, $data['offset'], $data['maxlength']), $data['fields']);
2238                        }
2239                        else
2240                        {
2241                                $result = $this->bo->catalog->get_multiple_entries($this->all_entries, $data['fields']);
2242                        }
2243
2244                        $prefs = ExecMethod('contactcenter.ui_preferences.get_preferences');
2245                       
2246                        $jsCode = array();
2247                        $count = 0;
2248                        foreach ($result as $each)
2249                        {
2250                                if (!is_array($each))
2251                                {
2252                                        continue;
2253                                }
2254
2255                                if($this-> typeContact == 'groups') {                           
2256                                       
2257                                        foreach ($each as $field => $value)     {
2258                                                                               
2259                                                if ($field === 'title') {
2260                                                        $optionName = '\\"'.$value.'\\"';
2261                                                         
2262                                                }
2263                                                else if ($field === 'short_name')       {
2264                                                       
2265                                                        $jsCode[] = '_this.entries.options[_this.entries.options.length] = new Option("'.$optionName.' ('.$value.')", "'.$count.'");';
2266                                                        $count++;                                                                                                                                                                       
2267                                                }                                       
2268                                        }
2269                                }
2270                               
2271                                else  {
2272                                        foreach ($each as $field => $value)     {
2273                                                if ($field === 'names_ordered') {
2274                                                         if(is_array($value))
2275                                $value = $value[0];
2276                                                        $name = '\\"'.$value.'\\"';
2277                                                }
2278                                                else if ($field === 'connections')      {
2279                                                       
2280                                                        foreach ($value as $connection)         {
2281                                                                if ($connection['id_type'] == $prefs['personCardEmail'])        {
2282                                                                        $jsCode[] = '_this.entries.options[_this.entries.options.length] = new Option("'.$name.' <'.$connection['connection_value'].'>", "'.$count.'");';
2283                                                                        $count++;
2284                                                                }
2285                                                        }
2286                                                }
2287                                        }
2288                                }
2289                        }
2290
2291                        $jsCodeFinal = implode("\n", $jsCode);
2292                       
2293                        $nEntries = count($result);
2294                       
2295                        if (!$nEntries)
2296                        {
2297                                return array(
2298                                        'msg'    => lang('Error while getting user information...'),
2299                                        'status' => 'abort'
2300                                );
2301                        }
2302
2303                        return array(
2304                                'msg'      => lang('Found %1 Entries!', $nEntries),
2305                                'status'   => 'ok',
2306                                'typeContact'   => $this -> typeContact,
2307                                'final'    => $nEntries + $data['offset'] < count($this->all_entries) ? false : true,
2308                                'offset'   => $data['offset'] + $nEntries,
2309                                'data'     => $jsCodeFinal
2310                        );
2311                }
2312               
2313                /*********************************************************************\
2314                 *                      Auxiliar Methods                             *
2315                \*********************************************************************/
2316
2317                /*!
2318               
2319                        @function save_session
2320                        @abstract Saves the data on the session
2321                        @author Raphael Derosso Pereira
2322               
2323                */
2324                function save_session()
2325                {
2326                        $GLOBALS['phpgw']->session->appsession('ui_data.page_info','contactcenter',$this->page_info);
2327                        $GLOBALS['phpgw']->session->appsession('ui_data.all_entries','contactcenter',$this->all_entries);
2328                }
2329
2330                /*!
2331               
2332                        @function convert_tree
2333                        @abstract Converts the tree array in the BO format to a JS tree array compatible
2334                                with the one available in eGW
2335                        @author Raphael Derosso Pereira
2336               
2337                        @param (array)  $tree    The tree in the BO format
2338                        @param (string) $name    The tree name
2339                        @param (string) $iconDir The dir where the icons are
2340                        @param (string) $parent  The parent
2341                */
2342
2343                function convert_tree($tree, &$iconDir, $parent='0')
2344                {
2345//                      echo "Entrou<br>\tPai: $parent <br>";
2346                        $rtree = array();
2347
2348                        if ($parent === '0')
2349                        {
2350//                              echo 'Root!<br>';
2351                                $rtree['0'] = array(
2352                                        'type'       => 'catalog_group',
2353                                        'id'         => '0',
2354                                        'pid'        => 'none',
2355                                        'caption'    => lang('Catalogues'),
2356                                        'class'      => 'bo_catalog_group_catalog',
2357                                        'class_args' => array('_ROOT_', '$this', '$this->get_branch_by_level($this->catalog_level[0])')
2358                                );
2359                        }
2360
2361                        foreach($tree as $id => $value)
2362                        {
2363//                              echo 'ID: '.$id.'<br>';
2364                                $rtree[$parent.'.'.$id] = array(
2365                                        'type'    => $value['type'],
2366                                        'id'      => $parent.'.'.$id,
2367                                        'pid'     => $parent,
2368                                        'caption' => $value['name']
2369                                );
2370                               
2371                                switch($value['type'])
2372                                {
2373                                        case 'catalog_group':
2374                                        case 'mixed_catalog_group':
2375                                                $rtree = $rtree + $this->convert_tree($value['sub_branch'],$iconDir,$parent.'.'.$id);
2376                                                break;
2377                                }
2378                        }
2379
2380                        if (count($rtree))
2381                        {
2382                                return $rtree;
2383                        }
2384                }
2385
2386                function get_catalog_add_contact($id){
2387
2388                        $array_participants = array();
2389                        if(!$this->bo->catalog->src_info) {
2390                                $ldap = CreateObject('contactcenter.bo_ldap_manager');
2391                                $this->bo->catalog->src_info = $ldap->srcs[1];
2392                        }
2393                       
2394                        $ds = $GLOBALS['phpgw']->common->ldapConnect($this->bo->catalog->src_info['host'], $this->bo->catalog->src_info['acc'], $this->bo->catalog->src_info['pw'], true);                             
2395                        $dn=$this->bo->catalog->src_info['dn'];
2396                        $justThese = array("givenname","givenname","sn","telephonenumber","mail");
2397                        $sr = ldap_read($ds,$id, "objectClass=*",$justThese);                                                   
2398                        $info = ldap_get_entries($ds, $sr);                                                     
2399                        for($z = 0; $z < 5; $z++) {
2400                                $participant = $info[0][$justThese[$z]];                                                                                                       
2401                                array_push($array_participants, $participant);
2402                        }
2403
2404                        ldap_close($ds);
2405                        echo serialize($array_participants);
2406                }       
2407               
2408                function get_catalog_participants_list($id)
2409                {
2410                       
2411                        $fields = $this->bo->catalog->get_fields(false);                       
2412                        $fields['names_ordered'] = true;
2413                        $fields['mail_forwarding_address'] = true;                     
2414                        $contact = $this->bo->catalog->get_single_entry($id,$fields);                   
2415                       
2416                        $array_participants = array();
2417                        $array_emails = array();
2418                       
2419                        $filter = null;
2420                        for($z = 0; $z < $contact['mail_forwarding_address']['count']; $z++) {                 
2421                                        if(strstr($contact['mail_forwarding_address'][$z],'@')) {
2422                                                $filter.="(mail=".$contact['mail_forwarding_address'][$z].")";
2423                                                $array_emails[$contact['mail_forwarding_address'][$z]] = "<font color=black>".$contact['mail_forwarding_address'][$z]."</font>";
2424                                        }                                                                                                                               
2425                                        else
2426                                                $array_participants[$z] = "<font color=red>".$contact['mail_forwarding_address'][$z]."</font>";
2427                        }                                       
2428                       
2429                        if($filter) {
2430                                $filter = "(|".$filter.")";                                                     
2431                                if(!$this->bo->catalog->src_info) {
2432                                        $ldap = CreateObject('contactcenter.bo_ldap_manager');
2433                                        $this->bo->catalog->src_info = $ldap->srcs[1];
2434                                }
2435                                $ds = $GLOBALS['phpgw']->common->ldapConnect($this->bo->catalog->src_info['host'], $this->bo->catalog->src_info['acc'], $this->bo->catalog->src_info['pw'], true);                             
2436                                $dn=$this->bo->catalog->src_info['dn'];
2437                                $justThese = array("cn","mail");
2438                                $sr = ldap_search($ds,$dn, $filter,$justThese);                                                         
2439                                $info = ldap_get_entries($ds, $sr);                                                     
2440                                for($z = 0; $z < $info['count']; $z++) {
2441                                        $participant =  '<font color=\'DARKBLUE\'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;'.$info[$z]['cn'][0].'&quot; &lt;'.$info[$z]['mail'][0].'&gt;</font><br>';
2442                                        $array_emails[$info[$z]['mail'][0]] = null;                                                             
2443                                        array_push($array_participants, $participant);                                 
2444                                }
2445                               
2446                                foreach($array_emails as $index => $email)
2447                                        if($email)
2448                                                array_push($array_participants, "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$email."<br>");                         
2449                                                                                               
2450                                ldap_close($ds);
2451                        }
2452                        sort($array_participants);
2453                        $innerHTML = '';                       
2454                        foreach($array_participants as $index => $participant){
2455                                $innerHTML .= $participant;
2456                        }
2457                        $return = array('size' => count($array_participants), 'names_ordered'=> $contact['names_ordered'], 'inner_html' => $innerHTML);                 
2458                        echo serialize($return);
2459                }       
2460
2461                function export_contacts($typeExport){
2462
2463                        $boGroup = CreateObject('contactcenter.bo_group');
2464                        $contacts = $boGroup->get_all_contacts();
2465                        $streamBuffer = '';
2466
2467                        if(!count($contacts))
2468                                echo null;
2469
2470
2471                        // Essa iteração transforma uma esturuta com contatos redundantes e separados por email ou tel.
2472                        // em outra com apenas 1 elemento pra cada contato
2473                        $i=0;
2474                        foreach($contacts as $object){
2475                                $i=($object['id_contact'] - 1);
2476                                $all_contacts[$i]['name']=$object['names_ordered'];
2477                                if ($object['id_typeof_contact_connection'] == 1)
2478                                        $all_contacts[$i]['email']=$object['connection_value'];
2479                                else
2480                                        $all_contacts[$i]['phone']=$object['connection_value'];
2481                        }
2482                       
2483                        switch($typeExport) {
2484
2485                                case 'outlook_en':
2486                                        $streamBuffer = "Name;E-mail Address;Notes;Mobile Phone;Pager;Company;".
2487                                                        "Job Title;Home Phone;Home Fax;Business Phone;Business Fax\r\n";                                                       
2488                                        foreach($all_contacts as $index => $object){
2489                                                $streamBuffer.= $object['name'].";".$object['email'].";;;;;;".$object['phone'].";;;\r\n";
2490                                        }
2491                                break;
2492                               
2493                                case 'outlook_pt-BR':
2494                                        $streamBuffer = "Nome;End. de email;Endereço residencial;".
2495                                        "Cidade do endereço residencial;CEP do endereço residencial;".
2496                                        "Estado;País/região do endereço residencial;Telefone residencial;".
2497                                        "Rua do endereço comercial;Cidade do endereço comercial;".
2498                                        "CEP do endereço comercial;Estado do endereço comercial;".
2499                                        "País/região do endereço comercial;Telefone comercial;Empresa;Cargo\r\n";                                                       
2500                                        foreach($all_contacts as $index => $object){
2501                                                $streamBuffer.= $object['name'].";".$object['email'].";;;;;;".$object['phone'].";;;;;;;;\r\n";
2502                                        }
2503                                break;
2504                               
2505                                case 'outlook2000_pt-BR':
2506                                        $streamBuffer = "\"Tratamento\",\"Nome\",\"Segundo Nome\",\"Sobrenome\",\"Sufixo\",".
2507                                        "\"Empresa\",\"Departamento\",\"Cargo\",\"Rua do endereço comercial\",\"Rua do endereço comercial 2\",".
2508                                        "\"Rua do endereço comercial 3\",\"Cidade do endereço comercial\",\"Estado do endereço comercial\",".
2509                                        "\"CEP do endereço comercial\",\"País do endereço comercial\",\"Endereço residencial\",\"Rua residencial 2\",".
2510                                        "\"Rua residencial 3\",\"Cidade do endereço residencial\",\"Estado\",\"CEP do endereço residencial\",\"País do endereço residencial\",".
2511                                        "\"Outro endereço\",\"Outro endereço 2\",\"Outro endereço 3\",\"Cidade\",\"Estado\",\"CEP\",\"País\",".
2512                                        "\"Telefone do assistente\",\"Fax comercial\",\"Telefone comercial\",\"Telefone comercial 2\",\"Retorno de chamada\",".
2513                                        "\"Telefone do carro\",\"Telefone principal da empresa\",\"Fax residencial\",\"Telefone residencial\",".
2514                                        "\"Telefone residencial 2\",\"ISDN\",\"Telefone celular\",\"Outro fax\",\"Outro telefone\",\"Pager\",\"Telefone principal\",".
2515                                        "\"Radiofone\",\"Telefone TTY/TDD\",\"Telex\",\"Aniversário\",\"Anotações\",\"Caixa postal\",\"Categorias\",".
2516                                        "\"Código da empresa\",\"Código do governo\",\"Cônjuge\",\"Conta\",\"Endereço de correio eletrônico\",".
2517                                        "\"Nome de exibição do correio eletr.\",\"Endereço de correio eletrônico 2\",".
2518                                        "\"Nome de exibição do correio eletr.2\",\"Endereço de correio eletrônico 3\",".
2519                                        "\"Nome de exibição do correio eletr.3\",\"Datas especiais\",\"Disponibilidade da Internet\",".
2520                                        "\"Filhos\",\"Hobby\",\"Idioma\",\"Indicação\",\"Informações para cobrança\",\"Iniciais\",\"Local\",".
2521                                        "\"Nome do assistente\",\"Nome do gerenciador\",\"Página da Web\",\"Palavras-chave\",\"Particular\",\"Personalizado 1\",\"Personalizado 2\",".
2522                                        "\"Personalizado 3\",\"Personalizado 4\",\"Prioridade\",\"Profissão\",\"Quilometragem\",\"Sala\",\"Sensibilidade\",\"Servidor de diretório\",".
2523                                        "\"Sexo\"\r\n";
2524                                        foreach($all_contacts as $index => $object){
2525                                                $streamBuffer.= ",\"".$object['name']."\",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,".$object['phone'].",,,,,,,,,,,,\"".$object['email']."\",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\r\n";
2526                                        }
2527                                break;
2528                               
2529                                case 'outlook2000_en':
2530                                        $streamBuffer = "Title,First Name,Middle Name,Last Name,Suffix,Company,Department,Job Title,".
2531                                        "Business Street,Business Street 2,Business Street 3,Business City,Business State,Business Postal Code,".
2532                                        "Business Country,Home Street,Home Street 2,Home Street 3,Home City,Home State,Home Postal Code,Home Country,".
2533                                        "Other Street,Other Street 2,Other Street 3,Other City,Other State,Other Postal Code,Other Country,".
2534                                        "Assistant's Phone,Business Fax,Business Phone,Business Phone 2,Callback,Car Phone,Company Main Phone,Home Fax,".
2535                                        "Home Phone,Home Phone 2,ISDN,Mobile Phone,Other Fax,Other Phone,Pager,Primary Phone,Radio Phone,TTY/TDD Phone,Telex,".
2536                                        "Account,Anniversary,Assistant's Name,Billing Information,Birthday,Categories,Children,Directory Server,E-mail Address,".
2537                                        "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,".
2538                                        "Gender,Government ID Number,Hobby,Initials,Internet Free Busy,Keywords,Language,Location,Manager's Name,Mileage,Notes,".
2539                                        "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";
2540                                        foreach($all_contacts as $index => $object){
2541                                                $streamBuffer.= ",".$object['name'].",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,".$object['phone'].",,,,,,,,,,,,".$object['email'].",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\r\n";
2542                                        }
2543                                break;
2544                                       
2545                                case 'thunderbird':
2546                                        $streamBuffer = "\n";                                                   
2547                                        foreach($all_contacts as $index => $object){
2548                                                $streamBuffer.= ",,".$object['name'].",,".$object['email'].",,".$object['phone'].",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\r\n";
2549                                        }
2550                                break;
2551                               
2552                                default:
2553                                        $streamBuffer = "Nome,Email,Telefone\r\n";
2554                                        foreach($all_contacts as $index => $object){
2555                                                $streamBuffer.= $object['name'].",".$object['email'].",".$object['phone']."\r\n";
2556                                        }
2557                                break;
2558                               
2559                        }
2560
2561                        $file = "contacts_".md5(microtime()).".swp";
2562                        $tempDir = ini_get("session.save_path");
2563                        $f = fopen($tempDir.'/'.$file,"w");
2564                        if(!$f)
2565                                echo null;
2566               
2567                        fputs($f,$streamBuffer);
2568                        fclose($f);
2569
2570                        echo $tempDir.'/'.$file;
2571                }
2572
2573
2574                function import_contacts($typeImport){
2575                                               
2576                        $delim          = ($typeImport == 'outlook' ? ';' : ',');
2577                       
2578                        if($file = $_SESSION['contactcenter']['importCSV']) {                           
2579                                unset($_SESSION['contactcenter']['importCSV']);                         
2580                                $len = filesize($file);
2581                                $count = 0;
2582                                $return = array('error' => false, '_new' => 0, '_existing' => 0, '_failure' => 0);
2583                                $handle = @fopen($file, "r") or die(serialize($return['error'] = true));                                                       
2584                                $header = @fgetcsv($handle, $len, $delim) or die(serialize($return['error'] = true));
2585                                if(count($header)  < 2 || count($header) > 100) {
2586                                        $return['error'] = true;
2587                                        $return['sizeheader'] = count($header);
2588                                        echo serialize($return);
2589                                        return;
2590                                }
2591
2592                                $boGroup = CreateObject('contactcenter.bo_group');
2593                                $boPeople = CreateObject('contactcenter.bo_people_catalog');
2594                       
2595                                switch($typeImport){
2596                                        case 'outlook2000':
2597                                                $name_pos=1;
2598                                                $email_pos=56;
2599                                                $phone_pos=44;
2600                                                break;
2601                                        case 'outlook':
2602                                                $name_pos=0;
2603                                                $email_pos=1;
2604                                                $phone_pos=7;
2605                                                break;
2606                                        case 'thunderbird':
2607                                                $name_pos=2;
2608                                                $email_pos=4;
2609                                                $phone_pos=7;
2610                                                break;
2611                                        default:
2612                                                $name_pos=0;
2613                                                $email_pos=1;
2614                                                $phone_pos=2;
2615                                }
2616                               
2617                                while (($data = fgetcsv($handle, $len, $delim))) {
2618
2619                                foreach ($header as $key=>$heading)
2620                                $row[$heading]=(isset($data[$key])) ? $data[$key] : '';
2621                               
2622                                        $sdata = array();
2623                                        $full_name  = trim($row[$header[$name_pos]]);
2624                                        $email          = trim($row[$header[$email_pos]]);
2625                                        $phone          = trim($row[$header[$phone_pos]]);
2626                                        $array_name = explode(' ', str_replace('"','',(str_replace('\'','',$full_name))));
2627                                        $sdata['given_names'] = $array_name[0];
2628                                        $array_name[0] = null;
2629                                        $sdata['family_names'] = trim(implode(' ',$array_name));
2630                                        $sdata['connections']['default_email']['connection_name'] = lang('Main');
2631                                        $sdata['connections']['default_email']['connection_value'] = $email;
2632
2633                                        $sdata['connections']['default_phone']['connection_name'] = lang('Main');
2634                                        $sdata['connections']['default_phone']['connection_value'] = $phone;
2635                                       
2636                                        //      verifica se email já existe!
2637                                        $contact = $boGroup->verify_contact($email);
2638
2639                                        if($contact){
2640                                                $return['_existing']++;
2641                                        }                                                                                                               
2642                                        else if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
2643                                                $return['_failure']++;
2644                                        }
2645                                        else if (!$sdata['given_names'] || !$email || !$boPeople ->quick_add($sdata)){                                         
2646                                                $return['_failure']++;                                                 
2647                                        }
2648                                        else
2649                                                $return['_new']++;
2650                        }                               
2651                                fclose($handle);
2652                                unlink($file);
2653                        }
2654                        else
2655                                $return['error'] = true;
2656
2657                        echo serialize($return);
2658                }
2659/*
2660                function convert_tree($tree, $name, &$iconDir, $parent='0')
2661                {
2662                        $new = null;
2663                        $code = null;
2664                       
2665                        if ($parent === '0')
2666                        {
2667
2668                                $code .= $name.".add(new dNode({id:'0', caption: '".lang('Catalogues')."'}),'none');\n";
2669                        }
2670                       
2671                        foreach ($tree as $id => $value)
2672                        {
2673                                $title = $value['name'];
2674
2675                                switch ($value['type'])
2676                                {                                       
2677                                        case 'unknown':
2678                                                $code .= $name.".add(new dNode({id: '{$parent}.{$id}', caption:'{$value['name']}', onFirstOpen: 'getCatalogTree(\\'{$parent}.{$id}\\');', onClick: 'getCatalogTree(\\'{$parent}.{$id}\\'); waitForTree(\\'{$parent}.{$id}\\', 0)'}),'$parent');\n";
2679                                                break;
2680                                       
2681                                        case 'catalog_group':
2682                                                $code .= $name.".add(new dNode({id: '{$parent}.{$id}', caption: '{$value['name']}'}),'$parent');\n";
2683                                                $code .= $this->convert_tree($value['sub_branch'],$name,$iconDir,$parent.'.'.$id);
2684                                                break;
2685
2686                                        case 'mixed_catalog_group':
2687                                                $code .= $name.".add(new dNode({id: '{$parent}.{$id}', caption: '{$value['name']}', onClick: 'setCatalog(\\'{$parent}.{$id}\\')'}),'$parent');\n";
2688
2689                                                $code .= $this->convert_tree($value['sub_branch'],$name,$iconDir,$parent.'.'.$id);
2690                                                break;
2691                                       
2692                                        case 'catalog':
2693                                                $code .= $name.".add(new dNode({id: '{$parent}.{$id}', caption: '{$value['name']}', onClick: 'setCatalog(\\'{$parent}.{$id}\\')'}),'$parent');\n";
2694                                               
2695                                }
2696                        }
2697                       
2698                        return $code;
2699                }
2700*/
2701        }
2702
2703?>
Note: See TracBrowser for help on using the repository browser.