1 <& /Admin/Elements/Header,
5 <& /Elements/ListActions, actions => \@results &>
7 <form method="post" enctype="multipart/form-data" action="ImportValues.html" name="ImportCFValues" id="ImportCFValues">
8 <input type="hidden" class="hidden" name="id" value="<% $id %>" />
10 % if ( $ARGS{Import} && $values ) {
11 % if ( $ARGS{ReplaceValues} ) {
12 <input type="hidden" class="hidden" name="ReplaceValues" value="1" />
13 <h2><&|/l&>The following values will replace existing ones:</&></h2>
15 <h2><&|/l&>The following values will be added to existing ones:</&></h2>
17 <table border="0" cellpadding="1" cellspacing="0" class="collection-as-table cf-csv-import">
18 <tr class="collection-as-table">
19 <th class="collection-as-table"><&|/l&>SortOrder</&></th>
20 <th class="collection-as-table"><&|/l&>Name</&></th>
21 <th class="collection-as-table"><&|/l&>Description</&></th>
22 <th class="collection-as-table"><&|/l&>Category</&></th>
23 <th class="collection-as-table"><&|/l&>Import</&></th>
26 % foreach my $value (@$values) {
27 <tr class="collection-as-table <% $i++ %2 ? 'oddline' : 'evenline'%>">
28 <td class="collection-as-table"><input name="row-<% $i %>-SortOrder" value="<% $value->{SortOrder} || '' %>" /></td>
29 <td class="collection-as-table"><input name="row-<% $i %>-Name" value="<% $value->{Name} || '' %>" /></td>
30 <td class="collection-as-table"><input name="row-<% $i %>-Description" value="<% $value->{Description} || '' %>" /></td>
31 <td class="collection-as-table"><input name="row-<% $i %>-Category" value="<% $value->{Category} || '' %>" /></td>
32 <td class="collection-as-table"><input type="checkbox" class="checkbox" name="row-<% $i %>-import" value="1" checked="checked" /></td>
37 <div class="button-cancel">
38 <& /Elements/Submit, Name => 'Cancel', Label => loc('Cancel'), Class => "extra-buttons" &>
40 <& /Elements/Submit, Name => 'Confirm', Label => loc('Confirm') &>
42 <h2><&|/l&>Please select a file with needed values using following format:</&></h2>
44 <&|/l&>- File encoding: UTF-8</&><br/>
45 <&|/l&>- Fields separated by ";"</&><br/>
46 <&|/l&>- No headers</&><br/>
47 <&|/l&>- Using the following columns and order: SortOrder, Name, Description, Category</&><br/>
48 <&|/l&>- Column "Name" is mandatory, other columns may be empty but must exists</&>
52 <tr><td class="label"><&|/l&>CSV file</&></td><td>
53 <input type="file" name="<% $name %>" class="CF-<%$CustomField->id%>-Import" />
55 <tr><td class="label"><&|/l&>Replace?</&>
56 <div class="hints"><&|/l&>Default is to add CSV values to existing ones</&></div>
58 <input type="checkbox" class="checkbox" id="cfvalues-replace" name="ReplaceValues" value="1" <% $ReplaceValuesChecked |n %> />
65 <div class="button-cancel">
66 <& /Elements/Submit, Name => 'Cancel', Label => loc('Cancel') &>
68 <& /Elements/Submit, Name => 'Import', Label => loc('Import') &>
73 my ($title, @results, $values);
74 my $CustomField = RT::CustomField->new( $session{'CurrentUser'} );
75 unless ( $CustomField->Load( $id ) ) {
76 Abort("CustomField not found");
78 $title = loc( 'Importing values for CustomField [_1]', $CustomField->Name );
79 my $name = 'Import-'. $CustomField->Id . '-Values';
81 my $ReplaceValuesChecked = ( $ARGS{ReplaceValues} ) ? qq[checked="checked"] : '';
83 if ( $ARGS{Cancel} ) {
84 $ReplaceValuesChecked = '';
86 } elsif ( $ARGS{Import} && $ARGS{$name} ) {
87 my $cgi_object = $m->cgi_object;
88 my $filename = $cgi_object->param($name);
89 push @results, "Importing ". $filename;
90 my $fh = $cgi_object->upload($name);
95 my $csv = Text::CSV->new ({ strict => 1, sep_char => ";", });
96 $values = $csv->csv (in => $fh, headers => [qw(SortOrder Name Description Category)], encoding => 'UTF-8', );
97 unless ( $values && ref($values) eq 'ARRAY' ) {
98 push @results, loc("[_1]: Wrong file format", $filename);
102 foreach my $value (@$values) {
105 foreach my $field (qw(SortOrder Name Description Category)) {
106 $value->{$field} =~ s/^\s+// if ( $value->{$field} );
107 $value->{$field} =~ s/\s+$// if ( $value->{$field} );
109 if ( ! $value->{Name} ) {
110 push @results, loc("Line [_1]: missing \"[_2]\"", $i, "Name");
113 if ( $value->{SortOrder} && $value->{SortOrder} !~ m/^\d+$/ ) {
114 push @results, loc("Line [_1]: \"[_2]\" must be numerical", $i, loc("SortOrder"));
117 # FIXME: check Category against BasedOn object?
119 push @$valid_values, $value;
122 $values = $valid_values;
125 push @results, loc("Unable to read file");
127 } elsif ( $ARGS{Confirm} ) {
128 # Prepare a new empty form
129 $ReplaceValuesChecked = '';
131 # Reconstructs values to import
132 my $submitted_values = ();
133 foreach my $key (keys %ARGS) {
134 next unless ( $key =~ m/^row-(\d+)-(.+)$/ );
135 $submitted_values->{$1}->{$2} = $ARGS{$key};
137 # Remove unwanted values
138 foreach my $i (keys %$submitted_values) {
139 if ( ! $submitted_values->{$i}->{import} || $submitted_values->{$i}->{import} eq "0" ) {
140 delete $submitted_values->{$i};
141 } elsif ( $submitted_values->{$i}->{import} ) {
142 delete $submitted_values->{$i}->{import};
145 unless ( $submitted_values && ref($submitted_values) eq 'HASH' && scalar(keys %$submitted_values) ) {
146 push @results, loc("No values to import");
148 if ( $ARGS{ReplaceValues} ) {
149 my $Values = $CustomField->Values;
150 while (my $Value = $Values->Next) {
151 my $value_id = $Value->id;
152 my ($val, $msg) = $Value->Delete;
154 push @results, loc("Old value [_1] deleted", $value_id);
156 push @results, loc("Error deleting old value [_1]: [_2]", $value_id, $msg);
160 foreach my $i (keys %$submitted_values) {
161 my ($val, $msg) = $CustomField->AddValue( %{$submitted_values->{$i}} );
163 push @results, loc("Line [_1]: successfully imported", $i);
165 push @results, loc("Line [_1]: import error: [_2]", $i, $msg);