public function validate($data) { $vdt = new Validate\Validator(); $vdt->addRule('page', new Validate\Rule\NumNatural())->addRule('page', new Validate\Rule\NumMin(1))->addRule('take', new Validate\Rule\NumNatural())->addRule('take', new Validate\Rule\NumMin(1))->addRule('take', new Validate\Rule\NumMax(100))->addFilter('endless', FilterFactory::booleanFilter())->addOptional('page')->addOptional('take')->addOptional('endless'); if (!$vdt->validate($data)) { throw new BearableException('Parámetros de paginación incorrectos.'); } return $vdt; }
public function votar($idPro) { $vdt = new Validate\Validator(); $vdt->addRule('postura', new Validate\Rule\InArray(array(-1, 0, 1)))->addRule('idPro', new Validate\Rule\NumNatural())->addFilter('publico', FilterFactory::booleanFilter()); $req = $this->request; $data = array_merge(array('idPro' => $idPro), $req->post()); if (!$vdt->validate($data)) { throw new TurnbackException($vdt->getErrors()); } $usuario = $this->session->getUser(); $propuesta = Propuesta::with('contenido')->findOrFail($idPro); $voto = VotoPropuesta::firstOrNew(array('propuesta_id' => $propuesta->id, 'usuario_id' => $usuario->id)); $cfgPtsAutr = [-1 => 0, 0 => 0, 1 => 2]; $cfgPtsPost = [-1 => 1, 0 => 2, 1 => 3]; $cfgCount = [-1 => 'votos_contra', 0 => 'votos_neutro', 1 => 'votos_favor']; $postura = $vdt->getData('postura'); $sumaAutr = $cfgPtsAutr[$postura]; $sumaPost = $cfgPtsPost[$postura]; if (!$voto->exists) { $usuario->increment('puntos', 3); UserlogCtrl::createLog('votPropues', $usuario->id, $propuesta); } else { if ($voto->postura != $postura) { $hoy = Carbon\Carbon::now(); if ($hoy->lt($voto->updated_at->addDays(3))) { throw new TurnbackException('No puede cambiar su voto tan pronto.'); } $usuario->decrement('puntos', 3); $propuesta->decrement($cfgCount[$voto->postura]); $sumaAutr -= $cfgPtsAutr[$voto->postura]; $sumaPost -= $cfgPtsPost[$voto->postura]; } else { if ($voto->publico != $vdt->getData('publico')) { $voto->timestamps = false; $voto->publico = $vdt->getData('publico'); $voto->save(); $this->flash('success', 'La privacidad de su voto fue cambiada exitosamente.'); $this->redirectTo('shwPropues', array('idPro' => $propuesta->id)); } else { throw new TurnbackException('No puede votar dos veces la misma postura.'); } } } $voto->postura = $postura; $voto->publico = $vdt->getData('publico'); $voto->save(); $propuesta->increment($cfgCount[$postura]); if ($sumaPost != 0) { $propuesta->contenido->increment('puntos', $sumaPost); } if ($sumaAutr != 0) { $propuesta->contenido->autor()->increment('puntos', $sumaPost); } $this->flash('success', 'Su voto fue registrado exitosamente.'); $this->redirectTo('shwPropues', array('idPro' => $propuesta->id)); }
private function validarEvento($data) { $vdt = new Validate\Validator(); $vdt->addRule('titulo', new Validate\Rule\MinLength(8))->addRule('titulo', new Validate\Rule\MaxLength(128))->addRule('categoria', new Validate\Rule\NumNatural())->addRule('categoria', new Validate\Rule\Exists('categorias'))->addRule('lugar', new Validate\Rule\MinLength(4))->addRule('lugar', new Validate\Rule\MaxLength(128))->addRule('fecha', new Validate\Rule\Date('Y-m-d H:i:s'))->addRule('tags', new Validate\Rule\Required())->addRule('cuerpo', new Validate\Rule\MinLength(8))->addRule('cuerpo', new Validate\Rule\MaxLength(8192))->addFilter('cuerpo', FilterFactory::escapeHTML())->addFilter('asociar', FilterFactory::booleanFilter())->addFilter('tags', FilterFactory::explode(',')); if (!$vdt->validate($data)) { throw new TurnbackException($vdt->getErrors()); } return $vdt; }