Skip to content

Instantly share code, notes, and snippets.

@jion
Last active November 3, 2015 21:06
Show Gist options
  • Save jion/c0fcfb8aaee14e82d326 to your computer and use it in GitHub Desktop.
Save jion/c0fcfb8aaee14e82d326 to your computer and use it in GitHub Desktop.
Controller basico + Servicio Symfony (php)
<?php
namespace Bundle\PersonaBundle\Controller;
class PersonaController extends Controller
{
public function nuevaAction (Request $request)
{
// Validar permisos de usuario
if ($this->get('security.context')->isGranted(Permiso::AUTH_CREAR_PERSONA) === false) {
return $this->sendFlashNotificationAndRedirect(
$request, Mensajes::usuarioSinPermiso('Crear Persona'),
self::FLASH_ERROR
);
}
$persona = new Persona();
$form = $this->createForm(new PersonaType(), $persona);
$form->handleRequest($request);
if ($form->isValid()) {
$personaManager = $this->get('bundle_persona.manager');
try {
$personaManager->persistir($persona);
// Notifico exito y redirijo
$this->sendFlashNotification('La persona fue dada de alta en el sistema correctamente.');
return $this->redirect(
$this->generateUrl('bundle_persona_ver', array(
'personaId' => $persona->getId()
))
);
} catch (PersonasExistentesException $ex) {
$personasSimilares = $ex->getMarkedEntities();
$existentes = array();
foreach ($personasSimilares as $entidadRepetida) {
$formPersonaHelper->marcarIgualdadesEnFormulario($entidadRepetida, $form);
$existentes[] = $entidadRepetida->getEntity();
}
}
}
$params = array();
$params['form'] = $form->createView();
if (isset($existentes)) {
$params['existentes'] = $existentes;
}
return $this->render('BundlePersonaBundle:Default:nuevaPersona.html.twig', $params);
}
}
<?php
namespace Bundle\PersonaBundle\Services;
class PersonaManager extends ContainerAware {
public function persistir($persona, $flush = true)
{
$similares = $this->markComparables($persona);
if (count($similares) > 0) {
throw new PersonasExistentesException($similares);
}
// Georeferencio
$persona
->setLatitud(null)
->setLongitud(null)
->setX(null)
->setY(null);
try {
$idesfManager = $this->get('bundle_persona.idesf.manager');
$idesfManager->georeferenciarPersona($persona);
} catch (\Exception $ex) {
$this->get('session')->getFlashBag()
->add(BaseController::FLASH_NOTICE, 'Advertencia: No se ha podido obtener la ubicación georeferenciada');
}
$em = $this->getDoctrine()->getManager();
// Borro imagenes vacias
$imagenDocFrente = $persona->getImagenDocumentoFrente();
if ($imagenDocFrente && $imagenDocFrente->isVacia()) {
$persona->setImagenDocumentoFrente(null);
$em->remove($imagenDocFrente);
}
$imagenDocDorso = $persona->getImagenDocumentoDorso();
if ($imagenDocDorso && $imagenDocDorso->isVacia()) {
$persona->setImagenDocumentoDorso(null);
$em->remove($imagenDocDorso);
}
// Genero el historial de la persona
$personaHistorico = $this->generarHistorico($persona);
// Si alguna de las imagenes está marcada para borrar, las desreferencio de la persona.
$imagenDocFrente = $persona->getImagenDocumentoFrente();
if ($imagenDocFrente instanceof ImagenDocumentoFrente && $imagenDocFrente->marcadaParaBorrar()) {
$persona->setImagenDocumentoFrente(null);
}
$imagenDocDorso = $persona->getImagenDocumentoDorso();
if ($imagenDocDorso instanceof ImagenDocumentoDorso && $imagenDocDorso->marcadaParaBorrar()) {
$persona->setImagenDocumentoDorso(null);
}
// Si la persona no existe, persisto
$em->persist($personaHistorico);
$em->persist($persona);
if ($flush) {
$em->flush();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment