-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdlgNewSession.pas
More file actions
166 lines (145 loc) · 4.28 KB
/
dlgNewSession.pas
File metadata and controls
166 lines (145 loc) · 4.28 KB
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
unit dlgNewsession;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, FireDAC.Stan.Intf, FireDAC.Stan.Option,
FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf,
FireDAC.DApt.Intf, FireDAC.Stan.Async, FireDAC.DApt, Data.DB,
FireDAC.Comp.DataSet, FireDAC.Comp.Client, Vcl.StdCtrls, Vcl.WinXPickers,
Vcl.Mask, Vcl.DBCtrls, Vcl.ExtCtrls, SCMDefines;
type
scmSessionMode = (smEditSession, smNewSession, swNotGiven);
TNewSession = class(TForm)
Panel1: TPanel;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Panel2: TPanel;
btnCancel: TButton;
btnPost: TButton;
DatePicker1: TDatePicker;
TimePicker1: TTimePicker;
DBEdit1: TDBEdit;
qrySessionDlg: TFDQuery;
dsSessionDlg: TDataSource;
procedure btnPostClick(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure btnCancelClick(Sender: TObject);
private
{ Private declarations }
fSessionMode: scmSessionMode;
fSessionID: integer;
fConnection: TFDConnection;
function LocateSession(ASessionID: Integer): Boolean;
public
{ Public declarations }
constructor CreateWithConnection(AOwner: TComponent;
aConnection: TFDConnection);
property SessionMode: scmSessionMode read fSessionMode write fSessionMode;
property SessionID: integer read fSessionID write fSessionID;
end;
var
NewSession: TNewSession;
implementation
{$R *.dfm}
uses
System.DateUtils;
procedure TNewSession.btnCancelClick(Sender: TObject);
begin
dsSessionDlg.DataSet.Cancel;
fSessionID := 0;
ModalResult := mrCancel;
end;
procedure TNewSession.btnPostClick(Sender: TObject);
var
dt: TDateTime;
begin
with dsSessionDlg.DataSet do
begin
// Finalise the edit. Note NewRecord is assigned by SCM, SessionStatusID = 1;
if (State = dsEdit) or (State = dsInsert) then
begin
dt := DatePicker1.Date + TimePicker1.Time;
// Manually assign date - only if modified.
if FieldByName('SessionStart').AsDateTime <> dt then
FieldByName('SessionStart').AsDateTime := dt;
FieldByName('SessionStatusID').AsInteger := 1;
FieldByName('SwimClubID').AsInteger := 1;
// finalise the changes...
Post;
end;
if (State = dsEdit) then fSessionID := FieldByName('SessionID').AsInteger;
ModalResult := mrOk;
end;
end;
constructor TNewSession.CreateWithConnection(AOwner: TComponent;
aConnection: TFDConnection);
begin
inherited Create(AOwner);
fConnection := aConnection;
end;
procedure TNewSession.FormCreate(Sender: TObject);
begin
if not Assigned(fConnection) then
raise Exception.Create('Connection not assigned.');
qrySessionDlg.Connection := fConnection;
qrySessionDlg.Open;
DBEdit1.DataSource := dsSessionDlg;
DBEdit1.DataField := 'Caption';
fSessionMode := swNotGiven;
end;
procedure TNewSession.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_ESCAPE then
begin
dsSessionDlg.DataSet.Cancel;
Key := 0;
fSessionID := 0;
ModalResult := mrCancel;
end;
end;
procedure TNewSession.FormShow(Sender: TObject);
var
dt: TDateTime;
begin
case fSessionMode of
smEditSession:
begin
if not LocateSession(fSessionID) then Close;
Caption := 'Edit session ...';
with dsSessionDlg.DataSet do
begin
dt := FieldByName('SessionStart').AsDateTime;
DatePicker1.Date := DateOf(dt);
TimePicker1.Time := TimeOf(dt);
// - - e d i t - -
if State <> dsEdit then
Edit;
end;
end;
smNewSession:
begin
Caption := 'Create new session ...';
// - - i n s e r t - -
dsSessionDlg.DataSet.Insert;
DatePicker1.Date := Date;
TimePicker1.Time := Time;
end;
swNotGiven:
Close;
end;
end;
function TNewSession.LocateSession(ASessionID: Integer): Boolean;
var
SearchOptions: TLocateOptions;
begin
result := false;
if not qrySessionDlg.Active then exit;
SearchOptions := [];
result := qrySessionDlg.Locate('SessionID', ASessionID, SearchOptions);
end;
end.