package LATMOS::Accounts::Bases::OCHelper; # $Id: OCHelper.pm 2932 2010-08-10 17:19:21Z nanardon $ use strict; use warnings; sub new { my ($class, $base, $otype) = @_; bless { _base => $base, _otype => $otype, }, $class; } sub base { $_[0]->{_base} } sub otype { $_[0]->{_otype} } # return STATUS, $info # info = { # step => 0, # name => { # name of object # ask => 0/1, # content => ... # }, # ask => [ list ], # contents => { name => ... } # } # STATUS => 'NEEDINFO', 'CREATED', 'ERROR', undef (what??) sub step { my ($self, $info) = @_; $info ||= {}; $info->{step} ||= 0; $info->{ask} = []; $info->{name}{ask} = 0; my $otype = $self->otype; foreach (keys %{ $self->base->{defattr} || {} }) { /^$otype\.(.*)/ or next; my $attr = $1; my $oattr = $self->base->attribute($otype, $attr) or next; $oattr->ro and next; $info->{contents}{$attr} = $self->base->{defattr}{$_} unless exists($info->{contents}{$attr}); } return($self->_step($info), $info); } # just return status, $info is reference sub _step { my ($self, $info) = @_; if ($info->{step} == 0) { $info->{name}{ask} = 1; foreach ($self->base->list_canonical_fields($self->otype, 'w')) { push(@{$info->{ask}}, $_); } $info->{step} = 1; return 'NEEDINFO'; } elsif ($info->{step} == 1) { if ($self->base->create_c_object($self->otype, $info->{name}{content}, %{$info->{contents} || {}}, )) { return 'CREATED'; } else { return 'ERROR'; } } else { return undef; } } 1;