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

ActionSearch::search()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 26
nc 3
nop 0
dl 0
loc 43
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace Apps\Controller\Front\Profile;
4
5
use Apps\Model\Front\Profile\FormUserSearch;
6
use Extend\Core\Arch\FrontAppController;
7
use Ffcms\Core\App;
8
use Ffcms\Core\Arch\View;
9
use Apps\ActiveRecord\Profile as ProfileRecords;
10
use Ffcms\Core\Helper\HTML\SimplePagination;
11
use Ffcms\Core\Network\Response;
12
use Ffcms\Core\Network\Request;
13
14
15
/**
16
 * Trait ActionSearch
17
 * @package Apps\Controller\Front\Profile
18
 * @property View $view
19
 * @property Request $request
20
 * @property Response $response
21
 * @method array getConfigs()
22
 */
23
trait ActionSearch
24
{
25
    /**
26
     * Search users
27
     * @return string
28
     * @throws \Ffcms\Core\Exception\SyntaxException
29
     */
30
    public function search()
31
    {
32
        // create model object
33
        $model = new FormUserSearch();
34
        $model->setSubmitMethod('GET');
35
36
        // get app configs
37
        $cfgs = $this->getConfigs();
38
39
        $records = null;
40
        $pagination = null;
41
        // check if request is sended
42
        if ($model->send() && $model->validate()) {
43
            // get records from db
44
            $records = ProfileRecords::where('nick', 'like', '%' . $model->query . '%');
45
            $page = (int)$this->request->query->get('page');
46
            $userPerPage = (int)$cfgs['usersOnPage'];
47
            if ($userPerPage < 1) {
48
                $userPerPage = 1;
49
            }
50
51
            $offset = $page * $userPerPage;
52
            // build pagination
53
            $pagination = new SimplePagination([
54
                'url' => ['profile/search', null, null, [$model->getFormName().'[query]' => $model->query, $model->getFormName().'[submit]' => true]],
55
                'page' => $page,
56
                'step' => $userPerPage,
57
                'total' => $records->count()
58
            ]);
59
            // make query finally
60
            $records = $records->skip($offset)
61
                ->take($userPerPage)
62
                ->get();
63
        }
64
65
        // display response
66
        return $this->view->render('search', [
67
            'model' => $model,
68
            'records' => $records,
69
            'pagination' => $pagination,
70
            'ratingOn' => (int)$cfgs['rating']
71
        ]);
72
    }
73
}