1
|
|
|
<?php |
2
|
|
|
namespace App\Controller\Admin; |
3
|
|
|
|
4
|
|
|
use App\Controller\AppController; |
5
|
|
|
use App\Event\Statistics; |
6
|
|
|
use Cake\Event\Event; |
7
|
|
|
use Cake\I18n\I18n; |
8
|
|
|
|
9
|
|
|
class GroupsController extends AppController |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Helpers. |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
public $helpers = ['I18n']; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Display all Groups. |
21
|
|
|
* |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
|
|
public function index() |
25
|
|
|
{ |
26
|
|
|
$this->paginate = [ |
27
|
|
|
'maxLimit' => 15 |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
$groups = $this->Groups |
|
|
|
|
31
|
|
|
->find() |
32
|
|
|
->order([ |
33
|
|
|
'Groups.created' => 'desc' |
34
|
|
|
]); |
35
|
|
|
|
36
|
|
|
$groups = $this->paginate($groups); |
37
|
|
|
$this->set(compact('groups')); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Add a Group. |
42
|
|
|
* |
43
|
|
|
* @return \Cake\Network\Response|void |
44
|
|
|
*/ |
45
|
|
|
public function add() |
46
|
|
|
{ |
47
|
|
|
$this->Groups->locale(I18n::defaultLocale()); |
|
|
|
|
48
|
|
|
$group = $this->Groups->newEntity($this->request->getParsedBody()); |
|
|
|
|
49
|
|
|
|
50
|
|
View Code Duplication |
if ($this->request->is('post')) { |
|
|
|
|
51
|
|
|
$group->setTranslations($this->request->getParsedBody()); |
52
|
|
|
|
53
|
|
|
if ($this->Groups->save($group)) { |
|
|
|
|
54
|
|
|
//Event. |
55
|
|
|
$this->eventManager()->attach(new Statistics()); |
|
|
|
|
56
|
|
|
|
57
|
|
|
$stats = new Event('Model.Groups.update', $this); |
58
|
|
|
$this->eventManager()->dispatch($stats); |
59
|
|
|
|
60
|
|
|
$this->Flash->success(__d('admin', 'Your group has been created successfully !')); |
61
|
|
|
|
62
|
|
|
return $this->redirect(['action' => 'index']); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->set(compact('group')); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Edit a Group. |
71
|
|
|
* |
72
|
|
|
* @return \Cake\Network\Response|void |
73
|
|
|
*/ |
74
|
|
|
public function edit() |
75
|
|
|
{ |
76
|
|
|
$this->Groups->locale(I18n::defaultLocale()); |
|
|
|
|
77
|
|
|
$group = $this->Groups |
|
|
|
|
78
|
|
|
->find('translations') |
79
|
|
|
->where([ |
80
|
|
|
'Groups.id' => $this->request->id |
81
|
|
|
]) |
82
|
|
|
->first(); |
83
|
|
|
|
84
|
|
|
//Check if the group is found. |
85
|
|
|
if (empty($group)) { |
86
|
|
|
$this->Flash->error(__d('admin', 'This group doesn\'t exist or has been deleted.')); |
87
|
|
|
|
88
|
|
|
return $this->redirect(['action' => 'index']); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
View Code Duplication |
if ($this->request->is('put')) { |
|
|
|
|
92
|
|
|
$this->Groups->patchEntity($group, $this->request->getParsedBody()); |
|
|
|
|
93
|
|
|
$group->setTranslations($this->request->getParsedBody()); |
94
|
|
|
|
95
|
|
|
if ($this->Groups->save($group)) { |
|
|
|
|
96
|
|
|
//Event. |
97
|
|
|
$this->eventManager()->attach(new Statistics()); |
|
|
|
|
98
|
|
|
|
99
|
|
|
$stats = new Event('Model.Groups.update', $this); |
100
|
|
|
$this->eventManager()->dispatch($stats); |
101
|
|
|
|
102
|
|
|
$this->Flash->success(__d('admin', 'This group has been updated successfully !')); |
103
|
|
|
|
104
|
|
|
return $this->redirect(['action' => 'index']); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->set(compact('group')); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Delete a group. |
113
|
|
|
* |
114
|
|
|
* @return \Cake\Network\Response |
115
|
|
|
*/ |
116
|
|
|
public function delete() |
117
|
|
|
{ |
118
|
|
|
$group = $this->Groups |
|
|
|
|
119
|
|
|
->find() |
120
|
|
|
->where([ |
121
|
|
|
'Groups.id' => $this->request->id |
122
|
|
|
]) |
123
|
|
|
->contain([ |
124
|
|
|
'Users' => function ($q) { |
125
|
|
|
return $q->limit(1); |
126
|
|
|
} |
127
|
|
|
]) |
128
|
|
|
->first(); |
129
|
|
|
|
130
|
|
|
//Check if the group is found. |
131
|
|
|
if (empty($group)) { |
132
|
|
|
$this->Flash->error(__d('admin', 'This group doesn\'t exist or has been deleted.')); |
133
|
|
|
|
134
|
|
|
return $this->redirect(['action' => 'index']); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
//Check if the group is assigned to one or more user(s). |
138
|
|
|
if (!empty($group->users)) { |
139
|
|
|
$this->Flash->error(__d('admin', 'This group is assigned to one or more user(s). You must change their group before to delete this group.')); |
140
|
|
|
|
141
|
|
|
return $this->redirect(['action' => 'index']); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if ($this->Groups->delete($group)) { |
|
|
|
|
145
|
|
|
//Event. |
146
|
|
|
$this->eventManager()->attach(new Statistics()); |
|
|
|
|
147
|
|
|
|
148
|
|
|
$stats = new Event('Model.Groups.update', $this); |
149
|
|
|
$this->eventManager()->dispatch($stats); |
150
|
|
|
|
151
|
|
|
$this->Flash->success(__d('admin', 'This group has been deleted successfully !')); |
152
|
|
|
|
153
|
|
|
return $this->redirect(['action' => 'index']); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$this->Flash->error(__d('admin', 'Unable to delete this group.')); |
157
|
|
|
|
158
|
|
|
return $this->redirect(['action' => 'index']); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.