Fixed charset + runaway <div>s
[usit-rt.git] / local / html / Ticket / Export.html
1 %## Default elements from RT. Header and the ticket elements. Both this and the 
2 %## html printed after the <%INIT>
3 <& /Elements/Header, 
4     Title => loc("#[_1]: [_2]", $TicketObj->Id, $TicketObj->Subject),
5     LinkRel => \%link_rel &>
6 <& /Elements/Tabs &> 
7
8 %## The html with %-prefixed perl code.
9 <div class="history">
10 <div id="ticket-history">
11 <div class="titlebox" id="">
12 <div class="titlebox-title">
13 <span class="left">Ticket to export</span>
14 <br><br>
15 <span>
16 <form action="Export.html" method="post">
17 %print "<input type=\"text\" name=\"email\" size=\"40\" value=\"".$user->EmailAddress."\"/>";
18 %print "<input type=\"hidden\" name=\"id\" value=\"".$TicketObj->Id."\"/>";
19 <br>
20 <input type="submit" value="Send email"/>
21 </span>
22 <div class="ticket-transaction message odd">
23 <pre style="white-space: -moz-pre-wrap;white-space: -pre-wrap;white-space: -o-pre-wrap;white-space: pre-wrap;word-wrap: break-word;">
24 %print "$mail_head";
25 %print "$mail";
26 </pre>
27 </div>
28 </div>
29 </div>
30 </div>
31 </div>
32
33 <%ARGS>
34 $id => undef
35 $TicketObj => undef
36 $email => undef
37 </%ARGS>
38
39 <%INIT>
40 my (@Actions, $Tickets);  
41
42
43 unless ($id || $TicketObj) {
44     Abort('No ticket specified');
45 }
46
47 $TicketObj ||= LoadTicket($ARGS{'id'});
48
49 unless ($TicketObj->CurrentUserHasRight('ShowTicket')) {
50     Abort("No permission to view ticket");
51 }
52
53 # used to convert html-formated email -> plain text for the body.
54 use HTML::Parse;
55 use HTML::FormatText;
56 use Encode;
57 my $formater = HTML::FormatText->new(leftmargin => 0, rightmargin => 75);
58
59 # using buildtin methods to get the attachments - all nice and cronological.
60 my $attachments = $m->comp('Elements/FindAttachments', Ticket => $TicketObj, Tickets => $Tickets);
61 my $attachment_content = $m->comp('Elements/LoadTextAttachments', Ticket => $TicketObj);
62
63 #Get some information.
64 my $queue = RT::Queue->new($RT::SystemUser);
65 $queue->Load($TicketObj->Queue);
66 my $owner = RT::User->new($RT::SystemUser);
67 $owner->Load($TicketObj->Owner);
68 my $user = RT::User->new($RT::SystemUser);
69 $user->Load($TicketObj->CurrentUser);
70 my $requestor = RT::User->new($RT::SystemUser);
71 my $people = $TicketObj->Requestors->UserMembersObj;
72 my @requestors;
73 while ( my $r = $people->Next ) {
74 push @requestors, $m->scomp('/Elements/ShowUserConcise', User => $r);
75 }
76 my $requestors = join ("\n              ", @requestors);
77
78
79 my $Transactions = $TicketObj->Transactions;
80
81 my @attachments = @{$attachments->ItemsArrayRef()};
82 my @attachment_content = @{$attachment_content->ItemsArrayRef()};
83 my $mail;
84 my $mailsendt=0;
85 my $attach=0;
86
87 my $resolved = "";
88 my $created;
89 my $mail_head;
90
91 # Find the last time the ticket was resolved.
92
93 #### NEED FIX HERE ###
94
95 while ( my $Transaction = $Transactions->Next ) {
96 if ($Transaction->Description =~ /Status changed from \'.*\' to \'resolved\'/) {
97     $resolved = $Transaction->Description." - ".
98                 $Transaction->CreatorObj->RealName." on ".
99                 $Transaction->CreatedAsString()."\n";
100     $resolved =~ s/Status changed from \'.*\' to \'resolved\' /Resolved:     /;
101 }
102
103 # Ignoring comments/statuschange/etc.
104 next if $Transaction->Description !~ /(Correspondence added|Ticket created )/;
105
106 # Get all the transactions:
107 my $Attach = $Transaction->Attachments->ItemsArrayRef();
108 next if ! defined $Attach->[0]; 
109 $mail .= $Transaction->BriefDescription." by ".$Transaction->CreatorObj->Name." - ".
110          $Transaction->CreatorObj->RealName."\n";
111 $mail .= "Date: ".$Transaction->CreatedAsString()." =================================================>\n\n";
112 $created = $Transaction->CreatedAsString() unless $created;
113 for (@$Attach) {
114         if ($_->ContentType =~ /text\/plain/ && not $_->Filename) {$mail .= $_->Content."\n\n"}
115         if ($_->ContentType =~ /text\/html/  && not $_->Filename) {
116         $mail .= $formater->format(parse_html("<div>".$_->Content."</div>"))."\n\n"
117         }
118
119 # Finding the mail attachments that will be attached to the final email.
120         if ($_->Filename) {$mail .= "Attatchment: ".$_->Filename."\n\n";$attach=1;}
121 }
122
123 }
124 # Putting together the metadata. Some posibilities are commented.
125 # The ticket can have a created after the first transaction if an older ticket
126 # is merged into a newer ticket, so we use the creation-time of the first
127 # transaction.
128
129     $mail_head .= "============================================================================\n";
130 #   $mail_head .= "Created Date:\t".$TicketObj->CreatedAsString."\n";
131     $mail_head .= "Created Date: $created\n";
132     $mail_head .= "Requestor(s): $requestors\n";
133 #    $mail_head .= "Requestor(s): ".$TicketObj->Requestors->MemberEmailAddressesAsString."\n".$requestors;
134 #    $mail_head .= "Cc:           $TicketObj->Cc\n" if $TicketObj->Cc;
135 #    $mail_head .= "X-AdminCc:    $TicketObj->AdminCc\n" if $TicketObj->AdminCc;
136     $mail_head .= "X-Queue:      ".$queue->Name."\n";
137     $mail_head .= "Owner:        ".$owner->Name." - ".$owner->RealName."\n";
138     $mail_head .= "Subject:      [rt #".$TicketObj->id."] ".$TicketObj->Subject."\n";
139     $mail_head .= $resolved;
140     $mail_head .= "============================================================================\n\n";
141
142 # Building an mime-email if we are to send an email.
143 if ($email){
144     my $mail = MIME::Entity->build(
145         Type => 'text/plain',
146         Charset => 'utf-8',
147         Data => encode("utf8", $mail_head."\n".$mail),
148     );
149
150     $mail->head->set(To=>RT::Interface::Email::EncodeToMIME(String => $email));
151     $mail->head->set(From=>RT::Interface::Email::EncodeToMIME(String => $user->EmailAddress));
152     $mail->head->set(Subject=>RT::Interface::Email::EncodeToMIME(String => "Fwd: [rt #".$TicketObj->id."] ".$TicketObj->Subject));
153    
154 # We could have been smart and done this part when getting the other 
155 # transactions, but we are not smart. 
156
157     if ($attach){
158         while ( my $Transaction = $Transactions->Next ) {
159             my $Attach = $Transaction->Attachments->ItemsArrayRef();
160             next if ! defined $Attach->[0];
161             for (@$Attach) {
162                 if ($_->Filename){
163                     $mail->attach(
164                         Type => $_->ContentType,
165                         Disposition => 'attachment',
166                         Filename => $_->Filename,
167                         Description => $_->Filename,
168                         Data => $_->Content,
169                     );
170                 }
171             }
172         }
173     }
174
175         RT::I18N::SetMIMEEntityToEncoding( $mail, RT->Config->Get('EmailOutputEncoding'), 'mime_words_ok', );
176
177     RT::Interface::Email::SendEmail( Entity => $mail );
178
179 # Marking the sending of an email with a transaction in the ticket.
180 # This should be something a little smarter, but that requires a change of
181 # lib/RT/Transactions_Overlay.pm
182
183     my $trans = RT::Transaction->new( $TicketObj->CurrentUser );
184     $trans->Create(
185             Ticket      => $TicketObj->Id,
186             Type        => 'Export',
187             Field       => "Email",
188             OldValue    => "Email",
189             NewValue    => "$email",
190             ActivateScrips => 0
191         );
192
193 #Redirecting back to the ticket.
194
195         RT::Interface::Web::Redirect( RT->Config->Get('WebURL') . "Ticket/Display.html?id=" . $TicketObj->id );
196     
197     $mailsendt=1;
198 }
199
200 my %link_rel;
201 </%INIT>