[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 DynamicContactsResource extends Resource { |
---|
| 11 | |
---|
| 12 | /** |
---|
| 13 | * Retorna uma lista de contatos recentes |
---|
| 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 uma lista de contatos recentes |
---|
| 20 | * @access public |
---|
| 21 | * */ |
---|
| 22 | function get($request) { |
---|
| 23 | |
---|
[6523] | 24 | $this->secured(); |
---|
| 25 | |
---|
[6473] | 26 | $response = new Response($request); |
---|
| 27 | $response->addHeader('Content-type', 'aplication/json'); |
---|
| 28 | $response->code = Response::OK; |
---|
| 29 | |
---|
| 30 | $h = new Hypermedia(); |
---|
| 31 | $c = new Collection($request->resources, 'DynamicContactsResource'); |
---|
| 32 | |
---|
| 33 | try { |
---|
| 34 | |
---|
[6523] | 35 | $dynamicContacts = Controller::find( |
---|
| 36 | array('concept' => 'dynamicContact'), false, array('filter' => array('=', 'owner', Config::me("uidNumber")), |
---|
| 37 | 'order' => array('number_of_messages', 'name', 'mail'), |
---|
| 38 | 'orderDesc' => array(true, false, false)) |
---|
[6473] | 39 | ); |
---|
| 40 | |
---|
| 41 | //Se nao foi encontrado contatos na consulta |
---|
[6523] | 42 | if (!$dynamicContacts) { |
---|
| 43 | $this->createException($request, $response, Response::NOTFOUND, 'Bad request', 'Dynamic Contact not found.'); |
---|
[6473] | 44 | return $response; |
---|
| 45 | } |
---|
| 46 | |
---|
[6523] | 47 | foreach ($dynamicContacts as $value) { |
---|
[6473] | 48 | $d = new Data(); |
---|
[6523] | 49 | $i = new Item($request->resources, 'DynamicContactsResource', $value['id']); |
---|
[6473] | 50 | |
---|
| 51 | $d->setName('name'); |
---|
[6523] | 52 | $d->setValue($value['name']); |
---|
| 53 | $d->setPrompt('Nome do Contato Recente'); |
---|
| 54 | $d->setDataType('string'); |
---|
| 55 | $d->setMaxLength('100'); |
---|
| 56 | $d->setMinLength(null); |
---|
| 57 | $d->setRequired(true); |
---|
[6473] | 58 | |
---|
[6523] | 59 | $i->addData($d); |
---|
[6473] | 60 | |
---|
| 61 | $d = new Data(); |
---|
[6523] | 62 | $d->setName('mail'); |
---|
| 63 | $d->setValue($value['mail']); |
---|
| 64 | $d->setPrompt('Email do Contato Recente'); |
---|
| 65 | $d->setDataType('string'); |
---|
| 66 | $d->setMaxLength('100'); |
---|
| 67 | $d->setMinLength(null); |
---|
| 68 | $d->setRequired(true); |
---|
[6473] | 69 | |
---|
[6523] | 70 | $i->addData($d); |
---|
[6473] | 71 | |
---|
| 72 | $d = new Data(); |
---|
[6523] | 73 | $d->setName('number_of_messages'); |
---|
| 74 | $d->setValue($value['number_of_messages']); |
---|
| 75 | $d->setPrompt('Quantidade de mensagens enviadas'); |
---|
| 76 | $d->setDataType('integer'); |
---|
| 77 | $d->setMaxLength('100'); |
---|
| 78 | $d->setMinLength(null); |
---|
| 79 | $d->setRequired(false); |
---|
[6473] | 80 | |
---|
[6523] | 81 | $i->addData($d); |
---|
[6473] | 82 | |
---|
[6523] | 83 | $d = new Data(); |
---|
| 84 | $d->setName('id'); |
---|
| 85 | $d->setValue($value['id']); |
---|
| 86 | $d->setPrompt('Id do contato dinamico'); |
---|
| 87 | $d->setDataType('integer'); |
---|
| 88 | $d->setMaxLength('100'); |
---|
| 89 | $d->setMinLength(null); |
---|
| 90 | $d->setRequired(false); |
---|
[6512] | 91 | |
---|
[6523] | 92 | $i->addData($d); |
---|
[6473] | 93 | |
---|
[6523] | 94 | $l = new Link(); |
---|
[6473] | 95 | |
---|
[6523] | 96 | $l->setHref(''); |
---|
| 97 | $l->setRel('delete'); |
---|
| 98 | $l->setAlt('Remover'); |
---|
| 99 | $l->setPrompt('Remover'); |
---|
| 100 | $l->setRender('link'); |
---|
[6473] | 101 | |
---|
[6523] | 102 | $i->addLink($l); |
---|
[6473] | 103 | |
---|
[6523] | 104 | $l = new Link(); |
---|
| 105 | $l->setHref(''); |
---|
| 106 | $l->setRel('put'); |
---|
| 107 | $l->setAlt('Atualizar'); |
---|
| 108 | $l->setPrompt('Atualizar'); |
---|
| 109 | $l->setRender('link'); |
---|
[6473] | 110 | |
---|
[6523] | 111 | $i->addLink($l); |
---|
[6473] | 112 | |
---|
[6523] | 113 | $l = new Link(); |
---|
| 114 | $l->setHref('/dynamiccontact/' . $value['id']); |
---|
| 115 | $l->setRel('get'); |
---|
| 116 | $l->setAlt('Buscar'); |
---|
| 117 | $l->setPrompt('Buscar'); |
---|
| 118 | $l->setRender('link'); |
---|
| 119 | |
---|
| 120 | $i->addLink($l); |
---|
| 121 | $c->addItem($i); |
---|
[6473] | 122 | } |
---|
| 123 | |
---|
[6523] | 124 | $t = new Template(); |
---|
| 125 | $d = new Data(); |
---|
[6473] | 126 | |
---|
[6523] | 127 | $d->setName('name'); |
---|
| 128 | $d->setValue(null); |
---|
| 129 | $d->setPrompt('Nome do Contato Recente'); |
---|
| 130 | $d->setDataType('string'); |
---|
| 131 | $d->setMaxLength(100); |
---|
| 132 | $d->setMinLength(null); |
---|
| 133 | $d->setRequired(false); |
---|
[6473] | 134 | |
---|
[6523] | 135 | $t->addData($d); |
---|
[6473] | 136 | |
---|
[6523] | 137 | $d = new Data(); |
---|
| 138 | $d->setName('mail'); |
---|
| 139 | $d->setValue(null); |
---|
| 140 | $d->setPrompt('Email do Contato Recente'); |
---|
| 141 | $d->setDataType('string'); |
---|
| 142 | $d->setMaxLength(100); |
---|
| 143 | $d->setMinLength(null); |
---|
| 144 | $d->setRequired(true); |
---|
[6473] | 145 | |
---|
[6523] | 146 | $t->addData($d); |
---|
[6473] | 147 | |
---|
[6523] | 148 | $d = new Data(); |
---|
| 149 | $d->setName('number_of_messages'); |
---|
| 150 | $d->setValue(null); |
---|
| 151 | $d->setPrompt('Quantidade de mensagens enviadas'); |
---|
| 152 | $d->setDataType('integer'); |
---|
| 153 | $d->setMaxLength(100); |
---|
| 154 | $d->setMinLength(null); |
---|
| 155 | $d->setRequired(false); |
---|
[6473] | 156 | |
---|
[6523] | 157 | $t->addData($d); |
---|
[6473] | 158 | |
---|
[6523] | 159 | $c->setTemplate($t); |
---|
[6473] | 160 | |
---|
[6523] | 161 | $h->setCollection($c); |
---|
| 162 | } catch (Exception $ex) { |
---|
| 163 | $this->createException($request, $response, Response::INTERNALSERVERERROR, 'Internal Server Error', $ex); |
---|
| 164 | return $response; |
---|
[6473] | 165 | } |
---|
| 166 | |
---|
| 167 | $response->body = $h->getHypermedia($request->accept[10][0]); |
---|
| 168 | return $response; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | /** |
---|
| 172 | * Salva um contato recente |
---|
| 173 | * |
---|
| 174 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 175 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 176 | * @sponsor Caixa Econômica Federal |
---|
| 177 | * @author José Vicente Tezza Jr. |
---|
| 178 | * @return retorna a uri do contato recente |
---|
| 179 | * @access public |
---|
| 180 | * */ |
---|
[6523] | 181 | function post($request) { |
---|
[6473] | 182 | |
---|
[6523] | 183 | $this->secured(); |
---|
| 184 | |
---|
| 185 | if (count($_POST) == 0) { |
---|
| 186 | $response = new Response($request); |
---|
| 187 | $response->code = Response::INTERNALSERVERERROR; |
---|
| 188 | return $response; |
---|
[6473] | 189 | } |
---|
| 190 | |
---|
| 191 | //recupera os atributos definidos no conceito 'user' |
---|
| 192 | $map = Config::get('dynamicContact', 'PostgreSQL.mapping'); |
---|
| 193 | |
---|
| 194 | $params = array(); |
---|
[6523] | 195 | foreach ($_POST as $key => $value) { |
---|
[6473] | 196 | |
---|
[6523] | 197 | if (!isset($map[$key]) || $key == 'id' || $key == 'timestamp' || $key == 'number_of_messages') { |
---|
| 198 | continue; |
---|
| 199 | } |
---|
| 200 | $params[$key] = $value; |
---|
[6473] | 201 | } |
---|
| 202 | |
---|
[6523] | 203 | if (count($params) == 0) { |
---|
| 204 | $response = new Response($request); |
---|
| 205 | $response->code = Response::INTERNALSERVERERROR; |
---|
| 206 | return $response; |
---|
[6473] | 207 | } |
---|
| 208 | |
---|
| 209 | |
---|
[6523] | 210 | $response = new Response($request); |
---|
| 211 | $response->addHeader('Content-type', 'aplication/json'); |
---|
| 212 | $response->code = Response::CREATED; |
---|
[6473] | 213 | |
---|
[6523] | 214 | |
---|
[6473] | 215 | //completar os atributos |
---|
[6523] | 216 | $params['owner'] = Config::me("uidNumber"); |
---|
| 217 | $params['number_of_messages'] = '1'; |
---|
| 218 | $params['timestamp'] = time(); |
---|
[6473] | 219 | |
---|
| 220 | |
---|
[6523] | 221 | try { |
---|
| 222 | $idDynamicContact = Controller::create( |
---|
| 223 | array('concept' => 'dynamicContact'), $params |
---|
| 224 | ); |
---|
[6473] | 225 | |
---|
[6523] | 226 | if (!$idDynamicContact) { |
---|
| 227 | throw new Exception(Controller::service('PostgreSQL')->error); |
---|
| 228 | } |
---|
| 229 | } catch (Exception $ex) { |
---|
| 230 | $response->code = Response::INTERNALSERVERERROR; |
---|
| 231 | return $response; |
---|
| 232 | } |
---|
[6473] | 233 | |
---|
[6523] | 234 | $response->body = json_encode(null); |
---|
[6473] | 235 | |
---|
[6523] | 236 | return $response; |
---|
| 237 | } |
---|
[6473] | 238 | |
---|
[6523] | 239 | private function createException($request, &$response, $code, $title, $description) { |
---|
| 240 | $response->code = $code; |
---|
[6473] | 241 | |
---|
[6523] | 242 | $h = new Hypermedia(); |
---|
| 243 | $c = new Collection($request->resources, 'DynamicContactResource'); |
---|
| 244 | $e = new Error(); |
---|
[6473] | 245 | |
---|
[6523] | 246 | $e->setCode($code); |
---|
| 247 | $e->setTitle($title); |
---|
| 248 | $e->setDescription($description); |
---|
[6473] | 249 | |
---|
[6523] | 250 | $c->setError($e); |
---|
| 251 | $h->setCollection($c); |
---|
| 252 | |
---|
| 253 | $response->body = $h->getHypermedia($request->accept[10][0]); |
---|
| 254 | } |
---|
| 255 | |
---|
[6473] | 256 | } |
---|
[6523] | 257 | |
---|
[6473] | 258 | ?> |
---|