Passed
Push — master ( a29a7e...12a432 )
by Mihail
08:06
created

Profile::actionFielddelete()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
cc 5
eloc 11
nc 4
nop 1
1
<?php
2
3
namespace Apps\Controller\Admin;
4
5
use Apps\ActiveRecord\Profile as ProfileRecords;
6
use Apps\ActiveRecord\ProfileField;
7
use Apps\Model\Admin\Profile\FormFieldUpdate;
8
use Apps\Model\Admin\Profile\FormSettings;
9
use Apps\Model\Front\Profile\FormSettings as FrontFormSettings;
10
use Extend\Core\Arch\AdminController;
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\Any;
16
17
/**
18
 * Class Profile. Admin controller of profile application.
19
 * @package Apps\Controller\Admin
20
 */
21
class Profile extends AdminController
22
{
23
    const VERSION = '1.0.0';
24
    const ITEM_PER_PAGE = 10;
25
26
    public $type = 'app';
27
28
    /**
29
     * List all profiles in website with pagination
30
     * @return string
31
     * @throws \Ffcms\Core\Exception\SyntaxException
32
     */
33
    public function actionIndex()
34
    {
35
        // init Active Record
36
        $query = ProfileRecords::with(['user']);
37
38
        // set current page and offset
39
        $page = (int)$this->request->query->get('page');
40
        $offset = $page * self::ITEM_PER_PAGE;
41
42
        // build pagination
43
        $pagination = new SimplePagination([
44
            'url' => ['profile/index'],
45
            'page' => $page,
46
            'step' => self::ITEM_PER_PAGE,
47
            'total' => $query->count()
48
        ]);
49
50
        // build listing objects
51
        $records = $query->orderBy('id', 'desc')
52
            ->skip($offset)
53
            ->take(self::ITEM_PER_PAGE)
54
            ->get();
55
56
        // display viewer
57
        return $this->view->render('index', [
58
            'records' => $records,
59
            'pagination' => $pagination
60
        ]);
61
    }
62
63
    /**
64
     * Redirect to user controller
65
     * @param $id
66
     */
67
    public function actionDelete($id)
68
    {
69
        $this->response->redirect('user/delete/' . $id);
70
    }
71
72
    /**
73
     * Profile edit action
74
     * @param int $id
75
     * @return string
76
     * @throws \Ffcms\Core\Exception\SyntaxException
77
     * @throws NotFoundException
78
     */
79
    public function actionUpdate($id)
80
    {
81
        if (!Any::isInt($id) || $id < 1) {
82
            throw new NotFoundException();
83
        }
84
85
        // get user profile via id
86
        $profile = ProfileRecords::find($id);
87
        if (!$profile) {
88
            throw new NotFoundException();
89
        }
90
91
        // check if user id for this profile_id is exist
92
        if (!App::$User->isExist($profile->user_id)) {
0 ignored issues
show
Bug introduced by
The property user_id does not seem to exist on Ffcms\Core\Arch\ActiveModel. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
93
            throw new NotFoundException();
94
        }
95
96
        // initialize settings form and process it
97
        $model = new FrontFormSettings($profile->user);
0 ignored issues
show
Bug introduced by
The property user does not seem to exist on Ffcms\Core\Arch\ActiveModel. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
98
        if ($model->send() && $model->validate()) {
99
            $model->save();
100
            App::$Session->getFlashBag()->add('success', __('Profile is updated'));
101
        }
102
103
        return $this->view->render('update', [
104
            'model' => $model,
105
            'profile' => $profile
106
        ]);
107
    }
108
109
    /**
110
     * List additional fields
111
     * @return string
112
     * @throws \Ffcms\Core\Exception\SyntaxException
113
     */
114
    public function actionFieldlist()
115
    {
116
        $records = ProfileField::all();
117
118
        return $this->view->render('field_list', [
119
            'records' => $records
120
        ]);
121
    }
122
123
    /**
124
     * Add and edit additional fields data
125
     * @param int $id
126
     * @return string
127
     * @throws \Ffcms\Core\Exception\SyntaxException
128
     */
129
    public function actionFieldupdate($id = null)
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
150
        ]);
151
    }
152
153
    /**
154
     * Delete custom field action
155
     * @param int $id
156
     * @return string
157
     * @throws \Ffcms\Core\Exception\SyntaxException
158
     * @throws ForbiddenException
159
     * @throws \Exception
160
     */
161
    public function actionFielddelete($id)
162
    {
163
        if (!Any::isInt($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) {
170
            throw new ForbiddenException();
171
        }
172
173
        $model = new FormFieldUpdate($record);
174
        // if delete is submited - lets remove this record
175
        if ($model->send()) {
176
            $model->delete();
177
            $this->response->redirect('profile/fieldlist');
178
        }
179
180
        return $this->view->render('field_delete', [
181
            'model' => $model
182
        ]);
183
    }
184
185
    /**
186
     * Show profiles settings
187
     * @return string
188
     * @throws \Ffcms\Core\Exception\SyntaxException
189
     */
190
    public function actionSettings()
191
    {
192
        $model = new FormSettings($this->getConfigs());
193
194
        if ($model->send()) {
195
            if ($model->validate()) {
196
                $this->setConfigs($model->getAllProperties());
197
                App::$Session->getFlashBag()->add('success', __('Settings is successful updated'));
198
                $this->response->redirect('profile/index');
199
            } else {
200
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
201
            }
202
        }
203
204
        return $this->view->render('settings', [
205
            'model' => $model
206
        ]);
207
    }
208
}
209