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