# ----------------------------------------------------------------------------- # tpb12 / admin / data_management.pl # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # date description # ---------- ----------------------------------------------------------------- # 11/12/2010 include $user in data sent to add_users.tt so that the javascript # can check if user is deleting themself # 09/28/2011 do_list_plans : return all if no search text; fix so matching # doesn't give errors for uninitialized variables; add deleted plans option # 06/22/2012 stop using ITO::Config # 10/03/2012 changes to dummy data # 01/08/2013 fix search for plans to work with date_created (change search to search_where) # 02/18/2013 replace $r->no_cache with set_cache_control (q.v. for details) # 03/11/2013 use standard libraries; add sysadmin validation for some functions; # add management for role, admin_role, directory_to_role # 09/05/2014 rename libraries to TPB12; rename program directory to tpb12 # ----------------------------------------------------------------------------- use strict; use warnings; use Apache2::Const -compile => qw(OK); use Apache2::Request; use Apache2::RequestIO; use Apache2::RequestUtil; use Date::Manip qw( UnixDate ); use Readonly; use ITO::Utility::FieldValidation; use ITO::Utility::LogError; use ITO::Utility::Utility; use ITO::TPB12::Utility::Utility2; use ITO::TPB12::AdminRole; use ITO::TPB12::Config; use ITO::TPB12::DirectoryToRole; use ITO::TPB12::Purpose; use ITO::TPB12::Role; use ITO::TPB12::RoleToUser; use ITO::TPB12::Section; use ITO::TPB12::TechPlan; use ITO::TPB::User; # ----------------------------------------------------------------------------- my $r = Apache2::RequestUtil->request; set_cache_control($r); my $req = Apache2::Request->new($r); $r->content_type('text/html; charset=utf-8'); Readonly my $TEMPLATE_PATH => getTemplatePaths($r); # nonstandard - normally, an /admin program doesn't use $admin, it is used # in tpb12 to restrict some admin functions to system administrators my ($error, $user, $admin) = userValidationTPB( $r, $TEMPLATE_PATH, 'tpb12 admin data_management'); return $error if (defined $error); { no strict 'refs'; my $run_mode = $req->param('run_mode') || 'list_configs'; my $subroutine = '_' . $run_mode; &{$subroutine}( $r, $req, $TEMPLATE_PATH, $user, $admin ); } # ----------------------------------------------------------------------------- # # request handlers # # ----------------------------------------------------------------------------- # # TPB Administrator functions # # ----------------------------------------------------------------------------- sub _list_plans { my ( $r, $req, $TEMPLATE_PATH, $user, $admin ) = @_; my %record = (); $user->template_configure( -template_options => { INCLUDE_PATH => $TEMPLATE_PATH, } ); $user->template_define( 'list', 'list_plans.tt' ); @{ $record{'purposes'} } = ITO::TPB12::Purpose->search_where( { 'status' => 'Active', }, { 'order_by' => 'seq' } ); $r->print( $user->template_render( 'list', %record ) ); return Apache2::Const::OK; } # ----------------------------------------------------------------------------- sub _do_list_plans { my ( $r, $req, $TEMPLATE_PATH, $user, $admin ) = @_; my %record = (); my $tech_plan = 'ITO::TPB12::TechPlan'; my $filtered = 0; my %where; my $search_type = $req->param('search_type'); if ($search_type) { ++ $filtered; $where{'plan_type'} = $search_type; $record{'search_type'} = $search_type; } my $search_purpose = $req->param('search_purpose'); if ($search_purpose) { ++ $filtered; $where{'purpose'} = $search_purpose; $record{'search_purpose'} = $search_purpose; $record{'search_purpose_text'} = ITO::TPB12::Purpose->retrieve($search_purpose)->name; } my $search_year = $req->param('search_year'); if ($search_year) { ++ $filtered; $where{'section_1_beginning'} = $search_year; $record{'search_year'} = $search_year; } my $search_created = $req->param('search_created'); if ($search_created) { ++ $filtered; $where{'date_created'} = { '-like' => $search_created . '%'}; $record{'search_created'} = $search_created; } my $search_status = $req->param('search_status'); if ($search_status) { ++ $filtered; $record{'search_status'} = $search_status; if ($search_status ne 'All') { $search_status = 'Initiated' if ($search_status eq 'Not Started'); $where{'status'} = $search_status; } } my $search_text = trim($req->param('search_text')); if ($search_text) { ++ $filtered; $record{'search_text'} = $search_text; } $record{'filtered'} = $filtered; my @tech_plans; if (%where) { @tech_plans = $tech_plan->search_where(\%where); } else { @tech_plans = $tech_plan->retrieve_all(); } @{ $record{'tech_plans'} } = (); if ($search_text) { foreach my $plan (@tech_plans) { if ( (defined $plan->title && $plan->title =~ /$search_text/i) || (defined $plan->district && defined $plan->district->name && $plan->district->name =~ /$search_text/i) || (defined $plan->school && defined $plan->school->name && $plan->school->name =~ /$search_text/i) || (defined $plan->county && defined $plan->county->name && $plan->county->name =~ /$search_text/i) ) { push( @{ $record{'tech_plans'} }, $plan); } } } else { @{ $record{'tech_plans'} } = @tech_plans; } my %tpb_hash = (); @{ $record{'tech_plans'} } = sort { tech_plan_compare(\%tpb_hash, $a, $b) } @{ $record{'tech_plans'} }; @{ $record{'purposes'} } = ITO::TPB12::Purpose->search_where( { 'status' => 'Active', }, { 'order_by' => 'seq' } ); $user->template_configure( -template_options => { INCLUDE_PATH => $TEMPLATE_PATH, } ); $user->template_define( 'list', 'list_plans.tt' ); $r->print( $user->template_render( 'list', %record ) ); return Apache2::Const::OK; } # ----------------------------------------------------------------------------- # # Administrator functions # # ----------------------------------------------------------------------------- sub _list_configs { my ( $r, $req, $TEMPLATE_PATH, $user, $admin ) = @_; if (! $admin ) { return errorWithAjax($r, $TEMPLATE_PATH, "Authorization failed - not Administrator (tpb12 admin data_management list_configs)", "Authorization failed.", ); } my %record = (); my $config = 'ITO::TPB12::Config'; @{ $record{'configs'} } = $config->retrieve_all_sorted_by('name'); $config->template_configure( -template_options => { INCLUDE_PATH => $TEMPLATE_PATH, } ); $config->template_define( 'list', 'config_list.tt' ); $r->print( $config->template_render( 'list', %record ) ); return Apache2::Const::OK; } # ----------------------------------------------------------------------------- sub _get_rendered_template { my ( $r, $req, $TEMPLATE_PATH, $user, $admin ) = @_; if (! $admin ) { return errorWithAjax($r, $TEMPLATE_PATH, "Authorization failed - not Administrator (tpb12 admin data_management get_rendered_template)", "Authorization failed.", ); } my %record = (); my $config = 'ITO::TPB12::Config'; my %dummy_data = ( 'user' => $user, 'creator_user' => $user, 'first_name' => $user->first_name, 'last_name' => $user->last_name, 'creator' => $user, 'last_update' => $user, 'original_email' => '(original email)', 'new_email' => '(new email)', ); $dummy_data{'user'}{'temp_password'} = '(temporary password)'; my @tpu = ITO::TPB12::TechPlanToUser->search( 'user' => $user->id ); if ( @tpu ) { $dummy_data{'tech_plan'} = $tpu[0]->tech_plan; } my @roles = sort { $a->role->role_priority <=> $b->role->role_priority } $user->roles; $dummy_data{'login_url'} = $roles[0]->role->login_url; my $text = fix_textarea($req->param('text'), 1); #warn("content = '" . $text . "'"); ITO::TPB12::Config->template_define('temp' => \$text); my $rendered_text = ITO::TPB12::Config->template_render('temp', %dummy_data); #warn("rendered_text = '" . $rendered_text . "'"); $r->print( $rendered_text ); return Apache2::Const::OK; } # ----------------------------------------------------------------------------- sub _edit_sections { my ( $r, $req, $TEMPLATE_PATH, $user, $admin ) = @_; if (! $admin ) { return errorWithAjax($r, $TEMPLATE_PATH, "Authorization failed - not Administrator (tpb12 admin data_management edit_sections)", "Authorization failed.", ); } my %record = (); my $section = 'ITO::TPB12::Section'; @{ $record{'sections'} } = $section->retrieve_all_sorted_by('seq'); my $tpb = 'ITO::TPB12::TechPlan'; my @columns = $tpb->columns( 'All' ); my %sections; foreach my $column (@columns) { next unless ($column =~ /^section_.*_status/); ++$sections{$column}; } @{ $record{'columns'} } = sort keys %sections; $section->template_configure( -template_options => { INCLUDE_PATH => $TEMPLATE_PATH } ); $section->template_define( 'template', 'sections_list.tt' ); $r->print( $section->template_render( 'template', %record ) ); return Apache2::Const::OK; } # ----------------------------------------------------------------------------- sub _edit_budget_categories { my ( $r, $req, $TEMPLATE_PATH, $user, $admin ) = @_; if (! $admin ) { return errorWithAjax($r, $TEMPLATE_PATH, "Authorization failed - not Administrator (tpb12 admin data_management edit_budget_categories)", "Authorization failed.", ); } my %record = (); my $budget_category = 'ITO::TPB12::BudgetCategory'; @{ $record{'budget_categories'} } = $budget_category->retrieve_all_sorted_by('seq'); $budget_category->template_configure( -template_options => { INCLUDE_PATH => $TEMPLATE_PATH } ); $budget_category->template_define( 'template', 'budget_categories.tt' ); $r->print( $budget_category->template_render( 'template', %record ) ); return Apache2::Const::OK; } # ----------------------------------------------------------------------------- sub _edit_purposes { my ( $r, $req, $TEMPLATE_PATH, $user, $admin ) = @_; if (! $admin ) { return errorWithAjax($r, $TEMPLATE_PATH, "Authorization failed - not Administrator (tpb12 admin data_management edit_purposes)", "Authorization failed.", ); } my %record = (); my $purpose = 'ITO::TPB12::Purpose'; @{ $record{'purposes'} } = $purpose->retrieve_all_sorted_by('seq'); $purpose->template_configure( -template_options => { INCLUDE_PATH => $TEMPLATE_PATH, } ); $purpose->template_define( 'list', 'purpose_list.tt' ); $r->print( $purpose->template_render( 'list', %record ) ); return Apache2::Const::OK; } # ----------------------------------------------------------------------------- sub _admin_roles { my ( $r, $req, $TEMPLATE_PATH, $user, $admin ) = @_; if (! $admin ) { return errorWithAjax($r, $TEMPLATE_PATH, "Authorization failed - not Administrator (tpb12 admin data_management admin_roles)", "Authorization failed.", ); } my %record = (); my $admin_role = 'ITO::TPB12::AdminRole'; @{ $record{'admin_roles'} } = sort { lc $a->role->name cmp lc $b->role->name || lc $a->administrator_role->name cmp lc $b->administrator_role->name } $admin_role->retrieve_all(); @{ $record{'roles'} } = ITO::TPB12::Role->retrieve_all_sorted_by('name'); $admin_role->template_configure( -template_options => { INCLUDE_PATH => $TEMPLATE_PATH, } ); $admin_role->template_define( 'list', 'admin_roles.tt' ); $r->print( $admin_role->template_render( 'list', %record ) ); return Apache2::Const::OK; } # ----------------------------------------------------------------------------- sub _directory_roles { my ( $r, $req, $TEMPLATE_PATH, $user, $admin ) = @_; if (! $admin ) { return errorWithAjax($r, $TEMPLATE_PATH, "Authorization failed - not Administrator (tpb12 admin data_management directory_roles)", "Authorization failed.", ); } my %record = (); my $roles = 'ITO::TPB12::DirectoryToRole'; @{ $record{'directory_roles'} } = $roles->retrieve_all_sorted_by('directory'); @{ $record{'roles'} } = ITO::TPB12::Role->retrieve_all_sorted_by('name'); $roles->template_configure( -template_options => { INCLUDE_PATH => $TEMPLATE_PATH, } ); $roles->template_define( 'list', 'directory_roles.tt' ); $r->print( $roles->template_render( 'list', %record ) ); return Apache2::Const::OK; } # ----------------------------------------------------------------------------- sub _roles { my ( $r, $req, $TEMPLATE_PATH, $user, $admin ) = @_; if (! $admin ) { return errorWithAjax($r, $TEMPLATE_PATH, "Authorization failed - not Administrator (tpb12 admin data_management roles)", "Authorization failed.", ); } my %record = (); my $role = 'ITO::TPB12::Role'; @{ $record{'roles'} } = $role->retrieve_all_sorted_by('name'); $role->template_configure( -template_options => { INCLUDE_PATH => $TEMPLATE_PATH, } ); $role->template_define( 'list', 'roles.tt' ); $r->print( $role->template_render( 'list', %record ) ); return Apache2::Const::OK; } # ----------------------------------------------------------------------------- sub _list_user_with_role { my ( $r, $req, $TEMPLATE_PATH, $user, $admin ) = @_; if (! $admin ) { return errorWithAjax($r, $TEMPLATE_PATH, "Authorization failed - not Administrator (tpb12 admin data_management list_user_with_role)", "Authorization failed.", ); } my %record = (); my $role = 'ITO::TPB12::Role'; (my $error, $role) = parameter_checks( $r, $req, $TEMPLATE_PATH, undef, $user, 'tpb12 admin data_management list_user_with_role', 0, $role, {$role => 'id'} ); return $error if (defined $error); my @rtu = ITO::TPB12::RoleToUser->search_where('role' => $role->id); my @users = map {$_->user} @rtu; @{ $record{'users'} } = sort { lc $a->last_name cmp lc $b->last_name || lc $a->first_name cmp lc $b->first_name } @users; $role->template_configure( -template_options => { INCLUDE_PATH => $TEMPLATE_PATH, } ); $role->template_define( 'list', 'user_role_list.tt' ); $r->print( $role->template_render( 'list', %record ) ); return Apache2::Const::OK; } # ----------------------------------------------------------------------------- # # utility subroutines # # ----------------------------------------------------------------------------- sub tech_plan_compare { my ($hash, $tech_plan_1, $tech_plan_2) = @_; my $id_1 = $tech_plan_1->id; my $id_2 = $tech_plan_2->id; if (! defined $$hash{$id_1}) { $$hash{$id_1}{district} = $tech_plan_1->district ? $tech_plan_1->district->name : ''; $$hash{$id_1}{school} = $tech_plan_1->school ? $tech_plan_1->school->name : ''; $$hash{$id_1}{title} = lc $tech_plan_1->title; } if (! defined $$hash{$id_2}) { $$hash{$id_2}{district} = $tech_plan_2->district ? $tech_plan_2->district->name : ''; $$hash{$id_2}{school} = $tech_plan_2->school ? $tech_plan_2->school->name : ''; $$hash{$id_2}{title} = lc $tech_plan_2->title; } return ( $$hash{$id_1}{district} cmp $$hash{$id_2}{district} || $$hash{$id_1}{school} cmp $$hash{$id_2}{school} || $$hash{$id_1}{title} cmp $$hash{$id_2}{title} ); } # -----------------------------------------------------------------------------