summaryrefslogtreecommitdiff
path: root/rt/lib/RT/Transaction.pm
blob: 1801fdcbe4b1f80ff646855d5393a98c8c2640dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
# BEGIN BPS TAGGED BLOCK {{{
#
# COPYRIGHT:
#
# This software is Copyright (c) 1996-2018 Best Practical Solutions, LLC
#                                          <sales@bestpractical.com>
#
# (Except where explicitly superseded by other copyright notices)
#
#
# LICENSE:
#
# This work is made available to you under the terms of Version 2 of
# the GNU General Public License. A copy of that license should have
# been provided with this software, but in any event can be snarfed
# from www.gnu.org.
#
# This work is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 or visit their web page on the internet at
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
#
#
# CONTRIBUTION SUBMISSION POLICY:
#
# (The following paragraph is not intended to limit the rights granted
# to you to modify and distribute this software under the terms of
# the GNU General Public License and is only of importance to you if
# you choose to contribute your changes and enhancements to the
# community by submitting them to Best Practical Solutions, LLC.)
#
# By intentionally submitting any modifications, corrections or
# derivatives to this work, or any other work intended for use with
# Request Tracker, to Best Practical Solutions, LLC, you confirm that
# you are the copyright holder for those contributions and you grant
# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
# royalty-free, perpetual, license to use, copy, create derivative
# works based on those contributions, and sublicense and distribute
# those contributions and any derivatives thereof.
#
# END BPS TAGGED BLOCK }}}

=head1 NAME

  RT::Transaction - RT's transaction object

=head1 SYNOPSIS

  use RT::Transaction;


=head1 DESCRIPTION


Each RT::Transaction describes an atomic change to a ticket object 
or an update to an RT::Ticket object.
It can have arbitrary MIME attachments.


=head1 METHODS


=cut


package RT::Transaction;

use base 'RT::Record';
use strict;
use warnings;


use vars qw( %_BriefDescriptions $PreferredContentType );

use RT::Attachments;
use RT::Scrips;
use RT::Ruleset;

use HTML::FormatText::WithLinks::AndTables;
use HTML::Scrubber;

# For EscapeHTML() and decode_entities()
require RT::Interface::Web;
require HTML::Entities;

sub Table {'Transactions'}

# {{{ sub Create 

=head2 Create

Create a new transaction.

This routine should _never_ be called by anything other than RT::Ticket. 
It should not be called 
from client code. Ever. Not ever.  If you do this, we will hunt you down and break your kneecaps.
Then the unpleasant stuff will start.

TODO: Document what gets passed to this

=cut

sub Create {
    my $self = shift;
    my %args = (
        id             => undef,
        TimeTaken      => 0,
        Type           => 'undefined',
        Data           => '',
        Field          => undef,
        OldValue       => undef,
        NewValue       => undef,
        MIMEObj        => undef,
        ActivateScrips => 1,
        CommitScrips   => 1,
        ObjectType     => 'RT::Ticket',
        ObjectId       => 0,
        ReferenceType  => undef,
        OldReference   => undef,
        NewReference   => undef,
        SquelchMailTo  => undef,
        CustomFields   => {},
        @_
    );

    $args{ObjectId} ||= $args{Ticket};

    #if we didn't specify a ticket, we need to bail
    unless ( $args{'ObjectId'} && $args{'ObjectType'}) {
        return ( 0, $self->loc( "Transaction->Create couldn't, as you didn't specify an object type and id"));
    }

    #lets create our transaction
    my %params = (
        Type      => $args{'Type'},
        Data      => $args{'Data'},
        Field     => $args{'Field'},
        OldValue  => $args{'OldValue'},
        NewValue  => $args{'NewValue'},
        Created   => $args{'Created'},
        ObjectType => $args{'ObjectType'},
        ObjectId => $args{'ObjectId'},
        ReferenceType => $args{'ReferenceType'},
        OldReference => $args{'OldReference'},
        NewReference => $args{'NewReference'},
    );

    # Parameters passed in during an import that we probably don't want to touch, otherwise
    foreach my $attr (qw(id Creator Created LastUpdated TimeTaken LastUpdatedBy)) {
        $params{$attr} = $args{$attr} if ($args{$attr});
    }
 
    my $id = $self->SUPER::Create(%params);
    $self->Load($id);
    if ( defined $args{'MIMEObj'} ) {
        my ($id, $msg) = $self->_Attach( $args{'MIMEObj'} );
        unless ( $id ) {
            $RT::Logger->error("Couldn't add attachment: $msg");
            return ( 0, $self->loc("Couldn't add attachment") );
        }
    }

    # Set up any custom fields passed at creation.  Has to happen 
    # before scrips.
    
    $self->UpdateCustomFields(%{ $args{'CustomFields'} });

    $self->AddAttribute(
        Name    => 'SquelchMailTo',
        Content => RT::User->CanonicalizeEmailAddress($_)
    ) for @{$args{'SquelchMailTo'} || []};

    my @return = ( $id, $self->loc("Transaction Created") );

    return @return unless $args{'ObjectType'} eq 'RT::Ticket';

    # Provide a way to turn off scrips if we need to
    unless ( $args{'ActivateScrips'} ) {
        $RT::Logger->debug('Skipping scrips for transaction #' .$self->Id);
        return @return;
    }

    $self->{'scrips'} = RT::Scrips->new(RT->SystemUser);

    $RT::Logger->debug('About to prepare scrips for transaction #' .$self->Id); 

    $self->{'scrips'}->Prepare(
        Stage       => 'TransactionCreate',
        Type        => $args{'Type'},
        Ticket      => $args{'ObjectId'},
        Transaction => $self->id,
    );

   # Entry point of the rule system
   my $ticket = RT::Ticket->new(RT->SystemUser);
   $ticket->Load($args{'ObjectId'});
   my $txn = RT::Transaction->new($RT::SystemUser);
   $txn->Load($self->id);

   my $rules = $self->{rules} = RT::Ruleset->FindAllRules(
        Stage       => 'TransactionCreate',
        Type        => $args{'Type'},
        TicketObj   => $ticket,
        TransactionObj => $txn,
   );

    if ($args{'CommitScrips'} ) {
        $RT::Logger->debug('About to commit scrips for transaction #' .$self->Id);
        $self->{'scrips'}->Commit();
        RT::Ruleset->CommitRules($rules);
    }

    return @return;
}


=head2 Scrips

Returns the Scrips object for this transaction.
This routine is only useful on a freshly created transaction object.
Scrips do not get persisted to the database with transactions.


=cut


sub Scrips {
    my $self = shift;
    return($self->{'scrips'});
}


=head2 Rules

Returns the array of Rule objects for this transaction.
This routine is only useful on a freshly created transaction object.
Rules do not get persisted to the database with transactions.


=cut


sub Rules {
    my $self = shift;
    return($self->{'rules'});
}



=head2 Delete

Delete this transaction. Currently DOES NOT CHECK ACLS

=cut

sub Delete {
    my $self = shift;


    $RT::Handle->BeginTransaction();

    my $attachments = $self->Attachments;

    while (my $attachment = $attachments->Next) {
        my ($id, $msg) = $attachment->Delete();
        unless ($id) {
            $RT::Handle->Rollback();
            return($id, $self->loc("System Error: [_1]", $msg));
        }
    }
    my ($id,$msg) = $self->SUPER::Delete();
        unless ($id) {
            $RT::Handle->Rollback();
            return($id, $self->loc("System Error: [_1]", $msg));
        }
    $RT::Handle->Commit();
    return ($id,$msg);
}




=head2 Message

Returns the L<RT::Attachments> object which contains the "top-level" object
attachment for this transaction.

=cut

sub Message {
    my $self = shift;

    # XXX: Where is ACL check?
    
    unless ( defined $self->{'message'} ) {

        $self->{'message'} = RT::Attachments->new( $self->CurrentUser );
        $self->{'message'}->Limit(
            FIELD => 'TransactionId',
            VALUE => $self->Id
        );
        $self->{'message'}->ChildrenOf(0);
    } else {
        $self->{'message'}->GotoFirstItem;
    }
    return $self->{'message'};
}



=head2 HasContent

Returns whether this transaction has attached mime objects.

=cut

sub HasContent {
    my $self = shift;
    my $type = $PreferredContentType || '';
    return !!$self->ContentObj( $type ? ( Type => $type) : () );
}



=head2 Content PARAMHASH

If this transaction has attached mime objects, returns the body of the first
textual part (as defined in RT::I18N::IsTextualContentType).  Otherwise,
returns the message "This transaction appears to have no content".

Takes a paramhash.  If the $args{'Quote'} parameter is set, wraps this message 
at $args{'Wrap'}.  $args{'Wrap'} defaults to $RT::MessageBoxWidth - 2 or 70.

If $args{'Type'} is set to C<text/html>, this will return an HTML 
part of the message, if available.  Otherwise it looks for a text/plain
part. If $args{'Type'} is missing, it defaults to the value of 
C<$RT::Transaction::PreferredContentType>, if that's missing too, 
defaults to textual.

=cut

sub Content {
    my $self = shift;
    my %args = (
        Type => $PreferredContentType || '',
        Quote => 0,
        Wrap  => 70,
        Wrap  => ( $RT::MessageBoxWidth || 72 ) - 2,
        @_
    );

    my $content;
    if ( my $content_obj = 
        $self->ContentObj( $args{Type} ? ( Type => $args{Type}) : () ) )
    {
        $content = $content_obj->Content ||'';

        if ( lc $content_obj->ContentType eq 'text/html' ) {
            $content =~ s/(?:(<\/div>)|<p>|<br\s*\/?>|<div(\s+class="[^"]+")?>)\s*--\s+<br\s*\/?>.*?$/$1/s if $args{'Quote'};

            if ($args{Type} ne 'text/html') {
                $content = RT::Interface::Email::ConvertHTMLToText($content);
            } else {
                # Scrub out <html>, <head>, <meta>, and <body>, and
                # leave all else untouched.
                my $scrubber = HTML::Scrubber->new();
                $scrubber->rules(
                    html => 0,
                    head => 0,
                    meta => 0,
                    body => 0,
                );
                $scrubber->default( 1 => { '*' => 1 } );
                $content = $scrubber->scrub( $content );
            }
        }
        else {
            $content =~ s/\n-- \n.*?$//s if $args{'Quote'};
            if ($args{Type} eq 'text/html') {
                # Extremely simple text->html converter
                $content =~ s/&/&#38;/g;
                $content =~ s/</&lt;/g;
                $content =~ s/>/&gt;/g;
                $content = qq|<pre style="white-space: pre-wrap; font-family: monospace;">$content</pre>|;
            }
        }
    }

    # If all else fails, return a message that we couldn't find any content
    else {
        $content = $self->loc('This transaction appears to have no content');
    }

    if ( $args{'Quote'} ) {
        if ($args{Type} eq 'text/html') {
            $content = '<div class="gmail_quote">'
                . $self->QuoteHeader
                . '<br /><blockquote class="gmail_quote" type="cite">'
                . $content
                . '</blockquote></div><br /><br />';
        } else {
            $content = $self->ApplyQuoteWrap(content => $content,
                                             cols    => $args{'Wrap'} );

            $content = $self->QuoteHeader . "\n$content\n\n";
        }
    }

    return ($content);
}

=head2 QuoteHeader

Returns text prepended to content when transaction is quoted
(see C<Quote> argument in L</Content>). By default returns
localized "On <date> <user name> wrote:\n".

=cut

sub QuoteHeader {
    my $self = shift;
    return $self->loc("On [_1], [_2] wrote:", $self->CreatedAsString, $self->CreatorObj->Name);
}

=head2 ApplyQuoteWrap PARAMHASH

Wrapper to calculate wrap criteria and apply quote wrapping if needed.

=cut

sub ApplyQuoteWrap {
    my $self = shift;
    my %args = @_;
    my $content = $args{content};

    # What's the longest line like?
    my $max = 0;
    foreach ( split ( /\n/, $args{content} ) ) {
        $max = length if length > $max;
    }

    if ( $max > 76 ) {
        require Text::Quoted;
        require Text::Wrapper;

        my $structure = Text::Quoted::extract($args{content});
        $content = $self->QuoteWrap(content_ref => $structure,
                                    cols        => $args{cols},
                                    max         => $max );
    }

    $content =~ s/^/> /gm;  # use regex since string might be multi-line
    return $content;
}

=head2 QuoteWrap PARAMHASH

Wrap the contents of transactions based on Wrap settings, maintaining
the quote character from the original.

=cut

sub QuoteWrap {
    my $self = shift;
    my %args = @_;
    my $ref = $args{content_ref};
    my $final_string;

    if ( ref $ref eq 'ARRAY' ){
        foreach my $array (@$ref){
            $final_string .= $self->QuoteWrap(content_ref => $array,
                                              cols        => $args{cols},
                                              max         => $args{max} );
        }
    }
    elsif ( ref $ref eq 'HASH' ){
        return $ref->{quoter} . "\n" if $ref->{empty}; # Blank line

        my $col = $args{cols} - (length $ref->{quoter});
        my $wrapper = Text::Wrapper->new( columns => $col );

        # Wrap on individual lines to honor incoming line breaks
        # Otherwise deliberate separate lines (like a list or a sig)
        # all get combined incorrectly into single paragraphs.

        my @lines = split /\n/, $ref->{text};
        my $wrap = join '', map { $wrapper->wrap($_) } @lines;
        my $quoter = $ref->{quoter};

        # Only add the space if actually quoting
        $quoter .= ' ' if length $quoter;
        $wrap =~ s/^/$quoter/mg;  # use regex since string might be multi-line

        return $wrap;
    }
    else{
        $RT::Logger->warning("Can't apply quoting with $ref");
        return;
    }
    return $final_string;
}


=head2 Addresses

Returns a hashref of addresses related to this transaction. See L<RT::Attachment/Addresses> for details.

=cut

sub Addresses {
    my $self = shift;

    if (my $attach = $self->Attachments->First) {
        return $attach->Addresses;
    }
    else {
        return {};
    }

}



=head2 ContentObj 

Returns the RT::Attachment object which contains the content for this Transaction

=cut


sub ContentObj {
    my $self = shift;
    my %args = ( Type => $PreferredContentType, Attachment => undef, @_ );

    # If we don't have any content, return undef now.
    # Get the set of toplevel attachments to this transaction.

    my $Attachment = $args{'Attachment'};

    $Attachment ||= $self->Attachments->First;

    return undef unless ($Attachment);

    my $Attachments = $self->Attachments;
    while ( my $Attachment = $Attachments->Next ) {
        if ( my $content = _FindPreferredContentObj( %args, Attachment => $Attachment ) ) {
            return $content;
        }
    }

    # If that fails, return the first top-level textual part which has some content.
    # We probably really want this to become "recurse, looking for the other type of
    # displayable".  For now, this maintains backcompat
    my $all_parts = $self->Attachments;
    while ( my $part = $all_parts->Next ) {
        next unless _IsDisplayableTextualContentType($part->ContentType)
        && $part->Content;
        return $part;
    }

    return;
}


sub _FindPreferredContentObj {
    my %args = @_;
    my $Attachment = $args{Attachment};

    # If we don't have any content, return undef now.
    return undef unless $Attachment;

    # If it's a textual part, just return the body.
    if ( _IsDisplayableTextualContentType($Attachment->ContentType) ) {
        return ($Attachment);
    }

    # If it's a multipart object, first try returning the first part with preferred
    # MIME type ('text/plain' by default).

    elsif ( $Attachment->ContentType =~ m|^multipart/mixed|i ) {
        my $kids = $Attachment->Children;
        while (my $child = $kids->Next) {
            my $ret =  _FindPreferredContentObj(%args, Attachment => $child);
            return $ret if ($ret);
        }
    }
    elsif ( $Attachment->ContentType =~ m|^multipart/|i ) {
        if ( $args{Type} ) {
            my $plain_parts = $Attachment->Children;
            $plain_parts->ContentType( VALUE => $args{Type} );
            $plain_parts->LimitNotEmpty;

            # If we actully found a part, return its content
            if ( my $first = $plain_parts->First ) {
                return $first;
            }
        } else {
            my $parts = $Attachment->Children;
            $parts->LimitNotEmpty;

            # If we actully found a part, return its content
            while (my $part = $parts->Next) {
                next unless _IsDisplayableTextualContentType($part->ContentType);
                return $part;
            }

        }
    }

    # If this is a message/rfc822 mail, we need to dig into it in order to find 
    # the actual textual content

    elsif ( $Attachment->ContentType =~ '^message/rfc822' ) {
        my $children = $Attachment->Children;
        while ( my $child = $children->Next ) {
            if ( my $content = _FindPreferredContentObj( %args, Attachment => $child ) ) {
                return $content;
            }
        }
    }

    # We found no content. suck
    return (undef);
}

=head2 _IsDisplayableTextualContentType

We may need to pull this out to another module later, but for now, this
is better than RT::I18N::IsTextualContentType because that believes that
a message/rfc822 email is displayable, despite it having no content

=cut

sub _IsDisplayableTextualContentType {
    my $type = shift;
    ($type =~ m{^text/(?:plain|html)\b}i) ? 1 : 0;
}


=head2 Subject

If this transaction has attached mime objects, returns the first one's subject
Otherwise, returns null
  
=cut

sub Subject {
    my $self = shift;
    return undef unless my $first = $self->Attachments->First;
    return $first->Subject;
}



=head2 Attachments

Returns all the RT::Attachment objects which are attached
to this transaction. Takes an optional parameter, which is
a ContentType that Attachments should be restricted to.

=cut

sub Attachments {
    my $self = shift;

    if ( $self->{'attachments'} ) {
        $self->{'attachments'}->GotoFirstItem;
        return $self->{'attachments'};
    }

    $self->{'attachments'} = RT::Attachments->new( $self->CurrentUser );

    unless ( $self->CurrentUserCanSee ) {
        $self->{'attachments'}->Limit(FIELD => 'id', VALUE => '0', SUBCLAUSE => 'acl');
        return $self->{'attachments'};
    }

    $self->{'attachments'}->Limit( FIELD => 'TransactionId', VALUE => $self->Id );

    # Get the self->{'attachments'} in the order they're put into
    # the database.  Arguably, we should be returning a tree
    # of self->{'attachments'}, not a set...but no current app seems to need
    # it.

    $self->{'attachments'}->OrderBy( FIELD => 'id', ORDER => 'ASC' );

    return $self->{'attachments'};
}



=head2 _Attach

A private method used to attach a mime object to this transaction.

=cut

sub _Attach {
    my $self       = shift;
    my $MIMEObject = shift;

    unless ( defined $MIMEObject ) {
        $RT::Logger->error("We can't attach a mime object if you don't give us one.");
        return ( 0, $self->loc("[_1]: no attachment specified", $self) );
    }

    my $Attachment = RT::Attachment->new( $self->CurrentUser );
    my ($id, $msg) = $Attachment->Create(
        TransactionId => $self->Id,
        Attachment    => $MIMEObject
    );
    return ( $Attachment, $msg || $self->loc("Attachment created") );
}



sub ContentAsMIME {
    my $self = shift;

    # RT::Attachments doesn't limit ACLs as strictly as RT::Transaction does
    # since it has less information available without looking to it's parent
    # transaction.  Check ACLs here before we go any further.
    return unless $self->CurrentUserCanSee;

    my $attachments = RT::Attachments->new( $self->CurrentUser );
    $attachments->OrderBy( FIELD => 'id', ORDER => 'ASC' );
    $attachments->Limit( FIELD => 'TransactionId', VALUE => $self->id );
    $attachments->Limit( FIELD => 'Parent',        VALUE => 0 );
    $attachments->RowsPerPage(1);

    my $top = $attachments->First;
    return unless $top;

    my $entity = MIME::Entity->build(
        Type        => 'message/rfc822',
        Description => 'transaction ' . $self->id,
        Data        => $top->ContentAsMIME(Children => 1)->as_string,
    );

    return $entity;
}



=head2 Description

Returns a text string which describes this transaction

=cut

sub Description {
    my $self = shift;

    unless ( $self->CurrentUserCanSee ) {
        return ( $self->loc("Permission Denied") );
    }

    unless ( defined $self->Type ) {
        return ( $self->loc("No transaction type specified"));
    }

    return $self->loc("[_1] by [_2]", $self->BriefDescription , $self->CreatorObj->Name );
}



=head2 BriefDescription

Returns a text string which briefly describes this transaction

=cut

{
    my $scrubber = HTML::Scrubber->new(default => 0); # deny everything

    sub BriefDescription {
        my $self = shift;
        my $desc = $self->BriefDescriptionAsHTML;
           $desc = $scrubber->scrub($desc);
           $desc = HTML::Entities::decode_entities($desc);
        return $desc;
    }
}

=head2 BriefDescriptionAsHTML

Returns an HTML string which briefly describes this transaction.

=cut

sub BriefDescriptionAsHTML {
    my $self = shift;

    unless ( $self->CurrentUserCanSee ) {
        return ( $self->loc("Permission Denied") );
    }

    my ($objecttype, $type, $field) = ($self->ObjectType, $self->Type, $self->Field);

    unless ( defined $type ) {
        return $self->loc("No transaction type specified");
    }

    my ($template, @params);

    my @code = grep { ref eq 'CODE' } map { $_BriefDescriptions{$_} }
        ( $field
            ? ("$objecttype-$type-$field", "$type-$field")
            : () ),
        "$objecttype-$type", $type;

    if (@code) {
        ($template, @params) = $code[0]->($self);
    }

    unless ($template) {
        ($template, @params) = (
            "Default: [_1]/[_2] changed from [_3] to [_4]", #loc
            $type,
            $field,
            (
                $self->OldValue
                ? "'" . $self->OldValue . "'"
                : $self->loc("(no value)")
            ),
            (
                $self->NewValue
                ? "'" . $self->NewValue . "'"
                : $self->loc("(no value)")
            ),
        );
    }
    return $self->loc($template, $self->_ProcessReturnValues(@params));
}

sub _ProcessReturnValues {
    my $self   = shift;
    my @values = @_;
    return map {
        if    (ref eq 'ARRAY')  { $_ = join "", $self->_ProcessReturnValues(@$_) }
        elsif (ref eq 'SCALAR') { $_ = $$_ }
        else                    { RT::Interface::Web::EscapeHTML(\$_) }
        $_
    } @values;
}

sub _FormatPrincipal {
    my $self = shift;
    my $principal = shift;
    if ($principal->IsUser) {
        return $self->_FormatUser( $principal->Object );
    } else {
        return $self->loc("group [_1]", $principal->Object->Name);
    }
}

sub _FormatUser {
    my $self = shift;
    my $user = shift;
    return [
        \'<span class="user" data-replace="user" data-user-id="', $user->id, \'">',
        $user->Format,
        \'</span>'
    ];
}

%_BriefDescriptions = (
    Create => sub {
        my $self = shift;
        return ( "[_1] created", $self->FriendlyObjectType );   #loc()
    },
    Enabled => sub {
        my $self = shift;
        return ( "[_1] enabled", $self->Field ? $self->loc($self->Field) : $self->FriendlyObjectType );   #loc()
    },
    Disabled => sub {
        my $self = shift;
        return ( "[_1] disabled", $self->Field ? $self->loc($self->Field) : $self->FriendlyObjectType );  #loc()
    },
    Status => sub {
        my $self = shift;
        if ( $self->Field eq 'Status' ) {
            if ( $self->NewValue eq 'deleted' ) {
                return ( "[_1] deleted", $self->FriendlyObjectType );   #loc()
            }
            else {
                my $canon = $self->Object->DOES("RT::Record::Role::Status")
                    ? sub { $self->Object->LifecycleObj->CanonicalCase(@_) }
                    : sub { return $_[0] };
                return (
                    "Status changed from [_1] to [_2]",
                    "'" . $self->loc( $canon->($self->OldValue) ) . "'",
                    "'" . $self->loc( $canon->($self->NewValue) ) . "'"
                );   # loc()
            }
        }

        # Generic:
        my $no_value = $self->loc("(no value)");
        return (
            "[_1] changed from [_2] to [_3]",
            $self->Field,
            ( $self->OldValue ? "'" . $self->OldValue . "'" : $no_value ),
            "'" . $self->NewValue . "'"
        ); #loc()
    },
    SystemError => sub {
        my $self = shift;
        return $self->Data // ("System error"); #loc()
    },
    AttachmentTruncate => sub {
        my $self = shift;
        if ( defined $self->Data ) {
            return ( "File '[_1]' truncated because its size ([_2] bytes) exceeded configured maximum size setting ([_3] bytes).",
                $self->Data, $self->OldValue, $self->NewValue ); #loc()
        }
        else {
            return ( "Content truncated because its size ([_1] bytes) exceeded configured maximum size setting ([_2] bytes).",
                $self->OldValue, $self->NewValue ); #loc()
        }
    },
    AttachmentDrop => sub {
        my $self = shift;
        if ( defined $self->Data ) {
            return ( "File '[_1]' dropped because its size ([_2] bytes) exceeded configured maximum size setting ([_3] bytes).",
                $self->Data, $self->OldValue, $self->NewValue ); #loc()
        }
        else {
            return ( "Content dropped because its size ([_1] bytes) exceeded configured maximum size setting ([_2] bytes).",
                $self->OldValue, $self->NewValue ); #loc()
        }
    },
    AttachmentError => sub {
        my $self = shift;
        if ( defined $self->Data ) {
            return ( "File '[_1]' insert failed. See error log for details.", $self->Data ); #loc()
        }
        else {
            return ( "Content insert failed. See error log for details." ); #loc()
        }
    },
    "Forward Transaction" => sub {
        my $self = shift;
        my $recipients = join ", ", map {
            RT::User->Format( Address => $_, CurrentUser => $self->CurrentUser )
        } RT::EmailParser->ParseEmailAddress($self->Data);

        return ( "Forwarded [_3]Transaction #[_1][_4] to [_2]",
            $self->Field, $recipients,
            [\'<a href="#txn-', $self->Field, \'">'], \'</a>'); #loc()
    },
    "Forward Ticket" => sub {
        my $self = shift;
        my $recipients = join ", ", map {
            RT::User->Format( Address => $_, CurrentUser => $self->CurrentUser )
        } RT::EmailParser->ParseEmailAddress($self->Data);

        return ( "Forwarded Ticket to [_1]", $recipients ); #loc()
    },
    CommentEmailRecord => sub {
        my $self = shift;
        return ("Outgoing email about a comment recorded"); #loc()
    },
    EmailRecord => sub {
        my $self = shift;
        return ("Outgoing email recorded"); #loc()
    },
    Correspond => sub {
        my $self = shift;
        return ("Correspondence added");    #loc()
    },
    Comment => sub {
        my $self = shift;
        return ("Comments added");          #loc()
    },
    CustomField => sub {
        my $self = shift;
        my $field = $self->loc('CustomField');

        my $cf;
        if ( $self->Field ) {
            $cf = RT::CustomField->new( $self->CurrentUser );
            $cf->SetContextObject( $self->Object );
            $cf->Load( $self->Field );
            $field = $cf->Name();
            $field = $self->loc('a custom field') if !defined($field);
        }

        my $new = $self->NewValue;
        my $old = $self->OldValue;

        if ( $cf ) {

            if ( $cf->Type eq 'DateTime' ) {
                if ($old) {
                    my $date = RT::Date->new( $self->CurrentUser );
                    $date->Set( Format => 'ISO', Value => $old );
                    $old = $date->AsString;
                }

                if ($new) {
                    my $date = RT::Date->new( $self->CurrentUser );
                    $date->Set( Format => 'ISO', Value => $new );
                    $new = $date->AsString;
                }
            }
            elsif ( $cf->Type eq 'Date' ) {
                if ($old) {
                    my $date = RT::Date->new( $self->CurrentUser );
                    $date->Set(
                        Format   => 'unknown',
                        Value    => $old,
                        Timezone => 'UTC',
                    );
                    $old = $date->AsString( Time => 0, Timezone => 'UTC' );
                }

                if ($new) {
                    my $date = RT::Date->new( $self->CurrentUser );
                    $date->Set(
                        Format   => 'unknown',
                        Value    => $new,
                        Timezone => 'UTC',
                    );
                    $new = $date->AsString( Time => 0, Timezone => 'UTC' );
                }
            }
        }

        if ( !defined($old) || $old eq '' ) {
            return ("[_1] [_2] added", $field, $new);   #loc()
        }
        elsif ( !defined($new) || $new eq '' ) {
            return ("[_1] [_2] deleted", $field, $old); #loc()
        }
        else {
            return ("[_1] [_2] changed to [_3]", $field, $old, $new);   #loc()
        }
    },
    Untake => sub {
        my $self = shift;
        return ("Untaken"); #loc()
    },
    Take => sub {
        my $self = shift;
        return ("Taken"); #loc()
    },
    Force => sub {
        my $self = shift;
        my $Old = RT::User->new( $self->CurrentUser );
        $Old->Load( $self->OldValue );
        my $New = RT::User->new( $self->CurrentUser );
        $New->Load( $self->NewValue );

        return ("Owner forcibly changed from [_1] to [_2]",
                map { $self->_FormatUser($_) } $Old, $New);  #loc()
    },
    Steal => sub {
        my $self = shift;
        my $Old = RT::User->new( $self->CurrentUser );
        $Old->Load( $self->OldValue );
        return ("Stolen from [_1]", $self->_FormatUser($Old));   #loc()
    },
    Give => sub {
        my $self = shift;
        my $New = RT::User->new( $self->CurrentUser );
        $New->Load( $self->NewValue );
        return ( "Given to [_1]", $self->_FormatUser($New));    #loc()
    },
    AddWatcher => sub {
        my $self = shift;
        my $principal = RT::Principal->new($self->CurrentUser);
        $principal->Load($self->NewValue);
        return ( "[_1] [_2] added", $self->loc($self->Field), $self->_FormatPrincipal($principal));    #loc()
    },
    DelWatcher => sub {
        my $self = shift;
        my $principal = RT::Principal->new($self->CurrentUser);
        $principal->Load($self->OldValue);
        return ( "[_1] [_2] deleted", $self->loc($self->Field), $self->_FormatPrincipal($principal));  #loc()
    },
    SetWatcher => sub {
        my $self = shift;
        my $principal = RT::Principal->new($self->CurrentUser);
        $principal->Load($self->NewValue);
        return ( "[_1] set to [_2]", $self->loc($self->Field), $self->_FormatPrincipal($principal));  #loc()
    },
    Subject => sub {
        my $self = shift;
        return ( "Subject changed to [_1]", $self->Data );  #loc()
    },
    AddLink => sub {
        my $self = shift;
        my $value;
        if ( $self->NewValue ) {
            my $URI = RT::URI->new( $self->CurrentUser );
            if ( $URI->FromURI( $self->NewValue ) ) {
                $value = [
                    \'<a href="', $URI->AsHREF, \'">',
                    $URI->AsString,
                    \'</a>'
                ];
            }
            else {
                $value = $self->NewValue;
            }

            if ( $self->Field eq 'DependsOn' ) {
                return ( "Dependency on [_1] added", $value );  #loc()
            }
            elsif ( $self->Field eq 'DependedOnBy' ) {
                return ( "Dependency by [_1] added", $value );  #loc()
            }
            elsif ( $self->Field eq 'RefersTo' ) {
                return ( "Reference to [_1] added", $value );   #loc()
            }
            elsif ( $self->Field eq 'ReferredToBy' ) {
                return ( "Reference by [_1] added", $value );   #loc()
            }
            elsif ( $self->Field eq 'MemberOf' ) {
                return ( "Membership in [_1] added", $value );  #loc()
            }
            elsif ( $self->Field eq 'HasMember' ) {
                return ( "Member [_1] added", $value );         #loc()
            }
            elsif ( $self->Field eq 'MergedInto' ) {
                return ( "Merged into [_1]", $value );          #loc()
            }
        }
        else {
            return ( "[_1]", $self->Data ); #loc()
        }
    },
    DeleteLink => sub {
        my $self = shift;
        my $value;
        if ( $self->OldValue ) {
            my $URI = RT::URI->new( $self->CurrentUser );
            if ( $URI->FromURI( $self->OldValue ) ) {
                $value = [
                    \'<a href="', $URI->AsHREF, \'">',
                    $URI->AsString,
                    \'</a>'
                ];
            }
            else {
                $value = $self->OldValue;
            }

            if ( $self->Field eq 'DependsOn' ) {
                return ( "Dependency on [_1] deleted", $value );    #loc()
            }
            elsif ( $self->Field eq 'DependedOnBy' ) {
                return ( "Dependency by [_1] deleted", $value );    #loc()
            }
            elsif ( $self->Field eq 'RefersTo' ) {
                return ( "Reference to [_1] deleted", $value );     #loc()
            }
            elsif ( $self->Field eq 'ReferredToBy' ) {
                return ( "Reference by [_1] deleted", $value );     #loc()
            }
            elsif ( $self->Field eq 'MemberOf' ) {
                return ( "Membership in [_1] deleted", $value );    #loc()
            }
            elsif ( $self->Field eq 'HasMember' ) {
                return ( "Member [_1] deleted", $value );           #loc()
            }
        }
        else {
            return ( "[_1]", $self->Data ); #loc()
        }
    },
    Told => sub {
        my $self = shift;
        if ( $self->Field eq 'Told' ) {
            my $t1 = RT::Date->new($self->CurrentUser);
            $t1->Set(Format => 'ISO', Value => $self->NewValue);
            my $t2 = RT::Date->new($self->CurrentUser);
            $t2->Set(Format => 'ISO', Value => $self->OldValue);
            return ( "[_1] changed from [_2] to [_3]", $self->loc($self->Field), $t2->AsString, $t1->AsString );    #loc()
        }
        else {
            return ( "[_1] changed from [_2] to [_3]",
                    $self->loc($self->Field),
                    ($self->OldValue? "'".$self->OldValue ."'" : $self->loc("(no value)")) , "'". $self->NewValue."'" );  #loc()
        }
    },
    Set => sub {
        my $self = shift;
        if ( $self->Field eq 'Password' ) {
            return ('Password changed');    #loc()
        }
        elsif ( $self->Field eq 'Queue' ) {
            my $q1 = RT::Queue->new( $self->CurrentUser );
            $q1->Load( $self->OldValue );
            my $q2 = RT::Queue->new( $self->CurrentUser );
            $q2->Load( $self->NewValue );
            return ("[_1] changed from [_2] to [_3]",
                    $self->loc($self->Field), $q1->Name // '#'.$q1->id, $q2->Name // '#'.$q2->id); #loc()
        }

        # Write the date/time change at local time:
        elsif ($self->Field =~  /^(?:Due|Starts|Started|Told|WillResolve)$/) {
            my $t1 = RT::Date->new($self->CurrentUser);
            $t1->Set(Format => 'ISO', Value => $self->NewValue);
            my $t2 = RT::Date->new($self->CurrentUser);
            $t2->Set(Format => 'ISO', Value => $self->OldValue);
            return ( "[_1] changed from [_2] to [_3]", $self->loc($self->Field), $t2->AsString, $t1->AsString );    #loc()
        }
        elsif ( $self->Field eq 'Owner' ) {
            my $Old = RT::User->new( $self->CurrentUser );
            $Old->Load( $self->OldValue );
            my $New = RT::User->new( $self->CurrentUser );
            $New->Load( $self->NewValue );

            if ( $Old->id == RT->Nobody->id ) {
                if ( $New->id == $self->Creator ) {
                    return ("Taken");   #loc()
                }
                else {
                    return ( "Given to [_1]", $self->_FormatUser($New) );    #loc()
                }
            }
            else {
                if ( $New->id == $self->Creator ) {
                    return ("Stolen from [_1]",  $self->_FormatUser($Old) );   #loc()
                }
                elsif ( $Old->id == $self->Creator ) {
                    if ( $New->id == RT->Nobody->id ) {
                        return ("Untaken"); #loc()
                    }
                    else {
                        return ( "Given to [_1]", $self->_FormatUser($New) ); #loc()
                    }
                }
                else {
                    return (
                        "Owner forcibly changed from [_1] to [_2]",
                        map { $self->_FormatUser($_) } $Old, $New
                    );   #loc()
                }
            }
        }
        else {
            return ( "[_1] changed from [_2] to [_3]",
                    $self->loc($self->Field),
                    ($self->OldValue? "'".$self->OldValue ."'" : $self->loc("(no value)")),
                    ($self->NewValue? "'".$self->NewValue ."'" : $self->loc("(no value)")));  #loc()
        }
    },
    "Set-TimeWorked" => sub {
        my $self = shift;
        my $old  = $self->OldValue || 0;
        my $new  = $self->NewValue || 0;
        my $duration = $new - $old;
        if ($duration < 0) {
            return ("Adjusted time worked by [quant,_1,minute,minutes]", $duration); # loc()
        }
        elsif ($duration < 60) {
            return ("Worked [quant,_1,minute,minutes]", $duration); # loc()
        } else {
            return ("Worked [quant,_1,hour,hours] ([quant,_2,minute,minutes])", sprintf("%.1f", $duration / 60), $duration); # loc()
        }
    },
    PurgeTransaction => sub {
        my $self = shift;
        return ("Transaction [_1] purged", $self->Data);    #loc()
    },
    AddReminder => sub {
        my $self = shift;
        my $ticket = RT::Ticket->new($self->CurrentUser);
        $ticket->Load($self->NewValue);
        if ( $ticket->CurrentUserHasRight('ShowTicket') ) {
            my $subject = [
                \'<a href="', RT->Config->Get('WebPath'),
                "/Ticket/Reminders.html?id=", $self->ObjectId,
                "#reminder-", $ticket->id, \'">', $ticket->Subject, \'</a>'
            ];
            return ("Reminder '[_1]' added", $subject); #loc()
        } else {
            return ("Reminder added"); #loc()
        }
    },
    OpenReminder => sub {
        my $self = shift;
        my $ticket = RT::Ticket->new($self->CurrentUser);
        $ticket->Load($self->NewValue);
        if ( $ticket->CurrentUserHasRight('ShowTicket') ) {
            my $subject = [
                \'<a href="', RT->Config->Get('WebPath'),
                "/Ticket/Reminders.html?id=", $self->ObjectId,
                "#reminder-", $ticket->id, \'">', $ticket->Subject, \'</a>'
            ];
            return ("Reminder '[_1]' reopened", $subject);  #loc()
        } else {
            return ("Reminder reopened");  #loc()
        }
    },
    ResolveReminder => sub {
        my $self = shift;
        my $ticket = RT::Ticket->new($self->CurrentUser);
        $ticket->Load($self->NewValue);
        if ( $ticket->CurrentUserHasRight('ShowTicket') ) {
            my $subject = [
                \'<a href="', RT->Config->Get('WebPath'),
                "/Ticket/Reminders.html?id=", $self->ObjectId,
                "#reminder-", $ticket->id, \'">', $ticket->Subject, \'</a>'
            ];
            return ("Reminder '[_1]' completed", $subject); #loc()
        } else {
            return ("Reminder completed"); #loc()
        }
    },
    AddMember => sub {
        my $self = shift;
        my $principal = RT::Principal->new($self->CurrentUser);
        $principal->Load($self->Field);

        if ($principal->IsUser) {
            return ("Added user '[_1]'", $principal->Object->Name); #loc()
        }
        else {
            return ("Added group '[_1]'", $principal->Object->Name); #loc()
        }
    },
    DeleteMember => sub {
        my $self = shift;
        my $principal = RT::Principal->new($self->CurrentUser);
        $principal->Load($self->Field);

        if ($principal->IsUser) {
            return ("Removed user '[_1]'", $principal->Object->Name); #loc()
        }
        else {
            return ("Removed group '[_1]'", $principal->Object->Name); #loc()
        }
    },
    AddMembership => sub {
        my $self = shift;
        my $principal = RT::Principal->new($self->CurrentUser);
        $principal->Load($self->Field);
        return ("Added to group '[_1]'", $principal->Object->Name); #loc()
    },
    DeleteMembership => sub {
        my $self = shift;
        my $principal = RT::Principal->new($self->CurrentUser);
        $principal->Load($self->Field);
        return ("Removed from group '[_1]'", $principal->Object->Name); #loc()
    },
);




=head2 IsInbound

Returns true if the creator of the transaction is a requestor of the ticket.
Returns false otherwise

=cut

sub IsInbound {
    my $self = shift;
    $self->ObjectType eq 'RT::Ticket' or return undef;
    return ( $self->TicketObj->IsRequestor( $self->CreatorObj->PrincipalId ) );
}



sub _OverlayAccessible {
    {

          ObjectType => { public => 1},
          ObjectId => { public => 1},

    }
};




sub _Set {
    my $self = shift;
    return ( 0, $self->loc('Transactions are immutable') );
}



=head2 _Value

Takes the name of a table column.
Returns its value as a string, if the user passes an ACL check

=cut

sub _Value {
    my $self  = shift;
    my $field = shift;

    #if the field is public, return it.
    if ( $self->_Accessible( $field, 'public' ) ) {
        return $self->SUPER::_Value( $field );
    }

    unless ( $self->CurrentUserCanSee ) {
        return undef;
    }

    return $self->SUPER::_Value( $field );
}


=head2 CurrentUserCanSee

Returns true if current user has rights to see this particular transaction.

This fact depends on type of the transaction, type of an object the transaction
is attached to and may be other conditions, so this method is prefered over
custom implementations.

It always returns true if current user is system user.

=cut

sub CurrentUserCanSee {
    my $self = shift;

    return 1 if $self->CurrentUser->PrincipalObj->Id == RT->SystemUser->Id;

    # Make sure the user can see the custom field before showing that it changed
    my $type = $self->__Value('Type');
    if ( $type eq 'CustomField' and my $cf_id = $self->__Value('Field') ) {
        my $cf = RT::CustomField->new( $self->CurrentUser );
        $cf->SetContextObject( $self->Object );
        $cf->Load( $cf_id );
        return 0 unless $cf->CurrentUserHasRight('SeeCustomField');
    }

    # Transactions that might have changed the ->Object's visibility to
    # the current user are marked readable
    return 1 if $self->{ _object_is_readable };

    # Defer to the object in question
    return $self->Object->CurrentUserCanSee("Transaction", $self);
}


sub Ticket {
    my $self = shift;
    return $self->ObjectId;
}

sub TicketObj {
    my $self = shift;
    return $self->Object;
}

sub OldValue {
    my $self = shift;
    if ( my $Object = $self->OldReferenceObject ) {
        return $Object->Content;
    }
    else {
        return $self->_Value('OldValue');
    }
}

sub NewValue {
    my $self = shift;
    if ( my $Object = $self->NewReferenceObject ) {
        return $Object->Content;
    }
    else {
        return $self->_Value('NewValue');
    }
}

sub Object {
    my $self  = shift;
    my $Object = $self->__Value('ObjectType')->new($self->CurrentUser);
    $Object->Load($self->__Value('ObjectId'));
    return $Object;
}

=head2 NewReferenceObject

=head2 OldReferenceObject

Returns an object of the class specified by the column C<ReferenceType> and
loaded with the id specified by the column C<NewReference> or C<OldReference>.
C<ReferenceType> is assumed to be an L<RT::Record> subclass.

The object may be unloaded (check C<< $object->id >>) if the reference is
corrupt (such as if the referenced record was improperly deleted).

Returns undef if either C<ReferenceType> or C<NewReference>/C<OldReference> is
false.

=cut

sub NewReferenceObject { $_[0]->_ReferenceObject("New") }
sub OldReferenceObject { $_[0]->_ReferenceObject("Old") }

sub _ReferenceObject {
    my $self  = shift;
    my $which = shift;
    my $type  = $self->__Value("ReferenceType");
    my $id    = $self->__Value("${which}Reference");
    return unless $type and $id;

    my $object = $type->new($self->CurrentUser);
    $object->Load( $id );
    return $object;
}

sub FriendlyObjectType {
    my $self = shift;
    return $self->loc( $self->Object->RecordType );
}

=head2 UpdateCustomFields

Takes a hash of:

    CustomField-C<Id> => Value

or:

    Object-RT::Transaction-CustomField-C<Id> => Value

parameters to update this transaction's custom fields.

=cut

sub UpdateCustomFields {
    my $self = shift;
    my %args = (@_);

    # This method used to have an API that took a hash of a single
    # value "ARGSRef", which was a reference to a hash of arguments.
    # This was insane. The next few lines of code preserve that API
    # while giving us something saner.
    my $args;
    if ($args{'ARGSRef'}) {
        RT->Deprecated( Arguments => "ARGSRef", Remove => "4.4" );
        $args = $args{ARGSRef};
    } else {
        $args = \%args;
    }

    foreach my $arg ( keys %$args ) {
        next
          unless ( $arg =~
            /^(?:Object-RT::Transaction--)?CustomField-(\d+)/ );
	next if $arg =~ /-Magic$/;
        next if $arg =~ /-TimeUnits$/;
        my $cfid   = $1;
        my $values = $args->{$arg};
        my $cf = $self->LoadCustomFieldByIdentifier($cfid);
        next unless $cf->ObjectTypeFromLookupType($cf->__Value('LookupType'))->isa(ref $self);
        foreach
          my $value ( UNIVERSAL::isa( $values, 'ARRAY' ) ? @$values : $values )
        {
            next unless (defined($value) && length($value));
            $self->_AddCustomFieldValue(
                Field             => $cfid,
                Value             => $value,
                RecordTransaction => 0,
            );
        }
    }
}

=head2 LoadCustomFieldByIdentifier

Finds and returns the custom field of the given name for the
transaction, overriding L<RT::Record/LoadCustomFieldByIdentifier> to
look for queue-specific CFs before global ones.

=cut

sub LoadCustomFieldByIdentifier {
    my $self  = shift;
    my $field = shift;

    return $self->SUPER::LoadCustomFieldByIdentifier($field)
        if ref $field or $field =~ /^\d+$/;

    return $self->SUPER::LoadCustomFieldByIdentifier($field)
        unless UNIVERSAL::can( $self->Object, 'QueueObj' );

    my $CFs = RT::CustomFields->new( $self->CurrentUser );
    $CFs->SetContextObject( $self->Object );
    $CFs->Limit( FIELD => 'Name', VALUE => $field, CASESENSITIVE => 0 );
    $CFs->LimitToLookupType($self->CustomFieldLookupType);
    $CFs->LimitToGlobalOrObjectId($self->Object->QueueObj->id);
    return $CFs->First || RT::CustomField->new( $self->CurrentUser );
}

=head2 CustomFieldLookupType

Returns the RT::Transaction lookup type, which can 
be passed to RT::CustomField->Create() via the 'LookupType' hash key.

=cut


sub CustomFieldLookupType {
    "RT::Queue-RT::Ticket-RT::Transaction";
}


=head2 SquelchMailTo

Similar to Ticket class SquelchMailTo method - returns a list of
transaction's squelched addresses.  As transactions are immutable, the
list of squelched recipients cannot be modified after creation.

=cut

sub SquelchMailTo {
    my $self = shift;
    return () unless $self->CurrentUserCanSee;
    return $self->Attributes->Named('SquelchMailTo');
}

=head2 Recipients

Returns the list of email addresses (as L<Email::Address> objects)
that this transaction would send mail to.  There may be duplicates.

=cut

sub Recipients {
    my $self = shift;
    my @recipients;
    foreach my $scrip ( @{ $self->Scrips->Prepared } ) {
        my $action = $scrip->ActionObj->Action;
        next unless $action->isa('RT::Action::SendEmail');

        foreach my $type (qw(To Cc Bcc)) {
            push @recipients, $action->$type();
        }
    }

    if ( $self->Rules ) {
        for my $rule (@{$self->Rules}) {
            next unless $rule->{hints} && $rule->{hints}{class} eq 'SendEmail';
            my $data = $rule->{hints}{recipients};
            foreach my $type (qw(To Cc Bcc)) {
                push @recipients, map {Email::Address->new($_)} @{$data->{$type}};
            }
        }
    }
    return @recipients;
}

=head2 DeferredRecipients($freq, $include_sent )

Takes the following arguments:

=over

=item * a string to indicate the frequency of digest delivery.  Valid values are "daily", "weekly", or "susp".

=item * an optional argument which, if true, will return addresses even if this notification has been marked as 'sent' for this transaction.

=back

Returns an array of users who should now receive the notification that
was recorded in this transaction.  Returns an empty array if there were
no deferred users, or if $include_sent was not specified and the deferred
notifications have been sent.

=cut

sub DeferredRecipients {
    my $self = shift;
    my $freq = shift;
    my $include_sent = @_? shift : 0;

    my $attr = $self->FirstAttribute('DeferredRecipients');

    return () unless ($attr);

    my $deferred = $attr->Content;

    return () unless ( ref($deferred) eq 'HASH' && exists $deferred->{$freq} );

    # Skip it.
   
    for my $user (keys %{$deferred->{$freq}}) {
        if ($deferred->{$freq}->{$user}->{_sent} && !$include_sent) { 
            delete $deferred->{$freq}->{$user} 
        }
    }
    # Now get our users.  Easy.
    
    return keys %{ $deferred->{$freq} };
}



# Transactions don't change. by adding this cache config directive, we don't lose pathalogically on long tickets.
sub _CacheConfig {
  {
     'cache_for_sec'  => 6000,
  }
}


=head2 ACLEquivalenceObjects

This method returns a list of objects for which a user's rights also apply
to this Transaction.

This currently only applies to Transaction Custom Fields on Tickets, so we return
the Ticket's Queue and the Ticket.

This method is called from L<RT::Principal/HasRight>.

=cut

sub ACLEquivalenceObjects {
    my $self = shift;

    return unless $self->ObjectType eq 'RT::Ticket';
    my $object = $self->Object;
    return $object,$object->QueueObj;

}





=head2 id

Returns the current value of id.
(In the database, id is stored as int(11).)


=cut


=head2 ObjectType

Returns the current value of ObjectType.
(In the database, ObjectType is stored as varchar(64).)



=head2 SetObjectType VALUE


Set ObjectType to VALUE.
Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
(In the database, ObjectType will be stored as a varchar(64).)


=cut


=head2 ObjectId

Returns the current value of ObjectId.
(In the database, ObjectId is stored as int(11).)



=head2 SetObjectId VALUE


Set ObjectId to VALUE.
Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
(In the database, ObjectId will be stored as a int(11).)


=cut


=head2 TimeTaken

Returns the current value of TimeTaken.
(In the database, TimeTaken is stored as int(11).)



=head2 SetTimeTaken VALUE


Set TimeTaken to VALUE.
Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
(In the database, TimeTaken will be stored as a int(11).)


=cut


=head2 Type

Returns the current value of Type.
(In the database, Type is stored as varchar(20).)



=head2 SetType VALUE


Set Type to VALUE.
Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
(In the database, Type will be stored as a varchar(20).)


=cut


=head2 Field

Returns the current value of Field.
(In the database, Field is stored as varchar(40).)



=head2 SetField VALUE


Set Field to VALUE.
Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
(In the database, Field will be stored as a varchar(40).)


=cut


=head2 OldValue

Returns the current value of OldValue.
(In the database, OldValue is stored as varchar(255).)



=head2 SetOldValue VALUE


Set OldValue to VALUE.
Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
(In the database, OldValue will be stored as a varchar(255).)


=cut


=head2 NewValue

Returns the current value of NewValue.
(In the database, NewValue is stored as varchar(255).)



=head2 SetNewValue VALUE


Set NewValue to VALUE.
Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
(In the database, NewValue will be stored as a varchar(255).)


=cut


=head2 ReferenceType

Returns the current value of ReferenceType.
(In the database, ReferenceType is stored as varchar(255).)



=head2 SetReferenceType VALUE


Set ReferenceType to VALUE.
Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
(In the database, ReferenceType will be stored as a varchar(255).)


=cut


=head2 OldReference

Returns the current value of OldReference.
(In the database, OldReference is stored as int(11).)



=head2 SetOldReference VALUE


Set OldReference to VALUE.
Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
(In the database, OldReference will be stored as a int(11).)


=cut


=head2 NewReference

Returns the current value of NewReference.
(In the database, NewReference is stored as int(11).)



=head2 SetNewReference VALUE


Set NewReference to VALUE.
Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
(In the database, NewReference will be stored as a int(11).)


=cut


=head2 Data

Returns the current value of Data.
(In the database, Data is stored as varchar(255).)



=head2 SetData VALUE


Set Data to VALUE.
Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
(In the database, Data will be stored as a varchar(255).)


=cut


=head2 Creator

Returns the current value of Creator.
(In the database, Creator is stored as int(11).)


=cut


=head2 Created

Returns the current value of Created.
(In the database, Created is stored as datetime.)


=cut



sub _CoreAccessible {
    {

        id =>
                {read => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => ''},
        ObjectType =>
                {read => 1, write => 1, sql_type => 12, length => 64,  is_blob => 0,  is_numeric => 0,  type => 'varchar(64)', default => ''},
        ObjectId =>
                {read => 1, write => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => '0'},
        TimeTaken =>
                {read => 1, write => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => '0'},
        Type =>
                {read => 1, write => 1, sql_type => 12, length => 20,  is_blob => 0,  is_numeric => 0,  type => 'varchar(20)', default => ''},
        Field =>
                {read => 1, write => 1, sql_type => 12, length => 40,  is_blob => 0,  is_numeric => 0,  type => 'varchar(40)', default => ''},
        OldValue =>
                {read => 1, write => 1, sql_type => 12, length => 255,  is_blob => 0,  is_numeric => 0,  type => 'varchar(255)', default => ''},
        NewValue =>
                {read => 1, write => 1, sql_type => 12, length => 255,  is_blob => 0,  is_numeric => 0,  type => 'varchar(255)', default => ''},
        ReferenceType =>
                {read => 1, write => 1, sql_type => 12, length => 255,  is_blob => 0,  is_numeric => 0,  type => 'varchar(255)', default => ''},
        OldReference =>
                {read => 1, write => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => ''},
        NewReference =>
                {read => 1, write => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => ''},
        Data =>
                {read => 1, write => 1, sql_type => 12, length => 255,  is_blob => 0,  is_numeric => 0,  type => 'varchar(255)', default => ''},
        Creator =>
                {read => 1, auto => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => '0'},
        Created =>
                {read => 1, auto => 1, sql_type => 11, length => 0,  is_blob => 0,  is_numeric => 0,  type => 'datetime', default => ''},

 }
};

sub FindDependencies {
    my $self = shift;
    my ($walker, $deps) = @_;

    $self->SUPER::FindDependencies($walker, $deps);

    $deps->Add( out => $self->Object );
    $deps->Add( in => $self->Attachments );

    my $type = $self->Type;
    if ($type eq "CustomField") {
        my $cf = RT::CustomField->new( RT->SystemUser );
        $cf->Load( $self->Field );
        $deps->Add( out => $cf );
    } elsif ($type =~ /^(Take|Untake|Force|Steal|Give)$/) {
        for my $field (qw/OldValue NewValue/) {
            my $user = RT::User->new( RT->SystemUser );
            $user->Load( $self->$field );
            $deps->Add( out => $user );
        }
    } elsif ($type eq "DelWatcher") {
        my $principal = RT::Principal->new( RT->SystemUser );
        $principal->Load( $self->OldValue );
        $deps->Add( out => $principal->Object );
    } elsif ($type eq "AddWatcher") {
        my $principal = RT::Principal->new( RT->SystemUser );
        $principal->Load( $self->NewValue );
        $deps->Add( out => $principal->Object );
    } elsif ($type eq "DeleteLink") {
        if ($self->OldValue) {
            my $base = RT::URI->new( $self->CurrentUser );
            $base->FromURI( $self->OldValue );
            $deps->Add( out => $base->Object ) if $base->Resolver and $base->Object;
        }
    } elsif ($type eq "AddLink") {
        if ($self->NewValue) {
            my $base = RT::URI->new( $self->CurrentUser );
            $base->FromURI( $self->NewValue );
            $deps->Add( out => $base->Object ) if $base->Resolver and $base->Object;
        }
    } elsif ($type eq "Set" and $self->Field eq "Queue") {
        for my $field (qw/OldValue NewValue/) {
            my $queue = RT::Queue->new( RT->SystemUser );
            $queue->Load( $self->$field );
            $deps->Add( out => $queue );
        }
    } elsif ($type =~ /^(Add|Open|Resolve)Reminder$/) {
        my $ticket = RT::Ticket->new( RT->SystemUser );
        $ticket->Load( $self->NewValue );
        $deps->Add( out => $ticket );
    }
}

sub __DependsOn {
    my $self = shift;
    my %args = (
        Shredder => undef,
        Dependencies => undef,
        @_,
    );
    my $deps = $args{'Dependencies'};

    $deps->_PushDependencies(
        BaseObject => $self,
        Flags => RT::Shredder::Constants::DEPENDS_ON,
        TargetObjects => $self->Attachments,
        Shredder => $args{'Shredder'}
    );

    return $self->SUPER::__DependsOn( %args );
}

sub Serialize {
    my $self = shift;
    my %args = (@_);
    my %store = $self->SUPER::Serialize(@_);

    my $type = $store{Type};
    if ($type eq "CustomField") {
        my $cf = RT::CustomField->new( RT->SystemUser );
        $cf->Load( $store{Field} );
        $store{Field} = \($cf->UID);

        $store{OldReference} = \($self->OldReferenceObject->UID) if $self->OldReference;
        $store{NewReference} = \($self->NewReferenceObject->UID) if $self->NewReference;
    } elsif ($type =~ /^(Take|Untake|Force|Steal|Give)$/) {
        for my $field (qw/OldValue NewValue/) {
            my $user = RT::User->new( RT->SystemUser );
            $user->Load( $store{$field} );
            $store{$field} = \($user->UID);
        }
    } elsif ($type eq "DelWatcher") {
        my $principal = RT::Principal->new( RT->SystemUser );
        $principal->Load( $store{OldValue} );
        $store{OldValue} = \($principal->UID);
    } elsif ($type eq "AddWatcher") {
        my $principal = RT::Principal->new( RT->SystemUser );
        $principal->Load( $store{NewValue} );
        $store{NewValue} = \($principal->UID);
    } elsif ($type eq "DeleteLink") {
        if ($store{OldValue}) {
            my $base = RT::URI->new( $self->CurrentUser );
            $base->FromURI( $store{OldValue} );
            if ($base->Resolver && (my $object = $base->Object)) {
                if ($args{serializer}->Observe(object => $object)) {
                    $store{OldValue} = \($object->UID);
                }
                elsif ($args{serializer}{HyperlinkUnmigrated}) {
                    $store{OldValue} = $base->AsHREF;
                }
                else {
                    $store{OldValue} = "(not migrated)";
                }
            }
        }
    } elsif ($type eq "AddLink") {
        if ($store{NewValue}) {
            my $base = RT::URI->new( $self->CurrentUser );
            $base->FromURI( $store{NewValue} );
            if ($base->Resolver && (my $object = $base->Object)) {
                if ($args{serializer}->Observe(object => $object)) {
                    $store{NewValue} = \($object->UID);
                }
                elsif ($args{serializer}{HyperlinkUnmigrated}) {
                    $store{NewValue} = $base->AsHREF;
                }
                else {
                    $store{NewValue} = "(not migrated)";
                }
            }
        }
    } elsif ($type eq "Set" and $store{Field} eq "Queue") {
        for my $field (qw/OldValue NewValue/) {
            my $queue = RT::Queue->new( RT->SystemUser );
            $queue->Load( $store{$field} );
            if ($args{serializer}->Observe(object => $queue)) {
                $store{$field} = \($queue->UID);
            }
            else {
                $store{$field} = "$RT::Organization: " . $queue->Name . " (not migrated)";

            }
        }
    } elsif ($type =~ /^(Add|Open|Resolve)Reminder$/) {
        my $ticket = RT::Ticket->new( RT->SystemUser );
        $ticket->Load( $store{NewValue} );
        $store{NewValue} = \($ticket->UID);
    }

    return %store;
}

sub PreInflate {
    my $class = shift;
    my ($importer, $uid, $data) = @_;

    if ($data->{Object} and ref $data->{Object}) {
        my $on_uid = ${ $data->{Object} };
        return if $importer->ShouldSkipTransaction($on_uid);
    }

    if ($data->{Type} eq "DeleteLink" and ref $data->{OldValue}) {
        my $uid = ${ $data->{OldValue} };
        my $obj = $importer->LookupObj( $uid );
        $data->{OldValue} = $obj->URI;
    } elsif ($data->{Type} eq "AddLink" and ref $data->{NewValue}) {
        my $uid = ${ $data->{NewValue} };
        my $obj = $importer->LookupObj( $uid );
        $data->{NewValue} = $obj->URI;
    }

    return $class->SUPER::PreInflate( $importer, $uid, $data );
}

RT::Base->_ImportOverlays();

1;