File Coverage

blib/lib/Catalyst/Plugin/RapidApp/NavCore/GridRole.pm
Criterion Covered Total %
statement 14 62 22.5
branch 1 28 3.5
condition 0 30 0.0
subroutine 5 10 50.0
pod 0 3 0.0
total 20 133 15.0


line stmt bran cond sub pod time code
1             package Catalyst::Plugin::RapidApp::NavCore::GridRole;
2 4     4   2337 use strict;
  4         10  
  4         106  
3 4     4   20 use warnings;
  4         9  
  4         87  
4 4     4   19 use Moose::Role;
  4         9  
  4         36  
5             with 'RapidApp::Module::StorCmp::Role::SavedSearch';
6              
7 4     4   18565 use RapidApp::Util qw(:all);
  4         8  
  4         6264  
8              
9             # This Role must be loaded in Grids for "Save Search" to be available in the
10             # options menu. Even if loaded, this Role will not enable itself unless the
11             # RapidApp::NavCore plugin has been loaded in the Catalyst app.
12              
13             has '_navcore_enabled', is => 'ro', isa => 'Bool', lazy => 1, default => sub {
14             my $self = shift;
15             my $c = $self->c;
16             return (
17             $c->does('Catalyst::Plugin::RapidApp::NavCore') ||
18             $c->registered_plugins('RapidApp::NavCore') #<-- this one doesn't seem to apply
19             ) ? 1 : 0;
20             };
21              
22             has 'plugin_config', is => 'ro', lazy => 1, default => sub {
23             my $self = shift;
24             my $c = $self->app;
25             my $config = clone($c->config->{'Plugin::RapidApp::NavCore'} || {});
26            
27             # -- Default configs --
28            
29             # allow_manage: Whether or not to allow managing the Navtree (Organize Navtree)
30             $config->{allow_manage} //= 1;
31            
32             # user_views: Whether or not to enable saved views/searches on a per-user basis
33             # (also requires Auth to be enabled)
34             $config->{user_views} //= 1;
35            
36             return $config;
37             };
38              
39             around 'options_menu_items' => sub {
40             my $orig = shift;
41             my $self = shift;
42            
43             return $self->$orig(@_) unless ($self->_navcore_enabled);
44            
45             my $save_cnf = {};
46             $save_cnf->{save_url} = $self->suburl('/save_search');
47             $save_cnf->{search_id} = $self->c->req->params->{search_id} if (defined $self->c->req->params->{search_id});
48             $save_cnf->{is_pub} = \1 if ($self->c->req->params->{public_search});
49            
50             # Turned off "Public Searches" now that we have the Manageable Public Navtree: (2012-02-18 by HV):
51             #$save_cnf->{pub_allowed} = \1 if ($self->c->model('DB')->has_roles(qw/admin modify_public_searches/));
52            
53             my $params = { %{$self->c->req->params} };
54             delete $params->{_dc} if (defined $params->{_dc});
55            
56             # Make sure search_id isn't saved (applies when saving searches of searches):
57             delete $params->{search_id} if (exists $params->{search_id});
58            
59             $save_cnf->{target_url} = $self->base_url;
60             $save_cnf->{target_params} = $self->json->encode($params);
61            
62             my $items = $self->$orig(@_) || [];
63            
64             push @$items, {
65             text => 'Save Search',
66             iconCls => 'ra-icon-save-as',
67             handler => RapidApp::JSONFunc->new( raw => 1, func =>
68             'function(cmp) { Ext.ux.RapidApp.NavCore.SaveSearchHandler(cmp,' . $self->json->encode($save_cnf) . '); }'
69             )
70             };
71            
72            
73             if ($self->c->req->params->{search_id}) {
74             push @$items, {
75             text => 'Delete Search',
76             iconCls => 'ra-icon-delete',
77             handler => RapidApp::JSONFunc->new( raw => 1, func =>
78             'function(cmp) { Ext.ux.RapidApp.NavCore.DeleteSearchHandler(cmp,"' .
79             $self->suburl('/delete_search') . '","' . $self->c->req->params->{search_id} . '"); }'
80             )
81             } unless (
82             $self->c->req->params->{public_search}
83             and not $self->c->model('DB')->has_roles(qw/admin modify_public_searches/)
84             );
85             }
86            
87             return $items;
88             };
89              
90              
91              
92              
93             sub load_saved_search {
94 21     21 0 147 my $self = shift;
95 21 50       62 my $search_id = $self->c->req->params->{search_id} or return 0;
96            
97            
98 0 0         return 0 unless ($self->_navcore_enabled);
99            
100            
101 0 0         my $Search = $self->c->model('RapidApp::CoreSchema::SavedState')->find($search_id)
102             or die usererr "Failed to load search ID '$search_id'";
103            
104 0           $self->apply_extconfig(
105             tabTitle => $Search->get_column('title'),
106             tabIconCls => $Search->get_column('iconcls'),
107             );
108            
109             # Merge in params, even though the request is already underway:
110 0   0 0     my $search_params = try{$self->json->decode($Search->get_column('params'))} || {};
  0            
111            
112             # TODO: this is now duplicated in the /view RequestMapper controller....
113 0           my @not_allowed_params = qw(search_id quick_search quick_search_cols quick_search_mode);
114 0   0       exists $search_params->{$_} and delete $search_params->{$_} for (@not_allowed_params);
115            
116 0           %{$self->c->req->params} = ( %{$self->c->req->params}, %$search_params );
  0            
  0            
117            
118             # Make sure search_id didn't get overridden: (issue with older saved searches)
119 0           $self->c->req->params->{search_id} = $search_id;
120            
121             # Update the DataStore 'baseParams' - needed because this already happened by this point...
122 0   0       my $baseParams = $self->DataStore->get_extconfig_param('baseParams') || {};
123 0   0 0     my $new_base_params = try{$self->json->decode($search_params->{base_params})} || {};
  0            
124 0           %$baseParams = ( %$baseParams, %$new_base_params );
125 0           $self->DataStore->apply_extconfig( baseParams => $baseParams );
126            
127 0 0 0       return if (!$Search->state_data || $Search->state_data eq '{}');
128 0 0         my $search_data = $self->json->decode($Search->state_data) or die usererr "Error deserializing grid_state";
129            
130 0           $self->apply_to_all_columns( hidden => \1 );
131              
132 0           return $self->batch_apply_opts_existing($search_data);
133             }
134              
135              
136             sub save_search {
137 0     0 0   my $self = shift;
138            
139 0           my $search_name = $self->c->req->params->{search_name};
140 0           my $state_data = $self->c->req->params->{state_data};
141 0           my $target_url = $self->c->req->params->{target_url};
142 0           my $target_params = $self->c->req->params->{target_params};
143 0           my $target_iconcls = $self->c->req->params->{target_iconcls};
144            
145             delete $self->c->req->params->{public_search} if (
146             defined $self->c->req->params->{public_search} and
147 0 0 0       $self->c->req->params->{public_search} eq 'false'
148             );
149            
150 0           my $public = $self->c->req->params->{public_search};
151            
152             # This codepath should never happen because if they don't have permission
153             # they shouldn't see the public checkbox in the first place:
154             #die usererr "You are not allowed to save/modify public searches" if (
155             # $public and
156             # not $self->c->model('DB')->has_roles(qw/admin modify_public_searches/)
157             #);
158            
159 0 0 0       $search_name = undef if (
      0        
160             defined $search_name and
161             $search_name eq '' or
162             $search_name eq 'false'
163             );
164            
165 0           my $Rs = $self->c->model('RapidApp::CoreSchema::SavedState');
166            
167             # Update existing search:
168 0           my $cur_id = $self->c->req->params->{cur_search_id};
169 0 0 0       if ($cur_id and not $self->c->req->params->{create_search}) {
170            
171             # TODO: check permissions
172            
173 0 0         my $Search = $Rs->find($cur_id)
174             or die usererr "Cannot update existing search '" . $cur_id . "'\n";
175            
176 0           return $Search->update({ state_data => $state_data });
177             }
178              
179 0 0 0       die usererr "Search name required"
180             unless (defined $search_name && $search_name ne '');
181            
182             # TODO: allow searches with the same name if different folder, etc
183 0 0         $Rs->search_rs({ 'me.title' => $search_name })->count
184             and die usererr "Search '" . $search_name . "' already exists";
185            
186 0           my $create = {
187             title => $search_name,
188             url => $target_url,
189             params => $target_params,
190             iconcls => $target_iconcls,
191             state_data => $state_data
192             };
193            
194 0     0     my $User = try{$self->c->user->get_from_storage};
  0            
195 0 0 0       if ($User && $self->plugin_config->{user_views}) {
196             # If Auth is enabled, assign the new search to the logged in User:
197 0           $create->{user_id} = $User->get_column('id');
198             }
199             else {
200             # Otherwise, drop it directly into the root of the Public Navtree:
201 0           $create->{node_id} = 0;
202             }
203            
204             # Turn off public search stuff:
205             #$create->{owner_id} = 1 if ($public);
206            
207 0           my $Row = $Rs->create($create);
208            
209             return {
210 0           success => \1,
211             msg => 'Created Search',
212             loadCnf => $Row->loadContentCnf
213             };
214             }
215              
216              
217              
218             sub delete_search {
219 0     0 0   my $self = shift;
220 0 0         my $search_id = $self->c->req->params->{search_id} or die usererr "Missing search_id";
221            
222             # TODO: enfornce permissions
223            
224            
225 0 0         my $Search = $self->c->model('RapidApp::CoreSchema::SavedState')->find($search_id)
226             or die usererr "Failed to find search ID '$search_id'";
227            
228 0           return $Search->delete;
229             }
230              
231              
232              
233              
234             1;
235