[6473] | 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 | $response = new Response($request); |
---|
| 25 | $response->addHeader('Content-type', 'aplication/json'); |
---|
| 26 | $response->code = Response::OK; |
---|
| 27 | |
---|
| 28 | $h = new Hypermedia(); |
---|
| 29 | $c = new Collection($request->resources, 'DynamicContactResource'); |
---|
| 30 | |
---|
| 31 | try { |
---|
| 32 | // $this->secured(); |
---|
| 33 | |
---|
| 34 | $dynamicContacts = Controller::find( |
---|
| 35 | array( 'concept' => 'dynamicContact' ), |
---|
| 36 | false, |
---|
| 37 | array( 'filter' => array( 'AND', array( '=', 'owner', Config::me("uidNumber") ) , array('=', 'id', $id) ) ) |
---|
| 38 | ); |
---|
| 39 | |
---|
| 40 | //Se nao foi encontrado contatos na consulta |
---|
| 41 | if($dynamicContacts===false){ |
---|
| 42 | $error = new Error(); |
---|
| 43 | $error->setCode(Response::NOTFOUND); |
---|
| 44 | $error->setTitle('Dynamic Contact not found'); |
---|
| 45 | $error->setDescription('Dynamic Contact not found.'); |
---|
| 46 | |
---|
| 47 | $c->setError($error); |
---|
| 48 | $h->setCollection($c); |
---|
| 49 | |
---|
| 50 | $response->code = Response::NOTFOUND; |
---|
| 51 | $response->body = $h->getHypermedia($request->accept[10][0]); |
---|
| 52 | return $response; |
---|
| 53 | } |
---|
| 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($dynamicContacts[0]['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($dynamicContacts[0]['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($dynamicContacts[0]['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($dynamicContacts[0]['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 | |
---|
| 160 | |
---|
| 161 | }catch (Exception $ex){ |
---|
| 162 | $error = new Error(); |
---|
| 163 | $error->setCode(Response::INTERNALSERVERERROR); |
---|
| 164 | $error->setTitle('Internal Server Error'); |
---|
| 165 | $error->setDescription($ex); |
---|
| 166 | |
---|
| 167 | $c->setError($error); |
---|
| 168 | $h->setCollection($c); |
---|
| 169 | |
---|
| 170 | $response->code = Response::INTERNALSERVERERROR; |
---|
| 171 | $response->body = $h->getHypermedia($request->accept[10][0]); |
---|
| 172 | return $response; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | $response->body = $h->getHypermedia($request->accept[10][0]); |
---|
| 176 | return $response; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | /** |
---|
| 180 | * Atualiza um contato recente |
---|
| 181 | * |
---|
| 182 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 183 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 184 | * @sponsor Caixa Econômica Federal |
---|
| 185 | * @author José Vicente Tezza Jr. |
---|
| 186 | * @return retorna a uri do contato recente |
---|
| 187 | * @access public |
---|
| 188 | * */ |
---|
| 189 | function put($request, $id){ |
---|
| 190 | $post = $request->dataDecoded; |
---|
| 191 | |
---|
| 192 | if(count($post) == 0){ |
---|
| 193 | $response = new Response($request); |
---|
| 194 | $response->code = Response::INTERNALSERVERERROR; |
---|
| 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 | $response = new Response($request); |
---|
| 212 | $response->code = Response::INTERNALSERVERERROR; |
---|
| 213 | return $response; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | //completar os atributos |
---|
| 217 | $params['owner'] = Config::me("uidNumber"); |
---|
| 218 | $params['timestamp'] = time(); |
---|
| 219 | $params['id'] = $id; |
---|
| 220 | |
---|
| 221 | |
---|
| 222 | $response = new Response($request); |
---|
| 223 | // $response->addHeader('Content-type', 'aplication/json'); |
---|
| 224 | // $response->code = Response::NOCONTENT; |
---|
| 225 | |
---|
| 226 | try{ |
---|
| 227 | |
---|
| 228 | // $this->secured(); |
---|
| 229 | |
---|
| 230 | $dynamicContact = Controller::read( |
---|
| 231 | array( 'concept' => 'dynamicContact' ), |
---|
| 232 | false, |
---|
| 233 | array( 'filter' => array( |
---|
| 234 | 'AND', |
---|
| 235 | array('=', 'owner', Config::me("uidNumber") ), |
---|
| 236 | array('=', 'id', $id) ) ) |
---|
| 237 | ); |
---|
| 238 | |
---|
[6512] | 239 | |
---|
[6473] | 240 | //Se existe o recurso |
---|
| 241 | if($dynamicContact){ |
---|
[6512] | 242 | $params['number_of_messages'] = $dynamicContact[0]['number_of_messages'] + 1; |
---|
| 243 | $dynamicContact = Controller::update( |
---|
| 244 | array('concept' => 'dynamicContact', |
---|
| 245 | 'id'=>$id), |
---|
[6473] | 246 | $params |
---|
| 247 | ); |
---|
| 248 | |
---|
| 249 | if(!$dynamicContact){ |
---|
| 250 | throw new Exception(Controller::service('PostgreSQL')->error); |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | } |
---|
| 254 | else{ |
---|
[6512] | 255 | $params['number_of_messages'] = '1'; |
---|
[6473] | 256 | $idDynamicContact = Controller::create( |
---|
| 257 | array('concept' => 'dynamicContact'), |
---|
| 258 | $params |
---|
| 259 | ); |
---|
| 260 | |
---|
| 261 | if(!$idDynamicContact){ |
---|
| 262 | throw new Exception(Controller::service('PostgreSQL')->error); |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | }catch (Exception $ex){ |
---|
| 268 | $response->code = Response::INTERNALSERVERERROR; |
---|
| 269 | return $response; |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | return $response; |
---|
| 273 | |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | /** |
---|
| 277 | * Remove um contato recente |
---|
| 278 | * |
---|
| 279 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 280 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 281 | * @sponsor Caixa Econômica Federal |
---|
| 282 | * @author José Vicente Tezza Jr. |
---|
| 283 | * @access public |
---|
| 284 | * */ |
---|
| 285 | function delete($request, $id){ |
---|
| 286 | |
---|
| 287 | $response = new Response($request); |
---|
| 288 | // $response->addHeader('Content-type', 'aplication/json'); |
---|
| 289 | $response->code = Response::NOCONTENT; |
---|
| 290 | |
---|
| 291 | try{ |
---|
| 292 | //Verifica se o recurso existe |
---|
| 293 | $responseGET = $this->get($request, $id); |
---|
| 294 | $code = $responseGET->code; |
---|
| 295 | |
---|
| 296 | //Se existe o recurso |
---|
| 297 | if($code == Response::OK){ |
---|
| 298 | |
---|
| 299 | // $this->secured(); |
---|
| 300 | |
---|
| 301 | $delete = Controller::delete(array('concept' => 'dynamicContact'), false, array('filter' => array('=', 'id', $id))); |
---|
| 302 | |
---|
| 303 | if(!$delete){ |
---|
| 304 | throw new Exception(Controller::service('PostgreSQL')->error); |
---|
| 305 | } |
---|
| 306 | } |
---|
| 307 | else{ |
---|
| 308 | $response->code = Response::NOTFOUND; |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | }catch (Exception $ex){ |
---|
| 312 | $response->code = Response::INTERNALSERVERERROR; |
---|
| 313 | return $response; |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | $response->body = json_encode(array('ABC'=>'123')); |
---|
| 317 | |
---|
| 318 | return $response; |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | } |
---|
| 322 | ?> |
---|