[6473] | 1 | <?php |
---|
| 2 | |
---|
[6523] | 3 | if (!defined('ROOTPATH')) |
---|
| 4 | define('ROOTPATH', dirname(__FILE__) . '/..'); |
---|
[6473] | 5 | |
---|
[6523] | 6 | require_once(ROOTPATH . '/rest/hypermedia/hypermedia.php'); |
---|
[6473] | 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 | * */ |
---|
[6523] | 22 | function get($request, $id) { |
---|
[6473] | 23 | |
---|
[6523] | 24 | $this->secured(); |
---|
[6473] | 25 | |
---|
[6523] | 26 | $response = new Response($request); |
---|
| 27 | $response->addHeader('Content-type', 'aplication/json'); |
---|
| 28 | $response->code = Response::OK; |
---|
[6473] | 29 | |
---|
[6523] | 30 | $h = new Hypermedia(); |
---|
| 31 | $c = new Collection($request->resources, 'DynamicContactResource', $id); |
---|
[6473] | 32 | |
---|
[6523] | 33 | try { |
---|
| 34 | $dynamicContact = Controller::read( |
---|
| 35 | array('concept' => 'dynamicContact'), false, array('filter' => array('AND', array('=', 'owner', Config::me("uidNumber")), array('=', 'id', $id))) |
---|
| 36 | ); |
---|
[6473] | 37 | |
---|
[6523] | 38 | //Se nao foi encontrado contatos na consulta |
---|
| 39 | if (!$dynamicContact) { |
---|
| 40 | $this->createException($request, $response, Response::NOTFOUND, 'Bad request', 'Dynamic Contact not found.'); |
---|
| 41 | return $response; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | //Normaliza dado |
---|
| 45 | if(is_array($dynamicContact)) |
---|
| 46 | $dynamicContact = $dynamicContact[0]; |
---|
[6473] | 47 | |
---|
| 48 | |
---|
[6523] | 49 | $t = new Template(); |
---|
| 50 | $d = new Data(); |
---|
[6473] | 51 | |
---|
[6523] | 52 | $d->setName('name'); |
---|
| 53 | $d->setValue(null); |
---|
| 54 | $d->setPrompt('Nome do Contato Recente'); |
---|
| 55 | $d->setDataType('string'); |
---|
| 56 | $d->setMaxLength(100); |
---|
| 57 | $d->setMinLength(null); |
---|
| 58 | $d->setRequired(true); |
---|
[6473] | 59 | |
---|
[6523] | 60 | $t->addData($d); |
---|
[6473] | 61 | |
---|
[6523] | 62 | $d = new Data(); |
---|
| 63 | $d->setName('mail'); |
---|
| 64 | $d->setValue(null); |
---|
| 65 | $d->setPrompt('Email do Contato Recente'); |
---|
| 66 | $d->setDataType('string'); |
---|
| 67 | $d->setMaxLength(100); |
---|
| 68 | $d->setMinLength(null); |
---|
| 69 | $d->setRequired(true); |
---|
[6473] | 70 | |
---|
[6523] | 71 | $t->addData($d); |
---|
[6473] | 72 | |
---|
[6523] | 73 | $d = new Data(); |
---|
| 74 | $d->setName('number_of_messages'); |
---|
| 75 | $d->setValue(null); |
---|
| 76 | $d->setPrompt('Quantidade de mensagens enviadas'); |
---|
| 77 | $d->setDataType('integer'); |
---|
| 78 | $d->setMaxLength(100); |
---|
| 79 | $d->setMinLength(null); |
---|
| 80 | $d->setRequired(false); |
---|
[6473] | 81 | |
---|
[6523] | 82 | $t->addData($d); |
---|
[6473] | 83 | |
---|
[6523] | 84 | $c->setTemplate($t); |
---|
[6473] | 85 | |
---|
[6523] | 86 | $d = new Data(); |
---|
| 87 | $d->setName('id'); |
---|
| 88 | $d->setValue($dynamicContact['id']); |
---|
| 89 | $d->setPrompt('Identificador do Contato Recente'); |
---|
| 90 | $d->setDataType('integer'); |
---|
| 91 | $d->setMaxLength(null); |
---|
| 92 | $d->setMinLength(null); |
---|
| 93 | $d->setRequired(true); |
---|
[6473] | 94 | |
---|
[6523] | 95 | $c->addData($d); |
---|
[6473] | 96 | |
---|
[6523] | 97 | $d = new Data(); |
---|
[6473] | 98 | |
---|
[6523] | 99 | $d->setName('name'); |
---|
| 100 | $d->setValue($dynamicContact['name']); |
---|
| 101 | $d->setPrompt('Nome do Contato Recente'); |
---|
| 102 | $d->setDataType('string'); |
---|
| 103 | $d->setMaxLength('100'); |
---|
| 104 | $d->setMinLength(null); |
---|
| 105 | $d->setRequired(true); |
---|
[6473] | 106 | |
---|
[6523] | 107 | $c->addData($d); |
---|
[6473] | 108 | |
---|
[6523] | 109 | $d = new Data(); |
---|
| 110 | $d->setName('mail'); |
---|
| 111 | $d->setValue($dynamicContact['mail']); |
---|
| 112 | $d->setPrompt('Email do Contato Recente'); |
---|
| 113 | $d->setDataType('string'); |
---|
| 114 | $d->setMaxLength('100'); |
---|
| 115 | $d->setMinLength(null); |
---|
| 116 | $d->setRequired(true); |
---|
[6473] | 117 | |
---|
[6523] | 118 | $c->addData($d); |
---|
[6473] | 119 | |
---|
[6523] | 120 | $d = new Data(); |
---|
| 121 | $d->setName('number_of_messages'); |
---|
| 122 | $d->setValue($dynamicContact['number_of_messages']); |
---|
| 123 | $d->setPrompt('Quantidade de mensagens enviadas'); |
---|
| 124 | $d->setDataType('integer'); |
---|
| 125 | $d->setMaxLength('100'); |
---|
| 126 | $d->setMinLength(null); |
---|
| 127 | $d->setRequired(false); |
---|
[6473] | 128 | |
---|
[6523] | 129 | $c->addData($d); |
---|
[6473] | 130 | |
---|
| 131 | |
---|
[6523] | 132 | $l = new Link(); |
---|
[6473] | 133 | |
---|
[6523] | 134 | $l->setHref(''); |
---|
| 135 | $l->setRel('delete'); |
---|
| 136 | $l->setAlt('Remover'); |
---|
| 137 | $l->setPrompt('Remover'); |
---|
| 138 | $l->setRender('link'); |
---|
[6473] | 139 | |
---|
[6523] | 140 | $c->addLink($l); |
---|
[6473] | 141 | |
---|
[6523] | 142 | $l = new Link(); |
---|
| 143 | $l->setHref(''); |
---|
| 144 | $l->setRel('put'); |
---|
| 145 | $l->setAlt('Atualizar'); |
---|
| 146 | $l->setPrompt('Atualizar'); |
---|
| 147 | $l->setRender('link'); |
---|
[6473] | 148 | |
---|
[6523] | 149 | $c->addLink($l); |
---|
[6473] | 150 | |
---|
| 151 | $h->setCollection($c); |
---|
[6523] | 152 | } catch (Exception $ex) { |
---|
| 153 | $this->createException($request, $response, Response::INTERNALSERVERERROR, 'Internal Server Error', 'Internal Server Error'); |
---|
| 154 | return $response; |
---|
| 155 | } |
---|
[6473] | 156 | |
---|
[6523] | 157 | $response->body = $h->getHypermedia($request->accept[10][0]); |
---|
| 158 | return $response; |
---|
| 159 | } |
---|
[6473] | 160 | |
---|
| 161 | /** |
---|
| 162 | * Atualiza um contato recente |
---|
| 163 | * |
---|
| 164 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 165 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 166 | * @sponsor Caixa Econômica Federal |
---|
| 167 | * @author José Vicente Tezza Jr. |
---|
| 168 | * @return retorna a uri do contato recente |
---|
| 169 | * @access public |
---|
| 170 | * */ |
---|
[6523] | 171 | function put($request, $id) { |
---|
| 172 | |
---|
| 173 | $this->secured(); |
---|
[6473] | 174 | |
---|
[6523] | 175 | $post = $request->dataDecoded; |
---|
| 176 | $response = new Response($request); |
---|
[6473] | 177 | |
---|
[6523] | 178 | if (count($post) == 0){ |
---|
| 179 | $this->createException($request, $response, Response::BADREQUEST, 'Bad request', 'Invalid template data'); |
---|
| 180 | return $response; |
---|
| 181 | } |
---|
[6473] | 182 | |
---|
[6523] | 183 | //recupera os atributos definidos no conceito 'user' |
---|
| 184 | $map = Config::get('dynamicContact', 'PostgreSQL.mapping'); |
---|
[6473] | 185 | |
---|
[6523] | 186 | $params = array(); |
---|
| 187 | foreach ($post as $key => $value) { |
---|
[6473] | 188 | |
---|
[6523] | 189 | if (!isset($map[$key]) || $key == 'id' || $key == 'timestamp' || $key == 'number_of_messages') { |
---|
| 190 | continue; |
---|
| 191 | } |
---|
| 192 | $params[$key] = $value; |
---|
| 193 | } |
---|
[6473] | 194 | |
---|
[6523] | 195 | if (count($params) == 0) { |
---|
| 196 | $this->createException($request, $response, Response::BADREQUEST, 'Bad request', 'Invalid template data'); |
---|
| 197 | return $response; |
---|
| 198 | } |
---|
[6473] | 199 | |
---|
[6523] | 200 | //completar os atributos |
---|
| 201 | $params['owner'] = Config::me("uidNumber"); |
---|
| 202 | $params['timestamp'] = time(); |
---|
| 203 | $params['id'] = $id; |
---|
[6473] | 204 | |
---|
[6523] | 205 | $response->addHeader('Content-type', 'aplication/json'); |
---|
| 206 | $response->code = Response::NOCONTENT; |
---|
[6473] | 207 | |
---|
[6523] | 208 | try { |
---|
[6473] | 209 | |
---|
[6523] | 210 | $dynamicContact = Controller::read( |
---|
| 211 | array('concept' => 'dynamicContact'), false, array('filter' => array( |
---|
| 212 | 'AND', |
---|
| 213 | array('=', 'owner', Config::me("uidNumber")), |
---|
| 214 | array('=', 'id', $id))) |
---|
| 215 | ); |
---|
[6473] | 216 | |
---|
| 217 | |
---|
[6523] | 218 | //Se existe o recurso |
---|
| 219 | if ($dynamicContact) { |
---|
| 220 | //Normaliza o recurso |
---|
| 221 | if(is_array($dynamicContact)) |
---|
| 222 | $dynamicContact = $dynamicContact[0]; |
---|
[6512] | 223 | |
---|
[6523] | 224 | $params['number_of_messages'] = $dynamicContact['number_of_messages'] + 1; |
---|
[6473] | 225 | |
---|
[6523] | 226 | $dynamicContact = Controller::update(array('concept' => 'dynamicContact', 'id' => $id), $params); |
---|
[6473] | 227 | |
---|
[6523] | 228 | if (!$dynamicContact) { |
---|
| 229 | $this->createException($request, $response, Response::INTERNALSERVERERROR, 'Internal Server Error', Controller::service('PostgreSQL')->error); |
---|
| 230 | return $response; |
---|
| 231 | } |
---|
| 232 | } else { |
---|
| 233 | /* |
---|
| 234 | $idDynamicContact = Controller::create( array('concept' => 'dynamicContact'), $params); |
---|
| 235 | */ |
---|
| 236 | //if (!$idDynamicContact) { |
---|
| 237 | $this->createException($request, $response, Response::NOTFOUND, 'Bad request', 'Invalid data'); |
---|
| 238 | return $response; |
---|
| 239 | //} |
---|
| 240 | } |
---|
| 241 | } catch (Exception $ex) { |
---|
| 242 | $this->createException($request, $response, Response::INTERNALSERVERERROR, 'Internal Server Error', 'Internal Server Error'); |
---|
| 243 | return $response; |
---|
| 244 | } |
---|
| 245 | $response->body = json_encode(null); |
---|
| 246 | return $response; |
---|
| 247 | } |
---|
[6473] | 248 | |
---|
| 249 | /** |
---|
| 250 | * Remove um contato recente |
---|
| 251 | * |
---|
| 252 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 253 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 254 | * @sponsor Caixa Econômica Federal |
---|
| 255 | * @author José Vicente Tezza Jr. |
---|
| 256 | * @access public |
---|
| 257 | * */ |
---|
[6523] | 258 | function delete($request, $id) { |
---|
[6473] | 259 | |
---|
[6523] | 260 | $this->secured(); |
---|
| 261 | |
---|
[6473] | 262 | $response = new Response($request); |
---|
[6523] | 263 | $response->addHeader('Content-type', 'aplication/json'); |
---|
[6473] | 264 | $response->code = Response::NOCONTENT; |
---|
| 265 | |
---|
[6523] | 266 | try { |
---|
| 267 | //Verifica se o recurso existe |
---|
| 268 | $dinamicContact = Controller::read(array('concept' => 'dynamicContact', 'id' => $id)); |
---|
[6473] | 269 | |
---|
[6523] | 270 | //Se existe o recurso |
---|
| 271 | if ($dinamicContact) { |
---|
[6473] | 272 | |
---|
[6523] | 273 | $delete = Controller::delete(array('concept' => 'dynamicContact', 'id' => $id)); |
---|
[6473] | 274 | |
---|
[6523] | 275 | if (!$delete) { |
---|
| 276 | $this->createException($request, $response, Response::INTERNALSERVERERROR, 'Internal Server Error', Controller::service('PostgreSQL')->error); |
---|
| 277 | return $response; |
---|
[6473] | 278 | } |
---|
[6523] | 279 | } else { |
---|
| 280 | $this->createException($request, $response, Response::NOTFOUND, 'Bad request', 'Invalid data'); |
---|
| 281 | return $response; |
---|
| 282 | } |
---|
| 283 | } catch (Exception $ex) { |
---|
| 284 | $this->createException($request, $response, Response::INTERNALSERVERERROR, 'Internal Server Error', 'Internal Server Error'); |
---|
| 285 | return $response; |
---|
[6473] | 286 | } |
---|
| 287 | |
---|
[6523] | 288 | $response->body = json_encode(null); |
---|
| 289 | return $response; |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | private function createException($request, &$response, $code, $title, $description) { |
---|
| 293 | $response->code = $code; |
---|
| 294 | |
---|
| 295 | $h = new Hypermedia(); |
---|
| 296 | $c = new Collection($request->resources, 'DynamicContactResource'); |
---|
| 297 | $e = new Error(); |
---|
[6473] | 298 | |
---|
[6523] | 299 | $e->setCode($code); |
---|
| 300 | $e->setTitle($title); |
---|
| 301 | $e->setDescription($description); |
---|
| 302 | |
---|
| 303 | $c->setError($e); |
---|
| 304 | $h->setCollection($c); |
---|
[6473] | 305 | |
---|
[6523] | 306 | $response->body = $h->getHypermedia($request->accept[10][0]); |
---|
| 307 | } |
---|
| 308 | |
---|
[6473] | 309 | } |
---|
[6523] | 310 | |
---|
[6473] | 311 | ?> |
---|