-
Notifications
You must be signed in to change notification settings - Fork 0
/
unitaddphone.pas
199 lines (165 loc) · 4.79 KB
/
unitaddphone.pas
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
unit UnitAddPHone;
{$mode ObjFPC}{$H+}
interface
uses
UnitData, Utils,
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
type
{ TFrmAddPhone }
TFrmAddPhone = class(TForm)
ButtonSave: TButton;
ButtonCancel: TButton;
ComboBoxType: TComboBox;
EditNumber: TEdit;
LabelUserPrompt: TLabel;
LabelType: TLabel;
LabelNumber: TLabel;
procedure ButtonSaveClick(Sender: TObject);
procedure ButtonCancelClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure InitializeTypeDropDown();
// TODO: Move the editMode stuff to a base class and then
// inherit from it here and in UnitAddContact
private
mEditMode: Boolean;
mPersonId: Integer;
mPhoneId: Integer;
mPhoneTypeIds : Array of integer;
public
function GetEditMode(): Boolean;
procedure SetEditMode(Value : Boolean);
procedure SetPersonId(Id: Integer);
procedure SetPhoneId(Id: Integer);
procedure SetPhoneTypeId(Id: Integer);
end;
var
FrmAddPhone: TFrmAddPhone;
implementation
{$R *.lfm}
{ TFrmAddPhone }
procedure TFrmAddPhone.ButtonCancelClick(Sender: TObject);
begin
self.close();
end;
procedure TFrmAddPhone.ButtonSaveClick(Sender: TObject);
var
Number: Int64;
begin
Number := ValidatePhone(EditNumber.Text);
if ( Number = -1 ) then
begin
MessageDlg('Must have a valid 10 digit phone number to save!', mtInformation, mbOkCancel, 0);
end
else
begin
if (mEditMode = false) then
begin
WriteLn(Format('Add Phone: PersonId: %d Ph Type Index: %d, Ph: %d',
[mPersonId, mPhoneTypeIds[ComboBoxType.ItemIndex], Number]));
Utils.AddPhone(DataModule1.QueryInsert, mPersonId, IntToStr(Number), mPhoneTypeIds[ComboBoxType.ItemIndex]);
end
else
begin
WriteLn(Format('Edit Phone: Id: %d, PersonId: %d Ph Type Index: %d, Ph: %s',
[mPhoneId, mPersonId, mPhoneTypeIds[ComboBoxType.ItemIndex], EditNumber.Text]));
Utils.EditPhone(DataModule1.QueryInsert, mPhoneId, mPersonId, EditNumber.Text, mPhoneTypeIds[ComboBoxType.ItemIndex]);
end;
self.close();
end;
end;
procedure TFrmAddPhone.FormCreate(Sender: TObject);
begin
InitializeTypeDropDown();
end;
procedure TFrmAddPhone.FormShow(Sender: TObject);
begin
if (mEditMode = true) then
begin
LabelUserPrompt.Caption := 'Editing Existing Phone:';
FrmAddPhone.Caption := 'Edit Phone';
end
else
begin
LabelUserPrompt.Caption := 'Add New Phone?';
FrmAddPhone.Caption := 'Add Phone';
end;
end;
procedure TFrmAddPhone.InitializeTypeDropDown();
var
count : LongInt; // Note: Count is a long int, but we only expect a few (< 10 rows)
i : Integer;
begin
try
try
i := 0;
ComboBoxType.Items.Clear();
ComboBoxType.Items.BeginUpdate;
try
DataModule1.InitializePhoneTypesQuery();
count := DataModule1.QueryPhoneType.RecordCount;
SetLength(mPhoneTypeIds, count);
while not DataModule1.QueryPhoneType.EOF do
begin
ComboBoxType.Items.Add(DataModule1.QueryPhoneType.FieldByName('Type').AsString);
mPhoneTypeIds[i] := DataModule1.QueryPhoneType.FieldByName('Id').AsInteger;
WriteLn(Format('mPhoneTypeIds[%d] = %d', [i, mPhoneTypeIds[i]]));
i := i + 1;
DataModule1.QueryPhoneType.Next;
end;
finally
ComboBoxType.Items.EndUpdate;
ComboBoxType.ItemIndex := 0;
end;
finally
DataModule1.QueryPhoneType.Close;
end;
except
on E: Exception do
begin
Utils.ShowException(E);
end;
end;
end;
function TFrmAddPhone.GetEditMode(): Boolean;
begin
GetEditMode := mEditMode;
end;
procedure TFrmAddPhone.SetEditMode(Value : Boolean);
begin
if (mEditMode <> Value) then
begin
mEditMode := value;
end;
end;
procedure TFrmAddPhone.SetPersonId(Id: Integer);
begin
mPersonId := Id;
end;
procedure TFrmAddPhone.SetPhoneId(Id: Integer);
begin
mPhoneId := Id;
end;
(* SetPhoneTypeId
*
* @param: Id : Actual Phone type ID from PhoneTypes Table
*
* Sets ComboBox ItemIndex to match the selected Item Id using mPhoneTypeIds[]
*)
procedure TFrmAddPhone.SetPhoneTypeId(Id: Integer);
var
i: Integer;
begin
i := ComboBoxType.Items.Count;
while (i > 0) do
begin
Writeln(Format('Comparing %d', [mPhoneTypeIds[i-1]]));
if (mPhoneTypeIds[i-1] = Id) then
begin
Writeln('Matches: ', Id);
ComboBoxType.ItemIndex := i-1;
end;
i := i - 1;
end;
end;
end.