src/Controller/ConselhoMunicipalController.php line 173
<?php
namespace App\Controller;
use App\Entity\ConselhoMunicipal;
use App\Entity\ConselhoMunicipalDocumento;
use App\Entity\ConselhoMunicipalContato;
use App\Repository\ConselhoMunicipalContatoRepository;
use App\Form\ConselhoMunicipalType;
use App\Repository\ConselhoMunicipalRepository;
use App\Repository\ConselhoMunicipalDocumentoRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
#[Route('/conselho/municipal')]
class ConselhoMunicipalController extends AbstractController
{
/**
* @IsGranted("ROLE_ADMIN")
*/
#[Route('/arquivo/upload', name: 'app_conselho_municipal_arquivo', methods: ['POST'])]
public function arquivo(Request $request, ConselhoMunicipalRepository $em, ConselhoMunicipalDocumentoRepository $documento): Response
{
$conselho = $em->find($request->get('conselho'));
if( $conselho){
$originalFilename = $_FILES['arquivo']['name'];
$extensao = pathinfo($originalFilename);
$fileName = uniqid().'.'.$extensao['extension'];
$entity = new ConselhoMunicipalDocumento();
$entity->setDescricao($request->get('descricao'));
$entity->setConselhoMunicipal($conselho);
$entity->setDocumento($fileName);
$uploaddir = 'arquivos/conselho/';
$uploadfile = $uploaddir . $fileName;
if (move_uploaded_file($_FILES['arquivo']['tmp_name'], $uploadfile)) {
$documento->save($entity,true);
return $this->redirectToRoute('app_conselho_municipal_edit', ['id'=>$conselho->getId(),'nav'=>'arquivos'], Response::HTTP_SEE_OTHER);
} else {
echo "Não foi possível realizar o upload do arquivo";
die();
}
}
}
/**
* @IsGranted("ROLE_ADMIN")
*/
#[Route('/contato', name: 'app_conselho_municipal_contato', methods: ['POST'])]
public function contato(Request $request, ConselhoMunicipalRepository $em, ConselhoMunicipalContatoRepository $contato): Response
{
$conselho = $em->find($request->get('conselho'));
if( $conselho){
if($request->get('contato') != "0"){
$entity = $contato->find($request->get('contato'));
}else{
$entity = new ConselhoMunicipalContato();
}
$entity->setContato($request->get('nome'));
$entity->setTelefone($request->get('telefone'));
$entity->setEmail($request->get('email'));
$entity->setCpf($request->get('cpf'));
$entity->setClasse($request->get('classe'));
$entity->setRepresentante($request->get('representante'));
$entity->setDataInicial($request->get('dataInicial'));
$entity->setDataFinal($request->get('dataFinal'));
$entity->setConselhoMunicipal($conselho);
$contato->save($entity,true);
return $this->redirectToRoute('app_conselho_municipal_edit', ['id'=>$conselho->getId(),'nav'=>'contatos'], Response::HTTP_SEE_OTHER);
}
}
/**
* @IsGranted("ROLE_ADMIN")
*/
#[Route('/', name: 'app_conselho_municipal_index', methods: ['GET'])]
public function index(ConselhoMunicipalRepository $conselhoMunicipalRepository,Request $request): Response
{
$filter['tipo'] = $request->get('tipo') ?: '';
$filter['descricao'] = $request->get('descricao') ?: '';
$filter['pagina'] = $request->get('pagina') ?: 1;
$filter['anterior'] = $this->verifica('anterior', (1 - $filter['pagina']));
$filter['primeira'] = 1;
$filter['total_registros'] = $conselhoMunicipalRepository->filterGridTotal($filter);
$filter['page'] = $request->get('page') ?: 10;
$filter['ultima'] = $this->verifica('ultima', $filter['total_registros'],$filter['page']);
$filter['proxima'] = $this->verifica('proxima', (1 + $filter['pagina']), $filter['ultima']);
$filter['dataInicial'] = '';
$filter['dataFinal'] = '';
return $this->render('conselho_municipal/index.html.twig', [
'filtro' => $filter,
'lista' => $conselhoMunicipalRepository->filterGrid($filter),
]);
}
#[Route('/conselhos', name: 'app_conselho_municipal_publico', methods: ['GET'])]
public function conselho(ConselhoMunicipalRepository $conselhoMunicipalRepository, Request $request): Response {
$filter['ano'] = $request->get('ano')?: date('Y');
//$filter['mes'] = $request->get('mes')?: date('m');
$filter['tipo'] = $request->get('tipo') ?: '';
$filter['descricao'] = $request->get('descricao') ?: '';
$filter['pagina'] = $request->get('pagina') ?: 1;
$filter['anterior'] = $this->verifica('anterior', (1 - $filter['pagina']));
$filter['primeira'] = 1;
$filter['total_registros'] = $conselhoMunicipalRepository->filterGridTotal($filter);
$filter['page'] = $request->get('page') ?: 10;
$filter['ultima'] = $this->verifica('ultima', $filter['total_registros'],$filter['page']);
$filter['proxima'] = $this->verifica('proxima', (1 + $filter['pagina']), $filter['ultima']);
$filter['dataInicial'] = '';
$filter['dataFinal'] = '';
if ($request->get('impressao')) {
$filter['page'] = 'all';
if ($request->get('pdf')) {
$html = $this->renderView('conselho_municipal/impressao_pdf.html.twig', [
'filtro' => $filter,
'lista' => $conselhoMunicipalRepository->filterGrid($filter),
]);
$dompdf = new Dompdf();
$dompdf->setPaper('A4', 'landscape');
$dompdf->set_option("isPhpEnabled", true);
$options = $dompdf->getOptions();
$options->setDefaultFont('Courier');
$dompdf->setOptions($options);
$dompdf->loadHtml($html);
$dompdf->render();
return new Response(
$dompdf->stream('resume', ["Attachment" => false]),
Response::HTTP_OK,
['Content-Type' => 'application/pdf']
);
}elseif($request->get('excel')){
$lista =$conselhoMunicipalRepository->filterGrid($filter);
$titulos = array("SECRETARIA","DESCRIÇÃO","LEGISLAÇÃO","CONTATO","REPRESENTANTE","E-MAIL");
$rows[] = implode(';', $titulos);
foreach ($lista as $value) {
foreach ($value->getConselhoMunicipalContatos() as $contato){
$data = array($value->getSecretaria(), $value->getDescricao() , $value->getLegislacao(),$contato->getContato(),$contato->getRepresentante(),$contato->getEmail());
$rows[] = implode(';', $data);
}
}
$content = implode("\n", $rows);
$response = new Response($content);
$response->headers->set('Content-Type', 'text/csv');
return $response;
}else{
return $this->render('conselho_municipal/impressao.html.twig', [
'filtro' => $filter,
'lista' => $conselhoMunicipalRepository->filterGrid($filter),
]);
}
}
return $this->render('conselho_municipal/conselhos.html.twig', [
'filtro' => $filter,
'lista' => $conselhoMunicipalRepository->filterGrid($filter),
]);
}
#[Route('/conselhos/ano', name: 'app_conselho_municipal_publico_ano', methods: ['GET'])]
public function conselho_ano(ConselhoMunicipalRepository $conselhoMunicipalRepository, Request $request): Response {
$lista = $conselhoMunicipalRepository->getAnos();
return $this->render('conselho_municipal/ano.html.twig', [
'itens' => $lista,
]);
}
/**
* @IsGranted("ROLE_ADMIN")
*/
#[Route('/new', name: 'app_conselho_municipal_new', methods: ['GET', 'POST'])]
public function new(Request $request, ConselhoMunicipalRepository $conselhoMunicipalRepository): Response
{
$nav['dados'] = 'show active';
$nav['arquivos'] = '';
$nav['conselho'] = '';
$conselhoMunicipal = new ConselhoMunicipal();
$form = $this->createForm(ConselhoMunicipalType::class, $conselhoMunicipal);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$conselhoMunicipalRepository->save($conselhoMunicipal, true);
return $this->redirectToRoute('app_conselho_municipal_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('conselho_municipal/new.html.twig', [
'conselho_municipal' => $conselhoMunicipal,
'form' => $form,
'nav' => $nav
]);
}
/**
* @IsGranted("ROLE_ADMIN")
*/
#[Route('/{id}', name: 'app_conselho_municipal_show', methods: ['GET'])]
public function show(ConselhoMunicipal $conselhoMunicipal): Response
{
return $this->render('conselho_municipal/show.html.twig', [
'conselho_municipal' => $conselhoMunicipal,
]);
}
/**
* @IsGranted("ROLE_ADMIN")
*/
#[Route('/{id}/edit', name: 'app_conselho_municipal_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, ConselhoMunicipal $conselhoMunicipal, ConselhoMunicipalRepository $conselhoMunicipalRepository): Response
{
$form = $this->createForm(ConselhoMunicipalType::class, $conselhoMunicipal);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$conselhoMunicipalRepository->save($conselhoMunicipal, true);
return $this->redirectToRoute('app_conselho_municipal_index', [], Response::HTTP_SEE_OTHER);
}
if($request->get('nav')){
if($request->get('nav') == 'arquivos'){
$nav['dados'] = '';
$nav['conselho'] = '';
$nav['arquivos'] = 'show active';
}else if($request->get('nav') == 'contatos'){
$nav['dados'] = '';
$nav['arquivos'] = '';
$nav['conselho'] = 'show active';
}
}else{
$nav['dados'] = 'show active';
$nav['arquivos'] = '';
$nav['conselho'] = '';
}
return $this->renderForm('conselho_municipal/edit.html.twig', [
'conselho_municipal' => $conselhoMunicipal,
'form' => $form,
'nav' => $nav,
]);
}
/**
* @IsGranted("ROLE_ADMIN")
*/
#[Route('/delete/{id}', name: 'app_conselho_municipal_delete', methods: ['GET'])]
public function delete(ConselhoMunicipalRepository $em ): Response {
if ($entity = $em->find($request->get('id'))) {
try {
$em->remove($entity, true);
$this->addFlash('success', "REGISTRO EXCLUIDO COM SUCESSO.");
return $this->redirectToRoute('app_conselho_municipal_index', [], Response::HTTP_SEE_OTHER);
} catch (\Exception $e) {
$this->addFlash('errors', "NÃO FOI POSSÍVEL EXCLUIR O REGISTRO. VERIFIQUE SE EXISTE ALGUM DOCUMENTO VINCULADO.");
return $this->redirectToRoute('app_conselho_municipal_index', [], Response::HTTP_SEE_OTHER);
}
}else{
$this->addFlash('errors', "NÃO FOI POSSÍVEL LOCALIZAR O REGISTRO INFORMADO.");
return $this->redirectToRoute('app_conselho_municipal_index', [], Response::HTTP_SEE_OTHER);
}
}
/**
* @IsGranted("ROLE_ADMIN")
*/
#[Route('/delete/documento/{id}', name: 'app_conselho_municipal_documento_delete', methods: ['GET'])]
public function deleteDocumento(Request $request, ConselhoMunicipalDocumento $documento, ConselhoMunicipalDocumentoRepository $documentoRepository): Response {
if ($documento) {
$conselho = $documento->getConselhoMunicipal();
$documentoRepository->remove($documento, true);
$form = $this->createForm(ConselhoMunicipalType::class, $conselho);
$form->handleRequest($request);
$nav['dados'] = '';
$nav['conselho'] = '';
$nav['arquivos'] = 'show active';
$this->addFlash('success', "DOCUMENTO EXCLUIDO COM SUCESSO.");
}
return $this->renderForm('conselho_municipal/edit.html.twig', [
'conselho_municipal' => $conselho,
'form' => $form,
'nav' => $nav
]);
//return $this->redirectToRoute('app_subvencao_social_e_index', [], Response::HTTP_SEE_OTHER);
}
/**
* @IsGranted("ROLE_ADMIN")
*/
#[Route('/delete/contato/{id}', name: 'app_conselho_municipal_contato_delete', methods: ['GET'])]
public function deleteContato(Request $request, ConselhoMunicipalContato $contato , ConselhoMunicipalContatoRepository $contatoRepository): Response {
if ($contato) {
$conselho = $contato->getConselhoMunicipal();
$contatoRepository->remove($contato, true);
$form = $this->createForm(ConselhoMunicipalType::class, $conselho);
$form->handleRequest($request);
$nav['dados'] = '';
$nav['arquivos'] = '';
$nav['conselho'] = 'show active';
$this->addFlash('success', "CONTATO EXCLUIDO COM SUCESSO.");
}
return $this->renderForm('conselho_municipal/edit.html.twig', [
'conselho_municipal' => $conselho,
'form' => $form,
'nav' => $nav
]);
//return $this->redirectToRoute('app_subvencao_social_e_index', [], Response::HTTP_SEE_OTHER);
}
private function verifica($str, $pagina, $page = null) {
if ($str == "anterior") {
if ($pagina < 1) {
return 1;
}
}
if ($str == "proxima") {
if ($pagina > $page) {
return $page;
}
return $pagina;
}
if ($str == "ultima") {
if($page == "all"){
$page = $pagina;
}
$valor = intdiv($pagina, $page);
if (($pagina % $page) > 0) {
return ++$valor;
} else {
return $valor;
}
}
}
}