<?php

require_once dirname(__FILE__) . '/../../../includes/bootstrap.php';
require_once 'include/clsBanco.inc.php';
require_once 'Educacenso/Model/IesDataMapper.php';
$iesMapper = new Educacenso_Model_IesDataMapper();
$iesUf = $iesMapper->findAll(array(), array('uf' => $_GET['uf']), array('nome' => 'ASC'));
// Adiciona "INSTITUIÇÃO NÃO CADASTRADA" nos resultados.
$iesUf = array_merge($iesUf, array($iesMapper->find(array('ies' => 9999999))));
header('Content-type: text/xml');
echo '<?xml version="1.0" encoding="ISO-8859-1"?>' . PHP_EOL;
echo '<query xmlns="sugestoes">' . PHP_EOL;
foreach ($iesUf as $ies) {
    echo sprintf('  <ies id="%d">%s</ies>', $ies->id, htmlspecialchars($ies->nome)) . PHP_EOL;
}
echo '</query>';
    /**
     * @see clsCadastro#Gerar()
     */
    public function Gerar()
    {
        global $coreExt;
        $this->campoOculto('id', $this->getEntity()->id);
        $this->campoOculto('servidor', $this->getRequest()->servidor);
        $cursoSuperiorMapper = new Educacenso_Model_CursoSuperiorDataMapper();
        $cursos = $cursoSuperiorMapper->findAll(array(), array(), array('id' => 'ASC', 'nome' => 'ASC'));
        // Licenciatura
        $licenciatura = $this->getEntity()->get('licenciatura') ? $this->getEntity()->get('licenciatura') : 0;
        $this->campoRadio('licenciatura', $this->_getLabel('licenciatura'), array(1 => 'Sim', 0 => 'Não'), $licenciatura);
        // Curso
        $opcoes = array();
        foreach ($cursos as $curso) {
            $opcoes[$curso->id] = $curso->nome;
        }
        $this->campoLista('curso', $this->_getLabel('curso'), $opcoes, $this->getEntity()->get('curso'));
        // Ano conclusão
        $opcoes = range(1960, date('Y'));
        rsort($opcoes);
        $opcoes = array_combine($opcoes, $opcoes);
        $this->campoLista('anoConclusao', $this->_getLabel('anoConclusao'), $opcoes, $this->getEntity()->anoConclusao);
        // UF da IES.
        $ufs = new clsPublicUf();
        $ufs = $ufs->lista();
        $opcoes = array();
        foreach ($ufs as $uf) {
            $opcoes[$uf['sigla_uf']] = $uf['sigla_uf'];
        }
        ksort($opcoes);
        // Caso não seja uma instância persistida, usa a UF do locale.
        $uf = $this->getEntity()->ies->uf ? $this->getEntity()->ies->uf : $coreExt['Config']->app->locale->province;
        $this->campoLista('uf', 'UF', $opcoes, $uf, 'getIes()');
        // IES.
        $opcoes = array();
        $iesMapper = new Educacenso_Model_IesDataMapper();
        $iesUf = $iesMapper->findAll(array(), array('uf' => $uf));
        foreach ($iesUf as $ies) {
            $opcoes[$ies->id] = $ies->nome;
        }
        // Adiciona a instituição "Não cadastrada".
        $ies = $iesMapper->find(array('ies' => 9999999));
        $opcoes[$ies->id] = $ies->nome;
        $this->campoLista('ies', $this->_getLabel('ies'), $opcoes, $this->getEntity()->ies->id);
        $this->url_cancelar = sprintf('index?servidor=%d&instituicao=%d', $this->getRequest()->servidor, $this->getRequest()->instituicao);
        // Javascript para Ajax.
        echo <<<EOT
      <script type="text/javascript">
      function getIes()
      {
        var ies = document.getElementById('ies').value;
        var uf  = document.getElementById('uf').value;

        var url  = '/modules/Educacenso/Views/IesAjaxController.php';
        var pars = '?uf=' + uf;

        var xml1 = new ajax(getIesXml);
        xml1.envia(url + pars);
      }

      function getIesXml(xml)
      {
        var ies = document.getElementById('ies');

        ies.length     = 1;
        ies.options[0] = new Option('Selecione uma IES', '', false, false);

        var iesItems = xml.getElementsByTagName('ies');

        for (var i = 0; i < iesItems.length; i++) {
          ies.options[ies.options.length] = new Option(
            iesItems[i].firstChild.nodeValue, iesItems[i].getAttribute('id'), false, false
          );
        }

        if (ies.length == 1) {
          ies.options[0] = new Option(
            'A UF não possui IES.', '', false, false
          );
        }
      }
      </script>
EOT;
    }