Viewing file: inc_Funcoes.php (9.17 KB) -rwxr-xr-x Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
include (INCLUDES . "inc_Conexao.php");
function salva($tabela,$dados) {
foreach($dados as $indice => $dd) {
if (substr($indice,4) == 'RowID')
$RowID = $dd;
}
if ($RowID != '') {
$sql = "UPDATE $tabela SET ";
foreach($dados as $campo => $valor) {
if (substr($campo,4) != 'RowID')
$sql .= $campo ." = '". $valor ."', ";
else {
$indice = $campo;
$chave = $valor;
}
}
$sql = substr($sql,0,-2);
$sql .= " WHERE $indice = $chave";
} else {
$sql = "INSERT INTO $tabela (";
foreach ($dados as $campo => $valor) {
if ($valor != '')
$sql .= $campo .',';
}
$sql = substr($sql, 0, -1);
$sql .= " ) VALUES (";
foreach ($dados as $campo => $valor) {
if ($valor != '')
$sql .= "'".$valor."',";
}
$sql = substr($sql, 0, -1);
$sql .= ") ";
}
/*
echo '<pre>';
echo $sql;
echo '</pre>';
*/
//die();
$exe = mysql_query($sql);
$test = mysql_error();
if ($exe)
return true;
else
return false;
}
function lista($tabela,$where='',$order='') {
$sql = "
SELECT
*
FROM $tabela
";
if ($where != '')
$sql .= " WHERE $where";
if ($order != '')
$sql .= " ORDER BY $order ";
$sql = $sql.';';
/*
echo '<pre>';
echo $sql;
echo '</pre>';
*/
return executa($sql);
}
function executa($sql) {//somente para select
$exe = mysql_query($sql);
//echo '<p>'.$sql.'</p>';
//die();
while ($rs = mysql_fetch_assoc($exe))
$array[] = $rs;
return $array;
}
//deleta registro do banco de dados
function deleta($tabela, $dados) {
foreach($dados as $indice => $dd) {
if (substr($indice,4) == 'RowID')
$RowID = $dd;
}
echo "---".$RowID;
//$sql=" DELETE FROM ".$tabela." WHERE ".$RowID ." = ".$dados[$RowID];
echo($sql);
die();
if ($exe = mysql_query($sql))
return true;
else
return false;
}
// Passando data do banco "AAAA-MM-DD" para "DD/MM/AAAA"
function mostraData ($data) {
if ($data!='') {
return (substr($data,8,2).'/'.substr($data,5,2).'/'.substr($data,0,4));
}
else
{
return '';
}
}
// Passando data do text box "DD/MM/AAAA" para "AAAA-MM-DD"
function gravaData ($data) {
if ($data != '') {
return (substr($data,6,4) . '-' . substr($data,3,2). '-' . substr($data,0,2));
}
else
{
return '';
}
}
function arrumaData($data,$destino) {
if ($data != '') {
if ($destino == 'grava') {
list($dia,$mes,$ano) = explode('/',$data);
return $ano.'-'.$mes.'-'.$dia;
} else
if ($destino == 'mostra') {
list($ano,$mes,$dia) = explode('-',$data);
return $dia.'/'.$mes.'/'.$ano;
}
}
}
function arrumaDataHora($DataHora) {
//esta função retorna um array onde a primeira posição é a data e a segunda a hora;
list($data,$hora) = explode(' ',$DataHora);
$retorno = arrumaData($data,'mostra').' '.substr($hora,0,5);
return $retorno;
}
function fvalor($valor)
{
if ($valor!="")
{
$temp = number_format ($valor, 2, ",", ".");
}
else
{
$temp="";
}
return($temp);
}
function setaMes($mes) {
$mesArray = array(
'01' => 'Janeiro',
'02' => 'Fevereiro',
'03' => 'Março',
'04' => 'Abril',
'05' => 'Maio',
'06' => 'Junho',
'07' => 'Julho',
'08' => 'Agosto',
'09' => 'Setembro',
'10' => 'Outubro',
'11' => 'Novembro',
'12' => 'Dezembro'
);
return $mesArray[$mes];
}
// Funções
function db_stmt_bind_param(&$stmt,$tipo_dados,$params)
{
array_unshift($params,$tipo_dados);
array_unshift($params,$stmt);
return call_user_func_array('mysqli_stmt_bind_param',$params);
}
/* Executa a query */
function ExecutaSQLStatment($conexao,$sql,$tipo_dados,$parametros)
{
if($stmt = $conexao->prepare($sql))
{
// Anexa os parâmetros na senteça sql
$res = db_stmt_bind_param($stmt,$tipo_dados,$parametros);
if($res)
{
// Executa a query
$exec = $stmt->execute();
if($exec)
{
$stmt->close();
$conexao->commit();
return true;
}
else
{
$msg = $stmt->error;
$stmt->close();
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}
}
/* Formata a data no formato escolhido */
function FormataData($data,$formato,$mostra_data = false)
{
list($data,$hora) = explode(" ",$data);
if($formato == "BD")
{
list($dia,$mes,$ano) = explode("/",$data);
$data = $ano . "-" . $mes . "-" . $dia;
}
elseif($formato == "SCREEN")
{
list($ano,$mes,$dia) = explode("-",$data);
$data = $dia . "/" . $mes . "/" . $ano;
}
if($mostra_data == true)
{
return $data . " " . $hora;
}
else
{
return $data;
}
}
function FormataTelefone($telefone,$formato)
{
if($formato == "BD")
{
$telefone = str_replace("(","",$telefone);
$telefone = str_replace(")","",$telefone);
$telefone = str_replace(" ","",$telefone);
$telefone = str_replace("-","",$telefone);
return $telefone;
}
elseif($formato == "SCREEN")
{
$telefone = str_replace("-","",$telefone);
$telefone = substr($telefone,0,4) . "-" . substr($telefone,4,4);
return $telefone;
}
}
/* Redireciona para uma página específica */
function Redireciona($pagina,$parametros = null)
{
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
if(!is_null($parametros))
{
$querystring = "?";
$num_parametro = 1;
foreach($parametros as $key => $valor)
{
if($num_parametro > 1)
{
$querystring .= "&";
}
$querystring .= "$key=$valor";
$num_parametro ++;
}
}
$url = "http://$host$uri/$pagina$querystring";
if(!headers_sent())
{
//If headers not sent yet... then do php redirect
header("Location: http://$host$uri/$pagina$querystring");
exit;
}
else
{
//If headers are sent... do java redirect... if java disabled, do html redirect.
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
echo '</noscript>';
exit;
}
}
//Adicionar WHERE ou AND a condição - AJUSTAR DEPOIS COM REGEX
function PreparaCondicaoSQL(&$ordem,&$condicao)
{
if($ordem > 0)
{
$condicao .= " AND ";
}
else
{
$condicao .= " WHERE ";
}
$ordem++;
}
//Prepara a querystring da requisição
function SerializaQuerystring($param)
{
if(count($param) > 0)
{
$querystring = "?";
foreach($param as $chave => $valor)
{
$querystring .= $chave . "=" . $valor . "&";
}
$querystring = substr($querystring, 0, strlen($querystring) - 1);
}
return $querystring;
}
//Verifica se o campo possui valor não padrão
function ValidaValor($valor)
{
if(in_array($valor,array(0,"00/00/0000")))
{
return $valor = "";
}
return $valor;
}
//Gera uma senha aleatória
function GeraSenhaAleatoria()
{
$caracteres_aceitos = 'abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789';
$max = strlen($caracteres_aceitos) - 1;
$senha = null;
for($i = 0; $i < 6; $i++)
{
$senha .= $caracteres_aceitos[mt_rand(0, $max)];
}
return $senha;
}
function Mostra($texto)
{?>
<script language="javascript">
alert("<?= $texto;?>")
</script>
<?}
function Mostra2($texto)
{?>
<script language="javascript">
prompt("<?= $texto;?>","<?= $texto;?>");
</script>
<?}
/**
*
*retorna extensao de um arquivo
*
*@return String
**/
function retornaExtensao($nomeArquivo)
{
$ex = strtolower(end(explode('.', $nomeArquivo)));
return $ex;
}
if (!function_exists('verifyValidade')) {
function verifyValidade ($conexao)
{
$sql = "SELECT DAD_Titulo,DAD_EndVigencia FROM irpen1br.docs_administrativos where DAD_TipoDocumento IN('convenios','contratos') AND DATEDIFF(DAD_EndVigencia, now()) >= 60";
return $conexao->query($sql);
}
}
?>
|