forked from FelipeOAlbert/simplecrud
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.php
More file actions
138 lines (121 loc) · 3.33 KB
/
controller.php
File metadata and controls
138 lines (121 loc) · 3.33 KB
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
<?php
/**
* Controller para parsear entre model e view
*
* PHP version 5
*
* @category PHP
* @package Banco
* @author Albert <albert@questa.com.br>
* @copyright 1997-2005 The PHP Group
* @license http://www.php.net/license/3_01.txt PHP License 3.01
* @version SVN: 1.0.1
* @since File available since Release 1.0.1
* @deprecated File deprecated in Release 2.0.0
*/
require_once('banco.php');
/**
* Controller
*
* Esta classe é responsável pela comunicação entre model e view
*/
class controller extends Banco{
private $post;
private $retorno;
/**
* Method constructor
*
* @return void
*/
function __construct()
{
$this->retorno = array('mensagem' => null, 'falha' => false);
}
/**
* Method index
*
* @return object
*/
public final function index()
{
$this->rodaquery('SELECT * FROM funcionario');
return $this->retornaDados();
}
/**
* Method salvar_post
*
* @param $id identificador do registro
* @return string
*/
public final function salvar_post($id)
{
$this->post = $_POST;
$this->valida_post();
if(!$this->retorno['falha']){
if($this->salvar($id, $this->post)){
$this->retorno['mensagem'][] = "Dados salvos com sucesso";
}else{
$this->retorno['mensagem'][] = "Erro ao salvar dados";
}
}
return $this->retorno;
}
/**
* Method getDados
*
* @param $id identificador do registro
* @return string
*/
public final function getDados($id)
{
if(intval($id) > 0){
$this->rodaquery('SELECT * FROM funcionario WHERE id ="'.$id.'"');
$this->retorno['data'] = $this->retornaDados(true);
}else{
$this->retorno['falha'] = true;
$this->retorno['mensagem'][] = "ID não encontrado";
}
return $this->retorno;
}
/**
* Method delete
*
* @return string json
*/
public final function delete()
{
if(intval($_POST['id']) > 0){
if($this->apagar($_POST['id'])){
$this->retorno['mensagem'] = "Registro apagado com sucesso";
}else{
$this->retorno['falha'] = true;
$this->retorno['mensagem'] = "Erro ao apagar registro";
}
}else{
$this->retorno['falha'] = true;
$this->retorno['mensagem'] = "ID não encontrado";
}
echo json_encode($this->retorno);
}
/**
* Method valida_post
*
* @return string
*/
public final function valida_post()
{
if(empty($this->post['nome'])){
$this->retorno['falha'] = true;
$this->retorno['mensagem'][] = "Campo nome obrigatório";
}
if(empty($this->post['profissao'])){
$this->retorno['falha'] = true;
$this->retorno['mensagem'][] = "Campo profissão obrigatório";
}
}
}
$controller = new controller();
if($_POST and isset($_POST['metodo']) and $_POST['metodo'] == 'delete'){
$controller->delete($_POST['id']);
}
?>