Guest User

Untitled

a guest
Aug 8th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.67 KB | None | 0 0
  1. package Interface::Controller::Privado;
  2. use Moose;
  3. use namespace::autoclean;
  4.  
  5. BEGIN { extends 'Catalyst::Controller'; }
  6.  
  7. =head1 NAME
  8.  
  9. interface::Controller::Privado - Catalyst Controller
  10.  
  11. =head1 DESCRIPTION
  12.  
  13. Catalyst Controller.
  14.  
  15. =head1 METHODS
  16.  
  17. =cut
  18.  
  19. =head2 base
  20.  
  21. =cut
  22.  
  23. sub base : Chained('/') : PathPart('privado') : CaptureArgs(0) {
  24. my ( $self, $c ) = @_;
  25. $c->stash(
  26. buscas => $c->model('EclapsDB::Busca'),
  27. procurar => $c->model('EclapsDB::Procurar'),
  28. spiders => $c->model('EclapsDB::Spider'),
  29. empresas => $c->model('EclapsDB::Empresa'),
  30. usertoempresas => $c->model('EclapsDB::UserToEmpresa'),
  31. procurarbuscafiltro => $c->model('EclapsDB::ProcurarBuscaFiltro'),
  32. usuarios => $c->model('EclapsDB::User'),
  33. roles => $c->model('EclapsDB::Role'),
  34. );
  35.  
  36. }
  37.  
  38. =head2 logar
  39.  
  40. Se loga autenticando o usuario.
  41.  
  42. =cut
  43.  
  44. sub logar : Chained('base') : PathPart('logar') : Args(0) {
  45. my ( $self, $c ) = @_;
  46. if (
  47. $c->authenticate(
  48. {
  49. username => $c->req->params->{'username'},
  50. password => $c->req->params->{'password'}
  51. }
  52. )
  53. )
  54. {
  55. $c->response->body('Estou logado');
  56. $c->res->redirect( $c->uri_for('inicio') );
  57. }
  58. else {
  59.  
  60. $c->stash( template => 'privado/logar.tt' );
  61.  
  62. }
  63. }
  64.  
  65. =head2 deslogar
  66.  
  67. Se desloga.
  68.  
  69. =cut
  70.  
  71. sub deslogar : Chained('base') : PathPart('deslogar') : Args(0) {
  72. my ( $self, $c ) = @_;
  73. $c->logout();
  74. $c->res->redirect('/');
  75. }
  76.  
  77. =head2 inicio
  78.  
  79. Depois de se autenticar, mostra as empresas que o usuario tem acesso.
  80.  
  81. =cut
  82.  
  83. sub inicio : Chained('base') : PathPart('inicio') : Args(0) {
  84. my ( $self, $c ) = @_;
  85.  
  86. unless ( $c->user_exists() ) {
  87. $c->res->redirect('/');
  88. $c->detach();
  89. return;
  90. }
  91.  
  92. my $itens_user =
  93. $c->stash->{'usertoempresas'}->search( { user_id => $c->user->id } );
  94. $c->stash( itens_user => $itens_user );
  95. }
  96.  
  97. sub configurar : Chained('base') : PathPart('empresa') : CaptureArgs(1) {
  98. my ( $self, $c, $empresa_id ) = @_;
  99.  
  100. unless ( $c->user_exists() ) {
  101. $c->res->redirect('/');
  102. $c->detach();
  103. return;
  104. }
  105.  
  106. $c->stash( empresa_id => $empresa_id );
  107.  
  108. my $empresa_nome =
  109. $c->stash->{'empresas'}->find( { id => $empresa_id } )->empresa;
  110. $c->stash( empresa_nome => $empresa_nome );
  111.  
  112. use aliased 'Interface::Form::Manipular';
  113.  
  114. my $manipular = Manipular->new(
  115. db => $c->stash->{'procurar'},
  116. empresa_id => $c->stash->{'empresa_id'},
  117. );
  118.  
  119. my $busca_empresa =
  120. $c->stash->{'buscas'}->search( { 'empresas_id' => $empresa_id } );
  121.  
  122. $c->stash( manipular => $manipular, busca_empresa => $busca_empresa );
  123.  
  124. }
  125.  
  126. =head2 mostrar
  127.  
  128. Mostra a página de configuração dos spiders.
  129.  
  130. =cut
  131.  
  132. sub mostrar : Chained('configurar') : PathPart('mostrar') : Args(0) {
  133. my ( $self, $c ) = @_;
  134.  
  135. unless ( $c->user_exists() ) {
  136. $c->res->redirect('/');
  137. $c->detach();
  138. return;
  139. }
  140.  
  141. $c->stash( template => 'privado/mostrar.tt' );
  142. }
  143.  
  144. =head2 salvar_busca
  145.  
  146. Adiciona a busca no banco de dados.
  147.  
  148. =cut
  149.  
  150. sub salvar_busca : Chained('configurar') : PathPart('salvar_busca') : Args(0) {
  151. my ( $self, $c ) = @_;
  152.  
  153. unless ( $c->user_exists() ) {
  154. $c->res->redirect('/');
  155. $c->detach();
  156. return;
  157. }
  158.  
  159. my $busca = $c->req->body_params->{'inserir_busca'};
  160. if ($busca) {
  161. my $manipular = $c->stash->{'manipular'};
  162. $manipular->db( $c->stash->{'buscas'} );
  163. $manipular->salvar_busca($busca);
  164. }
  165. $c->res->redirect(
  166. $c->uri_for( 'empresa', $c->stash->{'empresa_id'}, 'mostrar' ) );
  167. }
  168.  
  169. =head2 salvar_config
  170.  
  171. Salva a configuração dos spiders.
  172.  
  173. =cut
  174.  
  175. sub salvar_config : Chained('configurar') : PathPart('salvar_config') : Args(0)
  176. {
  177. my ( $self, $c ) = @_;
  178.  
  179. unless ( $c->user_exists() ) {
  180. $c->res->redirect('/');
  181. $c->detach();
  182. return;
  183. }
  184.  
  185. my $manipular = $c->stash->{'manipular'};
  186. $manipular->db( $c->stash->{'procurar'} );
  187. $manipular->salvar_config( $c->req->body_params );
  188. $c->res->redirect(
  189. $c->uri_for( 'empresa', $c->stash->{'empresa_id'}, 'mostrar' ) );
  190. }
  191.  
  192. =head2 filtro_palavras
  193.  
  194. Mostra as palavras que nao é para pegar dentro das buscas.
  195.  
  196. =cut
  197.  
  198. sub filtro_palavras : Chained('configurar') : PathPart('mostrar') :
  199. CaptureArgs(1) {
  200. my ( $self, $c, $busca_id ) = @_;
  201.  
  202. unless ( $c->user_exists() ) {
  203. $c->res->redirect('/');
  204. $c->detach();
  205. return;
  206. }
  207.  
  208. $c->stash( my_busca_id => $busca_id );
  209.  
  210. my $manipular = $c->stash->{'manipular'};
  211. $manipular->db( $c->stash->{'procurarbuscafiltro'} );
  212.  
  213. $c->stash->{'filtro_manipular'} = $manipular;
  214.  
  215. }
  216.  
  217. =head2 filtro
  218.  
  219. Mostra as palavras filtradas.
  220.  
  221. =cut
  222.  
  223. sub filtro : Chained('filtro_palavras') : PathPart('') : Args(0) {
  224. my ( $self, $c ) = @_;
  225.  
  226. my $palavras_filtro =
  227. $c->stash->{'filtro_manipular'}
  228. ->palavras_filtradas( $c->stash->{'my_busca_id'} );
  229.  
  230. $c->stash->{'palavras_filtro'} = $palavras_filtro;
  231.  
  232. }
  233.  
  234. =head2 salvar_filtro
  235.  
  236. Adiciona a palavra ao filtro.
  237.  
  238. =cut
  239.  
  240. sub salvar_filtro : Chained('filtro_palavras') : PathPart('salvar') : Args(0) {
  241. my ( $self, $c ) = @_;
  242.  
  243. # - Inclui a palavra na tabela de filtros.
  244.  
  245. my $palavra = $c->req->body_params->{'palavra'};
  246. my $busca_id = $c->stash->{'my_busca_id'};
  247.  
  248. if ($palavra) {
  249. $c->stash->{'filtro_manipular'}
  250. ->palavras_filtradas_salvar( $busca_id, $palavra );
  251. }
  252.  
  253. $c->res->redirect(
  254. $c->uri_for(
  255. 'empresa', $c->stash->{'empresa_id'},
  256. 'mostrar', $busca_id
  257. )
  258. );
  259.  
  260. }
  261.  
  262. =head2 deletar_filtro
  263.  
  264. Apaga o filtro desejado.
  265.  
  266. =cut
  267.  
  268. sub deletar_filtro : Chained('filtro_palavras') : PathPart('deletar') : Args(0)
  269. {
  270. my ( $self, $c ) = @_;
  271.  
  272. my $palavra = $c->req->body_params->{'select'};
  273. my $busca_id = $c->stash->{'my_busca_id'};
  274.  
  275. if ($palavra) {
  276.  
  277. $c->stash->{'filtro_manipular'}
  278. ->palavras_filtradas_deletar( $busca_id, $palavra );
  279.  
  280. }
  281.  
  282. $c->res->redirect(
  283. $c->uri_for(
  284. 'empresa', $c->stash->{'empresa_id'},
  285. 'mostrar', $busca_id
  286. )
  287. );
  288.  
  289. }
  290.  
  291. =head1 AUTHOR Daniel de Oliveira Mantovani,,,
  292.  
  293. =head1 LICENSE
  294.  
  295. This library is free software. You can redistribute it and/or modify
  296. it under the same terms as Perl itself.
  297.  
  298. =cut
  299.  
  300. __PACKAGE__->meta->make_immutable;
  301.  
  302. 1;
Add Comment
Please, Sign In to add comment