-sub Import {
- my $self = shift;
- my ( $ErrStr, $QueueObj, $Owner);
-
- my %args = (id => undef,
- EffectiveId => undef,
- Queue => undef,
- Requestor => undef,
- Type => 'ticket',
- Owner => $RT::Nobody->Id,
- Subject => '[no subject]',
- InitialPriority => undef,
- FinalPriority => undef,
- Status => 'new',
- TimeWorked => "0",
- Due => undef,
- Created => undef,
- Updated => undef,
- Resolved => undef,
- Told => undef,
- @_);
-
- if ( (defined($args{'Queue'})) && (!ref($args{'Queue'})) ) {
- $QueueObj=RT::Queue->new($RT::SystemUser);
- $QueueObj->Load($args{'Queue'});
- #TODO error check this and return 0 if it\'s not loading properly +++
- }
- elsif (ref($args{'Queue'}) eq 'RT::Queue') {
- $QueueObj=RT::Queue->new($RT::SystemUser);
- $QueueObj->Load($args{'Queue'}->Id);
- }
- else {
- $RT::Logger->debug("$self ". $args{'Queue'} .
- " not a recognised queue object.");
- }
-
- #Can't create a ticket without a queue.
- unless (defined ($QueueObj) and $QueueObj->Id) {
- $RT::Logger->debug( "$self No queue given for ticket creation.");
- return (0,'Could not create ticket. Queue not set');
- }
-
- #Now that we have a queue, Check the ACLS
- unless ($self->CurrentUser->HasQueueRight(Right => 'CreateTicket',
- QueueObj => $QueueObj )) {
- return (0,"No permission to create tickets in the queue '".
- $QueueObj->Name."'.");
- }
-
-
-
-
- # {{{ Deal with setting the owner
-
- # Attempt to take user object, user name or user id.
- # Assign to nobody if lookup fails.
- if (defined ($args{'Owner'})) {
- if ( ref($args{'Owner'}) ) {
- $Owner = $args{'Owner'};
- }
- else {
- $Owner = new RT::User($self->CurrentUser);
- $Owner->Load($args{'Owner'});
- if ( ! defined($Owner->id) ) {
- $Owner->Load($RT::Nobody->id);
- }
- }
- }
-
-
- #If we have a proposed owner and they don't have the right
- #to own a ticket, scream about it and make them not the owner
- if ((defined ($Owner)) and
- ($Owner->Id != $RT::Nobody->Id) and
- (!$Owner->HasQueueRight( QueueObj => $QueueObj,
- Right => 'OwnTicket'))) {
-
- $RT::Logger->warning("$self user ".$Owner->Name . "(".$Owner->id .
- ") was proposed ".
- "as a ticket owner but has no rights to own ".
- "tickets in '".$QueueObj->Name."'\n");
-
- $Owner = undef;
- }
-
- #If we haven't been handed a valid owner, make it nobody.
- unless (defined ($Owner)) {
- $Owner = new RT::User($self->CurrentUser);
- $Owner->Load($RT::Nobody->UserObj->Id);
- }
-
- # }}}
-
- unless ($self->ValidateStatus($args{'Status'})) {
- return (0,"'$args{'Status'}' is an invalid value for status");
- }
-
- $self->{'_AccessibleCache'}{Created} = { 'read'=>1, 'write'=>1 };
- $self->{'_AccessibleCache'}{Creator} = { 'read'=>1, 'auto'=>1 };
- $self->{'_AccessibleCache'}{LastUpdated} = { 'read'=>1, 'write'=>1 };
- $self->{'_AccessibleCache'}{LastUpdatedBy} = { 'read'=>1, 'auto'=>1 };
-
-
- # If we're coming in with an id, set that now.
- my $EffectiveId = undef;
- if ($args{'id'}) {
- $EffectiveId = $args{'id'};
-
- }
-
-
- my $id = $self->SUPER::Create(
- id => $args{'id'},
- EffectiveId => $EffectiveId,
- Queue => $QueueObj->Id,
- Owner => $Owner->Id,
- Subject => $args{'Subject'},
- InitialPriority => $args{'InitialPriority'},
- FinalPriority => $args{'FinalPriority'},
- Priority => $args{'InitialPriority'},
- Status => $args{'Status'},
- TimeWorked => $args{'TimeWorked'},
- Type => $args{'Type'},
- Created => $args{'Created'},
- Told => $args{'Told'},
- LastUpdated => $args{'Updated'},
- Resolved => $args{Resolved},
- Due => $args{'Due'},
- );
-
-
-
- # If the ticket didn't have an id
- # Set the ticket's effective ID now that we've created it.
- if ($args{'id'} ) {
- $self->Load($args{'id'});
- }
- else {
- my ($val, $msg) = $self->__Set(Field => 'EffectiveId', Value => $id);
-
- unless ($val) {
- $RT::Logger->err($self."->Import couldn't set EffectiveId: $msg\n");
- }
- }
-
- my $watcher;
- foreach $watcher (@{$args{'Cc'}}) {
- $self->_AddWatcher( Type => 'Cc', Person => $watcher, Silent => 1);
- }
- foreach $watcher (@{$args{'AdminCc'}}) {
- $self->_AddWatcher( Type => 'AdminCc', Person => $watcher, Silent => 1);
- }
- foreach $watcher (@{$args{'Requestor'}}) {
- $self->_AddWatcher( Type => 'Requestor', Person => $watcher, Silent => 1);
- }
-
- return($self->Id, $ErrStr);
-}
-
-# }}}
-
-# {{{ sub Delete
-
-sub Delete {
- my $self = shift;
- return (0, 'Deleting this object would violate referential integrity.'.
- ' That\'s bad.');
-}
-# }}}
-
-# {{{ Routines dealing with watchers.
-
-# {{{ Routines dealing with adding new watchers
-
-# {{{ sub AddWatcher
-
-=head2 AddWatcher
-
-AddWatcher takes a parameter hash. The keys are as follows:
-
-Email
-Type
-Owner
-
-If the watcher you\'re trying to set has an RT account, set the Owner paremeter to their User Id. Otherwise, set the Email parameter to their Email address.
-
-=cut
-
-sub AddWatcher {
- my $self = shift;
- my %args = ( Email => undef,
- Type => undef,
- Owner => undef,
- @_
- );
-
- # {{{ Check ACLS
- #If the watcher we're trying to add is for the current user
- if ( ( $self->CurrentUser->EmailAddress &&
- ($args{'Email'} eq $self->CurrentUser->EmailAddress) ) or
- ($args{'Owner'} eq $self->CurrentUser->Id)
- ) {
-
-
- # If it's an AdminCc and they don't have
- # 'WatchAsAdminCc' or 'ModifyTicket', bail
- if ($args{'Type'} eq 'AdminCc') {
- unless ($self->CurrentUserHasRight('ModifyTicket') or
- $self->CurrentUserHasRight('WatchAsAdminCc')) {
- return(0, 'Permission Denied');
- }
- }
-
- # If it's a Requestor or Cc and they don't have
- # 'Watch' or 'ModifyTicket', bail
- elsif (($args{'Type'} eq 'Cc') or
- ($args{'Type'} eq 'Requestor')) {
-
- unless ($self->CurrentUserHasRight('ModifyTicket') or
- $self->CurrentUserHasRight('Watch')) {
- return(0, 'Permission Denied');
- }
- }
- else {
- $RT::Logger->warn("$self -> AddWatcher hit code".
- " it never should. We got passed ".
- " a type of ". $args{'Type'});
- return (0,'Error in parameters to TicketAddWatcher');
- }
- }
- # If the watcher isn't the current user
- # and the current user doesn't have 'ModifyTicket'
- # bail
- else {
- unless ($self->CurrentUserHasRight('ModifyTicket')) {
- return (0, "Permission Denied");
- }
- }
- # }}}
-
- return ($self->_AddWatcher(%args));
-}
-
-
-#This contains the meat of AddWatcher. but can be called from a routine like
-# Create, which doesn't need the additional acl check
-sub _AddWatcher {
- my $self = shift;
- my %args = (
- Type => undef,
- Silent => undef,
- Email => undef,
- Owner => 0,
- Person => undef,
- @_ );
-
-
-
- #clear the watchers cache
- $self->{'watchers_cache'} = undef;
-
- if (defined $args{'Person'}) {
- #if it's an RT::User object, pull out the id and shove it in Owner
- if (ref ($args{'Person'}) =~ /RT::User/) {
- $args{'Owner'} = $args{'Person'}->id;
- }
- #if it's an int, shove it in Owner
- elsif ($args{'Person'} =~ /^\d+$/) {
- $args{'Owner'} = $args{'Person'};
- }
- #if it's an email address, shove it in Email
- else {
- $args{'Email'} = $args{'Person'};
- }
- }
-
- # Turn an email address int a watcher if we possibly can.
- if ($args{'Email'}) {
- my $watcher = new RT::User($self->CurrentUser);
- $watcher->LoadByEmail($args{'Email'});
- if ($watcher->Id) {
- $args{'Owner'} = $watcher->Id;
- delete $args{'Email'};
- }
- }
-
-
- # see if this user is already a watcher. if we have an owner, check it
- # otherwise, we've got an email-address watcher. use that.
-
- if ($self->IsWatcher(Type => $args{'Type'},
- Id => ($args{'Owner'} || $args{'Email'}) ) ) {
-
-
- return(0, 'That user is already that sort of watcher for this ticket');
- }
-
-
- require RT::Watcher;
- my $Watcher = new RT::Watcher ($self->CurrentUser);
- my ($retval, $msg) = ($Watcher->Create( Value => $self->Id,
- Scope => 'Ticket',
- Email => $args{'Email'},
- Type => $args{'Type'},
- Owner => $args{'Owner'},
- ));
-
- unless ($args{'Silent'}) {
- $self->_NewTransaction( Type => 'AddWatcher',
- NewValue => $Watcher->Email,
- Field => $Watcher->Type);
- }
-
- return ($retval, $msg);
-}
-
-# }}}
-
-# {{{ sub AddRequestor
-
-=head2 AddRequestor
-
-AddRequestor takes what AddWatcher does, except it presets
-the "Type" parameter to \'Requestor\'
-
-=cut
-
-sub AddRequestor {
- my $self = shift;
- return ($self->AddWatcher ( Type => 'Requestor', @_));
-}
-
-# }}}
-
-# {{{ sub AddCc
-
-=head2 AddCc
-
-AddCc takes what AddWatcher does, except it presets
-the "Type" parameter to \'Cc\'
-
-=cut
-
-sub AddCc {
- my $self = shift;
- return ($self->AddWatcher ( Type => 'Cc', @_));
-}
-# }}}
-
-# {{{ sub AddAdminCc
-
-=head2 AddAdminCc
-
-AddAdminCc takes what AddWatcher does, except it presets
-the "Type" parameter to \'AdminCc\'
-
-=cut
-
-sub AddAdminCc {
- my $self = shift;
- return ($self->AddWatcher ( Type => 'AdminCc', @_));
-}
-
-# }}}
-
-# }}}
-
-# {{{ sub DeleteWatcher
-
-=head2 DeleteWatcher id [type]
-
-DeleteWatcher takes a single argument which is either an email address
-or a watcher id.
-If the first argument is an email address, you need to specify the watcher type you're talking
-about as the second argument. Valid values are 'Requestor', 'Cc' or 'AdminCc'.
-It removes that watcher from this Ticket\'s list of watchers.
-
-
-=cut