Changeset 2505


Ignore:
Timestamp:
01/08/22 09:16:59 (2 years ago)
Author:
nanardon
Message:

Allow to create sub object when editing

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Objects.pm

    r2398 r2505  
    464464    my ($self, %cdata) = @_; 
    465465    my %data; 
     466    my $sub; 
    466467    my $res = 0; 
    467468    foreach my $cfield (keys %cdata) { 
     469 
     470        if (my ($sotype, $key, $scfield) = $cfield =~ /^(\w+)(?:\[(\w+)\])?\.(.*)/) { 
     471            $key ||= '_'; 
     472            $sub->{$sotype}{$key}{$scfield} = $cdata{$cfield}; 
     473            next; 
     474        } 
     475 
    468476        my $attribute = $self->attribute($cfield) or do { 
    469477            $self->base->log(LA_ERR, 
     
    514522    } 
    515523 
     524    my $subupdate = 0; 
     525    if ($sub) { 
     526 
     527        # Security: if caller is create_c_object calling it to check permission ? 
     528        # See below 
     529        # my ($caller) = caller(); 
     530        # my $subcreate = $caller eq 'create_c_object' ? 'create_c_object' : __SUB__; 
     531 
     532        # Trying to create subobject 
     533        foreach my $sotype (keys %$sub) { 
     534            my $SubKeyRef = $self->base->GetSubObjectKey($self->otype, $sotype) or do { 
     535                $self->base->log(LA_ERR, "Cannot create object type $sotype, subtype of " . $self->otype . " not defined"); 
     536                return; 
     537            }; 
     538            foreach my $skey (keys %{ $sub->{$sotype} || {} }) { 
     539                # TODO Check if object exists ! 
     540                # Building id 
     541                my $info = $sub->{$sotype}{$skey} || {}; 
     542                # For id: if key is given using it, otherwise using random 
     543                my $sid = 
     544                    $info->{$SubKeyRef} || 
     545                    $self->id . '-' . join('', map { ('a' .. 'z')[rand(26)] } (0 .. 6)); 
     546                $info->{$SubKeyRef} = $self->id; 
     547 
     548                # Here we don't check permission to create sub object: 
     549                $self->base->_create_c_object($sotype, $sid, %{ $info || {} }) or return; 
     550                $subupdate++; 
     551            } 
     552        } 
     553    } 
     554 
     555 
    516556    if (keys %updated) { 
    517557        $self->PostSetAttribute() or do { 
     
    530570        } 
    531571    } 
    532     return scalar(keys %updated); 
     572    return(scalar(keys %updated) + $subupdate); 
    533573} 
    534574 
Note: See TracChangeset for help on using the changeset viewer.