Passed
Push — master ( 3d6fe1...2c7b25 )
by Mihail
05:25
created

Profile::buildSitemapSchedule()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 22
rs 8.9197
1
<?php
2
3
namespace Apps\Controller\Front;
4
5
use Apps\ActiveRecord\Blacklist;
6
use Apps\ActiveRecord\WallPost;
7
use Apps\Model\Front\Profile\FormAvatarUpload;
8
use Apps\Model\Front\Profile\FormIgnoreAdd;
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\Profile\FormUserSearch;
13
use Apps\Model\Front\Profile\FormWallPostDelete;
14
use Apps\Model\Front\Profile\FormWallPost;
15
use Apps\Model\Front\Sitemap\EntityBuildMap;
16
use Extend\Core\Arch\FrontAppController;
17
use Ffcms\Core\App;
18
use Ffcms\Core\Exception\ForbiddenException;
19
use Ffcms\Core\Exception\NotFoundException;
20
use Ffcms\Core\Helper\HTML\SimplePagination;
21
use Ffcms\Core\Helper\Type\Obj;
22
use Ffcms\Core\Helper\Serialize;
23
use Ffcms\Core\Helper\Type\Str;
24
use Apps\ActiveRecord\Profile as ProfileRecords;
25
use Ffcms\Core\Helper\Url;
26
27
28
/**
29
 * Class Profile - user profiles interaction
30
 * @package Apps\Controller\Front
31
 */
32
class Profile extends FrontAppController
33
{
34
    const BLOCK_PER_PAGE = 10;
35
36
    /**
37
     * List user profiles on website by defined filter
38
     * @param string $filter_name
39
     * @param null|string|int $filter_value
40
     * @return string
41
     * @throws \Ffcms\Core\Exception\NativeException
42
     * @throws NotFoundException
43
     * @throws \Ffcms\Core\Exception\SyntaxException
44
     */
45
    public function actionIndex($filter_name, $filter_value = null)
46
    {
47
        $records = null;
48
49
        // set current page and offset
50
        $page = (int)App::$Request->query->get('page');
51
        $cfgs = Serialize::decode($this->application->configs);
52
        $userPerPage = (int)$cfgs['usersOnPage'];
53
        if ($userPerPage < 1) {
54
            $userPerPage = 1;
55
        }
56
        $offset = $page * $userPerPage;
57
58
        switch ($filter_name) {
59
            case 'rating': // rating list, order by rating DESC
60
                // check if rating is enabled
61
                if ((int)$cfgs['rating'] !== 1) {
62
                    throw new NotFoundException();
63
                }
64
                $records = (new ProfileRecords())->orderBy('rating', 'DESC');
65
                break;
66
            case 'hobby': // search by hobby
67
                if (Str::likeEmpty($filter_value)) {
0 ignored issues
show
Bug introduced by
It seems like $filter_value defined by parameter $filter_value on line 45 can also be of type integer or string; however, Ffcms\Core\Helper\Type\Str::likeEmpty() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
68
                    throw new NotFoundException();
69
                }
70
                $records = (new ProfileRecords())->where('hobby', 'like', '%' . $filter_value . '%');
71
                break;
72
            case 'city':
73
                if (Str::likeEmpty($filter_value)) {
0 ignored issues
show
Bug introduced by
It seems like $filter_value defined by parameter $filter_value on line 45 can also be of type integer or string; however, Ffcms\Core\Helper\Type\Str::likeEmpty() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
74
                    throw new NotFoundException();
75
                }
76
                $records = (new ProfileRecords())->where('city', '=', $filter_value);
77
                break;
78
            case 'born':
79
                if ($filter_value === null || !Obj::isLikeInt($filter_value)) {
80
                    throw new NotFoundException();
81
                }
82
                $records = (new ProfileRecords())->where('birthday', 'like', $filter_value . '-%');
83
                break;
84
            case 'all':
85
                $records = (new ProfileRecords())->orderBy('id', 'DESC');
86
                break;
87
            default:
88
                App::$Response->redirect('profile/index/all');
89
                break;
90
        }
91
92
        // build pagination
93
        $pagination = new SimplePagination([
94
            'url' => ['profile/index', $filter_name, $filter_value],
95
            'page' => $page,
96
            'step' => $userPerPage,
97
            'total' => $records->count()
98
        ]);
99
100
        return App::$View->render('index', [
101
            'records' => $records->skip($offset)->take($userPerPage)->get(),
102
            'pagination' => $pagination,
103
            'id' => $filter_name,
104
            'add' => $filter_value,
105
            'ratingOn' => (int)$cfgs['rating']
106
        ]);
107
    }
108
109
    /**
110
     * Show user profile: data, wall posts, other features
111
     * @param int $userId
112
     * @return string
113
     * @throws \Ffcms\Core\Exception\SyntaxException
114
     * @throws \Ffcms\Core\Exception\NativeException
115
     * @throws NotFoundException
116
     * @throws ForbiddenException
117
     */
118
    public function actionShow($userId)
119
    {
120
        $cfg = Serialize::decode($this->application->configs);
121 View Code Duplication
        if ((int)$cfg['guestView'] !== 1 && !App::$User->isAuth()) {
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...
122
            throw new ForbiddenException(__('You must login to view other profile'));
123
        }
124
        // check if target exists
125
        if (!App::$User->isExist($userId)) {
126
            throw new NotFoundException(__('This profile is not exist'));
127
        }
128
129
        $targetPersone = App::$User->identity($userId); // target user object instance of Apps\ActiveRecord\User
130
        $viewerPersone = App::$User->identity(); // current user object(viewer) instance of Apps\ActiveRecord\User
131
132
        $wallModel = null;
133
        // if current user is auth - allow to post messages on wall current user
134
        if (App::$User->isAuth() && $viewerPersone->getRole()->can('global/write')) {
135
            $wallModel = new FormWallPost();
136
            // check if request post is done and rules validated
137
            if ($wallModel->send() && $wallModel->validate()) {
138
                // maybe in blacklist?
139
                if (!Blacklist::check($viewerPersone->getId(), $targetPersone->getId())) {
140
                    App::$Session->getFlashBag()->add('error', __('This user are in your black list or you are in blacklist!'));
141
                } else {
142
                    // check if message added
143
                    if ($wallModel->makePost($targetPersone, $viewerPersone, (int)$cfg['delayBetweenPost'])) {
0 ignored issues
show
Bug introduced by
It seems like $targetPersone defined by \Ffcms\Core\App::$User->identity($userId) on line 129 can be null; however, Apps\Model\Front\Profile\FormWallPost::makePost() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
Bug introduced by
It seems like $viewerPersone defined by \Ffcms\Core\App::$User->identity() on line 130 can be null; however, Apps\Model\Front\Profile\FormWallPost::makePost() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
144
                        App::$Session->getFlashBag()->add('success', __('The message was successful posted!'));
145
                    } else {
146
                        App::$Session->getFlashBag()->add('warning', __('Posting message was failed! You need to wait some time...'));
147
                    }
148
                }
149
            }
150
        }
151
152
        $query = $targetPersone->getWall(); // relation hasMany from users to walls
153
        // pagination and query params
154
        $wallPage = (int)App::$Request->query->get('page');
155
        $wallItems = (int)$cfg['wallPostOnPage'];
156
        $wallOffset = $wallPage * $wallItems;
157
158
        // build pagination
159
        $wallPagination = new SimplePagination([
160
            'url' => ['profile/show', $userId, null],
161
            'page' => $wallPage,
162
            'step' => $wallItems,
163
            'total' => $query->count()
164
        ]);
165
166
        // get wall messages
167
        $wallRecords = $query->orderBy('id', 'desc')->skip($wallOffset)->take($wallItems)->get();
168
169
        return App::$View->render('show', [
170
            'user' => $targetPersone,
171
            'viewer' => $viewerPersone,
172
            'isSelf' => ($viewerPersone !== null && $viewerPersone->id === $targetPersone->id),
0 ignored issues
show
Bug introduced by
Accessing id on the interface Ffcms\Core\Interfaces\iUser suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
173
            'wall' => !Obj::isObject($wallModel) ? null : $wallModel->filter(),
174
            'notify' => App::$Session->getFlashBag()->all(),
175
            'wallRecords' => $wallRecords,
176
            'pagination' => $wallPagination,
177
            'ratingOn' => (int)$cfg['rating'] === 1
178
        ]);
179
    }
180
181
    /**
182
     * User avatar management
183
     * @throws \Ffcms\Core\Exception\NativeException
184
     * @throws \Ffcms\Core\Exception\SyntaxException
185
     */
186
    public function actionAvatar()
187
    {
188
        if (!App::$User->isAuth()) {
189
            throw new ForbiddenException('You must be authorized user!');
190
        }
191
192
        // get user identity and model object
193
        $user = App::$User->identity();
194
        $model = new FormAvatarUpload(true);
0 ignored issues
show
Unused Code introduced by
The call to FormAvatarUpload::__construct() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
195
196
        // validate model post data
197
        if ($model->send()) {
198
            if ($model->validate()) {
199
                $model->copyFile($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by \Ffcms\Core\App::$User->identity() on line 193 can be null; however, Apps\Model\Front\Profile...vatarUpload::copyFile() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
200
                App::$Session->getFlashBag()->add('success', __('Avatar is successful changed'));
201
            } else {
202
                App::$Session->getFlashBag()->add('error', __('File upload is failed!'));
203
            }
204
        }
205
206
        return App::$View->render('avatar', [
207
            'user' => $user,
208
            'model' => $model
209
        ]);
210
    }
211
212
    /**
213
     * Allow post owners and targets delete
214
     * @param int $postId
215
     * @return string
216
     * @throws \Ffcms\Core\Exception\SyntaxException
217
     * @throws \Ffcms\Core\Exception\NativeException
218
     * @throws ForbiddenException
219
     * @throws NotFoundException
220
     */
221
    public function actionWalldelete($postId)
222
    {
223
        // is user auth?
224
        if (!App::$User->isAuth()) {
225
            throw new ForbiddenException();
226
        }
227
228
        // is postId is integer?
229
        if (!Obj::isLikeInt($postId) || $postId < 1) {
230
            throw new NotFoundException();
231
        }
232
233
        // try to find the wall post
234
        $wallPost = WallPost::find($postId);
235
        if (null === $wallPost || false === $wallPost) {
236
            throw new NotFoundException();
237
        }
238
239
        // get user and check if he can delete this post
240
        $user = App::$User->identity();
241
        if ($wallPost->sender_id !== $user->id && $wallPost->target_id !== $user->id) {
0 ignored issues
show
Bug introduced by
Accessing id on the interface Ffcms\Core\Interfaces\iUser suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
242
            throw new ForbiddenException();
243
        }
244
245
        // check if submit sended
246
        $wallModel = new FormWallPostDelete($wallPost);
0 ignored issues
show
Compatibility introduced by
$wallPost of type object<Ffcms\Core\Arch\ActiveModel> is not a sub-type of object<Apps\ActiveRecord\WallPost>. It seems like you assume a child class of the class Ffcms\Core\Arch\ActiveModel to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
247
        if ($wallModel->send() && $wallModel->validate()) {
248
            $wallModel->make();
249
            App::$Response->redirect('profile/show/' . $wallPost->target_id);
250
        }
251
252
        return App::$View->render('wall_delete', [
253
            'post' => $wallPost,
254
            'model' => $wallModel->filter()
255
        ]);
256
    }
257
258
    /**
259
     * Show user messages (based on ajax, all in template)
260
     * @return string
261
     * @throws \Ffcms\Core\Exception\SyntaxException
262
     * @throws \Ffcms\Core\Exception\NativeException
263
     * @throws ForbiddenException
264
     */
265
    public function actionMessages()
266
    {
267
        if (!App::$User->isAuth()) {
268
            throw new ForbiddenException();
269
        }
270
271
        return App::$View->render('messages');
272
    }
273
274
    /**
275
     * User profile settings
276
     * @return string
277
     * @throws \Ffcms\Core\Exception\SyntaxException
278
     * @throws \Ffcms\Core\Exception\NativeException
279
     * @throws ForbiddenException
280
     */
281 View Code Duplication
    public function actionSettings()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
282
    {
283
        // check if auth
284
        if (!App::$User->isAuth()) {
285
            throw new ForbiddenException();
286
        }
287
        // get user object
288
        $user = App::$User->identity();
289
        // create model and pass user object
290
        $model = new FormSettings($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by \Ffcms\Core\App::$User->identity() on line 288 can be null; however, Apps\Model\Front\Profile...Settings::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
291
292
        // check if form is submited
293
        if ($model->send() && $model->validate()) {
294
            $model->save();
295
            App::$Session->getFlashBag()->add('success', __('Profile data are successful updated'));
296
        }
297
298
        // render view
299
        return App::$View->render('settings', [
300
            'model' => $model->filter()
301
        ]);
302
    }
303
304
    /**
305
     * Action change user password
306
     * @return string
307
     * @throws \Ffcms\Core\Exception\SyntaxException
308
     * @throws \Ffcms\Core\Exception\NativeException
309
     * @throws ForbiddenException
310
     */
311 View Code Duplication
    public function actionPassword()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
312
    {
313
        // check if user is authed
314
        if (!App::$User->isAuth()) {
315
            throw new ForbiddenException();
316
        }
317
318
        // get user object and create model with user object
319
        $user = App::$User->identity();
320
        $model = new FormPasswordChange($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by \Ffcms\Core\App::$User->identity() on line 319 can be null; however, Apps\Model\Front\Profile...rdChange::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
321
322
        // check if form is submited and validation is passed
323
        if ($model->send() && $model->validate()) {
324
            $model->make();
325
            App::$Session->getFlashBag()->add('success', __('Password is successful changed'));
326
        }
327
328
        // set response output
329
        return App::$View->render('password', [
330
            'model' => $model->filter()
331
        ]);
332
    }
333
334
    /**
335
     * Show users in blacklist and allow add new users
336
     * @return string
337
     * @throws \Ffcms\Core\Exception\SyntaxException
338
     * @throws \Ffcms\Core\Exception\NativeException
339
     * @throws ForbiddenException
340
     */
341
    public function actionIgnore()
342
    {
343
        // check if not auth
344
        if (!App::$User->isAuth()) {
345
            throw new ForbiddenException();
346
        }
347
348
        // get user object and init model of it
349
        $user = App::$User->identity();
350
        $model = new FormIgnoreAdd($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by \Ffcms\Core\App::$User->identity() on line 349 can be null; however, Apps\Model\Front\Profile...gnoreAdd::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
351
352
        // set user id from ?id= get param if form not sended
353
        if (!$model->send()) {
354
            $uid = (int)App::$Request->query->get('id');
355
            if ($uid > 0) {
356
                $model->id = $uid;
357
            }
358
        }
359
360
        // sended new block add?
361
        if ($model->send() && $model->validate()) {
362
            if ($model->save()) {
363
                App::$Session->getFlashBag()->add('success', __('User was successful blocked!'));
364
            } else {
365
                App::$Session->getFlashBag()->add('error', __('This user is always in ignore list'));
366
            }
367
        }
368
369
        // get blocked users
370
        $records = Blacklist::where('user_id', '=', $user->getId());
371
372
        $page = (int)App::$Request->query->get('page');
373
        $offset = $page * self::BLOCK_PER_PAGE;
374
375
        // build pagination
376
        $pagination = new SimplePagination([
377
            'url' => ['profile/ignore'],
378
            'page' => $page,
379
            'step' => self::BLOCK_PER_PAGE,
380
            'total' => $records->count()
381
        ]);
382
383
        return App::$View->render('ignore', [
384
            'records' => $records->skip($offset)->take(self::BLOCK_PER_PAGE)->get(),
385
            'model' => $model->filter(),
386
            'pagination' => $pagination
387
        ]);
388
    }
389
390
    /**
391
     * Show user logs
392
     * @return string
393
     * @throws \Ffcms\Core\Exception\SyntaxException
394
     * @throws \Ffcms\Core\Exception\NativeException
395
     * @throws ForbiddenException
396
     */
397
    public function actionLog()
398
    {
399
        // check if user is authorized
400
        if (!App::$User->isAuth()) {
401
            throw new ForbiddenException();
402
        }
403
404
        // get user object
405
        $user = App::$User->identity();
406
407
        // get log records
408
        $records = $user->getLogs();
409
        if ($records !== null && $records->count() > 0) {
410
            $records = $records->orderBy('id', 'DESC');
411
        }
412
413
        return App::$View->render('log', [
414
            'records' => $records
415
        ]);
416
    }
417
418
    /**
419
     * Unblock always blocked user
420
     * @param string $target_id
421
     * @return string
422
     * @throws \Ffcms\Core\Exception\SyntaxException
423
     * @throws \Ffcms\Core\Exception\NativeException
424
     * @throws ForbiddenException
425
     * @throws NotFoundException
426
     */
427
    public function actionUnblock($target_id)
428
    {
429
        // check if user is auth
430
        if (!App::$User->isAuth()) {
431
            throw new ForbiddenException();
432
        }
433
434
        // check if target is defined
435 View Code Duplication
        if (!Obj::isLikeInt($target_id) || $target_id < 1 || !App::$User->isExist($target_id)) {
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...
436
            throw new NotFoundException();
437
        }
438
439
        $user = App::$User->identity();
440
441
        // check if target user in blacklist of current user
442
        if (!Blacklist::have($user->getId(), $target_id)) {
443
            throw new NotFoundException();
444
        }
445
446
        $model = new FormIgnoreDelete($user, $target_id);
0 ignored issues
show
Bug introduced by
It seems like $user defined by \Ffcms\Core\App::$User->identity() on line 439 can be null; however, Apps\Model\Front\Profile...reDelete::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
447
        if ($model->send() && $model->validate()) {
448
            $model->make();
449
            App::$Response->redirect(Url::to('profile/ignore'));
450
        }
451
452
        return App::$View->render('unblock', [
453
            'model' => $model->filter()
454
        ]);
455
    }
456
457
    /**
458
     * Search users
459
     * @return string
460
     * @throws \Ffcms\Core\Exception\SyntaxException
461
     * @throws \Ffcms\Core\Exception\NativeException
462
     */
463
    public function actionSearch()
464
    {
465
        // create model object
466
        $model = new FormUserSearch();
467
        $model->setSubmitMethod('GET');
468
469
        // get app configs
470
        $cfgs = Serialize::decode($this->application->configs);
471
472
        $records = null;
473
        $pagination = null;
474
        // check if request is sended
475
        if ($model->send() && $model->validate()) {
476
            // get records from db
477
            $records = ProfileRecords::where('nick', 'like', '%' . $model->query . '%');
478
            $page = (int)App::$Request->query->get('page');
479
            $userPerPage = (int)$cfgs['usersOnPage'];
480
            if ($userPerPage < 1) {
481
                $userPerPage = 1;
482
            }
483
            $offset = $page * $userPerPage;
484
            // build pagination
485
            $pagination = new SimplePagination([
486
                'url' => ['profile/search', null, null, [$model->getFormName().'[query]' => $model->query, $model->getFormName().'[submit]' => true]],
487
                'page' => $page,
488
                'step' => $userPerPage,
489
                'total' => $records->count()
490
            ]);
491
            // make query finally
492
            $records = $records->skip($offset)->take($userPerPage)->get();
493
494
        }
495
496
        // display response
497
        return App::$View->render('search', [
498
            'model' => $model->filter(),
499
            'records' => $records,
500
            'pagination' => $pagination,
501
            'ratingOn' => (int)$cfgs['rating']
502
        ]);
503
    }
504
505
    /**
506
     * Cron schedule - build user profiles sitemap
507
     */
508
    public static function buildSitemapSchedule()
509
    {
510
        // get not empty user profiles
511
        $profiles = ProfileRecords::whereNotNull('nick');
512
        if ($profiles->count() < 1) {
513
            return;
514
        }
515
516
        // get languages if multilanguage enabled
517
        $langs = null;
518
        if (App::$Properties->get('multiLanguage')) {
519
            $langs = App::$Properties->get('languages');
520
        }
521
522
        // build sitemap from content items via business model
523
        $sitemap = new EntityBuildMap($langs);
524
        foreach ($profiles->get() as $user) {
525
            $sitemap->add('profile/show/' . $user->user_id, $user->updated_at, 'weekly', 0.2);
526
        }
527
528
        $sitemap->save('profile');
529
    }
530
}