#Include "Fivewin.ch"
#Include "xBrowse.ch"
#Define df_BaseMySql 1
Class tCtrlxBrw
Data oXBrwCtrl, oDbDs_DataBaseAds
Data bStrData
Data oGetDsFilter, nTpCampoFilter, cCampoFilter, lLastRecZero
Data bChangeBrw
Method New(f_oXBrowse,f_oDbDs_DataBaseAds) Constructor
Method End()
Method SetTAds()
Method tCtrlxBrw_Skip(f_nKip)
Method tCtrlxBrw_Seek(f_oBjCol,f_uVar)
Method tCtrlxBrw_EditFilterRdd()
Method tCtrlxBrw_EditFilterSql()
Method tCtrlxBrw_EditChange()
Method tCtrlxBrw_EditClearChar(f_cTxt, f_nTipo)
Endclass
//-----------------------------------------------------------------------------
Method New(f_oXBrowse,f_oDbDs_DataBaseAds,f_aGetFilter) Class tCtrlxBrw
Local oSelf
// f_aGetFilter[1] > oBjeto Get
// f_aGetFilter[2] > Tipo Filtro 1=SetAof contains / 2=Sql select Contains
// f_aGetFilter[3] > Nome do campo a pesquisar
///Override Method Skip In Class Txbrowse With oSelf:tCtrlxBrw_Skip
::lLastRecZero := .F.
::oDbDs_DataBaseAds := f_oDbDs_DataBaseAds
::oXBrwCtrl := f_oXBrowse
::oXBrwCtrl:cAlias := ::oDbDs_DataBaseAds:cAlias
If !Hb_IsNil(f_aGetFilter)
::oGetDsFilter := f_aGetFilter[1]
::nTpCampoFilter := f_aGetFilter[2]
::cCampoFilter := f_aGetFilter[3]
If f_aGetFilter[2] == 2 // Sql Contains
::oGetDsFilter:bChange := {||::tCtrlxBrw_EditFilterSql()}
ElseIf f_aGetFilter[2] == 1 // Rdd set aof
::oGetDsFilter:bChange := {||::tCtrlxBrw_EditFilterRdd()}
EndIf
::bChangeBrw := ::oXBrwCtrl:bChange
::oXBrwCtrl:bChange := {||::tCtrlxBrw_EditChange()}
::oXBrwCtrl:bGotFocus := {||::tCtrlxBrw_EditChange()}
EndIf
::oXBrwCtrl:bClrSel := {|| { CLR_BLACK, CLR_HGRAY} } // Giovany
::SetTAds()
::oDbDs_DataBaseAds:Refresh()
Return Self
//-----------------------------------------------------------------------------
Method End() Class tCtrlxBrw
Self := Nil
Return Nil
//-----------------------------------------------------------------------------
Method SetTAds() Class tCtrlxBrw
Local nTpGetRelPos := 1, cTmpQry := "", nTmp := 0
::oDbDs_DataBaseAds:lBufferOnSkip := .T.
If ::oDbDs_DataBaseAds:nOpenType == 3 // Open via Dataset
cTmpQry := Upper(::oDbDs_DataBaseAds:cQrySql)
If At("{STATIC}",cTmpQry) == 0
nTpGetRelPos := 2
Else
nTpGetRelPos := 1
EndIf
Else
nTpGetRelPos := 1
EndIf
If nTpGetRelPos == 2 // Open via Sql Dataset cursor vivo
::oXBrwCtrl:bKeyNo := {| n, _Self | If( n == nil, ;
nCalcRelPos((::oDbDs_DataBaseAds:GetRelKeyPos() * _Self:nLen) /10,::oXBrwCtrl) , ;
::oDbDs_DataBaseAds:SetRelKeyPos( Round(n / _Self:nLen,3) ) ) }
Else // Viao Rdd ou Static Cursor
::oXBrwCtrl:bKeyNo := {| n, _Self | If( n == nil, ;
nCalcRelPos((::oDbDs_DataBaseAds:GetRelKeyPos() * _Self:nLen),::oXBrwCtrl ) , ;
::oDbDs_DataBaseAds:SetRelKeyPos( Round(n / _Self:nLen,3) ) ) }
EndIf
::oXBrwCtrl:bBookMark := {| n | iif( n == nil,;
::oDbDs_DataBaseAds:Recno() , ;
::oDbDs_DataBaseAds:GoTo( n ) ) }
::oXBrwCtrl:bKeyCount := {|| ::oDbDs_DataBaseAds:KeyCount(1) }
::oXBrwCtrl:bBof := { || ::oDbDs_DataBaseAds:Bof() }
::oXBrwCtrl:bEof := { || ::oDbDs_DataBaseAds:Eof() }
::oXBrwCtrl:bSkip := { |n| Iif( n == Nil, n := 1, ), ::tCtrlxBrw_Skip( n )}
::oXBrwCtrl:bGoTop := { || ::oDbDs_DataBaseAds:GoTop() }
::oXBrwCtrl:bGoBottom := { || ::oDbDs_DataBaseAds:GoBottom() }
::oDbDs_DataBaseAds:OnSkip()
Return Nil
//-----------------------------------------------------------------------------
Method tCtrlxBrw_Skip(f_nSkip) Class tCtrlxBrw
Local nSkipper := 0
Default f_nSkip := 1
nSkipper := (::oDbDs_DataBaseAds:cAlias)->(DbSkipper(f_nSkip))
::oDbDs_DataBaseAds:OnSkip()
Return nSkipper
//-----------------------------------------------------------------------------
Method tCtrlxBrw_Seek(f_oBjCol,f_uVar,f_lTpSeek) Class tCtrlxBrw
LOCAL nRecMarca := ::oDbDs_DataBaseAds:Recno(), cTpData := ""
cTpData := VALTYPE(f_uVar)
IF cTpData == "N"
f_lTpSeek := .F.
ELSEIF cTpData == "D"
f_lTpSeek := .F.
ELSEIF cTpData == "C"
f_lTpSeek := .T.
ENDIF
//If ::oDbDs_DataBaseAds:nOpenType == 1
f_oBjCol:SetOrder()
::oDbDs_DataBaseAds:Seek(f_uVar,f_oBjCol:cSortOrder,f_lTpSeek)
//EndIf
IF ::oDbDs_DataBaseAds:EOF()
::oDbDs_DataBaseAds:GoTo(nRecMarca)
MSGSTOP("A ocorrencia solicitada não foi encontrada.",;
"Procedimento abortado...")
ENDIF
::oXBrwCtrl:Refresh(.T.)
IF ::oXBrwCtrl:bChange != nil
EVAL(::oXBrwCtrl:bChange)
ENDIF
RETURN NIL
//-----------------------------------------------------------------------------
Method tCtrlxBrw_EditFilterRdd() Class tCtrlxBrw
Local cTxtDigitacao := "", cAof := ""
//::oGetDsFilter:Save()
cTxtDigitacao := Alltrim(::tCtrlxBrw_EditClearChar(::oGetDsFilter:cText))
If Len(cTxtDigitacao) < 5
cTxtDigitacao := ""
Else
::lLastRecZero := .F.
EndIf
If ::lLastRecZero
Return Nil
EndIf
///? cTxtDigitacao
cAof := "CONTAINS("+::cCampoFilter+","+cTxtDigitacao+")"
::oGetDsFilter:oWnd:cTitle := cAof
::oGetDsFilter:oWnd:Refresh()
///msginfo(cAof)
::oDbDs_DataBaseAds:Filter(cAof)
::oDbDs_DataBaseAds:GoTop()
::oXBrwCtrl:Refresh(.T.)
If Len(cTxtDigitacao) < 5
::lLastRecZero := .T.
EndIf
Return Nil
//-----------------------------------------------------------------------------
Method tCtrlxBrw_EditFilterSql() Class tCtrlxBrw
Local cTxtDigitacao := ""
//::oGetDsFilter:Save()
cTxtDigitacao := Alltrim(::tCtrlxBrw_EditClearChar(::oGetDsFilter:cText))
If Len(cTxtDigitacao) < 5
cTxtDigitacao := ""
Else
::lLastRecZero := .F.
EndIf
If ::lLastRecZero
Return Nil
EndIf
///? cTxtDigitacao
::oDbDs_DataBaseAds:aVarsSql := {{"_TxtDigitacao_",cTxtDigitacao},;
{"_Campo_",::cCampoFilter}}
::oDbDs_DataBaseAds:DsExecute()
::oXBrwCtrl:Refresh(.T.)
If Len(cTxtDigitacao) < 5
::lLastRecZero := .T.
EndIf
Return Nil
//-----------------------------------------------------------------------------
Method tCtrlxBrw_EditChange() Class tCtrlxBrw
If ::oDbDs_DataBaseAds:LastRec() == 0
Return Nil
EndIf
::oGetDsFilter:VarPut(::oDbDs_DataBaseAds:VarGet(::cCampoFilter))
::oGetDsFilter:Refresh()
If !Hb_IsNil(::bChangeBrw)
Eval(::bChangeBrw)
EndIf
Return Nil
//-----------------------------------------------------------------------------
Method tCtrlxBrw_EditClearChar(f_cTxt) Class tCtrlxBrw
Local cRetTxt := f_cTxt
Local iFor := 0, cAnd := ""
Local aCharDeletes := { chr(34),"-",".","+","=","&","'","´"}
Local aPalavras := {}
cRetTxt := _CHAR_CLEAN(cRetTxt,{ " AND ",;
" NEAR ",;
" ANY "})
For iFor := 1 To Len(aCharDeletes)
cRetTxt := StrTran(cRetTxt,aCharDeletes[iFor]," ")
Next
aPalavras := Hb_aTokens(cRetTxt)
cRetTxt := ""
For iFor := 1 To Len(aPalavras)
If Len(aPalavras[iFor]) > 3
cRetTxt += (cAnd + Chr(34)+aPalavras[iFor])+Chr(34) //+ " "
cAnd := " and "
Endif
Next
Return cRetTxt
///////////////////////////////////////////////////////////////////////////////
FUNCTION nCalcRelPos(f_nVal,f_oXBrw)
Local nLenRegs := f_oXBrw:nLen, nRowPos := f_oXBrw:nRowSel
Local nRetorno := 0
nRetorno := Round(f_nVal,3)
If nRetorno == 0
nRetorno := 1
EndIf
If nRetorno < nRowPos
nRetorno := nRowPos
EndIf
nRetorno := Int(nRetorno)
RETURN nRetorno
///////////////////////////////////////////////////////////////////////////////
FUNCTION _CHAR_CLEAN(f_cTxt,f_aCaracteresRetira)
Local cTxtRetorno := f_cTxt, iFor := 0
For iFor := 1 To Len(f_aCaracteresRetira)
cTxtRetorno := StrTran(cTxtRetorno,f_aCaracteresRetira[iFor],"")
Next
RETURN cTxtRetorno