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

Revision 6148, 3.5 KB checked in by niltonneto, 12 years ago (diff)

Ticket #2507 - Restruturado diretório para alinhamento com projeto REST.

  • 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                                                $error = Errors::get(Errors::CATALOG_MIN_ARGUMENT_SEARCH, $this-> getMinArgumentSearch());                                             
27                                                throw new ResponseException($error['message'], $error['code']);
28                                        }
29                                }
30                        }
31               
32                        $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, '.
33                                        'phpgw_cc_contact_conns B, phpgw_cc_connections C where A.id_contact = B.id_contact and B.id_connection = C.id_connection '.
34                                        ' and '.$query_contact.' A.id_owner='.$this -> getUserId().' group by '.
35                                        ' 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)';
36               
37                        if (!$this->getDb()->query($query))
38                                return false;
39               
40                        $contacts = array();
41                        while($this->getDb()->next_record()) {
42                                $row = $this->getDb()->row();
43                                $id = $row['id_contact'];
44                                $contactType = ($row['id_typeof_contact_connection'] == 2 ? 'contactPhones' : 'contactMails');
45               
46                                if($contacts[$id] != null){
47                                        $contacts[$id][$contactType][] = $row['connection_value'];
48                                }
49                                else{
50                                        $contacts[$id] = array(
51                                                        'contactID'             => $row['id_contact'],
52                                                        $contactType    => array($row['connection_value']),
53                                                        'contactAlias' => ($row['alias'] != null ?  mb_convert_encoding($row['alias'],"UTF8", "ISO_8859-1") : ""),
54                                                        'contactFirstName'      => ($row['given_names'] != null ?  mb_convert_encoding($row['given_names'],"UTF8", "ISO_8859-1") : ""),
55                                                        'contactLastName'       => ($row['family_names'] != null ?  mb_convert_encoding($row['family_names'],"UTF8", "ISO_8859-1") : ""),
56                                                        'contactFullName'       => ($row['names_ordered'] != null ? mb_convert_encoding($row['names_ordered'],"UTF8", "ISO_8859-1") : ""),
57                                                        'contactBirthDate'      => ($row['birthdate'] != null ? $row['birthdate'] : ""),
58                                                        'contactNotes'          => ($row['notes'] != null ?  mb_convert_encoding($row['notes'],"UTF8", "ISO_8859-1") : ""),
59                                                        'contactHasImagePicture' => ($row['photo'] != null ? 1 : 0),
60                                        );
61                                        if($search != null &&
62                                                        stristr($contacts[$id]['contactAlias'], $search) == null &&
63                                                        stristr($contacts[$id]['contactFirstName'], $search) == null &&
64                                                        stristr($contacts[$id]['contactLastName'], $search) == null &&
65                                                        stristr($contacts[$id]['contactFullName'], $search) == null &&
66                                                        stristr($contacts[$id]['contactBirthDate'], $search) == null &&
67                                                        stristr($contacts[$id]['contactNotes'], $search) == null){
68                                                unset($contacts[$id]);
69                                        }
70                                }
71                        }
72                        $result = array ('contacts' => array_values($contacts));
73                        $this->setResult($result);
74                }
75                //to Send Response (JSON RPC format)
76                return $this->getResponse();           
77        }       
78
79}
Note: See TracBrowser for help on using the repository browser.