source: sandbox/webservice/api/rest/catalog/ContactsResource.php @ 6165

Revision 6165, 3.4 KB checked in by niltonneto, 12 years ago (diff)

Ticket #2507 - Refatorado e centralizado tratamento de Erros.

  • Property svn:executable set to *
Line 
1<?php
2
3class ContactsResource extends CatalogAdapter {
4        public function post($request){
5                // to Receive POST Params (use $this->params)
6                parent::post($request);
7               
8                if($this-> isLoggedIn())
9                {                       
10                        $search = "";
11                       
12                        if($this->getParams()) {
13                                $search = trim($this->getParam('search'));
14                               
15                                if($search != "") {
16                                        $search = mb_convert_encoding($search,"ISO_8859-1", "UTF8");
17                                }
18               
19                                if($this->getParam('contactID') > 0 && $this->getParam('contactType') == 1){
20                                        $query_contact = 'A.id_contact='.$this->getParam('contactID').' and';
21                                }
22                                elseif($this->getParam('contactType') == 2){
23                                        if($this-> getMinArgumentSearch() <= strlen($search))
24                                                return $this->getGlobalContacts($search, $this->getParam('contactID'));
25                                        else{
26                                                Errors::runException("CATALOG_MIN_ARGUMENT_SEARCH", $this-> getMinArgumentSearch());
27                                        }
28                                }
29                        }
30               
31                        $query = 'select B.id_typeof_contact_connection, A.photo, A.id_contact, A.alias, A.given_names, A.family_names, A.names_ordered, A.birthdate, A.notes, C.connection_value from phpgw_cc_contact A, '.
32                                        'phpgw_cc_contact_conns B, phpgw_cc_connections C where A.id_contact = B.id_contact and B.id_connection = C.id_connection '.
33                                        ' and '.$query_contact.' A.id_owner='.$this -> getUserId().' group by '.
34                                        ' B.id_typeof_contact_connection, A.photo, A.id_contact, A.alias, A.given_names, A.family_names,A.names_ordered,A.birthdate, A.notes,C.connection_value order by lower(A.names_ordered)';
35               
36                        if (!$this->getDb()->query($query))
37                                return false;
38               
39                        $contacts = array();
40                        while($this->getDb()->next_record()) {
41                                $row = $this->getDb()->row();
42                                $id = $row['id_contact'];
43                                $contactType = ($row['id_typeof_contact_connection'] == 2 ? 'contactPhones' : 'contactMails');
44               
45                                if($contacts[$id] != null){
46                                        $contacts[$id][$contactType][] = $row['connection_value'];
47                                }
48                                else{
49                                        $contacts[$id] = array(
50                                                        'contactID'             => $row['id_contact'],
51                                                        $contactType    => array($row['connection_value']),
52                                                        'contactAlias' => ($row['alias'] != null ?  mb_convert_encoding($row['alias'],"UTF8", "ISO_8859-1") : ""),
53                                                        'contactFirstName'      => ($row['given_names'] != null ?  mb_convert_encoding($row['given_names'],"UTF8", "ISO_8859-1") : ""),
54                                                        'contactLastName'       => ($row['family_names'] != null ?  mb_convert_encoding($row['family_names'],"UTF8", "ISO_8859-1") : ""),
55                                                        'contactFullName'       => ($row['names_ordered'] != null ? mb_convert_encoding($row['names_ordered'],"UTF8", "ISO_8859-1") : ""),
56                                                        'contactBirthDate'      => ($row['birthdate'] != null ? $row['birthdate'] : ""),
57                                                        'contactNotes'          => ($row['notes'] != null ?  mb_convert_encoding($row['notes'],"UTF8", "ISO_8859-1") : ""),
58                                                        'contactHasImagePicture' => ($row['photo'] != null ? 1 : 0),
59                                        );
60                                        if($search != null &&
61                                                        stristr($contacts[$id]['contactAlias'], $search) == null &&
62                                                        stristr($contacts[$id]['contactFirstName'], $search) == null &&
63                                                        stristr($contacts[$id]['contactLastName'], $search) == null &&
64                                                        stristr($contacts[$id]['contactFullName'], $search) == null &&
65                                                        stristr($contacts[$id]['contactBirthDate'], $search) == null &&
66                                                        stristr($contacts[$id]['contactNotes'], $search) == null){
67                                                unset($contacts[$id]);
68                                        }
69                                }
70                        }
71                        $result = array ('contacts' => array_values($contacts));
72                        $this->setResult($result);
73                }
74                //to Send Response (JSON RPC format)
75                return $this->getResponse();           
76        }       
77
78}
Note: See TracBrowser for help on using the repository browser.