source: sandbox/2.4.1-3/prototype/rest/contact/DynamicContactsResource.php @ 6357

Revision 6357, 4.8 KB checked in by gustavo, 12 years ago (diff)

Ticket #2768 - Melhorias na inserção de destinatários na criacao de mensagem

  • Property svn:executable set to *
Line 
1<?php
2
3use prototype\api\Config as Config;
4
5class DynamicContactsResource extends Resource {
6
7    /**
8     * Busca os eventos contidos nas agendas assinas do usuário
9     *
10     * @license    http://www.gnu.org/copyleft/gpl.html GPL
11     * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
12     * @sponsor    Caixa Econômica Federal
13     * @author     Adriano Coutinho da Silva
14     * @author     Adir Kuhn
15     * @return     Retorna a colection de contatos dinâmicos de um usuário
16     * @access     public
17     * */
18    function get($request) {
19        try {
20            $response = new Response($request);
21            $response->addHeader('Content-type', 'aplication/json');
22
23            $this->secured();
24            $dynamicContact = Controller::find(array('concept' => 'dynamicContact'), false, array('filter' => array('=', 'owner', Config::me("uidNumber"))));
25
26            $response->code = Response::OK;
27            $resourceHref = str_replace($request->baseUri, '', $request->uri);
28
29            if ($dynamicContact) {
30                $data = array();
31                foreach ($dynamicContact as $key => $value) {
32                    $data[$key] = array(
33                        'href' => '/dynamic-contacts/' . $value['id'],
34                        'data' => array(
35                            array(
36                                'name' => "name",
37                                'value' => $value['name'],
38                                'prompt' => "Nome do Contato"
39                            ),
40                            array(
41                                "name" => "email",
42                                "value" => $value['mail'],
43                                "prompt" => "Email do Contato"
44                            )
45                        ),
46                        'links' => array(
47                            array(
48                                'href' => $resourceHref . "/" . $value['id'],
49                                'rel' => 'delete',
50                                'prompt' => 'Remover contato dinamico',
51                                'name' => 'delete-contact',
52                                'render' => 'link'
53                            ),
54                            array(
55                                'href' => $resourceHref . '/' . $value['id'],
56                                'rel' => 'update',
57                                'prompt' => 'Editar contato dinamico',
58                                'name' => 'edit-contact',
59                                'render' => 'link'
60                            )
61                        )
62                    );
63                }
64            }else
65                $data = null;
66
67            $result = array(
68                "version" => "0.1",
69                "collection" => array(
70                    "href" => $resourceHref,
71                    "type" => "dynamic-contacts",
72                    "data" => array(
73                        "name" => "dynamic-contacts",
74                        "prompt" => "Contatos dinâmicos"
75                    ),
76                    "pagination" => null,
77                    "items" => $data,
78                    "queries" => null,
79                    "template" => array(
80                        "data" => array(
81                            array(
82                                "name" => "name",
83                                "value" => "",
84                                "prompt" => "Nome do contato",
85                                "dataType" => "string",
86                                "minLength" => 0,
87                                "maxLength" => 100,
88                                "required" => false
89                            ),
90                            array(
91                                "name" => "email",
92                                "value" => "",
93                                "prompt" => "Email do contato",
94                                "dataType" => "string",
95                                "minLength" => 0,
96                                "maxLength" => 100,
97                                "required" => true
98                            )
99                        )
100                    ),
101                    'error' => null
102                )
103            );
104        } catch (Exception $ex) {
105            $this->createException($response, Response::INTERNALSERVERERROR, 'Internal Server Error', $ex);
106            return $response;
107        }
108
109        $response->body = json_encode($result);
110        return $response;
111    }
112
113    function post($request) {
114
115        try {
116            $response = new Response($request);
117            $response->addHeader('Content-type', 'aplication/json');
118            $data = $_POST;
119
120            if ($this->validData($data)) {
121                $this->secured();
122
123                $response->code = Response::OK;
124
125                $exists = Controller::find(array('concept' => 'dynamicContact'), false, array('filter' => array('AND',
126                                array('=', 'owner', Config::me("uidNumber")),
127                                array('=', 'mail', $data['email'])
128                                )));
129
130                if ($exists) {
131                    //Controller::update(array('concept' => 'dynamicContact', 'id' => $exists[0]['id']), array('name' => $data['name'], 'email' => $data['email'], 'timestamp' => time()));
132
133                    $this->createException($response, Response::BADREQUEST, 'Bad request', Controller::service('PostgreSQL')->error);
134                    return $response;
135                }else
136                    $newDynamicContact = Controller::create(array('concept' => 'dynamicContact'), array('name' => $data['name'], 'mail' => $data['email'], 'owner' => Config::me("uidNumber"), 'timestamp' => time()));
137
138                if (!$newDynamicContact)
139                    $this->createException($response, Response::INTERNALSERVERERROR, 'Internal Server Error', Controller::service('PostgreSQL')->error);
140            } else {
141                $this->createException($response, Response::BADREQUEST, 'Bad request', 'Invalid template data');
142                return $response;
143            }
144        } catch (Exception $ex) {
145            $this->createException($response, Response::INTERNALSERVERERROR, 'Internal Server Error', $ex);
146            return $response;
147        }
148
149        return $response;
150    }
151
152    private function createException(&$response, $code, $title, $description) {
153        $result = array(
154            'version' => '0.1',
155            'collection' => array(
156                'error' => array(
157                    'code' => $code,
158                    'title' => $title,
159                    'description' => $description
160                )
161            )
162        );
163        $response->code = $code;
164        $response->body = json_encode($result);
165    }
166
167    private function validData($data) {
168        return ((array_key_exists('name', $data) && !empty($data['name'])) && (array_key_exists('email', $data) && !empty($data['email']))) ? true : false;
169    }
170
171}
172
173?>
Note: See TracBrowser for help on using the repository browser.