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

Revision 752, 98.0 KB checked in by niltonneto, 15 years ago (diff)

Resolve #453 e #454 (duplicado).

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