migrate Thingy import from csv to FormBuilder
This commit is contained in:
parent
033a1b7793
commit
ae7528b8ef
3 changed files with 90 additions and 25 deletions
|
|
@ -2836,8 +2836,11 @@ sub www_import {
|
|||
while (my $field = $fields->hashRef) {
|
||||
push(@insertColumns, $field) if ($session->form->process("fileContains_".$field->{fieldId}));
|
||||
}
|
||||
|
||||
|
||||
my $log = $self->session->log;
|
||||
use Data::Dumper;
|
||||
$log->info( "Importing columns: " . Dumper( \@insertColumns ) );
|
||||
my $storage = WebGUI::Storage->createTemp($self->session);
|
||||
$handleDuplicates = $session->form->process("handleDuplicates");
|
||||
|
||||
|
|
@ -2955,32 +2958,32 @@ sub www_importForm {
|
|||
$i18n = WebGUI::International->new($self->session, "Asset_Thingy");
|
||||
|
||||
$output = "<h1>".$i18n->get("import label")."</h1>";
|
||||
$form = WebGUI::HTMLForm->new($self->session,-action=>$self->getUrl);
|
||||
$form->hidden(
|
||||
-name => "thingId",
|
||||
-value => $thingId
|
||||
$form = WebGUI::FormBuilder->new($self->session,action=>$self->getUrl);
|
||||
$form->addField( "hidden",
|
||||
name => "thingId",
|
||||
value => $thingId
|
||||
);
|
||||
$form->hidden(
|
||||
-name => "func",
|
||||
-value => "import"
|
||||
$form->addField( "hidden",
|
||||
name => "func",
|
||||
value => "import"
|
||||
);
|
||||
|
||||
$form->file(
|
||||
-name => "importFile",
|
||||
-label => $i18n->get("import file label"),
|
||||
$form->addField( "file",
|
||||
name => "importFile",
|
||||
label => $i18n->get("import file label"),
|
||||
);
|
||||
|
||||
$form->selectBox(
|
||||
-name => "handleDuplicates",
|
||||
-label=> $i18n->get("duplicates label"),
|
||||
-options=> {
|
||||
$form->addField( "selectBox",
|
||||
name => "handleDuplicates",
|
||||
label=> $i18n->get("duplicates label"),
|
||||
options=> {
|
||||
"skip" => $i18n->get("skip label"),
|
||||
"overwrite" => $i18n->get("overwrite label"),
|
||||
},
|
||||
);
|
||||
$form->yesNo(
|
||||
-name=>"ignoreFirstLine",
|
||||
-label=>$i18n->get("ignore first line label"),
|
||||
$form->addField( "yesNo",
|
||||
name=>"ignoreFirstLine",
|
||||
label=>$i18n->get("ignore first line label"),
|
||||
);
|
||||
|
||||
$fieldOptions = "<table>"
|
||||
|
|
@ -2993,24 +2996,24 @@ sub www_importForm {
|
|||
[$self->getId,$thingId]);
|
||||
while (my $field = $fields->hashRef) {
|
||||
$fieldOptions .= "<tr><td>".$field->{label}."</td><td>";
|
||||
$fieldOptions .= WebGUI::Form::checkbox($self->session, {
|
||||
$fieldOptions .= WebGUI::Form::Checkbox->new($self->session, {
|
||||
checked => "",
|
||||
name => "fileContains_".$field->{fieldId},
|
||||
value => 1,
|
||||
});
|
||||
})->toHtml;
|
||||
$fieldOptions .= "</td><td>";
|
||||
$fieldOptions .= WebGUI::Form::checkbox($self->session, {
|
||||
$fieldOptions .= WebGUI::Form::Checkbox->new($self->session, {
|
||||
checked => "",
|
||||
name => "checkDuplicates_".$field->{fieldId},
|
||||
value => 1,
|
||||
});
|
||||
})->toHtml;
|
||||
$fieldOptions .= "</td></tr>";
|
||||
}
|
||||
$fieldOptions .= "</table>";
|
||||
$form->raw($fieldOptions);
|
||||
$form->submit;
|
||||
$form->addField( "ReadOnly", name => 'fieldOptions', value => $fieldOptions );
|
||||
$form->addField( "submit", name => "submit" );
|
||||
|
||||
$output .= $form->print;
|
||||
$output .= $form->toHtml;
|
||||
return $self->processStyle($output);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ use strict;
|
|||
use WebGUI::Test;
|
||||
use WebGUI::Test::MockAsset;
|
||||
use WebGUI::Session;
|
||||
use Test::More tests => 35; # increment this value for each test you create
|
||||
use Test::More tests => 39; # increment this value for each test you create
|
||||
use Test::Deep;
|
||||
use JSON;
|
||||
use WebGUI::Asset::Wobject::Thingy;
|
||||
|
|
@ -466,3 +466,63 @@ my $field = $session->db->quickHashRef(
|
|||
);
|
||||
ok( $field, "field exists" );
|
||||
cmp_deeply( $field, superhashof( \%fieldInfo ), 'field info saved' );
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
# www_importForm
|
||||
|
||||
my $thingy = WebGUI::Test->asset(
|
||||
className => 'WebGUI::Asset::Wobject::Thingy',
|
||||
);
|
||||
my $thingId = $thingy->addThing();
|
||||
my @fieldIds = (
|
||||
$thingy->addField({
|
||||
assetId => $thingy->getId,
|
||||
thingId => $thingId,
|
||||
sequenceNumber => 1,
|
||||
label => 'Name',
|
||||
fieldType => 'text',
|
||||
}),
|
||||
$thingy->addField({
|
||||
assetId => $thingy->getId,
|
||||
thingId => $thingId,
|
||||
sequenceNumber => 2,
|
||||
label => 'Age',
|
||||
fieldType => 'Integer',
|
||||
}),
|
||||
);
|
||||
|
||||
my $mech = WebGUI::Test::Mechanize->new( config => WebGUI::Test->file );
|
||||
$mech->get_ok( '/' );
|
||||
$mech->session->user({ userId => 3 });
|
||||
|
||||
$mech->get_ok( $thingy->getUrl( 'func=importForm;thingId=' . $thingId ) );
|
||||
$mech->submit_form_ok({
|
||||
fields => {
|
||||
importFile_file => WebGUI::Test::collateral( "thingy.csv" ),
|
||||
"fileContains_$fieldIds[0]" => 1,
|
||||
"fileContains_$fieldIds[1]" => 1,
|
||||
},
|
||||
},
|
||||
"Import data into thing",
|
||||
);
|
||||
|
||||
my @thingData = $session->db->buildArrayRefOfHashRefs(
|
||||
"select * from ". $session->db->quote_identifier("Thingy_".$thingId),
|
||||
);
|
||||
cmp_deeply(
|
||||
@thingData,
|
||||
bag(
|
||||
superhashof({
|
||||
"field_$fieldIds[0]" => "Andy Dufresne",
|
||||
"field_$fieldIds[1]" => "42",
|
||||
}),
|
||||
superhashof({
|
||||
"field_$fieldIds[0]" => "Red Ellis",
|
||||
"field_$fieldIds[1]" => "47",
|
||||
}),
|
||||
),
|
||||
" All rows imported ",
|
||||
) or diag( explain \@thingData );
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
2
t/supporting_collateral/thingy.csv
Normal file
2
t/supporting_collateral/thingy.csv
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
"Andy Dufresne",42
|
||||
"Red Ellis",47
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue