addHeader('Content-type', 'aplication/json'); $isValidId = $this->isValidId($id); if (!$isValidId) { $this->createException($response, Response::BADREQUEST, 'Malformed URL', 'Malformed URL'); return $response; } $response->code = Response::OK; $this->secured(); //$dynamicContact = false; $dynamicContact = Controller::find(array('concept' => 'dynamicContact'), false, array('filter' => array('AND', array('=', 'owner', Config::me("uidNumber")), array('=', 'id', $id)))); $resourceHref = str_replace($request->baseUri, '', $request->uri); if ($dynamicContact) { $data = array(); foreach ($dynamicContact as $key => $value) { $data[$key] = array( 'href' => '/dynamiccontacts/' . $value['id'], 'data' => array( array( 'name' => "name", 'value' => $value['name'], 'prompt' => "Nome do Contato" ), array( "name" => "email", "value" => $value['mail'], "prompt" => "Email do Contato" ) ), 'links' => array( array( 'href' => $resourceHref, 'rel' => 'delete', 'prompt' => 'Remover contato dinamico', 'name' => 'delete-contact', 'render' => 'link' ), array( 'href' => $resourceHref, 'rel' => 'update', 'prompt' => 'Editar contato dinamico', 'name' => 'edit-contact', 'render' => 'link' ) ) ); } } else { $this->createException($response, Response::NOTFOUND, 'Contact not found', ( $isValidId ? "The contact " . $id . " wasn't found on this server" : 'url malformed')); return $response; } $result = array( "version" => "0.1", "collection" => array( "href" => $resourceHref, "type" => "dynamic-contact", "data" => array( "name" => "dynamic-contact-" . $id, "prompt" => "Contato dinâmico" ), "pagination" => null, "items" => $data, "queries" => null, "template" => array( "data" => array( array( "name" => "name", "value" => "", "prompt" => "Nome do contato", "dataType" => "string", "minLength" => 0, "maxLength" => 100, "required" => false ), array( "name" => "email", "value" => "", "prompt" => "Email do contato", "dataType" => "string", "minLength" => 0, "maxLength" => 100, "required" => true ) ) ), 'error' => null ) ); } catch (Exception $ex) { $this->createException($response, Response::INTERNALSERVERERROR, 'Internal Server Error', $ex); return $response; } $response->body = json_encode($result); return $response; } /** * Deleta um item da coleção de contatos dinâmicos * * @license http://www.gnu.org/copyleft/gpl.html GPL * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) * @sponsor Caixa Econômica Federal * @author Adriano Coutinho da Silva * @author Adir Kuhn * @return status http * @access public * */ function delete($request, $id) { try { $response = new Response($request); $response->addHeader('Content-type', 'aplication/json'); $isValidId = $this->isValidId($id); if (!$isValidId) { $this->createException($response, Response::BADREQUEST, 'Malformed URL', 'Malformed URL'); return $response; } $this->secured(); $response->code = Response::OK; $error = null; $exists = Controller::find(array('concept' => 'dynamicContact'), false, array('filter' => array('AND', array('=', 'owner', Config::me("uidNumber")), array('=', 'id', $id) ))); if (!$exists) { $this->createException($response, Response::BADREQUEST, 'Contact not found', "The contact " . $id . " wasn't found on this server"); return $response; } $deletedDynamicContact = Controller::delete(array('concept' => 'dynamicContact'), false, array('filter' => array('=', 'id', $id))); if (!$deletedDynamicContact) $error = array( 'code' => '500', 'title' => 'Internal Server Error', 'description' => Controller::service('PostgreSQL')->error ); } catch (Exception $ex) { $this->createException($response, Response::INTERNALSERVERERROR, 'Internal Server Error', $ex); return $response; } return $response; } /** * Atualiza um item da coleção de contatos dinâmicos * * @license http://www.gnu.org/copyleft/gpl.html GPL * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) * @sponsor Caixa Econômica Federal * @author Adriano Coutinho da Silva * @author Adir Kuhn * @return status http * @access public * */ function put($request, $id) { try { $response = new Response($request); $response->addHeader('Content-type', 'aplication/json'); $isValidId = $this->isValidId($id); if (!$isValidId) { $this->createException($response, Response::BADREQUEST, 'Malformed URL', 'Malformed URL'); return $response; } $data = $request->dataDecoded; if ($this->validData($data)) { $this->secured(); $response->code = Response::OK; $exists = Controller::find(array('concept' => 'dynamicContact'), false, array('filter' => array('AND', array('=', 'owner', Config::me("uidNumber")), array('=', 'mail', $data['email']) ))); if (!empty($exists)) { $this->createException($response, Response::BADREQUEST, 'Bad request', Controller::service('PostgreSQL')->error); return $response; } $newDynamicContact = Controller::update(array('concept' => 'dynamicContact', 'id' => $id), array('name' => $data['name'], 'mail' => $data['email'], 'timestamp' => time())); if (!$newDynamicContact) $this->createException($response, Response::INTERNALSERVERERROR, 'Internal Server Error', Controller::service('PostgreSQL')->error); } else $this->createException($response, Response::BADREQUEST, 'Bad request', 'Invalid template data'); } catch (Exception $ex) { $this->createException($response, Response::INTERNALSERVERERROR, 'Internal Server Error', $ex); return $response; } return $response; } private function createException(&$response, $code, $title, $description) { $result = array( 'version' => '0.1', 'collection' => array( 'error' => array( 'code' => $code, 'title' => $title, 'description' => $description ) ) ); $response->code = $code; $response->body = json_encode($result); } private function validData($data) { return ((array_key_exists('name', $data) && !empty($data['name'])) && (array_key_exists('email', $data) && !empty($data['email']))) ? true : false; } private function isValidId($id) { if (is_numeric($id)) { return strlen($id) > 10 ? false : true; }else return false; } } ?>