]> git.uio.no Git - usit-rt.git/blob - lib/RT/CustomField.pm
Initial commit 4.0.5-3
[usit-rt.git] / lib / RT / CustomField.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC
6 #                                          <sales@bestpractical.com>
7 #
8 # (Except where explicitly superseded by other copyright notices)
9 #
10 #
11 # LICENSE:
12 #
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
16 # from www.gnu.org.
17 #
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 # General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28 #
29 #
30 # CONTRIBUTION SUBMISSION POLICY:
31 #
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
37 #
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
46 #
47 # END BPS TAGGED BLOCK }}}
48
49 package RT::CustomField;
50
51 use strict;
52 use warnings;
53
54
55
56 use base 'RT::Record';
57
58 sub Table {'CustomFields'}
59
60
61 use RT::CustomFieldValues;
62 use RT::ObjectCustomFields;
63 use RT::ObjectCustomFieldValues;
64
65 our %FieldTypes = (
66     Select => {
67         sort_order => 10,
68         selection_type => 1,
69
70         labels => [ 'Select multiple values',      # loc
71                     'Select one value',            # loc
72                     'Select up to [_1] values',    # loc
73                   ],
74
75         render_types => {
76             multiple => [
77
78                 # Default is the first one
79                 'Select box',              # loc
80                 'List',                    # loc
81             ],
82             single => [ 'Select box',              # loc
83                         'Dropdown',                # loc
84                         'List',                    # loc
85                       ]
86         },
87
88     },
89     Freeform => {
90         sort_order => 20,
91         selection_type => 0,
92
93         labels => [ 'Enter multiple values',       # loc
94                     'Enter one value',             # loc
95                     'Enter up to [_1] values',     # loc
96                   ]
97                 },
98     Text => {
99         sort_order => 30,
100         selection_type => 0,
101         labels         => [
102                     'Fill in multiple text areas',      # loc
103                     'Fill in one text area',            # loc
104                     'Fill in up to [_1] text areas',    # loc
105                   ]
106             },
107     Wikitext => {
108         sort_order => 40,
109         selection_type => 0,
110         labels         => [
111                     'Fill in multiple wikitext areas',      # loc
112                     'Fill in one wikitext area',            # loc
113                     'Fill in up to [_1] wikitext areas',    # loc
114                   ]
115                 },
116
117     Image => {
118         sort_order => 50,
119         selection_type => 0,
120         labels         => [
121                     'Upload multiple images',               # loc
122                     'Upload one image',                     # loc
123                     'Upload up to [_1] images',             # loc
124                   ]
125              },
126     Binary => {
127         sort_order => 60,
128         selection_type => 0,
129         labels         => [
130                     'Upload multiple files',                # loc
131                     'Upload one file',                      # loc
132                     'Upload up to [_1] files',              # loc
133                   ]
134               },
135
136     Combobox => {
137         sort_order => 70,
138         selection_type => 1,
139         labels         => [
140                     'Combobox: Select or enter multiple values',      # loc
141                     'Combobox: Select or enter one value',            # loc
142                     'Combobox: Select or enter up to [_1] values',    # loc
143                   ]
144                 },
145     Autocomplete => {
146         sort_order => 80,
147         selection_type => 1,
148         labels         => [
149                     'Enter multiple values with autocompletion',      # loc
150                     'Enter one value with autocompletion',            # loc
151                     'Enter up to [_1] values with autocompletion',    # loc
152                   ]
153     },
154
155     Date => {
156         sort_order => 90,
157         selection_type => 0,
158         labels         => [
159                     'Select multiple dates',                          # loc
160                     'Select date',                                    # loc
161                     'Select up to [_1] dates',                        # loc
162                   ]
163             },
164     DateTime => {
165         sort_order => 100,
166         selection_type => 0,
167         labels         => [
168                     'Select multiple datetimes',                      # loc
169                     'Select datetime',                                # loc
170                     'Select up to [_1] datetimes',                    # loc
171                   ]
172                 },
173
174     IPAddress => {
175         sort_order => 110,
176         selection_type => 0,
177
178         labels => [ 'Enter multiple IP addresses',       # loc
179                     'Enter one IP address',             # loc
180                     'Enter up to [_1] IP addresses',     # loc
181                   ]
182                 },
183     IPAddressRange => {
184         sort_order => 120,
185         selection_type => 0,
186
187         labels => [ 'Enter multiple IP address ranges',       # loc
188                     'Enter one IP address range',             # loc
189                     'Enter up to [_1] IP address ranges',     # loc
190                   ]
191                 },
192 );
193
194
195 our %FRIENDLY_OBJECT_TYPES =  ();
196
197 RT::CustomField->_ForObjectType( 'RT::Queue-RT::Ticket' => "Tickets", );    #loc
198 RT::CustomField->_ForObjectType(
199     'RT::Queue-RT::Ticket-RT::Transaction' => "Ticket Transactions", );    #loc
200 RT::CustomField->_ForObjectType( 'RT::User'  => "Users", );                           #loc
201 RT::CustomField->_ForObjectType( 'RT::Queue'  => "Queues", );                         #loc
202 RT::CustomField->_ForObjectType( 'RT::Group' => "Groups", );                          #loc
203
204 our $RIGHTS = {
205     SeeCustomField            => 'View custom fields',                                    # loc_pair
206     AdminCustomField          => 'Create, modify and delete custom fields',               # loc_pair
207     AdminCustomFieldValues    => 'Create, modify and delete custom fields values',        # loc_pair
208     ModifyCustomField         => 'Add, modify and delete custom field values for objects' # loc_pair
209 };
210
211 our $RIGHT_CATEGORIES = {
212     SeeCustomField          => 'General',
213     AdminCustomField        => 'Admin',
214     AdminCustomFieldValues  => 'Admin',
215     ModifyCustomField       => 'Staff',
216 };
217
218 # Tell RT::ACE that this sort of object can get acls granted
219 $RT::ACE::OBJECT_TYPES{'RT::CustomField'} = 1;
220
221 __PACKAGE__->AddRights(%$RIGHTS);
222 __PACKAGE__->AddRightCategories(%$RIGHT_CATEGORIES);
223
224 =head2 AddRights C<RIGHT>, C<DESCRIPTION> [, ...]
225
226 Adds the given rights to the list of possible rights.  This method
227 should be called during server startup, not at runtime.
228
229 =cut
230
231 sub AddRights {
232     my $self = shift;
233     my %new = @_;
234     $RIGHTS = { %$RIGHTS, %new };
235     %RT::ACE::LOWERCASERIGHTNAMES = ( %RT::ACE::LOWERCASERIGHTNAMES,
236                                       map { lc($_) => $_ } keys %new);
237 }
238
239 sub AvailableRights {
240     my $self = shift;
241     return $RIGHTS;
242 }
243
244 =head2 RightCategories
245
246 Returns a hashref where the keys are rights for this type of object and the
247 values are the category (General, Staff, Admin) the right falls into.
248
249 =cut
250
251 sub RightCategories {
252     return $RIGHT_CATEGORIES;
253 }
254
255 =head2 AddRightCategories C<RIGHT>, C<CATEGORY> [, ...]
256
257 Adds the given right and category pairs to the list of right categories.  This
258 method should be called during server startup, not at runtime.
259
260 =cut
261
262 sub AddRightCategories {
263     my $self = shift if ref $_[0] or $_[0] eq __PACKAGE__;
264     my %new = @_;
265     $RIGHT_CATEGORIES = { %$RIGHT_CATEGORIES, %new };
266 }
267
268 =head1 NAME
269
270   RT::CustomField_Overlay - overlay for RT::CustomField
271
272 =head1 DESCRIPTION
273
274 =head1 'CORE' METHODS
275
276 =head2 Create PARAMHASH
277
278 Create takes a hash of values and creates a row in the database:
279
280   varchar(200) 'Name'.
281   varchar(200) 'Type'.
282   int(11) 'MaxValues'.
283   varchar(255) 'Pattern'.
284   smallint(6) 'Repeated'.
285   varchar(255) 'Description'.
286   int(11) 'SortOrder'.
287   varchar(255) 'LookupType'.
288   smallint(6) 'Disabled'.
289
290 C<LookupType> is generally the result of either
291 C<RT::Ticket->CustomFieldLookupType> or C<RT::Transaction->CustomFieldLookupType>.
292
293 =cut
294
295 sub Create {
296     my $self = shift;
297     my %args = (
298         Name        => '',
299         Type        => '',
300         MaxValues   => 0,
301         Pattern     => '',
302         Description => '',
303         Disabled    => 0,
304         LookupType  => '',
305         Repeated    => 0,
306         LinkValueTo => '',
307         IncludeContentForValue => '',
308         @_,
309     );
310
311     unless ( $self->CurrentUser->HasRight(Object => $RT::System, Right => 'AdminCustomField') ) {
312         return (0, $self->loc('Permission Denied'));
313     }
314
315     if ( $args{TypeComposite} ) {
316         @args{'Type', 'MaxValues'} = split(/-/, $args{TypeComposite}, 2);
317     }
318     elsif ( $args{Type} =~ s/(?:(Single)|Multiple)$// ) {
319         # old style Type string
320         $args{'MaxValues'} = $1 ? 1 : 0;
321     }
322     $args{'MaxValues'} = int $args{'MaxValues'};
323
324     if ( !exists $args{'Queue'}) {
325     # do nothing -- things below are strictly backward compat
326     }
327     elsif (  ! $args{'Queue'} ) {
328         unless ( $self->CurrentUser->HasRight( Object => $RT::System, Right => 'AssignCustomFields') ) {
329             return ( 0, $self->loc('Permission Denied') );
330         }
331         $args{'LookupType'} = 'RT::Queue-RT::Ticket';
332     }
333     else {
334         my $queue = RT::Queue->new($self->CurrentUser);
335         $queue->Load($args{'Queue'});
336         unless ($queue->Id) {
337             return (0, $self->loc("Queue not found"));
338         }
339         unless ( $queue->CurrentUserHasRight('AssignCustomFields') ) {
340             return ( 0, $self->loc('Permission Denied') );
341         }
342         $args{'LookupType'} = 'RT::Queue-RT::Ticket';
343         $args{'Queue'} = $queue->Id;
344     }
345
346     my ($ok, $msg) = $self->_IsValidRegex( $args{'Pattern'} );
347     return (0, $self->loc("Invalid pattern: [_1]", $msg)) unless $ok;
348
349     if ( $args{'MaxValues'} != 1 && $args{'Type'} =~ /(text|combobox)$/i ) {
350         $RT::Logger->debug("Support for 'multiple' Texts or Comboboxes is not implemented");
351         $args{'MaxValues'} = 1;
352     }
353
354     if ( $args{'RenderType'} ||= undef ) {
355         my $composite = join '-', @args{'Type', 'MaxValues'};
356         return (0, $self->loc("This custom field has no Render Types"))
357             unless $self->HasRenderTypes( $composite );
358
359         if ( $args{'RenderType'} eq $self->DefaultRenderType( $composite ) ) {
360             $args{'RenderType'} = undef;
361         } else {
362             return (0, $self->loc("Invalid Render Type") )
363                 unless grep $_ eq  $args{'RenderType'}, $self->RenderTypes( $composite );
364         }
365     }
366
367     $args{'ValuesClass'} = undef if ($args{'ValuesClass'} || '') eq 'RT::CustomFieldValues';
368     if ( $args{'ValuesClass'} ||= undef ) {
369         return (0, $self->loc("This Custom Field can not have list of values"))
370             unless $self->IsSelectionType( $args{'Type'} );
371
372         unless ( $self->ValidateValuesClass( $args{'ValuesClass'} ) ) {
373             return (0, $self->loc("Invalid Custom Field values source"));
374         }
375     }
376
377     (my $rv, $msg) = $self->SUPER::Create(
378         Name        => $args{'Name'},
379         Type        => $args{'Type'},
380         RenderType  => $args{'RenderType'},
381         MaxValues   => $args{'MaxValues'},
382         Pattern     => $args{'Pattern'},
383         BasedOn     => $args{'BasedOn'},
384         ValuesClass => $args{'ValuesClass'},
385         Description => $args{'Description'},
386         Disabled    => $args{'Disabled'},
387         LookupType  => $args{'LookupType'},
388         Repeated    => $args{'Repeated'},
389     );
390
391     if ($rv) {
392         if ( exists $args{'LinkValueTo'}) {
393             $self->SetLinkValueTo($args{'LinkValueTo'});
394         }
395
396         if ( exists $args{'IncludeContentForValue'}) {
397             $self->SetIncludeContentForValue($args{'IncludeContentForValue'});
398         }
399
400         return ($rv, $msg) unless exists $args{'Queue'};
401
402         # Compat code -- create a new ObjectCustomField mapping
403         my $OCF = RT::ObjectCustomField->new( $self->CurrentUser );
404         $OCF->Create(
405             CustomField => $self->Id,
406             ObjectId => $args{'Queue'},
407         );
408     }
409
410     return ($rv, $msg);
411 }
412
413 =head2 Load ID/NAME
414
415 Load a custom field.  If the value handed in is an integer, load by custom field ID. Otherwise, Load by name.
416
417 =cut
418
419 sub Load {
420     my $self = shift;
421     my $id = shift || '';
422
423     if ( $id =~ /^\d+$/ ) {
424         return $self->SUPER::Load( $id );
425     } else {
426         return $self->LoadByName( Name => $id );
427     }
428 }
429
430
431
432 =head2 LoadByName (Queue => QUEUEID, Name => NAME)
433
434 Loads the Custom field named NAME.
435
436 Will load a Disabled Custom Field even if there is a non-disabled Custom Field
437 with the same Name.
438
439 If a Queue parameter is specified, only look for ticket custom fields tied to that Queue.
440
441 If the Queue parameter is '0', look for global ticket custom fields.
442
443 If no queue parameter is specified, look for any and all custom fields with this name.
444
445 BUG/TODO, this won't let you specify that you only want user or group CFs.
446
447 =cut
448
449 # Compatibility for API change after 3.0 beta 1
450 *LoadNameAndQueue = \&LoadByName;
451 # Change after 3.4 beta.
452 *LoadByNameAndQueue = \&LoadByName;
453
454 sub LoadByName {
455     my $self = shift;
456     my %args = (
457         Queue => undef,
458         Name  => undef,
459         @_,
460     );
461
462     unless ( defined $args{'Name'} && length $args{'Name'} ) {
463         $RT::Logger->error("Couldn't load Custom Field without Name");
464         return wantarray ? (0, $self->loc("No name provided")) : 0;
465     }
466
467     # if we're looking for a queue by name, make it a number
468     if ( defined $args{'Queue'} && ($args{'Queue'} =~ /\D/ || !$self->ContextObject) ) {
469         my $QueueObj = RT::Queue->new( $self->CurrentUser );
470         $QueueObj->Load( $args{'Queue'} );
471         $args{'Queue'} = $QueueObj->Id;
472         $self->SetContextObject( $QueueObj )
473             unless $self->ContextObject;
474     }
475
476     # XXX - really naive implementation.  Slow. - not really. still just one query
477
478     my $CFs = RT::CustomFields->new( $self->CurrentUser );
479     $CFs->SetContextObject( $self->ContextObject );
480     my $field = $args{'Name'} =~ /\D/? 'Name' : 'id';
481     $CFs->Limit( FIELD => $field, VALUE => $args{'Name'}, CASESENSITIVE => 0);
482     # Don't limit to queue if queue is 0.  Trying to do so breaks
483     # RT::Group type CFs.
484     if ( defined $args{'Queue'} ) {
485         $CFs->LimitToQueue( $args{'Queue'} );
486     }
487
488     # When loading by name, we _can_ load disabled fields, but prefer
489     # non-disabled fields.
490     $CFs->FindAllRows;
491     $CFs->OrderByCols(
492         { FIELD => "Disabled", ORDER => 'ASC' },
493     );
494
495     # We only want one entry.
496     $CFs->RowsPerPage(1);
497
498     # version before 3.8 just returns 0, so we need to test if wantarray to be
499     # backward compatible.
500     return wantarray ? (0, $self->loc("Not found")) : 0 unless my $first = $CFs->First;
501
502     return $self->LoadById( $first->id );
503 }
504
505
506
507
508 =head2 Custom field values
509
510 =head3 Values FIELD
511
512 Return a object (collection) of all acceptable values for this Custom Field.
513 Class of the object can vary and depends on the return value
514 of the C<ValuesClass> method.
515
516 =cut
517
518 *ValuesObj = \&Values;
519
520 sub Values {
521     my $self = shift;
522
523     my $class = $self->ValuesClass;
524     if ( $class ne 'RT::CustomFieldValues') {
525         eval "require $class" or die "$@";
526     }
527     my $cf_values = $class->new( $self->CurrentUser );
528     # if the user has no rights, return an empty object
529     if ( $self->id && $self->CurrentUserHasRight( 'SeeCustomField') ) {
530         $cf_values->LimitToCustomField( $self->Id );
531     } else {
532         $cf_values->Limit( FIELD => 'id', VALUE => 0, SUBCLAUSE => 'acl' );
533     }
534     return ($cf_values);
535 }
536
537
538 =head3 AddValue HASH
539
540 Create a new value for this CustomField.  Takes a paramhash containing the elements Name, Description and SortOrder
541
542 =cut
543
544 sub AddValue {
545     my $self = shift;
546     my %args = @_;
547
548     unless ($self->CurrentUserHasRight('AdminCustomField') || $self->CurrentUserHasRight('AdminCustomFieldValues')) {
549         return (0, $self->loc('Permission Denied'));
550     }
551
552     # allow zero value
553     if ( !defined $args{'Name'} || $args{'Name'} eq '' ) {
554         return (0, $self->loc("Can't add a custom field value without a name"));
555     }
556
557     my $newval = RT::CustomFieldValue->new( $self->CurrentUser );
558     return $newval->Create( %args, CustomField => $self->Id );
559 }
560
561
562
563
564 =head3 DeleteValue ID
565
566 Deletes a value from this custom field by id.
567
568 Does not remove this value for any article which has had it selected
569
570 =cut
571
572 sub DeleteValue {
573     my $self = shift;
574     my $id = shift;
575     unless ( $self->CurrentUserHasRight('AdminCustomField') || $self->CurrentUserHasRight('AdminCustomFieldValues') ) {
576         return (0, $self->loc('Permission Denied'));
577     }
578
579     my $val_to_del = RT::CustomFieldValue->new( $self->CurrentUser );
580     $val_to_del->Load( $id );
581     unless ( $val_to_del->Id ) {
582         return (0, $self->loc("Couldn't find that value"));
583     }
584     unless ( $val_to_del->CustomField == $self->Id ) {
585         return (0, $self->loc("That is not a value for this custom field"));
586     }
587
588     my $retval = $val_to_del->Delete;
589     unless ( $retval ) {
590         return (0, $self->loc("Custom field value could not be deleted"));
591     }
592     return ($retval, $self->loc("Custom field value deleted"));
593 }
594
595
596 =head2 ValidateQueue Queue
597
598 Make sure that the name specified is valid
599
600 =cut
601
602 sub ValidateName {
603     my $self = shift;
604     my $value = shift;
605
606     return 0 unless length $value;
607
608     return $self->SUPER::ValidateName($value);
609 }
610
611 =head2 ValidateQueue Queue
612
613 Make sure that the queue specified is a valid queue name
614
615 =cut
616
617 sub ValidateQueue {
618     my $self = shift;
619     my $id = shift;
620
621     return undef unless defined $id;
622     # 0 means "Global" null would _not_ be ok.
623     return 1 if $id eq '0';
624
625     my $q = RT::Queue->new( RT->SystemUser );
626     $q->Load( $id );
627     return undef unless $q->id;
628     return 1;
629 }
630
631
632
633 =head2 Types 
634
635 Retuns an array of the types of CustomField that are supported
636
637 =cut
638
639 sub Types {
640     return (sort {(($FieldTypes{$a}{sort_order}||999) <=> ($FieldTypes{$b}{sort_order}||999)) or ($a cmp $b)} keys %FieldTypes);
641 }
642
643
644 =head2 IsSelectionType 
645
646 Retuns a boolean value indicating whether the C<Values> method makes sense
647 to this Custom Field.
648
649 =cut
650
651 sub IsSelectionType {
652     my $self = shift;
653     my $type = @_? shift : $self->Type;
654     return undef unless $type;
655     return $FieldTypes{$type}->{selection_type};
656 }
657
658
659
660 =head2 IsExternalValues
661
662 =cut
663
664 sub IsExternalValues {
665     my $self = shift;
666     return 0 unless $self->IsSelectionType( @_ );
667     return $self->ValuesClass eq 'RT::CustomFieldValues'? 0 : 1;
668 }
669
670 sub ValuesClass {
671     my $self = shift;
672     return $self->_Value( ValuesClass => @_ ) || 'RT::CustomFieldValues';
673 }
674
675 sub SetValuesClass {
676     my $self = shift;
677     my $class = shift || 'RT::CustomFieldValues';
678     
679     if ( $class eq 'RT::CustomFieldValues' ) {
680         return $self->_Set( Field => 'ValuesClass', Value => undef, @_ );
681     }
682
683     return (0, $self->loc("This Custom Field can not have list of values"))
684         unless $self->IsSelectionType;
685
686     unless ( $self->ValidateValuesClass( $class ) ) {
687         return (0, $self->loc("Invalid Custom Field values source"));
688     }
689     return $self->_Set( Field => 'ValuesClass', Value => $class, @_ );
690 }
691
692 sub ValidateValuesClass {
693     my $self = shift;
694     my $class = shift;
695
696     return 1 if !defined $class || $class eq 'RT::CustomFieldValues';
697     return 1 if grep $class eq $_, RT->Config->Get('CustomFieldValuesSources');
698     return undef;
699 }
700
701
702 =head2 FriendlyType [TYPE, MAX_VALUES]
703
704 Returns a localized human-readable version of the custom field type.
705 If a custom field type is specified as the parameter, the friendly type for that type will be returned
706
707 =cut
708
709 sub FriendlyType {
710     my $self = shift;
711
712     my $type = @_ ? shift : $self->Type;
713     my $max  = @_ ? shift : $self->MaxValues;
714     $max = 0 unless $max;
715
716     if (my $friendly_type = $FieldTypes{$type}->{labels}->[$max>2 ? 2 : $max]) {
717         return ( $self->loc( $friendly_type, $max ) );
718     }
719     else {
720         return ( $self->loc( $type ) );
721     }
722 }
723
724 sub FriendlyTypeComposite {
725     my $self = shift;
726     my $composite = shift || $self->TypeComposite;
727     return $self->FriendlyType(split(/-/, $composite, 2));
728 }
729
730
731 =head2 ValidateType TYPE
732
733 Takes a single string. returns true if that string is a value
734 type of custom field
735
736
737 =cut
738
739 sub ValidateType {
740     my $self = shift;
741     my $type = shift;
742
743     if ( $type =~ s/(?:Single|Multiple)$// ) {
744         $RT::Logger->warning( "Prefix 'Single' and 'Multiple' to Type deprecated, use MaxValues instead at (". join(":",caller).")");
745     }
746
747     if ( $FieldTypes{$type} ) {
748         return 1;
749     }
750     else {
751         return undef;
752     }
753 }
754
755
756 sub SetType {
757     my $self = shift;
758     my $type = shift;
759     if ($type =~ s/(?:(Single)|Multiple)$//) {
760         $RT::Logger->warning("'Single' and 'Multiple' on SetType deprecated, use SetMaxValues instead at (". join(":",caller).")");
761         $self->SetMaxValues($1 ? 1 : 0);
762     }
763     $self->_Set(Field => 'Type', Value =>$type);
764 }
765
766 =head2 SetPattern STRING
767
768 Takes a single string representing a regular expression.  Performs basic
769 validation on that regex, and sets the C<Pattern> field for the CF if it
770 is valid.
771
772 =cut
773
774 sub SetPattern {
775     my $self = shift;
776     my $regex = shift;
777
778     my ($ok, $msg) = $self->_IsValidRegex($regex);
779     if ($ok) {
780         return $self->_Set(Field => 'Pattern', Value => $regex);
781     }
782     else {
783         return (0, $self->loc("Invalid pattern: [_1]", $msg));
784     }
785 }
786
787 =head2 _IsValidRegex(Str $regex) returns (Bool $success, Str $msg)
788
789 Tests if the string contains an invalid regex.
790
791 =cut
792
793 sub _IsValidRegex {
794     my $self  = shift;
795     my $regex = shift or return (1, 'valid');
796
797     local $^W; local $@;
798     local $SIG{__DIE__} = sub { 1 };
799     local $SIG{__WARN__} = sub { 1 };
800
801     if (eval { qr/$regex/; 1 }) {
802         return (1, 'valid');
803     }
804
805     my $err = $@;
806     $err =~ s{[,;].*}{};    # strip debug info from error
807     chomp $err;
808     return (0, $err);
809 }
810
811
812 =head2 SingleValue
813
814 Returns true if this CustomField only accepts a single value. 
815 Returns false if it accepts multiple values
816
817 =cut
818
819 sub SingleValue {
820     my $self = shift;
821     if (($self->MaxValues||0) == 1) {
822         return 1;
823     } 
824     else {
825         return undef;
826     }
827 }
828
829 sub UnlimitedValues {
830     my $self = shift;
831     if (($self->MaxValues||0) == 0) {
832         return 1;
833     } 
834     else {
835         return undef;
836     }
837 }
838
839
840 =head2 CurrentUserHasRight RIGHT
841
842 Helper function to call the custom field's queue's CurrentUserHasRight with the passed in args.
843
844 =cut
845
846 sub CurrentUserHasRight {
847     my $self  = shift;
848     my $right = shift;
849
850     return $self->CurrentUser->HasRight(
851         Object => $self,
852         Right  => $right,
853     );
854 }
855
856 =head2 ACLEquivalenceObjects
857
858 Returns list of objects via which users can get rights on this custom field. For custom fields
859 these objects can be set using L<ContextObject|/"ContextObject and SetContextObject">.
860
861 =cut
862
863 sub ACLEquivalenceObjects {
864     my $self = shift;
865
866     my $ctx = $self->ContextObject
867         or return;
868     return ($ctx, $ctx->ACLEquivalenceObjects);
869 }
870
871 =head2 ContextObject and SetContextObject
872
873 Set or get a context for this object. It can be ticket, queue or another object
874 this CF applies to. Used for ACL control, for example SeeCustomField can be granted on
875 queue level to allow people to see all fields applied to the queue.
876
877 =cut
878
879 sub SetContextObject {
880     my $self = shift;
881     return $self->{'context_object'} = shift;
882 }
883   
884 sub ContextObject {
885     my $self = shift;
886     return $self->{'context_object'};
887 }
888
889 sub ValidContextType {
890     my $self = shift;
891     my $class = shift;
892
893     my %valid;
894     $valid{$_}++ for split '-', $self->LookupType;
895     delete $valid{'RT::Transaction'};
896
897     return $valid{$class};
898 }
899
900 =head2 LoadContextObject
901
902 Takes an Id for a Context Object and loads the right kind of RT::Object
903 for this particular Custom Field (based on the LookupType) and returns it.
904 This is a good way to ensure you don't try to use a Queue as a Context
905 Object on a User Custom Field.
906
907 =cut
908
909 sub LoadContextObject {
910     my $self = shift;
911     my $type = shift;
912     my $contextid = shift;
913
914     unless ( $self->ValidContextType($type) ) {
915         RT->Logger->debug("Invalid ContextType $type for Custom Field ".$self->Id);
916         return;
917     }
918
919     my $context_object = $type->new( $self->CurrentUser );
920     my ($id, $msg) = $context_object->LoadById( $contextid );
921     unless ( $id ) {
922         RT->Logger->debug("Invalid ContextObject id: $msg");
923         return;
924     }
925     return $context_object;
926 }
927
928 =head2 ValidateContextObject
929
930 Ensure that a given ContextObject applies to this Custom Field.
931 For custom fields that are assigned to Queues or to Classes, this checks that the Custom
932 Field is actually applied to that objects.  For Global Custom Fields, it returns true
933 as long as the Object is of the right type, because you may be using
934 your permissions on a given Queue of Class to see a Global CF.
935 For CFs that are only applied Globally, you don't need a ContextObject.
936
937 =cut
938
939 sub ValidateContextObject {
940     my $self = shift;
941     my $object = shift;
942
943     return 1 if $self->IsApplied(0);
944
945     # global only custom fields don't have objects
946     # that should be used as context objects.
947     return if $self->ApplyGlobally;
948
949     # Otherwise, make sure we weren't passed a user object that we're
950     # supposed to treat as a queue.
951     return unless $self->ValidContextType(ref $object);
952
953     # Check that it is applied correctly
954     my ($applied_to) = grep {ref($_) eq $self->RecordClassFromLookupType} ($object, $object->ACLEquivalenceObjects);
955     return unless $applied_to;
956     return $self->IsApplied($applied_to->id);
957 }
958
959
960 sub _Set {
961     my $self = shift;
962
963     unless ( $self->CurrentUserHasRight('AdminCustomField') ) {
964         return ( 0, $self->loc('Permission Denied') );
965     }
966     return $self->SUPER::_Set( @_ );
967
968 }
969
970
971
972 =head2 _Value
973
974 Takes the name of a table column.
975 Returns its value as a string, if the user passes an ACL check
976
977 =cut
978
979 sub _Value {
980     my $self  = shift;
981     return undef unless $self->id;
982
983     # we need to do the rights check
984     unless ( $self->CurrentUserHasRight('SeeCustomField') ) {
985         $RT::Logger->debug(
986             "Permission denied. User #". $self->CurrentUser->id
987             ." has no SeeCustomField right on CF #". $self->id
988         );
989         return (undef);
990     }
991     return $self->__Value( @_ );
992 }
993
994
995 =head2 SetDisabled
996
997 Takes a boolean.
998 1 will cause this custom field to no longer be avaialble for objects.
999 0 will re-enable this field.
1000
1001 =cut
1002
1003
1004 =head2 SetTypeComposite
1005
1006 Set this custom field's type and maximum values as a composite value
1007
1008 =cut
1009
1010 sub SetTypeComposite {
1011     my $self = shift;
1012     my $composite = shift;
1013
1014     my $old = $self->TypeComposite;
1015
1016     my ($type, $max_values) = split(/-/, $composite, 2);
1017     if ( $type ne $self->Type ) {
1018         my ($status, $msg) = $self->SetType( $type );
1019         return ($status, $msg) unless $status;
1020     }
1021     if ( ($max_values || 0) != ($self->MaxValues || 0) ) {
1022         my ($status, $msg) = $self->SetMaxValues( $max_values );
1023         return ($status, $msg) unless $status;
1024     }
1025     my $render = $self->RenderType;
1026     if ( $render and not grep { $_ eq $render } $self->RenderTypes ) {
1027         # We switched types and our render type is no longer valid, so unset it
1028         # and use the default
1029         $self->SetRenderType( undef );
1030     }
1031     return 1, $self->loc(
1032         "Type changed from '[_1]' to '[_2]'",
1033         $self->FriendlyTypeComposite( $old ),
1034         $self->FriendlyTypeComposite( $composite ),
1035     );
1036 }
1037
1038 =head2 TypeComposite
1039
1040 Returns a composite value composed of this object's type and maximum values
1041
1042 =cut
1043
1044
1045 sub TypeComposite {
1046     my $self = shift;
1047     return join '-', ($self->Type || ''), ($self->MaxValues || 0);
1048 }
1049
1050 =head2 TypeComposites
1051
1052 Returns an array of all possible composite values for custom fields.
1053
1054 =cut
1055
1056 sub TypeComposites {
1057     my $self = shift;
1058     return grep !/(?:[Tt]ext|Combobox|Date|DateTime)-0/, map { ("$_-1", "$_-0") } $self->Types;
1059 }
1060
1061 =head2 RenderType
1062
1063 Returns the type of form widget to render for this custom field.  Currently
1064 this only affects fields which return true for L</HasRenderTypes>. 
1065
1066 =cut
1067
1068 sub RenderType {
1069     my $self = shift;
1070     return '' unless $self->HasRenderTypes;
1071
1072     return $self->_Value( 'RenderType', @_ )
1073         || $self->DefaultRenderType;
1074 }
1075
1076 =head2 SetRenderType TYPE
1077
1078 Sets this custom field's render type.
1079
1080 =cut
1081
1082 sub SetRenderType {
1083     my $self = shift;
1084     my $type = shift;
1085     return (0, $self->loc("This custom field has no Render Types"))
1086         unless $self->HasRenderTypes;
1087
1088     if ( !$type || $type eq $self->DefaultRenderType ) {
1089         return $self->_Set( Field => 'RenderType', Value => undef, @_ );
1090     }
1091
1092     if ( not grep { $_ eq $type } $self->RenderTypes ) {
1093         return (0, $self->loc("Invalid Render Type for custom field of type [_1]",
1094                                 $self->FriendlyType));
1095     }
1096
1097     # XXX: Remove this restriction once we support lists and cascaded selects
1098     if ( $self->BasedOnObj->id and $type =~ /List/ ) {
1099         return (0, $self->loc("We can't currently render as a List when basing categories on another custom field.  Please use another render type."));
1100     }
1101
1102     return $self->_Set( Field => 'RenderType', Value => $type, @_ );
1103 }
1104
1105 =head2 DefaultRenderType [TYPE COMPOSITE]
1106
1107 Returns the default render type for this custom field's type or the TYPE
1108 COMPOSITE specified as an argument.
1109
1110 =cut
1111
1112 sub DefaultRenderType {
1113     my $self = shift;
1114     my $composite    = @_ ? shift : $self->TypeComposite;
1115     my ($type, $max) = split /-/, $composite, 2;
1116     return unless $type and $self->HasRenderTypes($composite);
1117     return $FieldTypes{$type}->{render_types}->{ $max == 1 ? 'single' : 'multiple' }[0];
1118 }
1119
1120 =head2 HasRenderTypes [TYPE_COMPOSITE]
1121
1122 Returns a boolean value indicating whether the L</RenderTypes> and
1123 L</RenderType> methods make sense for this custom field.
1124
1125 Currently true only for type C<Select>.
1126
1127 =cut
1128
1129 sub HasRenderTypes {
1130     my $self = shift;
1131     my ($type, $max) = split /-/, (@_ ? shift : $self->TypeComposite), 2;
1132     return undef unless $type;
1133     return defined $FieldTypes{$type}->{render_types}
1134         ->{ $max == 1 ? 'single' : 'multiple' };
1135 }
1136
1137 =head2 RenderTypes [TYPE COMPOSITE]
1138
1139 Returns the valid render types for this custom field's type or the TYPE
1140 COMPOSITE specified as an argument.
1141
1142 =cut
1143
1144 sub RenderTypes {
1145     my $self = shift;
1146     my $composite    = @_ ? shift : $self->TypeComposite;
1147     my ($type, $max) = split /-/, $composite, 2;
1148     return unless $type and $self->HasRenderTypes($composite);
1149     return @{$FieldTypes{$type}->{render_types}->{ $max == 1 ? 'single' : 'multiple' }};
1150 }
1151
1152 =head2 SetLookupType
1153
1154 Autrijus: care to doc how LookupTypes work?
1155
1156 =cut
1157
1158 sub SetLookupType {
1159     my $self = shift;
1160     my $lookup = shift;
1161     if ( $lookup ne $self->LookupType ) {
1162         # Okay... We need to invalidate our existing relationships
1163         my $ObjectCustomFields = RT::ObjectCustomFields->new($self->CurrentUser);
1164         $ObjectCustomFields->LimitToCustomField($self->Id);
1165         $_->Delete foreach @{$ObjectCustomFields->ItemsArrayRef};
1166     }
1167     return $self->_Set(Field => 'LookupType', Value =>$lookup);
1168 }
1169
1170 =head2 LookupTypes
1171
1172 Returns an array of LookupTypes available
1173
1174 =cut
1175
1176
1177 sub LookupTypes {
1178     my $self = shift;
1179     return keys %FRIENDLY_OBJECT_TYPES;
1180 }
1181
1182 my @FriendlyObjectTypes = (
1183     "[_1] objects",            # loc
1184     "[_1]'s [_2] objects",        # loc
1185     "[_1]'s [_2]'s [_3] objects",   # loc
1186 );
1187
1188 =head2 FriendlyLookupType
1189
1190 Returns a localized description of the type of this custom field
1191
1192 =cut
1193
1194 sub FriendlyLookupType {
1195     my $self = shift;
1196     my $lookup = shift || $self->LookupType;
1197    
1198     return ($self->loc( $FRIENDLY_OBJECT_TYPES{$lookup} ))
1199                      if (defined  $FRIENDLY_OBJECT_TYPES{$lookup} );
1200
1201     my @types = map { s/^RT::// ? $self->loc($_) : $_ }
1202       grep { defined and length }
1203       split( /-/, $lookup )
1204       or return;
1205     return ( $self->loc( $FriendlyObjectTypes[$#types], @types ) );
1206 }
1207
1208 sub RecordClassFromLookupType {
1209     my $self = shift;
1210     my ($class) = ($self->LookupType =~ /^([^-]+)/);
1211     unless ( $class ) {
1212         $RT::Logger->error(
1213             "Custom Field #". $self->id 
1214             ." has incorrect LookupType '". $self->LookupType ."'"
1215         );
1216         return undef;
1217     }
1218     return $class;
1219 }
1220
1221 sub CollectionClassFromLookupType {
1222     my $self = shift;
1223
1224     my $record_class = $self->RecordClassFromLookupType;
1225     return undef unless $record_class;
1226
1227     my $collection_class;
1228     if ( UNIVERSAL::can($record_class.'Collection', 'new') ) {
1229         $collection_class = $record_class.'Collection';
1230     } elsif ( UNIVERSAL::can($record_class.'es', 'new') ) {
1231         $collection_class = $record_class.'es';
1232     } elsif ( UNIVERSAL::can($record_class.'s', 'new') ) {
1233         $collection_class = $record_class.'s';
1234     } else {
1235         $RT::Logger->error("Can not find a collection class for record class '$record_class'");
1236         return undef;
1237     }
1238     return $collection_class;
1239 }
1240
1241 =head1 ApplyGlobally
1242
1243 Certain custom fields (users, groups) should only be applied globally
1244 but rather than regexing in code for LookupType =~ RT::Queue, we'll codify
1245 the rules here.
1246
1247 =cut
1248
1249 sub ApplyGlobally {
1250     my $self = shift;
1251
1252     return ($self->LookupType =~ /^RT::(?:Group|User)/io);
1253
1254 }
1255
1256 =head1 AppliedTo
1257
1258 Returns collection with objects this custom field is applied to.
1259 Class of the collection depends on L</LookupType>.
1260 See all L</NotAppliedTo> .
1261
1262 Doesn't takes into account if object is applied globally.
1263
1264 =cut
1265
1266 sub AppliedTo {
1267     my $self = shift;
1268
1269     my ($res, $ocfs_alias) = $self->_AppliedTo;
1270     return $res unless $res;
1271
1272     $res->Limit(
1273         ALIAS     => $ocfs_alias,
1274         FIELD     => 'id',
1275         OPERATOR  => 'IS NOT',
1276         VALUE     => 'NULL',
1277     );
1278
1279     return $res;
1280 }
1281
1282 =head1 NotAppliedTo
1283
1284 Returns collection with objects this custom field is not applied to.
1285 Class of the collection depends on L</LookupType>.
1286 See all L</AppliedTo> .
1287
1288 Doesn't takes into account if object is applied globally.
1289
1290 =cut
1291
1292 sub NotAppliedTo {
1293     my $self = shift;
1294
1295     my ($res, $ocfs_alias) = $self->_AppliedTo;
1296     return $res unless $res;
1297
1298     $res->Limit(
1299         ALIAS     => $ocfs_alias,
1300         FIELD     => 'id',
1301         OPERATOR  => 'IS',
1302         VALUE     => 'NULL',
1303     );
1304
1305     return $res;
1306 }
1307
1308 sub _AppliedTo {
1309     my $self = shift;
1310
1311     my ($class) = $self->CollectionClassFromLookupType;
1312     return undef unless $class;
1313
1314     my $res = $class->new( $self->CurrentUser );
1315
1316     # If CF is a Group CF, only display user-defined groups
1317     if ( $class eq 'RT::Groups' ) {
1318         $res->LimitToUserDefinedGroups;
1319     }
1320
1321     $res->OrderBy( FIELD => 'Name' );
1322     my $ocfs_alias = $res->Join(
1323         TYPE   => 'LEFT',
1324         ALIAS1 => 'main',
1325         FIELD1 => 'id',
1326         TABLE2 => 'ObjectCustomFields',
1327         FIELD2 => 'ObjectId',
1328     );
1329     $res->Limit(
1330         LEFTJOIN => $ocfs_alias,
1331         ALIAS    => $ocfs_alias,
1332         FIELD    => 'CustomField',
1333         VALUE    => $self->id,
1334     );
1335     return ($res, $ocfs_alias);
1336 }
1337
1338 =head2 IsApplied
1339
1340 Takes object id and returns corresponding L<RT::ObjectCustomField>
1341 record if this custom field is applied to the object. Use 0 to check
1342 if custom field is applied globally.
1343
1344 =cut
1345
1346 sub IsApplied {
1347     my $self = shift;
1348     my $id = shift;
1349     my $ocf = RT::ObjectCustomField->new( $self->CurrentUser );
1350     $ocf->LoadByCols( CustomField => $self->id, ObjectId => $id || 0 );
1351     return undef unless $ocf->id;
1352     return $ocf;
1353 }
1354
1355 =head2 AddToObject OBJECT
1356
1357 Add this custom field as a custom field for a single object, such as a queue or group.
1358
1359 Takes an object 
1360
1361 =cut
1362
1363
1364 sub AddToObject {
1365     my $self  = shift;
1366     my $object = shift;
1367     my $id = $object->Id || 0;
1368
1369     unless (index($self->LookupType, ref($object)) == 0) {
1370         return ( 0, $self->loc('Lookup type mismatch') );
1371     }
1372
1373     unless ( $object->CurrentUserHasRight('AssignCustomFields') ) {
1374         return ( 0, $self->loc('Permission Denied') );
1375     }
1376
1377     if ( $self->IsApplied( $id ) ) {
1378         return ( 0, $self->loc("Custom field is already applied to the object") );
1379     }
1380
1381     if ( $id ) {
1382         # applying locally
1383         return (0, $self->loc("Couldn't apply custom field to an object as it's global already") )
1384             if $self->IsApplied( 0 );
1385     }
1386     else {
1387         my $applied = RT::ObjectCustomFields->new( $self->CurrentUser );
1388         $applied->LimitToCustomField( $self->id );
1389         while ( my $record = $applied->Next ) {
1390             $record->Delete;
1391         }
1392     }
1393
1394     my $ocf = RT::ObjectCustomField->new( $self->CurrentUser );
1395     my ( $oid, $msg ) = $ocf->Create(
1396         ObjectId => $id, CustomField => $self->id,
1397     );
1398     return ( $oid, $msg );
1399 }
1400
1401
1402 =head2 RemoveFromObject OBJECT
1403
1404 Remove this custom field  for a single object, such as a queue or group.
1405
1406 Takes an object 
1407
1408 =cut
1409
1410 sub RemoveFromObject {
1411     my $self = shift;
1412     my $object = shift;
1413     my $id = $object->Id || 0;
1414
1415     unless (index($self->LookupType, ref($object)) == 0) {
1416         return ( 0, $self->loc('Object type mismatch') );
1417     }
1418
1419     unless ( $object->CurrentUserHasRight('AssignCustomFields') ) {
1420         return ( 0, $self->loc('Permission Denied') );
1421     }
1422
1423     my $ocf = $self->IsApplied( $id );
1424     unless ( $ocf ) {
1425         return ( 0, $self->loc("This custom field does not apply to that object") );
1426     }
1427
1428     # XXX: Delete doesn't return anything
1429     my ( $oid, $msg ) = $ocf->Delete;
1430     return ( $oid, $msg );
1431 }
1432
1433
1434 =head2 AddValueForObject HASH
1435
1436 Adds a custom field value for a record object of some kind. 
1437 Takes a param hash of 
1438
1439 Required:
1440
1441     Object
1442     Content
1443
1444 Optional:
1445
1446     LargeContent
1447     ContentType
1448
1449 =cut
1450
1451 sub AddValueForObject {
1452     my $self = shift;
1453     my %args = (
1454         Object       => undef,
1455         Content      => undef,
1456         LargeContent => undef,
1457         ContentType  => undef,
1458         @_
1459     );
1460     my $obj = $args{'Object'} or return ( 0, $self->loc('Invalid object') );
1461
1462     unless ( $self->CurrentUserHasRight('ModifyCustomField') ) {
1463         return ( 0, $self->loc('Permission Denied') );
1464     }
1465
1466     unless ( $self->MatchPattern($args{'Content'}) ) {
1467         return ( 0, $self->loc('Input must match [_1]', $self->FriendlyPattern) );
1468     }
1469
1470     $RT::Handle->BeginTransaction;
1471
1472     if ( $self->MaxValues ) {
1473         my $current_values = $self->ValuesForObject($obj);
1474         my $extra_values = ( $current_values->Count + 1 ) - $self->MaxValues;
1475
1476         # (The +1 is for the new value we're adding)
1477
1478         # If we have a set of current values and we've gone over the maximum
1479         # allowed number of values, we'll need to delete some to make room.
1480         # which former values are blown away is not guaranteed
1481
1482         while ($extra_values) {
1483             my $extra_item = $current_values->Next;
1484             unless ( $extra_item->id ) {
1485                 $RT::Logger->crit( "We were just asked to delete "
1486                     ."a custom field value that doesn't exist!" );
1487                 $RT::Handle->Rollback();
1488                 return (undef);
1489             }
1490             $extra_item->Delete;
1491             $extra_values--;
1492         }
1493     }
1494
1495     if (my $canonicalizer = $self->can('_CanonicalizeValue'.$self->Type)) {
1496          $canonicalizer->($self, \%args);
1497     }
1498
1499
1500
1501     my $newval = RT::ObjectCustomFieldValue->new( $self->CurrentUser );
1502     my ($val, $msg) = $newval->Create(
1503         ObjectType   => ref($obj),
1504         ObjectId     => $obj->Id,
1505         Content      => $args{'Content'},
1506         LargeContent => $args{'LargeContent'},
1507         ContentType  => $args{'ContentType'},
1508         CustomField  => $self->Id
1509     );
1510
1511     unless ($val) {
1512         $RT::Handle->Rollback();
1513         return ($val, $self->loc("Couldn't create record: [_1]", $msg));
1514     }
1515
1516     $RT::Handle->Commit();
1517     return ($val);
1518
1519 }
1520
1521
1522
1523 sub _CanonicalizeValueDateTime {
1524     my $self    = shift;
1525     my $args    = shift;
1526     my $DateObj = RT::Date->new( $self->CurrentUser );
1527     $DateObj->Set( Format => 'unknown',
1528                    Value  => $args->{'Content'} );
1529     $args->{'Content'} = $DateObj->ISO;
1530 }
1531
1532 # For date, we need to store Content as ISO date
1533 sub _CanonicalizeValueDate {
1534     my $self = shift;
1535     my $args = shift;
1536
1537     # in case user input date with time, let's omit it by setting timezone
1538     # to utc so "hour" won't affect "day"
1539     my $DateObj = RT::Date->new( $self->CurrentUser );
1540     $DateObj->Set( Format   => 'unknown',
1541                    Value    => $args->{'Content'},
1542                    Timezone => 'UTC',
1543                  );
1544     $args->{'Content'} = $DateObj->Date( Timezone => 'UTC' );
1545 }
1546
1547 =head2 MatchPattern STRING
1548
1549 Tests the incoming string against the Pattern of this custom field object
1550 and returns a boolean; returns true if the Pattern is empty.
1551
1552 =cut
1553
1554 sub MatchPattern {
1555     my $self = shift;
1556     my $regex = $self->Pattern or return 1;
1557
1558     return (( defined $_[0] ? $_[0] : '') =~ $regex);
1559 }
1560
1561
1562
1563
1564 =head2 FriendlyPattern
1565
1566 Prettify the pattern of this custom field, by taking the text in C<(?#text)>
1567 and localizing it.
1568
1569 =cut
1570
1571 sub FriendlyPattern {
1572     my $self = shift;
1573     my $regex = $self->Pattern;
1574
1575     return '' unless length $regex;
1576     if ( $regex =~ /\(\?#([^)]*)\)/ ) {
1577         return '[' . $self->loc($1) . ']';
1578     }
1579     else {
1580         return $regex;
1581     }
1582 }
1583
1584
1585
1586
1587 =head2 DeleteValueForObject HASH
1588
1589 Deletes a custom field value for a ticket. Takes a param hash of Object and Content
1590
1591 Returns a tuple of (STATUS, MESSAGE). If the call succeeded, the STATUS is true. otherwise it's false
1592
1593 =cut
1594
1595 sub DeleteValueForObject {
1596     my $self = shift;
1597     my %args = ( Object => undef,
1598                  Content => undef,
1599                  Id => undef,
1600              @_ );
1601
1602
1603     unless ($self->CurrentUserHasRight('ModifyCustomField')) {
1604         return (0, $self->loc('Permission Denied'));
1605     }
1606
1607     my $oldval = RT::ObjectCustomFieldValue->new($self->CurrentUser);
1608
1609     if (my $id = $args{'Id'}) {
1610         $oldval->Load($id);
1611     }
1612     unless ($oldval->id) { 
1613         $oldval->LoadByObjectContentAndCustomField(
1614             Object => $args{'Object'}, 
1615             Content =>  $args{'Content'}, 
1616             CustomField => $self->Id,
1617         );
1618     }
1619
1620
1621     # check to make sure we found it
1622     unless ($oldval->Id) {
1623         return(0, $self->loc("Custom field value [_1] could not be found for custom field [_2]", $args{'Content'}, $self->Name));
1624     }
1625
1626     # for single-value fields, we need to validate that empty string is a valid value for it
1627     if ( $self->SingleValue and not $self->MatchPattern( '' ) ) {
1628         return ( 0, $self->loc('Input must match [_1]', $self->FriendlyPattern) );
1629     }
1630
1631     # delete it
1632
1633     my $ret = $oldval->Delete();
1634     unless ($ret) {
1635         return(0, $self->loc("Custom field value could not be found"));
1636     }
1637     return($oldval->Id, $self->loc("Custom field value deleted"));
1638 }
1639
1640
1641 =head2 ValuesForObject OBJECT
1642
1643 Return an L<RT::ObjectCustomFieldValues> object containing all of this custom field's values for OBJECT 
1644
1645 =cut
1646
1647 sub ValuesForObject {
1648     my $self = shift;
1649     my $object = shift;
1650
1651     my $values = RT::ObjectCustomFieldValues->new($self->CurrentUser);
1652     unless ($self->CurrentUserHasRight('SeeCustomField')) {
1653         # Return an empty object if they have no rights to see
1654         return ($values);
1655     }
1656     
1657     
1658     $values->LimitToCustomField($self->Id);
1659     $values->LimitToEnabled();
1660     $values->LimitToObject($object);
1661
1662     return ($values);
1663 }
1664
1665
1666 =head2 _ForObjectType PATH FRIENDLYNAME
1667
1668 Tell RT that a certain object accepts custom fields
1669
1670 Examples:
1671
1672     'RT::Queue-RT::Ticket'                 => "Tickets",                # loc
1673     'RT::Queue-RT::Ticket-RT::Transaction' => "Ticket Transactions",    # loc
1674     'RT::User'                             => "Users",                  # loc
1675     'RT::Group'                            => "Groups",                 # loc
1676
1677 This is a class method. 
1678
1679 =cut
1680
1681 sub _ForObjectType {
1682     my $self = shift;
1683     my $path = shift;
1684     my $friendly_name = shift;
1685
1686     $FRIENDLY_OBJECT_TYPES{$path} = $friendly_name;
1687
1688 }
1689
1690
1691 =head2 IncludeContentForValue [VALUE] (and SetIncludeContentForValue)
1692
1693 Gets or sets the  C<IncludeContentForValue> for this custom field. RT
1694 uses this field to automatically include content into the user's browser
1695 as they display records with custom fields in RT.
1696
1697 =cut
1698
1699 sub SetIncludeContentForValue {
1700     shift->IncludeContentForValue(@_);
1701 }
1702 sub IncludeContentForValue{
1703     my $self = shift;
1704     $self->_URLTemplate('IncludeContentForValue', @_);
1705 }
1706
1707
1708
1709 =head2 LinkValueTo [VALUE] (and SetLinkValueTo)
1710
1711 Gets or sets the  C<LinkValueTo> for this custom field. RT
1712 uses this field to make custom field values into hyperlinks in the user's
1713 browser as they display records with custom fields in RT.
1714
1715 =cut
1716
1717
1718 sub SetLinkValueTo {
1719     shift->LinkValueTo(@_);
1720 }
1721
1722 sub LinkValueTo {
1723     my $self = shift;
1724     $self->_URLTemplate('LinkValueTo', @_);
1725
1726 }
1727
1728
1729 =head2 _URLTemplate  NAME [VALUE]
1730
1731 With one argument, returns the _URLTemplate named C<NAME>, but only if
1732 the current user has the right to see this custom field.
1733
1734 With two arguments, attemptes to set the relevant template value.
1735
1736 =cut
1737
1738 sub _URLTemplate {
1739     my $self          = shift;
1740     my $template_name = shift;
1741     if (@_) {
1742
1743         my $value = shift;
1744         unless ( $self->CurrentUserHasRight('AdminCustomField') ) {
1745             return ( 0, $self->loc('Permission Denied') );
1746         }
1747         $self->SetAttribute( Name => $template_name, Content => $value );
1748         return ( 1, $self->loc('Updated') );
1749     } else {
1750         unless ( $self->id && $self->CurrentUserHasRight('SeeCustomField') ) {
1751             return (undef);
1752         }
1753
1754         my @attr = $self->Attributes->Named($template_name);
1755         my $attr = shift @attr;
1756
1757         if ($attr) { return $attr->Content }
1758
1759     }
1760 }
1761
1762 sub SetBasedOn {
1763     my $self = shift;
1764     my $value = shift;
1765
1766     return $self->_Set( Field => 'BasedOn', Value => $value, @_ )
1767         unless defined $value and length $value;
1768
1769     my $cf = RT::CustomField->new( $self->CurrentUser );
1770     $cf->SetContextObject( $self->ContextObject );
1771     $cf->Load( ref $value ? $value->id : $value );
1772
1773     return (0, "Permission denied")
1774         unless $cf->id && $cf->CurrentUserHasRight('SeeCustomField');
1775
1776     # XXX: Remove this restriction once we support lists and cascaded selects
1777     if ( $self->RenderType =~ /List/ ) {
1778         return (0, $self->loc("We can't currently render as a List when basing categories on another custom field.  Please use another render type."));
1779     }
1780
1781     return $self->_Set( Field => 'BasedOn', Value => $value, @_ )
1782 }
1783
1784 sub BasedOnObj {
1785     my $self = shift;
1786
1787     my $obj = RT::CustomField->new( $self->CurrentUser );
1788     $obj->SetContextObject( $self->ContextObject );
1789     if ( $self->BasedOn ) {
1790         $obj->Load( $self->BasedOn );
1791     }
1792     return $obj;
1793 }
1794
1795
1796
1797
1798
1799
1800 =head2 id
1801
1802 Returns the current value of id. 
1803 (In the database, id is stored as int(11).)
1804
1805
1806 =cut
1807
1808
1809 =head2 Name
1810
1811 Returns the current value of Name. 
1812 (In the database, Name is stored as varchar(200).)
1813
1814
1815
1816 =head2 SetName VALUE
1817
1818
1819 Set Name to VALUE. 
1820 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
1821 (In the database, Name will be stored as a varchar(200).)
1822
1823
1824 =cut
1825
1826
1827 =head2 Type
1828
1829 Returns the current value of Type. 
1830 (In the database, Type is stored as varchar(200).)
1831
1832
1833
1834 =head2 SetType VALUE
1835
1836
1837 Set Type to VALUE. 
1838 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
1839 (In the database, Type will be stored as a varchar(200).)
1840
1841
1842 =cut
1843
1844
1845 =head2 RenderType
1846
1847 Returns the current value of RenderType. 
1848 (In the database, RenderType is stored as varchar(64).)
1849
1850
1851
1852 =head2 SetRenderType VALUE
1853
1854
1855 Set RenderType to VALUE. 
1856 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
1857 (In the database, RenderType will be stored as a varchar(64).)
1858
1859
1860 =cut
1861
1862
1863 =head2 MaxValues
1864
1865 Returns the current value of MaxValues. 
1866 (In the database, MaxValues is stored as int(11).)
1867
1868
1869
1870 =head2 SetMaxValues VALUE
1871
1872
1873 Set MaxValues to VALUE. 
1874 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
1875 (In the database, MaxValues will be stored as a int(11).)
1876
1877
1878 =cut
1879
1880
1881 =head2 Pattern
1882
1883 Returns the current value of Pattern. 
1884 (In the database, Pattern is stored as text.)
1885
1886
1887
1888 =head2 SetPattern VALUE
1889
1890
1891 Set Pattern to VALUE. 
1892 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
1893 (In the database, Pattern will be stored as a text.)
1894
1895
1896 =cut
1897
1898
1899 =head2 Repeated
1900
1901 Returns the current value of Repeated. 
1902 (In the database, Repeated is stored as smallint(6).)
1903
1904
1905
1906 =head2 SetRepeated VALUE
1907
1908
1909 Set Repeated to VALUE. 
1910 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
1911 (In the database, Repeated will be stored as a smallint(6).)
1912
1913
1914 =cut
1915
1916
1917 =head2 BasedOn
1918
1919 Returns the current value of BasedOn. 
1920 (In the database, BasedOn is stored as int(11).)
1921
1922
1923
1924 =head2 SetBasedOn VALUE
1925
1926
1927 Set BasedOn to VALUE. 
1928 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
1929 (In the database, BasedOn will be stored as a int(11).)
1930
1931
1932 =cut
1933
1934
1935 =head2 Description
1936
1937 Returns the current value of Description. 
1938 (In the database, Description is stored as varchar(255).)
1939
1940
1941
1942 =head2 SetDescription VALUE
1943
1944
1945 Set Description to VALUE. 
1946 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
1947 (In the database, Description will be stored as a varchar(255).)
1948
1949
1950 =cut
1951
1952
1953 =head2 SortOrder
1954
1955 Returns the current value of SortOrder. 
1956 (In the database, SortOrder is stored as int(11).)
1957
1958
1959
1960 =head2 SetSortOrder VALUE
1961
1962
1963 Set SortOrder to VALUE. 
1964 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
1965 (In the database, SortOrder will be stored as a int(11).)
1966
1967
1968 =cut
1969
1970
1971 =head2 LookupType
1972
1973 Returns the current value of LookupType. 
1974 (In the database, LookupType is stored as varchar(255).)
1975
1976
1977
1978 =head2 SetLookupType VALUE
1979
1980
1981 Set LookupType to VALUE. 
1982 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
1983 (In the database, LookupType will be stored as a varchar(255).)
1984
1985
1986 =cut
1987
1988
1989 =head2 Creator
1990
1991 Returns the current value of Creator. 
1992 (In the database, Creator is stored as int(11).)
1993
1994
1995 =cut
1996
1997
1998 =head2 Created
1999
2000 Returns the current value of Created. 
2001 (In the database, Created is stored as datetime.)
2002
2003
2004 =cut
2005
2006
2007 =head2 LastUpdatedBy
2008
2009 Returns the current value of LastUpdatedBy. 
2010 (In the database, LastUpdatedBy is stored as int(11).)
2011
2012
2013 =cut
2014
2015
2016 =head2 LastUpdated
2017
2018 Returns the current value of LastUpdated. 
2019 (In the database, LastUpdated is stored as datetime.)
2020
2021
2022 =cut
2023
2024
2025 =head2 Disabled
2026
2027 Returns the current value of Disabled. 
2028 (In the database, Disabled is stored as smallint(6).)
2029
2030
2031
2032 =head2 SetDisabled VALUE
2033
2034
2035 Set Disabled to VALUE. 
2036 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
2037 (In the database, Disabled will be stored as a smallint(6).)
2038
2039
2040 =cut
2041
2042
2043
2044 sub _CoreAccessible {
2045     {
2046      
2047         id =>
2048         {read => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => ''},
2049         Name => 
2050         {read => 1, write => 1, sql_type => 12, length => 200,  is_blob => 0,  is_numeric => 0,  type => 'varchar(200)', default => ''},
2051         Type => 
2052         {read => 1, write => 1, sql_type => 12, length => 200,  is_blob => 0,  is_numeric => 0,  type => 'varchar(200)', default => ''},
2053         RenderType => 
2054         {read => 1, write => 1, sql_type => 12, length => 64,  is_blob => 0,  is_numeric => 0,  type => 'varchar(64)', default => ''},
2055         MaxValues => 
2056         {read => 1, write => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => ''},
2057         Pattern => 
2058         {read => 1, write => 1, sql_type => -4, length => 0,  is_blob => 1,  is_numeric => 0,  type => 'text', default => ''},
2059         Repeated => 
2060         {read => 1, write => 1, sql_type => 5, length => 6,  is_blob => 0,  is_numeric => 1,  type => 'smallint(6)', default => '0'},
2061         BasedOn => 
2062         {read => 1, write => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => ''},
2063         Description => 
2064         {read => 1, write => 1, sql_type => 12, length => 255,  is_blob => 0,  is_numeric => 0,  type => 'varchar(255)', default => ''},
2065         SortOrder => 
2066         {read => 1, write => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => '0'},
2067         LookupType => 
2068         {read => 1, write => 1, sql_type => 12, length => 255,  is_blob => 0,  is_numeric => 0,  type => 'varchar(255)', default => ''},
2069         Creator => 
2070         {read => 1, auto => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => '0'},
2071         Created => 
2072         {read => 1, auto => 1, sql_type => 11, length => 0,  is_blob => 0,  is_numeric => 0,  type => 'datetime', default => ''},
2073         LastUpdatedBy => 
2074         {read => 1, auto => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => '0'},
2075         LastUpdated => 
2076         {read => 1, auto => 1, sql_type => 11, length => 0,  is_blob => 0,  is_numeric => 0,  type => 'datetime', default => ''},
2077         Disabled => 
2078         {read => 1, write => 1, sql_type => 5, length => 6,  is_blob => 0,  is_numeric => 1,  type => 'smallint(6)', default => '0'},
2079
2080  }
2081 };
2082
2083
2084 RT::Base->_ImportOverlays();
2085
2086 1;