[6230] | 1 | <?php |
---|
| 2 | |
---|
| 3 | class DynamicContactsResource extends Resource { |
---|
| 4 | |
---|
| 5 | /** |
---|
| 6 | * Busca os eventos contidos nas agendas assinas 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 a colection de contatos dinâmicos de um usuário |
---|
| 14 | * @access public |
---|
| 15 | * */ |
---|
| 16 | function get($request) { |
---|
| 17 | try { |
---|
| 18 | $response = new Response($request); |
---|
| 19 | $response->addHeader('Content-type', 'aplication/json'); |
---|
| 20 | |
---|
| 21 | $this->secured(); |
---|
| 22 | $dynamicContact = Controller::find(array('concept' => 'dynamicContact'), false, array('filter' => array('=', 'owner', Config::me("uidNumber")))); |
---|
| 23 | |
---|
| 24 | $response->code = Response::OK; |
---|
| 25 | $resourceHref = str_replace($request->baseUri, '', $request->uri); |
---|
| 26 | |
---|
| 27 | if ($dynamicContact) { |
---|
| 28 | $data = array(); |
---|
| 29 | foreach ($dynamicContact as $key => $value) { |
---|
| 30 | $data[$key] = array( |
---|
| 31 | 'href' => '/dynamic-contacts/' . $value['id'], |
---|
| 32 | 'data' => array( |
---|
| 33 | array( |
---|
| 34 | 'name' => "name", |
---|
| 35 | 'value' => $value['name'], |
---|
| 36 | 'prompt' => "Nome do Contato" |
---|
| 37 | ), |
---|
| 38 | array( |
---|
| 39 | "name" => "email", |
---|
| 40 | "value" => $value['mail'], |
---|
| 41 | "prompt" => "Email do Contato" |
---|
| 42 | ) |
---|
| 43 | ), |
---|
| 44 | 'links' => array( |
---|
| 45 | array( |
---|
| 46 | 'href' => $resourceHref . "/" . $value['id'], |
---|
| 47 | 'rel' => 'delete', |
---|
| 48 | 'prompt' => 'Remover contato dinamico', |
---|
| 49 | 'name' => 'delete-contact', |
---|
| 50 | 'render' => 'link' |
---|
| 51 | ), |
---|
| 52 | array( |
---|
| 53 | 'href' => $resourceHref . '/' . $value['id'], |
---|
| 54 | 'rel' => 'update', |
---|
| 55 | 'prompt' => 'Editar contato dinamico', |
---|
| 56 | 'name' => 'edit-contact', |
---|
| 57 | 'render' => 'link' |
---|
| 58 | ) |
---|
| 59 | ) |
---|
| 60 | ); |
---|
| 61 | } |
---|
| 62 | }else |
---|
| 63 | $data = null; |
---|
| 64 | |
---|
| 65 | $result = array( |
---|
| 66 | "version" => "0.1", |
---|
| 67 | "collection" => array( |
---|
| 68 | "href" => $resourceHref, |
---|
| 69 | "type" => "dynamic-contacts", |
---|
| 70 | "data" => array( |
---|
| 71 | "name" => "dynamic-contacts", |
---|
| 72 | "prompt" => "Contatos dinâmicos" |
---|
| 73 | ), |
---|
| 74 | "pagination" => null, |
---|
| 75 | "items" => $data, |
---|
| 76 | "queries" => null, |
---|
| 77 | "template" => array( |
---|
| 78 | "data" => array( |
---|
| 79 | array( |
---|
| 80 | "name" => "name", |
---|
| 81 | "value" => "", |
---|
| 82 | "prompt" => "Nome do contato", |
---|
| 83 | "dataType" => "string", |
---|
| 84 | "minLength" => 0, |
---|
| 85 | "maxLength" => 100, |
---|
| 86 | "required" => false |
---|
| 87 | ), |
---|
| 88 | array( |
---|
| 89 | "name" => "email", |
---|
| 90 | "value" => "", |
---|
| 91 | "prompt" => "Email do contato", |
---|
| 92 | "dataType" => "string", |
---|
| 93 | "minLength" => 0, |
---|
| 94 | "maxLength" => 100, |
---|
| 95 | "required" => true |
---|
| 96 | ) |
---|
| 97 | ) |
---|
| 98 | ), |
---|
| 99 | 'error' => null |
---|
| 100 | ) |
---|
| 101 | ); |
---|
| 102 | } catch (Exception $ex) { |
---|
| 103 | $this->createException($response, Response::INTERNALSERVERERROR, 'Internal Server Error', $ex); |
---|
| 104 | return $response; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | $response->body = json_encode($result); |
---|
| 108 | return $response; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | function post($request) { |
---|
| 112 | |
---|
| 113 | try { |
---|
| 114 | $response = new Response($request); |
---|
| 115 | $response->addHeader('Content-type', 'aplication/json'); |
---|
| 116 | $data = $_POST; |
---|
| 117 | |
---|
| 118 | if ($this->validData($data)) { |
---|
| 119 | $this->secured(); |
---|
| 120 | |
---|
| 121 | $response->code = Response::OK; |
---|
| 122 | |
---|
| 123 | $exists = Controller::find(array('concept' => 'dynamicContact'), false, array('filter' => array('AND', |
---|
| 124 | array('=', 'owner', Config::me("uidNumber")), |
---|
| 125 | array('=', 'mail', $data['email']) |
---|
| 126 | ))); |
---|
| 127 | |
---|
| 128 | if ($exists) { |
---|
| 129 | //Controller::update(array('concept' => 'dynamicContact', 'id' => $exists[0]['id']), array('name' => $data['name'], 'email' => $data['email'], 'timestamp' => time())); |
---|
| 130 | |
---|
| 131 | $this->createException($response, Response::BADREQUEST, 'Bad request', Controller::service('PostgreSQL')->error); |
---|
| 132 | return $response; |
---|
| 133 | }else |
---|
| 134 | $newDynamicContact = Controller::create(array('concept' => 'dynamicContact'), array('name' => $data['name'], 'mail' => $data['email'], 'owner' => Config::me("uidNumber"), 'timestamp' => time())); |
---|
| 135 | |
---|
| 136 | if (!$newDynamicContact) |
---|
| 137 | $this->createException($response, Response::INTERNALSERVERERROR, 'Internal Server Error', Controller::service('PostgreSQL')->error); |
---|
| 138 | } else { |
---|
| 139 | $this->createException($response, Response::BADREQUEST, 'Bad request', 'Invalid template data'); |
---|
| 140 | return $response; |
---|
| 141 | } |
---|
| 142 | } catch (Exception $ex) { |
---|
| 143 | $this->createException($response, Response::INTERNALSERVERERROR, 'Internal Server Error', $ex); |
---|
| 144 | return $response; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | return $response; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | private function createException(&$response, $code, $title, $description) { |
---|
| 151 | $result = array( |
---|
| 152 | 'version' => '0.1', |
---|
| 153 | 'collection' => array( |
---|
| 154 | 'error' => array( |
---|
| 155 | 'code' => $code, |
---|
| 156 | 'title' => $title, |
---|
| 157 | 'description' => $description |
---|
| 158 | ) |
---|
| 159 | ) |
---|
| 160 | ); |
---|
| 161 | $response->code = $code; |
---|
| 162 | $response->body = json_encode($result); |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | private function validData($data) { |
---|
| 166 | return ((array_key_exists('name', $data) && !empty($data['name'])) && (array_key_exists('email', $data) && !empty($data['email']))) ? true : false; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | ?> |
---|