Passed
Push — master ( 3de7ee...b534e3 )
by Mihail
14:25
created

Profile::actionNotifications()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 23
nc 3
nop 1
dl 0
loc 39
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Apps\Controller\Front;
4
5
use Apps\ActiveRecord\Blacklist;
6
use Apps\ActiveRecord\Profile as ProfileRecords;
7
use Apps\ActiveRecord\UserLog;
8
use Apps\ActiveRecord\UserNotification;
9
use Apps\Model\Front\Profile\FormIgnoreDelete;
10
use Apps\Model\Front\Profile\FormPasswordChange;
11
use Apps\Model\Front\Profile\FormSettings;
12
use Apps\Model\Front\Sitemap\EntityBuildMap;
13
use Extend\Core\Arch\FrontAppController;
14
use Ffcms\Core\App;
15
use Ffcms\Core\Exception\ForbiddenException;
16
use Ffcms\Core\Exception\NotFoundException;
17
use Ffcms\Core\Exception\SyntaxException;
18
use Ffcms\Core\Helper\Type\Any;
19
use Ffcms\Core\Helper\Url;
20
21
/**
22
 * Class Profile. User profiles application front controller
23
 * @package Apps\Controller\Front
24
 */
25
class Profile extends FrontAppController
26
{
27
    const BLOCK_PER_PAGE = 10;
28
    const EVENT_CHANGE_PASSWORD = 'profile.changepassword.success';
29
30
    /**
31
     * Fatty action like actionIndex(), actionShow() are located in standalone traits.
32
     * This feature allow provide better read&write accessibility
33
     */
34
35
    use Profile\ActionIndex {
36
        index as actionIndex;
37
    }
38
39
    use Profile\ActionShow {
40
        show as actionShow;
41
    }
42
43
    use Profile\ActionFeed {
44
        feed as actionFeed;
45
    }
46
47
    use Profile\ActionWallDelete {
48
        wallDelete as actionWalldelete;
49
    }
50
51
    use Profile\ActionAvatar {
52
        avatar as actionAvatar;
53
    }
54
55
    use Profile\ActionNotifications {
56
        notifications as actionNotifications;
57
    }
58
59
    use Profile\ActionIgnore {
60
        ignore as actionIgnore;
61
    }
62
63
    use Profile\ActionSearch {
64
        search as actionSearch;
65
    }
66
67
68
    /**
69
     * Show user messages (based on ajax, all in template)
70
     * @return string
71
     * @throws \Ffcms\Core\Exception\SyntaxException
72
     * @throws ForbiddenException
73
     */
74
    public function actionMessages()
75
    {
76
        if (!App::$User->isAuth()) {
77
            throw new ForbiddenException();
78
        }
79
80
        return $this->view->render('messages');
81
    }
82
83
    /**
84
     * User profile settings
85
     * @return string
86
     * @throws \Ffcms\Core\Exception\SyntaxException
87
     * @throws ForbiddenException
88
     */
89
    public function actionSettings()
90
    {
91
        // check if auth
92
        if (!App::$User->isAuth()) {
93
            throw new ForbiddenException();
94
        }
95
96
        // get user object
97
        $user = App::$User->identity();
98
        // create model and pass user object
99
        $model = new FormSettings($user);
100
101
        // check if form is submited
102
        if ($model->send() && $model->validate()) {
103
            $model->save();
104
            App::$Session->getFlashBag()->add('success', __('Profile data are successful updated'));
105
        }
106
107
        // render view
108
        return $this->view->render('settings', [
109
            'model' => $model
110
        ]);
111
    }
112
113
    /**
114
     * Action change user password
115
     * @return string
116
     * @throws \Ffcms\Core\Exception\SyntaxException
117
     * @throws ForbiddenException
118
     */
119
    public function actionPassword()
120
    {
121
        // check if user is authed
122
        if (!App::$User->isAuth()) {
123
            throw new ForbiddenException();
124
        }
125
126
        // get user object and create model with user object
127
        $user = App::$User->identity();
128
        $model = new FormPasswordChange($user);
129
130
        // check if form is submited and validation is passed
131 View Code Duplication
        if ($model->send() && $model->validate()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
132
            $model->make();
133
            App::$Event->run(static::EVENT_CHANGE_PASSWORD, [
134
                'model' => $model
135
            ]);
136
137
            App::$Session->getFlashBag()->add('success', __('Password is successful changed'));
138
        }
139
140
        // set response output
141
        return $this->view->render('password', [
142
            'model' => $model
143
        ]);
144
    }
145
146
    /**
147
     * Show user logs
148
     * @return string
149
     * @throws \Ffcms\Core\Exception\SyntaxException
150
     * @throws ForbiddenException
151
     */
152
    public function actionLog()
153
    {
154
        // check if user is authorized
155
        if (!App::$User->isAuth()) {
156
            throw new ForbiddenException();
157
        }
158
159
        // get log records
160
        $records = UserLog::where('user_id', App::$User->identity()->getId());
161
        if ($records->count() > 0) {
162
            $records = $records->orderBy('id', 'DESC');
163
        }
164
165
        // render output view
166
        return $this->view->render('log', [
167
            'records' => $records
168
        ]);
169
    }
170
171
    /**
172
     * Unblock always blocked user
173
     * @param string $targetId
174
     * @return string
175
     * @throws \Ffcms\Core\Exception\SyntaxException
176
     * @throws ForbiddenException
177
     * @throws NotFoundException
178
     * @throws \Exception
179
     */
180
    public function actionUnblock($targetId)
181
    {
182
        // check if user is auth
183
        if (!App::$User->isAuth()) {
184
            throw new ForbiddenException();
185
        }
186
187
        // check if target is defined
188 View Code Duplication
        if (!Any::isInt($targetId) || $targetId < 1 || !App::$User->isExist($targetId)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
189
            throw new NotFoundException();
190
        }
191
192
        $user = App::$User->identity();
193
194
        // check if target user in blacklist of current user
195
        if (!Blacklist::have($user->getId(), $targetId)) {
196
            throw new NotFoundException();
197
        }
198
199
        $model = new FormIgnoreDelete($user, $targetId);
200
        if ($model->send() && $model->validate()) {
201
            $model->make();
202
            $this->response->redirect(Url::to('profile/ignore'));
203
        }
204
205
        return $this->view->render('unblock', [
206
            'model' => $model
207
        ]);
208
    }
209
210
    /**
211
     * Cron schedule - build user profiles sitemap
212
     */
213
    public static function buildSitemapSchedule()
214
    {
215
        // get not empty user profiles
216
        $profiles = ProfileRecords::whereNotNull('nick');
217
        if ($profiles->count() < 1) {
218
            return;
219
        }
220
221
        // get languages if multilanguage enabled
222
        $langs = null;
223
        if (App::$Properties->get('multiLanguage')) {
224
            $langs = App::$Properties->get('languages');
225
        }
226
227
        // build sitemap from content items via business model
228
        $sitemap = new EntityBuildMap($langs);
229
        foreach ($profiles->get() as $user) {
230
            $sitemap->add('profile/show/' . $user->user_id, $user->updated_at, 'weekly', 0.2);
231
        }
232
233
        try {
234
            $sitemap->save('profile');
235
        } catch (SyntaxException $e){}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
236
    }
237
238
    /**
239
     * Cleanup tables as scheduled action
240
     */
241
    public static function cleanupTablesSchedule()
242
    {
243
        // calculate date (now - 1week) for sql query
244
        $date = (new \DateTime('now'))->modify('-1 week')->format('Y-m-d');
245
        try {
246
            UserNotification::where('created_at', '<=', $date)->delete();
247
            UserLog::where('created_at', '<=', $date)->delete();
248
        } catch (\Exception $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
249
    }
250
}
251