fix TeleAPI import (what kind of crack was Christopher smoking that he couldn't fix...
[freeside.git] / FS / FS / phone_type.pm
1 package FS::phone_type;
2 use base qw( FS::Record );
3
4 use strict;
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 =item get_phone_types
95
96 returns a list of phone_types.
97
98 =cut
99
100 sub get_phone_types {
101   ## not using Home and Fax right now. false laziness with  /elements/contact.html
102   my @phone_types = qsearch({table=>'phone_type', order_by=>'ORDER BY weight DESC', extra_sql => " WHERE typename NOT IN ('Home','Fax')"});
103   return @phone_types;
104 }
105
106 # Used by FS::Setup to initialize a new database.
107 sub _populate_initial_data {
108   my ($class, %opts) = @_;
109
110   my $weight = 10;
111
112   foreach ("Work", "Home", "Mobile", "Fax") {
113     my $object = $class->new({ 'typename' => $_,
114                                'weight'   => $weight,
115                             });
116     my $error = $object->insert;
117     die "error inserting $class into database: $error\n"
118       if $error;
119
120     $weight += 10;
121   }
122
123   '';
124
125 }
126
127 # Used by FS::Upgrade to migrate to a new database.
128 sub _upgrade_data {
129   my $class = shift;
130
131   return $class->_populate_initial_data(@_)
132     unless scalar( qsearch( 'phone_type', {} ) );
133
134   '';
135
136 }
137
138 =back
139
140 =head1 BUGS
141
142 =head1 SEE ALSO
143
144 L<FS::contact_phone>, L<FS::Record>, schema.html from the base documentation.
145
146 =cut
147
148 1;
149