-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbanco.php
213 lines (188 loc) · 6.26 KB
/
banco.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
use Doctrine\DBAL\Driver\PDOException;
class Banco {
private $pdo, $tabela;
public function conexao() {
try {
require 'credentials.php';
$this->pdo = new PDO('mysql:host=localhost;dbname='.$databaseCredentials['name'], $databaseCredentials['username'], $databaseCredentials['password']);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->pdo->exec("SET CHARACTER SET utf8");
} catch (PDOException $e) {
echo 'Error: ' . $e->getMessage();
}
}
function geraHash($str) {
return sha1(md5($str));
}
public function obterCampos() {
$consulta = $this->pdo->query("desc " . $this->tabela);
while ($lista = $consulta->fetch()) {
$campos [] = $lista[0];
}
return $campos;
}
function geraSelect($default, $selecao, $value, $descricao, $select, $filtro) {
$sql = 'select * from ' .$this->tabela.' '. $filtro.' order by ' .$descricao;
echo '<select class="custom-select" name="'.$select.'" id="'.$select.'">';
echo '<option value="default" class="disabled" disabled>'.$default.'</option>';
$banco = new Banco();
$row = $banco->select($sql);
$cont = 0;
while ($cont < count($row)) {
$birobiro = "";
if ($row[$cont][$value] == $selecao) {
$birobiro = ' selected ';
}
echo '<option value="'.$row[$cont][$value].'"' .$birobiro. '>' .$row[$cont][$descricao]. '</option>';
$cont++;
}
echo '</select>';
}
public function validarData($campo) {
$data = DateTime::createfromFormat('d/m/Y', $campo);
if ($data && $data->format('d/m/Y')) {
return true;
} else {
return false;
}
}
public function geraStmt($sql, $vetor, $campos){
$stmt = $this->pdo->prepare($sql);
for ($j = 1; $j <= count($vetor)-1; $j++) {
if (is_numeric($vetor[$j])) {
$stmt->bindParam (':' . $campos[$j], $vetor[$j], PDO::PARAM_STR);
} elseif ($this->validarData($vetor[$j])) {
$stmt->bindParam(':' . $campos[$j], date("Y-m-d", strtotime(str_replace("/", "-", $vetor[$j]))), PDO::PARAM_STR);
} else {
$stmt->bindParam(':' . $campos[$j], $vetor[$j], PDO::PARAM_STR);
}
}
return $stmt;
}
public function validaLogin($user, $pass) {
$this->conexao();
try {
$stmt = $this->pdo->prepare("CALL RetornaUsuarioCredencial(:username, :pass)"); // CHAMA A PROCEDURE DO BANCO COM OS PARAMETROS
$stmt->bindParam(':username', $user, PDO::PARAM_STR);
$pass = $this->geraHash($pass);
$stmt->bindParam(':pass', $pass, PDO::PARAM_STR);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
echo 'Erro: '.$e->getMessage();
}
}
public function conflitoData($ag, $lab) {
$this->conexao();
try {
$stmt = $this->pdo->prepare("CALL ConflitoData(:lab_cod, :ag)"); // CHAMA A PROCEDURE DO BANCO COM OS PARAMETROS
$stmt->bindParam(':lab_cod', $lab, PDO::PARAM_INT);
$stmt->bindParam(':ag', $ag, PDO::PARAM_INT);
$stmt->execute();
// $stmt->debugDumpParams();
// echo $ag.'-', $lab;
// print_r($stmt->fetchAll(PDO::FETCH_ASSOC));
// exit;
if (count($stmt->fetchAll(PDO::FETCH_ASSOC)) > 0) {
echo "Há conflito entre datas neste laboratório. Verifique e tente novamente";
return true;
}
} catch (PDOException $e) {
echo 'Erro: '.$e->getMessage();
return true;
}
return false;
}
public function select($sql) {
$this->conexao();
try {
$consulta = $this->pdo->query($sql);
$vetor = null;
while ($linha = $consulta->fetch(PDO::FETCH_BOTH)) {
$vetor[] = $linha;
}
return $vetor;
} catch (PDOException $e) {
echo 'Error: ' . $e->getMessage();
}
}
public function inserir($vetor) {
$this->conexao();
try {
$campos = $this->obterCampos();
$sql = "INSERT INTO " . $this->tabela . "(";
$i = 0;
foreach ($campos as $v) {
if ($i == 1) {
$sql .= $v;
} elseif ($i > 1) {
$sql .= ", " . $v;
}
$i++;
}
$sql .= ") VALUES(";
$i = 0;
foreach ($campos as $v) {
if ($i == 1) {
$sql .= ":" . $v;
} elseif ($i > 1) {
$sql .= ", :" . $v;
}
$i++;
}
$sql .= ")";
#echo $sql;
$stmt = $this->geraStmt($sql, $vetor, $campos);
$stmt->execute();
} catch (PDOException $e) {
echo 'Error: ' . $e->getMessage();
}
}
public function delete($id) {
$this->conexao();
try {
$stmt = $this->pdo->prepare('DELETE FROM ' . $this->tabela . ' WHERE codigo = :id');
$stmt->bindParam(':id', $id);
$stmt->execute();
} catch (PDOException $e) {
echo 'Error: ' . $e->getMessage();
}
}
function update($vetor) {
$this->conexao();
try {
$sql = "UPDATE {$this->getTabela()} SET ";
$campos = $this->obterCampos();
$i = 0;
foreach ($campos as $v) {
if ($i == 1) {
$sql .= $v." = :" . $v;
} elseif ($i > 1) {
$sql .= ", ".$v." = :" . $v;
}
$i++;
}
$sql .= " WHERE {$campos[0]} = :{$campos[0]}";
echo $sql;
$stmt = $this->geraStmt($sql, $vetor, $campos);
$stmt->bindParam (':' . $campos[0], $vetor[0], PDO::PARAM_INT);
$stmt->execute();
echo $stmt->rowCount();
} catch (PDOException $e) {
echo 'Error: ' . $e->getMessage();
}
}
function getPdo() {
return $this->pdo;
}
function getTabela() {
return $this->tabela;
}
function setPdo($pdo) {
$this->pdo = $pdo;
}
function setTabela($tabela) {
$this->tabela = $tabela;
}
}