<%doc> Special edit component intended to edit scope templates. <%attr> title => 'Template Edit' section => 'Management' <%args> $id => undef $scope_use_obj => undef $user => $ui->get_current_user($r) $submit => undef $cancel => undef $showheader => 1 $select_id => undef $selectall_ids => undef; $selected => undef $dowindow => undef <%init> if(!$id){ return; #ugly hack so that when autohandler is calling all the pages in procession, this page dosnt get loaded #after we hit the [back] link } my @select_all; my $new_select_ids; my @delete_array; if($selectall_ids){ @select_all = split(" ", $selectall_ids); } my $DEBUG = 0; my $scope_obj; my $manager = $ui->get_permission_manager($r); my $page = $ui->table_view_page("DhcpScopeUse"); if($DEBUG){ foreach my $i (keys %ARGS){ print "$i $ARGS{$i}
"; } } $scope_obj = DhcpScope->retrieve($id); unless ( $manager && $manager->can($user, "edit", $scope_obj) ){ $m->comp('/generic/error.mhtml', error=>"You don't have permission to edit this object"); } if($submit){ if ($submit eq "Update"){ #there is a prexisting entry for this my %update_args; for my $t (@select_all){ my $temp_key = "$t".'template_select'; chomp($temp_key); #why this ugly hack? I tried building the hash key with $temp_key and using it directly to #access the information I wanted, but it NEVER WOULD... They look exactly the same when you output the info #but there must be some crucial difference since $ARGS{$temp_key} would always be null, so this is an ugly #work around :/ foreach my $i (keys %ARGS){ if($i == $temp_key){ $update_args{$i} = $ARGS{$i}; } } } eval{ for my $t (keys %update_args){ my $o = DhcpScopeUse->retrieve($t); $o->update({template=> $update_args{$t}}); } }; if ( my $e = $@ ){ $m->comp('/generic/error.mhtml', error=>$e); } #lets remove all the items we're supposed to delete #$ARGS{"delete"} can either contain a string with the DhcpScopeUse object id #we want to delete (if the user has selected a single item to delete) #or it can be an arrayref full of them. We try to use it #as an arrayref, but if this fails we treat it like a string ; eval{ @delete_array = @{$ARGS{"delete"}}; }; if($@){ push(@delete_array, $ARGS{"delete"}); } foreach my $d (@delete_array){ my $o = DhcpScopeUse->retrieve($d); if($o){ $o->delete(); #remove element from select_all array for(my $i; $i < scalar @select_all; $i++){ if($select_all[$i] == $d){ splice(@select_all, $i, 1); last; } } } } foreach (@select_all){ $new_select_ids.= "$_ "; } } else{ #if this wasn't set, we must have been inserting. my $new; eval{ $new = DhcpScopeUse->insert({scope=>$scope_obj, template=>$ARGS{"template_select"}}); }; if ( my $e = $@ ){ $m->comp('error.mhtml', error=>$e); } } } else{ $new_select_ids = $selectall_ids; } %if($submit){
Results
% if(@delete_array && !@select_all){ Values updated sucessfully, all templates removed % } % elsif(@select_all){ Values updated successfully,
Template(s) for <% $scope_obj->name %>: % } % else{ DHCP template added sucessfully:
% if(my $name = DhcpScope->retrieve($ARGS{"template_select"})){ % } % }
%}
Template Edit
[back]
% my %scope_name_hash = $ui->form_field(object=>$scope_obj, column=>"name", edit=>0, returnAsVar=>1); % my $template_num = DhcpScopeType->search(name=>"template")->first; % if(! $template_num){ % $m->comp('/generic/error.mhtml', error=>"Could not retrieve templates"); % } % my @template_objs = DhcpScope->search(type=> $template_num); % @template_objs = sort {$a->name cmp $b->name} @template_objs;
% print "$scope_name_hash{'label'} $scope_name_hash{'value'} "; % if(@select_all){ #if we need to make multiple pull down boxes: % foreach my $o (@select_all){ % #we need this to provide the current template as the default % my $cur_template = DhcpScopeUse->retrieve($o)->template; % }
Templates
delete
% } % else{ #we're inserting a new template...
Template:
% }