<?php
  include("conn.php");
  ob_clean();

  $resu = '<select id="xFavoritos" title="Favoritos" data-width="fit"  class="selectpicker" data-live-search="true">';
  //Traigo los atajos propios y publicos
  $sql = "select A.ID_ATAJO, A.DESC_ATAJO, GC.DESC_GRUPO_CONSULTA, GC.DESTINO
          from ATJ_ATAJOS A
               INNER JOIN CFG_GRUPO_CONSULTA GC
               ON A.ID_GRUPO_CONSULTA = GC.ID_GRUPO_CONSULTA
          where A.ID_USUARIO = ".$_SESSION['CurrentIdUser']."
             or A.PUBLICO = 1
             OR (PUBLICO = 2 AND 0 < (SELECT COUNT(*)
                                      FROM ATJ_ATAJOS_USUARIOS AU 
                                      WHERE AU.ID_ATAJO = A.ID_ATAJO 
                                        AND AU.ID_USUARIO = ".$_SESSION['CurrentIdUser'].")
                )
          order by GC.DESC_GRUPO_CONSULTA, A.DESC_ATAJO";
  $RsAtajos = $DBSegu->Execute($sql);
  if (!$RsAtajos) {
    print $DBSegu->ErrorMsg();
  }

  while (!$RsAtajos->EOF) {

    $resu .= "<option data-destino='" . utf8_encode($RsAtajos->fields['DESTINO']) . "' data-tokens='' value='" . utf8_encode($RsAtajos->fields['ID_ATAJO']) . "'>" . utf8_encode($RsAtajos->fields['DESC_GRUPO_CONSULTA']) . " - " . utf8_encode($RsAtajos->fields['DESC_ATAJO']) . "</option>"; 
    $RsAtajos->Movenext();     
  }

  $resu .= '  <option data-divider="true"></option>
              <option data-tokens="">Administrar Favoritos...</option>
            </select>';

  $jsondata = array();
  $jsondata['success'] = true;
  $jsondata['message'] = $resu;
  
  header('Content-type: application/json; charset=utf-8');
  echo json_encode($jsondata);
?>