OpenInteract/CommonHandler.pm untested

filter: Statementstmt 0.0% 0 / 577 Branchbran 0.0% 0 / 144 Conditioncond 0.0% 0 / 138 MC/DCmcdc 0.0% 0 / 86 Subroutinesub 0.0% 0 / 99 Podpod 0.0% 0 / 91 totaltotal 0.0% 0 / 1135 CC 73 SCAR 35.6
CC
73
Cov
0%
_search_build_where_clause
182.0
search
90.0
edit
90.0
CRAP 35.1
1 package OpenInteract::CommonHandler;
2  
3 # $Id: CommonHandler.pm,v 1.44 2003/11/15 02:02:32 lachoy Exp $
4  
5 use strict;
6 use Data::Dumper qw( Dumper );
7 use OpenInteract::Handler::GenericDispatcher;
8 use SPOPS::Secure qw( :level );
9 require Exporter;
10  
11 @OpenInteract::CommonHandler::ISA = qw( OpenInteract::Handler::GenericDispatcher );
12 $OpenInteract::CommonHandler::VERSION = sprintf("%d.%02d", q$Revision: 1.44 $ =~ /(\d+)\.(\d+)/);
13 @OpenInteract::CommonHandler::EXPORT_OK = qw( OK ERROR );
14  
15 use constant OK => '1';
16 use constant ERROR => '4';
17  
18  
19 ########################################
20 # SEARCH FORM
21 ########################################
22  
23 # Common handler method for a search form (easy)
24  
25 sub search_form {
26 my ( $class, $p ) = @_;
27 my $R = OpenInteract::Request->instance;
28 unless ( $class->MY_ALLOW_SEARCH_FORM ) {
29 $R->scrib( 0, "User requested search_form for ($class) and it's not allowed." );
30 return '<h1>Error</h1><p>Objects of this type cannot be searched.</p>';
31 }
32 $p ||= {};
33  
34 my %params = %{ $p };
35 $R->{page}{title} = $class->MY_SEARCH_FORM_TITLE;
36  
37 $class->_search_form_customize( \%params );
38 my $template_name = $class->_template_name(
39 \%params,
40 $class->MY_SEARCH_FORM_TEMPLATE( \%params ) );
41 return $R->template->handler( {}, \%params, { name => $template_name } );
42 }
43  
44  
45 ########################################
46 # SEARCH
47 ########################################
48  
49 # Common handler method for a search
50  
51 sub search {
52 my ( $class, $p ) = @_;
53 my $R = OpenInteract::Request->instance;
54 unless ( $class->MY_ALLOW_SEARCH ) {
55 $R->scrib( 0, "User requested search for ($class) and it's not allowed." );
56 return '<h1>Error</h1><p>Objects of this type cannot be searched.</p>';
57 }
58  
59 $p ||= {};
60 my %params = %{ $p };
61  
62 my $apr = $R->apache;
63  
64 if ( $class->MY_SEARCH_RESULTS_PAGED ) {
65 require OpenInteract::ResultsManage;
66 my $search_id = $class->_search_get_id;
67 my $results = OpenInteract::ResultsManage->new();
68  
69 # If the search has been run before, just set the ID
70  
71 if ( $search_id ) {
72 $R->DEBUG && $R->scrib( 1, "Retrieving search for ID ($search_id)" );
73 $results->{search_id} = $search_id;
74 }
75  
76 # Otherwise, run the search and get an iterator back, then
77 # pass the iterator to ResultsManage so we can reuse the
78 # results
79  
80 else {
81 $R->DEBUG && $R->scrib( 1, "Running search for the first time" );
82 my ( $iterator, $msg ) =
83 eval { $class->_search_build_and_run({ %params, is_paged => 1 }) };
84  
85 # TODO: We will probably catch a specific exception here
86 # when we use exceptions in OI
87  
88 if ( ! $iterator and $msg ) {
89 my $cap_task = $class->MY_SEARCH_RESULTS_CAP_FAIL_TASK;
90 return $class->$cap_task({ %params, error_msg => $msg });
91 }
92  
93 if ( $@ ) {
94 my $fail_task = $class->MY_SEARCH_FAIL_TASK;
95 return $class->$fail_task({ %params, error_msg => "Search failed: $@" });
96 }
97  
98 $results->save( $iterator );
99 $R->DEBUG && $R->scrib( 1, "Search ID ($results->{search_id})" );
100 $class->_search_save_id( $results->{search_id} );
101 }
102  
103 if ( $results->{search_id} ) {
104 $params{page_number_field} = $class->MY_SEARCH_RESULTS_PAGE_FIELD;
105 $params{current_page} = $apr->param( $params{page_number_field} ) || 1;
106 my $hits_per_page = $class->MY_SEARCH_RESULTS_PAGE_SIZE;
107 my ( $min, $max ) = $results->find_page_boundaries(
108 $params{current_page}, $hits_per_page );
109 $params{iterator} = $results->retrieve({ min => $min, max => $max,
110 return => 'iterator' });
111 $params{total_pages} = $results->find_total_page_count( $hits_per_page );
112 $params{total_hits} = $results->{num_records};
113 $params{search_id} = $results->{search_id};
114 $params{search_results_key} = $class->MY_SEARCH_RESULTS_KEY;
115 $R->DEBUG && $R->scrib( 1, "Search info: min: ($min); max: ($max)",
116 "records ($results->{num_records})" );
117 }
118 }
119  
120 # If we're not using paged results, then just run the normal
121 # search and get back an iterator
122  
123 else {
124 my ( $msg );
125 ( $params{iterator}, $msg ) =
126 eval { $class->_search_build_and_run( \%params ) };
127  
128 # TODO: We will probably catch a specific exception here
129 # when we use exceptions in OI
130  
131 if ( ! $params{iterator} and $msg ) {
132 my $cap_task = $class->MY_SEARCH_RESULTS_CAP_FAIL_TASK;
133 return $class->$cap_task({ %params, error_msg => $msg });
134 }
135  
136 if ( $@ ) {
137 my $fail_task = $class->MY_SEARCH_FAIL_TASK;
138 $R->scrib( 0, "Got error from running search: $@" );
139 return $class->$fail_task({ %params, error_msg => "Search failed: $@" });
140 }
141 }
142  
143 $R->{page}{title} = $class->MY_SEARCH_RESULTS_TITLE;
144  
145 $class->_search_customize( \%params );
146 my $template_name = $class->_template_name(
147 \%params,
148 $class->MY_SEARCH_RESULTS_TEMPLATE( \%params ) );
149 return $R->template->handler( {}, \%params, { name => $template_name } );
150 }
151  
152  
153 sub _search_get_id {
154 my ( $class ) = @_;
155 my $R = OpenInteract::Request->instance;
156 my $search_key = $class->MY_SEARCH_RESULTS_KEY;
157 return $R->apache->param( $search_key );
158 }
159  
160  
161 # If the handler wants to save the search ID elsewhere (session,
162 # etc.), override this
163  
164 sub _search_save_id { return $_[1] }
165  
166  
167 # Build the search and run it, returning an iterator
168  
169 sub _search_build_and_run {
170 my ( $class, $p ) = @_;
171 my $R = OpenInteract::Request->instance;
172  
173 # Grab the criteria and customize if necessary
174  
175 my $criteria = $class->_search_build_criteria( $p );
176  
177 my ( $tables, $where, $values ) =
178 $class->_search_build_where_clause( $criteria, $p );
179  
180 my ( $limit );
181 if ( $p->{min} or $p->{max} ) {
182 if ( $p->{min} and $p->{max} ) { $limit = "$p->{min},$p->{max}" }
183 elsif ( $p->{max} ) { $limit = $p->{max} }
184 }
185  
186 my $object_class = $class->MY_OBJECT_CLASS;
187  
188 if ( my $num_limit_results = $class->MY_SEARCH_RESULTS_CAP ) {
189 my $row = eval { $object_class->db_select({ select => [ 'count(*)' ],
190 from => $tables,
191 where => $where,
192 value => $values,
193 return => 'single' }) };
194 if ( $row->[0] > $num_limit_results ) {
195 my $msg = "Your search has returned too many results. " .
196 "(Limit: $num_limit_results) Please try again.";
197 return ( undef, $msg );
198 }
199 }
200  
201 $R->DEBUG && $R->scrib( 1, "RUN SEARCH (before): ", scalar localtime );
202 my $order = $class->MY_SEARCH_RESULTS_ORDER;
203 my $additional_params = $class->MY_SEARCH_ADDITIONAL_PARAMETERS || {};
204 my $iter = eval { $object_class->fetch_iterator({
205 from => $tables, where => $where,
206 value => $values, limit => $limit,
207 order => $order,
208 %{ $additional_params } }) };
209 $R->DEBUG && $R->scrib( 1, "RUN SEARCH (after): ", scalar localtime );
210  
211 return ( $iter, undef ) unless ( $@ );
212  
213 $R->scrib( 0, "Search failed: $@\nClass: $class\n",
214 "FROM", join( ',', @{ $tables } ), "\n",
215 "WHERE $where\n",
216 "ORDER BY $order\n",
217 "VALUES", join( ',', @{ $values } ) );
218 die "Search failed ($@)\n";
219 }
220  
221  
222 # Grab the specified fields and values out of the form
223 # submitted. Fields with multiple values are saved as arrayrefs.
224  
225 sub _search_build_criteria {
226 my ( $class, $p ) = @_;
227 my $R = OpenInteract::Request->instance;
228 my $apr = $R->apache;
229 my $object_class = $class->MY_OBJECT_CLASS;
230 my $object_table = $object_class->base_table;
231 my ( %search_params );
232  
233 # Go through each search field and assign a value. If the search
234 # field is a simple one (no table.field), then prepend the object
235 # table to the fieldname
236  
237 foreach my $field ( $class->MY_SEARCH_FIELDS ) {
238 my @value = $apr->param( $field );
239 next unless ( defined $value[0] and $value[0] ne '' );
240 my $full_field = ( $field =~ /\./ )
241 ? $field : "$object_table.$field";
242 $search_params{ $full_field } = ( scalar @value > 1 )
243 ? \@value : $value[0];
244 }
245 $class->_search_criteria_customize( \%search_params, $p );
246 $R->DEBUG && $R->scrib( 1, "($class) Found search parameters:\n",
247 Dumper( \%search_params ) );
248 return \%search_params
249 }
250  
251  
252 # Build a WHERE clause -- parameters with multiple values are 'OR',
253 # everything else is 'AND'. Example:
254 #
255 # ( table.last_name LIKE '%win%' OR table.last_name LIKE '%smi%' )
256 # AND ( table.first_name LIKE '%john%' )
257  
258 sub _search_build_where_clause {
259 my ( $class, $search_criteria, $p ) = @_;
260 my $R = OpenInteract::Request->instance;
261  
262 # Find all our configured information
263  
264 my $object_class = $class->MY_OBJECT_CLASS;
265 my $object_table = $object_class->base_table;
266 my %from_tables = ( $object_table => 1 );
267 my %exact_match = map { $_ => 1 } $class->_fq_fields( $class->MY_SEARCH_FIELDS_EXACT );
268 my %left_exact_match = map { $_ => 1 } $class->_fq_fields( $class->MY_SEARCH_FIELDS_LEFT_EXACT );
269 my %right_exact_match = map { $_ => 1 } $class->_fq_fields( $class->MY_SEARCH_FIELDS_RIGHT_EXACT );
270  
271 # Go through each of the criteria set -- note that each one must
272 # be a fully-qualified (table.field) fieldname or it is discarded.
273  
274 my ( @where, @value ) = ();
275 foreach my $field_name ( keys %{ $search_criteria } ) {
276 $R->DEBUG && $R->scrib( 2, "Testing ($field_name) with ",
277 "($search_criteria->{ $field_name })" );
278 next unless ( defined $search_criteria->{ $field_name } );
279  
280 # Discard non-qualified fieldnames. Note that this regex will
281 # greedily swallow everything to the last '.' to accommodate
282 # systems that use a 'db.table' syntax to refer to a table.
283  
284 my ( $table ) = $field_name =~ /^([\w\.]*)\./;
285 next unless ( $table );
286  
287 # Track the table used
288  
289 $from_tables{ $table }++;
290  
291 # See if we're using one or multiple values
292  
293 my $value_list = ( ref $search_criteria->{ $field_name } )
294 ? $search_criteria->{ $field_name }
295 : [ $search_criteria->{ $field_name } ];
296  
297 # Hold the items for this particular criterion, which will be
298 # join'd with an 'OR'
299  
300 my @where_param = ();
301 foreach my $value ( @{ $value_list } ) {
302  
303 # Value must be defined to be set
304  
305 next unless ( defined $value and $value ne '' );
306  
307 # Default is a LIKE match (see POD)
308  
309 my $oper = ( $exact_match{ $field_name } ) ? '=' : 'LIKE';
310 push @where_param, " $field_name $oper ? ";
311 my ( $search_value );
312 if ( $exact_match{ $field_name } ) {
313 $search_value = $value;
314 }
315 elsif ( $left_exact_match{ $field_name } ) {
316 $search_value = "$value%";
317 }
318 elsif ( $right_exact_match{ $field_name } ) {
319 $search_value = "%$value";
320 }
321 else {
322 $search_value = "%$value%";
323 }
324 push @value, $search_value;
325 $R->DEBUG && $R->scrib( 2, "Set ($field_name) $oper ($search_value)" );
326 }
327 push @where, '( ' . join( ' OR ', @where_param ) . ' )';
328 }
329  
330 # Generate any statements needed to link tables for searching.
331  
332 # DO NOT replace '@tables_used' in the foreach with 'keys
333 # %from_tables' since we may add items to %from_tables during the
334 # loop. Also don't do an 'each %table_links' and then check to see
335 # if the table is in %from_tables for the same reason.
336  
337 my %table_links = $class->MY_SEARCH_TABLE_LINKS;
338 my @tables_used = keys %from_tables;
339 foreach my $link_table ( @tables_used ) {
340 my $id_link = $table_links{ $link_table };
341 next unless ( $id_link );
342  
343 # See POD for what the values in MY_SEARCH_TABLE_LINKS mean
344  
345 if ( ref $id_link eq 'ARRAY' ) {
346 my $num_linking_fields = scalar @{ $id_link };
347 if ( $num_linking_fields == 2 ) {
348 my ( $object_field, $link_field ) = @{ $id_link };
349 $R->DEBUG && $R->scrib( 1, "Linking ($link_table) with my field ",
350 "($object_field) to ($link_field)" );
351 push @where, join( ' = ', "$object_table.$object_field",
352 "$link_table.$link_field" );
353 }
354  
355 # Remember to add the linking table to our FROM list!
356  
357 elsif ( $num_linking_fields == 3 ) {
358 my ( $base_id_field, $middle_table, $link_id_field ) = @{ $id_link };
359 $R->DEBUG && $R->scrib( 1, "Linking to ($link_table) through ",
360 "($middle_table)" );
361 push @where, join( ' = ', "$object_table.$base_id_field",
362 "$middle_table.$base_id_field" );
363 push @where, join( ' = ', "$middle_table.$link_id_field",
364 "$link_table.$link_id_field" );
365 $from_tables{ $middle_table }++;
366 }
367 else {
368 $R->scrib( 0, "Cannot generate a link clause for ",
369 "($link_table) from ($class)" );
370 die "Cannot generate linking clauses for ($link_table) from ",
371 "($class): if value of hash is an array reference it ",
372 "must have either two or three elements.\n";
373 }
374 }
375 else {
376 $R->DEBUG && $R->scrib( 1, "Straight link to ($link_table) with",
377 "($id_link)" );
378 push @where, join( ' = ', "$object_table.$id_link",
379 "$link_table.$id_link" );
380 }
381 }
382  
383 my @tables = keys %from_tables;
384 $class->_search_build_where_customize( \@tables, \@where, \@value, $p );
385  
386 my $clause = join( " AND ", @where );
387 $R->DEBUG && $R->scrib( 1, "($class) Built WHERE clause\n",
388 "FROM:", join( ', ', @tables ), "\n",
389 "WHERE: $clause\n",
390 "VALUES:", join( ', ', @value ) );
391 return ( \@tables, $clause, \@value );
392 }
393  
394  
395 # Take a list of fields and ensure that each one is fully-qualified
396  
397 sub _fq_fields {
398 my ( $class, @fields ) = @_;
399 my $object_class = $class->MY_OBJECT_CLASS;
400 my $object_table = $object_class->base_table;
401 return map { ( /\./ ) ? $_ : "$object_table.$_" } @fields;
402 }
403  
404  
405  
406 ########################################
407 # DISPLAY
408 ########################################
409  
410  
411 sub create {
412 my ( $class, $p ) = @_;
413 my $R = OpenInteract::Request->instance;
414 unless ( $class->MY_ALLOW_CREATE ) {
415 $R->scrib( 0, "User requested create for ($class) and it's not allowed." );
416 return '<h1>Error</h1><p>New objects of this type cannot be created.</p>';
417 }
418 unless ( $p->{level} >= $class->MY_OBJECT_CREATE_SECURITY ) {
419 $R->scrib( 0, "Request for create ($class) denied - inadequate security" );
420 return '<h1>Error</h1><p>You do not have permission to create new objects.</p>';
421 }
422 $p->{edit} = 1;
423 $p->{is_new_object} = 1;
424 return $class->show( $p );
425 }
426  
427  
428 sub show {
429 my ( $class, $p ) = @_;
430 my $R = OpenInteract::Request->instance;
431 unless ( $class->MY_ALLOW_SHOW ) {
432 $R->scrib( 0, "User requested show for ($class) and it's not allowed." );
433 return '<h1>Error</h1><p>Objects of this type cannot be viewed.</p>';
434 }
435 $p ||= {};
436  
437 my %params = %{ $p };
438  
439 # Assumption: Only users with SEC_LEVEL_WRITE can edit. Maybe
440 # create configuration for: object_update_level,
441 # object_create_level so we can have different security levels for
442 # create and modify?
443  
444 $params{do_edit} = ( $p->{edit} or $R->apache->param( 'edit' ) );
445  
446 # Setup our default info
447  
448 my $fail_method = $class->MY_SHOW_FAIL_TASK;
449 my $object_type = $class->MY_OBJECT_TYPE;
450 my $object_class = $class->MY_OBJECT_CLASS;
451 my $id_field = $object_class->id_field;
452 my $object = $object_class->new;
453 unless ( $p->{is_new_object} ) {
454 $object = $p->{ $object_type } ||
455 eval { $class->fetch_object( $p->{ $id_field }, $id_field ) };
456 return $class->$fail_method({ %params, error_msg => $@ }) if ( $@ );
457 }
458  
459 # If this is a saved object, see if we're supposed to ensure it's
460 # active. If the user is an admin, it doesn't matter.
461  
462 my $active_field = $class->MY_ACTIVE_CHECK;
463 if ( ! $R->{auth}{is_admin} and $object->is_saved and $active_field ) {
464 my $status = $object->{ $active_field };
465 unless ( $status =~ /^\s*(y|yes|1)\s*$/i ) {
466 $R->scrib( 0, "Object failed 'active' status check (Status: $status)" );
467 my $error_msg = "This object is currently inactive. Please check later.";
468 return $class->$fail_method({ %params, error_msg => $error_msg });
469 }
470 $R->DEBUG && $R->scrib( 1, "Object passed 'active' status check (Status: $status)" );
471 }
472  
473 # Ensure the object can be edited -- remember, 'fetch_object'
474 # ALWAYS returns an object or dies, so don't add another clause
475 # testing for the existence of $object
476  
477 unless ( $params{do_edit} or $object->is_saved ) {
478 $R->scrib( 0, "User has requested static display on a new object -- bailing." );
479 my $error_msg = 'Sorry, I could not display the object you requested.';
480 return $class->search_form({ error_msg => $error_msg });
481 }
482  
483 # Set both 'object' and the object type equal to the object so the
484 # template can use either.
485  
486 $params{task_security} = $p->{level};
487 $params{object} = $params{ $object_type } = $object;
488 $R->{page}{title} = $class->MY_OBJECT_FORM_TITLE;
489  
490 $class->_show_customize( \%params );
491 my $template_name = $class->_template_name(
492 \%params,
493 $class->MY_OBJECT_FORM_TEMPLATE( \%params ) );
494 return $R->template->handler( {}, \%params, { name => $template_name } );
495 }
496  
497  
498  
499 ########################################
500 # MODIFY
501 ########################################
502  
503 sub edit {
504 my ( $class, $p ) = @_;
505 $p ||= {};
506 my $R = OpenInteract::Request->instance;
507 unless ( $class->MY_ALLOW_EDIT ) {
508 $R->scrib( 0, "User requested edit for ($class) and it's not allowed." );
509 return '<h1>Error</h1><p>Objects of this type cannot be edited.</p>';
510 }
511  
512 $R->{page}{return_url} = $class->MY_EDIT_RETURN_URL;
513  
514 # Setup default info
515  
516 my $fail_method = $class->MY_EDIT_FAIL_TASK;
517 my $object_type = $class->MY_OBJECT_TYPE;
518 my $object_class = $class->MY_OBJECT_CLASS;
519 my $id_field = $object_class->id_field;
520 my $object = eval { $class->fetch_object( $p->{ $id_field }, $id_field ) };
521  
522 # If we cannot fetch the object for editing, there's clearly a bad
523 # error and we should go back to the search form rather than the
524 # display form.
525  
526 if ( $@ ) {
527 return $class->$fail_method({ %{ $p }, error_msg => $@ });
528 }
529  
530 # Assumption: SEC_LEVEL_WRITE is necessary. (Probably ok.)
531  
532 my $is_new = ( ! $object->is_saved );
533 my $object_level = ( $is_new ) ? SEC_LEVEL_WRITE : $object->{tmp_security_level};
534 if ( $object_level < SEC_LEVEL_WRITE ) {
535 my $error_msg = 'Sorry, you do not have access to modify this ' .
536 'object. No modifications made.';
537 return $class->$fail_method({ %{ $p }, error_msg => $error_msg });
538 }
539  
540 $R->scrib( 1, "Object is new object? ", ( $is_new ) ? 'yes' : 'no' );
541  
542 # We pass this to the customization routine so you can do
543 # comparisons, set off triggers based on changes, etc.
544  
545 my $old_data = $object->as_data_only;
546  
547 # Assign values from the form (specified by MY_EDIT_FIELDS,
548 # MY_EDIT_FIELDS_DATE, MY_EDIT_FIELDS_TOGGLED, ...)
549  
550 $class->_edit_assign_fields( $object );
551  
552 # If after customizing/inspecting the object you want to bail and
553 # go somewhere else, return the status 'ERROR' and fill \%opts
554 # with information on what you want to do. (Overriding this is
555 # quite common -- see POD.)
556  
557 my ( $status, $opts ) = $class->_edit_customize( $object, $old_data );
558 if ( $status == ERROR ) {
559 $opts->{object} = $opts->{ $object_type } = $object;
560 return $class->_execute_options( $opts );
561 }
562  
563 my %show_params = ( %{ $p }, $object_type => $object, object => $object );
564 eval { $object->save( $opts ) };
565 if ( $@ ) {
566 my $ei = OpenInteract::Error->set( SPOPS::Error->get );
567 $R->scrib( 0, "Object ($object_type) save failed: $@ ($ei->{system_msg})" );
568 $R->throw({ code => 407 });
569 $show_params{error_msg} = "Object modification failed. Error found: $ei->{system_msg}";
570 return $class->$fail_method( \%show_params );
571 }
572  
573 $class->_edit_post_action_customize( $object, $old_data );
574  
575 $show_params{status_msg} = ( $is_new )
576 ? 'Object created properly.'
577 : 'Object saved properly with changes.';
578 my $method = $class->MY_EDIT_DISPLAY_TASK;
579 return $class->$method( \%show_params );
580 }
581  
582  
583 # Assign values from GET/POST to the object
584  
585 sub _edit_assign_fields {
586 my ( $class, $object ) = @_;
587 my $R = OpenInteract::Request->instance;
588 my $apr = $R->apache;
589 my $object_type = $class->MY_OBJECT_TYPE;
590  
591 # Go through normal fields
592  
593 foreach my $field ( $class->MY_EDIT_FIELDS ) {
594 my $value = $class->_read_field( $apr, $field );
595 $R->DEBUG && $R->scrib( 1, "Object edit: ($object_type) ($field) ($value)" );
596 $object->{ $field } = $value;
597 }
598  
599 # Go through toggled (yes/no) fields
600  
601 foreach my $field ( $class->MY_EDIT_FIELDS_TOGGLED ) {
602 my $value = $class->_read_field_toggled( $apr, $field );
603 $R->DEBUG && $R->scrib( 1, "Object edit toggle: ($object_type) ($field) ($value)" );
604 $object->{ $field } = $value;
605 }
606  
607 # Go through date fields
608  
609 foreach my $field ( $class->MY_EDIT_FIELDS_DATE ) {
610 my $value = $class->_read_field_date( $apr, $field );
611 $R->DEBUG && $R->scrib( 1, "Object edit date: ($object_type) ($field) ($value)" );
612 $object->{ $field } = $value;
613 }
614  
615 # Go through datetime fields
616  
617 foreach my $field ( $class->MY_EDIT_FIELDS_DATETIME ) {
618 my $value = $class->_read_field_datetime( $apr, $field );
619 $R->DEBUG && $R->scrib( 1, "Object edit datetime: ($object_type) ($field) ($value)" );
620 $object->{ $field } = $value;
621 }
622  
623 return ( OK, undef );
624 }
625  
626  
627 ########################################
628 # READ FIELDS
629 ########################################
630  
631 # Just return the value
632  
633 sub _read_field {
634 my ( $class, $apr, $field ) = @_;
635 return $apr->param( $field );
636 }
637  
638  
639 # If any value, return 'yes', otherwise 'no'
640  
641 sub _read_field_toggled {
642 my ( $class, $apr, $field ) = @_;
643 return ( $apr->param( $field ) ) ? 'yes' : 'no';
644 }
645  
646  
647 # Default is to have the year, month and day in three separate fields.
648  
649 sub _read_field_date {
650 my ( $class, $apr, $field ) = @_;
651 my ( $y, $m, $d ) = ( $apr->param( $field . '_year' ),
652 $apr->param( $field . '_month' ),
653 $apr->param( $field . '_day' ) );
654 return undef unless ( $y and $m and $d );
655 return join( '-', $y, $m, $d );
656 }
657  
658  
659 sub _read_field_datetime {
660 my ( $class, $apr, $field ) = @_;
661 my $date = $class->_read_field_date( $apr, $field );
662 return undef unless ( $date );
663 my ( $h, $m, $am_pm ) = ( $apr->param( $field . '_hour' ),
664 $apr->param( $field . '_minute' ),
665 $apr->param( $field . '_am_pm' ) );
666 unless ( $h and $m and $am_pm ) {
667 $h = '12'; $m = '00'; $am_pm = 'AM';
668 }
669 return join( ' ', $date, "$h:$m $am_pm" );
670 }
671  
672  
673 sub _read_field_date_object {
674 my ( $class, $apr, $field ) = @_;
675 my $date = $class->_read_field_date( $apr, $field );
676 return Class::Date->new( $date );
677 }
678  
679  
680 ########################################
681 # REMOVE
682 ########################################
683  
684 sub remove {
685 my ( $class, $p ) = @_;
686 $p ||= {};
687 my $R = OpenInteract::Request->instance;
688 unless ( $class->MY_ALLOW_REMOVE ) {
689 $R->scrib( 0, "User requested remove for ($class) and it's not allowed." );
690 return $class->search_form({
691 error_msg => 'Objects of this type cannot be removed.' });
692 }
693  
694 my $apr = $R->apache;
695  
696 my $fail_method = $class->MY_REMOVE_FAIL_TASK;
697 my $object_type = $class->MY_OBJECT_TYPE;
698 my $object_class = $class->MY_OBJECT_CLASS;
699 my $id_field = $object_class->id_field;
700 my $object = eval { $class->fetch_object( $p->{ $id_field },
701 $id_field ) };
702  
703 if ( $@ ) {
704 return $class->$fail_method({ %{ $p }, error_msg => $@ });
705 }
706 unless ( $object->is_saved ) {
707 my $error_msg = 'Cannot fetch object for removal. No modifications made.';
708 return $class->$fail_method({ %{ $p }, error_msg => $error_msg });
709 }
710  
711  
712 # Assumption: SEC_LEVEL_WRITE is necessary to remove. (Probably ok.)
713  
714 if ( $object->{tmp_security_level} < SEC_LEVEL_WRITE ) {
715 my $error_msg = 'Sorry, you do not have access to remove this ' .
716 'object. No modifications made.';
717 return $class->$fail_method({ %{ $p }, error_msg => $error_msg });
718 }
719  
720 my %show_params = %{ $p };
721  
722 $class->_remove_customize( $object );
723 eval { $object->remove };
724 if ( $@ ) {
725 my $ei = OpenInteract::Error->set( SPOPS::Error->get );
726 $R->scrib( 0, "Cannot remove object ($object_type) ($@) ($ei->{system_msg})" );
727 $R->throw({ code => 405 });
728 $show_params{error_msg} = "Cannot remove object! See error log.";
729 return $class->$fail_method( \%show_params );
730 }
731  
732 $class->_remove_post_action_customize( $object );
733  
734 $show_params{status_msg} = 'Object successfully removed.';
735 my $method = $class->MY_REMOVE_DISPLAY_TASK;
736 return $class->$method( \%show_params );
737 }
738  
739  
740  
741 ########################################
742 # NOTIFY
743 ########################################
744  
745  
746 sub notify {
747 my ( $class, $p ) = @_;
748 my $R = OpenInteract::Request->instance;
749 unless ( $class->MY_ALLOW_NOTIFY ) {
750 $R->scrib( 0, "User requested notify for ($class) and it's not allowed." );
751 return '<h1>Error</h1><p>Objects of this type cannot be sent.';
752 }
753  
754 my $apr = $R->apache;
755 my $object_class = $class->MY_OBJECT_CLASS;
756 my @id_list = $p->{id_list} || $apr->param( $class->MY_NOTIFY_ID_FIELD );
757 my $email = $p->{email} || $apr->param( $class->MY_NOTIFY_EMAIL_FIELD );
758 unless ( $email ) {
759 return '<h2 align="center">Error</h2>' .
760 '<p>Error: Cannot run notification: no email address given.</p>';
761 }
762 unless ( scalar @id_list ) {
763 return '<h2 align="center">Error</h2>' .
764 '<p>Error: Cannot run notification: no objects specified.</p>';
765 }
766  
767 my @object_list = ();
768 foreach my $id ( @id_list ) {
769 my $object = eval { $object_class->fetch( $id ) };
770 push @object_list, $object if ( $object );
771 }
772 my %params = ( from_email => $class->MY_NOTIFY_FROM,
773 email => $email,
774 subject => $class->MY_NOTIFY_SUBJECT,
775 object => \@object_list,
776 notes => $apr->param( $class->MY_NOTIFY_NOTES_FIELD ),
777 type => $class->MY_OBJECT_TYPE );
778 $class->_notify_customize( \%params );
779 if ( OpenInteract::SPOPS->notify( \%params ) ) {
780 return '<h2 align="center">Success!</h2>' .
781 '<p>Notification sent properly!</p>';
782 }
783 return '<h2 align="center">Error</h2>' .
784 '<p>Error sending email. Please check error logs!</p>';
785 }
786  
787  
788 ########################################
789 # WIZARD
790 ########################################
791  
792 # Wizard stuff is pretty simple -- a lot of the difficult stuff is done
793 # via javascript.
794  
795  
796 # Start the wizard (simple search form, usually)
797  
798 sub wizard {
799 my ( $class, $p ) = @_;
800 my $R = OpenInteract::Request->instance;
801 unless ( $class->MY_ALLOW_WIZARD ) {
802 $R->scrib( 0, "User requested wizard for ($class) and it's not allowed." );
803 return '<h1>Error</h1><p>The wizard is not enabled for these objects.</p>';
804 }
805 $p ||= {};
806  
807 my %params = %{ $p };
808  
809 $R->{page}{title} = $class->MY_WIZARD_FORM_TITLE;
810 $R->{page}{_simple_}++;
811  
812 $class->_wizard_form_customize( \%params );
813 my $template_name = $class->_template_name(
814 \%params,
815 $class->MY_WIZARD_FORM_TEMPLATE( \%params ) );
816 return $R->template->handler( {}, \%params, { name => $template_name } );
817 }
818  
819  
820 # Run the search and present results; note that we truncate the
821 # iterator results with a max of 50, so we don't have any issues with
822 # paged results or with the user typing 'a' for a last name and
823 # getting back 100000 items...
824  
825 sub wizard_search {
826 my ( $class, $p ) = @_;
827 my $R = OpenInteract::Request->instance;
828 unless ( $class->MY_ALLOW_WIZARD ) {
829 $R->scrib( 0, "User requested wizard search for ($class) and it's not allowed." );
830 return '<h1>Error</h1><p>The wizard is not enabled for these objects.</p>';
831 }
832 $p ||= {};
833  
834 my %params = %{ $p };
835 ( $params{iterator}, $params{msg} ) =
836 $class->_search_build_and_run({ max => $class->MY_WIZARD_RESULTS_MAX });
837  
838 $R->{page}{title} = $class->MY_WIZARD_RESULTS_TITLE;
839 $R->{page}{_simple_}++;
840  
841 $class->_wizard_search_customize( \%params );
842 my $template_name = $class->_template_name(
843 \%params,
844 $class->MY_WIZARD_RESULTS_TEMPLATE( \%params ) );
845 return $R->template->handler( {}, \%params, { name => $template_name } );
846 }
847  
848  
849  
850 ########################################
851 # TASK FLOW MANIPULATION
852 ########################################
853  
854 # Find relevant information in \%opts to execute. Potential information:
855 # - class, method --> what to execute; if 'method' specified but not
856 # 'class', we use our own class
857 # - action --> Lookup the action and pass in $opts
858 # - error_msg: error message to pass around
859 # - status_msg: status message to pass around
860 # ... Whatever else is passed along
861  
862 # Currently only used in edit()
863  
864 sub _execute_options {
865 my ( $class, $opts ) = @_;
866 my $R = OpenInteract::Request->instance;
867 if ( my $method = $opts->{method} ) {
868 my $execute_class = $opts->{class} || $class;
869 $R->DEBUG && $R->scrib( 1, "Executing ($execute_class) ($method) after bail." );
870 return $execute_class->$method( $opts );
871 }
872  
873 if ( $opts->{action} ) {
874 my ( $execute_class, $method ) = $R->lookup_action( $opts->{action} );
875 if ( $execute_class and $method ) {
876 $R->DEBUG && $R->scrib( 1, "Executing ($execute_class) ($method) ",
877 "from ($opts->{action} after bail." );
878 return $execute_class->$method( $opts );
879 }
880 }
881 return "Cannot find next execute operation.";
882 }
883  
884  
885  
886 ########################################
887 # GENERIC OBJECT FETCH
888 ########################################
889  
890 # ALWAYS RETURNS OBJECT OR DIES
891  
892 # Retrieve a record: if no $id then return a new one; if $id throw a
893 # error and die if we cannot fetch; if object with $id not found,
894 # return a new one. You can always tell if the returned object is new
895 # by the '->is_saved()' flag (false if new, true if existing)
896  
897 sub fetch_object {
898 my ( $class, $id, @id_field_list ) = @_;
899 my $R = OpenInteract::Request->instance;
900  
901 unless ( $id ) {
902 my $apr = $R->apache;
903 foreach my $id_field ( @id_field_list ) {
904 $id = $apr->param( $id_field );
905 last if ( $id );
906 }
907 }
908  
909 my $object_class = $class->MY_OBJECT_CLASS;
910  
911 return $object_class->new unless ( $id );
912  
913 my $object = eval { $object_class->fetch( $id ) };
914 unless ( $@ ) {
915 $object ||= $object_class->new;
916 $class->_fetch_object_customize( $object );
917 return $object;
918 }
919  
920 my $ei = OpenInteract::Error->set( SPOPS::Error->get );
921 my $error_msg = undef;
922 if ( $ei->{type} eq 'security' ) {
923 $error_msg = "Permission denied: you do not have access to view " .
924 "the requested object. ";
925 }
926 else {
927 $R->throw({ code => 404 });
928 $error_msg = "Error encountered trying to retrieve object. The " .
929 "error has been logged. "
930 }
931 die "$error_msg\n";
932 }
933  
934  
935 ########################################
936 # OTHER
937 ########################################
938  
939 # Common template name specification
940  
941 sub _template_name {
942 my ( $class, $p, $default_name ) = @_;
943 return $p->{template_name} if ( $p->{template_name} );
944 my $package = $class->MY_PACKAGE;
945 my $template = $default_name;
946 return join( '::', $package, $template );
947 }
948  
949  
950 ########################################
951 # MANDATORY CONFIGURATION
952 ########################################
953  
954 sub MY_PACKAGE {
955 die "Please define class method MY_PACKAGE() in $_[0]\n";
956 }
957 sub MY_OBJECT_TYPE {
958 die "Please define class method MY_OBJECT_TYPE() in $_[0]\n";
959 }
960  
961  
962 ########################################
963 # DEFAULT CONFIGURATION
964 ########################################
965  
966 sub MY_HANDLER_PATH { return '/' . $_[0]->MY_OBJECT_TYPE }
967 sub MY_OBJECT_CLASS {
968 my $object_type = $_[0]->MY_OBJECT_TYPE;
969 return OpenInteract::Request->instance->$object_type();
970 }
971  
972 sub MY_ALLOW_SEARCH_FORM { return 1 }
973 sub MY_SEARCH_FORM_TITLE { return 'Search Form' }
974 sub MY_SEARCH_FORM_TEMPLATE { return 'search_form' }
975  
976 sub MY_ALLOW_SEARCH { return 1 }
977 sub MY_SEARCH_FIELDS { return () }
978 sub MY_SEARCH_FIELDS_EXACT { return () }
979 sub MY_SEARCH_FIELDS_LEFT_EXACT { return () }
980 sub MY_SEARCH_FIELDS_RIGHT_EXACT { return () }
981 sub MY_SEARCH_TABLE_LINKS { return () }
982 sub MY_SEARCH_ADDITIONAL_PARAMETERS { return {} }
983 sub MY_SEARCH_FAIL_TASK { return 'search_form' }
984 sub MY_SEARCH_RESULTS_CAP { return 0 }
985 sub MY_SEARCH_RESULTS_CAP_FAIL_TASK { return 'search_form' }
986 sub MY_SEARCH_RESULTS_ORDER { return undef }
987 sub MY_SEARCH_RESULTS_PAGED { return undef }
988 sub MY_SEARCH_RESULTS_KEY { return $_[0]->MY_OBJECT_TYPE . '_search_id' }
989 sub MY_SEARCH_RESULTS_PAGE_SIZE { return 50 }
990 sub MY_SEARCH_RESULTS_PAGE_FIELD { return 'pagenum' }
991 sub MY_SEARCH_RESULTS_TITLE { return 'Search Results' }
992 sub MY_SEARCH_RESULTS_TEMPLATE { return 'search_results' }
993  
994 sub MY_ALLOW_SHOW { return 1 }
995 sub MY_SHOW_FAIL_TASK { return 'search_form' }
996 sub MY_ACTIVE_CHECK { return undef }
997 sub MY_OBJECT_FORM_TITLE { return 'Object Detail' }
998 sub MY_OBJECT_FORM_TEMPLATE { return 'object_form' }
999  
1000 sub MY_ALLOW_CREATE { return undef }
1001 sub MY_OBJECT_CREATE_SECURITY { return SEC_LEVEL_WRITE }
1002  
1003 sub MY_ALLOW_EDIT { return undef }
1004 sub MY_EDIT_RETURN_URL { return $_[0]->MY_HANDLER_PATH . '/' }
1005 sub MY_EDIT_FIELDS { return () }
1006 sub MY_EDIT_FIELDS_TOGGLED { return () }
1007 sub MY_EDIT_FIELDS_DATE { return () }
1008 sub MY_EDIT_FIELDS_DATETIME { return () }
1009 sub MY_EDIT_FAIL_TASK { return 'search_form' }
1010 sub MY_EDIT_DISPLAY_TASK { return 'show' }
1011  
1012 sub MY_ALLOW_REMOVE { return undef }
1013 sub MY_REMOVE_FAIL_TASK { return 'search_form' }
1014 sub MY_REMOVE_DISPLAY_TASK { return 'search_form' }
1015  
1016 sub MY_ALLOW_NOTIFY { return undef }
1017 sub MY_NOTIFY_FROM { return undef }
1018 sub MY_NOTIFY_SUBJECT { return '' }
1019 sub MY_NOTIFY_ID_FIELD { my $oc = $_[0]->MY_OBJECT_CLASS; return $oc->id_field }
1020 sub MY_NOTIFY_EMAIL_FIELD { return 'email' }
1021 sub MY_NOTIFY_NOTES_FIELD { return 'notes' }
1022  
1023 sub MY_ALLOW_WIZARD { return undef }
1024 sub MY_WIZARD_FORM_TITLE { return 'Wizard: Search' }
1025 sub MY_WIZARD_FORM_TEMPLATE { return 'wizard_form' }
1026 sub MY_WIZARD_RESULTS_MAX { return 50 }
1027 sub MY_WIZARD_RESULTS_TITLE { return 'Wizard: Results' }
1028 sub MY_WIZARD_RESULTS_TEMPLATE { return 'wizard_results' }
1029  
1030  
1031 ########################################
1032 # CUSTOMIZATION INTERFACE
1033 ########################################
1034  
1035 # Template/param modifications
1036 sub _search_form_customize { return 1 }
1037 sub _search_customize { return 1 }
1038 sub _show_customize { return 1 }
1039 sub _notify_customize { return 1 }
1040 sub _wizard_form_customize { return 1 }
1041 sub _wizard_search_customize { return 1 }
1042  
1043 # Criteria/Object modifications
1044 sub _search_criteria_customize { return $_[1] }
1045 sub _search_build_where_customize { return 1 }
1046 sub _fetch_object_customize { return $_[1] }
1047 sub _edit_customize { return ( OK, undef ) }
1048 sub _edit_post_action_customize { return 1 }
1049 sub _remove_customize { return 1 }
1050 sub _remove_post_action_customize { return 1 }
1051  
1052  
1053 1;
1054  
1055 __END__