//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "main.h"
#include "basic.h"
#include "unislct.h"
#include "filter.h"
#include "fieldsN.h"
#include "fieldText.h"
#include "fieldList.h"
#include "fieldValue.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "tbltmpl"
#pragma resource "*.dfm"
void __fastcall UploadMetaInfo(TformDescription *t_desc, AnsiString formname)
{
TADOQuery *mrT = new TADOQuery(NULL);
try
{
mrT->Connection = frmMain->cnncMetaInfo;
t_desc->formname = formname;
if (SELECT(mrT, "select * from forms where name = '" + t_desc->formname + "'"))
{
t_desc->title = mrT->FieldByName("title")->AsString;
t_desc->edittitle = mrT->FieldByName("edittitle")->AsString;
t_desc->accepttitle = mrT->FieldByName("accepttitle")->AsString;
t_desc->table = mrT->FieldByName("table")->AsString;
t_desc->keyfield = mrT->FieldByName("keyfield")->AsString;
t_desc->namefield = mrT->FieldByName("namefield")->AsString;
t_desc->listfield = mrT->FieldByName("listfield")->AsString;
t_desc->shortnamefield = mrT->FieldByName("shortnamefield")->AsString;
t_desc->sortfield = mrT->FieldByName("sortfield")->AsString;
t_desc->sequencename = mrT->FieldByName("sequencename")->AsString;
t_desc->readonly = mrT->FieldByName("readonly")->AsBoolean;
t_desc->takefieldsfrom = mrT->FieldByName("takefieldsfrom")->AsString;
t_desc->type = mrT->FieldByName("type")->AsString;
t_desc->hideonaccept = mrT->FieldByName("hideonaccept")->AsBoolean;
t_desc->parentfield = mrT->FieldByName("parentfield")->AsString;
} else
{
throw Exception("Ошибка: Форма {" + t_desc->formname + "} отсутствует");
}
} __finally
{
delete mrT;
}
}
//---------------------------------------------------------------------------
__fastcall TfrmUniForm::TfrmUniForm(TComponent* Owner, TMenuItem* itm, AnsiString formname)
: TForm(Owner)
{
this->itm = itm;
uid = "";
link = false;
desc = new TformDescription();
slv = NULL; lnk = NULL;
desc->formname = formname;
form->dsc = desc;
parentform = NULL;
if (itm) itm->Enabled = false;
accept = false;
btnlst = new TList();
fltlst = new TList();
fldlst = new TList();
UploadInfo();
}
//---------------------------------------------------------------------------
|
void __fastcall TfrmUniForm::UploadInfo()
{
UploadMetaInfo(desc, desc->formname);
this->Caption = desc->title;
form->editpanel->Caption = desc->edittitle;
form->dbgBrowse->Columns->Items[0]->FieldName = desc->listfield;
form->dbgBrowse->Columns->Items[0]->Title->Caption = "Название";
if (desc->readonly) { form->tblBrowse->ReadOnly = true; }
}
//---------------------------------------------------------------------------
void __fastcall TfrmUniForm::ReorderFilters()
{
for (int r=0; r<fltlst->Count; r++)
{
TfrmFilter* fr = ((TfrmFilter*)((TfltAddInfo*)fltlst->Items[r])->pntr);
fr->Align = alNone;
fr->Visible = false;
}
for (int r=0; r<fltlst->Count; r++)
{
TfrmFilter* fr = ((TfrmFilter*)((TfltAddInfo*)fltlst->Items[r])->pntr);
fr->Visible = true;
fr->Align = alTop;
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmUniForm::Execute()
{
if (link)
{
form->Go();
// запомним мета-информацию по полям
int cnt = form->tblBrowse->Fields->Count;
TField **flms = new TField*[cnt];
for(int i = 0; i < cnt; i++)
{
TFieldType dtype = form->tblBrowse->Fields->Fields[i]->DataType;
if (dtype == ftInteger) flms[i] = new TIntegerField(this);
if (dtype == ftString) flms[i] = new TStringField(this);
if (dtype == ftFloat) flms[i] = new TFloatField(this);
if (dtype == ftDate) flms[i] = new TDateField(this);
if (dtype == ftDateTime) flms[i] = new TDateTimeField(this);
if (dtype == ftTime) flms[i] = new TTimeField(this);
flms[i]->Required = form->tblBrowse->Fields->Fields[i]->Required;
flms[i]->ReadOnly = form->tblBrowse->Fields->Fields[i]->ReadOnly;
flms[i]->FieldName = form->tblBrowse->Fields->Fields[i]->FieldName;
flms[i]->Size = form->tblBrowse->Fields->Fields[i]->Size;
flms[i]->Tag = (int)dtype;
}
form->tblBrowse->Close();
for(int i = 0; i < cnt; i++) flms[i]->DataSet = form->tblBrowse;
// добавим lookup-поле
TStringField *fld = new TStringField(form->tblBrowse);
fld->ReadOnly = true;
fld->FieldName = desc->listfield;
fld->FieldKind = fkLookup;
AnsiString tmp;
tmp = lnk->linktofield; StrRpl(tmp, ",", ";");
fld->KeyFields = tmp;
tmp = slv->keyfield; StrRpl(tmp, ",", ";");
fld->LookupKeyFields = tmp;
fld->LookupDataSet = form->tblAdd;
fld->LookupResultField = slv->listfield;
|
fld->Lookup = true;
if (form->tblAdd->Active)
fld->Size = form->tblAdd->FieldByName(slv->listfield)->Size;
fld->DataSet = form->tblBrowse;
form->dbgBrowse->Columns->Items[0]->FieldName = fld->FieldName;
form->dbgBrowse->Columns->Items[0]->Title->Caption = "Название";
}
AnsiString strw;
if (!accept ||!desc->hideonaccept)
{
strw = "select * from buttons where parentform = '" + desc->formname + "' ";
if (!desc->takefieldsfrom.IsEmpty()) strw += "or parentform = '" + desc->takefieldsfrom + "' ";
strw += "order by sortorder desc";
if (SELECT(mqrA, strw))
{
do
{
TToolButton* btn = form->AddLinkBtn(mqrA->FieldByName("title")->AsString, CommonButtonClick);
TbtnAddInfo *inf = new TbtnAddInfo();
inf->childform = mqrA->FieldByName("childform")->AsString;
inf->childtitle = mqrA->FieldByName("childtitle")->AsString;
inf->linkform = mqrA->FieldByName("linkform")->AsString;
inf->linktofield = mqrA->FieldByName("linktofield")->AsString;
inf->linkfromfield = mqrA->FieldByName("linkfromfield")->AsString;
inf->type = mqrA->FieldByName("type")->AsString;
inf->sql = mqrA->FieldByName("sql")->AsString;
btn->Tag = btnlst->Add(inf);
mqrA->Next();
} while (!mqrA->Eof);
}
}
strw = "select * from filters where parentform = '" + desc->formname + "' ";
if (!desc->takefieldsfrom.IsEmpty()) strw += "or parentform = '" + desc->takefieldsfrom + "' ";
strw += "order by sortorder desc";
if (SELECT(mqrA, strw))
{
do
{
TfrmFilter* flt = form->AddFilter(mqrA->FieldByName("title")->AsString, CommonFilterChange, CommonFilterClear);
TfltAddInfo *inf = new TfltAddInfo();
inf->childform = mqrA->FieldByName("childform")->AsString;
inf->filtersql = mqrA->FieldByName("filtersql")->AsString;
inf->sql = mqrA->FieldByName("sql")->AsString;
inf->type = mqrA->FieldByName("type")->AsString;
inf->name = mqrA->FieldByName("name")->AsString;
inf->code = mqrA->FieldByName("code")->AsInteger;
inf->logic = mqrA->FieldByName("logic")->AsInteger;
inf->pntr = flt;
inf->retid = "";
if (inf->logic == 1) form->FilterBy("a" + IntToStr(inf->code), "(1=0)");
|
if (lnk)
if (inf->childform == lnk->parentform)
{
inf->retid = lnk->linkvalue;
inf->rettitle = lnk->linktitle;
flt->edt->Text = lnk->linktitle;
// в uid возвращается выбранный код
AnsiString str = inf->filtersql;
if (retid.IsEmpty()) str="";
else StrRpl(str, "%1", lnk->linkvalue);
form->FilterBy("a" + IntToStr(inf->code), str);
flt->btn->Enabled = false;
flt->btnclr->Enabled = false;
flt->Enabled = false;
}
flt->Tag = fltlst->Add(inf);
mqrA->Next();
} while (!mqrA->Eof);
}
if (!accept ||!desc->hideonaccept)
{
// подгрузка полей БД из имеющейся метаинформации
strw = "select * from fields where parentform = '" + desc->formname + "' ";
if (!desc->takefieldsfrom.IsEmpty()) strw += "or parentform = '" + desc->takefieldsfrom + "' ";
strw += "order by sortorder";
if (SELECT(mqrA, strw))
{
do
{
// сразу создадим все формы полей, инициализируем их, а также оставим ВСЮ семантику в этих формах
TfieldAddInfo *inf = new TfieldAddInfo();
inf->fieldname = mqrA->FieldByName("fieldname")->AsString;
inf->title = mqrA->FieldByName("title")->AsString;
inf->type = mqrA->FieldByName("type")->AsString;
inf->childform = mqrA->FieldByName("childform")->AsString;
inf->sql = mqrA->FieldByName("sql")->AsString;
inf->ReadOnly = mqrA->FieldByName("readonly")->AsBoolean;
inf->isqbe = mqrA->FieldByName("isqbe")->AsBoolean;
inf->value = mqrA->FieldByName("value")->AsString.Trim();
inf->code = mqrA->FieldByName("code")->AsInteger;
if (inf->type == (AnsiString)"T")
{
TfrmeFT *fr = new TfrmeFT(this, form->dsrcBrowse, inf);
fr->Parent = form->editarea; fr->Name = ""; fr->Align = alTop; inf->form = fr;
}
if ((inf->type == (AnsiString)"I") || (inf->type == (AnsiString)"S"))
{
TfrmeFV *fr = new TfrmeFV(this, form->dsrcBrowse, inf);
fr->Parent = form->editarea; fr->Name = ""; fr->Align = alTop; inf->form = fr;
}
if (inf->type == (AnsiString)"C")
{
TfrmeFieldsN *fr = new TfrmeFieldsN(this, form->dsrcBrowse, inf);
fr->Parent = form->editarea; fr->Name = ""; fr->Align = alTop; inf->form = fr;
}
if (inf->type == (AnsiString)"F") // поле интерпетируется на этапе подстановки при New,
{ // а также для фильтрации (т.е. используется для
// реализации ограниченных наборов)
form->FilterBy("flt_" + inf->fieldname, inf->sql);
}
if (inf->type == (AnsiString)"L")
{
TfrmeList *fr = new TfrmeList(this, form->dsrcBrowse, inf);
if (SELECT(mqrB, "select * from fieldelements where parentfield = " +
mqrA->FieldByName("code")->AsString + " order by sortorder"))
{
do
{
fr->AddElement(mqrB->FieldByName("title")->AsString, mqrB->FieldByName("value")->AsString);
mqrB->Next();
} while (!mqrB->Eof);
fr->Go();
mqrB->Close();
}
fr->Parent = form->editarea; fr->Name = ""; fr->Align = alTop; inf->form = fr;
}
fldlst->Add(inf);
mqrA->Next();
} while (!mqrA->Eof);
}
mqrA->Close();
}
bool f = ((desc->type == (AnsiString)"T") && (!desc->parentfield.Trim().IsEmpty()));
form->spbTree->Visible = f;
form->dbnPlus->Visible = f;
form->SetTreeMode(f);
TuneFilters();
form->Go();
}
//---------------------------------------------------------------------------
void __fastcall TfrmUniForm::TuneFromBtn(TbtnAddInfo *lnkinfo, AnsiString linkvalue, AnsiString linktitle, AnsiString parentform)
{
if (!lnk) lnk = new TlnkFromBtn();
form->lnkb = lnk;
lnk->linkform = lnkinfo->linkform;
lnk->linktofield = lnkinfo->linktofield;
lnk->linkfromfield = lnkinfo->linkfromfield;
lnk->linkvalue = linkvalue;
lnk->linktitle = linktitle;
lnk->parentform = parentform;
}
//---------------------------------------------------------------------------
void __fastcall TfrmUniForm::TuneForLink()
{
link = true;
slv = new TformDescription();
UploadMetaInfo(slv, lnk->linkform);
form->slv = slv;
form->link = link;
desc->listfield = "add_lnkform_nam";
}
//---------------------------------------------------------------------------
void __fastcall TfrmUniForm::InitTblAdd()
{
// инициализация tblAdd
AnsiString str;
str = "select " + slv->listfield + ", " + slv->keyfield + " from " + slv->table;
form->tblAdd->SQL->Clear();
form->tblAdd->SQL->Add(str);
try
{
form->tblAdd->Close();
form->tblAdd->Open();
} catch(EOracleError &E)
{
ShowMessage(E.Message + ", запрос: '" + str + "'");
return;
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmUniForm::TuneFilters()
{
if (!parentform) return;
TList* patfrm_fltlst = ((TfrmUniForm*)parentform)->fltlst;
TList* patfrm_fldlst = ((TfrmUniForm*)parentform)->fldlst;
if (patfrm_fltlst)
{
for (int r=0; r<fltlst->Count; r++)
for (int i=0; i<patfrm_fltlst->Count; i++)
{
TfltAddInfo* tm_r = ((TfltAddInfo*)(fltlst->Items[r]));
TfltAddInfo* tm_i = ((TfltAddInfo*)(patfrm_fltlst->Items[i]));
if (tm_r->name == tm_i->name)
{
tm_r->retid = tm_i->retid;
tm_r->rettitle = tm_i->rettitle;
TfrmFilter* src = ((TfrmFilter*)tm_r->pntr);
src->edt->Text = tm_i->rettitle;
// в uid возвращается выбранный код
AnsiString str = tm_r->filtersql;
if (retid.IsEmpty()) str="";
else StrRpl(str, "%1", retid);
form->FilterBy("a" + IntToStr(tm_r->code), str);
}
}
}
if (accept && patfrm_fldlst)
{
TfrmUniForm* uni = ((TfrmUniForm*)parentform);
if (!uni->form->tblBrowse->IsEmpty())
{
for (int r=0; r<patfrm_fldlst->Count; r++)
for (int i=0; i<fltlst->Count; i++)
{
TfieldAddInfo *fl = ((TfieldAddInfo*)patfrm_fldlst->Items[r]);
TfrmeFieldsN *tm_i = ((TfrmeFieldsN*)(fl->form));
TfltAddInfo *tm_r = ((TfltAddInfo*)(fltlst->Items[i]));
if ((fl->type == (AnsiString)"C") && (fl->fieldname == tm_r->name))
if (!uni->form->tblBrowse->FieldByName(fl->fieldname)->IsNull)
{
tm_r->retid = uni->form->tblBrowse->FieldByName(fl->fieldname)->AsString;
tm_r->rettitle = tm_i->name->Text;
TfrmFilter* src = ((TfrmFilter*)tm_r->pntr);
src->edt->Text = tm_i->name->Text;
// в uid возвращается выбранный код
AnsiString str = tm_r->filtersql;
if (retid.IsEmpty()) str="";
else StrRpl(str, "%1", retid);
form->FilterBy("a" + IntToStr(tm_r->code), str);
}
}
}
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmUniForm::CommonButtonClick(TObject *Sender)
{
TbtnAddInfo* tm = ((TbtnAddInfo*)btnlst->Items[((TToolButton*)Sender)->Tag]);
if (form->tblBrowse->IsEmpty()) return;
// обработка настройки дочерней формы по нажатию кнопки контекстного перехода
AnsiString _title = tm->childtitle, _filter = tm->sql;
UseReplaces(_title, _filter);
bool s = (tm->type == (AnsiString)"S"), l = (tm->type == (AnsiString)"L");
if (!s &&!l) return;
try
{
TfrmUniForm *frm = new TfrmUniForm(frmMain, NULL, tm->childform);
frm->parentform = this;
if (!_filter.Trim().IsEmpty()) frm->form->FilterBy("parent", _filter);
if (!_title.Trim().IsEmpty()) frm->Caption = _title;
if (s || l) frm->TuneFromBtn(tm, form->tblBrowse->FieldByName(desc->keyfield)->AsString, form->tblBrowse->FieldByName(desc->listfield)->AsString, desc->formname);
if (l) frm->TuneForLink();
if (l) frm->InitTblAdd();
frm->Execute();
frm->FormStyle = fsMDIChild;
frm->Show();
} catch (Exception &E)
{
ShowMessage(E.Message);
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmUniForm::CommonFilterChange(TObject *Sender)
{
// обработка вызова дочерней формы по нажатию кнопки фильтра
TfrmFilter* src = ((TfrmFilter*)Sender);
TfltAddInfo* tm = ((TfltAddInfo*)fltlst->Items[src->Tag]);
int i = CallOtherForm(tm->childform, "", tm->sql, true, true);
if (i == mrOk)
{
tm->retid = retid;
tm->rettitle = rettitle;
src->edt->Text = rettitle;
// в uid возвращается выбранный код
AnsiString str = tm->filtersql;
if (retid.IsEmpty()) { CommonFilterClear(Sender); return; }
else StrRpl(str, "%1", retid);
form->FilterBy("a" + IntToStr(tm->code), str);
form->Go();
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmUniForm::CommonFilterClear(TObject *Sender)
{
// обработка вызова дочерней формы по нажатию кнопки фильтра
TfrmFilter* src = ((TfrmFilter*)Sender);
TfltAddInfo* tm = ((TfltAddInfo*)fltlst->Items[src->Tag]);
tm->retid = "";
src->edt->Text = "";
AnsiString str = "";
if (tm->logic == 1) str = "(1=0)";
form->FilterBy("a" + IntToStr(tm->code), str);
form->Go();
}
//---------------------------------------------------------------------------
void __fastcall TfrmUniForm::CommonFltSlctChange(TObject *Sender)
{
// обработка вызова дочерней формы по выбору из списка
}
//---------------------------------------------------------------------------
void __fastcall TfrmUniForm::FormClose(TObject *Sender,
TCloseAction &Action)
{
Action = caFree;
}
//---------------------------------------------------------------------------
void __fastcall TfrmUniForm::formbtbtnCancelClick(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TfrmUniForm::formbtbtnOKClick(TObject *Sender)
{
if (!form->tblBrowse->IsEmpty())
{
this->uid = form->tblBrowse->FieldByName(desc->keyfield)->AsString;
this->rettitle = form->tblBrowse->FieldByName(desc->listfield)->AsString;
this->ModalResult = mrOk;
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmUniForm::TuneForAccept()
{
if (form) form->TuneForAccept();
accept = true;
if (!desc->accepttitle.IsEmpty()) this->Caption = desc->accepttitle;
if (desc->hideonaccept) this->Width = 500;
}
//---------------------------------------------------------------------------
void __fastcall TfrmUniForm::FormDestroy(TObject *Sender)
{
if (desc) delete desc;
if (slv) delete slv;
if (lnk) delete lnk;
for (int r=0; r<btnlst->Count; r++)
delete ((TbtnAddInfo*)(btnlst->Items[r]));
delete btnlst;
for (int r=0; r<fltlst->Count; r++)
delete ((TfltAddInfo*)(fltlst->Items[r]));
delete fltlst;
for (int r=0; r<fldlst->Count; r++)
delete ((TfieldAddInfo*)(fldlst->Items[r]));
delete fldlst;
if (itm) itm->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TfrmUniForm::UseReplaces(AnsiString &title, AnsiString &filter)
{
if (!form->tblBrowse->IsEmpty() &&!form->tblBrowse->FieldByName(desc->keyfield)->IsNull)
{
StrRpl(title, "%1", form->tblBrowse->FieldByName(desc->listfield)->AsString);
StrRpl(filter, "%1", form->tblBrowse->FieldByName(desc->keyfield)->AsString);
}
StrRpl(filter, "<<", "|%%");
StrRpl(filter, ">>", "%%|");
// учтём подстановки полей вида <field>, а также их возможные NULL
filter = TranslateString(filter, FieldValue);
StrRpl(filter, "|%%", "<");
StrRpl(filter, "%%|", ">");
// учтём (ссылки на другие фильтры) подстаноки вида <<flt_value>>, а также их возможные NULL
filter = TranslateString(filter, FilterValue);
StrRpl(filter, "'NULL'", "NULL");
}
//---------------------------------------------------------------------------
int __fastcall TfrmUniForm::CallOtherForm(AnsiString _form, AnsiString _title, AnsiString _filter,
bool _modal, bool _accept)
{
UseReplaces(_title, _filter);
int res = 0;
try
{
TfrmUniForm *frm = new TfrmUniForm(((_modal)?(TForm*)Owner:(TForm*)frmMain), NULL, _form);
frm->parentform = this;
if (!_filter.Trim().IsEmpty()) frm->form->FilterBy("parent", _filter);
if (_accept) { frm->TuneForAccept(); frm->parentform = this; }
if (!_title.Trim().IsEmpty()) frm->Caption = _title;
frm->Execute();
if (_modal)
{
frm->FormStyle = fsNormal;
res = frm->ShowModal();
retid = frm->uid;
rettitle = frm->rettitle;
} else
{
frm->FormStyle = fsMDIChild;
frm->Show();
}
} catch (Exception &E)
{
ShowMessage(E.Message);
}
return res;
}
//---------------------------------------------------------------------------
void __fastcall TfrmUniForm::formdbgBrowseDblClick(TObject *Sender)
{
if (accept && form->btbtnOK->Visible && form->pnlLCOK->Visible)
formbtbtnOKClick(this);
}
//---------------------------------------------------------------------------
AnsiString __fastcall TfrmUniForm::FieldValue(AnsiString valname)
{
AnsiString tmp;
if (!form->tblBrowse->FieldByName(valname.Trim())->IsNull)
tmp= form->tblBrowse->FieldByName(valname.Trim())->AsString;
else
tmp= "NULL";
return tmp;
}
//---------------------------------------------------------------------------
AnsiString __fastcall TfrmUniForm::FilterValue(AnsiString valname)
{
AnsiString tmp;
// выясним текущее значение указанного фильтра
for (int r=0; r<fltlst->Count; r++)
{
TfltAddInfo* tm = ((TfltAddInfo*)(fltlst->Items[r]));
if (tm->name == valname.Trim())
{
if (!tm->retid.IsEmpty())
tmp= tm->retid;
else
tmp= "NULL";
}
}
return tmp;
}
//---------------------------------------------------------------------------
// ф-ция разбора строки
AnsiString __fastcall TfrmUniForm::TranslateString(AnsiString str, TGetValueEvent evt)
{
AnsiString tmp, filter, _filter = str;
tmp = "";
filter = "";
bool b = false;
for(int r=1; r<=_filter.Length(); r++)
{
if (_filter[r] == '<' &&!b) { b = true; tmp = ""; }
else
if (_filter[r] == '>' && b && (!tmp.Trim().IsEmpty())) { b = false; filter+=evt(tmp); }
else
if (_filter[r]!= '<' && b) { tmp += _filter[r]; }
else
if (!b) { filter+= _filter[r]; }
}
return filter;
}
//---------------------------------------------------------------------------
void __fastcall TfrmUniForm::FormShow(TObject *Sender)
{
if (accept) ReorderFilters();
}
//---------------------------------------------------------------------------