1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Controlador para las acciones y vistas con el usuario |
5
|
|
|
*/ |
6
|
|
|
class UserController extends AppController |
7
|
|
|
{ |
8
|
|
|
|
9
|
|
|
public function index() |
10
|
|
|
{ |
11
|
|
|
Redirect::toAction('page'); |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
public function page(int $page = 1)//validación 'int' con php7 |
15
|
|
|
{ |
16
|
|
|
$this->page = (new User)->paginate("page: $page", 'per_page: 7'); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function create() |
20
|
|
|
{ |
21
|
|
|
//se verifica si se ha enviado via POST los datos |
22
|
|
|
if (Input::hasPost('user')) { |
23
|
|
|
$obj = new User; |
24
|
|
|
//Intenta guardar el usuario junto con la foto |
25
|
|
|
if ($obj->saveWithPhoto(Input::post('user'))) { |
26
|
|
|
//Mensaje de éxito y retorna al listado |
27
|
|
|
Flash::valid('Usuario creado'); |
28
|
|
|
return Redirect::to(); |
29
|
|
|
} |
30
|
|
|
//Si falla se hacen persistentes los datos en el formulario |
31
|
|
|
$this->data = Input::post('user'); |
32
|
|
|
return; |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
View Code Duplication |
public function edit(int $id)//validación 'int' con php7 |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
//Carga los datos del usuario |
39
|
|
|
$this->user = (new User)->find($id); |
40
|
|
|
//se verifica si se ha enviado via POST los datos |
41
|
|
|
if (Input::hasPost('user')) { |
42
|
|
|
//Intenta guardar los cambios |
43
|
|
|
if ($this->user->update(Input::post('user'))) { |
44
|
|
|
//Mensaje de éxito y retorna al listado |
45
|
|
|
Flash::valid('Usuario actualizado'); |
46
|
|
|
return Redirect::to(); |
47
|
|
|
} |
48
|
|
|
//Si falla se hacen persistentes los datos en el formulario |
49
|
|
|
$this->user = Input::post('user'); |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
public function update_photo(int $id)//validación 'int' con php7 |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
//Carga los datos del usuario |
57
|
|
|
$this->user = (new User)->find($id); |
58
|
|
|
//se verifica si se ha enviado via POST los datos |
59
|
|
|
if (Input::hasPost('user')) { |
60
|
|
|
//Si falla al intentar actualizar |
61
|
|
|
if ($this->user->updatePhoto()) { |
62
|
|
|
//Mensaje de éxito y retorna al listado |
63
|
|
|
Flash::valid('Foto de usuario actualizada'); |
64
|
|
|
return Redirect::to(); |
65
|
|
|
} |
66
|
|
|
//se hacen persistentes los datos en el formulario |
67
|
|
|
$this->user = Input::post('user'); |
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.