source: sandbox/webservice/api/json-rpc/Catalog.php @ 5707

Revision 5707, 5.5 KB checked in by niltonneto, 12 years ago (diff)

Ticket #2507 - Adicionado algumas melhorias no método Catalog.getUserContacts().

  • Property svn:executable set to *
Line 
1<?php
2
3include_once("Expresso.php");
4
5class Catalog extends Expresso {       
6       
7        public function getUserContacts($params){
8                if($this->isLoggedIn($params)) {
9                        $search = "";
10                        if($params) {
11                               
12                                $search = trim($params['search']);
13                                if($params['contactID'] > 0){
14                                        $query_contact = 'A.id_contact='.$params['contactID'].' and';
15                                }
16                                elseif($search != "") {
17                                        $search = mb_convert_encoding($search,"ISO_8859-1", "UTF8");
18                                }
19                               
20                                if(($search || $params['contactID']) && $params['contactType'] == 2){
21                                        return $this->getGlobalContacts($search, $params['contactID']);                                 
22                                }
23                        }
24                       
25                        $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, '.
26                                'phpgw_cc_contact_conns B, phpgw_cc_connections C where A.id_contact = B.id_contact and B.id_connection = C.id_connection '.
27                                ' and '.$query_contact.' A.id_owner='.$this -> getUserId().' group by '.
28                                ' 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)';
29                       
30                        if (!$this->getDb()->query($query))
31                                return false;
32                       
33                        $contacts = array();
34                        while($this->getDb()->next_record()) {
35                                $row = $this->getDb()->row();
36                                $id = $row['id_contact'];
37                                $contactType = ($row['id_typeof_contact_connection'] == 2 ? 'contactPhones' : 'contactMails');                         
38                               
39                                if($contacts[$id] != null){
40                                        $contacts[$id][$contactType][] = $row['connection_value'];
41                                }       
42                                else{
43                                        $contacts[$id] = array(
44                                                'contactID'             => $row['id_contact'],
45                                                $contactType    => array($row['connection_value']),                                             
46                                                'contactAlias' => ($row['alias'] != null ?  mb_convert_encoding($row['alias'],"UTF8", "ISO_8859-1") : ""),
47                                                'contactFirstName'      => ($row['given_names'] != null ?  mb_convert_encoding($row['given_names'],"UTF8", "ISO_8859-1") : ""),
48                                                'contactLastName'       => ($row['family_names'] != null ?  mb_convert_encoding($row['family_names'],"UTF8", "ISO_8859-1") : ""),
49                                                'contactFullName'       => ($row['names_ordered'] != null ? mb_convert_encoding($row['names_ordered'],"UTF8", "ISO_8859-1") : ""),
50                                                'contactBirthDate'      => ($row['birthdate'] != null ? $row['birthdate'] : ""),
51                                                'contactNotes'          => ($row['notes'] != null ?  mb_convert_encoding($row['notes'],"UTF8", "ISO_8859-1") : ""),
52                                                'contactHasImagePicture' => ($row['photo'] != null ? 1 : 0),
53                                        );
54                                        if($search != null &&
55                                                        stristr($contacts[$id]['contactAlias'], $search) == null &&
56                                                        stristr($contacts[$id]['contactFirstName'], $search) == null &&
57                                                        stristr($contacts[$id]['contactLastName'], $search) == null &&
58                                                        stristr($contacts[$id]['contactFullName'], $search) == null &&
59                                                        stristr($contacts[$id]['contactBirthDate'], $search) == null &&
60                                                        stristr($contacts[$id]['contactNotes'], $search) == null){
61                                                unset($contacts[$id]);
62                                        }
63                                }                               
64                        }
65                        $this->result = array ('contacts' => array_values($contacts));
66                }
67                return $this->getResponse();
68        }       
69       
70        public function getImagePicture($params){               
71                if($this->isLoggedIn($params) && $params['contactID'] > 0) {                   
72                        $query = 'select A.id_contact, A.photo from phpgw_cc_contact A where A.id_contact='.$params['contactID'].' and A.id_owner='.$this -> getUserId();               
73                        if (!$this->getDb()->query($query))
74                                return false;
75
76                        $contact = array();
77                        if($this->getDb()->next_record()) {
78                                $row = $this->getDb()->row();
79                                if($row['photo'] != null) {
80                                        $contact[] = array(
81                                                'contactID'             => $row['id_contact'],
82                                                'contactImagePicture'   => ($row['photo'] != null ? base64_encode($row['photo']) : "")
83                                        );
84                                }                       
85                        }
86                        $this->result = array ('contacts' => $contact);
87                }
88                return $this->getResponse();
89        }       
90
91        private function getUserLdapAttrs($mail)
92        {
93                $filter="(&(phpgwAccountType=u)(mail=".$mail."))";
94                $ldap_context = $_SESSION['phpgw_info']['expressomail']['ldap_server']['dn'];
95                $justthese = array("dn", 'jpegPhoto','givenName', 'sn');
96                $this->getLdapCatalog()->ldapConnect(true);
97                $ds = $this->getLdapCatalog()->ds;
98                if ($ds){
99                        $sr = @ldap_search($ds, $ldap_context, $filter, $justthese);   
100                        if ($sr) {
101                                $entry = ldap_first_entry($ds, $sr);
102                                if($entry) {                                                                   
103                                        $givenName = @ldap_get_values_len($ds, $entry, "givenname");
104                                        $sn = @ldap_get_values_len($ds, $entry, "sn");
105                                        $contactHasImagePicture = (@ldap_get_values_len($ds, $entry, "jpegphoto") ? 1 : 0);
106                                        $dn = ldap_get_dn($ds, $entry);
107                                        return array(
108                                                "contactID" => $dn,
109                                                "contactFirstName" => $givenName[0],
110                                                "contactLastName"       => $sn[0],
111                                                "contactHasImagePicture" => $contactHasImagePicture
112                                        );
113                                }
114                        }
115                }
116                return false;
117        }       
118       
119        private function getGlobalContacts($search, $uidNumber){
120                $contacts = array();
121                $params = array ("search_for" => $search);
122                $result = $this->getLdapCatalog()->quicksearch($params);
123
124                foreach($result as $i => $row) {
125                        if(is_int($i)) {
126                                $contacts[$i] = array(
127                                        'contactMails'  => array($result[$i]['mail']),
128                                        'contactPhones' => array($result[$i]['phone']),
129                                        'contactAlias' => "",                                   
130                                        'contactFullName'       => ($result[$i]['cn'] != null ? mb_convert_encoding($row['cn'],"UTF8", "ISO_8859-1") : ""),
131                                        'contactBirthDate'      => "",
132                                        'contactNotes'          => ""
133                                );
134                                // Buscar atributos faltantes.
135                                $contacts[$i] = array_merge($this->getUserLdapAttrs($result[$i]['mail']), $contacts[$i]);                               
136                        }
137                }
138                $this->result = array ('contacts' => $contacts);
139               
140                return $this->getResponse();
141        }
142}
Note: See TracBrowser for help on using the repository browser.