Completed
Push — master ( 548c2e...71f869 )
by Reginaldo
275:33 queued 238:50
created

ApiController::banner()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 35
rs 8.8571
cc 3
eloc 20
nc 3
nop 0
1
<?php
2
3
class ApiController extends AppController {
4
5
	public function beforeFilter()
6
	{
7
		return true;
8
   	}
9
10
	public function wishlist($dados)
0 ignored issues
show
Unused Code introduced by
The parameter $dados is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
11
	{
12
		return true;
13
	}
14
15
	public function client($id_cliente = null)
16
	{
17
		$api = 'cliente';
18
	    $this->loadModel('Cliente');
19
		$this->autoRender = false;
20
		$this->response->type('json');
21
		
22
		$type = $this->request;
23
24
	    if (!$this->validate_use_api($type, $api)) {
25
	    	echo '{message: Você não tem permissão para usar nosso modulo}';
26
	    	return;
27
	    }
28
29
	    if ($type->is('get')) {
30
	    	$conditions = array(
31
				'ativo' => 1,
32
				'id_usuario' => $this->getIdUser(),
33
			);
34
35
			if (isset($id_cliente))
36
			 	$conditions['id'] = $id_cliente;
37
38
39
		    $cliente = $this->Cliente->find('all', 
40
				array('conditions' => 
41
					$conditions
42
				)
43
			);
44
45
			$this->response->body(json_encode($cliente));
46
	    } else if ($type->is('post')) {
47
	    	$dados = $this->request->data;
48
	    	
49
	    	if (empty($dados)) {
50
				$this->response->body(json_encode(array('message' => 'Ocorreu algum erro com os parametros passados')));
51
				return;
52
	    	}
53
54
	    	if (!empty($dados['nome1']) && !empty($dados['nome2']) && !empty($dados['email']) && !empty($dados['senha'])) {
55
	    		$this->postClient($dados);
56
	    	} 
57
58
	    	$this->loginClient($dados);
59 View Code Duplication
	    } else if ($type->is('put')) {
60
61
	    	$dados = $this->request->data;
62
	    	
63
			if (empty($dados)) {
64
				$this->response->body(json_encode(array('message' => 'Ocorreu algum erro com os parametros passados')));
65
				return;
66
	    	}
67
68
	    	if ($id_cliente == null) {
69
	    		$this->response->body(json_encode(array('message' => 'Você não passou o id do usuario')));
70
	    		return;
71
	    	}
72
73
	    	$this->putClient($dados, $id_cliente);
74
	    } else if ($type->is('delete')) {
75
	    	
76
	    	if ($id_cliente == null) {
77
	    		$this->response->body(json_encode(array('message' => 'Você não passou o id do usuario')));
78
	    		return;
79
	    	}
80
81
	    	$this->inactiveClient($id_cliente);
82
	    }
83
	}
84
85
	public function parent($id_cliente = null, $id_parente = null)
86
	{
87
		$api = 'parente';
88
89
	    $this->loadModel('Parente');
90
91
		$this->autoRender = false;
92
		$this->response->type('json');
93
		
94
		$type = $this->request;
95
96
	    if (!$this->validate_use_api($type, $api)) {
97
	    	echo '{message: Você não tem permissão para usar nosso modulo}';
98
	    	return;
99
	    }
100
101
	    if ($type->is('get')) {
102
	    	$conditions = array(
103
				'ativo' => 1,
104
				'usuario_id' => $this->getIdUser(),
105
			);
106
107
			$conditions['cliente_id'] = $id_cliente;
108
109
			if (isset($id_parente)) {
110
				$conditions['id'] = $id_parente;
111
			}
112
113
		    $parentes = $this->Parente->find('all', 
114
				array('conditions' => 
115
					$conditions
116
				)
117
			);
118
119
			$this->response->body(json_encode($parentes));
120
	    } else if ($type->is('post')) {
121
	    	$dados = $this->request->data;
122
	    	
123
	    	if (empty($dados)) {
124
				$this->response->body(json_encode(array('message' => 'Ocorreu algum erro com os parametros passados')));
125
				return;
126
	    	}
127
128
	    	if (!empty($dados['cliente_id'])) {
129
	    		$this->postParent($dados);
130
	    	} 
131
132
	    	$this->loginParent($dados);
133 View Code Duplication
	    } else if ($type->is('put')) {
134
135
	    	$dados = $this->request->data;
136
	    	
137
			if (empty($dados)) {
138
				$this->response->body(json_encode(array('message' => 'Ocorreu algum erro com os parametros passados')));
139
				return;
140
	    	}
141
142
	    	if ($id_parente == null) {
143
	    		$this->response->body(json_encode(array('message' => 'Você não passou o id do usuario')));
144
	    		return;
145
	    	}
146
147
	    	$this->putParent($dados, $id_parente);
148
	    } else if ($type->is('delete')) {
149
	    	
150
	    	if ($id_parente == null) {
151
	    		$this->response->body(json_encode(array('message' => 'Você não passou o id do usuario')));
152
	    		return;
153
	    	}
154
155
	    	$this->inactiveClient($id_parente);
156
	    }
157
	}
158
159
	public function occurrences($id_cliente = null) 
160
	{
161
		$api = 'parente';	
162
163
		$this->autoRender = false;
164
		$this->response->type('json');
165
		$this->loadModel('Ocorrencias');
166
167
		$type = $this->request;
168
169
	    if ($type->is('get'))
170
	    {
171
	    	$conditions = array(
172
				'ativo' => 1,
173
				'cliente_id' => $id_cliente,
174
			);
175
176
			$conditions['cliente_id'] = $id_cliente;
177
178
		    $ocorrencias = $this->Ocorrencias->find('all', 
179
				array('conditions' => 
180
					$conditions
181
				)
182
			);
183
184
			$this->response->body(json_encode($ocorrencias));	    	
185
	    }
186
	}
187
188
	public function newsletter()
189
	{
190
		$api = 'newsletter';
191
192
		$this->loadModel('Newsletter');
193
194
		$this->autoRender = false;
195
		$this->response->type('json');
196
		
197
		$type = $this->request;
198
199
	    if (!$this->validate_use_api($type, $api)) {
200
	    	echo '{message: Você não tem permissão para usar nosso modulo}';
201
	    	return;
202
	    }
203
204
    	$request = $this->request->data;
205
    	
206
    	if (empty($request)) {
207
			$this->response->body(json_encode(array('message' => 'Ocorreu algum erro com os parametros passados')));
208
			return;
209
    	}
210
211
		$dados = array(
212
			'email'  => $request['email'],
213
			'origem' => $request['origem'],
214
			'ativo'  => 1,
215
			'usuario_id' => $this->getIdUser()
216
		);
217
218
		$this->Newsletter->save($dados);
219
220
		$this->response->body('{"message": "success", "result":'.json_encode($dados).'}');
221
		return;		
222
	}
223
224
	public function banner()
225
	{
226
		$api = 'banner';
227
228
		$this->loadModel('Banner');
229
230
		$this->autoRender = false;
231
		$this->response->type('json');
232
233
		$type = $this->request;
234
235
		if (!$this->validate_use_api($type, $api)) {
236
	    	echo '{message: Você não tem permissão para usar nosso modulo}';
237
	    	return;
238
	    }
239
240
    	$conditions = array(
241
			'ativo' => 1,
242
			'id_usuario' => $this->getIdUser()
243
		);
244
245
	    $banner = $this->Banner->find('all', 
246
			array('conditions' => 
247
				$conditions
248
			)
249
		);
250
251
	    if (!empty($banner)) {
252
			$this->response->body('{"message": "success", "result":'.json_encode($banner).'}');
253
			return;
254
	    }
255
		
256
		$this->response->body('{"message": "error"}');
257
		return;	
258
	}
259
260 View Code Duplication
	public function loginClient($dados)
261
	{
262
263
    	$conditions = array(
264
			'ativo' => 1,
265
			'id_usuario' => $this->getIdUser(),
266
			'email' => $dados['email'],
267
			'senha' => sha1($dados['senha'])
268
		);
269
270
	    $cliente = $this->Cliente->find('all', 
271
			array('conditions' => 
272
				$conditions
273
			)
274
		);
275
276
	    if (!empty($cliente)) {
277
			$this->response->body('{"message": "success", "result":'.json_encode($cliente).'}');
278
			return;
279
	    }
280
		
281
		$this->response->body('{"message": "error"}');
282
		return;	    
283
	}
284
285
	public function postClient($dados)
286
	{
287
		$this->loadModel('Cliente');
288
289
    	$dados['senha'] = sha1($dados['senha']);
290
		$dados['ativo'] = 1;
291
		$dados['id_usuario'] = $this->instancia;
292
		
293
		if ($this->Cliente->save($dados)) {
294
			$this->response->body('{"message": "success", "result":'.json_encode($dados).'}');
295
			return;
296
		}
297
298
		$this->response->body('{"message": "error"}');
299
		return true;
300
	}
301
302 View Code Duplication
	public function putClient($dados, $id_cliente)
303
	{
304
		if ($dados['senha'] != '') {
305
			$dados['senha'] = sha1($dados['senha']);
306
		}
307
308
		$this->Cliente->id = $id_cliente;
309
		$this->Cliente->id_usuario = $this->getIdUser();
310
311
		if ($this->Cliente->save($dados)) {
312
			$this->response->body('{"message": "success", "result": '. json_encode($dados) .'}');
313
			return;
314
		}
315
316
		$this->response->body('{"message": "error"}');
317
		return;
318
	}
319
320 View Code Duplication
	public function inactiveClient($id_cliente) 
321
	{
322
		$dados['ativo'] = 0;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$dados was never initialized. Although not strictly required by PHP, it is generally a good practice to add $dados = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
323
		
324
		$this->Cliente->id = $id_cliente;
325
326
		if ($this->Cliente->save($dados)) {
327
			$this->response->body('{"message": "success", "result":'.json_encode($dados).'}');
328
			return;
329
		} else {
330
			$this->response->body('{"message": "error"}');
331
			return;
332
		}	
333
	}
334
335
336 View Code Duplication
	public function postParent($dados)
337
	{
338
    	$dados = array(
339
			'senha'      => sha1($dados['senha']),
340
			'usuario_id' => $this->getIdUser(),
341
			'cliente_id' => $dados['cliente_id'],
342
			'login'      => $dados['login'],
343
			'ativo'      => 1,
344
		);
345
		
346
		if ($this->Parente->save($dados)) {
347
			$this->response->body('{"message": "success", "result":'.json_encode($dados).'}');
348
			return;
349
		}
350
351
		$this->response->body('{"message": "error"}');
352
		return;
353
	}
354
355 View Code Duplication
	public function loginParent($dados) 
356
	{
357
    	$conditions = array(
358
			'ativo' => 1,
359
			'usuario_id' => $this->getIdUser(),
360
			'login' => $dados['login'],
361
			'senha' => sha1($dados['senha'])
362
		);
363
364
	    $parente = $this->Parente->find('all', 
365
			array('conditions' => 
366
				$conditions
367
			)
368
		);
369
370
	    if (!empty($parente)) {
371
			$this->response->body('{"message": "success", "result":'.json_encode($parente).'}');
372
			return;
373
	    }
374
		
375
		$this->response->body('{"message": "error"}');
376
		return;	
377
	}
378
379 View Code Duplication
	public function putParent($dados, $id_parente) 
380
	{
381
		if ($dados['senha'] != '') {
382
			$dados['senha'] = sha1($dados['senha']);
383
		}
384
385
		$this->Parente->id = $id_parente;
386
		$this->Parente->id_usuario = $this->getIdUser();
387
388
		if ($this->Parente->save($dados)) {
389
			$this->response->body('{"message": "success", "result": '. json_encode($dados) .'}');
390
			return;
391
		}
392
393
		$this->response->body('{"message": "error"}');
394
		return;
395
	}
396
397 View Code Duplication
	public function inactiveParent($id_parente) 
398
	{
399
		$dados['ativo'] = 0;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$dados was never initialized. Although not strictly required by PHP, it is generally a good practice to add $dados = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
400
		
401
		$this->Parente->id = $id_parente;
402
403
		if ($this->Parente->save($dados)) {
404
			$this->response->body('{"message": "success", "result":'.json_encode($dados).'}');
405
			return;
406
		} else {
407
			$this->response->body('{"message": "error"}');
408
			return;
409
		}	
410
	}
411
412
	/**
413
	* Valida o usuario que está tentando usar a api
414
	*/
415
	public function validate_use_api($req, $api)
416
	{
417
		$this->loadModel('Usuario');
418
		
419
		$data['auth'] = $req->query;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
420
		
421
		$resposta = $this->Usuario->find('all',
422
			array('conditions' => 
423
				array('Usuario.email' => $data['auth']['email'], 
424
					  'Usuario.senha' => sha1($data['auth']['senha'])
425
				)
426
			)
427
		)[0];
428
429
		if (empty($resposta))
430
		{
431
			return false;
432
		}
433
434
		$this->setIdUser($resposta['Usuario']['id']);
435
436
		if (!$this->verifyUseApi($api)) 
437
		{
438
			return false;
439
		}
440
441
		return true;
442
	}
443
444
	public function verifyUseApi($api)
445
	{
446
		$this->loadModel('ModuloRelacionaUsuario');
447
448
		$modulos = $this->ModuloRelacionaUsuario->find('all',
449
		array('conditions' => 
450
			array('ModuloRelacionaUsuario.id_usuario' => $this->getIdUser(), 
451
				  'ModuloRelacionaUsuario.ativo' => 1,
452
				  'Modulo.ativo' => 1
453
				)
454
			)
455
		);
456
457
		foreach ($modulos as $i => $modulo) {
458
			if ($modulo['Modulo']['modulo'] == $api) {
459
				return true;
460
			}
461
		}
462
463
		return false;
464
	}
465
466
	public function setIdUser($id)
467
	{
468
		$this->instancia = $id;
469
	}
470
471
	public function getIdUser()
472
	{
473
		if (!is_numeric($this->instancia))
474
		{
475
			return false;
476
		}
477
478
		return $this->instancia;
479
	}
480
}
481