/[H6]/AdminDialog.cpp
ViewVC logotype

Diff of /AdminDialog.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.5  
changed lines
  Added in v.31

  ViewVC Help
Powered by ViewVC 1.1.20