Location: PHPKode > projects > SolBT > sol/prioridades.php
<?php
  include("../sol/sessao.php");
  
  //Verifica permissao
  $nivelnecessario = 0;
  if ($_SESSION["univel"] > $nivelnecessario)
  {
    header('Location:/sol/index.php?erroperm='.$nivelnecessario);
  }
  
  //Alterar estas variaveis para cadastro
  $pagina_nome = "Prioridade";
  $maxcampodescricao = 30;
  $tabela = "prioridades";
  $campoid = "id_prioridade";
  $campo_descricao = "prioridade";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>SolBT - <?=$sitenome?></title>
    <link rel="StyleSheet" type="text/css" href="../sol/includes/estilos.css" id="estilo" />
    <script language='JavaScript' src='../sol/includes/FuncoesTela.js' type="text/javascript"></script>
    
    <script language='JavaScript' type="text/javascript">
      function AtivaEdicao(campoID)
      {
        EsconderForms();
        
        document.getElementById('frm' + campoID).style.display = '';
        document.getElementById('a' + campoID).style.display = 'none';
        document.getElementById('i' + campoID).style.display = 'none';
        document.getElementById('edt' + campoID).value = document.getElementById('a' + campoID).innerHTML;
        document.getElementById('edt' + campoID).focus();
      }
      
      function EsconderForms()
      {
        for (var i = 0; i < document.forms.length - 1; i++)
        {
          document.getElementById('frm' + i).style.display = 'none';
          document.getElementById('a' + i).style.display = '';
          document.getElementById('i' + i).style.display = '';
        }
      }
      
      function Esconde(campoID)
      {
        document.getElementById('frm' + campoID).style.display = 'none';
      }
      
      function TrimCampo(campoID)
      {
        String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };
        document.getElementById('edt' + campoID).value = document.getElementById('edt' + campoID).value.trim();
      }
      
      function AtivarBtnRedefine()
      {
        document.getElementById('btnAcima').disabled = '';
        document.getElementById('btnAbaixo').disabled = '';
        
        if (document.getElementById('niveis').selectedIndex == 0)
        {
          document.getElementById('btnAcima').disabled = 'disabled';
        }
        
        if (document.getElementById('niveis').selectedIndex == document.getElementById('niveis').length - 1)
        {
          document.getElementById('btnAbaixo').disabled = 'disabled';
        }
      }
      
      function MudaPrioridade(acima)
      {
        var idx = document.getElementById('niveis').selectedIndex;
        
        var Copia = document.createElement('select');

        for (var i = 0; i < document.getElementById('niveis').options.length; i++)
        {
          var NewItem = document.createElement('option');
          NewItem.text = document.getElementById('niveis').options[i].text;
          NewItem.value = document.getElementById('niveis').options[i].value;
          Copia.options.add(NewItem);
        }

        var Selecionado = document.createElement('option');
        var Novo = document.createElement('option');
        
        Selecionado.value = document.getElementById('niveis').options[idx].value;
        Selecionado.text = document.getElementById('niveis').options[idx].text;
        
        if (acima)
        {
          Novo.text = document.getElementById('niveis').options[idx - 1].text;
          Novo.value = document.getElementById('niveis').options[idx - 1].value;
          document.getElementById('niveis').length = 0;
        }
        else
        {
          Novo.text = document.getElementById('niveis').options[idx + 1].text;
          Novo.value = document.getElementById('niveis').options[idx + 1].value;
          document.getElementById('niveis').length = 0;
        }
        
        for (var i = 0; i < Copia.length; i++)
        {
          if (acima)
          {
            if (i == idx)
            {
              document.getElementById('niveis').options.add(Selecionado);
              document.getElementById('niveis').options.add(Novo);
            }
            document.getElementById('niveis').selectedIndex = idx - 1;
          }
          else
          {
            if (i == idx)
            {
              document.getElementById('niveis').options.add(Novo);
              document.getElementById('niveis').options.add(Selecionado);
            }
            document.getElementById('niveis').selectedIndex = idx + 1;
          }
        
          if ((Novo.value != Copia.options[i].value) && (Selecionado.value != Copia.options[i].value))
          {
            var NewItem = document.createElement('option');
            NewItem.text = Copia.options[i].text;
            NewItem.value = Copia.options[i].value;
            document.getElementById('niveis').options.add(NewItem);
          }
        }
        
        AtivarBtnRedefine();
        
        if (document.getElementById('btnAbaixo').disabled)
        {
          document.getElementById('btnAcima').focus();
        }
        
        if (document.getElementById('btnAcima').disabled)
        {
          document.getElementById('btnAbaixo').focus();
        }
        
        RecoloreLista();
        
        Copia.length = 0;
        
        document.getElementById('btnRedefine').disabled = '';
      }
      
      function RecoloreLista()
      {
        for (var i = 0; i < document.getElementById('niveis').options.length; i++)
        {
          var MyStyle = document.getElementById('spCor' + i).getAttribute('style');
          document.getElementById('niveis').options[i].setAttribute('style', MyStyle);
        }
      }
      
      function RedefinePrior()
      {
        var sGET = 'prioridades.php?redefinir=' + document.getElementById('niveis').options.length;
        for (var i = 0; i < document.getElementById('niveis').options.length; i++)
        {
          sGET = sGET + '&prior' + i + '=' + (document.getElementById('niveis').options.length - i - 1);
          sGET = sGET + '&idrdf' + i + '=' + document.getElementById('niveis').options[i].value;
        }
        
        window.open(sGET, '_self');
      }
    </script>
  </head>
  
  <body>
    <?php
      include("../sol/includes/menus.php");
    ?>
    
    <table width="100%">
      <?php
        include("../sol/includes/top.php");
      ?>
      
      <!-- CONTEUDO DA PAGINA -->
      <tr>
        <td id="Conteudo">
          <br />
          <span class="Titulo">Cadastro de <?=strtolower($pagina_nome)?>s</span><br />&nbsp;<br />
          <p id="avisoexc" style="display:none; color:#FF0000;"><em>Este registro n&atilde;o pode ser exclu&iacute;do pois possui lan&ccedil;amentos vinculados a ele.</em></p>
          <table cellspacing="1" cellpadding="15" style="background-color:#666666;">
            <tr valign="top" style="background-color:#FFFFFF;">
              <td>
                <!-- REDEFINE ORDEM -->
                <strong>Redefinir n&iacute;veis</strong><br />&nbsp;<br />
                <table cellspacing="0" cellpadding="0">
                  <tr valign="top">
                    <td>
                      <select name="niveis" id="niveis" size="7" onfocus="EsconderForms();" disabled="true" onchange="AtivarBtnRedefine();">
                        <option value="0" id="opcaoprior" selected></option>
                      </select>
                    </td>
                    <td align="center">
                      &nbsp; <input type="button" id="btnAcima" value="Diminiur" style="width:100px;" onfocus="EsconderForms();" onclick="MudaPrioridade(true)" disabled="true" /><br />
                      &nbsp; <input type="button" id="btnAbaixo" value="Aumentar" style="width:100px;" onfocus="EsconderForms();" onclick="MudaPrioridade(false)" disabled="true" />
                      <br />&nbsp;<br />
                      &nbsp; <input type="button" id="btnRedefine" value="Salvar" style="width:100px; font-weight:bold;" onfocus="EsconderForms();" disabled="true" onclick="RedefinePrior();" />
                    </td>
                  </tr>
                </table>
                <!-- FIM REDEFINE ORDEM -->
              </td>
              <td>
                <script language='JavaScript' type="text/javascript">
                  document.getElementById('niveis').options.length = 0;
                </script>                
                <strong>Editar prioridades</strong><br />&nbsp;<br />
                <table cellspacing="1" cellpadding="3" style="background-color:#666666;">
                  <tr style="color:#FFFFFF; font-weight:bold;">
                    <td align="center">ID</td>
                    <td><?=$pagina_nome?></td>
                    <td width="20">&nbsp;</td>
                  </tr>
                  <?php
                    //Conecta ao banco e abre query
                    $dados = new dados();
                    $dados->get_conexao();
                    
                    $temfilhos = false;
                    
                    //Atualiza ou insere
                    if (($_GET["acao"] == "editar") || ($_GET["acao"] == "inserir") || ($_GET["acao"] == "excluir"))
                    {
                      $idx = str_replace('#', '', $_GET["id"]);
                      
                      if ($_GET["acao"] == "editar")
                      {
                        if (!empty($_POST["edt".$idx]))
                        {
                          $sql = "UPDATE ".$tabela." SET ".$campo_descricao." = '".TrataStr($_POST["edt".$idx])."' WHERE ".$campoid." = ".$idx;
                          mysql_query($sql);
                        }
                      }
                      if ($_GET["acao"] == "excluir")
                      {
                        //Andamentos
                        $sql = "SELECT * FROM chamados WHERE prioridade = ".$idx;
                        $rs = mysql_query($sql);
                        
                        if (mysql_num_rows($rs) > 0) $temfilhos = true;
                      
                        if (!$temfilhos)
                        {
                          $sql = "DELETE FROM ".$tabela." WHERE ".$campoid." = ".$idx;
                          mysql_query($sql);
                          
                          //Redefine niveis
                          $sql = "SELECT id_prioridade, nivel FROM prioridades ORDER BY nivel ASC";
                          $rs = mysql_query($sql);
                          
                          $i = 0;
                          
                          while ($reg = mysql_fetch_array($rs))
                          {
                            $sqlr = "UPDATE prioridades SET nivel = ".$i." WHERE id_prioridade =".$reg['id_prioridade'];
                            mysql_query($sqlr);
                            $i++;
                          }
                        }
                      }
                      else
                      {
                        if (!empty($_POST["edtCampo"]))
                        {
                          $sql = "SELECT (MAX(nivel) + 1) AS max_nivel FROM ".$tabela;
                          $rs = mysql_query($sql);
                          $reg = mysql_fetch_array($rs);
                          
                          if ($reg["max_nivel"] == '') $reg["max_nivel"] = 0;
                          
                          $sql = "INSERT INTO ".$tabela." (nivel, ".$campo_descricao.") VALUES (".$reg["max_nivel"].", '".TrataStr($_POST["edtCampo"])."')";
                          mysql_query($sql);
                        }
                      }
                    }
                    
                    //Redefine prioridades
                    if (!empty($_GET["redefinir"]))
                    {
                      for ($r = 0; $r < $_GET["redefinir"]; $r++)
                      {
                        $_GET["idrdf"] = str_replace('#', '', $_GET["idrdf"]);
                        $sql = "UPDATE prioridades SET nivel = ".$_GET["prior".$r]." WHERE id_prioridade = ".$_GET["idrdf".$r];
                        mysql_query($sql);
                      }
                    }
                    
                    $sql = "SELECT * FROM ".$tabela." ORDER BY nivel DESC";
                    
                    $rs = mysql_query($sql);
                    
                    $i = 1;
                    
                    $ctnp = mysql_num_rows($rs);
                    
                    while ($reg = mysql_fetch_array($rs))
                    {
                      $valorcampo = utf8_encode($reg[$campo_descricao]);
                      $y = $reg[$campoid];
                  ?>
                  <tr style="background-color:#<? if ($i % 2 == 0) echo "E8E8E8"; else echo "FFFFFF"; ?>;">
                    <td align="center" valign="middle"><?=($y)?></td>
                    <td valign="middle">
                      <!-- CAMPO -->
                      <a id="a<?=($i - 1)?>" style="cursor:pointer;" onclick="AtivaEdicao(<?=($i - 1)?>);"><?=$valorcampo?></a>
                      <img src="/sol/includes/excluir.gif" border="0" id="i<?=($i - 1)?>" alt="Excluir..." align="top" onclick="if (confirm('Confirmar exclusão de <?=TrataStrForJS($valorcampo)?>?')) window.open('<?=$_SERVER["PHP_SELF"]?>?acao=excluir&id=<?=($y)?>', '_self');" style="cursor:pointer;" title="Excluir..." />
                      <form action="<?=$_SERVER["PHP_SELF"]?>?acao=editar&id=<?=$y?>" method="post" id="frm<?=($i - 1)?>" onsubmit="TrimCampo(<?=($i - 1)?>);">
                        <input type="text" id="edt<?=($i - 1)?>" name="edt<?=$y?>" value="<?=$valorcampo?>" maxlength="<?=$maxcampodescricao?>" size="<?=$maxcampodescricao?>" onblur="TrimCampo(<?=($i - 1)?>);" />
                        <input type="submit" value="OK" id="btn<?=($i - 1)?>" style="font-weight:bold;" />
                        <input type="button" value="Cancelar" onclick="EsconderForms();" />
                      </form>
                      
                      <!-- ESCONDE FORM E ADICIONA LISTA DE NIVEIS -->
                      <script language='JavaScript' type="text/javascript">
                        Esconde(<?=($i - 1)?>);
                        
                        //Adiciona o novo elemento na lista
                        var novo = document.createElement('option');
                        novo.setAttribute('id', 'opcaoprior');
                        novo.setAttribute('style', 'color:#<?=getCorPrioridade($reg["nivel"], $ctnp)?>');
                        novo.value = <?=$y?>;
                        novo.text = '<?=str_replace('&quot;', '"', TrataStrForJS($valorcampo))?>';
                        document.getElementById('niveis').options.add(novo);
                      </script>
                      <!-- FIM DE SCRIPT -->
                      
                    </td>
                    <td width="20" style="background-color:#<?=getCorPrioridade($reg["nivel"], $ctnp)?>">
                      <span id="spCor<?=($i - 1)?>" style="color:#<?=getCorPrioridade($reg["nivel"], $ctnp)?>">&nbsp;</span>
                    </td>
                  </tr>
                  <?php
                      $i++;
                    }
                  ?>
                </table>
                <script language='JavaScript' type="text/javascript">
                  if (document.getElementById('niveis').length > 1)
                  {
                    document.getElementById('niveis').selectedIndex = 0;
                    document.getElementById('btnAbaixo').disabled = '';
                    document.getElementById('niveis').disabled = '';
                  }
                </script>
              </td>
            </tr>
          </table>
          <br />&nbsp;<br />
          <strong>Escala de prioridades</strong><br />&nbsp;<br />
          <table cellspacing="0" cellpadding="0" width="300">
            <tr>
              <td width="20" style="background-color:#000000;">&nbsp;</td>
              <td width="20" style="background-color:#663333;">&nbsp;</td>
              <td width="20" style="background-color:#666699;">&nbsp;</td>
              <td width="20" style="background-color:#0000FF;">&nbsp;</td>
              <td width="20" style="background-color:#999900;">&nbsp;</td>
              <td width="20" style="background-color:#FF6600;">&nbsp;</td>
              <td width="20" style="background-color:#FF0000;">&nbsp;</td>
            </tr>
          </table>
          <table cellspacing="0" cellpadding="0" width="300">
            <tr valign="middle">
              <td width="33%" height="25" style="color:#000000;" align="left">M&iacute;nima</td>
              <td width="33%" height="25" style="color:#0000FF;" align="center">M&eacute;dia</td>
              <td width="33%" height="25" style="color:#FF0000;" align="right">M&aacute;xima</td>
            </tr>
          </table>
          <br />&nbsp;<br />
          <strong>Inserir novo registro</strong><br />&nbsp;<br />
          <?php
            if ($ctnp == 7)
            {
              echo '<em>Voc&ecirc; j&aacute; possui o n&uacute;mero m&aacute;ximo de 7 prioridades cadastradas.</em>';
            }
            else
            {
          ?>
          <form action="<?=$_SERVER["PHP_SELF"]?>?acao=inserir" method="post" id="frmInserir" onsubmit="TrimCampo('Campo');" onreset="document.getElementById('edtCampo').focus();">
            <?=$pagina_nome?>: <input type="text" id="edtCampo" name="edtCampo" value="" maxlength="<?=$maxcampodescricao?>" size="<?=$maxcampodescricao?>" onblur="TrimCampo('Campo');" onfocus="EsconderForms();" />
            <input type="submit" value="OK" id="btnCampo" style="font-weight:bold;" />
            <input type="reset" value="Cancelar" />
          </form>
          <br />
          <em>* Novas prioridades s&atilde;o criadas com n&iacute;vel m&iacute;nimo.<br />
          ** Voc&ecirc; pode criar at&eacute; 7 prioridades.</em>
          <?php
            }
          ?>
          <br />&nbsp;<br />
        </td>
      </tr>
      <!-- FIM DE CONTEUDO DA PAGINA -->
      
      <?php
        include("../sol/includes/rp.php");
      ?>
    </table>
  </body>
  <script language='JavaScript' type="text/javascript">
    <?php
      if ($temfilhos)
      {
    ?>
    document.getElementById('avisoexc').style.display = '';
    <?php
      }
    ?>
  </script>
</html>
<?php
  @mysql_close();
?>
Return current item: SolBT