IndexAction::getSearchModel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
4
namespace carono\yii2crud\actions;
5
6
7
use carono\yii2crud\CrudController;
8
use Closure;
9
use yii\db\ActiveRecord;
10
11
/**
12
 * Class IndexAction
13
 *
14
 * @package carono\yii2crud\actions
15
 * @property CrudController $controller
16
 */
17
class IndexAction extends Action
18
{
19
    public $view = 'index';
20
    public $modelSearchClass;
21
    public $query;
22
    public $dataProvider;
23
    public $condition;
24
    public $params;
25
    protected $searchModel;
26
27
    public function getSearchModel()
28
    {
29
        return $this->searchModel;
30
    }
31
32
    public function run()
33
    {
34
        /**
35
         * @var ActiveRecord $searchModel
36
         */
37
        $this->searchModel = $this->modelSearchClass ? new $this->modelSearchClass : null;
38
        $query = is_callable($this->query) ? call_user_func($this->query, $this->modelClass, $this) : $this->query;
39
40
        if (is_callable($this->condition)) {
41
            call_user_func($this->condition, $query, $this);
42
        } elseif ($this->condition) {
43
            $query->andWhere($this->condition);
44
        }
45
46
        $dataProvider = is_callable($this->dataProvider) ? call_user_func($this->dataProvider, $query, $this) : $this->dataProvider;
47
48
        return $this->render($this->view, ['searchModel' => $this->searchModel, 'dataProvider' => $dataProvider]);
49
    }
50
}