[6351] | 1 | <?php |
---|
| 2 | |
---|
[6357] | 3 | use prototype\api\Config as Config; |
---|
| 4 | |
---|
[6351] | 5 | class DynamicContactResource extends Resource { |
---|
| 6 | |
---|
| 7 | /** |
---|
| 8 | * Busca um contato dinâmico da lista 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 um item da colection de contatos dinâmicos de um usuário |
---|
| 16 | * @access public |
---|
| 17 | * */ |
---|
| 18 | function get($request, $id) { |
---|
| 19 | try { |
---|
| 20 | $response = new Response($request); |
---|
| 21 | $response->addHeader('Content-type', 'aplication/json'); |
---|
| 22 | |
---|
| 23 | $isValidId = $this->isValidId($id); |
---|
| 24 | |
---|
| 25 | if (!$isValidId) { |
---|
| 26 | $this->createException($response, Response::BADREQUEST, 'Malformed URL', 'Malformed URL'); |
---|
| 27 | return $response; |
---|
| 28 | } |
---|
| 29 | $response->code = Response::OK; |
---|
| 30 | |
---|
| 31 | $this->secured(); |
---|
| 32 | //$dynamicContact = false; |
---|
| 33 | $dynamicContact = Controller::find(array('concept' => 'dynamicContact'), false, array('filter' => array('AND', array('=', 'owner', Config::me("uidNumber")), array('=', 'id', $id)))); |
---|
| 34 | |
---|
| 35 | $resourceHref = str_replace($request->baseUri, '', $request->uri); |
---|
| 36 | |
---|
| 37 | if ($dynamicContact) { |
---|
| 38 | $data = array(); |
---|
| 39 | foreach ($dynamicContact as $key => $value) { |
---|
| 40 | $data[$key] = array( |
---|
| 41 | 'href' => '/dynamiccontacts/' . $value['id'], |
---|
| 42 | 'data' => array( |
---|
| 43 | array( |
---|
| 44 | 'name' => "name", |
---|
| 45 | 'value' => $value['name'], |
---|
| 46 | 'prompt' => "Nome do Contato" |
---|
| 47 | ), |
---|
| 48 | array( |
---|
| 49 | "name" => "email", |
---|
| 50 | "value" => $value['mail'], |
---|
| 51 | "prompt" => "Email do Contato" |
---|
| 52 | ) |
---|
| 53 | ), |
---|
| 54 | 'links' => array( |
---|
| 55 | array( |
---|
| 56 | 'href' => $resourceHref, |
---|
| 57 | 'rel' => 'delete', |
---|
| 58 | 'prompt' => 'Remover contato dinamico', |
---|
| 59 | 'name' => 'delete-contact', |
---|
| 60 | 'render' => 'link' |
---|
| 61 | ), |
---|
| 62 | array( |
---|
| 63 | 'href' => $resourceHref, |
---|
| 64 | 'rel' => 'update', |
---|
| 65 | 'prompt' => 'Editar contato dinamico', |
---|
| 66 | 'name' => 'edit-contact', |
---|
| 67 | 'render' => 'link' |
---|
| 68 | ) |
---|
| 69 | ) |
---|
| 70 | ); |
---|
| 71 | } |
---|
| 72 | } else { |
---|
| 73 | $this->createException($response, Response::NOTFOUND, 'Contact not found', ( $isValidId ? "The contact " . $id . " wasn't found on this server" : 'url malformed')); |
---|
| 74 | return $response; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | $result = array( |
---|
| 78 | "version" => "0.1", |
---|
| 79 | "collection" => array( |
---|
| 80 | "href" => $resourceHref, |
---|
| 81 | "type" => "dynamic-contact", |
---|
| 82 | "data" => array( |
---|
| 83 | "name" => "dynamic-contact-" . $id, |
---|
| 84 | "prompt" => "Contato dinâmico" |
---|
| 85 | ), |
---|
| 86 | "pagination" => null, |
---|
| 87 | "items" => $data, |
---|
| 88 | "queries" => null, |
---|
| 89 | "template" => array( |
---|
| 90 | "data" => array( |
---|
| 91 | array( |
---|
| 92 | "name" => "name", |
---|
| 93 | "value" => "", |
---|
| 94 | "prompt" => "Nome do contato", |
---|
| 95 | "dataType" => "string", |
---|
| 96 | "minLength" => 0, |
---|
| 97 | "maxLength" => 100, |
---|
| 98 | "required" => false |
---|
| 99 | ), |
---|
| 100 | array( |
---|
| 101 | "name" => "email", |
---|
| 102 | "value" => "", |
---|
| 103 | "prompt" => "Email do contato", |
---|
| 104 | "dataType" => "string", |
---|
| 105 | "minLength" => 0, |
---|
| 106 | "maxLength" => 100, |
---|
| 107 | "required" => true |
---|
| 108 | ) |
---|
| 109 | ) |
---|
| 110 | ), |
---|
| 111 | 'error' => null |
---|
| 112 | ) |
---|
| 113 | ); |
---|
| 114 | } catch (Exception $ex) { |
---|
| 115 | $this->createException($response, Response::INTERNALSERVERERROR, 'Internal Server Error', $ex); |
---|
| 116 | return $response; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | $response->body = json_encode($result); |
---|
| 120 | return $response; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | /** |
---|
| 124 | * Deleta um item da coleção de contatos dinâmicos |
---|
| 125 | * |
---|
| 126 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 127 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 128 | * @sponsor Caixa Econômica Federal |
---|
| 129 | * @author Adriano Coutinho da Silva |
---|
| 130 | * @author Adir Kuhn |
---|
| 131 | * @return status http |
---|
| 132 | * @access public |
---|
| 133 | * */ |
---|
| 134 | function delete($request, $id) { |
---|
| 135 | |
---|
| 136 | try { |
---|
| 137 | $response = new Response($request); |
---|
| 138 | $response->addHeader('Content-type', 'aplication/json'); |
---|
| 139 | |
---|
| 140 | $isValidId = $this->isValidId($id); |
---|
| 141 | if (!$isValidId) { |
---|
| 142 | $this->createException($response, Response::BADREQUEST, 'Malformed URL', 'Malformed URL'); |
---|
| 143 | return $response; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | $this->secured(); |
---|
| 147 | |
---|
| 148 | $response->code = Response::OK; |
---|
| 149 | $error = null; |
---|
| 150 | |
---|
| 151 | $exists = Controller::find(array('concept' => 'dynamicContact'), false, array('filter' => array('AND', |
---|
| 152 | array('=', 'owner', Config::me("uidNumber")), |
---|
| 153 | array('=', 'id', $id) |
---|
| 154 | ))); |
---|
| 155 | |
---|
| 156 | if (!$exists) { |
---|
| 157 | $this->createException($response, Response::BADREQUEST, 'Contact not found', "The contact " . $id . " wasn't found on this server"); |
---|
| 158 | return $response; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | $deletedDynamicContact = Controller::delete(array('concept' => 'dynamicContact'), false, array('filter' => array('=', 'id', $id))); |
---|
| 162 | |
---|
| 163 | if (!$deletedDynamicContact) |
---|
| 164 | $error = array( |
---|
| 165 | 'code' => '500', |
---|
| 166 | 'title' => 'Internal Server Error', |
---|
| 167 | 'description' => Controller::service('PostgreSQL')->error |
---|
| 168 | ); |
---|
| 169 | } catch (Exception $ex) { |
---|
| 170 | $this->createException($response, Response::INTERNALSERVERERROR, 'Internal Server Error', $ex); |
---|
| 171 | return $response; |
---|
| 172 | } |
---|
| 173 | return $response; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | /** |
---|
| 177 | * Atualiza um item da coleção de contatos dinâmicos |
---|
| 178 | * |
---|
| 179 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 180 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 181 | * @sponsor Caixa Econômica Federal |
---|
| 182 | * @author Adriano Coutinho da Silva |
---|
| 183 | * @author Adir Kuhn |
---|
| 184 | * @return status http |
---|
| 185 | * @access public |
---|
| 186 | * */ |
---|
| 187 | function put($request, $id) { |
---|
| 188 | try { |
---|
| 189 | $response = new Response($request); |
---|
| 190 | $response->addHeader('Content-type', 'aplication/json'); |
---|
| 191 | |
---|
| 192 | $isValidId = $this->isValidId($id); |
---|
| 193 | if (!$isValidId) { |
---|
| 194 | $this->createException($response, Response::BADREQUEST, 'Malformed URL', 'Malformed URL'); |
---|
| 195 | return $response; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | $data = $request->dataDecoded; |
---|
| 199 | |
---|
| 200 | if ($this->validData($data)) { |
---|
| 201 | |
---|
| 202 | $this->secured(); |
---|
| 203 | |
---|
| 204 | $response->code = Response::OK; |
---|
| 205 | $exists = Controller::find(array('concept' => 'dynamicContact'), false, array('filter' => array('AND', |
---|
| 206 | array('=', 'owner', Config::me("uidNumber")), |
---|
| 207 | array('=', 'mail', $data['email']) |
---|
| 208 | ))); |
---|
| 209 | |
---|
| 210 | if (!empty($exists)) { |
---|
| 211 | $this->createException($response, Response::BADREQUEST, 'Bad request', Controller::service('PostgreSQL')->error); |
---|
| 212 | return $response; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | $newDynamicContact = Controller::update(array('concept' => 'dynamicContact', 'id' => $id), array('name' => $data['name'], 'mail' => $data['email'], 'timestamp' => time())); |
---|
| 216 | |
---|
| 217 | if (!$newDynamicContact) |
---|
| 218 | $this->createException($response, Response::INTERNALSERVERERROR, 'Internal Server Error', Controller::service('PostgreSQL')->error); |
---|
| 219 | } else |
---|
| 220 | $this->createException($response, Response::BADREQUEST, 'Bad request', 'Invalid template data'); |
---|
| 221 | } catch (Exception $ex) { |
---|
| 222 | $this->createException($response, Response::INTERNALSERVERERROR, 'Internal Server Error', $ex); |
---|
| 223 | return $response; |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | return $response; |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | private function createException(&$response, $code, $title, $description) { |
---|
| 230 | $result = array( |
---|
| 231 | 'version' => '0.1', |
---|
| 232 | 'collection' => array( |
---|
| 233 | 'error' => array( |
---|
| 234 | 'code' => $code, |
---|
| 235 | 'title' => $title, |
---|
| 236 | 'description' => $description |
---|
| 237 | ) |
---|
| 238 | ) |
---|
| 239 | ); |
---|
| 240 | $response->code = $code; |
---|
| 241 | $response->body = json_encode($result); |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | private function validData($data) { |
---|
| 245 | return ((array_key_exists('name', $data) && !empty($data['name'])) && (array_key_exists('email', $data) && !empty($data['email']))) ? true : false; |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | private function isValidId($id) { |
---|
| 249 | if (is_numeric($id)) { |
---|
| 250 | return strlen($id) > 10 ? false : true; |
---|
| 251 | }else |
---|
| 252 | return false; |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | ?> |
---|