6 |
#include "AdminDialog.h" |
#include "AdminDialog.h" |
7 |
#include ".\admindialog.h" |
#include ".\admindialog.h" |
8 |
|
|
9 |
|
#include "databaselayer.h" |
10 |
|
#include "commonstorage.h" |
11 |
|
#include "Containers.h" |
12 |
|
|
13 |
|
#include "UserEditDialog.h" |
14 |
|
#include "EquipmentEditDialog.h" |
15 |
|
|
16 |
// AdminDialog dialog |
// AdminDialog dialog |
17 |
|
|
32 |
|
|
33 |
|
|
34 |
BEGIN_MESSAGE_MAP(AdminDialog, CDialog) |
BEGIN_MESSAGE_MAP(AdminDialog, CDialog) |
35 |
|
ON_BN_CLICKED(IDC_LOGIN, OnBnClickedLogin) |
36 |
|
ON_BN_CLICKED(IDC_LOGOUT, OnBnClickedLogout) |
37 |
|
ON_BN_CLICKED(IDC_RDUSERS, OnBnClickedRdusers) |
38 |
|
ON_BN_CLICKED(IDC_RDEQUIPMENT, OnBnClickedRdequipment) |
39 |
|
ON_BN_CLICKED(IDC_NEW, OnBnClickedNew) |
40 |
|
ON_BN_CLICKED(IDC_MODIFY, OnBnClickedModify) |
41 |
|
ON_BN_CLICKED(IDC_DELETE, OnBnClickedDelete) |
42 |
END_MESSAGE_MAP() |
END_MESSAGE_MAP() |
43 |
|
|
44 |
|
|
57 |
|
|
58 |
//CDialog::OnOK(); |
//CDialog::OnOK(); |
59 |
} |
} |
60 |
|
|
61 |
|
void AdminDialog::OnBnClickedLogin() |
62 |
|
{ |
63 |
|
CString username, password; |
64 |
|
GetDlgItem(IDC_USERNAME)->GetWindowText(username); |
65 |
|
GetDlgItem(IDC_PASSWORD)->GetWindowText(password); |
66 |
|
|
67 |
|
if (username.GetLength()>0) |
68 |
|
{ |
69 |
|
Person p = CommonStorage::Instance()->getDBLayer()->GetPerson(username); |
70 |
|
if ( (p.inits.GetLength()>0) && (p.pass == password) && (p.isadmin == 1)) |
71 |
|
{ |
72 |
|
CommonStorage::Instance()->setAdmin(true); |
73 |
|
|
74 |
|
ShowControls(true); |
75 |
|
((CButton*)GetDlgItem(IDC_RDUSERS))->SetCheck(true); |
76 |
|
LoadUsers(); |
77 |
|
} |
78 |
|
} |
79 |
|
GetDlgItem(IDC_USERNAME)->SetWindowText(""); |
80 |
|
GetDlgItem(IDC_PASSWORD)->SetWindowText(""); |
81 |
|
} |
82 |
|
|
83 |
|
void AdminDialog::ShowControls(bool login) |
84 |
|
{ |
85 |
|
GetDlgItem(IDC_USERNAME)->ShowWindow(!login); |
86 |
|
GetDlgItem(IDC_PASSWORD)->ShowWindow(!login); |
87 |
|
GetDlgItem(IDC_STATIC_USERNAME)->ShowWindow(!login); |
88 |
|
GetDlgItem(IDC_STATIC_PASSWORD)->ShowWindow(!login); |
89 |
|
GetDlgItem(IDC_LOGIN)->ShowWindow(!login); |
90 |
|
|
91 |
|
GetDlgItem(IDC_NEW)->ShowWindow(login); |
92 |
|
GetDlgItem(IDC_MODIFY)->ShowWindow(login); |
93 |
|
GetDlgItem(IDC_DELETE)->ShowWindow(login); |
94 |
|
GetDlgItem(IDC_LOGOUT)->ShowWindow(login); |
95 |
|
GetDlgItem(IDC_RDUSERS)->ShowWindow(login); |
96 |
|
GetDlgItem(IDC_RDEQUIPMENT)->ShowWindow(login); |
97 |
|
GetDlgItem(IDC_LIST)->ShowWindow(login); |
98 |
|
} |
99 |
|
|
100 |
|
void AdminDialog::OnBnClickedLogout() |
101 |
|
{ |
102 |
|
CommonStorage::Instance()->setAdmin(false); |
103 |
|
ShowControls(false); |
104 |
|
} |
105 |
|
|
106 |
|
BOOL AdminDialog::OnInitDialog() |
107 |
|
{ |
108 |
|
CDialog::OnInitDialog(); |
109 |
|
|
110 |
|
// TODO: Add extra initialization here |
111 |
|
CListCtrl *list = (CListCtrl *) GetDlgItem(IDC_LIST); |
112 |
|
list->SetExtendedStyle( LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT ); |
113 |
|
|
114 |
|
|
115 |
|
return TRUE; // return TRUE unless you set the focus to a control |
116 |
|
// EXCEPTION: OCX Property Pages should return FALSE |
117 |
|
} |
118 |
|
|
119 |
|
void AdminDialog::OnBnClickedRdusers() |
120 |
|
{ |
121 |
|
LoadUsers(); |
122 |
|
} |
123 |
|
|
124 |
|
void AdminDialog::OnBnClickedRdequipment() |
125 |
|
{ |
126 |
|
LoadEquipment(); |
127 |
|
} |
128 |
|
|
129 |
|
void AdminDialog::LoadEquipment(void) |
130 |
|
{ |
131 |
|
CListCtrl *list = (CListCtrl*) GetDlgItem(IDC_LIST); |
132 |
|
list->DeleteAllItems(); |
133 |
|
list->DeleteColumn(0); |
134 |
|
list->DeleteColumn(1); |
135 |
|
|
136 |
|
list->InsertColumn(0,"Barcode"); |
137 |
|
list->InsertColumn(1,"Name"); |
138 |
|
|
139 |
|
vector<Equipment> buffer = CommonStorage::Instance()->getDBLayer()->GetEquipmentAll(); |
140 |
|
|
141 |
|
for (unsigned int i=0; i < buffer.size(); i++) { |
142 |
|
int nItem = list->InsertItem(0, buffer[i].barcode); |
143 |
|
list->SetItemText(nItem,1,buffer[i].name); |
144 |
|
} |
145 |
|
|
146 |
|
list->DeleteColumn(2); |
147 |
|
list->DeleteColumn(3); |
148 |
|
list->SetColumnWidth(0, LVSCW_AUTOSIZE); |
149 |
|
list->SetColumnWidth(1, 200); |
150 |
|
} |
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
void AdminDialog::LoadUsers(void) |
155 |
|
{ |
156 |
|
CListCtrl *list = (CListCtrl*) GetDlgItem(IDC_LIST); |
157 |
|
list->DeleteAllItems(); |
158 |
|
|
159 |
|
list->DeleteColumn(0); |
160 |
|
list->DeleteColumn(1); |
161 |
|
|
162 |
|
|
163 |
|
list->InsertColumn(0,"Inits"); |
164 |
|
list->InsertColumn(1,"Name"); |
165 |
|
|
166 |
|
vector<Person> buffer = CommonStorage::Instance()->getDBLayer()->GetPersonAll(); |
167 |
|
|
168 |
|
for (unsigned int i=0; i < buffer.size(); i++) { |
169 |
|
int nItem = list->InsertItem(0, buffer[i].inits); |
170 |
|
list->SetItemText(nItem,1,buffer[i].name); |
171 |
|
} |
172 |
|
|
173 |
|
list->DeleteColumn(2); |
174 |
|
list->DeleteColumn(3); |
175 |
|
|
176 |
|
list->SetColumnWidth(0, LVSCW_AUTOSIZE); |
177 |
|
list->SetColumnWidth(1, 220); |
178 |
|
} |
179 |
|
|
180 |
|
void AdminDialog::OnBnClickedNew() |
181 |
|
{ |
182 |
|
if ( ((CButton*)GetDlgItem(IDC_RDUSERS))->GetCheck() ) { |
183 |
|
UserEditDialog user; |
184 |
|
|
185 |
|
if (user.DoModal() == IDOK) { |
186 |
|
CommonStorage::Instance()->getDBLayer()->AddPerson( user.m_currentPerson ); |
187 |
|
LoadUsers(); |
188 |
|
} |
189 |
|
|
190 |
|
} else { //equipment section |
191 |
|
EquipmentEditDialog equipment; |
192 |
|
|
193 |
|
if (equipment.DoModal() == IDOK) { |
194 |
|
CommonStorage::Instance()->getDBLayer()->AddEquipment( equipment.m_currentEquipment ); |
195 |
|
LoadEquipment(); |
196 |
|
} |
197 |
|
} |
198 |
|
} |
199 |
|
|
200 |
|
void AdminDialog::OnBnClickedModify() |
201 |
|
{ |
202 |
|
CListCtrl *list = (CListCtrl*) GetDlgItem(IDC_LIST); |
203 |
|
int sel = list->GetSelectionMark(); |
204 |
|
if (sel == -1) { |
205 |
|
MessageBox("You must select an user/equipment to modify"); |
206 |
|
return; |
207 |
|
} |
208 |
|
|
209 |
|
DatabaseLayer *dblayer = CommonStorage::Instance()->getDBLayer(); |
210 |
|
|
211 |
|
CString selected = list->GetItemText(sel,0); |
212 |
|
|
213 |
|
if ( ((CButton*)GetDlgItem(IDC_RDUSERS))->GetCheck() ) { |
214 |
|
UserEditDialog user; |
215 |
|
user.m_currentPerson = dblayer->GetPerson(selected); |
216 |
|
|
217 |
|
if (user.DoModal() == IDOK) { |
218 |
|
dblayer->UpdatePerson( user.m_currentPerson); |
219 |
|
LoadUsers(); |
220 |
|
} |
221 |
|
|
222 |
|
} else { //equipment section |
223 |
|
EquipmentEditDialog equipment; |
224 |
|
equipment.m_currentEquipment = dblayer->GetEquipment(selected); |
225 |
|
|
226 |
|
if (equipment.DoModal() == IDOK) { |
227 |
|
dblayer->UpdateEquipment( equipment.m_currentEquipment ); |
228 |
|
LoadEquipment(); |
229 |
|
} |
230 |
|
} |
231 |
|
} |
232 |
|
|
233 |
|
void AdminDialog::OnBnClickedDelete() |
234 |
|
{ |
235 |
|
CListCtrl *list = (CListCtrl*) GetDlgItem(IDC_LIST); |
236 |
|
int sel = list->GetSelectionMark(); |
237 |
|
|
238 |
|
if (sel == -1) { |
239 |
|
MessageBox("You must select an user/equipment to delete"); |
240 |
|
return; |
241 |
|
} |
242 |
|
|
243 |
|
DatabaseLayer *dblayer = CommonStorage::Instance()->getDBLayer(); |
244 |
|
|
245 |
|
CString id = list->GetItemText(sel,0); |
246 |
|
CString name = list->GetItemText(sel,1); |
247 |
|
CString question; |
248 |
|
question.Format("Are you sure you want to delete %s : %s ?", id,name); |
249 |
|
|
250 |
|
if ( ((CButton*)GetDlgItem(IDC_RDUSERS))->GetCheck() ) { |
251 |
|
if (MessageBox(question, "Delete user",MB_YESNO) == IDYES) { |
252 |
|
Person p = CommonStorage::Instance()->getDBLayer()->GetPerson(id); |
253 |
|
CommonStorage::Instance()->getDBLayer()->DeletePerson(p); |
254 |
|
LoadUsers(); |
255 |
|
} |
256 |
|
} else { |
257 |
|
if (MessageBox(question,"Delete equipment", MB_YESNO) == IDYES) { |
258 |
|
dblayer->DeleteEquipment(id); |
259 |
|
LoadEquipment(); |
260 |
|
} |
261 |
|
} |
262 |
|
} |