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

Revision 284, 82.7 KB checked in by rafaelraymundo, 16 years ago (diff)

Vide Trac - #197, #166, #198, #199

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