Test Failed
Push — master ( e3c39f...fe570d )
by Mihail
07:20
created

Apps/Controller/Front/Profile.php (8 issues)

1
<?php
2
3
namespace Apps\Controller\Front;
4
5
use Apps\ActiveRecord\Profile as ProfileRecords;
6
use Apps\ActiveRecord\UserLog;
7
use Apps\ActiveRecord\UserNotification;
8
use Apps\Model\Front\Profile\FormSettings;
9
use Apps\Model\Front\Sitemap\EntityBuildMap;
10
use Extend\Core\Arch\FrontAppController;
11
use Ffcms\Core\App;
12
use Ffcms\Core\Exception\ForbiddenException;
13
use Ffcms\Core\Exception\SyntaxException;
14
15
/**
16
 * Class Profile. User profiles application front controller
17
 * @package Apps\Controller\Front
18
 */
19
class Profile extends FrontAppController
20
{
21
    const BLOCK_PER_PAGE = 10;
22
    const EVENT_CHANGE_PASSWORD = 'profile.changepassword.success';
23
    const NOTIFY_PER_PAGE = 25;
24
    const FEED_PER_PAGE = 10;
25
    const LOG_PER_PAGE = 5;
26
27
    /**
28
     * Fat actions like actionIndex(), actionShow() are located in standalone traits.
29
     * This feature allow provide better read&write accessibility
30
     */
31
32
    use Profile\ActionIndex {
0 ignored issues
show
The trait Apps\Controller\Front\Profile\ActionIndex requires the property $query which is not provided by Apps\Controller\Front\Profile.
Loading history...
33
        index as actionIndex;
34
    }
35
36
    use Profile\ActionShow {
37
        show as actionShow;
38
    }
39
40
    use Profile\ActionFeed {
0 ignored issues
show
The trait Apps\Controller\Front\Profile\ActionFeed requires the property $query which is not provided by Apps\Controller\Front\Profile.
Loading history...
41
        feed as actionFeed;
42
    }
43
44
    use Profile\ActionWallDelete {
45
        wallDelete as actionWalldelete;
46
    }
47
48
    use Profile\ActionAvatar {
49
        avatar as actionAvatar;
50
    }
51
52
    use Profile\ActionNotifications {
0 ignored issues
show
The trait Apps\Controller\Front\Profile\ActionNotifications requires the property $query which is not provided by Apps\Controller\Front\Profile.
Loading history...
53
        notifications as actionNotifications;
54
    }
55
56
    use Profile\ActionIgnore {
0 ignored issues
show
The trait Apps\Controller\Front\Profile\ActionIgnore requires the property $query which is not provided by Apps\Controller\Front\Profile.
Loading history...
57
        ignore as actionIgnore;
58
    }
59
60
    use Profile\ActionSearch {
0 ignored issues
show
The trait Apps\Controller\Front\Profile\ActionSearch requires the property $query which is not provided by Apps\Controller\Front\Profile.
Loading history...
61
        search as actionSearch;
62
    }
63
64
    use Profile\ActionUnblock {
65
        unblock as actionUnblock;
66
    }
67
68
    use Profile\ActionPassword {
69
        password as actionPassword;
70
    }
71
72
73
    /**
74
     * Show user messages (based on ajax, all in template)
75
     * @return string
76
     * @throws ForbiddenException
77
     */
78
    public function actionMessages()
79
    {
80
        if (!App::$User->isAuth()) {
81
            throw new ForbiddenException();
82
        }
83
84
        return $this->view->render('profile/messages');
85
    }
86
87
    /**
88
     * User profile settings
89
     * @return string
90
     * @throws \Ffcms\Core\Exception\SyntaxException
91
     * @throws ForbiddenException
92
     */
93
    public function actionSettings()
94
    {
95
        // check if auth
96
        if (!App::$User->isAuth()) {
97
            throw new ForbiddenException();
98
        }
99
100
        // get user object
101
        $user = App::$User->identity();
102
        // create model and pass user object
103
        $model = new FormSettings($user);
0 ignored issues
show
It seems like $user can also be of type null; however, parameter $user of Apps\Model\Front\Profile...Settings::__construct() does only seem to accept Ffcms\Core\Interfaces\iUser, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
        $model = new FormSettings(/** @scrutinizer ignore-type */ $user);
Loading history...
104
105
        // check if form is submited
106
        if ($model->send() && $model->validate()) {
107
            $model->save();
108
            App::$Session->getFlashBag()->add('success', __('Profile data are successful updated'));
109
        }
110
111
        // render view
112
        return $this->view->render('profile/settings', [
113
            'model' => $model
114
        ]);
115
    }
116
117
    /**
118
     * Show user logs
119
     * @return string
120
     * @throws ForbiddenException
121
     */
122
    public function actionLog()
123
    {
124
        // check if user is authorized
125
        if (!App::$User->isAuth()) {
126
            throw new ForbiddenException();
127
        }
128
129
        // get log records
130
        $records = UserLog::where('user_id', App::$User->identity()->getId());
131
132
        // build pagination info
133
        $totalCount = $records->count();
134
        $page = (int)$this->request->query->get('page', 0);
135
        $offset = $page * static::LOG_PER_PAGE;
136
137
        // apply pagination limits
138
        $records = $records->skip($offset)
139
            ->take(static::LOG_PER_PAGE)
140
            ->orderBy('id', 'DESC')
141
            ->get();
142
143
        // render output view
144
        return $this->view->render('profile/log', [
145
            'records' => $records,
146
            'pagination' => [
147
                'step' => static::LOG_PER_PAGE,
148
                'total' => $totalCount,
149
                'page' => $page
150
            ]
151
        ]);
152
    }
153
154
    /**
155
     * Cron schedule - build user profiles sitemap
156
     */
157
    public static function buildSitemapSchedule()
158
    {
159
        // get not empty user profiles
160
        $profiles = ProfileRecords::whereNotNull('nick');
161
        if ($profiles->count() < 1) {
162
            return;
163
        }
164
165
        // get languages if multilanguage enabled
166
        $langs = null;
167
        if (App::$Properties->get('multiLanguage')) {
168
            $langs = App::$Properties->get('languages');
169
        }
170
171
        // build sitemap from content items via business model
172
        $sitemap = new EntityBuildMap($langs);
173
        foreach ($profiles->get() as $user) {
174
            $sitemap->add('profile/show/' . $user->user_id, $user->updated_at, 'weekly', 0.2);
175
        }
176
177
        try {
178
            $sitemap->save('profile');
179
        } catch (SyntaxException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
180
        }
181
    }
182
183
    /**
184
     * Cleanup tables as scheduled action
185
     */
186
    public static function cleanupTablesSchedule()
187
    {
188
        // calculate date (now - 1week) for sql query
189
        $date = (new \DateTime('now'))->modify('-1 week')->format('Y-m-d');
190
        try {
191
            UserNotification::where('created_at', '<=', $date)->delete();
192
            UserLog::where('created_at', '<=', $date)->delete();
193
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
194
        }
195
    }
196
}
197