| 1 | package Rapi::Blog::Module::GridBase; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Moose; | ||
| 7 | extends 'Catalyst::Plugin::RapidApp::RapidDbic::TableBase'; | ||
| 8 | |||
| 9 | use RapidApp::Util ':all'; | ||
| 10 | use Rapi::Blog::Util; | ||
| 11 | |||
| 12 | sub BUILD { | ||
| 13 | my $self = shift; | ||
| 14 | |||
| 15 | my $source_name = $self->ResultSource->source_name; | ||
| 16 | |||
| 17 | if($source_name eq 'Category') { | ||
| 18 | $self->apply_extconfig( | ||
| 19 | use_add_form => 0, | ||
| 20 | persist_immediately => { | ||
| 21 | create => 0, update => 0, destroy => 1 | ||
| 22 | }, | ||
| 23 | autoload_added_record => 0, | ||
| 24 | toggle_edit_cells_init_off => 0 | ||
| 25 | ) | ||
| 26 | } | ||
| 27 | elsif($source_name eq 'Post') { | ||
| 28 | |||
| 29 | $self->apply_extconfig( | ||
| 30 | reload_on_show => \1, | ||
| 31 | autoload_added_record => \1, | ||
| 32 | store_button_cnf => { | ||
| 33 | add => { | ||
| 34 | text => 'New Post', | ||
| 35 | iconCls => 'icon-post-add', | ||
| 36 | showtext => 1 | ||
| 37 | } | ||
| 38 | }, | ||
| 39 | ); | ||
| 40 | |||
| 41 | $self->add_plugin('ra-grid-expand-max-cell-height'); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | has '+use_edit_form', default => 0; | ||
| 46 | |||
| 47 | around 'get_add_form' => sub { | ||
| 48 | my ($orig, $self, @args) = @_; | ||
| 49 | |||
| 50 | return { | ||
| 51 | %{$self->$orig(@args)}, | ||
| 52 | autoScroll => \0, | ||
| 53 | bodyStyle => 'padding: 0px;', | ||
| 54 | } | ||
| 55 | }; | ||
| 56 | |||
| 57 | around 'get_add_edit_form_items' => sub { | ||
| 58 | my ($orig, $self, @args) = @_; | ||
| 59 | |||
| 60 | my @items = $self->$orig(@args); | ||
| 61 | |||
| 62 | if($self->ResultSource->source_name eq 'Post') { | ||
| 63 | |||
| 64 | # Actually check to see if there are any Categories, and if | ||
| 65 | # there are not, don't present the editor | ||
| 66 | unless($self->ResultSource->schema->resultset('Category')->count > 0) { | ||
| 67 | @items = grep { ($_->{name}||'') ne 'categories' } @items; | ||
| 68 | } | ||
| 69 | |||
| 70 | # Actually check to see if there are any Sections, and if | ||
| 71 | # there are not, don't present the editor | ||
| 72 | unless($self->ResultSource->schema->resultset('Section')->count > 0) { | ||
| 73 | @items = grep { ($_->{name}||'') ne 'section' } @items; | ||
| 74 | } | ||
| 75 | |||
| 76 | |||
| 77 | my @sets = ( | ||
| 78 | |||
| 79 | $self->_collect_to_fieldset( | ||
| 80 | \@items, [qw/name title section categories author ts published/], { | ||
| 81 | width => 410, | ||
| 82 | title => 'Attributes', | ||
| 83 | labelWidth => 70 | ||
| 84 | }), | ||
| 85 | |||
| 86 | $self->_collect_to_fieldset( | ||
| 87 | \@items, [qw/image/], { | ||
| 88 | width => 150, | ||
| 89 | title => 'Image', | ||
| 90 | },{hideLabel => \1}), | ||
| 91 | |||
| 92 | $self->_collect_to_fieldset( | ||
| 93 | \@items, [qw/custom_summary/], { | ||
| 94 | width => 350, | ||
| 95 | title => join('', | ||
| 96 | 'Custom Summary ', | ||
| 97 | '<span style="font-size:.9em;">', | ||
| 98 | '(leave blank for auto-generated summary)', | ||
| 99 | '</span>'), | ||
| 100 | },{hideLabel => \1, growMax => 110, growMin => 110, anchor => '-5' }) | ||
| 101 | ); | ||
| 102 | |||
| 103 | my $wrap = { | ||
| 104 | xtype => 'fieldset', | ||
| 105 | layout => 'hbox', | ||
| 106 | anchor => '100%', | ||
| 107 | hideBorders => \1, | ||
| 108 | collapsible => \1, | ||
| 109 | style => 'padding-bottom:0px;padding-top:5px;', | ||
| 110 | items => \@sets | ||
| 111 | }; | ||
| 112 | |||
| 113 | my $disp = { | ||
| 114 | xtype => 'label', | ||
| 115 | html => join('', | ||
| 116 | '<div class="rabl-add-form-body-label">', | ||
| 117 | '<span class="main"><i class="fa fa-pencil-square-o"></i> Post body:</span>', | ||
| 118 | '<span class="hashtag">(enter Tags in Twitter-style <i>#Hashtag</i> format)</span>', | ||
| 119 | '<span class="format"><i class="fa fa-keyboard-o"></i> Markdown/HTML</span>', | ||
| 120 | '</div>' | ||
| 121 | ) | ||
| 122 | }; | ||
| 123 | |||
| 124 | @items = ($wrap,$disp,@items); | ||
| 125 | |||
| 126 | # For 'body' to always be the last item to maintain nearest expected functionality, | ||
| 127 | # even in the event of un-handled schema changes | ||
| 128 | my $bodyItm; | ||
| 129 | @items = grep { ($_->{name}||'') eq 'body' ? ($bodyItm = $_ and 0) : 1 } @items; | ||
| 130 | push @items, $bodyItm; | ||
| 131 | |||
| 132 | my $eF = $items[$#items] || {}; # last element | ||
| 133 | if($eF->{xtype} eq 'ra-md-editor') { | ||
| 134 | $eF->{_noAutoHeight} = 1; | ||
| 135 | $eF->{plugins} = 'ra-parent-gluebottom'; | ||
| 136 | $eF->{hideLabel} = \1; | ||
| 137 | $eF->{anchor} = '100%'; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | return @items; | ||
| 142 | }; | ||
| 143 | |||
| 144 | |||
| 145 | |||
| 146 | sub _collect_to_fieldset { | ||
| 147 | my ($self, $items, $cols, $opt, $ovr) = @_; | ||
| 148 | |||
| 149 | my %colndx = map {$_=>1} @$cols; | ||
| 150 | my %pulled = (); | ||
| 151 | my @remaining = (); | ||
| 152 | for my $itm (@$items) { | ||
| 153 | $colndx{$itm->{name}} | ||
| 154 | ? $pulled{$itm->{name}} = $itm | ||
| 155 | : push @remaining, $itm | ||
| 156 | } | ||
| 157 | |||
| 158 | @$items = @remaining; | ||
| 159 | |||
| 160 | if($ovr) { | ||
| 161 | $pulled{$_} = { %{$pulled{$_}}, %$ovr } for (keys %pulled); | ||
| 162 | } | ||
| 163 | |||
| 164 | my @f_items = map { $pulled{$_} || () } @$cols; | ||
| 165 | |||
| 166 | return { | ||
| 167 | xtype => 'fieldset', | ||
| 168 | #style => 'float:left;margin-right:10px;', | ||
| 169 | |||
| 170 | %{$opt||{}}, | ||
| 171 | items => \@f_items | ||
| 172 | } | ||
| 173 | } | ||
| 174 | |||
| 175 | |||
| 176 | |||
| 177 | |||
| 178 | before 'load_saved_search' => sub { (shift)->apply_permissions }; | ||
| 179 | |||
| 180 | sub apply_permissions { | ||
| 181 | my $self = shift; | ||
| 182 | my $c = RapidApp->active_request_context or return; | ||
| 183 | |||
| 184 | # System 'administrator' role trumps everything: | ||
| 185 | return if ($c->check_user_roles('administrator')); | ||
| 186 | |||
| 187 | # Only admins can edit grids: | ||
| 188 | $self->apply_extconfig( store_exclude_api => [qw(update destroy)] ); | ||
| 189 | |||
| 190 | |||
| 191 | my $User = Rapi::Blog::Util->get_User; | ||
| 192 | |||
| 193 | my $source_name = $self->ResultSource->source_name; | ||
| 194 | |||
| 195 | if($source_name eq 'Post') { | ||
| 196 | if($User->author) { | ||
| 197 | # authors can only post as themselves | ||
| 198 | $self->apply_columns({ author => { allow_add => 0 } }); | ||
| 199 | |||
| 200 | } | ||
| 201 | else { | ||
| 202 | # Deny all changes to Post if the user is not an author | ||
| 203 | $self->apply_extconfig( store_exclude_api => [qw(create update destroy)] ); | ||
| 204 | } | ||
| 205 | } | ||
| 206 | elsif($source_name eq 'Comment') { | ||
| 207 | if($User->comment) { | ||
| 208 | # commentors can only comment as themselves and only for the current time | ||
| 209 | $self->apply_columns({ | ||
| 210 | user => { allow_add => 0 }, | ||
| 211 | ts => { allow_add => 0 }, | ||
| 212 | }); | ||
| 213 | } | ||
| 214 | else { | ||
| 215 | # Deny all changes if the user is does not have 'comment' | ||
| 216 | $self->apply_extconfig( store_exclude_api => [qw(create update destroy)] ); | ||
| 217 | } | ||
| 218 | } | ||
| 219 | elsif($source_name eq 'User') { | ||
| 220 | my @deny_columns = qw/email admin author comment disabled set_pw preauth_actions/; | ||
| 221 | $self->apply_colspec_columns(\@deny_columns, no_column => 1); | ||
| 222 | } | ||
| 223 | else { | ||
| 224 | # deny all changes unless otherwise specified: | ||
| 225 | $self->apply_extconfig( store_exclude_api => [qw(create update destroy)] ); | ||
| 226 | } | ||
| 227 | |||
| 228 | } | ||
| 229 | |||
| 230 | |||
| 231 | |||
| 232 | 1; | ||
| 233 |