<?
class BusinessExampleLookup
{
	function LookupAluno(&$lookup)
    {
        $filter = $lookup->GetFilterValue('filter');
        if (!$filter) 
        {
            $filter = $lookup->GetFilterValue();
        } 
        $lookup->AddFilterField( new MTextField('filter', $filter,'Aluno', 45));
        $columns = array(
           new MDataGridColumn('idaluno','Id','right', true,'5%',true),
           new MDataGridColumn('nome','Transaction','left', true,'95%',true),
        );
        $sql = new MSQL("a.idaluno,a.nome,c.nome as curso","aluno a, curso c", "(a.idcurso = c.idcurso)",'a.nome');
        if ( $filter )
        {
            $sql->where .= " and ( upper(a.nome) like upper('{$filter}%') )";
        }
        $lookup->setGrid('example',$sql,$columns, 'Lookup Alunos',15,0);
    }

	function LookupObjAluno(&$lookup)
    {
        global $MIOLO;
        $filter = MUtil::NVL($lookup->GetFilterValue('filter'), $lookup->GetFilterValue());
        $lookup->AddFilterField( new MTextField('filter', $filter,'Aluno', 45));
        $columns = array(
           new MObjectGridColumn('idAluno','Id','right', true,'5%',true),
           new MObjectGridColumn('nome','Nome','left', true,'95%',true),
        );
        $aluno = $MIOLO->getBusiness('example','aluno');
        $criteria = $aluno->getCriteria();
        $criteria->addColumnAttribute('idAluno');
        $criteria->addColumnAttribute('nome');
        $criteria->addCriteria('upper(nome)','LIKE',"upper('{$filter}%')");
        $criteria->addOrderAttribute('nome');
        $cursor = $criteria->retrieveAsCursor();
        $lookup->setCursorGrid($cursor,$columns, 'Lookup Alunos',15,0);
    }

	function LookupQueryAluno(&$lookup)
    {
        global $MIOLO;
        $filter = MUtil::NVL($lookup->GetFilterValue('filter'), $lookup->GetFilterValue());
        $lookup->AddFilterField( new MTextField('filter', $filter,'Aluno', 45));
        $columns = array(
           new MDataGridColumn('idAluno','Id','right', true,'5%',true),
           new MDataGridColumn('nome','Nome','left', true,'95%',true),
        );
        $aluno = $MIOLO->getBusiness('example','aluno');
        $query = $aluno->listAll();
        $lookup->setQueryGrid($query,$columns, 'Lookup Alunos',15,0);
    }

	function LookupCursoAluno(&$lookup)
    {   
        global $MIOLO;
 
        // initialize the filter values
        // "filter0" and "filter1" are values passed by the form
        $fCurso = MUtil::NVL($lookup->GetFilterValue('filter0'), $lookup->GetFilterValue());
        $fAluno = MUtil::NVL($lookup->GetFilterValue('filter1'), '');

        // get data to fill the mselection filter control
        $curso = $MIOLO->GetBusiness('example','curso');
        $query = $curso->listAll();

        // create the controls to build a filter
        $lookup->AddFilterField( new MSelection('filter0', $fCurso,'Curso', $query->result));
        $lookup->AddFilterField( new MTextField('filter1', $fAluno,'Aluno', 30));

        // define the columns of a MDataGrid to show the results of lookup
        $columns = array(
           new MDataGridColumn('idaluno','Id','right', true, '8%',true),
           new MDataGridColumn('nome','Nome','left', true, '50%',true),
           new MDataGridColumn('idcurso','IdCurso','left', true, '8%',true),
           new MDataGridColumn('curso','Curso','left', true, '36%',true),
        );

        // create a MSQL to query database
        $sql = new MSQL("a.idaluno,a.nome,c.idcurso,c.nome as curso","aluno a, curso c", "(a.idcurso = c.idcurso)",'a.nome');
        if ( $fAluno )
        {
            $sql->where .= " and ( upper(a.nome) like upper('{$fAluno}%') )";
        }
        $sql->where .= " and ( c.idcurso = '{$fCurso}' )";

        // $lookup is a MLookup object
        $lookup->setGrid('example',$sql,$columns,'Lookup Curso/Alunos',15,0);
    }

	function AutoCompleteAlunoId(&$lookup)
    {   
        global $MIOLO;
 
        $fAluno = $lookup->GetFilterValue();
        $aluno = $MIOLO->getBusiness('example','aluno');
        $criteria = $aluno->getCriteria();
        $criteria->addColumnAttribute('nome');
        $criteria->addColumnAttribute('curso.nome');
        $criteria->addCriteria('idAluno','=',"{$fAluno}");
        $query = $criteria->retrieveAsQuery();

        // $lookup is a MLookup object
        $lookup->setAutoComplete($query->result[0]);
    }

}
?>