+=item get_ticket SESSION_HASHREF, OPTION => VALUE ...
+
+Class method. Retrieves a ticket. If there is an error, returns the scalar
+error. Otherwise, currently returns a slightly tricky data structure containing
+the ticket's attributes, a list of the linked customers, each transaction's
+content, description, and create time.
+
+Accepts the following options:
+
+=over 4
+
+=item ticket_id
+
+The ticket id
+
+=back
+
+=cut
+
+sub get_ticket {
+ my($self, $session, %param) = @_;
+
+ $session = $self->session($session);
+
+ my $Ticket = RT::Ticket->new($session->{'CurrentUser'});
+ my $ticketid = $Ticket->Load( $param{'ticket_id'} );
+ return 'Could not load ticket' unless $ticketid;
+
+ my @custs = ();
+ foreach my $link ( @{ $Ticket->Customers->ItemsArrayRef } ) {
+ my $cust = $link->Target;
+ push @custs, $1 if $cust =~ /\/(\d+)$/;
+ }
+
+ my @txns = ();
+ my $transactions = $Ticket->Transactions;
+ while ( my $transaction = $transactions->Next ) {
+ my $t = { created => $transaction->Created,
+ content => $transaction->Content,
+ description => $transaction->Description,
+ type => $transaction->Type,
+ };
+ push @txns, $t;
+ }
+
+ { txns => [ @txns ],
+ custs => [ @custs ],
+ fields => _ticket_info($Ticket),
+ };
+}
+
+=item get_ticket_object SESSION_HASHREF, OPTION => VALUE...
+
+Class method. Retrieve the RT::Ticket object with the specified
+ticket_id. If custnum is supplied, will also check that the object
+is a member of that customer. If there is no ticket or the custnum
+check fails, returns nothing. The meaning of that case is
+"to this customer, the ticket does not exist".
+
+Options:
+
+=over 4
+
+=item ticket_id
+
+=item custnum
+
+=back
+
+=cut
+
+sub get_ticket_object {
+ my $self = shift;
+ my ($session, %opt) = @_;
+ $session = $self->session(shift);
+ my $Ticket = RT::Ticket->new($session->{CurrentUser});
+ $Ticket->Load($opt{'ticket_id'});
+ return if ( !$Ticket->id );
+ my $custnum = $opt{'custnum'};
+ if ( defined($custnum) && $custnum =~ /^\d+$/ ) {
+ # probably the most efficient way to check ticket ownership
+ my $Link = RT::Link->new($session->{CurrentUser});
+ $Link->LoadByCols( LocalBase => $opt{'ticket_id'},
+ Type => 'MemberOf',
+ Target => "freeside://freeside/cust_main/$custnum",
+ );
+ return if ( !$Link->id );
+ }
+ return $Ticket;
+}
+
+
+=item correspond_ticket SESSION_HASHREF, OPTION => VALUE ...
+
+Class method. Correspond on a ticket. If there is an error, returns the scalar
+error. Otherwise, returns the transaction id, error message, and
+RT::Transaction object.
+
+Accepts the following options:
+
+=over 4
+
+=item ticket_id
+
+The ticket id
+
+=item content
+
+Correspondence content
+
+=back
+
+=cut
+
+sub correspond_ticket {
+ my($self, $session, %param) = @_;
+
+ $session = $self->session($session);
+
+ my $Ticket = RT::Ticket->new($session->{'CurrentUser'});
+ my $ticketid = $Ticket->Load( $param{'ticket_id'} );
+ return 'Could not load ticket' unless $ticketid;
+ return 'No content' unless $param{'content'};
+
+ $Ticket->Correspond( Content => $param{'content'} );
+}
+
+=item queues SESSION_HASHREF [, ACL ]
+
+Retrieve a list of queues. Pass the name of an RT access control right,
+such as 'CreateTicket', to return only queues on which the current user
+has that right. Otherwise this will return all queues with the 'SeeQueue'
+right.
+
+=cut
+
+sub queues {
+ my( $self, $session, $acl ) = @_;
+ $session = $self->session($session);
+
+ my $showall = $acl ? 0 : 1;
+ my @result = ();
+ my $q = new RT::Queues($session->{'CurrentUser'});
+ $q->UnLimit;
+ while (my $queue = $q->Next) {
+ if ($showall || $queue->CurrentUserHasRight($acl)) {
+ push @result, {
+ Id => $queue->Id,
+ Name => $queue->Name,
+ Description => $queue->Description,
+ };
+ }
+ }
+ return map { $_->{Id} => $_->{Name} } @result;
+}
+