| 1 | package Rapi::Blog::Module::SectionTree; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Moose; | ||
| 5 | extends 'RapidApp::Module::Tree'; | ||
| 6 | |||
| 7 | use RapidApp::Util qw(:all); | ||
| 8 | use Rapi::Blog::Util; | ||
| 9 | |||
| 10 | has '+root_node_text', default => 'Sections'; | ||
| 11 | has '+show_root_node', default => 1; | ||
| 12 | has '+fetch_nodes_deep', default => 1; | ||
| 13 | has '+use_contextmenu', default => 1; | ||
| 14 | has '+no_dragdrop_menu', default => 1; | ||
| 15 | has '+setup_tbar', default => 1; | ||
| 16 | has '+no_recursive_delete', default => 0; | ||
| 17 | |||
| 18 | has '+add_button_text', default => 'Add Section'; | ||
| 19 | has '+add_button_iconCls', default => 'icon-folder-add'; | ||
| 20 | has '+delete_button_text', default => 'Delete Section'; | ||
| 21 | has '+delete_button_iconCls', default => 'icon-folder-delete'; | ||
| 22 | |||
| 23 | sub BUILD { | ||
| 24 | my $self = shift; | ||
| 25 | |||
| 26 | $self->root_node->{iconCls} = 'icon-sitemap-color'; | ||
| 27 | |||
| 28 | $self->apply_extconfig( | ||
| 29 | tabTitle => 'Manage Sections', | ||
| 30 | tabIconCls => 'icon-sitemap-color', | ||
| 31 | border => \1, | ||
| 32 | autoScroll => \1 | ||
| 33 | ); | ||
| 34 | |||
| 35 | } | ||
| 36 | |||
| 37 | sub is_admin { | ||
| 38 | my $self = shift; | ||
| 39 | my $User = Rapi::Blog::Util->get_User; | ||
| 40 | $User && $User->admin ? 1 : 0 | ||
| 41 | } | ||
| 42 | |||
| 43 | around 'content' => sub { | ||
| 44 | my ($orig, $self, @args) = @_; | ||
| 45 | my $cfg = $self->$orig(@args); | ||
| 46 | |||
| 47 | unless ($self->is_admin) { | ||
| 48 | my @ops = qw/add delete rename copy move/; | ||
| 49 | $cfg->{$_.'_node_url'} = undef for (@ops); | ||
| 50 | } | ||
| 51 | |||
| 52 | return $cfg | ||
| 53 | }; | ||
| 54 | |||
| 55 | |||
| 56 | sub get_node_id { | ||
| 57 | my ($self, $node) = @_; | ||
| 58 | |||
| 59 | my $id = (reverse split(/\//,$node))[0]; | ||
| 60 | $id = undef if ($id eq $self->root_node_name); | ||
| 61 | |||
| 62 | $id | ||
| 63 | } | ||
| 64 | |||
| 65 | sub Rs { (shift)->c->model('DB::Section') } | ||
| 66 | sub pRs { (shift)->c->model('DB::Post') } | ||
| 67 | |||
| 68 | sub Sections_of { | ||
| 69 | my ($self, $id) = @_; | ||
| 70 | $self->Rs | ||
| 71 | ->search_rs({ 'me.parent_id' => $id }) | ||
| 72 | ->all | ||
| 73 | } | ||
| 74 | |||
| 75 | sub Posts_of { | ||
| 76 | my ($self, $id) = @_; | ||
| 77 | $self->pRs | ||
| 78 | ->search_rs({ 'me.section_id' => $id }) | ||
| 79 | ->all | ||
| 80 | } | ||
| 81 | |||
| 82 | sub get_Section { | ||
| 83 | my ($self, $id) = @_; | ||
| 84 | $self->Rs->find($id) | ||
| 85 | } | ||
| 86 | |||
| 87 | sub get_Post { | ||
| 88 | my ($self, $id) = @_; | ||
| 89 | $self->pRs->find($id) | ||
| 90 | } | ||
| 91 | |||
| 92 | sub get_post_id { | ||
| 93 | my ($self, $id) = @_; | ||
| 94 | my ($junk,$post_id) = split(/^p-/,$id,2); | ||
| 95 | $post_id | ||
| 96 | } | ||
| 97 | |||
| 98 | sub fetch_nodes { | ||
| 99 | my ($self, $node) = @_; | ||
| 100 | |||
| 101 | my $id = $self->get_node_id($node); | ||
| 102 | |||
| 103 | my @nodes = (); | ||
| 104 | |||
| 105 | push @nodes, { | ||
| 106 | id => 'unsectioned', | ||
| 107 | text => '<i style="opacity:0.6;">unsectioned</i>', | ||
| 108 | iconCls => 'icon-outbox', | ||
| 109 | allowAdd => \0, | ||
| 110 | rootValidActions => \1, | ||
| 111 | } unless($id); | ||
| 112 | |||
| 113 | foreach my $Section ($self->Sections_of($id)) { | ||
| 114 | my $cfg = { | ||
| 115 | id => $Section->id, | ||
| 116 | text => $Section->name | ||
| 117 | }; | ||
| 118 | |||
| 119 | $cfg->{expanded} = \1 unless ($Section->sections->count > 0); | ||
| 120 | |||
| 121 | push @nodes, $cfg; | ||
| 122 | } | ||
| 123 | |||
| 124 | if($id) { | ||
| 125 | $id = undef if ($id eq 'unsectioned'); | ||
| 126 | foreach my $Post ($self->Posts_of($id)) { | ||
| 127 | my $cfg = { | ||
| 128 | id => join('-','p',$Post->id), | ||
| 129 | text => $Post->title, | ||
| 130 | iconCls => 'icon-post', | ||
| 131 | leaf => \1, | ||
| 132 | expanded => \1, | ||
| 133 | allowDrop => \0, | ||
| 134 | allowAdd => \0, | ||
| 135 | allowDelete => \0 | ||
| 136 | }; | ||
| 137 | push @nodes, $cfg; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | \@nodes | ||
| 142 | } | ||
| 143 | |||
| 144 | |||
| 145 | |||
| 146 | sub add_node { | ||
| 147 | my ($self,$name,$node,$params) = @_; | ||
| 148 | |||
| 149 | die usererr "Create Section: PERMISSION DENIED" unless ($self->is_admin); | ||
| 150 | |||
| 151 | my $id = $self->get_node_id($node); | ||
| 152 | |||
| 153 | # should be redundant: | ||
| 154 | die usererr "Cannot add a Section to a post" if ($self->get_post_id($id)); | ||
| 155 | |||
| 156 | my $Section = $self->Rs->create({ | ||
| 157 | parent_id => $id, | ||
| 158 | name => $name | ||
| 159 | }); | ||
| 160 | |||
| 161 | return { | ||
| 162 | msg => 'Created', | ||
| 163 | success => \1, | ||
| 164 | child => { | ||
| 165 | id => $Section->id, | ||
| 166 | text => $Section->name | ||
| 167 | } | ||
| 168 | |||
| 169 | }; | ||
| 170 | } | ||
| 171 | |||
| 172 | |||
| 173 | sub rename_node { | ||
| 174 | my ($self,$node,$name,$params) = @_; | ||
| 175 | |||
| 176 | die usererr "Rename: PERMISSION DENIED" unless ($self->is_admin); | ||
| 177 | |||
| 178 | my $id = $self->get_node_id($node); | ||
| 179 | die "Cannot rename the root node" unless ($id); | ||
| 180 | |||
| 181 | # strip whitespace | ||
| 182 | $name =~ s/^\s+//; | ||
| 183 | $name =~ s/\s+$//; | ||
| 184 | |||
| 185 | if(my $post_id = $self->get_post_id($id)) { | ||
| 186 | my $Post = $self->get_Post($post_id) or die "Post id '$post_id' not found"; | ||
| 187 | |||
| 188 | $Post->update({ title => $name }); | ||
| 189 | |||
| 190 | return { | ||
| 191 | msg => 'Renamed', | ||
| 192 | success => \1, | ||
| 193 | new_text => $Post->title, | ||
| 194 | }; | ||
| 195 | } | ||
| 196 | else { | ||
| 197 | my $Section = $self->get_Section($id) or die "Section id '$id' not found"; | ||
| 198 | |||
| 199 | $Section->update({ name => $name }); | ||
| 200 | |||
| 201 | return { | ||
| 202 | msg => 'Renamed', | ||
| 203 | success => \1, | ||
| 204 | new_text => $Section->name, | ||
| 205 | }; | ||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | sub delete_node { | ||
| 210 | my $self = shift; | ||
| 211 | my $node = shift; | ||
| 212 | |||
| 213 | die usererr "Delete: PERMISSION DENIED" unless ($self->is_admin); | ||
| 214 | |||
| 215 | my $id = $self->get_node_id($node); | ||
| 216 | die "Cannot rename the root node" unless ($id); | ||
| 217 | |||
| 218 | # should be redundant: | ||
| 219 | die usererr "Posts not allowed to be deleted from here" if ($self->get_post_id($id)); | ||
| 220 | |||
| 221 | my $Section = $self->get_Section($id) or die "Section id '$id' not found"; | ||
| 222 | |||
| 223 | $Section->delete; | ||
| 224 | |||
| 225 | return { | ||
| 226 | msg => "Deleted", | ||
| 227 | success => \1 | ||
| 228 | }; | ||
| 229 | } | ||
| 230 | |||
| 231 | |||
| 232 | sub move_node { | ||
| 233 | my $self = shift; | ||
| 234 | my $node = shift; | ||
| 235 | my $target = shift; | ||
| 236 | my $point = shift; | ||
| 237 | |||
| 238 | die usererr "Move Section: PERMISSION DENIED" unless ($self->is_admin); | ||
| 239 | |||
| 240 | my $id = $self->get_node_id($node); | ||
| 241 | my $tid = $self->get_node_id($target); | ||
| 242 | |||
| 243 | $tid = undef if ($tid eq 'unsectioned'); | ||
| 244 | |||
| 245 | # should be redundant: | ||
| 246 | die usererr "Posts cannot contain sub-items" if ($self->get_post_id($tid)); | ||
| 247 | |||
| 248 | if(my $post_id = $self->get_post_id($id)) { | ||
| 249 | my $Post = $self->get_Post($post_id) or die "Post id '$post_id' not found"; | ||
| 250 | |||
| 251 | $Post->section_id($tid); | ||
| 252 | $Post->update | ||
| 253 | } | ||
| 254 | else { | ||
| 255 | my $Section = $self->get_Section($id) or die "Section id '$id' not found"; | ||
| 256 | |||
| 257 | $Section->parent_id($tid); | ||
| 258 | $Section->update | ||
| 259 | } | ||
| 260 | } | ||
| 261 | |||
| 262 | |||
| 263 | |||
| 264 | |||
| 265 | 1; |