diff --git a/Web/lib/MJB/Web.pm b/Web/lib/MJB/Web.pm index 8134afc..7daf3d8 100644 --- a/Web/lib/MJB/Web.pm +++ b/Web/lib/MJB/Web.pm @@ -114,13 +114,17 @@ sub startup ($self) { $auth->post( '/password' )->to('UserSettings#do_change_password' )->name('do_change_password' ); # Dashboard - $auth->get ( '/dashboard' )->to('Dashboard#index' )->name('show_dashboard' ); - $auth->get ( '/dashboard/blog/:id' )->to('Dashboard#blog' )->name('show_dashboard_blog' ); - $auth->get ( '/dashboard/blog/:id/posts' )->to('Dashboard#blog_posts' )->name('show_dashboard_blog_posts' ); - $auth->get ( '/dashboard/blog/:id/post/*mdfile' )->to('Dashboard#blog_post' )->name('show_dashboard_blog_post' ); - $auth->post( '/dashboard/blog/:id/post/*mdfile' )->to('Dashboard#do_blog_post' )->name('do_dashboard_blog_post' ); - $auth->get ( '/dashboard/blog/:id/post' )->to('Dashboard#blog_post_create' )->name('show_dashboard_blog_post_create' ); - $auth->post( '/dashboard/blog/:id/post' )->to('Dashboard#do_blog_post_create' )->name('do_dashboard_blog_post_create' ); + $auth->get ( '/dashboard' )->to('Dashboard#index' )->name('show_dashboard' ); + $auth->get ( '/dashboard/blog/:id' )->to('Dashboard#blog' )->name('show_dashboard_blog' ); + $auth->get ( '/dashboard/blog/:id/posts' )->to('Dashboard#blog_posts' )->name('show_dashboard_blog_posts' ); + $auth->get ( '/dashboard/blog/:id/post/*mdfile' )->to('Dashboard#blog_post' )->name('show_dashboard_blog_post' ); + $auth->post( '/dashboard/blog/:id/post/*mdfile' )->to('Dashboard#do_blog_post' )->name('do_dashboard_blog_post' ); + $auth->get ( '/dashboard/blog/:id/post' )->to('Dashboard#blog_post_create' )->name('show_dashboard_blog_post_create' ); + $auth->post( '/dashboard/blog/:id/post' )->to('Dashboard#do_blog_post_create' )->name('do_dashboard_blog_post_create' ); + $auth->get ( '/dashboard/blog/:id/settings' )->to('Dashboard#blog_settings' )->name('show_dashboard_blog_settings' ); + $auth->post( '/dashboard/blog/:id/settings' )->to('Dashboard#do_blog_settings' )->name('do_dashboard_blog_settings' ); + $auth->get ( '/dashboard/blog/:id/config' )->to('Dashboard#blog_config' )->name('show_dashboard_blog_config' ); + $auth->post( '/dashboard/blog/:id/config' )->to('Dashboard#do_blog_config' )->name('do_dashboard_blog_config' ); # Blog Management $auth->get ( '/blog/create' )->to('Blog#create' )->name('show_blog_create' ); diff --git a/Web/lib/MJB/Web/Controller/Dashboard.pm b/Web/lib/MJB/Web/Controller/Dashboard.pm index cf0ec4e..3395791 100644 --- a/Web/lib/MJB/Web/Controller/Dashboard.pm +++ b/Web/lib/MJB/Web/Controller/Dashboard.pm @@ -150,4 +150,101 @@ sub do_blog_post_create ( $c ) { $c->redirect_to( $c->url_for( 'show_dashboard_blog_posts', { id => $blog->id } ) ); } +sub blog_settings ($c) { + my $blog = $c->stash->{blog} = $c->db->blog( $c->param('id') ); + + if ( $blog->person->id ne $c->stash->{person}->id ) { + $c->render( + text => "Error: This blog isn't owned by you.", + status => 404, + format => 'txt', + ); + return; + } + + my $data = $c->jekyll($blog->domain->name)->config->data; + + my $title = $c->stash->{form_title} = $data->{title}; + my $desc = $c->stash->{form_desc} = $data->{description}; + my $email = $c->stash->{form_email} = $data->{email}; + my $url = $c->stash->{form_url} = $data->{url}; + + +} + +sub do_blog_settings ($c) { + my $blog = $c->stash->{blog} = $c->db->blog( $c->param('id') ); + + if ( $blog->person->id ne $c->stash->{person}->id ) { + $c->render( + text => "Error: This blog isn't owned by you.", + status => 404, + format => 'txt', + ); + return; + } + + my $jekyll = $c->jekyll($blog->domain->name); + + my $title = $c->stash->{form_title} = $c->param( 'configTitle' ); + my $desc = $c->stash->{form_desc} = $c->param( 'configDesc' ); + my $email = $c->stash->{form_email} = $c->param( 'configEmail' ); + my $url = $c->stash->{form_url} = $c->param( 'configURL' ); + + $jekyll->config->data->{title} = $title; + $jekyll->config->data->{description} = $desc; + $jekyll->config->data->{email} = $email; + $jekyll->config->data->{url} = $url; + + $jekyll->write_config; + + $c->flash( confirmation => "The settings have been updated!!" ); + $c->redirect_to( $c->url_for( 'show_dashboard_blog_settings', { id => $blog->id } ) ); +} + +sub blog_config ( $c ) { + my $blog = $c->stash->{blog} = $c->db->blog( $c->param('id') ); + + if ( $blog->person->id ne $c->stash->{person}->id ) { + $c->render( + text => "Error: This blog isn't owned by you.", + status => 404, + format => 'txt', + ); + return; + } + + my $jekyll = $c->jekyll($blog->domain->name); + my $config = $jekyll->config; + + my $config_text = $c->stash->{form_config} = $config->as_text; +} + +sub do_blog_config ( $c ) { + my $blog = $c->stash->{blog} = $c->db->blog( $c->param('id') ); + + if ( $blog->person->id ne $c->stash->{person}->id ) { + $c->render( + text => "Error: This blog isn't owned by you.", + status => 404, + format => 'txt', + ); + return; + } + + my $jekyll = $c->jekyll($blog->domain->name); + my $config = $jekyll->config; + + my $config_text = $c->stash->{form_config} = $c->param( 'blogConfig' ); + + $config->set_from_text( $config_text ); + + $jekyll->write_config; + + $c->flash( confirmation => "The config has been updated!!" ); + $c->redirect_to( $c->url_for( 'show_dashboard_blog_config', { id => $blog->id } ) ); +} + + + 1; diff --git a/Web/templates/dashboard/blog_config.html.ep b/Web/templates/dashboard/blog_config.html.ep new file mode 100644 index 0000000..0cfec02 --- /dev/null +++ b/Web/templates/dashboard/blog_config.html.ep @@ -0,0 +1,56 @@ +% layout 'standard', title => 'Dashboard', sb_active => 'dashboard'; + +%= include 'dashboard/_blog_nav' + + + +% if ( my $confirmation = flash 'confirmation' ) { + +% } + +% if ( $c->stash->{success} ) { + +% } + +% if ( $c->stash->{errors} ) { + +% } + + +
+
+ + +
+ +
+ +
+
+ +Show Settings (Go Back) + diff --git a/Web/templates/dashboard/blog_settings.html.ep b/Web/templates/dashboard/blog_settings.html.ep new file mode 100644 index 0000000..66cbc5d --- /dev/null +++ b/Web/templates/dashboard/blog_settings.html.ep @@ -0,0 +1,71 @@ +% layout 'standard', title => 'Dashboard', sb_active => 'dashboard'; + +%= include 'dashboard/_blog_nav' + + + +% if ( my $confirmation = flash 'confirmation' ) { + +% } + +% if ( $c->stash->{success} ) { + +% } + +% if ( $c->stash->{errors} ) { + +% } + + +
+
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ +
+
+ +Show Config (Advanced) + diff --git a/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm b/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm index 04dc9de..bf28d1c 100644 --- a/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm +++ b/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm @@ -6,6 +6,7 @@ use File::Path qw( make_path ); use Storable qw( dclone ); use Mojo::File; use MJB::Backend::Jekyll::MarkdownFile; +use MJB::Backend::Jekyll::ConfigFile; # The root path for the repositories has root => ( @@ -29,6 +30,18 @@ has repo => ( required => 1, ); +has config => ( + is => 'lazy', +); + +sub _build_config { + my ( $self ) = @_; + + return MJB::Backend::Jekyll::ConfigFile->new( + path => $self->repo_path . "/_config.yml", + )->read; +} + has repo_path => ( is => 'lazy', ); @@ -105,6 +118,7 @@ sub get_post { )->read; } + sub new_post { my ( $self, $filename ) = @_; @@ -144,6 +158,31 @@ sub _post_path { return $self->repo_path . "/_posts/" . $title . ".markdown"; } +sub write_config { + my ( $self, $config ) = @_; + + $config ||= $self->config; + + $config->write; + + # Add the file to git + $self->system_command( [ qw( git add ), $config->path ], { + chdir => $self->repo_path, + }); + + # Commit the file + $self->system_command( [ qw( git commit -m ), "Updated Site Config" ], { + chdir => $self->repo_path, + }); + + # Push the repo to the store server + $self->system_command( [ qw( git push origin master ) ], { + chdir => $self->repo_path, + }); + + return 1; +} + sub write_post { my ( $self, $md_file ) = @_; diff --git a/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll/ConfigFile.pm b/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll/ConfigFile.pm new file mode 100644 index 0000000..8de5dae --- /dev/null +++ b/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll/ConfigFile.pm @@ -0,0 +1,63 @@ +package MJB::Backend::Jekyll::ConfigFile; +use Moo; +use YAML::XS qw( Load Dump ); + +# File path we are read/write from +has path => ( + is => 'ro', + required => 1, +); + +has data => ( + is => 'rw', + default => sub { return +{} }, +); + +sub as_text { + my ( $self ) = @_; + + return Dump($self->data); +} + +sub set_from_text { + my ( $self, $config ) = @_; + + $self->data( Load($config) ); + + return $self; +} + +sub read { + my ( $self ) = @_; + + + $self->data( { } ); + + open my $lf, "<", $self->path + or die "Failed to open " . $self->path . " for reading: $!"; + + my $content = do { local $/; <$lf> }; + + close $lf; + + $self->data( Load($content) ); + + return $self; +} + +sub write { + my ( $self, $file ) = @_; + + $file ||= $self->path; + + open my $sf, ">", $file + or die "Failed to open $file for writing: $!"; + + print $sf Dump($self->data); + + close $sf; + + return $self; +} + +1;