more work on submissionForm
This commit is contained in:
parent
378c73dd31
commit
226d87ca3e
3 changed files with 299 additions and 628 deletions
|
|
@ -51,390 +51,8 @@ These methods are available from this class:
|
|||
|
||||
=cut
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
=head2 _generateFields ( tabform, targetField, formDescription )
|
||||
|
||||
adds input fields to the tab based on the target field
|
||||
TODO: I should put this in the EMSSubmissionForm module instead
|
||||
|
||||
=head3 tabform
|
||||
|
||||
must be a tabform object
|
||||
|
||||
=head3 targetField
|
||||
|
||||
this is the definition of the field being described by the fields in the tab
|
||||
|
||||
=head3 formDescription
|
||||
|
||||
HASHREF to the current description
|
||||
|
||||
=cut
|
||||
|
||||
my $generators;
|
||||
|
||||
use lib '/root/pb/lib'; use dav;
|
||||
|
||||
sub _generateFields {
|
||||
my $tabform = shift;
|
||||
my $targetField = shift;
|
||||
my $formDescription = shift;
|
||||
my $fieldId = $targetField->{fieldId};
|
||||
my $fieldDescription = $formDescription->{$fieldId};
|
||||
my $dummy = $generators->{dummy}{generate};
|
||||
my $tab = $tabform->getTab($targetField->{fieldId});
|
||||
|
||||
dav::log '_generateFields::fieldId:', $targetField->{fieldId};
|
||||
|
||||
# TODO internationalize these
|
||||
$tab->checkbox(label => 'turn this field on',
|
||||
name => 'activeFields',
|
||||
value => $fieldId,
|
||||
checked => $fieldDescription->{on} || 0,
|
||||
);
|
||||
$tab->integer(label => 'display order',
|
||||
name => $fieldId . '_displayOrder',
|
||||
value => $fieldDescription->{displayOrder} || 0,
|
||||
);
|
||||
(($generators->{$targetField->{fieldType}}||{})->{generate} || $dummy)->($tab,$fieldId,$fieldDescription);
|
||||
$tab->checkbox(label => 'value is required',
|
||||
name => 'requiredFields',
|
||||
value => $fieldId,
|
||||
checked => $fieldDescription->{required} || 0,
|
||||
);
|
||||
$tab->text(label => 'default value',
|
||||
name => $fieldId . '_defaultValue',
|
||||
value => $fieldDescription->{defaultValue} || '',
|
||||
);
|
||||
$tab->text(label => 'override label',
|
||||
name => $fieldId . '_overrideLabel',
|
||||
value => $fieldDescription->{overrideLabel} || '',
|
||||
);
|
||||
$tab->textarea(label => 'override help',
|
||||
name => $fieldId . '_overrideHelp',
|
||||
value => $fieldDescription->{overrideHelp} || '',
|
||||
);
|
||||
$tab->hidden(
|
||||
name => $fieldId . '_fieldType',
|
||||
value => $targetField->{fieldType} || '',
|
||||
);
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
=head2 _readFields ( form, fieldId, formDescription )
|
||||
|
||||
copy field description values from session->form to description
|
||||
|
||||
=head3 form
|
||||
|
||||
the current session->form object
|
||||
|
||||
=head3 fieldId
|
||||
|
||||
fieldId for the field we are processing
|
||||
|
||||
=head3 formDescription
|
||||
|
||||
HASHREF to the current description
|
||||
|
||||
=cut
|
||||
|
||||
sub _readFields {
|
||||
my $form = shift;
|
||||
my $fieldId = shift;
|
||||
my $formDescription = shift;
|
||||
my $fieldDescription = $formDescription->{$fieldId} ||= {fieldType => $targetField->{fieldType}};
|
||||
my $dummy = $generators->{dummy}{readForm};
|
||||
|
||||
# we get the default value even if the field is not active...
|
||||
$fieldDescription->{defaultValue} = $form->get($fieldId . '_defaultValue');
|
||||
|
||||
return if ! grep $fieldId, ( @{$formDescription->{activeFields}} );
|
||||
|
||||
$fieldDescription->{on} = 1;
|
||||
$fieldDescription->{required} = grep $fieldId, ( @{$formDescription->{requiredFields}} );
|
||||
$fieldDescription->{displayOrder} = $form->get( $fieldId . '_displayOrder' );
|
||||
$fieldDescription->{overrideLabel} = $form->get($fieldId . '_overrideLabel');
|
||||
$fieldDescription->{overrideHelp} = $form->get($fieldId . '_overrideHelp');
|
||||
$fieldDescription->{fieldType} = $form->get($fieldId . '_fieldType');
|
||||
(($generators->{$description->{fieldType}}||{})->{readForm} || $dummy)->($form,$targetField,$fieldDescription);
|
||||
}
|
||||
|
||||
|
||||
# FUTURE: this list of functions shouldbe defined in the control classes themselves
|
||||
$generators = {
|
||||
dummy => { generate => sub {
|
||||
my $tab = shift;
|
||||
my $fieldId = shift;
|
||||
my $description = shift;
|
||||
$tab->readOnly(
|
||||
label => 'ERROR',
|
||||
value => $description->{fieldType} . ' is not defined in EMS Submission Form generators list',
|
||||
);
|
||||
},
|
||||
readForm => sub {
|
||||
# nothing to do here...
|
||||
} },
|
||||
dateTime => { generate => sub {
|
||||
my $tab = shift;
|
||||
my $fieldId = shift;
|
||||
my $description = shift;
|
||||
$tab->selectList(
|
||||
name => $fieldId . '_dateSelect',
|
||||
multiple => 0,
|
||||
size => 2,
|
||||
options => {
|
||||
# TODO internationalize this
|
||||
selectList => 'select list',
|
||||
dateSelect => 'date select',
|
||||
},
|
||||
defaultValue => $description->{dateSelect} || ['dateSelect'],
|
||||
label => 'datetime selection label',
|
||||
hoverHelp => 'datetime selection help',
|
||||
);
|
||||
$tab->textarea(
|
||||
name => $fieldId . '_dateTextArea',
|
||||
label => 'datetime textarea label',
|
||||
hoverHelp => 'datetime textarea help',
|
||||
defaultValue => $description->{dateTextArea} || '',
|
||||
)
|
||||
},
|
||||
readForm => sub {
|
||||
my $form = shift;
|
||||
my $fieldId = shift;
|
||||
my $description = shift;
|
||||
$description->{dateSelect} = $form->get($fieldId . '_dateSelect');
|
||||
my @options;
|
||||
for my $item ( split( , $form->get($fieldId . '_dateTextArea') ) {
|
||||
push @options, WebGUI::DateTime->new($item)->epoch;
|
||||
}
|
||||
$description->{dateTextArea} = [ @options ];
|
||||
# TODO perhaps we need to verify the text area is valid?
|
||||
}, },
|
||||
checkList => { generate => sub {
|
||||
my $tab = shift;
|
||||
my $fieldId = shift;
|
||||
my $description = shift;
|
||||
$tab->textarea(
|
||||
name => $fieldId . '_dateTextArea',
|
||||
label => 'checklist textarea label',
|
||||
hoverHelp => 'checklist textarea help',
|
||||
defaultValue => $description->{checkListTextArea} || '',
|
||||
);
|
||||
},
|
||||
readForm => sub {
|
||||
my $form = shift;
|
||||
my $fieldId = shift;
|
||||
my $description = shift;
|
||||
$description->{checkListTextArea} = $form->get($fieldId . '_checkListTextArea');
|
||||
}, },
|
||||
combo => { generate => sub {
|
||||
my $tab = shift;
|
||||
my $fieldId = shift;
|
||||
my $description = shift;
|
||||
$tab->readOnly(
|
||||
label => 'TODO',
|
||||
value => 'combo needs work<br>
|
||||
hmmm, needs some thought...',
|
||||
);
|
||||
},
|
||||
readForm => sub {
|
||||
my $form = shift;
|
||||
my $fieldId = shift;
|
||||
my $description = shift;
|
||||
# add the correct variables to the description hash that is passed in
|
||||
}, },
|
||||
integer => { generate => sub {
|
||||
my $tab = shift;
|
||||
my $fieldId = shift;
|
||||
my $description = shift;
|
||||
$tab->text(
|
||||
name => $fieldId . '_integerMin',
|
||||
label => 'integer min label',
|
||||
hoverHelp => 'integer min help',
|
||||
defaultValue => $description->{integerMin} || '',
|
||||
);
|
||||
$tab->text(
|
||||
name => $fieldId . '_integerMax',
|
||||
label => 'integer max label',
|
||||
hoverHelp => 'integer max help',
|
||||
defaultValue => $description->{integerMax} || '',
|
||||
);
|
||||
},
|
||||
readForm => sub {
|
||||
my $form = shift;
|
||||
my $fieldId = shift;
|
||||
my $description = shift;
|
||||
$description->{integerMax} = $form->get($fieldId . '_integerMax');
|
||||
$description->{integerMin} = $form->get($fieldId . '_integerMin');
|
||||
}, },
|
||||
float => { generate => sub {
|
||||
my $tab = shift;
|
||||
my $fieldId = shift;
|
||||
my $description = shift;
|
||||
$tab->text(
|
||||
name => $fieldId . '_floatMin',
|
||||
label => 'float min label',
|
||||
hoverHelp => 'float min help',
|
||||
defaultValue => $description->{floatMin} || '',
|
||||
);
|
||||
$tab->text(
|
||||
name => $fieldId . '_floatMax',
|
||||
label => 'float max label',
|
||||
hoverHelp => 'float max help',
|
||||
defaultValue => $description->{floatMax} || '',
|
||||
);
|
||||
},
|
||||
readForm => sub {
|
||||
my $form = shift;
|
||||
my $fieldId = shift;
|
||||
my $description = shift;
|
||||
$description->{floatMax} = $form->get($fieldId . '_floatMax');
|
||||
$description->{floatMin} = $form->get($fieldId . '_floatMin');
|
||||
}, },
|
||||
vendor => { generate => sub {
|
||||
my $tab = shift;
|
||||
my $fieldId = shift;
|
||||
my $description = shift;
|
||||
$tab->readOnly(
|
||||
label => 'TODO',
|
||||
value => 'vendor needs work -- this might get eliminated',
|
||||
);
|
||||
},
|
||||
readForm => sub {
|
||||
my $form = shift;
|
||||
my $fieldId = shift;
|
||||
my $description = shift;
|
||||
# add the correct variables to the description hash that is passed in
|
||||
}, },
|
||||
yesNo => { generate => sub {
|
||||
my $tab = shift;
|
||||
my $fieldId = shift;
|
||||
my $description = shift;
|
||||
# nothing here...
|
||||
#$tab->readOnly(
|
||||
# label => 'TODO',
|
||||
# value => $field->{fieldType} . ' needs work -- possibly no extra options',
|
||||
# );
|
||||
},
|
||||
readForm => sub {
|
||||
my $form = shift;
|
||||
my $fieldId = shift;
|
||||
my $description = shift;
|
||||
# nothing here...
|
||||
}, },
|
||||
text => { generate => sub {
|
||||
my $tab = shift;
|
||||
my $fieldId = shift;
|
||||
my $description = shift;
|
||||
$tab->list(
|
||||
name => $fieldId . '_textSelect',
|
||||
multiple => 0,
|
||||
options => {
|
||||
# TODO internationalize this
|
||||
selectList => 'select list',
|
||||
freeText => 'free text',
|
||||
},
|
||||
defaultValue => $description->{dateSelect} || ['freeText'],
|
||||
label => 'text selection label',
|
||||
hoverHelp => 'text selection help',
|
||||
);
|
||||
$tab->textarea(
|
||||
name => $fieldId . '_textTextArea',
|
||||
label => 'text textarea label',
|
||||
hoverHelp => 'text textarea help',
|
||||
defaultValue => $description->{textTextArea} || '',
|
||||
);
|
||||
},
|
||||
readForm => sub {
|
||||
my $form = shift;
|
||||
my $fieldId = shift;
|
||||
my $description = shift;
|
||||
$description->{textSelect} = $form->get($fieldId . '_textSelect');
|
||||
$description->{textTextArea} = $form->get($fieldId . '_textTextArea');
|
||||
}, },
|
||||
textarea => { generate => sub {
|
||||
my $tab = shift;
|
||||
my $fieldId = shift;
|
||||
my $description = shift;
|
||||
# nothing here...
|
||||
#$tab->readOnly(
|
||||
# label => 'TODO',
|
||||
# value => $field->{fieldType} . ' needs work -- might get eliminated or have no options',
|
||||
# );
|
||||
},
|
||||
readForm => sub {
|
||||
my $form = shift;
|
||||
my $fieldId = shift;
|
||||
my $description = shift;
|
||||
# nothing here...
|
||||
}, },
|
||||
file => { generate => sub {
|
||||
my $tab = shift;
|
||||
my $fieldId = shift;
|
||||
my $description = shift;
|
||||
# nothing here...
|
||||
#$tab->readOnly(
|
||||
# label => 'TODO',
|
||||
# value => $field->{fieldType} . ' needs work -- might get eliminated or have no options',
|
||||
# );
|
||||
},
|
||||
readForm => sub {
|
||||
my $form = shift;
|
||||
my $fieldId = shift;
|
||||
my $description = shift;
|
||||
# nothing here...
|
||||
}, },
|
||||
HTMLArea => { generate => sub {
|
||||
my $tab = shift;
|
||||
my $fieldId = shift;
|
||||
my $description = shift;
|
||||
# nothing here...
|
||||
#$tab->readOnly(
|
||||
# label => 'TODO',
|
||||
# value => $field->{fieldType} . ' needs work -- might get eliminated or have no options',
|
||||
# );
|
||||
},
|
||||
readForm => sub {
|
||||
my $form = shift;
|
||||
my $fieldId = shift;
|
||||
my $description = shift;
|
||||
# nothing here...
|
||||
}, },
|
||||
# TODO add all of the other control types
|
||||
};
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 _readForm ( parent, @fieldIds )
|
||||
|
||||
reads the form description out of the session->form
|
||||
|
||||
=head3 parent
|
||||
|
||||
the parent of the form
|
||||
|
||||
=head3 fieldIds
|
||||
|
||||
list of fieldIds that are in the form
|
||||
|
||||
=cut
|
||||
|
||||
sub _readForm {
|
||||
my $parent = shift;
|
||||
my $form = shift;
|
||||
my $fieldList = shift;
|
||||
my $formDescription;
|
||||
|
||||
$formDescription->{activeFields} = [ split ' ', $form->get('activeFields') ];
|
||||
$formDescription->{requiredFields} = [ split ' ', $form->get('requiredFields') ];
|
||||
|
||||
for my $fieldId ( split ' ', $fieldList ) {
|
||||
_readFields($form,$fieldId,$formDescription);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 addSubmission
|
||||
|
|
@ -446,11 +64,12 @@ Creates an EMSSubmission object based on the params
|
|||
|
||||
sub addSubmission {
|
||||
my $self = shift;
|
||||
my $session = $self->session;
|
||||
my $params = shift || {};
|
||||
return { isValid => 0, errors => [ 'no permissions' ] } if ! $self->canSubmit;
|
||||
my $newParams = $self->validateSubmission($params);
|
||||
return $newParams if ! $newParams->{isValid} ;
|
||||
my $form = $self->session->form;
|
||||
my $newParams = {};
|
||||
my $fieldList = $self->getFormDescription->{_fieldList};
|
||||
for my $field ( @$fieldList ) {
|
||||
$newParams->{$field} = $form->get($field);
|
||||
}
|
||||
$newParams->{className} = 'WebGUI::Asset::EMSSubmission';
|
||||
$newParams->{status} = 'pending';
|
||||
$newParams->{submissionId} = $self->get('nextSubmissionId');
|
||||
|
|
@ -688,124 +307,6 @@ sub view {
|
|||
return $self->processTemplate( $var, undef, $self->{_viewTemplate} );
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 validateSubmission ( submission )
|
||||
|
||||
test submitted data against form description
|
||||
|
||||
=head3 submission
|
||||
|
||||
hash ref with the submitted data
|
||||
|
||||
=cut
|
||||
|
||||
sub validateSubmission {
|
||||
my $self = shift;
|
||||
my $submission = shift;
|
||||
my $adminOverride = JSON->new->decode( $submission->{adminOverride} || ' { } ' );
|
||||
my $session = $self->session;
|
||||
my $target = { isValid => 1, adminOverride => $adminOverride };
|
||||
my $form = $self->getFormDescription;
|
||||
for my $field (keys %{$form}) {
|
||||
next if not defined $form->{$field}{type};
|
||||
my $value = $submission->{$field} || $form->{$field}{default} || '';
|
||||
$self->validateSubmissionField( $value, $form->{$field}, $field, $target );
|
||||
}
|
||||
return $target;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 validateSubmissionField ( value, fieldDef, name )
|
||||
|
||||
test field data against definition
|
||||
|
||||
=head4 value
|
||||
|
||||
value submitted
|
||||
|
||||
=head4 fieldDef
|
||||
|
||||
field definition
|
||||
|
||||
=head4 name
|
||||
|
||||
name of the field -- for error reporting
|
||||
|
||||
=cut
|
||||
|
||||
sub validateSubmissionField {
|
||||
my $self = shift;
|
||||
my $value = shift;
|
||||
my $fieldDef = shift;
|
||||
my $name = shift;
|
||||
my $target = shift;
|
||||
if( exists $target->{adminOverride}{$name} ) {
|
||||
if( ( $target->{adminOverride}{$name}{type} =~ /(float|integer)/i
|
||||
&& $target->{adminOverride}{$name}{value} == $value )
|
||||
|| $target->{adminOverride}{$name}{value} eq $value
|
||||
) {
|
||||
$target->{$name} = $value;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
if( $value eq '' ) {
|
||||
$target->{$name} = $value;
|
||||
return 1;
|
||||
}
|
||||
if( $fieldDef->{required} && $value eq '' ) {
|
||||
$target->{isvalid} = 0;
|
||||
push @{$target->{errors}}, $name . ' is a required field'; # TODO internationalize
|
||||
return 0;
|
||||
}
|
||||
my $type = $fieldDef->{type};
|
||||
if( $type eq 'url' ) {
|
||||
if( $value !~ /^http:/ ) { # TODO get a better test for Earls
|
||||
$target->{isValid} = 0;
|
||||
push @{$target->{errors}}, $name . ' is not a valid Url'; # TODO internationalize
|
||||
return 0;
|
||||
}
|
||||
} elsif( $type eq 'integer' ) {
|
||||
$value = int( $value );
|
||||
if( $fieldDef{integerMin} ne '' && $value < $fieldDef{integerMin} ) {
|
||||
$target->{isValid} = 0;
|
||||
push @{$target->{errors}}, $name . ' is less than the minimum allowed'; # TODO internationalize
|
||||
return 0;
|
||||
}
|
||||
if( $fieldDef{integerMax} ne '' && $value > $fieldDef{integerMax} ) {
|
||||
$target->{isValid} = 0;
|
||||
push @{$target->{errors}}, $name . ' is greater than the maximum allowed'; # TODO internationalize
|
||||
return 0;
|
||||
}
|
||||
} elsif( $type eq 'float' ) {
|
||||
if( $fieldDef{floatMin} ne '' && $value < $fieldDef{floatMin} ) {
|
||||
$target->{isValid} = 0;
|
||||
push @{$target->{errors}}, $name . ' is less than the minimum allowed'; # TODO internationalize
|
||||
return 0;
|
||||
}
|
||||
if( $fieldDef{floatMax} ne '' && $value > $fieldDef{floatMax} ) {
|
||||
$target->{isValid} = 0;
|
||||
push @{$target->{errors}}, $name . ' is greater than the maximum allowed'; # TODO internationalize
|
||||
return 0;
|
||||
}
|
||||
} elsif( $type eq 'text' ) {
|
||||
; # there is no test here...
|
||||
} elsif( $type eq 'textarea' ) {
|
||||
; # there is no test here...
|
||||
} elsif( $type eq 'selectList' ) {
|
||||
if( ! grep { $_ eq $value } @{$fieldDef->{options}} ) {
|
||||
$target->{isValid} = 0;
|
||||
push @{$target->{errors}}, $name . ' is not a valid Selection';
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
push @{$target->{errors}}, $type . ' is not a valid data type';
|
||||
return 0;
|
||||
}
|
||||
$target->{$name} = $value;
|
||||
return 1;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
|
|
@ -828,19 +329,27 @@ sub www_edit {
|
|||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 www_editSubmissionForm
|
||||
=head2 editSubmissionForm { parent, params }
|
||||
|
||||
is assetId is 'new' edit a blank form, else edit a form with stuff filled in...
|
||||
create an html form for user to enter params for a new submissionForm asset
|
||||
|
||||
=head3 parent
|
||||
|
||||
the parent ems object
|
||||
|
||||
=head3 params
|
||||
|
||||
optional set of possibly incorrect submission form params
|
||||
|
||||
=cut
|
||||
|
||||
sub editSubmissionForm {
|
||||
my $class = shift;
|
||||
my $parent = shift;
|
||||
my $params = shift || { };
|
||||
my $session = $parent->session;
|
||||
my $i18n = WebGUI::International->new($parent->session,'Asset_EventManagementSystem');
|
||||
my $form = $session->form;
|
||||
my $assetId = shift || $form->get('assetId');
|
||||
my $assetId = $params->{assetId} || $session->form->get('assetId');
|
||||
my $self;
|
||||
|
||||
if( ! defined( $assetId ) ) {
|
||||
|
|
@ -869,9 +378,119 @@ sub editSubmissionForm {
|
|||
$session->errorHandler->error(__PACKAGE__ . " - failed to instanciate asset with assetId $assetId");
|
||||
}
|
||||
}
|
||||
my $tabform = WebGUI::TabForm->new($session,undef,undef,$parent->getUrl());
|
||||
my $newform = WebGUI::HTMLForm->new($session,action => $parent->getUrl('func=editSubmissionFormSave'));
|
||||
$newform->submit;
|
||||
$newform->hidden(name => 'assetId', value => $assetId);
|
||||
my @fieldNames = qw/title description startDate duration seatsAvailable location/;
|
||||
my $fields;
|
||||
# fixed order for the regular tabs
|
||||
my @defs = reverse @{WebGUI::Asset::EMSSubmission->definition($session)};
|
||||
dav::dump 'editSubmissionForm::definition:', [@defs];
|
||||
for my $def ( @defs ) {
|
||||
foreach my $fieldName ( @fieldNames ) {
|
||||
my $properties = $def->{properties};
|
||||
if( defined $properties->{$fieldName} ) {
|
||||
$fields->{$fieldName} = { %{$properties->{$fieldName}} }; # a simple first level copy
|
||||
# field definitions don't contain their own name, we will need it later on
|
||||
$fields->{$fieldName}{fieldId} = $fieldName;
|
||||
};
|
||||
}
|
||||
}
|
||||
for my $metaField ( @{$parent->getEventMetaFields} ) {
|
||||
push @fieldNames, $metaField->{fieldId};
|
||||
$fields->{$metaField->{fieldId}} = { %$metaField }; # a simple first level copy
|
||||
# meta fields call it data type, we copy it to simplify later on
|
||||
$fields->{$metaField->{fieldId}}{fieldType} = $metaField->{dataType};
|
||||
}
|
||||
$newform->hidden( name => 'fieldNames', value => join( ' ', @fieldNames ) );
|
||||
@defs = reverse @{WebGUI::Asset::EMSSubmissionForm->definition($session)};
|
||||
dav::dump 'editSubmissionForm::dump submission form def', \@defs ;
|
||||
for my $def ( @defs ) {
|
||||
my $properties = $def->{properties};
|
||||
for my $fieldName ( qw/title menuTitle url description canSubmitGroupId daysBeforeCleanup
|
||||
deleteCreatedItems submissionDeadline pastDeadlineMessage/ ) {
|
||||
if( defined $properties->{$fieldName} ) {
|
||||
my %fieldParams = %{$properties->{$fieldName}};
|
||||
$fieldParams{name} = $fieldName;
|
||||
$fieldParams{value} = $params->{$fieldName} || $self ? $self->get($fieldName) : undef ;
|
||||
dav::dump 'editSubmissionForm::properties for ', $fieldName, \%fieldParams ;
|
||||
$newform->dynamicField(%fieldParams);
|
||||
}
|
||||
}
|
||||
}
|
||||
dav::dump 'editSubmissionForm::dump before generate:',$fields;
|
||||
|
||||
my $formDescription = $params->{formDescription} || $self ? $self->getFormDescription : { };
|
||||
for my $fieldId ( @fieldNames ) {
|
||||
my $field = $fields->{$fieldId};
|
||||
$newform->yesNo(
|
||||
label => $field->{label},
|
||||
name => $field->{fieldId} . '_yesNo',
|
||||
defaultValue => 0,
|
||||
value => $formDescription->{$field->{fieldId}},
|
||||
);
|
||||
}
|
||||
return $parent->processStyle(
|
||||
$parent->processTemplate({
|
||||
errors => $params->{errors} || [],
|
||||
backUrl => $parent->getUrl,
|
||||
pageForm => $newform->print,
|
||||
},$parent->get('eventSubmissionFormTemplateId')));
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------
|
||||
|
||||
=head2 processForm ( $parent )
|
||||
|
||||
pull data componenets out of $session->form
|
||||
|
||||
=head3 parent
|
||||
|
||||
reference to the EMS asset that is parent to the new submission form asset
|
||||
|
||||
=cut
|
||||
|
||||
use lib '/root/pb/lib'; use dav;
|
||||
|
||||
sub processForm {
|
||||
my $class = shift;
|
||||
my $parent = shift;
|
||||
my $form = $parent->session->form;
|
||||
my $params = {_isValid=>1};
|
||||
#if( $form->validToken ) {
|
||||
for my $fieldName ( qw/assetId title menuTitle url description canSubmitGroupId daysBeforeCleanup
|
||||
deleteCreatedItems submissionDeadline pastDeadlineMessage/ ) {
|
||||
$params->{$fieldName} = $form->get($fieldName);
|
||||
}
|
||||
my @fieldNames = split( ' ', $form->get('fieldNames') );
|
||||
$params->{formDescription} = { map { $_ => $form->get($_ . '_yesNo') } ( @fieldNames ) };
|
||||
$params->{formDescription}{_fieldList} = [ map { $params->{formDescription}{$_} ? $_ : () } ( @fieldNames ) ];
|
||||
if( scalar( @{$params->{formDescription}{_fieldList}} ) == 0 ) {
|
||||
$params->{_isValid} = 0;
|
||||
push @{$params->{errors}}, {text => 'you should turn on at least one entry field' }; # TODO internationalize this
|
||||
}
|
||||
dav::dump 'processForm::params:', $params;
|
||||
return $params;
|
||||
#} else {
|
||||
#return {_isValid => 0, errors => [ { text => 'invalid form token' } ] };
|
||||
#}
|
||||
}
|
||||
|
||||
#----------------------------------------------------------------
|
||||
|
||||
=head2 submitForm (... )
|
||||
|
||||
creates the form for the submitter to enter data
|
||||
|
||||
returns a form object
|
||||
|
||||
=cut
|
||||
|
||||
sub submitForm {
|
||||
# add the default items, then add items based on the fields in the def...
|
||||
|
||||
=head TODO work on this code
|
||||
# this is a bunch of code that will likely be useful for this function...
|
||||
{
|
||||
my @fieldNames = qw/startDate duration seatsAvailable location /;
|
||||
my @defs = reverse @{WebGUI::Asset::EMSSubmission->definition($session)};
|
||||
dav::dump 'editSubmissionForm::definition:', [@defs];
|
||||
|
|
@ -900,6 +519,7 @@ dav::dump 'editSubmissionForm::definition:', [@defs];
|
|||
}
|
||||
my $maintab = $tabform->getTab('main');
|
||||
$maintab->hidden(name => 'fieldList', value => join( ' ', @fieldNames ) );
|
||||
my @fieldNames = qw/startDate duration seatsAvailable location/;
|
||||
@defs = reverse @{WebGUI::Asset::EMSSubmissionForm->definition($session)};
|
||||
dav::dump 'editSubmissionForm::dump submission form def', \@defs ;
|
||||
for my $def ( @defs ) {
|
||||
|
|
@ -916,22 +536,26 @@ dav::dump 'editSubmissionForm::properties for ', $fieldName, \%param ;
|
|||
}
|
||||
}
|
||||
dav::dump 'editSubmissionForm::dump before generate:',$fields;
|
||||
my $formDescription;
|
||||
# TODO move the fieldList to session scratch or something on the server... - this is a security issue...
|
||||
if( my $fieldList = $form->get('fieldList') ) { # if this form variable exists then the form was submitted...
|
||||
$formDescription = _readForm($parent,$form,$fieldList); # so we get the description from the form
|
||||
} else {
|
||||
$formDescription = $self ? $self->getFormDescription : { };
|
||||
}
|
||||
for my $field ( values %$fields ) {
|
||||
next if $field->{fieldId} eq 'main' ;
|
||||
_generateFields($tabform, $field,$formDescription);
|
||||
}
|
||||
return $parent->processStyle(
|
||||
$parent->processTemplate({
|
||||
backUrl => $parent->getUrl,
|
||||
pageForm => $tabform->print,
|
||||
},$parent->get('eventSubmissionFormTemplateId')));
|
||||
}
|
||||
=cut
|
||||
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 update ( )
|
||||
|
||||
We overload the update method from WebGUI::Asset in order to handle file system privileges.
|
||||
|
||||
=cut
|
||||
|
||||
sub update {
|
||||
my $self = shift;
|
||||
my $properties = shift;
|
||||
if( ref $properties->{formDescription} eq 'HASH' ) {
|
||||
$properties->{formDescription} = JSON->new->encode($properties->{formDescription}||{});
|
||||
}
|
||||
$self->SUPER::update({%$properties, isHidden => 1});
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
|
|||
|
|
@ -81,15 +81,15 @@ default: 0
|
|||
|
||||
=head4 formDescription
|
||||
|
||||
a JSON description of the form data fields
|
||||
|
||||
TODO: write a comprehensive doc for this field
|
||||
a JSON description of the form data fields -- a hash of the names of fields (each is 1 for active, 0 for inactive) plus
|
||||
'_fieldList' added as an ARRAYREF of the fields that are active
|
||||
|
||||
=cut
|
||||
|
||||
sub addSubmissionForm {
|
||||
my $self = shift;
|
||||
my $params = shift;
|
||||
delete $params->{_isValid} if exists $params->{_isValid};
|
||||
$params->{className} = 'WebGUI::Asset::EMSSubmissionForm';
|
||||
$params->{canSubmitGroupId} ||= 2;
|
||||
$self->addGroupToSubmitList($params->{canSubmitGroupId});
|
||||
|
|
@ -644,7 +644,7 @@ call www_editSubmissionForm with assetId == new
|
|||
|
||||
sub www_addSubmissionForm {
|
||||
my $self = shift;
|
||||
$self->www_editSubmissionForm( 'new' );
|
||||
$self->www_editSubmissionForm( { assetId => 'new' } );
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
|
@ -827,14 +827,13 @@ sub www_editBadgeGroupSave {
|
|||
|
||||
=head2 www_editSubmissionForm
|
||||
|
||||
is assetId is 'new' edit a blank form, else edit a form with stuff filled in...
|
||||
calls editSubmissionForm in WebGUI::Asset::EMSSubmissionForm
|
||||
|
||||
=cut
|
||||
|
||||
sub www_editSubmissionForm {
|
||||
my $self = shift;
|
||||
return $self->session->privilege->insufficient() unless $self->canEdit;
|
||||
my $session = $self->session;
|
||||
return WebGUI::Asset::EMSSubmissionForm->editSubmissionForm($self,shift);
|
||||
}
|
||||
|
||||
|
|
@ -843,6 +842,7 @@ sub www_editSubmissionForm {
|
|||
|
||||
=head2 www_editSubmissionFormSave
|
||||
|
||||
test and save data posted from editSubmissionForm...
|
||||
|
||||
=cut
|
||||
|
||||
|
|
@ -850,11 +850,13 @@ sub www_editSubmissionFormSave {
|
|||
my $self = shift;
|
||||
return $self->session->privilege->insufficient() unless $self->canEdit;
|
||||
my $form = $self->session->form;
|
||||
return WebGUI::Asset::EMSSubmissionForm->processForm($self,); # TODO this function does not exist yet
|
||||
# it should read the form and verify the data
|
||||
# TODO call addSubmissionForm or update the submission form...
|
||||
# call edit if it fails
|
||||
return $self->www_view; # TODO where to go after this???
|
||||
my $formParams = WebGUI::Asset::EMSSubmissionForm->processForm($self);
|
||||
if( $formParams->{_isValid} ) {
|
||||
$self->addSubmissionForm($formParams);
|
||||
return $self->www_view;
|
||||
} else {
|
||||
return $self->www_editSubmissionForm($formParams);
|
||||
}
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
|
|
|||
|
|
@ -20,9 +20,9 @@ use Test::More;
|
|||
use Test::Deep;
|
||||
use JSON;
|
||||
use WebGUI::Test; # Must use this before any other WebGUI modules
|
||||
use WebGUI::Test::Activity;
|
||||
use WebGUI::Group;
|
||||
use WebGUI::User;
|
||||
use WebGUI::Test::Activity;
|
||||
use WebGUI::Session;
|
||||
use WebGUI::Asset::Wobject::EventManagementSystem;
|
||||
use WebGUI::Asset::Sku::EMSBadge;
|
||||
|
|
@ -68,11 +68,11 @@ sub logout { $session->user({userId => 1}); }
|
|||
|
||||
#----------------------------------------------------------------------------
|
||||
# put your tests here
|
||||
eval {
|
||||
my $use = use_ok 'WebGUI::Asset::EMSSubmissionForm';
|
||||
$use &&= use_ok 'WebGUI::Asset::EMSSubmission';
|
||||
|
||||
my $useform = use_ok 'WebGUI::Asset::EMSSubmissionForm';
|
||||
my $usesubmiss = use_ok 'WebGUI::Asset::EMSSubmission';
|
||||
|
||||
SKIP: { skip 'package compile failed!', 50 unless $useform && $usesubmiss;
|
||||
SKIP: { skip 'package compile failed!', 1 unless $use;
|
||||
|
||||
loginAdmin;
|
||||
|
||||
|
|
@ -142,90 +142,45 @@ is( $ems->hasForms, 0, 'ems currently has no forms' );
|
|||
#print 'press return to continue test' ; <>;
|
||||
|
||||
my $formAdesc = {
|
||||
title => { type => 'text' },
|
||||
descrition => { type => 'textarea' },
|
||||
duration => { default => 2.0 },
|
||||
startDate => { type => 'selectList',
|
||||
options => [ '1255150800', '1255237200', '1255323600' ],
|
||||
},
|
||||
_fieldList => [ qw/title description startDate/ ],
|
||||
title => 1,
|
||||
description => 1,
|
||||
duration => 0,
|
||||
startDate => 1,
|
||||
seatsAvailable => 0,
|
||||
location => 0,
|
||||
};
|
||||
|
||||
use lib '/root/pb/lib'; use dav;
|
||||
dav::dump $session;
|
||||
|
||||
my $frmA = $ems->addSubmissionForm({
|
||||
title => 'test A -- long',
|
||||
canSubmitGroupId => $submitGroupA->getId,
|
||||
daysBeforeCleanup => 1,
|
||||
formDescription => to_json( $formAdesc ),
|
||||
formDescription => $formAdesc,
|
||||
});
|
||||
isa_ok( $frmA, 'WebGUI::Asset::EMSSubmissionForm' );
|
||||
is( $ems->hasForms, 1, 'ems now has forms' );
|
||||
is_deeply( $frmA->getFormDescription, $formAdesc, 'form description matches' );
|
||||
my $submission = {
|
||||
title => 'titlea',
|
||||
description => 'the description',
|
||||
startDate => '1255150800',
|
||||
};
|
||||
my $result = $frmA->validateSubmission($submission);
|
||||
ok( $result->{isValid}, 'a valid submission' );
|
||||
print join( "\n", @{$result->{errors}} ),"\n" if defined $result->{errors};
|
||||
$submission = {
|
||||
title => 'titlea',
|
||||
description => 'the description',
|
||||
startDate => '1205150800',
|
||||
};
|
||||
$result = $frmA->validateSubmission($submission);
|
||||
ok( !$result->{isValid}, 'not a valid submission: invalid value in startDate' );
|
||||
print join( "\n", @{$result->{errors}} ),"\n" if defined $result->{errors};
|
||||
$submission = {
|
||||
title => 'titlea',
|
||||
duration => 3.0,
|
||||
description => 'the description',
|
||||
startDate => '1255150800',
|
||||
};
|
||||
$result = $frmA->validateSubmission($submission);
|
||||
ok( $result->{isValid} && ! defined $result->{duration}, 'valid submission: readonly field ignored' );
|
||||
print join( "\n", @{$result->{errors}} ),"\n" if defined $result->{errors};
|
||||
|
||||
|
||||
my $formBdesc = {
|
||||
title => { type => 'text' },
|
||||
description => { type => 'textarea' },
|
||||
duration => { type => 'float', default => 0.5, max => 0.5 },
|
||||
startDate => { default => '1255150800' },
|
||||
mfRequiredUrl => { type => 'url' },
|
||||
_fieldList => [ qw/title description duration mfRequiredUrl/ ],
|
||||
title => 1,
|
||||
description => 1,
|
||||
duration => 1,
|
||||
startDate => 0,
|
||||
mfRequiredUrl => 1,
|
||||
seatsAvailable => 0,
|
||||
location => 0,
|
||||
};
|
||||
my $frmB = $ems->addSubmissionForm({
|
||||
className => 'WebGUI::Asset::EMSSubmissionForm',
|
||||
title => 'test B -- short',
|
||||
daysBeforeCleanup => 0,
|
||||
canSubmitGroupId => $submitGroupB->getId,
|
||||
formDescription => to_json($formBdesc),
|
||||
formDescription => $formBdesc,
|
||||
});
|
||||
$submission = {
|
||||
title => 'title',
|
||||
description => 'description',
|
||||
mfRequiredUrl => 'http://google.com/',
|
||||
};
|
||||
$result = $frmB->validateSubmission($submission);
|
||||
ok( $result->{isValid}, 'valid submission: test valid metafield value' );
|
||||
print join( "\n", @{$result->{errors}} ),"\n" if defined $result->{errors};
|
||||
$submission = {
|
||||
title => 'title',
|
||||
description => 'description',
|
||||
mfRequiredUrl => 'joe@sams.org',
|
||||
};
|
||||
$result = $frmB->validateSubmission($submission);
|
||||
ok( !$result->{isValid}, 'invalid submission: test invalid metafield value' );
|
||||
print join( "\n", @{$result->{errors}} ),"\n" if defined $result->{errors};
|
||||
$submission = {
|
||||
title => 'titlea',
|
||||
duration => 0.6,
|
||||
description => 'the description',
|
||||
mfRequiredUrl => 'http://google.com/',
|
||||
adminOverride => to_json( { duration => { value => 0.6, type => 'float' } } ),
|
||||
};
|
||||
$result = $frmB->validateSubmission($submission);
|
||||
ok( $result->{isValid}, 'valid submission: field value override by admin' );
|
||||
print join( "\n", @{$result->{errors}} ),"\n" if defined $result->{errors};
|
||||
logout;
|
||||
|
||||
ok( !$ems->canSubmit, 'Visitor cannot submit to this ems' );
|
||||
|
|
@ -238,23 +193,18 @@ ok( $frmA->canSubmit, 'UserA can submit to formA' );
|
|||
ok( !$frmB->canSubmit, 'UserA cannot submit to formB' );
|
||||
#print 'press return to complete test' ; <>;
|
||||
ok( !$ems->hasSubmissions, 'UserA has no submissions' );
|
||||
# this one should work
|
||||
my $sub1 = $frmA->addSubmission({
|
||||
|
||||
my $submission = {
|
||||
title => 'my favorite thing to talk about',
|
||||
description => 'the description',
|
||||
startDate => '1255150800',
|
||||
});
|
||||
};
|
||||
$session->request->setup_body($submission);
|
||||
my $sub1 = $frmA->addSubmission;
|
||||
push @cleanup, sub { $sub1->delete; };
|
||||
print join( "\n", @{$sub1->{errors}} ),"\n" if defined $sub1->{errors};
|
||||
isa_ok( $sub1, 'WebGUI::Asset::EMSSubmission', "userA/formA valid submission succeeded" );
|
||||
is( $ems->hasSubmissions, 1, 'UserA has submissions on this ems' );
|
||||
|
||||
#this one should fail
|
||||
my $sub2 = $frmB->addSubmission({
|
||||
title => 'why i like to be important',
|
||||
});
|
||||
print join( "\n", @{$sub2->{errors}} ),"\n" if defined $sub2->{errors};
|
||||
ok( ref $sub2 eq 'HASH' && !$sub2->{isValid}, "UserA cannot submit to formB" );
|
||||
my $isa1 = isa_ok( $sub1, 'WebGUI::Asset::EMSSubmission', "userA/formA valid submission succeeded" );
|
||||
ok( $ems->hasSubmissions, 'UserA has submissions on this ems' );
|
||||
|
||||
loginUserB;
|
||||
|
||||
|
|
@ -262,14 +212,15 @@ ok( $ems->canSubmit, 'UserB can submit to this ems' );
|
|||
ok( !$frmA->canSubmit, 'UserB cannot submit to formA' );
|
||||
ok( $frmB->canSubmit, 'UserB can submit to formB' );
|
||||
|
||||
$sub2 = $frmB->addSubmission({
|
||||
my $submission = {
|
||||
title => 'why i like to be important',
|
||||
description => 'the description',
|
||||
mfRequiredUrl => 'http://google.com',
|
||||
});
|
||||
};
|
||||
$session->request->setup_body($submission);
|
||||
my $sub2 = $frmB->addSubmission;
|
||||
push @cleanup, sub { $sub2->delete; };
|
||||
print join( "\n", @{$sub2->{errors}} ),"\n" if defined $sub2->{errors};
|
||||
isa_ok( $sub2, 'WebGUI::Asset::EMSSubmission', "userB/FormB valid submission succeeded" );
|
||||
my $isa2 = isa_ok( $sub2, 'WebGUI::Asset::EMSSubmission', "userB/FormB valid submission succeeded" );
|
||||
|
||||
loginUserC;
|
||||
|
||||
|
|
@ -277,7 +228,8 @@ ok( $ems->canSubmit, 'UserC can submit to this ems' );
|
|||
ok( $frmA->canSubmit, 'UserC can submit to formA' );
|
||||
ok( $frmB->canSubmit, 'UserC can submit to formB' );
|
||||
|
||||
SKIP: { skip 'create submission failed', 8 unless ref $sub1 eq 'WebGUI::Asset::EMSSubmission' and ref $sub1 eq ref $sub2;
|
||||
# TODO fix num tests
|
||||
SKIP: { skip 'create submission failed', 8 unless $isa1 && $isa2;
|
||||
|
||||
loginUserA;
|
||||
|
||||
|
|
@ -349,12 +301,105 @@ is( $sub2, undef, 'approval created a ticket');
|
|||
|
||||
$versionTag->commit;
|
||||
|
||||
# TODO either remove this or make it a real test.
|
||||
#loginAdmin;
|
||||
#print $ems->www_addSubmissionForm;
|
||||
|
||||
SKIP: { skip 'requires HTML::Form', 2 unless use_ok 'HTML::Form';
|
||||
# this is not the greatest testm but it does run through the basic create submissionForm code.
|
||||
loginAdmin;
|
||||
|
||||
my %settings = (
|
||||
assetId => 'new',
|
||||
fieldNames => 'title description startDate duration seatsAvailable location nzymEeuHPQIsgXY0hZxDxA xlvMNwFi1FWwP0PrUAnxSQ',
|
||||
title => 'Untitled',
|
||||
menuTitle => 'Untitled',
|
||||
url => '',
|
||||
canSubmitGroupId => 2,
|
||||
daysBeforeCleanup => 7,
|
||||
deleteCreatedItems => 0,
|
||||
submissionDeadline => '1991-06-21',
|
||||
pastDeadlineMessage => 'The deadline for this submission is past, no more submissions will be taken at this time.',
|
||||
title_yesNo => 1,
|
||||
description_yesNo => 1,
|
||||
startDate_yesNo => 1,
|
||||
duration_yesNo => 1,
|
||||
seatsAvailable_yesNo => 1,
|
||||
location_yesNo => 1,
|
||||
nzymEeuHPQIsgXY0hZxDxA_yesNo => 1,
|
||||
xlvMNwFi1FWwP0PrUAnxSQ_yesNo => 1,
|
||||
);
|
||||
|
||||
my $expected = {
|
||||
'submissionDeadline' => '1991-06-21',
|
||||
'menuTitle' => 'Untitled',
|
||||
'pastDeadlineMessage' => 'The deadline for this submission is past, no more submissions will be taken at this time.',
|
||||
'formDescription' => {
|
||||
'location' => '1',
|
||||
'nzymEeuHPQIsgXY0hZxDxA' => 'xlvMNwFi1FWwP0PrUAnxSQ',
|
||||
'seatsAvailable' => '1',
|
||||
'duration' => '1',
|
||||
'title' => '1',
|
||||
'startDate' => '1',
|
||||
'description' => '1',
|
||||
'_fieldList' => [
|
||||
'title',
|
||||
'description',
|
||||
'startDate',
|
||||
'duration',
|
||||
'seatsAvailable',
|
||||
'location',
|
||||
'nzymEeuHPQIsgXY0hZxDxA'
|
||||
]
|
||||
},
|
||||
'description' => undef,
|
||||
'_isValid' => 1,
|
||||
'deleteCreatedItems' => undef,
|
||||
'canSubmitGroupId' => '2',
|
||||
'assetId' => 'new',
|
||||
'url' => undef,
|
||||
'daysBeforeCleanup' => '7',
|
||||
'title' => 'Untitled'
|
||||
} ;
|
||||
|
||||
my $htmlText = $ems->www_addSubmissionForm;
|
||||
my $form = HTML::Form->parse($htmlText,'http://localhost/');
|
||||
for my $input ( $form->inputs ) {
|
||||
$input->value($settings{$input->name})if exists $settings{$input->name};
|
||||
}
|
||||
$session->request->setup_body( { $form->form } );
|
||||
my $result = WebGUI::Asset::EMSSubmissionForm->processForm($ems);
|
||||
dav::dump $result;
|
||||
cmp_deeply( $result, $expected , 'test process form' );
|
||||
$expected = {
|
||||
'errors' => [
|
||||
{
|
||||
'text' => 'you should turn on at least one entry field'
|
||||
}
|
||||
],
|
||||
'submissionDeadline' => undef,
|
||||
'menuTitle' => undef,
|
||||
'pastDeadlineMessage' => undef,
|
||||
'formDescription' => {
|
||||
'_fieldList' => []
|
||||
},
|
||||
'description' => undef,
|
||||
'_isValid' => 0,
|
||||
'deleteCreatedItems' => undef,
|
||||
'canSubmitGroupId' => undef,
|
||||
'assetId' => undef,
|
||||
'url' => undef,
|
||||
'daysBeforeCleanup' => undef,
|
||||
'title' => undef,
|
||||
};
|
||||
$session->request->setup_body( { } );
|
||||
$result = WebGUI::Asset::EMSSubmissionForm->processForm($ems);
|
||||
dav::dump $result;
|
||||
cmp_deeply( $result, $expected , 'test process form' );
|
||||
} # end of skip HTML::Form
|
||||
|
||||
} # end of use packages skip
|
||||
|
||||
}; # end of eval
|
||||
print $@ if $@;
|
||||
|
||||
#done_testing();
|
||||
#print 'press return to complete test' ; <>;
|
||||
#----------------------------------------------------------------------------
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue