[6473] | 1 | <?php |
---|
| 2 | |
---|
[6523] | 3 | if (!defined('ROOTPATH')) |
---|
| 4 | define('ROOTPATH', dirname(__FILE__) . '/..'); |
---|
[6473] | 5 | |
---|
[6523] | 6 | require_once(ROOTPATH . '/rest/hypermedia/hypermedia.php'); |
---|
[6473] | 7 | |
---|
| 8 | use prototype\api\Config as Config; |
---|
| 9 | |
---|
| 10 | class DynamicContactsResource extends Resource { |
---|
| 11 | |
---|
| 12 | /** |
---|
| 13 | * Retorna uma lista de contatos recentes |
---|
| 14 | * |
---|
| 15 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 16 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 17 | * @sponsor Caixa Econômica Federal |
---|
| 18 | * @author José Vicente Tezza Jr. |
---|
| 19 | * @return Retorna uma lista de contatos recentes |
---|
| 20 | * @access public |
---|
| 21 | * */ |
---|
| 22 | function get($request) { |
---|
| 23 | |
---|
[6523] | 24 | $this->secured(); |
---|
| 25 | |
---|
[6473] | 26 | $response = new Response($request); |
---|
| 27 | $response->addHeader('Content-type', 'aplication/json'); |
---|
| 28 | $response->code = Response::OK; |
---|
| 29 | |
---|
| 30 | $h = new Hypermedia(); |
---|
| 31 | $c = new Collection($request->resources, 'DynamicContactsResource'); |
---|
| 32 | |
---|
| 33 | try { |
---|
| 34 | |
---|
[6523] | 35 | $dynamicContacts = Controller::find( |
---|
| 36 | array('concept' => 'dynamicContact'), false, array('filter' => array('=', 'owner', Config::me("uidNumber")), |
---|
| 37 | 'order' => array('number_of_messages', 'name', 'mail'), |
---|
| 38 | 'orderDesc' => array(true, false, false)) |
---|
[6473] | 39 | ); |
---|
| 40 | |
---|
| 41 | //Se nao foi encontrado contatos na consulta |
---|
[6523] | 42 | if (!$dynamicContacts) { |
---|
| 43 | $this->createException($request, $response, Response::NOTFOUND, 'Bad request', 'Dynamic Contact not found.'); |
---|
[6473] | 44 | return $response; |
---|
| 45 | } |
---|
| 46 | |
---|
[7304] | 47 | $dynamicContacts = array_slice($dynamicContacts, 0,50); |
---|
[6523] | 48 | foreach ($dynamicContacts as $value) { |
---|
[6473] | 49 | $d = new Data(); |
---|
[6523] | 50 | $i = new Item($request->resources, 'DynamicContactsResource', $value['id']); |
---|
[6473] | 51 | |
---|
| 52 | $d->setName('name'); |
---|
[6523] | 53 | $d->setValue($value['name']); |
---|
| 54 | $d->setPrompt('Nome do Contato Recente'); |
---|
| 55 | $d->setDataType('string'); |
---|
| 56 | $d->setMaxLength('100'); |
---|
| 57 | $d->setMinLength(null); |
---|
| 58 | $d->setRequired(true); |
---|
[6473] | 59 | |
---|
[6523] | 60 | $i->addData($d); |
---|
[6473] | 61 | |
---|
| 62 | $d = new Data(); |
---|
[6523] | 63 | $d->setName('mail'); |
---|
| 64 | $d->setValue($value['mail']); |
---|
| 65 | $d->setPrompt('Email do Contato Recente'); |
---|
| 66 | $d->setDataType('string'); |
---|
| 67 | $d->setMaxLength('100'); |
---|
| 68 | $d->setMinLength(null); |
---|
| 69 | $d->setRequired(true); |
---|
[6473] | 70 | |
---|
[6523] | 71 | $i->addData($d); |
---|
[6473] | 72 | |
---|
| 73 | $d = new Data(); |
---|
[6523] | 74 | $d->setName('number_of_messages'); |
---|
| 75 | $d->setValue($value['number_of_messages']); |
---|
| 76 | $d->setPrompt('Quantidade de mensagens enviadas'); |
---|
| 77 | $d->setDataType('integer'); |
---|
| 78 | $d->setMaxLength('100'); |
---|
| 79 | $d->setMinLength(null); |
---|
| 80 | $d->setRequired(false); |
---|
[6473] | 81 | |
---|
[6523] | 82 | $i->addData($d); |
---|
[6473] | 83 | |
---|
[6523] | 84 | $d = new Data(); |
---|
| 85 | $d->setName('id'); |
---|
| 86 | $d->setValue($value['id']); |
---|
| 87 | $d->setPrompt('Id do contato dinamico'); |
---|
| 88 | $d->setDataType('integer'); |
---|
| 89 | $d->setMaxLength('100'); |
---|
| 90 | $d->setMinLength(null); |
---|
| 91 | $d->setRequired(false); |
---|
[6512] | 92 | |
---|
[6523] | 93 | $i->addData($d); |
---|
[6473] | 94 | |
---|
[6523] | 95 | $l = new Link(); |
---|
[6473] | 96 | |
---|
[6523] | 97 | $l->setHref(''); |
---|
| 98 | $l->setRel('delete'); |
---|
| 99 | $l->setAlt('Remover'); |
---|
| 100 | $l->setPrompt('Remover'); |
---|
| 101 | $l->setRender('link'); |
---|
[6473] | 102 | |
---|
[6523] | 103 | $i->addLink($l); |
---|
[6473] | 104 | |
---|
[6523] | 105 | $l = new Link(); |
---|
| 106 | $l->setHref(''); |
---|
| 107 | $l->setRel('put'); |
---|
| 108 | $l->setAlt('Atualizar'); |
---|
| 109 | $l->setPrompt('Atualizar'); |
---|
| 110 | $l->setRender('link'); |
---|
[6473] | 111 | |
---|
[6523] | 112 | $i->addLink($l); |
---|
[6473] | 113 | |
---|
[6523] | 114 | $l = new Link(); |
---|
| 115 | $l->setHref('/dynamiccontact/' . $value['id']); |
---|
| 116 | $l->setRel('get'); |
---|
| 117 | $l->setAlt('Buscar'); |
---|
| 118 | $l->setPrompt('Buscar'); |
---|
| 119 | $l->setRender('link'); |
---|
| 120 | |
---|
| 121 | $i->addLink($l); |
---|
| 122 | $c->addItem($i); |
---|
[6473] | 123 | } |
---|
| 124 | |
---|
[6523] | 125 | $t = new Template(); |
---|
| 126 | $d = new Data(); |
---|
[6473] | 127 | |
---|
[6523] | 128 | $d->setName('name'); |
---|
| 129 | $d->setValue(null); |
---|
| 130 | $d->setPrompt('Nome do Contato Recente'); |
---|
| 131 | $d->setDataType('string'); |
---|
| 132 | $d->setMaxLength(100); |
---|
| 133 | $d->setMinLength(null); |
---|
| 134 | $d->setRequired(false); |
---|
[6473] | 135 | |
---|
[6523] | 136 | $t->addData($d); |
---|
[6473] | 137 | |
---|
[6523] | 138 | $d = new Data(); |
---|
| 139 | $d->setName('mail'); |
---|
| 140 | $d->setValue(null); |
---|
| 141 | $d->setPrompt('Email do Contato Recente'); |
---|
| 142 | $d->setDataType('string'); |
---|
| 143 | $d->setMaxLength(100); |
---|
| 144 | $d->setMinLength(null); |
---|
| 145 | $d->setRequired(true); |
---|
[6473] | 146 | |
---|
[6523] | 147 | $t->addData($d); |
---|
[6473] | 148 | |
---|
[6523] | 149 | $d = new Data(); |
---|
| 150 | $d->setName('number_of_messages'); |
---|
| 151 | $d->setValue(null); |
---|
| 152 | $d->setPrompt('Quantidade de mensagens enviadas'); |
---|
| 153 | $d->setDataType('integer'); |
---|
| 154 | $d->setMaxLength(100); |
---|
| 155 | $d->setMinLength(null); |
---|
| 156 | $d->setRequired(false); |
---|
[6473] | 157 | |
---|
[6523] | 158 | $t->addData($d); |
---|
[6473] | 159 | |
---|
[6523] | 160 | $c->setTemplate($t); |
---|
[6473] | 161 | |
---|
[6523] | 162 | $h->setCollection($c); |
---|
| 163 | } catch (Exception $ex) { |
---|
| 164 | $this->createException($request, $response, Response::INTERNALSERVERERROR, 'Internal Server Error', $ex); |
---|
| 165 | return $response; |
---|
[6473] | 166 | } |
---|
| 167 | |
---|
| 168 | $response->body = $h->getHypermedia($request->accept[10][0]); |
---|
| 169 | return $response; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | /** |
---|
| 173 | * Salva um contato recente |
---|
| 174 | * |
---|
| 175 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 176 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
| 177 | * @sponsor Caixa Econômica Federal |
---|
| 178 | * @author José Vicente Tezza Jr. |
---|
| 179 | * @return retorna a uri do contato recente |
---|
| 180 | * @access public |
---|
| 181 | * */ |
---|
[6523] | 182 | function post($request) { |
---|
[6473] | 183 | |
---|
[6523] | 184 | $this->secured(); |
---|
[6605] | 185 | |
---|
| 186 | //verificar se a preferencia de contatos dinamicos nao esta ativada |
---|
| 187 | if(!$this->isEnabledDynamicContacts(Config::me("uidNumber")) ){ |
---|
| 188 | $response = new Response($request); |
---|
| 189 | $this->createException($request, $response, Response::UNAUTHORIZED, 'Resource unauthorized', 'disabled dynamic contacts preference'); |
---|
| 190 | return $response; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | |
---|
[6523] | 194 | if (count($_POST) == 0) { |
---|
| 195 | $response = new Response($request); |
---|
| 196 | $response->code = Response::INTERNALSERVERERROR; |
---|
| 197 | return $response; |
---|
[6473] | 198 | } |
---|
| 199 | |
---|
[6578] | 200 | //recuperar os atributos definidos no conceito 'user' |
---|
[6473] | 201 | $map = Config::get('dynamicContact', 'PostgreSQL.mapping'); |
---|
| 202 | |
---|
| 203 | $params = array(); |
---|
[6523] | 204 | foreach ($_POST as $key => $value) { |
---|
[6473] | 205 | |
---|
[6523] | 206 | if (!isset($map[$key]) || $key == 'id' || $key == 'timestamp' || $key == 'number_of_messages') { |
---|
| 207 | continue; |
---|
| 208 | } |
---|
| 209 | $params[$key] = $value; |
---|
[6473] | 210 | } |
---|
| 211 | |
---|
[6523] | 212 | if (count($params) == 0) { |
---|
| 213 | $response = new Response($request); |
---|
| 214 | $response->code = Response::INTERNALSERVERERROR; |
---|
| 215 | return $response; |
---|
[6473] | 216 | } |
---|
| 217 | |
---|
[6523] | 218 | $response = new Response($request); |
---|
| 219 | $response->addHeader('Content-type', 'aplication/json'); |
---|
| 220 | $response->code = Response::CREATED; |
---|
[6473] | 221 | |
---|
[6523] | 222 | |
---|
[6473] | 223 | //completar os atributos |
---|
[6523] | 224 | $params['owner'] = Config::me("uidNumber"); |
---|
| 225 | $params['number_of_messages'] = '1'; |
---|
| 226 | $params['timestamp'] = time(); |
---|
[6473] | 227 | |
---|
| 228 | |
---|
[6523] | 229 | try { |
---|
[6473] | 230 | |
---|
[6578] | 231 | //verificar o limite maximo de contatos dinamicos nas preferencias do administrador |
---|
| 232 | $sql = "SELECT config_value ". |
---|
| 233 | "FROM phpgw_config ". |
---|
| 234 | "WHERE config_app = 'expressoMail1_2' ". |
---|
| 235 | "AND config_name = 'expressoMail_Number_of_dynamic_contacts'"; |
---|
| 236 | |
---|
| 237 | $numberOfMessages = Controller::service('PostgreSQL')->execResultSql($sql, true); |
---|
| 238 | $numberOfMessages = (count($numberOfMessages) > 0) ? (int)$numberOfMessages['config_value'] : 0; |
---|
| 239 | |
---|
| 240 | //realizar busca de contatos dinamicos ordenados pela data de utilizacao |
---|
| 241 | $dynamicContacts = Controller::find(array('concept' => 'dynamicContact'), false, array('filter' => array('=', 'owner', Config::me("uidNumber")), 'order' => array('timestamp') ) ); |
---|
| 242 | $numberOfDynamicContacts = ($dynamicContacts !== false) ? count($dynamicContacts) : 0; |
---|
| 243 | |
---|
| 244 | |
---|
| 245 | //se a quantidade de contatos dinamicos de usuario exceder o limite maximo definido nas preferencias do administrador, |
---|
| 246 | //remover o contato dinamico mais antigo |
---|
[6605] | 247 | if($numberOfMessages > 0 && $numberOfDynamicContacts >= $numberOfMessages){ |
---|
[6578] | 248 | $id = $dynamicContacts[0]['id']; |
---|
| 249 | $delete = Controller::delete(array('concept' => 'dynamicContact', 'id' => $id)); |
---|
| 250 | |
---|
| 251 | if (!$delete) { |
---|
| 252 | $this->createException($request, $response, Response::INTERNALSERVERERROR, 'Internal Server Error', Controller::service('PostgreSQL')->error); |
---|
| 253 | return $response; |
---|
| 254 | } |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | //inserir o novo contato dinamico |
---|
| 258 | $create = Controller::create( array('concept' => 'dynamicContact'), $params ); |
---|
| 259 | |
---|
| 260 | if (!$create) { |
---|
| 261 | throw new Exception(Controller::service('PostgreSQL')->error); |
---|
| 262 | } |
---|
| 263 | |
---|
[6523] | 264 | } catch (Exception $ex) { |
---|
[6578] | 265 | |
---|
[6523] | 266 | $response->code = Response::INTERNALSERVERERROR; |
---|
| 267 | return $response; |
---|
| 268 | } |
---|
| 269 | $response->body = json_encode(null); |
---|
[6473] | 270 | |
---|
[6523] | 271 | return $response; |
---|
| 272 | } |
---|
[6473] | 273 | |
---|
[6523] | 274 | private function createException($request, &$response, $code, $title, $description) { |
---|
| 275 | $response->code = $code; |
---|
[6473] | 276 | |
---|
[6523] | 277 | $h = new Hypermedia(); |
---|
| 278 | $c = new Collection($request->resources, 'DynamicContactResource'); |
---|
| 279 | $e = new Error(); |
---|
[6473] | 280 | |
---|
[6523] | 281 | $e->setCode($code); |
---|
| 282 | $e->setTitle($title); |
---|
| 283 | $e->setDescription($description); |
---|
[6473] | 284 | |
---|
[6523] | 285 | $c->setError($e); |
---|
| 286 | $h->setCollection($c); |
---|
| 287 | |
---|
| 288 | $response->body = $h->getHypermedia($request->accept[10][0]); |
---|
| 289 | } |
---|
| 290 | |
---|
[6605] | 291 | private function isEnabledDynamicContacts($user){ |
---|
| 292 | |
---|
[6783] | 293 | //recuperando as preferencias (suas preferencias, preferencia padrão, preferencia obrigatoria) |
---|
| 294 | //dos contatos dinamicos |
---|
| 295 | $sql = 'SELECT preference_owner, preference_value '. |
---|
| 296 | 'FROM phpgw_preferences '. |
---|
| 297 | 'WHERE preference_app = \'expressoMail\' AND '. |
---|
| 298 | 'preference_owner in (-1,-2, ' . $user . ')'; |
---|
[6605] | 299 | |
---|
[6783] | 300 | $preferences = Controller::service('PostgreSQL')->execResultSql($sql); |
---|
[6605] | 301 | |
---|
[6783] | 302 | $array = array(); |
---|
| 303 | if(count($preferences) > 0){ |
---|
| 304 | foreach($preferences as $preference){ |
---|
| 305 | //recupera a preferencia |
---|
| 306 | $preference_value = unserialize( $preference['preference_value'] ); |
---|
| 307 | |
---|
| 308 | //gera um array com o owner e o valor da preferencia: |
---|
| 309 | //true: SIM (1) |
---|
| 310 | //false: NAO (0) |
---|
| 311 | //null: escolha pelo usuario/ usar padrao / sem padrao |
---|
| 312 | $value = null; |
---|
| 313 | if(isset($preference_value['use_dynamic_contacts'])){ |
---|
| 314 | $value = (isset($preference_value['use_dynamic_contacts'])) ? $preference_value['use_dynamic_contacts'] == '1' : false; |
---|
| 315 | } |
---|
| 316 | $array[ $preference['preference_owner'] ] = $value; |
---|
| 317 | } |
---|
| 318 | } |
---|
[6605] | 319 | |
---|
[6783] | 320 | //preferencia obrigatoria (SIM) |
---|
| 321 | if(array_key_exists(-1,$array) && $array[-1]){ |
---|
| 322 | return true; |
---|
[6605] | 323 | } |
---|
[6783] | 324 | //preferencia do user (SIM) |
---|
| 325 | else if(array_key_exists($user,$array) && $array[$user] ){ |
---|
| 326 | return true; |
---|
| 327 | } |
---|
| 328 | //preferencia padrao (SIM) escolhida pelo user |
---|
| 329 | else if(array_key_exists($user, $array) && $array[$user] === null && |
---|
| 330 | array_key_exists(-2, $array) && $array[-2]){ |
---|
| 331 | return true; |
---|
| 332 | } |
---|
| 333 | return false; |
---|
[6605] | 334 | } |
---|
| 335 | |
---|
[6473] | 336 | } |
---|
[6523] | 337 | |
---|
[6473] | 338 | ?> |
---|