1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Controlador base para la construcción de CRUD para modelos rápidamente |
5
|
|
|
* |
6
|
|
|
* @category Kumbia |
7
|
|
|
* @package Controller |
8
|
|
|
*/ |
9
|
|
|
class ScaffoldController extends AdminController |
10
|
|
|
{ |
11
|
|
|
/** @var string Carpeta en views/_shared/scaffolds/ */ |
12
|
|
|
public $scaffold = 'kumbia'; |
13
|
|
|
/** @var string Nombre del modelo en CamelCase */ |
14
|
|
|
public $model = ''; |
15
|
|
|
|
16
|
|
|
public function index($page=1) |
17
|
|
|
{ |
18
|
|
|
$this->data = (new $this->model)->paginate("page: $page", 'order: id desc'); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Crea un Registro |
23
|
|
|
*/ |
24
|
|
|
public function crear() |
25
|
|
|
{ |
26
|
|
View Code Duplication |
if (Input::hasPost($this->model)) { |
|
|
|
|
27
|
|
|
|
28
|
|
|
$obj = new $this->model; |
29
|
|
|
//En caso que falle la operación de guardar |
30
|
|
|
if (!$obj->save(Input::post($this->model))) { |
31
|
|
|
Flash::error('Falló Operación'); |
32
|
|
|
//se hacen persistente los datos en el formulario |
33
|
|
|
$this->{$this->model} = $obj; |
34
|
|
|
return; |
35
|
|
|
} |
36
|
|
|
return Redirect::to(); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
// Sólo es necesario para el autoForm |
39
|
|
|
$this->{$this->model} = new $this->model; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Edita un Registro |
44
|
|
|
*/ |
45
|
|
|
public function editar($id) |
46
|
|
|
{ |
47
|
|
|
View::select('crear'); |
48
|
|
|
|
49
|
|
|
//se verifica si se ha enviado via POST los datos |
50
|
|
View Code Duplication |
if (Input::hasPost($this->model)) { |
|
|
|
|
51
|
|
|
$obj = new $this->model; |
52
|
|
|
if (!$obj->update(Input::post($this->model))) { |
53
|
|
|
Flash::error('Falló Operación'); |
54
|
|
|
//se hacen persistente los datos en el formulario |
55
|
|
|
$this->{$this->model} = Input::post($this->model); |
56
|
|
|
} else { |
57
|
|
|
return Redirect::to(); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
//Aplicando la autocarga de objeto, para comenzar la edición |
62
|
|
|
$this->{$this->model} = (new $this->model)->find((int) $id); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Borra un Registro |
67
|
|
|
*/ |
68
|
|
|
public function borrar($id) |
69
|
|
|
{ |
70
|
|
|
if (!(new $this->model)->delete((int) $id)) { |
71
|
|
|
Flash::error('Falló Operación'); |
72
|
|
|
} |
73
|
|
|
//enrutando al index para listar los articulos |
74
|
|
|
Redirect::to(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Ver un Registro |
79
|
|
|
*/ |
80
|
|
|
public function ver($id) |
81
|
|
|
{ |
82
|
|
|
$this->data = (new $this->model)->find_first((int) $id); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
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.