1 | <?php |
---|
2 | |
---|
3 | if (!defined('ROOTPATH')) |
---|
4 | define('ROOTPATH', dirname(__FILE__) . '/..'); |
---|
5 | |
---|
6 | require_once(ROOTPATH . '/rest/hypermedia/hypermedia.php'); |
---|
7 | |
---|
8 | use prototype\api\Config as Config; |
---|
9 | |
---|
10 | class DynamicContactResource extends Resource { |
---|
11 | |
---|
12 | /** |
---|
13 | * Retorna um contato recente |
---|
14 | * |
---|
15 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
16 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
17 | * @sponsor Caixa Econômica Federal |
---|
18 | * @author José Vicente Tezza Jr. |
---|
19 | * @return Retorna um contato recente |
---|
20 | * @access public |
---|
21 | * */ |
---|
22 | function get($request, $id) { |
---|
23 | |
---|
24 | $this->secured(); |
---|
25 | |
---|
26 | //verificar se a preferencia de contatos dinamicos nao esta ativada |
---|
27 | if(!$this->isEnabledDynamicContacts(Config::me("uidNumber")) ){ |
---|
28 | $response = new Response($request); |
---|
29 | $this->createException($request, $response, Response::UNAUTHORIZED, 'Resource unauthorized', 'disabled dynamic contacts preference'); |
---|
30 | return $response; |
---|
31 | } |
---|
32 | |
---|
33 | $response = new Response($request); |
---|
34 | $response->addHeader('Content-type', 'aplication/json'); |
---|
35 | $response->code = Response::OK; |
---|
36 | |
---|
37 | $h = new Hypermedia(); |
---|
38 | $c = new Collection($request->resources, 'DynamicContactResource', $id); |
---|
39 | |
---|
40 | try { |
---|
41 | $dynamicContact = Controller::read( |
---|
42 | array('concept' => 'dynamicContact'), false, array('filter' => array('AND', array('=', 'owner', Config::me("uidNumber")), array('=', 'id', $id))) |
---|
43 | ); |
---|
44 | |
---|
45 | //Se nao foi encontrado contatos na consulta |
---|
46 | if (!$dynamicContact) { |
---|
47 | $this->createException($request, $response, Response::NOTFOUND, 'Bad request', 'Dynamic Contact not found.'); |
---|
48 | return $response; |
---|
49 | } |
---|
50 | |
---|
51 | //Normaliza dado |
---|
52 | if(is_array($dynamicContact)) |
---|
53 | $dynamicContact = $dynamicContact[0]; |
---|
54 | |
---|
55 | |
---|
56 | $t = new Template(); |
---|
57 | $d = new Data(); |
---|
58 | |
---|
59 | $d->setName('name'); |
---|
60 | $d->setValue(null); |
---|
61 | $d->setPrompt('Nome do Contato Recente'); |
---|
62 | $d->setDataType('string'); |
---|
63 | $d->setMaxLength(100); |
---|
64 | $d->setMinLength(null); |
---|
65 | $d->setRequired(true); |
---|
66 | |
---|
67 | $t->addData($d); |
---|
68 | |
---|
69 | $d = new Data(); |
---|
70 | $d->setName('mail'); |
---|
71 | $d->setValue(null); |
---|
72 | $d->setPrompt('Email do Contato Recente'); |
---|
73 | $d->setDataType('string'); |
---|
74 | $d->setMaxLength(100); |
---|
75 | $d->setMinLength(null); |
---|
76 | $d->setRequired(true); |
---|
77 | |
---|
78 | $t->addData($d); |
---|
79 | |
---|
80 | $d = new Data(); |
---|
81 | $d->setName('number_of_messages'); |
---|
82 | $d->setValue(null); |
---|
83 | $d->setPrompt('Quantidade de mensagens enviadas'); |
---|
84 | $d->setDataType('integer'); |
---|
85 | $d->setMaxLength(100); |
---|
86 | $d->setMinLength(null); |
---|
87 | $d->setRequired(false); |
---|
88 | |
---|
89 | $t->addData($d); |
---|
90 | |
---|
91 | $c->setTemplate($t); |
---|
92 | |
---|
93 | $d = new Data(); |
---|
94 | $d->setName('id'); |
---|
95 | $d->setValue($dynamicContact['id']); |
---|
96 | $d->setPrompt('Identificador do Contato Recente'); |
---|
97 | $d->setDataType('integer'); |
---|
98 | $d->setMaxLength(null); |
---|
99 | $d->setMinLength(null); |
---|
100 | $d->setRequired(true); |
---|
101 | |
---|
102 | $c->addData($d); |
---|
103 | |
---|
104 | $d = new Data(); |
---|
105 | |
---|
106 | $d->setName('name'); |
---|
107 | $d->setValue($dynamicContact['name']); |
---|
108 | $d->setPrompt('Nome do Contato Recente'); |
---|
109 | $d->setDataType('string'); |
---|
110 | $d->setMaxLength('100'); |
---|
111 | $d->setMinLength(null); |
---|
112 | $d->setRequired(true); |
---|
113 | |
---|
114 | $c->addData($d); |
---|
115 | |
---|
116 | $d = new Data(); |
---|
117 | $d->setName('mail'); |
---|
118 | $d->setValue($dynamicContact['mail']); |
---|
119 | $d->setPrompt('Email do Contato Recente'); |
---|
120 | $d->setDataType('string'); |
---|
121 | $d->setMaxLength('100'); |
---|
122 | $d->setMinLength(null); |
---|
123 | $d->setRequired(true); |
---|
124 | |
---|
125 | $c->addData($d); |
---|
126 | |
---|
127 | $d = new Data(); |
---|
128 | $d->setName('number_of_messages'); |
---|
129 | $d->setValue($dynamicContact['number_of_messages']); |
---|
130 | $d->setPrompt('Quantidade de mensagens enviadas'); |
---|
131 | $d->setDataType('integer'); |
---|
132 | $d->setMaxLength('100'); |
---|
133 | $d->setMinLength(null); |
---|
134 | $d->setRequired(false); |
---|
135 | |
---|
136 | $c->addData($d); |
---|
137 | |
---|
138 | |
---|
139 | $l = new Link(); |
---|
140 | |
---|
141 | $l->setHref(''); |
---|
142 | $l->setRel('delete'); |
---|
143 | $l->setAlt('Remover'); |
---|
144 | $l->setPrompt('Remover'); |
---|
145 | $l->setRender('link'); |
---|
146 | |
---|
147 | $c->addLink($l); |
---|
148 | |
---|
149 | $l = new Link(); |
---|
150 | $l->setHref(''); |
---|
151 | $l->setRel('put'); |
---|
152 | $l->setAlt('Atualizar'); |
---|
153 | $l->setPrompt('Atualizar'); |
---|
154 | $l->setRender('link'); |
---|
155 | |
---|
156 | $c->addLink($l); |
---|
157 | |
---|
158 | $h->setCollection($c); |
---|
159 | } catch (Exception $ex) { |
---|
160 | $this->createException($request, $response, Response::INTERNALSERVERERROR, 'Internal Server Error', 'Internal Server Error'); |
---|
161 | return $response; |
---|
162 | } |
---|
163 | |
---|
164 | $response->body = $h->getHypermedia($request->accept[10][0]); |
---|
165 | return $response; |
---|
166 | } |
---|
167 | |
---|
168 | /** |
---|
169 | * Atualiza um contato recente |
---|
170 | * |
---|
171 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
172 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
173 | * @sponsor Caixa Econômica Federal |
---|
174 | * @author José Vicente Tezza Jr. |
---|
175 | * @return retorna a uri do contato recente |
---|
176 | * @access public |
---|
177 | * */ |
---|
178 | function put($request, $id) { |
---|
179 | |
---|
180 | $this->secured(); |
---|
181 | |
---|
182 | //verificar se a preferencia de contatos dinamicos nao esta ativada |
---|
183 | if(!$this->isEnabledDynamicContacts(Config::me("uidNumber")) ){ |
---|
184 | $response = new Response($request); |
---|
185 | $this->createException($request, $response, Response::UNAUTHORIZED, 'Resource unauthorized', 'disabled dynamic contacts preference'); |
---|
186 | return $response; |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | $post = $request->dataDecoded; |
---|
191 | $response = new Response($request); |
---|
192 | |
---|
193 | if (count($post) == 0){ |
---|
194 | $this->createException($request, $response, Response::BADREQUEST, 'Bad request', 'Invalid template data'); |
---|
195 | return $response; |
---|
196 | } |
---|
197 | |
---|
198 | //recupera os atributos definidos no conceito 'user' |
---|
199 | $map = Config::get('dynamicContact', 'PostgreSQL.mapping'); |
---|
200 | |
---|
201 | $params = array(); |
---|
202 | foreach ($post as $key => $value) { |
---|
203 | |
---|
204 | if (!isset($map[$key]) || $key == 'id' || $key == 'timestamp' || $key == 'number_of_messages') { |
---|
205 | continue; |
---|
206 | } |
---|
207 | $params[$key] = $value; |
---|
208 | } |
---|
209 | |
---|
210 | if (count($params) == 0) { |
---|
211 | $this->createException($request, $response, Response::BADREQUEST, 'Bad request', 'Invalid template data'); |
---|
212 | return $response; |
---|
213 | } |
---|
214 | |
---|
215 | //completar os atributos |
---|
216 | $params['owner'] = Config::me("uidNumber"); |
---|
217 | $params['timestamp'] = time(); |
---|
218 | $params['id'] = $id; |
---|
219 | |
---|
220 | $response->addHeader('Content-type', 'aplication/json'); |
---|
221 | $response->code = Response::NOCONTENT; |
---|
222 | |
---|
223 | try { |
---|
224 | |
---|
225 | $dynamicContact = Controller::read( |
---|
226 | array('concept' => 'dynamicContact'), false, array('filter' => array( |
---|
227 | 'AND', |
---|
228 | array('=', 'owner', Config::me("uidNumber")), |
---|
229 | array('=', 'id', $id))) |
---|
230 | ); |
---|
231 | |
---|
232 | |
---|
233 | //Se existe o recurso |
---|
234 | if ($dynamicContact) { |
---|
235 | //Normaliza o recurso |
---|
236 | if(is_array($dynamicContact)) |
---|
237 | $dynamicContact = $dynamicContact[0]; |
---|
238 | |
---|
239 | $params['number_of_messages'] = $dynamicContact['number_of_messages'] + 1; |
---|
240 | |
---|
241 | $dynamicContact = Controller::update(array('concept' => 'dynamicContact', 'id' => $id), $params); |
---|
242 | |
---|
243 | if (!$dynamicContact) { |
---|
244 | $this->createException($request, $response, Response::INTERNALSERVERERROR, 'Internal Server Error', Controller::service('PostgreSQL')->error); |
---|
245 | return $response; |
---|
246 | } |
---|
247 | } else { |
---|
248 | /* |
---|
249 | $idDynamicContact = Controller::create( array('concept' => 'dynamicContact'), $params); |
---|
250 | */ |
---|
251 | //if (!$idDynamicContact) { |
---|
252 | $this->createException($request, $response, Response::NOTFOUND, 'Bad request', 'Invalid data'); |
---|
253 | return $response; |
---|
254 | //} |
---|
255 | } |
---|
256 | } catch (Exception $ex) { |
---|
257 | $this->createException($request, $response, Response::INTERNALSERVERERROR, 'Internal Server Error', 'Internal Server Error'); |
---|
258 | return $response; |
---|
259 | } |
---|
260 | $response->body = json_encode(null); |
---|
261 | return $response; |
---|
262 | } |
---|
263 | |
---|
264 | /** |
---|
265 | * Remove um contato recente |
---|
266 | * |
---|
267 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
268 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
269 | * @sponsor Caixa Econômica Federal |
---|
270 | * @author José Vicente Tezza Jr. |
---|
271 | * @access public |
---|
272 | * */ |
---|
273 | function delete($request, $id) { |
---|
274 | |
---|
275 | $this->secured(); |
---|
276 | |
---|
277 | //verificar se a preferencia de contatos dinamicos nao esta ativada |
---|
278 | if(!$this->isEnabledDynamicContacts(Config::me("uidNumber")) ){ |
---|
279 | $response = new Response($request); |
---|
280 | $this->createException($request, $response, Response::UNAUTHORIZED, 'Resource unauthorized', 'disabled dynamic contacts preference'); |
---|
281 | return $response; |
---|
282 | } |
---|
283 | |
---|
284 | $response = new Response($request); |
---|
285 | $response->addHeader('Content-type', 'aplication/json'); |
---|
286 | $response->code = Response::NOCONTENT; |
---|
287 | |
---|
288 | try { |
---|
289 | //Verifica se o recurso existe |
---|
290 | $dinamicContact = Controller::read(array('concept' => 'dynamicContact', 'id' => $id)); |
---|
291 | |
---|
292 | //Se existe o recurso |
---|
293 | if ($dinamicContact) { |
---|
294 | |
---|
295 | $delete = Controller::delete(array('concept' => 'dynamicContact', 'id' => $id)); |
---|
296 | |
---|
297 | if (!$delete) { |
---|
298 | $this->createException($request, $response, Response::INTERNALSERVERERROR, 'Internal Server Error', Controller::service('PostgreSQL')->error); |
---|
299 | return $response; |
---|
300 | } |
---|
301 | } else { |
---|
302 | $this->createException($request, $response, Response::NOTFOUND, 'Bad request', 'Invalid data'); |
---|
303 | return $response; |
---|
304 | } |
---|
305 | } catch (Exception $ex) { |
---|
306 | $this->createException($request, $response, Response::INTERNALSERVERERROR, 'Internal Server Error', 'Internal Server Error'); |
---|
307 | return $response; |
---|
308 | } |
---|
309 | |
---|
310 | $response->body = json_encode(null); |
---|
311 | return $response; |
---|
312 | } |
---|
313 | |
---|
314 | private function createException($request, &$response, $code, $title, $description) { |
---|
315 | $response->code = $code; |
---|
316 | |
---|
317 | $h = new Hypermedia(); |
---|
318 | $c = new Collection($request->resources, 'DynamicContactResource'); |
---|
319 | $e = new Error(); |
---|
320 | |
---|
321 | $e->setCode($code); |
---|
322 | $e->setTitle($title); |
---|
323 | $e->setDescription($description); |
---|
324 | |
---|
325 | $c->setError($e); |
---|
326 | $h->setCollection($c); |
---|
327 | |
---|
328 | $response->body = $h->getHypermedia($request->accept[10][0]); |
---|
329 | } |
---|
330 | |
---|
331 | private function isEnabledDynamicContacts($user){ |
---|
332 | |
---|
333 | //recuperando as preferencias (suas preferencias, preferencia padrão, preferencia obrigatoria) |
---|
334 | //dos contatos dinamicos |
---|
335 | $sql = 'SELECT preference_owner, preference_value '. |
---|
336 | 'FROM phpgw_preferences '. |
---|
337 | 'WHERE preference_app = \'expressoMail\' AND '. |
---|
338 | 'preference_owner in (-1,-2, ' . $user . ')'; |
---|
339 | |
---|
340 | $preferences = Controller::service('PostgreSQL')->execResultSql($sql); |
---|
341 | |
---|
342 | $array = array(); |
---|
343 | if(count($preferences) > 0){ |
---|
344 | foreach($preferences as $preference){ |
---|
345 | //recupera a preferencia |
---|
346 | $preference_value = unserialize( $preference['preference_value'] ); |
---|
347 | |
---|
348 | //gera um array com o owner e o valor da preferencia: |
---|
349 | //true: SIM (1) |
---|
350 | //false: NAO (0) |
---|
351 | //null: escolha pelo usuario/ usar padrao / sem padrao |
---|
352 | $value = null; |
---|
353 | if(isset($preference_value['use_dynamic_contacts'])){ |
---|
354 | $value = (isset($preference_value['use_dynamic_contacts'])) ? $preference_value['use_dynamic_contacts'] == '1' : false; |
---|
355 | } |
---|
356 | $array[ $preference['preference_owner'] ] = $value; |
---|
357 | } |
---|
358 | } |
---|
359 | |
---|
360 | //preferencia obrigatoria (SIM) |
---|
361 | if(array_key_exists(-1,$array) && $array[-1]){ |
---|
362 | return true; |
---|
363 | } |
---|
364 | //preferencia do user (SIM) |
---|
365 | else if(array_key_exists($user,$array) && $array[$user] ){ |
---|
366 | return true; |
---|
367 | } |
---|
368 | //preferencia padrao (SIM) escolhida pelo user |
---|
369 | else if(array_key_exists($user, $array) && $array[$user] === null && |
---|
370 | array_key_exists(-2, $array) && $array[-2]){ |
---|
371 | return true; |
---|
372 | } |
---|
373 | return false; |
---|
374 | } |
---|
375 | } |
---|
376 | |
---|
377 | ?> |
---|