Completed
Push — master ( a8e8b7...373ce5 )
by Reginaldo
19:01
created

BannerController::s_adicionar_cadastro()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 19
nc 4
nop 0
dl 0
loc 31
rs 8.8571
c 0
b 0
f 0
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) {
0 ignored issues
show
Coding Style introduced by
s_editar_cadastro uses the super-global variable $_FILES which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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);
0 ignored issues
show
Bug introduced by
The call to uploadImage() misses a required argument $categoriaBanner.

This check looks for function calls that miss required arguments.

Loading history...
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() {
0 ignored issues
show
Coding Style introduced by
s_adicionar_cadastro uses the super-global variable $_FILES which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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);
0 ignored issues
show
Coding Style Compatibility introduced by
The method uploadImage() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
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
}