import torrus 1.0.9
[freeside.git] / FS / FS / phone_type.pm
1 package FS::phone_type;
2
3 use strict;
4 use base qw( FS::Record );
5 use FS::Record qw( qsearch ); # qsearchs );
6
7 =head1 NAME
8
9 FS::phone_type - Object methods for phone_type records
10
11 =head1 SYNOPSIS
12
13   use FS::phone_type;
14
15   $record = new FS::phone_type \%hash;
16   $record = new FS::phone_type { 'column' => 'value' };
17
18   $error = $record->insert;
19
20   $error = $new_record->replace($old_record);
21
22   $error = $record->delete;
23
24   $error = $record->check;
25
26 =head1 DESCRIPTION
27
28 An FS::phone_type object represents an phone number type (for example: Work,
29 Home, Mobile, Fax).  FS::phone_type inherits from FS::Record.  The following
30 fields are currently supported:
31
32 =over 4
33
34 =item phonetypenum
35
36 Primary key
37
38 =item typename
39
40 Type name
41
42 =back
43
44 =head1 METHODS
45
46 =over 4
47
48 =item new HASHREF
49
50 Creates a new type.  To add the type to the database, see L<"insert">.
51
52 Note that this stores the hash reference, not a distinct copy of the hash it
53 points to.  You can ask the object for a copy with the I<hash> method.
54
55 =cut
56
57 sub table { 'phone_type'; }
58
59 =item insert
60
61 Adds this record to the database.  If there is an error, returns the error,
62 otherwise returns false.
63
64 =item delete
65
66 Delete this record from the database.
67
68 =item replace OLD_RECORD
69
70 Replaces the OLD_RECORD with this one in the database.  If there is an error,
71 returns the error, otherwise returns false.
72
73 =item check
74
75 Checks all fields to make sure this is a valid type.  If there is
76 an error, returns the error, otherwise returns false.  Called by the insert
77 and replace methods.
78
79 =cut
80
81 sub check {
82   my $self = shift;
83
84   my $error = 
85     $self->ut_numbern('phonetypenum')
86     || $self->ut_number('weight')
87     || $self->ut_text('typename')
88   ;
89   return $error if $error;
90
91   $self->SUPER::check;
92 }
93
94 # Used by FS::Setup to initialize a new database.
95 sub _populate_initial_data {
96   my ($class, %opts) = @_;
97
98   my $weight = 10;
99
100   foreach ("Work", "Home", "Mobile", "Fax") {
101     my $object = $class->new({ 'typename' => $_,
102                                'weight'   => $weight,
103                             });
104     my $error = $object->insert;
105     die "error inserting $class into database: $error\n"
106       if $error;
107
108     $weight += 10;
109   }
110
111   '';
112
113 }
114
115 # Used by FS::Upgrade to migrate to a new database.
116 sub _upgrade_data {
117   my $class = shift;
118
119   return $class->_populate_initial_data(@_)
120     unless scalar( qsearch( 'phone_type', {} ) );
121
122   '';
123
124 }
125
126 =back
127
128 =head1 BUGS
129
130 =head1 SEE ALSO
131
132 L<FS::contact_phone>, L<FS::Record>, schema.html from the base documentation.
133
134 =cut
135
136 1;
137