Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sub editThingDataSave {
- my $self = shift;
- my $thingId = shift;
- my $thingDataId = shift;
- my $thingData = shift;
- my $session = $self->session;
- my (%thingData,$fields,@errors,$hadErrors,$newThingDataId);
- my $i18n = WebGUI::International->new($session, 'Asset_Thingy');
- $self->session->errorHandler->error("Inserting data. Thingdataid: $thingDataId");
- if ($thingDataId eq "new"){
- $thingData{dateCreated} = time();
- $thingData{createdById} = $session->user->userId;
- $thingData{ipAddress} = $session->env->getIp;
- }
- else {
- %thingData = $session->db->quickHash("select * from ".$session->db->dbh->quote_identifier("Thingy_".$thingId)
- ." where thingDataId = ?",[$thingDataId]);
- }
- %thingData = ( %thingData,
- thingDataId=>$thingDataId,
- updatedById=>$session->user->userId,
- updatedByName=>$session->user->username,
- lastUpDated=>time(),
- );
- $fields = $self->getFields($thingId);
- while (my $field = $fields->hashRef) {
- my $fieldName = 'field_'.$field->{fieldId};
- my $fieldValue;
- if ($field->{status} eq "required" || $field->{status} eq "editable"){
- my $fieldType = $field->{fieldType};
- $fieldType = "" if ($fieldType =~ m/^otherThing/x);
- # Modify the defaultValue for certain field types. For most types we want to use
- # the default in the database, for these we want the last known value for this thingData
- if ($self->field_isa($fieldType, 'WebGUI::Form::File')) {
- $field->{ defaultValue } = $thingData{ "field_" . $field->{ fieldId } };
- }
- $fieldValue = $thingData->{$fieldName} || $session->form->process($fieldName,$fieldType,$field->{defaultValue},$field);
- }
- if ($field->{status} eq "required" && ($fieldValue =~ /^\s$/x || $fieldValue eq "" || !(defined $fieldValue))) {
- push (@errors,{
- "error_message"=>$field->{label}." ".$i18n->get('is required error').".", "field_name"=>$fieldName,
- });
- }
- if ($field->{status} eq "hidden") {
- $fieldValue = $field->{defaultValue};
- WebGUI::Macro::process($self->session,\$fieldValue);
- }
- if ($field->{status} eq "visible") {
- $fieldValue = $field->{defaultValue};
- #WebGUI::Macro::process($self->session,\$fieldValue);
- }
- if ($field->{isUnique}) {
- unless ( $self->isUniqueEntry($thingId,$fieldName,$fieldValue,$thingDataId)) {
- push (@errors,{
- "error_message"=>$field->{label}. $i18n->get('needs to be unique error'),"field_name"=>$fieldName,
- });
- }
- }
- $thingData{$fieldName} = $fieldValue;
- }
- $self->session->errorHandler->error("Checking for errors : " . to_json(@errors));
- if (@errors) {
- return ('', \@errors);
- }
- $newThingDataId = $self->setCollateral("Thingy_".$thingId,"thingDataId",\%thingData,0,0);
- $self->indexThingData($thingId, \%thingData);
- # trigger workflow
- if($thingDataId eq "new"){
- my ($onAddWorkflowId) = $session->db->quickArray("select onAddWorkflowId from Thingy_things where thingId=?"
- ,[$thingId]);
- if ($onAddWorkflowId){
- $self->triggerWorkflow($onAddWorkflowId,$thingId,$newThingDataId);
- }
- }else{
- my ($onEditWorkflowId) = $session->db->quickArray("select onEditWorkflowId from Thingy_things where thingId=?"
- ,[$thingId]);
- if ($onEditWorkflowId){
- $self->triggerWorkflow($onEditWorkflowId,$thingId,$newThingDataId);
- }
- }
- return($newThingDataId,\@errors);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement