| 1 | package Silki::Controller::Domain; | ||
| 2 | { | ||
| 3 | $Silki::Controller::Domain::VERSION = '0.29'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use namespace::autoclean; | ||
| 9 | |||
| 10 | use Silki::I18N qw( loc ); | ||
| 11 | use Silki::Schema::Domain; | ||
| 12 | |||
| 13 | use Moose; | ||
| 14 | |||
| 15 | BEGIN { extends 'Silki::Controller::Base' } | ||
| 16 | |||
| 17 | with qw( | ||
| 18 | Silki::Role::Controller::Pager | ||
| 19 | ); | ||
| 20 | |||
| 21 | sub _set_domain : Chained('/') : PathPart('domain') : CaptureArgs(1) { | ||
| 22 | my $self = shift; | ||
| 23 | my $c = shift; | ||
| 24 | my $domain_id = shift; | ||
| 25 | |||
| 26 | my $domain = Silki::Schema::Domain->new( domain_id => $domain_id ); | ||
| 27 | |||
| 28 | $c->redirect_and_detach( $c->domain()->uri( with_host => 1 ) ) | ||
| 29 | unless $domain; | ||
| 30 | |||
| 31 | $c->stash()->{domain} = $domain; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub edit_form : Chained('_set_domain') : PathPart('edit_form') : Args(0) { | ||
| 35 | my $self = shift; | ||
| 36 | my $c = shift; | ||
| 37 | |||
| 38 | $self->_require_site_admin($c); | ||
| 39 | |||
| 40 | $c->stash()->{template} = '/domain/edit-form'; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub domain : Chained('_set_domain') : PathPart('') : Args(0) : ActionClass('+Silki::Action::REST') { | ||
| 44 | } | ||
| 45 | |||
| 46 | sub domain_PUT { | ||
| 47 | my $self = shift; | ||
| 48 | my $c = shift; | ||
| 49 | |||
| 50 | $self->_require_site_admin($c); | ||
| 51 | |||
| 52 | my %form_data = $c->request()->domain_params(); | ||
| 53 | |||
| 54 | my $domain = $c->stash()->{domain}; | ||
| 55 | |||
| 56 | eval { $domain->update(%form_data) }; | ||
| 57 | |||
| 58 | if ( my $e = $@ ) { | ||
| 59 | $c->redirect_with_error( | ||
| 60 | error => $e, | ||
| 61 | uri => $domain->entity_uri( view => 'edit_form' ), | ||
| 62 | form_data => \%form_data, | ||
| 63 | ); | ||
| 64 | } | ||
| 65 | |||
| 66 | $c->session_object() | ||
| 67 | ->add_message( | ||
| 68 | loc( 'The %1 domain has been updated.', $domain->web_hostname() ) ); | ||
| 69 | |||
| 70 | $c->redirect_and_detach( $c->domain()->application_uri( path => '/domains' ) ); | ||
| 71 | } | ||
| 72 | |||
| 73 | sub new_domain_form : Path('/new_domain_form') : Args(0) { | ||
| 74 | my $self = shift; | ||
| 75 | my $c = shift; | ||
| 76 | |||
| 77 | $self->_require_site_admin($c); | ||
| 78 | |||
| 79 | $c->stash()->{template} = '/domain/new-domain-form'; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub domain_collection : Path('/domains') : Args(0) : ActionClass('+Silki::Action::REST') { | ||
| 83 | } | ||
| 84 | |||
| 85 | sub domain_collection_GET_html { | ||
| 86 | my $self = shift; | ||
| 87 | my $c = shift; | ||
| 88 | |||
| 89 | $self->_require_site_admin($c); | ||
| 90 | |||
| 91 | my ( $limit, $offset ) = $self->_make_pager( $c, Silki::Schema::Domain->Count() ); | ||
| 92 | |||
| 93 | $c->stash()->{domains} = Silki::Schema::Domain->All( | ||
| 94 | limit => $limit, | ||
| 95 | offset => $offset, | ||
| 96 | ); | ||
| 97 | |||
| 98 | $c->stash()->{template} = '/domain/domains'; | ||
| 99 | } | ||
| 100 | |||
| 101 | sub domain_collection_POST { | ||
| 102 | my $self = shift; | ||
| 103 | my $c = shift; | ||
| 104 | |||
| 105 | $self->_require_site_admin($c); | ||
| 106 | |||
| 107 | my %form_data = $c->request()->domain_params(); | ||
| 108 | |||
| 109 | my $domain = eval { Silki::Schema::Domain->insert(%form_data) }; | ||
| 110 | |||
| 111 | if ( my $e = $@ ) { | ||
| 112 | $c->redirect_with_error( | ||
| 113 | error => $e, | ||
| 114 | uri => $c->domain()->application_uri( path => 'new_domain_form' ), | ||
| 115 | form_data => \%form_data, | ||
| 116 | ); | ||
| 117 | } | ||
| 118 | |||
| 119 | $c->redirect_and_detach( $domain->uri( with_host => 1 ) ); | ||
| 120 | } | ||
| 121 | |||
| 122 | __PACKAGE__->meta()->make_immutable(); | ||
| 123 | |||
| 124 | 1; | ||
| 125 | |||
| 126 | # ABSTRACT: Controller class for domains | ||
| 127 | |||
| 128 | __END__ |