Rework controller for proper chaining.

agranig/1_0_subfix
Andreas Granig 13 years ago
parent 3f7df5166b
commit 5ce4b91aa8

@ -1,6 +1,7 @@
package NGCP::Panel::Controller::Reseller;
use Moose;
use namespace::autoclean;
use Data::Dumper;
BEGIN { extends 'Catalyst::Controller'; }
@ -18,14 +19,11 @@ Catalyst Controller.
=cut
=head2 index
=cut
sub reseller : Path Chained('/') CaptureArgs(0) {
sub list :Chained('/') :PathPart('reseller') :CaptureArgs(0) {
my ($self, $c) = @_;
$c->log->debug("++++ Reseller::list");
my $resellers = [
{id => 1, contract_id => 1, name => 'reseller 1', status => 'active'},
{id => 2, contract_id => 2, name => 'reseller 2', status => 'active'},
@ -35,65 +33,96 @@ sub reseller : Path Chained('/') CaptureArgs(0) {
{id => 6, contract_id => 6, name => 'reseller 6', status => 'active'},
];
$c->stash(resellers => $resellers);
$c->stash(template => 'reseller.tt');
$c->stash(template => 'reseller/list.tt');
}
sub root :Chained('list') :PathPart('') :Args(0) {
my ($self, $c) = @_;
sub edit : Chained('reseller') PathPart('edit') :Args(1) {
my ($self, $c, $reseller_id) = @_;
$c->log->debug("++++ Reseller::root");
}
my $reseller;
if($c->flash->{reseller}) {
$reseller = $c->flash->{reseller};
} else {
my @rfilter = grep { $_->{id} == $reseller_id } @{ $c->stash->{resellers} };
$reseller = shift @rfilter;
}
sub create :Chained('list') :PathPart('create') :Args(0) {
my ($self, $c) = @_;
$c->log->debug("++++ Reseller::create");
my $form = NGCP::Panel::Form::Reseller->new;
$form->process(
params => $reseller,
action => $c->uri_for('/reseller/save', $reseller_id),
posted => ($c->request->method eq 'POST'),
params => $c->request->params,
action => $c->uri_for('create'),
);
if($form->validated) {
$c->log->debug("---- Reseller::create validated");
$c->flash(messages => [{type => 'success', text => 'Reseller successfully created!'}]);
$c->response->redirect($c->uri_for());
return;
}
$c->stash(create_flag => 1);
$c->stash(form => $form);
$c->stash(edit => $reseller);
}
sub create : Chained('reseller') PathPart('create') :Args(0) {
sub search :Chained('list') :PathPart('search') Args(0) {
my ($self, $c) = @_;
$c->response->redirect($c->uri_for('/reseller'));
$c->log->debug("++++ Reseller::search");
$c->flash(messages => [{type => 'info', text => 'Reseller search not implemented!'}]);
$c->response->redirect($c->uri_for());
}
sub save : Path('/reseller/save') :Args(1) {
sub base :Chained('/reseller/list') :PathPart('') :CaptureArgs(1) {
my ($self, $c, $reseller_id) = @_;
my $form = NGCP::Panel::Form::Reseller->new;
$form->process(
posted => ($c->req->method eq 'POST'),
params => $c->request->params,
);
if($form->validated) {
$c->log->debug(">>>>>> reseller data validated");
$c->response->redirect($c->uri_for('/reseller'));
# TODO: success message
} else {
$c->log->debug(">>>>>> reseller data NOT validated");
$c->flash(reseller => $c->request->params);
$c->response->redirect($c->uri_for('/reseller/edit', $reseller_id));
$c->log->debug("++++ Reseller::base");
unless($reseller_id && $reseller_id =~ /^\d+$/) {
$c->log->debug("---- invalid reseller_id '$reseller_id', going back");
# TODO: error message
$c->response->redirect($c->uri_for());
return;
}
# TODO: fetch details of reseller from model
my @rfilter = grep { $_->{id} == $reseller_id } @{ $c->stash->{resellers} };
$c->stash(reseller => shift @rfilter);
}
sub delete : Path('/reseller/delete') :Args(1) {
my ($self, $c, $reseller_id) = @_;
$c->response->redirect($c->uri_for('/reseller'));
sub edit :Chained('base') :PathPart('edit') :Args(0) {
my ($self, $c) = @_;
$c->log->debug("++++ Reseller::edit");
my $posted = ($c->request->method eq 'POST');
my $form = NGCP::Panel::Form::Reseller->new;
$form->process(
posted => 1,
params => $posted ? $c->request->params : $c->stash->{reseller},
action => $c->uri_for($c->stash->{reseller}->{id}, 'edit'),
);
if($posted && $form->validated) {
$c->log->debug("---- Reseller::edit validated");
$c->flash(messages => [{type => 'success', text => 'Reseller successfully changed!'}]);
$c->response->redirect($c->uri_for());
return;
}
$c->stash(form => $form);
}
sub search : Path('/reseller/search') :Args(0) {
sub delete :Chained('base') :PathPart('delete') :Args(0) {
my ($self, $c) = @_;
$c->response->redirect($c->uri_for('/reseller'));
# TODO: perform deletion
# $c->model('Provisioning')->reseller($c->stash->{reseller}->{id})->delete;
$c->flash(messages => [{type => 'info', text => 'Reseller delete not implemented!'}]);
$c->response->redirect($c->uri_for());
}
=head1 AUTHOR
Andreas Granig,,,

@ -10,6 +10,10 @@ has '+widget_wrapper' => ( default => 'Bootstrap' );
sub build_render_list {[qw/fields actions/]}
sub build_form_element_class { [qw/form-horizontal/] }
has_field 'id' => (
type => 'Hidden',
);
has_field 'name' => (
type => 'Text',
required => 1,
@ -35,7 +39,7 @@ has_field 'save' => (
has_block 'fields' => (
tag => 'div',
class => [qw/modal-body/],
render_list => [qw/contract_id name status/],
render_list => [qw/id contract_id name status/],
);
has_block 'actions' => (

@ -2079,7 +2079,7 @@ Layout: Content
margin-bottom: 8em;
}
#content .row {
margin-top: 5em;
margin-top: 1em;
}
#content .row:first-child {
margin-top: 0;

@ -1,7 +1,14 @@
<div class="row">
<a class="btn btn-primary btn-large" href="[% c.uri_for_action("/${helper.controller}/create") %]"><i class="icon-star"></i> Create [% helper.name %]</a>
<a class="btn btn-primary btn-large" href="[% c.uri_for_action("/${helper.controller}/search") %]"><i class="icon-search"></i> Search [% helper.name %]</a>
<a class="btn btn-primary btn-large" href="[% c.uri_for(c.action, c.req.captures, 'create') %]"><i class="icon-star"></i> Create [% helper.name %]</a>
<a class="btn btn-primary btn-large" href="[% c.uri_for(c.action, c.req.captures, 'search') %]"><i class="icon-search"></i> Search [% helper.name %]</a>
</div>
[% IF helper.messages -%]
<div class="row">
[% FOREACH m IN helper.messages -%]
<div class="alert alert-[% m.type %]">[% m.text %]</div>
[% END -%]
</div>
[% END -%]
<div class="row">
<table class="table table-bordered table-striped table-highlight table-hover">
<thead>
@ -21,8 +28,8 @@
[% END -%]
<td class="span3">
<div class="sw_actions pull-right">
<a class="btn btn-small btn-primary" href="[% c.uri_for_action("/${helper.controller}/edit", set.id) %]" style="display:inline;line-height:16px;"><i class="icon-edit" style="line-height:1em;margin-top:2px"></i> Edit</a>
<a class="btn btn-small btn-secondary" href="[% c.uri_for_action("/${helper.controller}/delete", set.id) %]" data-confirm="Delete" style="display:inline;line-height:16px;"><i class="icon-trash" style="line-height:1em;margin-top:2px"></i> Delete</a>
<a class="btn btn-small btn-primary" href="[% c.uri_for(set.id, 'edit') %]" style="display:inline;line-height:16px;"><i class="icon-edit" style="line-height:1em;margin-top:2px"></i> Edit</a>
<a class="btn btn-small btn-secondary" href="[% c.uri_for(set.id, 'delete') %]" data-confirm="Delete" style="display:inline;line-height:16px;"><i class="icon-trash" style="line-height:1em;margin-top:2px"></i> Delete</a>
</div>
</td>
</tr>
@ -30,11 +37,11 @@
</tbody>
</table>
</div>
[% IF helper.edit_object -%]
[% IF helper.edit_object || helper.create_flag == 1 -%]
<div id="mod_edit" class="modal hide" style="display:block">
<div class="modal-header">
<button id="mod_close" type="button" class="close">×</button>
<h3>Edit [% helper.name %]</h3>
<h3>[% helper.create_flag == 1 ? 'Create' : 'Edit' %] [% helper.name %]</h3>
</div>
[% helper.form_object.render %]
</div>
@ -42,7 +49,7 @@
$(function () {
$('#mod_edit').modal({keyboard: false, backdrop: 'static'});
$('#mod_close').click(function(event) {
window.location.href="[% c.uri_for("/${helper.controller}") %]";
window.location.href="[% c.uri_for() %]";
});
});

@ -1,12 +1,13 @@
[% META title = 'Resellers' -%]
[%
helper.name = 'Reseller';
helper.controller = 'reseller';
helper.data = resellers;
helper.messages = messages;
helper.column_titles = [ '#', 'Name', 'Contract #', 'Status' ];
helper.column_fields = [ 'id', 'name', 'contract_id', 'status' ];
helper.edit_object = edit;
helper.create_flag = create_flag;
helper.edit_object = reseller;
helper.form_object = form;
PROCESS 'helpers/table_form.tt';
Loading…
Cancel
Save