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

Revision 119, 80.2 KB checked in by niltonneto, 16 years ago (diff)

Internacionalização do CC

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