use strict; use Pos::Webtop::Toolbox (); use Pos::Policy::LayeredBurrito (); # base class package Pos::Webtop::Web::ContentModule::UserPrefs::PrefModule; my $className = "Pos::Webtop::Web::ContentModule::UserPrefs::PrefModule"; sub new { my $this = shift; my $class = ref($this) || $this; my ($self,$tb,$lb) = @{{@_}}{qw/self toolBox layeredBurrito/}; if ( not defined $self ) { $self = {}; bless($self,$class); } # note that the passing of an existing toolBox object is NOT AN OPTION # since we CANNOT create one of our own (if we did we would create a new # client object inside the toolBox which would destroy any persistedValues # that a script creating us had set before calling us. if ( not defined $tb ) { warn ref($self) . "::new(): toolBox was undef"; return undef; } $self->{'toolBox'} = $tb; # note that the passing of an existing layeredBurrito object is an option # since we can create one of our own. Passing an existing one in will # safe overhead on a page which instantiates a 'load' of PrefModules. $self->{'layeredBurrito'} = $lb; $self->{'done'} = 0; return $self; } sub isDone { my $self = shift; return $self->{'done'}; } sub setDone { my $self = shift; $self->{'done'} = 1; } # pure virtual sub getEmbeddedHtml { die(ref(shift) . " did not define main method required by base class $className"); } # pure virtual sub getPreviewText { die(ref(shift) . " did not define getPreviewLinkText method required by base class $className"); } sub setPreferenceName { my $self = shift; my ($preferenceName) = @{{@_}}{qw/preferenceName/}; $self->{'preferenceName'} = $preferenceName; } sub getPreferenceName { return shift->{'preferenceName'}; } sub getEmbeddedHeader { my $self = shift; my $tb = $self->{'toolBox'}; my $box = $tb->createBox( type=> "prefModule", title=> $self->getPreferenceName(), width => '400' ); $self->{'embeddedHeaderBox'} = $box; return $box->getHeaderHtml(); } sub getEmbeddedFooter { my $self = shift; my $box = $self->{'embeddedHeaderBox'}; return $box->getFooterHtml(); } sub getToolBox { return shift->{'toolBox'}; } sub getLayeredBurrito { my $self = shift; my $tb = $self->{'toolBox'}; if ( not defined $self->{'layeredBurrito'} ) { my $db = $tb->getPolicyDb(); $self->{'layeredBurrito'} = Pos::Policy::LayeredBurrito->new( db=> $db ); } return $self->{'layeredBurrito'}; } sub setPolicyTypeId { my $self = shift; my ($id) = @{{@_}}{qw/policyTypeId/}; $self->{'policyTypeId'} = $id; } sub getPolicyTypeId { return shift->{'policyTypeId'}; } # similar to ptf->getPolicyValues sub getPolicyValues { my $self = shift; my ($cache) = @{{@_}}{qw/cache/}; my $policyTypeId = $self->getPolicyTypeId(); my $tb = $self->getToolBox(); my $client = $tb->getClient(); my $userId = $client->getUserId(); $cache = 1 unless defined $cache; if ( not defined $userId ) { warn ref($self) . "::getPolicyValues(): userId was undef"; return undef; } if ( not defined $policyTypeId ) { warn ref($self) . "::getPolicyValues(): you have not set a policyTypeId for this PrefModule object"; return undef; } if ( (not defined $self->{'policyValuesCache'}) or (not $cache) ) { $self->{'policyValuesCache'} = $self->getLayeredBurrito()->getPolicyValues( cache=> $cache, layer=> "USER_PREF", id=> $userId, policyTypeId=> $policyTypeId ); } return $self->{'policyValuesCache'}; } sub isLocked { my $self = shift; my $locked = 0; my $values = $self->getPolicyValues(); foreach my $val ( @{ $values } ) { $val->{'policyValueType'} eq 'REQUIRED' and $locked = 1; } return $locked; } sub deleteUserPreference { my $self = shift; my $policyTypeId = $self->getPolicyTypeId(); my $tb = $self->getToolBox(); my $client = $tb->getClient(); my $userId = $client->getUserId(); if ( not defined $userId ) { warn ref($self) . "::deleteUserPreference(): userId was undef"; return undef; } if ( not defined $policyTypeId ) { warn ref($self) . "::deleteUserPreference(): you have not set a policyTypeId for this PrefModule object"; return undef; } if ( $self->isLocked() ) { warn ref($self) . "::deleteUserPreference(): tried to delete on a locked value.. sorry.. i just don't feel like breaking the rules for you today"; return undef; } $self->getLayeredBurrito()->deleteUserPreference( userId=> $userId, policyTypeId=> $policyTypeId ); # now we GET policy values to read in and update the object cache .. since # the caller may call preview on us and we will present them with old data which the # above sql updated my $unused = $self->getPolicyValues( cache=> 0 ); } sub setPolicyValues { my $self = shift; my ($values) = @{{@_}}{qw/values/}; my $policyTypeId = $self->getPolicyTypeId(); my $tb = $self->getToolBox(); my $client = $tb->getClient(); my $userId = $client->getUserId(); if ( not defined $userId ) { warn ref($self) . "::setPolicyValues(): userId was undef"; return undef; } if ( not defined $policyTypeId ) { warn ref($self) . "::setPolicyValues(): you have not set a policyTypeId for this PrefModule object"; return undef; } if ( $self->isLocked() ) { warn ref($self) . "::setPolicyValues(): tried to save on a locked value.. sorry.. i just don't feel like breaking the rules for you today"; return undef; } $self->getLayeredBurrito()->assignValuesAtLayer( layer=> "USER_PREF", typeId=> $policyTypeId, id=> $userId, values=> $values, userId=> $userId ); # now we GET policy values to read in and update the object cache .. since # the caller may call preview on us and we will present them with old data which the # above sql updated my $unused = $self->getPolicyValues( cache=> 0 ); } 1;