|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace carono\yii2crud\actions; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use yii\helpers\ArrayHelper; |
|
8
|
|
|
use yii\helpers\StringHelper; |
|
9
|
|
|
|
|
10
|
|
|
abstract class Action extends \yii\base\Action |
|
11
|
|
|
{ |
|
12
|
|
|
public $primaryKeyParam; |
|
13
|
|
|
public $layout; |
|
14
|
|
|
public $view; |
|
15
|
|
|
public $modelClass; |
|
16
|
|
|
public $renderParams; |
|
17
|
|
|
protected $params = []; |
|
18
|
|
|
|
|
19
|
|
|
public function init() |
|
20
|
|
|
{ |
|
21
|
|
|
if ($this->layout) { |
|
22
|
|
|
$this->controller->layout = $this->layout; |
|
23
|
|
|
} |
|
24
|
|
|
parent::init(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function __call($name, $params) |
|
28
|
|
|
{ |
|
29
|
|
|
if (StringHelper::startsWith($name, 'getMessage')) { |
|
30
|
|
|
$property = $this->{lcfirst(substr($name, 3))}; |
|
31
|
|
|
if ($property instanceof \Closure) { |
|
32
|
|
|
return call_user_func_array($property, $params); |
|
33
|
|
|
} |
|
34
|
|
|
return $property; |
|
35
|
|
|
} |
|
36
|
|
|
return parent::__call($name, $params); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function render($view, $params = []) |
|
40
|
|
|
{ |
|
41
|
|
|
if ($this->renderParams instanceof \Closure) { |
|
42
|
|
|
$params = array_merge($params, call_user_func($this->renderParams, $params, $this)); |
|
43
|
|
|
} |
|
44
|
|
|
if (is_array($this->renderParams)) { |
|
45
|
|
|
$params = array_merge($params, $this->renderParams); |
|
46
|
|
|
} |
|
47
|
|
|
return $this->controller->render($view, $params); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function runWithParams($params) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->params = $params; |
|
53
|
|
|
return parent::runWithParams($params); // TODO: Change the autogenerated stub |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function getPrimaryKeys() |
|
57
|
|
|
{ |
|
58
|
|
|
$ids = []; |
|
59
|
|
|
foreach ((array)$this->primaryKeyParam as $param => $field) { |
|
60
|
|
|
if (is_integer($param)) { |
|
61
|
|
|
$ids[$field] = ArrayHelper::getValue($this->params, $field); |
|
62
|
|
|
} else { |
|
63
|
|
|
$ids[$field] = ArrayHelper::getValue($this->params, $param); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
return $ids; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function findModel($class) |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->controller->findModel($this->getPrimaryKeys(), $class); |
|
72
|
|
|
} |
|
73
|
|
|
} |