addHeader('Content-type', 'aplication/json'); $this->secured(); $dynamicContact = Controller::find(array('concept' => 'dynamicContact'), false, array('filter' => array('=', 'owner', Config::me("uidNumber")))); $response->code = Response::OK; $resourceHref = str_replace($request->baseUri, '', $request->uri); if ($dynamicContact) { $data = array(); foreach ($dynamicContact as $key => $value) { $data[$key] = array( 'href' => '/dynamic-contacts/' . $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 . "/" . $value['id'], 'rel' => 'delete', 'prompt' => 'Remover contato dinamico', 'name' => 'delete-contact', 'render' => 'link' ), array( 'href' => $resourceHref . '/' . $value['id'], 'rel' => 'update', 'prompt' => 'Editar contato dinamico', 'name' => 'edit-contact', 'render' => 'link' ) ) ); } }else $data = null; $result = array( "version" => "0.1", "collection" => array( "href" => $resourceHref, "type" => "dynamic-contacts", "data" => array( "name" => "dynamic-contacts", "prompt" => "Contatos dinâmicos" ), "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; } function post($request) { try { $response = new Response($request); $response->addHeader('Content-type', 'aplication/json'); $data = $_POST; 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 ($exists) { //Controller::update(array('concept' => 'dynamicContact', 'id' => $exists[0]['id']), array('name' => $data['name'], 'email' => $data['email'], 'timestamp' => time())); $this->createException($response, Response::BADREQUEST, 'Bad request', Controller::service('PostgreSQL')->error); return $response; }else $newDynamicContact = Controller::create(array('concept' => 'dynamicContact'), array('name' => $data['name'], 'mail' => $data['email'], 'owner' => Config::me("uidNumber"), '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'); return $response; } } 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; } } ?>