AddAttachmentLinksToMail

From Request Tracker Wiki
Revision as of 06:09, 4 March 2019 by Phanousk (talk | contribs)
Jump to navigation Jump to search

This code adds list of links to files which was attached to the transaction template runs on, this mean only attachments that was attached to the current transaction, Add code into Template.

<pre>
 { my $res;
   my $Attachments = $Transaction-&gt;Attachments;
   $Attachments-&gt;Limit( FIELD =&gt; 'Filename', OPERATOR =&gt; '!=', VALUE =&gt; '' );
   while (my $a = $Attachments-&gt;Next) {
     $res .= "New attachments:\n" unless ($res);
     $res .= "  ". $RT::WebURL ."/Ticket/Attachment/". $a-&gt;TransactionId ."/". $a-&gt;id ."/". $a-&gt;Filename;
   }
   #Workaround to prevent possible commit death
   delete($Transaction-&gt;{attachments});
   $res;
 }
 
 </pre>

Or for a more verbose version to match the other headers (Jerrad):

<pre>
 { my $res;
   my $Attachments = $Transaction-&gt;Attachments;
   $Attachments-&gt;Limit( FIELD =&gt; 'Filename', OPERATOR =&gt; '!=', VALUE =&gt; '' );
   while (my $a = $Attachments-&gt;Next) {
     $res .= $RT::WebURL ."/Ticket/Attachment/". $a-&gt;TransactionId ."/". $a-&gt;id ."/". $a-&gt;Filename;
     $res .= "\n              ";
   }
   #Workaround to prevent possible commit death
   delete($Transaction-&gt;{attachments});
   $res = length($res) ? ' Attachments: ' . $res : '';
 }
 
 </pre>

To add links to all ticket's attachments use next Template snippet (works only with RT-3.4 and greater):

<pre>
 { my $res;
   my $Attachments = RT::Attachments-&gt;new( $Transaction-&gt;CurrentUser );
   $Attachments-&gt;Columns( qw(id TransactionId Filename ContentType Created));
   my $transactions = $Attachments-&gt;NewAlias('Transactions');
   $Attachments-&gt;Join(
       ALIAS1 =&gt; 'main',
       FIELD1 =&gt; 'TransactionId',
       ALIAS2 =&gt; $transactions,
       FIELD2 =&gt; 'id'
   );
   my $tickets = $Attachments-&gt;NewAlias('Tickets');
   $Attachments-&gt;Join(
       ALIAS1 =&gt; $transactions,
       FIELD1 =&gt; 'ObjectId',
       ALIAS2 =&gt; $tickets,
       FIELD2 =&gt; 'id'
   );
   $Attachments-&gt;Limit(
       ALIAS =&gt; $transactions,
       FIELD =&gt; 'ObjectType',
       VALUE =&gt; 'RT::Ticket'
   );
   $Attachments-&gt;Limit(
       ALIAS =&gt; $tickets,
       FIELD =&gt; 'EffectiveId',
       VALUE =&gt; $Ticket-&gt;id
   );
   $Attachments-&gt;Limit( FIELD =&gt; 'Filename', OPERATOR =&gt; '!=', VALUE =&gt; '' );
   while (my $a = $Attachments-&gt;Next) {
     $res .= "Ticket's attachments:\n" unless ($res);
     $res .= "  ". $RT::WebURL ."/Ticket/Attachment/". $a-&gt;TransactionId ."/". $a-&gt;id ."/". $a-&gt;Filename;
   }
   $res;
 }
 </pre>