-
Notifications
You must be signed in to change notification settings - Fork 0
/
Action.php
84 lines (72 loc) · 2.22 KB
/
Action.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
<?php
namespace rocketfirm\engine;
use rocketfirm\engine\ActiveRecord;
use Yii;
use yii\helpers\ArrayHelper;
/**
* @property string $view
* @property ActiveRecord $model
*
* @property Controller $controller
*/
abstract class Action extends \yii\base\Action
{
const EVENT_BEFORE_SAVE='beforeSave';
const EVENT_AFTER_SAVE='afterSave';
protected $templatePath = '/templates';
public $viewTemplate = false;
public $viewTitle = null;
public $formPath = null;
public $containerClass = null;
public $renderData=[];
protected $_view;
public function getView()
{
if ($this->_view === null)
$this->_view = $this->id;
return $this->_view;
}
public function setView($view)
{
$this->_view = $view;
}
public function redirect($actionId = null, $params = array())
{
$this->controller->redirectBack($actionId, $params);
}
public function render($data)
{
$data['_renderData']=$this->getRenderData();
if ($this->_view === null) {
if ($this->viewTemplate) {
if ($this->viewTemplate === true)
$this->viewTemplate = $this->id;
$this->_view = $this->templatePath . '/' . $this->viewTemplate;
if (is_callable($this->viewTitle))
{
$method=$this->viewTitle;
$this->viewTitle=$method($data);
}
$data = ArrayHelper::merge($data, [
'formPath' => $this->formPath ? : '/' . $this->controller->id . '/_form',
'title' => $this->viewTitle,
'containerClass' => $this->containerClass
]);
} else {
$this->_view = $this->id;
}
}
if (Yii::$app->request->isAjax)
$content = $this->controller->renderPartial($this->_view, $data);
else
$content = $this->controller->render($this->_view, $data);
return $content;
}
public function getRenderData()
{
if (!is_callable($this->renderData))
return $this->renderData;
$method=$this->renderData;
return $method($this);
}
}