-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofissionaisController.php
158 lines (142 loc) · 5.8 KB
/
profissionaisController.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
<?php
require_once "./classes/profissionais/Profissional.php";
require_once "./classes/profissionais/ProfissionalDAO.php";
require_once "./classes/capacitacao/Capacitacao.php";
require_once "./classes/capacitacao/CapacitacaoDAO.php";
require_once "./authController.php";
class profissionaisController {
public function newProfessional(){
$profissional = new Profissional();
$profissional->setNome($_POST["name"]);
$profissional->setSenha($_POST["password"]);
$profissional->setConfirmaSenha($_POST["password-confirm"]);
$profissional->setEmail($_POST["email"]);
$profissional->setEndereco($_POST["address"]);
$profissional->setTelefone(preg_replace('/[\(\)\-\s]/', '', $_POST["phone"]));
$profissional->setAtivo(1);
$erros = $profissional->validate(null);
$capacitacoesValidadas = array();
foreach($_POST as $key => $value) {
if (explode("-", $key)[0] == "serviceSelected"){
$serviceId = explode("-",$key)[1];
$capacitation = new Capacitacao();
$capacitation->setServico($serviceId);
$capacitation->setProfissional(0);
$capacitacoesValidadas[] = $capacitation;
$erros = array_merge($erros, $capacitation->validate(true));
}
}
if(count($erros) != 0) {
include "views/layout/header.php";
include "views/layout/side-bar.php";?>
<main>
<?php include "views/profissionais/cadastrar.php";?>
</main>
<?php include "views/layout/footer.php";
}
else
{
$db = new ProfissionalDAO();
$db->create($profissional);
$profissionalId = $db->db_connection->lastInsertId();
foreach($capacitacoesValidadas as $val) {
$db = new CapacitacaoDAO();
$val->setProfissional($profissionalId);
$db->create($val);
}
header('Location: index.php?acao=profissionais/listar');
}
}
public function getProfessionals() {
$db = new ProfissionalDAO();
$professionals = $db->all();
$db = new CapacitacaoDAO();
foreach ($professionals as $professional) {
$services = $db->findByProfessional($professional->getId());
$professional->setServicos($services);
}
return $professionals;
}
public function deleteProfessional($email){
require_once("./classes/profissionais/ProfissionalDAO.php");
$db = new ProfissionalDAO();
$db->delete($email);
header('Location: index.php?acao=profissionais/listar');
}
public function editProfessional($email) {
if(!isset($_POST['altera']))
{
require_once("./classes/profissionais/ProfissionalDAO.php");
$db = new ProfissionalDAO();
$professional = $db->findOne($email);
include_once "views/layout/header.php";
include_once "views/layout/side-bar.php";?>
<main>
<?php include_once "views/profissionais/editar.php";?>
</main>
<?php include_once "views/layout/footer.php";
}
else
{
$profissional = new Profissional();
$profissional->setNome($_POST["name"]);
$profissional->setSenha($_POST["password"]);
$profissional->setConfirmaSenha($_POST["password-confirm"]);
$profissional->setEmail($_POST["email"]);
$profissional->setEndereco($_POST["address"]);
$profissional->setTelefone(preg_replace('/[\(\)\-\s]/', '', $_POST["phone"]));
$profissional->setAtivo(1);
$erros = $profissional->validate($email);
$capacitacoesValidadas = array();
foreach($_POST as $key => $value) {
if (explode("-",$key)[0] == "serviceSelected"){
$serviceId = explode("-",$key)[1];
$professionalId = (new ProfissionalDAO())->findOne($email)->getId();
$capacitation = new Capacitacao();
$capacitation->setProfissional($professionalId);
$capacitation->setServico($serviceId);
$capacitacoesValidadas[] = $capacitation;
$erros = array_merge($erros, $capacitation->validate(false));
}
}
if(count($erros) != 0) {
include_once "views/layout/header.php";
include_once "views/layout/side-bar.php";?>
<main>
<?php include_once "views/profissionais/editar.php";?>
</main>
<?php include_once "views/layout/footer.php";
}
else
{
$db = new ProfissionalDAO();
$db->removeServicos($profissional->getEmail());
$db->update($profissional, $email);
foreach($capacitacoesValidadas as $val) {
$db = new CapacitacaoDAO();
$db->create($val);
}
header('Location: index.php?acao=profissionais/listar');
}
}
}
}
$auth = new AuthController();
$auth->isAuthenticate();
$action = explode("/", $_GET['acao']);
switch($action[0]) {
case 'excluir':
echo $action[1];
$controller = new profissionaisController();
$controller->deleteProfessional($action[1]);
break;
case 'cadastrar':
$controller = new profissionaisController();
$controller->newProfessional();
break;
case 'editar':
$controller = new profissionaisController();
$controller->editProfessional($action[1]);
break;
}
?>