1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/roboapp |
4
|
|
|
* @copyright Copyright (c) 2016 Roboapp |
5
|
|
|
* @license [MIT License](https://opensource.org/licenses/MIT) |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace roboapp\crud; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\base\InvalidConfigException; |
12
|
|
|
use yii\base\Model; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Action displays the models list with search support. |
16
|
|
|
* |
17
|
|
|
* @author iRipVanWinkle <[email protected]> |
18
|
|
|
* @since 1.0 |
19
|
|
|
*/ |
20
|
|
|
class Index extends BaseAction |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string | callable a PHP callable that will be called to create the search model. |
24
|
|
|
* The callable should return the new model instance. |
25
|
|
|
*/ |
26
|
|
|
public $searchModel; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string name of the view |
30
|
|
|
*/ |
31
|
|
|
public $view = 'index'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string the name variable model in the view template |
35
|
|
|
*/ |
36
|
|
|
public $nameVariableModel = 'model'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string the name variable data provider in the view template |
40
|
|
|
*/ |
41
|
|
|
public $nameVariableDataProvider = 'dataProvider'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string the method name for return DataProvider |
45
|
|
|
*/ |
46
|
|
|
public $searchMethodName = 'search'; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Creates new search model instance. |
50
|
|
|
* @return Model new model instance. |
51
|
|
|
* @throws InvalidConfigException on invalid configuration. |
52
|
|
|
*/ |
53
|
12 |
|
protected function newSearchModel() |
54
|
|
|
{ |
55
|
12 |
|
return \Yii::createObject($this->searchModel); |
56
|
3 |
|
} |
57
|
9 |
|
|
58
|
6 |
|
/** |
59
|
|
|
* @param Model $model new model instance. |
60
|
|
|
* @return mixed |
61
|
3 |
|
* @throws InvalidConfigException |
62
|
3 |
|
*/ |
63
|
|
|
protected function dataProvider($model) |
64
|
|
|
{ |
65
|
|
|
if (method_exists($model, $this->searchMethodName)) { |
66
|
|
|
return $model->{$this->searchMethodName}(Yii::$app->getRequest()->getQueryParams()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
throw new InvalidConfigException('"' . get_class($this) . '::searchModel" must be set existing method.'); |
70
|
9 |
|
} |
71
|
|
|
|
72
|
9 |
|
/** |
73
|
6 |
|
* Displays models list. |
74
|
|
|
* @return mixed response. |
75
|
|
|
*/ |
76
|
3 |
|
public function run() |
77
|
|
|
{ |
78
|
|
|
$searchModel = $this->newSearchModel(); |
79
|
|
|
$dataProvider = $this->dataProvider($searchModel); |
80
|
|
|
|
81
|
|
|
return $this->controller->render($this->view, [ |
82
|
|
|
$this->nameVariableModel => $searchModel, |
83
|
12 |
|
$this->nameVariableDataProvider => $dataProvider, |
84
|
|
|
]); |
85
|
12 |
|
} |
86
|
|
|
} |
87
|
|
|
|