/
IndexAction.php
94 lines (78 loc) · 2.64 KB
/
IndexAction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/**
* Обобщённый экшн для листинга сущностей.
* Делает всё то же, как если бы этот экшн был сгерерирован в gii. Возвращает просто модель, во view должно быть вызван метод search().
*
* @category Actions
* @package yupe.components.actions
* @since 0.9.4
*/
namespace yupe\components\actions;
use CAction;
use yupe\models\YModel;
use Yii;
use CEvent;
use CMap;
class IndexAction extends CAction
{
/**
* @var string Model class for action.
*/
public $modelClass;
/**
* @var string model scenario
*/
public $modelScenario = 'search';
/**
* @var string|callable the name of the view to be rendered.
*/
public $view = 'index';
/**
* @var string|callable the name of the layout to be applied to the views.
* This will be assigned to {@link CController::layout} before the view is rendered.
* Defaults to null, meaning the controller's layout will be used.
* If false, no layout will be applied.
*/
public $layout;
public function run()
{
$modelClass = $this->modelClass;
/* @var $model YModel */
$model = new $modelClass($this->modelScenario);
$model->unsetAttributes();
if (($data = Yii::app()->request->getParam(get_class($model))) !== null) {
$model->setAttributes($data);
}
$controller = $this->getController();
if ($this->layout !== null) {
$layout = $controller->layout;
$controller->layout = is_callable($this->layout) ? call_user_func($this->layout, $model) : $this->layout;
}
$this->onBeforeRender($event = new CEvent($this));
if (!$event->handled) {
$controller->render(is_callable($this->view) ? call_user_func($this->view, $model) : $this->view, ['model' => $model]);
$this->onAfterRender(new CEvent($this));
}
if ($this->layout !== null) {
$controller->layout = $layout;
}
}
/**
* Raised right before the action invokes the render method.
* Event handlers can set the {@link CEvent::handled} property
* to be true to stop further view rendering.
* @param CEvent $event event parameter
*/
public function onBeforeRender($event)
{
$this->raiseEvent('onBeforeRender', $event);
}
/**
* Raised right after the action invokes the render method.
* @param CEvent $event event parameter
*/
public function onAfterRender($event)
{
$this->raiseEvent('onAfterRender', $event);
}
}