/[H6]/AdminDialog.cpp
ViewVC logotype

Annotation of /AdminDialog.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 13 - (hide annotations) (download)
Thu Aug 31 15:57:02 2006 UTC (17 years, 7 months ago) by torben
File size: 6710 byte(s)
Minor fix to deletion of equipment
1 torben 5 // AdminDialog.cpp : implementation file
2     //
3    
4     #include "stdafx.h"
5     #include "h6-udlånssystem.h"
6     #include "AdminDialog.h"
7     #include ".\admindialog.h"
8    
9 torben 9 #include "databaselayer.h"
10     #include "commonstorage.h"
11     #include "Containers.h"
12 torben 5
13 torben 9 #include "UserEditDialog.h"
14     #include "EquipmentEditDialog.h"
15    
16 torben 5 // AdminDialog dialog
17    
18     IMPLEMENT_DYNAMIC(AdminDialog, CDialog)
19     AdminDialog::AdminDialog(CWnd* pParent /*=NULL*/)
20     : CDialog(AdminDialog::IDD, pParent)
21     {
22     }
23    
24     AdminDialog::~AdminDialog()
25     {
26     }
27    
28     void AdminDialog::DoDataExchange(CDataExchange* pDX)
29     {
30     CDialog::DoDataExchange(pDX);
31     }
32    
33    
34     BEGIN_MESSAGE_MAP(AdminDialog, CDialog)
35 torben 9 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 torben 5 END_MESSAGE_MAP()
43    
44    
45     // AdminDialog message handlers
46    
47     void AdminDialog::OnCancel()
48     {
49     // TODO: Add your specialized code here and/or call the base class
50    
51     //CDialog::OnCancel();
52     }
53    
54     void AdminDialog::OnOK()
55     {
56     // TODO: Add your specialized code here and/or call the base class
57    
58     //CDialog::OnOK();
59     }
60 torben 9
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 torben 10 MessageBox("You must select an user/equipment to delete");
240 torben 9 return;
241     }
242    
243 torben 10 DatabaseLayer *dblayer = CommonStorage::Instance()->getDBLayer();
244    
245     CString id = list->GetItemText(sel,0);
246 torben 9 CString name = list->GetItemText(sel,1);
247 torben 10 CString question;
248     question.Format("Are you sure you want to delete %s : %s ?", id,name);
249 torben 9
250 torben 10 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 torben 13 Equipment e;
259     e.barcode = id;
260     dblayer->DeleteEquipment(e);
261 torben 10 LoadEquipment();
262     }
263 torben 9 }
264     }

  ViewVC Help
Powered by ViewVC 1.1.20