|
1
|
|
|
<?php |
|
2
|
|
|
namespace Serverfireteam\Panel; |
|
3
|
|
|
|
|
4
|
|
|
use Illuminate\Routing\Controller; |
|
5
|
|
|
|
|
6
|
|
|
class CrudController extends Controller |
|
7
|
|
|
{ |
|
8
|
|
|
const ID_COLUMN = 'id'; |
|
9
|
|
|
|
|
10
|
|
|
public $grid; |
|
11
|
|
|
public $entity; |
|
12
|
|
|
public $set; |
|
13
|
|
|
public $edit; |
|
14
|
|
|
public $filter; |
|
15
|
|
|
protected $lang; |
|
16
|
|
|
public $helper_message; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(\Lang $lang) |
|
19
|
|
|
{ |
|
20
|
|
|
// $this->entity = $params['entity']; |
|
21
|
|
|
$route = \App::make('route'); |
|
22
|
|
|
$this->lang = $lang; |
|
23
|
|
|
$this->route = $route; |
|
|
|
|
|
|
24
|
|
|
if($route = $route::current()) |
|
25
|
|
|
{ |
|
26
|
|
|
$routeParamters = $route->parameters(); |
|
27
|
|
|
if(isset($routeParamters['entity'])) |
|
28
|
|
|
$this->setEntity($routeParamters['entity']); |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param string $entity name of the entity |
|
34
|
|
|
*/ |
|
35
|
|
|
public function all($entity) |
|
36
|
|
|
{ |
|
37
|
|
|
//$this->addStylesToGrid(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $entity name of the entity |
|
42
|
|
|
*/ |
|
43
|
|
|
public function edit($entity) |
|
44
|
|
|
{ |
|
45
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function getEntity() { |
|
49
|
|
|
return $this->entity; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function setEntity($entity) { |
|
53
|
|
|
$this->entity = $entity; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function getEntityModel() { |
|
57
|
|
|
|
|
58
|
|
|
$entity = $this->getEntity(); |
|
59
|
|
|
|
|
60
|
|
|
$appHelper = new libs\AppHelper; |
|
61
|
|
|
|
|
62
|
|
|
$modelClass = $appHelper->getModel($entity); |
|
63
|
|
|
|
|
64
|
|
|
return new $modelClass; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function addStylesToGrid($orderByColumn = self::ID_COLUMN, $paginateCount = 10) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->grid->edit('edit', trans('panel::fields.edit'), 'show|modify|delete'); |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
if ($orderByColumn === self::ID_COLUMN) { |
|
73
|
|
|
$orderByColumn = $this->getEntityModel()->getKeyName(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$this->grid->orderBy($orderByColumn, 'desc'); |
|
77
|
|
|
$this->grid->paginate($paginateCount); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function addHelperMessage($message) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->helper_message = $message; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Check whether a link is defined for the given URL / model name |
|
87
|
|
|
* @param $url |
|
88
|
|
|
* @throws \Exception |
|
89
|
|
|
*/ |
|
90
|
|
|
private function validateEntity($url) { |
|
91
|
|
|
if (!\Links::all()->pluck('url')->contains($url)) { |
|
92
|
|
|
throw new \Exception('This url is not set yet!'); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function returnView() |
|
97
|
|
|
{ |
|
98
|
|
|
$this->validateEntity($this->entity); |
|
99
|
|
|
return \View::make('panelViews::all', array( |
|
100
|
|
|
'grid' => $this->grid, |
|
101
|
|
|
'filter' => $this->filter, |
|
102
|
|
|
'title' => $this->entity , |
|
103
|
|
|
'current_entity' => $this->entity, |
|
104
|
|
|
'import_message' => (\Session::has('import_message')) ? \Session::get('import_message') : '' |
|
105
|
|
|
)); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function returnEditView() |
|
109
|
|
|
{ |
|
110
|
|
|
$this->validateEntity($this->entity); |
|
111
|
|
|
return \View::make('panelViews::edit', array('title' => $this->entity, |
|
112
|
|
|
'edit' => $this->edit, |
|
113
|
|
|
'helper_message' => $this->helper_message)); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function finalizeFilter() { |
|
117
|
|
|
$lang = \App::make('lang'); |
|
|
|
|
|
|
118
|
|
|
$this->filter->submit($this->lang->get('panel::fields.search')); |
|
119
|
|
|
$this->filter->reset($this->lang->get('panel::fields.reset')); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: