1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Controller\Admin; |
4
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\ProfileField; |
6
|
|
|
use Apps\Model\Admin\Profile\FormFieldUpdate; |
7
|
|
|
use Apps\Model\Admin\Profile\FormSettings; |
8
|
|
|
use Apps\Model\Front\Profile\FormSettings as FrontFormSettings; |
9
|
|
|
use Extend\Core\Arch\AdminController; |
10
|
|
|
use Apps\ActiveRecord\Profile as ProfileRecords; |
11
|
|
|
use Ffcms\Core\App; |
12
|
|
|
use Ffcms\Core\Exception\ForbiddenException; |
13
|
|
|
use Ffcms\Core\Exception\NotFoundException; |
14
|
|
|
use Ffcms\Core\Helper\HTML\SimplePagination; |
15
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
16
|
|
|
|
17
|
|
|
class Profile extends AdminController |
18
|
|
|
{ |
19
|
|
|
const VERSION = 0.1; |
20
|
|
|
const ITEM_PER_PAGE = 10; |
21
|
|
|
|
22
|
|
|
public $type = 'app'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* List all profiles in website with pagination |
26
|
|
|
* @return string |
27
|
|
|
* @throws \Ffcms\Core\Exception\NativeException |
28
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
29
|
|
|
*/ |
30
|
|
View Code Duplication |
public function actionIndex() |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
// init Active Record |
33
|
|
|
$query = new ProfileRecords(); |
34
|
|
|
|
35
|
|
|
// set current page and offset |
36
|
|
|
$page = (int)$this->request->query->get('page'); |
|
|
|
|
37
|
|
|
$offset = $page * self::ITEM_PER_PAGE; |
38
|
|
|
|
39
|
|
|
// build pagination |
40
|
|
|
$pagination = new SimplePagination([ |
41
|
|
|
'url' => ['profile/index'], |
42
|
|
|
'page' => $page, |
43
|
|
|
'step' => self::ITEM_PER_PAGE, |
44
|
|
|
'total' => $query->count() |
45
|
|
|
]); |
46
|
|
|
|
47
|
|
|
// build listing objects |
48
|
|
|
$records = $query->orderBy('id', 'desc')->skip($offset)->take(self::ITEM_PER_PAGE)->get(); |
49
|
|
|
|
50
|
|
|
// display viewer |
51
|
|
|
return $this->view->render('index', [ |
|
|
|
|
52
|
|
|
'records' => $records, |
53
|
|
|
'pagination' => $pagination |
54
|
|
|
]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Redirect to user controller |
59
|
|
|
* @param $id |
60
|
|
|
*/ |
61
|
|
|
public function actionDelete($id) |
62
|
|
|
{ |
63
|
|
|
$this->response->redirect('user/delete/' . $id); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Profile edit action |
68
|
|
|
* @param int $id |
69
|
|
|
* @return string |
70
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
71
|
|
|
* @throws \Ffcms\Core\Exception\NativeException |
72
|
|
|
* @throws NotFoundException |
73
|
|
|
*/ |
74
|
|
|
public function actionUpdate($id) |
75
|
|
|
{ |
76
|
|
|
if (!Obj::isLikeInt($id) || $id < 1) { |
77
|
|
|
throw new NotFoundException(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// get user profile via id |
81
|
|
|
$profile = ProfileRecords::find($id); |
82
|
|
|
if (false === $profile || null === $profile) { |
83
|
|
|
throw new NotFoundException(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// check if user id for this profile_id is exist |
87
|
|
|
if (!App::$User->isExist($profile->user_id)) { |
88
|
|
|
throw new NotFoundException(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// get user object from profile |
92
|
|
|
$user = $profile->User(); |
93
|
|
|
$model = new FrontFormSettings($user); |
94
|
|
|
|
95
|
|
|
if ($model->send() && $model->validate()) { |
96
|
|
|
$model->save(); |
97
|
|
|
App::$Session->getFlashBag()->add('success', __('Profile is updated')); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->view->render('update', [ |
|
|
|
|
101
|
|
|
'model' => $model->filter(), |
102
|
|
|
'user' => $user, |
103
|
|
|
'profile' => $profile |
104
|
|
|
]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* List additional fields |
109
|
|
|
* @return string |
110
|
|
|
* @throws \Ffcms\Core\Exception\NativeException |
111
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
112
|
|
|
*/ |
113
|
|
|
public function actionFieldlist() |
114
|
|
|
{ |
115
|
|
|
$records = ProfileField::all(); |
116
|
|
|
|
117
|
|
|
return $this->view->render('field_list', [ |
|
|
|
|
118
|
|
|
'records' => $records |
119
|
|
|
]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Add and edit additional fields data |
124
|
|
|
* @param int $id |
125
|
|
|
* @return string |
126
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
127
|
|
|
* @throws \Ffcms\Core\Exception\NativeException |
128
|
|
|
*/ |
129
|
|
|
public function actionFieldupdate($id) |
130
|
|
|
{ |
131
|
|
|
// get current record or new and init form DI |
132
|
|
|
$record = ProfileField::findOrNew($id); |
133
|
|
|
$model = new FormFieldUpdate($record); |
134
|
|
|
|
135
|
|
|
$isNew = false; |
136
|
|
|
if ($record->id === null) { |
137
|
|
|
$isNew = true; |
138
|
|
|
} |
139
|
|
|
// check if form is submited |
140
|
|
|
if ($model->send() && $model->validate()) { |
141
|
|
|
$model->save(); |
142
|
|
|
if (true === $isNew) { |
143
|
|
|
$this->response->redirect('profile/fieldlist'); |
|
|
|
|
144
|
|
|
} |
145
|
|
|
App::$Session->getFlashBag()->add('success', __('Profile field was successful updated')); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $this->view->render('field_update', [ |
|
|
|
|
149
|
|
|
'model' => $model->filter() |
150
|
|
|
]); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Delete custom field action |
155
|
|
|
* @param int $id |
156
|
|
|
* @return string |
157
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
158
|
|
|
* @throws \Ffcms\Core\Exception\NativeException |
159
|
|
|
* @throws ForbiddenException |
160
|
|
|
*/ |
161
|
|
|
public function actionFielddelete($id) |
162
|
|
|
{ |
163
|
|
|
if (!Obj::isLikeInt($id) || $id < 1) { |
164
|
|
|
throw new ForbiddenException(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
// check if record with $id is exist |
168
|
|
|
$record = ProfileField::find($id); |
169
|
|
|
if ($record === null || $record === false) { |
170
|
|
|
throw new ForbiddenException(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$model = new FormFieldUpdate($record); |
|
|
|
|
174
|
|
|
|
175
|
|
|
// if delete is submited - lets remove this record |
176
|
|
|
if ($model->send()) { |
177
|
|
|
$model->delete(); |
178
|
|
|
$this->response->redirect('profile/fieldlist'); |
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $this->view->render('field_delete', [ |
|
|
|
|
182
|
|
|
'model' => $model->filter() |
183
|
|
|
]); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Show profiles settings |
188
|
|
|
* @return string |
189
|
|
|
* @throws \Ffcms\Core\Exception\NativeException |
190
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
191
|
|
|
*/ |
192
|
|
View Code Duplication |
public function actionSettings() |
|
|
|
|
193
|
|
|
{ |
194
|
|
|
$model = new FormSettings($this->getConfigs()); |
195
|
|
|
|
196
|
|
|
if ($model->send()) { |
197
|
|
|
if ($model->validate()) { |
198
|
|
|
$this->setConfigs($model->getAllProperties()); |
199
|
|
|
App::$Session->getFlashBag()->add('success', __('Settings is successful updated')); |
200
|
|
|
$this->response->redirect('profile/index'); |
|
|
|
|
201
|
|
|
} else { |
202
|
|
|
App::$Session->getFlashBag()->add('error', __('Form validation is failed')); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return $this->view->render('settings', [ |
|
|
|
|
207
|
|
|
'model' => $model->filter() |
208
|
|
|
]); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
|
212
|
|
|
} |
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.