1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
require ROOT . DS . 'app/Vendor/m2brimagem.php'; |
4
|
|
|
|
5
|
|
|
class BannerController extends AppController { |
6
|
|
|
|
7
|
|
|
public function listar_cadastros() { |
8
|
|
|
$banners = $this->Banner->find('all', |
9
|
|
|
array('conditions' => |
10
|
|
|
array('ativo' => 1, |
11
|
|
|
'usuario_id' => $this->instancia |
12
|
|
|
) |
13
|
|
|
) |
14
|
|
|
); |
15
|
|
|
|
16
|
|
|
$this->set('banners', $banners); |
17
|
|
|
|
18
|
|
|
$this->layout = 'wadmin'; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
View Code Duplication |
public function adicionar_cadastro() { |
22
|
|
|
$this->loadModel('CategoriaBanner'); |
23
|
|
|
|
24
|
|
|
$this->set('categorias', $this->CategoriaBanner->find('all', |
25
|
|
|
array('conditions' => |
26
|
|
|
array('ativo' => 1, |
27
|
|
|
'usuario_id' => $this->instancia |
28
|
|
|
) |
29
|
|
|
) |
30
|
|
|
) |
31
|
|
|
); |
32
|
|
|
|
33
|
|
|
$this->layout = 'wadmin'; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
View Code Duplication |
public function editar_cadastro($id) { |
37
|
|
|
$this->loadModel('CategoriaBanner'); |
38
|
|
|
|
39
|
|
|
$this->set('categorias', $this->CategoriaBanner->find('all', |
40
|
|
|
array('conditions' => |
41
|
|
|
array('ativo' => 1, |
42
|
|
|
'usuario_id' => $this->instancia |
43
|
|
|
) |
44
|
|
|
) |
45
|
|
|
) |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
$this->set('banner', $this->Banner->find('all', |
49
|
|
|
array('conditions' => |
50
|
|
|
array('ativo' => 1, |
51
|
|
|
'id' => $id |
52
|
|
|
) |
53
|
|
|
) |
54
|
|
|
)[0] |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
$this->layout = 'wadmin'; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function s_editar_cadastro($id) { |
|
|
|
|
61
|
|
|
$dados = $this->request->data('dados'); |
62
|
|
|
|
63
|
|
|
$image = $_FILES['imagem']; |
64
|
|
|
|
65
|
|
View Code Duplication |
if (!empty($image['name'])) { |
66
|
|
|
$retorno = $this->uploadImage($image); |
|
|
|
|
67
|
|
|
|
68
|
|
|
if (!$retorno['status']) |
69
|
|
|
$this->Session->setFlash('Não foi possivel salvar a imagem tente novamente'); |
70
|
|
|
|
71
|
|
|
$dados['imagem'] = $retorno['nome']; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
$dados['id_usuario'] = $this->instancia; |
76
|
|
|
$dados['ativo'] = 1; |
77
|
|
|
|
78
|
|
|
$this->Banner->id = $id; |
79
|
|
|
|
80
|
|
|
if ($this->Banner->save($dados)) { |
81
|
|
|
$this->Session->setFlash('Banner editado com sucesso!','default','good'); |
82
|
|
|
return $this->redirect('/banner/listar_cadastros'); |
83
|
|
|
} else { |
84
|
|
|
$this->Session->setFlash('Ocorreu um erro ao editar o Banner!','default','good'); |
85
|
|
|
return $this->redirect('/banner/listar_cadastros'); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function s_adicionar_cadastro() { |
|
|
|
|
90
|
|
|
$this->loadModel('CategoriaBanner'); |
91
|
|
|
|
92
|
|
|
$dados = $this->request->data('dados'); |
93
|
|
|
|
94
|
|
|
$categoriaBanner = $this->CategoriaBanner->find('first', array( |
95
|
|
|
'conditions' => array( |
96
|
|
|
'CategoriaBanner.id' => $dados['categoria_banner_id'] |
97
|
|
|
) |
98
|
|
|
) |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
$image = $_FILES['imagem']; |
102
|
|
|
|
103
|
|
|
$retorno = $this->uploadImage($image, $categoriaBanner); |
104
|
|
|
|
105
|
|
|
if (!$retorno['status']) |
106
|
|
|
$this->Session->setFlash('Não foi possivel salvar a imagem tente novamente'); |
107
|
|
|
|
108
|
|
|
$dados['src'] = $retorno['nome']; |
109
|
|
|
$dados['usuario_id'] = $this->instancia; |
110
|
|
|
$dados['ativo'] = 1; |
111
|
|
|
|
112
|
|
|
if($this->Banner->save($dados)) { |
113
|
|
|
$this->Session->setFlash('Banner salvo com sucesso!'); |
114
|
|
|
return $this->redirect('/banner/listar_cadastros'); |
115
|
|
|
} else { |
116
|
|
|
$this->Session->setFlash('Ocorreu um erro ao salva o banner!'); |
117
|
|
|
return $this->redirect('/banner/listar_cadastros'); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function uploadImage(&$image, $categoriaBanner) { |
122
|
|
|
$type = substr($image['name'], -4); |
123
|
|
|
|
124
|
|
|
$nameImage = uniqid() . md5($image['name']) . $type; |
125
|
|
|
|
126
|
|
|
$dir = APP . 'webroot/uploads/banner/imagens/'; |
127
|
|
|
|
128
|
|
|
$returnUpload = move_uploaded_file($image['tmp_name'], $dir . $nameImage); |
129
|
|
|
|
130
|
|
|
$oImg = new m2brimagem($dir . $nameImage); |
131
|
|
|
|
132
|
|
|
$valida = $oImg->valida(); |
133
|
|
|
|
134
|
|
|
if ($valida == 'OK') { |
135
|
|
|
|
136
|
|
|
$oImg->redimensiona($categoriaBanner['CategoriaBanner']['width'], $categoriaBanner['CategoriaBanner']['height'], 'crop'); |
137
|
|
|
|
138
|
|
|
$oImg->grava($dir . $nameImage); |
139
|
|
|
|
140
|
|
|
} else { |
141
|
|
|
|
142
|
|
|
die($valida); |
|
|
|
|
143
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if (!$returnUpload) |
147
|
|
|
return array('nome' => null, 'status' => false); |
148
|
|
|
|
149
|
|
|
return array('nome' => $nameImage, 'status' => true); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
View Code Duplication |
public function excluir_cadastro() { |
153
|
|
|
$this->layout = 'ajax'; |
154
|
|
|
|
155
|
|
|
$id = $this->request->data('id'); |
156
|
|
|
|
157
|
|
|
$dados = array ('ativo' => '0'); |
158
|
|
|
$parametros = array ('id' => $id); |
159
|
|
|
|
160
|
|
|
if ($this->Banner->updateAll($dados, $parametros)) { |
161
|
|
|
echo json_encode(true); |
162
|
|
|
} else { |
163
|
|
|
echo json_encode(false); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
} |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: