Guest User

Untitled

a guest
Jul 25th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.07 KB | None | 0 0
  1. package Portal::Controller::Bids;
  2. use Moose;
  3. use namespace::autoclean;
  4. #
  5. use Data::Dumper;
  6. #
  7.  
  8. BEGIN {extends 'Catalyst::Controller'; }
  9.  
  10. =head1 NAME
  11.  
  12. Portal::Controller::Bids - Catalyst Controller
  13.  
  14. =head1 DESCRIPTION
  15.  
  16. Catalyst Controller.
  17.  
  18. =head1 METHODS
  19.  
  20. =cut
  21.  
  22.  
  23. =head2 index
  24.  
  25. =cut
  26.  
  27. sub user : Chained( '/' ) : PathPart( 'bids' ) : CaptureArgs( 0 ) {
  28. my ( $self, $c ) = @_;
  29. $c->res->redirect( $c->uri_for( $c->controller( 'Auth' )->action_for( 'login' ) ) ) unless ( $c->user );
  30. }
  31.  
  32. sub plans : Chained( 'user' ) : PathPart( '' ) : CaptureArgs( 0 ) {
  33. my ( $self, $c ) = @_;
  34. $c->res->redirect( $c->uri_for( $c->controller( 'Auth' )->action_for( 'unauthorized' ) ) ) unless ( $c->check_user_roles( 'Plans' ) );
  35. }
  36.  
  37. sub index : Chained( 'user' ) : PathPart( '' ): Args( 0 ) {
  38. my ( $self, $c ) = @_;
  39.  
  40. if ( $c->req->method eq 'GET' ) {
  41. my @bids = $c->model( 'DB::Bids' )->search({ active => 1 }, order_by => [ { -desc => 'logged_date' }, { -desc => 'id' } ] )->all;
  42. $c->stash->{bids} = \@bids if ( @bids );
  43. } elsif ( $c->req->method eq 'POST' && $c->req->param( 'all_bids' ) ) {
  44. my @bids = $c->model( 'DB::Bids' )->search({}, order_by => { -desc => qw/logged_date id/ } )->all;
  45. $c->stash->{results} = 'All Bids';
  46. $c->stash->{bids} = \@bids if ( @bids );
  47. } else {
  48. my $args = $c->req->params();
  49. my $search;
  50. $search->{id} = { 'like', '%' . $args->{bid_id} . "%" } if ( $args->{bid_id} );
  51. $search->{pm_id} = $args->{pm_id} if ( $args->{pm_id} ne '-1' );
  52. $search->{apm_id} = $args->{apm_id} if ( $args->{apm_id} ne '-1' );
  53. $search->{due_client} = $args->{due_client} if ( $args->{due_client} );
  54. $search->{due_sub} = $args->{due_sub} if ( $args->{due_sub} );
  55. $search->{project} = { 'like', '%' . $args->{project} . '%' } if ( $args->{project} );
  56. $search->{location} = { 'like', '%' . $args->{location} . '%' } if ( $args->{location} );
  57. $search->{street} = { 'like', '%' . $args->{street} . '%' } if ( $args->{street} );
  58. $search->{city} = { 'like', '%' . $args->{city} . '%' } if ( $args->{city} );
  59. $search->{state_prov} = { 'like', '%' . $args->{state_prov} . '%' } if ( $args->{state_prov} );
  60. $search->{zip} = { 'like', '%' . $args->{zip} . '%' } if ( $args->{zip} );
  61. $search->{active} = $args->{active} if ( $args->{active} ne '-1' );
  62.  
  63. if ( $search ) {
  64. my @bids = $c->model( 'DB::Bids' )->search( $search, { order_by => 'id' } )->all;
  65. $c->stash->{results} = 'Results: ' . scalar( @bids );
  66. $c->stash->{bids} = \@bids if ( @bids );
  67. } else {
  68. $c->stash->{results} = 'No search criteria';
  69. }
  70. $c->log->warn( Dumper( $search ) );
  71. }
  72.  
  73. # Add users
  74. my @users = $c->model( 'DB::Users' )->all;
  75. $c->stash->{users} = \@users;
  76. # Add javascript
  77. $c->stash->{jscript} = [ $c->uri_for( '/static/js/bids/index.js' ) ];
  78. }
  79.  
  80. sub view : Chained( 'user' ) : PathPart( 'view' ) : Args( 1 ) {
  81. my ( $self, $c, $id ) = @_;
  82. my $bid = $c->model( "DB::Bids" )->find( $id );
  83.  
  84. $c->go( 'default' ) unless( $bid );
  85.  
  86. my @plans = $bid->plans->search({}, { order_by => 'id asc' } )->all;
  87.  
  88. $c->stash->{bid} = $bid;
  89. $c->stash->{plans} = \@plans;
  90. }
  91.  
  92. sub edit : Chained( 'plans' ) : PathPart( 'edit' ) : Args( 1 ) {
  93. my ( $self, $c, $id ) = @_;
  94.  
  95. if ( $c->req->method eq 'GET' ) {
  96.  
  97. my @users = $c->model( 'DB::Users' )->all;
  98. $c->stash->{users} = \@users;
  99.  
  100. my $bid = $c->model( 'DB::Bids' )->find( $id );
  101. $c->stash->{bid} = $bid;
  102. } elsif ( $c->req->method eq 'POST' ) {
  103. my $args = $c->req->params();
  104.  
  105. my $bid = $c->model( 'DB::Bids' )->find( $id );
  106. $bid->pm_id( $args->{pm_id} );
  107. $bid->apm_id( $args->{apm_id} );
  108. $bid->project( $args->{project} );
  109. $bid->location( $args->{location} );
  110. $bid->street( $args->{street} );
  111. $bid->space( $args->{space} );
  112. $bid->city( $args->{city} );
  113. $bid->state_prov( $args->{state_prov} );
  114. $bid->zip( $args->{zip} );
  115. $bid->password( $args->{password} );
  116. $bid->due_client( $args->{due_client} );
  117. $bid->due_sub( $args->{due_sub} );
  118. $bid->active( $args->{active} );
  119.  
  120. my @tags = reverse( $c->req->param( 'plan_tag' ) );
  121. my @active = reverse( $c->req->param( 'plan_active' ) );
  122. if ( $args->{active} == 0 ) { # If the bid goes inactive mark all plans inactive
  123. $_ = 0 for ( @active );
  124. }
  125. for my $plan ( $bid->plans ) {
  126. $plan->tag( pop( @tags ) );
  127. $plan->active( pop( @active ) );
  128. $plan->update();
  129. }
  130.  
  131. $bid->update(); # Update Bid
  132. $c->res->redirect( $c->uri_for( $c->controller( 'Bids' )->action_for( 'view' ) ) . '/' . $bid->id );
  133. }
  134. $c->stash->{jscript} = [ $c->uri_for( '/static/js/bids/edit.js' ) ];
  135. }
  136.  
  137. sub add : Chained( 'plans' ) : PathPart( 'add' ) : Args( 0 ) {
  138. my ( $self, $c ) = @_;
  139.  
  140. if ( $c->req->method eq 'POST' ) {
  141. my $p = $c->req->params();
  142.  
  143. $p->{creator_id} = $c->user->id; # Add the users ID
  144. $p->{file} = $c->req->upload( 'plan_file' ); # Add the file to the params
  145.  
  146. if ( $c->model( 'DB::Bids' )->add_bid( $p ) ) {
  147. # Creation Success
  148. } else {
  149. # Creation Failed
  150. }
  151. } else {
  152. my @users = $c->model( "DB::Users" )->search({ active => 1 })->all;
  153. $c->stash->{users} = \@users;
  154. $c->stash->{jscript} = [ $c->uri_for( '/static/js/bids/add.js' ) ];
  155. }
  156. }
  157.  
  158. =head1 AUTHOR
  159.  
  160. Frank Switalski,,,
  161.  
  162. =head1 LICENSE
  163.  
  164. This library is free software. You can redistribute it and/or modify
  165. it under the same terms as Perl itself.
  166.  
  167. =cut
  168.  
  169. __PACKAGE__->meta->make_immutable;
  170.  
  171. --------------------------------------------------------------------------------------------------------------------------
  172.  
  173. package Portal::Schema;
  174.  
  175. use strict;
  176. use warnings;
  177.  
  178. use base 'DBIx::Class::Schema';
  179.  
  180. __PACKAGE__->load_namespaces;
  181.  
  182.  
  183. # Created by DBIx::Class::Schema::Loader v0.04006 @ 2009-12-23 13:24:25
  184. # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:rnSUiKvdGPieUmrw2V71HA
  185.  
  186.  
  187. # You can replace this text with custom content, and it will be preserved on regeneration
  188. 1;
  189. package Portal::Schema::Result::Bids;
  190.  
  191. use strict;
  192. use warnings;
  193. use random;
  194. use DateTime;
  195.  
  196. use base 'DBIx::Class';
  197.  
  198. __PACKAGE__->load_components( "InflateColumn::DateTime", "Core" );
  199. __PACKAGE__->table( "bids" );
  200. __PACKAGE__->add_columns(
  201. "id",
  202. { data_type => "text" },
  203. "client_id",
  204. { data_type => "integer" },
  205. "pm_id",
  206. { data_type => "integer" },
  207. "apm_id",
  208. { data_type => "integer" },
  209. "project",
  210. { data_type => "text" },
  211. "due_client",
  212. { data_type => "date" },
  213. "due_sub",
  214. { data_type => "date" },
  215. "location",
  216. { data_type => "text" },
  217. "street",
  218. { data_type => "text" },
  219. "space",
  220. { data_type => "text" },
  221. "city",
  222. { data_type => "text" },
  223. "state_prov",
  224. { data_type => "text" },
  225. "zip",
  226. { data_type => "text" },
  227. "mall_phone",
  228. { data_type => "text" },
  229. "union_status",
  230. { data_type => "integer" },
  231. "logged_by",
  232. { data_type => "integer" },
  233. "logged_date",
  234. { data_type => "date" },
  235. "status",
  236. { data_type => "integer" },
  237. "password",
  238. { data_type => "text" },
  239. "description",
  240. { data_type => "text" },
  241. "security",
  242. { data_type => "integer" },
  243. "active",
  244. { data_type => "integer" }
  245. );
  246. __PACKAGE__->set_primary_key( "id" );
  247. __PACKAGE__->add_unique_constraint( "bids_pkey", ["id"] );
  248.  
  249. __PACKAGE__->has_one(
  250. "pm" => "Portal::Schema::Result::Users",
  251. { "foreign.id" => "self.pm_id" }
  252. );
  253.  
  254. __PACKAGE__->has_one(
  255. "apm" => "Portal::Schema::Result::Users",
  256. { "foreign.id" => "self.apm_id" }
  257. );
  258.  
  259. __PACKAGE__->has_one(
  260. "logger" => "Portal::Schema::Result::Users",
  261. { "foreign.id" => "self.logged_by" }
  262. );
  263.  
  264. __PACKAGE__->has_many(
  265. "plans",
  266. "Portal::Schema::Result::Plans",
  267. { "foreign.bid_id" => "self.id" }
  268. );
  269.  
  270. sub add_bid {
  271. my ( $self, $p ) = @_;
  272.  
  273. my $rand = 1 + rand( 10000 );
  274.  
  275. my $bid = $self->create({
  276. client_id => -1, ######################################### FIX THIS
  277. pm_id => $p->{pm_id},
  278. apm_id => $p->{apm_id},
  279. project => $p->{project},
  280. due_client => $p->{due_client},
  281. due_sub => $p->{due_sub},
  282. location => $p->{location},
  283. street => $p->{street},
  284. space => $p->{space},
  285. city => $p->{city},
  286. state_prov => $p->{state_prov},
  287. zip => $p->{zip},
  288. mall_phone => $p->{mall_phone},
  289. union_status => -1,
  290. logged_by => $p->{creator_id},
  291. status => -1,
  292. password => $p->{password},
  293. description => $p->{description},
  294. security => $rand,
  295. active => 1
  296. });
  297.  
  298. my $plan = $self->create_related( 'plans', {
  299. id => '00',
  300. tag => $p->{plan_tag},
  301. filename => $p->{plan}->filename,
  302. logged_by => $p->{creator_id},
  303. logged_date => DateTime->now(),
  304. active => 1
  305. });
  306.  
  307. my $file = $p->{file}->copy_to( '/home/fswitalski/' . $p->{plan}->filename );
  308.  
  309. if ( $bid && $plan && $file ) {
  310. return 1;
  311. }
  312. }
  313.  
  314. 1;
  315.  
  316. --------------------------------------------------------------------------------------------------------------
  317.  
  318. package Portal::Schema;
  319.  
  320. use strict;
  321. use warnings;
  322.  
  323. use base 'DBIx::Class::Schema';
  324.  
  325. __PACKAGE__->load_namespaces;
  326.  
  327.  
  328. # Created by DBIx::Class::Schema::Loader v0.04006 @ 2009-12-23 13:24:25
  329. # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:rnSUiKvdGPieUmrw2V71HA
  330.  
  331.  
  332. # You can replace this text with custom content, and it will be preserved on regeneration
  333. 1;
Add Comment
Please, Sign In to add comment