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

Revision 38, 75.6 KB checked in by niltonneto, 17 years ago (diff)

Vide change_log.txt

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