source: branches/2.2/mobile/inc/class.ui_mobilecc.inc.php @ 3646

Revision 3646, 26.6 KB checked in by eduardoalex, 13 years ago (diff)

Ticket #1413 - Ajuste para não usar o contato que não possua e-mail

Line 
1<?php
2        class ui_mobilecc{
3               
4                var $nextmatchs;
5                var $bo;
6                var $page_info = array (
7                                'actual_catalog' => false,
8                                'actual_letter' => null,
9                                'actual_max_contacts' => null,
10                                'request_from' => null
11                        );
12               
13                var $public_functions = array(
14                        'index' => true,
15                        'change_max_results' => true,
16                        'change_catalog' => true,
17                        'delete_contacts' => true,
18                        'change_letter' => true,
19                        'choose_contact' => true,
20                        'init_cc' => true,
21                        'contact_view' => true,
22                        'contact_add_edit' => true,
23                        'contact_add' => true,
24                        'contact_edit' => true
25                );
26                var $template;
27               
28                /**
29                 * Construtor...
30                 *
31                 */
32                public function ui_mobilecc() {
33                        $this->template = CreateObject('phpgwapi.Template', PHPGW_SERVER_ROOT . '/mobile/templates/'.$GLOBALS['phpgw_info']['server']['template_set']);
34                        $this->bo = CreateObject('mobile.bo_mobilecc');
35                        $page_info = $GLOBALS['phpgw']->session->appsession('mobilecc.page_info','mobile');
36                       
37                        if($page_info) {
38                                $this->page_info = $page_info;
39                        }
40                        else {
41                                $this->set_page_info_to_default();
42                        }
43                       
44                        //if()
45
46                }
47               
48                private function set_page_info_to_default() { //Valores default para iniciar o módulo de contatos
49                        $this->page_info['actual_catalog'] = 'bo_people_catalog';
50                        $this->page_info['actual_letter'] = 'a';
51                        $this->page_info['actual_max_contacts'] = 10;
52                        $this->page_info['request_from'] = null;
53                }
54               
55                private function save_session() {
56                        $GLOBALS['phpgw']->session->appsession('mobilecc.page_info','mobile',$this->page_info);
57                }
58               
59                public function index($params) {
60                        $GLOBALS['phpgw_info']['mobiletemplate']->set_error_msg($params["error_message"]);
61                        $GLOBALS['phpgw_info']['mobiletemplate']->set_success_msg($params["success_message"]);
62                        $this->contacts_list();
63                }
64               
65                public function change_max_results($params) {
66                        $this->page_info['actual_max_contacts'] = $params['results'];
67                        $this->contacts_list();
68                        $this->save_session();
69                }
70               
71                public function change_letter($params) {
72                        $this->page_info['actual_letter'] = $params['letter'];
73                        $this->page_info['actual_max_contacts'] = 10;
74                        $this->contacts_list();
75                        $this->save_session();
76                }
77               
78                public function change_catalog($params) {
79
80                        $this->page_info['actual_catalog'] = $params['catalog'];
81                        $this->page_info['actual_max_contacts'] = 10;
82                        $this->page_info['actual_letter'] = 'a';
83                       
84                        $this->contacts_list();
85                       
86                        $this->save_session();
87                }
88               
89                /**
90                 * Função de inicio do módulo para escolha de um contato para outro módulo.
91                 *
92                 * @return
93                 * @param $params Object
94                 */
95                public function choose_contact($params) {
96                        $this->set_page_info_to_default();
97                        $this->page_info['request_from'] = $params['request_from']; //Para escolher contato vindo de outro modulo, mudo apenas o request_from
98                        $this->contacts_list();
99                        $this->save_session();
100                }
101               
102                /**
103                 * Função de inicio do módulo de cc
104                 *
105                 * @return
106                 * @param $params Object
107                 */
108                public function init_cc($params) {
109                        $this->set_page_info_to_default();
110                        $this->contacts_list();
111                        $this->save_session();
112                }
113               
114                /**
115                 * Monta a lista de contatos na tela, de acordo com a busca. Se não foi feita
116                 * busca, mostra apenas o formulário para pesquisa.
117                 *
118                 * @return
119                 */
120               
121                function contacts_list() {
122                       
123                        $this->template->set_file(
124                                Array(
125                                        'contacts_list' => 'cc_main.tpl',
126                                        'home_search_bar' => 'search_bar.tpl'
127                                )
128                        );
129                        $this->template->set_block("contacts_list","catalog_row");
130                        $this->template->set_block("contacts_list","main_body");
131                        $this->template->set_block("contacts_list","pagging_block");
132                        $this->template->set_block('home_search_bar','search_bar');
133
134                        //Langs gerais da página
135                        $this->template->set_var("lang_back",lang("back"));
136                        $this->template->set_var("lang_more",lang("more"));
137                        $this->template->set_var("lang_search",lang("search"));
138                        $this->template->set_var("lang_contacts",ucfirst(lang("contacts")));
139                        $this->template->set_var("actual_catalog",$this->page_info["actual_catalog"]);
140                        $this->template->set_var("next_max_results",$this->page_info["actual_max_contacts"]+10);
141                        $this->template->set_var("show_actions",strpos($this->page_info["actual_catalog"],"ldap")===false?
142                                                                                                                "block":"none");
143                        $this->template->set_var("contacts_request_from",
144                                                                                $this->page_info["request_from"]==null?
145                                                                                "none":$this->page_info["request_from"]);
146                       
147                        $this->template->set_var('search',$this->template->fp('out','search_bar'));
148                       
149                        //Combo de catálogos
150                        $catalogs = $this->bo->get_all_catalogs();
151                        foreach($catalogs as $catalog) {
152                                $this->template->set_var("catalog_value",$catalog["catalog"]);
153                                $this->template->set_var("catalog_name",$catalog["label"]);
154                                if($this->page_info['actual_catalog']==$catalog['catalog'])
155                                        $this->template->set_var("selected","selected");
156                                else
157                                        $this->template->set_var("selected"," ");
158                                $this->template->fp("catalogs","catalog_row",true);
159                        }
160                        $this->bo->set_catalog($this->page_info["actual_catalog"]);
161                        $contacts = $this->bo->search($this->page_info["actual_letter"]."%",
162                                                                                        $this->page_info["actual_max_contacts"]);
163                               
164                       
165                        //Letras da paginação
166                        $max_letters = 5;
167                        if ( in_array($this->page_info['actual_letter'],
168                                                  range("a","c"))){ //Letras de A à C iniciam sempre com A             
169       
170                                $this->template->set_var('show_back','none');
171                                $this->template->set_var('show_next','inline');
172                                $first_letter = "a";
173                                $this->template->set_var('href_next',"index.php?menuaction=mobile.".
174                                                                                 "ui_mobilecc.change_letter&letter=f");
175                        }
176                        else if ( in_array($this->page_info['actual_letter'],
177                                                  range("x","z"))) { //Letras de X à Z terminam sempre no Z
178                                $this->template->set_var('show_back','inline');
179                                $this->template->set_var('show_next','none');
180                                $first_letter = "v";
181                                $this->template->set_var('href_back',"index.php?menuaction=mobile.".
182                                                                                 "ui_mobilecc.change_letter&letter=u");
183                                }
184                        else { //Letras do meio
185                                $this->template->set_var('show_back','inline');
186                                $this->template->set_var('show_next','inline');
187                               
188                                $first_letter = chr(ord($this->page_info["actual_letter"])-3);//Inicio 3 letras antes
189                                $last_letter = chr(ord($first_letter)+($max_letters+1));//A ultima é a máxima quantidade de letras mais 1 do next_letter
190                               
191                                $this->template->set_var('href_back',"index.php?menuaction=mobile.".
192                                                                                 "ui_mobilecc.change_letter&letter=".$first_letter);
193                                $this->template->set_var('href_next',"index.php?menuaction=mobile.".
194                                                                                 "ui_mobilecc.change_letter&letter=".$last_letter);
195                                $first_letter++;
196                        }
197                       
198                        for($i=1;$i<=$max_letters;$i++) { //Roda as letras
199                                        $this->template->set_var("href","index.php?menuaction=mobile.".
200                                                                                        "ui_mobilecc.change_letter&letter=".$first_letter);
201                                        $this->template->set_var("letter",strtoupper($first_letter));
202                                        if($first_letter===$this->page_info["actual_letter"])
203                                                $this->template->set_var("class_button","letter-contact-selected");
204                                        else
205                                                $this->template->set_var("class_button","btn_off");
206                                        $this->template->set_var("letter",strtoupper($first_letter));
207                                        $this->template->fp("pagging_letters","pagging_block",true);
208                                        $first_letter++;
209                        }
210                       
211
212                        if($contacts['has_more'])
213                                $this->template->set_var("show_more","block");
214                        else
215                                $this->template->set_var("show_more","none");
216                        unset($contacts['has_more']);
217                                               
218                        $this->template->set_var("contacts",$this->print_contacts($contacts,true,$this->page_info['request_from']));
219                       
220
221                        $GLOBALS['phpgw_info']['mobiletemplate']->set_content($this->template->fp('out','main_body'));
222                }
223               
224                /**
225                 * Remove os contatos selecionados
226                 *
227                 * @return
228                 * @param $contacts Object
229                 * @param $show_checkbox Object[optional]
230                 */
231               
232                function delete_contacts($params) {
233                        $this->bo->set_catalog($params['catalog']);
234                        $status = $this->bo->remove_multiple_entries($params['contacts']);
235                       
236                        $type = $this->page_info['actual_catalog']!=='bo_group_manager'?"contacts":"groups";
237                       
238                        if($status['success'])
239                                header("Location: index.php?menuaction=mobile.ui_mobilecc.index&success_message=".lang("selected $type were removed successfully"));
240                        else
241                                header("Location: index.php?menuaction=mobile.ui_mobilecc.index&error_message=".lang("one or more $type couldnt be removed"));
242                }
243               
244                static function print_contacts($contacts,$show_checkbox=false,$request_from = null) {
245                        $functions = CreateObject('mobile.common_functions');
246                        $p = CreateObject('phpgwapi.Template', PHPGW_SERVER_ROOT . '/mobile/templates/'.$GLOBALS['phpgw_info']['server']['template_set']);
247                                        $p->set_file(
248                                                Array(
249                                                        'cc_t' => 'contacts_list.tpl'
250                                                )
251                                        );
252                        $p->set_block('cc_t', 'rows_contacts');
253                        $p->set_block('cc_t', 'row_contacts');
254                        $p->set_block('cc_t', 'row_groups');
255                        $p->set_block('cc_t', 'no_contacts');
256
257                        $bg = "par";
258                        if(!empty($contacts)) {
259                                foreach($contacts as $id => $contact) {
260
261                                        $p->set_var('show_check',$show_checkbox?"inline":"none");
262                                        $p->set_var('bg',$bg=="par"?$bg="reset-dt":$bg="par");
263                                                                               
264                                        if($contact["catalog"]!=="bo_group_manager") {  //Contatos             
265                                                $id=strpos($contact["catalog"],"ldap")===false?$contact["id_contact"]:$id;
266
267                        $mail = '&nbsp;'; $tel = '&nbsp;';
268                        foreach($contact['connections'] as $key => $conn) {
269
270                            $test = false;
271                            if ($conn['connection_is_default']) {
272                                $test = true;
273                            }
274                           
275                            if (is_array($conn)){
276                                $test = true;
277                            }
278                           
279                            if ( $test) {
280                                if ( ($conn['id_type'] == 1) )
281                                    $mail = $conn['connection_value'];
282                                else if ( ($conn['id_type'] == 2) )
283                                    $tel = $conn['connection_value'];
284                            }
285                                                }
286
287                                               
288                        $cn = is_array($contact["names_ordered"])?$contact["names_ordered"][0]:$contact["names_ordered"];
289                        $vtel = ($tel==null || $tel=='&nbsp;')?"none":"inline";
290                                               
291                                                if(($mail=='&nbsp;' || $mail==null) && isset($request_from))//Se vier de outro módulo e não possuir e-mail, não mostre.
292                                                        continue;
293
294                                                $p->set_var('show_tel',$vtel);
295                                                $p->set_var('email',$mail);
296                                                $p->set_var('tel',$tel);
297                                                $p->set_var('contact_id',$id);
298                                                $p->set_var('lang_tel',lang("tel"));
299                                                $p->set_var('contact_name',$functions->strach_string($cn,17));
300
301                                                $block = "row_contacts";
302                                        }
303                                        else { //Grupos
304                                                $id=$contact["id_group"];
305                                                $mail = $cn = $contact["title"];
306                                                $p->set_var('group_id',$contact["id_group"]);
307                                                $p->set_var('group_name',$contact["title"]);
308                                                $block = "row_groups";
309                                        }
310                                       
311                                        if($request_from==null) {
312                                                $p->set_var('lang_see_details',strtoupper(lang("see details")));
313                                                $cat_encode = urlencode($contact["catalog"]);
314                                                $p->set_var('href_details',"ui_mobilecc.contact_view&id=$id&catalog=".urlencode($contact["catalog"]));
315                                        }
316                                        else {
317                                                $p->set_var('lang_see_details',strtoupper(lang("select")));
318                                                $p->set_var("href_details","ui_mobilemail.add_recipient&mail=$mail&cn=$cn");
319                                        }
320                                       
321                                        $p->fp('rows',$block,True);
322
323                                }
324                               
325                        }
326                        else {
327                                $p->set_var("lang_no_results",lang("no results found"));
328                                $p->parse("rows","no_contacts");
329                        }
330                        return $p->fp('out','rows_contacts');
331                }
332
333                /**
334                 * Show details from contact selected
335                 *
336                 * @param $id int
337                 * @param $catalog String
338                 * @return $contact
339                 */
340                function contact_view($params) {
341
342                        if ( empty($params['id']) || empty($params['catalog']) ){
343                                header('Location: ../mobile/index.php?menuaction=mobile.ui_mobilecc.init_cc');
344                        }
345                       
346                        $this->template->set_file(
347                                Array(
348                                        'cc_v' => 'contact_view.tpl'
349                                )
350                        );
351
352                        if ( isset($params['success'])){
353                                $GLOBALS['phpgw_info']['mobiletemplate']->set_success_msg(lang('contact save successfully'));
354                        }
355
356                        $this->template->set_block('cc_v','body');
357                        $this->template->set_block('cc_v','people');
358                        $this->template->set_block('cc_v','people_ldap');
359                        $this->template->set_block('cc_v','group');
360                        $this->template->set_block('cc_v','group_row');
361                        $this->template->set_block('cc_v','buttom');
362                        $this->template->set_block('cc_v','buttom_use_contact');
363
364                        $email_to = "";
365
366                        switch ($params['catalog']){
367
368                                case 'bo_shared_people_manager';
369                                case 'bo_people_catalog':
370
371                                                                $this->bo->set_catalog($params['catalog']);
372
373                                                                $result = $this->bo->bo->get_single_entry($params['id'], array("given_names"=>true,"names_ordered"=>true,"alias"=>true,"family_names"=>true,"companies"=>true,"relations"=>true,"connections"=>true));
374
375                                                                asort($result['connections']);
376
377                                                                $this->template->set_var('photo', '../index.php?menuaction=contactcenter.ui_data.data_manager&method=get_photo&id='.$id);
378                                                                $this->template->set_var('id',$params['id']);
379                                                                $this->template->set_var('catalog',$params['catalog']);
380                                       
381                                                                $this->template->set_var('cc_name',$result['names_ordered']);
382                                                                $this->template->set_var('lang_title_alias',lang("Alias"));
383                                                                $this->template->set_var('lang_alias',$result['alias']);
384                                                               
385                                                                $this->template->set_var('lang_title_name',lang("Name"));
386                                                                $this->template->set_var('lang_name',$result['given_names']);
387                                                               
388                                                                $this->template->set_var('lang_title_lastname',lang("Family Names"));
389                                                                $this->template->set_var('lang_lastname',$result['family_names']);
390                                                               
391                                                                $var_phone = "";
392                                                                $var_email = "";
393                                                                foreach($result['connections'] as $conn):
394                                                                        if ( $conn['id_type'] == 1 ){
395                                                                                if ( !empty($var_email) )
396                                                                                        $var_email .= ' | ';
397                                                                                $var_email .= $conn['connection_value'];
398                                                                               
399                                                                                if ( empty($email_to) )
400                                                                                        $email_to = $var_email;
401                                                                               
402                                                                        }else if ($conn['id_type'] == 2){
403                                                                                if ( !empty($var_phone))
404                                                                                        $var_phone .= ' | ';
405                                                                                $var_phone .= $conn['connection_value'];
406                                                                        }
407                                                                endforeach;
408                                                               
409                                                                $this->template->set_var('lang_title_email',lang("Email"));
410                                                                $this->template->set_var('lang_email',$var_email);
411                                                               
412                                                                $this->template->set_var('lang_title_phone',lang("Phone"));
413                                                                $this->template->set_var('lang_phone',$var_phone);
414                               
415                                                                $this->template->set_var('lang_edit',lang("edit"));
416                                                               
417                                                                $this->template->parse("row_body","people");
418                                                               
419                                                                if ($params['catalog'] == 'bo_people_catalog')
420                                                                        $this->template->parse("buttom_editar","buttom");
421                                                                       
422                                                        break;
423                                                       
424                                case 'bo_group_manager':
425
426                                                                $this->bo->set_catalog($params['catalog']);
427                                                                $result = $this->bo->bo->get_single_entry($params['id'], array("id_group"=>true,"title"=>true,"short_name"=>true));
428                                                                $data   = $this->bo->bo->get_contacts_by_group($params['id']);
429                                                               
430                                                                $email_to = '<'.$result['short_name'].'>';
431                                                               
432                                                                $this->template->set_var('gr_name', $result['title']);
433                                                                $this->template->set_var('email_to', $email_to);
434
435                                                                $this->template->set_var('lang_title_name',lang("Name"));
436                                                                $this->template->set_var('lang_title_email',lang("Email"));
437                                                               
438                                                                foreach($data as $dados){
439                                                                        $this->template->set_var('lang_name', $dados['names_ordered']);
440                                                                        $this->template->set_var('lang_email', $dados['connection_value']);
441                                                                       
442                                                                        $this->template->fp('group_rows','group_row',True);
443                                                                }
444
445                                                                $this->template->set_var('email_to', $email_to);
446                                                                $this->template->parse("buttom_use","buttom_use_contact");
447                                                               
448                                                                $this->template->parse("row_body","group");
449                                                        break;
450                                               
451                                default:               
452
453                                                                if(strpos($params['catalog'],'bo_global_ldap_catalog')==false){
454
455                                                                        $this->bo->set_catalog($params['catalog']);
456
457                                                                        $fields = $this->bo->bo->get_fields(true);
458                                                                        $result = $this->bo->bo->get_single_entry($params['id'], $fields);
459
460                                                                        $this->template->set_var('photo', '../index.php?menuaction=contactcenter.ui_data.data_manager&method=get_photo&id='.$id);
461
462                                                                        $this->template->set_var('cc_name',$result['names_ordered'][0]);
463
464                                                                        $this->template->set_var('lang_title_name',lang("Name"));
465                                                                        $this->template->set_var('lang_name',$result['given_names'][0]);
466                                                                       
467                                                                        $this->template->set_var('lang_title_lastname',lang("Family Names"));
468                                                                        $this->template->set_var('lang_lastname',$result['family_names'][0]);
469                                                                       
470                                                                        $var_phone = "";
471                                                                        $var_email = "";
472                                                                        foreach($result['connections'] as $conn):
473                                                                                if ( $conn['id_type'] == 1 ){
474                                                                                        if ( !empty($var_email) )
475                                                                                                $var_email .= ' | ';
476                                                                                        $var_email .= $conn['connection_value'];
477                                                                                       
478                                                                                        if ( empty($email_to) )
479                                                                                                $email_to = $var_email;
480                                                                                       
481                                                                                }else if ($conn['id_type'] == 2){
482                                                                                        if ( !empty($var_phone))
483                                                                                                $var_phone .= ' | ';
484                                                                                        $var_phone .= $conn['connection_value'];
485                                                                                }
486                                                                        endforeach;
487                                               
488                                                                        $this->template->set_var('email_to', $email_to);
489                                                                       
490                                                                        $this->template->set_var('lang_title_email',lang("Email"));
491                                                                        $this->template->set_var('lang_email',$var_email);
492                                                                       
493                                                                        $this->template->set_var('lang_title_phone',lang("Phone"));
494                                                                        $this->template->set_var('lang_phone',$var_phone);
495
496                                                                        $this->template->parse("row_body","people_ldap");
497
498                                                                }else{
499                                                                        header('Location: ../mobile/index.php?menuaction=mobile.ui_mobilecc.init_cc');
500                                                                }
501
502                                                        break;
503                        }
504
505                        if ( !empty($email_to)){
506                                $this->template->set_var('email_to', $email_to);
507                                $this->template->parse("buttom_use","buttom_use_contact");
508                        }
509
510                        $this->template->set_var('lang_contact_title',lang("context contact"));
511                        $this->template->set_var('lang_back',lang("back"));
512                        $this->template->set_var('lang_use_contact',lang("use contact"));
513                        $this->template->set_var('lang_selecteds',lang("selecteds"));
514
515                        $GLOBALS['phpgw_info']['mobiletemplate']->set_content($this->template->fp('out','body'));
516                }
517
518                /**
519                 * View Add/Edit contact
520                 *
521                 * @param $id int
522                 * @param $catalog String
523                 * @return $contact
524                 */
525                function contact_add_edit($params) {
526
527                        $this->template->set_file(
528                                Array(
529                                        'cc_e' => 'contact_add_edit.tpl'
530                                )
531                        );
532
533                        $this->template->set_block('cc_e','body');
534
535                        $view = false;
536                        if ( isset($params['erro'])){
537                                $GLOBALS['phpgw_info']['mobiletemplate']->set_error_msg($params['erro']);
538                               
539                                $result['alias']                        = $params['alias'];
540                                $result['given_names']          = $params['given_names'];
541                                $result['family_names']         = $params['family_names'];
542                                $result['names_ordered']        = $params['names_ordered'];
543                                $var_phone                                      = $params['phone'];
544                                $var_email                                      = $params['email'];
545                                $var_connection_email           = $params['id_connection_email'];
546                                $var_connection_phone           = $params['id_connection_phone'];
547                                $view = true;
548                               
549                        }
550
551                        if ( empty($params['id']) ){
552                                $title_contact = "title add contact";
553                                $form_action = "index.php?menuaction=mobile.ui_mobilecc.contact_add";
554                                $confirm = lang("confirm add");
555                                $params['catalog'] = 'bo_people_catalog';
556                        }else{
557                                $title_contact = "title edit contact";
558                                $form_action = "index.php?menuaction=mobile.ui_mobilecc.contact_edit";
559                                $confirm = lang("confirm edit");
560                               
561                                $view = true;
562
563                                if ( !isset($params['erro'])){
564                                        $this->bo->set_catalog($params['catalog']);
565                                        $result = $this->bo->bo->get_single_entry($params['id'], array("given_names"=>true,"names_ordered"=>true,"alias"=>true,"family_names"=>true,"companies"=>true,"relations"=>true,"connections"=>true));
566
567                                        $var_phone = "";
568                                        $var_email = "";
569                                        foreach($result['connections'] as $conn):
570                                                if ( $conn['id_type'] == 1 ){
571                                                        if ( (empty($var_email)) && ($conn['connection_is_default']) ){
572                                                                $var_email = $conn['connection_value'];
573                                                                $var_connection_email = $conn['id_connection'];
574                                                        }
575                                                }else if ($conn['id_type'] == 2){
576                                                        if ( (empty($var_phone)) &&  ($conn['connection_is_default'])){
577                                                                $var_phone = $conn['connection_value'];
578                                                                $var_connection_phone = $conn['id_connection'];
579                                                        }
580                                                }
581                                        endforeach;
582                                }
583
584                                                               
585                        }
586                       
587                        if ($view){
588                                        $this->template->set_var('lang_alias',$result['alias']);
589                                        $this->template->set_var('lang_name',$result['given_names']);
590                                        $this->template->set_var('lang_lastname',$result['family_names']);
591                                       
592                                        $this->template->set_var('var_connection_email', $var_connection_email);
593                                        $this->template->set_var('lang_email',$var_email);
594                                       
595                                        $this->template->set_var('var_connection_phone', $var_connection_phone);
596                                        $this->template->set_var('lang_phone',$var_phone);
597
598                        }
599                       
600                        $this->template->set_var('cc_name',$result['names_ordered']);
601                        $this->template->set_var('lang_title_alias',lang("Alias"));
602                        $this->template->set_var('lang_title_name',lang("Name"));
603                        $this->template->set_var('lang_title_lastname',lang("Family Names"));
604                        $this->template->set_var('lang_title_email',lang("Email"));
605                        $this->template->set_var('lang_title_phone',lang("Phone"));
606
607                        $this->template->set_var('catalog', $params['catalog']);
608                               
609                        $this->template->set_var('lang_title_add_edit',lang($title_contact));
610                        $this->template->set_var('form_action', $form_action);
611                       
612                        $this->template->set_var('lang_contact_title',lang("context contact"));
613                        $this->template->set_var('lang_back',lang("back"));
614                        $this->template->set_var('lang_cancel',lang("cancel"));
615                        $this->template->set_var('lang_confirm', $confirm);
616                        $this->template->set_var('lang_selecteds',lang("selecteds"));
617                        $this->template->set_var('id',$params['id']);
618                       
619                        $GLOBALS['phpgw_info']['mobiletemplate']->set_content($this->template->fp('out','body'));
620                }
621               
622                /**
623                 * Add contact
624                 *
625                 * @param $id int
626                 * @param $catalog String
627                 * @return $contact
628                 */
629                function contact_add($params) {
630                       
631                        $data['alias'] = $params['alias'];
632                        $data['given_names'] = $params['given_names'];
633                        $data['family_names'] = $params['family_names'];
634                        $data['names_ordered'] = $data['given_names'] . ' ' . $data['family_names'];
635                        $data['is_quick_add'] = true;
636
637                        $answer = $this->verifyData($params);
638                       
639                        if (!empty($answer)){
640                                $retorno = 'alias='.$data['alias'] . '&given_names='.$data['given_names'] . '&family_names='.$data['family_names'] . '&names_ordered='.$data['given_names'] . ' ' . $data['family_names'];
641                                $retorno .= '&id_connection_email='.$params['id_connection_email'] . '&id_connection_phone='.$params['id_connection_phone'];
642                                $retorno .= '&email='.$params['email'] . '&phone='.$params['phone'];
643                                header('Location: ../mobile/index.php?menuaction=mobile.ui_mobilecc.contact_add_edit&erro='.$answer.'&'.$retorno);
644                        }
645                        else
646                        {
647                                $this->bo->set_catalog($params['catalog']);
648       
649                                if ( !empty($params['email']) ){
650                                        $data['connections']['default_email']['connection_value'] = $params['email'];
651                                }
652                               
653                                if ( !empty($params['phone']) ){
654                                        $data['connections']['default_phone']['connection_value'] = $params['phone'];
655                                }
656       
657                                $this->bo->set_catalog($params['catalog']);
658                                $contact_id = $this->bo->bo->quick_add($data);
659       
660                                header('Location: ../mobile/index.php?menuaction=mobile.ui_mobilecc.contact_view&id='.$contact_id.'&catalog='.$params['catalog'] . '&success=1');
661                        }
662                }
663
664                /**
665                 * Edit contact
666                 *
667                 * @param $id int
668                 * @param $catalog String
669                 * @return $contact
670                 */
671                function contact_edit($params) {
672                       
673                        $data['alias'] = $params['alias'];
674                        $data['given_names'] = $params['given_names'];
675                        $data['family_names'] = $params['family_names'];
676                        $data['names_ordered'] = $data['given_names'] . ' ' . $data['family_names'];
677                       
678                        $cont = 0;
679
680                        $answer = $this->verifyData($params);
681
682                        if (!empty($answer)){
683                                $retorno = '&catalog=' . $params['catalog'] . '&id='. $params['id'] .'&alias='.$data['alias'] . '&given_names='.$data['given_names'] . '&family_names='.$data['family_names'] . '&names_ordered='.$data['given_names'] . ' ' . $data['family_names'];
684                                $retorno .= '&id_connection_email='.$params['id_connection_email'] . '&id_connection_phone='.$params['id_connection_phone'];
685                                $retorno .= '&email='.$params['email'] . '&phone='.$params['phone'];
686                                header('Location: ../mobile/index.php?menuaction=mobile.ui_mobilecc.contact_add_edit&erro='.$answer.'&'.$retorno);
687                        }
688                        else
689                        {
690
691                                $this->bo->set_catalog($params['catalog']);
692                                $types = $this->bo->bo->get_all_connections_types();
693       
694                                if ( !empty($params['email']) || !empty($params['id_connection_email']) ){
695                                        $cont++;
696                               
697                                        if (empty($params['id_connection_email'])){
698                                                $data['connections']['connection' . $cont]['connection_is_default'] = true;
699                                                $data['connections']['connection' . $cont]['connection_name'] = $types[1];
700                                        }
701       
702                                        $data['connections']['connection' . $cont]['id_connection'] = $params['id_connection_email'];
703                                        $data['connections']['connection' . $cont]['id_typeof_connection'] = 1;
704                                        $data['connections']['connection' . $cont]['connection_value'] = $params['email'];
705       
706                                }
707       
708                                if ( !empty($params['phone']) || !empty($params['id_connection_phone']) ){
709                                        $cont++;
710       
711                                        if (empty($params['id_connection_phone'])){
712                                                $data['connections']['connection' . $cont]['connection_is_default'] = true;
713                                                $data['connections']['connection' . $cont]['connection_name'] = $types[2];
714                                        }
715       
716                                        $data['connections']['connection' . $cont]['id_connection'] = $params['id_connection_phone'];
717                                        $data['connections']['connection' . $cont]['id_typeof_connection'] = 2;
718                                        $data['connections']['connection' . $cont]['connection_value'] = $params['phone'];
719       
720                                }
721       
722                                $contact_id = $this->bo->bo->update_single_info($params['id'], $data);
723
724                                header('Location: ../mobile/index.php?menuaction=mobile.ui_mobilecc.contact_view&id='.$contact_id.'&catalog='.$params['catalog'].'&success=1');
725                               
726                        }
727                       
728                }
729
730                /**
731                 * Validate data when register contact
732                 * Validate E-mail, Phone and Name(NotEmpty)
733                 *
734                 * @param $data Array
735                 * @return Boolean
736                 */
737                static function verifyData($data){
738
739                        $valid = '';
740                        $field = false;
741
742                        // Verify if phone is valid
743                        if ( !empty($data['phone']) && empty($valid) ){
744                               
745                                $field = true;
746                               
747                                $pattern = "#^(?:(?:\(?\+?(?P<country>\d{2,4})\)?\s*)?\(?(?P<city>\d{2,3})\)?\s*)?(?P<n1>\d{3,4})[-\s.]?(?P<n2>\d{4})$#";
748
749                                if (!preg_match($pattern, $data['phone'])){
750                                        $valid = lang('invalid field phone');
751                                }
752
753                        }
754
755                        // Verify if e-mail is valid
756                        if ( !empty($data['email']) ){
757
758                                $field = true;
759                               
760                                $pattern = "^[a-z0-9_\.\-]+@[a-z0-9_\.\-]*[a-z0-9_\-]+\.[a-z]{2,4}$";
761
762                                if (!eregi($pattern, $data['email'])){
763                                        $valid = lang('invalid field e-mail');
764                                }
765
766                        }
767
768                        // Verify if exist e-mail or phone
769                        if (!$field)
770                                $valid = lang('Tel or email is required');
771                       
772                        // Verify if name is empty
773                        if ( empty($data['given_names']) )
774                                $valid = lang('Name is mandatory');
775                       
776                        return $valid;
777                       
778                }
779
780               
781        }
782       
783       
784?>
Note: See TracBrowser for help on using the repository browser.