Files

307 lines
6.7 KiB
C++

// ZonesDlg.cpp : implementation file
//
#include "stdafx.h"
#include "sp.h"
#include "ZonesDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//memory leak debugging help
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
/////////////////////////////////////////////////////////////////////////////
// CZonesDlg dialog
CZonesDlg::CZonesDlg(CWnd* pParent /*=NULL*/)
: CDialog(CZonesDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CZonesDlg)
//}}AFX_DATA_INIT
m_pApp= (CSpApp*)AfxGetApp();
//Initialize recordset pointer
rs=m_pApp->rsPool->GetRS("CZonesDlg");
}
CZonesDlg::~CZonesDlg()
{
m_pApp->rsPool->ReleaseRS(&rs->m_nID);
}
void CZonesDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CZonesDlg)
DDX_Control(pDX, IDC_ZONE_LIST_LABEL, m_lblZoneList);
DDX_Control(pDX, IDC_ZONE_NAME, m_edZoneName);
DDX_Control(pDX, IDC_ZONE_DESCRIPTION, m_edZoneDescription);
DDX_Control(pDX, IDC_ZONE_COMBO, m_cbZoneList);
DDX_Control(pDX, IDC_ZONE_DELETE, m_btnZoneDelete);
DDX_Control(pDX, IDC_ZONE_ADD, m_btnZoneAdd);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CZonesDlg, CDialog)
//{{AFX_MSG_MAP(CZonesDlg)
ON_BN_CLICKED(IDC_ZONE_DELETE, OnZoneDelete)
ON_BN_CLICKED(IDC_ZONE_ADD, OnZoneAdd)
ON_CBN_CLOSEUP(IDC_ZONE_COMBO, OnCloseupZoneCombo)
ON_EN_KILLFOCUS(IDC_ZONE_DESCRIPTION, OnKillfocusZoneDescription)
ON_EN_KILLFOCUS(IDC_ZONE_NAME, OnKillfocusZoneName)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CZonesDlg message handlers
void CZonesDlg::OnZoneDelete()
{
CString q,strData;
q.Format("SELECT * FROM clients WHERE (czone = %s );",m_cbZoneList.GetCurrentRowID());
rs->Query(q);
if(rs->IsEmpty())//no clients in that zone
{
q.Format("DELETE zones.*, zones.id FROM zones WHERE (((zones.id)=%s));",m_cbZoneList.GetCurrentRowID());
if(AfxMessageBox("Are you sure?",MB_YESNO)==IDYES)
{
rs->Ex(q);
//a deleted zone can't be selected
m_strSelectedZone="";
}
}
else
{
q="DATA INTEGRITY PROTECTION:\r\nYou cannot delete this zone because\r\nthe following clients are set to use it:\r\n";
//fill combo box with available zones
rs->MoveFirst();
rs->FetchField("company",&strData);
q=q+strData+"\r\n";
while(rs->MoveForward())
{
rs->FetchField("company",&strData);
q=q+strData+"\r\n";
}
AfxMessageBox(q);
}
FillZoneList();
}
void CZonesDlg::OnZoneAdd()
{
CString q,name,info;
if(m_bAddMode)
{
m_bAddMode=false;
m_cbZoneList.ShowWindow(TRUE);
m_lblZoneList.ShowWindow(TRUE);
m_btnZoneAdd.SetWindowText("Add");
m_btnZoneDelete.ShowWindow(TRUE);
m_edZoneDescription.GetWindowText(info);
m_edZoneName.GetWindowText(name);
if(name.IsEmpty())
{
AfxMessageBox("You must enter a zone name");
}
else
{
q.Format("INSERT INTO zones ( name, zoneinfo ) SELECT \"%s\", \"%s\";",name,info);
rs->Ex(q);
rs->Close();
//rs->AddNewRecord();
//rs->UpdateField("name",&name);
//rs->UpdateField("zoneinfo",&info);
}
FillZoneList();
}
else//not add mode
{
DisableAll(false);
m_cbZoneList.ShowWindow(FALSE);
m_lblZoneList.ShowWindow(FALSE);
m_btnZoneAdd.SetWindowText("SAVE");
m_btnZoneDelete.ShowWindow(FALSE);
m_edZoneDescription.SetWindowText("");
m_edZoneName.SetWindowText("");
m_edZoneName.SetFocus();
m_bAddMode=true;
}
}
BOOL CZonesDlg::OnInitDialog()
{
CDialog::OnInitDialog();
m_edZoneName.SetLimitText(50);
m_edZoneDescription.SetLimitText(255);
FillZoneList();
m_bAddMode=false;
Security();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
bool CZonesDlg::FillZoneList()
{
CString strData;
CString strIndex;
long lData;
m_cbZoneList.Clear();
rs->Query("SELECT zones.* FROM zones ORDER BY zones.name;");
if(!rs->IsEmpty())
{
//fill combo box with available zones
rs->MoveFirst();
rs->FetchField("name",&strData);
rs->FetchField("id",&lData);
strIndex.Format("%u",lData);
m_cbZoneList.AddRow(strData,strIndex);
while(rs->MoveForward())
{
rs->FetchField("name",&strData);
rs->FetchField("id",&lData);
strIndex.Format("%u",lData);
m_cbZoneList.AddRow(strData,strIndex);
}
//pretend user has selected so that other fields get filled in
if(m_strSelectedZone.IsEmpty()) //first time in
{
m_cbZoneList.SetCurSel(0);
m_strSelectedZone=m_cbZoneList.GetCurrentRowID();
}
else//something valid was selected before so stick with it
m_cbZoneList.Select(m_strSelectedZone);
OnCloseupZoneCombo();
}
else
DisableAll(true);
return true;
}
void CZonesDlg::OnCloseupZoneCombo()
{
//user has made a selection, update the visible list
CString q;
CString strData;
q.Format("SELECT zones.* FROM zones WHERE (((zones.id)=%s));",m_cbZoneList.GetCurrentRowID());
rs->Query(q);
if(!rs->IsEmpty())
{
rs->FetchField("name",&strData);
m_edZoneName.SetWindowText(strData);
rs->FetchField("zoneinfo",&strData);
m_edZoneDescription.SetWindowText(strData);
}
//save current selection so that updates and changes
//will still show what was last selected
m_strSelectedZone=m_cbZoneList.GetCurrentRowID();
}
void CZonesDlg::OnKillfocusZoneDescription()
{
if(!m_bAddMode)
{
SaveField(&m_edZoneDescription,"zoneinfo",true);
FillZoneList();
}
}
void CZonesDlg::OnKillfocusZoneName()
{
if(!m_bAddMode)
{
SaveField(&m_edZoneName,"name",false);
FillZoneList();
}
}
bool CZonesDlg::SaveField(CEdit *edControl,CString fldname,bool AllowEmpty)
{
CString str;
//do nothing if not changed
if(edControl->GetModify()!=TRUE) return true;
edControl->GetWindowText(str);
//dont save empty fields if not allowed to
if(!AllowEmpty)
{
if(str.IsEmpty())
{
edControl->Undo();
return false;
}
}
rs->UpdateField(fldname,&str);
rs->SaveRecord();
return true;
}
void CZonesDlg::DisableAll(bool disable)
{
if(m_bReadOnly) return;
BOOL e=disable ? FALSE:TRUE;
m_btnZoneDelete.EnableWindow(e);
m_cbZoneList.EnableWindow(e);
m_edZoneDescription.EnableWindow(e);
m_edZoneName.EnableWindow(e);
}
void CZonesDlg::Security()
{
m_bReadOnly=false;
int x=m_pApp->Allowed(RZONES,true);
if(x==0)//no access allowed
{
m_pApp->SecurityWarning();
CDialog::OnCancel();
}
if(x==2)//read only
{
m_bReadOnly=true;
m_btnZoneAdd.ShowWindow(FALSE);
m_btnZoneDelete.ShowWindow(FALSE);
m_edZoneDescription.SetReadOnly(TRUE);
m_edZoneName.SetReadOnly(TRUE);
}
}